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