c89.c revision 76378
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#ifndef lint
3076378Sschweikhstatic const char rcsid[] =
3176378Sschweikh  "$FreeBSD: head/usr.bin/c89/c89.c 76378 2001-05-08 20:27:14Z schweikh $";
3276378Sschweikh#endif /* not lint */
3376378Sschweikh
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 */
5076378Sschweikhstatic 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;
6676378Sschweikh	char **Argv;
6776378Sschweikh
6876378Sschweikh	Argc = 0;
6976378Sschweikh	Argv = malloc((argc + 1 + N_ARGS_PREPENDED) * sizeof *Argv);
7076378Sschweikh	if (Argv == NULL)
7176378Sschweikh		err(1, "malloc");
7276378Sschweikh	Argv[Argc++] = argv[0];
7376378Sschweikh	for (i = 0; i < N_ARGS_PREPENDED; ++i)
7476378Sschweikh		Argv[Argc++] = args_prepended[i];
7576378Sschweikh	while ((i = getopt(argc, argv, "cD:EgI:l:L:o:OsU:")) != -1) {
7676378Sschweikh		if (i == '?')
7776378Sschweikh			usage();
7876378Sschweikh		if (i == 'l') {
7976378Sschweikh			if (argv[optind - 1][0] == '-') /* -llib */
8076378Sschweikh				optind -= 1;
8176378Sschweikh			else                            /* -l lib */
8276378Sschweikh				optind -= 2;
8376378Sschweikh			break; /* -llib or -l lib starts the operands. */
8476378Sschweikh		}
8576378Sschweikh	}
8676378Sschweikh	if (argc == optind) {
8776378Sschweikh		warnx("missing operand");
8876378Sschweikh		usage();
8976378Sschweikh	}
9076378Sschweikh
9176378Sschweikh	/* Append argv[1..] at the end of Argv[]. */
9276378Sschweikh	for (i = 1; i <= argc; ++i)
9376378Sschweikh		Argv[Argc++] = argv[i];
9476378Sschweikh	(void)execv(CC, Argv);
9576378Sschweikh	err(1, "execv(" CC ")");
9676378Sschweikh}
9776378Sschweikh
9876378Sschweikhstatic void
9976378Sschweikhusage(void)
10076378Sschweikh{
10176378Sschweikh	fprintf(stderr,
10276378Sschweikh"usage: c89 [-c] [-D name[=value]] [...] [-E] [-g] [-I directory ...]\n"
10376378Sschweikh"       [-L directory ...] [-o outfile] [-O] [-s] [-U name ...] operand ...\n"
10476378Sschweikh"\n"
10576378Sschweikh"       where operand is one or more of file.c, file.o, file.a\n"
10676378Sschweikh"       or -llibrary\n");
10776378Sschweikh	exit(1);
10876378Sschweikh}
109