1 /* cvm/module_command.c - CVM command module main routine
2 * Copyright (C) 2010 Bruce Guenter <bruce@untroubled.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-1307 USA
17 */
18 #include <unistd.h>
19 #include "module.h"
20
read_input(void)21 static int read_input(void)
22 {
23 size_t rd;
24
25 cvm_module_inbuflen = 0;
26 for (cvm_module_inbuflen = 0;
27 cvm_module_inbuflen <= BUFSIZE;
28 cvm_module_inbuflen += rd) {
29 if ((rd = read(0, cvm_module_inbuffer+cvm_module_inbuflen,
30 BUFSIZE-cvm_module_inbuflen)) == (unsigned)-1)
31 return CVME_IO;
32 if (rd == 0) break;
33 }
34 return 0;
35 }
36
write_output(void)37 static int write_output(void)
38 {
39 size_t wr;
40 unsigned char* ptr;
41
42 for (ptr = cvm_module_outbuffer;
43 cvm_module_outbuflen > 0;
44 cvm_module_outbuflen -= wr, ptr += wr) {
45 if ((wr = write(1, ptr, cvm_module_outbuflen)) == (unsigned)-1
46 || wr == 0)
47 return CVME_IO;
48 }
49 return 0;
50 }
51
52 extern void usage(void);
53
command_main(void)54 int command_main(void)
55 {
56 int code;
57 int wcode;
58 if ((code = cvm_module_init()) != 0) return code;
59 if ((code = read_input()) != 0) { cvm_module_stop(); return code; }
60 code = cvm_module_handle_request();
61 cvm_module_fact_end(code);
62 if ((wcode = write_output()) != 0 && code == 0) code = wcode;
63 cvm_module_stop();
64 return code & CVME_MASK;
65 }
66