/usr.bin/yacc/main.c

https://bitbucket.org/freebsd/freebsd-head/ · C · 438 lines · 336 code · 61 blank · 41 comment · 99 complexity · ac53961b2f52d5dda8ce567d4c5f1df8 MD5 · raw file

  1. /*
  2. * Copyright (c) 1989 The Regents of the University of California.
  3. * All rights reserved.
  4. *
  5. * This code is derived from software contributed to Berkeley by
  6. * Robert Paul Corbett.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in the
  15. * documentation and/or other materials provided with the distribution.
  16. * 4. Neither the name of the University nor the names of its contributors
  17. * may be used to endorse or promote products derived from this software
  18. * without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  21. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  22. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  23. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  24. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  25. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  26. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  27. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  28. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  29. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  30. * SUCH DAMAGE.
  31. */
  32. #if 0
  33. #ifndef lint
  34. static char sccsid[] = "@(#)main.c 5.5 (Berkeley) 5/24/93";
  35. #endif
  36. #endif
  37. #include <sys/cdefs.h>
  38. __FBSDID("$FreeBSD$");
  39. #include <paths.h>
  40. #include <signal.h>
  41. #include <stdlib.h>
  42. #include <string.h>
  43. #include <unistd.h>
  44. #include "defs.h"
  45. char dflag;
  46. char lflag;
  47. char rflag;
  48. char tflag;
  49. char vflag;
  50. const char *symbol_prefix;
  51. const char *file_prefix = "y";
  52. char temp_form[] = "yacc.XXXXXXXXXXX";
  53. int lineno;
  54. int outline;
  55. char *action_file_name;
  56. char *code_file_name;
  57. char *defines_file_name;
  58. const char *input_file_name = "";
  59. char *output_file_name;
  60. char *text_file_name;
  61. char *union_file_name;
  62. char *verbose_file_name;
  63. FILE *action_file; /* a temp file, used to save actions associated */
  64. /* with rules until the parser is written */
  65. FILE *code_file; /* y.code.c (used when the -r option is specified) */
  66. FILE *defines_file; /* y.tab.h */
  67. FILE *input_file; /* the input file */
  68. FILE *output_file; /* y.tab.c */
  69. FILE *text_file; /* a temp file, used to save text until all */
  70. /* symbols have been defined */
  71. FILE *union_file; /* a temp file, used to save the union */
  72. /* definition until all symbol have been */
  73. /* defined */
  74. FILE *verbose_file; /* y.output */
  75. int nitems;
  76. int nrules;
  77. int nsyms;
  78. int ntokens;
  79. int nvars;
  80. int start_symbol;
  81. char **symbol_name;
  82. short *symbol_value;
  83. short *symbol_prec;
  84. char *symbol_assoc;
  85. short *ritem;
  86. short *rlhs;
  87. short *rrhs;
  88. short *rprec;
  89. char *rassoc;
  90. short **derives;
  91. char *nullable;
  92. static void create_file_names(void);
  93. static void getargs(int, char **);
  94. static void onintr(int);
  95. static void open_files(void);
  96. static void set_signals(void);
  97. static void usage(void);
  98. volatile sig_atomic_t sigdie;
  99. __dead2 void
  100. done(int k)
  101. {
  102. if (action_file) { fclose(action_file); unlink(action_file_name); }
  103. if (text_file) { fclose(text_file); unlink(text_file_name); }
  104. if (union_file) { fclose(union_file); unlink(union_file_name); }
  105. if (sigdie) { _exit(k); }
  106. exit(k);
  107. }
  108. static void
  109. onintr(int signo __unused)
  110. {
  111. sigdie = 1;
  112. done(1);
  113. }
  114. static void
  115. set_signals(void)
  116. {
  117. #ifdef SIGINT
  118. if (signal(SIGINT, SIG_IGN) != SIG_IGN)
  119. signal(SIGINT, onintr);
  120. #endif
  121. #ifdef SIGTERM
  122. if (signal(SIGTERM, SIG_IGN) != SIG_IGN)
  123. signal(SIGTERM, onintr);
  124. #endif
  125. #ifdef SIGHUP
  126. if (signal(SIGHUP, SIG_IGN) != SIG_IGN)
  127. signal(SIGHUP, onintr);
  128. #endif
  129. }
  130. static void
  131. usage(void)
  132. {
  133. fprintf(stderr, "%s\n%s\n",
  134. "usage: yacc [-dlrtv] [-b file_prefix] [-o output_filename]",
  135. " [-p symbol_prefix] filename");
  136. exit(1);
  137. }
  138. static void
  139. getargs(int argc, char *argv[])
  140. {
  141. int ch;
  142. while ((ch = getopt(argc, argv, "b:dlo:p:rtvy")) != -1)
  143. {
  144. switch (ch)
  145. {
  146. case 'b':
  147. file_prefix = optarg;
  148. break;
  149. case 'd':
  150. dflag = 1;
  151. break;
  152. case 'l':
  153. lflag = 1;
  154. break;
  155. case 'o':
  156. output_file_name = optarg;
  157. break;
  158. case 'p':
  159. symbol_prefix = optarg;
  160. break;
  161. case 'r':
  162. rflag = 1;
  163. break;
  164. case 't':
  165. tflag = 1;
  166. break;
  167. case 'v':
  168. vflag = 1;
  169. break;
  170. case 'y':
  171. /* for bison compatibility -- byacc is already POSIX compatible */
  172. break;
  173. default:
  174. usage();
  175. }
  176. }
  177. if (optind + 1 != argc)
  178. usage();
  179. if (strcmp(argv[optind], "-") == 0)
  180. input_file = stdin;
  181. else
  182. input_file_name = argv[optind];
  183. }
  184. void *
  185. allocate(size_t n)
  186. {
  187. void *p;
  188. p = NULL;
  189. if (n)
  190. {
  191. p = calloc(1, n);
  192. if (!p) no_space();
  193. }
  194. return (p);
  195. }
  196. static void
  197. create_file_names(void)
  198. {
  199. int i, len;
  200. const char *tmpdir;
  201. if (!(tmpdir = getenv("TMPDIR")))
  202. tmpdir = _PATH_TMP;
  203. len = strlen(tmpdir);
  204. i = len + strlen(temp_form) + 1;
  205. if (len && tmpdir[len-1] != '/')
  206. ++i;
  207. action_file_name = malloc(i);
  208. if (action_file_name == 0) no_space();
  209. text_file_name = malloc(i);
  210. if (text_file_name == 0) no_space();
  211. union_file_name = malloc(i);
  212. if (union_file_name == 0) no_space();
  213. strcpy(action_file_name, tmpdir);
  214. strcpy(text_file_name, tmpdir);
  215. strcpy(union_file_name, tmpdir);
  216. if (len && tmpdir[len - 1] != '/')
  217. {
  218. action_file_name[len] = '/';
  219. text_file_name[len] = '/';
  220. union_file_name[len] = '/';
  221. ++len;
  222. }
  223. strcpy(action_file_name + len, temp_form);
  224. strcpy(text_file_name + len, temp_form);
  225. strcpy(union_file_name + len, temp_form);
  226. action_file_name[len + 5] = 'a';
  227. text_file_name[len + 5] = 't';
  228. union_file_name[len + 5] = 'u';
  229. if (output_file_name != 0)
  230. {
  231. file_prefix = output_file_name;
  232. len = strlen(file_prefix);
  233. }
  234. else
  235. {
  236. len = strlen(file_prefix);
  237. output_file_name = malloc(len + 7);
  238. if (output_file_name == 0)
  239. no_space();
  240. strcpy(output_file_name, file_prefix);
  241. strcpy(output_file_name + len, OUTPUT_SUFFIX);
  242. }
  243. if (rflag)
  244. {
  245. code_file_name = malloc(len + 8);
  246. if (code_file_name == 0)
  247. no_space();
  248. strcpy(code_file_name, file_prefix);
  249. if (file_prefix == output_file_name)
  250. {
  251. /*
  252. * XXX ".tab.c" here is OUTPUT_SUFFIX, but since its length is
  253. * in various magic numbers, don't bother using the macro.
  254. */
  255. if (len >= 6 && strcmp(code_file_name + len - 6, ".tab.c") == 0)
  256. strcpy(code_file_name + len - 6, CODE_SUFFIX);
  257. else if (len >= 2 && strcmp(code_file_name + len - 2, ".c") == 0)
  258. strcpy(code_file_name + len - 2, CODE_SUFFIX);
  259. else
  260. strcpy(code_file_name + len, CODE_SUFFIX);
  261. }
  262. else
  263. strcpy(code_file_name + len, CODE_SUFFIX);
  264. }
  265. else
  266. code_file_name = output_file_name;
  267. if (dflag)
  268. {
  269. defines_file_name = malloc(len + 7);
  270. if (defines_file_name == 0)
  271. no_space();
  272. strcpy(defines_file_name, file_prefix);
  273. if (file_prefix == output_file_name)
  274. {
  275. #define BISON_DEFINES_SUFFIX ".h"
  276. if (len >= 2 && strcmp(defines_file_name + len - 2, ".c") == 0)
  277. strcpy(defines_file_name + len - 2, BISON_DEFINES_SUFFIX);
  278. else
  279. strcpy(defines_file_name + len, BISON_DEFINES_SUFFIX);
  280. }
  281. else
  282. strcpy(defines_file_name + len, DEFINES_SUFFIX);
  283. }
  284. if (vflag)
  285. {
  286. verbose_file_name = malloc(len + 8);
  287. if (verbose_file_name == 0)
  288. no_space();
  289. strcpy(verbose_file_name, file_prefix);
  290. if (file_prefix == output_file_name)
  291. {
  292. if (len >= 6 && strcmp(verbose_file_name + len - 6, ".tab.c") == 0)
  293. strcpy(verbose_file_name + len - 6, VERBOSE_SUFFIX);
  294. else if (len >= 2 && strcmp(verbose_file_name + len - 2, ".c") == 0)
  295. strcpy(verbose_file_name + len - 2, VERBOSE_SUFFIX);
  296. else
  297. strcpy(verbose_file_name + len, VERBOSE_SUFFIX);
  298. }
  299. else
  300. strcpy(verbose_file_name + len, VERBOSE_SUFFIX);
  301. }
  302. }
  303. static void
  304. open_files(void)
  305. {
  306. int fd;
  307. create_file_names();
  308. if (input_file == 0)
  309. {
  310. input_file = fopen(input_file_name, "r");
  311. if (input_file == 0)
  312. open_error(input_file_name);
  313. }
  314. fd = mkstemp(action_file_name);
  315. if (fd < 0 || (action_file = fdopen(fd, "w")) == NULL) {
  316. if (fd >= 0)
  317. close(fd);
  318. open_error(action_file_name);
  319. }
  320. fd = mkstemp(text_file_name);
  321. if (fd < 0 || (text_file = fdopen(fd, "w")) == NULL) {
  322. if (fd >= 0)
  323. close(fd);
  324. open_error(text_file_name);
  325. }
  326. fd = mkstemp(union_file_name);
  327. if (fd < 0 || (union_file = fdopen(fd, "w")) == NULL) {
  328. if (fd >= 0)
  329. close(fd);
  330. open_error(union_file_name);
  331. }
  332. text_file = fopen(text_file_name, "w");
  333. if (text_file == 0)
  334. open_error(text_file_name);
  335. if (vflag)
  336. {
  337. verbose_file = fopen(verbose_file_name, "w");
  338. if (verbose_file == 0)
  339. open_error(verbose_file_name);
  340. }
  341. if (dflag)
  342. {
  343. defines_file = fopen(defines_file_name, "w");
  344. if (defines_file == 0)
  345. open_error(defines_file_name);
  346. union_file = fopen(union_file_name, "w");
  347. if (union_file == 0)
  348. open_error(union_file_name);
  349. }
  350. output_file = fopen(output_file_name, "w");
  351. if (output_file == 0)
  352. open_error(output_file_name);
  353. if (rflag)
  354. {
  355. code_file = fopen(code_file_name, "w");
  356. if (code_file == 0)
  357. open_error(code_file_name);
  358. }
  359. else
  360. code_file = output_file;
  361. }
  362. int
  363. main(int argc, char *argv[])
  364. {
  365. set_signals();
  366. getargs(argc, argv);
  367. open_files();
  368. reader();
  369. lr0();
  370. lalr();
  371. make_parser();
  372. verbose();
  373. output();
  374. done(0);
  375. /*NOTREACHED*/
  376. return (0);
  377. }