hack.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458
  1. /* To be done:
  2. archive members as targets or dependencies.
  3. deletion of targets on interrupts. */
  4. #include <stdio.h>
  5. #include <sys/param.h>
  6. #include <stat.h>
  7. #include <sys/file.h>
  8. #define max(a, b) ((a) > (b) ? (a) : (b))
  9. /* A `struct linebuffer' is a structure which holds a line of text.
  10. `readline' reads a line from a stream into a linebuffer
  11. and works regardless of the length of the line. */
  12. struct linebuffer
  13. {
  14. long size;
  15. char *buffer;
  16. };
  17. struct linebuffer lb1;
  18. /* Structure that represents the info on one file
  19. that the makefile says how to make.
  20. All of these are chained together through `next'. */
  21. struct file
  22. {
  23. struct file *next;
  24. char *name;
  25. struct dep *deps;
  26. struct cmd *cmds;
  27. int double_colon; /* Nonzero for double-colon entry */
  28. int is_target; /* Nonzero if file is described as target */
  29. char *prefix; /* common prefix, if implicit rule has been used */
  30. };
  31. /* Chain of files the makefile knows how to make. */
  32. struct file *files;
  33. /* Structure representing one dependency of a file.
  34. Each struct file's `deps' points to a chain of these,
  35. chained through the `next'.
  36. Note that the first two words of this match a struct nameseq. */
  37. struct dep
  38. {
  39. struct dep *next;
  40. char *name;
  41. struct file *file;
  42. int changed;
  43. };
  44. /* Structure representing one command line for remaking a file.
  45. Each struct file's `cmds' points to a chain of these,
  46. chained through the `next'. */
  47. struct cmd
  48. {
  49. struct cmd *next;
  50. char *line;
  51. };
  52. /* Structure used in chains of names, for parsing and globbing */
  53. struct nameseq
  54. {
  55. struct nameseq *next;
  56. char *name;
  57. };
  58. /* Structure that represents one macro definition.
  59. All these are changed together through `next'. */
  60. struct macro
  61. {
  62. struct macro *next;
  63. char *name;
  64. char *value;
  65. int user; /* Nonzero if specified by command-line arg */
  66. };
  67. /* Chain of all macro definitions. */
  68. struct macro *macros;
  69. /* Pointers to struct macro's for the 4 built-in macros,
  70. $*, $@, $? and $<. */
  71. struct macro *star_macro;
  72. struct macro *at_macro;
  73. struct macro *qmark_macro;
  74. struct macro *less_macro;
  75. /* First file defined in the makefile whose name does not
  76. start with `.'. This is the default to remake if the
  77. command line does not specify. */
  78. struct file *default_goal_file;
  79. /* Pointer to structure for the file .SUFFIXES
  80. whose dependencies are the suffixes to be searched. */
  81. struct file *suffix_file;
  82. /* Pointer to structure for the file .DEFAULT
  83. whose commands are used for any file that has none of its own.
  84. This is zero if the makefiles do not define .DEFAULT. */
  85. struct file *default_file;
  86. /* Nonzero means do not print commands to be executed (-s). */
  87. int silent_flag;
  88. /* Nonzero means just touch the files
  89. that would appear to need remaking (-t) */
  90. int touch_flag;
  91. /* Nonzero means just print what commands would need to be executed,
  92. don't actually execute them (-n). */
  93. int just_print_flag;
  94. /* Print debugging trace info (-d). */
  95. int debug_flag;
  96. /* Nonzero means ignore status codes returned by commands
  97. executed to remake files. Just treat them all as successful (-i). */
  98. int ignore_errors_flag;
  99. /* Nonzero means don't remake anything, just print the data base
  100. that results from reading the makefile (-p). */
  101. int print_data_base_flag;
  102. /* Nonzero means don't remake anything; just return a nonzero status
  103. if the specified targets are not up to date (-q). */
  104. int question_flag;
  105. /* Nonzero means do not use any of the builtin rules (-r). */
  106. int no_builtin_rules_flag;
  107. /* Nonzero means keep going even if remaking some file fails (-k). */
  108. int keep_going_flag;
  109. /* The next two describe the macro output buffer.
  110. This buffer is used to hold the macro-expansion of a line of the makefile.
  111. It is made bigger with realloc whenever it is too small.
  112. macro_buffer_length is the size currently allocated.
  113. macro_buffer is the address of the buffer. */
  114. int macro_buffer_length;
  115. char *macro_buffer;
  116. /* Our working directory */
  117. char wdir[MAXPATHLEN];
  118. /* Forward declarations */
  119. struct macro *define_macro ();
  120. struct macro *lookup_macro ();
  121. char *macro_expand ();
  122. struct file *enter_file ();
  123. struct file *lookup_file ();
  124. struct cmd *store_command_line ();
  125. char *savestring ();
  126. void initbuffer ();
  127. void freebuffer ();
  128. long readline ();
  129. char *concat ();
  130. struct nameseq *parse_file_seq ();
  131. struct nameseq *multi_glob ();
  132. /* Data base of implicit rules */
  133. char *default_suffixes = ".o .c .e .r .f .p .y .yr .ye .l .s";
  134. char *implicit_rules[] =
  135. {
  136. ".c.o",
  137. "$(CC) -c $(CFLAGS) $<",
  138. ".p.o",
  139. "$(PC) -c $(PFLAGS) $<",
  140. ".e.o",
  141. "$(FC) -c $(RFLAGS) $(EFLAGS) $(FFLAGS) $<",
  142. ".r.o",
  143. "$(FC) -c $(RFLAGS) $(EFLAGS) $(FFLAGS) $<",
  144. ".f.o",
  145. "$(FC) -c $(RFLAGS) $(EFLAGS) $(FFLAGS) $<",
  146. ".s.o",
  147. "$(AS) -o $@ $<",
  148. ".yr.r",
  149. "$(YACCR) $(YFLAGS) $< ; mv y.tab.r $@",
  150. ".ye.e",
  151. "$(YACCE) $(YFLAGS) $< ; mv y.tab.e $@",
  152. ".y.c",
  153. "$(YACC) $(YFLAGS) $< ; mv y.tab.c $@",
  154. ".y.o",
  155. "$(YACC) $(YFLAGS) $< ; $(CC) $(CFLAGS) -c y.tab.c ;\
  156. rm y.tab.c ; mv y.tab.o $@",
  157. ".ye.o",
  158. "$(YACCE) $(YFLAGS) $< ; $(EC) $(EFLAGS) -c y.tab.e ;\
  159. rm y.tab.e ; mv y.tab.o $@",
  160. ".yr.o",
  161. "$(YACCR) $(YFLAGS) $< ; $(RC) $(RFLAGS) -c y.tab.r ;\
  162. rm y.tab.r ; mv y.tab.o $@",
  163. ".l.c",
  164. "$(LEX) $(LFLAGS) $< ; mv lex.yy.c $@",
  165. ".l.o",
  166. "$(LEX) $(LFLAGS) $< ; $(CC) $(CFLAGS) -c lex.yy.c; \
  167. rm lex.yy.c; mv lex.yy.o $@",
  168. 0,
  169. };
  170. char *default_macros[] =
  171. {
  172. "AS = as",
  173. "CC = cc",
  174. "FC = fc",
  175. "RC = fc",
  176. "EC = fc",
  177. "PC = pc",
  178. "LEX = lex",
  179. "YACC = yacc",
  180. "YACCR = yacc -r",
  181. "YACCE = yacc -e",
  182. 0,
  183. };
  184. main (argc, argv, envp)
  185. int argc;
  186. char **argv;
  187. char **envp;
  188. {
  189. register struct file *f;
  190. register int i;
  191. int num_remade = 0;
  192. int status = 0;
  193. int this_status;
  194. register char **p;
  195. char *ptr;
  196. files = 0;
  197. macros = 0;
  198. macro_buffer = 0;
  199. getwd (wdir);
  200. /* Search for command line arguments that define macros,
  201. and do the definitions. */
  202. for (i = 1; i < argc; i++)
  203. {
  204. /* Don't even try an arg that is a switch. */
  205. if (!strcmp (argv[i], "-f"))
  206. i++;
  207. else if (argv[i][0] != '-')
  208. {
  209. /* If the arg is a macro definition,
  210. replace it with "-"
  211. so that it will not be taken as a file to remake. */
  212. if (try_macro_definition (argv[i], 1))
  213. argv[i] = "-";
  214. }
  215. }
  216. /* Decode the switches now */
  217. decode_switches (argc, argv);
  218. /* Define the initial list of suffixes */
  219. suffix_file = enter_file (".SUFFIXES");
  220. if (!no_builtin_rules_flag)
  221. {
  222. ptr = default_suffixes;
  223. suffix_file->deps = (struct dep *) parse_file_seq (&ptr, 0,
  224. sizeof (struct dep));
  225. for (p = implicit_rules; *p;)
  226. {
  227. f = enter_file (*p++);
  228. f->cmds = (struct cmd *) xmalloc (sizeof (struct cmd));
  229. f->cmds->line = *p++;
  230. }
  231. for (p = default_macros; *p;)
  232. try_macro_definition (*p++);
  233. }
  234. /* Read all the specified or default makefiles */
  235. read_all_makefiles (argc, argv);
  236. if (lookup_file (".IGNORE"))
  237. ignore_errors_flag = 1;
  238. if (lookup_file (".SILENT"))
  239. silent_flag = 1;
  240. default_file = lookup_file (".DEFAULT");
  241. /* Make each struct dep point at the struct file for the file depended on. */
  242. snap_deps ();
  243. define_automatic_macros ();
  244. /* Search command line for files to remake. */
  245. for (i = 1; i < argc; i++)
  246. {
  247. /* Anything not a switch or an argument of a switch
  248. is a file to be remade.
  249. Note that macro definition args were replaced with
  250. dummies that look like switches. */
  251. if (!strcmp (argv[i], "-f"))
  252. i++;
  253. else if (argv[i][0] != '-')
  254. {
  255. f = enter_file (argv[i]);
  256. num_remade++;
  257. this_status = update_file (f, 1);
  258. if (this_status)
  259. {
  260. status = this_status;
  261. if (!keep_going_flag)
  262. break;
  263. }
  264. }
  265. }
  266. /* Command line did not specify any file to remake => use default. */
  267. if (!num_remade && default_goal_file)
  268. status = update_file (default_goal_file, 1);
  269. if (print_data_base_flag)
  270. print_data_base ();
  271. return status;
  272. }
  273. decode_switches (argc, argv)
  274. int argc;
  275. char **argv;
  276. {
  277. register int i;
  278. register char *sw;
  279. debug_flag = 0;
  280. ignore_errors_flag = 0;
  281. keep_going_flag = 0;
  282. just_print_flag = 0;
  283. print_data_base_flag = 0;
  284. question_flag = 0;
  285. no_builtin_rules_flag = 0;
  286. silent_flag = 0;
  287. touch_flag = 0;
  288. for (i = 1; i < argc; i++)
  289. {
  290. sw = argv[i];
  291. if (strcmp (sw, "-f") && *sw++ == '-')
  292. while (*sw)
  293. switch (*sw++)
  294. {
  295. case 'd':
  296. debug_flag = 1;
  297. break;
  298. case 'i':
  299. ignore_errors_flag = 1;
  300. break;
  301. case 'k':
  302. keep_going_flag = 1;
  303. break;
  304. case 'n':
  305. just_print_flag = 1;
  306. break;
  307. case 'p':
  308. print_data_base_flag = 1;
  309. break;
  310. case 'q':
  311. question_flag = 1;
  312. break;
  313. case 'r':
  314. no_builtin_rules_flag = 1;
  315. break;
  316. case 's':
  317. silent_flag = 1;
  318. break;
  319. case 't':
  320. touch_flag = 1;
  321. break;
  322. default:
  323. fatal ("unknown switch: %c", sw[-1]);
  324. }
  325. }
  326. }
  327. /* Implement macros. */
  328. /* Define macro named NAME with value VALUE.
  329. LENGTH is the length of NAME, which does not
  330. need to be null-terminated.
  331. USER nonzero means this is a definition specified by the
  332. user in the command line; it should not be overridden
  333. by a later definition in a makefile. */
  334. struct macro *
  335. define_macro (name, length, value, user)
  336. char *name, *value;
  337. int length;
  338. int user;
  339. {
  340. register struct macro *m;
  341. register char *s;
  342. m = lookup_macro (name, length);
  343. if (m)
  344. {
  345. if (user || !m->user)
  346. {
  347. m->value = concat (value, "", "");
  348. m->user = user;
  349. }
  350. return m;
  351. }
  352. s = (char *) xmalloc (length + 1);
  353. bcopy (name, s, length);
  354. s[length] = 0;
  355. m = (struct macro *) xmalloc (sizeof (struct macro));
  356. m->name = s;
  357. m->value = concat (value, "", "");
  358. m->user = user;
  359. m->next = macros;
  360. return macros = m;
  361. }
  362. struct macro *
  363. lookup_macro (name, length)
  364. char *name;
  365. int length;
  366. {
  367. register struct macro *m;
  368. for (m = macros; m; m = m->next)
  369. if (!strncmp (m->name, name, length) && m->name[length] == 0)
  370. return m;
  371. return 0;
  372. }
  373. /* Define the automatic macros, and record the addresses
  374. of their structures so we can redefine them quickly. */
  375. define_automatic_macros ()
  376. {
  377. register char *mflags;
  378. star_macro = define_macro ("*", 1, savestring (0, 0), 1);
  379. at_macro = define_macro ("@", 1, savestring (0, 0), 1);
  380. qmark_macro = define_macro ("?", 1, savestring (0, 0), 1);
  381. less_macro = define_macro ("<", 1, savestring (0, 0), 1);
  382. /* Define the built-in macro MFLAGS to contain the command line switches.
  383. We can ignore here those switches that cause commands from the
  384. makefile not to be run. */
  385. mflags = (char *) xmalloc (20);
  386. strcpy (mflags, "-");
  387. if (debug_flag) strcat (mflags, "d");
  388. if (ignore_errors_flag) strcat (mflags, "i");
  389. if (keep_going_flag) strcat (mflags, "k");
  390. if (no_builtin_rules_flag) strcat (mflags, "r");
  391. if (silent_flag) strcat (mflags, "s");
  392. if (mflags[1] == 0) mflags = "";
  393. define_macro ("MFLAGS", 6, mflags, 0);
  394. }
  395. /* Try to interpret LINE (a null-terminated string)
  396. as a macro definition. If it is one, define the macro and return 1.
  397. Otherwise return 0.
  398. USER nonzero means this is a definition specified by the
  399. user in the command line; it should not be overridden
  400. by a later definition in a makefile. */
  401. int
  402. try_macro_definition (line, user)
  403. char *line;
  404. int user;
  405. {
  406. register int c;
  407. register char *p = line;
  408. register char *beg;
  409. register char *end;
  410. while (1)
  411. {
  412. c = *p++;
  413. if (c == 0 || c == '\t' || c == ':' || c == '#')
  414. return 0;
  415. if (c == '=')
  416. break;
  417. }
  418. beg = line;
  419. while (*beg == ' ') beg++;
  420. end = p - 1;
  421. while (end[-1] == ' ') end--;
  422. while (*p == ' ' || *p == '\t') p++;
  423. macro_expand (p);
  424. define_macro (beg, end - beg, macro_buffer, user);
  425. return 1;
  426. }
  427. char *
  428. macro_buffer_output (ptr, string, length)
  429. char *ptr, *string;
  430. int length;
  431. {
  432. register int newlen = length + (ptr - macro_buffer);
  433. register char *new;
  434. if (newlen > macro_buffer_length)
  435. {
  436. macro_buffer_length = max (2 * macro_buffer_length, newlen + 100);
  437. new = (char *) xrealloc (macro_buffer, macro_buffer_length);
  438. ptr += new - macro_buffer;
  439. macro_buffer = new;
  440. }
  441. bcopy (string, ptr, length);
  442. return ptr + length;
  443. }
  444. /* Scan LINE for macro calls. Build in macro_buffer
  445. the result of replacing all macro calls in LINE with the macro values.
  446. Return the address of the resulting string, which is null-terminated
  447. and is only valid until the next time this function is called. */
  448. char *
  449. macro_expand (line)
  450. char *line;
  451. {
  452. register char *p, *o, *p1;
  453. char *p0;
  454. register struct macro *m;
  455. if (!macro_buffer)
  456. {
  457. macro_buffer_length = 500;
  458. macro_buffer = (char *) xmalloc (macro_buffer_length);
  459. }
  460. p = line;
  461. o = macro_buffer;
  462. while (1)
  463. {
  464. p1 = (char *) index (p, '$');
  465. o = macro_buffer_output (o, p, p1 ? p1 - p : strlen (p) + 1);
  466. if (p1 == 0)
  467. break;
  468. p = p1;
  469. p++;
  470. if (*p == '$')
  471. {
  472. o = macro_buffer_output (o, p, 1);
  473. p += 2;
  474. }
  475. else if (*p == '(' || *p == '{')
  476. {
  477. p++;
  478. p0 = p;
  479. for (p1 = p; *p1 && *p1 != ')' && *p1 != '}'; p1++)
  480. ;
  481. p = p1 + 1;
  482. }
  483. else
  484. {
  485. p0 = p;
  486. p1 = ++p;
  487. }
  488. /* p0 is start of macro name, p1 is first character after end of name.
  489. p is advanced past the macro reference. */
  490. m = lookup_macro (p0, p1 - p0);
  491. if (m)
  492. o = macro_buffer_output (o, m->value, strlen (m->value));
  493. }
  494. return macro_buffer;
  495. }
  496. /* Access the list of all file records.
  497. lookup_file given a name, return the struct file * for that name,
  498. or 0 if there is none.
  499. enter_file similar, but create one if there is none. */
  500. struct file *
  501. lookup_file (name)
  502. char *name;
  503. {
  504. register struct file *f = files;
  505. for (; f; f = f->next)
  506. {
  507. if (!strcmp (f->name, name))
  508. return f;
  509. }
  510. return 0;
  511. }
  512. struct file *
  513. enter_file (name)
  514. char *name;
  515. {
  516. register struct file *f = lookup_file (name);
  517. if (f) return f;
  518. f = (struct file *) xmalloc (sizeof (struct file));
  519. f->name = name;
  520. f->cmds = 0;
  521. f->deps = 0;
  522. f->double_colon = 0;
  523. f->prefix = 0;
  524. f->is_target = 0;
  525. f->next = files;
  526. files = f;
  527. return f;
  528. }
  529. /* Print the contents of the files and macros data bases */
  530. print_data_base ()
  531. {
  532. register struct macro *m;
  533. register struct file *f;
  534. register struct dep *d;
  535. register struct cmd *cmd;
  536. printf ("Macros\n\n");
  537. for (m = macros; m; m = m->next)
  538. {
  539. printf ("Macro %s, value is %s\n",
  540. m->name, m->value);
  541. }
  542. printf ("\nFiles\n\n");
  543. for (f = files; f; f = f->next)
  544. {
  545. printf ("File %s:", f->name);
  546. if (f->double_colon)
  547. printf (":");
  548. if (f->prefix)
  549. printf (" (implicit rule prefix %s)", f->prefix);
  550. printf ("\n");
  551. for (d = f->deps; d; d = d->next)
  552. printf (" depends on %s\n", d->name);
  553. if (f->cmds)
  554. printf (" commands to execute:\n");
  555. for (cmd = f->cmds; cmd; cmd = cmd->next)
  556. printf (" %s\n", cmd->line);
  557. }
  558. printf ("\nDone\n");
  559. }
  560. read_all_makefiles (argc, argv)
  561. int argc;
  562. char **argv;
  563. {
  564. register int i;
  565. int num_makefiles = 0;
  566. for (i = 1; i < argc; i++)
  567. {
  568. if (!strcmp (argv[i], "-f"))
  569. {
  570. read_makefile (argv[++i]);
  571. num_makefiles++;
  572. }
  573. }
  574. if (num_makefiles == 0)
  575. {
  576. if (file_mtime ("makefile") >= 0)
  577. read_makefile ("makefile");
  578. else if (file_mtime ("Makefile") >= 0)
  579. read_makefile ("Makefile");
  580. else
  581. fatal ("no arguments or description file");
  582. }
  583. }
  584. read_makefile (filename)
  585. char *filename;
  586. {
  587. register FILE *infile;
  588. struct linebuffer lb;
  589. register char *p;
  590. char *p2;
  591. struct cmd *commands = 0;
  592. /* Last struct cmd attached to current file,
  593. or 0 if none so far. Used for adding new ones to end of chain. */
  594. struct cmd *lastc;
  595. struct nameseq *filenames = 0;
  596. struct dep *deps = 0;
  597. int lineno = 0;
  598. int two_colon;
  599. /* First, get a stream to read */
  600. if (!strcmp (filename, "-"))
  601. infile = stdin;
  602. else
  603. infile = fopen (filename, "r");
  604. if (!infile)
  605. pfatal_with_name (filename);
  606. /* Loop over lines in the file */
  607. initbuffer (&lb);
  608. while (!feof (infile))
  609. {
  610. readline (&lb, infile);
  611. lineno++;
  612. /* Look for a #-comment and discard it if found */
  613. discard_line_comment (lb.buffer);
  614. p = lb.buffer;
  615. while (*p == ' ') p++;
  616. if (try_macro_definition (p, 0))
  617. continue;
  618. else if (*p == 0)
  619. continue;
  620. else if (*p == '\t')
  621. {
  622. /* This line is a shell command */
  623. while (*p && (*p == ' ' || *p == '\t')) p++;
  624. if (!filenames)
  625. fatal ("description file \"%s\" has command before any target file", filename);
  626. lastc = store_command_line (p, &commands, lastc);
  627. }
  628. else
  629. {
  630. /* This line describes some target files */
  631. record_files (filenames, deps, commands, two_colon);
  632. lastc = 0;
  633. p2 = macro_expand (p);
  634. filenames = multi_glob (parse_file_seq (&p2, ':',
  635. sizeof (struct nameseq)),
  636. wdir,
  637. sizeof (struct nameseq));
  638. if (!*p2++)
  639. fatal ("no separator in file \"%s\", line %d", filename, lineno);
  640. /* Is this a one-colon or two-colon entry? */
  641. two_colon = *p2 == ':';
  642. if (two_colon) p2++;
  643. /* Parse the dependencies. */
  644. deps = (struct dep *)
  645. multi_glob (parse_file_seq (&p2, ';', sizeof (struct dep)),
  646. wdir,
  647. sizeof (struct dep));
  648. commands = 0;
  649. /* Did we stop at end of line, or at a semicolon? */
  650. if (*p2 == ';')
  651. {
  652. /* Semicolon means rest of line is a command */
  653. lastc = store_command_line (p2 + 1, &commands, 0);
  654. }
  655. }
  656. }
  657. record_files (filenames, deps, commands, two_colon);
  658. freebuffer (&lb);
  659. if (infile != stdin)
  660. fclose (infile);
  661. }
  662. /* Record a description line for files FILENAMES,
  663. with dependencies DEPS, commands to execute COMMANDS.
  664. TWO_COLON is nonzero if a double colon was used.
  665. The links of FILENAMES are freed, and so are any names in it
  666. that are not incorporated into other data structures. */
  667. record_files (filenames, deps, commands, two_colon)
  668. struct nameseq *filenames;
  669. struct dep *deps;
  670. struct cmd *commands;
  671. int two_colon;
  672. {
  673. register struct file *f;
  674. register char *name;
  675. register struct dep *d;
  676. struct nameseq *nextf;
  677. for (; filenames; filenames = nextf)
  678. {
  679. nextf = filenames->next;
  680. name = filenames->name;
  681. free (filenames);
  682. if (!two_colon)
  683. {
  684. /* Single-colon. Combine these dependencies
  685. with any others in file's existing record, if any. */
  686. f = enter_file (name);
  687. if (f->double_colon)
  688. fatal ("target file \"%s\" has both : and :: entries", f->name);
  689. if (commands && f->cmds)
  690. fatal ("target file \"%s\" has commands specified twice", f->name);
  691. f->cmds = commands;
  692. /* Defining .SUFFIXES with no dependencies
  693. clears out the list of suffixes. */
  694. if (f == suffix_file && deps == 0)
  695. f->deps = 0;
  696. else if (f->deps)
  697. {
  698. for (d = f->deps; d->next; d = d->next);
  699. d->next = deps;
  700. }
  701. else
  702. f->deps = deps;
  703. }
  704. else
  705. {
  706. /* Double-colon. Make a new record even if file has one. */
  707. f = lookup_file (name);
  708. if (f && !f->double_colon)
  709. fatal ("target file \"%s\" has both : and :: entries", f->name);
  710. f = (struct file *) xmalloc (sizeof (struct file));
  711. f->deps = deps;
  712. f->cmds = commands;
  713. f->name = name;
  714. f->next = files;
  715. f->double_colon = 1;
  716. f->prefix = 0;
  717. f->is_target = 0;
  718. files = f;
  719. }
  720. /* Free name if not needed further */
  721. if (name != f->name)
  722. free (name);
  723. /* See if this is first file seen whose name
  724. does not start with a `.'. */
  725. if (!default_goal_file && *name != '.')
  726. default_goal_file = f;
  727. }
  728. }
  729. /* Scan the null-terminated string LINE for a # not quoted.
  730. If one is found, replace it with a null character. */
  731. discard_line_comment (line)
  732. char *line;
  733. {
  734. register char *p = line;
  735. register int c;
  736. register int instring = 0;
  737. while (c = *p++)
  738. {
  739. if (c == '\\')
  740. {
  741. if (!*p++)
  742. break;
  743. }
  744. else if (c == '"')
  745. instring = ~instring;
  746. else if (instring)
  747. continue;
  748. else if (c == '#')
  749. {
  750. p[-1] = 0;
  751. break;
  752. }
  753. }
  754. }
  755. struct cmd *
  756. store_command_line (string, cmds, lastc)
  757. char *string;
  758. struct cmd **cmds;
  759. struct cmd *lastc;
  760. {
  761. register struct cmd *c;
  762. c = (struct cmd *) xmalloc (sizeof (struct cmd));
  763. c->line = savestring (string, strlen (string));
  764. c->next = 0;
  765. if (lastc)
  766. lastc->next = c;
  767. else
  768. *cmds = c;
  769. return c;
  770. }
  771. /* Parse a string into a sequence of filenames
  772. represented as a chain of struct nameseq's in reverse order.
  773. Return that chain.
  774. The string is passed as STRINGP, the address of a string pointer.
  775. The string pointer is updated to point at the first character
  776. not parsed, which either is a null char or equals STOPCHAR.
  777. SIZE is how big to construct chain elements.
  778. This is useful if we want them actually to be other structures
  779. that have room for additional info. */
  780. struct nameseq *
  781. parse_file_seq (stringp, stopchar, size)
  782. char **stringp;
  783. int stopchar;
  784. int size;
  785. {
  786. register struct nameseq *new = 0;
  787. register struct nameseq *new1;
  788. register char *p = *stringp;
  789. char *q;
  790. char *name;
  791. register int c;
  792. struct nameseq *nexto;
  793. while (1)
  794. {
  795. /* Skip whitespace; see if any more names are left. */
  796. while (*p == ' ' || *p == '\t') p++;
  797. if (*p == 0 || *p == stopchar)
  798. break;
  799. /* Yes, find end of next name. */
  800. q = p;
  801. while (1)
  802. {
  803. c = *p++;
  804. if (c == 0 || c == ' ' || c == '\t' || c == stopchar)
  805. break;
  806. }
  807. p--;
  808. /* Extract the filename just found, and skip it. */
  809. name = savestring (q, p - q);
  810. /* Add it to the front of the chain. */
  811. new1 = (struct nameseq *) xmalloc (size);
  812. new1->name = name;
  813. new1->next = new;
  814. new = new1;
  815. }
  816. *stringp = p;
  817. return new;
  818. }
  819. alpha_compare (s1, s2)
  820. char **s1, **s2;
  821. {
  822. return strcmp (*s1, *s2);
  823. }
  824. /* Given a chain of struct nameseq's describing a sequence of filenames,
  825. in reverse of the intended order,
  826. return a new chain describing the result of globbing the filenames.
  827. The new chain is in forward order.
  828. The links of the old chain are freed or used in the new chain.
  829. Likewise for the names in the old chain.
  830. Globbing is done in directory DIR.
  831. SIZE is how big to construct chain elements.
  832. This is useful if we want them actually to be other structures
  833. that have room for additional info. */
  834. struct nameseq *
  835. multi_glob (chain, dir, size)
  836. struct nameseq *chain;
  837. char *dir;
  838. int size;
  839. {
  840. register struct nameseq *new = 0;
  841. register struct nameseq *tem;
  842. register struct nameseq *old;
  843. register char **vector;
  844. register int i;
  845. int length;
  846. extern char **glob_vector ();
  847. struct nameseq *nexto;
  848. for (old = chain; old; old = nexto)
  849. {
  850. nexto = old->next;
  851. if (glob_pattern_p (old->name))
  852. {
  853. vector = glob_vector (old->name, dir);
  854. free (old->name);
  855. for (i = 0; vector[i]; i++);
  856. length = i;
  857. qsort (vector, length, sizeof (char *), alpha_compare);
  858. for (i = length - 1; i >= 0; i--)
  859. {
  860. tem = (struct nameseq *) xmalloc (size);
  861. tem->name = vector[i];
  862. tem->next = new;
  863. new = tem;
  864. }
  865. free (vector);
  866. free (old);
  867. }
  868. else
  869. {
  870. old->next = new;
  871. new = old;
  872. }
  873. }
  874. return new;
  875. }
  876. /* For each dependency of each file, make it point at the
  877. file that it depends on. */
  878. snap_deps ()
  879. {
  880. register struct file *f;
  881. register struct dep *d;
  882. for (f = files; f; f = f->next)
  883. for (d = f->deps; d; d = d->next)
  884. d->file = enter_file (d->name);
  885. }
  886. /* If FILE is not up to date, execute the commands for it.
  887. Return 0 if successful, 1 if unsuccessful;
  888. but with some flag settings, just call `exit' if unsuccessful.
  889. TOPLEVEL is nonzero if this file is a top level goal;
  890. in that case, if nothing needs to be done, say so. */
  891. int
  892. update_file (file, toplevel)
  893. struct file *file;
  894. int toplevel;
  895. {
  896. register struct dep *d;
  897. register int thisdate;
  898. register int must_redo;
  899. int dep_status = 0;
  900. int status;
  901. int noerror;
  902. int noprint;
  903. register char *line;
  904. int dep_size = 0;
  905. register struct cmd *cmd;
  906. if (debug_flag)
  907. printf ("Considering target file \"%s\".\n", file->name);
  908. if (file->cmds == 0 && file->is_target)
  909. if (!try_implicit_rule (file))
  910. {
  911. if (default_file)
  912. file->cmds = default_file->cmds;
  913. else
  914. fatal ("no specification for making file \"%s\"", file->name);
  915. }
  916. /* Update all the files we depend on, if necessary. */
  917. for (d = file->deps; d; d = d->next)
  918. {
  919. dep_status |= update_file (d->file, 0);
  920. if (dep_status && !keep_going_flag)
  921. {
  922. if (debug_flag)
  923. printf ("Giving up on target file \"%s\".\n", file->name);
  924. return dep_status;
  925. }
  926. }
  927. if (dep_status || print_data_base_flag)
  928. {
  929. if (debug_flag)
  930. printf ("Giving up on target file \"%s\".\n", file->name);
  931. return dep_status;
  932. }
  933. if (debug_flag)
  934. printf ("Finished dependencies of target file \"%s\".\n", file->name);
  935. thisdate = file_mtime (file->name);
  936. /* Update if any file depended on is more recent than this file,
  937. or if this file does not exist.
  938. Must in any case check all of the dependencies
  939. so we can create $@. */
  940. must_redo = thisdate < 0;
  941. for (d = file->deps; d; d = d->next)
  942. {
  943. d->changed = thisdate < 0 || thisdate < file_mtime (d->name);
  944. if (debug_flag)
  945. printf ("File \"%s\" is %s than file \"%s\".\n",
  946. file->name, d->changed ? "older" : "newer", d->name);
  947. if (d->changed)
  948. {
  949. must_redo = 1;
  950. dep_size += strlen (d->name) + 1;
  951. }
  952. }
  953. if (debug_flag && must_redo)
  954. printf ("Must remake target file \"%s\".\n", file->name);
  955. /* If we find the file needs to be remade,
  956. execute its commands one by one. */
  957. if (question_flag)
  958. return must_redo;
  959. if (must_redo && touch_flag)
  960. {
  961. /* Should set file's modification date and do nothing else. */
  962. printf ("touch(%s)\n", file->name);
  963. close (open (file->name, O_APPEND + O_CREAT, 0666));
  964. }
  965. else if (must_redo)
  966. {
  967. /* First set the automatic macros according to this file */
  968. at_macro->value = file->name;
  969. star_macro->value = file->prefix ? file->prefix : "";
  970. less_macro->value = file->prefix ? file->deps->name : "";
  971. line = (char *) xmalloc (dep_size + 1);
  972. line[0] = 0;
  973. for (d = file->deps; d; d = d->next)
  974. {
  975. if (d->changed)
  976. {
  977. strcat (line, d->name);
  978. strcat (line, " ");
  979. }
  980. }
  981. line[strlen (line) - 1] = 0;
  982. qmark_macro->value = line;
  983. /* Now execute the command lines */
  984. for (cmd = file->cmds; cmd; cmd = cmd->next)
  985. {
  986. macro_expand (cmd->line);
  987. line = macro_buffer;
  988. noprint = 0;
  989. noerror = 0;
  990. /* Print each line that does not start with `@',
  991. unless -s was specified. */
  992. while (*line)
  993. {
  994. if (*line == '@')
  995. noprint = 1;
  996. else if (*line == '-')
  997. noerror = 1;
  998. else if (*line == ' ' || *line == '\t')
  999. ;
  1000. else
  1001. break;
  1002. line++;
  1003. }
  1004. if (!silent_flag && (!noprint || just_print_flag))
  1005. printf ("%s\n", line);
  1006. /* If -n was specified, don't really execute. */
  1007. if (just_print_flag)
  1008. continue;
  1009. status = system (line);
  1010. if (status && !ignore_errors_flag && !noerror)
  1011. return status;
  1012. }
  1013. /* Now free the macro values made above */
  1014. free (qmark_macro->value);
  1015. qmark_macro->value = "";
  1016. }
  1017. else if (toplevel && !silent_flag)
  1018. printf ("File '%s' is up to date.\n", file->name);
  1019. return 0;
  1020. }
  1021. /* For a FILE which has no commands specified, try to figure out some
  1022. from the implicit rules and suffix list.
  1023. Returns 1 if a suitable implicit rule was found,
  1024. after modifying FILE to contain the appropriate commands and deps,
  1025. or returns 0 if no implicit rule was found. */
  1026. int
  1027. try_implicit_rule (file)
  1028. struct file *file;
  1029. {
  1030. register struct dep *d;
  1031. register int flen = strlen (file->name);
  1032. register int len;
  1033. char *rulename;
  1034. struct file *rulefile;
  1035. char *prefix;
  1036. char *depname;
  1037. /* Try each defined suffix, looking for one this file has. */
  1038. for (d = suffix_file->deps; d; d = d->next)
  1039. {
  1040. len = strlen (d->name);
  1041. if (!strcmp (file->name + flen - len, d->name))
  1042. break;
  1043. }
  1044. if (!d) return 0;
  1045. /* Found one; now extract the name sans suffix. */
  1046. prefix = savestring (file->name, flen - len);
  1047. /* Now try the remaining suffixes, looking for one for which
  1048. there is an existing file and a rule for making this one from it. */
  1049. for (d = d->next; d; d = d->next)
  1050. {
  1051. depname = concat (star_macro->value, d->name, "");
  1052. if (file_mtime (less_macro->value) >= 0)
  1053. {
  1054. rulename = concat (d->name, file->name + flen - len, "");
  1055. rulefile = lookup_file (rulename);
  1056. free (rulename);
  1057. if (rulefile)
  1058. break;
  1059. }
  1060. free (depname);
  1061. }
  1062. if (!rulefile)
  1063. {
  1064. free (prefix);
  1065. return 0;
  1066. }
  1067. /* We found the implicit rule. Stick it into FILE. */
  1068. file->cmds = rulefile->cmds;
  1069. d = (struct dep *) xmalloc (sizeof (struct dep));
  1070. d->name = depname;
  1071. d->file = enter_file (depname);
  1072. d->next = file->deps;
  1073. file->deps = d;
  1074. file->prefix = prefix;
  1075. return 1;
  1076. }
  1077. /* Initialize a linebuffer for use */
  1078. void
  1079. initbuffer (linebuffer)
  1080. struct linebuffer *linebuffer;
  1081. {
  1082. linebuffer->size = 200;
  1083. linebuffer->buffer = (char *) xmalloc (200);
  1084. }
  1085. void
  1086. freebuffer (linebuffer)
  1087. struct linebuffer *linebuffer;
  1088. {
  1089. free (linebuffer->buffer);
  1090. }
  1091. /* Read a line of text from `stream' into `linebuffer'.
  1092. Return the length of the line.
  1093. Combine continuation lines into one line. */
  1094. long
  1095. readline (linebuffer, stream)
  1096. struct linebuffer *linebuffer;
  1097. FILE *stream;
  1098. {
  1099. char *buffer = linebuffer->buffer;
  1100. register char *p = linebuffer->buffer;
  1101. register char *end = p + linebuffer->size;
  1102. register char *p1;
  1103. register int c;
  1104. while (1)
  1105. {
  1106. c = getc (stream);
  1107. if (p == end)
  1108. {
  1109. buffer = (char *) xrealloc (buffer, linebuffer->size *= 2);
  1110. p += buffer - linebuffer->buffer;
  1111. end += buffer - linebuffer->buffer;
  1112. linebuffer->buffer = buffer;
  1113. }
  1114. if (c < 0)
  1115. break;
  1116. if (c == '\n')
  1117. {
  1118. p1 = p;
  1119. while (p1 != buffer && p1[-1] == '\\') p1--;
  1120. if ((p1 - p) & 1)
  1121. {
  1122. /* Line ends with odd number of backslashes -- continuation! */
  1123. /* Discard the backslash that means continuation. */
  1124. p--;
  1125. /* Discard whitespace at start of next line. */
  1126. while ((c = getc (stream)) == ' ' || c == '\t');
  1127. ungetc (c, stream);
  1128. /* Replace it all with one space. */
  1129. c = ' ';
  1130. }
  1131. else
  1132. /* Line not continued. Return what we have so far. */
  1133. break;
  1134. }
  1135. *p++ = c;
  1136. }
  1137. *p = 0;
  1138. return p - buffer;
  1139. }
  1140. int
  1141. file_mtime (name)
  1142. char *name;
  1143. {
  1144. struct stat st;
  1145. if (stat (name, &st) < 0)
  1146. return -1;
  1147. return st.st_mtime;
  1148. }
  1149. /* Return a newly-allocated string whose contents concatenate those of s1, s2, s3. */
  1150. char *
  1151. concat (s1, s2, s3)
  1152. char *s1, *s2, *s3;
  1153. {
  1154. int len1 = strlen (s1), len2 = strlen (s2), len3 = strlen (s3);
  1155. char *result = (char *) xmalloc (len1 + len2 + len3 + 1);
  1156. strcpy (result, s1);
  1157. strcpy (result + len1, s2);
  1158. strcpy (result + len1 + len2, s3);
  1159. *(result + len1 + len2 + len3) = 0;
  1160. return result;
  1161. }
  1162. /* Print error message and exit. */
  1163. fatal (s1, s2, s3)
  1164. char *s1, *s2, *s3;
  1165. {
  1166. printf ("make: ");
  1167. printf (s1, s2, s3);
  1168. printf (". Stop.\n");
  1169. exit (1);
  1170. }
  1171. /* Print error message. `s1' is printf control string, `s2' is arg for it. */
  1172. error (s1, s2)
  1173. char *s1, *s2;
  1174. {
  1175. printf ("make: ");
  1176. printf (s1, s2);
  1177. printf ("\n");
  1178. }
  1179. pfatal_with_name (name)
  1180. char *name;
  1181. {
  1182. extern int errno, sys_nerr;
  1183. extern char *sys_errlist[];
  1184. char *s;
  1185. if (errno < sys_nerr)
  1186. s = concat ("", sys_errlist[errno], " for %s");
  1187. else
  1188. s = "cannot open %s";
  1189. fatal (s, name);
  1190. }
  1191. /* Like malloc but get fatal error if memory is exhausted. */
  1192. int
  1193. xmalloc (size)
  1194. int size;
  1195. {
  1196. int result = malloc (size);
  1197. if (!result)
  1198. fatal ("virtual memory exhausted", 0);
  1199. return result;
  1200. }
  1201. int
  1202. xrealloc (ptr, size)
  1203. char *ptr;
  1204. int size;
  1205. {
  1206. int result = realloc (ptr, size);
  1207. if (!result)
  1208. fatal ("virtual memory exhausted");
  1209. return result;
  1210. }
  1211. char *
  1212. savestring (string, length)
  1213. char *string;
  1214. int length;
  1215. {
  1216. register char *out = (char *) xmalloc (length + 1);
  1217. bcopy (string, out, length);
  1218. out[length] = 0;
  1219. return out;
  1220. }