Exemplo n.º 1
0
/*
 * rcs_main()
 *
 * Handler for the `rcs' program.
 * Returns 0 on success, or >0 on error.
 */
int
rcs_main(int argc, char **argv)
{
	int fd;
	int i, j, ch, flags, kflag, lkmode;
	const char *nflag, *oldfilename, *orange;
	char fpath[PATH_MAX];
	char *logstr, *logmsg, *descfile;
	char *alist, *comment, *elist, *lrev, *urev;
	mode_t fmode;
	RCSFILE *file;
	RCSNUM *logrev;
	struct rcs_access *acp;
	time_t rcs_mtime = -1;

	kflag = RCS_KWEXP_ERR;
	lkmode = RCS_LOCK_INVAL;
	fmode =  S_IRUSR|S_IRGRP|S_IROTH;
	flags = RCS_RDWR|RCS_PARSE_FULLY;
	lrev = urev = descfile = NULL;
	logstr = alist = comment = elist = NULL;
	nflag = oldfilename = orange = NULL;

	/* match GNU */
	if (1 < argc && argv[1][0] != '-')
		warnx("warning: No options were given; "
		    "this usage is obsolescent.");

	while ((ch = rcs_getopt(argc, argv, RCSPROG_OPTSTRING)) != -1) {
		switch (ch) {
		case 'A':
			oldfilename = rcs_optarg;
			rcsflags |= CO_ACLAPPEND;
			break;
		case 'a':
			alist = rcs_optarg;
			break;
		case 'c':
			comment = rcs_optarg;
			break;
		case 'e':
			elist = rcs_optarg;
			rcsflags |= RCSPROG_EFLAG;
			break;
		case 'I':
			rcsflags |= INTERACTIVE;
			break;
		case 'i':
			flags |= RCS_CREATE;
			break;
		case 'k':
			kflag = rcs_kflag_get(rcs_optarg);
			if (RCS_KWEXP_INVAL(kflag)) {
				warnx("invalid RCS keyword substitution mode");
				(usage)();
			}
			break;
		case 'L':
			if (lkmode == RCS_LOCK_LOOSE)
				warnx("-U overridden by -L");
			lkmode = RCS_LOCK_STRICT;
			break;
		case 'l':
			if (rcsflags & RCSPROG_UFLAG)
				warnx("-u overridden by -l");
			lrev = rcs_optarg;
			rcsflags &= ~RCSPROG_UFLAG;
			rcsflags |= RCSPROG_LFLAG;
			break;
		case 'm':
			free(logstr);
			logstr = xstrdup(rcs_optarg);
			break;
		case 'M':
			/* ignore for the moment */
			break;
		case 'n':
			nflag = rcs_optarg;
			break;
		case 'N':
			nflag = rcs_optarg;
			rcsflags |= RCSPROG_NFLAG;
			break;
		case 'o':
			orange = rcs_optarg;
			break;
		case 'q':
			rcsflags |= QUIET;
			break;
		case 't':
			descfile = rcs_optarg;
			rcsflags |= DESCRIPTION;
			break;
		case 'T':
			rcsflags |= PRESERVETIME;
			break;
		case 'U':
			if (lkmode == RCS_LOCK_STRICT)
				warnx("-L overridden by -U");
			lkmode = RCS_LOCK_LOOSE;
			break;
		case 'u':
			if (rcsflags & RCSPROG_LFLAG)
				warnx("-l overridden by -u");
			urev = rcs_optarg;
			rcsflags &= ~RCSPROG_LFLAG;
			rcsflags |= RCSPROG_UFLAG;
			break;
		case 'V':
			printf("%s\n", rcs_version);
			exit(0);
		case 'x':
			/* Use blank extension if none given. */
			rcs_suffixes = rcs_optarg ? rcs_optarg : "";
			break;
		case 'z':
			/*
			 * kept for compatibility
			 */
			break;
		default:
			(usage)();
		}
	}

	argc -= rcs_optind;
	argv += rcs_optind;

	if (argc == 0) {
		warnx("no input file");
		(usage)();
	}

	for (i = 0; i < argc; i++) {
		fd = rcs_choosefile(argv[i], fpath, sizeof(fpath));
		if (fd < 0 && !(flags & RCS_CREATE)) {
			warn("%s", fpath);
			continue;
		}

		if (!(rcsflags & QUIET))
			(void)fprintf(stderr, "RCS file: %s\n", fpath);

		if ((file = rcs_open(fpath, fd, flags, fmode)) == NULL) {
			close(fd);
			continue;
		}

		if (rcsflags & DESCRIPTION) {
			if (rcs_set_description(file, descfile, rcsflags) == -1) {
				warn("%s", descfile);
				rcs_close(file);
				continue;
			}
		}
		else if (flags & RCS_CREATE) {
			if (rcs_set_description(file, NULL, rcsflags) == -1) {
				warn("stdin");
				rcs_close(file);
				continue;
			}
		}

		if (rcsflags & PRESERVETIME)
			rcs_mtime = rcs_get_mtime(file);

		if (nflag != NULL)
			rcs_attach_symbol(file, nflag);

		if (logstr != NULL) {
			if ((logmsg = strchr(logstr, ':')) == NULL) {
				warnx("missing log message");
				rcs_close(file);
				continue;
			}

			*logmsg++ = '\0';
			if ((logrev = rcsnum_parse(logstr)) == NULL) {
				warnx("`%s' bad revision number", logstr);
				rcs_close(file);
				continue;
			}

			if (rcs_rev_setlog(file, logrev, logmsg) < 0) {
				warnx("failed to set logmsg for `%s' to `%s'",
				    logstr, logmsg);
				rcs_close(file);
				rcsnum_free(logrev);
				continue;
			}

			rcsnum_free(logrev);
		}

		/* entries to add from <oldfile> */
		if (rcsflags & CO_ACLAPPEND) {
			RCSFILE *oldfile;
			int ofd;
			char ofpath[PATH_MAX];

			ofd = rcs_choosefile(oldfilename, ofpath, sizeof(ofpath));
			if (ofd < 0) {
				if (!(flags & RCS_CREATE))
					warn("%s", ofpath);
				exit(1);
			}
			if ((oldfile = rcs_open(ofpath, ofd, RCS_READ)) == NULL)
				exit(1);

			TAILQ_FOREACH(acp, &(oldfile->rf_access), ra_list)
				rcs_access_add(file, acp->ra_name);

			rcs_close(oldfile);
			(void)close(ofd);
		}

		/* entries to add to the access list */
		if (alist != NULL) {
			struct rcs_argvector *aargv;

			aargv = rcs_strsplit(alist, ",");
			for (j = 0; aargv->argv[j] != NULL; j++)
				rcs_access_add(file, aargv->argv[j]);

			rcs_argv_destroy(aargv);
		}

		if (comment != NULL)
			rcs_comment_set(file, comment);

		if (elist != NULL) {
			struct rcs_argvector *eargv;

			eargv = rcs_strsplit(elist, ",");
			for (j = 0; eargv->argv[j] != NULL; j++)
				rcs_access_remove(file, eargv->argv[j]);

			rcs_argv_destroy(eargv);
		} else if (rcsflags & RCSPROG_EFLAG) {
			struct rcs_access *rap;

			/* XXX rcs_access_remove(file, NULL); ?? */
			while (!TAILQ_EMPTY(&(file->rf_access))) {
				rap = TAILQ_FIRST(&(file->rf_access));
				TAILQ_REMOVE(&(file->rf_access), rap, ra_list);
				free(rap->ra_name);
				free(rap);
			}
			/* not synced anymore */
			file->rf_flags &= ~RCS_SYNCED;
		}

		rcs_kwexp_set(file, kflag);

		if (lkmode != RCS_LOCK_INVAL)
			(void)rcs_lock_setmode(file, lkmode);

		if (rcsflags & RCSPROG_LFLAG) {
			RCSNUM *rev;
			const char *username;
			char rev_str[RCS_REV_BUFSZ];

			if (file->rf_head == NULL) {
				warnx("%s contains no revisions", fpath);
				rcs_close(file);
				continue;
			}

			if ((username = getlogin()) == NULL)
				err(1, "getlogin");
			if (lrev == NULL) {
				rev = rcsnum_alloc();
				rcsnum_cpy(file->rf_head, rev, 0);
			} else if ((rev = rcsnum_parse(lrev)) == NULL) {
				warnx("unable to unlock file");
				rcs_close(file);
				continue;
			}
			rcsnum_tostr(rev, rev_str, sizeof(rev_str));
			/* Make sure revision exists. */
			if (rcs_findrev(file, rev) == NULL)
				errx(1, "%s: cannot lock nonexisting "
				    "revision %s", fpath, rev_str);
			if (rcs_lock_add(file, username, rev) != -1 &&
			    !(rcsflags & QUIET))
				(void)fprintf(stderr, "%s locked\n", rev_str);
			rcsnum_free(rev);
		}

		if (rcsflags & RCSPROG_UFLAG) {
			RCSNUM *rev;
			const char *username;
			char rev_str[RCS_REV_BUFSZ];

			if (file->rf_head == NULL) {
				warnx("%s contains no revisions", fpath);
				rcs_close(file);
				continue;
			}

			if ((username = getlogin()) == NULL)
				err(1, "getlogin");
			if (urev == NULL) {
				rev = rcsnum_alloc();
				rcsnum_cpy(file->rf_head, rev, 0);
			} else if ((rev = rcsnum_parse(urev)) == NULL) {
				warnx("unable to unlock file");
				rcs_close(file);
				continue;
			}
			rcsnum_tostr(rev, rev_str, sizeof(rev_str));
			/* Make sure revision exists. */
			if (rcs_findrev(file, rev) == NULL)
				errx(1, "%s: cannot unlock nonexisting "
				    "revision %s", fpath, rev_str);
			if (rcs_lock_remove(file, username, rev) == -1 &&
			    !(rcsflags & QUIET))
				warnx("%s: warning: No locks are set.", fpath);
			else {
				if (!(rcsflags & QUIET))
					(void)fprintf(stderr,
					    "%s unlocked\n", rev_str);
			}
			rcsnum_free(rev);
		}

		if (orange != NULL) {
			struct rcs_delta *rdp, *nrdp;
			char b[RCS_REV_BUFSZ];

			rcs_rev_select(file, orange);
			for (rdp = TAILQ_FIRST(&(file->rf_delta));
			    rdp != NULL; rdp = nrdp) {
				nrdp = TAILQ_NEXT(rdp, rd_list);

				/*
				 * Delete selected revisions.
				 */
				if (rdp->rd_flags & RCS_RD_SELECT) {
					rcsnum_tostr(rdp->rd_num, b, sizeof(b));
					if (!(rcsflags & QUIET)) {
						(void)fprintf(stderr, "deleting"
						    " revision %s\n", b);
					}
					(void)rcs_rev_remove(file, rdp->rd_num);
				}
			}
		}

		rcs_write(file);

		if (rcsflags & PRESERVETIME)
			rcs_set_mtime(file, rcs_mtime);

		rcs_close(file);

		if (!(rcsflags & QUIET))
			(void)fprintf(stderr, "done\n");
	}

	return (0);
}
Exemplo n.º 2
0
static void
rcsclean_file(char *fname, const char *rev_str)
{
	int fd, match;
	RCSFILE *file;
	char fpath[PATH_MAX], numb[RCS_REV_BUFSZ];
	RCSNUM *rev;
	BUF *b1, *b2;
	time_t rcs_mtime = -1;

	b1 = b2 = NULL;
	file = NULL;
	rev = NULL;

	if ((fd = rcs_choosefile(fname, fpath, sizeof(fpath))) < 0)
		goto out;

	if ((file = rcs_open(fpath, fd, RCS_RDWR)) == NULL)
		goto out;

	if (flags & PRESERVETIME)
		rcs_mtime = rcs_get_mtime(file);

	rcs_kwexp_set(file, kflag);

	if (rev_str == NULL)
		rev = file->rf_head;
	else if ((rev = rcs_getrevnum(rev_str, file)) == NULL) {
		warnx("%s: Symbolic name `%s' is undefined.", fpath, rev_str);
		goto out;
	}

	if ((b1 = rcs_getrev(file, rev)) == NULL) {
		warnx("failed to get needed revision");
		goto out;
	}
	if ((b2 = buf_load(fname)) == NULL) {
		warnx("failed to load `%s'", fname);
		goto out;
	}

	/* If buffer lengths are the same, compare contents as well. */
	if (buf_len(b1) != buf_len(b2))
		match = 0;
	else {
		size_t len, n;

		len = buf_len(b1);

		match = 1;
		for (n = 0; n < len; ++n)
			if (buf_getc(b1, n) != buf_getc(b2, n)) {
				match = 0;
				break;
			}
	}

	if (match == 1) {
		if (uflag == 1 && !TAILQ_EMPTY(&(file->rf_locks))) {
			if (!(flags & QUIET) && nflag == 0) {
				printf("rcs -u%s %s\n",
				    rcsnum_tostr(rev, numb, sizeof(numb)),
				    fpath);
			}
			(void)rcs_lock_remove(file, locker, rev);
		}

		if (TAILQ_EMPTY(&(file->rf_locks))) {
			if (!(flags & QUIET))
				printf("rm -f %s\n", fname);

			if (nflag == 0)
				(void)unlink(fname);
		}
	}

	rcs_write(file);
	if (flags & PRESERVETIME)
		rcs_set_mtime(file, rcs_mtime);

out:
	if (b1 != NULL)
		buf_free(b1);
	if (b2 != NULL)
		buf_free(b2);
	if (file != NULL)
		rcs_close(file);
}