summaryrefslogtreecommitdiff
path: root/usr.bin/cvs/history.c
blob: 590bfc18f722f086f672acd6d7556139a8a893b7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
/*	$OpenBSD: history.c,v 1.18 2005/05/31 08:58:48 xsa Exp $	*/
/*
 * Copyright (c) 2004 Jean-Francois Brousseau <jfb@openbsd.org>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. The name of the author may not be used to endorse or promote products
 *    derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
 * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL  DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include <sys/param.h>
#include <sys/stat.h>

#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include "cvs.h"
#include "log.h"
#include "proto.h"

#define CVS_HISTORY_MAXMOD    16

/* history flags */
#define CVS_HF_A     0x01
#define CVS_HF_C     0x02
#define CVS_HF_E     0x04
#define CVS_HF_L     0x08
#define CVS_HF_M     0x10
#define CVS_HF_O     0x20
#define CVS_HF_T     0x40
#define CVS_HF_W     0x80

#define CVS_HF_EXCL (CVS_HF_C|CVS_HF_E|CVS_HF_M|CVS_HF_O|CVS_HF_T|CVS_HF_X)

static int  cvs_history_init      (struct cvs_cmd *, int, char **, int *);
static void cvs_history_print     (struct cvs_hent *);
static int  cvs_history_pre_exec (struct cvsroot *);

extern char *__progname;

struct cvs_cmd cvs_cmd_history = {
	CVS_OP_HISTORY, CVS_REQ_HISTORY, "history",
	{ "hi", "his" },
	"Show repository access history",
	"[-aceloTw] [-b str] [-D date] [-f file] [-m module] [-n module] "
	"[-p path] [-r rev] [-t tag] [-u user] [-x ACEFGMORTUW] [-z tz]",
	"ab:cD:ef:lm:n:op:r:Tt:u:wx:z:",
	NULL,
	0,
	cvs_history_init,
	cvs_history_pre_exec,
	NULL,
	NULL,
	NULL,
	NULL,
	CVS_CMD_SENDDIR
};

static int flags = 0;
static char *rev = NULL;
static char *user = NULL;
static char *zone = "+0000";
static char *tag = NULL;
static u_int nbmod = 0;
static u_int rep = 0;
static char *modules[CVS_HISTORY_MAXMOD];

static int
cvs_history_init(struct cvs_cmd *cmd, int argc, char **argv, int *arg)
{
	int ch;

	while ((ch = getopt(argc, argv, cmd->cmd_opts)) != -1) {
		switch (ch) {
		case 'a':
			flags |= CVS_HF_A;
			break;
		case 'b':
			break;
		case 'c':
			rep++;
			flags |= CVS_HF_C;
			break;
		case 'D':
			break;
		case 'e':
			rep++;
			flags |= CVS_HF_E;
			break;
		case 'f':
			break;
		case 'l':
			flags |= CVS_HF_L;
			break;
		case 'm':
			rep++;
			flags |= CVS_HF_M;
			if (nbmod == CVS_HISTORY_MAXMOD) {
				cvs_log(LP_ERR, "too many `-m' options");
				return (CVS_EX_USAGE);
			}
			modules[nbmod++] = optarg;
			break;
		case 'n':
			break;
		case 'o':
			rep++;
			flags |= CVS_HF_O;
			break;
		case 'r':
			rev = optarg;
			break;
		case 'T':
			rep++;
			flags |= CVS_HF_T;
			break;
		case 't':
			tag = optarg;
			break;
		case 'u':
			user = optarg;
			break;
		case 'w':
			flags |= CVS_HF_W;
			break;
		case 'x':
			rep++;
			break;
		case 'z':
			zone = optarg;
			break;
		default:
			return (CVS_EX_USAGE);
		}
	}

	if (rep > 1) {
		cvs_log(LP_ERR,
		    "Only one report type allowed from: \"-Tcomxe\"");
		return (CVS_EX_USAGE);
	} else if (rep == 0)
		flags |= CVS_HF_O;    /* use -o as default */

	*arg = optind;
	return (0);
}

static int
cvs_history_pre_exec(struct cvsroot *root)
{
	if ((flags & CVS_HF_A) && (cvs_sendarg(root, "-a", 0) < 0))
		return (CVS_EX_PROTO);

	if ((flags & CVS_HF_C) && (cvs_sendarg(root, "-c", 0) < 0))
		return (CVS_EX_PROTO);

	if ((flags & CVS_HF_O) && (cvs_sendarg(root, "-o", 0) < 0))
		return (CVS_EX_PROTO);

	if (rev != NULL) {
		if ((cvs_sendarg(root, "-r", 0) < 0) ||
		    (cvs_sendarg(root, rev, 0) < 0))
			return (CVS_EX_PROTO);
	}

	if (tag != NULL) {
		if ((cvs_sendarg(root, "-t", 0) < 0) ||
		    (cvs_sendarg(root, tag, 0) < 0))
			return (CVS_EX_PROTO);
	}

	/* if no user is specified, get login name of command issuer */
	if (!(flags & CVS_HF_A) && (user == NULL)) {
		if ((user = getlogin()) == NULL) {
			cvs_log(LP_ERRNO, "cannot get login name");
			return (CVS_EX_DATA);
		}
	}
	if (!(flags & CVS_HF_A))
		if ((cvs_sendarg(root, "-u", 0) < 0) ||
		    (cvs_sendarg(root, user, 0) < 0))
			return (CVS_EX_PROTO);

	if ((cvs_sendarg(root, "-z", 0) < 0) ||
	    (cvs_sendarg(root, zone, 0) < 0))
		return (CVS_EX_PROTO);

	return (0);
}


static void
cvs_history_print(struct cvs_hent *hent)
{
	struct tm etime;

	if (localtime_r(&(hent->ch_date), &etime) == NULL) {
		cvs_log(LP_ERR, "failed to convert timestamp to structure");
		return;
	}

	printf("%c %4d-%02d-%02d %02d:%02d +%04d %-16s %-16s\n",
	    hent->ch_event, etime.tm_year + 1900, etime.tm_mon + 1,
	    etime.tm_mday, etime.tm_hour, etime.tm_min,
	    0, hent->ch_user, hent->ch_repo);
}