64
|
1 |
# HG changeset patch |
|
2 |
# Parent 92fa48ef53c909928706ab4c51518953339a38e4 |
|
3 |
Unified command option parsing |
|
4 |
|
66
|
5 |
* cmdopts_parse() & cmdopts_free() in utils.c/h |
|
6 |
* /roster uses parser |
|
7 |
* buddy_search() now expects argument in utf8 |
|
8 |
* /say_to uses parser |
|
9 |
|
65
|
10 |
diff -r 92fa48ef53c9 mcabber/mcabber/commands.c |
|
11 |
--- a/mcabber/mcabber/commands.c Sun Jan 27 00:40:37 2013 +0200 |
66
|
12 |
+++ b/mcabber/mcabber/commands.c Mon Feb 25 00:57:09 2013 +0200 |
|
13 |
@@ -755,7 +755,7 @@ |
|
14 |
g_slist_free(notes); |
|
15 |
} |
|
16 |
|
|
17 |
-static void roster_note(char *arg) |
|
18 |
+static void roster_note(gchar *arg) |
|
19 |
{ |
|
20 |
const char *bjid; |
|
21 |
guint type; |
|
22 |
@@ -781,14 +781,9 @@ |
|
23 |
} |
|
24 |
|
|
25 |
if (arg && *arg) { // Set a note |
|
26 |
- gchar *msg, *notetxt; |
|
27 |
- msg = to_utf8(arg); |
|
28 |
- if (!strcmp(msg, "-")) |
|
29 |
- notetxt = NULL; // delete note |
|
30 |
- else |
|
31 |
- notetxt = msg; |
|
32 |
- xmpp_set_storage_rosternotes(bjid, notetxt); |
|
33 |
- g_free(msg); |
|
34 |
+ if (!strcmp(arg, "-")) |
|
35 |
+ arg = NULL; // delete note |
|
36 |
+ xmpp_set_storage_rosternotes(bjid, arg); |
|
37 |
} else { // Display a note |
|
38 |
struct annotation *note = xmpp_get_storage_rosternotes(bjid, FALSE); |
|
39 |
if (note) { |
|
40 |
@@ -819,86 +814,136 @@ |
|
41 |
/* All these do_*() functions will be called with a "arg" parameter */ |
|
42 |
/* (with arg not null) */ |
|
43 |
|
|
44 |
-static void do_roster(char *arg) |
|
45 |
+static void do_roster(char *args) |
|
46 |
{ |
|
47 |
- char **paramlst; |
|
48 |
- char *subcmd; |
|
49 |
- |
|
50 |
- paramlst = split_arg(arg, 2, 1); // subcmd, arg |
|
51 |
- subcmd = *paramlst; |
|
52 |
- arg = *(paramlst+1); |
|
53 |
- |
|
54 |
- if (!subcmd || !*subcmd) { |
|
55 |
- scr_LogPrint(LPRINT_NORMAL, "Missing parameter."); |
|
56 |
- free_arg_lst(paramlst); |
|
57 |
- return; |
|
58 |
+ enum roster_subcommand_t { |
|
59 |
+ roster_scmd_bottom, roster_scmd_top, roster_scmd_up, roster_scmd_down, |
|
60 |
+ roster_scmd_group_prev, roster_scmd_group_next, |
|
61 |
+ roster_scmd_alternate, |
|
62 |
+ roster_scmd_unread_first, roster_scmd_unread_next, |
|
63 |
+ roster_scmd_search, |
|
64 |
+ roster_scmd_display, |
|
65 |
+ roster_scmd_hide_offline, roster_scmd_show_offline, roster_scmd_toggle_offline, |
|
66 |
+ roster_scmd_item_lock, roster_scmd_item_unlock, roster_scmd_item_toggle_lock, |
|
67 |
+ roster_scmd_note, |
|
68 |
+ roster_scmd_resource_lock, roster_scmd_resource_unlock, |
|
69 |
+ roster_scmd_hide, roster_scmd_show, roster_scmd_toggle, |
|
70 |
+ } subcmd; |
|
71 |
+#define ROSTER_SCMD_NOARG(NAME) \ |
|
72 |
+ { 0, #NAME, { NULL, NULL, NULL, NULL }, (gpointer)roster_scmd_##NAME } |
|
73 |
+// all of them have at most one argument |
|
74 |
+#define ROSTER_SCMD(NAME, FLAGS, VALUE) \ |
|
75 |
+ { 0, #NAME, { NULL, \ |
|
76 |
+ (cmdarg_t[1]){ \ |
|
77 |
+ { CMDOPT_LAST | FLAGS, { .arg = VALUE } } \ |
|
78 |
+ }, NULL, NULL }, (gpointer)roster_scmd_##NAME } |
|
79 |
+ cmdopts_t options = { |
|
80 |
+ NULL, |
|
81 |
+ (cmdarg_t[1]){ |
|
82 |
+ { CMDOPT_REQUIRED | CMDOPT_SUBCOMMAND | CMDOPT_LAST, { .cmd = NULL } } |
|
83 |
+ }, |
|
84 |
+ (subcmd_t[23]){ |
|
85 |
+ ROSTER_SCMD_NOARG(bottom), |
|
86 |
+ ROSTER_SCMD_NOARG(top), |
|
87 |
+ ROSTER_SCMD(up, 0, "1"), |
|
88 |
+ ROSTER_SCMD(down, 0, "1"), |
|
89 |
+ ROSTER_SCMD_NOARG(group_prev), |
|
90 |
+ ROSTER_SCMD_NOARG(group_next), |
|
91 |
+ ROSTER_SCMD_NOARG(alternate), |
|
92 |
+ ROSTER_SCMD_NOARG(unread_first), |
|
93 |
+ ROSTER_SCMD_NOARG(unread_next), |
|
94 |
+ ROSTER_SCMD(search, CMDOPT_REQUIRED, NULL), |
|
95 |
+ ROSTER_SCMD(display, 0, NULL), |
|
96 |
+ ROSTER_SCMD_NOARG(hide_offline), |
|
97 |
+ ROSTER_SCMD_NOARG(show_offline), |
|
98 |
+ ROSTER_SCMD_NOARG(toggle_offline), |
|
99 |
+ ROSTER_SCMD(item_lock, 0, "."), |
|
100 |
+ ROSTER_SCMD(item_unlock, 0, "."), |
|
101 |
+ ROSTER_SCMD(item_toggle_lock, 0, "."), |
|
102 |
+ ROSTER_SCMD(note, 0, NULL), |
|
103 |
+ ROSTER_SCMD(resource_lock, 0, NULL), |
|
104 |
+ ROSTER_SCMD(resource_unlock, 0, NULL), |
|
105 |
+ ROSTER_SCMD_NOARG(hide), |
|
106 |
+ ROSTER_SCMD_NOARG(show), |
|
107 |
+ { CMDOPT_LAST, "toggle", { NULL, NULL, NULL, NULL }, |
|
108 |
+ (gpointer)roster_scmd_toggle }, |
|
109 |
+ }, |
|
110 |
+ NULL, |
|
111 |
+ }; |
|
112 |
+ gchar *arg; |
|
113 |
+ |
|
114 |
+ { |
|
115 |
+ const char *error = cmdopts_parse (args, &options); |
|
116 |
+ if (error != NULL) { |
|
117 |
+ scr_log_print (LPRINT_NORMAL, error); |
|
118 |
+ return; |
|
119 |
+ } |
|
120 |
} |
|
121 |
|
|
122 |
- if (!strcasecmp(subcmd, "top")) { |
|
123 |
+ subcmd = (enum roster_subcommand_t) (options.args[0].value.cmd -> userdata); |
|
124 |
+ if (options.args[0].value.cmd -> options.args != NULL) { |
|
125 |
+ arg = options.args[0].value.cmd -> options.args[0].value.arg; |
|
126 |
+ } |
|
127 |
+ |
|
128 |
+ if (subcmd == roster_scmd_top) { |
|
129 |
scr_roster_top(); |
|
130 |
update_roster = TRUE; |
|
131 |
- } else if (!strcasecmp(subcmd, "bottom")) { |
|
132 |
+ } else if (subcmd == roster_scmd_bottom) { |
|
133 |
scr_roster_bottom(); |
|
134 |
update_roster = TRUE; |
|
135 |
- } else if (!strcasecmp(subcmd, "hide")) { |
|
136 |
+ } else if (subcmd == roster_scmd_hide) { |
|
137 |
scr_roster_visibility(0); |
|
138 |
- } else if (!strcasecmp(subcmd, "show")) { |
|
139 |
+ } else if (subcmd == roster_scmd_show) { |
|
140 |
scr_roster_visibility(1); |
|
141 |
- } else if (!strcasecmp(subcmd, "toggle")) { |
|
142 |
+ } else if (subcmd == roster_scmd_toggle) { |
|
143 |
scr_roster_visibility(-1); |
|
144 |
- } else if (!strcasecmp(subcmd, "hide_offline")) { |
|
145 |
+ } else if (subcmd == roster_scmd_hide_offline) { |
|
146 |
buddylist_set_hide_offline_buddies(TRUE); |
|
147 |
if (current_buddy) |
|
148 |
buddylist_build(); |
|
149 |
update_roster = TRUE; |
|
150 |
- } else if (!strcasecmp(subcmd, "show_offline")) { |
|
151 |
+ } else if (subcmd == roster_scmd_show_offline) { |
|
152 |
buddylist_set_hide_offline_buddies(FALSE); |
|
153 |
buddylist_build(); |
|
154 |
update_roster = TRUE; |
|
155 |
- } else if (!strcasecmp(subcmd, "toggle_offline")) { |
|
156 |
+ } else if (subcmd == roster_scmd_toggle_offline) { |
|
157 |
buddylist_set_hide_offline_buddies(-1); |
|
158 |
buddylist_build(); |
|
159 |
update_roster = TRUE; |
|
160 |
- } else if (!strcasecmp(subcmd, "display")) { |
|
161 |
+ } else if (subcmd == roster_scmd_display) { |
|
162 |
scr_roster_display(arg); |
|
163 |
- } else if (!strcasecmp(subcmd, "item_lock")) { |
|
164 |
+ } else if (subcmd == roster_scmd_item_lock) { |
|
165 |
roster_buddylock(arg, 1); |
|
166 |
- } else if (!strcasecmp(subcmd, "item_unlock")) { |
|
167 |
+ } else if (subcmd == roster_scmd_item_unlock) { |
|
168 |
roster_buddylock(arg, 0); |
|
169 |
- } else if (!strcasecmp(subcmd, "item_toggle_lock")) { |
|
170 |
+ } else if (subcmd == roster_scmd_item_toggle_lock) { |
|
171 |
roster_buddylock(arg, -1); |
|
172 |
- } else if (!strcasecmp(subcmd, "unread_first")) { |
|
173 |
+ } else if (subcmd == roster_scmd_unread_first) { |
|
174 |
scr_roster_unread_message(0); |
|
175 |
- } else if (!strcasecmp(subcmd, "unread_next")) { |
|
176 |
+ } else if (subcmd == roster_scmd_unread_next) { |
|
177 |
scr_roster_unread_message(1); |
|
178 |
- } else if (!strcasecmp(subcmd, "alternate")) { |
|
179 |
+ } else if (subcmd == roster_scmd_alternate) { |
|
180 |
scr_roster_jump_alternate(); |
|
181 |
- } else if (!strncasecmp(subcmd, "search", 6)) { |
|
182 |
- strip_arg_special_chars(arg); |
|
183 |
- if (!arg || !*arg) { |
|
184 |
- scr_LogPrint(LPRINT_NORMAL, "What name or JID are you looking for?"); |
|
185 |
- free_arg_lst(paramlst); |
|
186 |
- return; |
|
187 |
- } |
|
188 |
+ } else if (subcmd == roster_scmd_search) { |
|
189 |
scr_roster_search(arg); |
|
190 |
update_roster = TRUE; |
|
191 |
- } else if (!strcasecmp(subcmd, "up")) { |
|
192 |
+ } else if (subcmd == roster_scmd_up) { |
|
193 |
roster_updown(-1, arg); |
|
194 |
- } else if (!strcasecmp(subcmd, "down")) { |
|
195 |
+ } else if (subcmd == roster_scmd_down) { |
|
196 |
roster_updown(1, arg); |
|
197 |
- } else if (!strcasecmp(subcmd, "group_prev")) { |
|
198 |
+ } else if (subcmd == roster_scmd_group_prev) { |
|
199 |
scr_roster_prev_group(); |
|
200 |
- } else if (!strcasecmp(subcmd, "group_next")) { |
|
201 |
+ } else if (subcmd == roster_scmd_group_next) { |
|
202 |
scr_roster_next_group(); |
|
203 |
- } else if (!strcasecmp(subcmd, "note")) { |
|
204 |
+ } else if (subcmd == roster_scmd_note) { |
|
205 |
roster_note(arg); |
|
206 |
- } else if (!strcasecmp(subcmd, "resource_lock")) { |
|
207 |
+ } else if (subcmd == roster_scmd_resource_lock) { |
|
208 |
roster_resourcelock(arg, TRUE); |
|
209 |
- } else if (!strcasecmp(subcmd, "resource_unlock")) { |
|
210 |
+ } else { // roster_resource_unlock |
|
211 |
roster_resourcelock(arg, FALSE); |
|
212 |
- } else |
|
213 |
- scr_LogPrint(LPRINT_NORMAL, "Unrecognized parameter!"); |
|
214 |
- free_arg_lst(paramlst); |
|
215 |
+ } |
|
216 |
+ |
|
217 |
+ cmdopts_free(&options); |
|
218 |
} |
|
219 |
|
|
220 |
void do_color(char *arg) |
|
221 |
@@ -1634,130 +1679,109 @@ |
65
|
222 |
|
|
223 |
static void do_say_to(char *arg) |
|
224 |
{ |
|
225 |
- char **paramlst; |
|
226 |
- char *fjid, *msg_utf8; |
|
227 |
+ char *fjid; |
|
228 |
char *msg; |
|
229 |
- char *unescaped_msg = NULL; |
|
230 |
- char *uncompletedfjid = NULL; |
|
231 |
- char *file = NULL; |
|
232 |
+ char *file; |
|
233 |
+ gchar *freeme = NULL; // fjid |
|
234 |
+ gchar *freeme2 = NULL; // msg |
|
235 |
LmMessageSubType msg_type = LM_MESSAGE_SUB_TYPE_NOT_SET; |
|
236 |
- bool quiet = FALSE; |
|
237 |
- bool eval = FALSE; |
|
238 |
+ cmdopts_t options = { |
|
239 |
+ (cmdopt_t[5]){ |
|
240 |
+ { CMDOPT_SWITCH, 'n', "normal", { .swc = 0 } }, |
|
241 |
+ { CMDOPT_SWITCH, 'h', "headline", { .swc = 0 } }, |
|
242 |
+ { CMDOPT_SWITCH, 'e', "escapes", { .swc = 0 } }, |
|
243 |
+ { CMDOPT_SWITCH, 'q', "quiet", { .swc = 0 } }, |
|
244 |
+ { CMDOPT_LAST, 'f', "file", { .opt = NULL } }, |
|
245 |
+ }, |
|
246 |
+ (cmdarg_t[2]){ |
|
247 |
+ { CMDOPT_REQUIRED, { .arg = NULL } }, |
|
248 |
+ { CMDOPT_LAST | CMDOPT_PLAIN | CMDOPT_CATCHALL, { .arg = NULL } }, |
|
249 |
+ }, |
|
250 |
+ NULL, |
|
251 |
+ NULL, |
|
252 |
+ }; |
|
253 |
|
|
254 |
if (!xmpp_is_online()) { |
|
255 |
scr_LogPrint(LPRINT_NORMAL, "You are not connected."); |
|
256 |
return; |
|
257 |
} |
|
258 |
|
|
259 |
- msg_type = scan_mtype(&arg); |
|
260 |
- paramlst = split_arg(arg, 2, 1); // jid, message (or option, jid, message) |
|
261 |
- |
|
262 |
- if (!*paramlst) { // No parameter? |
|
263 |
- scr_LogPrint(LPRINT_NORMAL, "Please specify a Jabber ID."); |
|
264 |
- free_arg_lst(paramlst); |
|
265 |
- return; |
|
266 |
+ { // parse arguments |
|
267 |
+ const char *error = cmdopts_parse(arg, &options); |
|
268 |
+ if (error != NULL) { |
|
269 |
+ scr_log_print(LPRINT_NORMAL, error); |
|
270 |
+ return; |
|
271 |
+ } |
|
272 |
} |
|
273 |
|
|
274 |
- // Check for an option parameter |
|
275 |
- while (*paramlst) { |
|
276 |
- if (!strcmp(*paramlst, "-q")) { |
|
277 |
- char **oldparamlst = paramlst; |
|
278 |
- paramlst = split_arg(*(oldparamlst+1), 2, 1); // jid, message |
|
279 |
- free_arg_lst(oldparamlst); |
|
280 |
- quiet = TRUE; |
|
281 |
- } else if (!strcmp(*paramlst, "-e")) { |
|
282 |
- char **oldparamlst = paramlst; |
|
283 |
- paramlst = split_arg(*(oldparamlst+1), 2, 1); // jid, message |
|
284 |
- free_arg_lst(oldparamlst); |
|
285 |
- eval = TRUE; |
|
286 |
- } else if (!strcmp(*paramlst, "-f")) { |
|
287 |
- char **oldparamlst = paramlst; |
|
288 |
- paramlst = split_arg(*(oldparamlst+1), 2, 1); // filename, jid |
|
289 |
- free_arg_lst(oldparamlst); |
|
290 |
- if (!*paramlst) { |
|
291 |
- scr_LogPrint(LPRINT_NORMAL, "Wrong usage."); |
|
292 |
- free_arg_lst(paramlst); |
|
293 |
- return; |
|
294 |
- } |
|
295 |
- file = g_strdup(*paramlst); |
|
296 |
- // One more parameter shift... |
|
297 |
- oldparamlst = paramlst; |
|
298 |
- paramlst = split_arg(*(oldparamlst+1), 2, 1); // jid, nothing |
|
299 |
- free_arg_lst(oldparamlst); |
|
300 |
- } else |
|
301 |
- break; |
|
302 |
+ if (options.opts[0].value.swc) { |
|
303 |
+ msg_type = LM_MESSAGE_SUB_TYPE_NORMAL; |
|
304 |
+ } else if (options.opts[1].value.swc) { |
|
305 |
+ msg_type = LM_MESSAGE_SUB_TYPE_HEADLINE; |
|
306 |
} |
|
307 |
|
|
308 |
- if (!*paramlst) { |
|
309 |
- scr_LogPrint(LPRINT_NORMAL, "Wrong usage."); |
|
310 |
- free_arg_lst(paramlst); |
|
311 |
- return; |
|
312 |
- } |
|
313 |
- |
|
314 |
- fjid = *paramlst; |
|
315 |
- msg = *(paramlst+1); |
|
316 |
- |
|
317 |
+ fjid = options.args[0].value.arg; |
|
318 |
+ msg = options.args[1].value.arg; |
|
319 |
+ file = options.opts[4].value.opt; |
|
320 |
+ |
|
321 |
+ // ideally, this should go to commandline parsing subsystem |
|
322 |
if (fjid[0] == '.') { |
|
323 |
const gchar *cjid = (current_buddy ? CURRENT_JID : NULL); |
|
324 |
if (fjid[1] == '\0') { |
|
325 |
- fjid = g_strdup(cjid); |
|
326 |
+ fjid = (char *)cjid; // FIXME |
|
327 |
} else if (fjid[1] == JID_RESOURCE_SEPARATOR) { |
|
328 |
char *res_utf8 = to_utf8(fjid+2); |
|
329 |
- fjid = g_strdup_printf("%s%c%s", cjid, JID_RESOURCE_SEPARATOR, res_utf8); |
|
330 |
+ freeme = fjid = g_strdup_printf("%s%c%s", cjid, JID_RESOURCE_SEPARATOR, res_utf8); |
|
331 |
g_free(res_utf8); |
|
332 |
- } else |
|
333 |
- fjid = to_utf8(fjid); |
|
334 |
- } else |
|
335 |
- fjid = to_utf8(fjid); |
|
336 |
- |
|
337 |
+ } |
|
338 |
+ } |
|
339 |
+ |
|
340 |
+ // ditto |
|
341 |
if (!strchr(fjid, JID_DOMAIN_SEPARATOR)) { |
|
342 |
const gchar *append_server = settings_opt_get("default_server"); |
|
343 |
if (append_server) { |
|
344 |
gchar *res = strchr(fjid, JID_RESOURCE_SEPARATOR); |
|
345 |
- uncompletedfjid = fjid; |
|
346 |
if (res) { |
|
347 |
*res++ = '\0'; |
|
348 |
fjid = g_strdup_printf("%s%c%s%c%s", fjid, JID_DOMAIN_SEPARATOR, append_server, |
|
349 |
JID_RESOURCE_SEPARATOR, res); |
|
350 |
} else |
|
351 |
fjid = g_strdup_printf("%s%c%s", fjid, JID_DOMAIN_SEPARATOR, append_server); |
|
352 |
+ g_free(freeme); |
|
353 |
+ freeme = fjid; |
|
354 |
} |
|
355 |
} |
|
356 |
|
|
357 |
+ // as well |
|
358 |
if (check_jid_syntax(fjid)) { |
|
359 |
scr_LogPrint(LPRINT_NORMAL, "Please specify a valid Jabber ID."); |
|
360 |
- free_arg_lst(paramlst); |
|
361 |
- g_free(uncompletedfjid); |
|
362 |
- g_free(fjid); |
|
363 |
+ g_free(freeme); |
|
364 |
return; |
|
365 |
} |
|
366 |
|
|
367 |
- if (!file) { |
|
368 |
- msg_utf8 = to_utf8(msg); |
|
369 |
- if (eval) { |
|
370 |
- unescaped_msg = ut_unescape_tabs_cr(msg_utf8); |
|
371 |
+ if (file == NULL) { |
|
372 |
+ if (options.opts[2].value.swc) { |
|
373 |
+ freeme2 = ut_unescape_tabs_cr(msg); |
|
374 |
// We must not free() if the original string was returned |
|
375 |
- if (unescaped_msg == msg_utf8) |
|
376 |
- unescaped_msg = NULL; |
|
377 |
+ if (freeme2 == msg) |
|
378 |
+ freeme2 = NULL; |
|
379 |
+ else |
|
380 |
+ msg = freeme2; |
|
381 |
} |
|
382 |
- msg = (unescaped_msg ? unescaped_msg : msg_utf8); |
|
383 |
} else { |
|
384 |
char *filename_xp; |
|
385 |
if (msg) |
|
386 |
scr_LogPrint(LPRINT_NORMAL, "say_to: extra parameter ignored."); |
|
387 |
filename_xp = expand_filename(file); |
|
388 |
- msg = msg_utf8 = load_message_from_file(filename_xp); |
|
389 |
+ freeme2 = msg = load_message_from_file(filename_xp); |
|
390 |
g_free(filename_xp); |
|
391 |
- g_free(file); |
|
392 |
} |
|
393 |
|
|
394 |
- send_message_to(fjid, msg, NULL, msg_type, quiet); |
|
395 |
- |
|
396 |
- g_free(uncompletedfjid); |
|
397 |
- g_free(fjid); |
|
398 |
- g_free(msg_utf8); |
|
399 |
- g_free(unescaped_msg); |
|
400 |
- free_arg_lst(paramlst); |
|
401 |
+ send_message_to(fjid, msg, NULL, msg_type, options.opts[3].value.swc); |
|
402 |
+ |
|
403 |
+ cmdopts_free(&options); |
|
404 |
+ g_free(freeme); |
|
405 |
+ g_free(freeme2); |
|
406 |
} |
|
407 |
|
|
408 |
// buffer_updown(updown, nblines) |
66
|
409 |
diff -r 92fa48ef53c9 mcabber/mcabber/roster.c |
|
410 |
--- a/mcabber/mcabber/roster.c Sun Jan 27 00:40:37 2013 +0200 |
|
411 |
+++ b/mcabber/mcabber/roster.c Mon Feb 25 00:57:09 2013 +0200 |
|
412 |
@@ -1586,13 +1586,14 @@ |
|
413 |
// Look for a buddy whose name or jid contains string. |
|
414 |
// Search begins at current_buddy; if no match is found in the the buddylist, |
|
415 |
// return NULL; |
|
416 |
+// Note: before this function considered its argument to be in local encoding, |
|
417 |
+// now argument must be in utf8. |
|
418 |
GList *buddy_search(char *string) |
|
419 |
{ |
|
420 |
GList *buddy = current_buddy; |
|
421 |
roster *roster_usr; |
|
422 |
if (!buddylist || !current_buddy) return NULL; |
|
423 |
for (;;) { |
|
424 |
- gchar *jid_locale, *name_locale; |
|
425 |
char *found = NULL; |
|
426 |
|
|
427 |
buddy = g_list_next(buddy); |
|
428 |
@@ -1601,17 +1602,13 @@ |
|
429 |
|
|
430 |
roster_usr = (roster*)buddy->data; |
|
431 |
|
|
432 |
- jid_locale = from_utf8(roster_usr->jid); |
|
433 |
- if (jid_locale) { |
|
434 |
- found = strcasestr(jid_locale, string); |
|
435 |
- g_free(jid_locale); |
|
436 |
+ if (roster_usr->jid) { |
|
437 |
+ found = strcasestr(roster_usr->jid, string); |
|
438 |
if (found) |
|
439 |
return buddy; |
|
440 |
} |
|
441 |
- name_locale = from_utf8(roster_usr->name); |
|
442 |
- if (name_locale) { |
|
443 |
- found = strcasestr(name_locale, string); |
|
444 |
- g_free(name_locale); |
|
445 |
+ if (roster_usr->name) { |
|
446 |
+ found = strcasestr(roster_usr->name, string); |
|
447 |
if (found) |
|
448 |
return buddy; |
|
449 |
} |
64
|
450 |
diff -r 92fa48ef53c9 mcabber/mcabber/utils.c |
|
451 |
--- a/mcabber/mcabber/utils.c Sun Jan 27 00:40:37 2013 +0200 |
66
|
452 |
+++ b/mcabber/mcabber/utils.c Mon Feb 25 00:57:09 2013 +0200 |
|
453 |
@@ -555,6 +555,313 @@ |
64
|
454 |
*str = tolower(*str); |
|
455 |
} |
|
456 |
|
|
457 |
+// in_space -> in_space, in_optstart, in_argstart |
|
458 |
+// in_optstart -> in_shortoptend, in_longoptstart, in_argstart ('-') |
|
459 |
+// in_shortoptend -> in_space, error |
|
460 |
+// in_longoptstart -> in_longopt, in_space, in_argstart ('---') |
|
461 |
+// in_longopt -> in_longopt, in_space, error |
|
462 |
+// in_argstart -> in_arg, success |
|
463 |
+// in_arg -> in_arg, in_space, error |
|
464 |
+ |
|
465 |
+// arguments: rw buffer in utf8, end of buffer pointer, options description struct |
|
466 |
+static const char *cmdopts_parse_internal(gchar *arg, gchar *e, cmdopts_t *options) |
|
467 |
+{ |
|
468 |
+ // parser state |
|
469 |
+ enum { |
|
470 |
+ in_space, |
|
471 |
+ in_optstart, |
|
472 |
+ in_shortoptstart, |
|
473 |
+ in_shortoptend, |
|
474 |
+ in_longoptstart, |
|
475 |
+ in_longopt, |
|
476 |
+ in_argstart, |
|
477 |
+ in_arg, |
|
478 |
+ } state = in_space; |
|
479 |
+ // current pointer, start of object pointer |
|
480 |
+ gchar *p, *s; |
66
|
481 |
+ // inside of quotes in non-plain argument |
64
|
482 |
+ gboolean quotes = FALSE; |
66
|
483 |
+ // non-option argument or end-of-options marker encountered |
64
|
484 |
+ gboolean opts_ended = FALSE; |
|
485 |
+ // option, for which argument is currently parsed |
|
486 |
+ cmdopt_t *option = NULL; |
|
487 |
+ // argument, that we currently parse |
|
488 |
+ cmdarg_t *argument = NULL; |
|
489 |
+ // flags of option/argument |
|
490 |
+ guint flags = 0; |
|
491 |
+ // error message to return |
|
492 |
+ const char *error = NULL; |
|
493 |
+ |
|
494 |
+ p = arg; |
|
495 |
+ // we allow parser to do one extra run on final '\0' |
|
496 |
+ while (p <= e && error == NULL) { |
|
497 |
+ if (state == in_space) { // space between args/options |
|
498 |
+ if (*p == ' ' || *p == '\0') { // still space |
|
499 |
+ p ++; |
|
500 |
+ } else if (*p == '-' && !opts_ended) { // option |
|
501 |
+ state = in_optstart; |
|
502 |
+ p ++; |
|
503 |
+ } else { // argument |
|
504 |
+ if (!option) { |
|
505 |
+ opts_ended = TRUE; |
|
506 |
+ } |
|
507 |
+ s = p; |
|
508 |
+ state = in_argstart; |
|
509 |
+ } |
|
510 |
+ } else if (state == in_optstart) { // long/short option |
|
511 |
+ if (*p == ' ' || *p == '\0') { // argument '-' |
|
512 |
+ opts_ended = TRUE; |
|
513 |
+ s = p - 1; |
|
514 |
+ state = in_argstart; |
|
515 |
+ } else if (*p == '-') { // long option |
|
516 |
+ state = in_longoptstart; |
|
517 |
+ p ++; |
|
518 |
+ } else { // short option |
|
519 |
+ s = p; |
|
520 |
+ state = in_shortoptend; |
|
521 |
+ p ++; |
|
522 |
+ } |
|
523 |
+ } else if (state == in_shortoptend) { // short option |
|
524 |
+ if (*p == ' ' || *p == '\0') { // option really ended |
|
525 |
+ gboolean found = FALSE; |
|
526 |
+ option = options -> opts; |
|
527 |
+ if (option) { |
|
528 |
+ do { |
|
529 |
+ if (option -> shortopt == *s) { |
|
530 |
+ found = TRUE; |
|
531 |
+ break; |
|
532 |
+ } |
|
533 |
+ } while ((!(option++ -> flags & CMDOPT_LAST)) && !found); |
|
534 |
+ } |
|
535 |
+ if (found) { // option is known |
|
536 |
+ if (option -> flags & CMDOPT_SWITCH) { // it is switch |
|
537 |
+ if (option -> flags & CMDOPT_CATCHALL) { |
|
538 |
+ option -> value.swc ++; |
|
539 |
+ } else { |
|
540 |
+ option -> value.swc = !option -> value.swc; |
|
541 |
+ } |
|
542 |
+ option = NULL; |
|
543 |
+ } else { // it is option |
|
544 |
+ if (*p == '\0') { |
|
545 |
+ error = "Short option argument not specified"; |
|
546 |
+ } |
|
547 |
+ } |
|
548 |
+ state = in_space; |
|
549 |
+ p ++; |
|
550 |
+ } else { // option is unknown |
|
551 |
+ error = "Unknown short option"; |
|
552 |
+ } |
|
553 |
+ } else { // short option not ended |
|
554 |
+ error = "Extra characters at short option end"; |
|
555 |
+ } |
|
556 |
+ } else if (state == in_longoptstart) { // long option initialization |
|
557 |
+ if (*p == ' ' || *p == '\0') { // end of options '--' |
|
558 |
+ opts_ended = TRUE; |
|
559 |
+ state = in_space; |
|
560 |
+ p ++; |
|
561 |
+ } else if (*p == '-') { // argument, starting with '---' |
|
562 |
+ opts_ended = TRUE; |
|
563 |
+ s = p - 2; |
|
564 |
+ state = in_argstart; |
|
565 |
+ } else { // it is long option |
|
566 |
+ s = p; |
|
567 |
+ state = in_longopt; |
|
568 |
+ p ++; |
|
569 |
+ } |
|
570 |
+ } else if (state == in_longopt) { // long option name |
|
571 |
+ if (*p == ' ' || *p == '\0') { // long option ended |
|
572 |
+ gboolean found = FALSE; |
|
573 |
+ gboolean eof = *p == '\0'; |
|
574 |
+ *p = '\0'; |
|
575 |
+ option = options -> opts; |
|
576 |
+ if (option) { |
|
577 |
+ do { |
|
578 |
+ if (!g_strcmp0 (option -> longopt, s)) { |
|
579 |
+ found = TRUE; |
|
580 |
+ break; |
|
581 |
+ } |
|
582 |
+ } while ((!(option++ -> flags & CMDOPT_LAST)) && !found); |
|
583 |
+ } |
|
584 |
+ if (found) { // option is known |
|
585 |
+ if (option -> flags & CMDOPT_SWITCH) { // it is switch |
|
586 |
+ if (option -> flags & CMDOPT_CATCHALL) { |
|
587 |
+ option -> value.swc ++; |
|
588 |
+ } else { |
|
589 |
+ option -> value.swc = !option -> value.swc; |
|
590 |
+ } |
|
591 |
+ option = NULL; |
|
592 |
+ } else { // it is option |
|
593 |
+ if (eof) { |
|
594 |
+ error = "Long option argument not specified"; |
|
595 |
+ } |
|
596 |
+ } |
|
597 |
+ state = in_space; |
|
598 |
+ p ++; |
|
599 |
+ } else { // option is unknown |
|
600 |
+ error = "Unknown long option"; |
|
601 |
+ } |
|
602 |
+ } else { // still long option |
|
603 |
+ p ++; |
|
604 |
+ } |
|
605 |
+ } else if (state == in_argstart) { // option/command argument initialization |
|
606 |
+ if (option) { |
|
607 |
+ flags = option -> flags & ~CMDOPT_CATCHALL; // catchall in options indicates multi-options |
|
608 |
+ } else { |
|
609 |
+ if (!argument) { |
|
610 |
+ argument = options -> args; |
|
611 |
+ } |
|
612 |
+ if (!argument) { // no need to parse arguments at all |
|
613 |
+ break; |
|
614 |
+ } |
|
615 |
+ flags = argument -> flags; |
|
616 |
+ if ((flags & CMDOPT_CATCHALL) && (flags & CMDOPT_PLAIN)) { // can finish right away |
|
617 |
+ argument -> value.arg = s; |
|
618 |
+ break; |
|
619 |
+ } |
|
620 |
+ } |
|
621 |
+ quotes = FALSE; |
|
622 |
+ state = in_arg; |
|
623 |
+ } else if (state == in_arg) { // option/command argument value |
|
624 |
+ if (*p == '\0' && quotes) { // end of line in quotes |
|
625 |
+ error = "Unfinished quoted argument"; |
|
626 |
+ } else if ((*p == ' ' && (!quotes) && !(flags & CMDOPT_CATCHALL)) || *p == '\0') { // argument ended |
|
627 |
+ if (*p != '\0') { |
|
628 |
+ *p = '\0'; |
|
629 |
+ p ++; |
|
630 |
+ } |
|
631 |
+ if (option) { // option argument |
|
632 |
+ if (option -> flags & CMDOPT_CATCHALL) { // multi-value option |
|
633 |
+ option -> value.multiopt = g_slist_append (option -> value.multiopt, s); |
|
634 |
+ } else { // single-value option |
|
635 |
+ option -> value.opt = s; |
|
636 |
+ } |
|
637 |
+ option = NULL; |
|
638 |
+ } else { // command argument |
|
639 |
+ if (argument -> flags & CMDOPT_SUBCOMMAND) { |
|
640 |
+ gboolean found = FALSE; |
|
641 |
+ subcmd_t *subcommand = options -> cmds; |
|
642 |
+ if (subcommand) { |
|
643 |
+ do { |
|
644 |
+ if (!g_strcmp0(s, subcommand -> name)) { |
|
645 |
+ found = TRUE; |
|
646 |
+ break; |
|
647 |
+ } |
|
648 |
+ } while (!(subcommand++ -> flags & CMDOPT_LAST)); |
|
649 |
+ } |
|
650 |
+ if (found) { |
|
651 |
+ argument -> value.cmd = subcommand; |
66
|
652 |
+ error = cmdopts_parse_internal(p, e, &(subcommand -> options)); |
64
|
653 |
+ break; |
|
654 |
+ } else { |
|
655 |
+ error = "Unknown subcommand"; |
|
656 |
+ } |
|
657 |
+ } else { |
|
658 |
+ argument -> value.arg = s; |
|
659 |
+ if (argument -> flags & CMDOPT_LAST) { // last argument |
|
660 |
+ break; |
|
661 |
+ } |
|
662 |
+ argument ++; |
|
663 |
+ } |
|
664 |
+ } |
|
665 |
+ state = in_space; |
|
666 |
+ } else if (*p == '\\' && !(flags & CMDOPT_PLAIN)) { // next char escape |
|
667 |
+ memmove(p, p+1, e-(p+1)); |
|
668 |
+ e --; |
|
669 |
+ if (*p == '\0') { |
|
670 |
+ error = "Escape at the end of line"; |
|
671 |
+ } |
|
672 |
+ p ++; |
|
673 |
+ } else if (*p == '"' && !(flags & CMDOPT_PLAIN)) { // quotation start/end |
|
674 |
+ memmove(p, p+1, e-(p+1)); |
|
675 |
+ e --; |
|
676 |
+ quotes = !quotes; |
|
677 |
+ } else { // still argument |
|
678 |
+ p ++; |
|
679 |
+ } |
|
680 |
+ } |
|
681 |
+ } |
|
682 |
+ |
|
683 |
+ // check required flags on options |
|
684 |
+ if (error == NULL && options -> opts) { |
|
685 |
+ option = options -> opts; |
|
686 |
+ do { |
|
687 |
+ if (option -> flags & CMDOPT_REQUIRED) { |
|
688 |
+ if (option -> flags & CMDOPT_SWITCH) { |
|
689 |
+ // no way to check trigger switches, but no point in it as well |
|
690 |
+ if (option -> flags & CMDOPT_CATCHALL && option -> value.swc == 0) { |
|
691 |
+ error = "Required switch is not specified"; |
|
692 |
+ break; |
|
693 |
+ } |
|
694 |
+ } else { |
|
695 |
+ if ((option -> flags & CMDOPT_CATCHALL && option -> value.multiopt == NULL) || |
|
696 |
+ ((!(option -> flags & CMDOPT_CATCHALL)) && option -> value.opt == NULL)) { |
|
697 |
+ error = "Required option is not specified"; |
|
698 |
+ break; |
|
699 |
+ } |
|
700 |
+ } |
|
701 |
+ } |
|
702 |
+ } while (!(option++ -> flags & CMDOPT_LAST)); |
|
703 |
+ } |
|
704 |
+ |
|
705 |
+ // check required flags on arguments |
|
706 |
+ if (error == NULL && options -> args) { |
|
707 |
+ argument = options -> args; |
|
708 |
+ do { |
|
709 |
+ if (argument -> flags & CMDOPT_REQUIRED) { |
65
|
710 |
+ if (argument -> flags & CMDOPT_SUBCOMMAND) { |
|
711 |
+ if (argument -> value.cmd == NULL) { |
|
712 |
+ error = "Subcommand is not specified"; |
|
713 |
+ break; |
|
714 |
+ } |
|
715 |
+ } else { |
|
716 |
+ if (argument -> value.arg == NULL) { |
|
717 |
+ error = "Required argument is not specified"; |
|
718 |
+ break; |
|
719 |
+ } |
64
|
720 |
+ } |
|
721 |
+ } |
|
722 |
+ } while (!(argument++ -> flags & CMDOPT_LAST)); |
|
723 |
+ } |
|
724 |
+ |
|
725 |
+ return error; |
|
726 |
+} |
|
727 |
+ |
|
728 |
+const char *cmdopts_parse(const char *arg, cmdopts_t *options) |
|
729 |
+{ |
|
730 |
+ gchar *utf8 = to_utf8(arg); |
|
731 |
+ gchar *e; |
66
|
732 |
+ const char *error; |
64
|
733 |
+ |
|
734 |
+ for (e = utf8; *e; e++); |
|
735 |
+ options -> freeme = utf8; |
66
|
736 |
+ error = cmdopts_parse_internal(utf8, e, options); |
|
737 |
+ if (error) { |
|
738 |
+ cmdopts_free(options); |
|
739 |
+ } |
|
740 |
+ return error; |
64
|
741 |
+} |
|
742 |
+ |
|
743 |
+void cmdopts_free(cmdopts_t *options) |
|
744 |
+{ |
|
745 |
+ cmdopt_t *option = options -> opts; |
|
746 |
+ subcmd_t *subcommand = options -> cmds; |
|
747 |
+ if (option) { |
|
748 |
+ do { |
|
749 |
+ if ((option -> flags & (CMDOPT_CATCHALL|CMDOPT_SWITCH)) == CMDOPT_CATCHALL) { |
|
750 |
+ g_slist_free(option -> value.multiopt); |
|
751 |
+ option -> value.multiopt = NULL; |
|
752 |
+ } |
|
753 |
+ } while (!(option++ -> flags & CMDOPT_LAST)); |
|
754 |
+ } |
|
755 |
+ if (subcommand) { |
|
756 |
+ do { |
66
|
757 |
+ cmdopts_free(&(subcommand -> options)); |
64
|
758 |
+ } while (!(subcommand++ -> flags & CMDOPT_LAST)); |
|
759 |
+ } |
|
760 |
+ g_free(options -> freeme); |
|
761 |
+ options -> freeme = NULL; |
|
762 |
+} |
|
763 |
+ |
|
764 |
// strip_arg_special_chars(string) |
|
765 |
// Remove quotes and backslashes before an escaped quote |
|
766 |
// Only quotes need a backslash |
|
767 |
diff -r 92fa48ef53c9 mcabber/mcabber/utils.h |
|
768 |
--- a/mcabber/mcabber/utils.h Sun Jan 27 00:40:37 2013 +0200 |
66
|
769 |
+++ b/mcabber/mcabber/utils.h Mon Feb 25 00:57:09 2013 +0200 |
|
770 |
@@ -43,6 +43,101 @@ |
64
|
771 |
char **split_arg(const char *arg, unsigned int n, int dontstriplast); |
|
772 |
void free_arg_lst(char **arglst); |
|
773 |
|
|
774 |
+// error cmdopts_parse (argstring, optionlist) |
|
775 |
+// Function parses command argument string according to provided list of |
|
776 |
+// options and arguments. If in this process it encounters an error, it |
|
777 |
+// returns error string (that should be displayed and g_free'd afterwards). |
|
778 |
+// Note: For now returned error is constant string, that shouldn't be freed, |
|
779 |
+// but we're getting there. |
|
780 |
+// After processing you should free freeme and any GSList values of catchall |
|
781 |
+// options (only lists itself, not values). For your convenience, there is |
|
782 |
+// cmdopts_free(), that does exactly that. |
|
783 |
+// The function recognizes four kinds of expressions: |
|
784 |
+// - Options with arguments in a form '-f bar' or '--foo bar' |
|
785 |
+// - Switches without arguments in a form '-f' or '--foo' |
|
786 |
+// - End-of-options marker '--' |
|
787 |
+// - Individual arguments ('-' and '---' are considered arguments too) |
|
788 |
+// To define command line syntax, you pass cmdopts_t struct, that contains |
|
789 |
+// two contiguous lists of cmdopt_t and cmdarg_t structs accordingly. The |
|
790 |
+// last struct in list must have CMDOPT_LAST flag set. |
|
791 |
+// You can specify your own default values, they will be replaced/appended |
|
792 |
+// if needed. |
|
793 |
+// You can omit specifying longopt or shortopt (put NULL or '\0' there). |
|
794 |
+// Note: returned values and arguments are already converted to utf8. |
|
795 |
+ |
|
796 |
+// Flags: |
|
797 |
+// Only applies to options, defined if option does not have argument. |
65
|
798 |
+#define CMDOPT_SWITCH ( 1<<0 ) |
64
|
799 |
+// Don't process quotes and escapes in argument (applies to option arguments too). |
|
800 |
+#define CMDOPT_PLAIN ( 1<<1 ) |
|
801 |
+// For options - put all encountered values into GSList value.multiopt |
|
802 |
+// instead of overwriting value.opt. |
|
803 |
+// For switches - increment value.swc instead of logical flipping. |
|
804 |
+// For arguments - grab the rest of the line without splitting on spaces. |
|
805 |
+// Implicitly last argument. |
65
|
806 |
+#define CMDOPT_CATCHALL ( 1<<2 ) |
64
|
807 |
+// Option/argument must have value. |
65
|
808 |
+#define CMDOPT_REQUIRED ( 1<<3 ) |
64
|
809 |
+// Last entry in struct sequence. |
65
|
810 |
+#define CMDOPT_LAST ( 1<<4 ) |
64
|
811 |
+// Argument only, argument is the name for subcommand. |
|
812 |
+// Implicitly last argument. |
65
|
813 |
+#define CMDOPT_SUBCOMMAND ( 1<<5 ) |
64
|
814 |
+ |
66
|
815 |
+// FURTHER TODO: |
|
816 |
+// Allow to specify catchall argument in the middle of string (requires some reverse parser)? |
|
817 |
+// Better error messages (caller frees them) |
|
818 |
+// --help generates error with short usage, based on info in options struct |
|
819 |
+ |
64
|
820 |
+// thoughts about future: |
66
|
821 |
+// integration with command structure |
64
|
822 |
+// cmdopt/cmdarg struct contains argument type, that implies completion id and argument correctness checks |
|
823 |
+// cmdopt/cmdarg struct contains default value |
|
824 |
+// when building completion for command, we allow options (if not before --) |
|
825 |
+// |
66
|
826 |
+// so, the process of command execution would look like: |
64
|
827 |
+// - we walk through the options, set default values |
66
|
828 |
+// - we parse argument string, populating options - on this or on next step |
|
829 |
+// - we check for required options availability - we can call generic argument check routine, based on argument type |
64
|
830 |
+// - we call callback |
|
831 |
+// - we free resources |
|
832 |
+typedef struct cmdopts_struct cmdopts_t; |
66
|
833 |
+typedef struct subcmd_struct subcmd_t; |
|
834 |
+typedef union { |
|
835 |
+ GSList *multiopt; |
|
836 |
+ gchar *opt; |
|
837 |
+ guint swc; |
|
838 |
+} cmdopt_value_t; |
64
|
839 |
+typedef struct { |
66
|
840 |
+ guint flags; |
|
841 |
+ char shortopt; |
|
842 |
+ const char *longopt; |
|
843 |
+ cmdopt_value_t value; |
64
|
844 |
+} cmdopt_t; |
66
|
845 |
+typedef union { |
|
846 |
+ gchar *arg; |
|
847 |
+ subcmd_t *cmd; |
|
848 |
+} cmdarg_value_t; |
64
|
849 |
+typedef struct { |
66
|
850 |
+ guint flags; |
|
851 |
+ cmdarg_value_t value; |
64
|
852 |
+} cmdarg_t; |
|
853 |
+struct cmdopts_struct { |
|
854 |
+ cmdopt_t *opts; |
|
855 |
+ cmdarg_t *args; |
|
856 |
+ subcmd_t *cmds; |
66
|
857 |
+ gchar *freeme; // private |
|
858 |
+}; |
|
859 |
+struct subcmd_struct { |
|
860 |
+ guint flags; |
|
861 |
+ const char *name; |
|
862 |
+ cmdopts_t options; |
|
863 |
+ gpointer userdata; // unused, for user convenience |
64
|
864 |
+}; |
|
865 |
+ |
|
866 |
+const char *cmdopts_parse (const char *arg, cmdopts_t *options); |
|
867 |
+void cmdopts_free(cmdopts_t *options); |
|
868 |
+ |
|
869 |
void replace_nl_with_dots(char *bufstr); |
|
870 |
char *ut_expand_tabs(const char *text); |
|
871 |
char *ut_unescape_tabs_cr(const char *text); |