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

[or-cvs] Added poptReadOptions() and poptReadDefaultOptions()



Update of /home/or/cvsroot/src/common
In directory moria.seul.org:/tmp/cvs-serv21936/src/common

Modified Files:
	config.c 
Log Message:
Added poptReadOptions() and poptReadDefaultOptions()


Index: config.c
===================================================================
RCS file: /home/or/cvsroot/src/common/config.c,v
retrieving revision 1.1.1.1
retrieving revision 1.2
diff -u -d -r1.1.1.1 -r1.2
--- config.c	26 Jun 2002 22:45:50 -0000	1.1.1.1
+++ config.c	28 Jun 2002 18:14:55 -0000	1.2
@@ -8,6 +8,9 @@
 /*
  * Changes :
  * $Log$
+ * Revision 1.2  2002/06/28 18:14:55  montrose
+ * Added poptReadOptions() and poptReadDefaultOptions()
+ *
  * Revision 1.1.1.1  2002/06/26 22:45:50  arma
  * initial commit: current code
  *
@@ -40,6 +43,8 @@
 #include <string.h>
 #include <errno.h>
 #include <ctype.h>
+#include <popt.h>
+#include <limits.h>
 
 #include "config.h"
 #include "log.h"
@@ -345,3 +350,58 @@
   
   return retval;
 }
+
+int poptReadOptions(poptContext optCon, const unsigned char *fname)
+/**
+poptReadOptions reads popt-style options from the specified filename.
+RETURN VALUE: INT_MIN = problem opening config file, else standard poptGetNextOpt() return value
+**/
+{
+   FILE *fp;
+   int argc, c;
+   char **argv;
+   char line[256];
+   line[0] = line[1] = '-';  /* prepend expected long name option flag */
+   fp = open_config(fname);
+   if ( fp == NULL ) return INT_MIN;
+   c = 0;
+   /**
+   this loop skips over all leading whitespace and blank lines then returns all text
+   from that point to the next newline.
+   **/
+   while ( c >= -1 && fscanf(fp,"%*[ \n]%[^\n]",&line[2]) == 1 )
+   {
+      switch ( line[2] )
+      {
+      case '#':   /* comments begin with this */
+      case '[':   /* section header. ignore for now. maybe do something special in future version... */
+         continue;/* ignore */
+      default:    /* we got a bite, lets reel it in now */
+         poptParseArgvString(line,&argc,(const char ***)&argv); /* Argv-ify what we found */
+         poptStuffArgs(optCon,(const char **)argv);   /* stuff new arguments so they can be interpreted */
+         free(argv);                                  /* free storage allocated by poptParseArgvString */
+         c = poptGetNextOpt(optCon);                  /* interpret option read from config file */
+      }
+   }
+   close_config(fp);
+   return c;
+}
+
+int poptReadDefaultOptions(const char *cmd, poptContext optCon)
+/**
+reads popt-style options from /etc/<cmd>rc and ~/.<cmd>rc
+RETURN VALUE: same as poptReadOptions()
+**/
+{
+   char fname[256];
+   int c;
+   sprintf(fname,"/etc/%src",cmd);
+   c = poptReadOptions(optCon,fname);
+   if ( c == INT_MIN || c >= -1 )
+   {
+      sprintf(fname,"~/.%src",cmd);
+      c = poptReadOptions(optCon,fname);
+   }
+   return c;
+}
+