Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

448 lines
9.3 KiB

  1. #include <signal.h>
  2. #include "defs.h"
  3. char dflag;
  4. char lflag;
  5. char rflag;
  6. char tflag;
  7. char vflag;
  8. char *symbol_prefix;
  9. char *file_prefix = "y";
  10. char *myname = "yacc";
  11. char *temp_form = "yacc.XXXXXXX";
  12. #ifdef TRIPLISH
  13. char *parser_name = "sql";
  14. int ParserChoice = eSQLParser;
  15. #endif
  16. #if defined(KYLEP_CHANGE)
  17. char *baseclass = 0;
  18. char *ctorargs = 0;
  19. #endif // KYLEP_CHANGE
  20. int lineno;
  21. int outline;
  22. char *action_file_name;
  23. char *code_file_name;
  24. char *defines_file_name;
  25. char *input_file_name = "";
  26. char *output_file_name;
  27. char *text_file_name;
  28. char *union_file_name;
  29. char *verbose_file_name;
  30. FILE *action_file; /* a temp file, used to save actions associated */
  31. /* with rules until the parser is written */
  32. FILE *code_file; /* y.code.c (used when the -r option is specified) */
  33. FILE *defines_file; /* y.tab.h */
  34. FILE *input_file; /* the input file */
  35. FILE *output_file; /* y.tab.c */
  36. FILE *text_file; /* a temp file, used to save text until all */
  37. /* symbols have been defined */
  38. FILE *union_file; /* a temp file, used to save the union */
  39. /* definition until all symbol have been */
  40. /* defined */
  41. FILE *verbose_file; /* y.output */
  42. int nitems;
  43. int nrules;
  44. int nsyms;
  45. int ntokens;
  46. int nvars;
  47. int start_symbol;
  48. char **symbol_name;
  49. short *symbol_value;
  50. short *symbol_prec;
  51. char *symbol_assoc;
  52. short *ritem;
  53. short *rlhs;
  54. short *rrhs;
  55. short *rprec;
  56. char *rassoc;
  57. short **derives;
  58. char *nullable;
  59. #if !defined(KYLEP_CHANGE)
  60. extern char *mktemp();
  61. extern char *getenv();
  62. #endif
  63. done(k)
  64. int k;
  65. {
  66. if (action_file) { fclose(action_file); unlink(action_file_name); }
  67. if (text_file) { fclose(text_file); unlink(text_file_name); }
  68. if (union_file) { fclose(union_file); unlink(union_file_name); }
  69. exit(k);
  70. }
  71. #if defined(KYLEP_CHANGE)
  72. // without this declaration compilation fails
  73. void __cdecl onintr(int);
  74. #endif
  75. #if defined(KYLEP_CHANGE)
  76. void __cdecl
  77. onintr(k)
  78. int k;
  79. #else
  80. onintr()
  81. #endif // KYLEP_CHANGE
  82. {
  83. done(1);
  84. }
  85. set_signals()
  86. {
  87. #ifdef SIGINT
  88. if (signal(SIGINT, SIG_IGN) != SIG_IGN)
  89. signal(SIGINT, onintr);
  90. #endif
  91. #ifdef SIGTERM
  92. if (signal(SIGTERM, SIG_IGN) != SIG_IGN)
  93. signal(SIGTERM, onintr);
  94. #endif
  95. #ifdef SIGHUP
  96. if (signal(SIGHUP, SIG_IGN) != SIG_IGN)
  97. signal(SIGHUP, onintr);
  98. #endif
  99. }
  100. usage()
  101. {
  102. #ifdef TRIPLISH
  103. fprintf(stderr, "usage: %s [-f sql/triplish] [-dlrtv] [-b file_prefix] [-p symbol_prefix] [-c baseclass <args>] filename\n", myname);
  104. #else
  105. fprintf(stderr, "usage: %s [-dlrtv] [-b file_prefix] [-p symbol_prefix] [-c baseclass <args>] filename\n", myname);
  106. #endif
  107. exit(1);
  108. }
  109. #if defined(KYLEP_CHANGE)
  110. void
  111. #endif
  112. getargs(argc, argv)
  113. int argc;
  114. char *argv[];
  115. {
  116. register int i;
  117. register char *s;
  118. if (argc > 0) myname = argv[0];
  119. for (i = 1; i < argc; ++i)
  120. {
  121. s = argv[i];
  122. if (*s != '-') break;
  123. switch (*++s)
  124. {
  125. case '\0':
  126. input_file = stdin;
  127. if (i + 1 < argc) usage();
  128. return;
  129. case '-':
  130. ++i;
  131. goto no_more_options;
  132. #ifdef TRIPLISH
  133. case 'f':
  134. if (*++s)
  135. parser_name = s;
  136. else if (++i < argc)
  137. parser_name = argv[i];
  138. else
  139. usage();
  140. if ( 0 == _stricmp( "sql", parser_name ) )
  141. ParserChoice = eSQLParser;
  142. else if ( 0 == _stricmp( "triplish", parser_name ) )
  143. ParserChoice = eTriplishParser;
  144. else
  145. usage();
  146. continue;
  147. #endif
  148. case 'b':
  149. if (*++s)
  150. file_prefix = s;
  151. else if (++i < argc)
  152. file_prefix = argv[i];
  153. else
  154. usage();
  155. continue;
  156. #if defined(KYLEP_CHANGE)
  157. case 'c':
  158. if ( ++i < argc )
  159. {
  160. baseclass = argv[i];
  161. if ( ++i < argc )
  162. ctorargs = argv[i];
  163. else
  164. usage();
  165. }
  166. else
  167. usage();
  168. break;
  169. #endif
  170. case 'd':
  171. dflag = 1;
  172. break;
  173. case 'l':
  174. lflag = 1;
  175. break;
  176. case 'p':
  177. if (*++s)
  178. symbol_prefix = s;
  179. else if (++i < argc)
  180. symbol_prefix = argv[i];
  181. else
  182. usage();
  183. continue;
  184. case 'r':
  185. rflag = 1;
  186. break;
  187. case 't':
  188. tflag = 1;
  189. break;
  190. case 'v':
  191. vflag = 1;
  192. break;
  193. default:
  194. usage();
  195. }
  196. for (;;)
  197. {
  198. switch (*++s)
  199. {
  200. case '\0':
  201. goto end_of_option;
  202. case 'd':
  203. dflag = 1;
  204. break;
  205. case 'l':
  206. lflag = 1;
  207. break;
  208. case 'r':
  209. rflag = 1;
  210. break;
  211. case 't':
  212. tflag = 1;
  213. break;
  214. case 'v':
  215. vflag = 1;
  216. break;
  217. default:
  218. usage();
  219. }
  220. }
  221. end_of_option:;
  222. }
  223. no_more_options:;
  224. if (i + 1 != argc) usage();
  225. input_file_name = argv[i];
  226. }
  227. char *
  228. allocate(n)
  229. unsigned n;
  230. {
  231. register char *p;
  232. p = NULL;
  233. if (n)
  234. {
  235. p = CALLOC(1, n);
  236. if (!p) no_space();
  237. }
  238. return (p);
  239. }
  240. create_file_names()
  241. {
  242. int i, len;
  243. char *tmpdir;
  244. #if defined(KYLEP_CHANGE)
  245. tmpdir = getenv("TMPDIR");
  246. if (tmpdir == 0) tmpdir = getenv("TEMP");
  247. if (tmpdir == 0) tmpdir = "/tmp";
  248. #else
  249. tmpdir = getenv("TMPDIR");
  250. if (tmpdir == 0) tmpdir = "/tmp";
  251. #endif
  252. len = strlen(tmpdir);
  253. i = len + 13;
  254. if (len && tmpdir[len-1] != '/')
  255. ++i;
  256. action_file_name = MALLOC(i);
  257. if (action_file_name == 0) no_space();
  258. text_file_name = MALLOC(i);
  259. if (text_file_name == 0) no_space();
  260. union_file_name = MALLOC(i);
  261. if (union_file_name == 0) no_space();
  262. strcpy(action_file_name, tmpdir);
  263. strcpy(text_file_name, tmpdir);
  264. strcpy(union_file_name, tmpdir);
  265. if (len && tmpdir[len - 1] != '/')
  266. {
  267. action_file_name[len] = '/';
  268. text_file_name[len] = '/';
  269. union_file_name[len] = '/';
  270. ++len;
  271. }
  272. strcpy(action_file_name + len, temp_form);
  273. strcpy(text_file_name + len, temp_form);
  274. strcpy(union_file_name + len, temp_form);
  275. action_file_name[len + 5] = 'a';
  276. text_file_name[len + 5] = 't';
  277. union_file_name[len + 5] = 'u';
  278. mktemp(action_file_name);
  279. mktemp(text_file_name);
  280. mktemp(union_file_name);
  281. len = strlen(file_prefix);
  282. output_file_name = MALLOC(len + 7);
  283. if (output_file_name == 0)
  284. no_space();
  285. strcpy(output_file_name, file_prefix);
  286. strcpy(output_file_name + len, OUTPUT_SUFFIX);
  287. if (rflag)
  288. {
  289. code_file_name = MALLOC(len + 8);
  290. if (code_file_name == 0)
  291. no_space();
  292. strcpy(code_file_name, file_prefix);
  293. strcpy(code_file_name + len, CODE_SUFFIX);
  294. }
  295. else
  296. code_file_name = output_file_name;
  297. if (dflag)
  298. {
  299. defines_file_name = MALLOC(len + 7);
  300. if (defines_file_name == 0)
  301. no_space();
  302. strcpy(defines_file_name, file_prefix);
  303. strcpy(defines_file_name + len, DEFINES_SUFFIX);
  304. }
  305. if (vflag)
  306. {
  307. verbose_file_name = MALLOC(len + 8);
  308. if (verbose_file_name == 0)
  309. no_space();
  310. strcpy(verbose_file_name, file_prefix);
  311. strcpy(verbose_file_name + len, VERBOSE_SUFFIX);
  312. }
  313. }
  314. open_files()
  315. {
  316. create_file_names();
  317. if (input_file == 0)
  318. {
  319. input_file = fopen(input_file_name, "r");
  320. if (input_file == 0)
  321. open_error(input_file_name);
  322. }
  323. action_file = fopen(action_file_name, "w");
  324. if (action_file == 0)
  325. open_error(action_file_name);
  326. text_file = fopen(text_file_name, "w");
  327. if (text_file == 0)
  328. open_error(text_file_name);
  329. if (vflag)
  330. {
  331. verbose_file = fopen(verbose_file_name, "w");
  332. if (verbose_file == 0)
  333. open_error(verbose_file_name);
  334. }
  335. if (dflag)
  336. {
  337. defines_file = fopen(defines_file_name, "w");
  338. if (defines_file == 0)
  339. open_error(defines_file_name);
  340. union_file = fopen(union_file_name, "w");
  341. if (union_file == 0)
  342. open_error(union_file_name);
  343. }
  344. output_file = fopen(output_file_name, "w");
  345. if (output_file == 0)
  346. open_error(output_file_name);
  347. if (rflag)
  348. {
  349. code_file = fopen(code_file_name, "w");
  350. if (code_file == 0)
  351. open_error(code_file_name);
  352. }
  353. else
  354. code_file = output_file;
  355. }
  356. int
  357. #if defined(KYLEP_CHANGE)
  358. __cdecl
  359. #endif
  360. main(argc, argv)
  361. int argc;
  362. char *argv[];
  363. {
  364. set_signals();
  365. getargs(argc, argv);
  366. open_files();
  367. reader();
  368. lr0();
  369. lalr();
  370. make_parser();
  371. verbose();
  372. output();
  373. done(0);
  374. /*NOTREACHED*/
  375. }