2 * Copyright (C) 1997-2011 Andre Noll <maan@systemlinux.org>
4 * Licensed under the GPL v2. For licencing details see COPYING.
7 /** \file client.c the client program used to connect to para_server */
17 #include "client.cmdline.h"
22 #include "buffer_tree.h"
28 static struct sched sched;
29 static struct client_task *ct;
30 static struct stdin_task sit;
31 static struct stdout_task sot;
33 static int client_loglevel = LL_ERROR;
34 DEFINE_STDERR_LOGGER(stderr_log, client_loglevel);
35 __printf_2_3 void (*para_log)(int, const char*, ...) = stderr_log;
38 #include "interactive.h"
39 #include "server_completion.h"
40 #include "afs_completion.h"
44 struct btr_node *btrn;
49 static void exec_pre_select(struct sched *s, struct task *t)
51 struct exec_task *et = container_of(t, struct exec_task, task);
52 int ret = btr_node_status(et->btrn, 0, BTR_NT_LEAF);
58 static void exec_post_select(__a_unused struct sched *s, struct task *t)
60 struct exec_task *et = container_of(t, struct exec_task, task);
61 struct btr_node *btrn = et->btrn;
66 ret = btr_node_status(btrn, 0, BTR_NT_LEAF);
71 sz = btr_next_buffer(btrn, &buf);
74 et->result_buf = para_realloc(et->result_buf, et->result_size + sz - 1);
75 memcpy(et->result_buf + et->result_size - 1, buf, sz - 1);
76 et->result_size += sz - 1;
77 et->result_buf[et->result_size - 1] = '\0';
79 btr_consume(btrn, sz);
82 static int make_client_argv(const char *line)
86 free_argv(ct->conf.inputs);
87 ret = create_argv(line, " ", &ct->conf.inputs);
89 ct->conf.inputs_num = ret;
93 static int execute_client_command(const char *cmd, char **result)
96 struct sched command_sched = {.default_timeout = {.tv_sec = 1}};
97 struct exec_task exec_task = {
99 .pre_select = exec_pre_select,
100 .post_select = exec_post_select,
101 .status = "client exec task",
103 .result_buf = para_strdup(""),
107 ret = make_client_argv(cmd);
110 exec_task.btrn = btr_new_node(&(struct btr_node_description)
111 EMBRACE(.name = "exec_collect"));
112 register_task(&command_sched, &exec_task.task);
113 ret = client_connect(ct, &command_sched, NULL, exec_task.btrn);
116 schedule(&command_sched);
117 *result = exec_task.result_buf;
118 btr_remove_node(exec_task.btrn);
119 client_disconnect(ct);
122 btr_free_node(exec_task.btrn);
124 free(exec_task.result_buf);
128 static int extract_matches_from_command(const char *word, char *cmd,
134 ret = execute_client_command(cmd, &buf);
137 ret = create_argv(buf, "\n", &sl);
141 ret = i9e_extract_completions(word, sl, matches);
146 static int complete_attributes(const char *word, char ***matches)
148 return extract_matches_from_command(word, "lsatt", matches);
151 static void complete_addblob(__a_unused const char *blob_type,
152 __a_unused struct i9e_completion_info *ci,
153 __a_unused struct i9e_completion_result *cr)
155 cr->filename_completion_desired = true;
158 static void generic_blob_complete(const char *blob_type,
159 struct i9e_completion_info *ci,
160 struct i9e_completion_result *cr)
163 sprintf(cmd, "ls%s", blob_type);
164 extract_matches_from_command(ci->word, cmd, &cr->matches);
167 static void complete_catblob(const char *blob_type,
168 struct i9e_completion_info *ci,
169 struct i9e_completion_result *cr)
171 generic_blob_complete(blob_type, ci, cr);
174 static void complete_lsblob(const char *blob_type,
175 struct i9e_completion_info *ci,
176 struct i9e_completion_result *cr)
178 char *opts[] = {"-i", "-l", "-r", NULL};
180 if (ci->word[0] == '-')
181 return i9e_complete_option(opts, ci, cr);
182 generic_blob_complete(blob_type, ci, cr);
185 static void complete_rmblob(const char *blob_type,
186 struct i9e_completion_info *ci,
187 struct i9e_completion_result *cr)
189 generic_blob_complete(blob_type, ci, cr);
192 static void complete_mvblob(const char *blob_type,
193 struct i9e_completion_info *ci,
194 struct i9e_completion_result *cr)
196 generic_blob_complete(blob_type, ci, cr);
199 /* these don't need any completions */
200 I9E_DUMMY_COMPLETER(ff);
201 I9E_DUMMY_COMPLETER(hup);
202 I9E_DUMMY_COMPLETER(jmp);
203 I9E_DUMMY_COMPLETER(next);
204 I9E_DUMMY_COMPLETER(nomore);
205 I9E_DUMMY_COMPLETER(pause);
206 I9E_DUMMY_COMPLETER(play);
207 I9E_DUMMY_COMPLETER(si);
208 I9E_DUMMY_COMPLETER(term);
209 I9E_DUMMY_COMPLETER(version);
210 I9E_DUMMY_COMPLETER(stop);
211 I9E_DUMMY_COMPLETER(addatt);
212 I9E_DUMMY_COMPLETER(init);
214 static struct i9e_completer completers[];
216 static void help_completer(struct i9e_completion_info *ci,
217 struct i9e_completion_result *result)
219 result->matches = i9e_complete_commands(ci->word, completers);
222 static void stat_completer(struct i9e_completion_info *ci,
223 struct i9e_completion_result *cr)
225 char *opts[] = {"-n=", "-p", NULL};
226 //PARA_CRIT_LOG("word: %s\n", ci->word);
227 i9e_complete_option(opts, ci, cr);
230 static void sender_completer(struct i9e_completion_info *ci,
231 struct i9e_completion_result *cr)
233 char *senders[] = {"http", "dccp", "udp", NULL};
234 char *http_cmds[] = {"on", "off", "allow", "deny", "help", NULL};
235 char *dccp_cmds[] = {"on", "off", "allow", "deny", "help", NULL};
236 char *udp_cmds[] ={"on", "off", "add", "delete", "help", NULL};
240 //PARA_CRIT_LOG("wn: %d\n", ci->word_num);
241 if (ci->word_num == 0 || ci->word_num > 3)
243 if (ci->word_num == 1 || (ci->word_num == 2 && *ci->word != '\0')) {
244 i9e_extract_completions(ci->word, senders, &cr->matches);
247 sender = ci->argv[1];
248 //PARA_CRIT_LOG("sender: %s\n", sender);
249 if (strcmp(sender, "http") == 0)
251 else if (strcmp(sender, "dccp") == 0)
253 else if (strcmp(sender, "udp") == 0)
257 i9e_extract_completions(ci->word, cmds, &cr->matches);
260 static void add_completer(struct i9e_completion_info *ci,
261 struct i9e_completion_result *cr)
263 char *opts[] = {"-a", "-l", "-f", "-v", "--", NULL};
265 if (ci->word[0] == '-')
266 i9e_complete_option(opts, ci, cr);
267 cr->filename_completion_desired = true;
270 static void ls_completer(struct i9e_completion_info *ci,
271 struct i9e_completion_result *cr)
274 "--", "-l", "-ls", "-ll", "-lv", "-lp", "-lm", "-lc", "-p",
275 "-a", "-r", "-d", "-sp", "-sl", "-ss", "-sn", "-sf", "-sc",
276 "-si", "-sy", "-sb", "-sd", "-sa", NULL
278 if (ci->word[0] == '-')
279 i9e_complete_option(opts, ci, cr);
280 cr->filename_completion_desired = true;
283 static void setatt_completer(struct i9e_completion_info *ci,
284 struct i9e_completion_result *cr)
287 int i, ret, num_atts;
289 if (ci->word_num == 0)
292 if (*ci->word == '/' || *ci->word == '\0')
293 cr->filename_completion_desired = true;
294 if (*ci->word == '/')
296 ret = execute_client_command("lsatt", &buf);
299 ret = create_argv(buf, "\n", &sl);
303 sl = para_realloc(sl, (2 * num_atts + 1) * sizeof(char *));
304 for (i = 0; i < num_atts; i++) {
306 sl[i] = make_message("%s+", orig);
307 sl[num_atts + i] = make_message("%s-", orig);
310 sl[2 * num_atts] = NULL;
311 ret = i9e_extract_completions(ci->word, sl, &cr->matches);
317 static void lsatt_completer(struct i9e_completion_info *ci,
318 struct i9e_completion_result *cr)
320 char *opts[] = {"-i", "-l", "-r", NULL};
322 if (ci->word[0] == '-')
323 i9e_complete_option(opts, ci, cr);
325 complete_attributes(ci->word, &cr->matches);
328 static void mvatt_completer(struct i9e_completion_info *ci,
329 struct i9e_completion_result *cr)
331 complete_attributes(ci->word, &cr->matches);
334 static void rmatt_completer(struct i9e_completion_info *ci,
335 struct i9e_completion_result *cr)
337 complete_attributes(ci->word, &cr->matches);
340 static void check_completer(struct i9e_completion_info *ci,
341 struct i9e_completion_result *cr)
343 char *opts[] = {"-a", "-m", "-p", NULL};
344 i9e_complete_option(opts, ci, cr);
347 static void rm_completer(struct i9e_completion_info *ci,
348 struct i9e_completion_result *cr)
350 char *opts[] = {"-v", "-f", "-p", NULL};
352 if (ci->word[0] == '-') {
353 i9e_complete_option(opts, ci, cr);
356 cr->filename_completion_desired = true;
359 static void touch_completer(struct i9e_completion_info *ci,
360 struct i9e_completion_result *cr)
362 char *opts[] = {"-n=", "-l=", "-y=", "-i=", "-a=", "-v", "-p", NULL};
364 if (ci->word[0] == '-')
365 i9e_complete_option(opts, ci, cr);
366 cr->filename_completion_desired = true;
369 static void cpsi_completer(struct i9e_completion_info *ci,
370 struct i9e_completion_result *cr)
372 char *opts[] = {"-a", "-y", "-i", "-l", "-n", "-v", NULL};
374 if (ci->word[0] == '-')
375 i9e_complete_option(opts, ci, cr);
376 cr->filename_completion_desired = true;
379 static void select_completer(struct i9e_completion_info *ci,
380 struct i9e_completion_result *cr)
382 char *mood_buf, *pl_buf, **moods, **playlists, **mops;
383 int num_moods, num_pl, i, n, ret;
385 ret = execute_client_command("lsmood", &mood_buf);
388 ret = execute_client_command("lspl", &pl_buf);
392 ret = create_argv(mood_buf, "\n", &moods);
396 ret = create_argv(pl_buf, "\n", &playlists);
400 n = num_moods + num_pl;
401 mops = para_malloc((n + 1) * sizeof(char *));
402 for (i = 0; i < num_moods; i++)
403 mops[i] = make_message("m/%s", moods[i]);
404 for (i = 0; i < num_pl; i++)
405 mops[num_moods + i] = make_message("p/%s", playlists[i]);
407 i9e_extract_completions(ci->word, mops, &cr->matches);
409 free_argv(playlists);
418 #define DEFINE_BLOB_COMPLETER(cmd, blob_type) \
419 static void cmd ## blob_type ## _completer( \
420 struct i9e_completion_info *ci, \
421 struct i9e_completion_result *cr) \
422 {complete_ ## cmd ## blob(#blob_type, ci, cr);}
424 DEFINE_BLOB_COMPLETER(add, mood)
425 DEFINE_BLOB_COMPLETER(add, lyr)
426 DEFINE_BLOB_COMPLETER(add, img)
427 DEFINE_BLOB_COMPLETER(add, pl)
428 DEFINE_BLOB_COMPLETER(cat, mood)
429 DEFINE_BLOB_COMPLETER(cat, lyr)
430 DEFINE_BLOB_COMPLETER(cat, img)
431 DEFINE_BLOB_COMPLETER(cat, pl)
432 DEFINE_BLOB_COMPLETER(ls, mood)
433 DEFINE_BLOB_COMPLETER(ls, lyr)
434 DEFINE_BLOB_COMPLETER(ls, img)
435 DEFINE_BLOB_COMPLETER(ls, pl)
436 DEFINE_BLOB_COMPLETER(rm, mood)
437 DEFINE_BLOB_COMPLETER(rm, lyr)
438 DEFINE_BLOB_COMPLETER(rm, img)
439 DEFINE_BLOB_COMPLETER(rm, pl)
440 DEFINE_BLOB_COMPLETER(mv, mood)
441 DEFINE_BLOB_COMPLETER(mv, lyr)
442 DEFINE_BLOB_COMPLETER(mv, img)
443 DEFINE_BLOB_COMPLETER(mv, pl)
445 static int client_i9e_line_handler(char *line)
449 client_disconnect(ct);
452 PARA_DEBUG_LOG("line handler: %s\n", line);
453 ret = make_client_argv(line);
456 ret = client_connect(ct, &sched, NULL, NULL);
459 i9e_attach_to_stdout(ct->btrn);
463 static void client_sighandler(int s)
465 i9e_signal_dispatch(s);
468 static struct i9e_completer completers[] = {
474 __noreturn static void interactive_session(void)
478 struct sigaction act;
479 struct i9e_client_info ici = {
481 .prompt = "para_client> ",
482 .line_handler = client_i9e_line_handler,
483 .loglevel = client_loglevel,
484 .completers = completers,
487 PARA_NOTICE_LOG("\n%s\n", VERSION_TEXT("client"));
488 if (ct->conf.history_file_given)
489 history_file = para_strdup(ct->conf.history_file_arg);
491 char *home = para_homedir();
492 history_file = make_message("%s/.paraslash/client.history",
496 ici.history_file = history_file;
498 act.sa_handler = client_sighandler;
499 sigemptyset(&act.sa_mask);
501 sigaction(SIGINT, &act, NULL);
502 sched.select_function = i9e_select;
504 ret = i9e_open(&ici, &sched);
508 ret = schedule(&sched);
510 para_log = stderr_log;
513 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
514 exit(ret < 0? EXIT_FAILURE : EXIT_SUCCESS);
518 __noreturn static void print_completions(void)
520 int ret = i9e_print_completions(completers);
521 exit(ret <= 0? EXIT_FAILURE : EXIT_SUCCESS);
524 #else /* HAVE_READLINE */
526 __noreturn static void interactive_session(void)
528 PARA_EMERG_LOG("interactive sessions not available\n");
532 __noreturn static void print_completions(void)
534 PARA_EMERG_LOG("command completion not available\n");
538 #endif /* HAVE_READLINE */
540 static void supervisor_post_select(struct sched *s, struct task *t)
542 if (ct->task.error < 0) {
543 t->error = ct->task.error;
546 if (ct->status == CL_SENDING) {
547 stdin_set_defaults(&sit);
548 register_task(s, &sit.task);
549 t->error = -E_TASK_STARTED;
552 if (ct->status == CL_RECEIVING) {
553 stdout_set_defaults(&sot);
554 register_task(s, &sot.task);
555 t->error = -E_TASK_STARTED; return;
559 static struct task svt = {
560 .post_select = supervisor_post_select,
561 .status = "supervisor task"
565 * The client program to connect to para_server.
567 * \param argc Usual argument count.
568 * \param argv Usual argument vector.
570 * When called without a paraslash command, an interactive session is started.
571 * Otherwise, the client task and the supervisor task are started. The former
572 * communicates with para_server while the latter monitors whether the client
573 * task intends to read from stdin or write to stdout.
575 * Once it has been determined whether the client command corresponds to a
576 * stdin command (addmood, addimg, ..), either the stdin task or the stdout
577 * task is set up to replace the supervisor task.
579 * \return EXIT_SUCCESS or EXIT_FAILURE
581 * \sa client_open(), stdin.c, stdout.c, para_client(1), para_server(1)
583 int main(int argc, char *argv[])
587 init_random_seed_or_die();
588 sched.default_timeout.tv_sec = 1;
590 ret = client_parse_config(argc, argv, &ct, &client_loglevel);
593 if (ct->conf.complete_given)
596 interactive_session(); /* does not return */
599 * We add buffer tree nodes for stdin and stdout even though
600 * only one of them will be needed. This simplifies the code
601 * a bit wrt. to the buffer tree setup.
603 sit.btrn = btr_new_node(&(struct btr_node_description)
604 EMBRACE(.name = "stdin"));
605 ret = client_open(argc, argv, &ct, &client_loglevel, sit.btrn, NULL, &sched);
608 sot.btrn = btr_new_node(&(struct btr_node_description)
609 EMBRACE(.name = "stdout", .parent = ct->btrn));
610 register_task(&sched, &svt);
611 ret = schedule(&sched);
614 btr_free_node(sit.btrn);
615 btr_free_node(sot.btrn);
617 /* can not use PARA_LOG here because ct is NULL */
618 fprintf(stderr, "%s\n", para_strerror(-ret));