2 * Copyright (C) 1997 Andre Noll <maan@tuebingen.mpg.de>
4 * Licensed under the GPL v2. For licencing details see COPYING.
7 /** \file client.c The client program used to connect to para_server. */
16 #include "client.cmdline.h"
21 #include "buffer_tree.h"
27 static struct sched sched;
28 static struct client_task *ct;
29 static struct stdin_task sit;
30 static struct stdout_task sot;
32 static int client_loglevel = LL_ERROR;
33 DEFINE_STDERR_LOGGER(stderr_log, client_loglevel);
34 __printf_2_3 void (*para_log)(int, const char*, ...) = stderr_log;
37 #include "interactive.h"
38 #include "server.completion.h"
39 #include "afs.completion.h"
43 struct btr_node *btrn;
48 static void exec_pre_select(struct sched *s, void *context)
50 struct exec_task *et = context;
51 int ret = btr_node_status(et->btrn, 0, BTR_NT_LEAF);
57 static int exec_post_select(__a_unused struct sched *s, void *context)
59 struct exec_task *et = context;
60 struct btr_node *btrn = et->btrn;
65 ret = btr_node_status(btrn, 0, BTR_NT_LEAF);
68 sz = btr_next_buffer(btrn, &buf);
71 et->result_buf = para_realloc(et->result_buf, et->result_size + sz - 1);
72 memcpy(et->result_buf + et->result_size - 1, buf, sz - 1);
73 et->result_size += sz - 1;
74 et->result_buf[et->result_size - 1] = '\0';
76 btr_consume(btrn, sz);
80 static int make_client_argv(const char *line)
84 free_argv(ct->conf.inputs);
85 ret = create_argv(line, " ", &ct->conf.inputs);
87 ct->conf.inputs_num = ret;
91 static int execute_client_command(const char *cmd, char **result)
94 struct sched command_sched = {.default_timeout = {.tv_sec = 1}};
95 struct exec_task exec_task = {
96 .result_buf = para_strdup(""),
100 ret = make_client_argv(cmd);
103 exec_task.btrn = btr_new_node(&(struct btr_node_description)
104 EMBRACE(.name = "exec_collect"));
105 exec_task.task = task_register(&(struct task_info) {
106 .name = "client exec",
107 .pre_select = exec_pre_select,
108 .post_select = exec_post_select,
109 .context = &exec_task,
111 ret = client_connect(ct, &command_sched, NULL, exec_task.btrn);
114 schedule(&command_sched);
115 sched_shutdown(&command_sched);
116 *result = exec_task.result_buf;
117 btr_remove_node(&exec_task.btrn);
120 btr_remove_node(&exec_task.btrn);
122 free(exec_task.result_buf);
126 static int extract_matches_from_command(const char *word, char *cmd,
132 ret = execute_client_command(cmd, &buf);
135 ret = create_argv(buf, "\n", &sl);
139 ret = i9e_extract_completions(word, sl, matches);
144 static int complete_attributes(const char *word, char ***matches)
146 return extract_matches_from_command(word, "lsatt", matches);
149 static void complete_addblob(__a_unused const char *blob_type,
150 __a_unused struct i9e_completion_info *ci,
151 __a_unused struct i9e_completion_result *cr)
153 cr->filename_completion_desired = true;
156 static void generic_blob_complete(const char *blob_type,
157 struct i9e_completion_info *ci,
158 struct i9e_completion_result *cr)
161 sprintf(cmd, "ls%s", blob_type);
162 extract_matches_from_command(ci->word, cmd, &cr->matches);
165 static void complete_catblob(const char *blob_type,
166 struct i9e_completion_info *ci,
167 struct i9e_completion_result *cr)
169 generic_blob_complete(blob_type, ci, cr);
172 static void complete_lsblob(const char *blob_type,
173 struct i9e_completion_info *ci,
174 struct i9e_completion_result *cr)
176 char *opts[] = {"-i", "-l", "-r", NULL};
178 if (ci->word[0] == '-')
179 return i9e_complete_option(opts, ci, cr);
180 generic_blob_complete(blob_type, ci, cr);
183 static void complete_rmblob(const char *blob_type,
184 struct i9e_completion_info *ci,
185 struct i9e_completion_result *cr)
187 generic_blob_complete(blob_type, ci, cr);
190 static void complete_mvblob(const char *blob_type,
191 struct i9e_completion_info *ci,
192 struct i9e_completion_result *cr)
194 generic_blob_complete(blob_type, ci, cr);
197 /* these don't need any completions */
198 I9E_DUMMY_COMPLETER(ff);
199 I9E_DUMMY_COMPLETER(hup);
200 I9E_DUMMY_COMPLETER(jmp);
201 I9E_DUMMY_COMPLETER(next);
202 I9E_DUMMY_COMPLETER(nomore);
203 I9E_DUMMY_COMPLETER(pause);
204 I9E_DUMMY_COMPLETER(play);
205 I9E_DUMMY_COMPLETER(si);
206 I9E_DUMMY_COMPLETER(term);
207 I9E_DUMMY_COMPLETER(stop);
208 I9E_DUMMY_COMPLETER(addatt);
209 I9E_DUMMY_COMPLETER(init);
210 I9E_DUMMY_COMPLETER(tasks);
212 static struct i9e_completer completers[];
214 static void help_completer(struct i9e_completion_info *ci,
215 struct i9e_completion_result *result)
217 result->matches = i9e_complete_commands(ci->word, completers);
220 static void version_completer(struct i9e_completion_info *ci,
221 struct i9e_completion_result *cr)
223 char *opts[] = {"-v", NULL};
224 i9e_complete_option(opts, ci, cr);
227 static void stat_completer(struct i9e_completion_info *ci,
228 struct i9e_completion_result *cr)
230 char *opts[] = {"-n=", "-p", NULL};
231 //PARA_CRIT_LOG("word: %s\n", ci->word);
232 i9e_complete_option(opts, ci, cr);
235 static void sender_completer(struct i9e_completion_info *ci,
236 struct i9e_completion_result *cr)
238 char *senders[] = {"http", "dccp", "udp", NULL};
239 char *http_cmds[] = {"on", "off", "allow", "deny", "help", NULL};
240 char *dccp_cmds[] = {"on", "off", "allow", "deny", "help", NULL};
241 char *udp_cmds[] ={"on", "off", "add", "delete", "help", NULL};
245 //PARA_CRIT_LOG("wn: %d\n", ci->word_num);
246 if (ci->word_num == 0 || ci->word_num > 3)
248 if (ci->word_num == 1 || (ci->word_num == 2 && *ci->word != '\0')) {
249 i9e_extract_completions(ci->word, senders, &cr->matches);
252 sender = ci->argv[1];
253 //PARA_CRIT_LOG("sender: %s\n", sender);
254 if (strcmp(sender, "http") == 0)
256 else if (strcmp(sender, "dccp") == 0)
258 else if (strcmp(sender, "udp") == 0)
262 i9e_extract_completions(ci->word, cmds, &cr->matches);
265 static void add_completer(struct i9e_completion_info *ci,
266 struct i9e_completion_result *cr)
268 char *opts[] = {"-a", "-l", "-f", "-v", "--", NULL};
270 if (ci->word[0] == '-')
271 i9e_complete_option(opts, ci, cr);
272 cr->filename_completion_desired = true;
275 static void ls_completer(struct i9e_completion_info *ci,
276 struct i9e_completion_result *cr)
279 "--", "-l", "-l=s", "-l=l", "-l=v", "-l=p", "-l=m", "-l=c",
280 "-p", "-a", "-r", "-d", "-s=p", "-s=l", "-s=s", "-s=n", "-s=f",
281 "-s=c", "-s=i", "-s=y", "-s=b", "-s=d", "-s=a", NULL
283 if (ci->word[0] == '-')
284 i9e_complete_option(opts, ci, cr);
285 cr->filename_completion_desired = true;
288 static void setatt_completer(struct i9e_completion_info *ci,
289 struct i9e_completion_result *cr)
292 int i, ret, num_atts;
294 if (ci->word_num == 0)
297 if (*ci->word == '/' || *ci->word == '\0')
298 cr->filename_completion_desired = true;
299 if (*ci->word == '/')
301 ret = execute_client_command("lsatt", &buf);
304 ret = create_argv(buf, "\n", &sl);
308 sl = para_realloc(sl, (2 * num_atts + 1) * sizeof(char *));
309 for (i = 0; i < num_atts; i++) {
311 sl[i] = make_message("%s+", orig);
312 sl[num_atts + i] = make_message("%s-", orig);
315 sl[2 * num_atts] = NULL;
316 i9e_extract_completions(ci->word, sl, &cr->matches);
322 static void lsatt_completer(struct i9e_completion_info *ci,
323 struct i9e_completion_result *cr)
325 char *opts[] = {"-i", "-l", "-r", NULL};
327 if (ci->word[0] == '-')
328 i9e_complete_option(opts, ci, cr);
330 complete_attributes(ci->word, &cr->matches);
333 static void mvatt_completer(struct i9e_completion_info *ci,
334 struct i9e_completion_result *cr)
336 complete_attributes(ci->word, &cr->matches);
339 static void rmatt_completer(struct i9e_completion_info *ci,
340 struct i9e_completion_result *cr)
342 complete_attributes(ci->word, &cr->matches);
345 static void check_completer(struct i9e_completion_info *ci,
346 struct i9e_completion_result *cr)
348 char *opts[] = {"-a", "-m", "-p", NULL};
349 i9e_complete_option(opts, ci, cr);
352 static void rm_completer(struct i9e_completion_info *ci,
353 struct i9e_completion_result *cr)
355 char *opts[] = {"-v", "-f", "-p", NULL};
357 if (ci->word[0] == '-') {
358 i9e_complete_option(opts, ci, cr);
361 cr->filename_completion_desired = true;
364 static void touch_completer(struct i9e_completion_info *ci,
365 struct i9e_completion_result *cr)
367 char *opts[] = {"-n=", "-l=", "-y=", "-i=", "-a=", "-v", "-p", NULL};
369 if (ci->word[0] == '-')
370 i9e_complete_option(opts, ci, cr);
371 cr->filename_completion_desired = true;
374 static void cpsi_completer(struct i9e_completion_info *ci,
375 struct i9e_completion_result *cr)
377 char *opts[] = {"-a", "-y", "-i", "-l", "-n", "-v", NULL};
379 if (ci->word[0] == '-')
380 i9e_complete_option(opts, ci, cr);
381 cr->filename_completion_desired = true;
384 static void select_completer(struct i9e_completion_info *ci,
385 struct i9e_completion_result *cr)
387 char *mood_buf, *pl_buf, **moods, **playlists, **mops;
388 int num_moods, num_pl, i, n, ret;
390 ret = execute_client_command("lsmood", &mood_buf);
393 ret = execute_client_command("lspl", &pl_buf);
397 ret = create_argv(mood_buf, "\n", &moods);
401 ret = create_argv(pl_buf, "\n", &playlists);
405 n = num_moods + num_pl;
406 mops = para_malloc((n + 1) * sizeof(char *));
407 for (i = 0; i < num_moods; i++)
408 mops[i] = make_message("m/%s", moods[i]);
409 for (i = 0; i < num_pl; i++)
410 mops[num_moods + i] = make_message("p/%s", playlists[i]);
412 i9e_extract_completions(ci->word, mops, &cr->matches);
414 free_argv(playlists);
423 #define DEFINE_BLOB_COMPLETER(cmd, blob_type) \
424 static void cmd ## blob_type ## _completer( \
425 struct i9e_completion_info *ci, \
426 struct i9e_completion_result *cr) \
427 {complete_ ## cmd ## blob(#blob_type, ci, cr);}
429 DEFINE_BLOB_COMPLETER(add, mood)
430 DEFINE_BLOB_COMPLETER(add, lyr)
431 DEFINE_BLOB_COMPLETER(add, img)
432 DEFINE_BLOB_COMPLETER(add, pl)
433 DEFINE_BLOB_COMPLETER(cat, mood)
434 DEFINE_BLOB_COMPLETER(cat, lyr)
435 DEFINE_BLOB_COMPLETER(cat, img)
436 DEFINE_BLOB_COMPLETER(cat, pl)
437 DEFINE_BLOB_COMPLETER(ls, mood)
438 DEFINE_BLOB_COMPLETER(ls, lyr)
439 DEFINE_BLOB_COMPLETER(ls, img)
440 DEFINE_BLOB_COMPLETER(ls, pl)
441 DEFINE_BLOB_COMPLETER(rm, mood)
442 DEFINE_BLOB_COMPLETER(rm, lyr)
443 DEFINE_BLOB_COMPLETER(rm, img)
444 DEFINE_BLOB_COMPLETER(rm, pl)
445 DEFINE_BLOB_COMPLETER(mv, mood)
446 DEFINE_BLOB_COMPLETER(mv, lyr)
447 DEFINE_BLOB_COMPLETER(mv, img)
448 DEFINE_BLOB_COMPLETER(mv, pl)
450 static int client_i9e_line_handler(char *line)
454 PARA_DEBUG_LOG("line: %s\n", line);
455 ret = make_client_argv(line);
458 ret = client_connect(ct, &sched, NULL, NULL);
461 i9e_attach_to_stdout(ct->btrn[0]);
465 static struct i9e_completer completers[] = {
471 __noreturn static void interactive_session(void)
475 struct sigaction act;
476 struct i9e_client_info ici = {
478 .prompt = "para_client> ",
479 .line_handler = client_i9e_line_handler,
480 .loglevel = client_loglevel,
481 .completers = completers,
484 PARA_NOTICE_LOG("\n%s\n", version_text("client"));
485 if (ct->conf.history_file_given)
486 history_file = para_strdup(ct->conf.history_file_arg);
488 char *home = para_homedir();
489 history_file = make_message("%s/.paraslash/client.history",
493 ici.history_file = history_file;
495 act.sa_handler = i9e_signal_dispatch;
496 sigemptyset(&act.sa_mask);
498 sigaction(SIGINT, &act, NULL);
499 sched.select_function = i9e_select;
501 ret = i9e_open(&ici, &sched);
505 ret = schedule(&sched);
506 sched_shutdown(&sched);
508 para_log = stderr_log;
511 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
512 exit(ret < 0? EXIT_FAILURE : EXIT_SUCCESS);
516 __noreturn static void print_completions(void)
518 int ret = i9e_print_completions(completers);
519 exit(ret <= 0? EXIT_FAILURE : EXIT_SUCCESS);
522 #else /* HAVE_READLINE */
524 __noreturn static void interactive_session(void)
526 PARA_EMERG_LOG("interactive sessions not available\n");
530 __noreturn static void print_completions(void)
532 PARA_EMERG_LOG("command completion not available\n");
536 #endif /* HAVE_READLINE */
538 struct supervisor_task {
539 bool stdout_task_started;
543 static int supervisor_post_select(struct sched *s, void *context)
545 struct supervisor_task *svt = context;
546 int ret = task_status(ct->task);
550 if (!svt->stdout_task_started && ct->status == CL_EXECUTING) {
551 stdout_task_register(&sot, s);
552 svt->stdout_task_started = true;
555 if (ct->status == CL_SENDING) {
556 stdin_task_register(&sit, s);
557 return -E_TASK_STARTED;
562 static struct supervisor_task 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_connect(ct, &sched, sit.btrn, NULL);
608 sot.btrn = btr_new_node(&(struct btr_node_description)
609 EMBRACE(.name = "stdout", .parent = ct->btrn[0]));
610 supervisor_task.task = task_register(&(struct task_info) {
611 .name = "supervisor",
612 .post_select = supervisor_post_select,
613 .context = &supervisor_task,
616 ret = schedule(&sched);
618 ret = task_status(ct->task);
621 /* these are not errors */
622 case -E_SERVER_CMD_SUCCESS:
628 default: ret = -E_SERVER_CMD_FAILURE;
632 sched_shutdown(&sched);
635 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
637 btr_remove_node(&sit.btrn);
638 btr_remove_node(&sot.btrn);
639 return ret < 0? EXIT_FAILURE : EXIT_SUCCESS;