c89.c revision 87258
176378Sschweikh/*-
276378Sschweikh * This is the Posix.2 mandated C compiler.  Basically, a hook to the
376378Sschweikh * cc(1) command.
476378Sschweikh *
576378Sschweikh * Copyright (c) 2001 by Jens Schweikhardt
676378Sschweikh *
776378Sschweikh * Redistribution and use in source and binary forms, with or without
876378Sschweikh * modification, are permitted provided that the following conditions
976378Sschweikh * are met:
1076378Sschweikh * 1. Redistributions of source code must retain the above copyright
1176378Sschweikh *    notice, this list of conditions and the following disclaimer.
1276378Sschweikh * 2. Redistributions in binary form must reproduce the above copyright
1376378Sschweikh *    notice, this list of conditions and the following disclaimer in the
1476378Sschweikh *    documentation and/or other materials provided with the distribution.
1576378Sschweikh *
1676378Sschweikh * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND
1776378Sschweikh * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1876378Sschweikh * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1976378Sschweikh * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
2076378Sschweikh * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2176378Sschweikh * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2276378Sschweikh * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2376378Sschweikh * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2476378Sschweikh * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2576378Sschweikh * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2676378Sschweikh * SUCH DAMAGE.
2776378Sschweikh *
2876378Sschweikh */
2976378Sschweikh
3087258Smarkm#include <sys/cdefs.h>
3187258Smarkm
3287258Smarkm__FBSDID("$FreeBSD: head/usr.bin/c89/c89.c 87258 2001-12-03 01:15:28Z markm $");
3387258Smarkm
3476378Sschweikh#include <err.h>
3576378Sschweikh#include <stdio.h>
3676378Sschweikh#include <stdlib.h>
3776378Sschweikh#include <string.h>
3876378Sschweikh
3976378Sschweikh#include <unistd.h>
4076378Sschweikh
4176378Sschweikh#define	CC "/usr/bin/cc"	/* The big kahuna doing the actual work. */
4276378Sschweikh#define	N_ARGS_PREPENDED (sizeof(args_prepended) / sizeof(args_prepended[0]))
4376378Sschweikh
4476378Sschweikh/*
4576378Sschweikh * We do not add -D_POSIX_SOURCE here because any POSIX source is supposed to
4676378Sschweikh * define it before inclusion of POSIX headers. This has the additional
4776378Sschweikh * benefit of making c89 -D_ANSI_SOURCE do the right thing (or any other
4876378Sschweikh * -D_FOO_SOURCE feature test macro we support.)
4976378Sschweikh */
5087210Smarkmstatic const char	*args_prepended[] = {
5176378Sschweikh	"-std=iso9899:199409",
5276378Sschweikh	"-pedantic"
5376378Sschweikh};
5476378Sschweikh
5576378Sschweikhstatic void	usage(void);
5676378Sschweikh
5776378Sschweikh/*
5876378Sschweikh * Prepend the strings from args_prepended[] to the arg list; parse options,
5976378Sschweikh * accepting only the POSIX c89 mandated options. Then exec cc to do the
6076378Sschweikh * actual work.
6176378Sschweikh */
6276378Sschweikhint
6376378Sschweikhmain(int argc, char **argv)
6476378Sschweikh{
6576378Sschweikh	int Argc, i;
6687210Smarkm	size_t j;
6787210Smarkm	union {
6887210Smarkm		const char **a;
6987210Smarkm		char * const *b;
7087210Smarkm	} Argv;
7176378Sschweikh
7276378Sschweikh	Argc = 0;
7387210Smarkm	Argv.a = malloc((argc + 1 + N_ARGS_PREPENDED) * sizeof *Argv.a);
7487210Smarkm	if (Argv.a == NULL)
7576378Sschweikh		err(1, "malloc");
7687210Smarkm	Argv.a[Argc++] = argv[0];
7787210Smarkm	for (j = 0; j < N_ARGS_PREPENDED; ++j)
7887210Smarkm		Argv.a[Argc++] = args_prepended[j];
7976378Sschweikh	while ((i = getopt(argc, argv, "cD:EgI:l:L:o:OsU:")) != -1) {
8076378Sschweikh		if (i == '?')
8176378Sschweikh			usage();
8276378Sschweikh		if (i == 'l') {
8376378Sschweikh			if (argv[optind - 1][0] == '-') /* -llib */
8476378Sschweikh				optind -= 1;
8576378Sschweikh			else                            /* -l lib */
8676378Sschweikh				optind -= 2;
8776378Sschweikh			break; /* -llib or -l lib starts the operands. */
8876378Sschweikh		}
8976378Sschweikh	}
9076378Sschweikh	if (argc == optind) {
9176378Sschweikh		warnx("missing operand");
9276378Sschweikh		usage();
9376378Sschweikh	}
9476378Sschweikh
9587210Smarkm	/* Append argv[1..] at the end of Argv[].a. */
9676378Sschweikh	for (i = 1; i <= argc; ++i)
9787210Smarkm		Argv.a[Argc++] = argv[i];
9887210Smarkm	(void)execv(CC, Argv.b);
9976378Sschweikh	err(1, "execv(" CC ")");
10076378Sschweikh}
10176378Sschweikh
10276378Sschweikhstatic void
10376378Sschweikhusage(void)
10476378Sschweikh{
10576378Sschweikh	fprintf(stderr,
10676378Sschweikh"usage: c89 [-c] [-D name[=value]] [...] [-E] [-g] [-I directory ...]\n"
10776378Sschweikh"       [-L directory ...] [-o outfile] [-O] [-s] [-U name ...] operand ...\n"
10876378Sschweikh"\n"
10976378Sschweikh"       where operand is one or more of file.c, file.o, file.a\n"
11076378Sschweikh"       or -llibrary\n");
11176378Sschweikh	exit(1);
11276378Sschweikh}
113