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