What: The ability to read a configuration file as stdout from an executable

Why: The configuration files (particularly for the Director) are very
        complex. In my case I find it easier to have a program generate them
        from meta-information about the clients.

Notes:

The attached patch implements this. It's a simple change to lex.c, and
functions globally: if the first character of any configuration file's
name is a pipe ("|"), then the rest of the "filename" is considered to
be the path to an executable. The program is run, stdout is read, and
the output is the configuration file.

For example, starting the director like this:

        # bacula-dir -c '|/usr/local/sbin/generate-dir-config'

... would cause it to run /usr/local/sbin/generate-dir-config to
generate a new configuration file.

Comments welcome.

-- Jorj
diff --recursive -u bacula-2.0.3/src/lib/lex.c 
bacula-2.0.3.patched/src/lib/lex.c
--- bacula-2.0.3/src/lib/lex.c  2006-11-21 11:13:58.000000000 -0500
+++ bacula-2.0.3.patched/src/lib/lex.c  2007-03-08 09:54:40.000000000 -0500
@@ -37,6 +37,12 @@
 #include "bacula.h"
 #include "lex.h"
 
+#if defined(HAVE_WIN32)
+#define POPEN_MODE "rb"
+#else
+#define POPEN_MODE "r"
+#endif
+
 extern int debug_level;
 
 /* Debug level for this source file */
@@ -133,7 +139,11 @@
    Dmsg1(dbglvl, "Close lex file: %s\n", lf->fname);
 
    of = lf->next;
-   fclose(lf->fd);
+
+   if (lf->is_pipe)
+     pclose(lf->fd);
+   else
+     fclose(lf->fd);
    Dmsg1(dbglvl, "Close cfg file %s\n", lf->fname);
    free(lf->fname);
    if (of) {
@@ -159,6 +169,7 @@
  * the next field.
  *
  */
+
 LEX *lex_open_file(LEX *lf, const char *filename, LEX_ERROR_HANDLER 
*scan_error)
 
 {
@@ -166,9 +177,14 @@
    FILE *fd;
    char *fname = bstrdup(filename);
 
-
-   if ((fd = fopen(fname, "rb")) == NULL) {
-      return NULL;
+   if (fname && fname[0] == '|') {
+      if ((fd = popen(&fname[1], POPEN_MODE)) == NULL) {
+        return NULL;
+      }
+   } else {
+      if ((fd = fopen(fname, "rb")) == NULL) {
+        return NULL;
+      }
    }
    Dmsg1(400, "Open config file: %s\n", fname);
    nf = (LEX *)malloc(sizeof(LEX));
@@ -188,6 +204,7 @@
       lex_set_default_error_handler(lf);
    }
    lf->fd = fd;
+   lf->is_pipe = (fname[0] == '|');
    lf->fname = fname;
    lf->state = lex_none;
    lf->ch = L_EOL;
diff --recursive -u bacula-2.0.3/src/lib/lex.h 
bacula-2.0.3.patched/src/lib/lex.h
--- bacula-2.0.3/src/lib/lex.h  2006-11-21 08:20:11.000000000 -0500
+++ bacula-2.0.3.patched/src/lib/lex.h  2007-03-08 09:33:41.000000000 -0500
@@ -98,6 +98,7 @@
    int options;                       /* scan options */
    char *fname;                       /* filename */
    FILE *fd;                          /* file descriptor */
+   int is_pipe : 1;                   /* did we use popen to open it? */
    char line[MAXSTRING];              /* input line */
    char str[MAXSTRING];               /* string being scanned */
    int str_len;                       /* length of string */

Attachment: pgps7VkU1BjqE.pgp
Description: PGP signature

-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys-and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
_______________________________________________
Bacula-users mailing list
Bacula-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/bacula-users

Reply via email to