Annotation of src/usr.bin/cvs/cmd.c, Revision 1.22
1.22 ! xsa 1: /* $OpenBSD: cmd.c,v 1.21 2005/05/25 04:12:54 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:
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: #if 0
62: &cvs_cmd_edit,
63: &cvs_cmd_editors,
64: &cvs_cmd_export,
65: #endif
66: &cvs_cmd_history,
67: &cvs_cmd_import,
68: &cvs_cmd_init,
69: #if defined(HAVE_KERBEROS)
70: &cvs_cmd_kserver,
71: #endif
72: &cvs_cmd_log,
73: #if 0
74: &cvs_cmd_login,
75: &cvs_cmd_logout,
76: #endif
77: &cvs_cmd_rdiff,
78: &cvs_cmd_release,
79: &cvs_cmd_remove,
80: &cvs_cmd_rlog,
81: &cvs_cmd_rtag,
82: &cvs_cmd_server,
83: &cvs_cmd_status,
84: &cvs_cmd_tag,
85: #if 0
86: &cvs_cmd_unedit,
87: #endif
88: &cvs_cmd_update,
89: &cvs_cmd_version,
90: #if 0
91: &cvs_cmd_watch,
92: &cvs_cmd_watchers,
93: #endif
94: NULL
95: };
96:
97:
98:
99: /*
100: * cvs_findcmd()
101: *
102: * Find the entry in the command dispatch table whose name or one of its
103: * aliases matches <cmd>.
104: * Returns a pointer to the command entry on success, NULL on failure.
105: */
106: struct cvs_cmd*
107: cvs_findcmd(const char *cmd)
108: {
109: int i, j;
110: struct cvs_cmd *cmdp;
111:
112: cmdp = NULL;
113:
114: for (i = 0; (cvs_cdt[i] != NULL) && (cmdp == NULL); i++) {
115: if (strcmp(cmd, cvs_cdt[i]->cmd_name) == 0)
116: cmdp = cvs_cdt[i];
117: else {
118: for (j = 0; j < CVS_CMD_MAXALIAS; j++) {
119: if (strcmp(cmd,
120: cvs_cdt[i]->cmd_alias[j]) == 0) {
121: cmdp = cvs_cdt[i];
122: break;
123: }
124: }
125: }
126: }
127:
128: return (cmdp);
129: }
130:
131: struct cvs_cmd*
132: cvs_findcmdbyreq(int reqid)
133: {
134: int i;
135: struct cvs_cmd *cmdp;
136:
137: cmdp = NULL;
138: for (i = 0; cvs_cdt[i] != NULL; i++)
139: if (cvs_cdt[i]->cmd_req == reqid) {
140: cmdp = cvs_cdt[i];
141: break;
142: }
143:
144: return (cmdp);
145: }
146:
147:
1.1 joris 148: /*
149: * start the execution of a command.
150: */
151: int
152: cvs_startcmd(struct cvs_cmd *cmd, int argc, char **argv)
153: {
154: int i;
155: int ret;
156: struct cvsroot *root;
1.19 jfb 157: int (*ex_hdlr)(CVSFILE *, void *);
1.5 joris 158:
159: /* if the command requested is the server one, just call the
160: * cvs_server() function to handle it, and return after it.
161: */
1.19 jfb 162: if (cmd->cmd_op == CVS_OP_SERVER)
163: return cvs_server(argc, argv);
1.1 joris 164:
1.19 jfb 165: if (cmd->cmd_init != NULL) {
166: if ((ret = (*cmd->cmd_init)(cmd, argc, argv, &i)) != 0)
1.3 joris 167: return (ret);
168:
169: argc -= i;
170: argv += i;
171: }
1.1 joris 172:
1.19 jfb 173: if (!(cmd->cmd_flags & CVS_CMD_ALLOWSPEC) && (argc > 0))
174: return (CVS_EX_USAGE);
1.1 joris 175:
1.17 joris 176: if ((root = cvsroot_get(".")) == NULL)
1.15 jfb 177: return (CVS_EX_BADROOT);
1.18 xsa 178:
1.19 jfb 179: if ((root->cr_method != CVS_METHOD_LOCAL) && (cvs_connect(root) < 0))
180: return (CVS_EX_PROTO);
1.21 jfb 181:
182: cvs_log(LP_TRACE, "cvs_startcmd() CVSROOT=%s", root->cr_str);
1.1 joris 183:
1.19 jfb 184: if (cmd->cmd_pre_exec != NULL) {
185: if ((ret = cmd->cmd_pre_exec(root)) != 0)
186: return (ret);
1.1 joris 187: }
1.6 joris 188:
1.19 jfb 189: if (root->cr_method == CVS_METHOD_LOCAL)
190: ex_hdlr = cmd->cmd_exec_local;
191: else
192: ex_hdlr = cmd->cmd_exec_remote;
193:
194: if (argc > 0) {
195: cvs_files = cvs_file_getspec(argv, argc, cmd->file_flags,
196: ex_hdlr, NULL);
1.17 joris 197: } else {
1.19 jfb 198: cvs_files = cvs_file_get(".", cmd->file_flags,
199: ex_hdlr, NULL);
1.17 joris 200: }
201:
202: if (cvs_files == NULL)
203: return (CVS_EX_DATA);
1.1 joris 204:
1.19 jfb 205: if (cmd->cmd_post_exec != NULL) {
206: if ((ret = cmd->cmd_post_exec(root)) != 0)
207: return (ret);
208: }
209:
1.1 joris 210: if (root->cr_method != CVS_METHOD_LOCAL) {
1.19 jfb 211: if (cmd->cmd_flags & CVS_CMD_SENDDIR) {
1.1 joris 212: if (cvs_senddir(root, cvs_files) < 0)
1.11 joris 213: return (CVS_EX_PROTO);
1.1 joris 214: }
215:
1.19 jfb 216: if (cmd->cmd_flags & CVS_CMD_SENDARGS2) {
1.1 joris 217: for (i = 0; i < argc; i++) {
218: if (cvs_sendarg(root, argv[i], 0) < 0)
1.11 joris 219: return (CVS_EX_PROTO);
1.1 joris 220: }
221: }
222:
1.19 jfb 223: if (cmd->cmd_req != CVS_REQ_NONE && cvs_sendreq(root, cmd->cmd_req,
1.1 joris 224: (cmd->cmd_op == CVS_OP_INIT) ? root->cr_dir : NULL) < 0)
1.11 joris 225: return (CVS_EX_PROTO);
1.1 joris 226: }
1.19 jfb 227:
228: if (cmd->cmd_cleanup != NULL)
229: (*cmd->cmd_cleanup)();
1.1 joris 230:
231: return (0);
232: }