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