Subversion Repositories

?revision_form?Rev ?revision_input??revision_submit??revision_endform?

Rev 2 | Blame | Compare with Previous | Last modification | View Log | RSS feed

#! /bin/sh /usr/share/dpatch/dpatch-run
## 25_pmksetup.dpatch by Magnus Holmgren <magnus@kibibyte.se>
##
## DP: Adds -s parameter to pmksetup, which outputs config to standard
## DP: output, as well as -g, which forces data gathering to take place
## DP: immediately.

@DPATCH@
diff -urNad trunk~/pmksetup.c trunk/pmksetup.c
--- trunk~/pmksetup.c   2006-10-15 00:31:11.000000000 +0200
+++ trunk/pmksetup.c    2007-08-05 15:17:25.000000000 +0200
@@ -66,6 +66,7 @@
 extern int      optind;
 
 FILE           *sfp;                   /* scratch file pointer */
+bool            tostdout = false;
 char            sfn[MAXPATHLEN];       /* scratch file name */
 
 htable         *ht;
@@ -154,7 +155,7 @@
  ***********************************************************************/
 
 bool gather_data(htable *pht) {
-       printf("==> Looking for default parameters...\n");
+       fprintf(stderr, "==> Looking for default parameters...\n");
 
        /* gather env variables */
        if (get_env_vars(pht) == false) {
@@ -850,7 +851,7 @@
                vsnprintf(buf, sizeof(buf), fmt, plst);
                va_end(plst);
 
-               printf("%s\n", buf);
+               fprintf(stderr, "%s\n", buf);
        }
 }
 
@@ -883,6 +884,13 @@
        optind = 1;
        while ((ch = getopt(argc, argv, PMKSTP_OPT_STR)) != -1) {
                switch(ch) {
+                       case 'g' :
+                               if (gather_data(ht) == false)
+                                       return(false);
+
+                               process_clopts = true;
+                               break;
+
                        case 'r' :
                                /* mark to be deleted in hash */
                                if (record_data(ht, optarg, PMKSTP_REC_REMV, NULL) == false) {
@@ -932,6 +940,7 @@
                                process_clopts = true;
                                break;
 
+                       case 's' :
                        case 'v' :
                        case 'V' :
                                /*
@@ -949,11 +958,11 @@
        argc -= optind;
        argv += optind;
 
-       printf("PMKSETUP version %s", PREMAKE_VERSION);
+       fprintf(stderr, "PMKSETUP version %s", PREMAKE_VERSION);
 #ifdef DEBUG
-       printf(" [SUB #%s] [SNAP #%s]", PREMAKE_SUBVER_PMKSETUP, PREMAKE_SNAP);
+       fprintf(stderr, " [SUB #%s] [SNAP #%s]", PREMAKE_SUBVER_PMKSETUP, PREMAKE_SNAP);
 #endif /* DEBUG */
-       printf("\n\n");
+       fprintf(stderr, "\n\n");
 
        if (process_clopts == false) {
                /* standard behavior, gathering data */
@@ -967,7 +976,7 @@
                /* switch backup flag */
                cfg_backup = true;
 
-               printf("==> Configuration file found: %s\n",
+               fprintf(stderr, "==> Configuration file found: %s\n",
                                                PREMAKE_CONFIG_PATH);
                if (parse_pmkconf(config, ht, PRS_PMKCONF_SEP,
                                                check_opt) == false) {
@@ -978,10 +987,10 @@
                        fclose(config);
                }
        } else {
-               printf("==> Configuration file not found.\n");
+               fprintf(stderr, "==> Configuration file not found.\n");
        }
 
-       printf("==> Merging remaining data...\n");
+       fprintf(stderr, "==> Merging remaining data...\n");
        /* writing the remaining data stored in the hash */
        if (write_new_data(ht) == false)
                return(false);
@@ -1099,6 +1108,9 @@
        /* if child status is ok, writing changes */
        if (status == 0) {
 #endif
+               if (tostdout) {
+                       return;
+               }
                /*
                 * check if pmk.conf already exists
                 * NOTE: no race condition here for access(), BUT
@@ -1106,7 +1118,7 @@
                 */
                if (access(PREMAKE_CONFIG_PATH, F_OK) == 0) { /* see above */
                        /* backup configuration file */
-                       printf("==> Backing up configuration file: %s\n",
+                       fprintf(stderr, "==> Backing up configuration file: %s\n",
                                                PREMAKE_CONFIG_PATH_BAK);
 
                        if (rename(PREMAKE_CONFIG_PATH,
@@ -1118,7 +1130,7 @@
                }
 
                /* copying the temporary config to the system one */
-               printf("==> Saving configuration file: %s\n",
+               fprintf(stderr, "==> Saving configuration file: %s\n",
                                                PREMAKE_CONFIG_PATH);
                if (fcopy(sfn, PREMAKE_CONFIG_PATH,
                                        PREMAKE_CONFIG_MODE) == false) {
@@ -1129,15 +1141,17 @@
        }
 #endif
 
+       if (!tostdout) {
 #ifdef PMKSETUP_DEBUG
-       debugf("%s has not been deleted!", sfn);
+               debugf("%s has not been deleted!", sfn);
 #else
-       if (unlink(sfn) == -1) {
-               errorf("cannot remove temporary file: '%s' : %s.",
-                                       sfn, strerror(errno));
-               error = true;
-       }
+               if (unlink(sfn) == -1) {
+                       errorf("cannot remove temporary file: '%s' : %s.",
+                                               sfn, strerror(errno));
+                       error = true;
+               }
 #endif /* PMKSETUP_DEBUG */
+       }
 
 #ifndef WITHOUT_FORK
        if (status != 0) {
@@ -1165,7 +1179,7 @@
  ***********************************************************************/
 
 void usage(void) {
-       fprintf(stderr, "usage: pmksetup [-hVv] "
+       fprintf(stderr, "usage: pmksetup [-hsVv] "
                "[-r variable] [-u variable=value]\n");
        exit(EXIT_FAILURE);
 }
@@ -1188,6 +1202,7 @@
        optind = 1;
        while ((ch = getopt(argc, argv, PMKSTP_OPT_STR)) != -1) {
                switch(ch) {
+                       case 'g' :
                        case 'r' :
                        case 'u' :
                                /*
@@ -1206,6 +1221,10 @@
                                        verbose_flag = 1;
                                break;
 
+                       case 's':
+                               tostdout = true;
+                               break;
+
                        case '?' :
                        default :
                                usage();
@@ -1213,7 +1232,6 @@
                }
        }
 
-
        if (getuid() == 0) {
 #ifdef PMKSETUP_DEBUG
                debugf("PRIVSEP_USER = '%s'", PRIVSEP_USER);
@@ -1227,22 +1245,27 @@
                gid = pw->pw_gid;
        }
 
-       /* check if syconfdir exists */
-       if (access(CONFDIR, F_OK) != 0) { /* no race condition, just mkdir() */
-               verbosef("==> Creating '%s' directory.", CONFDIR);
-               if (mkdir(CONFDIR, S_IRWXU | S_IRGRP | S_IXGRP |
-                                               S_IROTH | S_IXOTH) != 0) {
-                       errorf("cannot create '%s' directory : %s.",
-                                               CONFDIR, strerror(errno));
-                       exit(EXIT_FAILURE);
-               }
+       if (tostdout) {
+               sfp = stdout;
        }
+       else {
+               /* check if syconfdir exists */
+               if (access(CONFDIR, F_OK) != 0) { /* no race condition, just mkdir() */
+                       verbosef("==> Creating '%s' directory.", CONFDIR);
+                       if (mkdir(CONFDIR, S_IRWXU | S_IRGRP | S_IXGRP |
+                                                          S_IROTH | S_IXOTH) != 0) {
+                               errorf("cannot create '%s' directory : %s.",
+                                                       CONFDIR, strerror(errno));
+                               exit(EXIT_FAILURE);
+                       }
+               }
 
-       sfp = tmp_open(PREMAKE_CONFIG_TMP, "w", sfn, sizeof(sfn));
-       if (sfp == NULL) {
-               errorf("cannot open temporary file '%s' : %s.",
-                               sfn, strerror(errno));
-               exit(EXIT_FAILURE);
+               sfp = tmp_open(PREMAKE_CONFIG_TMP, "w", sfn, sizeof(sfn));
+               if (sfp == NULL) {
+                       errorf("cannot open temporary file '%s' : %s.",
+                                       sfn, strerror(errno));
+                       exit(EXIT_FAILURE);
+               }
        }
 
 #ifndef WITHOUT_FORK
diff -urNad trunk~/pmksetup.h trunk/pmksetup.h
--- trunk~/pmksetup.h   2005-05-14 13:37:38.000000000 +0200
+++ trunk/pmksetup.h    2007-08-05 15:09:51.000000000 +0200
@@ -85,7 +85,7 @@
 #define        ECHO_NL         "\\n"
 #define        ECHO_HT         "\\t"
 
-#define PMKSTP_OPT_STR "hr:u:vV"       /* "a:hr:u:vV" */
+#define PMKSTP_OPT_STR "ghr:su:vV"     /* "a:hr:u:vV" */
 
 #define EMSG_PRIV_FMT  "Failed to change privilege (%s)"