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