From 7347cc64e47415f9212c1dfb9c0fe9b86e981104 Mon Sep 17 00:00:00 2001 From: George Koehler Date: Fri, 11 Nov 2016 17:05:50 -0500 Subject: [PATCH 1/7] Pass DEFAULT_PLATFORM to ackbuilder so util/ack sees it. --- Makefile | 1 + 1 file changed, 1 insertion(+) diff --git a/Makefile b/Makefile index 635650bc4..febd87aff 100644 --- a/Makefile +++ b/Makefile @@ -88,6 +88,7 @@ $(BUILDDIR)/build.$(BUILDSYSTEM): first/ackbuilder.lua Makefile $(BUILD_FILES) $ @$(LUA) first/ackbuilder.lua \ first/build.lua build.lua \ --$(BUILDSYSTEM) \ + DEFAULT_PLATFORM=$(DEFAULT_PLATFORM) \ OBJDIR=$(OBJDIR) \ BINDIR=$(BINDIR) \ LIBDIR=$(LIBDIR) \ From 7b3f870f6385f60b9cae18241b3ae058f890b986 Mon Sep 17 00:00:00 2001 From: George Koehler Date: Fri, 11 Nov 2016 17:06:25 -0500 Subject: [PATCH 2/7] Don't build mktables.c in the ack binary. It only worked by accident because main() in main.c was found before main() in mktables.c. Also add build dependencies on the local *.h files. --- util/ack/build.lua | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/util/ack/build.lua b/util/ack/build.lua index 4394484b4..cb9b66fd4 100644 --- a/util/ack/build.lua +++ b/util/ack/build.lua @@ -18,12 +18,28 @@ normalrule { cprogram { name = "ack", srcs = { - "./*.c", + "./data.c", + "./files.c", + "./grows.c", + "./list.c", + "./main.c", + "./rmach.c", + "./run.c", + "./scan.c", + "./svars.c", + "./trans.c", + "./util.c", "+tables", }, deps = { "h+emheaders", "h+local", + "./ack.h", + "./data.h", + "./dmach.h", + "./grows.h", + "./list.h", + "./trans.h", } } From 65278bc9a9ffe8a924c616765afea3bf4e3728d5 Mon Sep 17 00:00:00 2001 From: George Koehler Date: Sat, 12 Nov 2016 21:12:45 -0500 Subject: [PATCH 3/7] In util/ack, add prototypes and static to functions. Declare most functions before using them. I declare some functions in ack.h and some in trans.h (because trans.h declares type trf). I leave declarations of scanb() and scanvars() in .c files because they need type growstring. (I can't #include "grows.h" in another header file as long as grows.h doesn't guard against multiple inclusion.) Functions used within one file become static. I remove a few tiny functions. I move a few functions or declarations to more convenient places. Some functions now return void instead of a garbage int. I feel that keyword "register" is obsolete, so I removed it from where I was editing. This commit doesn't touch mktables.c --- util/ack/ack.h | 41 +++++++++++++------- util/ack/data.h | 2 +- util/ack/files.c | 39 ++++++++++--------- util/ack/grows.c | 13 +++---- util/ack/grows.h | 10 ++--- util/ack/list.c | 6 +-- util/ack/list.h | 4 ++ util/ack/main.c | 65 ++++++++++++++++++-------------- util/ack/rmach.c | 38 +++++++++---------- util/ack/run.c | 16 +++++--- util/ack/scan.c | 22 ++++++----- util/ack/svars.c | 13 +++---- util/ack/trans.c | 98 ++++++++++++++++++++---------------------------- util/ack/trans.h | 23 ++++++++++++ util/ack/util.c | 48 ++++++++---------------- 15 files changed, 230 insertions(+), 208 deletions(-) diff --git a/util/ack/ack.h b/util/ack/ack.h index 0877a8c0d..bbf3da58d 100644 --- a/util/ack/ack.h +++ b/util/ack/ack.h @@ -6,6 +6,8 @@ #define RCS_ACK "$Id$" #endif +#include /* size_t, free() */ + /****************************************************************************/ /* User settable options */ /****************************************************************************/ @@ -61,20 +63,33 @@ typedef struct { #define p_cont(elem) ((path *)l_content(elem)) -/* Return values of setpath() */ -enum f_path { F_OK, F_NOMATCH, F_NOPATH } ; - /* Own routines */ -enum f_path getpath(); -enum f_path scan_end(); -extern void noodstop(); -extern char *getvar(); -extern char *keeps(); -extern char *basename(); -extern char *skipblank(); -extern char *firstblank(); -extern char *getcore(); -extern char *changecore(); + +/* rmach.c */ +void setlist(char *); + +/* svars.c */ +void setsvar(char *, char *); +void setpvar(char *, char *(*)(void)); +char *getvar(const char *); + +/* util.c */ +char *ack_basename(const char *); +void clr_noscan(char *); +char *skipblank(char *); +char *firstblank(char *); +void fatal(const char *, ...); +void vprint(const char *, ...); +#ifdef DEBUG +void prns(const char *); +#endif +void fuerror(const char *, ...); +void werror(const char *, ...); +void quit(int); +char *keeps(const char *); +#define throws(str) free(str) +void *getcore(size_t); +void *changecore(void *, size_t); #define freecore(area) free(area) #define DEBUG 1 /* Allow debugging of Ack */ diff --git a/util/ack/data.h b/util/ack/data.h index d6ae07855..ea845c4f7 100644 --- a/util/ack/data.h +++ b/util/ack/data.h @@ -48,4 +48,4 @@ EXTERN path in; /* The current single input pathname */ EXTERN path out; /* The current output pathname */ EXTERN path orig; /* The original input path */ EXTERN char *p_basename; /* The current basename */ -EXTERN char *p_suffix; /* The current input suffix */ +EXTERN const char *p_suffix; /* The current input suffix */ diff --git a/util/ack/files.c b/util/ack/files.c index f2dbc8ed2..e149b90f7 100644 --- a/util/ack/files.c +++ b/util/ack/files.c @@ -16,7 +16,7 @@ static char rcs_id[] = "$Id$" ; #endif -char *add_u(part,ptr) char *ptr ; { +static char *add_u(int part, char *ptr) { if ( part>=26 ) { ptr=add_u(part/26-1,ptr) ; } @@ -24,7 +24,7 @@ char *add_u(part,ptr) char *ptr ; { return ptr+1 ; } -char *unique() { +static char *unique(void) { /* Get the next unique part of the internal filename */ static int u_next = 0 ; static char buf[10] ; @@ -36,7 +36,7 @@ char *unique() { return buf ; } -setfiles(phase) register trf *phase ; { +int setfiles(trf *phase) { /* Set the out structure according to the in structure, the transformation and some global data */ growstring pathname ; @@ -94,18 +94,7 @@ setfiles(phase) register trf *phase ; { return 1 ; } -disc_files(phase) trf *phase ; { - path temp ; - - if ( !phase->t_combine ) { - file_final(&in) ; - } else { - disc_inputs(phase) ; - } - temp=in ; in=out ; out=temp ; -} - -file_final(file) path *file ; { +static void file_final(path *file) { if ( file->p_path ) { if ( !file->p_keep && t_flag<=1 ) { if ( unlink(file->p_path)!=0 ) { @@ -119,7 +108,18 @@ file_final(file) path *file ; { file->p_keep=NO ; } -disc_inputs(phase) trf *phase ; { +void disc_files(trf *phase) { + path temp ; + + if ( !phase->t_combine ) { + file_final(&in) ; + } else { + disc_inputs(phase) ; + } + temp=in ; in=out ; out=temp ; +} + +void disc_inputs(trf *phase) { /* Remove all the input files of this phase */ /* Only for combiners */ register path *l_in ; @@ -132,7 +132,7 @@ disc_inputs(phase) trf *phase ; { l_clear(&phase->t_inputs) ; } -rmfile(file) path *file ; { +void rmfile(path *file) { /* Remove a file, do not complain when is does not exist */ if ( file->p_path ) { if ( t_flag<=1 ) unlink(file->p_path) ; @@ -143,8 +143,7 @@ rmfile(file) path *file ; { } } -void -rmtemps() { +void rmtemps(void) { /* Called in case of disaster, always remove the current output file! */ register list_elem *elem ; @@ -159,7 +158,7 @@ rmtemps() { } } -add_input(file,phase) path *file ; trf *phase ; { +void add_input(path *file, trf *phase) { register path *store ; #ifdef DEBUG if ( debug ) { diff --git a/util/ack/grows.c b/util/ack/grows.c index 81c3c002d..a74e27731 100644 --- a/util/ack/grows.c +++ b/util/ack/grows.c @@ -19,7 +19,7 @@ static char rcs_id[] = "$Id$" ; static char rcs_grows[] = RCS_GROWS ; #endif -gr_add(id,c) register growstring *id ; char c ; { +int gr_add(growstring *id, int c) { if ( id->gr_size==id->gr_max) { if ( id->gr_size==0 ) { /* The first time */ id->gr_max= 2*GR_MORE ; @@ -32,8 +32,8 @@ gr_add(id,c) register growstring *id ; char c ; { *(id->gr_string+id->gr_size++)= c ; } -gr_cat(id,string) growstring *id ; char *string ; { - register char *ptr ; +int gr_cat(growstring *id, const char *string) { + const char *ptr ; #ifdef DEBUG if ( id->gr_size && *(id->gr_string+id->gr_size-1) ) { @@ -50,8 +50,7 @@ gr_cat(id,string) growstring *id ; char *string ; { } } -void -gr_throw(id) register growstring *id ; { +void gr_throw(growstring *id) { /* Throw the string away */ if ( id->gr_max==0 ) return ; freecore(id->gr_string) ; @@ -60,11 +59,11 @@ gr_throw(id) register growstring *id ; { id->gr_size=0 ; } -gr_init(id) growstring *id ; { +void gr_init(growstring *id) { id->gr_size=0 ; id->gr_max=0 ; } -char *gr_final(id) growstring *id ; { +char *gr_final(growstring *id) { /* Throw away the bookkeeping, adjust the string to its final length and return a pointer to a string to be get rid of with throws diff --git a/util/ack/grows.h b/util/ack/grows.h index 9b9511049..04f95aa59 100644 --- a/util/ack/grows.h +++ b/util/ack/grows.h @@ -20,8 +20,8 @@ typedef struct { /* Routines used */ -extern void gr_throw() ; /* To free the core */ -extern int gr_add() ; /* To add one character */ -extern int gr_cat() ; /* concatenate the contents and the string */ -extern int gr_init() ; /* Initialize the bookkeeping */ -extern char *gr_final() ; /* Transform to a stable storage string */ +void gr_throw(growstring *) ; /* To free the core */ +int gr_add(growstring *, int) ; /* To add one character */ +int gr_cat(growstring *, const char *) ; /* To append a string */ +void gr_init(growstring *) ; /* Initialize the bookkeeping */ +char *gr_final(growstring *) ; /* Move to a stable storage string */ diff --git a/util/ack/list.c b/util/ack/list.c index 278f55580..5370e96cb 100644 --- a/util/ack/list.c +++ b/util/ack/list.c @@ -33,7 +33,7 @@ Routines: */ -l_add(header,string) list_head *header ; char *string ; { +void l_add(list_head *header, char *string) { register list_elem *new; /* NOSTRICT */ @@ -49,7 +49,7 @@ l_add(header,string) list_head *header ; char *string ; { header->ca_last= new ; } -l_clear(header) list_head *header ; { +void l_clear(list_head *header) { register list_elem *old, *next; for ( old=header->ca_first ; old ; old= next ) { next= old->ca_next ; @@ -59,7 +59,7 @@ l_clear(header) list_head *header ; { header->ca_last = (list_elem *) 0 ; } -l_throw(header) list_head *header ; { +void l_throw(list_head *header) { register list_elem *old, *next; for ( old=header->ca_first ; old ; old= next ) { throws(l_content(*old)) ; diff --git a/util/ack/list.h b/util/ack/list.h index 3e6d55170..f96d8ce15 100644 --- a/util/ack/list.h +++ b/util/ack/list.h @@ -29,3 +29,7 @@ typedef struct ca_elem list_elem ; /* The decl. for elements */ /* To be used for scanning lists, ptr is the running variable */ #define scanlist(elem,ptr) \ for ( ptr= elem ; ptr; ptr= l_next(*ptr) ) + +void l_add(list_head *, char *); +void l_clear(list_head *); +void l_throw(list_head *); diff --git a/util/ack/main.c b/util/ack/main.c index 99e27dceb..539f42180 100644 --- a/util/ack/main.c +++ b/util/ack/main.c @@ -22,11 +22,20 @@ static char rcs_ack[] = RCS_ACK ; static int sigs[] = { SIGINT, SIGHUP, SIGTERM, 0 } ; static int arg_count; -extern char *getenv(); +static char *srcvar(void); +static char *getsuffix(void); +static void varinit(void); +static void vieuwargs(int, char **); +static void firstarg(char *); +static int process(char *); +static int startrf(trf *); +static void block(trf *); +static int mayprep(void); +static void scanneeds(void); +static void setneeds(const char *, int); +static void noodstop(int); -void vieuwargs(); - -main(argc,argv) char **argv ; { +int main(int argc, char **argv) { register list_elem *elem ; register char *frontend ; register int *n_sig ; @@ -102,7 +111,7 @@ main(argc,argv) char **argv ; { orig.p_path=phase->t_origname ; if ( p_basename ) throws(p_basename) ; if ( orig.p_path ) { - p_basename= keeps(basename(orig.p_path)) ; + p_basename= keeps(ack_basename(orig.p_path)) ; } else { p_basename=0 ; } @@ -115,15 +124,15 @@ main(argc,argv) char **argv ; { exit(0) ; } -char *srcvar() { +static char *srcvar(void) { return orig.p_path ; } -char *getsuffix() { +static char *getsuffix(void) { return strrchr(orig.p_path, SUFCHAR) ; } -varinit() { +static void varinit(void) { /* initialize the string variables */ register char *envstr ; extern char *em_dir; @@ -138,8 +147,7 @@ varinit() { /************************* flag processing ***********************/ -void -vieuwargs(argc,argv) char **argv ; { +void vieuwargs(int argc, char **argv) { register char *argp; register int nextarg ; register int eaten ; @@ -206,14 +214,16 @@ vieuwargs(argc,argv) char **argv ; { case 'r': if ( argp[2]!=SUFCHAR ) { error("-r must be followed by %c",SUFCHAR) ; } - keeptail(&argp[2]); eaten=1 ; + l_add(&tail_list, &argp[2]) ; + eaten=1 ; break ; case '.': if ( rts ) { if ( strcmp(rts,&argp[1])!=0 ) fuerror("Two run-time systems?") ; } else { rts= &argp[1] ; - keephead(rts) ; keeptail(rts) ; + l_add(&head_list, rts) ; + l_add(&tail_list, rts) ; } eaten=1 ; break ; @@ -238,7 +248,7 @@ vieuwargs(argc,argv) char **argv ; { register char *tokeep ; tokeep=keeps(argp) ; if ( argp[1]=='R' ) { - do_Rflag(tokeep); + l_add(&R_list, tokeep) ; } else { *tokeep |= NO_SCAN ; } @@ -251,7 +261,7 @@ vieuwargs(argc,argv) char **argv ; { return ; } -firstarg(argp) register char *argp ; { +static void firstarg(char *argp) { register char *name ; name=strrchr(argp,'/') ; @@ -265,7 +275,7 @@ firstarg(argp) register char *argp ; { /************************* argument processing ***********************/ -process(arg) char *arg ; { +static int process(char *arg) { /* Process files & library arguments */ trf *phase ; register trf *tmp ; @@ -282,7 +292,7 @@ process(arg) char *arg ; { return 1 ; } if ( p_basename ) throws(p_basename) ; - p_basename= keeps(basename(arg)) ; + p_basename= keeps(ack_basename(arg)) ; /* Try to find a path through the transformations */ switch( getpath(&phase) ) { case F_NOPATH : @@ -325,7 +335,7 @@ process(arg) char *arg ; { return startrf(phase) ; } -int startrf(first) trf *first ; { +static int startrf(trf *first) { /* Start the transformations at the indicated phase */ register trf *phase ; @@ -397,7 +407,7 @@ if ( debug ) vprint("Transformation sequence complete for %s\n", return 1 ; } -block(first) trf *first ; { +static void block(trf *first) { /* One of the input files of this phase could not be produced, block all combiners taking their input from this one. */ @@ -406,7 +416,8 @@ block(first) trf *first ; { if ( phase->t_combine ) phase->t_blocked=YES ; } } -mayprep() { + +static int mayprep(void) { int file ; char fc ; file=open(in.p_path,0); @@ -416,15 +427,7 @@ mayprep() { return fc=='#' ; } -keephead(suffix) char *suffix ; { - l_add(&head_list, suffix) ; -} - -keeptail(suffix) char *suffix ; { - l_add(&tail_list, suffix) ; -} - -scanneeds() { +static void scanneeds(void) { register list_elem *elem ; scanlist(l_first(head_list), elem) { setneeds(l_content(*elem),0) ; } l_clear(&head_list) ; @@ -432,7 +435,7 @@ scanneeds() { l_clear(&tail_list) ; } -setneeds(suffix,tail) char *suffix ; { +static void setneeds(const char *suffix, int tail) { trf *phase ; p_suffix= suffix ; @@ -456,3 +459,7 @@ setneeds(suffix,tail) char *suffix ; { break ; } } + +static void noodstop(int sig) { + quit(-3) ; +} diff --git a/util/ack/rmach.c b/util/ack/rmach.c index 1e33e8713..4b3061be4 100644 --- a/util/ack/rmach.c +++ b/util/ack/rmach.c @@ -47,19 +47,21 @@ static char rcs_dmach[] = RCS_DMACH ; #define CALL "callname" #define END "end" -extern growstring scanb(); -extern growstring scanvars(); +/* trans.c */ +growstring scanb(const char *) ; +growstring scanvars(const char *) ; + +static void intrf(void) ; +static void open_in(char *) ; +static void close_in(void) ; +static int getinchar(void) ; +static int getln(void) ; -int getln() ; -int getinchar() ; static char *ty_name ; static char *bol ; - -void open_in(); - static char *inname ; -setlist(name) char *name ; { +void setlist(char *name) { /* Name is sought in the internal tables, if not present, the a file of that name is sought in first the current and then the EM Lib directory @@ -92,9 +94,7 @@ setlist(name) char *name ; { #endif } -static int inoptlist(nm) - char *nm ; -{ +static int inoptlist(char *nm) { register char *p=Optlist ; while ( p && *p ) { @@ -107,7 +107,7 @@ static int inoptlist(nm) return 0; } -intrf() { +static void intrf(void) { register trf *new ; growstring bline ; int twice ; @@ -234,7 +234,8 @@ intrf() { rts, new->t_rts) ; } rts= new->t_rts ; - keephead(rts) ; keeptail(rts) ; + l_add(&head_list, rts) ; + l_add(&tail_list, rts) ; } #ifdef DEBUG if ( debug>=3 ) { @@ -264,8 +265,7 @@ static FILE *infile ; static char *inptr ; char *em_dir = EM_DIR; -void -open_in(name) register char *name ; { +static void open_in(char *name) { register dmach *cmac ; gr_init(&rline) ; @@ -298,12 +298,12 @@ open_in(name) register char *name ; { } } -close_in() { +static void close_in(void) { if ( !incore ) fclose(infile) ; gr_throw(&rline) ; } -char *readline() { +static char *readline(void) { /* Get a line from the input, return 0 if at end, The line is stored in a volatile buffer, @@ -355,7 +355,7 @@ char *readline() { } } -int getinchar() { +static int getinchar(void) { register int token ; if ( incore ) { @@ -369,7 +369,7 @@ int getinchar() { return token ; } -int getln() { +static int getln(void) { register char *c_ptr ; do { diff --git a/util/ack/run.c b/util/ack/run.c index 8d0481a58..87d8745a0 100644 --- a/util/ack/run.c +++ b/util/ack/run.c @@ -5,6 +5,7 @@ */ #include +#include #include #include "ack.h" #include "list.h" @@ -19,13 +20,17 @@ static char rcs_id[] = "$Id$" ; #define ARG_MORE 40 /* The size of args chunks to allocate */ -extern growstring scanvars(); +/* trans.c */ +growstring scanvars(const char *); + +static int run_exec(trf *, const char *); +static void x_arg(char *); static char **arglist ; /* The first argument */ static unsigned argcount ; /* The current number of arguments */ static unsigned argmax; /* The maximum number of arguments so far */ -int runphase(phase) register trf *phase ; { +int runphase(trf *phase) { register list_elem *elem ; char *prog ; int result ; growstring bline ; @@ -70,10 +75,11 @@ int runphase(phase) register trf *phase ; { return result ; } -int run_exec(phase,prog) trf *phase ; char *prog ; { +static int run_exec(trf *phase, const char *prog) { int status, child, waitchild ; - do_flush(); + fflush(stdout) ; + fflush(stderr) ; while ( (child=fork())== -1 ) ; if ( child ) { /* The parent */ @@ -136,7 +142,7 @@ int run_exec(phase,prog) trf *phase ; char *prog ; { /*NOTREACHED*/ } -x_arg(string) char *string ; { +static void x_arg(char *string) { /* Add one execute argument to the argument vector */ if ( argcount==argmax ) { if ( argmax==0 ) { diff --git a/util/ack/scan.c b/util/ack/scan.c index d020e6a5b..3aaab8492 100644 --- a/util/ack/scan.c +++ b/util/ack/scan.c @@ -15,9 +15,14 @@ static char rcs_id[] = "$Id$" ; #endif -void try(); +static void start_scan(void); +static void try(list_elem *, const char *); +static void scan_found(void); +static int satisfy(trf *, const char *); +static enum f_path scan_end(trf **); +static void find_cpp(void); -enum f_path getpath(first) register trf **first ; { +enum f_path getpath(trf **first) { /* Try to find a transformation path */ start_scan(); @@ -49,7 +54,7 @@ static int suf_found; /* Was the suffix at least recognized ? */ /******************** The hard work ********************/ -start_scan() { +static void start_scan(void) { register list_elem *scan ; scanlist(l_first(tr_list),scan) { @@ -63,8 +68,7 @@ start_scan() { last_ocount= 0 ; } -void -try(f_scan,suffix) list_elem *f_scan; char *suffix; { +static void try(list_elem *f_scan, const char *suffix) { register list_elem *scan ; register trf *trafo ; /* Try to find a transformation path starting at f_scan for a @@ -134,7 +138,7 @@ try(f_scan,suffix) list_elem *f_scan; char *suffix; { } } -scan_found() { +static void scan_found(void) { register list_elem *scan; int ncount, ocount, pcount ; @@ -182,7 +186,7 @@ scan_found() { } } -int satisfy(trafo,suffix) register trf *trafo; char *suffix ; { +static int satisfy(trf *trafo, const char *suffix) { register char *f_char, *l_char ; /* Check whether this transformation is present for the current machine and the parameter suffix is among @@ -207,7 +211,7 @@ int satisfy(trafo,suffix) register trf *trafo; char *suffix ; { return 0 ; } -enum f_path scan_end(first) trf **first ; { /* Finalization */ +static enum f_path scan_end(trf **first) { /* Finalization */ /* Return value indicating whether a transformation was found */ /* Set the flags for the transformation up to, but not including, the combiner @@ -248,7 +252,7 @@ enum f_path scan_end(first) trf **first ; { /* Finalization */ return F_OK ; } -find_cpp() { +static void find_cpp(void) { register list_elem *elem ; scanlist( l_first(tr_list), elem ) { if ( t_cont(*elem)->t_isprep ) { diff --git a/util/ack/svars.c b/util/ack/svars.c index 2d7e4b42c..38e8f95b9 100644 --- a/util/ack/svars.c +++ b/util/ack/svars.c @@ -44,9 +44,6 @@ static char rcs_id[] = "$Id$" ; */ -extern char *getcore(); -extern fatal(); - struct vars { char *v_name; enum { routine, string } v_type; @@ -60,7 +57,7 @@ struct vars { static struct vars *v_first ; -static struct vars *newvar(name) char *name; { +static struct vars *newvar(char *name) { register struct vars *new ; for ( new=v_first ; new ; new= new->v_next ) { @@ -72,14 +69,14 @@ static struct vars *newvar(name) char *name; { return new ; } } - new= (struct vars *)getcore( (unsigned)sizeof (struct vars)); + new= (struct vars *)getcore(sizeof (struct vars)); new->v_name= name ; new->v_next= v_first ; v_first= new ; return new ; } -setsvar(name,str) char *name, *str ; { +void setsvar(char *name, char *str) { register struct vars *new ; new= newvar(name); @@ -90,7 +87,7 @@ setsvar(name,str) char *name, *str ; { new->v_value.v_string= str; } -setpvar(name,rout) char *name, *(*rout)() ; { +void setpvar(char *name, char *(*rout)(void)) { register struct vars *new ; new= newvar(name); @@ -101,7 +98,7 @@ setpvar(name,rout) char *name, *(*rout)() ; { new->v_value.v_routine= rout; } -char *getvar(name) char *name ; { +char *getvar(const char *name) { register struct vars *scan ; for ( scan=v_first ; scan ; scan= scan->v_next ) { diff --git a/util/ack/trans.c b/util/ack/trans.c index 3cb2d7ea8..92e54ae9e 100644 --- a/util/ack/trans.c +++ b/util/ack/trans.c @@ -26,13 +26,15 @@ static int touch_head= NO ; static growstring tail ; static int touch_tail= NO ; -char *headvar(),*tailvar() ; +static char *headvar(void); +static char *tailvar(void); +static void set_Rflag(char *); +static void condit(growstring *, list_head *, list_head *, char *); +static int mapflag(list_head *, const char *); +static int mapexpand(char *, const char *); +static void getcallargs(trf *); -void condit(); -void doassign(); -void set_Rflag(); - -int transform(phase) register trf *phase ; { +int transform(trf *phase) { int ok ; if ( !setfiles(phase) ) { @@ -45,12 +47,13 @@ int transform(phase) register trf *phase ; { /* Free the space occupied by the arguments, except for the linker, since we are bound to exit soon and do not foresee further need of memory space */ - if ( !phase->t_linker ) discardargs(phase) ; + if ( !phase->t_linker ) + l_throw(&phase->t_args) ; disc_files(phase) ; return ok ; } -getmapflags(phase) register trf *phase ; { +void getmapflags(trf *phase) { register path *l_in ; register list_elem *elem ; int scanned ; @@ -110,16 +113,12 @@ getmapflags(phase) register trf *phase ; { } -do_Rflag(argp) char *argp ; { - l_add(&R_list,argp) ; -} - -char *headvar() { +static char *headvar(void) { if ( !touch_head) return "" ; return gr_start(head) ; } -add_head(str) char *str; { +void add_head(const char *str) { if ( !touch_head) { gr_init(&head) ; touch_head=YES ; @@ -127,12 +126,12 @@ add_head(str) char *str; { gr_cat(&head,str) ; } -char *tailvar() { +static char *tailvar(void) { if ( !touch_tail ) return "" ; return gr_start(tail) ; } -add_tail(str) char *str ; { +void add_tail(const char *str) { if ( !touch_tail ) { gr_init(&tail) ; touch_tail=YES ; @@ -141,7 +140,7 @@ add_tail(str) char *str ; { } -transini() { +void transini(void) { register list_elem *elem ; register trf *phase ; @@ -157,8 +156,7 @@ transini() { setpvar(keeps(TAIL),tailvar) ; } -void -set_Rflag(argp) register char *argp ; { +static void set_Rflag(char *argp) { register char *eos ; register list_elem *prog ; register int length ; @@ -206,12 +204,12 @@ set_Rflag(argp) register char *argp ; { /* */ /**************************************************************************/ -growstring scanb(line) char *line ; { +growstring scanb(const char *line) { /* Scan a line for backslashes, setting the NO_SCAN bit in characters preceded by a backslash. */ - register char *in_c ; - register int token ; + const char *in_c ; + int token ; growstring result ; enum { TEXT, ESCAPED } state = TEXT ; @@ -237,7 +235,7 @@ growstring scanb(line) char *line ; { return result ; } -growstring scanvars(line) char *line ; { +growstring scanvars(const char *line) { /* Scan a line variable replacements started by S_VAR. Two sequences exist: S_VAR name E_VAR, S_VAR name A_VAR text E_VAR. neither name nor text may contain further replacements. @@ -248,11 +246,10 @@ growstring scanvars(line) char *line ; { This to allow later recognition in mapflags, where B_SLASH would be preventing any recognition. */ - register char *in_c ; - register int token ; - growstring result ; - growstring name ; - register char *tr ; + const char *in_c ; + int token ; + growstring result, name ; + char *tr ; enum { TEXT, FIRST, NAME, SKIP, COPY } state = TEXT ; gr_init(&result) ; gr_init(&name) ; @@ -331,7 +328,7 @@ growstring scanvars(line) char *line ; { return result ; } -growstring scanexpr(line) char *line ; { +static growstring scanexpr(const char *line) { /* Scan a line for conditional or flag expressions, dependent on the type. The format is S_EXPR suflist M_EXPR suflist T_EXPR tail C_EXPR @@ -339,11 +336,9 @@ growstring scanexpr(line) char *line ; { growstring for futher treatment. Nesting is not allowed. */ - register char *in_c ; - char *heads ; - register int token ; - growstring sufs, tailval ; - growstring result ; + const char *in_c, *heads ; + int token ; + growstring sufs, tailval, result ; static list_head fsuff, lsuff ; enum { TEXT, FDOT, FSUF, LDOT, LSUF, FTAIL } state = TEXT ; @@ -418,10 +413,8 @@ growstring scanexpr(line) char *line ; { return result ; } -void -condit(line,fsuff,lsuff,tailval) growstring *line ; - list_head *fsuff, *lsuff; - char *tailval ; +static void condit(growstring *line, list_head *fsuff, list_head *lsuff, + char *tailval) { register list_elem *first ; register list_elem *last ; @@ -446,7 +439,7 @@ condit(line,fsuff,lsuff,tailval) growstring *line ; #endif } -int mapflag(maplist,cflag) list_head *maplist ; char *cflag ; { +static int mapflag(list_head *maplist, const char *cflag) { /* Expand a flag expression */ /* The flag "cflag" is checked for each of the mapflags. A mapflag entry has the form @@ -468,12 +461,9 @@ int mapflag(maplist,cflag) list_head *maplist ; char *cflag ; { return 0 ; } -int mapexpand(mapentry,cflag) - char *mapentry, *cflag ; -{ - register char *star ; - register char *ptr ; - register char *space ; +static int mapexpand(char *mapentry, const char *cflag) { + const char *star ; + char *ptr, *space ; int length ; star=strchr(mapentry,STAR) ; @@ -510,10 +500,9 @@ int mapexpand(mapentry,cflag) return 1 ; } -void -doassign(line,star,length) char *line, *star ; { +void doassign(const char *line, const char *star, int length) { growstring varval, name, temp ; - register char *ptr ; + const char *ptr ; gr_init(&varval) ; gr_init(&name) ; @@ -544,7 +533,7 @@ doassign(line,star,length) char *line, *star ; { #define ISBLANK(c) ( (c)==SPACE || (c)==TAB ) -unravel(line,action) char *line ; int (*action)() ; { +static void unravel(char *line, void (*action)(char *)) { /* Unravel the line, get arguments a la shell */ /* each argument is handled to action */ /* The input string is left intact */ @@ -581,7 +570,7 @@ unravel(line,action) char *line ; int (*action)() ; { } } -char *c_rep(string,place,rep) char *string, *place, *rep ; { +static char *c_rep(char *string, char *place, char *rep) { /* Produce a string in stable storage produced from 'string' with the character at place replaced by rep */ @@ -605,8 +594,7 @@ char *c_rep(string,place,rep) char *string, *place, *rep ; { static list_head *curargs ; static list_head *comb_args ; -void -addargs(string) char *string ; { +static void addargs(char *string) { register char *temp, *repc ; register list_elem *elem ; @@ -653,7 +641,7 @@ addargs(string) char *string ; { l_add(curargs,temp) ; } -getcallargs(phase) register trf *phase ; { +static void getcallargs(trf *phase) { growstring arg1, arg2 ; arg1= scanvars(phase->t_argd) ; @@ -670,7 +658,3 @@ getcallargs(phase) register trf *phase ; { unravel( gr_start(arg2), addargs ) ; gr_throw(&arg2) ; } - -discardargs(phase) register trf *phase ; { - l_throw(&phase->t_args) ; -} diff --git a/util/ack/trans.h b/util/ack/trans.h index 70be80832..54086a563 100644 --- a/util/ack/trans.h +++ b/util/ack/trans.h @@ -44,3 +44,26 @@ struct transform { } ; #define t_cont(elem) ((trf *)l_content(elem)) + +/* files.c */ +int setfiles(trf *); +void disc_files(trf *); +void disc_inputs(trf *); +void rmfile(path *); +void rmtemps(void); +void add_input(path *, trf *); + +/* run.c */ +int runphase(trf *); + +/* scan.c */ +enum f_path { F_OK, F_NOMATCH, F_NOPATH } ; +enum f_path getpath(trf **); + +/* trans.c */ +int transform(trf *); +void getmapflags(trf *); +void add_head(const char *); +void add_tail(const char *); +void transini(void); +void doassign(const char *, const char *, int); diff --git a/util/ack/util.c b/util/ack/util.c index 419f087e3..caad71cdd 100644 --- a/util/ack/util.c +++ b/util/ack/util.c @@ -32,15 +32,11 @@ extern int n_error; # define STDOUT stderr #endif -void fuerror(const char* fmt, ...); -void werror(const char* fmt, ...); - -char *basename(string) char *string ; { +char *ack_basename(const char *string) { static char retval[256] ; - char *last_dot, *last_start ; - register char *store; - register char *fetch ; - register int ctoken ; + const char *last_dot, *last_start, *fetch ; + char *store ; + int ctoken ; last_dot= (char *)0 ; last_start= string ; @@ -65,21 +61,21 @@ out: return retval ; } -clr_noscan(str) char *str ; { +void clr_noscan(char *str) { register char *ptr ; for ( ptr=str ; *ptr ; ptr++ ) { *ptr&= ~NO_SCAN ; } } -char *skipblank(str) char *str ; { +char *skipblank(char *str) { register char *ptr ; for ( ptr=str ; *ptr==SPACE || *ptr==TAB ; ptr++ ) ; return ptr ; } -char *firstblank(str) char *str ; { +char *firstblank(char *str) { register char *ptr ; for ( ptr=str ; *ptr && *ptr!=SPACE && *ptr!=TAB ; ptr++ ) ; @@ -110,7 +106,7 @@ void vprint(const char* fmt, ...) } #ifdef DEBUG -prns(s) register char *s ; { +void prns(const char *s) { for ( ; *s ; s++ ) { putc((*s&0377)&~NO_SCAN,STDOUT) ; } @@ -153,48 +149,36 @@ void error(const char *fmt, ...) { va_end(ap); } -do_flush() { - fflush(stdout) ; - fflush(stderr) ; -} - -void -noodstop() { - quit(-3) ; -} - -quit(code) { +void quit(int code) { rmtemps(); exit(code); } + /****** char *keeps(string) Keep the string in stable storage. throws(string) Remove the string stored by keep from stable storage. + throws() is now a macro in ack.h. ***********/ -char *keeps(str) char *str ; { +char *keeps(const char *str) { register char *result ; result= getcore( (unsigned)(strlen(str)+1) ) ; if ( !result ) fatal("Out of core") ; return strcpy(result,str) ; } -throws(str) char *str ; { - freecore(str) ; -} - -char *getcore(size) unsigned size ; { - register char *retptr ; +void *getcore(size_t size) { + void *retptr ; retptr= calloc(1,size) ; if ( !retptr ) fatal("Out of memory") ; return retptr ; } -char *changecore(ptr,size) char *ptr ; unsigned size ; { - register char *retptr ; +void *changecore(void *ptr, size_t size) { + void *retptr ; retptr= realloc(ptr,size) ; if ( !retptr ) fatal("Out of memory") ; From e617f425032bccafe36657dcf615a6abaf026fd3 Mon Sep 17 00:00:00 2001 From: George Koehler Date: Sun, 13 Nov 2016 11:55:09 -0500 Subject: [PATCH 4/7] Don't print a string after possibly freeing it. new= newvar(name) takes ownership of the string and might free its memory. Don't print name. Do print new->v_name. Also #include for strcmp(). --- util/ack/svars.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/util/ack/svars.c b/util/ack/svars.c index 38e8f95b9..e540e5d02 100644 --- a/util/ack/svars.c +++ b/util/ack/svars.c @@ -4,6 +4,7 @@ * */ +#include #include "ack.h" #ifndef NORCSID @@ -81,7 +82,7 @@ void setsvar(char *name, char *str) { new= newvar(name); #ifdef DEBUG - if ( debug>=2 ) vprint("%s=%s\n", name, str) ; + if ( debug>=2 ) vprint("%s=%s\n", new->v_name, str) ; #endif new->v_type= string; new->v_value.v_string= str; @@ -92,7 +93,7 @@ void setpvar(char *name, char *(*rout)(void)) { new= newvar(name); #ifdef DEBUG - if ( debug>=2 ) vprint("%s= (*%o)()\n",name,rout) ; + if ( debug>=2 ) vprint("%s= (*%o)()\n", new->v_name, rout) ; #endif new->v_type= routine; new->v_value.v_routine= rout; From fafc8a0b8a3041beb410ed04219c5e20c5f6572f Mon Sep 17 00:00:00 2001 From: George Koehler Date: Sun, 13 Nov 2016 12:45:01 -0500 Subject: [PATCH 5/7] Don't retry fork() in a loop. If fork() fails, then report a fatal error. Don't spin the cpu retrying fork() until it succeeds. It can fail when we reach a limit on the number of processes. Spinning on the cpu would slow down other processes when we want them to exit. This would get bad if we had a parallel build with multiple ack processes spinning. --- util/ack/run.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/util/ack/run.c b/util/ack/run.c index 87d8745a0..6b5b69fcd 100644 --- a/util/ack/run.c +++ b/util/ack/run.c @@ -80,7 +80,10 @@ static int run_exec(trf *phase, const char *prog) { fflush(stdout) ; fflush(stderr) ; - while ( (child=fork())== -1 ) ; + child= fork() ; + if ( child== - 1) { + fatal("Cannot fork %s", prog) ; + } if ( child ) { /* The parent */ do { From 07b04c64c8d8c4db0365763aa9be1696fc47cdd5 Mon Sep 17 00:00:00 2001 From: George Koehler Date: Mon, 14 Nov 2016 23:33:44 -0500 Subject: [PATCH 6/7] Let ack(1) compile files with non-ASCII names. This commit changes how ack(1) parses backslashes in its descr files. Before this commit, ack set the high bit of each character escaped by a backslash, and later cleared all high bits in command arguments, but this lost the high bits in non-ASCII filenames. After this commit, ack keeps backslashes in strings while processing them. Functions scanvars(), scanexpr(), doassign(), unravel(), addargs() now understand backslashes. I remove from ack_basename() the warning about non-ASCII characters. This commit makes some incompatible changes for backslashes in descr files. None of our descr files uses backslashes, except for those backslashes that continue lines, and there are no changes for those backslashes. The problem with non-ASCII filenames had its cause in a feature that we weren't using. With this commit, ack now understands backslashes after the = sign in both "var NAME=value" and "mapflag -flag NAME=value". Before, ack never scanned backslashes in "var" lines, so "var A=\{B}" failed to prevent expansion of B. Now it does. Before, ack did scan for backslashes in the "-flag NAME=" part of "mapflag" lines. Now it doesn't, so it is no longer possible to map a flag that contains a literal space, tab, or star "*". I removed the expansion of "{{" to "{". One can use "\{" for a literal "{", and "\{" now works in "var" lines. Before and now, ack never expanded "{" in flags for "mapflag", so the correct way to map a literal flag "-{" remains "mapflag -{ ...", not "mapflag -{{ ...". (The other way "mapflag -\{ ..." stops working with this commit.) Backslashes in strange places, like "{NA\ME}", probably have different behavior now. Backslashes in "program" lines now work. Before, ack scanned for backslashes there but forgot to clear the high bits later. Escaping < or > as \< or \> now works, and prevents substitution of the input or output file paths. Before, ack only expanded the first < or > in each argument. Now, it expands every unescaped < or > in an argument, but this is an accident of how I rewrote the code. I don't suggest to put more than one each of < or > in a command. The code no longer optimizes away its recursive calls when the argument is "<". The code continues to set or clear the high bit NO_SCAN on the first characters of flags. This doesn't seem to be a problem, because flags usually begin with an ASCII hyphen '-'. --- util/ack/ack.h | 4 - util/ack/rmach.c | 20 +-- util/ack/run.c | 14 +-- util/ack/trans.c | 321 +++++++++++++++++++++++++++-------------------- util/ack/util.c | 19 --- 5 files changed, 197 insertions(+), 181 deletions(-) diff --git a/util/ack/ack.h b/util/ack/ack.h index bbf3da58d..01eec8212 100644 --- a/util/ack/ack.h +++ b/util/ack/ack.h @@ -75,14 +75,10 @@ char *getvar(const char *); /* util.c */ char *ack_basename(const char *); -void clr_noscan(char *); char *skipblank(char *); char *firstblank(char *); void fatal(const char *, ...); void vprint(const char *, ...); -#ifdef DEBUG -void prns(const char *); -#endif void fuerror(const char *, ...); void werror(const char *, ...); void quit(int); diff --git a/util/ack/rmach.c b/util/ack/rmach.c index 4b3061be4..13f904a38 100644 --- a/util/ack/rmach.c +++ b/util/ack/rmach.c @@ -47,10 +47,6 @@ static char rcs_dmach[] = RCS_DMACH ; #define CALL "callname" #define END "end" -/* trans.c */ -growstring scanb(const char *) ; -growstring scanvars(const char *) ; - static void intrf(void) ; static void open_in(char *) ; static void close_in(void) ; @@ -108,8 +104,8 @@ static int inoptlist(char *nm) { } static void intrf(void) { + /* Read in trf (transformation) */ register trf *new ; - growstring bline ; int twice ; int name_seen=0 ; @@ -130,20 +126,14 @@ static void intrf(void) { } else if ( strcmp(ty_name,PROG)==0 ) { if ( new->t_prog ) twice=YES ; - bline= scanb(bol); /* Scan for \ */ - new->t_prog= gr_final(&bline); + new->t_prog= keeps(bol); } else if ( strcmp(ty_name,MAPF)==0 ) { - /* First read the mapflags line - and scan for backslashes */ - bline= scanb(bol) ; - l_add(&new->t_mapf,gr_final(&bline)) ; + l_add(&new->t_mapf,keeps(bol)) ; } else if ( strcmp(ty_name,ARGS)==0 ) { if ( new->t_argd ) twice=YES ; - bline= scanb(bol) ; - new->t_argd= keeps(gr_start(bline)) ; - gr_throw(&bline) ; + new->t_argd= keeps(bol) ; } else if ( strcmp(ty_name,STD_IN)==0 ) { if ( new->t_stdin ) twice=YES ; @@ -242,7 +232,7 @@ static void intrf(void) { register list_elem *elem ; vprint("%s: from %s to %s '%s'\n", new->t_name,new->t_in ? new->t_in : "(null)",new->t_out,new->t_prog) ; - vprint("\targs: ") ; prns(new->t_argd) ; + vprint("\targs: %s",new->t_argd) ; scanlist( l_first(new->t_mapf), elem ) { vprint("\t%s\n",l_content(*elem)) ; } diff --git a/util/ack/run.c b/util/ack/run.c index 6b5b69fcd..ed88d66c7 100644 --- a/util/ack/run.c +++ b/util/ack/run.c @@ -20,9 +20,6 @@ static char rcs_id[] = "$Id$" ; #define ARG_MORE 40 /* The size of args chunks to allocate */ -/* trans.c */ -growstring scanvars(const char *); - static int run_exec(trf *, const char *); static void x_arg(char *); @@ -32,11 +29,9 @@ static unsigned argmax; /* The maximum number of arguments so far */ int runphase(trf *phase) { register list_elem *elem ; - char *prog ; int result ; - growstring bline ; + char *prog ; int result ; - bline=scanvars(phase->t_prog) ; - prog=gr_final(&bline) ; + prog=phase->t_prog ; if ( v_flag || debug ) { if ( v_flag==1 && !debug ) { vprint("%s",phase->t_name) ; @@ -70,9 +65,8 @@ int runphase(trf *phase) { x_arg(l_content(*elem)) ; } x_arg( (char *)0 ) ; - result=run_exec(phase,prog) ; - throws(prog) ; - return result ; + result=run_exec(phase,prog) ; + return result ; } static int run_exec(trf *phase, const char *prog) { diff --git a/util/ack/trans.c b/util/ack/trans.c index 92e54ae9e..98a75a066 100644 --- a/util/ack/trans.c +++ b/util/ack/trans.c @@ -33,6 +33,8 @@ static void condit(growstring *, list_head *, list_head *, char *); static int mapflag(list_head *, const char *); static int mapexpand(char *, const char *); static void getcallargs(trf *); +static growstring without_bslash(const char *); +static void getprogram(trf *); int transform(trf *phase) { int ok ; @@ -42,6 +44,7 @@ int transform(trf *phase) { return 0 ; } getcallargs(phase) ; + getprogram(phase) ; ok= runphase(phase) ; if ( !ok ) rmfile(&out) ; /* Free the space occupied by the arguments, @@ -78,8 +81,9 @@ void getmapflags(trf *phase) { scanlist(l_first(phase->t_inputs),elem) { l_in = p_cont(*elem) ; if ( mapflag(&(phase->t_mapf),l_in->p_path) ) { - ptr= keeps(getvar(LIBVAR)) ; - clr_noscan(ptr) ; + growstring temp; + temp= without_bslash(getvar(LIBVAR)) ; + ptr= gr_final(&temp); #ifdef DEBUG if ( debug >=4 ) { vprint("phase %s, library %s(%s)\n", @@ -204,71 +208,47 @@ static void set_Rflag(char *argp) { /* */ /**************************************************************************/ -growstring scanb(const char *line) { - /* Scan a line for backslashes, setting the NO_SCAN bit in characters - preceded by a backslash. - */ - const char *in_c ; - int token ; - growstring result ; - enum { TEXT, ESCAPED } state = TEXT ; - - gr_init(&result) ; - for ( in_c= line ; *in_c ; in_c++ ) { - token= *in_c&0377 ; - switch( state ) { - case TEXT : - if ( token==BSLASH ) { - state= ESCAPED ; - } else { - gr_add(&result,token) ; - } - break ; - case ESCAPED : - gr_add(&result,token|NO_SCAN) ; - state=TEXT ; - break ; - } - } - gr_add(&result,0) ; - if ( state!=TEXT ) werror("flag line ends with %c",BSLASH) ; - return result ; -} - -growstring scanvars(const char *line) { +static growstring scanvars(const char *line) { /* Scan a line variable replacements started by S_VAR. - Two sequences exist: S_VAR name E_VAR, S_VAR name A_VAR text E_VAR. + Two sequences exist: S_VAR name C_VAR, S_VAR name A_VAR text C_VAR. neither name nor text may contain further replacements. In the first form an error message is issued if the name is not present in the variables, the second form produces text in that case. - The sequence S_VAR S_VAR is transformed into S_VAR. - This to allow later recognition in mapflags, where B_SLASH - would be preventing any recognition. */ const char *in_c ; - int token ; + int token, token_r ; growstring result, name ; char *tr ; enum { TEXT, FIRST, NAME, SKIP, COPY } state = TEXT ; + int escaped = NO; gr_init(&result) ; gr_init(&name) ; for ( in_c= line ; *in_c ; in_c++ ) { token= *in_c&0377 ; + token_r= (escaped ? 0 : token); + + /* A backslash escapes the next character. */ + if ( token_r==BSLASH ) { + if ( state==TEXT || state==COPY ) { + /* Keep BSLASH for later scans. */ + gr_add(&result,token) ; + } + escaped= YES; + continue; + } + escaped= NO; + switch( state ) { case TEXT : - if ( token==S_VAR ) { + if ( token_r==S_VAR ) { state= FIRST ; } else { gr_add(&result,token) ; } break ; case FIRST : - switch ( token ) { - case S_VAR : - state= TEXT ; - gr_add(&result,token) ; - break ; + switch ( token_r ) { case A_VAR : case C_VAR : fatal("empty string variable name") ; @@ -279,7 +259,7 @@ growstring scanvars(const char *line) { } break ; case NAME: - switch ( token ) { + switch ( token_r ) { case A_VAR : gr_add(&name,0) ; if ( tr=getvar(gr_start(name)) ) { @@ -311,16 +291,17 @@ growstring scanvars(const char *line) { } break ; case SKIP : - if ( token==C_VAR ) state= TEXT ; + if ( token_r==C_VAR ) state= TEXT ; break ; case COPY : - if ( token==C_VAR ) state= TEXT ; else { + if ( token_r==C_VAR ) state= TEXT ; else { gr_add(&result,token) ; } break ; } } gr_add(&result,0) ; + if ( escaped ) werror("flag line ends with %c",BSLASH) ; if ( state!=TEXT ) { werror("flag line misses %c",C_VAR) ; gr_throw(&name) ; @@ -332,68 +313,80 @@ static growstring scanexpr(const char *line) { /* Scan a line for conditional or flag expressions, dependent on the type. The format is S_EXPR suflist M_EXPR suflist T_EXPR tail C_EXPR - the head and tail are passed to treat, together with the + the head and tail are passed to condit(), together with the growstring for futher treatment. Nesting is not allowed. */ const char *in_c, *heads ; - int token ; + int token, token_r ; growstring sufs, tailval, result ; static list_head fsuff, lsuff ; enum { TEXT, FDOT, FSUF, LDOT, LSUF, FTAIL } state = TEXT ; + int escaped = NO; gr_init(&result) ; gr_init(&sufs) ; gr_init(&tailval) ; for ( in_c= line ; *in_c ; in_c++ ) { token= *in_c&0377 ; + token_r= (escaped ? 0 : token); + + /* A backslash escapes the next character. */ + if ( token_r==BSLASH ) { + if ( state==TEXT || state==FTAIL ) { + /* Keep BSLASH for later scans. */ + gr_add(&result,token) ; + } + escaped= YES; + continue; + } + escaped= NO; + switch( state ) { case TEXT : - if ( token==S_EXPR ) { + if ( token_r==S_EXPR ) { state= FDOT ; heads=in_c ; } else gr_add(&result,token) ; break ; case FDOT : - if ( token==M_EXPR ) { + if ( token_r==M_EXPR ) { state=LDOT ; break ; } - token &= ~NO_SCAN ; if ( token!=SUFCHAR ) { error("Missing %c in expression",SUFCHAR) ; } gr_add(&sufs,token) ; state=FSUF ; break ; case FSUF : - if ( token==M_EXPR || (token&~NO_SCAN)==SUFCHAR) { + if ( token_r==M_EXPR || token==SUFCHAR ) { gr_add(&sufs,0) ; l_add(&fsuff,gr_final(&sufs)) ; } - if ( token==M_EXPR ) { + if ( token_r==M_EXPR ) { state=LDOT ; - } else gr_add(&sufs,token&~NO_SCAN) ; + } else gr_add(&sufs,token) ; break ; case LDOT : - if ( token==T_EXPR ) { + if ( token_r==T_EXPR ) { state=FTAIL ; break ; } - token &= ~NO_SCAN ; if ( token!=SUFCHAR ) { error("Missing %c in expression",SUFCHAR) ; } gr_add(&sufs,token) ; state=LSUF ; break ; case LSUF : - if ( token==T_EXPR || (token&~NO_SCAN)==SUFCHAR) { + if ( token_r==T_EXPR || token==SUFCHAR) { gr_add(&sufs,0) ; l_add(&lsuff,gr_final(&sufs)) ; } - if ( token==T_EXPR ) { + if ( token_r==T_EXPR ) { state=FTAIL ; - } else gr_add(&sufs,token&~NO_SCAN) ; + } else gr_add(&sufs,token) ; break ; case FTAIL : - if ( token==C_EXPR ) { + if ( token_r==C_EXPR ) { /* Found one !! */ gr_add(&tailval,0) ; condit(&result,&fsuff,&lsuff,gr_start(tailval)) ; @@ -503,6 +496,7 @@ static int mapexpand(char *mapentry, const char *cflag) { void doassign(const char *line, const char *star, int length) { growstring varval, name, temp ; const char *ptr ; + int escaped = NO ; gr_init(&varval) ; gr_init(&name) ; @@ -517,12 +511,21 @@ void doassign(const char *line, const char *star, int length) { } temp= scanvars(ptr+1) ; for ( ptr=gr_start(temp); *ptr; ptr++ ) switch ( *ptr ) { + case BSLASH : + escaped= YES ; + gr_add(&varval,*ptr) ; + break ; case STAR : - if ( star ) { - while ( length-- ) gr_add(&varval,*star++|NO_SCAN) ; + if ( star && !escaped ) { + while ( length-- ) { + gr_add(&varval,BSLASH) ; + gr_add(&varval,*star++) ; + } break ; } + /* FALLTHROUGH */ default : + escaped= NO ; gr_add(&varval,*ptr) ; break ; } @@ -533,15 +536,16 @@ void doassign(const char *line, const char *star, int length) { #define ISBLANK(c) ( (c)==SPACE || (c)==TAB ) -static void unravel(char *line, void (*action)(char *)) { +static void unravel(const char *line, void (*action)(char *)) { /* Unravel the line, get arguments a la shell */ /* each argument is handled to action */ /* The input string is left intact */ - register char *in_c ; - register int token ; - enum { BLANK, ARG } state = BLANK ; + const char *in_c ; + int token ; + enum { BLANK, ARG, ESCAPED } state = BLANK ; growstring argum ; + /* Loop for each character of line, including final '\0' */ in_c=line ; for (;;) { token= *in_c&0377 ; @@ -549,9 +553,13 @@ static void unravel(char *line, void (*action)(char *)) { case BLANK : if ( token==0 ) break ; if ( !ISBLANK(token) ) { - state= ARG ; gr_init(&argum) ; - gr_add(&argum,token&~NO_SCAN) ; + gr_add(&argum,token) ; + if ( token == BSLASH ) { + state= ESCAPED ; + } else { + state= ARG ; + } } break ; case ARG : @@ -561,84 +569,94 @@ static void unravel(char *line, void (*action)(char *)) { gr_throw(&argum) ; state=BLANK ; } else { - gr_add(&argum,token&~NO_SCAN) ; + gr_add(&argum,token) ; + if ( token == BSLASH ) state= ESCAPED ; } break ; + case ESCAPED : + gr_add(&argum,token) ; + state= ARG ; + break ; } if ( token == 0 ) break ; in_c++ ; } } -static char *c_rep(char *string, char *place, char *rep) { - /* Produce a string in stable storage produced from 'string' - with the character at place replaced by rep - */ - growstring name ; - register char *nc ; - register char *xc ; - - gr_init(&name) ; - for ( nc=string ; *nc && ncp_path)) ; + gr_init(&argum); + for ( in_c= prefix1 ; *in_c ; in_c++ ) { + gr_add(&argum,*in_c) ; + } + for ( in_c= prefix2 ; *in_c ; in_c++ ) { + gr_add(&argum,*in_c) ; + } + for ( in_c= string ; *in_c ; in_c++ ) { + token= *in_c&0377 ; + if ( escaped ) { + /* Strip BSLASH, keep escaped character. */ + gr_add(&argum,token) ; + escaped= NO ; + continue; + } + switch ( token ) { + case BSLASH: + escaped= YES ; + break; + case C_IN: /* Input file */ + if ( in.p_path ) { /* Not for the combiners */ + for ( tr= in.p_path ; *tr; tr++ ) { + gr_add(&argum,*tr); } + } else { /* For the combiners */ + gr_add(&argum,0); + tr= gr_final(&argum); + in_c++; + scanlist( l_first(*comb_args), elem ) { + char *p = p_cont(*elem)->p_path ; + addargs3(tr,p,in_c) ; + } + throws(tr); + return; } - return ; - } - if ( in.p_path ) { /* Not for the combiners */ - temp=c_rep(string,repc,in.p_path) ; - addargs(temp) ; - throws(temp) ; - } else { /* For the combiners */ - scanlist( l_first(*comb_args), elem ) { - temp=c_rep(string,repc,p_cont(*elem)->p_path); - addargs(temp) ; - throws(temp) ; - } - } - return ; - } - repc=strchr(string,C_OUT) ; - if ( repc ) { - /* replace the outfile token as with the infile token */ + break; + case C_OUT: /* Output file */ #ifdef DEBUG - if ( !out.p_path ) fatal("missing output filename") ; + if ( !out.p_path ) fatal("missing output filename") ; #endif - temp=c_rep(string,repc,out.p_path) ; - addargs(temp) ; - throws(temp) ; - return ; + for ( tr= out.p_path ; *tr ; tr++ ) { + gr_add(&argum,*tr) ; + } + break; + default: + gr_add(&argum,token) ; + break; + } } - temp= keeps(string) ; - clr_noscan(temp) ; - l_add(curargs,temp) ; + gr_add(&argum,0) ; + tr= gr_final(&argum) ; + l_add(curargs,tr) ; +} + +static void addargs(char *string) { + addargs3("", "", string) ; } static void getcallargs(trf *phase) { @@ -646,11 +664,11 @@ static void getcallargs(trf *phase) { arg1= scanvars(phase->t_argd) ; #ifdef DEBUG - if ( debug>=3 ) { vprint("\tvars: ") ; prns(gr_start(arg1)) ; } + if ( debug>=3 ) vprint("\tvars: %s", gr_start(arg1)) ; #endif arg2= scanexpr(gr_start(arg1)) ; #ifdef DEBUG - if ( debug>=3 ) { vprint("\texpr: ") ; prns(gr_start(arg2)) ; } + if ( debug>=3 ) vprint("\texpr: %s", gr_start(arg2)) ; #endif gr_throw(&arg1) ; curargs= &phase->t_args ; @@ -658,3 +676,40 @@ static void getcallargs(trf *phase) { unravel( gr_start(arg2), addargs ) ; gr_throw(&arg2) ; } + +static growstring without_bslash(const char *string) { + /* Strip backslashes from a copy of the string. */ + growstring result; + const char *in_c ; + int token ; + int escaped = NO ; + + gr_init(&result) ; + for ( in_c= string ; *in_c ; in_c++ ) { + token= *in_c&0377 ; + if ( token==BSLASH && !escaped ) { + escaped= YES ; + } else { + gr_add(&result,token); + escaped= NO ; + } + } + gr_add(&result,0); + return result; +} + +static void getprogram(trf *phase) { + growstring prog1, prog2 ; + const char *in_c ; + int token ; + int escaped = NO ; + + /* Expand string variables in program name. */ + prog1= scanvars(phase->t_prog) ; + throws(phase->t_prog) ; + + /* Strip backslashes. */ + prog2= without_bslash(gr_start(prog1)); + gr_throw(&prog1); + phase->t_prog= gr_final(&prog2); +} diff --git a/util/ack/util.c b/util/ack/util.c index caad71cdd..b3754f28e 100644 --- a/util/ack/util.c +++ b/util/ack/util.c @@ -46,9 +46,6 @@ char *ack_basename(const char *string) { case '/' : last_start=fetch+1 ; break ; case 0 : goto out ; } - if ( !isascii(ctoken) || !isprint(ctoken) ) { - werror("non-ascii characters in argument %s",string) ; - } } out: if ( ! *last_start ) fuerror("empty filename \"%s\"",string) ; @@ -61,13 +58,6 @@ out: return retval ; } -void clr_noscan(char *str) { - register char *ptr ; - for ( ptr=str ; *ptr ; ptr++ ) { - *ptr&= ~NO_SCAN ; - } -} - char *skipblank(char *str) { register char *ptr ; @@ -105,15 +95,6 @@ void vprint(const char* fmt, ...) va_end(ap); } -#ifdef DEBUG -void prns(const char *s) { - for ( ; *s ; s++ ) { - putc((*s&0377)&~NO_SCAN,STDOUT) ; - } - putc('\n',STDOUT) ; -} -#endif - /* VARARGS1 */ void fuerror(const char *fmt, ...) { /* Fatal user error */ From 486c5162422f0f89aa3b89a91c4cc849bede0fe4 Mon Sep 17 00:00:00 2001 From: George Koehler Date: Tue, 15 Nov 2016 11:58:13 -0500 Subject: [PATCH 7/7] Stop trying to remove core dumps. unlink("core") doesn't work with OpenBSD, where core dumps have names like "ncg.core". Users who don't want core dumps can turn them off with "ulimit -c 0" in sh(1). Then the system doesn't write a core dump. That's better than writing core then unlinking it. --- util/ack/ack.1.X | 2 +- util/ack/run.c | 2 -- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/util/ack/ack.1.X b/util/ack/ack.1.X index 689992ac7..1e35bca81 100644 --- a/util/ack/ack.1.X +++ b/util/ack/ack.1.X @@ -100,7 +100,7 @@ Note: \fIack\fP refuses to overwrite argument \fI.e\fP files. .IP \-t Preserve all intermediate files. If two \fB\-t\fP are used, -\fIack\fP also preserves core dumps and output of failed transformations. +\fIack\fP also preserves output of failed transformations. .IP \-w Suppress all warning messages. diff --git a/util/ack/run.c b/util/ack/run.c index ed88d66c7..32cb59fe5 100644 --- a/util/ack/run.c +++ b/util/ack/run.c @@ -87,8 +87,6 @@ static int run_exec(trf *phase, const char *prog) { } } while ( waitchild!=child) ; if ( status ) { - if ( status&0200 && (status&0177)!=SIGQUIT && - t_flag<=1 ) unlink("core") ; switch ( status&0177 ) { case 0 : break ;