[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.3

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 *);
                     59: static int  cvs_req_directory  (int, char *);
1.2       jfb        60: static int  cvs_req_argument   (int, char *);
1.3     ! jfb        61: static int  cvs_req_globalopt  (int, char *);
1.1       jfb        62: static int  cvs_req_version    (int, char *);
                     63:
                     64:
                     65: struct cvs_reqhdlr {
                     66:        int (*hdlr)(int, char *);
                     67: } cvs_req_swtab[CVS_REQ_MAX + 1] = {
                     68:        { NULL               },
                     69:        { cvs_req_root       },
                     70:        { NULL               },
                     71:        { NULL               },
                     72:        { cvs_req_directory  },
                     73:        { NULL               },
                     74:        { NULL               },
                     75:        { NULL               },
                     76:        { NULL               },
                     77:        { NULL               },
                     78:        { NULL               }, /* 10 */
                     79:        { NULL               },
                     80:        { NULL               },
                     81:        { NULL               },
                     82:        { NULL               },
                     83:        { NULL               },
                     84:        { NULL               },
                     85:        { NULL               },
                     86:        { NULL               },
                     87:        { NULL               },
1.2       jfb        88:        { cvs_req_argument   }, /* 20 */
                     89:        { cvs_req_argument   },
1.3     ! jfb        90:        { cvs_req_globalopt  },
1.1       jfb        91:        { NULL               },
                     92:        { NULL               },
                     93:        { NULL               },
                     94:        { NULL               },
                     95:        { NULL               },
                     96:        { NULL               },
                     97:        { NULL               },
                     98:        { NULL               }, /* 30 */
                     99:        { NULL               },
                    100:        { NULL               },
                    101:        { NULL               },
                    102:        { NULL               },
                    103:        { NULL               },
                    104:        { NULL               },
                    105:        { NULL               },
                    106:        { NULL               },
                    107:        { NULL               },
                    108:        { NULL               }, /* 40 */
                    109:        { NULL               },
                    110:        { NULL               },
                    111:        { NULL               },
                    112:        { NULL               },
                    113:        { NULL               },
                    114:        { NULL               },
                    115:        { NULL               },
                    116:        { NULL               },
                    117:        { NULL               },
                    118:        { NULL               }, /* 50 */
                    119:        { NULL               },
                    120:        { NULL               },
                    121:        { NULL               },
                    122:        { NULL               },
                    123:        { NULL               },
                    124:        { NULL               },
                    125:        { NULL               },
                    126:        { NULL               },
                    127:        { NULL               },
                    128:        { NULL               }, /* 60 */
                    129:        { NULL               },
                    130:        { NULL               },
                    131:        { NULL               },
                    132:        { NULL               },
                    133:        { NULL               },
                    134:        { NULL               },
                    135:        { NULL               },
                    136:        { NULL               },
                    137:        { cvs_req_version    },
                    138: };
                    139:
                    140:
                    141:
                    142: /*
1.2       jfb       143:  * Argument array built by `Argument' and `Argumentx' requests.
                    144:  */
                    145:
                    146: static char *cvs_req_args[CVS_PROTO_MAXARG];
                    147: static int   cvs_req_nargs = 0;
                    148:
                    149:
                    150:
                    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:
                    161: int
                    162: cvs_req_handle(char *line)
                    163: {
                    164:        char *cp, *cmd;
                    165:        struct cvs_req *req;
                    166:
                    167:        cmd = line;
                    168:
                    169:        cp = strchr(cmd, ' ');
                    170:        if (cp != NULL)
                    171:                *(cp++) = '\0';
                    172:
                    173:        req = cvs_req_getbyname(cmd);
                    174:        if (req == NULL)
                    175:                return (-1);
                    176:        else if (cvs_req_swtab[req->req_id].hdlr == NULL) {
                    177:                cvs_log(LP_ERRNO, "handler for `%s' not implemented", cmd);
                    178:                return (-1);
                    179:        }
                    180:
                    181:        return (*cvs_req_swtab[req->req_id].hdlr)(req->req_id, cp);
                    182: }
                    183:
                    184:
                    185:
                    186: static int
                    187: cvs_req_root(int reqid, char *line)
                    188: {
                    189:
                    190:
                    191:
                    192:        return (0);
                    193: }
                    194:
                    195: static int
                    196: cvs_req_directory(int reqid, char *line)
                    197: {
                    198:
                    199:
1.2       jfb       200:
                    201:        return (0);
                    202: }
                    203:
                    204:
                    205: static int
                    206: cvs_req_argument(int reqid, char *line)
                    207: {
                    208:        char *nap;
                    209:
                    210:        if (cvs_req_nargs == CVS_PROTO_MAXARG) {
                    211:                cvs_log(LP_ERR, "too many arguments");
                    212:                return (-1);
                    213:        }
                    214:
                    215:        if (reqid == CVS_REQ_ARGUMENT) {
                    216:                cvs_req_args[cvs_req_nargs] = strdup(line);
                    217:                if (cvs_req_args[cvs_req_nargs] == NULL) {
                    218:                        cvs_log(LP_ERRNO, "failed to copy argument");
                    219:                        return (-1);
                    220:                }
                    221:                cvs_req_nargs++;
                    222:        }
                    223:        else if (reqid == CVS_REQ_ARGUMENTX) {
                    224:                if (cvs_req_nargs == 0)
                    225:                        cvs_log(LP_WARN, "no argument to append to");
                    226:                else {
                    227:                        asprintf(&nap, "%s%s", cvs_req_args[cvs_req_nargs - 1],
                    228:                            line);
                    229:                        if (nap == NULL) {
                    230:                                cvs_log(LP_ERRNO,
                    231:                                    "failed to append to argument");
                    232:                                return (-1);
                    233:                        }
                    234:                        free(cvs_req_args[cvs_req_nargs - 1]);
                    235:                        cvs_req_args[cvs_req_nargs - 1] = nap;
                    236:                }
1.3     ! jfb       237:        }
        !           238:
        !           239:        return (0);
        !           240: }
        !           241:
        !           242:
        !           243: static int
        !           244: cvs_req_globalopt(int reqid, char *line)
        !           245: {
        !           246:        if ((*line != '-') || (*(line + 2) != '\0')) {
        !           247:                cvs_log(LP_ERR,
        !           248:                    "invalid `Global_option' request format");
        !           249:                return (-1);
        !           250:        }
        !           251:
        !           252:        switch (*(line + 1)) {
        !           253:        case 'l':
        !           254:                cvs_nolog = 1;
        !           255:                break;
        !           256:        case 'n':
        !           257:                break;
        !           258:        case 'Q':
        !           259:                verbosity = 0;
        !           260:                break;
        !           261:        case 'q':
        !           262:                if (verbosity > 1)
        !           263:                        verbosity = 1;
        !           264:                break;
        !           265:        case 'r':
        !           266:                cvs_readonly = 1;
        !           267:                break;
        !           268:        case 't':
        !           269:                cvs_trace = 1;
        !           270:                break;
        !           271:        default:
        !           272:                cvs_log(LP_ERR, "unknown global option `%s'", line);
        !           273:                return (-1);
1.2       jfb       274:        }
1.1       jfb       275:
                    276:        return (0);
                    277: }
                    278:
                    279:
                    280: static int
                    281: cvs_req_version(int reqid, char *line)
                    282: {
                    283:        cvs_printf("%s\n", CVS_VERSION);
                    284:        return (0);
                    285: }