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