Annotation of src/usr.bin/cvs/cmd.c, Revision 1.27
1.27 ! xsa 1: /* $OpenBSD: cmd.c,v 1.26 2005/06/14 15:27:31 joris 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: &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:
91:
92:
93: /*
94: * cvs_findcmd()
95: *
96: * Find the entry in the command dispatch table whose name or one of its
97: * aliases matches <cmd>.
98: * Returns a pointer to the command entry on success, NULL on failure.
99: */
100: struct cvs_cmd*
101: cvs_findcmd(const char *cmd)
102: {
103: int i, j;
104: struct cvs_cmd *cmdp;
105:
106: cmdp = NULL;
107:
108: for (i = 0; (cvs_cdt[i] != NULL) && (cmdp == NULL); i++) {
109: if (strcmp(cmd, cvs_cdt[i]->cmd_name) == 0)
110: cmdp = cvs_cdt[i];
111: else {
112: for (j = 0; j < CVS_CMD_MAXALIAS; j++) {
113: if (strcmp(cmd,
114: cvs_cdt[i]->cmd_alias[j]) == 0) {
115: cmdp = cvs_cdt[i];
116: break;
117: }
118: }
119: }
120: }
121:
122: return (cmdp);
123: }
124:
125: struct cvs_cmd*
126: cvs_findcmdbyreq(int reqid)
127: {
128: int i;
129: struct cvs_cmd *cmdp;
130:
131: cmdp = NULL;
132: for (i = 0; cvs_cdt[i] != NULL; i++)
133: if (cvs_cdt[i]->cmd_req == reqid) {
134: cmdp = cvs_cdt[i];
135: break;
136: }
137:
138: return (cmdp);
139: }
140:
141:
1.1 joris 142: /*
143: * start the execution of a command.
144: */
145: int
146: cvs_startcmd(struct cvs_cmd *cmd, int argc, char **argv)
147: {
148: int i;
149: int ret;
150: struct cvsroot *root;
1.19 jfb 151: int (*ex_hdlr)(CVSFILE *, void *);
1.26 joris 152: char fpath[MAXPATHLEN];
1.5 joris 153:
154: /* if the command requested is the server one, just call the
155: * cvs_server() function to handle it, and return after it.
156: */
1.19 jfb 157: if (cmd->cmd_op == CVS_OP_SERVER)
158: return cvs_server(argc, argv);
1.1 joris 159:
1.25 xsa 160: if ((root = cvsroot_get(".")) == NULL)
161: return (CVS_EX_BADROOT);
162:
1.23 joris 163: i = 1;
1.19 jfb 164: if (cmd->cmd_init != NULL) {
165: if ((ret = (*cmd->cmd_init)(cmd, argc, argv, &i)) != 0)
1.3 joris 166: return (ret);
1.23 joris 167: }
1.3 joris 168:
1.23 joris 169: argc -= i;
170: argv += i;
1.1 joris 171:
1.19 jfb 172: if (!(cmd->cmd_flags & CVS_CMD_ALLOWSPEC) && (argc > 0))
173: return (CVS_EX_USAGE);
1.18 xsa 174:
1.19 jfb 175: if ((root->cr_method != CVS_METHOD_LOCAL) && (cvs_connect(root) < 0))
176: return (CVS_EX_PROTO);
1.21 jfb 177:
178: cvs_log(LP_TRACE, "cvs_startcmd() CVSROOT=%s", root->cr_str);
1.1 joris 179:
1.19 jfb 180: if (cmd->cmd_pre_exec != NULL) {
181: if ((ret = cmd->cmd_pre_exec(root)) != 0)
182: return (ret);
1.1 joris 183: }
1.6 joris 184:
1.19 jfb 185: if (root->cr_method == CVS_METHOD_LOCAL)
186: ex_hdlr = cmd->cmd_exec_local;
187: else
188: ex_hdlr = cmd->cmd_exec_remote;
189:
190: if (argc > 0) {
191: cvs_files = cvs_file_getspec(argv, argc, cmd->file_flags,
192: ex_hdlr, NULL);
1.17 joris 193: } else {
1.19 jfb 194: cvs_files = cvs_file_get(".", cmd->file_flags,
195: ex_hdlr, NULL);
1.17 joris 196: }
197:
198: if (cvs_files == NULL)
199: return (CVS_EX_DATA);
1.1 joris 200:
1.26 joris 201: cvs_file_getpath(cvs_files, fpath, sizeof(fpath));
202:
1.19 jfb 203: if (cmd->cmd_post_exec != NULL) {
204: if ((ret = cmd->cmd_post_exec(root)) != 0)
205: return (ret);
206: }
207:
1.1 joris 208: if (root->cr_method != CVS_METHOD_LOCAL) {
1.19 jfb 209: if (cmd->cmd_flags & CVS_CMD_SENDDIR) {
1.1 joris 210: if (cvs_senddir(root, cvs_files) < 0)
1.11 joris 211: return (CVS_EX_PROTO);
1.1 joris 212: }
213:
1.19 jfb 214: if (cmd->cmd_flags & CVS_CMD_SENDARGS2) {
1.1 joris 215: for (i = 0; i < argc; i++) {
216: if (cvs_sendarg(root, argv[i], 0) < 0)
1.11 joris 217: return (CVS_EX_PROTO);
1.1 joris 218: }
219: }
220:
1.19 jfb 221: if (cmd->cmd_req != CVS_REQ_NONE && cvs_sendreq(root, cmd->cmd_req,
1.1 joris 222: (cmd->cmd_op == CVS_OP_INIT) ? root->cr_dir : NULL) < 0)
1.11 joris 223: return (CVS_EX_PROTO);
1.1 joris 224: }
1.19 jfb 225:
226: if (cmd->cmd_cleanup != NULL)
227: (*cmd->cmd_cleanup)();
1.24 joris 228:
1.26 joris 229: if (cmd->cmd_flags & CVS_CMD_PRUNEDIRS)
230: cvs_file_prune(fpath);
231:
1.24 joris 232: if (root->cr_method != CVS_METHOD_LOCAL)
233: cvs_disconnect(root);
1.1 joris 234:
235: return (0);
236: }