ckwart.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722
  1. #include "ckcsym.h"
  2. char *wartv = "Wart Version 2.15, 18 September 2020 ";
  3. #define CKWART_C
  4. #ifdef MDEBUG
  5. /* Use the real ones in this module only */
  6. #ifdef malloc
  7. #undef malloc
  8. #endif /* malloc */
  9. #ifdef calloc
  10. #undef calloc
  11. #endif /* calloc */
  12. #ifdef realloc
  13. #undef realloc
  14. #endif /* realloc */
  15. #ifdef free
  16. #undef free
  17. #endif /* free */
  18. #endif /* MDEBUG */
  19. #ifdef MAC
  20. #define VOID void
  21. #endif /* MAC */
  22. /* W A R T */
  23. /*
  24. A small subset of "lex".
  25. Authors: Jeff Damens, Frank da Cruz
  26. Columbia University Center for Computing Activites.
  27. First released November 1984.
  28. Copyright (C) 1984, 2009,
  29. Trustees of Columbia University in the City of New York.
  30. All rights reserved. See the C-Kermit COPYING.TXT file or the
  31. copyright text in the ckcmai.c module for disclaimer and permissions.
  32. */
  33. /*
  34. * input format is:
  35. * lines to be copied | %state <state names...>
  36. * %%
  37. * <state> | <state,state,...> CHAR { actions }
  38. * ...
  39. * %%
  40. * more lines to be copied
  41. */
  42. #include "ckcdeb.h" /* Includes */
  43. #ifdef STRATUS
  44. /* Actually call printf, not our printf-catcher for Kermit */
  45. #ifdef printf
  46. #undef printf
  47. #endif /* printf */
  48. #ifdef fprintf
  49. #undef fprintf
  50. #endif /* fprintf */
  51. #endif /* STRATUS */
  52. #ifdef MAC
  53. /* Same deal for Macintosh */
  54. #ifdef printf
  55. #undef printf
  56. #endif /* printf */
  57. #ifdef fprintf
  58. #undef fprintf
  59. #endif /* fprintf */
  60. #endif /* MAC */
  61. #ifdef UNIX
  62. /* And UNIX */
  63. #ifdef printf
  64. #undef printf
  65. #endif /* printf */
  66. #ifdef fprintf
  67. #undef fprintf
  68. #endif /* fprintf */
  69. #endif /* UNIX */
  70. /*
  71. The following "char" should be changed to "short", "int", or "long" if your
  72. wart program will generate more than 127 states. Since wart is used mainly
  73. with C-Kermit, which has about 80 states, "char" is adequate. This keeps
  74. the program about 3K-4K smaller, which can be critical on 16-bit
  75. architectures.
  76. */
  77. #ifdef IRIX60
  78. /*
  79. Also use short or int if your compiler complains inordinately about
  80. "integer conversion resulted in a change of sign"...
  81. */
  82. #define TBL_TYPE "short" /* C data type of state table */
  83. #else
  84. #define TBL_TYPE "char" /* C data type of state table */
  85. #endif /* IRIX60 */
  86. #define C_L 014 /* Formfeed */
  87. #define SEP 1 /* Token types */
  88. #define LBRACK 2
  89. #define RBRACK 3
  90. #define WORD 4
  91. #define COMMA 5
  92. /* Storage sizes */
  93. #define MAXSTATES 50 /* max number of states */
  94. #define MAXWORD 50 /* max # of chars/word */
  95. #define SBYTES ((MAXSTATES+6)/8) /* # of bytes for state bitmask */
  96. /* Name of wart function in generated program */
  97. #ifndef FNAME
  98. #define FNAME "wart"
  99. #endif /* FNAME */
  100. /* Structure for state information */
  101. struct transx {
  102. CHAR states[SBYTES]; /* included states */
  103. int anyst; /* true if this good from any state */
  104. CHAR inchr; /* input character */
  105. int actno; /* associated action */
  106. struct transx *nxt;
  107. }; /* next transition */
  108. typedef struct transx *trans;
  109. /* Function prototypes */
  110. _PROTOTYP( VOID setwstate, (int, trans) );
  111. _PROTOTYP( int teststate, (int, trans) );
  112. _PROTOTYP( trans rdinput, (FILE *, FILE *) );
  113. _PROTOTYP( VOID initial, (FILE *, FILE *) );
  114. _PROTOTYP( int isin, (char *, int) );
  115. _PROTOTYP( int isword, (int) );
  116. _PROTOTYP( VOID rdword, (FILE *, char *) );
  117. _PROTOTYP( VOID rdstates, (FILE *, FILE *) );
  118. _PROTOTYP( trans newtrans, (void) );
  119. _PROTOTYP( trans rdrules, (FILE *, FILE *) );
  120. _PROTOTYP( VOID statelist, (FILE *, trans) );
  121. _PROTOTYP( VOID copyact, (FILE *, FILE *, int) );
  122. _PROTOTYP( int faction, (trans, int, int) );
  123. _PROTOTYP( VOID emptytbl, (void) );
  124. _PROTOTYP( VOID addaction, (int, int, int) );
  125. _PROTOTYP( VOID writetbl, (FILE *) );
  126. _PROTOTYP( VOID warray, (FILE *, char *, int [], int, char *) );
  127. _PROTOTYP( VOID prolog, (FILE *) );
  128. _PROTOTYP( VOID epilogue, (FILE *) );
  129. _PROTOTYP( VOID copyrest, (FILE *, FILE *) );
  130. _PROTOTYP( int gettoken, (FILE *) );
  131. _PROTOTYP( VOID rdcmnt, (FILE *) );
  132. _PROTOTYP( VOID clrhash, (void) );
  133. _PROTOTYP( int hash, (char *) );
  134. _PROTOTYP( VOID enter, (char *, int) );
  135. _PROTOTYP( int lkup, (char *) );
  136. _PROTOTYP( static char* copy, (char *s) );
  137. /* Variables and tables */
  138. /* lt 1992-10-08 Begin
  139. * provide definition for deblog variable
  140. * ckcdeb.h declares as extern. DECC AXP is strict about ref/def model
  141. * Variable is unused herein, to the best of my knowledge.
  142. */
  143. #ifdef VMS
  144. int deblog;
  145. #endif /* VMS */
  146. /* lt 1992-10-08 End
  147. */
  148. static int lines, nstates, nacts;
  149. static char tokval[MAXWORD];
  150. static int tbl[MAXSTATES*96];
  151. char *tbl_type = TBL_TYPE;
  152. char *txt1 = "\n#define BEGIN state =\n\nint state = 0;\n\nint\n";
  153. char *fname = FNAME; /* Generated function name goes here */
  154. /* rest of program... */
  155. char *txt2 = "()\n\
  156. {\n\
  157. int c,actno;\n\
  158. extern ";
  159. /* Data type of state table is inserted here (short or int) */
  160. char *txt2a =
  161. " tbl[];\n\
  162. while (1) {\n\
  163. c = input() - 32;\n\
  164. debug(F000,\"PROTO input\",ckitoa(state),c+32);\n\
  165. if (c < 0 || c > 95) c = 0;\n";
  166. char *txt2b = " if ((actno = tbl[c + state*96]) != -1)\n\
  167. switch(actno) {\n";
  168. /* this program's output goes here, followed by final text... */
  169. char *txt3 = "\n }\n }\n}\n\n";
  170. /*
  171. * turn on the bit associated with the given state
  172. *
  173. */
  174. VOID
  175. setwstate(state,t) int state; trans t; {
  176. int idx,msk;
  177. idx = state/8; /* byte associated with state */
  178. msk = 0x80 >> (state % 8); /* bit mask for state */
  179. t->states[idx] |= msk;
  180. }
  181. /*
  182. * see if the state is involved in the transition
  183. *
  184. */
  185. int
  186. teststate(state,t) int state; trans t; {
  187. int idx,msk;
  188. idx = state/8;
  189. msk = 0x80 >> (state % 8);
  190. return(t->states[idx] & msk);
  191. }
  192. /*
  193. * read input from here...
  194. *
  195. */
  196. trans
  197. rdinput(infp,outfp) FILE *infp,*outfp; {
  198. trans x;
  199. lines = 1; /* line counter */
  200. nstates = 0; /* no states */
  201. nacts = 0; /* no actions yet */
  202. fprintf(outfp,"\n%c* WARNING -- This C source program generated by ",'/');
  203. fprintf(outfp,"Wart preprocessor. */\n");
  204. fprintf(outfp,"%c* Do not edit this file; edit the Wart-format ",'/');
  205. fprintf(outfp,"source file instead, */\n");
  206. fprintf(outfp,"%c* and then run it through Wart to produce a new ",'/');
  207. fprintf(outfp,"C source file. */\n\n");
  208. fprintf(outfp,"%c* Wart Version Info: */\n",'/');
  209. fprintf(outfp,"char *wartv = \"%s\";\n\n",wartv);
  210. initial(infp,outfp); /* read state names, initial defs */
  211. prolog(outfp); /* write out our initial code */
  212. x = rdrules(infp,outfp); /* read rules */
  213. epilogue(outfp); /* write out epilogue code */
  214. return(x);
  215. }
  216. /*
  217. * initial - read initial definitions and state names. Returns
  218. * on EOF or %%.
  219. *
  220. */
  221. VOID
  222. initial(infp,outfp) FILE *infp, *outfp; {
  223. int c;
  224. char wordbuf[MAXWORD];
  225. while ((c = getc(infp)) != EOF) {
  226. if (c == '%') {
  227. rdword(infp,wordbuf);
  228. if (strcmp(wordbuf,"states") == 0)
  229. rdstates(infp,outfp);
  230. else if (strcmp(wordbuf,"%") == 0) return;
  231. else fprintf(outfp,"%%%s",wordbuf);
  232. }
  233. else putc(c,outfp);
  234. if (c == '\n') lines++;
  235. }
  236. }
  237. /*
  238. * boolean function to tell if the given character can be part of
  239. * a word.
  240. *
  241. */
  242. int
  243. isin(s,c) char *s; int c; {
  244. for (; *s != '\0'; s++)
  245. if (*s == (char) c) return(1);
  246. return(0);
  247. }
  248. int
  249. isword(c) int c; {
  250. static char special[] = ".%_-$@"; /* these are allowable */
  251. return(isalnum(c) || isin(special,c));
  252. }
  253. /*
  254. * read the next word into the given buffer.
  255. *
  256. */
  257. VOID
  258. rdword(fp,buf) FILE *fp; char *buf; {
  259. int len = 0,c;
  260. while (isword(c = getc(fp)) && ++len < MAXWORD) *buf++ = (char) c;
  261. *buf++ = '\0'; /* tie off word */
  262. ungetc(c,fp); /* put break char back */
  263. }
  264. /*
  265. * read state names, up to a newline.
  266. *
  267. */
  268. VOID
  269. rdstates(fp,ofp) FILE *fp,*ofp; {
  270. int c;
  271. char wordbuf[MAXWORD];
  272. while ((c = getc(fp)) != EOF && c != '\n') {
  273. if (isspace(c) || c == C_L) continue; /* skip whitespace */
  274. ungetc(c,fp); /* put char back */
  275. rdword(fp,wordbuf); /* read the whole word */
  276. enter(wordbuf,++nstates); /* put into symbol tbl */
  277. fprintf(ofp,"#define %s %d\n",wordbuf,nstates);
  278. }
  279. lines++;
  280. }
  281. /*
  282. * allocate a new, empty transition node
  283. *
  284. */
  285. trans
  286. newtrans() {
  287. trans new;
  288. int i;
  289. new = (trans) malloc(sizeof (struct transx));
  290. for (i=0; i<SBYTES; i++) new->states[i] = 0;
  291. new->anyst = 0;
  292. new->nxt = NULL;
  293. return(new);
  294. }
  295. /*
  296. * read all the rules.
  297. *
  298. */
  299. trans
  300. rdrules(fp,out) FILE *fp,*out; {
  301. trans head,cur,prev;
  302. int curtok;
  303. head = cur = prev = NULL;
  304. while ((curtok = gettoken(fp)) != SEP)
  305. switch(curtok) {
  306. case LBRACK:
  307. if (cur == NULL)
  308. cur = newtrans();
  309. else
  310. fatal("duplicate state list");
  311. statelist(fp,cur); /* set states */
  312. continue; /* prepare to read char */
  313. case WORD:
  314. if ((int)strlen(tokval) != 1)
  315. fatal("multiple chars in state");
  316. if (cur == NULL) {
  317. cur = newtrans();
  318. cur->anyst = 1;
  319. }
  320. cur->actno = ++nacts;
  321. cur->inchr = (char) (tokval[0] - 32);
  322. if (head == NULL)
  323. head = cur;
  324. else
  325. prev->nxt = cur;
  326. prev = cur;
  327. cur = NULL;
  328. copyact(fp,out,nacts);
  329. break;
  330. default: fatal("bad input format");
  331. }
  332. return(head);
  333. }
  334. /*
  335. * read a list of (comma-separated) states, set them in the
  336. * given transition.
  337. *
  338. */
  339. VOID
  340. statelist(fp,t) FILE *fp; trans t; {
  341. int curtok,sval;
  342. curtok = COMMA;
  343. while (curtok != RBRACK) {
  344. if (curtok != COMMA) fatal("missing comma");
  345. if ((curtok = gettoken(fp)) != WORD) fatal("missing state name");
  346. if ((sval = lkup(tokval)) == -1) {
  347. fprintf(stderr,"state %s undefined\n",tokval);
  348. fatal("undefined state");
  349. }
  350. setwstate(sval,t);
  351. curtok = gettoken(fp);
  352. }
  353. }
  354. /*
  355. * copy an action from the input to the output file
  356. *
  357. */
  358. VOID
  359. copyact(inp,outp,actno) FILE *inp,*outp; int actno; {
  360. int c,bcnt;
  361. fprintf(outp,"case %d:\n",actno);
  362. while (c = getc(inp), (isspace(c) || c == C_L))
  363. if (c == '\n') lines++;
  364. if (c == '{') {
  365. bcnt = 1;
  366. fputs(" {",outp);
  367. while (bcnt > 0 && (c = getc(inp)) != EOF) {
  368. if (c == '{') bcnt++;
  369. else if (c == '}') bcnt--;
  370. else if (c == '\n') lines++;
  371. putc(c,outp);
  372. }
  373. if (bcnt > 0) fatal("action doesn't end");
  374. } else {
  375. while (c != '\n' && c != EOF) {
  376. putc(c,outp);
  377. c = getc(inp);
  378. }
  379. lines++;
  380. }
  381. fprintf(outp,"\n break;\n");
  382. }
  383. /*
  384. * find the action associated with a given character and state.
  385. * returns -1 if one can't be found.
  386. *
  387. */
  388. int
  389. faction(hd,state,chr) trans hd; int state,chr; {
  390. while (hd != NULL) {
  391. if (hd->anyst || teststate(state,hd))
  392. if (hd->inchr == ('.' - 32) || hd->inchr == (char) chr)
  393. return(hd->actno);
  394. hd = hd->nxt;
  395. }
  396. return(-1);
  397. }
  398. /*
  399. * empty the table...
  400. *
  401. */
  402. VOID
  403. emptytbl() {
  404. int i;
  405. for (i=0; i<nstates*96; i++) tbl[i] = -1;
  406. }
  407. /*
  408. * add the specified action to the output for the given state and chr.
  409. *
  410. */
  411. VOID
  412. addaction(act,state,chr) int act,state,chr; {
  413. tbl[state*96 + chr] = act;
  414. }
  415. VOID
  416. writetbl(fp) FILE *fp; {
  417. warray(fp,"tbl",tbl,96*(nstates+1),TBL_TYPE);
  418. }
  419. /*
  420. * write an array to the output file, given its name and size.
  421. *
  422. */
  423. VOID
  424. warray(fp,nam,cont,siz,typ) FILE *fp; char *nam; int cont[],siz; char *typ; {
  425. int i;
  426. fprintf(fp,"%s %s[] = {\n",typ,nam);
  427. for (i = 0; i < siz - 1; ) {
  428. fprintf(fp," %2d,",cont[i]);
  429. if ((++i % 16) == 0) putc('\n',fp);
  430. }
  431. fprintf(fp,"%2d\n};\n",cont[siz-1]);
  432. }
  433. /*
  434. There was an #ifdef rat's next here here regarding main's return type.
  435. The following should be equivalnt and is much simpler. OS2 actually
  436. means IBM OS/2 or MS Windows, but OS/2 itself is long gone.
  437. -fdc, Fri Sep 18 19:42:48 2020
  438. */
  439. #ifdef OS2
  440. void
  441. #else
  442. int
  443. #endif /* OS2 */
  444. main(argc,argv) int argc; char **argv; {
  445. trans head;
  446. int state,c;
  447. FILE *infile,*outfile;
  448. if (argc > 1) {
  449. if ((infile = fopen(argv[1],"r")) == NULL) {
  450. fprintf(stderr,"Can't open %s\n",argv[1]);
  451. fatal("unreadable input file");
  452. }
  453. } else infile = stdin;
  454. if (argc > 2) {
  455. if ((outfile = fopen(argv[2],"w")) == NULL) {
  456. fprintf(stderr,"Can't write to %s\n",argv[2]);
  457. fatal("bad output file");
  458. }
  459. } else outfile = stdout;
  460. clrhash(); /* empty hash table */
  461. head = rdinput(infile,outfile); /* read input file */
  462. emptytbl(); /* empty our tables */
  463. for (state = 0; state <= nstates; state++)
  464. for (c = 1; c < 96; c++) /* find actions, */
  465. addaction(faction(head,state,c),state,c); /* add to tbl */
  466. writetbl(outfile);
  467. copyrest(infile,outfile);
  468. printf("%d states, %d actions\n",nstates,nacts);
  469. exit(GOOD_EXIT);
  470. }
  471. /*
  472. * fatal error handler
  473. *
  474. */
  475. VOID
  476. fatal(msg) char *msg; {
  477. fprintf(stderr,"error in line %d: %s\n",lines,msg);
  478. exit(BAD_EXIT);
  479. }
  480. VOID
  481. prolog(outfp) FILE *outfp; {
  482. int c;
  483. while ((c = *txt1++) != '\0') putc(c,outfp);
  484. while ((c = *fname++) != '\0') putc(c,outfp);
  485. while ((c = *txt2++) != '\0') putc(c,outfp);
  486. while ((c = *tbl_type++) != '\0') putc(c,outfp);
  487. while ((c = *txt2a++) != '\0') putc(c,outfp);
  488. while ((c = *txt2b++) != '\0') putc(c,outfp);
  489. }
  490. VOID
  491. epilogue(outfp) FILE *outfp; {
  492. int c;
  493. while ((c = *txt3++) != '\0') putc(c,outfp);
  494. }
  495. VOID
  496. copyrest(in,out) FILE *in,*out; {
  497. int c;
  498. while ((c = getc(in)) != EOF) putc(c,out);
  499. }
  500. /*
  501. * gettoken - returns token type of next token, sets tokval
  502. * to the string value of the token if appropriate.
  503. *
  504. */
  505. int
  506. gettoken(fp) FILE *fp; {
  507. int c;
  508. while (1) { /* loop if reading comments... */
  509. do {
  510. c = getc(fp);
  511. if (c == '\n') lines++;
  512. } while ((isspace(c) || c == C_L)); /* skip whitespace */
  513. switch(c) {
  514. case EOF:
  515. return(SEP);
  516. case '%':
  517. if ((c = getc(fp)) == '%') return(SEP);
  518. tokval[0] = '%';
  519. tokval[1] = (char) c;
  520. rdword(fp,tokval+2);
  521. return(WORD);
  522. case '<':
  523. return(LBRACK);
  524. case '>':
  525. return(RBRACK);
  526. case ',':
  527. return(COMMA);
  528. case '/':
  529. if ((c = getc(fp)) == '*') {
  530. rdcmnt(fp); /* skip over the comment */
  531. continue;
  532. } else { /* and keep looping */
  533. ungetc(c,fp); /* put this back into input */
  534. c = '/'; /* put character back, fall thru */
  535. }
  536. default:
  537. if (isword(c)) {
  538. ungetc(c,fp);
  539. rdword(fp,tokval);
  540. return(WORD);
  541. } else fatal("Invalid character in input");
  542. }
  543. }
  544. }
  545. /*
  546. * skip over a comment
  547. *
  548. */
  549. VOID
  550. rdcmnt(fp) FILE *fp; {
  551. int c,star,prcnt;
  552. prcnt = star = 0; /* no star seen yet */
  553. while (!((c = getc(fp)) == '/' && star)) {
  554. if (c == EOF || (prcnt && c == '%')) fatal("Unterminated comment");
  555. prcnt = (c == '%');
  556. star = (c == '*');
  557. if (c == '\n') lines++;
  558. }
  559. }
  560. /*
  561. * symbol table management for wart
  562. *
  563. * entry points:
  564. * clrhash - empty hash table.
  565. * enter - enter a name into the symbol table
  566. * lkup - find a name's value in the symbol table.
  567. *
  568. */
  569. #define HASHSIZE 101 /* # of entries in hash table */
  570. struct sym {
  571. char *name; /* symbol name */
  572. int val; /* value */
  573. struct sym *hnxt; /* next on collision chain */
  574. } *htab[HASHSIZE]; /* the hash table */
  575. /*
  576. * empty the hash table before using it...
  577. *
  578. */
  579. VOID
  580. clrhash() {
  581. int i;
  582. for (i=0; i<HASHSIZE; i++) htab[i] = NULL;
  583. }
  584. /*
  585. * compute the value of the hash for a symbol
  586. *
  587. */
  588. int
  589. hash(name) char *name; {
  590. int sum;
  591. for (sum = 0; *name != '\0'; name++) sum += (sum + *name);
  592. sum %= HASHSIZE; /* take sum mod hashsize */
  593. if (sum < 0) sum += HASHSIZE; /* disallow negative hash value */
  594. return(sum);
  595. }
  596. /*
  597. * make a private copy of a string...
  598. *
  599. */
  600. static char*
  601. copy(s) char *s; {
  602. char *new;
  603. new = (char *) malloc((int)strlen(s) + 1);
  604. strcpy(new,s);
  605. return(new);
  606. }
  607. /*
  608. * enter state name into the hash table
  609. *
  610. */
  611. VOID
  612. enter(name,svalue) char *name; int svalue; {
  613. int h;
  614. struct sym *cur;
  615. if (lkup(name) != -1) {
  616. fprintf(stderr,"state \"%s\" appears twice...\n", name);
  617. exit(BAD_EXIT);
  618. }
  619. h = hash(name);
  620. cur = (struct sym *)malloc(sizeof (struct sym));
  621. cur->name = copy(name);
  622. cur->val = svalue;
  623. cur->hnxt = htab[h];
  624. htab[h] = cur;
  625. }
  626. /*
  627. * find name in the symbol table, return its value. Returns -1
  628. * if not found.
  629. *
  630. */
  631. int
  632. lkup(name) char *name; {
  633. struct sym *cur;
  634. for (cur = htab[hash(name)]; cur != NULL; cur = cur->hnxt)
  635. if (strcmp(cur->name,name) == 0) return(cur->val);
  636. return(-1);
  637. }