resample: Simplify initialization().
[paraslash.git] / client.c
1 /*
2  * Copyright (C) 1997 Andre Noll <maan@tuebingen.mpg.de>
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, void *context)
49 {
50         struct exec_task *et = context;
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, void *context)
58 {
59         struct exec_task *et = context;
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                 .result_buf = para_strdup(""),
97                 .result_size = 1,
98         };
99         *result = NULL;
100         ret = make_client_argv(cmd);
101         if (ret < 0)
102                 goto out;
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,
110         }, &command_sched);
111         ret = client_connect(ct, &command_sched, NULL, exec_task.btrn);
112         if (ret < 0)
113                 goto out;
114         schedule(&command_sched);
115         sched_shutdown(&command_sched);
116         *result = exec_task.result_buf;
117         btr_remove_node(&exec_task.btrn);
118         ret = 1;
119 out:
120         btr_remove_node(&exec_task.btrn);
121         if (ret < 0)
122                 free(exec_task.result_buf);
123         return ret;
124 }
125
126 static int extract_matches_from_command(const char *word, char *cmd,
127                 char ***matches)
128 {
129         char *buf, **sl;
130         int ret;
131
132         ret = execute_client_command(cmd, &buf);
133         if (ret < 0)
134                 return ret;
135         ret = create_argv(buf, "\n", &sl);
136         free(buf);
137         if (ret < 0)
138                 return ret;
139         ret = i9e_extract_completions(word, sl, matches);
140         free_argv(sl);
141         return ret;
142 }
143
144 static int complete_attributes(const char *word, char ***matches)
145 {
146         return extract_matches_from_command(word, "lsatt", matches);
147 }
148
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)
152 {
153         cr->filename_completion_desired = true;
154 }
155
156 static void generic_blob_complete(const char *blob_type,
157                 struct i9e_completion_info *ci,
158                 struct i9e_completion_result *cr)
159 {
160         char cmd[20];
161         sprintf(cmd, "ls%s", blob_type);
162         extract_matches_from_command(ci->word, cmd, &cr->matches);
163 }
164
165 static void complete_catblob(const char *blob_type,
166                 struct i9e_completion_info *ci,
167                 struct i9e_completion_result *cr)
168 {
169         generic_blob_complete(blob_type, ci, cr);
170 }
171
172 static void complete_lsblob(const char *blob_type,
173                 struct i9e_completion_info *ci,
174                 struct i9e_completion_result *cr)
175 {
176         char *opts[] = {"-i", "-l", "-r", NULL};
177
178         if (ci->word[0] == '-')
179                 return i9e_complete_option(opts, ci, cr);
180         generic_blob_complete(blob_type, ci, cr);
181 }
182
183 static void complete_rmblob(const char *blob_type,
184                 struct i9e_completion_info *ci,
185                 struct i9e_completion_result *cr)
186 {
187         generic_blob_complete(blob_type, ci, cr);
188 }
189
190 static void complete_mvblob(const char *blob_type,
191                 struct i9e_completion_info *ci,
192                 struct i9e_completion_result *cr)
193 {
194         generic_blob_complete(blob_type, ci, cr);
195 }
196
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);
211
212 static struct i9e_completer completers[];
213
214 static void help_completer(struct i9e_completion_info *ci,
215                 struct i9e_completion_result *result)
216 {
217         result->matches = i9e_complete_commands(ci->word, completers);
218 }
219
220 static void version_completer(struct i9e_completion_info *ci,
221                 struct i9e_completion_result *cr)
222 {
223         char *opts[] = {"-v", NULL};
224         i9e_complete_option(opts, ci, cr);
225 }
226
227 static void stat_completer(struct i9e_completion_info *ci,
228                 struct i9e_completion_result *cr)
229 {
230         char *opts[] = {"-n=", "-p", NULL};
231         //PARA_CRIT_LOG("word: %s\n", ci->word);
232         i9e_complete_option(opts, ci, cr);
233 }
234
235 static void sender_completer(struct i9e_completion_info *ci,
236                 struct i9e_completion_result *cr)
237 {
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};
242         char *sender;
243         char **cmds;
244
245         //PARA_CRIT_LOG("wn: %d\n", ci->word_num);
246         if (ci->word_num == 0 || ci->word_num > 3)
247                 return;
248         if (ci->word_num == 1 || (ci->word_num == 2 && *ci->word != '\0')) {
249                 i9e_extract_completions(ci->word, senders, &cr->matches);
250                 return;
251         }
252         sender = ci->argv[1];
253         //PARA_CRIT_LOG("sender: %s\n", sender);
254         if (strcmp(sender, "http") == 0)
255                 cmds = http_cmds;
256         else if (strcmp(sender, "dccp") == 0)
257                 cmds = dccp_cmds;
258         else if (strcmp(sender, "udp") == 0)
259                 cmds = udp_cmds;
260         else
261                 return;
262         i9e_extract_completions(ci->word, cmds, &cr->matches);
263 }
264
265 static void add_completer(struct i9e_completion_info *ci,
266                 struct i9e_completion_result *cr)
267 {
268         char *opts[] = {"-a", "-l", "-f", "-v", "--", NULL};
269
270         if (ci->word[0] == '-')
271                 i9e_complete_option(opts, ci, cr);
272         cr->filename_completion_desired = true;
273 }
274
275 static void ls_completer(struct i9e_completion_info *ci,
276                 struct i9e_completion_result *cr)
277 {
278         char *opts[] = {
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", "-F", "-b", NULL
282         };
283         if (ci->word[0] == '-')
284                 i9e_complete_option(opts, ci, cr);
285         cr->filename_completion_desired = true;
286 }
287
288 static void setatt_completer(struct i9e_completion_info *ci,
289                 struct i9e_completion_result *cr)
290 {
291         char *buf, **sl;
292         int i, ret, num_atts;
293
294         if (ci->word_num == 0)
295                 return;
296
297         if (*ci->word == '/' || *ci->word == '\0')
298                 cr->filename_completion_desired = true;
299         if (*ci->word == '/')
300                 return;
301         ret = execute_client_command("lsatt", &buf);
302         if (ret < 0)
303                 return;
304         ret = create_argv(buf, "\n", &sl);
305         if (ret < 0)
306                 goto out;
307         num_atts = ret;
308         sl = para_realloc(sl, (2 * num_atts + 1) * sizeof(char *));
309         for (i = 0; i < num_atts; i++) {
310                 char *orig = sl[i];
311                 sl[i] = make_message("%s+", orig);
312                 sl[num_atts + i] = make_message("%s-", orig);
313                 free(orig);
314         }
315         sl[2 * num_atts] = NULL;
316         i9e_extract_completions(ci->word, sl, &cr->matches);
317 out:
318         free(buf);
319         free_argv(sl);
320 }
321
322 static void lsatt_completer(struct i9e_completion_info *ci,
323                 struct i9e_completion_result *cr)
324 {
325         char *opts[] = {"-i", "-l", "-r", NULL};
326         i9e_complete_option(opts, ci, cr);
327 }
328
329 static void mvatt_completer(struct i9e_completion_info *ci,
330                 struct i9e_completion_result *cr)
331 {
332         complete_attributes(ci->word, &cr->matches);
333 }
334
335 static void rmatt_completer(struct i9e_completion_info *ci,
336                 struct i9e_completion_result *cr)
337 {
338         complete_attributes(ci->word, &cr->matches);
339 }
340
341 static void check_completer(struct i9e_completion_info *ci,
342                 struct i9e_completion_result *cr)
343 {
344         char *opts[] = {"-a", "-m", "-p", NULL};
345         i9e_complete_option(opts, ci, cr);
346 }
347
348 static void rm_completer(struct i9e_completion_info *ci,
349                 struct i9e_completion_result *cr)
350 {
351         char *opts[] = {"-v", "-f", "-p", NULL};
352
353         if (ci->word[0] == '-') {
354                 i9e_complete_option(opts, ci, cr);
355                 return;
356         }
357         cr->filename_completion_desired = true;
358 }
359
360 static void touch_completer(struct i9e_completion_info *ci,
361                 struct i9e_completion_result *cr)
362 {
363         char *opts[] = {"-n=", "-l=", "-y=", "-i=", "-a=", "-v", "-p", NULL};
364
365         if (ci->word[0] == '-')
366                 i9e_complete_option(opts, ci, cr);
367         cr->filename_completion_desired = true;
368 }
369
370 static void cpsi_completer(struct i9e_completion_info *ci,
371                 struct i9e_completion_result *cr)
372 {
373         char *opts[] = {"-a", "-y", "-i", "-l", "-n", "-v", NULL};
374
375         if (ci->word[0] == '-')
376                 i9e_complete_option(opts, ci, cr);
377         cr->filename_completion_desired = true;
378 }
379
380 static void select_completer(struct i9e_completion_info *ci,
381                 struct i9e_completion_result *cr)
382 {
383         char *mood_buf, *pl_buf, **moods, **playlists, **mops;
384         int num_moods, num_pl, i, n, ret;
385
386         ret = execute_client_command("lsmood", &mood_buf);
387         if (ret < 0)
388                 return;
389         ret = execute_client_command("lspl", &pl_buf);
390         if (ret < 0)
391                 goto free_mood_buf;
392
393         ret = create_argv(mood_buf, "\n", &moods);
394         if (ret < 0)
395                 goto free_pl_buf;
396         num_moods = ret;
397         ret = create_argv(pl_buf, "\n", &playlists);
398         if (ret < 0)
399                 goto free_moods;
400         num_pl = ret;
401         n = num_moods + num_pl;
402         mops = para_malloc((n + 1) * sizeof(char *));
403         for (i = 0; i < num_moods; i++)
404                 mops[i] = make_message("m/%s", moods[i]);
405         for (i = 0; i < num_pl; i++)
406                 mops[num_moods + i] = make_message("p/%s", playlists[i]);
407         mops[n] = NULL;
408         i9e_extract_completions(ci->word, mops, &cr->matches);
409         free_argv(mops);
410         free_argv(playlists);
411 free_moods:
412         free_argv(moods);
413 free_pl_buf:
414         free(pl_buf);
415 free_mood_buf:
416         free(mood_buf);
417 }
418
419 #define DEFINE_BLOB_COMPLETER(cmd, blob_type) \
420         static void cmd ## blob_type ## _completer( \
421                 struct i9e_completion_info *ci, \
422                 struct i9e_completion_result *cr) \
423         {complete_ ## cmd ## blob(#blob_type, ci, cr);}
424
425 DEFINE_BLOB_COMPLETER(add, mood)
426 DEFINE_BLOB_COMPLETER(add, lyr)
427 DEFINE_BLOB_COMPLETER(add, img)
428 DEFINE_BLOB_COMPLETER(add, pl)
429 DEFINE_BLOB_COMPLETER(cat, mood)
430 DEFINE_BLOB_COMPLETER(cat, lyr)
431 DEFINE_BLOB_COMPLETER(cat, img)
432 DEFINE_BLOB_COMPLETER(cat, pl)
433 DEFINE_BLOB_COMPLETER(ls, mood)
434 DEFINE_BLOB_COMPLETER(ls, lyr)
435 DEFINE_BLOB_COMPLETER(ls, img)
436 DEFINE_BLOB_COMPLETER(ls, pl)
437 DEFINE_BLOB_COMPLETER(rm, mood)
438 DEFINE_BLOB_COMPLETER(rm, lyr)
439 DEFINE_BLOB_COMPLETER(rm, img)
440 DEFINE_BLOB_COMPLETER(rm, pl)
441 DEFINE_BLOB_COMPLETER(mv, mood)
442 DEFINE_BLOB_COMPLETER(mv, lyr)
443 DEFINE_BLOB_COMPLETER(mv, img)
444 DEFINE_BLOB_COMPLETER(mv, pl)
445
446 static int client_i9e_line_handler(char *line)
447 {
448         int ret;
449
450         PARA_DEBUG_LOG("line: %s\n", line);
451         ret = make_client_argv(line);
452         if (ret <= 0)
453                 return ret;
454         ret = client_connect(ct, &sched, NULL, NULL);
455         if (ret < 0)
456                 return ret;
457         i9e_attach_to_stdout(ct->btrn[0]);
458         return 1;
459 }
460
461 static struct i9e_completer completers[] = {
462         SERVER_COMPLETERS
463         AFS_COMPLETERS
464         {.name = NULL}
465 };
466
467 __noreturn static void interactive_session(void)
468 {
469         int ret;
470         char *history_file;
471         struct sigaction act;
472         struct i9e_client_info ici = {
473                 .fds = {0, 1, 2},
474                 .prompt = "para_client> ",
475                 .line_handler = client_i9e_line_handler,
476                 .loglevel = client_loglevel,
477                 .completers = completers,
478         };
479
480         PARA_NOTICE_LOG("\n%s\n", version_text("client"));
481         if (ct->conf.history_file_given)
482                 history_file = para_strdup(ct->conf.history_file_arg);
483         else {
484                 char *home = para_homedir();
485                 history_file = make_message("%s/.paraslash/client.history",
486                         home);
487                 free(home);
488         }
489         ici.history_file = history_file;
490
491         act.sa_handler = i9e_signal_dispatch;
492         sigemptyset(&act.sa_mask);
493         act.sa_flags = 0;
494         sigaction(SIGINT, &act, NULL);
495         sched.select_function = i9e_select;
496
497         ret = i9e_open(&ici, &sched);
498         if (ret < 0)
499                 goto out;
500         para_log = i9e_log;
501         ret = schedule(&sched);
502         sched_shutdown(&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 struct supervisor_task {
535         bool stdout_task_started;
536         struct task *task;
537 };
538
539 static int supervisor_post_select(struct sched *s, void *context)
540 {
541         struct supervisor_task *svt = context;
542         int ret = task_status(ct->task);
543
544         if (ret < 0)
545                 return ret;
546         if (!svt->stdout_task_started && ct->status == CL_EXECUTING) {
547                 stdout_task_register(&sot, s);
548                 svt->stdout_task_started = true;
549                 return 1;
550         }
551         if (ct->status == CL_SENDING) {
552                 stdin_task_register(&sit, s);
553                 return -E_TASK_STARTED;
554         }
555         return 0;
556 }
557
558 static struct supervisor_task supervisor_task;
559
560 /**
561  * The client program to connect to para_server.
562  *
563  * \param argc Usual argument count.
564  * \param argv Usual argument vector.
565  *
566  * When called without a paraslash command, an interactive session is started.
567  * Otherwise, the client task and the supervisor task are started. The former
568  * communicates with para_server while the latter monitors whether the client
569  * task intends to read from stdin or write to stdout.
570  *
571  * Once it has been determined whether the client command corresponds to a
572  * stdin command (addmood, addimg, ..), either the stdin task or the stdout
573  * task is set up to replace the supervisor task.
574  *
575  * \return EXIT_SUCCESS or EXIT_FAILURE
576  *
577  * \sa client_open(), stdin.c, stdout.c, para_client(1), para_server(1)
578  */
579 int main(int argc, char *argv[])
580 {
581         int ret;
582
583         init_random_seed_or_die();
584         sched.default_timeout.tv_sec = 1;
585
586         ret = client_parse_config(argc, argv, &ct, &client_loglevel);
587         if (ret < 0)
588                 goto out;
589         if (ct->conf.complete_given)
590                 print_completions();
591         if (ret == 0)
592                 interactive_session(); /* does not return */
593
594         /*
595          * We add buffer tree nodes for stdin and stdout even though
596          * only one of them will be needed. This simplifies the code
597          * a bit wrt. to the buffer tree setup.
598          */
599         sit.btrn = btr_new_node(&(struct btr_node_description)
600                 EMBRACE(.name = "stdin"));
601         ret = client_connect(ct, &sched, sit.btrn, NULL);
602         if (ret < 0)
603                 goto out;
604         sot.btrn = btr_new_node(&(struct btr_node_description)
605                 EMBRACE(.name = "stdout", .parent = ct->btrn[0]));
606         supervisor_task.task = task_register(&(struct task_info) {
607                 .name = "supervisor",
608                 .post_select = supervisor_post_select,
609                 .context = &supervisor_task,
610         }, &sched);
611
612         ret = schedule(&sched);
613         if (ret >= 0) {
614                 ret = task_status(ct->task);
615                 if (ret < 0) {
616                         switch (ret) {
617                         /* these are not errors */
618                         case -E_SERVER_CMD_SUCCESS:
619                         case -E_EOF:
620                         case -E_SERVER_EOF:
621                         case -E_BTR_EOF:
622                                 ret = 0;
623                                 break;
624                         default: ret = -E_SERVER_CMD_FAILURE;
625                         }
626                 }
627         }
628         sched_shutdown(&sched);
629 out:
630         if (ret < 0)
631                 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
632         client_close(ct);
633         btr_remove_node(&sit.btrn);
634         btr_remove_node(&sot.btrn);
635         return ret < 0? EXIT_FAILURE : EXIT_SUCCESS;
636 }