Annotation of src/usr.bin/cvs/history.c, Revision 1.17
1.17 ! xsa 1: /* $OpenBSD: history.c,v 1.16 2005/05/29 00:18:01 xsa Exp $ */
1.1 jfb 2: /*
3: * Copyright (c) 2004 Jean-Francois Brousseau <jfb@openbsd.org>
1.5 tedu 4: * All rights reserved.
1.1 jfb 5: *
1.5 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.5 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.5 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.5 tedu 24: * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1.1 jfb 25: */
26:
27: #include <sys/param.h>
28: #include <sys/stat.h>
29:
30: #include <errno.h>
31: #include <stdio.h>
32: #include <fcntl.h>
33: #include <stdlib.h>
34: #include <unistd.h>
35: #include <string.h>
36:
37: #include "cvs.h"
38: #include "rcs.h"
39: #include "log.h"
1.3 jfb 40: #include "proto.h"
1.1 jfb 41:
42: #define CVS_HISTORY_MAXMOD 16
43:
44: /* history flags */
45: #define CVS_HF_A 0x01
46: #define CVS_HF_C 0x02
47: #define CVS_HF_E 0x04
48: #define CVS_HF_L 0x08
49: #define CVS_HF_M 0x10
50: #define CVS_HF_O 0x20
51: #define CVS_HF_T 0x40
52: #define CVS_HF_W 0x80
53:
54: #define CVS_HF_EXCL (CVS_HF_C|CVS_HF_E|CVS_HF_M|CVS_HF_O|CVS_HF_T|CVS_HF_X)
55:
1.14 jfb 56: static int cvs_history_init (struct cvs_cmd *, int, char **, int *);
57: static void cvs_history_print (struct cvs_hent *);
58: static int cvs_history_pre_exec (struct cvsroot *);
1.1 jfb 59:
60: extern char *__progname;
61:
1.14 jfb 62: struct cvs_cmd cvs_cmd_history = {
63: CVS_OP_HISTORY, CVS_REQ_HISTORY, "history",
64: { "hi", "his" },
65: "Show repository access history",
1.15 xsa 66: "[-aceloTw] [-b str] [-D date] [-f file] [-m module] [-n module] "
67: "[-p path] [-r rev] [-t tag] [-u user] [-x ACEFGMORTUW] [-z tz]",
68: "ab:cD:ef:lm:n:op:r:Tt:u:wx:z:",
1.14 jfb 69: NULL,
70: 0,
71: cvs_history_init,
72: cvs_history_pre_exec,
73: NULL,
1.9 joris 74: NULL,
75: NULL,
76: NULL,
77: CVS_CMD_SENDDIR
78: };
79:
80: static int flags = 0;
1.16 xsa 81: static char *rev = NULL;
1.9 joris 82: static char *user = NULL;
83: static char *zone = "+0000";
84: static char *tag = NULL;
85: static u_int nbmod = 0;
86: static u_int rep = 0;
87: static char *modules[CVS_HISTORY_MAXMOD];
1.1 jfb 88:
1.14 jfb 89: static int
90: cvs_history_init(struct cvs_cmd *cmd, int argc, char **argv, int *arg)
1.1 jfb 91: {
1.9 joris 92: int ch;
1.1 jfb 93:
1.14 jfb 94: while ((ch = getopt(argc, argv, cmd->cmd_opts)) != -1) {
1.1 jfb 95: switch (ch) {
96: case 'a':
97: flags |= CVS_HF_A;
98: break;
1.15 xsa 99: case 'b':
100: break;
1.1 jfb 101: case 'c':
102: rep++;
103: flags |= CVS_HF_C;
104: break;
1.15 xsa 105: case 'D':
106: break;
1.1 jfb 107: case 'e':
108: rep++;
109: flags |= CVS_HF_E;
110: break;
1.15 xsa 111: case 'f':
112: break;
1.1 jfb 113: case 'l':
114: flags |= CVS_HF_L;
115: break;
116: case 'm':
117: rep++;
118: flags |= CVS_HF_M;
119: if (nbmod == CVS_HISTORY_MAXMOD) {
120: cvs_log(LP_ERR, "too many `-m' options");
1.13 joris 121: return (CVS_EX_USAGE);
1.1 jfb 122: }
123: modules[nbmod++] = optarg;
124: break;
1.15 xsa 125: case 'n':
126: break;
1.1 jfb 127: case 'o':
128: rep++;
129: flags |= CVS_HF_O;
130: break;
1.16 xsa 131: case 'r':
132: rev = optarg;
133: break;
1.1 jfb 134: case 'T':
135: rep++;
136: flags |= CVS_HF_T;
137: break;
138: case 't':
139: tag = optarg;
140: break;
141: case 'u':
142: user = optarg;
143: break;
144: case 'w':
145: flags |= CVS_HF_W;
146: break;
147: case 'x':
148: rep++;
149: break;
150: case 'z':
151: zone = optarg;
152: break;
153: default:
1.13 joris 154: return (CVS_EX_USAGE);
1.1 jfb 155: }
156: }
157:
158: if (rep > 1) {
159: cvs_log(LP_ERR,
160: "Only one report type allowed from: \"-Tcomxe\"");
1.13 joris 161: return (CVS_EX_USAGE);
1.4 deraadt 162: } else if (rep == 0)
1.1 jfb 163: flags |= CVS_HF_O; /* use -o as default */
164:
1.9 joris 165: *arg = optind;
166: return (0);
167: }
1.1 jfb 168:
1.14 jfb 169: static int
170: cvs_history_pre_exec(struct cvsroot *root)
1.9 joris 171: {
1.15 xsa 172: if ((flags & CVS_HF_A) && (cvs_sendarg(root, "-a", 0) < 0))
173: return (CVS_EX_PROTO);
1.1 jfb 174:
1.9 joris 175: if ((flags & CVS_HF_C) && (cvs_sendarg(root, "-c", 0) < 0))
1.13 joris 176: return (CVS_EX_PROTO);
1.1 jfb 177:
1.9 joris 178: if ((flags & CVS_HF_O) && (cvs_sendarg(root, "-o", 0) < 0))
1.13 joris 179: return (CVS_EX_PROTO);
1.16 xsa 180:
181: if (rev != NULL) {
182: if ((cvs_sendarg(root, "-r", 0) < 0) ||
183: (cvs_sendarg(root, rev, 0) < 0))
184: return (CVS_EX_PROTO);
185: }
1.1 jfb 186:
1.9 joris 187: if (tag != NULL) {
188: if ((cvs_sendarg(root, "-t", 0) < 0) ||
189: (cvs_sendarg(root, tag, 0) < 0))
1.13 joris 190: return (CVS_EX_PROTO);
1.9 joris 191: }
1.1 jfb 192:
1.15 xsa 193: /* if no user is specified, get login name of command issuer */
194: if (!(flags & CVS_HF_A) && (user == NULL)) {
195: if ((user = getlogin()) == NULL) {
196: cvs_log(LP_ERRNO, "cannot get login name");
197: return (CVS_EX_DATA);
198: }
199: }
200: if (!(flags & CVS_HF_A))
1.9 joris 201: if ((cvs_sendarg(root, "-u", 0) < 0) ||
202: (cvs_sendarg(root, user, 0) < 0))
1.13 joris 203: return (CVS_EX_PROTO);
1.9 joris 204:
205: if ((cvs_sendarg(root, "-z", 0) < 0) ||
206: (cvs_sendarg(root, zone, 0) < 0))
1.13 joris 207: return (CVS_EX_PROTO);
1.1 jfb 208:
209: return (0);
210: }
211:
212:
213: static void
214: cvs_history_print(struct cvs_hent *hent)
215: {
216: struct tm etime;
217:
218: if (localtime_r(&(hent->ch_date), &etime) == NULL) {
1.17 ! xsa 219: cvs_log(LP_ERR, "failed to convert timestamp to structure");
1.1 jfb 220: return;
221: }
222:
223: printf("%c %4d-%02d-%02d %02d:%02d +%04d %-16s %-16s\n",
224: hent->ch_event, etime.tm_year + 1900, etime.tm_mon + 1,
225: etime.tm_mday, etime.tm_hour, etime.tm_min,
226: 0, hent->ch_user, hent->ch_repo);
227: }