file_write: Kill non-btr code.
[paraslash.git] / client.c
1 /*
2  * Copyright (C) 1997-2009 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 "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                 sit.buf = para_malloc(sit.bufsize),
39                 register_task(&sit.task);
40                 ct->inbuf = sit.buf;
41                 ct->in_loaded = &sit.loaded;
42                 ct->in_error = &sit.task.error;
43                 t->error = -E_TASK_STARTED;
44                 return;
45         }
46         if (ct->status == CL_RECEIVING) {
47                 stdout_set_defaults(&sot);
48                 sot.bufp = &ct->buf;
49                 sot.loaded = &ct->loaded;
50                 sot.input_error = &ct->task.error;
51                 register_task(&sot.task);
52                 t->error = -E_TASK_STARTED;
53                 return;
54         }
55 }
56
57 static struct task svt = {
58         .post_select = supervisor_post_select,
59         .status = "supervisor task"
60 };
61
62 static int client_loglevel; /* loglevel */
63 INIT_STDERR_LOGGING(client_loglevel);
64
65
66 /**
67  * The client program to connect to para_server.
68  *
69  * \param argc Usual argument count.
70  * \param argv Usual argument vector.
71  *
72  * It registers two tasks: The client task that communicates with para_server
73  * and the supervisor task that minitors whether the client task intends to
74  * read from stdin or write to stdout.
75  *
76  * Once it has been determined whether the client command corresponds to a
77  * stdin command (addmood, addimg, ..), either the stdin task or the stdout
78  * task is set up to replace the supervisor task.
79  *
80  * \return EXIT_SUCCESS or EXIT_FAILURE
81  *
82  * \sa client_open(), stdin.c, stdout.c, para_client(1), para_server(1)
83  */
84 int main(int argc, char *argv[])
85 {
86
87         int ret;
88         static struct sched s;
89
90         init_random_seed_or_die();
91         s.default_timeout.tv_sec = 1;
92         s.default_timeout.tv_usec = 0;
93         ret = client_open(argc, argv, &ct, &client_loglevel);
94         if (ret < 0) /* can not use PARA_LOG here because ct is NULL */
95                 exit(EXIT_FAILURE);
96         register_task(&svt);
97         ret = schedule(&s);
98         if (ret < 0)
99                 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
100         client_close(ct);
101         return ret >= 0? EXIT_SUCCESS: EXIT_FAILURE;
102 }