1 /* Copyright (C) 1997 Andre Noll <maan@tuebingen.mpg.de>, see file COPYING. */
3 /** \file client.c The client program used to connect to para_server. */
9 #include "client.lsg.h"
18 #include "buffer_tree.h"
22 /** Array of error strings. */
25 static struct sched sched;
26 static struct client_task *ct;
27 static struct stdin_task sit;
28 static struct stdout_task sot;
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;
35 #include "interactive.h"
36 #include "server_cmd.lsg.h"
40 struct btr_node *btrn;
45 static void exec_pre_monitor(struct sched *s, void *context)
47 struct exec_task *et = context;
48 int ret = btr_node_status(et->btrn, 0, BTR_NT_LEAF);
54 static int exec_post_monitor(__a_unused struct sched *s, void *context)
56 struct exec_task *et = context;
57 struct btr_node *btrn = et->btrn;
62 ret = btr_node_status(btrn, 0, BTR_NT_LEAF);
65 sz = btr_next_buffer(btrn, &buf);
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';
73 btr_consume(btrn, sz);
77 /* Called from the line handler and the completers. This overwrites ct->lpr. */
78 static int create_merged_lpr(const char *line)
80 const struct lls_command *cmd = CLIENT_CMD_PTR;
82 char *cmdline, **argv, *errctx;
83 struct lls_parse_result *argv_lpr;
84 static struct lls_parse_result *orig_lpr;
89 cmdline = make_message("-- %s", line);
90 ret = create_shifted_argv(cmdline, " ", &argv);
95 if (argc == 2) { /* no words (only blanks in line) */
99 argv[0] = para_strdup("--");
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 original lpr.
105 ret = lls(lls_parse(argc, argv, cmd, &argv_lpr, &errctx));
109 ret = lls(lls_merge(orig_lpr, argv_lpr, cmd, &ct->lpr, &errctx));
110 lls_free_parse_result(argv_lpr, cmd);
116 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
122 /* called from completers */
123 static int execute_client_command(const char *cmd, char **result)
126 struct sched command_sched = {.default_timeout = 1000};
127 struct exec_task exec_task = {
128 .result_buf = para_strdup(""),
131 struct lls_parse_result *old_lpr = ct->lpr;
134 ret = create_merged_lpr(cmd);
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_monitor = exec_pre_monitor,
142 .post_monitor = exec_post_monitor,
143 .context = &exec_task,
145 ret = client_connect(ct, &command_sched, NULL, exec_task.btrn);
148 schedule(&command_sched);
149 sched_shutdown(&command_sched);
150 lls_free_parse_result(ct->lpr, CLIENT_CMD_PTR);
152 *result = exec_task.result_buf;
153 btr_remove_node(&exec_task.btrn);
156 btr_remove_node(&exec_task.btrn);
158 free(exec_task.result_buf);
162 static int extract_matches_from_command(const char *word, char *cmd,
168 ret = execute_client_command(cmd, &buf);
171 ret = create_argv(buf, "\n", &sl);
175 ret = i9e_extract_completions(word, sl, matches);
180 static int complete_attributes(const char *word, char ***matches)
182 return extract_matches_from_command(word, "lsatt", matches);
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)
189 cr->filename_completion_desired = true;
192 static void generic_blob_complete(const char *blob_type,
193 struct i9e_completion_info *ci,
194 struct i9e_completion_result *cr)
197 sprintf(cmd, "ls%s", blob_type);
198 extract_matches_from_command(ci->word, cmd, &cr->matches);
201 static void complete_catblob(const char *blob_type,
202 struct i9e_completion_info *ci,
203 struct i9e_completion_result *cr)
205 generic_blob_complete(blob_type, ci, cr);
208 static void complete_lsblob(const char *blob_type,
209 struct i9e_completion_info *ci,
210 struct i9e_completion_result *cr)
212 char *opts[] = {LSG_SERVER_CMD_LSIMG_OPTS, NULL};
214 if (ci->word[0] == '-')
215 return i9e_complete_option(opts, ci, cr);
216 generic_blob_complete(blob_type, ci, cr);
219 static void complete_rmblob(const char *blob_type,
220 struct i9e_completion_info *ci,
221 struct i9e_completion_result *cr)
223 generic_blob_complete(blob_type, ci, cr);
226 static void complete_mvblob(const char *blob_type,
227 struct i9e_completion_info *ci,
228 struct i9e_completion_result *cr)
230 generic_blob_complete(blob_type, ci, cr);
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);
247 static struct i9e_completer completers[];
249 static void ll_completer(struct i9e_completion_info *ci,
250 struct i9e_completion_result *cr)
252 i9e_ll_completer(ci, cr);
255 static void help_completer(struct i9e_completion_info *ci,
256 struct i9e_completion_result *cr)
258 char *opts[] = {LSG_SERVER_CMD_HELP_OPTS, NULL};
260 if (ci->word[0] == '-') {
261 i9e_complete_option(opts, ci, cr);
264 cr->matches = i9e_complete_commands(ci->word, completers);
267 static void stat_completer(struct i9e_completion_info *ci,
268 struct i9e_completion_result *cr)
270 char *opts[] = {LSG_SERVER_CMD_STAT_OPTS, NULL};
271 i9e_complete_option(opts, ci, cr);
274 static void version_completer(struct i9e_completion_info *ci,
275 struct i9e_completion_result *cr)
277 char *opts[] = {LSG_SERVER_CMD_VERSION_OPTS, NULL};
278 i9e_complete_option(opts, ci, cr);
281 static void sender_completer(struct i9e_completion_info *ci,
282 struct i9e_completion_result *cr)
284 char *senders[] = {"http", "dccp", "udp", NULL};
285 char *http_cmds[] = {"on", "off", "allow", "deny", "help", NULL};
286 char *dccp_cmds[] = {"on", "off", "allow", "deny", "help", NULL};
287 char *udp_cmds[] ={"on", "off", "add", "delete", "help", NULL};
291 //PARA_CRIT_LOG("wn: %d\n", ci->word_num);
292 if (ci->word_num == 0 || ci->word_num > 3)
294 if (ci->word_num == 1 || (ci->word_num == 2 && *ci->word != '\0')) {
295 i9e_extract_completions(ci->word, senders, &cr->matches);
298 sender = ci->argv[1];
299 //PARA_CRIT_LOG("sender: %s\n", sender);
300 if (strcmp(sender, "http") == 0)
302 else if (strcmp(sender, "dccp") == 0)
304 else if (strcmp(sender, "udp") == 0)
308 i9e_extract_completions(ci->word, cmds, &cr->matches);
311 static void add_completer(struct i9e_completion_info *ci,
312 struct i9e_completion_result *cr)
314 char *opts[] = {LSG_SERVER_CMD_ADD_OPTS, NULL};
316 if (ci->word[0] == '-')
317 i9e_complete_option(opts, ci, cr);
318 cr->filename_completion_desired = true;
321 static void ls_completer(struct i9e_completion_info *ci,
322 struct i9e_completion_result *cr)
324 char *opts[] = {LSG_SERVER_CMD_LS_OPTS, NULL};
325 if (ci->word[0] == '-')
326 i9e_complete_option(opts, ci, cr);
327 cr->filename_completion_desired = true;
330 static void setatt_completer(struct i9e_completion_info *ci,
331 struct i9e_completion_result *cr)
334 int i, ret, num_atts;
336 if (ci->word_num == 0)
339 if (*ci->word == '/' || *ci->word == '\0')
340 cr->filename_completion_desired = true;
341 if (*ci->word == '/')
343 ret = execute_client_command("lsatt", &buf);
346 ret = create_argv(buf, "\n", &sl);
350 sl = arr_realloc(sl, 2 * num_atts + 1, sizeof(char *));
351 for (i = 0; i < num_atts; i++) {
353 sl[i] = make_message("%s+", orig);
354 sl[num_atts + i] = make_message("%s-", orig);
357 sl[2 * num_atts] = NULL;
358 i9e_extract_completions(ci->word, sl, &cr->matches);
364 static void lsatt_completer(struct i9e_completion_info *ci,
365 struct i9e_completion_result *cr)
367 char *opts[] = {LSG_SERVER_CMD_LSATT_OPTS, NULL};
368 i9e_complete_option(opts, ci, cr);
371 static void mvatt_completer(struct i9e_completion_info *ci,
372 struct i9e_completion_result *cr)
374 complete_attributes(ci->word, &cr->matches);
377 static void rmatt_completer(struct i9e_completion_info *ci,
378 struct i9e_completion_result *cr)
380 complete_attributes(ci->word, &cr->matches);
383 static void check_completer(struct i9e_completion_info *ci,
384 struct i9e_completion_result *cr)
386 char *opts[] = {LSG_SERVER_CMD_CHECK_OPTS, NULL};
387 i9e_complete_option(opts, ci, cr);
390 static void rm_completer(struct i9e_completion_info *ci,
391 struct i9e_completion_result *cr)
393 char *opts[] = {LSG_SERVER_CMD_RM_OPTS, NULL};
395 if (ci->word[0] == '-') {
396 i9e_complete_option(opts, ci, cr);
399 cr->filename_completion_desired = true;
402 static void touch_completer(struct i9e_completion_info *ci,
403 struct i9e_completion_result *cr)
405 char *opts[] = {LSG_SERVER_CMD_TOUCH_OPTS, NULL};
407 if (ci->word[0] == '-')
408 i9e_complete_option(opts, ci, cr);
409 cr->filename_completion_desired = true;
412 static void cpsi_completer(struct i9e_completion_info *ci,
413 struct i9e_completion_result *cr)
415 char *opts[] = {LSG_SERVER_CMD_CPSI_OPTS, NULL};
417 if (ci->word[0] == '-')
418 i9e_complete_option(opts, ci, cr);
419 cr->filename_completion_desired = true;
422 static void select_completer(struct i9e_completion_info *ci,
423 struct i9e_completion_result *cr)
425 char *mood_buf, *pl_buf, **moods, **playlists, **mops;
426 int num_moods, num_pl, i, n, ret;
428 ret = execute_client_command("lsmood", &mood_buf);
431 ret = execute_client_command("lspl", &pl_buf);
435 ret = create_argv(mood_buf, "\n", &moods);
439 ret = create_argv(pl_buf, "\n", &playlists);
443 n = num_moods + num_pl;
444 mops = arr_alloc(n + 1, sizeof(char *));
445 for (i = 0; i < num_moods; i++)
446 mops[i] = make_message("m/%s", moods[i]);
447 for (i = 0; i < num_pl; i++)
448 mops[num_moods + i] = make_message("p/%s", playlists[i]);
450 i9e_extract_completions(ci->word, mops, &cr->matches);
452 free_argv(playlists);
461 #define DEFINE_BLOB_COMPLETER(cmd, blob_type) \
462 static void cmd ## blob_type ## _completer( \
463 struct i9e_completion_info *ci, \
464 struct i9e_completion_result *cr) \
465 {complete_ ## cmd ## blob(#blob_type, ci, cr);}
467 DEFINE_BLOB_COMPLETER(add, mood)
468 DEFINE_BLOB_COMPLETER(add, lyr)
469 DEFINE_BLOB_COMPLETER(add, img)
470 DEFINE_BLOB_COMPLETER(add, pl)
471 DEFINE_BLOB_COMPLETER(cat, mood)
472 DEFINE_BLOB_COMPLETER(cat, lyr)
473 DEFINE_BLOB_COMPLETER(cat, img)
474 DEFINE_BLOB_COMPLETER(cat, pl)
475 DEFINE_BLOB_COMPLETER(ls, mood)
476 DEFINE_BLOB_COMPLETER(ls, lyr)
477 DEFINE_BLOB_COMPLETER(ls, img)
478 DEFINE_BLOB_COMPLETER(ls, pl)
479 DEFINE_BLOB_COMPLETER(rm, mood)
480 DEFINE_BLOB_COMPLETER(rm, lyr)
481 DEFINE_BLOB_COMPLETER(rm, img)
482 DEFINE_BLOB_COMPLETER(rm, pl)
483 DEFINE_BLOB_COMPLETER(mv, mood)
484 DEFINE_BLOB_COMPLETER(mv, lyr)
485 DEFINE_BLOB_COMPLETER(mv, img)
486 DEFINE_BLOB_COMPLETER(mv, pl)
488 static int client_i9e_line_handler(char *line)
491 static bool first = true;
496 lls_free_parse_result(ct->lpr, CLIENT_CMD_PTR);
497 ret = create_merged_lpr(line);
500 ret = client_connect(ct, &sched, NULL, NULL);
503 i9e_attach_to_stdout(ct->btrn[0]);
507 static struct i9e_completer completers[] = {
508 #define LSG_SERVER_CMD_CMD(_name) {.name = #_name, \
509 .completer = _name ## _completer}
510 LSG_SERVER_CMD_SUBCOMMANDS
511 #undef LSG_SERVER_CMD_CMD
515 __noreturn static void interactive_session(void)
518 struct sigaction act;
519 struct i9e_client_info ici = {
521 .prompt = "para_client> ",
522 .line_handler = client_i9e_line_handler,
523 .loglevel = client_loglevel,
524 .completers = completers,
527 PARA_NOTICE_LOG("\n%s\n", version_text("client"));
528 if (CLIENT_OPT_GIVEN(HISTORY_FILE, ct->lpr))
529 ici.history_file = para_strdup(CLIENT_OPT_STRING_VAL(
530 HISTORY_FILE, ct->lpr));
532 char *home = para_homedir();
533 ici.history_file = make_message("%s/.paraslash/client.history",
537 act.sa_handler = i9e_signal_dispatch;
538 sigemptyset(&act.sa_mask);
540 sigaction(SIGINT, &act, NULL);
541 sched.poll_function = i9e_poll;
543 ret = i9e_open(&ici, &sched);
547 ret = schedule(&sched);
548 sched_shutdown(&sched);
550 para_log = stderr_log;
553 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
554 exit(ret < 0? EXIT_FAILURE : EXIT_SUCCESS);
558 __noreturn static void print_completions(void)
562 ret = i9e_print_completions(completers);
563 exit(ret <= 0? EXIT_FAILURE : EXIT_SUCCESS);
566 #else /* HAVE_READLINE */
568 __noreturn static void interactive_session(void)
570 PARA_EMERG_LOG("interactive sessions not available\n");
574 __noreturn static void print_completions(void)
576 PARA_EMERG_LOG("command completion not available\n");
580 #endif /* HAVE_READLINE */
582 struct supervisor_task {
583 bool stdout_task_started;
587 static int supervisor_post_monitor(struct sched *s, void *context)
589 struct supervisor_task *svt = context;
590 int ret = task_status(ct->task);
594 if (!svt->stdout_task_started && ct->status == CL_EXECUTING) {
595 stdout_task_register(&sot, s);
596 svt->stdout_task_started = true;
599 if (ct->status == CL_SENDING) {
600 stdin_task_register(&sit, s);
601 return -E_TASK_STARTED;
606 static struct supervisor_task supervisor_task;
609 * The client program to connect to para_server.
611 * \param argc Usual argument count.
612 * \param argv Usual argument vector.
614 * When called without a paraslash command, an interactive session is started.
615 * Otherwise, the client task and the supervisor task are started. The former
616 * communicates with para_server while the latter monitors whether the client
617 * task intends to read from stdin or write to stdout.
619 * Once it has been determined whether the client command corresponds to a
620 * stdin command (addmood, addimg, ..), either the stdin task or the stdout
621 * task is set up to replace the supervisor task.
623 * \return EXIT_SUCCESS or EXIT_FAILURE
625 * \sa \ref client_open(), \ref stdin.c, \ref stdout.c, para_client(1),
628 int main(int argc, char *argv[])
633 sched.default_timeout = 1000;
635 ret = client_parse_config(argc, argv, &ct, &client_loglevel);
638 if (CLIENT_OPT_GIVEN(COMPLETE, ct->lpr))
641 interactive_session(); /* does not return */
644 * We add buffer tree nodes for stdin and stdout even though
645 * only one of them will be needed. This simplifies the code
646 * a bit wrt. to the buffer tree setup.
648 sit.btrn = btr_new_node(&(struct btr_node_description)
649 EMBRACE(.name = "stdin"));
650 ret = client_connect(ct, &sched, sit.btrn, NULL);
653 sot.btrn = btr_new_node(&(struct btr_node_description)
654 EMBRACE(.name = "stdout", .parent = ct->btrn[0]));
655 supervisor_task.task = task_register(&(struct task_info) {
656 .name = "supervisor",
657 .post_monitor = supervisor_post_monitor,
658 .context = &supervisor_task,
661 ret = schedule(&sched);
663 ret = task_status(ct->task);
666 /* these are not errors */
667 case -E_SERVER_CMD_SUCCESS:
671 default: ret = -E_SERVER_CMD_FAILURE;
675 sched_shutdown(&sched);
679 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
681 btr_remove_node(&sit.btrn);
682 btr_remove_node(&sot.btrn);
683 return ret < 0? EXIT_FAILURE : EXIT_SUCCESS;