const to args
[lttv.git] / ltt / branches / poly / lttv / option.c
CommitLineData
1cab0928 1#include <popt.h>
eccb5352 2
3d218f2a 3#include <lttv/hook.h>
c71d80de 4#include "lttv.h"
5#include "option.h"
eccb5352 6
7/* Extensible array of popt command line options. Modules add options as
8 they are loaded and initialized. */
9
10typedef struct _lttv_option {
11 lttv_option_hook hook;
12 void *hook_data;
13} lttv_option;
14
eccb5352 15static GArray *lttv_options_command;
16
17static GArray *lttv_options_command_popt;
18
19// unneeded static lttv_key *key ;
20
21static int command_argc;
22
23static char **command_argv;
24
25/* Lists of hooks to be called at different places */
26
27static lttv_hooks
28 *hooks_options_before,
29 *hooks_options_after;
30
31static gboolean init_done = FALSE;
32
33void lttv_options_command_parse(void *hook_data, void *call_data);
34
35
36void lttv_option_init(int argc, char **argv) {
37
38 lttv_hooks *hooks_init_after;
39
40 if(init_done) return;
41 else init_done = TRUE;
42
43 command_argc = argc;
44 command_argv = argv;
45
46 hooks_options_before = lttv_hooks_new();
47 hooks_options_after = lttv_hooks_new();
48
1cab0928 49 lttv_attributes_set_pointer_pathname(lttv_global_attributes(),
eccb5352 50 "hooks/options/before", hooks_options_before);
51
1cab0928 52 lttv_attributes_set_pointer_pathname(lttv_global_attributes(),
eccb5352 53 "hooks/options/after", hooks_options_after);
54
55 lttv_options_command_popt = g_array_new(0,0,sizeof(struct poptOption));
56 lttv_options_command = g_array_new(0,0,sizeof(lttv_option));
57
1cab0928 58 hooks_init_after = lttv_attributes_get_pointer_pathname(lttv_global_attributes(),
eccb5352 59 "hooks/init/after");
60 lttv_hooks_add(hooks_init_after, lttv_options_command_parse, NULL);
61
62}
63
64void lttv_option_destroy() {
65
f68ad60d 66 struct poptOption *poption;
67
68 for(i=0; i<lttv_options_command_popt->len,i++) {
69 poption = &g_array_index (lttv_options_command_popt, struct poptOption, i);
70
71 g_free(poption.longName);
72 g_free(poption.shortName);
73 g_free(poption.descrip);
74 g_free(poption.argDescrip);
75 }
eccb5352 76 g_array_free(lttv_options_command_popt,TRUE) ;
77 g_array_free(lttv_options_command,TRUE) ;
78
1cab0928 79 lttv_attributes_set_pointer_pathname(lttv_global_attributes(),
eccb5352 80 "hooks/options/before", NULL);
81
1cab0928 82 lttv_attributes_set_pointer_pathname(lttv_global_attributes(),
eccb5352 83 "hooks/options/after", NULL);
84
85 lttv_hooks_destroy(hooks_options_before);
86 lttv_hooks_destroy(hooks_options_after);
87
88}
89
90
91static int poptToLTT[] = {
92 POPT_ARG_NONE, POPT_ARG_STRING, POPT_ARG_INT, POPT_ARG_LONG
93};
94
95
f68ad60d 96void lttv_option_add(const char *long_name, const char char_name,
97 const char *description, const char *argDescription,
98 const lttv_option_type t, const void *p,
99 const lttv_option_hook h, void *hook_data)
eccb5352 100{
101 struct poptOption poption;
102
103 lttv_option option;
104
f68ad60d 105 poption.longName = (char *)g_strdup(long_name);
eccb5352 106 poption.shortName = char_name;
f68ad60d 107 poption.descrip = (char *)g_strdup(description);
108 poption.argDescrip = (char *)g_strdup(argDescription);
eccb5352 109 poption.argInfo = poptToLTT[t];
110 poption.arg = p;
111 poption.val = lttv_options_command->len + 1;
112
113 option.hook = h;
114 option.hook_data = hook_data;
115
116 g_array_append_val(lttv_options_command_popt,poption);
117 g_array_append_val(lttv_options_command,option);
118}
119
120
121static struct poptOption endOption = { NULL, '\0', 0, NULL, 0};
122
123/* As we may load modules in the hooks called for argument processing,
124 * we have to recreate the argument context each time the
125 * lttv_options_command_popt is modified. This way we will be able to
126 * parse arguments defined by the modules
127 */
128
129void lttv_options_command_parse(void *hook_data, void *call_data)
130{
131 int rc;
132 int lastrc;
133 poptContext c;
134 lttv_option *option;
135
136 lttv_hooks_call(hooks_options_before,NULL);
137 /* Always add then remove the null option around the get context */
138 g_array_append_val(lttv_options_command_popt, endOption);
139 /* Compiler warning caused by const char ** for command_argv in header */
140 /* Nothing we can do about it. Header should not put it const. */
141 c = poptGetContext("lttv", command_argc, (const char**)command_argv,
142 (struct poptOption *)(lttv_options_command_popt->data),0);
143
144 /* We remove the null option here to be able to add options correctly */
145 g_array_remove_index(lttv_options_command_popt,
146 lttv_options_command_popt->len - 1);
147
148 /* There is no last good offset */
149 lastrc = -1;
150
151 /* Parse options while not end of options event */
152 while((rc = poptGetNextOpt(c)) != -1) {
153
154 if(rc == POPT_ERROR_BADOPT) {
155 /* We need to redo the context with information added by modules */
156 g_array_append_val(lttv_options_command_popt, endOption);
157 poptFreeContext(c);
158 c = poptGetContext("lttv", command_argc, (const char**)command_argv,
159 (struct poptOption *)lttv_options_command_popt->data,0);
160 g_array_remove_index(lttv_options_command_popt,
161 lttv_options_command_popt->len);
162
163 /* Cut out the already parsed elements */
164 if(lastrc != -1)
165 while(poptGetNextOpt(c) != lastrc) { } ;
166
167 /* Get the same option once again */
168 g_assert(rc = poptGetNextOpt(c) != -1) ;
169 if(rc == POPT_ERROR_BADOPT) {
170 /* If here again we have a parsing error with all context info ok,
171 * then there is a problem in the arguments themself, give up */
172 g_critical("option %s: %s", poptBadOption(c,0), poptStrerror(rc));
173 break ;
174 }
175 }
176
177 /* Remember this offset as the last good option value */
178 lastrc = rc;
179
180 /* Execute the hook registered with this option */
181 option = ((lttv_option *)lttv_options_command->data) + rc - 1;
182 if(option->hook != NULL) option->hook(option->hook_data);
183
184 }
185
186 poptFreeContext(c);
187
188 lttv_hooks_call(hooks_options_after,NULL);
189
190}
191
This page took 0.02881 seconds and 4 git commands to generate.