fcc.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676
  1. /* fcc
  2. Driver for fast cc-compatible ACK C compiler.
  3. Derived from the C compiler driver from Minix.
  4. Compile this file with
  5. cc -O -I<EM home dir>/config driver.c
  6. Install the resulting binaries in the EM bin directory.
  7. Suggested name: fcc
  8. */
  9. #ifdef sun3
  10. #define MACHNAME "m68020"
  11. #define SYSNAME "sun3"
  12. #endif
  13. #ifdef vax4
  14. #define MACHNAME "vax4"
  15. #define SYSNAME "vax4"
  16. #endif
  17. #include <errno.h>
  18. #include <signal.h>
  19. #include <stdio.h>
  20. #include <em_path.h>
  21. #if __STDC__
  22. #include <stdarg.h>
  23. #else
  24. #include <varargs.h>
  25. #endif
  26. /*
  27. Version producing cc-compatible .o files in one pass.
  28. */
  29. #define MAXARGC 256 /* maximum number of arguments allowed in a list */
  30. #define USTR_SIZE 128 /* maximum length of string variable */
  31. typedef char USTRING[USTR_SIZE];
  32. struct arglist {
  33. int al_argc;
  34. char *al_argv[MAXARGC];
  35. };
  36. #define CPP_NAME "$H/lib.bin/cpp"
  37. #define LD_NAME "/bin/ld"
  38. #define AS_NAME "/bin/as"
  39. #define SHELL "/bin/sh"
  40. char *CPP;
  41. char *COMP;
  42. int kids = -1;
  43. int ecount = 0;
  44. struct arglist CPP_FLAGS = {
  45. 7,
  46. {
  47. "-Dunix",
  48. "-D_EM_WSIZE=4",
  49. "-D_EM_PSIZE=4",
  50. "-D_EM_SSIZE=2",
  51. "-D_EM_LSIZE=4",
  52. "-D_EM_FSIZE=4",
  53. "-D_EM_DSIZE=8",
  54. }
  55. };
  56. struct arglist LD_HEAD = {
  57. #ifdef sun3
  58. 8,
  59. {
  60. "-dc",
  61. "-dp",
  62. "-e",
  63. "start",
  64. "-X",
  65. "-L/usr/lib/fsoft",
  66. "/usr/lib/crt0.o",
  67. "/usr/lib/Fcrt1.o"
  68. }
  69. #endif
  70. #ifdef vax4
  71. 2,
  72. {
  73. "-X",
  74. "/lib/crt0.o"
  75. }
  76. #endif
  77. };
  78. struct arglist LD_TAIL = {
  79. 2,
  80. {
  81. "$H/lib/$S/tail_ext",
  82. "-lc"
  83. }
  84. };
  85. struct arglist LD_FLAGS;
  86. struct arglist COMP_FLAGS;
  87. char *o_FILE = "a.out"; /* default name for executable file */
  88. #define remove(str) ((noexec || unlink(str)), (str)[0] = '\0')
  89. #define cleanup(str) (str && str[0] && remove(str))
  90. #define init(al) ((al)->al_argc = 1)
  91. char ProgCall[128];
  92. struct arglist SRCFILES;
  93. struct arglist LDFILES;
  94. int RET_CODE = 0;
  95. struct arglist CALL_VEC;
  96. int o_flag = 0;
  97. int c_flag = 0;
  98. int v_flag = 0;
  99. int O_flag = 0;
  100. #if __STDC__
  101. char *mkstr(char *, ...);
  102. #else
  103. char *mkstr();
  104. #endif
  105. char *malloc();
  106. char *alloc();
  107. char *extension();
  108. char *expand_string();
  109. USTRING ofile;
  110. USTRING BASE;
  111. USTRING tmp_file;
  112. int noexec = 0;
  113. extern char *strcat(), *strcpy(), *mktemp(), *strchr();
  114. trapcc(sig)
  115. int sig;
  116. {
  117. signal(sig, SIG_IGN);
  118. if (kids != -1) kill(kids, sig);
  119. cleanup(ofile);
  120. cleanup(tmp_file);
  121. exit(1);
  122. }
  123. #define lang_suffix() "c"
  124. #define comp_name() "$H/lib.bin/c_cccompat"
  125. int
  126. lang_opt(str)
  127. char *str;
  128. {
  129. switch(str[1]) {
  130. case '-': /* debug options */
  131. case 'R': /* strict K&R */
  132. case 'w': /* disable warnings */
  133. append(&COMP_FLAGS, str);
  134. return 1;
  135. }
  136. return 0;
  137. }
  138. main(argc, argv)
  139. char *argv[];
  140. {
  141. char *str;
  142. char **argvec;
  143. int count;
  144. char *ext;
  145. register struct arglist *call = &CALL_VEC;
  146. char *file;
  147. char *ldfile;
  148. int compile_cnt = 0;
  149. setbuf(stdout, (char *) 0);
  150. basename(*argv++,ProgCall);
  151. COMP = expand_string(comp_name());
  152. CPP = expand_string(CPP_NAME);
  153. #ifdef vax4
  154. append(&CPP_FLAGS, "-Dvax");
  155. #endif
  156. #ifdef sun3
  157. append(&CPP_FLAGS, "-Dsun");
  158. append(&CPP_FLAGS, "-Dmc68020");
  159. append(&CPP_FLAGS, "-Dmc68000");
  160. #endif
  161. if (signal(SIGHUP, SIG_IGN) != SIG_IGN)
  162. signal(SIGHUP, trapcc);
  163. if (signal(SIGINT, SIG_IGN) != SIG_IGN)
  164. signal(SIGINT, trapcc);
  165. if (signal(SIGQUIT, SIG_IGN) != SIG_IGN)
  166. signal(SIGQUIT, trapcc);
  167. while (--argc > 0) {
  168. if (*(str = *argv++) != '-') {
  169. append(&SRCFILES, str);
  170. continue;
  171. }
  172. if (lang_opt(str)) {
  173. }
  174. else switch (str[1]) {
  175. case 'c': /* stop after producing .o files */
  176. c_flag = 1;
  177. break;
  178. case 'D': /* preprocessor #define */
  179. case 'U': /* preprocessor #undef */
  180. append(&CPP_FLAGS, str);
  181. break;
  182. case 'I': /* include directory */
  183. append(&CPP_FLAGS, str);
  184. break;
  185. case 'g': /* debugger support */
  186. append(&COMP_FLAGS, str);
  187. break;
  188. case 'o': /* target file */
  189. if (argc-- >= 0) {
  190. o_flag = 1;
  191. o_FILE = *argv++;
  192. ext = extension(o_FILE);
  193. if (ext != o_FILE && ! strcmp(ext, lang_suffix())
  194. ) {
  195. error("-o would overwrite %s", o_FILE);
  196. }
  197. }
  198. break;
  199. case 'u': /* mark identifier as undefined */
  200. append(&LD_FLAGS, str);
  201. if (argc-- >= 0)
  202. append(&LD_FLAGS, *argv++);
  203. break;
  204. case 'O': /* use built in peephole optimizer */
  205. O_flag = 1;
  206. break;
  207. case 'v': /* verbose */
  208. v_flag++;
  209. if (str[2] == 'n')
  210. noexec = 1;
  211. break;
  212. case 'l': /* library file */
  213. append(&SRCFILES, str);
  214. break;
  215. case 't': /* -target? */
  216. if (! strcmp(str, "-target")) {
  217. if (argc-- >= 0) argv++;
  218. break;
  219. }
  220. warning("%s flag ignored", str);
  221. break;
  222. case 'M': /* use other compiler (for testing) */
  223. strcpy(COMP, str+2);
  224. break;
  225. case 's': /* strip, -sun3? */
  226. if (! strcmp(str, "-sun3")) {
  227. break;
  228. }
  229. /* fall through */
  230. case 'n': /* text not read-only */
  231. case 'N': /* text read-only */
  232. case 'r': /* relocation produced */
  233. case 'S': /* strip, but leave locals and globals */
  234. if (str[2] == '\0') {
  235. append(&LD_FLAGS, str);
  236. break;
  237. }
  238. /* fall through */
  239. default:
  240. warning("%s flag ignored", str);
  241. break;
  242. }
  243. }
  244. if (ecount) exit(1);
  245. count = SRCFILES.al_argc;
  246. argvec = &(SRCFILES.al_argv[0]);
  247. while (count-- > 0) {
  248. ext = extension(*argvec);
  249. if (*argvec[0] != '-' &&
  250. ext != *argvec++ && (! strcmp(ext, lang_suffix())
  251. )) {
  252. compile_cnt++;
  253. }
  254. }
  255. if (compile_cnt > 1 && c_flag && o_flag) {
  256. warning("-o flag ignored");
  257. o_flag = 0;
  258. }
  259. append(&COMP_FLAGS, "-L");
  260. count = SRCFILES.al_argc;
  261. argvec = &(SRCFILES.al_argv[0]);
  262. while (count-- > 0) {
  263. register char *f;
  264. basename(file = *argvec++, BASE);
  265. ext = extension(file);
  266. if (file[0] != '-' &&
  267. ext != file && (!strcmp(ext, lang_suffix())
  268. )) {
  269. if (compile_cnt > 1) printf("%s\n", file);
  270. ldfile = c_flag ? ofile : alloc((unsigned)strlen(BASE)+3);
  271. if (
  272. !strcmp(ext, "s") &&
  273. needsprep(file)) {
  274. strcpy(tmp_file, TMP_DIR);
  275. strcat(tmp_file, "/F_XXXXXX");
  276. mktemp(tmp_file);
  277. init(call);
  278. append(call, CPP);
  279. concat(call, &CPP_FLAGS);
  280. append(call, file);
  281. if (runvec(call, tmp_file)) {
  282. file = tmp_file;
  283. }
  284. else {
  285. remove(tmp_file);
  286. tmp_file[0] = '\0';
  287. continue;
  288. }
  289. }
  290. init(call);
  291. if (o_flag && c_flag) {
  292. f = o_FILE;
  293. }
  294. else f = mkstr(ldfile, BASE, ".", "o", (char *)0);
  295. if (strcmp(ext, "s")) {
  296. append(call, COMP);
  297. concat(call, &CPP_FLAGS);
  298. concat(call, &COMP_FLAGS);
  299. append(call, file);
  300. append(call, f);
  301. }
  302. else {
  303. append(call, AS_NAME);
  304. append(call, "-o");
  305. append(call, f);
  306. #ifdef sun3
  307. append(call, "-mc68020");
  308. #endif
  309. append(call, file);
  310. }
  311. if (runvec(call, (char *) 0)) {
  312. file = f;
  313. }
  314. else {
  315. remove(f);
  316. continue;
  317. }
  318. cleanup(tmp_file);
  319. tmp_file[0] = '\0';
  320. }
  321. else if (file[0] != '-' &&
  322. strcmp(ext, "o") && strcmp(ext, "a")) {
  323. warning("file with unknown suffix (%s) passed to the loader", ext);
  324. }
  325. if (c_flag)
  326. continue;
  327. append(&LDFILES, file);
  328. }
  329. /* *.s to a.out */
  330. if (RET_CODE == 0 && LDFILES.al_argc > 0) {
  331. init(call);
  332. expand(&LD_HEAD);
  333. expand(&LD_TAIL);
  334. append(call, expand_string(LD_NAME));
  335. concat(call, &LD_FLAGS);
  336. append(call, "-o");
  337. append(call, o_FILE);
  338. concat(call, &LD_HEAD);
  339. concat(call, &LDFILES);
  340. concat(call, &LD_TAIL);
  341. if (! runvec(call, (char *) 0)) {
  342. exit(RET_CODE);
  343. }
  344. }
  345. exit(RET_CODE);
  346. }
  347. needsprep(name)
  348. char *name;
  349. {
  350. int file;
  351. char fc;
  352. file = open(name,0);
  353. if (file < 0) return 0;
  354. if (read(file, &fc, 1) != 1) fc = 0;
  355. close(file);
  356. return fc == '#';
  357. }
  358. char *
  359. alloc(u)
  360. unsigned u;
  361. {
  362. char *p = malloc(u);
  363. if (p == 0)
  364. panic("no space");
  365. return p;
  366. }
  367. char *
  368. expand_string(s)
  369. char *s;
  370. {
  371. char buf[1024];
  372. register char *p = s;
  373. register char *q = &buf[0];
  374. int expanded = 0;
  375. if (!p) return p;
  376. while (*p) {
  377. if (*p == '$') {
  378. p++;
  379. expanded = 1;
  380. switch(*p++) {
  381. case 'H':
  382. strcpy(q, EM_DIR);
  383. break;
  384. case 'M':
  385. strcpy(q, MACHNAME);
  386. break;
  387. case 'S':
  388. strcpy(q, SYSNAME);
  389. break;
  390. default:
  391. panic("internal error");
  392. break;
  393. }
  394. while (*q) q++;
  395. }
  396. else *q++ = *p++;
  397. }
  398. if (! expanded) return s;
  399. *q++ = '\0';
  400. p = alloc((unsigned int) (q - buf));
  401. return strcpy(p, buf);
  402. }
  403. append(al, arg)
  404. register struct arglist *al;
  405. char *arg;
  406. {
  407. if (!arg || !*arg) return;
  408. if (al->al_argc >= MAXARGC)
  409. panic("argument list overflow");
  410. al->al_argv[(al->al_argc)++] = arg;
  411. }
  412. expand(al)
  413. register struct arglist *al;
  414. {
  415. register int i = al->al_argc;
  416. register char **p = &(al->al_argv[0]);
  417. while (i-- > 0) {
  418. *p = expand_string(*p);
  419. p++;
  420. }
  421. }
  422. concat(al1, al2)
  423. struct arglist *al1, *al2;
  424. {
  425. register i = al2->al_argc;
  426. register char **p = &(al1->al_argv[al1->al_argc]);
  427. register char **q = &(al2->al_argv[0]);
  428. if ((al1->al_argc += i) >= MAXARGC)
  429. panic("argument list overflow");
  430. while (i-- > 0) {
  431. *p++ = *q++;
  432. }
  433. }
  434. #if __STDC__
  435. /*VARARGS*/
  436. char *
  437. mkstr(char *dst, ...)
  438. {
  439. va_list ap;
  440. va_start(ap, dst);
  441. {
  442. register char *p;
  443. register char *q;
  444. q = dst;
  445. p = va_arg(ap, char *);
  446. while (p) {
  447. while (*q++ = *p++);
  448. q--;
  449. p = va_arg(ap, char *);
  450. }
  451. }
  452. va_end(ap);
  453. return dst;
  454. }
  455. #else
  456. /*VARARGS*/
  457. char *
  458. mkstr(va_alist)
  459. va_dcl
  460. {
  461. va_list ap;
  462. char *dst;
  463. va_start(ap);
  464. {
  465. register char *p;
  466. register char *q;
  467. dst = q = va_arg(ap, char *);
  468. p = va_arg(ap, char *);
  469. while (p) {
  470. while (*q++ = *p++);
  471. q--;
  472. p = va_arg(ap, char *);
  473. }
  474. }
  475. va_end(ap);
  476. return dst;
  477. }
  478. #endif
  479. basename(str, dst)
  480. char *str;
  481. register char *dst;
  482. {
  483. register char *p1 = str;
  484. register char *p2 = p1;
  485. while (*p1)
  486. if (*p1++ == '/')
  487. p2 = p1;
  488. p1--;
  489. while (*p1 != '.' && p1 >= p2) p1--;
  490. if (p1 >= p2) {
  491. *p1 = '\0';
  492. while (*dst++ = *p2++);
  493. *p1 = '.';
  494. }
  495. else
  496. while (*dst++ = *p2++);
  497. }
  498. char *
  499. extension(fn)
  500. char *fn;
  501. {
  502. register char *c = fn;
  503. while (*c++) ;
  504. while (*--c != '.' && c >= fn) { }
  505. if (c++ < fn || !*c) return fn;
  506. return c;
  507. }
  508. runvec(vec, outp)
  509. struct arglist *vec;
  510. char *outp;
  511. {
  512. int pid, status;
  513. if (v_flag) {
  514. pr_vec(vec);
  515. putc('\n', stderr);
  516. }
  517. if ((pid = fork()) == 0) { /* start up the process */
  518. if (outp) { /* redirect standard output */
  519. close(1);
  520. if (creat(outp, 0666) != 1)
  521. panic("cannot create output file");
  522. }
  523. ex_vec(vec);
  524. }
  525. if (pid == -1)
  526. panic("no more processes");
  527. kids = pid;
  528. wait(&status);
  529. if (status) switch(status & 0177) {
  530. case SIGHUP:
  531. case SIGINT:
  532. case SIGQUIT:
  533. case SIGTERM:
  534. case 0:
  535. break;
  536. default:
  537. error("%s died with signal %d\n", vec->al_argv[1], status&0177);
  538. }
  539. kids = -1;
  540. return status ? ((RET_CODE = 1), 0) : 1;
  541. }
  542. /*VARARGS1*/
  543. error(str, s1, s2)
  544. char *str, *s1, *s2;
  545. {
  546. fprintf(stderr, "%s: ", ProgCall);
  547. fprintf(stderr, str, s1, s2);
  548. putc('\n', stderr);
  549. ecount++;
  550. }
  551. /*VARARGS1*/
  552. warning(str, s1, s2)
  553. char *str, *s1, *s2;
  554. {
  555. fprintf(stderr, "%s: (warning) ", ProgCall);
  556. fprintf(stderr, str, s1, s2);
  557. putc('\n', stderr);
  558. }
  559. panic(str)
  560. char *str;
  561. {
  562. error(str);
  563. trapcc(SIGINT);
  564. }
  565. pr_vec(vec)
  566. register struct arglist *vec;
  567. {
  568. register char **ap = &vec->al_argv[1];
  569. vec->al_argv[vec->al_argc] = 0;
  570. fprintf(stderr, "%s", *ap);
  571. while (*++ap) {
  572. fprintf(stderr, " %s", *ap);
  573. }
  574. }
  575. extern int errno;
  576. ex_vec(vec)
  577. register struct arglist *vec;
  578. {
  579. if (noexec)
  580. exit(0);
  581. vec->al_argv[vec->al_argc] = 0;
  582. execv(vec->al_argv[1], &(vec->al_argv[1]));
  583. if (errno == ENOEXEC) { /* not an a.out, try it with the SHELL */
  584. vec->al_argv[0] = SHELL;
  585. execv(SHELL, &(vec->al_argv[0]));
  586. }
  587. if (access(vec->al_argv[1], 1) == 0) {
  588. /* File is executable. */
  589. error("cannot execute %s", vec->al_argv[1]);
  590. } else {
  591. error("%s is not executable", vec->al_argv[1]);
  592. }
  593. exit(1);
  594. }