Annotation of src/usr.bin/cvs/add.c, Revision 1.21
1.21 ! jfb 1: /* $OpenBSD: add.c,v 1.20 2005/05/20 20:00:53 joris Exp $ */
1.1 jfb 2: /*
3: * Copyright (c) 2004 Jean-Francois Brousseau <jfb@openbsd.org>
1.8 tedu 4: * All rights reserved.
1.1 jfb 5: *
1.8 tedu 6: * Redistribution and use in source and binary forms, with or without
7: * modification, are permitted provided that the following conditions
8: * are met:
1.1 jfb 9: *
1.8 tedu 10: * 1. Redistributions of source code must retain the above copyright
11: * notice, this list of conditions and the following disclaimer.
1.1 jfb 12: * 2. The name of the author may not be used to endorse or promote products
1.8 tedu 13: * derived from this software without specific prior written permission.
1.1 jfb 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
1.8 tedu 24: * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1.1 jfb 25: */
26:
27: #include <sys/types.h>
28:
29: #include <errno.h>
30: #include <stdio.h>
31: #include <stdlib.h>
32: #include <string.h>
1.10 xsa 33: #include <unistd.h>
1.1 jfb 34:
35: #include "cvs.h"
36: #include "log.h"
1.2 jfb 37: #include "proto.h"
1.1 jfb 38:
39:
1.3 jfb 40: extern char *__progname;
41:
42:
1.21 ! jfb 43: static int cvs_add_file(CVSFILE *, void *);
! 44: static int cvs_add_init(struct cvs_cmd *, int, char **, int *);
! 45: static int cvs_add_pre_exec(struct cvsroot *);
! 46:
! 47: struct cvs_cmd cvs_cmd_add = {
! 48: CVS_OP_ADD, CVS_REQ_ADD, "add",
! 49: { "ad", "new" },
! 50: "Add a new file/directory to the repository",
! 51: "[-k mode] [-m msg] file ...",
! 52: "k:m:",
! 53: NULL,
! 54: 0,
! 55: cvs_add_init,
! 56: cvs_add_pre_exec,
! 57: cvs_add_file,
1.15 joris 58: cvs_add_file,
1.21 ! jfb 59: NULL,
! 60: NULL,
1.15 joris 61: CVS_CMD_ALLOWSPEC | CVS_CMD_SENDDIR | CVS_CMD_SENDARGS2
62: };
1.3 jfb 63:
1.15 joris 64: static int kflag = RCS_KWEXP_DEFAULT;
65: static char *koptstr;
1.3 jfb 66:
1.21 ! jfb 67: static int
! 68: cvs_add_init(struct cvs_cmd *cmd, int argc, char **argv, int *arg)
1.1 jfb 69: {
1.15 joris 70: int ch;
1.1 jfb 71:
1.13 jfb 72: cvs_msg = NULL;
1.1 jfb 73:
1.21 ! jfb 74: while ((ch = getopt(argc, argv, cmd->cmd_opts)) != -1) {
1.1 jfb 75: switch (ch) {
76: case 'k':
1.13 jfb 77: koptstr = optarg;
78: kflag = rcs_kflag_get(koptstr);
79: if (RCS_KWEXP_INVAL(kflag)) {
80: cvs_log(LP_ERR,
81: "invalid RCS keyword expansion mode");
82: rcs_kflag_usage();
1.19 joris 83: return (CVS_EX_USAGE);
1.13 jfb 84: }
1.1 jfb 85: break;
86: case 'm':
1.13 jfb 87: if ((cvs_msg = strdup(optarg)) == NULL) {
88: cvs_log(LP_ERRNO, "failed to copy message");
1.19 joris 89: return (CVS_EX_DATA);
1.13 jfb 90: }
1.1 jfb 91: break;
92: default:
1.19 joris 93: return (CVS_EX_USAGE);
1.1 jfb 94: }
95: }
96:
1.15 joris 97: *arg = optind;
98: return (0);
99: }
1.10 xsa 100:
1.21 ! jfb 101: static int
! 102: cvs_add_pre_exec(struct cvsroot *root)
1.15 joris 103: {
104: char buf[16];
1.3 jfb 105:
1.21 ! jfb 106: if ((root->cr_method != CVS_METHOD_LOCAL) &&
! 107: (kflag != RCS_KWEXP_DEFAULT)) {
1.15 joris 108: strlcpy(buf, "-k", sizeof(buf));
109: strlcat(buf, koptstr, sizeof(buf));
110: if (cvs_sendarg(root, buf, 0) < 0)
1.19 joris 111: return (CVS_EX_PROTO);
1.10 xsa 112: }
1.3 jfb 113:
114: return (0);
115: }
116:
1.21 ! jfb 117: static int
1.3 jfb 118: cvs_add_file(CVSFILE *cf, void *arg)
119: {
1.10 xsa 120: int ret;
1.3 jfb 121: struct cvsroot *root;
122:
1.10 xsa 123: ret = 0;
124: root = CVS_DIR_ROOT(cf);
125:
1.3 jfb 126: if (cf->cf_type == DT_DIR) {
1.12 jfb 127: if (root->cr_method != CVS_METHOD_LOCAL)
128: ret = cvs_senddir(root, cf);
1.20 joris 129:
130: if (ret == -1)
131: ret = CVS_EX_PROTO;
1.3 jfb 132:
1.10 xsa 133: return (ret);
1.1 jfb 134: }
135:
1.5 krapht 136: if (root->cr_method != CVS_METHOD_LOCAL) {
1.12 jfb 137: if (cf->cf_cvstat == CVS_FST_UNKNOWN)
138: ret = cvs_sendreq(root, CVS_REQ_ISMODIFIED,
1.21 ! jfb 139: cf->cf_name);
1.7 deraadt 140: } else {
1.3 jfb 141: cvs_log(LP_INFO, "scheduling file `%s' for addition",
1.21 ! jfb 142: cf->cf_name);
1.3 jfb 143: cvs_log(LP_INFO, "use `%s commit' to add this file permanently",
144: __progname);
1.1 jfb 145: }
146:
1.10 xsa 147: return (ret);
1.1 jfb 148: }