gui: Combine open_stat_pipe() and para_open_stat_pipe().
[paraslash.git] / client.c
1 /*
2 * Copyright (C) 1997-2010 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 <openssl/rc4.h>
11 #include <stdbool.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
25 INIT_CLIENT_ERRLISTS;
26
27 static struct client_task *ct;
28 static struct stdin_task sit;
29 static struct stdout_task sot;
30
31 static void supervisor_post_select(__a_unused struct sched *s, struct task *t)
32 {
33 if (ct->task.error < 0) {
34 t->error = ct->task.error;
35 return;
36 }
37 if (ct->status == CL_SENDING) {
38 stdin_set_defaults(&sit);
39 register_task(&sit.task);
40 t->error = -E_TASK_STARTED;
41 return;
42 }
43 if (ct->status == CL_RECEIVING) {
44 stdout_set_defaults(&sot);
45 register_task(&sot.task);
46 t->error = -E_TASK_STARTED;
47 return;
48 }
49 }
50
51 static struct task svt = {
52 .post_select = supervisor_post_select,
53 .status = "supervisor task"
54 };
55
56 static int client_loglevel = LL_ERROR; /* loglevel */
57 INIT_STDERR_LOGGING(client_loglevel);
58
59 /**
60 * The client program to connect to para_server.
61 *
62 * \param argc Usual argument count.
63 * \param argv Usual argument vector.
64 *
65 * It registers two tasks: The client task that communicates with para_server
66 * and the supervisor task that minitors whether the client task intends to
67 * read from stdin or write to stdout.
68 *
69 * Once it has been determined whether the client command corresponds to a
70 * stdin command (addmood, addimg, ..), either the stdin task or the stdout
71 * task is set up to replace the supervisor task.
72 *
73 * \return EXIT_SUCCESS or EXIT_FAILURE
74 *
75 * \sa client_open(), stdin.c, stdout.c, para_client(1), para_server(1)
76 */
77 int main(int argc, char *argv[])
78 {
79
80 int ret;
81 static struct sched s;
82
83 init_random_seed_or_die();
84 s.default_timeout.tv_sec = 1;
85 s.default_timeout.tv_usec = 0;
86 /*
87 * We add buffer tree nodes for stdin and stdout even though
88 * only one of them will be needed. This simplifies the code
89 * a bit wrt. to the buffer tree setup.
90 */
91 sit.btrn = btr_new_node(&(struct btr_node_description)
92 EMBRACE(.name = "stdin"));
93 ret = client_open(argc, argv, &ct, &client_loglevel, sit.btrn, NULL);
94 if (ret < 0)
95 goto out;
96 sot.btrn = btr_new_node(&(struct btr_node_description)
97 EMBRACE(.name = "stdout", .parent = ct->btrn));
98 register_task(&svt);
99 ret = schedule(&s);
100 out:
101 client_close(ct);
102 btr_free_node(sit.btrn);
103 btr_free_node(sot.btrn);
104 if (ret < 0) {
105 /* can not use PARA_LOG here because ct is NULL */
106 fprintf(stderr, "%s\n", para_strerror(-ret));
107 return EXIT_FAILURE;
108 }
109 return EXIT_SUCCESS;
110 }