Gearman Developer Documentation

libgearman-server/conf.c
Go to the documentation of this file.
00001 /* Gearman server and library
00002  * Copyright (C) 2009 Eric Day
00003  * All rights reserved.
00004  *
00005  * Use and distribution licensed under the BSD license.  See
00006  * the COPYING file in the parent directory for full text.
00007  */
00008 
00014 #include "common.h"
00015 
00016 /*
00017  * Public definitions
00018  */
00019 
00020 gearman_conf_st *gearman_conf_create(gearman_conf_st *conf)
00021 {
00022   if (conf == NULL)
00023   {
00024     conf= (gearman_conf_st *)malloc(sizeof(gearman_conf_st));
00025     if (conf == NULL)
00026       return NULL;
00027 
00028     conf->options.allocated= true;
00029   }
00030   else
00031   {
00032     conf->options.allocated= false;
00033   }
00034 
00035   conf->last_return= GEARMAN_SUCCESS;
00036   conf->last_errno= 0;
00037   conf->module_count= 0;
00038   conf->option_count= 0;
00039   conf->short_count= 0;
00040   conf->module_list= NULL;
00041   conf->option_list= NULL;
00042   conf->option_short[0]= 0;
00043   conf->last_error[0]= 0;
00044 
00045   /* We always need a NULL terminated getopt list. */
00046   conf->option_getopt= (struct option *)malloc(sizeof(struct option));
00047   if (conf->option_getopt == NULL)
00048   {
00049     gearman_conf_free(conf);
00050     return NULL;
00051   }
00052 
00053   memset(conf->option_getopt, 0, sizeof(sizeof(struct option)));
00054 
00055   return conf;
00056 }
00057 
00058 void gearman_conf_free(gearman_conf_st *conf)
00059 {
00060   uint32_t x;
00061 
00062   for (x= 0; x < conf->module_count; x++)
00063     gearman_conf_module_free(conf->module_list[x]);
00064 
00065   for (x= 0; x < conf->option_count; x++)
00066   {
00067     free((char *)conf->option_getopt[x].name);
00068 
00069     if (conf->option_list[x].value_list != NULL)
00070       free(conf->option_list[x].value_list);
00071   }
00072 
00073   if (conf->module_list != NULL)
00074     free(conf->module_list);
00075 
00076   if (conf->option_list != NULL)
00077     free(conf->option_list);
00078 
00079   if (conf->option_getopt != NULL)
00080     free(conf->option_getopt);
00081 
00082   if (conf->options.allocated)
00083     free(conf);
00084 }
00085 
00086 gearman_return_t gearman_conf_return(gearman_conf_st *conf)
00087 {
00088   return conf->last_return;
00089 }
00090 
00091 const char *gearman_conf_error(gearman_conf_st *conf)
00092 {
00093   return (const char *)(conf->last_error);
00094 }
00095 
00096 int gearman_conf_errno(gearman_conf_st *conf)
00097 {
00098   return conf->last_errno;
00099 }
00100 
00101 gearman_return_t gearman_conf_parse_args(gearman_conf_st *conf, int argc,
00102                                          char *argv[])
00103 {
00104   int c;
00105   int opt_index;
00106   gearman_conf_option_st *option;
00107   char **value_list;
00108 
00109   /* This will supress errors being printed to stderr. */
00110   opterr= 0;
00111 
00112   while (1)
00113   {
00114     c= getopt_long(argc, argv, conf->option_short, conf->option_getopt,
00115                    &opt_index);
00116     if (c == -1)
00117       break;
00118 
00119     switch (c)
00120     {
00121     case 0:
00122       /* We have a long option, use index. */
00123       break;
00124 
00125     default:
00126       /* Find the long option index that matches the short character. */
00127       for (opt_index= 0; opt_index < (int)conf->option_count; opt_index++)
00128       {
00129         if (conf->option_getopt[opt_index].val == c)
00130           break;
00131       }
00132 
00133       if (opt_index == (int)conf->option_count)
00134       {
00135         gearman_conf_error_set(conf, "ERROR", " Unknown option: %s", argv[optind - 1]);
00136         return GEARMAN_UNKNOWN_OPTION;
00137       }
00138     }
00139 
00140     option= &conf->option_list[opt_index];
00141     value_list= (char **)realloc(option->value_list,
00142                                  sizeof(char *) * (option->value_count + 1));
00143     if (value_list == NULL)
00144     {
00145       gearman_conf_error_set(conf, "gearman_conf_parse_args", " realloc");
00146       return GEARMAN_MEMORY_ALLOCATION_FAILURE;
00147     }
00148 
00149     option->value_list= value_list;
00150     option->value_list[option->value_count]= optarg;
00151     option->value_count++;
00152   }
00153 
00154   if (optind < argc)
00155   {
00156     gearman_conf_error_set(conf, "gearman_conf_parse_args", "Unknown option: %s", argv[optind]);
00157     return GEARMAN_UNKNOWN_OPTION;
00158   }
00159 
00160   return GEARMAN_SUCCESS;
00161 }
00162 
00163 void gearman_conf_usage(gearman_conf_st *conf)
00164 {
00165   uint32_t x;
00166   uint32_t y;
00167   gearman_conf_module_st *module;
00168   gearman_conf_option_st *option;
00169   bool print_header;
00170   char display[GEARMAN_CONF_DISPLAY_WIDTH];
00171   size_t max_length;
00172   size_t new_length;
00173   const char *help_start;
00174   const char *help_next;
00175 
00176   for (x= 0; x < conf->module_count; x++)
00177   {
00178     module= conf->module_list[x];
00179     print_header= true;
00180 
00181     /* Find the maximum option length for this module. */
00182     max_length= 0;
00183 
00184     for (y= 0; y < conf->option_count; y++)
00185     {
00186       if (module != conf->option_list[y].module)
00187         continue;
00188 
00189       new_length= strlen(conf->option_getopt[y].name);
00190 
00191       if (conf->option_list[y].value_name != NULL)
00192         new_length+= strlen(conf->option_list[y].value_name) + 1;
00193 
00194       if (new_length > max_length)
00195         max_length= new_length;
00196     }
00197 
00198     /* Truncate option length if it's too long. */
00199     max_length+= 8;
00200     if (max_length > GEARMAN_CONF_DISPLAY_WIDTH - 2)
00201       max_length= GEARMAN_CONF_DISPLAY_WIDTH - 2;
00202 
00203     /* Print out all options.allocated for this module. */
00204     for (y= 0; y < conf->option_count; y++)
00205     {
00206       option= &conf->option_list[y];
00207 
00208       if (option->module != module)
00209         continue;
00210 
00211       if (print_header)
00212       {
00213         printf("\n%s Options:\n\n",
00214                module->name == NULL ? "Main" : module->name);
00215         print_header= false;
00216       }
00217 
00218       /* Build string with possible short option. */
00219       snprintf(display, GEARMAN_CONF_DISPLAY_WIDTH, "     --%s%s%s%80s",
00220                conf->option_getopt[y].name,
00221                option->value_name == NULL ? "" : "=",
00222                option->value_name == NULL ? "" : option->value_name, "");
00223       display[max_length - 1]= ' ';
00224       display[max_length]= 0;
00225 
00226       if (conf->option_getopt[y].val != 0)
00227       {
00228         display[1]= '-';
00229         display[2]= (signed char)conf->option_getopt[y].val;
00230         display[3]= ',';
00231       }
00232 
00233       /* If there is no help, just print the option. */
00234       if (option->help == NULL)
00235       {
00236         printf("%s\n", display);
00237         continue;
00238       }
00239 
00240       /* Make sure the help string is properly wrapped. */
00241       help_start= option->help;
00242 
00243       while (strlen(help_start) > (GEARMAN_CONF_DISPLAY_WIDTH - max_length))
00244       {
00245         help_next= help_start + (GEARMAN_CONF_DISPLAY_WIDTH - max_length);
00246         while (help_next != help_start && *help_next != ' ')
00247           help_next--;
00248 
00249         if (help_next == help_start)
00250           help_next= strchr(help_start, ' ');
00251 
00252         if (help_next == NULL)
00253           break;
00254 
00255         printf("%s%.*s\n", display, (int)(help_next - help_start), help_start);
00256         memset(display, ' ', max_length - 1);
00257 
00258         help_start= help_next + 1;
00259       }
00260 
00261       printf("%s%s\n", display, help_start);
00262     }
00263   }
00264 
00265   printf("\n\n");
00266 }