client: Implement interactive mode.
[paraslash.git] / client.c
1 /*
2  * Copyright (C) 1997-2011 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 <stdbool.h>
11 #include <signal.h>
12
13 #include "para.h"
14 #include "list.h"
15 #include "sched.h"
16 #include "crypt.h"
17 #include "client.cmdline.h"
18 #include "string.h"
19 #include "stdin.h"
20 #include "stdout.h"
21 #include "client.h"
22 #include "buffer_tree.h"
23 #include "error.h"
24 #include "version.h"
25
26 INIT_CLIENT_ERRLISTS;
27
28 static struct sched sched;
29 static struct client_task *ct;
30 static struct stdin_task sit;
31 static struct stdout_task sot;
32
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;
36
37 #ifdef HAVE_READLINE
38 #include "interactive.h"
39 #include "server_completion.h"
40 #include "afs_completion.h"
41
42 struct exec_task {
43         struct task task;
44         struct btr_node *btrn;
45         char *result_buf;
46         size_t result_size;
47 };
48
49 static void exec_pre_select(struct sched *s, struct task *t)
50 {
51         struct exec_task *et = container_of(t, struct exec_task, task);
52         int ret = btr_node_status(et->btrn, 0, BTR_NT_LEAF);
53
54         if (ret != 0)
55                 sched_min_delay(s);
56 }
57
58 static void exec_post_select(__a_unused struct sched *s, struct task *t)
59 {
60         struct exec_task *et = container_of(t, struct exec_task, task);
61         struct btr_node *btrn = et->btrn;
62         char *buf;
63         size_t sz;
64         int ret;
65
66         ret = btr_node_status(btrn, 0, BTR_NT_LEAF);
67         if (ret <= 0) {
68                 t->error = ret;
69                 return;
70         }
71         sz = btr_next_buffer(btrn, &buf);
72         if (sz <= 1)
73                 goto out;
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';
78 out:
79         btr_consume(btrn, sz);
80 }
81
82 static int make_client_argv(const char *line)
83 {
84         int ret;
85
86         free_argv(ct->conf.inputs);
87         ret = create_argv(line, " ", &ct->conf.inputs);
88         if (ret >= 0)
89                 ct->conf.inputs_num = ret;
90         return ret;
91 }
92
93 static int execute_client_command(const char *cmd, char **result)
94 {
95         int ret;
96         struct sched command_sched = {.default_timeout = {.tv_sec = 1}};
97         struct exec_task exec_task = {
98                 .task = {
99                         .pre_select = exec_pre_select,
100                         .post_select = exec_post_select,
101                         .status = "client exec task",
102                 },
103                 .result_buf = para_strdup(""),
104                 .result_size = 1,
105         };
106         *result = NULL;
107         ret = make_client_argv(cmd);
108         if (ret < 0)
109                 goto out;
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);
114         if (ret < 0)
115                 goto out;
116         schedule(&command_sched);
117         *result = exec_task.result_buf;
118         btr_remove_node(exec_task.btrn);
119         client_disconnect(ct);
120         ret = 1;
121 out:
122         btr_free_node(exec_task.btrn);
123         if (ret < 0)
124                 free(exec_task.result_buf);
125         return ret;
126 }
127
128 static int extract_matches_from_command(const char *word, char *cmd,
129                 char ***matches)
130 {
131         char *buf, **sl;
132         int ret;
133
134         ret = execute_client_command(cmd, &buf);
135         if (ret < 0)
136                 return ret;
137         ret = create_argv(buf, "\n", &sl);
138         free(buf);
139         if (ret < 0)
140                 return ret;
141         ret = i9e_extract_completions(word, sl, matches);
142         free_argv(sl);
143         return ret;
144 }
145
146 static int complete_attributes(const char *word, char ***matches)
147 {
148         return extract_matches_from_command(word, "lsatt", matches);
149 }
150
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)
154 {
155         cr->filename_completion_desired = true;
156 }
157
158 static void generic_blob_complete(const char *blob_type,
159                 struct i9e_completion_info *ci,
160                 struct i9e_completion_result *cr)
161 {
162         char cmd[20];
163         sprintf(cmd, "ls%s", blob_type);
164         extract_matches_from_command(ci->word, cmd, &cr->matches);
165 }
166
167 static void complete_catblob(const char *blob_type,
168                 struct i9e_completion_info *ci,
169                 struct i9e_completion_result *cr)
170 {
171         generic_blob_complete(blob_type, ci, cr);
172 }
173
174 static void complete_lsblob(const char *blob_type,
175                 struct i9e_completion_info *ci,
176                 struct i9e_completion_result *cr)
177 {
178         char *opts[] = {"-i", "-l", "-r", NULL};
179
180         if (ci->word[0] == '-')
181                 return i9e_complete_option(opts, ci, cr);
182         generic_blob_complete(blob_type, ci, cr);
183 }
184
185 static void complete_rmblob(const char *blob_type,
186                 struct i9e_completion_info *ci,
187                 struct i9e_completion_result *cr)
188 {
189         generic_blob_complete(blob_type, ci, cr);
190 }
191
192 static void complete_mvblob(const char *blob_type,
193                 struct i9e_completion_info *ci,
194                 struct i9e_completion_result *cr)
195 {
196         generic_blob_complete(blob_type, ci, cr);
197 }
198
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);
213
214 static struct i9e_completer completers[];
215
216 static void help_completer(struct i9e_completion_info *ci,
217                 struct i9e_completion_result *result)
218 {
219         result->matches = i9e_complete_commands(ci->word, completers);
220 }
221
222 static void stat_completer(struct i9e_completion_info *ci,
223                 struct i9e_completion_result *cr)
224 {
225         char *opts[] = {"-n=", "-p", NULL};
226         //PARA_CRIT_LOG("word: %s\n", ci->word);
227         i9e_complete_option(opts, ci, cr);
228 }
229
230 static void sender_completer(struct i9e_completion_info *ci,
231                 struct i9e_completion_result *cr)
232 {
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};
237         char *sender;
238         char **cmds;
239
240         //PARA_CRIT_LOG("wn: %d\n", ci->word_num);
241         if (ci->word_num == 0 || ci->word_num > 3)
242                 return;
243         if (ci->word_num == 1 || (ci->word_num == 2 && *ci->word != '\0')) {
244                 i9e_extract_completions(ci->word, senders, &cr->matches);
245                 return;
246         }
247         sender = ci->argv[1];
248         //PARA_CRIT_LOG("sender: %s\n", sender);
249         if (strcmp(sender, "http") == 0)
250                 cmds = http_cmds;
251         else if (strcmp(sender, "dccp") == 0)
252                 cmds = dccp_cmds;
253         else if (strcmp(sender, "udp") == 0)
254                 cmds = udp_cmds;
255         else
256                 return;
257         i9e_extract_completions(ci->word, cmds, &cr->matches);
258 }
259
260 static void add_completer(struct i9e_completion_info *ci,
261                 struct i9e_completion_result *cr)
262 {
263         char *opts[] = {"-a", "-l", "-f", "-v", "--", NULL};
264
265         if (ci->word[0] == '-')
266                 i9e_complete_option(opts, ci, cr);
267         cr->filename_completion_desired = true;
268 }
269
270 static void ls_completer(struct i9e_completion_info *ci,
271                 struct i9e_completion_result *cr)
272 {
273         char *opts[] = {
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
277         };
278         if (ci->word[0] == '-')
279                 i9e_complete_option(opts, ci, cr);
280         cr->filename_completion_desired = true;
281 }
282
283 static void setatt_completer(struct i9e_completion_info *ci,
284                 struct i9e_completion_result *cr)
285 {
286         char *buf, **sl;
287         int i, ret, num_atts;
288
289         if (ci->word_num == 0)
290                 return;
291
292         if (*ci->word == '/' || *ci->word == '\0')
293                 cr->filename_completion_desired = true;
294         if (*ci->word == '/')
295                 return;
296         ret = execute_client_command("lsatt", &buf);
297         if (ret < 0)
298                 return;
299         ret = create_argv(buf, "\n", &sl);
300         if (ret < 0)
301                 goto out;
302         num_atts = ret;
303         sl = para_realloc(sl, (2 * num_atts + 1) * sizeof(char *));
304         for (i = 0; i < num_atts; i++) {
305                 char *orig = sl[i];
306                 sl[i] = make_message("%s+", orig);
307                 sl[num_atts + i] = make_message("%s-", orig);
308                 free(orig);
309         }
310         sl[2 * num_atts] = NULL;
311         ret = i9e_extract_completions(ci->word, sl, &cr->matches);
312 out:
313         free(buf);
314         free_argv(sl);
315 }
316
317 static void lsatt_completer(struct i9e_completion_info *ci,
318                 struct i9e_completion_result *cr)
319 {
320         char *opts[] = {"-i", "-l", "-r", NULL};
321
322         if (ci->word[0] == '-')
323                 i9e_complete_option(opts, ci, cr);
324         else
325                 complete_attributes(ci->word, &cr->matches);
326 }
327
328 static void mvatt_completer(struct i9e_completion_info *ci,
329                 struct i9e_completion_result *cr)
330 {
331         complete_attributes(ci->word, &cr->matches);
332 }
333
334 static void rmatt_completer(struct i9e_completion_info *ci,
335                 struct i9e_completion_result *cr)
336 {
337         complete_attributes(ci->word, &cr->matches);
338 }
339
340 static void check_completer(struct i9e_completion_info *ci,
341                 struct i9e_completion_result *cr)
342 {
343         char *opts[] = {"-a", "-m", "-p", NULL};
344         i9e_complete_option(opts, ci, cr);
345 }
346
347 static void rm_completer(struct i9e_completion_info *ci,
348                 struct i9e_completion_result *cr)
349 {
350         char *opts[] = {"-v", "-f", "-p", NULL};
351
352         if (ci->word[0] == '-') {
353                 i9e_complete_option(opts, ci, cr);
354                 return;
355         }
356         cr->filename_completion_desired = true;
357 }
358
359 static void touch_completer(struct i9e_completion_info *ci,
360                 struct i9e_completion_result *cr)
361 {
362         char *opts[] = {"-n=", "-l=", "-y=", "-i=", "-a=", "-v", "-p", NULL};
363
364         if (ci->word[0] == '-')
365                 i9e_complete_option(opts, ci, cr);
366         cr->filename_completion_desired = true;
367 }
368
369 static void cpsi_completer(struct i9e_completion_info *ci,
370                 struct i9e_completion_result *cr)
371 {
372         char *opts[] = {"-a", "-y", "-i", "-l", "-n", "-v", NULL};
373
374         if (ci->word[0] == '-')
375                 i9e_complete_option(opts, ci, cr);
376         cr->filename_completion_desired = true;
377 }
378
379 static void select_completer(struct i9e_completion_info *ci,
380                 struct i9e_completion_result *cr)
381 {
382         char *mood_buf, *pl_buf, **moods, **playlists, **mops;
383         int num_moods, num_pl, i, n, ret;
384
385         ret = execute_client_command("lsmood", &mood_buf);
386         if (ret < 0)
387                 return;
388         ret = execute_client_command("lspl", &pl_buf);
389         if (ret < 0)
390                 goto free_mood_buf;
391
392         ret = create_argv(mood_buf, "\n", &moods);
393         if (ret < 0)
394                 goto free_pl_buf;
395         num_moods = ret;
396         ret = create_argv(pl_buf, "\n", &playlists);
397         if (ret < 0)
398                 goto free_moods;
399         num_pl = ret;
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]);
406         mops[n] = NULL;
407         i9e_extract_completions(ci->word, mops, &cr->matches);
408         free_argv(mops);
409         free_argv(playlists);
410 free_moods:
411         free_argv(moods);
412 free_pl_buf:
413         free(pl_buf);
414 free_mood_buf:
415         free(mood_buf);
416 }
417
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);}
423
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)
444
445 static int client_i9e_line_handler(char *line)
446 {
447         int ret;
448
449         client_disconnect(ct);
450         if (!line || !*line)
451                 return 0;
452         PARA_DEBUG_LOG("line handler: %s\n", line);
453         ret = make_client_argv(line);
454         if (ret < 0)
455                 return ret;
456         ret = client_connect(ct, &sched, NULL, NULL);
457         if (ret < 0)
458                 return ret;
459         i9e_attach_to_stdout(ct->btrn);
460         return 1;
461 }
462
463 static void client_sighandler(int s)
464 {
465         i9e_signal_dispatch(s);
466 }
467
468 static struct i9e_completer completers[] = {
469         SERVER_COMPLETERS
470         AFS_COMPLETERS
471         {.name = NULL}
472 };
473
474 __noreturn static void interactive_session(void)
475 {
476         int ret;
477         char *history_file;
478         struct sigaction act;
479         struct i9e_client_info ici = {
480                 .fds = {0, 1, 2},
481                 .prompt = "para_client> ",
482                 .line_handler = client_i9e_line_handler,
483                 .loglevel = client_loglevel,
484                 .completers = completers,
485         };
486
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);
490         else {
491                 char *home = para_homedir();
492                 history_file = make_message("%s/.paraslash/client.history",
493                         home);
494                 free(home);
495         }
496         ici.history_file = history_file;
497
498         act.sa_handler = client_sighandler;
499         sigemptyset(&act.sa_mask);
500         act.sa_flags = 0;
501         sigaction(SIGINT, &act, NULL);
502         sched.select_function = i9e_select;
503
504         ret = i9e_open(&ici, &sched);
505         if (ret < 0)
506                 goto out;
507         para_log = i9e_log;
508         ret = schedule(&sched);
509         i9e_close();
510         para_log = stderr_log;
511 out:
512         if (ret < 0)
513                 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
514         exit(ret < 0? EXIT_FAILURE : EXIT_SUCCESS);
515         //client_close(ct);
516 }
517
518 __noreturn static void print_completions(void)
519 {
520         int ret = i9e_print_completions(completers);
521         exit(ret <= 0? EXIT_FAILURE : EXIT_SUCCESS);
522 }
523
524 #else /* HAVE_READLINE */
525
526 __noreturn static void interactive_session(void)
527 {
528         PARA_EMERG_LOG("interactive sessions not available\n");
529         exit(EXIT_FAILURE);
530 }
531
532 __noreturn static void print_completions(void)
533 {
534         PARA_EMERG_LOG("command completion not available\n");
535         exit(EXIT_FAILURE);
536 }
537
538 #endif /* HAVE_READLINE */
539
540 static void supervisor_post_select(struct sched *s, struct task *t)
541 {
542         if (ct->task.error < 0) {
543                 t->error = ct->task.error;
544                 return;
545         }
546         if (ct->status == CL_SENDING) {
547                 stdin_set_defaults(&sit);
548                 register_task(s, &sit.task);
549                 t->error = -E_TASK_STARTED;
550                 return;
551         }
552         if (ct->status == CL_RECEIVING) {
553                 stdout_set_defaults(&sot);
554                 register_task(s, &sot.task);
555                 t->error = -E_TASK_STARTED; return;
556         }
557 }
558
559 static struct task svt = {
560         .post_select = supervisor_post_select,
561         .status = "supervisor task"
562 };
563
564 /**
565  * The client program to connect to para_server.
566  *
567  * \param argc Usual argument count.
568  * \param argv Usual argument vector.
569  *
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.
574  *
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.
578  *
579  * \return EXIT_SUCCESS or EXIT_FAILURE
580  *
581  * \sa client_open(), stdin.c, stdout.c, para_client(1), para_server(1)
582  */
583 int main(int argc, char *argv[])
584 {
585         int ret;
586
587         init_random_seed_or_die();
588         sched.default_timeout.tv_sec = 1;
589
590         ret = client_parse_config(argc, argv, &ct, &client_loglevel);
591         if (ret < 0)
592                 goto out;
593         if (ct->conf.complete_given)
594                 print_completions();
595         if (ret == 0)
596                 interactive_session(); /* does not return */
597
598         /*
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.
602          */
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);
606         if (ret < 0)
607                 goto out;
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);
612 out:
613         client_close(ct);
614         btr_free_node(sit.btrn);
615         btr_free_node(sot.btrn);
616         if (ret < 0) {
617                 /* can not use PARA_LOG here because ct is NULL */
618                 fprintf(stderr, "%s\n", para_strerror(-ret));
619                 return EXIT_FAILURE;
620         }
621         return EXIT_SUCCESS;
622 }