[PATCH 7/8] ul.c: escape handling refactored

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Separate function for escape handling to make switch statement
more readable.

Signed-off-by: Sami Kerola <kerolasa@xxxxxx>
---
 text-utils/ul.c |   76 ++++++++++++++++++++++++++++++------------------------
 1 files changed, 42 insertions(+), 34 deletions(-)

diff --git a/text-utils/ul.c b/text-utils/ul.c
index 683acdb..04da64e 100644
--- a/text-utils/ul.c
+++ b/text-utils/ul.c
@@ -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;
 
-- 
1.7.5

--
To unsubscribe from this list: send the line "unsubscribe util-linux" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[Index of Archives]     [Netdev]     [Ethernet Bridging]     [Linux Wireless]     [Kernel Newbies]     [Security]     [Linux for Hams]     [Netfilter]     [Bugtraq]     [Yosemite News]     [MIPS Linux]     [ARM Linux]     [Linux RAID]     [Linux Admin]     [Samba]

  Powered by Linux