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

1.34    ! joris       1: /*     $OpenBSD: cmd.c,v 1.33 2005/07/26 16:03:20 xsa 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/queue.h>
                     28: #include <sys/time.h>
1.34    ! joris      29: #include <sys/stat.h>
1.1       joris      30:
1.22      xsa        31: #include <errno.h>
                     32: #include <stdio.h>
1.1       joris      33: #include <stdlib.h>
1.22      xsa        34: #include <string.h>
1.1       joris      35: #include <unistd.h>
                     36:
                     37: #include "cvs.h"
                     38: #include "log.h"
                     39: #include "proto.h"
                     40:
1.19      jfb        41:
                     42: /*
                     43:  * Command dispatch table
                     44:  * ----------------------
                     45:  *
                     46:  * The synopsis field should only contain the list of arguments that the
                     47:  * command supports, without the actual command's name.
                     48:  *
                     49:  * Command handlers are expected to return 0 if no error occurred, or one of
                     50:  * the CVS_EX_* error codes in case of an error.  In case the error
                     51:  * returned is 1, the command's usage string is printed to standard
                     52:  * error before returning.
                     53:  */
                     54: struct cvs_cmd *cvs_cdt[] = {
                     55:        &cvs_cmd_add,
                     56:        &cvs_cmd_admin,
                     57:        &cvs_cmd_annotate,
                     58:        &cvs_cmd_checkout,
                     59:        &cvs_cmd_commit,
                     60:        &cvs_cmd_diff,
                     61:        &cvs_cmd_edit,
                     62:        &cvs_cmd_editors,
                     63:        &cvs_cmd_export,
                     64:        &cvs_cmd_history,
                     65:        &cvs_cmd_import,
                     66:        &cvs_cmd_init,
                     67: #if defined(HAVE_KERBEROS)
                     68:        &cvs_cmd_kserver,
                     69: #endif
                     70:        &cvs_cmd_log,
                     71: #if 0
                     72:        &cvs_cmd_login,
                     73:        &cvs_cmd_logout,
                     74: #endif
                     75:        &cvs_cmd_rdiff,
                     76:        &cvs_cmd_release,
                     77:        &cvs_cmd_remove,
                     78:        &cvs_cmd_rlog,
                     79:        &cvs_cmd_rtag,
                     80:        &cvs_cmd_server,
                     81:        &cvs_cmd_status,
                     82:        &cvs_cmd_tag,
                     83:        &cvs_cmd_unedit,
                     84:        &cvs_cmd_update,
                     85:        &cvs_cmd_version,
                     86:        &cvs_cmd_watch,
                     87:        &cvs_cmd_watchers,
                     88:        NULL
                     89: };
                     90:
1.34    ! joris      91: #define MISSING_CVS_DIR                0x01
        !            92: #define MISSING_CVS_ENTRIES    0x02
        !            93: #define MISSING_CVS_REPO       0x04
1.19      jfb        94:
                     95: /*
                     96:  * cvs_findcmd()
                     97:  *
                     98:  * Find the entry in the command dispatch table whose name or one of its
                     99:  * aliases matches <cmd>.
                    100:  * Returns a pointer to the command entry on success, NULL on failure.
                    101:  */
1.32      xsa       102: struct cvs_cmd *
1.19      jfb       103: cvs_findcmd(const char *cmd)
                    104: {
                    105:        int i, j;
                    106:        struct cvs_cmd *cmdp;
                    107:
                    108:        cmdp = NULL;
                    109:
                    110:        for (i = 0; (cvs_cdt[i] != NULL) && (cmdp == NULL); i++) {
                    111:                if (strcmp(cmd, cvs_cdt[i]->cmd_name) == 0)
                    112:                        cmdp = cvs_cdt[i];
                    113:                else {
                    114:                        for (j = 0; j < CVS_CMD_MAXALIAS; j++) {
                    115:                                if (strcmp(cmd,
                    116:                                    cvs_cdt[i]->cmd_alias[j]) == 0) {
                    117:                                        cmdp = cvs_cdt[i];
                    118:                                        break;
                    119:                                }
                    120:                        }
                    121:                }
                    122:        }
                    123:
                    124:        return (cmdp);
                    125: }
                    126:
1.32      xsa       127: struct cvs_cmd *
1.19      jfb       128: cvs_findcmdbyreq(int reqid)
                    129: {
                    130:        int i;
                    131:        struct cvs_cmd *cmdp;
                    132:
                    133:        cmdp = NULL;
                    134:        for (i = 0; cvs_cdt[i] != NULL; i++)
                    135:                if (cvs_cdt[i]->cmd_req == reqid) {
                    136:                        cmdp = cvs_cdt[i];
                    137:                        break;
                    138:                }
                    139:
                    140:        return (cmdp);
                    141: }
                    142:
                    143:
1.1       joris     144: /*
                    145:  * start the execution of a command.
                    146:  */
                    147: int
                    148: cvs_startcmd(struct cvs_cmd *cmd, int argc, char **argv)
                    149: {
1.34    ! joris     150:        int i, ret, error;
1.1       joris     151:        struct cvsroot *root;
1.19      jfb       152:        int (*ex_hdlr)(CVSFILE *, void *);
1.31      joris     153:        CVSFILE *cf;
1.34    ! joris     154:        struct stat st;
1.5       joris     155:
                    156:        /* if the command requested is the server one, just call the
                    157:         * cvs_server() function to handle it, and return after it.
                    158:         */
1.19      jfb       159:        if (cmd->cmd_op == CVS_OP_SERVER)
                    160:                return cvs_server(argc, argv);
1.1       joris     161:
1.25      xsa       162:        if ((root = cvsroot_get(".")) == NULL)
                    163:                return (CVS_EX_BADROOT);
                    164:
1.23      joris     165:        i = 1;
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);
1.23      joris     169:        }
1.3       joris     170:
1.23      joris     171:        argc -= i;
                    172:        argv += i;
1.34    ! joris     173:
        !           174:        /*
        !           175:         * Check if we have the administrative files present, if we are
        !           176:         * missing one, we will error out because we cannot continue.
        !           177:         *
        !           178:         * We are not checking for CVS/Root since we fetched the root
        !           179:         * above via cvsroot_get().
        !           180:         *
        !           181:         * checkout, export, import and release do not depend on these files.
        !           182:         */
        !           183:        error = 0;
        !           184:        if ((cmd->cmd_op != CVS_OP_CHECKOUT) && (cmd->cmd_op != CVS_OP_EXPORT) &&
        !           185:            (cmd->cmd_op != CVS_OP_IMPORT) && (cmd->cmd_op != CVS_OP_RELEASE)) {
        !           186:                /* check for the CVS directory */
        !           187:                ret = stat(CVS_PATH_CVSDIR, &st);
        !           188:                if (((ret == -1) && (errno == ENOENT)) || ((ret != -1) &&
        !           189:                    !(S_ISDIR(st.st_mode))))
        !           190:                        error |= MISSING_CVS_DIR;
        !           191:
        !           192:                /* check if the CVS/Entries file exists */
        !           193:                ret = stat(CVS_PATH_ENTRIES, &st);
        !           194:                if (((ret == -1) && (errno == ENOENT)) || ((ret != -1) &&
        !           195:                    !(S_ISREG(st.st_mode))))
        !           196:                        error |= MISSING_CVS_ENTRIES;
        !           197:
        !           198:                /* check if the CVS/Repository file exists */
        !           199:                ret = stat(CVS_PATH_REPOSITORY, &st);
        !           200:                if (((ret == -1) && (errno == ENOENT)) || ((ret != -1) &&
        !           201:                    !(S_ISREG(st.st_mode))))
        !           202:                        error |= MISSING_CVS_REPO;
        !           203:        }
        !           204:
        !           205:        if (error > 0) {
        !           206:                if (error & MISSING_CVS_DIR) {
        !           207:                        cvs_log(LP_ABORT, "missing '%s' directory",
        !           208:                            CVS_PATH_CVSDIR);
        !           209:                        return (CVS_EX_FILE);
        !           210:                }
        !           211:
        !           212:                if (error & MISSING_CVS_ENTRIES)
        !           213:                        cvs_log(LP_ABORT, "missing '%s' file", CVS_PATH_ENTRIES);
        !           214:                if (error & MISSING_CVS_REPO)
        !           215:                        cvs_log(LP_ABORT, "missing '%s' file", CVS_PATH_REPOSITORY);
        !           216:                return (CVS_EX_FILE);
        !           217:        }
1.1       joris     218:
1.19      jfb       219:        if (!(cmd->cmd_flags & CVS_CMD_ALLOWSPEC) && (argc > 0))
                    220:                return (CVS_EX_USAGE);
1.18      xsa       221:
1.33      xsa       222:        cvs_log(LP_TRACE, "cvs_startcmd() CVSROOT=%s", root->cr_str);
                    223:
1.19      jfb       224:        if ((root->cr_method != CVS_METHOD_LOCAL) && (cvs_connect(root) < 0))
                    225:                return (CVS_EX_PROTO);
1.1       joris     226:
1.19      jfb       227:        if (cmd->cmd_pre_exec != NULL) {
                    228:                if ((ret = cmd->cmd_pre_exec(root)) != 0)
                    229:                        return (ret);
1.1       joris     230:        }
1.6       joris     231:
1.19      jfb       232:        if (root->cr_method == CVS_METHOD_LOCAL)
                    233:                ex_hdlr = cmd->cmd_exec_local;
                    234:        else
                    235:                ex_hdlr = cmd->cmd_exec_remote;
                    236:
                    237:        if (argc > 0) {
1.31      joris     238:                ret = cvs_file_getspec(argv, argc, cmd->file_flags,
                    239:                    ex_hdlr, NULL, NULL);
1.17      joris     240:        } else {
1.31      joris     241:                ret = cvs_file_get(".", cmd->file_flags,
                    242:                    ex_hdlr, NULL, NULL);
1.17      joris     243:        }
                    244:
1.31      joris     245:        if (ret != CVS_EX_OK)
                    246:                return (cvs_error);
1.26      joris     247:
1.19      jfb       248:        if (cmd->cmd_post_exec != NULL) {
                    249:                if ((ret = cmd->cmd_post_exec(root)) != 0)
                    250:                        return (ret);
                    251:        }
                    252:
1.1       joris     253:        if (root->cr_method != CVS_METHOD_LOCAL) {
1.31      joris     254:                /*
                    255:                 * If we have to send the directory the command
                    256:                 * has been issued in, obtain it.
                    257:                 */
1.19      jfb       258:                if (cmd->cmd_flags & CVS_CMD_SENDDIR) {
1.31      joris     259:                        cf = cvs_file_loadinfo(".", CF_NOFILES, NULL, NULL, 1);
                    260:                        if (cf == NULL)
                    261:                                return (CVS_EX_DATA);
                    262:                        if (cvs_senddir(root, cf) < 0) {
                    263:                                cvs_file_free(cf);
1.11      joris     264:                                return (CVS_EX_PROTO);
1.31      joris     265:                        }
                    266:                        cvs_file_free(cf);
1.1       joris     267:                }
                    268:
1.19      jfb       269:                if (cmd->cmd_flags & CVS_CMD_SENDARGS2) {
1.1       joris     270:                        for (i = 0; i < argc; i++) {
                    271:                                if (cvs_sendarg(root, argv[i], 0) < 0)
1.11      joris     272:                                        return (CVS_EX_PROTO);
1.1       joris     273:                        }
                    274:                }
                    275:
1.31      joris     276:                if (cmd->cmd_req != CVS_REQ_NONE &&
                    277:                    cvs_sendreq(root, cmd->cmd_req,
1.1       joris     278:                    (cmd->cmd_op == CVS_OP_INIT) ? root->cr_dir : NULL) < 0)
1.11      joris     279:                        return (CVS_EX_PROTO);
1.1       joris     280:        }
1.19      jfb       281:
                    282:        if (cmd->cmd_cleanup != NULL)
                    283:                (*cmd->cmd_cleanup)();
1.24      joris     284:
1.31      joris     285: #if 0
1.29      joris     286:        if (cvs_cmdop != CVS_OP_SERVER && cmd->cmd_flags & CVS_CMD_PRUNEDIRS)
1.26      joris     287:                cvs_file_prune(fpath);
1.31      joris     288: #endif
1.28      joris     289:
1.24      joris     290:        if (root->cr_method != CVS_METHOD_LOCAL)
                    291:                cvs_disconnect(root);
1.1       joris     292:
                    293:        return (0);
                    294: }