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= GEARMAN_CONF_MODULE_ALLOCATED;
00036 }
00037 else
00038 module->options= 0;
00039
00040 module->current_option= 0;
00041 module->current_value= 0;
00042 module->conf= conf;
00043 module->name= name;
00044
00045 module_list= realloc(conf->module_list, sizeof(gearman_conf_module_st *) *
00046 (conf->module_count + 1));
00047 if (module_list == NULL)
00048 {
00049 gearman_conf_module_free(module);
00050 GEARMAN_CONF_ERROR_SET(conf, "gearman_conf_module_create", "realloc");
00051 return NULL;
00052 }
00053
00054 conf->module_list= module_list;
00055 conf->module_list[conf->module_count]= module;
00056 conf->module_count++;
00057
00058 return module;
00059 }
00060
00061 void gearman_conf_module_free(gearman_conf_module_st *module)
00062 {
00063 if (module->options & GEARMAN_CONF_MODULE_ALLOCATED)
00064 free(module);
00065 }
00066
00067 gearman_conf_module_st *gearman_conf_module_find(gearman_conf_st *conf,
00068 const char *name)
00069 {
00070 uint32_t x;
00071
00072 for (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 uint32_t x;
00094
00095
00096 for (x= 0; x < conf->option_count && short_name != 0; x++)
00097 {
00098 if (conf->option_getopt[x].val == short_name)
00099 short_name= 0;
00100 }
00101
00102
00103 option_list= realloc(conf->option_list, sizeof(gearman_conf_option_st) *
00104 (conf->option_count + 1));
00105 if (option_list == NULL)
00106 {
00107 GEARMAN_CONF_ERROR_SET(conf, "gearman_conf_module_add_option", "realloc");
00108 conf->last_return= GEARMAN_MEMORY_ALLOCATION_FAILURE;
00109 return;
00110 }
00111
00112 conf->option_list= option_list;
00113 option_list= &conf->option_list[conf->option_count];
00114
00115 option_getopt= realloc(conf->option_getopt,
00116 sizeof(struct option) * (conf->option_count + 2));
00117 if (option_getopt == NULL)
00118 {
00119 GEARMAN_CONF_ERROR_SET(conf, "gearman_conf_module_add_option", "realloc");
00120 conf->last_return= GEARMAN_MEMORY_ALLOCATION_FAILURE;
00121 return;
00122 }
00123
00124 conf->option_getopt= option_getopt;
00125 option_getopt= &conf->option_getopt[conf->option_count];
00126
00127 conf->option_count++;
00128 memset(&conf->option_getopt[conf->option_count], 0,
00129 sizeof(sizeof(struct option)));
00130
00131 option_list->module= module;
00132 option_list->name= name;
00133 option_list->value_name= value_name;
00134 option_list->help= help;
00135 option_list->value_list= NULL;
00136 option_list->value_count= 0;
00137
00138 if (module->name == NULL)
00139 {
00140
00141 option_getopt->name= strdup(name);
00142 if (option_getopt->name == NULL)
00143 {
00144 GEARMAN_CONF_ERROR_SET(conf, "gearman_conf_module_add_option", "strdup");
00145 conf->last_return= GEARMAN_MEMORY_ALLOCATION_FAILURE;
00146 return;
00147 }
00148 }
00149 else
00150 {
00151 option_getopt->name= malloc(strlen(module->name) + strlen(name) + 2);
00152 if (option_getopt->name == NULL)
00153 {
00154 GEARMAN_CONF_ERROR_SET(conf, "gearman_conf_module_add_option", "malloc");
00155 conf->last_return= GEARMAN_MEMORY_ALLOCATION_FAILURE;
00156 return;
00157 }
00158
00159 sprintf((char *)option_getopt->name, "%s-%s", module->name, name);
00160 }
00161
00162 option_getopt->has_arg= value_name == NULL ? 0 : 1;
00163 option_getopt->flag= NULL;
00164 option_getopt->val= short_name;
00165
00166
00167 if (short_name != 0 &&
00168 conf->short_count < (GEARMAN_CONF_MAX_OPTION_SHORT - 2))
00169 {
00170 conf->option_short[conf->short_count++]= (char)short_name;
00171 if (value_name != NULL)
00172 conf->option_short[conf->short_count++]= ':';
00173 conf->option_short[conf->short_count]= '0';
00174 }
00175 }
00176
00177 bool gearman_conf_module_value(gearman_conf_module_st *module,
00178 const char **name, const char **value)
00179 {
00180 gearman_conf_option_st *option;
00181
00182 for (; module->current_option < module->conf->option_count;
00183 module->current_option++)
00184 {
00185 option= &module->conf->option_list[module->current_option];
00186 if (option->module != module)
00187 continue;
00188
00189 if (module->current_value < option->value_count)
00190 {
00191 *name= option->name;
00192 *value= option->value_list[module->current_value++];
00193 return true;
00194 }
00195
00196 module->current_value= 0;
00197 }
00198
00199 module->current_option= 0;
00200
00201 return false;
00202 }