00001
00002
00003
00004
00005
00006
00007
00008
00014 #include "common.h"
00015
00016
00017
00018
00019
00020 gearman_conf_module_st *gearman_conf_module_create(gearman_conf_st *conf,
00021 gearman_conf_module_st *module,
00022 const char *name)
00023 {
00024 gearman_conf_module_st **module_list;
00025
00026 if (module == NULL)
00027 {
00028 module= malloc(sizeof(gearman_conf_module_st));
00029 if (module == NULL)
00030 {
00031 gearman_conf_error_set(conf, "gearman_conf_module_create", "malloc");
00032 return NULL;
00033 }
00034
00035 module->options.allocated= true;
00036 }
00037 else
00038 {
00039 module->options.allocated= false;
00040 }
00041
00042 module->current_option= 0;
00043 module->current_value= 0;
00044 module->conf= conf;
00045 module->name= name;
00046
00047 module_list= realloc(conf->module_list, sizeof(gearman_conf_module_st *) *
00048 (conf->module_count + 1));
00049 if (module_list == NULL)
00050 {
00051 gearman_conf_module_free(module);
00052 gearman_conf_error_set(conf, "gearman_conf_module_create", "realloc");
00053 return NULL;
00054 }
00055
00056 conf->module_list= module_list;
00057 conf->module_list[conf->module_count]= module;
00058 conf->module_count++;
00059
00060 return module;
00061 }
00062
00063 void gearman_conf_module_free(gearman_conf_module_st *module)
00064 {
00065 if (module->options.allocated)
00066 free(module);
00067 }
00068
00069 gearman_conf_module_st *gearman_conf_module_find(gearman_conf_st *conf,
00070 const char *name)
00071 {
00072 for (uint32_t x= 0; x < conf->module_count; x++)
00073 {
00074 if (name == NULL || conf->module_list[x]->name == NULL)
00075 {
00076 if (name == conf->module_list[x]->name)
00077 return conf->module_list[x];
00078 }
00079 else if (!strcmp(name, conf->module_list[x]->name))
00080 return conf->module_list[x];
00081 }
00082
00083 return NULL;
00084 }
00085
00086 void gearman_conf_module_add_option(gearman_conf_module_st *module,
00087 const char *name, int short_name,
00088 const char *value_name, const char *help)
00089 {
00090 gearman_conf_st *conf= module->conf;
00091 gearman_conf_option_st *option_list;
00092 struct option *option_getopt;
00093
00094
00095 for (uint32_t x= 0; x < conf->option_count && short_name != 0; x++)
00096 {
00097 if (conf->option_getopt[x].val == short_name)
00098 short_name= 0;
00099 }
00100
00101
00102 option_list= realloc(conf->option_list, sizeof(gearman_conf_option_st) *
00103 (conf->option_count + 1));
00104 if (option_list == NULL)
00105 {
00106 gearman_conf_error_set(conf, "gearman_conf_module_add_option", "realloc");
00107 conf->last_return= GEARMAN_MEMORY_ALLOCATION_FAILURE;
00108 return;
00109 }
00110
00111 conf->option_list= option_list;
00112 option_list= &conf->option_list[conf->option_count];
00113
00114 option_getopt= realloc(conf->option_getopt,
00115 sizeof(struct option) * (conf->option_count + 2));
00116 if (option_getopt == NULL)
00117 {
00118 gearman_conf_error_set(conf, "gearman_conf_module_add_option", "realloc");
00119 conf->last_return= GEARMAN_MEMORY_ALLOCATION_FAILURE;
00120 return;
00121 }
00122
00123 conf->option_getopt= option_getopt;
00124 option_getopt= &conf->option_getopt[conf->option_count];
00125
00126 conf->option_count++;
00127 memset(&conf->option_getopt[conf->option_count], 0,
00128 sizeof(sizeof(struct option)));
00129
00130 option_list->module= module;
00131 option_list->name= name;
00132 option_list->value_name= value_name;
00133 option_list->help= help;
00134 option_list->value_list= NULL;
00135 option_list->value_count= 0;
00136
00137 if (module->name == NULL)
00138 {
00139
00140 option_getopt->name= strdup(name);
00141 if (option_getopt->name == NULL)
00142 {
00143 gearman_conf_error_set(conf, "gearman_conf_module_add_option", "strdup");
00144 conf->last_return= GEARMAN_MEMORY_ALLOCATION_FAILURE;
00145 return;
00146 }
00147 }
00148 else
00149 {
00150
00151 size_t option_string_length= strlen(module->name) + strlen(name) + 2;
00152 char *option_string=
00153 malloc(option_string_length * sizeof(char));
00154
00155 if (option_string == NULL)
00156 {
00157 gearman_conf_error_set(conf, "gearman_conf_module_add_option", "malloc");
00158 conf->last_return= GEARMAN_MEMORY_ALLOCATION_FAILURE;
00159 return;
00160 }
00161
00162 snprintf(option_string, option_string_length, "%s-%s", module->name, name);
00163
00164 option_getopt->name= option_string;
00165 }
00166
00167 option_getopt->has_arg= value_name == NULL ? 0 : 1;
00168 option_getopt->flag= NULL;
00169 option_getopt->val= short_name;
00170
00171
00172 if (short_name != 0 &&
00173 conf->short_count < (GEARMAN_CONF_MAX_OPTION_SHORT - 2))
00174 {
00175 conf->option_short[conf->short_count++]= (char)short_name;
00176 if (value_name != NULL)
00177 conf->option_short[conf->short_count++]= ':';
00178 conf->option_short[conf->short_count]= '0';
00179 }
00180 }
00181
00182 bool gearman_conf_module_value(gearman_conf_module_st *module,
00183 const char **name, const char **value)
00184 {
00185 gearman_conf_option_st *option;
00186
00187 for (; module->current_option < module->conf->option_count;
00188 module->current_option++)
00189 {
00190 option= &module->conf->option_list[module->current_option];
00191 if (option->module != module)
00192 continue;
00193
00194 if (module->current_value < option->value_count)
00195 {
00196 *name= option->name;
00197 *value= option->value_list[module->current_value++];
00198 return true;
00199 }
00200
00201 module->current_value= 0;
00202 }
00203
00204 module->current_option= 0;
00205
00206 return false;
00207 }