Project Info Name.......: gnu-hello
Description: GNU Hello


gnu-hello       
Info
Commit...:c0c3f2053fcb85377cd0b8923cd69a03e0a4336c
Author...:Sami Kerola
Committer:Sami Kerola
Date.....:Sun Jan 20 21:57:36 2019 +0000
Parents..:d6949b544656aee5bc3b96ef5c95ea35a2dc8d88

Message
hello: move option parsing to function

* src/hello.c: This makes main() easier to read.

Changes
diff --git a/src/hello.c b/src/hello.c
line changes: +35/-29
index 6c44f8b..1791133
--- a/src/hello.c
+++ b/src/hello.c
@@ -79,16 +79,11 @@ print_help (FILE *restrict out)
   exit(out == stderr ? EXIT_FAILURE : EXIT_SUCCESS);
 }
 
-int
-main (int argc, char *argv[])
+void
+parse_options (int argc, char *argv[], const char **greeting_msg)
 {
   int optc;
   int lose = 0;
-  const char *greeting_msg;
-  wchar_t *mb_greeting;
-  mbstate_t mbstate = { 0, };
-  size_t len;
-
   enum {
     OPT_HELP = CHAR_MAX + 1,
     OPT_VERSION
@@ -101,26 +96,6 @@ main (int argc, char *argv[])
     {NULL, 0, NULL, 0}
   };
 
-  set_program_name (argv[0]);
-
-  /* Set locale via LC_ALL.  */
-  setlocale (LC_ALL, "");
-
-#if ENABLE_NLS
-  /* Set the text message domain.  */
-  bindtextdomain (PACKAGE, LOCALEDIR);
-  textdomain (PACKAGE);
-#endif
-
-  /* Having initialized gettext, get the default message. */
-  greeting_msg = _("Hello, world!");
-
-  /* Even exiting has subtleties.  On exit, if any writes failed, change
-     the exit status.  The /dev/full device on GNU/Linux can be used for
-     testing; for instance, hello >/dev/full should exit unsuccessfully.
-     This is implemented in the Gnulib module "closeout".  */
-  atexit (close_stdout);
-
   while ((optc = getopt_long (argc, argv, "g:t", longopts, NULL)) != -1)
     switch (optc)
       {
@@ -130,12 +105,12 @@ main (int argc, char *argv[])
 	exit (EXIT_SUCCESS);
 	break;
       case 'g':
-	greeting_msg = optarg;
+	*greeting_msg = optarg;
 	break;
       case OPT_HELP:
 	print_help (stdout);
       case 't':
-	greeting_msg = _("hello, world");
+	*greeting_msg = _("hello, world");
 	break;
       default:
 	lose = 1;
@@ -150,6 +125,37 @@ main (int argc, char *argv[])
       emit_try_help ();
       exit (EXIT_FAILURE);
     }
+}
+
+int
+main (int argc, char *argv[])
+{
+  const char *greeting_msg;
+  wchar_t *mb_greeting;
+  mbstate_t mbstate = { 0, };
+  size_t len;
+
+  set_program_name (argv[0]);
+
+  /* Set locale via LC_ALL.  */
+  setlocale (LC_ALL, "");
+
+#if ENABLE_NLS
+  /* Set the text message domain.  */
+  bindtextdomain (PACKAGE, LOCALEDIR);
+  textdomain (PACKAGE);
+#endif
+
+  /* Having initialized gettext, get the default message. */
+  greeting_msg = _("Hello, world!");
+
+  /* Even exiting has subtleties.  On exit, if any writes failed, change
+     the exit status.  The /dev/full device on GNU/Linux can be used for
+     testing; for instance, hello >/dev/full should exit unsuccessfully.
+     This is implemented in the Gnulib module "closeout".  */
+  atexit (close_stdout);
+
+  parse_options(argc, argv, &greeting_msg);
 
   len = strlen(greeting_msg) + 1;
   mb_greeting = xmalloc(len * sizeof(wchar_t));

This website is licensed under AGPL-3.0. Feel free to copy!