scc

simple c99 compiler
git clone git://git.simple-cc.org/scc
Log | Files | Refs | Submodules | README | LICENSE

as.c (1467B)


      1 #include <stdio.h>
      2 #include <stdlib.h>
      3 
      4 #include <scc/arg.h>
      5 #include <scc/config.h>
      6 #include <scc/scc.h>
      7 
      8 #define MAXCMD 400
      9 
     10 char *argv0;
     11 
     12 static char *arch, *format, *prefix, *outfile;
     13 static int debug;
     14 
     15 static void
     16 usage(void)
     17 {
     18 	fputs("usage: as [-o outfile][-m arch][-f format] file ...\n",
     19 	      stderr);
     20 	exit(EXIT_FAILURE);
     21 }
     22 
     23 int
     24 main(int argc, char *argv[])
     25 {
     26 	int cnt, len, rem, r;
     27 	char cmd[MAXCMD];
     28 
     29 	if (!(arch = getenv("ARCH")))
     30 		arch = ARCH;
     31 	if (!(format = getenv("FORMAT")))
     32 		format = FORMAT;
     33 	if (!(prefix = getenv("SCCPREFIX")))
     34 		prefix = PREFIX;
     35 
     36 	ARGBEGIN {
     37 	case 'o':
     38 		outfile = EARGF(usage());
     39 		break;
     40 	case 'm':
     41 		arch = EARGF(usage());
     42 		break;
     43 	case 'f':
     44 		format = EARGF(usage());
     45 		break;
     46 	case 'd':
     47 		debug = 1;
     48 		break;
     49 	} ARGEND
     50 
     51 	if (argc < 1)
     52 		usage();
     53 
     54 	r = snprintf(cmd, sizeof(cmd), "%s/libexec/scc/as-%s", prefix, arch);
     55 	if (r < 0 || r >= sizeof(cmd))
     56 		goto toolong;
     57 
     58 	cnt = r;
     59 	rem = sizeof(cmd) - cnt;
     60 	if (outfile) {
     61 		r = snprintf(cmd + cnt, rem, " -o %s", outfile);
     62 		if (r < 0 || r >= rem)
     63 			goto toolong;
     64 		rem -= r;
     65 		cnt += r;
     66 	}
     67 
     68 	for ( ; *argv; ++argv) {
     69 		r = snprintf(cmd + cnt, rem, " %s", *argv);
     70 		if (r < 0 || r >= rem)
     71 			goto toolong;
     72 		rem -= r;
     73 		cnt += r;
     74 	}
     75 
     76 	if (debug)
     77 		fprintf(stderr, "as: command line '%s'\n", cmd);
     78 
     79 	r = system(cmd);
     80 	if (r == 0)
     81 		return 0;
     82 	if (r < 0)
     83 		perror("as");
     84 	exit(EXIT_FAILURE);
     85 
     86 toolong:
     87 	fputs("as: too long command line\n", stderr);
     88 	exit(EXIT_FAILURE);
     89 }