c89.c revision 200462
1153838Sdfr/*-
2153838Sdfr * This is the Posix.2 mandated C compiler.  Basically, a hook to the
3153838Sdfr * cc(1) command.
4153838Sdfr *
5153838Sdfr * Copyright (c) 2001 by Jens Schweikhardt
6153838Sdfr *
7153838Sdfr * Redistribution and use in source and binary forms, with or without
8153838Sdfr * modification, are permitted provided that the following conditions
9153838Sdfr * are met:
10153838Sdfr * 1. Redistributions of source code must retain the above copyright
11153838Sdfr *    notice, this list of conditions and the following disclaimer.
12153838Sdfr * 2. Redistributions in binary form must reproduce the above copyright
13153838Sdfr *    notice, this list of conditions and the following disclaimer in the
14153838Sdfr *    documentation and/or other materials provided with the distribution.
15153838Sdfr *
16153838Sdfr * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND
17153838Sdfr * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18153838Sdfr * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19153838Sdfr * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20153838Sdfr * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21153838Sdfr * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22153838Sdfr * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23153838Sdfr * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24153838Sdfr * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25153838Sdfr * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26153838Sdfr * SUCH DAMAGE.
27153838Sdfr *
28153838Sdfr */
29153838Sdfr
30153838Sdfr#include <sys/cdefs.h>
31153838Sdfr__FBSDID("$FreeBSD: head/usr.bin/c89/c89.c 200462 2009-12-13 03:14:06Z delphij $");
32153838Sdfr
33153838Sdfr#include <err.h>
34153838Sdfr#include <stdio.h>
35153838Sdfr#include <stdlib.h>
36153838Sdfr#include <string.h>
37153838Sdfr
38153838Sdfr#include <unistd.h>
39153838Sdfr
40153838Sdfr#define	CC "/usr/bin/cc"	/* The big kahuna doing the actual work. */
41153838Sdfr#define	N_ARGS_PREPENDED (sizeof(args_prepended) / sizeof(args_prepended[0]))
42153838Sdfr
43153838Sdfr/*
44153838Sdfr * We do not add -D_POSIX_SOURCE here because any POSIX source is supposed to
45178828Sdfr * define it before inclusion of POSIX headers. This has the additional
46178828Sdfr * benefit of making c89 -D_ANSI_SOURCE do the right thing (or any other
47178828Sdfr * -D_FOO_SOURCE feature test macro we support.)
48178828Sdfr */
49178828Sdfrstatic const char	*args_prepended[] = {
50178828Sdfr	"-std=iso9899:199409",
51153838Sdfr	"-pedantic"
52153838Sdfr};
53153838Sdfr
54static void	usage(void);
55
56/*
57 * Prepend the strings from args_prepended[] to the arg list; parse options,
58 * accepting only the POSIX c89 mandated options. Then exec cc to do the
59 * actual work.
60 */
61int
62main(int argc, char **argv)
63{
64	int Argc, i;
65	size_t j;
66	union {
67		const char **a;
68		char * const *b;
69	} Argv;
70
71	Argc = 0;
72	Argv.a = malloc((argc + 1 + N_ARGS_PREPENDED) * sizeof *Argv.a);
73	if (Argv.a == NULL)
74		err(1, "malloc");
75	Argv.a[Argc++] = argv[0];
76	for (j = 0; j < N_ARGS_PREPENDED; ++j)
77		Argv.a[Argc++] = args_prepended[j];
78	while ((i = getopt(argc, argv, "cD:EgI:l:L:o:OsU:")) != -1) {
79		if (i == '?')
80			usage();
81		if (i == 'l') {
82			if (argv[optind - 1][0] == '-') /* -llib */
83				optind -= 1;
84			else                            /* -l lib */
85				optind -= 2;
86			break; /* -llib or -l lib starts the operands. */
87		}
88	}
89	if (argc == optind) {
90		warnx("missing operand");
91		usage();
92	}
93
94	/* Append argv[1..] at the end of Argv[].a. */
95	for (i = 1; i <= argc; ++i)
96		Argv.a[Argc++] = argv[i];
97	(void)execv(CC, Argv.b);
98	err(1, "execv(" CC ")");
99}
100
101static void
102usage(void)
103{
104	fprintf(stderr,
105"usage: c89 [-cEgOs] [-D name[=value]] ... [-I directory] ... [-L directory] ...\n"
106"           [-o outfile] [-U name] ... operand ...\n"
107"\n"
108"       where operand is one or more of file.c, file.o, file.a\n"
109"       or -llibrary\n");
110	exit(1);
111}
112