Hi Karl,

On Saturday 09 July 2005 05:06 pm, Karl Berry wrote:
> Hi Bruce,
> 
>     configury magic needed to actually make it work.  (details....;)
> 
> It would be very helpful if you could make a stab at defining that
> configury magic :).  At least, it's not immediately obvious to me where
> to start.

But I _hate_ autoconf.  :-)  Attached anyway.

> Also, I'd like to request a simpler change.  I don't see any purpose in
> allowing the options like --version and --help to be set in the
> configuration file.

I don't either, but it was easier than worrying about it.  The
attached patch simply ignores them.  With a little effort, it
could not look for them, too, but I leave that as an exercise for
the reader.  :)

> All that really needs to be allowed is a single 
> setting in the config file, say "greeting", which overrides the "hello,
> world" text.  Ok?

How about, "greeting" overrides just the "hello" and not the "greeted"
(world) part?

> On the other hand, I guess that might actually make it worse, since part
> of the point here is to reuse the longopts structure, right?

Naw.  I just needed to fix the loop-over-the-longopts thing to ignore
-h and -v.

> I don't 
> like duplicating that switch statement to handle all the options when
> they are not really useful.  Hmm.  I'm not sure what is best.

Two things:

1) this is only an example showing how it works.  It is not meant to
   be saying, "you have to use your longopts list."  You could, of course,
   set a counter to the value:  (sizeof(longopts)/sizeof(longopts[0]))-2
   and stop when it reaches zero.  Do you want to bother?

2) You otta be using AutoOpts anyway.  "help" and "version" are automatically
   supported and marked as not "presettable"  viz.
     export AUTOGEN_HELP=yes
   will have no effect, though:
     export AUTOGEN_TIMEOUT=180
   will work as expected.
   hello.c does not do environment variables (yet? ;)

> I've added a --greeting (-g) option to allow it from the command line.

I've tweaked it to only fiddle with the "hello" part and leave "world" alone.

Cheers - Bruce
Index: configure.ac
===================================================================
RCS file: /cvsroot/hello/hello/configure.ac,v
retrieving revision 1.5
diff -b -B -u -p -r1.5 configure.ac
--- configure.ac	28 May 2005 22:19:27 -0000	1.5
+++ configure.ac	10 Jul 2005 03:36:46 -0000
@@ -44,10 +44,25 @@ AM_MISSING_PROG(HELP2MAN, help2man)
 
 dnl Checks for header files.
 AC_HEADER_STDC([])
-AC_CHECK_HEADERS([string.h fcntl.h sys/file.h sys/param.h])
+AC_CHECK_HEADERS([string.h fcntl.h sys/file.h sys/param.h autoopts/options.h])
 
 dnl Checks for functions.
 AC_FUNC_ALLOCA
+
+[if test "X${ac_cv_header_autoopts_options_h}" == Xno
+then
+  :
+else
+  f=`autoopts-config cflags` 2>/dev/null
+  if test X"${f}" = X
+  then
+    :
+  else]
+    AC_DEFINE([HAVE_LIBOPTS],[1],[define if we can find libopts])
+    [CFLAGS="${CFLAGS} ${f}"
+    LIBS="${LIBS} `autoopts-config ldflags`"
+  fi
+fi]
 
 # i18n support from GNU gettext.
 AM_GNU_GETTEXT_VERSION([0.14.5])
Index: src/hello.c
===================================================================
RCS file: /cvsroot/hello/hello/src/hello.c,v
retrieving revision 1.7
diff -b -B -u -p -r1.7 hello.c
--- src/hello.c	9 Jul 2005 23:23:23 -0000	1.7
+++ src/hello.c	10 Jul 2005 03:36:47 -0000
@@ -79,6 +79,10 @@ extern char *alloca ();
 #endif
 #include "system.h"
 
+#ifdef HAVE_LIBOPTS
+# include <autoopts/options.h>
+#endif
+
 struct option longopts[] =
 {
   { "greeting", required_argument, NULL, 'g' },
@@ -102,6 +106,7 @@ main (argc, argv)
   int optc;
   int h = 0, v = 0, t = 0, m = 0, n = 0, lose = 0, z = 0;
   char *greeting = NULL;
+  char *greeted = NULL;
 
   progname = argv[0];
 
@@ -116,6 +121,43 @@ main (argc, argv)
   textdomain (PACKAGE);
 #endif
 
+#ifdef HAVE_LIBOPTS
+  {
+    const tOptionValue *ov_p = configFileLoad("hello.conf");
+
+    if (ov_p != NULL) {
+      struct option* lo_p = longopts;
+      while (lo_p->name != NULL) {
+	tOptionValue *opt_p = optionGetValue(ov_p, lo_p->name);
+	if (opt_p != NULL) switch (lo_p->val) {
+	  {
+	  case 'v':
+	  case 'h':
+	    break;
+	  case 'g':
+	    if (opt_p->valType != OPARG_TYPE_STRING)
+	      continue;
+	    greeting = strdup(opt_p->v.strVal);
+	    break;
+	  case 'm':
+	    m = 2;
+	    break;
+	  case 'n':
+	    n = 2;
+	    break;
+	  case 't':
+	    t = 2;
+	    break;
+	  }
+	}
+	lo_p++;
+      }
+
+      optionUnloadNested(ov_p);
+    }
+  }
+#endif
+
   while ((optc = getopt_long (argc, argv, "g:hmntv", longopts, (int *) 0))
          != EOF)
     switch (optc)
@@ -130,12 +172,14 @@ main (argc, argv)
         h = 1;
         break;
       case 'm':
+	if (t == 2) t = 0;
         m = 1;
         break;
       case 'n':
         n = 1;
         break;
       case 't':
+	if (m == 2) m = 0;
         t = 1;
         break;
       default:
@@ -212,11 +256,29 @@ For more information about these matters
               "2005", PACKAGE);
       exit (0);
     }
-  if (m && t)
+  if (m && t) do
+    {
+#ifdef HAVE_LIBOPTS
+      /*  If the conflict is due to presetting, then override the preset
+       *  by the command line setting.  */
+      if (m == 2)
+	{
+	  if (t != 2)
+	    {
+	      m = 0;
+	      break;
+	    }
+	}
+      else if (t == 2)
     {
+	  t = 0;
+	  break;
+	}
+#endif
       fprintf (stderr, _("%s: Incompatible flags: -m and -t\n"), progname);
       exit (1);
     }
+  while (0);
   
   if (m)
     {
@@ -333,9 +395,10 @@ For more information about these matters
 "));
       else
         {
+	  char* fmt = _("%s, world!\n");
           if (!greeting)
-            greeting = _("Hello, world!");
-          puts (greeting);
+            greeting = _("Hello");
+          printf (fmt, greeting);
         }
     }
 
_______________________________________________
http://lists.gnu.org/mailman/listinfo/bug-hello

Reply via email to