c89.c revision 87258
1/*-
2 * This is the Posix.2 mandated C compiler.  Basically, a hook to the
3 * cc(1) command.
4 *
5 * Copyright (c) 2001 by Jens Schweikhardt
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 *    notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 *    notice, this list of conditions and the following disclaimer in the
14 *    documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 *
28 */
29
30#include <sys/cdefs.h>
31
32__FBSDID("$FreeBSD: head/usr.bin/c89/c89.c 87258 2001-12-03 01:15:28Z markm $");
33
34#include <err.h>
35#include <stdio.h>
36#include <stdlib.h>
37#include <string.h>
38
39#include <unistd.h>
40
41#define	CC "/usr/bin/cc"	/* The big kahuna doing the actual work. */
42#define	N_ARGS_PREPENDED (sizeof(args_prepended) / sizeof(args_prepended[0]))
43
44/*
45 * We do not add -D_POSIX_SOURCE here because any POSIX source is supposed to
46 * define it before inclusion of POSIX headers. This has the additional
47 * benefit of making c89 -D_ANSI_SOURCE do the right thing (or any other
48 * -D_FOO_SOURCE feature test macro we support.)
49 */
50static const char	*args_prepended[] = {
51	"-std=iso9899:199409",
52	"-pedantic"
53};
54
55static void	usage(void);
56
57/*
58 * Prepend the strings from args_prepended[] to the arg list; parse options,
59 * accepting only the POSIX c89 mandated options. Then exec cc to do the
60 * actual work.
61 */
62int
63main(int argc, char **argv)
64{
65	int Argc, i;
66	size_t j;
67	union {
68		const char **a;
69		char * const *b;
70	} Argv;
71
72	Argc = 0;
73	Argv.a = malloc((argc + 1 + N_ARGS_PREPENDED) * sizeof *Argv.a);
74	if (Argv.a == NULL)
75		err(1, "malloc");
76	Argv.a[Argc++] = argv[0];
77	for (j = 0; j < N_ARGS_PREPENDED; ++j)
78		Argv.a[Argc++] = args_prepended[j];
79	while ((i = getopt(argc, argv, "cD:EgI:l:L:o:OsU:")) != -1) {
80		if (i == '?')
81			usage();
82		if (i == 'l') {
83			if (argv[optind - 1][0] == '-') /* -llib */
84				optind -= 1;
85			else                            /* -l lib */
86				optind -= 2;
87			break; /* -llib or -l lib starts the operands. */
88		}
89	}
90	if (argc == optind) {
91		warnx("missing operand");
92		usage();
93	}
94
95	/* Append argv[1..] at the end of Argv[].a. */
96	for (i = 1; i <= argc; ++i)
97		Argv.a[Argc++] = argv[i];
98	(void)execv(CC, Argv.b);
99	err(1, "execv(" CC ")");
100}
101
102static void
103usage(void)
104{
105	fprintf(stderr,
106"usage: c89 [-c] [-D name[=value]] [...] [-E] [-g] [-I directory ...]\n"
107"       [-L directory ...] [-o outfile] [-O] [-s] [-U name ...] operand ...\n"
108"\n"
109"       where operand is one or more of file.c, file.o, file.a\n"
110"       or -llibrary\n");
111	exit(1);
112}
113