xref: /titanic_41/usr/src/tools/aw/aw.c (revision 5b2ba9d3d1e04dde804c89e9879f7dfc9bcebf61)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
524b9abbaSab196087  * Common Development and Distribution License (the "License").
624b9abbaSab196087  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
211b30f017Sgvasick 
227c478bd9Sstevel@tonic-gate /*
237fbf8d03SScott Rotondo  * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate /*
287c478bd9Sstevel@tonic-gate  * Wrapper for the GNU assembler to make it accept the Sun assembler
297c478bd9Sstevel@tonic-gate  * arguments where possible.
307c478bd9Sstevel@tonic-gate  *
317c478bd9Sstevel@tonic-gate  * There are several limitations; the Sun assembler takes multiple
327c478bd9Sstevel@tonic-gate  * source files, we only take one.
337c478bd9Sstevel@tonic-gate  *
347c478bd9Sstevel@tonic-gate  * -b, -s, -xF, -T plain not supported.
357c478bd9Sstevel@tonic-gate  * -S isn't supported either, because while GNU as does generate
367c478bd9Sstevel@tonic-gate  * listings with -a, there's no obvious mapping between sub-options.
377c478bd9Sstevel@tonic-gate  * -K pic, -K PIC not supported either, though it's not clear what
387c478bd9Sstevel@tonic-gate  * these actually do ..
397c478bd9Sstevel@tonic-gate  * -Qy (not supported) adds a string to the .comment section
407c478bd9Sstevel@tonic-gate  * describing the assembler version, while
417c478bd9Sstevel@tonic-gate  * -Qn (supported) suppresses the string (also the default).
427c478bd9Sstevel@tonic-gate  *
437c478bd9Sstevel@tonic-gate  * We also add '-#' support to see invocation lines..
447c478bd9Sstevel@tonic-gate  * We also add '-xarch=amd64' in case we need to feed the assembler
457c478bd9Sstevel@tonic-gate  * something different (or in case we need to invoke a different binary
467c478bd9Sstevel@tonic-gate  * altogether!)
477c478bd9Sstevel@tonic-gate  */
487c478bd9Sstevel@tonic-gate 
497c478bd9Sstevel@tonic-gate #include <sys/types.h>
507c478bd9Sstevel@tonic-gate #include <sys/wait.h>
517c478bd9Sstevel@tonic-gate #include <stdio.h>
527c478bd9Sstevel@tonic-gate #include <unistd.h>
537c478bd9Sstevel@tonic-gate #include <string.h>
547c478bd9Sstevel@tonic-gate #include <stdlib.h>
5524b9abbaSab196087 #include <sys/param.h>
567c478bd9Sstevel@tonic-gate 
577c478bd9Sstevel@tonic-gate static const char *progname;
587c478bd9Sstevel@tonic-gate static int verbose;
597c478bd9Sstevel@tonic-gate 
607c478bd9Sstevel@tonic-gate struct aelist {
617c478bd9Sstevel@tonic-gate 	int ael_argc;
627c478bd9Sstevel@tonic-gate 	struct ae {
637c478bd9Sstevel@tonic-gate 		struct ae *ae_next;
647c478bd9Sstevel@tonic-gate 		char *ae_arg;
657c478bd9Sstevel@tonic-gate 	} *ael_head, *ael_tail;
667c478bd9Sstevel@tonic-gate };
677c478bd9Sstevel@tonic-gate 
687c478bd9Sstevel@tonic-gate static struct aelist *
newael(void)697c478bd9Sstevel@tonic-gate newael(void)
707c478bd9Sstevel@tonic-gate {
717c478bd9Sstevel@tonic-gate 	return (calloc(sizeof (struct aelist), 1));
727c478bd9Sstevel@tonic-gate }
737c478bd9Sstevel@tonic-gate 
747c478bd9Sstevel@tonic-gate static void
newae(struct aelist * ael,const char * arg)757c478bd9Sstevel@tonic-gate newae(struct aelist *ael, const char *arg)
767c478bd9Sstevel@tonic-gate {
777c478bd9Sstevel@tonic-gate 	struct ae *ae;
787c478bd9Sstevel@tonic-gate 
797c478bd9Sstevel@tonic-gate 	ae = calloc(sizeof (*ae), 1);
807c478bd9Sstevel@tonic-gate 	ae->ae_arg = strdup(arg);
817c478bd9Sstevel@tonic-gate 	if (ael->ael_tail == NULL)
827c478bd9Sstevel@tonic-gate 		ael->ael_head = ae;
837c478bd9Sstevel@tonic-gate 	else
847c478bd9Sstevel@tonic-gate 		ael->ael_tail->ae_next = ae;
857c478bd9Sstevel@tonic-gate 	ael->ael_tail = ae;
867c478bd9Sstevel@tonic-gate 	ael->ael_argc++;
877c478bd9Sstevel@tonic-gate }
887c478bd9Sstevel@tonic-gate 
897c478bd9Sstevel@tonic-gate static void
fixae_arg(struct ae * ae,const char * newarg)907c478bd9Sstevel@tonic-gate fixae_arg(struct ae *ae, const char *newarg)
917c478bd9Sstevel@tonic-gate {
927c478bd9Sstevel@tonic-gate 	free(ae->ae_arg);
937c478bd9Sstevel@tonic-gate 	ae->ae_arg = strdup(newarg);
947c478bd9Sstevel@tonic-gate }
957c478bd9Sstevel@tonic-gate 
967c478bd9Sstevel@tonic-gate static char **
aeltoargv(struct aelist * ael)977c478bd9Sstevel@tonic-gate aeltoargv(struct aelist *ael)
987c478bd9Sstevel@tonic-gate {
997c478bd9Sstevel@tonic-gate 	struct ae *ae;
1007c478bd9Sstevel@tonic-gate 	char **argv;
1017c478bd9Sstevel@tonic-gate 	int argc;
1027c478bd9Sstevel@tonic-gate 
1037c478bd9Sstevel@tonic-gate 	argv = calloc(sizeof (*argv), ael->ael_argc + 1);
1047c478bd9Sstevel@tonic-gate 
1057c478bd9Sstevel@tonic-gate 	for (argc = 0, ae = ael->ael_head; ae; ae = ae->ae_next, argc++) {
1067c478bd9Sstevel@tonic-gate 		argv[argc] = ae->ae_arg;
1077c478bd9Sstevel@tonic-gate 		if (ae == ael->ael_tail)
1087c478bd9Sstevel@tonic-gate 			break;
1097c478bd9Sstevel@tonic-gate 	}
1107c478bd9Sstevel@tonic-gate 
1117c478bd9Sstevel@tonic-gate 	return (argv);
1127c478bd9Sstevel@tonic-gate }
1137c478bd9Sstevel@tonic-gate 
1147c478bd9Sstevel@tonic-gate static int
error(const char * arg)1157c478bd9Sstevel@tonic-gate error(const char *arg)
1167c478bd9Sstevel@tonic-gate {
1177c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr,
1187c478bd9Sstevel@tonic-gate 	    "%s: as->gas mapping failed at or near arg '%s'\n", progname, arg);
1197c478bd9Sstevel@tonic-gate 	return (2);
1207c478bd9Sstevel@tonic-gate }
1217c478bd9Sstevel@tonic-gate 
1227c478bd9Sstevel@tonic-gate static int
usage(const char * arg)1237c478bd9Sstevel@tonic-gate usage(const char *arg)
1247c478bd9Sstevel@tonic-gate {
1257c478bd9Sstevel@tonic-gate 	if (arg != NULL)
1267c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "error: %s\n", arg);
1277c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "Usage: %s [-V] [-#]\n"
1287c478bd9Sstevel@tonic-gate 	    "\t[-xarch=architecture]\n"
1297c478bd9Sstevel@tonic-gate 	    "\t[-o objfile] [-L]\n"
1307c478bd9Sstevel@tonic-gate 	    "\t[-P [[-Ipath] [-Dname] [-Dname=def] [-Uname]]...]\n"
1317c478bd9Sstevel@tonic-gate 	    "\t[-m] [-n] file.s ...\n", progname);
1327c478bd9Sstevel@tonic-gate 	return (3);
1337c478bd9Sstevel@tonic-gate }
1347c478bd9Sstevel@tonic-gate 
1357c478bd9Sstevel@tonic-gate static void
copyuntil(FILE * in,FILE * out,int termchar)1367c478bd9Sstevel@tonic-gate copyuntil(FILE *in, FILE *out, int termchar)
1377c478bd9Sstevel@tonic-gate {
1387c478bd9Sstevel@tonic-gate 	int c;
1397c478bd9Sstevel@tonic-gate 
1407c478bd9Sstevel@tonic-gate 	while ((c = fgetc(in)) != EOF) {
1417c478bd9Sstevel@tonic-gate 		if (out && fputc(c, out) == EOF)
1427c478bd9Sstevel@tonic-gate 			exit(1);
1437c478bd9Sstevel@tonic-gate 		if (c == termchar)
1447c478bd9Sstevel@tonic-gate 			break;
1457c478bd9Sstevel@tonic-gate 	}
1467c478bd9Sstevel@tonic-gate }
1477c478bd9Sstevel@tonic-gate 
1487c478bd9Sstevel@tonic-gate /*
14924b9abbaSab196087  * Variant of copyuntil(), used for copying the path used
15024b9abbaSab196087  * for .file directives. This version removes the workspace
15124b9abbaSab196087  * from the head of the path, or failing that, attempts to remove
15224b9abbaSab196087  * /usr/include. This is a workaround for the way gas handles
15324b9abbaSab196087  * these directives. The objects produced by gas contain STT_FILE
15424b9abbaSab196087  * symbols for every .file directive. These FILE symbols contain our
15524b9abbaSab196087  * workspace paths, leading to wsdiff incorrectly flagging them as
15624b9abbaSab196087  * having changed. By clipping off the workspace from these paths,
15724b9abbaSab196087  * we eliminate these false positives.
15824b9abbaSab196087  */
15924b9abbaSab196087 static void
copyuntil_path(FILE * in,FILE * out,int termchar,const char * wspace,size_t wspace_len)16024b9abbaSab196087 copyuntil_path(FILE *in, FILE *out, int termchar,
16124b9abbaSab196087     const char *wspace, size_t wspace_len)
16224b9abbaSab196087 {
16324b9abbaSab196087 #define	PROTO_INC "/proto/root_i386/usr/include/"
16424b9abbaSab196087 #define	SYS_INC "/usr/include/"
16524b9abbaSab196087 
1663cf6f95fSab196087 	static const size_t proto_inc_len = sizeof (PROTO_INC) - 1;
1673cf6f95fSab196087 	static const size_t sys_inc_len = sizeof (SYS_INC) - 1;
16824b9abbaSab196087 
16924b9abbaSab196087 	/*
17024b9abbaSab196087 	 * Dynamically sized buffer for reading paths. Retained
17124b9abbaSab196087 	 * and reused between calls.
17224b9abbaSab196087 	 */
17324b9abbaSab196087 	static char	*buf = NULL;
1743cf6f95fSab196087 	static size_t	bufsize = 0;
17524b9abbaSab196087 
1763cf6f95fSab196087 	size_t	bufcnt = 0;
17724b9abbaSab196087 	char	*bufptr;
17824b9abbaSab196087 	int	c;
17924b9abbaSab196087 
18024b9abbaSab196087 	/* Read the path into the buffer */
18124b9abbaSab196087 	while ((c = fgetc(in)) != EOF) {
18224b9abbaSab196087 		/*
18324b9abbaSab196087 		 * If we need a buffer, or need a larger buffer,
18424b9abbaSab196087 		 * fix that here.
18524b9abbaSab196087 		 */
18624b9abbaSab196087 		if (bufcnt >= bufsize) {
1873cf6f95fSab196087 			bufsize = (bufsize == 0) ? MAXPATHLEN : (bufsize * 2);
18824b9abbaSab196087 			buf = realloc(buf, bufsize + 1); /* + room for NULL */
18924b9abbaSab196087 			if (buf == NULL) {
19024b9abbaSab196087 				perror("realloc");
19124b9abbaSab196087 				exit(1);
19224b9abbaSab196087 			}
19324b9abbaSab196087 		}
19424b9abbaSab196087 
19524b9abbaSab196087 		buf[bufcnt++] = c;
19624b9abbaSab196087 		if (c == termchar)
19724b9abbaSab196087 			break;
19824b9abbaSab196087 	}
19924b9abbaSab196087 	if (bufcnt == 0)
20024b9abbaSab196087 		return;
20124b9abbaSab196087 
20224b9abbaSab196087 	/*
20324b9abbaSab196087 	 * We have a non-empty buffer, and thus the opportunity
20424b9abbaSab196087 	 * to do some surgery on it before passing it to the output.
20524b9abbaSab196087 	 */
20624b9abbaSab196087 	buf[bufcnt] = '\0';
20724b9abbaSab196087 	bufptr = buf;
20824b9abbaSab196087 
20924b9abbaSab196087 	/*
21024b9abbaSab196087 	 * If our workspace is at the start, remove it.
21124b9abbaSab196087 	 * If not, then look for the system /usr/include instead.
21224b9abbaSab196087 	 */
21324b9abbaSab196087 	if ((wspace_len > 0) && (wspace_len < bufcnt) &&
21424b9abbaSab196087 	    (strncmp(bufptr, wspace, wspace_len) == 0)) {
21524b9abbaSab196087 		bufptr += wspace_len;
21624b9abbaSab196087 		bufcnt -= wspace_len;
21724b9abbaSab196087 
21824b9abbaSab196087 		/*
21924b9abbaSab196087 		 * Further opportunity: Also clip the prefix
22024b9abbaSab196087 		 * that leads to /usr/include in the proto.
22124b9abbaSab196087 		 */
22224b9abbaSab196087 		if ((proto_inc_len < bufcnt) &&
22324b9abbaSab196087 		    (strncmp(bufptr, PROTO_INC, proto_inc_len) == 0)) {
22424b9abbaSab196087 			bufptr += proto_inc_len;
22524b9abbaSab196087 			bufcnt -= proto_inc_len;
22624b9abbaSab196087 		}
22724b9abbaSab196087 	} else if ((sys_inc_len < bufcnt) &&
22824b9abbaSab196087 	    (strncmp(bufptr, SYS_INC, sys_inc_len) == 0)) {
22924b9abbaSab196087 		bufptr += sys_inc_len;
23024b9abbaSab196087 		bufcnt -= sys_inc_len;
23124b9abbaSab196087 	}
23224b9abbaSab196087 
23324b9abbaSab196087 	/* Output whatever is left */
23424b9abbaSab196087 	if (out && (fwrite(bufptr, 1, bufcnt, out) != bufcnt)) {
23524b9abbaSab196087 		perror("fwrite");
23624b9abbaSab196087 		exit(1);
23724b9abbaSab196087 	}
23824b9abbaSab196087 
23924b9abbaSab196087 #undef PROTO_INC
24024b9abbaSab196087 #undef SYS_INC
24124b9abbaSab196087 }
24224b9abbaSab196087 
24324b9abbaSab196087 /*
2447c478bd9Sstevel@tonic-gate  * The idea here is to take directives like this emitted
2457c478bd9Sstevel@tonic-gate  * by cpp:
2467c478bd9Sstevel@tonic-gate  *
2477c478bd9Sstevel@tonic-gate  *	# num
2487c478bd9Sstevel@tonic-gate  *
2497c478bd9Sstevel@tonic-gate  * and convert them to directives like this that are
2507c478bd9Sstevel@tonic-gate  * understood by the GNU assembler:
2517c478bd9Sstevel@tonic-gate  *
2527c478bd9Sstevel@tonic-gate  *	.line num
2537c478bd9Sstevel@tonic-gate  *
2547c478bd9Sstevel@tonic-gate  * and similarly:
2557c478bd9Sstevel@tonic-gate  *
2567c478bd9Sstevel@tonic-gate  *	# num "string" optional stuff
2577c478bd9Sstevel@tonic-gate  *
2587c478bd9Sstevel@tonic-gate  * is converted to
2597c478bd9Sstevel@tonic-gate  *
2607c478bd9Sstevel@tonic-gate  *	.line num
2617c478bd9Sstevel@tonic-gate  *	.file "string"
2627c478bd9Sstevel@tonic-gate  *
2637c478bd9Sstevel@tonic-gate  * While this could be done with a sequence of sed
2647c478bd9Sstevel@tonic-gate  * commands, this is simpler and faster..
2657c478bd9Sstevel@tonic-gate  */
2667c478bd9Sstevel@tonic-gate static pid_t
filter(int pipein,int pipeout)2677c478bd9Sstevel@tonic-gate filter(int pipein, int pipeout)
2687c478bd9Sstevel@tonic-gate {
2697c478bd9Sstevel@tonic-gate 	pid_t pid;
2707c478bd9Sstevel@tonic-gate 	FILE *in, *out;
27124b9abbaSab196087 	char *wspace;
27224b9abbaSab196087 	size_t wspace_len;
2737c478bd9Sstevel@tonic-gate 
2747c478bd9Sstevel@tonic-gate 	if (verbose)
2757c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "{#line filter} ");
2767c478bd9Sstevel@tonic-gate 
2777c478bd9Sstevel@tonic-gate 	switch (pid = fork()) {
2787c478bd9Sstevel@tonic-gate 	case 0:
2797c478bd9Sstevel@tonic-gate 		if (dup2(pipein, 0) == -1 ||
2807c478bd9Sstevel@tonic-gate 		    dup2(pipeout, 1) == -1) {
2817c478bd9Sstevel@tonic-gate 			perror("dup2");
2827c478bd9Sstevel@tonic-gate 			exit(1);
2837c478bd9Sstevel@tonic-gate 		}
2847c478bd9Sstevel@tonic-gate 		closefrom(3);
2857c478bd9Sstevel@tonic-gate 		break;
2867c478bd9Sstevel@tonic-gate 	case -1:
2877c478bd9Sstevel@tonic-gate 		perror("fork");
2887c478bd9Sstevel@tonic-gate 	default:
2897c478bd9Sstevel@tonic-gate 		return (pid);
2907c478bd9Sstevel@tonic-gate 	}
2917c478bd9Sstevel@tonic-gate 
2927c478bd9Sstevel@tonic-gate 	in = fdopen(0, "r");
2937c478bd9Sstevel@tonic-gate 	out = fdopen(1, "w");
2947c478bd9Sstevel@tonic-gate 
29524b9abbaSab196087 	/*
29624b9abbaSab196087 	 * Key off the CODEMGR_WS environment variable to detect
29724b9abbaSab196087 	 * if we're in an activated workspace, and to get the
29824b9abbaSab196087 	 * path to the workspace.
29924b9abbaSab196087 	 */
30024b9abbaSab196087 	wspace = getenv("CODEMGR_WS");
30124b9abbaSab196087 	if (wspace != NULL)
30224b9abbaSab196087 		wspace_len = strlen(wspace);
30324b9abbaSab196087 
3047c478bd9Sstevel@tonic-gate 	while (!feof(in)) {
3057c478bd9Sstevel@tonic-gate 		int c, num;
3067c478bd9Sstevel@tonic-gate 
3077c478bd9Sstevel@tonic-gate 		switch (c = fgetc(in)) {
3087c478bd9Sstevel@tonic-gate 		case '#':
3097c478bd9Sstevel@tonic-gate 			switch (fscanf(in, " %d", &num)) {
3107c478bd9Sstevel@tonic-gate 			case 0:
3117c478bd9Sstevel@tonic-gate 				/*
3127c478bd9Sstevel@tonic-gate 				 * discard comment lines completely
3137c478bd9Sstevel@tonic-gate 				 * discard ident strings completely too.
3147c478bd9Sstevel@tonic-gate 				 * (GNU as politely ignores them..)
3157c478bd9Sstevel@tonic-gate 				 */
3167c478bd9Sstevel@tonic-gate 				copyuntil(in, NULL, '\n');
3177c478bd9Sstevel@tonic-gate 				break;
3187c478bd9Sstevel@tonic-gate 			default:
3197c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, "fscanf botch?");
3207c478bd9Sstevel@tonic-gate 				/*FALLTHROUGH*/
3217c478bd9Sstevel@tonic-gate 			case EOF:
3227c478bd9Sstevel@tonic-gate 				exit(1);
3237c478bd9Sstevel@tonic-gate 				/*NOTREACHED*/
3247c478bd9Sstevel@tonic-gate 			case 1:
3257c478bd9Sstevel@tonic-gate 				/*
3267c478bd9Sstevel@tonic-gate 				 * This line has a number at the beginning;
3277c478bd9Sstevel@tonic-gate 				 * if it has a string after the number, then
3287c478bd9Sstevel@tonic-gate 				 * it's a filename.
32924b9abbaSab196087 				 *
33024b9abbaSab196087 				 * If this is an activated workspace, use
33124b9abbaSab196087 				 * copyuntil_path() to do path rewriting
33224b9abbaSab196087 				 * that will prevent workspace paths from
33324b9abbaSab196087 				 * being burned into the resulting object.
33424b9abbaSab196087 				 * If not in an activated workspace, then
33524b9abbaSab196087 				 * copy the existing path straight through
33624b9abbaSab196087 				 * without interpretation.
3377c478bd9Sstevel@tonic-gate 				 */
3387c478bd9Sstevel@tonic-gate 				if (fgetc(in) == ' ' && fgetc(in) == '"') {
3397c478bd9Sstevel@tonic-gate 					(void) fprintf(out, "\t.file \"");
34024b9abbaSab196087 					if (wspace != NULL)
34124b9abbaSab196087 						copyuntil_path(in, out, '"',
34224b9abbaSab196087 						    wspace, wspace_len);
34324b9abbaSab196087 					else
3447c478bd9Sstevel@tonic-gate 						copyuntil(in, out, '"');
3457c478bd9Sstevel@tonic-gate 					(void) fputc('\n', out);
3467c478bd9Sstevel@tonic-gate 				}
3477c478bd9Sstevel@tonic-gate 				(void) fprintf(out, "\t.line %d\n", num - 1);
3487c478bd9Sstevel@tonic-gate 				/*
3497c478bd9Sstevel@tonic-gate 				 * discard the rest of the line
3507c478bd9Sstevel@tonic-gate 				 */
3517c478bd9Sstevel@tonic-gate 				copyuntil(in, NULL, '\n');
3527c478bd9Sstevel@tonic-gate 				break;
3537c478bd9Sstevel@tonic-gate 			}
3547c478bd9Sstevel@tonic-gate 			break;
3557c478bd9Sstevel@tonic-gate 		case '\n':
3567c478bd9Sstevel@tonic-gate 			/*
3577c478bd9Sstevel@tonic-gate 			 * preserve newlines
3587c478bd9Sstevel@tonic-gate 			 */
3597c478bd9Sstevel@tonic-gate 			(void) fputc(c, out);
3607c478bd9Sstevel@tonic-gate 			break;
3617c478bd9Sstevel@tonic-gate 		case EOF:
3627c478bd9Sstevel@tonic-gate 			/*
3637c478bd9Sstevel@tonic-gate 			 * don't write EOF!
3647c478bd9Sstevel@tonic-gate 			 */
3657c478bd9Sstevel@tonic-gate 			break;
3667c478bd9Sstevel@tonic-gate 		default:
3677c478bd9Sstevel@tonic-gate 			/*
3687c478bd9Sstevel@tonic-gate 			 * lines that don't begin with '#' are copied
3697c478bd9Sstevel@tonic-gate 			 */
3707c478bd9Sstevel@tonic-gate 			(void) fputc(c, out);
3717c478bd9Sstevel@tonic-gate 			copyuntil(in, out, '\n');
3727c478bd9Sstevel@tonic-gate 			break;
3737c478bd9Sstevel@tonic-gate 		}
3747c478bd9Sstevel@tonic-gate 
3757c478bd9Sstevel@tonic-gate 		if (ferror(out))
3767c478bd9Sstevel@tonic-gate 			exit(1);
3777c478bd9Sstevel@tonic-gate 	}
3787c478bd9Sstevel@tonic-gate 
3797c478bd9Sstevel@tonic-gate 	exit(0);
3807c478bd9Sstevel@tonic-gate 	/*NOTREACHED*/
3817c478bd9Sstevel@tonic-gate }
3827c478bd9Sstevel@tonic-gate 
3837c478bd9Sstevel@tonic-gate static pid_t
invoke(char ** argv,int pipein,int pipeout)3847c478bd9Sstevel@tonic-gate invoke(char **argv, int pipein, int pipeout)
3857c478bd9Sstevel@tonic-gate {
3867c478bd9Sstevel@tonic-gate 	pid_t pid;
3877c478bd9Sstevel@tonic-gate 
3887c478bd9Sstevel@tonic-gate 	if (verbose) {
3897c478bd9Sstevel@tonic-gate 		char **dargv = argv;
3907c478bd9Sstevel@tonic-gate 
3917c478bd9Sstevel@tonic-gate 		while (*dargv)
3927c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "%s ", *dargv++);
3937c478bd9Sstevel@tonic-gate 	}
3947c478bd9Sstevel@tonic-gate 
3957c478bd9Sstevel@tonic-gate 	switch (pid = fork()) {
3967c478bd9Sstevel@tonic-gate 	case 0:
3977c478bd9Sstevel@tonic-gate 		if (pipein >= 0 && dup2(pipein, 0) == -1) {
3987c478bd9Sstevel@tonic-gate 			perror("dup2");
3997c478bd9Sstevel@tonic-gate 			exit(1);
4007c478bd9Sstevel@tonic-gate 		}
4017c478bd9Sstevel@tonic-gate 		if (pipeout >= 0 && dup2(pipeout, 1) == -1) {
4027c478bd9Sstevel@tonic-gate 			perror("dup2");
4037c478bd9Sstevel@tonic-gate 			exit(1);
4047c478bd9Sstevel@tonic-gate 		}
4057c478bd9Sstevel@tonic-gate 		closefrom(3);
4067c478bd9Sstevel@tonic-gate 		(void) execvp(argv[0], argv);
4077c478bd9Sstevel@tonic-gate 		perror("execvp");
4087c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "%s: couldn't run %s\n",
4097c478bd9Sstevel@tonic-gate 		    progname, argv[0]);
4107c478bd9Sstevel@tonic-gate 		break;
4117c478bd9Sstevel@tonic-gate 	case -1:
4127c478bd9Sstevel@tonic-gate 		perror("fork");
4137c478bd9Sstevel@tonic-gate 	default:
4147c478bd9Sstevel@tonic-gate 		return (pid);
4157c478bd9Sstevel@tonic-gate 	}
4167c478bd9Sstevel@tonic-gate 	exit(2);
4177c478bd9Sstevel@tonic-gate 	/*NOTREACHED*/
4187c478bd9Sstevel@tonic-gate }
4197c478bd9Sstevel@tonic-gate 
4207c478bd9Sstevel@tonic-gate static int
pipeline(char ** ppargv,char ** asargv)4217c478bd9Sstevel@tonic-gate pipeline(char **ppargv, char **asargv)
4227c478bd9Sstevel@tonic-gate {
4237c478bd9Sstevel@tonic-gate 	int pipedes[4];
4247c478bd9Sstevel@tonic-gate 	int active = 0;
4257c478bd9Sstevel@tonic-gate 	int rval = 0;
4267c478bd9Sstevel@tonic-gate 	pid_t pid_pp, pid_f, pid_as;
4277c478bd9Sstevel@tonic-gate 
4287c478bd9Sstevel@tonic-gate 	if (pipe(pipedes) == -1 || pipe(pipedes + 2) == -1) {
4297c478bd9Sstevel@tonic-gate 		perror("pipe");
4307c478bd9Sstevel@tonic-gate 		return (4);
4317c478bd9Sstevel@tonic-gate 	}
4327c478bd9Sstevel@tonic-gate 
4337c478bd9Sstevel@tonic-gate 	if ((pid_pp = invoke(ppargv, -1, pipedes[0])) > 0)
4347c478bd9Sstevel@tonic-gate 		active++;
4357c478bd9Sstevel@tonic-gate 
4367c478bd9Sstevel@tonic-gate 	if (verbose)
4377c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "| ");
4387c478bd9Sstevel@tonic-gate 
4397c478bd9Sstevel@tonic-gate 	if ((pid_f = filter(pipedes[1], pipedes[2])) > 0)
4407c478bd9Sstevel@tonic-gate 		active++;
4417c478bd9Sstevel@tonic-gate 
4427c478bd9Sstevel@tonic-gate 	if (verbose)
4437c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "| ");
4447c478bd9Sstevel@tonic-gate 
4457c478bd9Sstevel@tonic-gate 	if ((pid_as = invoke(asargv, pipedes[3], -1)) > 0)
4467c478bd9Sstevel@tonic-gate 		active++;
4477c478bd9Sstevel@tonic-gate 
4487c478bd9Sstevel@tonic-gate 	if (verbose) {
4497c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "\n");
4507c478bd9Sstevel@tonic-gate 		(void) fflush(stderr);
4517c478bd9Sstevel@tonic-gate 	}
4527c478bd9Sstevel@tonic-gate 
4537c478bd9Sstevel@tonic-gate 	closefrom(3);
4547c478bd9Sstevel@tonic-gate 
4557c478bd9Sstevel@tonic-gate 	if (active != 3)
4567c478bd9Sstevel@tonic-gate 		return (5);
4577c478bd9Sstevel@tonic-gate 
4587c478bd9Sstevel@tonic-gate 	while (active != 0) {
4597c478bd9Sstevel@tonic-gate 		pid_t pid;
4607c478bd9Sstevel@tonic-gate 		int stat;
4617c478bd9Sstevel@tonic-gate 
4627c478bd9Sstevel@tonic-gate 		if ((pid = wait(&stat)) == -1) {
4637c478bd9Sstevel@tonic-gate 			rval++;
4647c478bd9Sstevel@tonic-gate 			break;
4657c478bd9Sstevel@tonic-gate 		}
4667c478bd9Sstevel@tonic-gate 
4677c478bd9Sstevel@tonic-gate 		if (!WIFEXITED(stat))
4687c478bd9Sstevel@tonic-gate 			continue;
4697c478bd9Sstevel@tonic-gate 
4707c478bd9Sstevel@tonic-gate 		if (pid == pid_pp || pid == pid_f || pid == pid_as) {
4717c478bd9Sstevel@tonic-gate 			active--;
4727c478bd9Sstevel@tonic-gate 			if (WEXITSTATUS(stat) != 0)
4737c478bd9Sstevel@tonic-gate 				rval++;
4747c478bd9Sstevel@tonic-gate 		}
4757c478bd9Sstevel@tonic-gate 	}
4767c478bd9Sstevel@tonic-gate 
4777c478bd9Sstevel@tonic-gate 	return (rval);
4787c478bd9Sstevel@tonic-gate }
4797c478bd9Sstevel@tonic-gate 
4807c478bd9Sstevel@tonic-gate int
main(int argc,char * argv[])4817c478bd9Sstevel@tonic-gate main(int argc, char *argv[])
4827c478bd9Sstevel@tonic-gate {
4837c478bd9Sstevel@tonic-gate 	struct aelist *cpp = NULL;
4847c478bd9Sstevel@tonic-gate 	struct aelist *m4 = NULL;
4857c478bd9Sstevel@tonic-gate 	struct aelist *as = newael();
4867c478bd9Sstevel@tonic-gate 	char **asargv;
4877c478bd9Sstevel@tonic-gate 	char *outfile = NULL;
4887c478bd9Sstevel@tonic-gate 	char *srcfile = NULL;
4897fbf8d03SScott Rotondo 	const char *dir, *cmd;
4907fbf8d03SScott Rotondo 	static char as_pgm[MAXPATHLEN];
4917fbf8d03SScott Rotondo 	static char as64_pgm[MAXPATHLEN];
4927fbf8d03SScott Rotondo 	static char m4_pgm[MAXPATHLEN];
4937fbf8d03SScott Rotondo 	static char m4_cmdefs[MAXPATHLEN];
4947fbf8d03SScott Rotondo 	static char cpp_pgm[MAXPATHLEN];
4957c478bd9Sstevel@tonic-gate 	int as64 = 0;
4967c478bd9Sstevel@tonic-gate 	int code;
4977c478bd9Sstevel@tonic-gate 
4987c478bd9Sstevel@tonic-gate 	if ((progname = strrchr(argv[0], '/')) == NULL)
4997c478bd9Sstevel@tonic-gate 		progname = argv[0];
5007c478bd9Sstevel@tonic-gate 	else
5017c478bd9Sstevel@tonic-gate 		progname++;
5027c478bd9Sstevel@tonic-gate 
5037c478bd9Sstevel@tonic-gate 	/*
5047c478bd9Sstevel@tonic-gate 	 * Helpful when debugging, or when changing tool versions..
5057c478bd9Sstevel@tonic-gate 	 */
5067fbf8d03SScott Rotondo 	if ((cmd = getenv("AW_AS")) != NULL)
5077fbf8d03SScott Rotondo 		strlcpy(as_pgm, cmd, sizeof (as_pgm));
5087fbf8d03SScott Rotondo 	else {
5097fbf8d03SScott Rotondo 		if ((dir = getenv("AW_AS_DIR")) == NULL)
5107fbf8d03SScott Rotondo 			dir = DEFAULT_AS_DIR;	/* /usr/sfw/bin */
5117fbf8d03SScott Rotondo 		(void) snprintf(as_pgm, sizeof (as_pgm), "%s/gas", dir);
5127fbf8d03SScott Rotondo 	}
5137c478bd9Sstevel@tonic-gate 
5147fbf8d03SScott Rotondo 	if ((cmd = getenv("AW_AS64")) != NULL)
5157fbf8d03SScott Rotondo 		strlcpy(as64_pgm, cmd, sizeof (as64_pgm));
5167fbf8d03SScott Rotondo 	else {
5177fbf8d03SScott Rotondo 		if ((dir = getenv("AW_AS64_DIR")) == NULL)
5187fbf8d03SScott Rotondo 			dir = DEFAULT_AS64_DIR;	/* /usr/sfw/bin */
5197fbf8d03SScott Rotondo 		(void) snprintf(as64_pgm, sizeof (as_pgm), "%s/gas", dir);
5207fbf8d03SScott Rotondo 	}
5217c478bd9Sstevel@tonic-gate 
5227fbf8d03SScott Rotondo 	if ((cmd = getenv("AW_M4")) != NULL)
5237fbf8d03SScott Rotondo 		strlcpy(m4_pgm, cmd, sizeof (m4_pgm));
5247fbf8d03SScott Rotondo 	else {
5257fbf8d03SScott Rotondo 		if ((dir = getenv("AW_M4_DIR")) == NULL)
5267fbf8d03SScott Rotondo 			dir = DEFAULT_M4_DIR;	/* /usr/ccs/bin */
5277fbf8d03SScott Rotondo 		(void) snprintf(m4_pgm, sizeof (m4_pgm), "%s/m4", dir);
5287fbf8d03SScott Rotondo 	}
5297c478bd9Sstevel@tonic-gate 
5307fbf8d03SScott Rotondo 	if ((cmd = getenv("AW_M4LIB")) != NULL)
5317fbf8d03SScott Rotondo 		strlcpy(m4_cmdefs, cmd, sizeof (m4_cmdefs));
5327fbf8d03SScott Rotondo 	else {
5337fbf8d03SScott Rotondo 		if ((dir = getenv("AW_M4LIB_DIR")) == NULL)
5347fbf8d03SScott Rotondo 			dir = DEFAULT_M4LIB_DIR;	/* /usr/ccs/lib */
5357fbf8d03SScott Rotondo 		(void) snprintf(m4_cmdefs, sizeof (m4_cmdefs),
5367fbf8d03SScott Rotondo 		    "%s/cm4defs", dir);
5377fbf8d03SScott Rotondo 	}
5387c478bd9Sstevel@tonic-gate 
5397fbf8d03SScott Rotondo 	if ((cmd = getenv("AW_CPP")) != NULL)
5407fbf8d03SScott Rotondo 		strlcpy(cpp_pgm, cmd, sizeof (cpp_pgm));
5417fbf8d03SScott Rotondo 	else {
5427fbf8d03SScott Rotondo 		if ((dir = getenv("AW_CPP_DIR")) == NULL)
5437fbf8d03SScott Rotondo 			dir = DEFAULT_CPP_DIR;	/* /usr/ccs/lib */
5447fbf8d03SScott Rotondo 		(void) snprintf(cpp_pgm, sizeof (cpp_pgm), "%s/cpp", dir);
5457fbf8d03SScott Rotondo 	}
5467c478bd9Sstevel@tonic-gate 
5477c478bd9Sstevel@tonic-gate 	newae(as, as_pgm);
5487c478bd9Sstevel@tonic-gate 	newae(as, "--warn");
5497c478bd9Sstevel@tonic-gate 	newae(as, "--fatal-warnings");
5507c478bd9Sstevel@tonic-gate 	newae(as, "--traditional-format");
5517c478bd9Sstevel@tonic-gate 
5527c478bd9Sstevel@tonic-gate 	/*
5537c478bd9Sstevel@tonic-gate 	 * Walk the argument list, translating as we go ..
5547c478bd9Sstevel@tonic-gate 	 */
5557c478bd9Sstevel@tonic-gate 	while (--argc > 0) {
5567c478bd9Sstevel@tonic-gate 		char *arg;
5577c478bd9Sstevel@tonic-gate 		int arglen;
5587c478bd9Sstevel@tonic-gate 
5597c478bd9Sstevel@tonic-gate 		arg = *++argv;
5607c478bd9Sstevel@tonic-gate 		arglen = strlen(arg);
5617c478bd9Sstevel@tonic-gate 
5627c478bd9Sstevel@tonic-gate 		if (*arg != '-') {
5637c478bd9Sstevel@tonic-gate 			char *filename;
5647c478bd9Sstevel@tonic-gate 
5657c478bd9Sstevel@tonic-gate 			/*
5667c478bd9Sstevel@tonic-gate 			 * filenames ending in '.s' are taken to be
5677c478bd9Sstevel@tonic-gate 			 * assembler files, and provide the default
5687c478bd9Sstevel@tonic-gate 			 * basename of the output file.
5697c478bd9Sstevel@tonic-gate 			 *
5707c478bd9Sstevel@tonic-gate 			 * other files are passed through to the
5717c478bd9Sstevel@tonic-gate 			 * preprocessor, if present, or to gas if not.
5727c478bd9Sstevel@tonic-gate 			 */
5737c478bd9Sstevel@tonic-gate 			filename = arg;
574*5b2ba9d3SPiotr Jasiukajtis 			if ((arglen > 2) &&
575*5b2ba9d3SPiotr Jasiukajtis 			    ((strcmp(arg + arglen - 2, ".s") == 0) ||
576*5b2ba9d3SPiotr Jasiukajtis 			    (strcmp(arg + arglen - 2, ".S") == 0))) {
5777c478bd9Sstevel@tonic-gate 				/*
5787c478bd9Sstevel@tonic-gate 				 * Though 'as' allows multiple assembler
5797c478bd9Sstevel@tonic-gate 				 * files to be processed in one invocation
5807c478bd9Sstevel@tonic-gate 				 * of the assembler, ON only processes one
5817c478bd9Sstevel@tonic-gate 				 * file at a time, which makes things a lot
5827c478bd9Sstevel@tonic-gate 				 * simpler!
5837c478bd9Sstevel@tonic-gate 				 */
5847c478bd9Sstevel@tonic-gate 				if (srcfile == NULL)
5857c478bd9Sstevel@tonic-gate 					srcfile = arg;
5867c478bd9Sstevel@tonic-gate 				else
5877c478bd9Sstevel@tonic-gate 					return (usage(
5887c478bd9Sstevel@tonic-gate 					    "one assembler file at a time"));
5897c478bd9Sstevel@tonic-gate 
5907c478bd9Sstevel@tonic-gate 				/*
5917c478bd9Sstevel@tonic-gate 				 * If we haven't seen a -o option yet,
5927c478bd9Sstevel@tonic-gate 				 * default the output to the basename
5937c478bd9Sstevel@tonic-gate 				 * of the input, substituting a .o on the end
5947c478bd9Sstevel@tonic-gate 				 */
5957c478bd9Sstevel@tonic-gate 				if (outfile == NULL) {
5967c478bd9Sstevel@tonic-gate 					char *argcopy;
5977c478bd9Sstevel@tonic-gate 
5987c478bd9Sstevel@tonic-gate 					argcopy = strdup(arg);
5997c478bd9Sstevel@tonic-gate 					argcopy[arglen - 1] = 'o';
6007c478bd9Sstevel@tonic-gate 
6017c478bd9Sstevel@tonic-gate 					if ((outfile = strrchr(
6027c478bd9Sstevel@tonic-gate 					    argcopy, '/')) == NULL)
6037c478bd9Sstevel@tonic-gate 						outfile = argcopy;
6047c478bd9Sstevel@tonic-gate 					else
6057c478bd9Sstevel@tonic-gate 						outfile++;
6067c478bd9Sstevel@tonic-gate 				}
6077c478bd9Sstevel@tonic-gate 			}
6087c478bd9Sstevel@tonic-gate 			if (cpp)
6097c478bd9Sstevel@tonic-gate 				newae(cpp, filename);
6107c478bd9Sstevel@tonic-gate 			else if (m4)
6117c478bd9Sstevel@tonic-gate 				newae(m4, filename);
6127c478bd9Sstevel@tonic-gate 			else
6137c478bd9Sstevel@tonic-gate 				newae(as, filename);
6147c478bd9Sstevel@tonic-gate 			continue;
6157c478bd9Sstevel@tonic-gate 		} else
6167c478bd9Sstevel@tonic-gate 			arglen--;
6177c478bd9Sstevel@tonic-gate 
6187c478bd9Sstevel@tonic-gate 		switch (arg[1]) {
6197c478bd9Sstevel@tonic-gate 		case 'K':
6207c478bd9Sstevel@tonic-gate 			/*
6217c478bd9Sstevel@tonic-gate 			 * -K pic
6227c478bd9Sstevel@tonic-gate 			 * -K PIC
6237c478bd9Sstevel@tonic-gate 			 */
6247c478bd9Sstevel@tonic-gate 			if (arglen == 1) {
6257c478bd9Sstevel@tonic-gate 				if ((arg = *++argv) == NULL || *arg == '\0')
6267c478bd9Sstevel@tonic-gate 					return (usage("malformed -K"));
6277c478bd9Sstevel@tonic-gate 				argc--;
6287c478bd9Sstevel@tonic-gate 			} else {
6297c478bd9Sstevel@tonic-gate 				arg += 2;
6307c478bd9Sstevel@tonic-gate 			}
6317c478bd9Sstevel@tonic-gate 			if (strcmp(arg, "PIC") != 0 && strcmp(arg, "pic") != 0)
6327c478bd9Sstevel@tonic-gate 				return (usage("malformed -K"));
6337c478bd9Sstevel@tonic-gate 			break;		/* just ignore -Kpic for gcc */
6347c478bd9Sstevel@tonic-gate 		case 'Q':
6357c478bd9Sstevel@tonic-gate 			if (strcmp(arg, "-Qn") == 0)
6367c478bd9Sstevel@tonic-gate 				break;
6377c478bd9Sstevel@tonic-gate 			/*FALLTHROUGH*/
6387c478bd9Sstevel@tonic-gate 		case 'b':
6397c478bd9Sstevel@tonic-gate 		case 's':
6407c478bd9Sstevel@tonic-gate 		case 'T':
6417c478bd9Sstevel@tonic-gate 			/*
6427c478bd9Sstevel@tonic-gate 			 * -b	Extra symbol table for source browser ..
6437c478bd9Sstevel@tonic-gate 			 *	not relevant to gas, thus should error.
6447c478bd9Sstevel@tonic-gate 			 * -s	Put stabs in .stabs section not stabs.excl
6457c478bd9Sstevel@tonic-gate 			 *	not clear if there's an equivalent
6467c478bd9Sstevel@tonic-gate 			 * -T	4.x migration option
6477c478bd9Sstevel@tonic-gate 			 */
6487c478bd9Sstevel@tonic-gate 		default:
6497c478bd9Sstevel@tonic-gate 			return (error(arg));
6507c478bd9Sstevel@tonic-gate 		case 'x':
6517c478bd9Sstevel@tonic-gate 			/*
6527c478bd9Sstevel@tonic-gate 			 * Accept -xarch special case to invoke alternate
6537c478bd9Sstevel@tonic-gate 			 * assemblers or assembler flags for different
6547c478bd9Sstevel@tonic-gate 			 * architectures.
6557c478bd9Sstevel@tonic-gate 			 */
6567c478bd9Sstevel@tonic-gate 			if (strcmp(arg, "-xarch=amd64") == 0 ||
6577c478bd9Sstevel@tonic-gate 			    strcmp(arg, "-xarch=generic64") == 0) {
6587c478bd9Sstevel@tonic-gate 				as64++;
6597c478bd9Sstevel@tonic-gate 				fixae_arg(as->ael_head, as64_pgm);
6607c478bd9Sstevel@tonic-gate 				break;
6617c478bd9Sstevel@tonic-gate 			}
6627c478bd9Sstevel@tonic-gate 			/*
6637c478bd9Sstevel@tonic-gate 			 * XX64: Is this useful to gas?
6647c478bd9Sstevel@tonic-gate 			 */
6657c478bd9Sstevel@tonic-gate 			if (strcmp(arg, "-xmodel=kernel") == 0)
6667c478bd9Sstevel@tonic-gate 				break;
6677c478bd9Sstevel@tonic-gate 
6687c478bd9Sstevel@tonic-gate 			/*
6697c478bd9Sstevel@tonic-gate 			 * -xF	Generates performance analysis data
6707c478bd9Sstevel@tonic-gate 			 *	no equivalent
6717c478bd9Sstevel@tonic-gate 			 */
6727c478bd9Sstevel@tonic-gate 			return (error(arg));
6737c478bd9Sstevel@tonic-gate 		case 'V':
6747c478bd9Sstevel@tonic-gate 			newae(as, arg);
6757c478bd9Sstevel@tonic-gate 			break;
6767c478bd9Sstevel@tonic-gate 		case '#':
6777c478bd9Sstevel@tonic-gate 			verbose++;
6787c478bd9Sstevel@tonic-gate 			break;
6797c478bd9Sstevel@tonic-gate 		case 'L':
6807c478bd9Sstevel@tonic-gate 			newae(as, "--keep-locals");
6817c478bd9Sstevel@tonic-gate 			break;
6827c478bd9Sstevel@tonic-gate 		case 'n':
6837c478bd9Sstevel@tonic-gate 			newae(as, "--no-warn");
6847c478bd9Sstevel@tonic-gate 			break;
6857c478bd9Sstevel@tonic-gate 		case 'o':
6867c478bd9Sstevel@tonic-gate 			if (arglen != 1)
6877c478bd9Sstevel@tonic-gate 				return (usage("bad -o flag"));
6887c478bd9Sstevel@tonic-gate 			if ((arg = *++argv) == NULL || *arg == '\0')
6897c478bd9Sstevel@tonic-gate 				return (usage("bad -o flag"));
6907c478bd9Sstevel@tonic-gate 			outfile = arg;
6917c478bd9Sstevel@tonic-gate 			argc--;
6927c478bd9Sstevel@tonic-gate 			arglen = strlen(arg + 1);
6937c478bd9Sstevel@tonic-gate 			break;
6947c478bd9Sstevel@tonic-gate 		case 'm':
6957c478bd9Sstevel@tonic-gate 			if (cpp)
6967c478bd9Sstevel@tonic-gate 				return (usage("-m conflicts with -P"));
6977c478bd9Sstevel@tonic-gate 			if (m4 == NULL) {
6987c478bd9Sstevel@tonic-gate 				m4 = newael();
6997c478bd9Sstevel@tonic-gate 				newae(m4, m4_pgm);
7007c478bd9Sstevel@tonic-gate 				newae(m4, m4_cmdefs);
7017c478bd9Sstevel@tonic-gate 			}
7027c478bd9Sstevel@tonic-gate 			break;
7037c478bd9Sstevel@tonic-gate 		case 'P':
7047c478bd9Sstevel@tonic-gate 			if (m4)
7057c478bd9Sstevel@tonic-gate 				return (usage("-P conflicts with -m"));
7067c478bd9Sstevel@tonic-gate 			if (cpp == NULL) {
7077c478bd9Sstevel@tonic-gate 				cpp = newael();
7087c478bd9Sstevel@tonic-gate 				newae(cpp, cpp_pgm);
7097c478bd9Sstevel@tonic-gate 				newae(cpp, "-D__GNUC_AS__");
7107c478bd9Sstevel@tonic-gate 			}
7117c478bd9Sstevel@tonic-gate 			break;
7127c478bd9Sstevel@tonic-gate 		case 'D':
7137c478bd9Sstevel@tonic-gate 		case 'U':
7147c478bd9Sstevel@tonic-gate 			if (cpp)
7157c478bd9Sstevel@tonic-gate 				newae(cpp, arg);
7167c478bd9Sstevel@tonic-gate 			else if (m4)
7177c478bd9Sstevel@tonic-gate 				newae(m4, arg);
7187c478bd9Sstevel@tonic-gate 			else
7197c478bd9Sstevel@tonic-gate 				newae(as, arg);
7207c478bd9Sstevel@tonic-gate 			break;
7217c478bd9Sstevel@tonic-gate 		case 'I':
7227c478bd9Sstevel@tonic-gate 			if (cpp)
7237c478bd9Sstevel@tonic-gate 				newae(cpp, arg);
7247c478bd9Sstevel@tonic-gate 			else
7257c478bd9Sstevel@tonic-gate 				newae(as, arg);
7267c478bd9Sstevel@tonic-gate 			break;
7277c478bd9Sstevel@tonic-gate 		case '-':	/* a gas-specific option */
7287c478bd9Sstevel@tonic-gate 			newae(as, arg);
7297c478bd9Sstevel@tonic-gate 			break;
7307c478bd9Sstevel@tonic-gate 		}
7317c478bd9Sstevel@tonic-gate 	}
7327c478bd9Sstevel@tonic-gate 
7337c478bd9Sstevel@tonic-gate #if defined(__i386)
7347c478bd9Sstevel@tonic-gate 	if (as64)
7357c478bd9Sstevel@tonic-gate 		newae(as, "--64");
7367c478bd9Sstevel@tonic-gate 	else
7377c478bd9Sstevel@tonic-gate 		newae(as, "--32");
7387c478bd9Sstevel@tonic-gate #endif
7397c478bd9Sstevel@tonic-gate 
7407c478bd9Sstevel@tonic-gate 	if (srcfile == NULL)
7417c478bd9Sstevel@tonic-gate 		return (usage("no source file(s) specified"));
7427c478bd9Sstevel@tonic-gate 	if (outfile == NULL)
7437c478bd9Sstevel@tonic-gate 		outfile = "a.out";
7447c478bd9Sstevel@tonic-gate 	newae(as, "-o");
7457c478bd9Sstevel@tonic-gate 	newae(as, outfile);
7467c478bd9Sstevel@tonic-gate 
7477c478bd9Sstevel@tonic-gate 	asargv = aeltoargv(as);
7487c478bd9Sstevel@tonic-gate 	if (cpp) {
7497c478bd9Sstevel@tonic-gate #if defined(__sparc)
7507c478bd9Sstevel@tonic-gate 		newae(cpp, "-Dsparc");
7517c478bd9Sstevel@tonic-gate 		newae(cpp, "-D__sparc");
7527c478bd9Sstevel@tonic-gate 		if (as64)
7537c478bd9Sstevel@tonic-gate 			newae(cpp, "-D__sparcv9");
7547c478bd9Sstevel@tonic-gate 		else
7557c478bd9Sstevel@tonic-gate 			newae(cpp, "-D__sparcv8");
7567c478bd9Sstevel@tonic-gate #elif defined(__i386) || defined(__x86)
7577c478bd9Sstevel@tonic-gate 		if (as64) {
7587c478bd9Sstevel@tonic-gate 			newae(cpp, "-D__x86_64");
7597c478bd9Sstevel@tonic-gate 			newae(cpp, "-D__amd64");
7607c478bd9Sstevel@tonic-gate 		} else {
7617c478bd9Sstevel@tonic-gate 			newae(cpp, "-Di386");
7627c478bd9Sstevel@tonic-gate 			newae(cpp, "-D__i386");
7637c478bd9Sstevel@tonic-gate 		}
7647c478bd9Sstevel@tonic-gate #else
7657c478bd9Sstevel@tonic-gate #error	"need isa-dependent defines"
7667c478bd9Sstevel@tonic-gate #endif
7677c478bd9Sstevel@tonic-gate 		code = pipeline(aeltoargv(cpp), asargv);
7687c478bd9Sstevel@tonic-gate 	} else if (m4)
7697c478bd9Sstevel@tonic-gate 		code = pipeline(aeltoargv(m4), asargv);
7707c478bd9Sstevel@tonic-gate 	else {
7717c478bd9Sstevel@tonic-gate 		/*
7727c478bd9Sstevel@tonic-gate 		 * XXX	should arrange to fork/exec so that we
7737c478bd9Sstevel@tonic-gate 		 *	can unlink the output file if errors are
7747c478bd9Sstevel@tonic-gate 		 *	detected..
7757c478bd9Sstevel@tonic-gate 		 */
7767c478bd9Sstevel@tonic-gate 		(void) execvp(asargv[0], asargv);
7777c478bd9Sstevel@tonic-gate 		perror("execvp");
7787c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "%s: couldn't run %s\n",
7797c478bd9Sstevel@tonic-gate 		    progname, asargv[0]);
7807c478bd9Sstevel@tonic-gate 		code = 7;
7817c478bd9Sstevel@tonic-gate 	}
7827c478bd9Sstevel@tonic-gate 	if (code != 0)
7837c478bd9Sstevel@tonic-gate 		(void) unlink(outfile);
7847c478bd9Sstevel@tonic-gate 	return (code);
7857c478bd9Sstevel@tonic-gate }
786