server: Convert non-afs commands to lopsub.
[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 /** Array of error strings. */
26 DEFINE_PARA_ERRLIST;
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 "afs.completion.h"
40 #include "server_cmd.lsg.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, void *context)
50 {
51 struct exec_task *et = context;
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 int exec_post_select(__a_unused struct sched *s, void *context)
59 {
60 struct exec_task *et = context;
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 return ret;
69 sz = btr_next_buffer(btrn, &buf);
70 if (sz <= 1)
71 goto out;
72 et->result_buf = para_realloc(et->result_buf, et->result_size + sz - 1);
73 memcpy(et->result_buf + et->result_size - 1, buf, sz - 1);
74 et->result_size += sz - 1;
75 et->result_buf[et->result_size - 1] = '\0';
76 out:
77 btr_consume(btrn, sz);
78 return 0;
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 .result_buf = para_strdup(""),
98 .result_size = 1,
99 };
100 *result = NULL;
101 ret = make_client_argv(cmd);
102 if (ret < 0)
103 goto out;
104 exec_task.btrn = btr_new_node(&(struct btr_node_description)
105 EMBRACE(.name = "exec_collect"));
106 exec_task.task = task_register(&(struct task_info) {
107 .name = "client exec",
108 .pre_select = exec_pre_select,
109 .post_select = exec_post_select,
110 .context = &exec_task,
111 }, &command_sched);
112 ret = client_connect(ct, &command_sched, NULL, exec_task.btrn);
113 if (ret < 0)
114 goto out;
115 schedule(&command_sched);
116 sched_shutdown(&command_sched);
117 *result = exec_task.result_buf;
118 btr_remove_node(&exec_task.btrn);
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(stop);
209 I9E_DUMMY_COMPLETER(addatt);
210 I9E_DUMMY_COMPLETER(init);
211 I9E_DUMMY_COMPLETER(tasks);
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 version_completer(struct i9e_completion_info *ci,
222 struct i9e_completion_result *cr)
223 {
224 char *opts[] = {"-v", NULL};
225 i9e_complete_option(opts, ci, cr);
226 }
227
228 static void stat_completer(struct i9e_completion_info *ci,
229 struct i9e_completion_result *cr)
230 {
231 char *opts[] = {"-n=", "-p", NULL};
232 //PARA_CRIT_LOG("word: %s\n", ci->word);
233 i9e_complete_option(opts, ci, cr);
234 }
235
236 static void sender_completer(struct i9e_completion_info *ci,
237 struct i9e_completion_result *cr)
238 {
239 char *senders[] = {"http", "dccp", "udp", NULL};
240 char *http_cmds[] = {"on", "off", "allow", "deny", "help", NULL};
241 char *dccp_cmds[] = {"on", "off", "allow", "deny", "help", NULL};
242 char *udp_cmds[] ={"on", "off", "add", "delete", "help", NULL};
243 char *sender;
244 char **cmds;
245
246 //PARA_CRIT_LOG("wn: %d\n", ci->word_num);
247 if (ci->word_num == 0 || ci->word_num > 3)
248 return;
249 if (ci->word_num == 1 || (ci->word_num == 2 && *ci->word != '\0')) {
250 i9e_extract_completions(ci->word, senders, &cr->matches);
251 return;
252 }
253 sender = ci->argv[1];
254 //PARA_CRIT_LOG("sender: %s\n", sender);
255 if (strcmp(sender, "http") == 0)
256 cmds = http_cmds;
257 else if (strcmp(sender, "dccp") == 0)
258 cmds = dccp_cmds;
259 else if (strcmp(sender, "udp") == 0)
260 cmds = udp_cmds;
261 else
262 return;
263 i9e_extract_completions(ci->word, cmds, &cr->matches);
264 }
265
266 static void add_completer(struct i9e_completion_info *ci,
267 struct i9e_completion_result *cr)
268 {
269 char *opts[] = {"-a", "-l", "-f", "-v", "--", NULL};
270
271 if (ci->word[0] == '-')
272 i9e_complete_option(opts, ci, cr);
273 cr->filename_completion_desired = true;
274 }
275
276 static void ls_completer(struct i9e_completion_info *ci,
277 struct i9e_completion_result *cr)
278 {
279 char *opts[] = {
280 "--", "-l", "-l=s", "-l=l", "-l=v", "-l=p", "-l=m", "-l=c",
281 "-p", "-a", "-r", "-d", "-s=p", "-s=l", "-s=s", "-s=n", "-s=f",
282 "-s=c", "-s=i", "-s=y", "-s=b", "-s=d", "-s=a", "-F", "-b", NULL
283 };
284 if (ci->word[0] == '-')
285 i9e_complete_option(opts, ci, cr);
286 cr->filename_completion_desired = true;
287 }
288
289 static void setatt_completer(struct i9e_completion_info *ci,
290 struct i9e_completion_result *cr)
291 {
292 char *buf, **sl;
293 int i, ret, num_atts;
294
295 if (ci->word_num == 0)
296 return;
297
298 if (*ci->word == '/' || *ci->word == '\0')
299 cr->filename_completion_desired = true;
300 if (*ci->word == '/')
301 return;
302 ret = execute_client_command("lsatt", &buf);
303 if (ret < 0)
304 return;
305 ret = create_argv(buf, "\n", &sl);
306 if (ret < 0)
307 goto out;
308 num_atts = ret;
309 sl = para_realloc(sl, (2 * num_atts + 1) * sizeof(char *));
310 for (i = 0; i < num_atts; i++) {
311 char *orig = sl[i];
312 sl[i] = make_message("%s+", orig);
313 sl[num_atts + i] = make_message("%s-", orig);
314 free(orig);
315 }
316 sl[2 * num_atts] = NULL;
317 i9e_extract_completions(ci->word, sl, &cr->matches);
318 out:
319 free(buf);
320 free_argv(sl);
321 }
322
323 static void lsatt_completer(struct i9e_completion_info *ci,
324 struct i9e_completion_result *cr)
325 {
326 char *opts[] = {"-i", "-l", "-r", NULL};
327 i9e_complete_option(opts, ci, cr);
328 }
329
330 static void mvatt_completer(struct i9e_completion_info *ci,
331 struct i9e_completion_result *cr)
332 {
333 complete_attributes(ci->word, &cr->matches);
334 }
335
336 static void rmatt_completer(struct i9e_completion_info *ci,
337 struct i9e_completion_result *cr)
338 {
339 complete_attributes(ci->word, &cr->matches);
340 }
341
342 static void check_completer(struct i9e_completion_info *ci,
343 struct i9e_completion_result *cr)
344 {
345 char *opts[] = {"-a", "-m", "-p", NULL};
346 i9e_complete_option(opts, ci, cr);
347 }
348
349 static void rm_completer(struct i9e_completion_info *ci,
350 struct i9e_completion_result *cr)
351 {
352 char *opts[] = {"-v", "-f", "-p", NULL};
353
354 if (ci->word[0] == '-') {
355 i9e_complete_option(opts, ci, cr);
356 return;
357 }
358 cr->filename_completion_desired = true;
359 }
360
361 static void touch_completer(struct i9e_completion_info *ci,
362 struct i9e_completion_result *cr)
363 {
364 char *opts[] = {"-n=", "-l=", "-y=", "-i=", "-a=", "-v", "-p", NULL};
365
366 if (ci->word[0] == '-')
367 i9e_complete_option(opts, ci, cr);
368 cr->filename_completion_desired = true;
369 }
370
371 static void cpsi_completer(struct i9e_completion_info *ci,
372 struct i9e_completion_result *cr)
373 {
374 char *opts[] = {"-a", "-y", "-i", "-l", "-n", "-v", NULL};
375
376 if (ci->word[0] == '-')
377 i9e_complete_option(opts, ci, cr);
378 cr->filename_completion_desired = true;
379 }
380
381 static void select_completer(struct i9e_completion_info *ci,
382 struct i9e_completion_result *cr)
383 {
384 char *mood_buf, *pl_buf, **moods, **playlists, **mops;
385 int num_moods, num_pl, i, n, ret;
386
387 ret = execute_client_command("lsmood", &mood_buf);
388 if (ret < 0)
389 return;
390 ret = execute_client_command("lspl", &pl_buf);
391 if (ret < 0)
392 goto free_mood_buf;
393
394 ret = create_argv(mood_buf, "\n", &moods);
395 if (ret < 0)
396 goto free_pl_buf;
397 num_moods = ret;
398 ret = create_argv(pl_buf, "\n", &playlists);
399 if (ret < 0)
400 goto free_moods;
401 num_pl = ret;
402 n = num_moods + num_pl;
403 mops = para_malloc((n + 1) * sizeof(char *));
404 for (i = 0; i < num_moods; i++)
405 mops[i] = make_message("m/%s", moods[i]);
406 for (i = 0; i < num_pl; i++)
407 mops[num_moods + i] = make_message("p/%s", playlists[i]);
408 mops[n] = NULL;
409 i9e_extract_completions(ci->word, mops, &cr->matches);
410 free_argv(mops);
411 free_argv(playlists);
412 free_moods:
413 free_argv(moods);
414 free_pl_buf:
415 free(pl_buf);
416 free_mood_buf:
417 free(mood_buf);
418 }
419
420 #define DEFINE_BLOB_COMPLETER(cmd, blob_type) \
421 static void cmd ## blob_type ## _completer( \
422 struct i9e_completion_info *ci, \
423 struct i9e_completion_result *cr) \
424 {complete_ ## cmd ## blob(#blob_type, ci, cr);}
425
426 DEFINE_BLOB_COMPLETER(add, mood)
427 DEFINE_BLOB_COMPLETER(add, lyr)
428 DEFINE_BLOB_COMPLETER(add, img)
429 DEFINE_BLOB_COMPLETER(add, pl)
430 DEFINE_BLOB_COMPLETER(cat, mood)
431 DEFINE_BLOB_COMPLETER(cat, lyr)
432 DEFINE_BLOB_COMPLETER(cat, img)
433 DEFINE_BLOB_COMPLETER(cat, pl)
434 DEFINE_BLOB_COMPLETER(ls, mood)
435 DEFINE_BLOB_COMPLETER(ls, lyr)
436 DEFINE_BLOB_COMPLETER(ls, img)
437 DEFINE_BLOB_COMPLETER(ls, pl)
438 DEFINE_BLOB_COMPLETER(rm, mood)
439 DEFINE_BLOB_COMPLETER(rm, lyr)
440 DEFINE_BLOB_COMPLETER(rm, img)
441 DEFINE_BLOB_COMPLETER(rm, pl)
442 DEFINE_BLOB_COMPLETER(mv, mood)
443 DEFINE_BLOB_COMPLETER(mv, lyr)
444 DEFINE_BLOB_COMPLETER(mv, img)
445 DEFINE_BLOB_COMPLETER(mv, pl)
446
447 static int client_i9e_line_handler(char *line)
448 {
449 int ret;
450
451 PARA_DEBUG_LOG("line: %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[0]);
459 return 1;
460 }
461
462 I9E_DUMMY_COMPLETER(SUPERCOMMAND_UNAVAILABLE);
463 static struct i9e_completer completers[] = {
464 AFS_COMPLETERS
465 #define LSG_SERVER_CMD_CMD(_name) {.name = #_name, \
466 .completer = _name ## _completer}
467 LSG_SERVER_CMD_COMMANDS
468 #undef LSG_SERVER_CMD_CMD
469 {.name = NULL}
470 };
471
472 __noreturn static void interactive_session(void)
473 {
474 int ret;
475 char *history_file;
476 struct sigaction act;
477 struct i9e_client_info ici = {
478 .fds = {0, 1, 2},
479 .prompt = "para_client> ",
480 .line_handler = client_i9e_line_handler,
481 .loglevel = client_loglevel,
482 .completers = completers,
483 };
484
485 PARA_NOTICE_LOG("\n%s\n", version_text("client"));
486 if (ct->conf.history_file_given)
487 history_file = para_strdup(ct->conf.history_file_arg);
488 else {
489 char *home = para_homedir();
490 history_file = make_message("%s/.paraslash/client.history",
491 home);
492 free(home);
493 }
494 ici.history_file = history_file;
495
496 act.sa_handler = i9e_signal_dispatch;
497 sigemptyset(&act.sa_mask);
498 act.sa_flags = 0;
499 sigaction(SIGINT, &act, NULL);
500 sched.select_function = i9e_select;
501
502 ret = i9e_open(&ici, &sched);
503 if (ret < 0)
504 goto out;
505 para_log = i9e_log;
506 ret = schedule(&sched);
507 sched_shutdown(&sched);
508 i9e_close();
509 para_log = stderr_log;
510 out:
511 if (ret < 0)
512 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
513 exit(ret < 0? EXIT_FAILURE : EXIT_SUCCESS);
514 //client_close(ct);
515 }
516
517 __noreturn static void print_completions(void)
518 {
519 int ret = i9e_print_completions(completers);
520 exit(ret <= 0? EXIT_FAILURE : EXIT_SUCCESS);
521 }
522
523 #else /* HAVE_READLINE */
524
525 __noreturn static void interactive_session(void)
526 {
527 PARA_EMERG_LOG("interactive sessions not available\n");
528 exit(EXIT_FAILURE);
529 }
530
531 __noreturn static void print_completions(void)
532 {
533 PARA_EMERG_LOG("command completion not available\n");
534 exit(EXIT_FAILURE);
535 }
536
537 #endif /* HAVE_READLINE */
538
539 struct supervisor_task {
540 bool stdout_task_started;
541 struct task *task;
542 };
543
544 static int supervisor_post_select(struct sched *s, void *context)
545 {
546 struct supervisor_task *svt = context;
547 int ret = task_status(ct->task);
548
549 if (ret < 0)
550 return ret;
551 if (!svt->stdout_task_started && ct->status == CL_EXECUTING) {
552 stdout_task_register(&sot, s);
553 svt->stdout_task_started = true;
554 return 1;
555 }
556 if (ct->status == CL_SENDING) {
557 stdin_task_register(&sit, s);
558 return -E_TASK_STARTED;
559 }
560 return 0;
561 }
562
563 static struct supervisor_task supervisor_task;
564
565 /**
566 * The client program to connect to para_server.
567 *
568 * \param argc Usual argument count.
569 * \param argv Usual argument vector.
570 *
571 * When called without a paraslash command, an interactive session is started.
572 * Otherwise, the client task and the supervisor task are started. The former
573 * communicates with para_server while the latter monitors whether the client
574 * task intends to read from stdin or write to stdout.
575 *
576 * Once it has been determined whether the client command corresponds to a
577 * stdin command (addmood, addimg, ..), either the stdin task or the stdout
578 * task is set up to replace the supervisor task.
579 *
580 * \return EXIT_SUCCESS or EXIT_FAILURE
581 *
582 * \sa client_open(), stdin.c, stdout.c, para_client(1), para_server(1)
583 */
584 int main(int argc, char *argv[])
585 {
586 int ret;
587
588 init_random_seed_or_die();
589 sched.default_timeout.tv_sec = 1;
590
591 ret = client_parse_config(argc, argv, &ct, &client_loglevel);
592 if (ret < 0)
593 goto out;
594 if (ct->conf.complete_given)
595 print_completions();
596 if (ret == 0)
597 interactive_session(); /* does not return */
598
599 /*
600 * We add buffer tree nodes for stdin and stdout even though
601 * only one of them will be needed. This simplifies the code
602 * a bit wrt. to the buffer tree setup.
603 */
604 sit.btrn = btr_new_node(&(struct btr_node_description)
605 EMBRACE(.name = "stdin"));
606 ret = client_connect(ct, &sched, sit.btrn, NULL);
607 if (ret < 0)
608 goto out;
609 sot.btrn = btr_new_node(&(struct btr_node_description)
610 EMBRACE(.name = "stdout", .parent = ct->btrn[0]));
611 supervisor_task.task = task_register(&(struct task_info) {
612 .name = "supervisor",
613 .post_select = supervisor_post_select,
614 .context = &supervisor_task,
615 }, &sched);
616
617 ret = schedule(&sched);
618 if (ret >= 0) {
619 ret = task_status(ct->task);
620 if (ret < 0) {
621 switch (ret) {
622 /* these are not errors */
623 case -E_SERVER_CMD_SUCCESS:
624 case -E_EOF:
625 case -E_SERVER_EOF:
626 case -E_BTR_EOF:
627 ret = 0;
628 break;
629 default: ret = -E_SERVER_CMD_FAILURE;
630 }
631 }
632 }
633 sched_shutdown(&sched);
634 out:
635 if (ret < 0)
636 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
637 client_close(ct);
638 btr_remove_node(&sit.btrn);
639 btr_remove_node(&sot.btrn);
640 return ret < 0? EXIT_FAILURE : EXIT_SUCCESS;
641 }