xref: /openbsd/usr.bin/cvs/config.c (revision 3cab2bb3)
1 /*	$OpenBSD: config.c,v 1.17 2015/11/05 09:48:21 nicm Exp $	*/
2 /*
3  * Copyright (c) 2006 Joris Vink <joris@openbsd.org>
4  *
5  * Permission to use, copy, modify, and distribute this software for any
6  * purpose with or without fee is hereby granted, provided that the above
7  * copyright notice and this permission notice appear in all copies.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16  */
17 
18 #include <sys/types.h>
19 #include <sys/dirent.h>
20 #include <sys/resource.h>
21 
22 #include <errno.h>
23 #include <limits.h>
24 #include <stdlib.h>
25 #include <string.h>
26 
27 #include "cvs.h"
28 #include "config.h"
29 
30 void
31 cvs_parse_configfile(void)
32 {
33 	cvs_log(LP_TRACE, "cvs_parse_configfile()");
34 	cvs_read_config(CVS_PATH_CONFIG, config_parse_line);
35 }
36 
37 int
38 config_parse_line(char *line, int lineno)
39 {
40 	struct rlimit rl;
41 	const char *errstr;
42 	char *val, *opt, *ep;
43 
44 	opt = line;
45 	if ((val = strrchr(opt, '=')) == NULL)
46 		fatal("cvs_parse_configfile: bad option '%s'", opt);
47 
48 	*(val++) = '\0';
49 
50 	if (!strcmp(opt, "tag")) {
51 		free(cvs_tagname);
52 		cvs_tagname = xstrdup(val);
53 	} else if (!strcmp(opt, "umask")) {
54 		cvs_umask = strtol(val, &ep, 8);
55 
56 		if (val == ep || *ep != '\0')
57 			fatal("cvs_parse_configfile: umask %s is "
58 			    "invalid", val);
59 		if (cvs_umask < 0 || cvs_umask > 07777)
60 			fatal("cvs_parse_configfile: umask %s is "
61 			    "invalid", val);
62 	} else if (!strcmp(opt, "dlimit")) {
63 		if (getrlimit(RLIMIT_DATA, &rl) != -1) {
64 			rl.rlim_cur = (int)strtonum(val, 0, INT_MAX,
65 			    &errstr);
66 			if (errstr != NULL)
67 				fatal("cvs_parse_configfile: %s: %s",
68 				    val, errstr);
69 			rl.rlim_cur = rl.rlim_cur * 1024;
70 			(void)setrlimit(RLIMIT_DATA, &rl);
71 		}
72 	} else {
73 		cvs_log(LP_ERR, "ignoring unknown option '%s'", opt);
74 	}
75 
76 	return (0);
77 }
78 
79 void
80 cvs_read_config(char *name, int (*cb)(char *, int))
81 {
82 	FILE *fp;
83 	size_t len;
84 	int lineno;
85 	char *p, *buf, *lbuf, fpath[PATH_MAX];
86 
87 	(void)xsnprintf(fpath, sizeof(fpath), "%s/%s",
88 	    current_cvsroot->cr_dir, name);
89 
90 	if ((fp = fopen(fpath, "r")) == NULL)
91 		return;
92 
93 	lbuf = NULL;
94 	lineno = 0;
95 	while ((buf = fgetln(fp, &len)) != NULL) {
96 		lineno++;
97 		if (buf[len - 1] == '\n') {
98 			buf[len - 1] = '\0';
99 		} else {
100 			lbuf = xmalloc(len + 1);
101 			memcpy(lbuf, buf, len);
102 			lbuf[len] = '\0';
103 			buf = lbuf;
104 		}
105 
106 		p = buf;
107 		while (*p == ' ' || *p == '\t')
108 			p++;
109 
110 		if (p[0] == '#' || p[0] == '\0')
111 			continue;
112 
113 		if (cb(p, lineno) < 0)
114 			break;
115 	}
116 
117 	free(lbuf);
118 	(void)fclose(fp);
119 }
120