1 /* $Id: manpath.c,v 1.44 2021/11/05 18:03:08 schwarze Exp $ */ 2 /* 3 * Copyright (c) 2011,2014,2015,2017-2019 Ingo Schwarze <schwarze@openbsd.org> 4 * Copyright (c) 2011 Kristaps Dzonsons <kristaps@bsd.lv> 5 * 6 * Permission to use, copy, modify, and distribute this software for any 7 * purpose with or without fee is hereby granted, provided that the above 8 * copyright notice and this permission notice appear in all copies. 9 * 10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHORS DISCLAIM ALL WARRANTIES 11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR 13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 17 */ 18 #include "config.h" 19 20 #include <sys/types.h> 21 #include <sys/stat.h> 22 23 #include <ctype.h> 24 #include <errno.h> 25 #include <limits.h> 26 #include <stdio.h> 27 #include <stdlib.h> 28 #include <string.h> 29 30 #include "mandoc_aux.h" 31 #include "mandoc.h" 32 #include "manconf.h" 33 34 static void manconf_file(struct manconf *, const char *, int); 35 static void manpath_add(struct manpaths *, const char *, char); 36 static void manpath_parseline(struct manpaths *, char *, char); 37 38 39 void 40 manconf_parse(struct manconf *conf, const char *file, char *pend, char *pbeg) 41 { 42 int use_path_from_file = 1; 43 44 /* Always prepend -m. */ 45 manpath_parseline(&conf->manpath, pbeg, 'm'); 46 47 if (pend != NULL && *pend != '\0') { 48 /* If -M is given, it overrides everything else. */ 49 manpath_parseline(&conf->manpath, pend, 'M'); 50 use_path_from_file = 0; 51 pbeg = pend = NULL; 52 } else if ((pbeg = getenv("MANPATH")) == NULL || *pbeg == '\0') { 53 /* No MANPATH; use man.conf(5) only. */ 54 pbeg = pend = NULL; 55 } else if (*pbeg == ':') { 56 /* Prepend man.conf(5) to MANPATH. */ 57 pend = pbeg + 1; 58 pbeg = NULL; 59 } else if ((pend = strstr(pbeg, "::")) != NULL) { 60 /* Insert man.conf(5) into MANPATH. */ 61 *pend = '\0'; 62 pend += 2; 63 } else if (pbeg[strlen(pbeg) - 1] == ':') { 64 /* Append man.conf(5) to MANPATH. */ 65 pend = NULL; 66 } else { 67 /* MANPATH overrides man.conf(5) completely. */ 68 use_path_from_file = 0; 69 pend = NULL; 70 } 71 72 manpath_parseline(&conf->manpath, pbeg, '\0'); 73 74 if (file == NULL) 75 file = MAN_CONF_FILE; 76 manconf_file(conf, file, use_path_from_file); 77 78 manpath_parseline(&conf->manpath, pend, '\0'); 79 } 80 81 void 82 manpath_base(struct manpaths *dirs) 83 { 84 char path_base[] = MANPATH_BASE; 85 manpath_parseline(dirs, path_base, '\0'); 86 } 87 88 /* 89 * Parse a FULL pathname from a colon-separated list of arrays. 90 */ 91 static void 92 manpath_parseline(struct manpaths *dirs, char *path, char option) 93 { 94 char *dir; 95 96 if (NULL == path) 97 return; 98 99 for (dir = strtok(path, ":"); dir; dir = strtok(NULL, ":")) 100 manpath_add(dirs, dir, option); 101 } 102 103 /* 104 * Add a directory to the array, ignoring bad directories. 105 * Grow the array one-by-one for simplicity's sake. 106 */ 107 static void 108 manpath_add(struct manpaths *dirs, const char *dir, char option) 109 { 110 char buf[PATH_MAX]; 111 struct stat sb; 112 char *cp; 113 size_t i; 114 115 if ((cp = realpath(dir, buf)) == NULL) 116 goto fail; 117 118 for (i = 0; i < dirs->sz; i++) 119 if (strcmp(dirs->paths[i], dir) == 0) 120 return; 121 122 if (stat(cp, &sb) == -1) 123 goto fail; 124 125 dirs->paths = mandoc_reallocarray(dirs->paths, 126 dirs->sz + 1, sizeof(*dirs->paths)); 127 dirs->paths[dirs->sz++] = mandoc_strdup(cp); 128 return; 129 130 fail: 131 if (option != '\0') 132 mandoc_msg(MANDOCERR_BADARG_BAD, 0, 0, 133 "-%c %s: %s", option, dir, strerror(errno)); 134 } 135 136 void 137 manconf_free(struct manconf *conf) 138 { 139 size_t i; 140 141 for (i = 0; i < conf->manpath.sz; i++) 142 free(conf->manpath.paths[i]); 143 144 free(conf->manpath.paths); 145 free(conf->output.includes); 146 free(conf->output.man); 147 free(conf->output.paper); 148 free(conf->output.style); 149 } 150 151 static void 152 manconf_file(struct manconf *conf, const char *file, int use_path_from_file) 153 { 154 const char *const toks[] = { "manpath", "output" }; 155 char manpath_default[] = MANPATH_DEFAULT; 156 157 FILE *stream; 158 char *line, *cp, *ep; 159 size_t linesz, tok, toklen; 160 ssize_t linelen; 161 162 if ((stream = fopen(file, "r")) == NULL) 163 goto out; 164 165 line = NULL; 166 linesz = 0; 167 168 while ((linelen = getline(&line, &linesz, stream)) != -1) { 169 cp = line; 170 ep = cp + linelen - 1; 171 while (ep > cp && isspace((unsigned char)*ep)) 172 *ep-- = '\0'; 173 while (isspace((unsigned char)*cp)) 174 cp++; 175 if (cp == ep || *cp == '#') 176 continue; 177 178 for (tok = 0; tok < sizeof(toks)/sizeof(toks[0]); tok++) { 179 toklen = strlen(toks[tok]); 180 if (cp + toklen < ep && 181 isspace((unsigned char)cp[toklen]) && 182 strncmp(cp, toks[tok], toklen) == 0) { 183 cp += toklen; 184 while (isspace((unsigned char)*cp)) 185 cp++; 186 break; 187 } 188 } 189 190 switch (tok) { 191 case 0: /* manpath */ 192 if (use_path_from_file) 193 manpath_add(&conf->manpath, cp, '\0'); 194 *manpath_default = '\0'; 195 break; 196 case 1: /* output */ 197 manconf_output(&conf->output, cp, 1); 198 break; 199 default: 200 break; 201 } 202 } 203 free(line); 204 fclose(stream); 205 206 out: 207 if (use_path_from_file && *manpath_default != '\0') 208 manpath_parseline(&conf->manpath, manpath_default, '\0'); 209 } 210 211 int 212 manconf_output(struct manoutput *conf, const char *cp, int fromfile) 213 { 214 const char *const toks[] = { 215 /* Tokens requiring an argument. */ 216 "includes", "man", "paper", "style", "indent", "width", 217 "outfilename", "tagfilename", 218 /* Token taking an optional argument. */ 219 "tag", 220 /* Tokens not taking arguments. */ 221 "fragment", "mdoc", "noval", "toc" 222 }; 223 const size_t ntoks = sizeof(toks) / sizeof(toks[0]); 224 225 const char *errstr; 226 char *oldval; 227 size_t len, tok; 228 229 for (tok = 0; tok < ntoks; tok++) { 230 len = strlen(toks[tok]); 231 if (strncmp(cp, toks[tok], len) == 0 && 232 strchr(" = ", cp[len]) != NULL) { 233 cp += len; 234 if (*cp == '=') 235 cp++; 236 while (isspace((unsigned char)*cp)) 237 cp++; 238 break; 239 } 240 } 241 242 if (tok < 8 && *cp == '\0') { 243 mandoc_msg(MANDOCERR_BADVAL_MISS, 0, 0, "-O %s=?", toks[tok]); 244 return -1; 245 } 246 if (tok > 8 && tok < ntoks && *cp != '\0') { 247 mandoc_msg(MANDOCERR_BADVAL, 0, 0, "-O %s=%s", toks[tok], cp); 248 return -1; 249 } 250 251 switch (tok) { 252 case 0: 253 if (conf->includes != NULL) { 254 oldval = mandoc_strdup(conf->includes); 255 break; 256 } 257 conf->includes = mandoc_strdup(cp); 258 return 0; 259 case 1: 260 if (conf->man != NULL) { 261 oldval = mandoc_strdup(conf->man); 262 break; 263 } 264 conf->man = mandoc_strdup(cp); 265 return 0; 266 case 2: 267 if (conf->paper != NULL) { 268 oldval = mandoc_strdup(conf->paper); 269 break; 270 } 271 conf->paper = mandoc_strdup(cp); 272 return 0; 273 case 3: 274 if (conf->style != NULL) { 275 oldval = mandoc_strdup(conf->style); 276 break; 277 } 278 conf->style = mandoc_strdup(cp); 279 return 0; 280 case 4: 281 if (conf->indent) { 282 mandoc_asprintf(&oldval, "%zu", conf->indent); 283 break; 284 } 285 conf->indent = strtonum(cp, 0, 1000, &errstr); 286 if (errstr == NULL) 287 return 0; 288 mandoc_msg(MANDOCERR_BADVAL_BAD, 0, 0, 289 "-O indent=%s is %s", cp, errstr); 290 return -1; 291 case 5: 292 if (conf->width) { 293 mandoc_asprintf(&oldval, "%zu", conf->width); 294 break; 295 } 296 conf->width = strtonum(cp, 1, 1000, &errstr); 297 if (errstr == NULL) 298 return 0; 299 mandoc_msg(MANDOCERR_BADVAL_BAD, 0, 0, 300 "-O width=%s is %s", cp, errstr); 301 return -1; 302 case 6: 303 if (conf->outfilename != NULL) { 304 oldval = mandoc_strdup(conf->outfilename); 305 break; 306 } 307 conf->outfilename = mandoc_strdup(cp); 308 return 0; 309 case 7: 310 if (conf->tagfilename != NULL) { 311 oldval = mandoc_strdup(conf->tagfilename); 312 break; 313 } 314 conf->tagfilename = mandoc_strdup(cp); 315 return 0; 316 /* 317 * If the index of the following token changes, 318 * do not forget to adjust the range check above the switch. 319 */ 320 case 8: 321 if (conf->tag != NULL) { 322 oldval = mandoc_strdup(conf->tag); 323 break; 324 } 325 conf->tag = mandoc_strdup(cp); 326 return 0; 327 case 9: 328 conf->fragment = 1; 329 return 0; 330 case 10: 331 conf->mdoc = 1; 332 return 0; 333 case 11: 334 conf->noval = 1; 335 return 0; 336 case 12: 337 conf->toc = 1; 338 return 0; 339 default: 340 mandoc_msg(MANDOCERR_BADARG_BAD, 0, 0, "-O %s", cp); 341 return -1; 342 } 343 if (fromfile) { 344 free(oldval); 345 return 0; 346 } else { 347 mandoc_msg(MANDOCERR_BADVAL_DUPE, 0, 0, 348 "-O %s=%s: already set to %s", toks[tok], cp, oldval); 349 free(oldval); 350 return -1; 351 } 352 } 353