From: erg Date: Fri, 8 Apr 2005 20:45:34 +0000 (+0000) Subject: Update with new lefty, fixing many bugs and supporting new features X-Git-Tag: LAST_LIBGRAPH~32^2~7703 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=070479236d25977cde7d8306635695af2fb7c203;p=graphviz Update with new lefty, fixing many bugs and supporting new features --- diff --git a/cmd/lefty/lefty.c b/cmd/lefty/lefty.c index 51432f2e2..7423a894a 100644 --- a/cmd/lefty/lefty.c +++ b/cmd/lefty/lefty.c @@ -1,4 +1,4 @@ -/* $Id$ $Revision$ */ +/* $Id$ $Revision$ */ /* vim:set shiftwidth=4 ts=8: */ /********************************************************** @@ -14,13 +14,9 @@ * AT&T Research, Florham Park NJ * **********************************************************/ +/* Lefteris Koutsofios - AT&T Labs Research */ -/* Lefteris Koutsofios - AT&T Bell Laboratories */ - -#ifdef FEATURE_GTK -#include -#include -#endif +#define LEFTYVERSION "10 Mar 2005" #include "common.h" #include "g.h" @@ -35,55 +31,35 @@ #include "internal.h" #include "txtview.h" #include "gfxview.h" + #ifdef FEATURE_GMAP #include "gmap.h" #include "gmap2l.h" #endif + #ifndef FEATURE_MS #include -#ifdef HAVE_UNISTD_H -#include -#include +# ifdef STATS +# include +# endif #endif -#ifdef STATS -#include -#endif -#endif - -/* the gnu way is to test for features explicitly */ -#ifdef HAVE_FILE_R -#define canread(fp) ((fp)->_r > 0) -#else -#ifdef HAVE_FILE_IO_READ_END -#define canread(fp) ((fp)->_IO_read_end > (fp)->_IO_read_ptr) -#else -#ifdef HAVE_FILE_CNT -#define canread(fp) ((fp)->_cnt > 0) -#else -#ifdef HAVE_FILE_NEXT -#define canread(fp) ((fp)->next < (fp)->endb) -#else -#ifdef HAVE___FREADABLE -#define canread(fp) (__freadable(fp)) -/* the other way is to presume a feature based on OS */ +#if HAVE_FILE_CNT +#define canread(f) ((f)->_cnt>0) #else -#ifdef FEATURE_CS -#define canread(fp) ((fp)->next < (fp)->endb) -#else -#ifdef FEATURE_GNU -#define canread(fp) ((fp)->_IO_read_end > (fp)->_IO_read_ptr) -#else -#if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__Mac_OSX__) -#define canread(fp) ((fp)->_r > 0) -#endif -#endif -#endif -#endif - -#endif -#endif -#endif +# if HAVE_FILE_NEXT +# define canread(f) ((f)->_next<(f)->_endb) +# else +# if HAVE_FILE_IO_READ_END +# define canread(f) ((f)->_IO_read_end>(f)->_IO_read_ptr) +# else +# if HAVE_R_IN_FILE +# define canread(f) ((f)->_r>0) +# else +# define canread(f) (1) +# endif +# endif +# endif #endif #ifdef FEATURE_GMAP @@ -91,7 +67,7 @@ static int gmapon; #endif static Grect_t txtcoords = { - {1, 1}, {449, 599}, + { 1, 1 }, { 449, 599 }, }; #ifdef STATS @@ -103,15 +79,14 @@ static int endflag = FALSE; static char *exprstr; static FILE *fp; -static int processinput(int); -static void processargs(int, char **); -static void processstr(char *); -static void printusage(void); +static int processinput (int); +static void processargs (int, char **); +static void processstr (char *); +static void printusage (void); -#if defined(FEATURE_X11) || defined(FEATURE_NONE) || defined(FEATURE_GTK) +#if defined(FEATURE_X11) || defined(FEATURE_NONE) -int main(int argc, char **argv) -{ +int main (int argc, char **argv) { Tobj co; Psrc_t src; @@ -120,159 +95,124 @@ int main(int argc, char **argv) Mt_certify = 1; #endif #ifdef STATS - stime = time(NULL); + stime = time (NULL); #endif idlerunmode = 0; exprstr = NULL; fp = NULL; - init(argv[0]); - Minit(GFXprune); - Ginit(); - FD_SET(Gxfd, &inputfds); + init (argv[0]); + Minit (GFXprune); + Ginit (); + FD_SET (Gxfd, &inputfds); Eerrlevel = 1; Estackdepth = 2; Eshowbody = 1; Eshowcalls = 1; - processstr(leftyoptions); + processstr (leftyoptions); argv++, argc--; - processargs(argc, argv); - - if (setjmp(exitljbuf)) - goto eop; - - Cinit(); - IOinit(); - Tinit(); - Pinit(); - Einit(); - Sinit(); - Dinit(); - Iinit(); - TXTinit(txtcoords); - GFXinit(); + processargs (argc, argv); + + if (setjmp (exitljbuf)) + goto eop; + + Cinit (); + IOinit (); + Tinit (); + Pinit (); + Einit (); + Sinit (); + Dinit (); + Iinit (); + TXTinit (txtcoords); + GFXinit (); #ifdef FEATURE_GMAP - gmapon = TRUE, G2Linit(); + gmapon = TRUE, G2Linit (); #endif if (exprstr) { - src.flag = CHARSRC, src.s = exprstr, src.fp = NULL; - src.tok = -1, src.lnum = 1; - while ((co = Punit(&src))) - Eunit(co); + src.flag = CHARSRC, src.s = exprstr, src.fp = NULL; + src.tok = -1, src.lnum = 1; + while ((co = Punit (&src))) + Eunit (co); } if (fp) { - src.flag = FILESRC, src.s = NULL, src.fp = fp; - src.tok = -1, src.lnum = 1; - while ((co = Punit(&src))) - Eunit(co); + src.flag = FILESRC, src.s = NULL, src.fp = fp; + src.tok = -1, src.lnum = 1; + while ((co = Punit (&src))) + Eunit (co); } if (endflag) - goto eop; - - TXTupdate(); - + goto eop; -#ifdef FEATURE_GTK - while (gtk_events_pending()) { - gtk_main_iteration(); - } + TXTupdate (); Gneedredraw = FALSE; - while (TRUE) { - if (gtk_events_pending()) { - GdkEvent *eve; - - gtk_main_iteration(); - if (Gneedredraw) - GFXredraw(), Gneedredraw = FALSE; - if (Gbuttonsdown > 0) { - GFXmove(), Gprocessevents(FALSE, G_ONEEVENT); - processinput(FALSE); - } else { - if (Mcouldgc) { - if (!processinput(FALSE)) - Mdogc(M_GCINCR); - } else if (idlerunmode) { - if (!processinput(FALSE)) - GFXidle(); - } else - processinput(TRUE); - } + for (;;) { + if (Gneedredraw) + GFXredraw (), Gneedredraw = FALSE; + if (Gbuttonsdown > 0) { + GFXmove (), Gprocessevents (FALSE, G_ONEEVENT); + processinput (FALSE); + } else { + if (Mcouldgc) { + if (!processinput (FALSE)) + Mdogc (M_GCINCR); + } + if (idlerunmode) { + if (!processinput (FALSE)) + GFXidle (); #ifdef FEATURE_GMAP - if (gmapon) - GMAPupdate(); + } else if (GMAPneedupdate) { + processinput (FALSE); #endif - if (Erun) - TXTupdate(), Erun = FALSE; - } - } -#else - - Gneedredraw = FALSE; - for (;;) { - if (Gneedredraw) - GFXredraw(), Gneedredraw = FALSE; - if (Gbuttonsdown > 0) { - GFXmove(), Gprocessevents(FALSE, G_ONEEVENT); - processinput(FALSE); - } else { - if (Mcouldgc) { - if (!processinput(FALSE)) - Mdogc(M_GCINCR); - } - if (idlerunmode) { - if (!processinput(FALSE)) - GFXidle(); - } else - processinput(TRUE); - } + } else + processinput (TRUE); + } #ifdef FEATURE_GMAP - if (gmapon) - GMAPupdate(); + if (gmapon) + GMAPupdate (); #endif - if (Erun) - TXTupdate(), Erun = FALSE; + if (Erun) + TXTupdate (), Erun = FALSE; } -#endif /* end of else part of FEATURE_GTK */ - - eop: +eop: #ifdef PARANOID #ifdef FEATURE_GMAP if (gmapon) - G2Lterm(); + G2Lterm (); #endif - GFXterm(); - TXTterm(); - Iterm(); - Dterm(); - Sterm(); - Eterm(); - Pterm(); - Tterm(); - IOterm(); - Cterm(); - Gterm(); - Mterm(); - FD_CLR(Gxfd, &inputfds); - term(); + GFXterm (); + TXTterm (); + Iterm (); + Dterm (); + Sterm (); + Eterm (); + Pterm (); + Tterm (); + IOterm (); + Cterm (); + Gterm (); + Mterm (); + FD_CLR (Gxfd, &inputfds); + term (); #endif - printusage(); + printusage (); return 0; } #else -extern char **__argv; +extern char ** __argv; extern int __argc; HANDLE hinstance, hprevinstance; -int PASCAL WinMain(HANDLE hInstance, HANDLE hPrevInstance, - LPSTR lpCmdLine, int nCmdShow) -{ +int PASCAL WinMain ( + HANDLE hInstance, HANDLE hPrevInstance, LPSTR lpCmdLine, int nCmdShow +) { Tobj co; Psrc_t src; @@ -281,10 +221,10 @@ int PASCAL WinMain(HANDLE hInstance, HANDLE hPrevInstance, idlerunmode = 0; exprstr = NULL; fp = NULL; - init(NULL); - Ginit(); + init (NULL); + Ginit (); #ifndef FEATURE_MS - FD_SET(Gxfd, &inputfds); + FD_SET (Gxfd, &inputfds); #endif Eerrlevel = 1; @@ -292,80 +232,80 @@ int PASCAL WinMain(HANDLE hInstance, HANDLE hPrevInstance, Eshowbody = 1; Eshowcalls = 1; - processstr(leftyoptions); + processstr (leftyoptions); __argv++, __argc--; - processargs(__argc, __argv); - - if (setjmp(exitljbuf)) - goto eop; - - Cinit(); - IOinit(); - Minit(GFXprune); - Tinit(); - Pinit(); - Einit(); - Sinit(); - Dinit(); - Iinit(); - TXTinit(txtcoords); - GFXinit(); + processargs (__argc, __argv); + + if (setjmp (exitljbuf)) + goto eop; + + Cinit (); + IOinit (); + Minit (GFXprune); + Tinit (); + Pinit (); + Einit (); + Sinit (); + Dinit (); + Iinit (); + TXTinit (txtcoords); + GFXinit (); if (exprstr) { - src.flag = CHARSRC, src.s = exprstr, src.fp = NULL; - src.tok = -1, src.lnum = 1; - while ((co = Punit(&src))) - Eunit(co); + src.flag = CHARSRC, src.s = exprstr, src.fp = NULL; + src.tok = -1, src.lnum = 1; + while ((co = Punit (&src))) + Eunit (co); } if (fp) { - src.flag = FILESRC, src.s = NULL, src.fp = fp; - src.tok = -1, src.lnum = 1; - while ((co = Punit(&src))) - Eunit(co); + src.flag = FILESRC, src.s = NULL, src.fp = fp; + src.tok = -1, src.lnum = 1; + while ((co = Punit (&src))) + Eunit (co); } if (endflag) - goto eop; + goto eop; - TXTupdate(); + TXTupdate (); Gneedredraw = FALSE; for (;;) { - if (Gneedredraw) - GFXredraw(), Gneedredraw = FALSE; - if (Gbuttonsdown > 0) - GFXmove(), Gprocessevents(FALSE, G_ONEEVENT); - else { - if (Mcouldgc) { - if (!processinput(FALSE)) - Mdogc(M_GCINCR); - } else if (idlerunmode) { - if (!processinput(FALSE)) - GFXidle(); - } else - processinput(TRUE); - } - if (Erun) - TXTupdate(), Erun = FALSE; + if (Gneedredraw) + GFXredraw (), Gneedredraw = FALSE; + if (Gbuttonsdown > 0) + GFXmove (), Gprocessevents (FALSE, G_ONEEVENT); + else { + if (Mcouldgc) { + if (!processinput (FALSE)) + Mdogc (M_GCINCR); + } else if (idlerunmode) { + if (!processinput (FALSE)) + GFXidle (); + } else + processinput (TRUE); + } + if (Erun) + TXTupdate (), Erun = FALSE; } - eop: +eop: #ifdef PARANOID - GFXterm(); - TXTterm(); - Iterm(); - Dterm(); - Sterm(); - Eterm(); - Pterm(); - Tterm(); - Mterm(); - IOterm(); - Cterm(); - Gterm(); - term(); + GFXterm (); + TXTterm (); + Iterm (); + Dterm (); + Sterm (); + Eterm (); + Pterm (); + Tterm (); + Mterm (); + IOterm (); + Cterm (); + Gterm (); + term (); #endif - printusage(); - exit(0); + printusage (); + exit (0); } #endif @@ -375,8 +315,7 @@ static struct timeval longwait = { 1000, 0 }; static struct timeval zerowait = { 0, 0 }; #endif -static int processinput(int waitflag) -{ +static int processinput (int waitflag) { fd_set fdset; #ifndef FEATURE_MS struct timeval tz, *tzp; @@ -388,147 +327,162 @@ static int processinput(int waitflag) int ioi; rtn = 0; - while (Gprocessevents(FALSE, G_MANYEVENTS)) - rtn = 1; + while (Gprocessevents (FALSE, G_MANYEVENTS)) + rtn = 1; #ifndef FEATURE_MS for (ioi = 0, n = 0; ioi < ion; ioi++) - if (iop[ioi].inuse && iop[ioi].ismonitored && - FD_ISSET(fileno(iop[ioi].ifp), &inputfds) && - canread(iop[ioi].ifp)) - GFXmonitorfile(ioi), n++; + if ( + iop[ioi].inuse && iop[ioi].ismonitored && + FD_ISSET (fileno (iop[ioi].ifp), &inputfds) && + canread (iop[ioi].ifp) + ) + GFXmonitorfile (ioi), n++; if (n || rtn) - return 1; + return 1; if (Gneedredraw) - return 0; + return 0; tz = (waitflag && !rtn) ? longwait : zerowait; tzp = &tz; fdset = inputfds; - if ((n = select(FD_SETSIZE, &fdset, NULL, NULL, tzp)) <= 0) - return rtn; + if ((n = select (FD_SETSIZE, &fdset, NULL, NULL, tzp)) <= 0) + return rtn; rtn = 1; - if (FD_ISSET(Gxfd, &fdset)) - Gprocessevents(TRUE, G_MANYEVENTS), n--; + if (FD_ISSET (Gxfd, &fdset)) + Gprocessevents (TRUE, G_MANYEVENTS), n--; if (!n) - return rtn; - Gsync(); + return rtn; + Gsync (); for (ioi = 0; n > 0 && ioi < ion; ioi++) - if (iop[ioi].inuse && iop[ioi].ismonitored && - FD_ISSET(fileno(iop[ioi].ifp), &fdset)) - GFXmonitorfile(ioi), n--; + if ( + iop[ioi].inuse && iop[ioi].ismonitored && + FD_ISSET (fileno (iop[ioi].ifp), &fdset) + ) + GFXmonitorfile (ioi), n--; #else for (ioi = 0, n = 0, evn = 0; ioi < ion; ioi++) { - if (!iop[ioi].inuse || !IOismonitored(ioi, inputfds)) - continue; - if ((iop[ioi].type == IO_FILE && canread(iop[ioi].ifp)) || - (iop[ioi].type == IO_PIPE && iop[ioi].buf[0])) - GFXmonitorfile(ioi), n++; - if (iop[ioi].type != IO_PIPE) - continue; - evs[evn++] = iop[ioi].ifp; + if (!iop[ioi].inuse || !IOismonitored (ioi, inputfds)) + continue; + if ( + (iop[ioi].type == IO_FILE && canread (iop[ioi].ifp)) || + (iop[ioi].type == IO_PIPE && iop[ioi].buf[0]) + ) + GFXmonitorfile (ioi), n++; + if (iop[ioi].type != IO_PIPE) + continue; + evs[evn++] = iop[ioi].ifp; } if (n) - return 1; + return 1; if (Gneedredraw) - return 0; - n = MsgWaitForMultipleObjects(evn, evs, FALSE, - (waitflag && !rtn) ? 1 : 0, QS_ALLINPUT); + return 0; + n = MsgWaitForMultipleObjects ( + evn, evs, FALSE, (waitflag && !rtn) ? 1 : 0, QS_ALLINPUT + ); if (n == WAIT_TIMEOUT || n < WAIT_OBJECT_0 || n > WAIT_OBJECT_0 + evn) - return rtn; + return rtn; if (n == WAIT_OBJECT_0 + evn) - Gprocessevents(TRUE, G_MANYEVENTS); - Gsync(); + Gprocessevents (TRUE, G_MANYEVENTS); + Gsync (); for (ioi = 0; ioi < ion; ioi++) - if (iop[ioi].inuse && IOismonitored(ioi) && - (iop[ioi].type == IO_FILE || (iop[ioi].type == IO_PIPE && - evs[n - WAIT_OBJECT_0] == - iop[ioi].ifp))) - GFXmonitorfile(ioi); + if ( + iop[ioi].inuse && IOismonitored (ioi) && + (iop[ioi].type == IO_FILE || (iop[ioi].type == IO_PIPE && + evs[n - WAIT_OBJECT_0] == iop[ioi].ifp)) + ) + GFXmonitorfile (ioi); #endif return rtn; } -static void processstr(char *buf) -{ +static void processstr (char *buf) { char *words[100]; char *s, *s1; int i; if (!(s = buf) || *s == 0) - return; - s = strdup(s); - for (i = 0, s1 = s; *s1;) { - for (; *s1 && *s1 == ' '; s1++); - if (!*s1) - break; - words[i++] = s1; - for (; *s1 && *s1 != ' '; s1++); - if (*s1) - *s1 = '\000', s1++; + return; + s = strdup (s); + for (i = 0, s1 = s; *s1; ) { + for (; *s1 && *s1 == ' '; s1++) + ; + if (!*s1) + break; + words[i++] = s1; + for (; *s1 && *s1 != ' '; s1++) + ; + if (*s1) + *s1 = '\000', s1++; } words[i] = NULL; - processargs(i, words); - free(s); + processargs (i, words); + free (s); } -static void processargs(int argc, char *argv[]) -{ +static void processargs (int argc, char *argv[]) { while (argc) { - if (Strcmp(argv[0], "-x") == 0) - endflag = TRUE; - else if (Strcmp(argv[0], "-e") == 0) - exprstr = argv[1], argv++, argc--; - else if (Strcmp(argv[0], "-el") == 0) - Eerrlevel = atoi(argv[1]), argv++, argc--; - else if (Strcmp(argv[0], "-sd") == 0) - Estackdepth = atoi(argv[1]), argv++, argc--; - else if (Strcmp(argv[0], "-sb") == 0) - Eshowbody = atoi(argv[1]), argv++, argc--; - else if (Strcmp(argv[0], "-sc") == 0) - Eshowcalls = atoi(argv[1]), argv++, argc--; - else if (Strcmp(argv[0], "-df") == 0) - Gdefaultfont = argv[1], argv++, argc--; - else if (Strcmp(argv[0], "-w") == 0) - warnflag = TRUE; - else if (Strcmp(argv[0], "-ps") == 0) - Gpscanvasname = argv[1], argv++, argc--; - else if (Strcmp(argv[0], "-V") == 0) - fprintf(stderr, "lefty version %s (%s)\n", VERSION, BUILDDATE); - else if (Strcmp(argv[0], "-") == 0) - fp = stdin; - else { - if ((fp = fopen(argv[0], "r")) == NULL) - panic(POS, "main", "cannot open input file: %s", argv[0]); - } - argv++, argc--; + if (strcmp (argv[0], "-x") == 0) + endflag = TRUE; + else if (strcmp (argv[0], "-e") == 0) + exprstr = argv[1], argv++, argc--; + else if (strcmp (argv[0], "-el") == 0) + Eerrlevel = atoi (argv[1]), argv++, argc--; + else if (strcmp (argv[0], "-sd") == 0) + Estackdepth = atoi (argv[1]), argv++, argc--; + else if (strcmp (argv[0], "-sb") == 0) + Eshowbody = atoi (argv[1]), argv++, argc--; + else if (strcmp (argv[0], "-sc") == 0) + Eshowcalls = atoi (argv[1]), argv++, argc--; + else if (strcmp (argv[0], "-df") == 0) + Gdefaultfont = argv[1], argv++, argc--; + else if (strcmp (argv[0], "-w") == 0) + warnflag = TRUE; + else if (strcmp (argv[0], "-ps") == 0) + Gpscanvasname = argv[1], argv++, argc--; + else if (strcmp (argv[0], "-V") == 0) + fprintf (stderr, "lefty version %s\n", LEFTYVERSION); + else if (strcmp (argv[0], "-") == 0) + fp = stdin; + else { + if ((fp = fopen (argv[0], "r")) == NULL) + panic (POS, "main", "cannot open input file: %s", argv[0]); + } + argv++, argc--; } if (Eerrlevel > 1) - Gerrflag = TRUE; + Gerrflag = TRUE; } -static void printusage(void) -{ +static void printusage (void) { #ifdef STATS struct rusage ru; - getrusage(RUSAGE_SELF, &ru); - Mreport(); + getrusage (RUSAGE_SELF, &ru); + Mreport (); #ifdef FEATURE_RUSAGE - printf("user time %13.3lf\n", - ru.ru_utime.tv_sec + ru.ru_utime.tv_nsec / 1000000000.0); - printf("system time %13.3lf\n", - ru.ru_stime.tv_sec + ru.ru_stime.tv_nsec / 1000000000.0); + printf ( + "user time %13.3lf\n", + ru.ru_utime.tv_sec + ru.ru_utime.tv_nsec / 1000000000.0 + ); + printf ( + "system time %13.3lf\n", + ru.ru_stime.tv_sec + ru.ru_stime.tv_nsec / 1000000000.0 + ); #else - printf("user time %13.3lf\n", - ru.ru_utime.tv_sec + ru.ru_utime.tv_usec / 1000000.0); - printf("system time %13.3lf\n", - ru.ru_stime.tv_sec + ru.ru_stime.tv_usec / 1000000.0); + printf ( + "user time %13.3lf\n", + ru.ru_utime.tv_sec + ru.ru_utime.tv_usec / 1000000.0 + ); + printf ( + "system time %13.3lf\n", + ru.ru_stime.tv_sec + ru.ru_stime.tv_usec / 1000000.0 + ); #endif - printf("resident size %10d\n", ru.ru_maxrss * getpagesize()); - printf("input %8d\n", ru.ru_inblock); - printf("output %8d\n", ru.ru_oublock); - printf("socket msgs sent %8d\n", ru.ru_msgsnd); - printf("socket msgs rcvd %8d\n", ru.ru_msgrcv); - printf("real time %8d\n", time(NULL) - stime); - fflush(stdout); + printf ("resident size %10d\n", ru.ru_maxrss * getpagesize ()); + printf ("input %8d\n", ru.ru_inblock); + printf ("output %8d\n", ru.ru_oublock); + printf ("socket msgs sent %8d\n", ru.ru_msgsnd); + printf ("socket msgs rcvd %8d\n", ru.ru_msgrcv); + printf ("real time %8d\n", time (NULL) - stime); + fflush (stdout); #endif } diff --git a/cmd/lefty/lex.c b/cmd/lefty/lex.c index 31c61a19c..fd3b2282a 100644 --- a/cmd/lefty/lex.c +++ b/cmd/lefty/lex.c @@ -14,13 +14,12 @@ * AT&T Research, Florham Park NJ * **********************************************************/ - -/* Lefteris Koutsofios - AT&T Bell Laboratories */ +/* Lefteris Koutsofios - AT&T Labs Research */ #include "common.h" #include "lex.h" -Ltype_t Ltok; +int Ltok; char Lstrtok[MAXTOKEN]; Lname_t Lnames[] = { @@ -78,216 +77,202 @@ static struct keyword { char *str; int tok; } keywords[] = { - { - "function", L_FUNCTION}, { - "local", L_LOCAL}, { - "if", L_IF}, { - "else", L_ELSE}, { - "while", L_WHILE}, { - "for", L_FOR}, { - "in", L_IN}, { - "break", L_BREAK}, { - "continue", L_CONTINUE}, { - "return", L_RETURN}, { - "internal", L_INTERNAL}, { - NULL, 0} + { "function", L_FUNCTION, }, + { "local", L_LOCAL, }, + { "if", L_IF, }, + { "else", L_ELSE, }, + { "while", L_WHILE, }, + { "for", L_FOR, }, + { "in", L_IN, }, + { "break", L_BREAK, }, + { "continue", L_CONTINUE, }, + { "return", L_RETURN, }, + { "internal", L_INTERNAL, }, + { NULL, 0, }, }; /* single character token mapping */ static struct keychar { int chr, tok; } keychars[] = { - { - ';', L_SEMI}, { - '|', L_OR}, { - '&', L_AND}, { - '+', L_PLUS}, { - '-', L_MINUS}, { - '*', L_MUL}, { - '/', L_DIV}, { - '%', L_MOD}, { - '.', L_DOT}, { - '[', L_LB}, { - ']', L_RB}, { - '(', L_LP}, { - ')', L_RP}, { - '{', L_LCB}, { - '}', L_RCB}, { - ':', L_COLON}, { - ',', L_COMMA}, { - '\000', 0} + { ';', L_SEMI }, + { '|', L_OR }, + { '&', L_AND }, + { '+', L_PLUS }, + { '-', L_MINUS }, + { '*', L_MUL }, + { '/', L_DIV }, + { '%', L_MOD }, + { '.', L_DOT }, + { '[', L_LB }, + { ']', L_RB }, + { '(', L_LP }, + { ')', L_RP }, + { '{', L_LCB }, + { '}', L_RCB }, + { ':', L_COLON }, + { ',', L_COMMA }, + { '\000', 0 } }; -static Ltype_t gtok(void); -static int sgetc(void); -static void sungetc(void); +static int gtok (void); +static int sgetc (void); +static void sungetc (void); -void Lsetsrc(int src, char *s, FILE * fp, int tok, int lnum) -{ +void Lsetsrc (int src, char *s, FILE *fp, int tok, int lnum) { if (src == CHARSRC) - unitp = ucp = s; + unitp = ucp = s; else if (src == FILESRC) - unitp = ucp = unitbuf, unitbuf[0] = '\000', lfp = fp; + unitp = ucp = unitbuf, unitbuf[0] = '\000', lfp = fp; lsrc = src; linenum = lnum; seeneof = FALSE; if (tok == -1 || tok == L_EOF) - Ltok = gtok(); + Ltok = gtok (); else - Ltok = (Ltype_t) tok; + Ltok = tok; } -void Lgetsrc(int *srcp, char **sp, FILE ** fpp, int *tokp, int *lnump) -{ +void Lgetsrc (int *srcp, char **sp, FILE **fpp, int *tokp, int *lnump) { *srcp = lsrc; *sp = ucp; *fpp = lfp; *tokp = Ltok; if (*ucp && *ucp == '\n') - linenum++; + linenum++; *lnump = linenum; } -void Lprintpos(void) -{ +void Lprintpos (void) { char *s1, *s2; char c; - printf("at line %d: ", linenum); - for (s1 = ucp; s1 > unitp && *s1 != '\n'; s1--); - for (s2 = ucp; *s2 && *s2 != '\n'; s2++); + printf ("at line %d: ", linenum); + for (s1 = ucp; s1 > unitp && *s1 != '\n'; s1--) + ; + for (s2 = ucp; *s2 && *s2 != '\n'; s2++) + ; c = *s2, *s2 = '\000'; - printf("%s\n", s1); + printf ("%s\n", s1); *s2 = c; } -void Lgtok(void) -{ - Ltok = gtok(); +void Lgtok (void) { + Ltok = gtok (); } -static Ltype_t gtok(void) -{ +static int gtok (void) { struct keyword *kwp; struct keychar *kcp; int c, qc, nc; char *p; - while ((c = sgetc()) != EOF) { - if (c == '#') - while ((c = sgetc()) != '\n'); - if (c != ' ' && c != '\t' && c != '\n') - break; + while ((c = sgetc ()) != EOF) { + if (c == '#') + while ((c = sgetc ()) != '\n') + ; + if (c != ' ' && c != '\t' && c != '\n') + break; } if (c == EOF) - return L_EOF; + return L_EOF; /* check for keywords and identifiers */ - if (isalpha(c) || c == '_') { - p = &Lstrtok[0], *p++ = c; - while (isalpha((c = sgetc())) || isdigit(c) || c == '_') - *p++ = c; - sungetc(); - *p = '\000'; - for (kwp = &keywords[0]; kwp->str; kwp++) - if (Strcmp(kwp->str, Lstrtok) == 0) - return (Ltype_t) (kwp->tok); - return L_ID; + if (isalpha (c) || c == '_') { + p = &Lstrtok[0], *p++ = c; + while (isalpha ((c = sgetc ())) || isdigit (c) || c == '_') + *p++ = c; + sungetc (); + *p = '\000'; + for (kwp = &keywords[0]; kwp->str; kwp++) + if (strcmp (kwp->str, Lstrtok) == 0) + return kwp->tok; + return L_ID; } /* check for number constant */ - if (isdigit(c)) { - p = &Lstrtok[0], *p++ = c; - while (isdigit((c = sgetc()))) - *p++ = c; - if (c == '.') { - *p++ = c; - while (isdigit((c = sgetc()))) - *p++ = c; - } - sungetc(); - *p = '\000'; - return L_NUMBER; + if (isdigit (c)) { + p = &Lstrtok[0], *p++ = c; + while (isdigit ((c = sgetc ()))) + *p++ = c; + if (c == '.') { + *p++ = c; + while (isdigit ((c = sgetc ()))) + *p++ = c; + } + sungetc (); + *p = '\000'; + return L_NUMBER; } /* check for string constants */ if (c == '"' || c == '\'') { - p = &Lstrtok[0]; - qc = c; - while ((c = sgetc()) != EOF && c != qc) - *p++ = c; /* FIXME: deal with \'s */ - if (c == EOF) - return L_EOF; - *p = '\000'; - return L_STRING; + p = &Lstrtok[0]; + qc = c; + while ((c = sgetc ()) != EOF && c != qc) + *p++ = c; /* FIXME: deal with \'s */ + if (c == EOF) + return L_EOF; + *p = '\000'; + return L_STRING; } /* check for single letter keywords */ for (kcp = &keychars[0]; kcp->chr; kcp++) - if (kcp->chr == c) - return (Ltype_t) (kcp->tok); + if (kcp->chr == c) + return kcp->tok; /* check for 2/1 letter keywords */ if (c == '=' || c == '~' || c == '<' || c == '>') { - nc = sgetc(); - if (nc == '=') { - switch (c) { - case '=': - return L_EQ; - case '~': - return L_NE; - case '<': - return L_LE; - case '>': - return L_GE; - } - } else { - sungetc(); - switch (c) { - case '=': - return L_ASSIGN; - case '~': - return L_NOT; - case '<': - return L_LT; - case '>': - return L_GT; - } - } + nc = sgetc (); + if (nc == '=') { + switch (c) { + case '=': return L_EQ; + case '~': return L_NE; + case '<': return L_LE; + case '>': return L_GE; + } + } else { + sungetc (); + switch (c) { + case '=': return L_ASSIGN; + case '~': return L_NOT; + case '<': return L_LT; + case '>': return L_GT; + } + } } return L_EOF; } -static int sgetc(void) -{ +static int sgetc (void) { if (seeneof) - return EOF; + return EOF; if (*ucp == '\000') { - if (lsrc == CHARSRC) { - seeneof = TRUE; - linenum++; - return EOF; - } else if (lsrc == FILESRC) { - if (ucp != unitp) - *unitp = *(ucp - 1), ucp = unitp + 1; - else - ucp = unitp; - *ucp = '\000'; - if (!fgets(ucp, MAXBUF - (ucp - unitp), lfp)) { - seeneof = TRUE; - return EOF; - } - } + if (lsrc == CHARSRC) { + seeneof = TRUE; + linenum++; + return EOF; + } else if (lsrc == FILESRC) { + if (ucp != unitp) + *unitp = *(ucp - 1), ucp = unitp + 1; + else + ucp = unitp; + *ucp = '\000'; + if (!fgets (ucp, MAXBUF - (ucp - unitp), lfp)) { + seeneof = TRUE; + return EOF; + } + } } if (*ucp == '\n') - linenum++; + linenum++; return *ucp++; } -static void sungetc(void) -{ +static void sungetc (void) { if (seeneof) { - seeneof = FALSE; - return; + seeneof = FALSE; + return; } if (ucp == unitp) - panic(POS, "sungetc", "unget before start of string"); + panic (POS, "sungetc", "unget before start of string"); ucp--; if (*ucp == '\n') - linenum--; + linenum--; }