=================================================================== RCS file: /cvs/mandoc/main.c,v retrieving revision 1.218 retrieving revision 1.223 diff -u -p -r1.218 -r1.223 --- mandoc/main.c 2015/02/03 21:16:02 1.218 +++ mandoc/main.c 2015/03/06 09:24:59 1.223 @@ -1,4 +1,4 @@ -/* $Id: main.c,v 1.218 2015/02/03 21:16:02 schwarze Exp $ */ +/* $Id: main.c,v 1.223 2015/03/06 09:24:59 kristaps Exp $ */ /* * Copyright (c) 2008-2012 Kristaps Dzonsons * Copyright (c) 2010-2012, 2014, 2015 Ingo Schwarze @@ -25,6 +25,7 @@ #include #include #include +#include #include #include #include @@ -103,7 +104,6 @@ static enum mandoclevel passthrough(const char *, int static void spawn_pager(void); static int toptions(struct curparse *, char *); static void usage(enum argmode) __attribute__((noreturn)); -static void version(void) __attribute__((noreturn)); static int woptions(struct curparse *, char *); static const int sec_prios[] = {1, 4, 5, 8, 6, 3, 7, 2, 9}; @@ -134,8 +134,9 @@ main(int argc, char *argv[]) int options; int c; - progname = strrchr(argv[0], '/'); - if (progname == NULL) + if (argc < 1) + progname = "mandoc"; + else if ((progname = strrchr(argv[0], '/')) == NULL) progname = argv[0]; else ++progname; @@ -256,9 +257,6 @@ main(int argc, char *argv[]) case 'w': outmode = OUTMODE_FLN; break; - case 'V': - version(); - /* NOTREACHED */ default: show_usage = 1; break; @@ -287,8 +285,10 @@ main(int argc, char *argv[]) /* Parse arguments. */ - argc -= optind; - argv += optind; + if (argc > 0) { + argc -= optind; + argv += optind; + } resp = NULL; /* @@ -303,18 +303,20 @@ main(int argc, char *argv[]) argc = 1; } } else if (argc > 1 && - ((uc = argv[0]) != NULL) && + ((uc = (unsigned char *)argv[0]) != NULL) && ((isdigit(uc[0]) && (uc[1] == '\0' || (isalpha(uc[1]) && uc[2] == '\0'))) || (uc[0] == 'n' && uc[1] == '\0'))) { - search.sec = uc; + search.sec = (char *)uc; argv++; argc--; } if (search.arch == NULL) search.arch = getenv("MACHINE"); +#ifdef MACHINE if (search.arch == NULL) search.arch = MACHINE; +#endif } rc = MANDOCLEVEL_OK; @@ -414,13 +416,13 @@ main(int argc, char *argv[]) if (OUTT_MAN == curp.outtype) mparse_keep(curp.mp); - if (argc == 0) { + if (argc < 1) { if (use_pager && isatty(STDOUT_FILENO)) spawn_pager(); parse(&curp, STDIN_FILENO, "", &rc); } - while (argc) { + while (argc > 0) { rctmp = mparse_open(curp.mp, &fd, resp != NULL ? resp->file : *argv); if (rc < rctmp) @@ -483,37 +485,29 @@ out: } static void -version(void) -{ - - printf("mandoc %s\n", VERSION); - exit((int)MANDOCLEVEL_OK); -} - -static void usage(enum argmode argmode) { switch (argmode) { case ARG_FILE: - fputs("usage: mandoc [-acfhklV] [-Ios=name] " + fputs("usage: mandoc [-acfhkl] [-Ios=name] " "[-Kencoding] [-mformat] [-Ooption]\n" "\t [-Toutput] [-Wlevel] [file ...]\n", stderr); break; case ARG_NAME: - fputs("usage: man [-acfhklVw] [-C file] [-I os=name] " + fputs("usage: man [-acfhklw] [-C file] [-I os=name] " "[-K encoding] [-M path] [-m path]\n" "\t [-O option=value] [-S subsection] [-s section] " "[-T output] [-W level]\n" "\t [section] name ...\n", stderr); break; case ARG_WORD: - fputs("usage: whatis [-acfhklVw] [-C file] " + fputs("usage: whatis [-acfhklw] [-C file] " "[-M path] [-m path] [-O outkey] [-S arch]\n" "\t [-s section] name ...\n", stderr); break; case ARG_EXPR: - fputs("usage: apropos [-acfhklVw] [-C file] " + fputs("usage: apropos [-acfhklw] [-C file] " "[-M path] [-m path] [-O outkey] [-S arch]\n" "\t [-s section] expression ...\n", stderr); break; @@ -526,16 +520,16 @@ fs_lookup(const struct manpaths *paths, size_t ipath, const char *sec, const char *arch, const char *name, struct manpage **res, size_t *ressz) { + glob_t globinfo; struct manpage *page; char *file; - int form; + int form, globres; + form = FORM_SRC; mandoc_asprintf(&file, "%s/man%s/%s.%s", paths->paths[ipath], sec, name, sec); - if (access(file, R_OK) != -1) { - form = FORM_SRC; + if (access(file, R_OK) != -1) goto found; - } free(file); mandoc_asprintf(&file, "%s/cat%s/%s.0", @@ -549,21 +543,31 @@ fs_lookup(const struct manpaths *paths, size_t ipath, if (arch != NULL) { mandoc_asprintf(&file, "%s/man%s/%s/%s.%s", paths->paths[ipath], sec, arch, name, sec); - if (access(file, R_OK) != -1) { - form = FORM_SRC; + if (access(file, R_OK) != -1) goto found; - } free(file); } - return(0); + mandoc_asprintf(&file, "%s/man%s/%s.*", + paths->paths[ipath], sec, name); + globres = glob(file, 0, NULL, &globinfo); + if (globres != 0 && globres != GLOB_NOMATCH) + fprintf(stderr, "%s: %s: glob: %s\n", + progname, file, strerror(errno)); + free(file); + if (globres == 0) + file = mandoc_strdup(*globinfo.gl_pathv); + globfree(&globinfo); + if (globres != 0) + return(0); + found: #if HAVE_SQLITE3 fprintf(stderr, "%s: outdated mandoc.db lacks %s(%s) entry,\n" " consider running # makewhatis %s\n", progname, name, sec, paths->paths[ipath]); #endif - + *res = mandoc_reallocarray(*res, ++*ressz, sizeof(struct manpage)); page = *res + (*ressz - 1); page->file = file;