From 27d8b631cd5b628577194b689b6182a8b4fc4bae Mon Sep 17 00:00:00 2001 From: PatR Date: Sat, 25 Apr 2015 02:11:13 -0700 Subject: [PATCH] isspace() usage Replace most uses of isspace() with a simple test for ' ' after processing the string buffer with mungspaces (which replaces tab with space, converts instances of consecutive whitespace into a single space, and removes leading and trailing spaces). The uses where this wasn't done now cast their argument to (uchar) so that platforms with signed chars will never pass negative values to it. I didn't mess with the menu coloring code (except for casts to the isspace() argument); it almost certainly could benefit from using mungspaces. I did mess with the symset processing quite a bit, and hope I haven't accidentally broken anything. Default symbols and DECgraphics symbols still parse and display ok, so the rest of dat/symbols should be ok too. I didn't test symbols in the user's config file because I don't remember how that's supposed to work. --- src/end.c | 15 ++-- src/engrave.c | 12 ++-- src/files.c | 187 ++++++++++++++++++++++++-------------------------- src/hacklib.c | 3 +- src/options.c | 58 ++++++++-------- 5 files changed, 133 insertions(+), 142 deletions(-) diff --git a/src/end.c b/src/end.c index 38c83f378..f3d3928a2 100644 --- a/src/end.c +++ b/src/end.c @@ -1,4 +1,4 @@ -/* NetHack 3.5 end.c $NHDT-Date: 1425319883 2015/03/02 18:11:23 $ $NHDT-Branch: master $:$NHDT-Revision: 1.81 $ */ +/* NetHack 3.5 end.c $NHDT-Date: 1429953061 2015/04/25 09:11:01 $ $NHDT-Branch: master $:$NHDT-Revision: 1.93 $ */ /* NetHack 3.5 end.c $Date: 2012/04/09 02:56:30 $ $Revision: 1.79 $ */ /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */ /* NetHack may be freely redistributed. See license for details. */ @@ -1605,10 +1605,11 @@ wordcount(p) char *p; { int words = 0; - while(*p){ - while(*p && isspace(*p))p++; - if(*p) words++; - while(*p && !isspace(*p))p++; + + while (*p) { + while (*p && isspace((uchar)*p)) p++; + if (*p) words++; + while (*p && !isspace((uchar)*p)) p++; } return words; } @@ -1620,8 +1621,8 @@ bel_copy1(inp, out) char *in = *inp; out += strlen(out); /* eos() */ - while(*in && isspace(*in)) in++; - while(*in && !isspace(*in)) *out++ = *in++; + while (*in && isspace((uchar)*in)) in++; + while (*in && !isspace((uchar)*in)) *out++ = *in++; *out = '\0'; *inp = in; } diff --git a/src/engrave.c b/src/engrave.c index 5a8aa38f3..a29221549 100644 --- a/src/engrave.c +++ b/src/engrave.c @@ -1,11 +1,10 @@ -/* NetHack 3.5 engrave.c $NHDT-Date$ $NHDT-Branch$:$NHDT-Revision$ */ +/* NetHack 3.5 engrave.c $NHDT-Date: 1429953062 2015/04/25 09:11:02 $ $NHDT-Branch: master $:$NHDT-Revision: 1.47 $ */ /* NetHack 3.5 engrave.c $Date: 2012/12/20 01:48:36 $ $Revision: 1.39 $ */ /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */ /* NetHack may be freely redistributed. See license for details. */ #include "hack.h" #include "lev.h" -#include STATIC_VAR NEARDATA struct engr *head_engr; @@ -973,10 +972,13 @@ doengrave() /* Prompt for engraving! */ Sprintf(qbuf,"What do you want to %s the %s here?", everb, eloc); getlin(qbuf, ebuf); + /* convert tabs to spaces and condense consecutive spaces to one */ + mungspaces(ebuf); /* Count the actual # of chars engraved not including spaces */ len = strlen(ebuf); - for (sp = ebuf; *sp; sp++) if (isspace(*sp)) len -= 1; + for (sp = ebuf; *sp; sp++) + if (*sp == ' ') len -= 1; if (len == 0 || index(ebuf, '\033')) { if (zapwand) { @@ -997,7 +999,7 @@ doengrave() /* Mix up engraving if surface or state of mind is unsound. Note: this won't add or remove any spaces. */ for (sp = ebuf; *sp; sp++) { - if (isspace(*sp)) continue; + if (*sp == ' ') continue; if (((type == DUST || type == ENGR_BLOOD) && !rn2(25)) || (Blind && !rn2(11)) || (Confusion && !rn2(7)) || (Stunned && !rn2(4)) || (Hallucination && !rn2(2))) @@ -1081,7 +1083,7 @@ doengrave() /* Chop engraving down to size if necessary */ if (len > maxelen) { for (sp = ebuf; (maxelen && *sp); sp++) - if (!isspace(*sp)) maxelen--; + if (*sp == ' ') maxelen--; if (!maxelen && *sp) { *sp = (char)0; if (multi) nomovemsg = "You cannot write any more."; diff --git a/src/files.c b/src/files.c index 37af6e9c1..f50b8c33e 100644 --- a/src/files.c +++ b/src/files.c @@ -1,4 +1,4 @@ -/* NetHack 3.5 files.c $NHDT-Date: 1428972596 2015/04/14 00:49:56 $ $NHDT-Branch: master $:$NHDT-Revision: 1.164 $ */ +/* NetHack 3.5 files.c $NHDT-Date: 1429953063 2015/04/25 09:11:03 $ $NHDT-Branch: master $:$NHDT-Revision: 1.166 $ */ /* NetHack 3.5 files.c $Date: 2012/03/10 02:49:08 $ $Revision: 1.124 $ */ /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */ /* NetHack may be freely redistributed. See license for details. */ @@ -10,8 +10,6 @@ #include "wintty.h" /* more() */ #endif -#include - #if (!defined(MAC) && !defined(O_WRONLY) && !defined(AZTEC_C)) || defined(USE_FCNTL) #include #endif @@ -2060,9 +2058,9 @@ int prefixid; #define match_varname(INP,NAM,LEN) match_optname(INP, NAM, LEN, TRUE) int -parse_config_line(fp, buf, src) +parse_config_line(fp, origbuf, src) FILE *fp; -char *buf; +char *origbuf; int src; { #if defined(MICRO) && !defined(NOCWD_ASSUMPTIONS) @@ -2071,37 +2069,39 @@ int src; #ifdef SYSCF int n; #endif - char *bufp, *altp; + char *bufp, *altp, buf[BUFSZ]; uchar translate[MAXPCHARS]; int len; - /* lines beginning with '#' are comments */ - if (*buf == '#') - return 1; - /* remove trailing whitespace */ - bufp = eos(buf); - while (--bufp > buf && isspace(*bufp)) - continue; - - if (bufp <= buf) - return 1; /* skip all-blank lines */ - else - *(bufp + 1) = '\0'; /* terminate line */ + /* convert any tab to space, condense consecutive spaces into one, + remove leading and trailing spaces (exception: if there is nothing + but spaces, one of them will be kept even though it leads/trails) */ + mungspaces(strcpy(buf, origbuf)); + /* lines beginning with '#' are comments; accept empty lines too */ + if (!*buf || *buf == '#' || !strcmp(buf, " ")) return 1; /* find the '=' or ':' */ bufp = index(buf, '='); altp = index(buf, ':'); if (!bufp || (altp && altp < bufp)) bufp = altp; if (!bufp) return 0; - - /* skip whitespace between '=' and value */ - do { ++bufp; } while (isspace(*bufp)); + /* skip past '=', then space between it and value, if any */ + ++bufp; + if (*bufp == ' ') ++bufp; /* Go through possible variables */ /* some of these (at least LEVELS and SAVE) should now set the * appropriate fqn_prefix[] rather than specialized variables */ if (match_varname(buf, "OPTIONS", 4)) { + /* hack: un-mungspaces to allow consecutive spaces in + general options until we verify that this is unnecessary; + '=' or ':' is guaranteed to be present */ + bufp = index(origbuf, '='); + altp = index(origbuf, ':'); + if (!bufp || (altp && altp < bufp)) bufp = altp; + ++bufp; /* skip '='; parseoptions() handles spaces */ + parseoptions(bufp, TRUE, TRUE); if (plname[0]) /* If a name was given */ plnamesuffix(); /* set the character class */ @@ -2190,16 +2190,17 @@ int src; } else if (src == SET_IN_SYS && match_varname(buf, "SHELLERS", 8)) { if (sysopt.shellers) free(sysopt.shellers); sysopt.shellers = dupstr(bufp); - } else if (src == SET_IN_SYS && match_varname(buf, "EXPLORERS", 7)) { - if (sysopt.explorers) free(sysopt.explorers); + } else if (src == SET_IN_SYS && match_varname(buf, "EXPLORERS", 7)) { + if (sysopt.explorers) free(sysopt.explorers); sysopt.explorers = dupstr(bufp); } else if (src == SET_IN_SYS && match_varname(buf, "DEBUGFILES", 5)) { - if (sysopt.debugfiles) free(sysopt.debugfiles); /* if showdebug() has already been called (perhaps we've added some debugpline() calls to option processing) and has found a value for getenv("DEBUGFILES"), don't override that */ - if (sysopt.env_dbgfl == 0) - sysopt.debugfiles = dupstr(bufp); + if (sysopt.env_dbgfl == 0) { + if (sysopt.debugfiles) free(sysopt.debugfiles); + sysopt.debugfiles = dupstr(bufp); + } } else if (src == SET_IN_SYS && match_varname(buf, "SUPPORT", 7)) { if (sysopt.support) free(sysopt.support); sysopt.support = dupstr(bufp); @@ -2308,33 +2309,29 @@ int src; } else if (match_varname(buf, "SYMBOLS", 4)) { char *op, symbuf[BUFSZ]; boolean morelines; + do { - morelines = FALSE; - - /* strip leading and trailing white space */ - while (isspace(*bufp)) bufp++; - op = eos(bufp); - while (--op >= bufp && isspace(*op)) *op = '\0'; - /* check for line continuation (trailing '\') */ op = eos(bufp); - if (--op >= bufp && *op == '\\') { + morelines = (--op >= bufp && *op == '\\'); + if (morelines) { *op = '\0'; - morelines = TRUE; /* strip trailing space now that '\' is gone */ - while (--op >= bufp && isspace(*op)) *op = '\0'; + if (--op >= bufp && *op == ' ') *op = '\0'; } /* parse here */ parsesymbols(bufp); - if (morelines) - do { - *symbuf = '\0'; - if (!fgets(symbuf, BUFSZ, fp)) { - morelines = FALSE; - break; - } - bufp = symbuf; - } while (*bufp == '#'); + if (morelines) { + do { + *symbuf = '\0'; + if (!fgets(symbuf, BUFSZ, fp)) { + morelines = FALSE; + break; + } + mungspaces(symbuf); + bufp = symbuf; + } while (*bufp == '#'); + } } while (morelines); switch_symbols(TRUE); } else if (match_varname(buf, "WIZKIT", 6)) { @@ -2355,7 +2352,7 @@ int src; extern int amii_numcolors; int val = atoi( bufp ); amii_numcolors = 1L << min( DEPTH, val ); -#if defined(SYSFLAGS) +# ifdef SYSFLAGS } else if (match_varname(buf, "DRIPENS", 7)) { int i, val; char *t; @@ -2364,7 +2361,7 @@ int src; sscanf(t, "%d", &val ); sysflags.amii_dripens[i] = val; } -#endif +# endif } else if (match_varname(buf, "SCREENMODE", 10 )) { extern long amii_scrnmode; if (!stricmp(bufp,"req")) @@ -2450,7 +2447,7 @@ int src; { sscanf(t, "%d", &backg[i]); } -#endif +#endif /*AMIGA*/ #ifdef USER_SOUNDS } else if (match_varname(buf, "SOUNDDIR", 8)) { sounddir = dupstr(bufp); @@ -2717,46 +2714,36 @@ int which_set; struct symparse *symp = (struct symparse *)0; char *bufp, *commentp, *altp; - if (*buf == '#') + /* convert each instance of whitespace (tabs, consecutive spaces) + into a single space; leading and trailing spaces are stripped */ + mungspaces(buf); + if (!*buf || *buf == '#' || !strcmp(buf, " ")) return 1; - - /* remove trailing comment(s) */ - commentp = eos(buf); - while (--commentp > buf) { - if (*commentp != '#') continue; + /* remove trailing comment, if any */ + if ((commentp = rindex(buf, '#')) != 0) { *commentp = '\0'; + /* remove space preceding the stripped comment, if any; + we know 'commentp > buf' because *buf=='#' was caught above */ + if (commentp[-1] == ' ') *--commentp = '\0'; } - /* remove trailing whitespace */ - bufp = eos(buf); - while (--bufp > buf && isspace(*bufp)) - continue; - - if (bufp <= buf) - return 1; /* skip all-blank lines */ - else - *(bufp + 1) = '\0'; /* terminate line */ - - /* skip leading whitespace on option name */ - while (isspace(*buf)) ++buf; - /* find the '=' or ':' */ bufp = index(buf, '='); altp = index(buf, ':'); if (!bufp || (altp && altp < bufp)) bufp = altp; if (!bufp) { if (strncmpi(buf, "finish", 6) == 0) { - /* end current graphics set */ - if (chosen_symset_start) - chosen_symset_end = TRUE; - chosen_symset_start = FALSE; - return 1; + /* end current graphics set */ + if (chosen_symset_start) + chosen_symset_end = TRUE; + chosen_symset_start = FALSE; + return 1; } return 0; } - - /* skip whitespace between '=' and value */ - do { ++bufp; } while (isspace(*bufp)); + /* skip '=' and space which follows, if any */ + ++bufp; + if (*bufp == ' ') ++bufp; symp = match_sym(buf); if (!symp) @@ -2829,17 +2816,19 @@ int which_set; case 0: /* start of symset */ if (!strcmpi(bufp, symset[which_set].name)) { - /* matches desired one */ - chosen_symset_start = TRUE; - /* these init_*() functions clear symset fields too */ - if (which_set == ROGUESET) init_r_symbols(); - else if (which_set == PRIMARY) init_l_symbols(); + /* matches desired one */ + chosen_symset_start = TRUE; + /* these init_*() functions clear symset fields too */ + if (which_set == ROGUESET) + init_r_symbols(); + else if (which_set == PRIMARY) + init_l_symbols(); } break; case 1: /* finish symset */ if (chosen_symset_start) - chosen_symset_end = TRUE; + chosen_symset_end = TRUE; chosen_symset_start = FALSE; break; case 2: @@ -2851,14 +2840,14 @@ int which_set; case 4: /* color:off */ if (chosen_symset_start) { if (bufp) { - if (!strcmpi(bufp, "true") || - !strcmpi(bufp, "yes") || - !strcmpi(bufp, "on")) - symset[which_set].nocolor = 0; + if (!strcmpi(bufp, "true") || + !strcmpi(bufp, "yes") || + !strcmpi(bufp, "on")) + symset[which_set].nocolor = 0; else if (!strcmpi(bufp, "false") || - !strcmpi(bufp, "no") || - !strcmpi(bufp, "off")) - symset[which_set].nocolor = 1; + !strcmpi(bufp, "no") || + !strcmpi(bufp, "off")) + symset[which_set].nocolor = 1; } } break; @@ -2866,17 +2855,17 @@ int which_set; if (chosen_symset_start) { int n = 0; while (known_restrictions[n]) { - if (!strcmpi(known_restrictions[n], bufp)) { - switch(n) { - case 0: symset[which_set].primary = 1; - break; - case 1: symset[which_set].rogue = 1; - break; - } - break; /* while loop */ - } - n++; - } + if (!strcmpi(known_restrictions[n], bufp)) { + switch(n) { + case 0: symset[which_set].primary = 1; + break; + case 1: symset[which_set].rogue = 1; + break; + } + break; /* while loop */ + } + n++; + } } break; } diff --git a/src/hacklib.c b/src/hacklib.c index 6f10e5bf7..93eab1f93 100644 --- a/src/hacklib.c +++ b/src/hacklib.c @@ -1,4 +1,4 @@ -/* NetHack 3.5 hacklib.c $NHDT-Date: 1428806394 2015/04/12 02:39:54 $ $NHDT-Branch: master $:$NHDT-Revision: 1.34 $ */ +/* NetHack 3.5 hacklib.c $NHDT-Date: 1429953063 2015/04/25 09:11:03 $ $NHDT-Branch: master $:$NHDT-Revision: 1.38 $ */ /* NetHack 3.5 hacklib.c $Date: 2009/05/06 10:46:32 $ $Revision: 1.23 $ */ /* SCCS Id: @(#)hacklib.c 3.5 2007/04/30 */ /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */ @@ -135,6 +135,7 @@ char *bp; boolean was_space = TRUE; for (p = p2 = bp; (c = *p) != '\0'; p++) { + if (c == '\n') break; /* treat newline the same as end-of-string */ if (c == '\t') c = ' '; if (c != ' ' || !was_space) *p2++ = c; was_space = (c == ' '); diff --git a/src/options.c b/src/options.c index 2291b0c13..1e3d18338 100644 --- a/src/options.c +++ b/src/options.c @@ -1,4 +1,4 @@ -/* NetHack 3.5 options.c $NHDT-Date: 1428088105 2015/04/03 19:08:25 $ $NHDT-Branch: scshunt-regex $:$NHDT-Revision: 1.182 $ */ +/* NetHack 3.5 options.c $NHDT-Date: 1429953065 2015/04/25 09:11:05 $ $NHDT-Branch: master $:$NHDT-Revision: 1.186 $ */ /* NetHack 3.5 options.c $Date: 2012/04/09 02:56:30 $ $Revision: 1.153 $ */ /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */ /* NetHack may be freely redistributed. See license for details. */ @@ -544,7 +544,7 @@ boolean val_allowed; *q = index(user_string, '='); if (!p || (q && q < p)) p = q; - while(p && p > user_string && isspace(*(p-1))) p--; + while (p && p > user_string && isspace((uchar)*(p-1))) p--; if (p) len = (int)(p - user_string); } @@ -1176,7 +1176,7 @@ char *str; tmps = cs; tmps++; - while (*tmps && isspace(*tmps)) tmps++; + while (*tmps && isspace((uchar)*tmps)) tmps++; for (i = 0; i < SIZE(colornames); i++) if (strstri(tmps, colornames[i].name) == tmps) { @@ -1191,7 +1191,7 @@ char *str; tmps = strchr(str, '&'); if (tmps) { tmps++; - while (*tmps && isspace(*tmps)) tmps++; + while (*tmps && isspace((uchar)*tmps)) tmps++; for (i = 0; i < SIZE(attrnames); i++) if (strstri(tmps, attrnames[i].name) == tmps) { a = attrnames[i].attr; @@ -1205,7 +1205,7 @@ char *str; tmps = str; if ((*tmps == '"') || (*tmps == '\'')) { cs--; - while (isspace(*cs)) cs--; + while (isspace((uchar)*cs)) cs--; if (*cs == *tmps) { *cs = '\0'; tmps++; @@ -1281,9 +1281,9 @@ boolean tinitial, tfrom_file; } /* strip leading and trailing white space */ - while (isspace(*opts)) opts++; + while (isspace((uchar)*opts)) opts++; op = eos(opts); - while (--op >= opts && isspace(*op)) *op = '\0'; + while (--op >= opts && isspace((uchar)*op)) *op = '\0'; if (!*opts) return; negated = FALSE; @@ -4115,8 +4115,7 @@ const char *mapping; ape->pattern = (char *) alloc(textsize+1); Strcpy(ape->pattern, text2); ape->grab = grab; - if (!*apehead) ape->next = (struct autopickup_exception *)0; - else ape->next = *apehead; + ape->next = *apehead; *apehead = ape; } else { raw_print("syntax error in AUTOPICKUP_EXCEPTION"); @@ -4207,7 +4206,7 @@ parsesymbols(opts) register char *opts; { int val; - char *op, *symname, *strval, *p; + char *op, *symname, *strval; struct symparse *symp; if ((op = index(opts, ',')) != 0) { @@ -4218,18 +4217,13 @@ register char *opts; /* S_sample:string */ symname = opts; strval = index(opts, ':'); - if (!symname || !strval) return; - *strval++ = 0; + if (!strval) strval = index(opts, '='); + if (!strval) return; + *strval++ = '\0'; - /* strip leading and trailing white space from symname */ - while (isspace(*symname)) symname++; - p = eos(symname); - while (--p >= symname && isspace(*p)) *p = '\0'; - - /* strip leading and trailing white space from strval */ - while (isspace(*strval)) strval++; - p = eos(strval); - while (--p >= strval && isspace(*p)) *p = '\0'; + /* strip leading and trailing white space from symname and strval */ + mungspaces(symname); + mungspaces(strval); symp = match_sym(symname); if (!symp) return; @@ -4250,8 +4244,12 @@ char *buf; struct symparse *sp = loadsyms; if (!p || (q && q < p)) p = q; - while(p && p > buf && isspace(*(p-1))) p--; - if (p) len = (int)(p - buf); + if (p) { + /* note: there will be at most one space before the '=' + because caller has condensed buf[] with mungspaces() */ + if (p > buf && p[-1] == ' ') p--; + len = (int)(p - buf); + } while(sp->range) { if ((len >= strlen(sp->name)) && !strncmpi(buf, sp->name, len)) return sp; @@ -4424,7 +4422,7 @@ struct fruit *replace_fruit; for(c = pl_fruit; *c >= '0' && *c <= '9'; c++) ; - if (isspace(*c) || *c == 0) numeric = TRUE; + if (isspace((uchar)*c) || *c == 0) numeric = TRUE; } if (found || numeric || !strncmp(str, "cursed ", 7) || @@ -4847,34 +4845,34 @@ char *op; wn = tfg = tbg = (char *)0; /* until first non-space in case there's leading spaces - before colorname*/ - while(*newop && isspace(*newop)) newop++; + if (*newop == ' ') newop++; if (*newop) wn = newop; else return 0; /* until first space - colorname*/ - while(*newop && !isspace(*newop)) newop++; + while (*newop && *newop != ' ') newop++; if (*newop) *newop = '\0'; else return 0; newop++; /* until first non-space - before foreground*/ - while(*newop && isspace(*newop)) newop++; + if (*newop == ' ') newop++; if (*newop) tfg = newop; else return 0; /* until slash - foreground */ - while(*newop && *newop != '/') newop++; + while (*newop && *newop != '/') newop++; if (*newop) *newop = '\0'; else return 0; newop++; /* until first non-space (in case there's leading space after slash) - before background */ - while(*newop && isspace(*newop)) newop++; + if (*newop == ' ') newop++; if (*newop) tbg = newop; else return 0; /* until first space - background */ - while(*newop && !isspace(*newop)) newop++; + while (*newop && *newop != ' ') newop++; if (*newop) *newop++ = '\0'; for (j = 0; j < 4; ++j) {