Annotation of src/usr.bin/cvs/req.c, Revision 1.4
1.1 jfb 1: /* $OpenBSD$ */
2: /*
3: * Copyright (c) 2004 Jean-Francois Brousseau <jfb@openbsd.org>
4: * All rights reserved.
5: *
6: * Redistribution and use in source and binary forms, with or without
7: * modification, are permitted provided that the following conditions
8: * are met:
9: *
10: * 1. Redistributions of source code must retain the above copyright
11: * notice, this list of conditions and the following disclaimer.
12: * 2. The name of the author may not be used to endorse or promote products
13: * derived from this software without specific prior written permission.
14: *
15: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
16: * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
17: * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
18: * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
21: * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
22: * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
23: * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
24: * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25: */
26:
27:
28: #include <sys/types.h>
29: #include <sys/stat.h>
30:
31: #include <fcntl.h>
32: #include <stdio.h>
33: #include <errno.h>
34: #include <stdlib.h>
35: #include <unistd.h>
36: #include <signal.h>
37: #include <string.h>
38: #include <sysexits.h>
39: #ifdef CVS_ZLIB
40: #include <zlib.h>
41: #endif
42:
43: #include "buf.h"
44: #include "cvs.h"
45: #include "log.h"
46: #include "file.h"
47: #include "proto.h"
48:
49:
50: extern int verbosity;
51: extern int cvs_compress;
52: extern char *cvs_rsh;
53: extern int cvs_trace;
54: extern int cvs_nolog;
55: extern int cvs_readonly;
56:
57:
58: static int cvs_req_root (int, char *);
1.4 ! jfb 59: static int cvs_req_validreq (int, char *);
! 60: static int cvs_req_validresp (int, char *);
1.1 jfb 61: static int cvs_req_directory (int, char *);
1.2 jfb 62: static int cvs_req_argument (int, char *);
1.3 jfb 63: static int cvs_req_globalopt (int, char *);
1.1 jfb 64: static int cvs_req_version (int, char *);
65:
66:
67: struct cvs_reqhdlr {
68: int (*hdlr)(int, char *);
69: } cvs_req_swtab[CVS_REQ_MAX + 1] = {
70: { NULL },
71: { cvs_req_root },
1.4 ! jfb 72: { cvs_req_validreq },
! 73: { cvs_req_validresp },
1.1 jfb 74: { cvs_req_directory },
75: { NULL },
76: { NULL },
77: { NULL },
78: { NULL },
79: { NULL },
80: { NULL }, /* 10 */
81: { NULL },
82: { NULL },
83: { NULL },
84: { NULL },
85: { NULL },
86: { NULL },
87: { NULL },
88: { NULL },
89: { NULL },
1.2 jfb 90: { cvs_req_argument }, /* 20 */
91: { cvs_req_argument },
1.3 jfb 92: { cvs_req_globalopt },
1.1 jfb 93: { NULL },
94: { NULL },
95: { NULL },
96: { NULL },
97: { NULL },
98: { NULL },
99: { NULL },
100: { NULL }, /* 30 */
101: { NULL },
102: { NULL },
103: { NULL },
104: { NULL },
105: { NULL },
106: { NULL },
107: { NULL },
108: { NULL },
109: { NULL },
110: { NULL }, /* 40 */
111: { NULL },
112: { NULL },
113: { NULL },
114: { NULL },
115: { NULL },
116: { NULL },
117: { NULL },
118: { NULL },
119: { NULL },
120: { NULL }, /* 50 */
121: { NULL },
122: { NULL },
123: { NULL },
124: { NULL },
125: { NULL },
126: { NULL },
127: { NULL },
128: { NULL },
129: { NULL },
130: { NULL }, /* 60 */
131: { NULL },
132: { NULL },
133: { NULL },
134: { NULL },
135: { NULL },
136: { NULL },
137: { NULL },
138: { NULL },
139: { cvs_req_version },
140: };
141:
142:
143:
144: /*
1.2 jfb 145: * Argument array built by `Argument' and `Argumentx' requests.
146: */
147:
148: static char *cvs_req_args[CVS_PROTO_MAXARG];
149: static int cvs_req_nargs = 0;
150:
151:
152:
153:
154:
155: /*
1.1 jfb 156: * cvs_req_handle()
157: *
158: * Generic request handler dispatcher. The handler expects the first line
159: * of the command as single argument.
160: * Returns the return value of the command on success, or -1 on failure.
161: */
162:
163: int
164: cvs_req_handle(char *line)
165: {
166: char *cp, *cmd;
167: struct cvs_req *req;
168:
169: cmd = line;
170:
171: cp = strchr(cmd, ' ');
172: if (cp != NULL)
173: *(cp++) = '\0';
174:
175: req = cvs_req_getbyname(cmd);
176: if (req == NULL)
177: return (-1);
178: else if (cvs_req_swtab[req->req_id].hdlr == NULL) {
179: cvs_log(LP_ERRNO, "handler for `%s' not implemented", cmd);
180: return (-1);
181: }
182:
183: return (*cvs_req_swtab[req->req_id].hdlr)(req->req_id, cp);
184: }
185:
186:
187:
188: static int
189: cvs_req_root(int reqid, char *line)
190: {
191:
1.4 ! jfb 192: return (0);
! 193: }
! 194:
! 195:
! 196: static int
! 197: cvs_req_validreq(int reqid, char *line)
! 198: {
! 199: char *vreq;
! 200:
! 201: vreq = cvs_req_getvalid();
! 202: if (vreq == NULL)
! 203: return (-1);
! 204:
! 205: cvs_sendresp(CVS_RESP_VALIDREQ, vreq);
! 206:
! 207: return (0);
! 208: }
! 209:
! 210: static int
! 211: cvs_req_validresp(int reqid, char *line)
! 212: {
! 213: char *sp, *ep;
! 214: struct cvs_resp *resp;
1.1 jfb 215:
1.4 ! jfb 216: sp = line;
! 217: do {
! 218: ep = strchr(sp, ' ');
! 219: if (ep != NULL)
! 220: *(ep++) = '\0';
! 221:
! 222: resp = cvs_resp_getbyname(sp);
! 223: if (resp != NULL)
! 224: ;
! 225:
! 226: if (ep != NULL)
! 227: sp = ep + 1;
! 228: } while (ep != NULL);
1.1 jfb 229:
230: return (0);
231: }
232:
233: static int
234: cvs_req_directory(int reqid, char *line)
235: {
236:
237:
1.2 jfb 238:
239: return (0);
240: }
241:
242:
243: static int
244: cvs_req_argument(int reqid, char *line)
245: {
246: char *nap;
247:
248: if (cvs_req_nargs == CVS_PROTO_MAXARG) {
249: cvs_log(LP_ERR, "too many arguments");
250: return (-1);
251: }
252:
253: if (reqid == CVS_REQ_ARGUMENT) {
254: cvs_req_args[cvs_req_nargs] = strdup(line);
255: if (cvs_req_args[cvs_req_nargs] == NULL) {
256: cvs_log(LP_ERRNO, "failed to copy argument");
257: return (-1);
258: }
259: cvs_req_nargs++;
260: }
261: else if (reqid == CVS_REQ_ARGUMENTX) {
262: if (cvs_req_nargs == 0)
263: cvs_log(LP_WARN, "no argument to append to");
264: else {
265: asprintf(&nap, "%s%s", cvs_req_args[cvs_req_nargs - 1],
266: line);
267: if (nap == NULL) {
268: cvs_log(LP_ERRNO,
269: "failed to append to argument");
270: return (-1);
271: }
272: free(cvs_req_args[cvs_req_nargs - 1]);
273: cvs_req_args[cvs_req_nargs - 1] = nap;
274: }
1.3 jfb 275: }
276:
277: return (0);
278: }
279:
280:
281: static int
282: cvs_req_globalopt(int reqid, char *line)
283: {
284: if ((*line != '-') || (*(line + 2) != '\0')) {
285: cvs_log(LP_ERR,
286: "invalid `Global_option' request format");
287: return (-1);
288: }
289:
290: switch (*(line + 1)) {
291: case 'l':
292: cvs_nolog = 1;
293: break;
294: case 'n':
295: break;
296: case 'Q':
297: verbosity = 0;
298: break;
299: case 'q':
300: if (verbosity > 1)
301: verbosity = 1;
302: break;
303: case 'r':
304: cvs_readonly = 1;
305: break;
306: case 't':
307: cvs_trace = 1;
308: break;
309: default:
310: cvs_log(LP_ERR, "unknown global option `%s'", line);
311: return (-1);
1.2 jfb 312: }
1.1 jfb 313:
314: return (0);
315: }
316:
317:
318: static int
319: cvs_req_version(int reqid, char *line)
320: {
321: cvs_printf("%s\n", CVS_VERSION);
322: return (0);
323: }