]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
ul.c: escape handling refactored
authorSami Kerola <kerolasa@iki.fi>
Sat, 30 Apr 2011 11:06:26 +0000 (13:06 +0200)
committerKarel Zak <kzak@redhat.com>
Tue, 17 May 2011 13:58:05 +0000 (15:58 +0200)
Separate function for escape handling to make switch statement
more readable.

Signed-off-by: Sami Kerola <kerolasa@iki.fi>
text-utils/ul.c

index 9536c18be6eadb5e4265897e76fc2d2402139537..42a2ee279d11d16ebc9d3f8c00ee2e8f2fc2946b 100644 (file)
@@ -70,6 +70,7 @@ static int put1wc(int c)
 #endif
 
 static void usage(FILE *out);
+static int handle_escape(FILE * f);
 static void filter(FILE *f);
 static void flushln(void);
 static void overstrike(void);
@@ -235,6 +236,45 @@ int main(int argc, char **argv)
        return EXIT_SUCCESS;
 }
 
+static int handle_escape(FILE * f)
+{
+       wint_t c;
+
+       switch (c = getwc(f)) {
+       case HREV:
+               if (halfpos == 0) {
+                       mode |= SUPERSC;
+                       halfpos--;
+               } else if (halfpos > 0) {
+                       mode &= ~SUBSC;
+                       halfpos--;
+               } else {
+                       halfpos = 0;
+                       reverse();
+               }
+               return 0;
+       case HFWD:
+               if (halfpos == 0) {
+                       mode |= SUBSC;
+                       halfpos++;
+               } else if (halfpos < 0) {
+                       mode &= ~SUPERSC;
+                       halfpos++;
+               } else {
+                       halfpos = 0;
+                       fwd();
+               }
+               return 0;
+       case FREV:
+               reverse();
+               return 0;
+       default:
+               /* unknown escape */
+               ungetwc(c, f);
+               return 1;
+       }
+}
+
 static void filter(FILE *f)
 {
        wint_t c;
@@ -264,43 +304,11 @@ static void filter(FILE *f)
                continue;
 
        case IESC:
-               switch (c = getwc(f)) {
-
-               case HREV:
-                       if (halfpos == 0) {
-                               mode |= SUPERSC;
-                               halfpos--;
-                       } else if (halfpos > 0) {
-                               mode &= ~SUBSC;
-                               halfpos--;
-                       } else {
-                               halfpos = 0;
-                               reverse();
-                       }
-                       continue;
-
-               case HFWD:
-                       if (halfpos == 0) {
-                               mode |= SUBSC;
-                               halfpos++;
-                       } else if (halfpos < 0) {
-                               mode &= ~SUPERSC;
-                               halfpos++;
-                       } else {
-                               halfpos = 0;
-                               fwd();
-                       }
-                       continue;
-
-               case FREV:
-                       reverse();
-                       continue;
-
-               default:
+               if(handle_escape(f)) {
+                       c = getwc(f);
                        errx(EXIT_FAILURE,
                                _("unknown escape sequence in input: %o, %o"),
                                IESC, c);
-                       break;
                }
                continue;