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