version 1.330, 2019/07/14 18:16:13 |
version 1.334, 2019/07/26 21:03:19 |
|
|
}; |
}; |
|
|
struct curparse { |
struct curparse { |
struct mparse *mp; |
|
struct manoutput *outopts; /* output options */ |
struct manoutput *outopts; /* output options */ |
void *outdata; /* data for output */ |
void *outdata; /* data for output */ |
char *os_s; /* operating system for display */ |
|
int wstop; /* stop after a file with a warning */ |
int wstop; /* stop after a file with a warning */ |
enum mandoc_os os_e; /* check base system conventions */ |
|
enum outt outtype; /* which output to use */ |
enum outt outtype; /* which output to use */ |
}; |
}; |
|
|
Line 99 static int fs_search(const struct mansearch *, |
|
Line 96 static int fs_search(const struct mansearch *, |
|
const struct manpaths *, int, char**, |
const struct manpaths *, int, char**, |
struct manpage **, size_t *); |
struct manpage **, size_t *); |
static void outdata_alloc(struct curparse *); |
static void outdata_alloc(struct curparse *); |
static void parse(struct curparse *, int, const char *); |
static void parse(struct mparse *, int, const char *, |
|
struct curparse *); |
static void passthrough(int, int); |
static void passthrough(int, int); |
|
static void run_pager(struct tag_files *); |
static pid_t spawn_pager(struct tag_files *); |
static pid_t spawn_pager(struct tag_files *); |
static void usage(enum argmode) __attribute__((__noreturn__)); |
static void usage(enum argmode) __attribute__((__noreturn__)); |
static int woptions(struct curparse *, char *); |
static int woptions(char *, enum mandoc_os *, int *); |
|
|
static const int sec_prios[] = {1, 4, 5, 8, 6, 3, 7, 2, 9}; |
static const int sec_prios[] = {1, 4, 5, 8, 6, 3, 7, 2, 9}; |
static char help_arg[] = "help"; |
static char help_arg[] = "help"; |
Line 113 static char *help_argv[] = {help_arg, NULL}; |
|
Line 112 static char *help_argv[] = {help_arg, NULL}; |
|
int |
int |
main(int argc, char *argv[]) |
main(int argc, char *argv[]) |
{ |
{ |
struct manconf conf; |
struct manconf conf; /* Manpaths and output options. */ |
struct mansearch search; |
|
struct curparse curp; |
struct curparse curp; |
struct winsize ws; |
struct winsize ws; /* Result of ioctl(TIOCGWINSZ). */ |
struct tag_files *tag_files; |
struct mansearch search; /* Search options. */ |
struct manpage *res, *resp; |
struct manpage *res, *resp; /* Search results. */ |
|
struct mparse *mp; /* Opaque parser object. */ |
|
struct tag_files *tag_files; /* Tagging state variables. */ |
|
const char *conf_file; /* -C: alternate config file. */ |
|
const char *os_s; /* -I: Operating system for display. */ |
const char *progname, *sec, *thisarg; |
const char *progname, *sec, *thisarg; |
char *conf_file, *defpaths, *auxpaths; |
char *defpaths; /* -M: override manpaths. */ |
char *oarg, *tagarg; |
char *auxpaths; /* -m: additional manpaths. */ |
|
char *oarg; /* -O: output option string. */ |
|
char *tagarg; /* -O tag: default value. */ |
unsigned char *uc; |
unsigned char *uc; |
size_t i, sz, ssz; |
size_t sz; /* Number of elements in res[]. */ |
|
size_t i, ssz; |
|
int options; /* Parser options. */ |
|
int show_usage; /* Invalid argument: give up. */ |
|
int use_pager; /* According to command line. */ |
int prio, best_prio; |
int prio, best_prio; |
enum outmode outmode; |
|
int fd, startdir; |
int fd, startdir; |
int show_usage; |
|
int options; |
|
int use_pager; |
|
int status, signum; |
|
int c; |
int c; |
pid_t pager_pid, tc_pgid, man_pgid, pid; |
enum mandoc_os os_e; /* Check base system conventions. */ |
|
enum outmode outmode; /* According to command line. */ |
|
|
#if HAVE_PROGNAME |
#if HAVE_PROGNAME |
progname = getprogname(); |
progname = getprogname(); |
Line 165 main(int argc, char *argv[]) |
|
Line 169 main(int argc, char *argv[]) |
|
/* Search options. */ |
/* Search options. */ |
|
|
memset(&conf, 0, sizeof(conf)); |
memset(&conf, 0, sizeof(conf)); |
conf_file = defpaths = NULL; |
conf_file = NULL; |
auxpaths = NULL; |
defpaths = auxpaths = NULL; |
|
|
memset(&search, 0, sizeof(struct mansearch)); |
memset(&search, 0, sizeof(struct mansearch)); |
search.outkey = "Nd"; |
search.outkey = "Nd"; |
Line 185 main(int argc, char *argv[]) |
|
Line 189 main(int argc, char *argv[]) |
|
|
|
/* Parser and formatter options. */ |
/* Parser and formatter options. */ |
|
|
|
os_e = MANDOC_OS_OTHER; |
|
os_s = NULL; |
memset(&curp, 0, sizeof(struct curparse)); |
memset(&curp, 0, sizeof(struct curparse)); |
curp.outtype = OUTT_LOCALE; |
curp.outtype = OUTT_LOCALE; |
curp.outopts = &conf.output; |
curp.outopts = &conf.output; |
Line 225 main(int argc, char *argv[]) |
|
Line 231 main(int argc, char *argv[]) |
|
"-I %s", optarg); |
"-I %s", optarg); |
return mandoc_msg_getrc(); |
return mandoc_msg_getrc(); |
} |
} |
if (curp.os_s != NULL) { |
if (os_s != NULL) { |
mandoc_msg(MANDOCERR_BADARG_DUPE, 0, 0, |
mandoc_msg(MANDOCERR_BADARG_DUPE, 0, 0, |
"-I %s", optarg); |
"-I %s", optarg); |
return mandoc_msg_getrc(); |
return mandoc_msg_getrc(); |
} |
} |
curp.os_s = mandoc_strdup(optarg + 3); |
os_s = optarg + 3; |
break; |
break; |
case 'K': |
case 'K': |
options &= ~(MPARSE_UTF8 | MPARSE_LATIN1); |
options &= ~(MPARSE_UTF8 | MPARSE_LATIN1); |
Line 296 main(int argc, char *argv[]) |
|
Line 302 main(int argc, char *argv[]) |
|
} |
} |
break; |
break; |
case 'W': |
case 'W': |
if (woptions(&curp, optarg) == -1) |
if (woptions(optarg, &os_e, &curp.wstop) == -1) |
return mandoc_msg_getrc(); |
return mandoc_msg_getrc(); |
break; |
break; |
case 'w': |
case 'w': |
Line 549 main(int argc, char *argv[]) |
|
Line 555 main(int argc, char *argv[]) |
|
} |
} |
|
|
mchars_alloc(); |
mchars_alloc(); |
curp.mp = mparse_alloc(options, curp.os_e, curp.os_s); |
mp = mparse_alloc(options, os_e, os_s); |
|
|
if (argc < 1) { |
if (argc < 1) { |
if (use_pager) { |
if (use_pager) { |
Line 559 main(int argc, char *argv[]) |
|
Line 565 main(int argc, char *argv[]) |
|
} |
} |
thisarg = "<stdin>"; |
thisarg = "<stdin>"; |
mandoc_msg_setinfilename(thisarg); |
mandoc_msg_setinfilename(thisarg); |
parse(&curp, STDIN_FILENO, thisarg); |
parse(mp, STDIN_FILENO, thisarg, &curp); |
mandoc_msg_setinfilename(NULL); |
mandoc_msg_setinfilename(NULL); |
} |
} |
|
|
Line 589 main(int argc, char *argv[]) |
|
Line 595 main(int argc, char *argv[]) |
|
thisarg = *argv; |
thisarg = *argv; |
|
|
mandoc_msg_setinfilename(thisarg); |
mandoc_msg_setinfilename(thisarg); |
fd = mparse_open(curp.mp, thisarg); |
fd = mparse_open(mp, thisarg); |
if (fd != -1) { |
if (fd != -1) { |
if (use_pager) { |
if (use_pager) { |
use_pager = 0; |
use_pager = 0; |
Line 599 main(int argc, char *argv[]) |
|
Line 605 main(int argc, char *argv[]) |
|
} |
} |
|
|
if (resp == NULL || resp->form == FORM_SRC) |
if (resp == NULL || resp->form == FORM_SRC) |
parse(&curp, fd, thisarg); |
parse(mp, fd, thisarg, &curp); |
else |
else |
passthrough(fd, conf.output.synopsisonly); |
passthrough(fd, conf.output.synopsisonly); |
|
|
Line 635 main(int argc, char *argv[]) |
|
Line 641 main(int argc, char *argv[]) |
|
else |
else |
argv++; |
argv++; |
if (--argc) |
if (--argc) |
mparse_reset(curp.mp); |
mparse_reset(mp); |
} |
} |
if (startdir != -1) { |
if (startdir != -1) { |
(void)fchdir(startdir); |
(void)fchdir(startdir); |
Line 661 main(int argc, char *argv[]) |
|
Line 667 main(int argc, char *argv[]) |
|
} |
} |
} |
} |
mandoc_xr_free(); |
mandoc_xr_free(); |
mparse_free(curp.mp); |
mparse_free(mp); |
mchars_free(); |
mchars_free(); |
|
|
out: |
out: |
|
|
mansearch_free(res, sz); |
mansearch_free(res, sz); |
} |
} |
|
|
free(curp.os_s); |
|
|
|
/* |
|
* When using a pager, finish writing both temporary files, |
|
* fork it, wait for the user to close it, and clean up. |
|
*/ |
|
|
|
if (tag_files != NULL) { |
if (tag_files != NULL) { |
fclose(stdout); |
fclose(stdout); |
tag_write(); |
tag_write(); |
man_pgid = getpgid(0); |
run_pager(tag_files); |
tag_files->tcpgid = man_pgid == getpid() ? |
|
getpgid(getppid()) : man_pgid; |
|
pager_pid = 0; |
|
signum = SIGSTOP; |
|
for (;;) { |
|
|
|
/* Stop here until moved to the foreground. */ |
|
|
|
tc_pgid = tcgetpgrp(tag_files->ofd); |
|
if (tc_pgid != man_pgid) { |
|
if (tc_pgid == pager_pid) { |
|
(void)tcsetpgrp(tag_files->ofd, |
|
man_pgid); |
|
if (signum == SIGTTIN) |
|
continue; |
|
} else |
|
tag_files->tcpgid = tc_pgid; |
|
kill(0, signum); |
|
continue; |
|
} |
|
|
|
/* Once in the foreground, activate the pager. */ |
|
|
|
if (pager_pid) { |
|
(void)tcsetpgrp(tag_files->ofd, pager_pid); |
|
kill(pager_pid, SIGCONT); |
|
} else |
|
pager_pid = spawn_pager(tag_files); |
|
|
|
/* Wait for the pager to stop or exit. */ |
|
|
|
while ((pid = waitpid(pager_pid, &status, |
|
WUNTRACED)) == -1 && errno == EINTR) |
|
continue; |
|
|
|
if (pid == -1) { |
|
mandoc_msg(MANDOCERR_WAIT, 0, 0, |
|
"%s", strerror(errno)); |
|
break; |
|
} |
|
if (!WIFSTOPPED(status)) |
|
break; |
|
|
|
signum = WSTOPSIG(status); |
|
} |
|
tag_unlink(); |
tag_unlink(); |
} else if (curp.outtype != OUTT_LINT) |
} else if (curp.outtype != OUTT_LINT && |
|
(search.argmode == ARG_FILE || sz > 0)) |
mandoc_msg_summary(); |
mandoc_msg_summary(); |
|
|
return (int)mandoc_msg_getrc(); |
return (int)mandoc_msg_getrc(); |
Line 887 fs_search(const struct mansearch *cfg, const struct ma |
|
Line 842 fs_search(const struct mansearch *cfg, const struct ma |
|
} |
} |
|
|
static void |
static void |
parse(struct curparse *curp, int fd, const char *file) |
parse(struct mparse *mp, int fd, const char *file, struct curparse *curp) |
{ |
{ |
struct roff_meta *meta; |
struct roff_meta *meta; |
|
|
Line 896 parse(struct curparse *curp, int fd, const char *file) |
|
Line 851 parse(struct curparse *curp, int fd, const char *file) |
|
assert(file); |
assert(file); |
assert(fd >= 0); |
assert(fd >= 0); |
|
|
mparse_readfd(curp->mp, fd, file); |
mparse_readfd(mp, fd, file); |
if (fd != STDIN_FILENO) |
if (fd != STDIN_FILENO) |
close(fd); |
close(fd); |
|
|
Line 914 parse(struct curparse *curp, int fd, const char *file) |
|
Line 869 parse(struct curparse *curp, int fd, const char *file) |
|
html_reset(curp); |
html_reset(curp); |
|
|
mandoc_xr_reset(); |
mandoc_xr_reset(); |
meta = mparse_result(curp->mp); |
meta = mparse_result(mp); |
|
|
/* Execute the out device, if it exists. */ |
/* Execute the out device, if it exists. */ |
|
|
Line 952 parse(struct curparse *curp, int fd, const char *file) |
|
Line 907 parse(struct curparse *curp, int fd, const char *file) |
|
tree_man(curp->outdata, meta); |
tree_man(curp->outdata, meta); |
break; |
break; |
case OUTT_MAN: |
case OUTT_MAN: |
mparse_copy(curp->mp); |
mparse_copy(mp); |
break; |
break; |
case OUTT_PDF: |
case OUTT_PDF: |
case OUTT_ASCII: |
case OUTT_ASCII: |
|
|
} |
} |
|
|
static int |
static int |
woptions(struct curparse *curp, char *arg) |
woptions(char *arg, enum mandoc_os *os_e, int *wstop) |
{ |
{ |
char *v, *o; |
char *v, *o; |
const char *toks[11]; |
const char *toks[11]; |
Line 1113 woptions(struct curparse *curp, char *arg) |
|
Line 1068 woptions(struct curparse *curp, char *arg) |
|
o = arg; |
o = arg; |
switch (getsubopt(&arg, (char * const *)toks, &v)) { |
switch (getsubopt(&arg, (char * const *)toks, &v)) { |
case 0: |
case 0: |
curp->wstop = 1; |
*wstop = 1; |
break; |
break; |
case 1: |
case 1: |
case 2: |
case 2: |
Line 1136 woptions(struct curparse *curp, char *arg) |
|
Line 1091 woptions(struct curparse *curp, char *arg) |
|
break; |
break; |
case 8: |
case 8: |
mandoc_msg_setmin(MANDOCERR_BASE); |
mandoc_msg_setmin(MANDOCERR_BASE); |
curp->os_e = MANDOC_OS_OPENBSD; |
*os_e = MANDOC_OS_OPENBSD; |
break; |
break; |
case 9: |
case 9: |
mandoc_msg_setmin(MANDOCERR_BASE); |
mandoc_msg_setmin(MANDOCERR_BASE); |
curp->os_e = MANDOC_OS_NETBSD; |
*os_e = MANDOC_OS_NETBSD; |
break; |
break; |
default: |
default: |
mandoc_msg(MANDOCERR_BADARG_BAD, 0, 0, "-W %s", o); |
mandoc_msg(MANDOCERR_BADARG_BAD, 0, 0, "-W %s", o); |
Line 1150 woptions(struct curparse *curp, char *arg) |
|
Line 1105 woptions(struct curparse *curp, char *arg) |
|
return 0; |
return 0; |
} |
} |
|
|
|
/* |
|
* Wait until moved to the foreground, |
|
* then fork the pager and wait for the user to close it. |
|
*/ |
|
static void |
|
run_pager(struct tag_files *tag_files) |
|
{ |
|
int signum, status; |
|
pid_t man_pgid, tc_pgid; |
|
pid_t pager_pid, wait_pid; |
|
|
|
man_pgid = getpgid(0); |
|
tag_files->tcpgid = man_pgid == getpid() ? getpgid(getppid()) : |
|
man_pgid; |
|
pager_pid = 0; |
|
signum = SIGSTOP; |
|
|
|
for (;;) { |
|
/* Stop here until moved to the foreground. */ |
|
|
|
tc_pgid = tcgetpgrp(tag_files->ofd); |
|
if (tc_pgid != man_pgid) { |
|
if (tc_pgid == pager_pid) { |
|
(void)tcsetpgrp(tag_files->ofd, man_pgid); |
|
if (signum == SIGTTIN) |
|
continue; |
|
} else |
|
tag_files->tcpgid = tc_pgid; |
|
kill(0, signum); |
|
continue; |
|
} |
|
|
|
/* Once in the foreground, activate the pager. */ |
|
|
|
if (pager_pid) { |
|
(void)tcsetpgrp(tag_files->ofd, pager_pid); |
|
kill(pager_pid, SIGCONT); |
|
} else |
|
pager_pid = spawn_pager(tag_files); |
|
|
|
/* Wait for the pager to stop or exit. */ |
|
|
|
while ((wait_pid = waitpid(pager_pid, &status, |
|
WUNTRACED)) == -1 && errno == EINTR) |
|
continue; |
|
|
|
if (wait_pid == -1) { |
|
mandoc_msg(MANDOCERR_WAIT, 0, 0, |
|
"%s", strerror(errno)); |
|
break; |
|
} |
|
if (!WIFSTOPPED(status)) |
|
break; |
|
|
|
signum = WSTOPSIG(status); |
|
} |
|
} |
|
|
static pid_t |
static pid_t |
spawn_pager(struct tag_files *tag_files) |
spawn_pager(struct tag_files *tag_files) |
{ |
{ |
Line 1193 spawn_pager(struct tag_files *tag_files) |
|
Line 1206 spawn_pager(struct tag_files *tag_files) |
|
|
|
use_ofn = 1; |
use_ofn = 1; |
#if HAVE_LESS_T |
#if HAVE_LESS_T |
if ((cmdlen = strlen(argv[0])) >= 4) { |
if (*tag_files->tfn != '\0' && (cmdlen = strlen(argv[0])) >= 4) { |
cp = argv[0] + cmdlen - 4; |
cp = argv[0] + cmdlen - 4; |
if (strcmp(cp, "less") == 0) { |
if (strcmp(cp, "less") == 0) { |
argv[argc++] = mandoc_strdup("-T"); |
argv[argc++] = mandoc_strdup("-T"); |