xref: /openbsd-src/usr.bin/cvs/checkout.c (revision 11efff7f3ac2b3cfeff0c0cddc14294d9b3aca4f)
1 /*	$OpenBSD: checkout.c,v 1.12 2004/12/14 01:11:51 jfb Exp $	*/
2 /*
3  * Copyright (c) 2004 Jean-Francois Brousseau <jfb@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/types.h>
28 
29 #include <errno.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <unistd.h>
33 #include <string.h>
34 #include <sysexits.h>
35 
36 #include "cvs.h"
37 #include "log.h"
38 #include "file.h"
39 #include "proto.h"
40 
41 
42 
43 /*
44  * cvs_checkout()
45  *
46  * Handler for the `cvs checkout' command.
47  * Returns 0 on success, or one of the known system exit codes on failure.
48  */
49 int
50 cvs_checkout(int argc, char **argv)
51 {
52 	int i, ch;
53 	struct cvsroot *root;
54 
55 	while ((ch = getopt(argc, argv, "c")) != -1) {
56 		switch (ch) {
57 		case 'c':
58 			break;
59 		default:
60 			return (EX_USAGE);
61 		}
62 	}
63 
64 	argc -= optind;
65 	argv += optind;
66 
67 	if (argc == 0) {
68 		cvs_log(LP_ERR,
69 		    "must specify at least one module or directory");
70 		return (EX_USAGE);
71 	}
72 
73 	if ((cvs_files = cvs_file_get(".", 0)) == NULL)
74 		return (EX_USAGE);
75 
76 	root = CVS_DIR_ROOT(cvs_files);
77 	if (root == NULL) {
78 		cvs_log(LP_ERR,
79 		    "No CVSROOT specified!  Please use the `-d' option");
80 		cvs_log(LP_ERR,
81 		    "or set the CVSROOT environment variable.");
82 		return (EX_USAGE);
83 	}
84 	if (root->cr_method != CVS_METHOD_LOCAL) {
85 		if (cvs_connect(root) < 0)
86 			return (EX_DATAERR);
87 
88 		/* first send the expand modules command */
89 		for (i = 0; i < argc; i++)
90 			if (cvs_sendarg(root, argv[i], 0) < 0)
91 				break;
92 
93 		if ((cvs_senddir(root, cvs_files) < 0) ||
94 		    (cvs_sendreq(root, CVS_REQ_XPANDMOD, NULL) < 0))
95 			cvs_log(LP_ERR, "failed to expand module");
96 
97 		/* XXX not too sure why we have to send this arg */
98 		if (cvs_sendarg(root, "-N", 0) < 0)
99 			return (EX_PROTOCOL);
100 
101 		for (i = 0; i < argc; i++)
102 			if (cvs_sendarg(root, argv[i], 0) < 0)
103 				return (EX_PROTOCOL);
104 
105 		if ((cvs_senddir(root, cvs_files) < 0) ||
106 		    (cvs_sendreq(root, CVS_REQ_CO, NULL) < 0)) {
107 			cvs_log(LP_ERR, "failed to checkout");
108 			return (EX_PROTOCOL);
109 		}
110 	}
111 
112 	return (0);
113 }
114