From c4a9d6a45c90b4d4f9514e10c63a883f676fa821 Mon Sep 17 00:00:00 2001 From: PatR Date: Fri, 25 Dec 2015 21:54:01 -0800 Subject: [PATCH] newline handling In light of the recent 'bad options' feedback issue where \r messed up message display, try to to make newline handling be more consistent. I'm sure there are lots of places that still handle \n manually, but it's a start. --- include/extern.h | 1 + src/files.c | 19 ++++--------------- src/hacklib.c | 16 ++++++++++++++++ src/pager.c | 11 ++++------- src/version.c | 7 ++----- 5 files changed, 27 insertions(+), 27 deletions(-) diff --git a/include/extern.h b/include/extern.h index 5bfae6080..ad7748bb5 100644 --- a/include/extern.h +++ b/include/extern.h @@ -835,6 +835,7 @@ E char *FDECL(lcase, (char *)); E char *FDECL(ucase, (char *)); E char *FDECL(upstart, (char *)); E char *FDECL(mungspaces, (char *)); +E char *FDECL(strip_newline, (char *)); E char *FDECL(eos, (char *)); E boolean FDECL(str_end_is, (const char *, const char *)); E char *FDECL(strkitten, (char *, CHAR_P)); diff --git a/src/files.c b/src/files.c index 18481a5f4..a524af027 100644 --- a/src/files.c +++ b/src/files.c @@ -2563,7 +2563,7 @@ read_config_file(filename, src) const char *filename; int src; { - char buf[4 * BUFSZ], *p; + char buf[4 * BUFSZ]; FILE *fp; boolean rv = TRUE; /* assume successful parse */ @@ -2581,13 +2581,7 @@ line at this level. OR: Forbid multiline stuff for alternate config sources. */ #endif - if ((p = index(buf, '\n')) != 0) { - /* in case file has CR+LF format on non-CR+LF platform */ - if (p > buf && *(p - 1) == '\r') - --p; - *p = '\0'; /* strip newline */ - } - if (!parse_config_line(fp, buf, src)) { + if (!parse_config_line(fp, strip_newline(buf), src)) { static const char badoptionline[] = "Bad option line: \"%s\""; /* truncate buffer if it's long; this is actually conservative */ @@ -3505,7 +3499,6 @@ char *nowin_buf; unsigned oid; /* book identifier */ { dlb *fp; - char *endp; char line[BUFSZ], lastline[BUFSZ]; int scope = 0; @@ -3556,18 +3549,14 @@ unsigned oid; /* book identifier */ *line = *lastline = '\0'; while (dlb_fgets(line, sizeof line, fp) != 0) { linect++; - if ((endp = index(line, '\n')) != 0) - *endp = 0; + (void) strip_newline(line); switch (line[0]) { case '%': if (!strncmpi(&line[1], "section ", sizeof("section ") - 1)) { char *st = &line[9]; /* 9 from "%section " */ scope = SECTIONSCOPE; - if (!strcmpi(st, tribsection)) - matchedsection = TRUE; - else - matchedsection = FALSE; + matchedsection = !strcmpi(st, tribsection) ? TRUE : FALSE; } else if (!strncmpi(&line[1], "title ", sizeof("title ") - 1)) { char *st = &line[7]; /* 7 from "%title " */ char *p1, *p2; diff --git a/src/hacklib.c b/src/hacklib.c index a8ecfaea1..256ac5743 100644 --- a/src/hacklib.c +++ b/src/hacklib.c @@ -18,6 +18,7 @@ char * ucase (char *) char * upstart (char *) char * mungspaces (char *) + char * strip_newline (char *) char * eos (char *) boolean str_end_is (const char *, const char *) char * strkitten (char *,char) @@ -158,6 +159,21 @@ char *bp; return bp; } +/* remove \n from end of line; remove \r too if one is there */ +char * +strip_newline(str) +char *str; +{ + char *p = index(str, '\n'); + + if (p) { + if (p > str && *(p - 1) == '\r') + --p; + *p = '\0'; + } + return str; +} + /* return the end of a string (pointing at '\0') */ char * eos(s) diff --git a/src/pager.c b/src/pager.c index 0a4fc87a7..0664bef5e 100644 --- a/src/pager.c +++ b/src/pager.c @@ -476,7 +476,7 @@ boolean user_typed_name, without_asking; } else if (!skipping_entry) { if (!(ep = index(buf, '\n'))) goto bad_data_file; - *ep = 0; + (void) strip_newline((ep > buf) ? ep - 1 : ep); /* if we match a key that begins with "~", skip this entry */ chk_skip = (*buf == '~') ? 1 : 0; if (pmatch(&buf[chk_skip], dbase_str) @@ -524,8 +524,7 @@ boolean user_typed_name, without_asking; for (i = 0; i < entry_count; i++) { if (!dlb_fgets(buf, BUFSZ, fp)) goto bad_data_file; - if ((ep = index(buf, '\n')) != 0) - *ep = 0; + (void) strip_newline(buf); if (index(buf + 1, '\t') != 0) (void) tabexpand(buf + 1); putstr(datawin, 0, buf + 1); @@ -1126,7 +1125,7 @@ char *cbuf; { dlb *fp; char bufr[BUFSZ]; - register char *buf = &bufr[6], *ep, ctrl, meta; + register char *buf = &bufr[6], ctrl, meta; fp = dlb_fopen(CMDHELPFILE, "r"); if (!fp) { @@ -1140,9 +1139,7 @@ char *cbuf; if ((ctrl && *buf == '^' && *(buf + 1) == ctrl) || (meta && *buf == 'M' && *(buf + 1) == '-' && *(buf + 2) == meta) || *buf == q) { - ep = index(buf, '\n'); - if (ep) - *ep = 0; + (void) strip_newline(buf); if (ctrl && buf[2] == '\t') { buf = bufr + 1; (void) strncpy(buf, "^? ", 8); diff --git a/src/version.c b/src/version.c index 97c8d0321..becefd8f7 100644 --- a/src/version.c +++ b/src/version.c @@ -57,7 +57,7 @@ int doextversion() { dlb *f; - char *cr, *pd, buf[BUFSZ]; + char *pd, buf[BUFSZ]; winid win = create_nhwindow(NHW_TEXT); boolean rtadded = FALSE; @@ -94,10 +94,7 @@ doextversion() boolean prolog = TRUE; /* to skip indented program name */ while (dlb_fgets(buf, BUFSZ, f)) { - if ((cr = index(buf, '\n')) != 0) - *cr = 0; - if ((cr = index(buf, '\r')) != 0) - *cr = 0; + (void) strip_newline(buf); if (index(buf, '\t') != 0) (void) tabexpand(buf); -- 2.40.0