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