util-linux.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Sami Kerola <kerolasa@iki.fi>
To: util-linux@vger.kernel.org
Cc: kerolasa@iki.fi
Subject: [PATCH 04/14] pg: refactor argument handing
Date: Mon,  8 Oct 2012 08:08:10 +0100	[thread overview]
Message-ID: <1349680100-18120-5-git-send-email-kerolasa@iki.fi> (raw)
In-Reply-To: <1349680100-18120-1-git-send-email-kerolasa@iki.fi>

Add function parse_arguments(), which has the same code block that was in
over long, and too deeply intended, main().

Signed-off-by: Sami Kerola <kerolasa@iki.fi>
---
 text-utils/pg.c | 159 ++++++++++++++++++++++++++++++--------------------------
 1 file changed, 84 insertions(+), 75 deletions(-)

diff --git a/text-utils/pg.c b/text-utils/pg.c
index c4a86ca..f9b6ac8 100644
--- a/text-utils/pg.c
+++ b/text-utils/pg.c
@@ -1581,12 +1581,91 @@ found_bw:
 		fclose(fbuf);
 }
 
+static int
+parse_arguments(int arg, int argc, char **argv)
+{
+	FILE *input;
+
+	files.first = arg;
+	files.last = arg + argc - 1;
+	for (; argv[arg]; arg += nextfile) {
+		nextfile = 1;
+		files.current = arg;
+		if (argc > 2) {
+			static int firsttime;
+			firsttime++;
+			if (firsttime > 1) {
+				mesg(_("(Next file: "));
+				mesg(argv[arg]);
+				mesg(")");
+ newfile:
+				if (ontty) {
+					prompt(-1);
+					switch (cmd.key) {
+					case 'n':
+						/*
+						 * Next file.
+						 */
+						if (cmd.count == 0)
+							cmd.count = 1;
+						nextfile = cmd.count;
+						if (checkf()) {
+							nextfile = 1;
+							mesg(":");
+							goto newfile;
+						}
+						continue;
+					case 'p':
+						/*
+						 * Previous file.
+						 */
+						if (cmd.count == 0)
+							cmd.count = 1;
+						nextfile = 0 - cmd.count;
+						if (checkf()) {
+							nextfile = 1;
+							mesg(":");
+							goto newfile;
+						}
+						continue;
+					case 'q':
+					case 'Q':
+						quit(exitstatus);
+					}
+				} else
+					mesg("\n");
+			}
+		}
+		if (strcmp(argv[arg], "-") == 0)
+			input = stdin;
+		else {
+			input = fopen(argv[arg], "r");
+			if (input == NULL) {
+				pgerror(errno, argv[arg]);
+				exitstatus++;
+				continue;
+			}
+		}
+		if (ontty == 0 && argc > 2) {
+			/*
+			 * Use the prefix as specified by SUSv2.
+			 */
+			write_all(1, "::::::::::::::\n", 15);
+			write_all(1, argv[arg], strlen(argv[arg]));
+			write_all(1, "\n::::::::::::::\n", 16);
+		}
+		pgfile(input, argv[arg]);
+		if (input != stdin)
+			fclose(input);
+	}
+	return exitstatus;
+}
+
 int
 main(int argc, char **argv)
 {
 	int arg, i;
 	char *p;
-	FILE *input;
 
 	progname = basename(argv[0]);
 	xasprintf(&copyright,
@@ -1703,81 +1782,11 @@ endargs:
 			invopt(argv[arg]);
 		}
 	}
-	if (argc == 1) {
+	if (argc == 1)
 		pgfile(stdin, "stdin");
-	} else {
-		files.first = arg;
-		files.last = arg + argc - 1;
-		for ( ; argv[arg]; arg += nextfile) {
-			nextfile = 1;
-			files.current = arg;
-			if (argc > 2) {
-				static int firsttime;
-				firsttime++;
-				if (firsttime > 1) {
-					mesg(_("(Next file: "));
-					mesg(argv[arg]);
-					mesg(")");
-newfile:
-					if (ontty) {
-					prompt(-1);
-					switch(cmd.key) {
-					case 'n':
-						/*
-					 	* Next file.
-					 	*/
-						if (cmd.count == 0)
-							cmd.count = 1;
-						nextfile = cmd.count;
-						if (checkf()) {
-							nextfile = 1;
-							mesg(":");
-							goto newfile;
-						}
-						continue;
-					case 'p':
-						/*
-					 	* Previous file.
-					 	*/
-						if (cmd.count == 0)
-							cmd.count = 1;
-						nextfile = 0 - cmd.count;
-						if (checkf()) {
-							nextfile = 1;
-							mesg(":");
-							goto newfile;
-						}
-						continue;
-					case 'q':
-					case 'Q':
-						quit(exitstatus);
-				}
-				} else mesg("\n");
-				}
-			}
-			if (strcmp(argv[arg], "-") == 0)
-				input = stdin;
-			else {
-				input = fopen(argv[arg], "r");
-				if (input == NULL) {
-					pgerror(errno, argv[arg]);
-					exitstatus++;
-					continue;
-				}
-			}
-			if (ontty == 0 && argc > 2) {
-				/*
-				 * Use the prefix as specified by SUSv2.
-				 */
-				write_all(1, "::::::::::::::\n", 15);
-				write_all(1, argv[arg], strlen(argv[arg]));
-				write_all(1, "\n::::::::::::::\n", 16);
-			}
-			pgfile(input, argv[arg]);
-			if (input != stdin)
-				fclose(input);
-		}
-	}
+	else
+		exitstatus = parse_arguments(arg, argc, argv);
+
 	quit(exitstatus);
 	/*NOTREACHED*/
 	return 0;
-- 
1.7.12.2


  parent reply	other threads:[~2012-10-08  7:08 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-10-08  7:08 [PATCH 00/14] various: clean ups and fixes Sami Kerola
2012-10-08  7:08 ` [PATCH 01/14] ddate: remove from util-linux Sami Kerola
2012-10-08  9:06   ` Petr Uzel
2012-10-08  7:08 ` [PATCH 02/14] column: add --output-separator option Sami Kerola
2012-10-08  7:08 ` [PATCH 03/14] docs: fix column.1 groff syntax error Sami Kerola
2012-10-08  7:08 ` Sami Kerola [this message]
2012-10-08  7:08 ` [PATCH 05/14] pg: use libc error printing facilities Sami Kerola
2012-10-08  7:08 ` [PATCH 06/14] pg: add const qualifiers where suitable Sami Kerola
2012-10-08  7:08 ` [PATCH 07/14] pg: add noreturn function attributes Sami Kerola
2012-10-08  7:08 ` [PATCH 08/14] pg: use unistd.h STDOUT_FILENO Sami Kerola
2012-10-08  7:08 ` [PATCH 09/14] pg: do not turn off warnigns artificially Sami Kerola
2012-10-08  7:08 ` [PATCH 10/14] pg: fix coding style Sami Kerola
2012-10-08  7:08 ` [PATCH 11/14] more: " Sami Kerola
2012-10-08  7:08 ` [PATCH 12/14] more: align void in functions with prototypes, and remove void casts Sami Kerola
2012-10-08  7:08 ` [PATCH 13/14] more: remove few memory leaks Sami Kerola
2012-10-08  7:08 ` [PATCH 14/14] swapon: remove loop declaration [smatch scan] Sami Kerola

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1349680100-18120-5-git-send-email-kerolasa@iki.fi \
    --to=kerolasa@iki.fi \
    --cc=util-linux@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).