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

Annotation of src/usr.bin/cvs/cmd.c, Revision 1.20

1.20    ! jfb         1: /*     $OpenBSD: cmd.c,v 1.19 2005/05/24 04:12:25 jfb Exp $    */
1.1       joris       2: /*
                      3:  * Copyright (c) 2005 Joris Vink <joris@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: #include <sys/param.h>
                     28: #include <sys/queue.h>
                     29: #include <sys/time.h>
                     30:
                     31: #include <stdlib.h>
                     32: #include <stdio.h>
                     33: #include <unistd.h>
                     34: #include <errno.h>
                     35: #include <string.h>
                     36:
                     37: #include "cvs.h"
                     38: #include "log.h"
                     39: #include "rcs.h"
                     40: #include "proto.h"
                     41:
1.19      jfb        42:
                     43: /*
                     44:  * Command dispatch table
                     45:  * ----------------------
                     46:  *
                     47:  * The synopsis field should only contain the list of arguments that the
                     48:  * command supports, without the actual command's name.
                     49:  *
                     50:  * Command handlers are expected to return 0 if no error occurred, or one of
                     51:  * the CVS_EX_* error codes in case of an error.  In case the error
                     52:  * returned is 1, the command's usage string is printed to standard
                     53:  * error before returning.
                     54:  */
                     55: struct cvs_cmd *cvs_cdt[] = {
                     56:        &cvs_cmd_add,
                     57:        &cvs_cmd_admin,
                     58:        &cvs_cmd_annotate,
                     59:        &cvs_cmd_checkout,
                     60:        &cvs_cmd_commit,
                     61:        &cvs_cmd_diff,
                     62: #if 0
                     63:        &cvs_cmd_edit,
                     64:        &cvs_cmd_editors,
                     65:        &cvs_cmd_export,
                     66: #endif
                     67:        &cvs_cmd_history,
                     68:        &cvs_cmd_import,
                     69:        &cvs_cmd_init,
                     70: #if defined(HAVE_KERBEROS)
                     71:        &cvs_cmd_kserver,
                     72: #endif
                     73:        &cvs_cmd_log,
                     74: #if 0
                     75:        &cvs_cmd_login,
                     76:        &cvs_cmd_logout,
                     77: #endif
                     78:        &cvs_cmd_rdiff,
                     79:        &cvs_cmd_release,
                     80:        &cvs_cmd_remove,
                     81:        &cvs_cmd_rlog,
                     82:        &cvs_cmd_rtag,
                     83:        &cvs_cmd_server,
                     84:        &cvs_cmd_status,
                     85:        &cvs_cmd_tag,
                     86: #if 0
                     87:        &cvs_cmd_unedit,
                     88: #endif
                     89:        &cvs_cmd_update,
                     90:        &cvs_cmd_version,
                     91: #if 0
                     92:        &cvs_cmd_watch,
                     93:        &cvs_cmd_watchers,
                     94: #endif
                     95:        NULL
                     96: };
                     97:
                     98:
                     99:
                    100: /*
                    101:  * cvs_findcmd()
                    102:  *
                    103:  * Find the entry in the command dispatch table whose name or one of its
                    104:  * aliases matches <cmd>.
                    105:  * Returns a pointer to the command entry on success, NULL on failure.
                    106:  */
                    107: struct cvs_cmd*
                    108: cvs_findcmd(const char *cmd)
                    109: {
                    110:        int i, j;
                    111:        struct cvs_cmd *cmdp;
                    112:
                    113:        cmdp = NULL;
                    114:
                    115:        for (i = 0; (cvs_cdt[i] != NULL) && (cmdp == NULL); i++) {
                    116:                if (strcmp(cmd, cvs_cdt[i]->cmd_name) == 0)
                    117:                        cmdp = cvs_cdt[i];
                    118:                else {
                    119:                        for (j = 0; j < CVS_CMD_MAXALIAS; j++) {
                    120:                                if (strcmp(cmd,
                    121:                                    cvs_cdt[i]->cmd_alias[j]) == 0) {
                    122:                                        cmdp = cvs_cdt[i];
                    123:                                        break;
                    124:                                }
                    125:                        }
                    126:                }
                    127:        }
                    128:
                    129:        return (cmdp);
                    130: }
                    131:
                    132: struct cvs_cmd*
                    133: cvs_findcmdbyreq(int reqid)
                    134: {
                    135:        int i;
                    136:        struct cvs_cmd *cmdp;
                    137:
                    138:        cmdp = NULL;
                    139:        for (i = 0; cvs_cdt[i] != NULL; i++)
                    140:                if (cvs_cdt[i]->cmd_req == reqid) {
                    141:                        cmdp = cvs_cdt[i];
                    142:                        break;
                    143:                }
                    144:
                    145:        return (cmdp);
                    146: }
                    147:
                    148:
1.1       joris     149: /*
                    150:  * start the execution of a command.
                    151:  */
                    152: int
                    153: cvs_startcmd(struct cvs_cmd *cmd, int argc, char **argv)
                    154: {
                    155:        int i;
                    156:        int ret;
                    157:        struct cvsroot *root;
1.19      jfb       158:        int (*ex_hdlr)(CVSFILE *, void *);
1.5       joris     159:
                    160:        /* if the command requested is the server one, just call the
                    161:         * cvs_server() function to handle it, and return after it.
                    162:         */
1.19      jfb       163:        if (cmd->cmd_op == CVS_OP_SERVER)
                    164:                return cvs_server(argc, argv);
1.1       joris     165:
1.19      jfb       166:        if (cmd->cmd_init != NULL) {
                    167:                if ((ret = (*cmd->cmd_init)(cmd, argc, argv, &i)) != 0)
1.3       joris     168:                        return (ret);
                    169:
                    170:                argc -= i;
                    171:                argv += i;
                    172:        }
1.1       joris     173:
1.19      jfb       174:        if (!(cmd->cmd_flags & CVS_CMD_ALLOWSPEC) && (argc > 0))
                    175:                return (CVS_EX_USAGE);
1.1       joris     176:
1.17      joris     177:        if ((root = cvsroot_get(".")) == NULL)
1.15      jfb       178:                return (CVS_EX_BADROOT);
1.18      xsa       179:
1.19      jfb       180:        if ((root->cr_method != CVS_METHOD_LOCAL) && (cvs_connect(root) < 0))
                    181:                return (CVS_EX_PROTO);
1.18      xsa       182:        if (cvs_trace)
                    183:                cvs_log(LP_TRACE, "cvs_startcmd() CVSROOT=%s", root->cr_str);
1.1       joris     184:
1.19      jfb       185:        if (cmd->cmd_pre_exec != NULL) {
                    186:                if ((ret = cmd->cmd_pre_exec(root)) != 0)
                    187:                        return (ret);
1.1       joris     188:        }
1.6       joris     189:
1.19      jfb       190:        if (root->cr_method == CVS_METHOD_LOCAL)
                    191:                ex_hdlr = cmd->cmd_exec_local;
                    192:        else
                    193:                ex_hdlr = cmd->cmd_exec_remote;
                    194:
                    195:        if (argc > 0) {
                    196:                cvs_files = cvs_file_getspec(argv, argc, cmd->file_flags,
                    197:                    ex_hdlr, NULL);
1.17      joris     198:        } else {
1.19      jfb       199:                cvs_files = cvs_file_get(".", cmd->file_flags,
                    200:                    ex_hdlr, NULL);
1.17      joris     201:        }
                    202:
                    203:        if (cvs_files == NULL)
                    204:                return (CVS_EX_DATA);
1.1       joris     205:
1.19      jfb       206:        if (cmd->cmd_post_exec != NULL) {
                    207:                if ((ret = cmd->cmd_post_exec(root)) != 0)
                    208:                        return (ret);
                    209:        }
                    210:
1.1       joris     211:        if (root->cr_method != CVS_METHOD_LOCAL) {
1.19      jfb       212:                if (cmd->cmd_flags & CVS_CMD_SENDDIR) {
1.1       joris     213:                        if (cvs_senddir(root, cvs_files) < 0)
1.11      joris     214:                                return (CVS_EX_PROTO);
1.1       joris     215:                }
                    216:
1.19      jfb       217:                if (cmd->cmd_flags & CVS_CMD_SENDARGS2) {
1.1       joris     218:                        for (i = 0; i < argc; i++) {
                    219:                                if (cvs_sendarg(root, argv[i], 0) < 0)
1.11      joris     220:                                        return (CVS_EX_PROTO);
1.1       joris     221:                        }
                    222:                }
                    223:
1.19      jfb       224:                if (cmd->cmd_req != CVS_REQ_NONE && cvs_sendreq(root, cmd->cmd_req,
1.1       joris     225:                    (cmd->cmd_op == CVS_OP_INIT) ? root->cr_dir : NULL) < 0)
1.11      joris     226:                        return (CVS_EX_PROTO);
1.1       joris     227:        }
1.19      jfb       228:
                    229:        if (cmd->cmd_cleanup != NULL)
                    230:                (*cmd->cmd_cleanup)();
1.1       joris     231:
                    232:        return (0);
                    233: }