]> granicus.if.org Git - nethack/commitdiff
newline handling
authorPatR <rankin@nethack.org>
Sat, 26 Dec 2015 05:54:01 +0000 (21:54 -0800)
committerPatR <rankin@nethack.org>
Sat, 26 Dec 2015 05:54:01 +0000 (21:54 -0800)
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
src/files.c
src/hacklib.c
src/pager.c
src/version.c

index 5bfae608096eeb01e33835ee2ffb4c128596d205..ad7748bb5253916f4e9a8fa305842c89a2fe7acf 100644 (file)
@@ -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));
index 18481a5f46719addcb1879566149cf37523c1d7d..a524af027bf7beca7b3456122a2b779cb7c14584 100644 (file)
@@ -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;
index a8ecfaea1cef6da22b2bc8519d2c7a3c47ecc097..256ac57437b3ba527f9c866bbba1277eb70c5b32 100644 (file)
@@ -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)
index 0a4fc87a78bab3829c3829625f1027ef4cbb2e8f..0664bef5e9ab078ba0c4fa65f4596eed5b294572 100644 (file)
@@ -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);
index 97c8d0321f3210fc42fd1b84ad73e2d8422d9172..becefd8f766ae23b4eda9241a1186c1c4f4a6d56 100644 (file)
@@ -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);