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


gnu-hello       
Info
Commit...:c27f3e682a9b82d7384d8f525679998285d21182
Author...:karl <>
Committer:karl <>
Date.....:Wed Oct 18 22:36:48 2006 +0000
Parents..:fb7e4be123125590385a37485c8cc14b8683cca5

Message
[project @ 2006-10-18 22:36:48 by karl]
split out help/version to separate routines.

Changes
diff --git a/src/hello.c b/src/hello.c
line changes: +102/-74
index 9eae0e0..62f9ba9
--- a/src/hello.c
+++ b/src/hello.c
@@ -33,11 +33,15 @@ static const struct option longopts[] =
   { NULL, 0, NULL, 0 }
 };
 
+static void my_exit (void);
+static void print_help (void);
+static void print_version (void);
+
 int
 main (int argc, char *argv[])
 {
   int optc;
-  int h = 0, v = 0, t = 0, n = 0, lose = 0;
+  int t = 0, n = 0, lose = 0;
   char *greeting = NULL;
 
   program_name = argv[0];
@@ -51,17 +55,20 @@ main (int argc, char *argv[])
   textdomain (PACKAGE);
 #endif
 
-  while ((optc = getopt_long (argc, argv, "g:hntv", longopts, NULL) != -1)
+  while ((optc = getopt_long (argc, argv, "g:hntv", longopts, NULL)) != -1)
     switch (optc)
       {
+      /* One goal here is having --help and --version exit immediately.  */
       case 'v':
-        v = 1;
+        print_version ();
+        my_exit ();
         break;
       case 'g':
         greeting = optarg;
         break;
       case 'h':
-        h = 1;
+        print_help ();
+        my_exit ();
         break;
       case 'n':
         n = 1;
@@ -84,90 +91,111 @@ main (int argc, char *argv[])
       exit (1);
     }
 
-  /* If `help' or `version' is requested, ignore the other options. */
-  if (h)
+  /* Print greeting message and exit. */
+  if (t)
+    printf (_("hello, world\n"));
+
+  else if (n)
+    /* TRANSLATORS: Use box drawing characters or other fancy stuff
+       if your encoding (e.g., UTF-8) allows it.  If done so add the
+       following note, please:
+
+       [Note: For best viewing results use a UTF-8 locale, please.]
+    */
+	printf (_("\
++---------------+\n\
+| Hello, world! |\n\
++---------------+\n\
+"));
+
+  else
     {
-      /* Print help info and exit.  This long message is split into
-         several pieces to help translators be able to align different
-         blocks and identify the various pieces.  */
-
-      /* TRANSLATORS: --help output 1
-         no-wrap */
-      fputs (_("\
-GNU hello, THE greeting printing program.\n"), stdout);
-      printf ("\n");
-      /* TRANSLATORS: --help output 2
-         no-wrap */
-      printf (_("\
+      if (!greeting)
+        greeting = _("Hello, world!");
+      puts (greeting);
+    }
+  
+  my_exit ();
+}
+
+
+
+/* Even exiting has subtleties.  The /dev/full device on GNU/Linux can
+   be used for testing whether writes are checked properly.  For instance,
+   hello >/dev/null should exit unsuccessfully.  */
+   
+static void
+my_exit (void)
+{
+  /* Exit unsuccessfully if the write failed.  This is implemented in
+     the Gnulib module "closeout".  */
+  close_stdout ();
+
+  /* Otherwise, exit successfully.  */
+  exit (0);
+}
+
+
+
+/* Print help info.  This long message is split into
+   several pieces to help translators be able to align different
+   blocks and identify the various pieces.  */
+
+static void
+print_help (void)
+{
+  /* TRANSLATORS: --help output 1 (synopsis)
+     no-wrap */
+        printf (_("\
 Usage: %s [OPTION]...\n"), program_name);
 
-      printf ("\n");
-      /* TRANSLATORS: --help output 3 : options 1/2
-         no-wrap */
-      fputs (_("\
+  /* TRANSLATORS: --help output 2 (brief description)
+     no-wrap */
+  fputs (_("\
+Print a friendly, customizable greeting.\n"), stdout);
+
+  puts ("");
+  /* TRANSLATORS: --help output 3: options 1/2
+     no-wrap */
+  fputs (_("\
   -h, --help          display this help and exit\n\
   -v, --version       display version information and exit\n"), stdout);
 
-      printf ("\n");
-      /* TRANSLATORS: --help output 4 : options 2/2
-         no-wrap */
-      fputs (_("\
+  puts ("");
+  /* TRANSLATORS: --help output 4: options 2/2
+     no-wrap */
+  fputs (_("\
   -t, --traditional       use traditional greeting format\n\
   -n, --next-generation   use next-generation greeting format\n\
   -g, --greeting=TEXT     use TEXT as the greeting message\n"), stdout);
 
-      printf ("\n");
-      /* TRANSLATORS: --help output 5 (end)
-         TRANSLATORS: the placeholder indicates the bug-reporting address
-         for this application. Please add _another line_ with the
-         address for translation bugs.
-         no-wrap */
-      printf (_("\
+  printf ("\n");
+  /* TRANSLATORS: --help output 5 (end)
+     TRANSLATORS: the placeholder indicates the bug-reporting address
+     for this application.  Please add _another line_ with the
+     address for translation bugs.
+     no-wrap */
+  printf (_("\
 Report bugs to <%s>.\n"), PACKAGE_BUGREPORT);
-    }
+}
 
-  else if (v)
-    {
-      /* Print version number.  */
-      printf ("hello (GNU %s) %s\n", PACKAGE, VERSION);
-      /* xgettext: no-wrap */
-      puts ("");
-      printf (_("\
+
+
+/* Print version and copyright information.  */
+
+static void
+print_version (void)
+{
+  printf ("hello (GNU %s) %s\n", PACKAGE, VERSION);
+  /* xgettext: no-wrap */
+  puts ("");
+  
+  /* It is important to separate the year from the rest of the message,
+     as done here, to avoid having to retranslate the message when a new
+     year comes around.  */
+  printf (_("\
 Copyright (C) %s Free Software Foundation, Inc.\n\
 License: GNU GPL v2+ <http://www.gnu.org/licenses/gpl.html>\n\
 This is free software.  There is NO WARRANTY, to the extent permitted by law.\n"),
               "2006");
-    }
-
-  else
-    /* Print greeting message and exit. */
-    {
-      if (t)
-        printf (_("hello, world\n"));
-      else if (n)
-	/* TRANSLATORS: Use box drawing characters or other fancy stuff
-	   if your encoding (e.g., UTF-8) allows it.  If done so add the
-	   following note, please:
-
-	   [Note: For best viewing results use a UTF-8 locale, please.]
-	*/
-	printf (_("\
-+---------------+\n\
-| Hello, world! |\n\
-+---------------+\n\
-"));
-      else
-        {
-          if (!greeting)
-            greeting = _("Hello, world!");
-          puts (greeting);
-        }
-    }
-
-  /* Exit unsuccessfully if the write failed.  (This is implemented in
-     the Gnulib module "closeout".  */
-  close_stdout ();
-
-  /* Otherwise, exit successfully.  */
-  exit (0);
 }

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