00001
00002
00003
00004
00005
00006
00007
00008
00014 #include "common.h"
00015
00016
00017
00018
00019
00020 gearman_conf_st *gearman_conf_create(gearman_conf_st *conf)
00021 {
00022 if (conf == NULL)
00023 {
00024 conf= malloc(sizeof(gearman_conf_st));
00025 if (conf == NULL)
00026 return NULL;
00027
00028 conf->options= GEARMAN_CONF_ALLOCATED;
00029 }
00030 else
00031 conf->options= 0;
00032
00033 conf->last_return= GEARMAN_SUCCESS;
00034 conf->last_errno= 0;
00035 conf->module_count= 0;
00036 conf->option_count= 0;
00037 conf->short_count= 0;
00038 conf->module_list= NULL;
00039 conf->option_list= NULL;
00040 conf->option_short[0]= 0;
00041 conf->last_error[0]= 0;
00042
00043
00044 conf->option_getopt= malloc(sizeof(struct option));
00045 if (conf->option_getopt == NULL)
00046 {
00047 gearman_conf_free(conf);
00048 return NULL;
00049 }
00050
00051 memset(conf->option_getopt, 0, sizeof(sizeof(struct option)));
00052
00053 return conf;
00054 }
00055
00056 void gearman_conf_free(gearman_conf_st *conf)
00057 {
00058 uint32_t x;
00059
00060 for (x= 0; x < conf->module_count; x++)
00061 gearman_conf_module_free(conf->module_list[x]);
00062
00063 for (x= 0; x < conf->option_count; x++)
00064 {
00065 free((char *)conf->option_getopt[x].name);
00066
00067 if (conf->option_list[x].value_list != NULL)
00068 free(conf->option_list[x].value_list);
00069 }
00070
00071 if (conf->module_list != NULL)
00072 free(conf->module_list);
00073
00074 if (conf->option_list != NULL)
00075 free(conf->option_list);
00076
00077 if (conf->option_getopt != NULL)
00078 free(conf->option_getopt);
00079
00080 if (conf->options & GEARMAN_CONF_ALLOCATED)
00081 free(conf);
00082 }
00083
00084 gearman_return_t gearman_conf_return(gearman_conf_st *conf)
00085 {
00086 return conf->last_return;
00087 }
00088
00089 const char *gearman_conf_error(gearman_conf_st *conf)
00090 {
00091 return (const char *)(conf->last_error);
00092 }
00093
00094 int gearman_conf_errno(gearman_conf_st *conf)
00095 {
00096 return conf->last_errno;
00097 }
00098
00099 void gearman_conf_set_options(gearman_conf_st *conf,
00100 gearman_conf_options_t options, uint32_t data)
00101 {
00102 if (data)
00103 conf->options |= options;
00104 else
00105 conf->options &= ~options;
00106 }
00107
00108 gearman_return_t gearman_conf_parse_args(gearman_conf_st *conf, int argc,
00109 char *argv[])
00110 {
00111 int c;
00112 int opt_index;
00113 gearman_conf_option_st *option;
00114 char **value_list;
00115
00116
00117 opterr= 0;
00118
00119 while (1)
00120 {
00121 c= getopt_long(argc, argv, conf->option_short, conf->option_getopt,
00122 &opt_index);
00123 if (c == -1)
00124 break;
00125
00126 switch (c)
00127 {
00128 case 0:
00129
00130 break;
00131
00132 default:
00133
00134 for (opt_index= 0; opt_index < (int)conf->option_count; opt_index++)
00135 {
00136 if (conf->option_getopt[opt_index].val == c)
00137 break;
00138 }
00139
00140 if (opt_index == (int)conf->option_count)
00141 {
00142 GEARMAN_CONF_ERROR_SET(conf, "ERROR", " Unknown option: %s",
00143 argv[optind - 1]);
00144 return GEARMAN_UNKNOWN_OPTION;
00145 }
00146 }
00147
00148 option= &conf->option_list[opt_index];
00149 value_list= realloc(option->value_list,
00150 sizeof(char *) * (option->value_count + 1));
00151 if (value_list == NULL)
00152 {
00153 GEARMAN_CONF_ERROR_SET(conf, "gearman_conf_parse_args", "realloc");
00154 return GEARMAN_MEMORY_ALLOCATION_FAILURE;
00155 }
00156
00157 option->value_list= value_list;
00158 option->value_list[option->value_count]= optarg;
00159 option->value_count++;
00160 }
00161
00162 if (optind < argc)
00163 {
00164 GEARMAN_CONF_ERROR_SET(conf, "gearman_conf_parse_args",
00165 "Unknown option: %s", argv[optind]);
00166 return GEARMAN_UNKNOWN_OPTION;
00167 }
00168
00169 return GEARMAN_SUCCESS;
00170 }
00171
00172 void gearman_conf_usage(gearman_conf_st *conf)
00173 {
00174 uint32_t x;
00175 uint32_t y;
00176 gearman_conf_module_st *module;
00177 gearman_conf_option_st *option;
00178 bool print_header;
00179 char display[GEARMAN_CONF_DISPLAY_WIDTH];
00180 size_t max_length;
00181 size_t new_length;
00182 const char *help_start;
00183 const char *help_next;
00184
00185 for (x= 0; x < conf->module_count; x++)
00186 {
00187 module= conf->module_list[x];
00188 print_header= true;
00189
00190
00191 max_length= 0;
00192
00193 for (y= 0; y < conf->option_count; y++)
00194 {
00195 if (module != conf->option_list[y].module)
00196 continue;
00197
00198 new_length= strlen(conf->option_getopt[y].name);
00199
00200 if (conf->option_list[y].value_name != NULL)
00201 new_length+= strlen(conf->option_list[y].value_name) + 1;
00202
00203 if (new_length > max_length)
00204 max_length= new_length;
00205 }
00206
00207
00208 max_length+= 8;
00209 if (max_length > GEARMAN_CONF_DISPLAY_WIDTH - 2)
00210 max_length= GEARMAN_CONF_DISPLAY_WIDTH - 2;
00211
00212
00213 for (y= 0; y < conf->option_count; y++)
00214 {
00215 option= &conf->option_list[y];
00216
00217 if (option->module != module)
00218 continue;
00219
00220 if (print_header)
00221 {
00222 printf("\n%s Options:\n\n",
00223 module->name == NULL ? "Main" : module->name);
00224 print_header= false;
00225 }
00226
00227
00228 snprintf(display, GEARMAN_CONF_DISPLAY_WIDTH, " --%s%s%s%80s",
00229 conf->option_getopt[y].name,
00230 option->value_name == NULL ? "" : "=",
00231 option->value_name == NULL ? "" : option->value_name, "");
00232 display[max_length - 1]= ' ';
00233 display[max_length]= 0;
00234
00235 if (conf->option_getopt[y].val != 0)
00236 {
00237 display[1]= '-';
00238 display[2]= (signed char)conf->option_getopt[y].val;
00239 display[3]= ',';
00240 }
00241
00242
00243 if (option->help == NULL)
00244 {
00245 printf("%s\n", display);
00246 continue;
00247 }
00248
00249
00250 help_start= option->help;
00251
00252 while (strlen(help_start) > (GEARMAN_CONF_DISPLAY_WIDTH - max_length))
00253 {
00254 help_next= help_start + (GEARMAN_CONF_DISPLAY_WIDTH - max_length);
00255 while (help_next != help_start && *help_next != ' ')
00256 help_next--;
00257
00258 if (help_next == help_start)
00259 help_next= strchr(help_start, ' ');
00260
00261 if (help_next == NULL)
00262 break;
00263
00264 printf("%s%.*s\n", display, (int)(help_next - help_start), help_start);
00265 memset(display, ' ', max_length - 1);
00266
00267 help_start= help_next + 1;
00268 }
00269
00270 printf("%s%s\n", display, help_start);
00271 }
00272 }
00273
00274 printf("\n\n");
00275 }