Annotation of src/usr.bin/cvs/cmd.c, Revision 1.19
1.19 ! jfb 1: /* $OpenBSD: cmd.c,v 1.18 2005/05/23 17:43:54 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/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: printf("[init]\n");
! 168: if ((ret = (*cmd->cmd_init)(cmd, argc, argv, &i)) != 0)
1.3 joris 169: return (ret);
170:
171: argc -= i;
172: argv += i;
173: }
1.1 joris 174:
1.19 ! jfb 175: if (!(cmd->cmd_flags & CVS_CMD_ALLOWSPEC) && (argc > 0))
! 176: return (CVS_EX_USAGE);
1.1 joris 177:
1.17 joris 178: if ((root = cvsroot_get(".")) == NULL)
1.15 jfb 179: return (CVS_EX_BADROOT);
1.18 xsa 180:
1.19 ! jfb 181: if ((root->cr_method != CVS_METHOD_LOCAL) && (cvs_connect(root) < 0))
! 182: return (CVS_EX_PROTO);
1.18 xsa 183: if (cvs_trace)
184: cvs_log(LP_TRACE, "cvs_startcmd() CVSROOT=%s", root->cr_str);
1.1 joris 185:
1.19 ! jfb 186: if (cmd->cmd_pre_exec != NULL) {
! 187: printf("[pre-exec]\n");
! 188: if ((ret = cmd->cmd_pre_exec(root)) != 0)
! 189: return (ret);
1.1 joris 190: }
1.6 joris 191:
1.19 ! jfb 192: if (root->cr_method == CVS_METHOD_LOCAL)
! 193: ex_hdlr = cmd->cmd_exec_local;
! 194: else
! 195: ex_hdlr = cmd->cmd_exec_remote;
! 196:
! 197: if (argc > 0) {
! 198: cvs_files = cvs_file_getspec(argv, argc, cmd->file_flags,
! 199: ex_hdlr, NULL);
1.17 joris 200: } else {
1.19 ! jfb 201: cvs_files = cvs_file_get(".", cmd->file_flags,
! 202: ex_hdlr, NULL);
1.17 joris 203: }
204:
205: if (cvs_files == NULL)
206: return (CVS_EX_DATA);
1.1 joris 207:
1.19 ! jfb 208: if (cmd->cmd_post_exec != NULL) {
! 209: printf("[post-exec]\n");
! 210: if ((ret = cmd->cmd_post_exec(root)) != 0)
! 211: return (ret);
! 212: }
! 213:
1.1 joris 214: if (root->cr_method != CVS_METHOD_LOCAL) {
1.19 ! jfb 215: if (cmd->cmd_flags & CVS_CMD_SENDDIR) {
1.1 joris 216: if (cvs_senddir(root, cvs_files) < 0)
1.11 joris 217: return (CVS_EX_PROTO);
1.1 joris 218: }
219:
1.19 ! jfb 220: if (cmd->cmd_flags & CVS_CMD_SENDARGS2) {
1.1 joris 221: for (i = 0; i < argc; i++) {
222: if (cvs_sendarg(root, argv[i], 0) < 0)
1.11 joris 223: return (CVS_EX_PROTO);
1.1 joris 224: }
225: }
226:
1.19 ! jfb 227: if (cmd->cmd_req != CVS_REQ_NONE && cvs_sendreq(root, cmd->cmd_req,
1.1 joris 228: (cmd->cmd_op == CVS_OP_INIT) ? root->cr_dir : NULL) < 0)
1.11 joris 229: return (CVS_EX_PROTO);
1.1 joris 230: }
1.19 ! jfb 231:
! 232: if (cmd->cmd_cleanup != NULL)
! 233: (*cmd->cmd_cleanup)();
1.1 joris 234:
235: return (0);
236: }