Annotation of src/usr.bin/rcs/rlog.c, Revision 1.57
1.57 ! xsa 1: /* $OpenBSD: rlog.c,v 1.56 2006/10/12 17:20:12 niallo Exp $ */
1.1 joris 2: /*
3: * Copyright (c) 2005 Joris Vink <joris@openbsd.org>
1.20 xsa 4: * Copyright (c) 2005, 2006 Xavier Santolaria <xsa@openbsd.org>
1.1 joris 5: * All rights reserved.
6: *
7: * Redistribution and use in source and binary forms, with or without
8: * modification, are permitted provided that the following conditions
9: * are met:
10: *
11: * 1. Redistributions of source code must retain the above copyright
12: * notice, this list of conditions and the following disclaimer.
13: * 2. The name of the author may not be used to endorse or promote products
14: * derived from this software without specific prior written permission.
15: *
16: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
17: * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
18: * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
19: * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22: * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23: * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24: * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25: * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26: */
27:
1.57 ! xsa 28: #include <err.h>
! 29: #include <libgen.h>
! 30: #include <stdio.h>
! 31: #include <stdlib.h>
! 32: #include <string.h>
! 33: #include <unistd.h>
1.1 joris 34:
1.19 xsa 35: #include "rcsprog.h"
1.3 niallo 36: #include "diff.h"
1.1 joris 37:
1.44 ray 38: static void rlog_file(const char *, RCSFILE *);
1.32 xsa 39: static void rlog_rev_print(struct rcs_delta *);
1.1 joris 40:
1.38 xsa 41: #define RLOG_OPTSTRING "hLl::NqRr::s:TtVw::x::z::"
1.1 joris 42: #define REVSEP "----------------------------"
43: #define REVEND \
1.6 xsa 44: "============================================================================="
1.1 joris 45:
1.36 ray 46: static int hflag, Lflag, lflag, rflag, tflag, Nflag, wflag;
1.23 xsa 47: static char *llist = NULL;
1.20 xsa 48: static char *slist = NULL;
49: static char *wlist = NULL;
1.36 ray 50: static char *revisions = NULL;
1.1 joris 51:
1.21 xsa 52: void
53: rlog_usage(void)
54: {
55: fprintf(stderr,
1.25 jmc 56: "usage: rlog [-bhLNqRtV] [-ddates] [-l[lockers]] [-r[revs]]\n"
1.28 xsa 57: " [-sstates] [-w[logins]] [-xsuffixes]\n"
58: " [-ztz] file ...\n");
1.21 xsa 59: }
60:
1.1 joris 61: int
62: rlog_main(int argc, char **argv)
63: {
1.44 ray 64: RCSFILE *file;
1.1 joris 65: int Rflag;
1.52 joris 66: int i, ch, fd;
1.1 joris 67: char fpath[MAXPATHLEN];
68:
1.45 joris 69: rcsnum_flags |= RCSNUM_NO_MAGIC;
1.36 ray 70: hflag = Rflag = rflag = 0;
71: while ((ch = rcs_getopt(argc, argv, RLOG_OPTSTRING)) != -1) {
1.1 joris 72: switch (ch) {
73: case 'h':
74: hflag = 1;
75: break;
1.11 xsa 76: case 'L':
77: Lflag = 1;
78: break;
1.23 xsa 79: case 'l':
80: lflag = 1;
81: llist = rcs_optarg;
82: break;
1.1 joris 83: case 'N':
84: Nflag = 1;
85: break;
86: case 'q':
1.47 xsa 87: /*
88: * kept for compatibility
89: */
1.1 joris 90: break;
1.37 xsa 91: case 'R':
92: Rflag = 1;
93: break;
1.36 ray 94: case 'r':
95: rflag = 1;
96: revisions = rcs_optarg;
97: break;
1.20 xsa 98: case 's':
99: slist = rcs_optarg;
100: break;
1.8 xsa 101: case 'T':
102: /*
103: * kept for compatibility
104: */
105: break;
1.1 joris 106: case 't':
107: tflag = 1;
108: break;
109: case 'V':
110: printf("%s\n", rcs_version);
111: exit(0);
1.20 xsa 112: case 'w':
113: wflag = 1;
114: wlist = rcs_optarg;
115: break;
1.14 xsa 116: case 'x':
1.33 ray 117: /* Use blank extension if none given. */
118: rcs_suffixes = rcs_optarg ? rcs_optarg : "";
1.14 xsa 119: break;
1.26 joris 120: case 'z':
121: timezone_flag = rcs_optarg;
1.27 xsa 122: break;
1.1 joris 123: default:
1.21 xsa 124: (usage());
125: exit(1);
1.1 joris 126: }
127: }
128:
1.5 joris 129: argc -= rcs_optind;
130: argv += rcs_optind;
1.1 joris 131:
132: if (argc == 0) {
1.48 xsa 133: warnx("no input file");
1.1 joris 134: (usage)();
135: exit(1);
136: }
137:
1.41 deraadt 138: if (hflag == 1 && tflag == 1) {
1.48 xsa 139: warnx("warning: -t overrides -h.");
1.7 xsa 140: hflag = 0;
141: }
1.6 xsa 142:
1.1 joris 143: for (i = 0; i < argc; i++) {
1.55 ray 144: fd = rcs_choosefile(argv[i], fpath, sizeof(fpath));
145: if (fd < 0) {
1.56 niallo 146: warn("%s", fpath);
1.1 joris 147: continue;
1.55 ray 148: }
1.1 joris 149:
1.52 joris 150: if ((file = rcs_open(fpath, fd,
151: RCS_READ|RCS_PARSE_FULLY)) == NULL)
1.11 xsa 152: continue;
153:
1.41 deraadt 154: if (Lflag == 1 && TAILQ_EMPTY(&(file->rf_locks))) {
1.11 xsa 155: rcs_close(file);
156: continue;
157: }
158:
1.9 xsa 159: if (Rflag == 1) {
160: printf("%s\n", fpath);
1.11 xsa 161: rcs_close(file);
1.9 xsa 162: continue;
163: }
164:
1.44 ray 165: rlog_file(argv[i], file);
1.9 xsa 166:
1.1 joris 167: rcs_close(file);
168: }
169:
170: return (0);
171: }
172:
1.32 xsa 173: static void
1.44 ray 174: rlog_file(const char *fname, RCSFILE *file)
1.1 joris 175: {
176: char numb[64];
1.36 ray 177: u_int nrev;
1.1 joris 178: struct rcs_sym *sym;
179: struct rcs_access *acp;
1.20 xsa 180: struct rcs_delta *rdp;
1.10 xsa 181: struct rcs_lock *lkp;
1.45 joris 182: char *workfile, *p;
1.1 joris 183:
1.36 ray 184: if (rflag == 1)
1.46 xsa 185: nrev = rcs_rev_select(file, revisions);
1.36 ray 186: else
187: nrev = file->rf_ndelta;
188:
1.45 joris 189: if ((workfile = basename(fname)) == NULL)
1.50 xsa 190: err(1, "basename");
1.45 joris 191:
192: /*
193: * In case they specified 'foo,v' as argument.
194: */
195: if ((p = strrchr(workfile, ',')) != NULL)
196: *p = '\0';
197:
1.43 ray 198: printf("\nRCS file: %s", file->rf_path);
1.45 joris 199: printf("\nWorking file: %s", workfile);
1.1 joris 200: printf("\nhead:");
201: if (file->rf_head != NULL)
202: printf(" %s", rcsnum_tostr(file->rf_head, numb, sizeof(numb)));
203:
204: printf("\nbranch:");
205: if (rcs_branch_get(file) != NULL) {
206: printf(" %s", rcsnum_tostr(rcs_branch_get(file),
207: numb, sizeof(numb)));
208: }
209:
210: printf("\nlocks: %s", (file->rf_flags & RCS_SLOCK) ? "strict" : "");
1.10 xsa 211: TAILQ_FOREACH(lkp, &(file->rf_locks), rl_list)
212: printf("\n\t%s: %s", lkp->rl_name,
213: rcsnum_tostr(lkp->rl_num, numb, sizeof(numb)));
1.1 joris 214: printf("\naccess list:\n");
215: TAILQ_FOREACH(acp, &(file->rf_access), ra_list)
216: printf("\t%s\n", acp->ra_name);
217:
218: if (Nflag == 0) {
219: printf("symbolic names:\n");
220: TAILQ_FOREACH(sym, &(file->rf_symbols), rs_list) {
221: printf("\t%s: %s\n", sym->rs_name,
222: rcsnum_tostr(sym->rs_num, numb, sizeof(numb)));
223: }
224: }
225:
226: printf("keyword substitution: %s\n",
227: file->rf_expand == NULL ? "kv" : file->rf_expand);
228:
1.20 xsa 229: printf("total revisions: %u", file->rf_ndelta);
230:
1.41 deraadt 231: if (file->rf_head != NULL && hflag == 0 && tflag == 0)
1.36 ray 232: printf(";\tselected revisions: %u", nrev);
1.20 xsa 233:
234: printf("\n");
235:
1.1 joris 236:
1.41 deraadt 237: if (hflag == 0 || tflag == 1)
1.13 xsa 238: printf("description:\n%s", file->rf_desc);
1.1 joris 239:
1.42 ray 240: if (hflag == 0 && tflag == 0 &&
241: !(lflag == 1 && TAILQ_EMPTY(&file->rf_locks))) {
1.36 ray 242: TAILQ_FOREACH(rdp, &(file->rf_delta), rd_list) {
243: /*
244: * if selections are enabled verify that entry is
1.39 niallo 245: * selected.
1.36 ray 246: */
1.41 deraadt 247: if (rflag == 0 || (rdp->rd_flags & RCS_RD_SELECT))
1.36 ray 248: rlog_rev_print(rdp);
249: }
1.20 xsa 250: }
1.1 joris 251:
252: printf("%s\n", REVEND);
1.16 xsa 253: }
254:
255: static void
1.20 xsa 256: rlog_rev_print(struct rcs_delta *rdp)
1.16 xsa 257: {
1.20 xsa 258: int i, found;
1.40 joris 259: struct tm t;
260: char *author, numb[64], *fmt, timeb[64];
1.51 joris 261: struct rcs_argvector *largv, *sargv, *wargv;
1.20 xsa 262:
263: i = found = 0;
264: author = NULL;
265:
1.23 xsa 266: /* -l[lockers] */
267: if (lflag == 1) {
1.42 ray 268: if (rdp->rd_locker != NULL)
269: found++;
1.23 xsa 270:
271: if (llist != NULL) {
272: /* if locker is empty, no need to go further. */
273: if (rdp->rd_locker == NULL)
274: return;
1.51 joris 275: largv = rcs_strsplit(llist, ",");
1.34 pat 276: for (i = 0; largv->argv[i] != NULL; i++) {
277: if (strcmp(rdp->rd_locker, largv->argv[i])
278: == 0) {
1.23 xsa 279: found++;
280: break;
281: }
282: found = 0;
283: }
1.51 joris 284: rcs_argv_destroy(largv);
1.23 xsa 285: }
286: }
1.40 joris 287:
1.20 xsa 288: /* -sstates */
289: if (slist != NULL) {
1.51 joris 290: sargv = rcs_strsplit(slist, ",");
1.34 pat 291: for (i = 0; sargv->argv[i] != NULL; i++) {
292: if (strcmp(rdp->rd_state, sargv->argv[i]) == 0) {
1.20 xsa 293: found++;
294: break;
295: }
296: found = 0;
297: }
1.51 joris 298: rcs_argv_destroy(sargv);
1.20 xsa 299: }
1.40 joris 300:
1.20 xsa 301: /* -w[logins] */
302: if (wflag == 1) {
303: if (wlist != NULL) {
1.51 joris 304: wargv = rcs_strsplit(wlist, ",");
1.34 pat 305: for (i = 0; wargv->argv[i] != NULL; i++) {
306: if (strcmp(rdp->rd_author, wargv->argv[i])
307: == 0) {
1.20 xsa 308: found++;
309: break;
310: }
311: found = 0;
312: }
1.51 joris 313: rcs_argv_destroy(wargv);
1.20 xsa 314: } else {
315: if ((author = getlogin()) == NULL)
1.50 xsa 316: err(1, "getlogin");
1.16 xsa 317:
1.20 xsa 318: if (strcmp(rdp->rd_author, author) == 0)
319: found++;
320: }
321: }
1.16 xsa 322:
1.20 xsa 323: /* XXX dirty... */
1.41 deraadt 324: if ((((slist != NULL && wflag == 1) ||
325: (slist != NULL && lflag == 1) ||
326: (lflag == 1 && wflag == 1)) && found < 2) ||
327: (((slist != NULL && lflag == 1 && wflag == 1) ||
328: (slist != NULL || lflag == 1 || wflag == 1)) && found == 0))
1.20 xsa 329: return;
330:
331: printf("%s\n", REVSEP);
332:
333: rcsnum_tostr(rdp->rd_num, numb, sizeof(numb));
334:
1.22 xsa 335: printf("revision %s", numb);
336: if (rdp->rd_locker != NULL)
337: printf("\tlocked by: %s;", rdp->rd_locker);
1.40 joris 338:
339: if (timezone_flag != NULL) {
340: rcs_set_tz(timezone_flag, rdp, &t);
1.53 xsa 341: fmt = "%Y-%m-%d %H:%M:%S%z";
1.40 joris 342: } else {
343: t = rdp->rd_date;
344: fmt = "%Y/%m/%d %H:%M:%S";
345: }
346:
347: strftime(timeb, sizeof(timeb), fmt, &t);
348:
349: printf("\ndate: %s; author: %s; state: %s;\n", timeb, rdp->rd_author,
1.41 deraadt 350: rdp->rd_state);
1.40 joris 351:
1.20 xsa 352: printf("%s", rdp->rd_log);
1.1 joris 353: }