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_select(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_select(__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 orignal 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 = {.tv_sec = 1}};
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_select = exec_pre_select,
142 .post_select = exec_post_select,
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);
246 I9E_DUMMY_COMPLETER(tasks);
248 static struct i9e_completer completers[];
250 static void help_completer(struct i9e_completion_info *ci,
251 struct i9e_completion_result *cr)
253 char *opts[] = {LSG_SERVER_CMD_HELP_OPTS, NULL};
255 if (ci->word[0] == '-') {
256 i9e_complete_option(opts, ci, cr);
259 cr->matches = i9e_complete_commands(ci->word, completers);
262 static void stat_completer(struct i9e_completion_info *ci,
263 struct i9e_completion_result *cr)
265 char *opts[] = {LSG_SERVER_CMD_STAT_OPTS, NULL};
266 i9e_complete_option(opts, ci, cr);
269 static void version_completer(struct i9e_completion_info *ci,
270 struct i9e_completion_result *cr)
272 char *opts[] = {LSG_SERVER_CMD_VERSION_OPTS, NULL};
273 i9e_complete_option(opts, ci, cr);
276 static void sender_completer(struct i9e_completion_info *ci,
277 struct i9e_completion_result *cr)
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};
286 //PARA_CRIT_LOG("wn: %d\n", ci->word_num);
287 if (ci->word_num == 0 || ci->word_num > 3)
289 if (ci->word_num == 1 || (ci->word_num == 2 && *ci->word != '\0')) {
290 i9e_extract_completions(ci->word, senders, &cr->matches);
293 sender = ci->argv[1];
294 //PARA_CRIT_LOG("sender: %s\n", sender);
295 if (strcmp(sender, "http") == 0)
297 else if (strcmp(sender, "dccp") == 0)
299 else if (strcmp(sender, "udp") == 0)
303 i9e_extract_completions(ci->word, cmds, &cr->matches);
306 static void add_completer(struct i9e_completion_info *ci,
307 struct i9e_completion_result *cr)
309 char *opts[] = {LSG_SERVER_CMD_ADD_OPTS, NULL};
311 if (ci->word[0] == '-')
312 i9e_complete_option(opts, ci, cr);
313 cr->filename_completion_desired = true;
316 static void ls_completer(struct i9e_completion_info *ci,
317 struct i9e_completion_result *cr)
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;
325 static void setatt_completer(struct i9e_completion_info *ci,
326 struct i9e_completion_result *cr)
329 int i, ret, num_atts;
331 if (ci->word_num == 0)
334 if (*ci->word == '/' || *ci->word == '\0')
335 cr->filename_completion_desired = true;
336 if (*ci->word == '/')
338 ret = execute_client_command("lsatt", &buf);
341 ret = create_argv(buf, "\n", &sl);
345 sl = para_realloc(sl, (2 * num_atts + 1) * sizeof(char *));
346 for (i = 0; i < num_atts; i++) {
348 sl[i] = make_message("%s+", orig);
349 sl[num_atts + i] = make_message("%s-", orig);
352 sl[2 * num_atts] = NULL;
353 i9e_extract_completions(ci->word, sl, &cr->matches);
359 static void lsatt_completer(struct i9e_completion_info *ci,
360 struct i9e_completion_result *cr)
362 char *opts[] = {LSG_SERVER_CMD_LSATT_OPTS, NULL};
363 i9e_complete_option(opts, ci, cr);
366 static void mvatt_completer(struct i9e_completion_info *ci,
367 struct i9e_completion_result *cr)
369 complete_attributes(ci->word, &cr->matches);
372 static void rmatt_completer(struct i9e_completion_info *ci,
373 struct i9e_completion_result *cr)
375 complete_attributes(ci->word, &cr->matches);
378 static void check_completer(struct i9e_completion_info *ci,
379 struct i9e_completion_result *cr)
381 char *opts[] = {LSG_SERVER_CMD_CHECK_OPTS, NULL};
382 i9e_complete_option(opts, ci, cr);
385 static void rm_completer(struct i9e_completion_info *ci,
386 struct i9e_completion_result *cr)
388 char *opts[] = {LSG_SERVER_CMD_RM_OPTS, NULL};
390 if (ci->word[0] == '-') {
391 i9e_complete_option(opts, ci, cr);
394 cr->filename_completion_desired = true;
397 static void touch_completer(struct i9e_completion_info *ci,
398 struct i9e_completion_result *cr)
400 char *opts[] = {LSG_SERVER_CMD_TOUCH_OPTS, NULL};
402 if (ci->word[0] == '-')
403 i9e_complete_option(opts, ci, cr);
404 cr->filename_completion_desired = true;
407 static void cpsi_completer(struct i9e_completion_info *ci,
408 struct i9e_completion_result *cr)
410 char *opts[] = {LSG_SERVER_CMD_CPSI_OPTS, NULL};
412 if (ci->word[0] == '-')
413 i9e_complete_option(opts, ci, cr);
414 cr->filename_completion_desired = true;
417 static void select_completer(struct i9e_completion_info *ci,
418 struct i9e_completion_result *cr)
420 char *mood_buf, *pl_buf, **moods, **playlists, **mops;
421 int num_moods, num_pl, i, n, ret;
423 ret = execute_client_command("lsmood", &mood_buf);
426 ret = execute_client_command("lspl", &pl_buf);
430 ret = create_argv(mood_buf, "\n", &moods);
434 ret = create_argv(pl_buf, "\n", &playlists);
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]);
445 i9e_extract_completions(ci->word, mops, &cr->matches);
447 free_argv(playlists);
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);}
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)
483 static int client_i9e_line_handler(char *line)
486 static bool first = true;
491 lls_free_parse_result(ct->lpr, CLIENT_CMD_PTR);
492 ret = create_merged_lpr(line);
495 ret = client_connect(ct, &sched, NULL, NULL);
498 i9e_attach_to_stdout(ct->btrn[0]);
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
510 __noreturn static void interactive_session(void)
513 struct sigaction act;
514 struct i9e_client_info ici = {
516 .prompt = "para_client> ",
517 .line_handler = client_i9e_line_handler,
518 .loglevel = client_loglevel,
519 .completers = completers,
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));
527 char *home = para_homedir();
528 ici.history_file = make_message("%s/.paraslash/client.history",
532 act.sa_handler = i9e_signal_dispatch;
533 sigemptyset(&act.sa_mask);
535 sigaction(SIGINT, &act, NULL);
536 sched.select_function = i9e_select;
538 ret = i9e_open(&ici, &sched);
542 ret = schedule(&sched);
543 sched_shutdown(&sched);
545 para_log = stderr_log;
548 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
549 exit(ret < 0? EXIT_FAILURE : EXIT_SUCCESS);
553 __noreturn static void print_completions(void)
557 ret = i9e_print_completions(completers);
558 exit(ret <= 0? EXIT_FAILURE : EXIT_SUCCESS);
561 #else /* HAVE_READLINE */
563 __noreturn static void interactive_session(void)
565 PARA_EMERG_LOG("interactive sessions not available\n");
569 __noreturn static void print_completions(void)
571 PARA_EMERG_LOG("command completion not available\n");
575 #endif /* HAVE_READLINE */
577 struct supervisor_task {
578 bool stdout_task_started;
582 static int supervisor_post_select(struct sched *s, void *context)
584 struct supervisor_task *svt = context;
585 int ret = task_status(ct->task);
589 if (!svt->stdout_task_started && ct->status == CL_EXECUTING) {
590 stdout_task_register(&sot, s);
591 svt->stdout_task_started = true;
594 if (ct->status == CL_SENDING) {
595 stdin_task_register(&sit, s);
596 return -E_TASK_STARTED;
601 static struct supervisor_task supervisor_task;
604 * The client program to connect to para_server.
606 * \param argc Usual argument count.
607 * \param argv Usual argument vector.
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.
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.
618 * \return EXIT_SUCCESS or EXIT_FAILURE
620 * \sa \ref client_open(), \ref stdin.c, \ref stdout.c, para_client(1),
623 int main(int argc, char *argv[])
628 sched.default_timeout.tv_sec = 1;
630 ret = client_parse_config(argc, argv, &ct, &client_loglevel);
633 if (CLIENT_OPT_GIVEN(COMPLETE, ct->lpr))
636 interactive_session(); /* does not return */
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.
643 sit.btrn = btr_new_node(&(struct btr_node_description)
644 EMBRACE(.name = "stdin"));
645 ret = client_connect(ct, &sched, sit.btrn, NULL);
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,
656 ret = schedule(&sched);
658 ret = task_status(ct->task);
661 /* these are not errors */
662 case -E_SERVER_CMD_SUCCESS:
668 default: ret = -E_SERVER_CMD_FAILURE;
672 sched_shutdown(&sched);
676 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
678 btr_remove_node(&sit.btrn);
679 btr_remove_node(&sot.btrn);
680 return ret < 0? EXIT_FAILURE : EXIT_SUCCESS;