| 1 | |
| 2 | #include <popt.h> |
| 3 | #include <glib.h> |
| 4 | #include <lttv/option.h> |
| 5 | |
| 6 | #define g_info(format...) g_log (G_LOG_DOMAIN, G_LOG_LEVEL_INFO, format) |
| 7 | #define g_debug(format...) g_log (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, format) |
| 8 | |
| 9 | typedef struct _LttvOption { |
| 10 | char *long_name; |
| 11 | char char_name; |
| 12 | char *description; |
| 13 | char *arg_description; |
| 14 | LttvOptionType t; |
| 15 | gpointer p; |
| 16 | LttvOptionHook hook; |
| 17 | gpointer hook_data; |
| 18 | } LttvOption; |
| 19 | |
| 20 | GHashTable *options; |
| 21 | |
| 22 | |
| 23 | static void |
| 24 | list_options(gpointer key, gpointer value, gpointer user_data) |
| 25 | { |
| 26 | g_ptr_array_add((GPtrArray *)user_data, value); |
| 27 | } |
| 28 | |
| 29 | |
| 30 | static void |
| 31 | free_option(LttvOption *option) |
| 32 | { |
| 33 | g_free(option->long_name); |
| 34 | g_free(option->description); |
| 35 | g_free(option->arg_description); |
| 36 | g_free(option); |
| 37 | } |
| 38 | |
| 39 | |
| 40 | void lttv_option_init(int argc, char **argv) |
| 41 | { |
| 42 | g_info("Init option.c"); |
| 43 | options = g_hash_table_new(g_str_hash, g_str_equal); |
| 44 | } |
| 45 | |
| 46 | |
| 47 | void lttv_option_destroy() |
| 48 | { |
| 49 | LttvOption option; |
| 50 | |
| 51 | GPtrArray *list = g_ptr_array_new(); |
| 52 | |
| 53 | int i; |
| 54 | |
| 55 | g_info("Destroy option.c"); |
| 56 | g_hash_table_foreach(options, list_options, list); |
| 57 | g_hash_table_destroy(options); |
| 58 | |
| 59 | for(i = 0 ; i < list->len ; i++) { |
| 60 | free_option((LttvOption *)list->pdata[i]); |
| 61 | } |
| 62 | g_ptr_array_free(list, TRUE); |
| 63 | } |
| 64 | |
| 65 | |
| 66 | void lttv_option_add(const char *long_name, const char char_name, |
| 67 | const char *description, const char *arg_description, |
| 68 | const LttvOptionType t, void *p, |
| 69 | const LttvOptionHook h, void *hook_data) |
| 70 | { |
| 71 | LttvOption *option; |
| 72 | |
| 73 | g_info("Add option %s", long_name); |
| 74 | if(g_hash_table_lookup(options, long_name) != NULL) { |
| 75 | g_warning("duplicate option"); |
| 76 | return; |
| 77 | } |
| 78 | |
| 79 | option = g_new(LttvOption, 1); |
| 80 | option->long_name = g_strdup(long_name); |
| 81 | option->char_name = char_name; |
| 82 | option->description = g_strdup(description); |
| 83 | option->arg_description = g_strdup(arg_description); |
| 84 | option->t = t; |
| 85 | option->p = p; |
| 86 | option->hook = h; |
| 87 | option->hook_data = hook_data; |
| 88 | g_hash_table_insert(options, option->long_name, option); |
| 89 | } |
| 90 | |
| 91 | |
| 92 | void |
| 93 | lttv_option_remove(const char *long_name) |
| 94 | { |
| 95 | LttvOption *option = g_hash_table_lookup(options, long_name); |
| 96 | |
| 97 | g_info("Remove option %s", long_name); |
| 98 | if(option == NULL) { |
| 99 | g_warning("trying to remove unknown option %s", long_name); |
| 100 | return; |
| 101 | } |
| 102 | g_hash_table_remove(options, long_name); |
| 103 | free_option(option); |
| 104 | } |
| 105 | |
| 106 | |
| 107 | static int poptToLTT[] = { |
| 108 | POPT_ARG_NONE, POPT_ARG_STRING, POPT_ARG_INT, POPT_ARG_LONG |
| 109 | }; |
| 110 | |
| 111 | static struct poptOption endOption = { NULL, '\0', 0, NULL, 0}; |
| 112 | |
| 113 | |
| 114 | static void |
| 115 | build_popts(GPtrArray **plist, struct poptOption **ppopts, poptContext *pc, |
| 116 | int argc, char **argv) |
| 117 | { |
| 118 | LttvOption *option; |
| 119 | |
| 120 | GPtrArray *list; |
| 121 | |
| 122 | struct poptOption *popts; |
| 123 | |
| 124 | poptContext c; |
| 125 | |
| 126 | guint i; |
| 127 | |
| 128 | list = g_ptr_array_new(); |
| 129 | |
| 130 | g_hash_table_foreach(options, list_options, list); |
| 131 | |
| 132 | /* Build a popt options array from our list */ |
| 133 | |
| 134 | popts = g_new(struct poptOption, list->len + 1); |
| 135 | |
| 136 | for(i = 0 ; i < list->len ; i++) { |
| 137 | option = (LttvOption *)list->pdata[i]; |
| 138 | popts[i].longName = option->long_name; |
| 139 | popts[i].shortName = option->char_name; |
| 140 | popts[i].descrip = option->description; |
| 141 | popts[i].argDescrip = option->arg_description; |
| 142 | popts[i].argInfo = poptToLTT[option->t]; |
| 143 | popts[i].arg = option->p; |
| 144 | popts[i].val = i + 1; |
| 145 | } |
| 146 | |
| 147 | /* Terminate the array for popt and create the context */ |
| 148 | |
| 149 | popts[list->len] = endOption; |
| 150 | c = poptGetContext(argv[0], argc, (const char**)argv, popts, 0); |
| 151 | |
| 152 | *plist = list; |
| 153 | *ppopts = popts; |
| 154 | *pc = c; |
| 155 | } |
| 156 | |
| 157 | |
| 158 | static void |
| 159 | destroy_popts(GPtrArray **plist, struct poptOption **ppopts, poptContext *pc) |
| 160 | { |
| 161 | g_ptr_array_free(*plist, TRUE); *plist = NULL; |
| 162 | g_free(*ppopts); *ppopts = NULL; |
| 163 | poptFreeContext(*pc); |
| 164 | } |
| 165 | |
| 166 | |
| 167 | void lttv_option_parse(int argc, char **argv) |
| 168 | { |
| 169 | GPtrArray *list; |
| 170 | |
| 171 | LttvOption *option; |
| 172 | |
| 173 | int i, rc, first_arg; |
| 174 | |
| 175 | struct poptOption *popts; |
| 176 | |
| 177 | poptContext c; |
| 178 | |
| 179 | i = 0; |
| 180 | |
| 181 | first_arg = 0; |
| 182 | |
| 183 | build_popts(&list, &popts, &c, argc, argv); |
| 184 | |
| 185 | /* Parse options while not end of options event */ |
| 186 | |
| 187 | while((rc = poptGetNextOpt(c)) != -1) { |
| 188 | |
| 189 | /* The option was recognized and the rc value returned is the argument |
| 190 | position in the array. Call the associated hook if present. */ |
| 191 | |
| 192 | if(rc > 0) { |
| 193 | option = (LttvOption *)(list->pdata[rc - 1]); |
| 194 | g_info("Option %s encountered", option->long_name); |
| 195 | if(option->hook != NULL) { |
| 196 | g_info("Option %s hook called", option->long_name); |
| 197 | option->hook(option->hook_data); |
| 198 | } |
| 199 | i++; |
| 200 | } |
| 201 | |
| 202 | else if(rc == POPT_ERROR_BADOPT && i != first_arg) { |
| 203 | g_info("Option %s not recognized, rescan options with new additions", |
| 204 | poptBadOption(c,0)); |
| 205 | |
| 206 | /* Perhaps this option is newly added, restart parsing */ |
| 207 | |
| 208 | destroy_popts(&list, &popts, &c); |
| 209 | build_popts(&list, &popts, &c, argc, argv); |
| 210 | |
| 211 | /* Get back to the same argument */ |
| 212 | |
| 213 | first_arg = i; |
| 214 | for(i = 0; i < first_arg; i++) { |
| 215 | rc = poptGetNextOpt(c); |
| 216 | option = (LttvOption *)(list->pdata[rc - 1]); |
| 217 | g_info("Option %s rescanned, skipped", option->long_name); |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | else { |
| 222 | |
| 223 | /* The option has some error and it is not because this is a newly |
| 224 | added option not recognized. */ |
| 225 | |
| 226 | g_error("option %s: %s", poptBadOption(c,0), poptStrerror(rc)); |
| 227 | break; |
| 228 | } |
| 229 | |
| 230 | } |
| 231 | |
| 232 | destroy_popts(&list, &popts, &c); |
| 233 | } |
| 234 | |
| 235 | static void show_help(LttvOption *option) |
| 236 | { |
| 237 | printf("--%s -%c argument: %s\n" , option->long_name, |
| 238 | option->char_name, |
| 239 | option->arg_description); |
| 240 | printf(" %s\n" , option->description); |
| 241 | |
| 242 | } |
| 243 | |
| 244 | void lttv_option_show_help(void) |
| 245 | { |
| 246 | LttvOption option; |
| 247 | |
| 248 | GPtrArray *list = g_ptr_array_new(); |
| 249 | |
| 250 | int i; |
| 251 | |
| 252 | g_hash_table_foreach(options, list_options, list); |
| 253 | |
| 254 | printf("Built-in commands available:\n"); |
| 255 | printf("\n"); |
| 256 | |
| 257 | for(i = 0 ; i < list->len ; i++) { |
| 258 | show_help((LttvOption *)list->pdata[i]); |
| 259 | } |
| 260 | g_ptr_array_free(list, TRUE); |
| 261 | |
| 262 | |
| 263 | } |
| 264 | |