[BACK]Return to req.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / cvs

Annotation of src/usr.bin/cvs/req.c, Revision 1.7

1.7     ! tedu        1: /*     $OpenBSD: req.c,v 1.6 2004/12/06 21:03:12 deraadt Exp $ */
1.1       jfb         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.5       jfb        62: static int  cvs_req_case       (int, char *);
1.2       jfb        63: static int  cvs_req_argument   (int, char *);
1.3       jfb        64: static int  cvs_req_globalopt  (int, char *);
1.1       jfb        65: static int  cvs_req_version    (int, char *);
                     66:
                     67:
                     68: struct cvs_reqhdlr {
                     69:        int (*hdlr)(int, char *);
                     70: } cvs_req_swtab[CVS_REQ_MAX + 1] = {
                     71:        { NULL               },
                     72:        { cvs_req_root       },
1.4       jfb        73:        { cvs_req_validreq   },
                     74:        { cvs_req_validresp  },
1.1       jfb        75:        { cvs_req_directory  },
                     76:        { NULL               },
                     77:        { NULL               },
                     78:        { NULL               },
                     79:        { NULL               },
                     80:        { NULL               },
                     81:        { NULL               }, /* 10 */
                     82:        { NULL               },
                     83:        { NULL               },
                     84:        { NULL               },
                     85:        { NULL               },
                     86:        { NULL               },
                     87:        { NULL               },
                     88:        { NULL               },
1.5       jfb        89:        { cvs_req_case       },
1.1       jfb        90:        { NULL               },
1.2       jfb        91:        { cvs_req_argument   }, /* 20 */
                     92:        { cvs_req_argument   },
1.3       jfb        93:        { cvs_req_globalopt  },
1.1       jfb        94:        { NULL               },
                     95:        { NULL               },
                     96:        { NULL               },
                     97:        { NULL               },
                     98:        { NULL               },
                     99:        { NULL               },
                    100:        { NULL               },
                    101:        { NULL               }, /* 30 */
                    102:        { NULL               },
                    103:        { NULL               },
                    104:        { NULL               },
                    105:        { NULL               },
                    106:        { NULL               },
                    107:        { NULL               },
                    108:        { NULL               },
                    109:        { NULL               },
                    110:        { NULL               },
                    111:        { NULL               }, /* 40 */
                    112:        { NULL               },
                    113:        { NULL               },
                    114:        { NULL               },
                    115:        { NULL               },
                    116:        { NULL               },
                    117:        { NULL               },
                    118:        { NULL               },
                    119:        { NULL               },
                    120:        { NULL               },
                    121:        { NULL               }, /* 50 */
                    122:        { NULL               },
                    123:        { NULL               },
                    124:        { NULL               },
                    125:        { NULL               },
                    126:        { NULL               },
                    127:        { NULL               },
                    128:        { NULL               },
                    129:        { NULL               },
                    130:        { NULL               },
                    131:        { NULL               }, /* 60 */
                    132:        { NULL               },
                    133:        { NULL               },
                    134:        { NULL               },
                    135:        { NULL               },
                    136:        { NULL               },
                    137:        { NULL               },
                    138:        { NULL               },
                    139:        { NULL               },
                    140:        { cvs_req_version    },
                    141: };
                    142:
                    143:
                    144:
                    145: /*
1.2       jfb       146:  * Argument array built by `Argument' and `Argumentx' requests.
                    147:  */
                    148:
                    149: static char *cvs_req_args[CVS_PROTO_MAXARG];
                    150: static int   cvs_req_nargs = 0;
                    151:
                    152:
                    153: /*
1.1       jfb       154:  * cvs_req_handle()
                    155:  *
                    156:  * Generic request handler dispatcher.  The handler expects the first line
                    157:  * of the command as single argument.
                    158:  * Returns the return value of the command on success, or -1 on failure.
                    159:  */
                    160: int
                    161: cvs_req_handle(char *line)
                    162: {
                    163:        char *cp, *cmd;
                    164:        struct cvs_req *req;
                    165:
                    166:        cmd = line;
                    167:
                    168:        cp = strchr(cmd, ' ');
                    169:        if (cp != NULL)
                    170:                *(cp++) = '\0';
                    171:
                    172:        req = cvs_req_getbyname(cmd);
                    173:        if (req == NULL)
                    174:                return (-1);
                    175:        else if (cvs_req_swtab[req->req_id].hdlr == NULL) {
                    176:                cvs_log(LP_ERRNO, "handler for `%s' not implemented", cmd);
                    177:                return (-1);
                    178:        }
                    179:
                    180:        return (*cvs_req_swtab[req->req_id].hdlr)(req->req_id, cp);
                    181: }
                    182:
                    183:
                    184: static int
                    185: cvs_req_root(int reqid, char *line)
                    186: {
                    187:
1.4       jfb       188:        return (0);
                    189: }
                    190:
                    191:
                    192: static int
                    193: cvs_req_validreq(int reqid, char *line)
                    194: {
                    195:        char *vreq;
                    196:
                    197:        vreq = cvs_req_getvalid();
                    198:        if (vreq == NULL)
                    199:                return (-1);
                    200:
                    201:        cvs_sendresp(CVS_RESP_VALIDREQ, vreq);
                    202:
                    203:        return (0);
                    204: }
                    205:
                    206: static int
                    207: cvs_req_validresp(int reqid, char *line)
                    208: {
                    209:        char *sp, *ep;
                    210:        struct cvs_resp *resp;
1.1       jfb       211:
1.4       jfb       212:        sp = line;
                    213:        do {
                    214:                ep = strchr(sp, ' ');
                    215:                if (ep != NULL)
                    216:                        *(ep++) = '\0';
                    217:
                    218:                resp = cvs_resp_getbyname(sp);
                    219:                if (resp != NULL)
                    220:                        ;
                    221:
                    222:                if (ep != NULL)
                    223:                        sp = ep + 1;
                    224:        } while (ep != NULL);
1.1       jfb       225:
                    226:        return (0);
                    227: }
                    228:
                    229: static int
                    230: cvs_req_directory(int reqid, char *line)
                    231: {
                    232:
1.5       jfb       233:        return (0);
                    234: }
                    235:
                    236: /*
                    237:  * cvs_req_case()
                    238:  *
                    239:  * Handler for the `Case' requests, which toggles case sensitivity ON or OFF
                    240:  */
                    241: static int
                    242: cvs_req_case(int reqid, char *line)
                    243: {
                    244:        cvs_nocase = 1;
1.2       jfb       245:        return (0);
                    246: }
                    247:
                    248:
                    249: static int
                    250: cvs_req_argument(int reqid, char *line)
                    251: {
                    252:        char *nap;
                    253:
                    254:        if (cvs_req_nargs == CVS_PROTO_MAXARG) {
                    255:                cvs_log(LP_ERR, "too many arguments");
                    256:                return (-1);
                    257:        }
                    258:
                    259:        if (reqid == CVS_REQ_ARGUMENT) {
                    260:                cvs_req_args[cvs_req_nargs] = strdup(line);
                    261:                if (cvs_req_args[cvs_req_nargs] == NULL) {
                    262:                        cvs_log(LP_ERRNO, "failed to copy argument");
                    263:                        return (-1);
                    264:                }
                    265:                cvs_req_nargs++;
1.6       deraadt   266:        } else if (reqid == CVS_REQ_ARGUMENTX) {
1.2       jfb       267:                if (cvs_req_nargs == 0)
                    268:                        cvs_log(LP_WARN, "no argument to append to");
                    269:                else {
                    270:                        asprintf(&nap, "%s%s", cvs_req_args[cvs_req_nargs - 1],
                    271:                            line);
                    272:                        if (nap == NULL) {
                    273:                                cvs_log(LP_ERRNO,
                    274:                                    "failed to append to argument");
                    275:                                return (-1);
                    276:                        }
                    277:                        free(cvs_req_args[cvs_req_nargs - 1]);
                    278:                        cvs_req_args[cvs_req_nargs - 1] = nap;
                    279:                }
1.3       jfb       280:        }
                    281:
                    282:        return (0);
                    283: }
                    284:
                    285:
                    286: static int
                    287: cvs_req_globalopt(int reqid, char *line)
                    288: {
                    289:        if ((*line != '-') || (*(line + 2) != '\0')) {
                    290:                cvs_log(LP_ERR,
                    291:                    "invalid `Global_option' request format");
                    292:                return (-1);
                    293:        }
                    294:
                    295:        switch (*(line + 1)) {
                    296:        case 'l':
                    297:                cvs_nolog = 1;
                    298:                break;
                    299:        case 'n':
                    300:                break;
                    301:        case 'Q':
                    302:                verbosity = 0;
                    303:                break;
                    304:        case 'q':
                    305:                if (verbosity > 1)
                    306:                        verbosity = 1;
                    307:                break;
                    308:        case 'r':
                    309:                cvs_readonly = 1;
                    310:                break;
                    311:        case 't':
                    312:                cvs_trace = 1;
                    313:                break;
                    314:        default:
                    315:                cvs_log(LP_ERR, "unknown global option `%s'", line);
                    316:                return (-1);
1.2       jfb       317:        }
1.1       jfb       318:
                    319:        return (0);
                    320: }
                    321:
                    322:
                    323: static int
                    324: cvs_req_version(int reqid, char *line)
                    325: {
                    326:        cvs_printf("%s\n", CVS_VERSION);
                    327:        return (0);
                    328: }