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