xref: /openbsd-src/usr.bin/rcs/rcsutil.c (revision 6541b77c94127ecd11c0a5f7b2cdfe2db3a4a356)
1*6541b77cSguenther /*	$OpenBSD: rcsutil.c,v 1.48 2023/08/11 05:02:21 guenther Exp $	*/
29349b09eSxsa /*
39349b09eSxsa  * Copyright (c) 2005, 2006 Joris Vink <joris@openbsd.org>
49349b09eSxsa  * Copyright (c) 2006 Xavier Santolaria <xsa@openbsd.org>
59349b09eSxsa  * Copyright (c) 2006 Niall O'Higgins <niallo@openbsd.org>
69349b09eSxsa  * Copyright (c) 2006 Ray Lai <ray@openbsd.org>
79349b09eSxsa  * All rights reserved.
89349b09eSxsa  *
99349b09eSxsa  * Redistribution and use in source and binary forms, with or without
109349b09eSxsa  * modification, are permitted provided that the following conditions
119349b09eSxsa  * are met:
129349b09eSxsa  *
139349b09eSxsa  * 1. Redistributions of source code must retain the above copyright
149349b09eSxsa  *    notice, this list of conditions and the following disclaimer.
159349b09eSxsa  * 2. The name of the author may not be used to endorse or promote products
169349b09eSxsa  *    derived from this software without specific prior written permission.
179349b09eSxsa  *
189349b09eSxsa  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
199349b09eSxsa  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
209349b09eSxsa  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
219349b09eSxsa  * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
229349b09eSxsa  * EXEMPLARY, OR CONSEQUENTIAL  DAMAGES (INCLUDING, BUT NOT LIMITED TO,
239349b09eSxsa  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
249349b09eSxsa  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
259349b09eSxsa  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
269349b09eSxsa  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
279349b09eSxsa  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
289349b09eSxsa  */
299349b09eSxsa 
304781e2faSxsa #include <sys/stat.h>
31a393799fSchl #include <sys/time.h>
324781e2faSxsa 
334781e2faSxsa #include <ctype.h>
344781e2faSxsa #include <err.h>
354781e2faSxsa #include <fcntl.h>
364781e2faSxsa #include <stdio.h>
378ac837e5Snicm #include <stdlib.h>
384781e2faSxsa #include <string.h>
394781e2faSxsa #include <unistd.h>
409349b09eSxsa 
419349b09eSxsa #include "rcsprog.h"
429349b09eSxsa 
439349b09eSxsa /*
449349b09eSxsa  * rcs_get_mtime()
459349b09eSxsa  *
469349b09eSxsa  * Get <filename> last modified time.
47*6541b77cSguenther  * Returns last modified time on success, or a timespec with tv_nsec
48*6541b77cSguenther  * set to UTIME_OMIT on failure.
499349b09eSxsa  */
50*6541b77cSguenther struct timespec
rcs_get_mtime(RCSFILE * file)51bc8d37c3Sjoris rcs_get_mtime(RCSFILE *file)
529349b09eSxsa {
539349b09eSxsa 	struct stat st;
54*6541b77cSguenther 	struct timespec mtime = { .tv_sec = 0, .tv_nsec = UTIME_OMIT };
559349b09eSxsa 
56394437e6Stobias 	if (file->rf_file == NULL)
57*6541b77cSguenther 		return mtime;
58394437e6Stobias 
59394437e6Stobias 	if (fstat(fileno(file->rf_file), &st) == -1) {
60bc8d37c3Sjoris 		warn("%s", file->rf_path);
61*6541b77cSguenther 		return mtime;
629349b09eSxsa 	}
63bc8d37c3Sjoris 
64*6541b77cSguenther 	return st.st_mtim;
659349b09eSxsa }
669349b09eSxsa 
679349b09eSxsa /*
689349b09eSxsa  * rcs_set_mtime()
699349b09eSxsa  *
70*6541b77cSguenther  * Set <filename> last modified time to <mtime> if its tv_nsec isn't UTIME_OMIT
719349b09eSxsa  */
729349b09eSxsa void
rcs_set_mtime(RCSFILE * file,struct timespec mtime)73*6541b77cSguenther rcs_set_mtime(RCSFILE *file, struct timespec mtime)
749349b09eSxsa {
75*6541b77cSguenther 	struct timespec ts[2];
769349b09eSxsa 
77*6541b77cSguenther 	if (file->rf_file == NULL || mtime.tv_nsec == UTIME_OMIT)
789349b09eSxsa 		return;
799349b09eSxsa 
80*6541b77cSguenther 	ts[0] = ts[1] = mtime;
819349b09eSxsa 
82*6541b77cSguenther 	if (futimens(fileno(file->rf_file), ts) == -1)
83a3660ae3Sxsa 		err(1, "utimes");
849349b09eSxsa }
859349b09eSxsa 
869349b09eSxsa int
rcs_getopt(int argc,char ** argv,const char * optstr)879349b09eSxsa rcs_getopt(int argc, char **argv, const char *optstr)
889349b09eSxsa {
899349b09eSxsa 	char *a;
909349b09eSxsa 	const char *c;
919349b09eSxsa 	static int i = 1;
929349b09eSxsa 	int opt, hasargument, ret;
939349b09eSxsa 
949349b09eSxsa 	hasargument = 0;
959349b09eSxsa 	rcs_optarg = NULL;
969349b09eSxsa 
979349b09eSxsa 	if (i >= argc)
989349b09eSxsa 		return (-1);
999349b09eSxsa 
1009349b09eSxsa 	a = argv[i++];
1019349b09eSxsa 	if (*a++ != '-')
1029349b09eSxsa 		return (-1);
1039349b09eSxsa 
1049349b09eSxsa 	ret = 0;
1059349b09eSxsa 	opt = *a;
1069349b09eSxsa 	for (c = optstr; *c != '\0'; c++) {
1079349b09eSxsa 		if (*c == opt) {
1089349b09eSxsa 			a++;
1099349b09eSxsa 			ret = opt;
1109349b09eSxsa 
1119349b09eSxsa 			if (*(c + 1) == ':') {
1129349b09eSxsa 				if (*(c + 2) == ':') {
1139349b09eSxsa 					if (*a != '\0')
1149349b09eSxsa 						hasargument = 1;
1159349b09eSxsa 				} else {
1169349b09eSxsa 					if (*a != '\0') {
1179349b09eSxsa 						hasargument = 1;
1189349b09eSxsa 					} else {
1199349b09eSxsa 						ret = 1;
1209349b09eSxsa 						break;
1219349b09eSxsa 					}
1229349b09eSxsa 				}
1239349b09eSxsa 			}
1249349b09eSxsa 
1259349b09eSxsa 			if (hasargument == 1)
1269349b09eSxsa 				rcs_optarg = a;
1279349b09eSxsa 
1289349b09eSxsa 			if (ret == opt)
1299349b09eSxsa 				rcs_optind++;
1309349b09eSxsa 			break;
1319349b09eSxsa 		}
1329349b09eSxsa 	}
1339349b09eSxsa 
1349349b09eSxsa 	if (ret == 0)
1359349b09eSxsa 		warnx("unknown option -%c", opt);
1369349b09eSxsa 	else if (ret == 1)
1379349b09eSxsa 		warnx("missing argument for option -%c", opt);
1389349b09eSxsa 
1399349b09eSxsa 	return (ret);
1409349b09eSxsa }
1419349b09eSxsa 
1429349b09eSxsa /*
1439349b09eSxsa  * rcs_choosefile()
1449349b09eSxsa  *
1459349b09eSxsa  * Given a relative filename, decide where the corresponding RCS file
1469349b09eSxsa  * should be.  Tries each extension until a file is found.  If no file
1479349b09eSxsa  * was found, returns a path with the first extension.
1489349b09eSxsa  *
1492ea70e8bSray  * Opens and returns file descriptor to RCS file.
1509349b09eSxsa  */
151bc8d37c3Sjoris int
rcs_choosefile(const char * filename,char * out,size_t len)152bc8d37c3Sjoris rcs_choosefile(const char *filename, char *out, size_t len)
1539349b09eSxsa {
154bc8d37c3Sjoris 	int fd;
1559349b09eSxsa 	struct stat sb;
156b9fc9a72Sderaadt 	char *p, *ext, name[PATH_MAX], *next, *ptr, rcsdir[PATH_MAX],
157b9fc9a72Sderaadt 	    *suffixes, rcspath[PATH_MAX];
1589349b09eSxsa 
1599349b09eSxsa 	/*
1609349b09eSxsa 	 * If `filename' contains a directory, `rcspath' contains that
1619349b09eSxsa 	 * directory, including a trailing slash.  Otherwise `rcspath'
1629349b09eSxsa 	 * contains an empty string.
1639349b09eSxsa 	 */
1649349b09eSxsa 	if (strlcpy(rcspath, filename, sizeof(rcspath)) >= sizeof(rcspath))
165bc8d37c3Sjoris 		errx(1, "rcs_choosefile: truncation");
166bc8d37c3Sjoris 
1679349b09eSxsa 	/* If `/' is found, end string after `/'. */
1689349b09eSxsa 	if ((ptr = strrchr(rcspath, '/')) != NULL)
1699349b09eSxsa 		*(++ptr) = '\0';
1709349b09eSxsa 	else
1719349b09eSxsa 		rcspath[0] = '\0';
1729349b09eSxsa 
1739349b09eSxsa 	/* Append RCS/ to `rcspath' if it exists. */
1749349b09eSxsa 	if (strlcpy(rcsdir, rcspath, sizeof(rcsdir)) >= sizeof(rcsdir) ||
1759349b09eSxsa 	    strlcat(rcsdir, RCSDIR, sizeof(rcsdir)) >= sizeof(rcsdir))
176bc8d37c3Sjoris 		errx(1, "rcs_choosefile: truncation");
177bc8d37c3Sjoris 
178dc8143a2Sotto 	if (stat(rcsdir, &sb) == 0 && S_ISDIR(sb.st_mode))
179bc8d37c3Sjoris 		if (strlcpy(rcspath, rcsdir, sizeof(rcspath))
180bc8d37c3Sjoris 		    >= sizeof(rcspath) ||
1819349b09eSxsa 		    strlcat(rcspath, "/", sizeof(rcspath)) >= sizeof(rcspath))
182bc8d37c3Sjoris 			errx(1, "rcs_choosefile: truncation");
1839349b09eSxsa 
1849349b09eSxsa 	/* Name of file without path. */
1859349b09eSxsa 	if ((ptr = strrchr(filename, '/')) == NULL) {
1869349b09eSxsa 		if (strlcpy(name, filename, sizeof(name)) >= sizeof(name))
187bc8d37c3Sjoris 			errx(1, "rcs_choosefile: truncation");
1889349b09eSxsa 	} else {
1899349b09eSxsa 		/* Skip `/'. */
1909349b09eSxsa 		if (strlcpy(name, ptr + 1, sizeof(name)) >= sizeof(name))
191bc8d37c3Sjoris 			errx(1, "rcs_choosefile: truncation");
1929349b09eSxsa 	}
1939349b09eSxsa 
1949349b09eSxsa 	/* Name of RCS file without an extension. */
1959349b09eSxsa 	if (strlcat(rcspath, name, sizeof(rcspath)) >= sizeof(rcspath))
196bc8d37c3Sjoris 		errx(1, "rcs_choosefile: truncation");
1979349b09eSxsa 
1989349b09eSxsa 	/*
1999349b09eSxsa 	 * If only the empty suffix was given, use existing rcspath.
2009349b09eSxsa 	 * This ensures that there is at least one suffix for strsep().
2019349b09eSxsa 	 */
2029349b09eSxsa 	if (strcmp(rcs_suffixes, "") == 0) {
2036b99bb86Sray 		if (strlcpy(out, rcspath, len) >= len)
2046b99bb86Sray 			errx(1, "rcs_choosefile: truncation");
20599e66255Sniallo 		fd = open(rcspath, O_RDONLY);
206bc8d37c3Sjoris 		return (fd);
2079349b09eSxsa 	}
2089349b09eSxsa 
2099349b09eSxsa 	/*
2109349b09eSxsa 	 * Cycle through slash-separated `rcs_suffixes', appending each
2119349b09eSxsa 	 * extension to `rcspath' and testing if the file exists.  If it
2129349b09eSxsa 	 * does, return that string.  Otherwise return path with first
2139349b09eSxsa 	 * extension.
2149349b09eSxsa 	 */
2159349b09eSxsa 	suffixes = xstrdup(rcs_suffixes);
216d30d92fcSray 	for (next = suffixes; (ext = strsep(&next, "/")) != NULL;) {
217b9fc9a72Sderaadt 		char fpath[PATH_MAX];
2189349b09eSxsa 
2199349b09eSxsa 		if ((p = strrchr(rcspath, ',')) != NULL) {
2209349b09eSxsa 			if (!strcmp(p, ext)) {
221bc8d37c3Sjoris 				if ((fd = open(rcspath, O_RDONLY)) == -1)
222bc8d37c3Sjoris 					continue;
223bc8d37c3Sjoris 
224bc8d37c3Sjoris 				if (fstat(fd, &sb) == -1)
225bc8d37c3Sjoris 					err(1, "%s", rcspath);
226bc8d37c3Sjoris 
227bc8d37c3Sjoris 				if (strlcpy(out, rcspath, len) >= len)
2280d9cd38cSnaddy 					errx(1, "rcs_choosefile: truncation");
229bc8d37c3Sjoris 
2308ac837e5Snicm 				free(suffixes);
231bc8d37c3Sjoris 				return (fd);
2329349b09eSxsa 			}
2339349b09eSxsa 
2349349b09eSxsa 			continue;
2359349b09eSxsa 		}
2369349b09eSxsa 
2379349b09eSxsa 		/* Construct RCS file path. */
2389349b09eSxsa 		if (strlcpy(fpath, rcspath, sizeof(fpath)) >= sizeof(fpath) ||
2399349b09eSxsa 		    strlcat(fpath, ext, sizeof(fpath)) >= sizeof(fpath))
240bc8d37c3Sjoris 			errx(1, "rcs_choosefile: truncation");
2419349b09eSxsa 
2429349b09eSxsa 		/* Don't use `filename' as RCS file. */
2439349b09eSxsa 		if (strcmp(fpath, filename) == 0)
2449349b09eSxsa 			continue;
2459349b09eSxsa 
246bc8d37c3Sjoris 		if ((fd = open(fpath, O_RDONLY)) == -1)
247bc8d37c3Sjoris 			continue;
248bc8d37c3Sjoris 
249bc8d37c3Sjoris 		if (fstat(fd, &sb) == -1)
250bc8d37c3Sjoris 			err(1, "%s", fpath);
251bc8d37c3Sjoris 
252bc8d37c3Sjoris 		if (strlcpy(out, fpath, len) >= len)
253bc8d37c3Sjoris 			errx(1, "rcs_choosefile: truncation");
254bc8d37c3Sjoris 
2558ac837e5Snicm 		free(suffixes);
256bc8d37c3Sjoris 		return (fd);
2579349b09eSxsa 	}
2589349b09eSxsa 
2599349b09eSxsa 	/*
2609349b09eSxsa 	 * `suffixes' should now be NUL separated, so the first
2619349b09eSxsa 	 * extension can be read just by reading `suffixes'.
2629349b09eSxsa 	 */
2632ea70e8bSray 	if (strlcat(rcspath, suffixes, sizeof(rcspath)) >= sizeof(rcspath))
264bc8d37c3Sjoris 		errx(1, "rcs_choosefile: truncation");
2659349b09eSxsa 
2668ac837e5Snicm 	free(suffixes);
267bc8d37c3Sjoris 
2686b99bb86Sray 	if (strlcpy(out, rcspath, len) >= len)
2696b99bb86Sray 		errx(1, "rcs_choosefile: truncation");
270bc8d37c3Sjoris 
27199e66255Sniallo 	fd = open(rcspath, O_RDONLY);
27299e66255Sniallo 
273bc8d37c3Sjoris 	return (fd);
2749349b09eSxsa }
2759349b09eSxsa 
2769349b09eSxsa /*
2779349b09eSxsa  * Set <str> to <new_str>.  Print warning if <str> is redefined.
2789349b09eSxsa  */
2799349b09eSxsa void
rcs_setrevstr(char ** str,char * new_str)2809349b09eSxsa rcs_setrevstr(char **str, char *new_str)
2819349b09eSxsa {
2829349b09eSxsa 	if (new_str == NULL)
2839349b09eSxsa 		return;
2849349b09eSxsa 	if (*str != NULL)
2859349b09eSxsa 		warnx("redefinition of revision number");
2869349b09eSxsa 	*str = new_str;
2879349b09eSxsa }
2889349b09eSxsa 
2899349b09eSxsa /*
2909349b09eSxsa  * Set <str1> or <str2> to <new_str>, depending on which is not set.
2919349b09eSxsa  * If both are set, error out.
2929349b09eSxsa  */
2939349b09eSxsa void
rcs_setrevstr2(char ** str1,char ** str2,char * new_str)2949349b09eSxsa rcs_setrevstr2(char **str1, char **str2, char *new_str)
2959349b09eSxsa {
2969349b09eSxsa 	if (new_str == NULL)
2979349b09eSxsa 		return;
2989349b09eSxsa 	if (*str1 == NULL)
2999349b09eSxsa 		*str1 = new_str;
3009349b09eSxsa 	else if (*str2 == NULL)
3019349b09eSxsa 		*str2 = new_str;
3029349b09eSxsa 	else
303a3660ae3Sxsa 		errx(1, "too many revision numbers");
3049349b09eSxsa }
3059349b09eSxsa 
3069349b09eSxsa /*
3079349b09eSxsa  * Get revision from file.  The revision can be specified as a symbol or
3089349b09eSxsa  * a revision number.
3099349b09eSxsa  */
3109349b09eSxsa RCSNUM *
rcs_getrevnum(const char * rev_str,RCSFILE * file)3119349b09eSxsa rcs_getrevnum(const char *rev_str, RCSFILE *file)
3129349b09eSxsa {
3139349b09eSxsa 	RCSNUM *rev;
3149349b09eSxsa 
3159349b09eSxsa 	/* Search for symbol. */
3169349b09eSxsa 	rev = rcs_sym_getrev(file, rev_str);
3179349b09eSxsa 
3189349b09eSxsa 	/* Search for revision number. */
3199349b09eSxsa 	if (rev == NULL)
3209349b09eSxsa 		rev = rcsnum_parse(rev_str);
3219349b09eSxsa 
3229349b09eSxsa 	return (rev);
3239349b09eSxsa }
3249349b09eSxsa 
3259349b09eSxsa /*
3269349b09eSxsa  * Prompt for and store user's input in an allocated string.
3279349b09eSxsa  *
3289349b09eSxsa  * Returns the string's pointer.
3299349b09eSxsa  */
3309349b09eSxsa char *
rcs_prompt(const char * prompt,int flags)33100f4e67cSmillert rcs_prompt(const char *prompt, int flags)
3329349b09eSxsa {
3339349b09eSxsa 	BUF *bp;
3349349b09eSxsa 	size_t len;
3359349b09eSxsa 	char *buf;
3369349b09eSxsa 
33700f4e67cSmillert 	if (!(flags & INTERACTIVE) && isatty(STDIN_FILENO))
33800f4e67cSmillert 		flags |= INTERACTIVE;
33900f4e67cSmillert 
3400ee14128Sray 	bp = buf_alloc(0);
34100f4e67cSmillert 	if (flags & INTERACTIVE)
3429349b09eSxsa 		(void)fprintf(stderr, "%s", prompt);
34300f4e67cSmillert 	if (flags & INTERACTIVE)
3449349b09eSxsa 		(void)fprintf(stderr, ">> ");
345f8a4e2aaSray 	clearerr(stdin);
3469349b09eSxsa 	while ((buf = fgetln(stdin, &len)) != NULL) {
3479349b09eSxsa 		/* The last line may not be EOL terminated. */
3489349b09eSxsa 		if (buf[0] == '.' && (len == 1 || buf[1] == '\n'))
3499349b09eSxsa 			break;
3509349b09eSxsa 		else
3517bb3ddb0Sray 			buf_append(bp, buf, len);
3529349b09eSxsa 
35300f4e67cSmillert 		if (flags & INTERACTIVE)
3549349b09eSxsa 			(void)fprintf(stderr, ">> ");
3559349b09eSxsa 	}
3567bb3ddb0Sray 	buf_putc(bp, '\0');
3579349b09eSxsa 
3587bb3ddb0Sray 	return (buf_release(bp));
3599349b09eSxsa }
3609349b09eSxsa 
3619349b09eSxsa u_int
rcs_rev_select(RCSFILE * file,const char * range)3626e2f6b91Sray rcs_rev_select(RCSFILE *file, const char *range)
3639349b09eSxsa {
3649349b09eSxsa 	int i;
3659349b09eSxsa 	u_int nrev;
3661294ab80Sotto 	const char *ep;
3679349b09eSxsa 	char *lstr, *rstr;
3689349b09eSxsa 	struct rcs_delta *rdp;
3692dc36bedSjoris 	struct rcs_argvector *revargv, *revrange;
3709349b09eSxsa 	RCSNUM lnum, rnum;
3719349b09eSxsa 
3729349b09eSxsa 	nrev = 0;
3739349b09eSxsa 	(void)memset(&lnum, 0, sizeof(lnum));
3749349b09eSxsa 	(void)memset(&rnum, 0, sizeof(rnum));
3759349b09eSxsa 
3769349b09eSxsa 	if (range == NULL) {
3779349b09eSxsa 		TAILQ_FOREACH(rdp, &file->rf_delta, rd_list)
3789349b09eSxsa 			if (rcsnum_cmp(rdp->rd_num, file->rf_head, 0) == 0) {
3799349b09eSxsa 				rdp->rd_flags |= RCS_RD_SELECT;
3809349b09eSxsa 				return (1);
3819349b09eSxsa 			}
3829349b09eSxsa 		return (0);
3839349b09eSxsa 	}
3849349b09eSxsa 
3852dc36bedSjoris 	revargv = rcs_strsplit(range, ",");
3869349b09eSxsa 	for (i = 0; revargv->argv[i] != NULL; i++) {
3872dc36bedSjoris 		revrange = rcs_strsplit(revargv->argv[i], ":");
3889349b09eSxsa 		if (revrange->argv[0] == NULL)
3899349b09eSxsa 			/* should not happen */
390a3660ae3Sxsa 			errx(1, "invalid revision range: %s", revargv->argv[i]);
3919349b09eSxsa 		else if (revrange->argv[1] == NULL)
3929349b09eSxsa 			lstr = rstr = revrange->argv[0];
3939349b09eSxsa 		else {
3949349b09eSxsa 			if (revrange->argv[2] != NULL)
395a3660ae3Sxsa 				errx(1, "invalid revision range: %s",
3969349b09eSxsa 				    revargv->argv[i]);
3979349b09eSxsa 			lstr = revrange->argv[0];
3989349b09eSxsa 			rstr = revrange->argv[1];
3999349b09eSxsa 			if (strcmp(lstr, "") == 0)
4009349b09eSxsa 				lstr = NULL;
4019349b09eSxsa 			if (strcmp(rstr, "") == 0)
4029349b09eSxsa 				rstr = NULL;
4039349b09eSxsa 		}
4049349b09eSxsa 
4059349b09eSxsa 		if (lstr == NULL)
4069349b09eSxsa 			lstr = RCS_HEAD_INIT;
4079349b09eSxsa 		if (rcsnum_aton(lstr, &ep, &lnum) == 0 || (*ep != '\0'))
408a3660ae3Sxsa 			errx(1, "invalid revision: %s", lstr);
4099349b09eSxsa 
4109349b09eSxsa 		if (rstr != NULL) {
4119349b09eSxsa 			if (rcsnum_aton(rstr, &ep, &rnum) == 0 || (*ep != '\0'))
412a3660ae3Sxsa 				errx(1, "invalid revision: %s", rstr);
4139349b09eSxsa 		} else
4149349b09eSxsa 			rcsnum_cpy(file->rf_head, &rnum, 0);
4159349b09eSxsa 
4162dc36bedSjoris 		rcs_argv_destroy(revrange);
4179349b09eSxsa 
4189349b09eSxsa 		TAILQ_FOREACH(rdp, &file->rf_delta, rd_list)
4199349b09eSxsa 			if (rcsnum_cmp(rdp->rd_num, &lnum, 0) <= 0 &&
4209349b09eSxsa 			    rcsnum_cmp(rdp->rd_num, &rnum, 0) >= 0 &&
4219349b09eSxsa 			    !(rdp->rd_flags & RCS_RD_SELECT)) {
4229349b09eSxsa 				rdp->rd_flags |= RCS_RD_SELECT;
4239349b09eSxsa 				nrev++;
4249349b09eSxsa 			}
4259349b09eSxsa 	}
4262dc36bedSjoris 	rcs_argv_destroy(revargv);
4279349b09eSxsa 
4288ac837e5Snicm 	free(lnum.rn_id);
4298ac837e5Snicm 	free(rnum.rn_id);
4309349b09eSxsa 
4319349b09eSxsa 	return (nrev);
4329349b09eSxsa }
433d491b5edSray 
434d491b5edSray /*
435d491b5edSray  * Load description from <in> to <file>.
436d491b5edSray  * If <in> starts with a `-', <in> is taken as the description.
437d491b5edSray  * Otherwise <in> is the name of the file containing the description.
438d491b5edSray  * If <in> is NULL, the description is read from stdin.
4394b1a1f86Sray  * Returns 0 on success, -1 on failure, setting errno.
440d491b5edSray  */
4414b1a1f86Sray int
rcs_set_description(RCSFILE * file,const char * in,int flags)44200f4e67cSmillert rcs_set_description(RCSFILE *file, const char *in, int flags)
443d491b5edSray {
444d491b5edSray 	BUF *bp;
445d491b5edSray 	char *content;
446d491b5edSray 	const char *prompt =
447d491b5edSray 	    "enter description, terminated with single '.' or end of file:\n"
448d491b5edSray 	    "NOTE: This is NOT the log message!\n";
449d491b5edSray 
450d491b5edSray 	/* Description is in file <in>. */
451d491b5edSray 	if (in != NULL && *in != '-') {
4520ee14128Sray 		if ((bp = buf_load(in)) == NULL)
4534b1a1f86Sray 			return (-1);
4547bb3ddb0Sray 		buf_putc(bp, '\0');
4557bb3ddb0Sray 		content = buf_release(bp);
456d491b5edSray 	/* Description is in <in>. */
457d491b5edSray 	} else if (in != NULL)
458d491b5edSray 		/* Skip leading `-'. */
459d491b5edSray 		content = xstrdup(in + 1);
460d491b5edSray 	/* Get description from stdin. */
461d491b5edSray 	else
46200f4e67cSmillert 		content = rcs_prompt(prompt, flags);
463d491b5edSray 
464d491b5edSray 	rcs_desc_set(file, content);
4658ac837e5Snicm 	free(content);
4664b1a1f86Sray 	return (0);
467d491b5edSray }
468714c2363Sxsa 
469714c2363Sxsa /*
470714c2363Sxsa  * Split the contents of a file into a list of lines.
471714c2363Sxsa  */
472714c2363Sxsa struct rcs_lines *
rcs_splitlines(u_char * data,size_t len)473c2ac0f30Sxsa rcs_splitlines(u_char *data, size_t len)
474714c2363Sxsa {
4753de4517bSniallo 	u_char *c, *p;
476714c2363Sxsa 	struct rcs_lines *lines;
477714c2363Sxsa 	struct rcs_line *lp;
47814b185ffSniallo 	size_t i, tlen;
479714c2363Sxsa 
480e3a3b5caStedu 	lines = xcalloc(1, sizeof(*lines));
481714c2363Sxsa 	TAILQ_INIT(&(lines->l_lines));
482714c2363Sxsa 
483e3a3b5caStedu 	lp = xcalloc(1, sizeof(*lp));
484714c2363Sxsa 	TAILQ_INSERT_TAIL(&(lines->l_lines), lp, l_list);
485714c2363Sxsa 
4863de4517bSniallo 
48714b185ffSniallo 	p = c = data;
48814b185ffSniallo 	for (i = 0; i < len; i++) {
48914b185ffSniallo 		if (*p == '\n' || (i == len - 1)) {
49041e7f8c9Sniallo 			tlen = p - c + 1;
491714c2363Sxsa 			lp = xmalloc(sizeof(*lp));
49214b185ffSniallo 			lp->l_line = c;
49314b185ffSniallo 			lp->l_len = tlen;
494714c2363Sxsa 			lp->l_lineno = ++(lines->l_nblines);
495714c2363Sxsa 			TAILQ_INSERT_TAIL(&(lines->l_lines), lp, l_list);
4963de4517bSniallo 			c = p + 1;
4973de4517bSniallo 		}
4983de4517bSniallo 		p++;
499714c2363Sxsa 	}
500714c2363Sxsa 
501714c2363Sxsa 	return (lines);
502714c2363Sxsa }
503714c2363Sxsa 
504714c2363Sxsa void
rcs_freelines(struct rcs_lines * lines)505714c2363Sxsa rcs_freelines(struct rcs_lines *lines)
506714c2363Sxsa {
507714c2363Sxsa 	struct rcs_line *lp;
508714c2363Sxsa 
509714c2363Sxsa 	while ((lp = TAILQ_FIRST(&(lines->l_lines))) != NULL) {
510714c2363Sxsa 		TAILQ_REMOVE(&(lines->l_lines), lp, l_list);
5118ac837e5Snicm 		free(lp);
512714c2363Sxsa 	}
513714c2363Sxsa 
5148ac837e5Snicm 	free(lines);
515714c2363Sxsa }
516714c2363Sxsa 
517714c2363Sxsa BUF *
rcs_patchfile(u_char * data,size_t dlen,u_char * patch,size_t plen,int (* p)(struct rcs_lines *,struct rcs_lines *))518c2ac0f30Sxsa rcs_patchfile(u_char *data, size_t dlen, u_char *patch, size_t plen,
519714c2363Sxsa     int (*p)(struct rcs_lines *, struct rcs_lines *))
520714c2363Sxsa {
521714c2363Sxsa 	struct rcs_lines *dlines, *plines;
522714c2363Sxsa 	struct rcs_line *lp;
523714c2363Sxsa 	BUF *res;
524714c2363Sxsa 
52514b185ffSniallo 	dlines = rcs_splitlines(data, dlen);
52614b185ffSniallo 	plines = rcs_splitlines(patch, plen);
527714c2363Sxsa 
528714c2363Sxsa 	if (p(dlines, plines) < 0) {
529714c2363Sxsa 		rcs_freelines(dlines);
530714c2363Sxsa 		rcs_freelines(plines);
531714c2363Sxsa 		return (NULL);
532714c2363Sxsa 	}
533714c2363Sxsa 
5340ee14128Sray 	res = buf_alloc(1024);
535714c2363Sxsa 	TAILQ_FOREACH(lp, &dlines->l_lines, l_list) {
53614b185ffSniallo 		if (lp->l_line == NULL)
53714b185ffSniallo 			continue;
5387bb3ddb0Sray 		buf_append(res, lp->l_line, lp->l_len);
539714c2363Sxsa 	}
540714c2363Sxsa 
541714c2363Sxsa 	rcs_freelines(dlines);
542714c2363Sxsa 	rcs_freelines(plines);
543714c2363Sxsa 	return (res);
544714c2363Sxsa }
545714c2363Sxsa 
546714c2363Sxsa /*
547714c2363Sxsa  * rcs_yesno()
548714c2363Sxsa  *
5499642f3ecSmillert  * Read a char from standard input, returns defc if the
5509642f3ecSmillert  * user enters an equivalent to defc, else whatever char
5519642f3ecSmillert  * was entered.  Converts input to lower case.
552714c2363Sxsa  */
553714c2363Sxsa int
rcs_yesno(int defc)5549642f3ecSmillert rcs_yesno(int defc)
555714c2363Sxsa {
556714c2363Sxsa 	int c, ret;
557714c2363Sxsa 
558714c2363Sxsa 	fflush(stderr);
559714c2363Sxsa 	fflush(stdout);
560714c2363Sxsa 
561f8a4e2aaSray 	clearerr(stdin);
5629642f3ecSmillert 	if (isalpha(c = getchar()))
5639642f3ecSmillert 		c = tolower(c);
5649642f3ecSmillert 	if (c == defc || c == '\n' || (c == EOF && feof(stdin)))
5659642f3ecSmillert 		ret = defc;
566714c2363Sxsa 	else
5679642f3ecSmillert 		ret = c;
5689642f3ecSmillert 
569714c2363Sxsa 	while (c != EOF && c != '\n')
570714c2363Sxsa 		c = getchar();
571714c2363Sxsa 
572714c2363Sxsa 	return (ret);
573714c2363Sxsa }
574714c2363Sxsa 
575714c2363Sxsa /*
576714c2363Sxsa  * rcs_strsplit()
577714c2363Sxsa  *
578714c2363Sxsa  * Split a string <str> of <sep>-separated values and allocate
579714c2363Sxsa  * an argument vector for the values found.
580714c2363Sxsa  */
581714c2363Sxsa struct rcs_argvector *
rcs_strsplit(const char * str,const char * sep)5826e2f6b91Sray rcs_strsplit(const char *str, const char *sep)
583714c2363Sxsa {
584714c2363Sxsa 	struct rcs_argvector *av;
585714c2363Sxsa 	size_t i = 0;
586714c2363Sxsa 	char *cp, *p;
587714c2363Sxsa 
588714c2363Sxsa 	cp = xstrdup(str);
589714c2363Sxsa 	av = xmalloc(sizeof(*av));
590714c2363Sxsa 	av->str = cp;
591b2d55e7dSray 	av->argv = xmalloc(sizeof(*(av->argv)));
592714c2363Sxsa 
593714c2363Sxsa 	while ((p = strsep(&cp, sep)) != NULL) {
594714c2363Sxsa 		av->argv[i++] = p;
595caa2ffb0Sderaadt 		av->argv = xreallocarray(av->argv,
596714c2363Sxsa 		    i + 1, sizeof(*(av->argv)));
597714c2363Sxsa 	}
598714c2363Sxsa 	av->argv[i] = NULL;
599714c2363Sxsa 
600714c2363Sxsa 	return (av);
601714c2363Sxsa }
602714c2363Sxsa 
603714c2363Sxsa /*
604714c2363Sxsa  * rcs_argv_destroy()
605714c2363Sxsa  *
606714c2363Sxsa  * Free an argument vector previously allocated by rcs_strsplit().
607714c2363Sxsa  */
608714c2363Sxsa void
rcs_argv_destroy(struct rcs_argvector * av)609714c2363Sxsa rcs_argv_destroy(struct rcs_argvector *av)
610714c2363Sxsa {
6118ac837e5Snicm 	free(av->str);
6128ac837e5Snicm 	free(av->argv);
6138ac837e5Snicm 	free(av);
614714c2363Sxsa }
615168f4dcaSotto 
616168f4dcaSotto /*
617168f4dcaSotto  * Strip suffix from filename.
618168f4dcaSotto  */
619168f4dcaSotto void
rcs_strip_suffix(char * filename)620168f4dcaSotto rcs_strip_suffix(char *filename)
621168f4dcaSotto {
622168f4dcaSotto 	char *p, *suffixes, *next, *ext;
623168f4dcaSotto 
624168f4dcaSotto 	if ((p = strrchr(filename, ',')) != NULL) {
625168f4dcaSotto 		suffixes = xstrdup(rcs_suffixes);
626168f4dcaSotto 		for (next = suffixes; (ext = strsep(&next, "/")) != NULL;) {
627168f4dcaSotto 			if (!strcmp(p, ext)) {
628168f4dcaSotto 				*p = '\0';
629168f4dcaSotto 				break;
630168f4dcaSotto 			}
631168f4dcaSotto 		}
6328ac837e5Snicm 		free(suffixes);
633168f4dcaSotto 	}
634168f4dcaSotto }
635