void manpath_manconf(struct manpaths *dirs, const char *file) { FILE *stream; char *p, *q; size_t len, keysz; keysz = strlen(MAN_CONF_KEY); assert(keysz > 0); if (NULL == (stream = fopen(file, "r"))) return; while (NULL != (p = fgetln(stream, &len))) { if (0 == len || '\n' != p[--len]) break; p[len] = '\0'; while (isspace((unsigned char)*p)) p++; if (strncmp(MAN_CONF_KEY, p, keysz)) continue; p += keysz; while (isspace(*p)) p++; if ('\0' == *p) continue; if (NULL == (q = strrchr(p, '/'))) continue; *q = '\0'; manpath_add(dirs, p); } fclose(stream); }
/* * Parse a FULL pathname from a colon-separated list of arrays. */ static void manpath_parseline(struct manpaths *dirs, char *path, int complain) { char *dir; if (NULL == path) return; for (dir = strtok(path, ":"); dir; dir = strtok(NULL, ":")) manpath_add(dirs, dir, complain); }
static void manconf_file(struct manconf *conf, const char *file) { const char *const toks[] = { "manpath", "output", "_whatdb" }; char manpath_default[] = MANPATH_DEFAULT; FILE *stream; char *line, *cp, *ep; size_t linesz, tok, toklen; ssize_t linelen; if ((stream = fopen(file, "r")) == NULL) goto out; line = NULL; linesz = 0; while ((linelen = getline(&line, &linesz, stream)) != -1) { cp = line; ep = cp + linelen - 1; while (ep > cp && isspace((unsigned char)*ep)) *ep-- = '\0'; while (isspace((unsigned char)*cp)) cp++; if (cp == ep || *cp == '#') continue; for (tok = 0; tok < sizeof(toks)/sizeof(toks[0]); tok++) { toklen = strlen(toks[tok]); if (cp + toklen < ep && isspace((unsigned char)cp[toklen]) && strncmp(cp, toks[tok], toklen) == 0) { cp += toklen; while (isspace((unsigned char)*cp)) cp++; break; } } switch (tok) { case 2: /* _whatdb */ while (ep > cp && ep[-1] != '/') ep--; if (ep == cp) continue; *ep = '\0'; /* FALLTHROUGH */ case 0: /* manpath */ manpath_add(&conf->manpath, cp, 0); *manpath_default = '\0'; break; case 1: /* output */ manconf_output(&conf->output, cp, 1); break; default: break; } } free(line); fclose(stream); out: if (*manpath_default != '\0') manpath_parseline(&conf->manpath, manpath_default, 0); }