]> err.no Git - backuppcd/commitdiff
Add getopt handling
authorTollef Fog Heen <tfheen@err.no>
Fri, 18 Jul 2008 08:46:31 +0000 (10:46 +0200)
committerTollef Fog Heen <tfheen@err.no>
Fri, 18 Jul 2008 08:46:31 +0000 (10:46 +0200)
backuppcd.c

index 1cc3031374bfea9e5af84b99151b8338535e5a3d..7bbf7baaee6ed41baa8f42ba1f7dda11f4146674 100644 (file)
@@ -34,6 +34,7 @@
 #include <fnmatch.h>
 
 #include <stdio.h>
+#include <getopt.h>
 
 #define DAEMON_RET_SUCCESS 0
 #define DAEMON_RET_FAILURE 1
@@ -3685,15 +3686,13 @@ static void daemon_start(int argc, char **argv) {
  *
  */
 
-#ifdef HAVE_LIBCONFIG
-static int backuppc_opt_remove_svc(const char *shortvar, const char *var, const char *arguments, const char *value, lc_flags_t flags, void *extra) {
+static int backuppc_opt_remove_svc(void) {
        if (daemon_remove() < 0) {
                exit(EXIT_FAILURE);
        }
 
        exit(EXIT_SUCCESS);
 }
-#endif
 
 /*
  * SYNOPSIS:
@@ -3711,8 +3710,7 @@ static int backuppc_opt_remove_svc(const char *shortvar, const char *var, const
  *
  */
 
-#ifdef HAVE_LIBCONFIG
-static int backuppc_opt_stop_svc(const char *shortvar, const char *var, const char *arguments, const char *value, lc_flags_t flags, void *extra) {
+static int backuppc_opt_stop_svc(void) {
        if (daemon_stop() < 0) {
                exit(EXIT_FAILURE);
        }
@@ -3720,19 +3718,19 @@ static int backuppc_opt_stop_svc(const char *shortvar, const char *var, const ch
        exit(EXIT_SUCCESS);
 }
 
-static int backuppc_opt_showvers(const char *shortvar, const char *var, const char *arguments, const char *value, lc_flags_t flags, void *extra) {
+static int backuppc_opt_showvers(void) {
        printf("%s\n", PACKAGE_VERSION);
        exit(EXIT_SUCCESS);
 }
 
-static int backuppc_opt_prio(const char *shortvar, const char *var, const char *arguments, const char *value, lc_flags_t flags, void *extra) {
+static int backuppc_opt_prio(const char *value) {
        char *prio_names[3] = {"min", "max", "normal"};
        int prio_vals[3] = {BPC_PRIO_MIN, BPC_PRIO_MAX, BPC_PRIO_NORM};
        int prio_val = -1;
        int i;
 
        if (!value) {
-               return(LC_CBRET_ERROR);
+               return -1;
        }
 
        if (strlen(value) > 0) {
@@ -3742,7 +3740,7 @@ static int backuppc_opt_prio(const char *shortvar, const char *var, const char *
                                        prio_val = prio_vals[i];
                                } else {
                                        fprintf(stderr, "Ambigious priority name: %s, should be Min, Max, or Normal.\n", value);
-                                       return(LC_CBRET_ERROR);
+                                       return -1;
                                }
                        }
                }
@@ -3750,36 +3748,19 @@ static int backuppc_opt_prio(const char *shortvar, const char *var, const char *
 
        if (prio_val == -1) {
                fprintf(stderr, "Invalid priority name: %s, should be Min, Max, or Normal.\n", value);
-               return(LC_CBRET_ERROR);
+               return -1;
        }
 
        backuppc_setpriority(prio_val);
 
-       return(LC_CBRET_OKAY);
+       return 0;
 }
 
-#endif
-
-/*
- * SYNOPSIS:
- *   static void backuppc_pathmangle(
- *                                   char *pathname
- *                                  )
- *
- * ARGUMENTS:
- *   char *pathname               Pathname to mangle. 
- *
- * RETURN VALUE:
- *   (none)
- *
- * NOTES:
- *
- */
 int main(int argc, char *argv[]) {
-       int lc_p_ret = 0;
        char *update_source = NULL, *update_dest = NULL, *update_delefile = NULL;
        char *config_file = NULL;
        int do_switch = 0;
+       int option_index = 0;
 
        /*
         * Initialize the authentication subsystem.
@@ -3791,24 +3772,87 @@ int main(int argc, char *argv[]) {
        /*
         * Register configuration commands and command line arguments.
         */
-#ifdef HAVE_LIBCONFIG
-       lc_register_callback("Remove", 'r', LC_VAR_NONE, backuppc_opt_remove_svc, NULL);
-       lc_register_callback("Stop", 'k', LC_VAR_NONE, backuppc_opt_stop_svc, NULL);
-       lc_register_callback("Version", 'V', LC_VAR_NONE, backuppc_opt_showvers, NULL);
-       lc_register_callback("Priority", 'P', LC_VAR_STRING, backuppc_opt_prio, NULL);
-       lc_register_var("Port", LC_VAR_INT, &backuppc_port, 'p');
-       lc_register_var("UpdateURL", LC_VAR_STRING, &backuppc_updateurl, 'U');
-       lc_register_var("Source", LC_VAR_STRING, &update_source, 0);
-       lc_register_var("Destination", LC_VAR_STRING, &update_dest, 0);
-       lc_register_var("Switch", LC_VAR_BOOL_BY_EXISTANCE, &do_switch, 0);
-       lc_register_var("DeleteFile", LC_VAR_STRING, &update_delefile, 0);
-       lc_register_var("BinaryFile", LC_VAR_STRING, &backuppc_binfile, 0);
-       lc_register_var("ConfigFile", LC_VAR_STRING, &config_file, 'C');
-       lc_register_var("NotifyServer", LC_VAR_STRING, &backuppc_notifyserv, 'S');
+
+#define OPTION_Source 256
+#define OPTION_Destination 257
+#define OPTION_Switch 258
+#define OPTION_DeleteFile 259
+#define OPTION_BinaryFile 260
+
+       static struct option long_options[] = {
+               { "remove", 0, 0, 'r'},
+               { "stop", 0, 0, 'k'},
+               { "version", 0, 0, 'V'},
+               { "priority", 1, 0, 'P'},
+               { "port", 1, 0, 'p' },
+               { "updateurl", 1, 0, 'U'},
+               { "source", 1, 0, OPTION_Source },
+               { "destination", 1, 0, OPTION_Destination},
+               { "switch", 0, 0, OPTION_Switch },
+               { "deletefile", 1, 0, OPTION_DeleteFile},
+               { "binaryfile", 1, 0, OPTION_BinaryFile},
+               { "configfile", 1, 0, 'C'},
+               { "notifyserver", 1, 0, 'S' },
+               {0}
+       };
+
+       while (1) {
+               int c;
+               c = getopt_long(argc, argv, "rkVPp:U:C:S:",
+                               long_options, &option_index);
+               if (c == -1) {
+                       break;
+               }
+               switch (c) {
+               case 'r':
+                       backuppc_opt_remove_svc();
+                       break;
+               case 'k':
+                       backuppc_opt_stop_svc();
+                       break;
+               case 'V':
+                       backuppc_opt_showvers();
+                       break;
+               case 'P':
+                       backuppc_opt_prio(optarg);
+                       break;
+               case 'p':
+                       backuppc_port = strtoul(optarg, NULL, 0);
+                       break;
+               case 'U':
+                       backuppc_updateurl = strdup(optarg);
+                       break;
+               case 'C':
+                       config_file = strdup(optarg);
+                       break;
+               case 'S':
+                       backuppc_notifyserv = strdup(optarg);
+                       break;
+               case OPTION_Source:
+                       update_source = strdup(optarg);
+                       break;
+               case OPTION_Destination:
+                       update_dest = strdup(optarg);
+                       break;
+               case OPTION_Switch:
+                       do_switch = 1;
+                       break;
+               case OPTION_DeleteFile:
+                       update_delefile = strdup(optarg);
+                       break;
+               case OPTION_BinaryFile:
+                       backuppc_binfile = strdup(optarg);
+                       break;
+               default:
+                       /* XXX FIXME Usage */
+                       exit(EXIT_FAILURE);
+               }
+       }
        /*
         * Process standard config files, command line arguments, and
         * environment variables.
         */
+#ifdef HAVE_LIBCONFIG
        lc_p_ret = lc_process(argc, argv, "backuppcd", LC_CONF_SPACE, SYSCONFDIR "/backuppcd.conf");
        if (lc_p_ret < 0) {
                fprintf(stderr, "Error processing configuration information: %s.\n", lc_geterrstr());