2 * Copyright (C) 1997-2013 Andre Noll <maan@systemlinux.org>
4 * Licensed under the GPL v2. For licencing details see COPYING.
7 /** \file client.c The client program used to connect to para_server. */
16 #include "client.cmdline.h"
21 #include "buffer_tree.h"
27 static struct sched sched
;
28 static struct client_task
*ct
;
29 static struct stdin_task sit
;
30 static struct stdout_task sot
;
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
;
37 #include "interactive.h"
38 #include "server_completion.h"
39 #include "afs_completion.h"
43 struct btr_node
*btrn
;
48 static void exec_pre_select(struct sched
*s
, struct task
*t
)
50 struct exec_task
*et
= container_of(t
, struct exec_task
, task
);
51 int ret
= btr_node_status(et
->btrn
, 0, BTR_NT_LEAF
);
57 static int exec_post_select(__a_unused
struct sched
*s
, struct task
*t
)
59 struct exec_task
*et
= container_of(t
, struct exec_task
, task
);
60 struct btr_node
*btrn
= et
->btrn
;
65 ret
= btr_node_status(btrn
, 0, BTR_NT_LEAF
);
68 sz
= btr_next_buffer(btrn
, &buf
);
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';
76 btr_consume(btrn
, sz
);
80 static int make_client_argv(const char *line
)
84 free_argv(ct
->conf
.inputs
);
85 ret
= create_argv(line
, " ", &ct
->conf
.inputs
);
87 ct
->conf
.inputs_num
= ret
;
91 static int execute_client_command(const char *cmd
, char **result
)
94 struct sched command_sched
= {.default_timeout
= {.tv_sec
= 1}};
95 struct exec_task exec_task
= {
97 .pre_select
= exec_pre_select
,
98 .post_select
= exec_post_select
,
99 .status
= "client exec task",
101 .result_buf
= para_strdup(""),
105 ret
= make_client_argv(cmd
);
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
);
114 schedule(&command_sched
);
115 *result
= exec_task
.result_buf
;
116 btr_remove_node(&exec_task
.btrn
);
117 client_disconnect(ct
);
120 btr_remove_node(&exec_task
.btrn
);
122 free(exec_task
.result_buf
);
126 static int extract_matches_from_command(const char *word
, char *cmd
,
132 ret
= execute_client_command(cmd
, &buf
);
135 ret
= create_argv(buf
, "\n", &sl
);
139 ret
= i9e_extract_completions(word
, sl
, matches
);
144 static int complete_attributes(const char *word
, char ***matches
)
146 return extract_matches_from_command(word
, "lsatt", matches
);
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
)
153 cr
->filename_completion_desired
= true;
156 static void generic_blob_complete(const char *blob_type
,
157 struct i9e_completion_info
*ci
,
158 struct i9e_completion_result
*cr
)
161 sprintf(cmd
, "ls%s", blob_type
);
162 extract_matches_from_command(ci
->word
, cmd
, &cr
->matches
);
165 static void complete_catblob(const char *blob_type
,
166 struct i9e_completion_info
*ci
,
167 struct i9e_completion_result
*cr
)
169 generic_blob_complete(blob_type
, ci
, cr
);
172 static void complete_lsblob(const char *blob_type
,
173 struct i9e_completion_info
*ci
,
174 struct i9e_completion_result
*cr
)
176 char *opts
[] = {"-i", "-l", "-r", NULL
};
178 if (ci
->word
[0] == '-')
179 return i9e_complete_option(opts
, ci
, cr
);
180 generic_blob_complete(blob_type
, ci
, cr
);
183 static void complete_rmblob(const char *blob_type
,
184 struct i9e_completion_info
*ci
,
185 struct i9e_completion_result
*cr
)
187 generic_blob_complete(blob_type
, ci
, cr
);
190 static void complete_mvblob(const char *blob_type
,
191 struct i9e_completion_info
*ci
,
192 struct i9e_completion_result
*cr
)
194 generic_blob_complete(blob_type
, ci
, cr
);
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(version
);
208 I9E_DUMMY_COMPLETER(stop
);
209 I9E_DUMMY_COMPLETER(addatt
);
210 I9E_DUMMY_COMPLETER(init
);
212 static struct i9e_completer completers
[];
214 static void help_completer(struct i9e_completion_info
*ci
,
215 struct i9e_completion_result
*result
)
217 result
->matches
= i9e_complete_commands(ci
->word
, completers
);
220 static void stat_completer(struct i9e_completion_info
*ci
,
221 struct i9e_completion_result
*cr
)
223 char *opts
[] = {"-n=", "-p", NULL
};
224 //PARA_CRIT_LOG("word: %s\n", ci->word);
225 i9e_complete_option(opts
, ci
, cr
);
228 static void sender_completer(struct i9e_completion_info
*ci
,
229 struct i9e_completion_result
*cr
)
231 char *senders
[] = {"http", "dccp", "udp", NULL
};
232 char *http_cmds
[] = {"on", "off", "allow", "deny", "help", NULL
};
233 char *dccp_cmds
[] = {"on", "off", "allow", "deny", "help", NULL
};
234 char *udp_cmds
[] ={"on", "off", "add", "delete", "help", NULL
};
238 //PARA_CRIT_LOG("wn: %d\n", ci->word_num);
239 if (ci
->word_num
== 0 || ci
->word_num
> 3)
241 if (ci
->word_num
== 1 || (ci
->word_num
== 2 && *ci
->word
!= '\0')) {
242 i9e_extract_completions(ci
->word
, senders
, &cr
->matches
);
245 sender
= ci
->argv
[1];
246 //PARA_CRIT_LOG("sender: %s\n", sender);
247 if (strcmp(sender
, "http") == 0)
249 else if (strcmp(sender
, "dccp") == 0)
251 else if (strcmp(sender
, "udp") == 0)
255 i9e_extract_completions(ci
->word
, cmds
, &cr
->matches
);
258 static void add_completer(struct i9e_completion_info
*ci
,
259 struct i9e_completion_result
*cr
)
261 char *opts
[] = {"-a", "-l", "-f", "-v", "--", NULL
};
263 if (ci
->word
[0] == '-')
264 i9e_complete_option(opts
, ci
, cr
);
265 cr
->filename_completion_desired
= true;
268 static void ls_completer(struct i9e_completion_info
*ci
,
269 struct i9e_completion_result
*cr
)
272 "--", "-l", "-ls", "-ll", "-lv", "-lp", "-lm", "-lc", "-p",
273 "-a", "-r", "-d", "-sp", "-sl", "-ss", "-sn", "-sf", "-sc",
274 "-si", "-sy", "-sb", "-sd", "-sa", NULL
276 if (ci
->word
[0] == '-')
277 i9e_complete_option(opts
, ci
, cr
);
278 cr
->filename_completion_desired
= true;
281 static void setatt_completer(struct i9e_completion_info
*ci
,
282 struct i9e_completion_result
*cr
)
285 int i
, ret
, num_atts
;
287 if (ci
->word_num
== 0)
290 if (*ci
->word
== '/' || *ci
->word
== '\0')
291 cr
->filename_completion_desired
= true;
292 if (*ci
->word
== '/')
294 ret
= execute_client_command("lsatt", &buf
);
297 ret
= create_argv(buf
, "\n", &sl
);
301 sl
= para_realloc(sl
, (2 * num_atts
+ 1) * sizeof(char *));
302 for (i
= 0; i
< num_atts
; i
++) {
304 sl
[i
] = make_message("%s+", orig
);
305 sl
[num_atts
+ i
] = make_message("%s-", orig
);
308 sl
[2 * num_atts
] = NULL
;
309 i9e_extract_completions(ci
->word
, sl
, &cr
->matches
);
315 static void lsatt_completer(struct i9e_completion_info
*ci
,
316 struct i9e_completion_result
*cr
)
318 char *opts
[] = {"-i", "-l", "-r", NULL
};
319 i9e_complete_option(opts
, ci
, cr
);
322 static void mvatt_completer(struct i9e_completion_info
*ci
,
323 struct i9e_completion_result
*cr
)
325 complete_attributes(ci
->word
, &cr
->matches
);
328 static void rmatt_completer(struct i9e_completion_info
*ci
,
329 struct i9e_completion_result
*cr
)
331 complete_attributes(ci
->word
, &cr
->matches
);
334 static void check_completer(struct i9e_completion_info
*ci
,
335 struct i9e_completion_result
*cr
)
337 char *opts
[] = {"-a", "-m", "-p", NULL
};
338 i9e_complete_option(opts
, ci
, cr
);
341 static void rm_completer(struct i9e_completion_info
*ci
,
342 struct i9e_completion_result
*cr
)
344 char *opts
[] = {"-v", "-f", "-p", NULL
};
346 if (ci
->word
[0] == '-') {
347 i9e_complete_option(opts
, ci
, cr
);
350 cr
->filename_completion_desired
= true;
353 static void touch_completer(struct i9e_completion_info
*ci
,
354 struct i9e_completion_result
*cr
)
356 char *opts
[] = {"-n=", "-l=", "-y=", "-i=", "-a=", "-v", "-p", NULL
};
358 if (ci
->word
[0] == '-')
359 i9e_complete_option(opts
, ci
, cr
);
360 cr
->filename_completion_desired
= true;
363 static void cpsi_completer(struct i9e_completion_info
*ci
,
364 struct i9e_completion_result
*cr
)
366 char *opts
[] = {"-a", "-y", "-i", "-l", "-n", "-v", NULL
};
368 if (ci
->word
[0] == '-')
369 i9e_complete_option(opts
, ci
, cr
);
370 cr
->filename_completion_desired
= true;
373 static void select_completer(struct i9e_completion_info
*ci
,
374 struct i9e_completion_result
*cr
)
376 char *mood_buf
, *pl_buf
, **moods
, **playlists
, **mops
;
377 int num_moods
, num_pl
, i
, n
, ret
;
379 ret
= execute_client_command("lsmood", &mood_buf
);
382 ret
= execute_client_command("lspl", &pl_buf
);
386 ret
= create_argv(mood_buf
, "\n", &moods
);
390 ret
= create_argv(pl_buf
, "\n", &playlists
);
394 n
= num_moods
+ num_pl
;
395 mops
= para_malloc((n
+ 1) * sizeof(char *));
396 for (i
= 0; i
< num_moods
; i
++)
397 mops
[i
] = make_message("m/%s", moods
[i
]);
398 for (i
= 0; i
< num_pl
; i
++)
399 mops
[num_moods
+ i
] = make_message("p/%s", playlists
[i
]);
401 i9e_extract_completions(ci
->word
, mops
, &cr
->matches
);
403 free_argv(playlists
);
412 #define DEFINE_BLOB_COMPLETER(cmd, blob_type) \
413 static void cmd ## blob_type ## _completer( \
414 struct i9e_completion_info *ci, \
415 struct i9e_completion_result *cr) \
416 {complete_ ## cmd ## blob(#blob_type, ci, cr);}
418 DEFINE_BLOB_COMPLETER(add
, mood
)
419 DEFINE_BLOB_COMPLETER(add
, lyr
)
420 DEFINE_BLOB_COMPLETER(add
, img
)
421 DEFINE_BLOB_COMPLETER(add
, pl
)
422 DEFINE_BLOB_COMPLETER(cat
, mood
)
423 DEFINE_BLOB_COMPLETER(cat
, lyr
)
424 DEFINE_BLOB_COMPLETER(cat
, img
)
425 DEFINE_BLOB_COMPLETER(cat
, pl
)
426 DEFINE_BLOB_COMPLETER(ls
, mood
)
427 DEFINE_BLOB_COMPLETER(ls
, lyr
)
428 DEFINE_BLOB_COMPLETER(ls
, img
)
429 DEFINE_BLOB_COMPLETER(ls
, pl
)
430 DEFINE_BLOB_COMPLETER(rm
, mood
)
431 DEFINE_BLOB_COMPLETER(rm
, lyr
)
432 DEFINE_BLOB_COMPLETER(rm
, img
)
433 DEFINE_BLOB_COMPLETER(rm
, pl
)
434 DEFINE_BLOB_COMPLETER(mv
, mood
)
435 DEFINE_BLOB_COMPLETER(mv
, lyr
)
436 DEFINE_BLOB_COMPLETER(mv
, img
)
437 DEFINE_BLOB_COMPLETER(mv
, pl
)
439 static int client_i9e_line_handler(char *line
)
443 client_disconnect(ct
);
444 PARA_DEBUG_LOG("line: %s\n", line
);
445 ret
= make_client_argv(line
);
448 ret
= client_connect(ct
, &sched
, NULL
, NULL
);
451 i9e_attach_to_stdout(ct
->btrn
);
455 static struct i9e_completer completers
[] = {
461 __noreturn
static void interactive_session(void)
465 struct sigaction act
;
466 struct i9e_client_info ici
= {
468 .prompt
= "para_client> ",
469 .line_handler
= client_i9e_line_handler
,
470 .loglevel
= client_loglevel
,
471 .completers
= completers
,
474 PARA_NOTICE_LOG("\n%s\n", version_text("client"));
475 if (ct
->conf
.history_file_given
)
476 history_file
= para_strdup(ct
->conf
.history_file_arg
);
478 char *home
= para_homedir();
479 history_file
= make_message("%s/.paraslash/client.history",
483 ici
.history_file
= history_file
;
485 act
.sa_handler
= i9e_signal_dispatch
;
486 sigemptyset(&act
.sa_mask
);
488 sigaction(SIGINT
, &act
, NULL
);
489 sched
.select_function
= i9e_select
;
491 ret
= i9e_open(&ici
, &sched
);
495 ret
= schedule(&sched
);
497 para_log
= stderr_log
;
500 PARA_ERROR_LOG("%s\n", para_strerror(-ret
));
501 exit(ret
< 0? EXIT_FAILURE
: EXIT_SUCCESS
);
505 __noreturn
static void print_completions(void)
507 int ret
= i9e_print_completions(completers
);
508 exit(ret
<= 0? EXIT_FAILURE
: EXIT_SUCCESS
);
511 #else /* HAVE_READLINE */
513 __noreturn
static void interactive_session(void)
515 PARA_EMERG_LOG("interactive sessions not available\n");
519 __noreturn
static void print_completions(void)
521 PARA_EMERG_LOG("command completion not available\n");
525 #endif /* HAVE_READLINE */
527 static int supervisor_post_select(struct sched
*s
, __a_unused
struct task
*t
)
529 if (ct
->task
.error
< 0)
530 return ct
->task
.error
;
531 if (ct
->status
== CL_SENDING
) {
532 stdin_set_defaults(&sit
);
533 register_task(s
, &sit
.task
);
534 return -E_TASK_STARTED
;
536 if (ct
->status
== CL_RECEIVING
) {
537 stdout_set_defaults(&sot
);
538 register_task(s
, &sot
.task
);
539 return -E_TASK_STARTED
;
544 static struct task svt
= {
545 .post_select
= supervisor_post_select
,
546 .status
= "supervisor task"
550 * The client program to connect to para_server.
552 * \param argc Usual argument count.
553 * \param argv Usual argument vector.
555 * When called without a paraslash command, an interactive session is started.
556 * Otherwise, the client task and the supervisor task are started. The former
557 * communicates with para_server while the latter monitors whether the client
558 * task intends to read from stdin or write to stdout.
560 * Once it has been determined whether the client command corresponds to a
561 * stdin command (addmood, addimg, ..), either the stdin task or the stdout
562 * task is set up to replace the supervisor task.
564 * \return EXIT_SUCCESS or EXIT_FAILURE
566 * \sa client_open(), stdin.c, stdout.c, para_client(1), para_server(1)
568 int main(int argc
, char *argv
[])
572 init_random_seed_or_die();
573 sched
.default_timeout
.tv_sec
= 1;
575 ret
= client_parse_config(argc
, argv
, &ct
, &client_loglevel
);
578 if (ct
->conf
.complete_given
)
581 interactive_session(); /* does not return */
584 * We add buffer tree nodes for stdin and stdout even though
585 * only one of them will be needed. This simplifies the code
586 * a bit wrt. to the buffer tree setup.
588 sit
.btrn
= btr_new_node(&(struct btr_node_description
)
589 EMBRACE(.name
= "stdin"));
590 ret
= client_connect(ct
, &sched
, sit
.btrn
, NULL
);
593 sot
.btrn
= btr_new_node(&(struct btr_node_description
)
594 EMBRACE(.name
= "stdout", .parent
= ct
->btrn
));
595 register_task(&sched
, &svt
);
596 ret
= schedule(&sched
);
597 if (ret
>= 0 && ct
->task
.error
< 0) {
598 switch(ct
->task
.error
) {
599 /* these are not errors */
600 case -E_SERVER_CMD_SUCCESS
:
606 default: ret
= -E_SERVER_CMD_FAILURE
;
611 PARA_ERROR_LOG("%s\n", para_strerror(-ret
));
613 btr_remove_node(&sit
.btrn
);
614 btr_remove_node(&sot
.btrn
);
615 return ret
< 0? EXIT_FAILURE
: EXIT_SUCCESS
;