From 553b6890624d04e64ca0973d8fe86b107cb363b7 Mon Sep 17 00:00:00 2001 From: "David M. Rogers" Date: Wed, 13 Oct 2021 11:45:42 -0400 Subject: [PATCH] Moved global variables into globals.c --- Makefile | 1 + code.c | 1 - exec.c | 1 - exec.h | 16 ++++++++++------ globals.c | 30 ++++++++++++++++++++++++++++++ io.h | 2 +- lex.c | 14 ++++++-------- rc.h | 33 ++++++++++++++++++--------------- subr.c | 1 - 9 files changed, 66 insertions(+), 33 deletions(-) create mode 100644 globals.c diff --git a/Makefile b/Makefile index 6de563b..c8b7566 100644 --- a/Makefile +++ b/Makefile @@ -3,6 +3,7 @@ include Make.$(shell uname) TARG=rc OFILES=\ + globals.$O \ code.$O\ exec.$O\ getflags.$O\ diff --git a/code.c b/code.c index eeaa3ed..876da4b 100644 --- a/code.c +++ b/code.c @@ -15,7 +15,6 @@ char *fnstr(tree*); void outcode(tree*, int); void codeswitch(tree*, int); int iscase(tree*); -code *codecopy(code*); void codefree(code*); int diff --git a/exec.c b/exec.c index 3ad8a0d..a24cd74 100644 --- a/exec.c +++ b/exec.c @@ -6,7 +6,6 @@ /* * Start executing the given code at the given pc with the given redirection */ -char *argv0="rc"; void start(code *c, int pc, var *local) diff --git a/exec.h b/exec.h index 06d2991..fcab495 100644 --- a/exec.h +++ b/exec.h @@ -56,18 +56,22 @@ struct thread{ tree *treenodes; /* tree nodes created by this process */ thread *ret; /* who continues when this finishes */ }; -thread *runq; + code *codecopy(code*); -code *codebuf; /* compiler output */ -int ntrap; /* number of outstanding traps */ -int trap[NSIG]; /* number of outstanding traps per type */ + +/* globals.c */ +extern thread *runq; +extern code *codebuf; /* compiler output */ +extern int ntrap; /* number of outstanding traps */ +extern int trap[NSIG]; /* number of outstanding traps per type */ +extern int eflagok; /* kludge flag so that -e doesn't exit in startup */ +extern int havefork; + struct builtin{ char *name; void (*fnc)(void); }; extern struct builtin Builtin[]; -int eflagok; /* kludge flag so that -e doesn't exit in startup */ -int havefork; void execcd(void), execwhatis(void), execeval(void), execexec(void); int execforkexec(void); diff --git a/globals.c b/globals.c new file mode 100644 index 0000000..b29bfe3 --- /dev/null +++ b/globals.c @@ -0,0 +1,30 @@ +#include "rc.h" +#include "io.h" + +int mypid; + +/* globals associated with lex.c and subr.c */ +int doprompt = 1; /* is it time for a prompt? */ +int lastdol; /* was the last token read '$' or '$#' or '"'? */ +int lastword; /* was the last token read a word or compound word terminator? */ +char tok[NTOK]; +int ndot = 0; // FIXME - never decreases? +int lastc; +int lastword; +int kidpid; + +io *rc_err; + +/* globals associated with exec.c */ +char *promptstr; +tree *cmdtree; +thread *runq; +code *codebuf; /* compiler output */ +int ntrap; /* number of outstanding traps */ +int trap[NSIG]; /* number of outstanding traps per type */ +int eflagok; /* kludge flag so that -e doesn't exit in startup */ + +char *argv0="rc"; +var *gvar[NVAR]; /* hash for globals */ + +int nerror = 0; /* number of errors encountered during compilation */ diff --git a/io.h b/io.h index 21cc6b8..68b9e89 100644 --- a/io.h +++ b/io.h @@ -10,7 +10,7 @@ struct io{ int fd; char *bufp, *ebuf, *strp, buf[NBUF]; }; -io *err; +extern io *err; io *openfd(int), *openstr(void), *opencore(char *, int); int emptybuf(io*); void pchr(io*, int); diff --git a/lex.c b/lex.c index d2bef32..f1e40e2 100644 --- a/lex.c +++ b/lex.c @@ -3,7 +3,11 @@ #include "io.h" #include "getflags.h" #include "fns.h" -int getnext(void); + +static int future = EOF; +static int inquote; +static int incomm; +static int getnext(void); int wordchr(int c) @@ -21,10 +25,6 @@ idchr(int c) */ return c>' ' && !strchr("!\"#$%&'()+,-./:;<=>?@[\\]^`{|}~", c); } -int future = EOF; -int doprompt = 1; -int inquote; -int incomm; /* * Look ahead in the input stream */ @@ -52,7 +52,7 @@ advance(void) * read a character from the input stream */ -int +static int getnext(void) { int c; @@ -163,8 +163,6 @@ addutf(char *p, int c) } return p; } -int lastdol; /* was the last token read '$' or '$#' or '"'? */ -int lastword; /* was the last token read a word or compound word terminator? */ int yylex(void) diff --git a/rc.h b/rc.h index 8a6a5bb..1d87051 100644 --- a/rc.h +++ b/rc.h @@ -53,7 +53,7 @@ tree *mung1(tree*, tree*), *mung2(tree*, tree*, tree*); tree *mung3(tree*, tree*, tree*, tree*), *epimung(tree*, tree*); tree *simplemung(tree*), *heredoc(tree*); void freetree(tree*); -tree *cmdtree; +extern tree *cmdtree; /* * The first word of any code vector is a reference count. * Always create a new reference to a code vector by calling codecopy(.). @@ -64,10 +64,10 @@ union code{ int i; char *s; }; -char *promptstr; -int doprompt; +extern char *promptstr; +extern int doprompt; #define NTOK 8192 -char tok[NTOK]; +extern char tok[NTOK]; #define APPEND 1 #define WRITE 2 #define READ 3 @@ -87,7 +87,7 @@ struct var{ }; var *vlook(char*), *gvlook(char*), *newvar(char*, var*); #define NVAR 521 -var *gvar[NVAR]; /* hash for globals */ +extern var *gvar[NVAR]; /* hash for globals */ #define new(type) ((type *)emalloc(sizeof(type))) void *emalloc(long); void *Malloc(ulong); @@ -98,7 +98,7 @@ struct here{ char *name; struct here *next; }; -int mypid; +extern int mypid; /* * Glob character escape in strings: * In a string, GLOB must be followed by *?[ or GLOB. @@ -117,10 +117,13 @@ int mypid; #define threebyte(c) ((c&0xf0)==0xe0) #define fourbyte(c) ((c&0xf8)==0xf0) -char **argp; -char **args; -int nerror; /* number of errors encountered during compilation */ -int doprompt; /* is it time for a prompt? */ +extern char *argv0; +//char **argp; +//char **args; +extern int nerror; /* number of errors encountered during compilation */ +extern int doprompt; /* is it time for a prompt? */ +extern int lastword, lastdol; + /* * Which fds are the reading/writing end of a pipe? * Unfortunately, this can vary from system to system. @@ -129,14 +132,14 @@ int doprompt; /* is it time for a prompt? */ */ #define PRD 0 #define PWR 1 -char *Rcmain, *Fdprefix; +extern char *Rcmain, *Fdprefix; #define register /* * How many dot commands have we executed? * Used to ensure that -v flag doesn't print rcmain. */ -int ndot; +extern int ndot; char *getstatus(void); -int lastc; -int lastword; -int kidpid; +extern int lastc; +extern int lastword; +extern int kidpid; diff --git a/subr.c b/subr.c index a2d8a18..1fe3b9b 100644 --- a/subr.c +++ b/subr.c @@ -22,7 +22,6 @@ efree(void *p) free(p); else pfmt(err, "free 0\n"); } -extern int lastword, lastdol; void yyerror(char *m)