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__FBSDID("$FreeBSD$");
3287258Smarkm
3376378Sschweikh#include <err.h>
3476378Sschweikh#include <stdio.h>
3576378Sschweikh#include <stdlib.h>
36200462Sdelphij#include <string.h>
3776378Sschweikh
3876378Sschweikh#include <unistd.h>
3976378Sschweikh
4076378Sschweikh#define	CC "/usr/bin/cc"	/* The big kahuna doing the actual work. */
4176378Sschweikh#define	N_ARGS_PREPENDED (sizeof(args_prepended) / sizeof(args_prepended[0]))
4276378Sschweikh
4376378Sschweikh/*
4476378Sschweikh * We do not add -D_POSIX_SOURCE here because any POSIX source is supposed to
4576378Sschweikh * define it before inclusion of POSIX headers. This has the additional
4676378Sschweikh * benefit of making c89 -D_ANSI_SOURCE do the right thing (or any other
4776378Sschweikh * -D_FOO_SOURCE feature test macro we support.)
4876378Sschweikh */
4987210Smarkmstatic const char	*args_prepended[] = {
5076378Sschweikh	"-std=iso9899:199409",
5176378Sschweikh	"-pedantic"
5276378Sschweikh};
5376378Sschweikh
5476378Sschweikhstatic void	usage(void);
5576378Sschweikh
5676378Sschweikh/*
5776378Sschweikh * Prepend the strings from args_prepended[] to the arg list; parse options,
5876378Sschweikh * accepting only the POSIX c89 mandated options. Then exec cc to do the
5976378Sschweikh * actual work.
6076378Sschweikh */
6176378Sschweikhint
6276378Sschweikhmain(int argc, char **argv)
6376378Sschweikh{
6476378Sschweikh	int Argc, i;
6587210Smarkm	size_t j;
6687210Smarkm	union {
6787210Smarkm		const char **a;
6887210Smarkm		char * const *b;
6987210Smarkm	} Argv;
7076378Sschweikh
7176378Sschweikh	Argc = 0;
7287210Smarkm	Argv.a = malloc((argc + 1 + N_ARGS_PREPENDED) * sizeof *Argv.a);
7387210Smarkm	if (Argv.a == NULL)
7476378Sschweikh		err(1, "malloc");
75248131Sdim	Argv.a[Argc++] = CC;
7687210Smarkm	for (j = 0; j < N_ARGS_PREPENDED; ++j)
7787210Smarkm		Argv.a[Argc++] = args_prepended[j];
7876378Sschweikh	while ((i = getopt(argc, argv, "cD:EgI:l:L:o:OsU:")) != -1) {
7976378Sschweikh		if (i == '?')
8076378Sschweikh			usage();
8176378Sschweikh		if (i == 'l') {
8276378Sschweikh			if (argv[optind - 1][0] == '-') /* -llib */
8376378Sschweikh				optind -= 1;
8476378Sschweikh			else                            /* -l lib */
8576378Sschweikh				optind -= 2;
8676378Sschweikh			break; /* -llib or -l lib starts the operands. */
8776378Sschweikh		}
8876378Sschweikh	}
8976378Sschweikh	if (argc == optind) {
9076378Sschweikh		warnx("missing operand");
9176378Sschweikh		usage();
9276378Sschweikh	}
9376378Sschweikh
9487210Smarkm	/* Append argv[1..] at the end of Argv[].a. */
9576378Sschweikh	for (i = 1; i <= argc; ++i)
9687210Smarkm		Argv.a[Argc++] = argv[i];
9787210Smarkm	(void)execv(CC, Argv.b);
9876378Sschweikh	err(1, "execv(" CC ")");
9976378Sschweikh}
10076378Sschweikh
10176378Sschweikhstatic void
10276378Sschweikhusage(void)
10376378Sschweikh{
10476378Sschweikh	fprintf(stderr,
105146466Sru"usage: c89 [-cEgOs] [-D name[=value]] ... [-I directory] ... [-L directory] ...\n"
106146466Sru"           [-o outfile] [-U name] ... operand ...\n"
10776378Sschweikh"\n"
10876378Sschweikh"       where operand is one or more of file.c, file.o, file.a\n"
10976378Sschweikh"       or -llibrary\n");
11076378Sschweikh	exit(1);
11176378Sschweikh}
112