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.2 2000/06/19 19:04:05 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 ignorecase = TRUE;
110 static int skiptok(char term[]);
111 static int pol_tok(struct token_st *token);
112 static void dumptok(struct token_st *token);
115 static int getpol_tok(struct token_st *token);
116 static int getcmd(void);
117 static int gettok (TOKEN *tok);
118 static void getblank(char *s, int toksiz);
119 static int getalpha(char *s, int toksiz);
120 static void getquote(char *qs, int toksiz);
121 static void getescape(char *s, int delim, int toksiz);
122 static int getnumber (char str[], int strsize, double *fnum, int *inum);
123 static void eatline(void);
124 static int type(int c);
125 static void inittable(SYMBOL *table[]);
126 static void freetable(SYMBOL *table[]);
127 static int hash(char *s);
128 static SYMBOL *lookup(SYMBOL *table[], char *s);
129 static SYMBOL *install(SYMBOL *table[], char *s, int def);
130 static void outch(int c);
131 static void inc_line(void);
132 static int getlinect(void);
133 static void setlinect(int n);
134 static void synerr(char *msg);
135 static int pol_getch(FILE *fp);
136 static void ungets(char *s);
148 meta.con = AMPERSAND;
154 pol.skipchars[0] = EOS;
156 inittable (cmdtable); /* initialize symbol tables */
157 inittable (usertable);
158 inittable (skiptable);
160 for (i = 0; i < NUMCMD; i++)
161 install (cmdtable, cmdlist[i].keyword, cmdlist[i].code);
163 token.ready = FALSE; /* no token read yet */
168 * char *w - word for pol to ignore and skip over in input
170 * pol_tok() compares all tokens to words given to this routine. If it finds it,
171 * it will immediately read another token.
175 pol_skpword (char *w)
177 if (install (skiptable, w, 0) == NULL)
178 synerr ("Too many skip words defined");
183 * skip all characters that appear in string s
186 pol_skpchar (char *s)
188 strncpy (pol.skipchars, s, MAXSKIPCHAR);
191 /* pol_install (str, code)
193 * char *str - token string to install
194 * int code - code to return for token
196 * pol_tok() looks for these user defined tokens. If it finds one,
197 * it stores the tokens code in the token structure and returns TT_USERTOK
200 pol_install (char *str, int code)
202 if (install (usertable, str, code) == NULL)
204 synerr ("Out ot memory installing user tokens");
211 /* get_word - matches tokens on a letter by letter basis
213 * char *search - string to search for
214 * int nlet - maximum number of chars to search for match
218 pol_word (char *search, int nlet)
221 if (pol.trace == TRUE)
222 printf ("matching current token %s against word %s\n", token.tokstr, search);
224 if (strncasecmp (search, token.tokstr, nlet) == 0) {
231 /* pol_usertok (str,code)
232 * see if current token is a user defined token set with pol_install()
234 * char *str - token string as read from input
235 * int *code - returned code for user defined symbol
236 * return value - TRUE if current token has been user defined
237 * FALSE if current token is not user defined
240 pol_usertok (char *str, int *code)
244 if (pol.trace == TRUE)
245 printf ("checking if current token '%s' is user defined\n", token.tokstr);
247 if (token.type == TT_USERTOK) {
249 strcpy (str, token.tokstr);
258 /* isstring (s) - returns TRUE if current token is a string
260 * char *s - pointer to place to store token string
264 pol_string (char *str)
268 if (token.type == TT_STRING) {
269 strcpy (str, token.tokstr);
276 /* pol_integer - test for an integer
278 * int *n: returned integer value
279 * int typecode = TT_INT if accept only integer values
280 * = TT_REAL if accept both real and integer values
281 * int boundcode= TRUE if force to lie between boundries
282 * = FALSE can take any value it likes
283 * int bb1: lower bound
284 * int bb2: upper bound
287 pol_integer (int *n, int typecode, int boundcode, int bb1, int bb2)
291 if (pol.trace == TRUE)
292 printf ("checking if current token %s is an integer\n", token.tokstr);
294 if (token.type == TT_INT || token.type == TT_REAL) {
295 if (boundcode == TRUE) {
296 if (token.inum < bb1)
298 else if (token.inum > bb2)
312 pol_float (double *n, double typecode, double boundcode, double bb1, double bb2)
316 if (pol.trace == TRUE)
317 printf ("checking if current token %s is an floating point number\n", token.tokstr);
319 if (token.type == TT_INT || token.type == TT_REAL) {
320 if (boundcode == TRUE) {
321 if (token.fnum < bb1)
323 else if (token.fnum > bb2)
336 /*----------------------------------------------------------------------*/
337 /* pol_skip() - skip over any token except for end of command sequence */
339 /* returns TRUE if succesful skip */
340 /* returns FALSE if already at end of command or EOF */
341 /*----------------------------------------------------------------------*/
345 char term[5]; /* string of characters not to skip */
348 if (pol.nl_eoc == TRUE) {
354 return (skiptok (term));
357 void pol_reader(void)
359 while (pol_skip() == TRUE)
362 dumptok (&token); /* skip end of command token */
365 /* skiptok (term) - skip a token unless the first character of a token is
366 * in the string of terminators, term.
367 * char *term - string of termination characters, don't skip these characters
368 * skiptok() also does NOT skip TT_EOF
369 * returns (TRUE) if succesful skip of a token
370 * returns (FALSE) if didn't skip, read termination character or TT_EOF
374 skiptok (char term[])
378 if (token.type == TT_EOF
379 || (token.type == TT_SPECLCHAR && strchr(term, token.tokstr[0]) != NULL))
388 pol_tok (struct token_st *token)
390 if (token->ready == FALSE)
393 if (token->type == TT_EOF && pol_lookchar() != EOF)
395 return (token->type);
399 dumptok (struct token_st *token)
401 if (token->ready == FALSE)
403 token->ready = FALSE;
407 getpol_tok (struct token_st *token)
411 token->ready = FALSE;
415 if (token->type == TT_BLANK)
417 if (token->type == TT_SPECLCHAR) {
418 if (strchr(pol.skipchars, token->tokstr[0]) != NULL)
420 if (token->tokstr[0] == NEWLINE)
422 if (token->tokstr[0] == meta.cmd) {
426 if (token->tokstr[0] == meta.com) { /* skip comment */
430 if (token->tokstr[0] == meta.out) {
431 getescape(token->tokstr, meta.out, MAXTOK);
432 fputs (token->tokstr, stderr);
435 if (token->tokstr[0] == meta.con) { /* continuation across NEWLINE */
436 while (pol_lookchar() == BLANK || pol_lookchar() == TAB)
438 if (pol_lookchar() == NEWLINE)
441 if (token->tokstr[0] == meta.ter) { /* get input from terminal */
442 pol_usefile (P_USE_FILE, "");
445 return (token->type);
449 /* look for filler words */
451 if (lookup (skiptable, token->tokstr) != NULL) /* ignore words in skip table */
454 /* look for user defined symbols */
456 if ((sym = lookup (usertable, token->tokstr)) != NULL) {
457 token->type = TT_USERTOK;
458 token->code = sym->code;
462 if (pol.trace == TRUE)
463 printf ("Read token '%s', type = %d\n", token->tokstr, token->type);
465 return (token->type);
469 static int getcmd(void)
476 tt = getalpha (str, MAXTOK);
477 if (tt == TT_ERROR) {
478 synerr ("error in pol parameter command");
482 if ((cmd = lookup (cmdtable,str)) == NULL) {
483 synerr ("unrecognized command");
500 pol_usefile (P_USE_FILE, tok.tokstr);
512 printf("eoc = %c str = %c com = %c cmd = %c prg = %c\n",
513 meta.eoc, meta.str, meta.com, meta.cmd, meta.prg);
514 printf("con = %c out = %c ter = %c inb = %c\n",
515 meta.con, meta.out, meta.ter, meta.inb);
518 if (found == FALSE) {
520 if (tt != TT_SPECLCHAR) {
521 synerr("illegal command character");
526 meta.eoc = tok.tokstr[0];
529 meta.str = tok.tokstr[0];
532 meta.com = tok.tokstr[0];
535 meta.cmd = tok.tokstr[0];
538 meta.prg = tok.tokstr[0];
541 meta.con = tok.tokstr[0];
544 meta.out = tok.tokstr[0];
547 meta.ter = tok.tokstr[0];
550 meta.inb = tok.tokstr[0];
553 printf("command not implemented\n");
555 } /* switch (tok->type) */
556 } /* if (found == FALSE) */
557 pol_reader(); /* clean up command */
558 } /* if legal command */
570 int toksiz = MAXTOK; /* maximum length of token string */
572 while ((c = pol_inchar()) == BLANK || c == TAB)
582 if (c == BLANK || c == TAB) { /* skip white space */
583 getblank(tok->tokstr, toksiz);
585 } else if (toktype == LETTER) {
586 toktype = getalpha (tok->tokstr, toksiz);
587 } else if (c == meta.str) { /* quoted string */
588 getquote (tok->tokstr, toksiz);
590 } else if (type(c) == DIGIT || c == PLUS || c == HYPHEN || c == PERIOD) {
591 toktype = getnumber (tok->tokstr, toksiz, &fnum, &inum);
592 } else if (c == EOF) {
593 tok->tokstr[0] = EOS;
598 tok->tokstr[1] = EOS;
599 toktype = TT_SPECLCHAR;
604 if (tok->type == TT_REAL || tok->type == TT_INT) {
617 getblank (char *s, int toksiz)
621 while ((c = pol_inchar()) == BLANK || c == TAB)
631 getalpha (char *s, int toksiz)
633 int i, chartype, alphatype;
635 if (type(pol_lookchar()) != LETTER) {
640 alphatype = TT_ALPHA;
641 for (i = 0; i < toksiz; i++) { /* get alphanumeric token */
643 chartype = type (s[i]);
644 if (chartype != LETTER && chartype != DIGIT)
646 if (chartype == DIGIT)
647 alphatype = TT_ALPNUM;
652 synerr("token too long.");
654 s[i] = EOS; /* terminate token */
659 /* getquote - get quoted string from file */
660 /* have already gotten delimiter in qs[0] */
662 getquote (char *qs, int toksiz)
666 delim = pol_inchar(); /* char = delimiter */
667 getescape(qs, delim, toksiz);
672 getescape ( /* reads up to delim */
680 for (i = 0; (c = pol_inchar()) != delim; i++) {
682 synerr ("Missing closing delimiter.");
686 synerr("string too long.");
691 synerr("end of file inside quotation");
693 } else if (c == BSLASH) { /* escape character */
695 c = pol_inchar(); /* get escaped character */
703 gettext (char *str, int lim)
707 while ((c = pol_inchar()) == BLANK || c == TAB)
711 for (i = 0; i < lim && (c = pol_inchar()) != EOF && c != NEWLINE; i++)
717 /*----------------------------------------------*/
718 /* Get a number for gettok() */
719 /*----------------------------------------------*/
723 char str[], /* string to return token in */
724 int strsize, /* maximum length of token string */
725 double *fnum, /* floating point value of number read */
726 int *inum /* integer value of number read */
730 double sign, whole, frac, powerof10, exp, expsign;
734 isSigned = FALSE; /* TRUE if number prefixed by '+' or '-' */
744 } else if (c == PLUS) {
748 } else if (c == PERIOD) {
749 if (type(pol_lookchar()) != DIGIT) {
752 return (TT_SPECLCHAR);
754 pol_ungetch (PERIOD);
755 } else if (type(c) != DIGIT) {
761 if (isSigned == TRUE) {
764 pol_inchar(); /* get period */
765 c = pol_lookchar(); /* look at character past period */
766 pol_ungetch (PERIOD); /* put back period */
767 if (type(c) != DIGIT) {
769 return (TT_SPECLCHAR);
771 } else if (type (c) != DIGIT) {
773 return (TT_SPECLCHAR);
778 while (type(c = pol_inchar()) == DIGIT) {
781 whole = 10.0 * whole + (c - '0');
783 pol_ungetch (c); /* put back non-numeric character */
785 if (c != PERIOD && tolower(c) != 'e') {
787 *fnum = whole * sign;
790 else if (*fnum > MAX_INT)
797 if (pol_lookchar() == PERIOD) {
806 while (type(c = pol_inchar()) == DIGIT) {
809 frac += (double) (c - '0') / powerof10;
817 if (tolower(c) != 'e')
822 if ((c = pol_inchar()) == PLUS) {
826 } else if (c == HYPHEN) {
830 } else if (type(c) != DIGIT) {
831 --sp; /* erase 'e' */
839 while (type(c = pol_inchar()) == DIGIT) {
842 exp = 10 * exp + (c - '0');
849 *fnum = sign * (whole + frac) * pow (10.0, expsign * exp);
852 else if (*fnum > MAX_INT)
870 type ( /* return type of ASCII character */
874 if (isalpha(c) || c == UNDERLIN)
882 /*----------------------------------------------------------------------*/
884 /* hash table routines. Kernighan & Ritchie */
886 /*----------------------------------------------------------------------*/
893 inittable (SYMBOL *table[])
897 for (i = 0; i < HASHSIZE; i++)
902 * free all memory allocated to table, then clear table
906 freetable (SYMBOL *table[])
911 for (i = 0; i < HASHSIZE; i++) {
923 hash ( /* form hash value of string s */
929 for (hashval = 0; *s != EOS; )
931 return (hashval % HASHSIZE);
934 /* Look for s in hash table */
936 lookup ( SYMBOL *table[], char *s )
939 SYMBOL *found = NULL;
941 for (np = table[hash(s)]; np != NULL; np = np->next)
942 if (strcasecmp(s, np->name) == 0) {
943 found = np; /* found it */
951 install (SYMBOL *table[], char *name, int def)
953 static char installerr[] = "install: out of memory";
957 if ((np = lookup (table, name)) == NULL) { /* not found */
958 np = (SYMBOL *) malloc (sizeof(*np));
963 if ((np->name = strdup(name)) == NULL) {
967 str_lower (np->name);
969 hashval = hash(np->name);
970 np->next = table[hashval];
972 } else /* already there */
977 /*----------------------------------------------------------------------*/
979 /*----------------------------------------------------------------------*/
983 static int currentf = -1; /* pointer to current fp */
984 static FILE *filep[MAXFILE]; /* == NULL for string input */
985 static char *fname[MAXFILE]; /* pointer to filename */
986 static int linect[MAXFILE]; /* line count in file */
988 static char inputline[MAXLINE]; /* current input line */
989 static int lineptr; /* current position in inputline */
998 static void inc_line(void)
1004 static int getlinect(void)
1006 return (linect[currentf]);
1013 linect[currentf] = n;
1019 fputs (fname[currentf], stderr);
1020 fprintf(stderr, "%d", linect[currentf]);
1021 fputc (COLON, stderr);
1022 fputs (msg, stderr);
1023 fputc (NEWLINE, stderr);
1026 /*----------------------------------------------------------------------*/
1028 /*----------------------------------------------------------------------*/
1031 static int bp = 0; /* pointer to next free position */
1032 static int buf[BUFSIZE]; /* pushed back input characters */
1034 /* pol_usefile - set source of POL input
1036 * int source - source of input
1037 * P_USE_STR - have POL use strings as input
1038 * P_USE_FILE - use file. filename is in str
1043 pol_usefile (int source, char *fn)
1048 if (currentf >= MAXFILE) {
1050 synerr ("files nested too deeply");
1054 bp = 0; /* clear any pushed back input */
1056 if (source == P_USE_STR) {
1057 filep[currentf] = NULL;
1058 linect[currentf] = 1;
1059 } else if (source == P_USE_FILE) {
1060 if (fn == NULL || strlen(fn) == 0) {
1062 } else if ((fp = fopen(fn, "r")) == NULL) {
1064 synerr ("can't open file");
1067 filep[currentf] = fp;
1068 linect[currentf] = 1;
1069 fname[currentf] = strdup (fn);
1073 void pol_closefile(void)
1075 if (currentf >= 0) {
1076 if (filep[currentf] != NULL)
1077 fclose (filep[currentf]);
1082 /*-----------------------------*/
1083 /* Lowest Level Input Routines */
1084 /*-----------------------------*/
1087 int pol_lookchar(void)
1096 int pol_inchar(void)
1103 while (currentf >= 0 && (c = pol_getch(filep[currentf])) == EOF && filep[currentf] != NULL) {
1110 /*--------------------------------------------------------------*/
1111 /* getch - get a (possibly pushed back) character */
1112 /* if fp == NULL, then get character from inputline */
1113 /*--------------------------------------------------------------*/
1116 pol_getch (FILE *fp)
1124 if ((c = inputline[lineptr]) == EOS)
1136 /* push character back on input */
1141 sys_error (ERR_SEVERE, "too many characters pushed back [pol_ungetch]");
1147 /* push back string onto input */
1153 for (i = strlen(s) - 1; i >= 0; i--)
1158 get_inputline (FILE *fp)
1162 if (fgets (inputline, MAXLINE, fp) == NULL)
1169 set_inputline (char *line)
1173 strncpy (inputline, line, MAXLINE);