filter: Use lsu_merge_config_file_options().
[paraslash.git] / client.c
1 /* Copyright (C) 1997 Andre Noll <maan@tuebingen.mpg.de>, see file COPYING. */
2
3 /** \file client.c The client program used to connect to para_server. */
4
5 #include <regex.h>
6 #include <signal.h>
7 #include <lopsub.h>
8
9 #include "client.lsg.h"
10 #include "para.h"
11 #include "list.h"
12 #include "sched.h"
13 #include "crypt.h"
14 #include "string.h"
15 #include "stdin.h"
16 #include "stdout.h"
17 #include "client.h"
18 #include "buffer_tree.h"
19 #include "error.h"
20 #include "version.h"
21
22 /** Array of error strings. */
23 DEFINE_PARA_ERRLIST;
24
25 static struct sched sched;
26 static struct client_task *ct;
27 static struct stdin_task sit;
28 static struct stdout_task sot;
29
30 static int client_loglevel = LL_ERROR;
31 DEFINE_STDERR_LOGGER(stderr_log, client_loglevel);
32 __printf_2_3 void (*para_log)(int, const char*, ...) = stderr_log;
33
34 #ifdef HAVE_READLINE
35 #include "interactive.h"
36 #include "server_cmd.lsg.h"
37
38 struct exec_task {
39         struct task *task;
40         struct btr_node *btrn;
41         char *result_buf;
42         size_t result_size;
43 };
44
45 static void exec_pre_select(struct sched *s, void *context)
46 {
47         struct exec_task *et = context;
48         int ret = btr_node_status(et->btrn, 0, BTR_NT_LEAF);
49
50         if (ret != 0)
51                 sched_min_delay(s);
52 }
53
54 static int exec_post_select(__a_unused struct sched *s, void *context)
55 {
56         struct exec_task *et = context;
57         struct btr_node *btrn = et->btrn;
58         char *buf;
59         size_t sz;
60         int ret;
61
62         ret = btr_node_status(btrn, 0, BTR_NT_LEAF);
63         if (ret <= 0)
64                 return ret;
65         sz = btr_next_buffer(btrn, &buf);
66         if (sz <= 1)
67                 goto out;
68         et->result_buf = para_realloc(et->result_buf, et->result_size + sz - 1);
69         memcpy(et->result_buf + et->result_size - 1, buf, sz -  1);
70         et->result_size += sz - 1;
71         et->result_buf[et->result_size - 1] = '\0';
72 out:
73         btr_consume(btrn, sz);
74         return 0;
75 }
76
77 /* Called from the line handler and the completers. This overwrites ct->lpr. */
78 static int create_merged_lpr(const char *line)
79 {
80         const struct lls_command *cmd = CLIENT_CMD_PTR;
81         int argc, ret;
82         char *cmdline, **argv, *errctx;
83         struct lls_parse_result *argv_lpr;
84         static struct lls_parse_result *orig_lpr;
85
86         if (!orig_lpr)
87                 orig_lpr = ct->lpr;
88         ct->lpr = NULL;
89         cmdline = make_message("-- %s", line);
90         ret = create_shifted_argv(cmdline, " ", &argv);
91         free(cmdline);
92         if (ret < 0)
93                 return ret;
94         argc = ret;
95         if (argc == 2) { /* no words (only blanks in line) */
96                 free_argv(argv);
97                 return 0;
98         }
99         argv[0] = para_strdup("--");
100         /*
101          * The original lpr for the interactive session has no non-option
102          * arguments. We create a fresh lpr from the words in "line" and merge
103          * it with the orignal lpr.
104          */
105         ret = lls(lls_parse(argc, argv, cmd, &argv_lpr, &errctx));
106         free_argv(argv);
107         if (ret < 0)
108                 goto fail;
109         ret = lls(lls_merge(orig_lpr, argv_lpr, cmd, &ct->lpr, &errctx));
110         lls_free_parse_result(argv_lpr, cmd);
111         if (ret < 0)
112                 goto fail;
113         return 1;
114 fail:
115         if (errctx)
116                 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
117         free(errctx);
118         assert(ret < 0);
119         return ret;
120 }
121
122 /* called from completers */
123 static int execute_client_command(const char *cmd, char **result)
124 {
125         int ret;
126         struct sched command_sched = {.default_timeout = {.tv_sec = 1}};
127         struct exec_task exec_task = {
128                 .result_buf = para_strdup(""),
129                 .result_size = 1,
130         };
131         struct lls_parse_result *old_lpr = ct->lpr;
132
133         *result = NULL;
134         ret = create_merged_lpr(cmd);
135         if (ret <= 0)
136                 goto out;
137         exec_task.btrn = btr_new_node(&(struct btr_node_description)
138                 EMBRACE(.name = "exec_collect"));
139         exec_task.task = task_register(&(struct task_info) {
140                 .name = "client exec",
141                 .pre_select = exec_pre_select,
142                 .post_select = exec_post_select,
143                 .context = &exec_task,
144         }, &command_sched);
145         ret = client_connect(ct, &command_sched, NULL, exec_task.btrn);
146         if (ret < 0)
147                 goto out;
148         schedule(&command_sched);
149         sched_shutdown(&command_sched);
150         lls_free_parse_result(ct->lpr, CLIENT_CMD_PTR);
151         ct->lpr = old_lpr;
152         *result = exec_task.result_buf;
153         btr_remove_node(&exec_task.btrn);
154         ret = 1;
155 out:
156         btr_remove_node(&exec_task.btrn);
157         if (ret < 0)
158                 free(exec_task.result_buf);
159         return ret;
160 }
161
162 static int extract_matches_from_command(const char *word, char *cmd,
163                 char ***matches)
164 {
165         char *buf, **sl;
166         int ret;
167
168         ret = execute_client_command(cmd, &buf);
169         if (ret < 0)
170                 return ret;
171         ret = create_argv(buf, "\n", &sl);
172         free(buf);
173         if (ret < 0)
174                 return ret;
175         ret = i9e_extract_completions(word, sl, matches);
176         free_argv(sl);
177         return ret;
178 }
179
180 static int complete_attributes(const char *word, char ***matches)
181 {
182         return extract_matches_from_command(word, "lsatt", matches);
183 }
184
185 static void complete_addblob(__a_unused const char *blob_type,
186                 __a_unused struct i9e_completion_info *ci,
187                 __a_unused struct i9e_completion_result *cr)
188 {
189         cr->filename_completion_desired = true;
190 }
191
192 static void generic_blob_complete(const char *blob_type,
193                 struct i9e_completion_info *ci,
194                 struct i9e_completion_result *cr)
195 {
196         char cmd[20];
197         sprintf(cmd, "ls%s", blob_type);
198         extract_matches_from_command(ci->word, cmd, &cr->matches);
199 }
200
201 static void complete_catblob(const char *blob_type,
202                 struct i9e_completion_info *ci,
203                 struct i9e_completion_result *cr)
204 {
205         generic_blob_complete(blob_type, ci, cr);
206 }
207
208 static void complete_lsblob(const char *blob_type,
209                 struct i9e_completion_info *ci,
210                 struct i9e_completion_result *cr)
211 {
212         char *opts[] = {LSG_SERVER_CMD_LSIMG_OPTS, NULL};
213
214         if (ci->word[0] == '-')
215                 return i9e_complete_option(opts, ci, cr);
216         generic_blob_complete(blob_type, ci, cr);
217 }
218
219 static void complete_rmblob(const char *blob_type,
220                 struct i9e_completion_info *ci,
221                 struct i9e_completion_result *cr)
222 {
223         generic_blob_complete(blob_type, ci, cr);
224 }
225
226 static void complete_mvblob(const char *blob_type,
227                 struct i9e_completion_info *ci,
228                 struct i9e_completion_result *cr)
229 {
230         generic_blob_complete(blob_type, ci, cr);
231 }
232
233 /* these don't need any completions */
234 I9E_DUMMY_COMPLETER(ff);
235 I9E_DUMMY_COMPLETER(hup);
236 I9E_DUMMY_COMPLETER(jmp);
237 I9E_DUMMY_COMPLETER(next);
238 I9E_DUMMY_COMPLETER(nomore);
239 I9E_DUMMY_COMPLETER(pause);
240 I9E_DUMMY_COMPLETER(play);
241 I9E_DUMMY_COMPLETER(si);
242 I9E_DUMMY_COMPLETER(term);
243 I9E_DUMMY_COMPLETER(stop);
244 I9E_DUMMY_COMPLETER(addatt);
245 I9E_DUMMY_COMPLETER(init);
246 I9E_DUMMY_COMPLETER(tasks);
247
248 static struct i9e_completer completers[];
249
250 static void help_completer(struct i9e_completion_info *ci,
251                 struct i9e_completion_result *cr)
252 {
253         char *opts[] = {LSG_SERVER_CMD_HELP_OPTS, NULL};
254
255         if (ci->word[0] == '-') {
256                 i9e_complete_option(opts, ci, cr);
257                 return;
258         }
259         cr->matches = i9e_complete_commands(ci->word, completers);
260 }
261
262 static void stat_completer(struct i9e_completion_info *ci,
263                 struct i9e_completion_result *cr)
264 {
265         char *opts[] = {LSG_SERVER_CMD_STAT_OPTS, NULL};
266         i9e_complete_option(opts, ci, cr);
267 }
268
269 static void version_completer(struct i9e_completion_info *ci,
270                 struct i9e_completion_result *cr)
271 {
272         char *opts[] = {LSG_SERVER_CMD_VERSION_OPTS, NULL};
273         i9e_complete_option(opts, ci, cr);
274 }
275
276 static void sender_completer(struct i9e_completion_info *ci,
277                 struct i9e_completion_result *cr)
278 {
279         char *senders[] = {"http", "dccp", "udp", NULL};
280         char *http_cmds[] = {"on", "off", "allow", "deny", "help", NULL};
281         char *dccp_cmds[] = {"on", "off", "allow", "deny", "help", NULL};
282         char *udp_cmds[] ={"on", "off", "add", "delete", "help", NULL};
283         char *sender;
284         char **cmds;
285
286         //PARA_CRIT_LOG("wn: %d\n", ci->word_num);
287         if (ci->word_num == 0 || ci->word_num > 3)
288                 return;
289         if (ci->word_num == 1 || (ci->word_num == 2 && *ci->word != '\0')) {
290                 i9e_extract_completions(ci->word, senders, &cr->matches);
291                 return;
292         }
293         sender = ci->argv[1];
294         //PARA_CRIT_LOG("sender: %s\n", sender);
295         if (strcmp(sender, "http") == 0)
296                 cmds = http_cmds;
297         else if (strcmp(sender, "dccp") == 0)
298                 cmds = dccp_cmds;
299         else if (strcmp(sender, "udp") == 0)
300                 cmds = udp_cmds;
301         else
302                 return;
303         i9e_extract_completions(ci->word, cmds, &cr->matches);
304 }
305
306 static void add_completer(struct i9e_completion_info *ci,
307                 struct i9e_completion_result *cr)
308 {
309         char *opts[] = {LSG_SERVER_CMD_ADD_OPTS, NULL};
310
311         if (ci->word[0] == '-')
312                 i9e_complete_option(opts, ci, cr);
313         cr->filename_completion_desired = true;
314 }
315
316 static void ls_completer(struct i9e_completion_info *ci,
317                 struct i9e_completion_result *cr)
318 {
319         char *opts[] = {LSG_SERVER_CMD_LS_OPTS, NULL};
320         if (ci->word[0] == '-')
321                 i9e_complete_option(opts, ci, cr);
322         cr->filename_completion_desired = true;
323 }
324
325 static void setatt_completer(struct i9e_completion_info *ci,
326                 struct i9e_completion_result *cr)
327 {
328         char *buf, **sl;
329         int i, ret, num_atts;
330
331         if (ci->word_num == 0)
332                 return;
333
334         if (*ci->word == '/' || *ci->word == '\0')
335                 cr->filename_completion_desired = true;
336         if (*ci->word == '/')
337                 return;
338         ret = execute_client_command("lsatt", &buf);
339         if (ret < 0)
340                 return;
341         ret = create_argv(buf, "\n", &sl);
342         if (ret < 0)
343                 goto out;
344         num_atts = ret;
345         sl = para_realloc(sl, (2 * num_atts + 1) * sizeof(char *));
346         for (i = 0; i < num_atts; i++) {
347                 char *orig = sl[i];
348                 sl[i] = make_message("%s+", orig);
349                 sl[num_atts + i] = make_message("%s-", orig);
350                 free(orig);
351         }
352         sl[2 * num_atts] = NULL;
353         i9e_extract_completions(ci->word, sl, &cr->matches);
354 out:
355         free(buf);
356         free_argv(sl);
357 }
358
359 static void lsatt_completer(struct i9e_completion_info *ci,
360                 struct i9e_completion_result *cr)
361 {
362         char *opts[] = {LSG_SERVER_CMD_LSATT_OPTS, NULL};
363         i9e_complete_option(opts, ci, cr);
364 }
365
366 static void mvatt_completer(struct i9e_completion_info *ci,
367                 struct i9e_completion_result *cr)
368 {
369         complete_attributes(ci->word, &cr->matches);
370 }
371
372 static void rmatt_completer(struct i9e_completion_info *ci,
373                 struct i9e_completion_result *cr)
374 {
375         complete_attributes(ci->word, &cr->matches);
376 }
377
378 static void check_completer(struct i9e_completion_info *ci,
379                 struct i9e_completion_result *cr)
380 {
381         char *opts[] = {LSG_SERVER_CMD_CHECK_OPTS, NULL};
382         i9e_complete_option(opts, ci, cr);
383 }
384
385 static void rm_completer(struct i9e_completion_info *ci,
386                 struct i9e_completion_result *cr)
387 {
388         char *opts[] = {LSG_SERVER_CMD_RM_OPTS, NULL};
389
390         if (ci->word[0] == '-') {
391                 i9e_complete_option(opts, ci, cr);
392                 return;
393         }
394         cr->filename_completion_desired = true;
395 }
396
397 static void touch_completer(struct i9e_completion_info *ci,
398                 struct i9e_completion_result *cr)
399 {
400         char *opts[] = {LSG_SERVER_CMD_TOUCH_OPTS, NULL};
401
402         if (ci->word[0] == '-')
403                 i9e_complete_option(opts, ci, cr);
404         cr->filename_completion_desired = true;
405 }
406
407 static void cpsi_completer(struct i9e_completion_info *ci,
408                 struct i9e_completion_result *cr)
409 {
410         char *opts[] = {LSG_SERVER_CMD_CPSI_OPTS, NULL};
411
412         if (ci->word[0] == '-')
413                 i9e_complete_option(opts, ci, cr);
414         cr->filename_completion_desired = true;
415 }
416
417 static void select_completer(struct i9e_completion_info *ci,
418                 struct i9e_completion_result *cr)
419 {
420         char *mood_buf, *pl_buf, **moods, **playlists, **mops;
421         int num_moods, num_pl, i, n, ret;
422
423         ret = execute_client_command("lsmood", &mood_buf);
424         if (ret < 0)
425                 return;
426         ret = execute_client_command("lspl", &pl_buf);
427         if (ret < 0)
428                 goto free_mood_buf;
429
430         ret = create_argv(mood_buf, "\n", &moods);
431         if (ret < 0)
432                 goto free_pl_buf;
433         num_moods = ret;
434         ret = create_argv(pl_buf, "\n", &playlists);
435         if (ret < 0)
436                 goto free_moods;
437         num_pl = ret;
438         n = num_moods + num_pl;
439         mops = para_malloc((n + 1) * sizeof(char *));
440         for (i = 0; i < num_moods; i++)
441                 mops[i] = make_message("m/%s", moods[i]);
442         for (i = 0; i < num_pl; i++)
443                 mops[num_moods + i] = make_message("p/%s", playlists[i]);
444         mops[n] = NULL;
445         i9e_extract_completions(ci->word, mops, &cr->matches);
446         free_argv(mops);
447         free_argv(playlists);
448 free_moods:
449         free_argv(moods);
450 free_pl_buf:
451         free(pl_buf);
452 free_mood_buf:
453         free(mood_buf);
454 }
455
456 #define DEFINE_BLOB_COMPLETER(cmd, blob_type) \
457         static void cmd ## blob_type ## _completer( \
458                 struct i9e_completion_info *ci, \
459                 struct i9e_completion_result *cr) \
460         {complete_ ## cmd ## blob(#blob_type, ci, cr);}
461
462 DEFINE_BLOB_COMPLETER(add, mood)
463 DEFINE_BLOB_COMPLETER(add, lyr)
464 DEFINE_BLOB_COMPLETER(add, img)
465 DEFINE_BLOB_COMPLETER(add, pl)
466 DEFINE_BLOB_COMPLETER(cat, mood)
467 DEFINE_BLOB_COMPLETER(cat, lyr)
468 DEFINE_BLOB_COMPLETER(cat, img)
469 DEFINE_BLOB_COMPLETER(cat, pl)
470 DEFINE_BLOB_COMPLETER(ls, mood)
471 DEFINE_BLOB_COMPLETER(ls, lyr)
472 DEFINE_BLOB_COMPLETER(ls, img)
473 DEFINE_BLOB_COMPLETER(ls, pl)
474 DEFINE_BLOB_COMPLETER(rm, mood)
475 DEFINE_BLOB_COMPLETER(rm, lyr)
476 DEFINE_BLOB_COMPLETER(rm, img)
477 DEFINE_BLOB_COMPLETER(rm, pl)
478 DEFINE_BLOB_COMPLETER(mv, mood)
479 DEFINE_BLOB_COMPLETER(mv, lyr)
480 DEFINE_BLOB_COMPLETER(mv, img)
481 DEFINE_BLOB_COMPLETER(mv, pl)
482
483 static int client_i9e_line_handler(char *line)
484 {
485         int ret;
486         static bool first = true;
487
488         if (first)
489                 first = false;
490         else
491                 lls_free_parse_result(ct->lpr, CLIENT_CMD_PTR);
492         ret = create_merged_lpr(line);
493         if (ret <= 0)
494                 return ret;
495         ret = client_connect(ct, &sched, NULL, NULL);
496         if (ret < 0)
497                 return ret;
498         i9e_attach_to_stdout(ct->btrn[0]);
499         return 1;
500 }
501
502 static struct i9e_completer completers[] = {
503 #define LSG_SERVER_CMD_CMD(_name) {.name = #_name, \
504         .completer = _name ## _completer}
505         LSG_SERVER_CMD_SUBCOMMANDS
506 #undef LSG_SERVER_CMD_CMD
507         {.name = NULL}
508 };
509
510 __noreturn static void interactive_session(void)
511 {
512         int ret;
513         struct sigaction act;
514         struct i9e_client_info ici = {
515                 .fds = {0, 1, 2},
516                 .prompt = "para_client> ",
517                 .line_handler = client_i9e_line_handler,
518                 .loglevel = client_loglevel,
519                 .completers = completers,
520         };
521
522         PARA_NOTICE_LOG("\n%s\n", version_text("client"));
523         if (CLIENT_OPT_GIVEN(HISTORY_FILE, ct->lpr))
524                 ici.history_file = para_strdup(CLIENT_OPT_STRING_VAL(
525                         HISTORY_FILE, ct->lpr));
526         else {
527                 char *home = para_homedir();
528                 ici.history_file = make_message("%s/.paraslash/client.history",
529                         home);
530                 free(home);
531         }
532         act.sa_handler = i9e_signal_dispatch;
533         sigemptyset(&act.sa_mask);
534         act.sa_flags = 0;
535         sigaction(SIGINT, &act, NULL);
536         sched.select_function = i9e_select;
537
538         ret = i9e_open(&ici, &sched);
539         if (ret < 0)
540                 goto out;
541         para_log = i9e_log;
542         ret = schedule(&sched);
543         sched_shutdown(&sched);
544         i9e_close();
545         para_log = stderr_log;
546 out:
547         if (ret < 0)
548                 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
549         exit(ret < 0? EXIT_FAILURE : EXIT_SUCCESS);
550         //client_close(ct);
551 }
552
553 __noreturn static void print_completions(void)
554 {
555         int ret;
556
557         ret = i9e_print_completions(completers);
558         exit(ret <= 0? EXIT_FAILURE : EXIT_SUCCESS);
559 }
560
561 #else /* HAVE_READLINE */
562
563 __noreturn static void interactive_session(void)
564 {
565         PARA_EMERG_LOG("interactive sessions not available\n");
566         exit(EXIT_FAILURE);
567 }
568
569 __noreturn static void print_completions(void)
570 {
571         PARA_EMERG_LOG("command completion not available\n");
572         exit(EXIT_FAILURE);
573 }
574
575 #endif /* HAVE_READLINE */
576
577 struct supervisor_task {
578         bool stdout_task_started;
579         struct task *task;
580 };
581
582 static int supervisor_post_select(struct sched *s, void *context)
583 {
584         struct supervisor_task *svt = context;
585         int ret = task_status(ct->task);
586
587         if (ret < 0)
588                 return ret;
589         if (!svt->stdout_task_started && ct->status == CL_EXECUTING) {
590                 stdout_task_register(&sot, s);
591                 svt->stdout_task_started = true;
592                 return 1;
593         }
594         if (ct->status == CL_SENDING) {
595                 stdin_task_register(&sit, s);
596                 return -E_TASK_STARTED;
597         }
598         return 0;
599 }
600
601 static struct supervisor_task supervisor_task;
602
603 /**
604  * The client program to connect to para_server.
605  *
606  * \param argc Usual argument count.
607  * \param argv Usual argument vector.
608  *
609  * When called without a paraslash command, an interactive session is started.
610  * Otherwise, the client task and the supervisor task are started. The former
611  * communicates with para_server while the latter monitors whether the client
612  * task intends to read from stdin or write to stdout.
613  *
614  * Once it has been determined whether the client command corresponds to a
615  * stdin command (addmood, addimg, ..), either the stdin task or the stdout
616  * task is set up to replace the supervisor task.
617  *
618  * \return EXIT_SUCCESS or EXIT_FAILURE
619  *
620  * \sa \ref client_open(), \ref stdin.c, \ref stdout.c, para_client(1),
621  * para_server(1).
622  */
623 int main(int argc, char *argv[])
624 {
625         int ret;
626
627         init_random_seed_or_die();
628         sched.default_timeout.tv_sec = 1;
629
630         ret = client_parse_config(argc, argv, &ct, &client_loglevel);
631         if (ret < 0)
632                 goto out;
633         if (CLIENT_OPT_GIVEN(COMPLETE, ct->lpr))
634                 print_completions();
635         if (ret == 0)
636                 interactive_session(); /* does not return */
637
638         /*
639          * We add buffer tree nodes for stdin and stdout even though
640          * only one of them will be needed. This simplifies the code
641          * a bit wrt. to the buffer tree setup.
642          */
643         sit.btrn = btr_new_node(&(struct btr_node_description)
644                 EMBRACE(.name = "stdin"));
645         ret = client_connect(ct, &sched, sit.btrn, NULL);
646         if (ret < 0)
647                 goto out;
648         sot.btrn = btr_new_node(&(struct btr_node_description)
649                 EMBRACE(.name = "stdout", .parent = ct->btrn[0]));
650         supervisor_task.task = task_register(&(struct task_info) {
651                 .name = "supervisor",
652                 .post_select = supervisor_post_select,
653                 .context = &supervisor_task,
654         }, &sched);
655
656         ret = schedule(&sched);
657         if (ret >= 0) {
658                 ret = task_status(ct->task);
659                 if (ret < 0) {
660                         switch (ret) {
661                         /* these are not errors */
662                         case -E_SERVER_CMD_SUCCESS:
663                         case -E_EOF:
664                         case -E_SERVER_EOF:
665                         case -E_BTR_EOF:
666                                 ret = 0;
667                                 break;
668                         default: ret = -E_SERVER_CMD_FAILURE;
669                         }
670                 }
671         }
672         sched_shutdown(&sched);
673 out:
674         if (ret < 0)
675                 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
676         client_close(ct);
677         btr_remove_node(&sit.btrn);
678         btr_remove_node(&sot.btrn);
679         return ret < 0? EXIT_FAILURE : EXIT_SUCCESS;
680 }