1 /*****************************************************************************
4 ** POL - Problem Oriented Language
6 ** This is part of the CTSim program
7 ** Copyright (C) 1983-2000 Kevin Rosenberg
9 ** $Id: ezpol.cpp,v 1.3 2000/07/13 07:03:21 kevin Exp $
11 ** This program is free software; you can redistribute it and/or modify
12 ** it under the terms of the GNU General Public License (version 2) as
13 ** published by the Free Software Foundation.
15 ** This program is distributed in the hope that it will be useful,
16 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ** GNU General Public License for more details.
20 ** You should have received a copy of the GNU General Public License
21 ** along with this program; if not, write to the Free Software
22 ** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 ******************************************************************************/
25 /*----------------------------------------------------------------------*/
30 #include "ctsupport.h"
34 static const int HASHSIZE=20;
37 /* Tables words stored with install() & found with lookup() */
38 static SYMBOL *skiptable[HASHSIZE]; /* words to ignore and skip */
39 static SYMBOL *cmdtable[HASHSIZE]; /* pol parameter commands */
40 static SYMBOL *usertable[HASHSIZE]; /* user defined symbols */
42 static struct token_st token; /* current token */
44 static struct metachar {
45 char eoc; /* end of command character */
46 char str; /* string delimiter */
47 char com; /* comment character */
48 char cmd; /* pol parameter command character */
49 char prg; /* program load character */
50 char con; /* continuation across newline character */
51 char out; /* character that delimits output to terminal */
52 char ter; /* character indicates insertion of input from terminal */
53 char inb; /* input from graphics device */
56 /* current pol state */
58 static struct pol_st {
59 char skipchars[MAXSKIPCHAR]; /* characters to skip */
60 int nl_eoc; /* TRUE if newline character ends a command */
61 int trace; /* TRUE if trace is on */
69 /* internal codes for pol commands */
87 static struct key cmdlist[] = {
98 { "nl_eoc",PC_NL_EOC,},
99 { "nl_neoc", PC_NL_NEOC,},
101 { "troff", PC_TROFF,},
106 #define NUMCMD (sizeof(cmdlist) / sizeof (struct key))
108 static int skiptok(char term[]);
109 static int pol_tok(struct token_st *token);
110 static void dumptok(struct token_st *token);
113 static int getpol_tok(struct token_st *token);
114 static int getcmd(void);
115 static int gettok (TOKEN *tok);
116 static void getblank(char *s, int toksiz);
117 static int getalpha(char *s, int toksiz);
118 static void getquote(char *qs, int toksiz);
119 static void getescape(char *s, int delim, int toksiz);
120 static int getnumber (char str[], int strsize, double *fnum, int *inum);
121 static void eatline(void);
122 static int type(int c);
123 static void inittable(SYMBOL *table[]);
124 static void freetable(SYMBOL *table[]);
125 static int hash(char *s);
126 static SYMBOL *lookup(SYMBOL *table[], char *s);
127 static SYMBOL *install(SYMBOL *table[], char *s, int def);
128 static void outch(int c);
129 static void inc_line(void);
130 static int getlinect(void);
131 static void setlinect(int n);
132 static void synerr(char *msg);
133 static int pol_getch(FILE *fp);
134 static void ungets(char *s);
144 meta.con = AMPERSAND;
150 pol.skipchars[0] = EOS;
152 inittable (cmdtable); /* initialize symbol tables */
153 inittable (usertable);
154 inittable (skiptable);
156 for (unsigned int i = 0; i < NUMCMD; i++)
157 install (cmdtable, cmdlist[i].keyword, cmdlist[i].code);
159 token.ready = FALSE; /* no token read yet */
164 * char *w - word for pol to ignore and skip over in input
166 * pol_tok() compares all tokens to words given to this routine. If it finds it,
167 * it will immediately read another token.
171 pol_skpword (char *w)
173 if (install (skiptable, w, 0) == NULL)
174 synerr ("Too many skip words defined");
179 * skip all characters that appear in string s
182 pol_skpchar (char *s)
184 strncpy (pol.skipchars, s, MAXSKIPCHAR);
187 /* pol_install (str, code)
189 * char *str - token string to install
190 * int code - code to return for token
192 * pol_tok() looks for these user defined tokens. If it finds one,
193 * it stores the tokens code in the token structure and returns TT_USERTOK
196 pol_install (char *str, int code)
198 if (install (usertable, str, code) == NULL)
200 synerr ("Out ot memory installing user tokens");
207 /* get_word - matches tokens on a letter by letter basis
209 * char *search - string to search for
210 * int nlet - maximum number of chars to search for match
214 pol_word (char *search, int nlet)
217 if (pol.trace == TRUE)
218 printf ("matching current token %s against word %s\n", token.tokstr, search);
220 if (strncasecmp (search, token.tokstr, nlet) == 0) {
227 /* pol_usertok (str,code)
228 * see if current token is a user defined token set with pol_install()
230 * char *str - token string as read from input
231 * int *code - returned code for user defined symbol
232 * return value - TRUE if current token has been user defined
233 * FALSE if current token is not user defined
236 pol_usertok (char *str, int *code)
240 if (pol.trace == TRUE)
241 printf ("checking if current token '%s' is user defined\n", token.tokstr);
243 if (token.type == TT_USERTOK) {
245 strcpy (str, token.tokstr);
254 /* isstring (s) - returns TRUE if current token is a string
256 * char *s - pointer to place to store token string
260 pol_string (char *str)
264 if (token.type == TT_STRING) {
265 strcpy (str, token.tokstr);
272 /* pol_integer - test for an integer
274 * int *n: returned integer value
275 * int typecode = TT_INT if accept only integer values
276 * = TT_REAL if accept both real and integer values
277 * int boundcode= TRUE if force to lie between boundries
278 * = FALSE can take any value it likes
279 * int bb1: lower bound
280 * int bb2: upper bound
283 pol_integer (int *n, int typecode, int boundcode, int bb1, int bb2)
287 if (pol.trace == TRUE)
288 printf ("checking if current token %s is an integer\n", token.tokstr);
290 if (token.type == TT_INT || token.type == TT_REAL) {
291 if (boundcode == TRUE) {
292 if (token.inum < bb1)
294 else if (token.inum > bb2)
308 pol_float (double *n, double typecode, double boundcode, double bb1, double bb2)
312 if (pol.trace == TRUE)
313 printf ("checking if current token %s is an floating point number\n", token.tokstr);
315 if (token.type == TT_INT || token.type == TT_REAL) {
316 if (boundcode == TRUE) {
317 if (token.fnum < bb1)
319 else if (token.fnum > bb2)
332 /*----------------------------------------------------------------------*/
333 /* pol_skip() - skip over any token except for end of command sequence */
335 /* returns TRUE if succesful skip */
336 /* returns FALSE if already at end of command or EOF */
337 /*----------------------------------------------------------------------*/
341 char term[5]; /* string of characters not to skip */
344 if (pol.nl_eoc == TRUE) {
350 return (skiptok (term));
353 void pol_reader(void)
355 while (pol_skip() == TRUE)
358 dumptok (&token); /* skip end of command token */
361 /* skiptok (term) - skip a token unless the first character of a token is
362 * in the string of terminators, term.
363 * char *term - string of termination characters, don't skip these characters
364 * skiptok() also does NOT skip TT_EOF
365 * returns (TRUE) if succesful skip of a token
366 * returns (FALSE) if didn't skip, read termination character or TT_EOF
370 skiptok (char term[])
374 if (token.type == TT_EOF
375 || (token.type == TT_SPECLCHAR && strchr(term, token.tokstr[0]) != NULL))
384 pol_tok (struct token_st *token)
386 if (token->ready == FALSE)
389 if (token->type == TT_EOF && pol_lookchar() != EOF)
391 return (token->type);
395 dumptok (struct token_st *token)
397 if (token->ready == FALSE)
399 token->ready = FALSE;
403 getpol_tok (struct token_st *token)
407 token->ready = FALSE;
411 if (token->type == TT_BLANK)
413 if (token->type == TT_SPECLCHAR) {
414 if (strchr(pol.skipchars, token->tokstr[0]) != NULL)
416 if (token->tokstr[0] == NEWLINE)
418 if (token->tokstr[0] == meta.cmd) {
422 if (token->tokstr[0] == meta.com) { /* skip comment */
426 if (token->tokstr[0] == meta.out) {
427 getescape(token->tokstr, meta.out, MAXTOK);
428 fputs (token->tokstr, stderr);
431 if (token->tokstr[0] == meta.con) { /* continuation across NEWLINE */
432 while (pol_lookchar() == BLANK || pol_lookchar() == TAB)
434 if (pol_lookchar() == NEWLINE)
437 if (token->tokstr[0] == meta.ter) { /* get input from terminal */
438 pol_usefile (P_USE_FILE, "");
441 return (token->type);
445 /* look for filler words */
447 if (lookup (skiptable, token->tokstr) != NULL) /* ignore words in skip table */
450 /* look for user defined symbols */
452 if ((sym = lookup (usertable, token->tokstr)) != NULL) {
453 token->type = TT_USERTOK;
454 token->code = sym->code;
458 if (pol.trace == TRUE)
459 printf ("Read token '%s', type = %d\n", token->tokstr, token->type);
461 return (token->type);
465 static int getcmd(void)
472 tt = getalpha (str, MAXTOK);
473 if (tt == TT_ERROR) {
474 synerr ("error in pol parameter command");
478 if ((cmd = lookup (cmdtable,str)) == NULL) {
479 synerr ("unrecognized command");
496 pol_usefile (P_USE_FILE, tok.tokstr);
508 printf("eoc = %c str = %c com = %c cmd = %c prg = %c\n",
509 meta.eoc, meta.str, meta.com, meta.cmd, meta.prg);
510 printf("con = %c out = %c ter = %c inb = %c\n",
511 meta.con, meta.out, meta.ter, meta.inb);
514 if (found == FALSE) {
516 if (tt != TT_SPECLCHAR) {
517 synerr("illegal command character");
522 meta.eoc = tok.tokstr[0];
525 meta.str = tok.tokstr[0];
528 meta.com = tok.tokstr[0];
531 meta.cmd = tok.tokstr[0];
534 meta.prg = tok.tokstr[0];
537 meta.con = tok.tokstr[0];
540 meta.out = tok.tokstr[0];
543 meta.ter = tok.tokstr[0];
546 meta.inb = tok.tokstr[0];
549 printf("command not implemented\n");
551 } /* switch (tok->type) */
552 } /* if (found == FALSE) */
553 pol_reader(); /* clean up command */
554 } /* if legal command */
566 int toksiz = MAXTOK; /* maximum length of token string */
568 while ((c = pol_inchar()) == BLANK || c == TAB)
578 if (c == BLANK || c == TAB) { /* skip white space */
579 getblank(tok->tokstr, toksiz);
581 } else if (toktype == LETTER) {
582 toktype = getalpha (tok->tokstr, toksiz);
583 } else if (c == meta.str) { /* quoted string */
584 getquote (tok->tokstr, toksiz);
586 } else if (type(c) == DIGIT || c == PLUS || c == HYPHEN || c == PERIOD) {
587 toktype = getnumber (tok->tokstr, toksiz, &fnum, &inum);
588 } else if (c == EOF) {
589 tok->tokstr[0] = EOS;
594 tok->tokstr[1] = EOS;
595 toktype = TT_SPECLCHAR;
600 if (tok->type == TT_REAL || tok->type == TT_INT) {
613 getblank (char *s, int toksiz)
617 while ((c = pol_inchar()) == BLANK || c == TAB)
627 getalpha (char *s, int toksiz)
629 int i, chartype, alphatype;
631 if (type(pol_lookchar()) != LETTER) {
636 alphatype = TT_ALPHA;
637 for (i = 0; i < toksiz; i++) { /* get alphanumeric token */
639 chartype = type (s[i]);
640 if (chartype != LETTER && chartype != DIGIT)
642 if (chartype == DIGIT)
643 alphatype = TT_ALPNUM;
648 synerr("token too long.");
650 s[i] = EOS; /* terminate token */
655 /* getquote - get quoted string from file */
656 /* have already gotten delimiter in qs[0] */
658 getquote (char *qs, int toksiz)
662 delim = pol_inchar(); /* char = delimiter */
663 getescape(qs, delim, toksiz);
668 getescape ( /* reads up to delim */
676 for (i = 0; (c = pol_inchar()) != delim; i++) {
678 synerr ("Missing closing delimiter.");
682 synerr("string too long.");
687 synerr("end of file inside quotation");
689 } else if (c == BSLASH) { /* escape character */
691 c = pol_inchar(); /* get escaped character */
699 gettext (char *str, int lim)
703 while ((c = pol_inchar()) == BLANK || c == TAB)
707 for (i = 0; i < lim && (c = pol_inchar()) != EOF && c != NEWLINE; i++)
713 /*----------------------------------------------*/
714 /* Get a number for gettok() */
715 /*----------------------------------------------*/
719 char str[], /* string to return token in */
720 int strsize, /* maximum length of token string */
721 double *fnum, /* floating point value of number read */
722 int *inum /* integer value of number read */
726 double sign, whole, frac, powerof10, exp, expsign;
730 isSigned = FALSE; /* TRUE if number prefixed by '+' or '-' */
740 } else if (c == PLUS) {
744 } else if (c == PERIOD) {
745 if (type(pol_lookchar()) != DIGIT) {
748 return (TT_SPECLCHAR);
750 pol_ungetch (PERIOD);
751 } else if (type(c) != DIGIT) {
757 if (isSigned == TRUE) {
760 pol_inchar(); /* get period */
761 c = pol_lookchar(); /* look at character past period */
762 pol_ungetch (PERIOD); /* put back period */
763 if (type(c) != DIGIT) {
765 return (TT_SPECLCHAR);
767 } else if (type (c) != DIGIT) {
769 return (TT_SPECLCHAR);
774 while (type(c = pol_inchar()) == DIGIT) {
777 whole = 10.0 * whole + (c - '0');
779 pol_ungetch (c); /* put back non-numeric character */
781 if (c != PERIOD && tolower(c) != 'e') {
783 *fnum = whole * sign;
786 else if (*fnum > MAX_INT)
793 if (pol_lookchar() == PERIOD) {
802 while (type(c = pol_inchar()) == DIGIT) {
805 frac += (double) (c - '0') / powerof10;
813 if (tolower(c) != 'e')
818 if ((c = pol_inchar()) == PLUS) {
822 } else if (c == HYPHEN) {
826 } else if (type(c) != DIGIT) {
827 --sp; /* erase 'e' */
835 while (type(c = pol_inchar()) == DIGIT) {
838 exp = 10 * exp + (c - '0');
845 *fnum = sign * (whole + frac) * pow (10.0, expsign * exp);
848 else if (*fnum > MAX_INT)
866 type ( /* return type of ASCII character */
870 if (isalpha(c) || c == UNDERLIN)
878 /*----------------------------------------------------------------------*/
880 /* hash table routines. Kernighan & Ritchie */
882 /*----------------------------------------------------------------------*/
889 inittable (SYMBOL *table[])
893 for (i = 0; i < HASHSIZE; i++)
898 * free all memory allocated to table, then clear table
902 freetable (SYMBOL *table[])
907 for (i = 0; i < HASHSIZE; i++) {
919 hash ( /* form hash value of string s */
925 for (hashval = 0; *s != EOS; )
927 return (hashval % HASHSIZE);
930 /* Look for s in hash table */
932 lookup ( SYMBOL *table[], char *s )
935 SYMBOL *found = NULL;
937 for (np = table[hash(s)]; np != NULL; np = np->next)
938 if (strcasecmp(s, np->name) == 0) {
939 found = np; /* found it */
947 install (SYMBOL *table[], char *name, int def)
949 static char installerr[] = "install: out of memory";
953 if ((np = lookup (table, name)) == NULL) { /* not found */
954 np = (SYMBOL *) malloc (sizeof(*np));
959 if ((np->name = strdup(name)) == NULL) {
963 str_lower (np->name);
965 hashval = hash(np->name);
966 np->next = table[hashval];
968 } else /* already there */
973 /*----------------------------------------------------------------------*/
975 /*----------------------------------------------------------------------*/
979 static int currentf = -1; /* pointer to current fp */
980 static FILE *filep[MAXFILE]; /* == NULL for string input */
981 static char *fname[MAXFILE]; /* pointer to filename */
982 static int linect[MAXFILE]; /* line count in file */
984 static char inputline[MAXLINE]; /* current input line */
985 static int lineptr; /* current position in inputline */
994 static void inc_line(void)
1000 static int getlinect(void)
1002 return (linect[currentf]);
1009 linect[currentf] = n;
1015 fputs (fname[currentf], stderr);
1016 fprintf(stderr, "%d", linect[currentf]);
1017 fputc (COLON, stderr);
1018 fputs (msg, stderr);
1019 fputc (NEWLINE, stderr);
1022 /*----------------------------------------------------------------------*/
1024 /*----------------------------------------------------------------------*/
1027 static int bp = 0; /* pointer to next free position */
1028 static int buf[BUFSIZE]; /* pushed back input characters */
1030 /* pol_usefile - set source of POL input
1032 * int source - source of input
1033 * P_USE_STR - have POL use strings as input
1034 * P_USE_FILE - use file. filename is in str
1039 pol_usefile (int source, char *fn)
1044 if (currentf >= MAXFILE) {
1046 synerr ("files nested too deeply");
1050 bp = 0; /* clear any pushed back input */
1052 if (source == P_USE_STR) {
1053 filep[currentf] = NULL;
1054 linect[currentf] = 1;
1055 } else if (source == P_USE_FILE) {
1056 if (fn == NULL || strlen(fn) == 0) {
1058 } else if ((fp = fopen(fn, "r")) == NULL) {
1060 synerr ("can't open file");
1063 filep[currentf] = fp;
1064 linect[currentf] = 1;
1065 fname[currentf] = strdup (fn);
1069 void pol_closefile(void)
1071 if (currentf >= 0) {
1072 if (filep[currentf] != NULL)
1073 fclose (filep[currentf]);
1078 /*-----------------------------*/
1079 /* Lowest Level Input Routines */
1080 /*-----------------------------*/
1083 int pol_lookchar(void)
1092 int pol_inchar(void)
1099 while (currentf >= 0 && (c = pol_getch(filep[currentf])) == EOF && filep[currentf] != NULL) {
1106 /*--------------------------------------------------------------*/
1107 /* getch - get a (possibly pushed back) character */
1108 /* if fp == NULL, then get character from inputline */
1109 /*--------------------------------------------------------------*/
1112 pol_getch (FILE *fp)
1120 if ((c = inputline[lineptr]) == EOS)
1132 /* push character back on input */
1137 sys_error (ERR_SEVERE, "too many characters pushed back [pol_ungetch]");
1143 /* push back string onto input */
1149 for (i = strlen(s) - 1; i >= 0; i--)
1154 get_inputline (FILE *fp)
1158 if (fgets (inputline, MAXLINE, fp) == NULL)
1165 set_inputline (char *line)
1169 strncpy (inputline, line, MAXLINE);