osx_write.c: Use realloc() instead of free() and malloc()
[paraslash.git] / client.c
1 /*
2  * Copyright (C) 1997-2006 Andre Noll <maan@systemlinux.org>
3  *
4  *     This program is free software; you can redistribute it and/or modify
5  *     it under the terms of the GNU General Public License as published by
6  *     the Free Software Foundation; either version 2 of the License, or
7  *     (at your option) any later version.
8  *
9  *     This program is distributed in the hope that it will be useful,
10  *     but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *     GNU General Public License for more details.
13  *
14  *     You should have received a copy of the GNU General Public License
15  *     along with this program; if not, write to the Free Software
16  *     Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
17  */
18
19 /** \file client.c the client program used to connect to para_server */
20
21 #include "para.h"
22 #include "list.h"
23 #include "sched.h"
24 #include "client.cmdline.h"
25 #include "string.h"
26 #include "stdin.h"
27 #include "stdout.h"
28 #include "client.h"
29 #include "error.h"
30
31 INIT_CLIENT_ERRLISTS;
32
33 static struct private_client_data *pcd;
34 static struct stdin_task sit;
35 static struct stdout_task sot;
36
37 /*
38  * client log function
39  */
40 void para_log(int ll, const char* fmt,...)
41 {
42         va_list argp;
43
44         /* ignore log message if loglevel is not high enough */
45         if (pcd && ll < pcd->conf.loglevel_arg)
46                 return;
47         va_start(argp, fmt);
48         vfprintf(stderr, fmt, argp);
49         va_end(argp);
50 }
51
52 static void client_event_handler(struct task *t)
53 {
54         struct private_client_data *p = t->private_data;
55
56         PARA_NOTICE_LOG("%s\n", PARA_STRERROR(-t->ret));
57         if (t->ret != -E_HANDSHAKE_COMPLETE) {
58                 unregister_task(t);
59                 p->eof = 1;
60                 return;
61         }
62         if (p->status == CL_SENDING) {
63                 stdin_set_defaults(&sit);
64                 sit.buf = para_malloc(sit.bufsize),
65                 register_task(&sit.task);
66                 p->inbuf = sit.buf;
67                 p->in_loaded = &sit.loaded;
68                 p->in_eof = &sit.eof;
69                 return;
70         }
71         stdout_set_defaults(&sot);
72         sot.buf = p->buf;
73         sot.loaded = &p->loaded;
74         sot.input_eof = &p->eof;
75         register_task(&sot.task);
76 }
77
78 /*
79  * MAIN
80  */
81 int main(int argc, char *argv[])
82 {
83
84         int ret;
85         struct sched s;
86
87         s.default_timeout.tv_sec = 1;
88         s.default_timeout.tv_usec = 0;
89         ret = client_parse_config(argc, argv, &pcd);
90         if (ret < 0)
91                 goto out;
92         pcd->task.event_handler = client_event_handler;
93         ret = client_open(pcd);
94         if (ret < 0)
95                 goto out;
96         ret = sched(&s);
97         client_close(pcd);
98 out:
99         if (ret < 0)
100                 PARA_ERROR_LOG("%s\n", PARA_STRERROR(-ret));
101         return ret >= 0? EXIT_SUCCESS: EXIT_FAILURE;
102 }