main.c revision 260e9a87725c090ba5835b1f9f0b62fa2f96036f
/* $Id: main.c,v 1.225 2015/03/10 13:50:03 schwarze Exp $ */
/*
* Copyright (c) 2008-2012 Kristaps Dzonsons <kristaps@bsd.lv>
* Copyright (c) 2010-2012, 2014, 2015 Ingo Schwarze <schwarze@openbsd.org>
* Copyright (c) 2010 Joerg Sonnenberger <joerg@netbsd.org>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include "config.h"
#include <assert.h>
#include <ctype.h>
#include <errno.h>
#include <fcntl.h>
#include <glob.h>
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "mandoc.h"
#include "mandoc_aux.h"
#include "main.h"
#include "mdoc.h"
#include "man.h"
#include "manpath.h"
#include "mansearch.h"
# if !defined(lint)
# define __attribute__(x)
# endif
#endif /* !defined(__GNUC__) || (__GNUC__ < 2) */
enum outmode {
OUTMODE_DEF = 0,
};
typedef void (*out_free)(void *);
enum outt {
OUTT_ASCII = 0, /* -Tascii */
OUTT_LOCALE, /* -Tlocale */
OUTT_UTF8, /* -Tutf8 */
OUTT_TREE, /* -Ttree */
OUTT_MAN, /* -Tman */
OUTT_HTML, /* -Thtml */
OUTT_LINT, /* -Tlint */
OUTT_PS, /* -Tps */
OUTT_PDF /* -Tpdf */
};
struct curparse {
int wstop; /* stop after a file with a warning */
void *outdata; /* data for output */
};
const char *, const char *,
const struct manpaths *, int, char**,
static int koptions(int *, char *);
#if HAVE_SQLITE3
int mandocdb(int, char**);
#endif
static int moptions(int *, char *);
const char *, int, int, const char *);
const char *, enum mandoclevel *);
static enum mandoclevel passthrough(const char *, int, int);
static pid_t spawn_pager(void);
static char help_arg[] = "help";
static const char *progname;
int
{
char *auxpaths;
char *defos;
unsigned char *uc;
char sec;
int fd;
int show_usage;
int options;
int c;
if (argc < 1)
progname = "mandoc";
else
++progname;
#if HAVE_SQLITE3
#endif
/* Search options. */
else
/* Parser and formatter options. */
pager_pid = 1;
show_usage = 0;
synopsis_only = 0;
"aC:cfhI:iK:klM:m:O:S:s:T:VW:w"))) {
switch (c) {
case 'a':
break;
case 'C':
break;
case 'c':
pager_pid = 0;
break;
case 'f':
break;
case 'h':
synopsis_only = 1;
pager_pid = 0;
break;
case 'I':
"%s: -I %s: Bad argument\n",
return((int)MANDOCLEVEL_BADARG);
}
if (defos) {
"%s: -I %s: Duplicate argument\n",
return((int)MANDOCLEVEL_BADARG);
}
break;
case 'i':
break;
case 'K':
return((int)MANDOCLEVEL_BADARG);
break;
case 'k':
break;
case 'l':
break;
case 'M':
break;
case 'm':
break;
case 'O':
break;
case 'S':
break;
case 's':
break;
case 'T':
return((int)MANDOCLEVEL_BADARG);
break;
case 'W':
return((int)MANDOCLEVEL_BADARG);
break;
case 'w':
break;
default:
show_usage = 1;
break;
}
}
if (show_usage)
/* Postprocess options. */
if (outmode == OUTMODE_DEF) {
case ARG_FILE:
pager_pid = 0;
break;
case ARG_NAME:
break;
default:
break;
}
}
/* Parse arguments. */
if (argc > 0) {
}
/*
* Quirks for help(1)
* and for a man(1) section argument without -s.
*/
if (*progname == 'h') {
if (argc == 0) {
argc = 1;
}
} else if (argc > 1 &&
argv++;
argc--;
}
#ifdef MACHINE
#endif
}
rc = MANDOCLEVEL_OK;
/* man(1), whatis(1), apropos(1) */
if (argc == 0)
outmode == OUTMODE_ONE)
/* Access the mandoc database. */
#if HAVE_SQLITE3
mansearch_setup(1);
#else
fputs("mandoc: database support not compiled in\n",
stderr);
return((int)MANDOCLEVEL_BADARG);
}
sz = 0;
#endif
if (sz == 0) {
goto out;
}
/*
* For standard man(1) and -a output mode,
* prepare for copying filename pointers
* into the program parameter array.
*/
if (outmode == OUTMODE_ONE) {
argc = 1;
best_prio = 10;
} else if (outmode == OUTMODE_ALL)
/* Iterate all matching manuals. */
for (i = 0; i < sz; i++) {
if (outmode == OUTMODE_FLN)
else if (outmode == OUTMODE_LST)
else if (outmode == OUTMODE_ONE) {
/* Search for the best section. */
if ('\0' == sec)
continue;
continue;
}
}
/*
* For man(1), -a and -i output mode, fall through
* to the main mandoc(1) code iterating files
* and running the parsers on each of them.
*/
goto out;
}
/* mandoc(1) */
return((int)MANDOCLEVEL_BADARG);
/*
* Conditionally start up the lookaside buffer before parsing.
*/
if (argc < 1) {
pager_pid = spawn_pager();
}
while (argc > 0) {
if (fd != -1) {
pager_pid = spawn_pager();
/* For .so only; ignore failure. */
} else {
}
}
break;
resp++;
else
argv++;
if (--argc)
}
out:
#if HAVE_SQLITE3
mansearch_setup(0);
#endif
}
/*
* If a pager is attached, flush the pipe leading to it
* and signal end of file such that the user can browse
* to the end. Then wait for the user to close the pager.
*/
}
return((int)rc);
}
static void
{
switch (argmode) {
case ARG_FILE:
fputs("usage: mandoc [-acfhkl] [-Ios=name] "
"[-Kencoding] [-mformat] [-Ooption]\n"
"\t [-Toutput] [-Wlevel] [file ...]\n", stderr);
break;
case ARG_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 [-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 [-acfhklw] [-C file] "
"[-M path] [-m path] [-O outkey] [-S arch]\n"
"\t [-s section] expression ...\n", stderr);
break;
}
exit((int)MANDOCLEVEL_BADARG);
}
static int
{
char *file;
goto found;
goto found;
}
goto found;
}
if (globres == 0)
if (globres != 0)
return(0);
#if HAVE_SQLITE3
" consider running # makewhatis %s\n",
#endif
return(1);
}
static void
{
const char *const sections[] =
{"1", "8", "6", "2", "3", "3p", "5", "7", "4", "9"};
while (argc) {
return;
return;
}
"%s: No entry for %s in the manual.\n",
argv++;
argc--;
}
}
static void
enum mandoclevel *level)
{
enum mandoclevel rc;
/* Begin by parsing the file itself. */
/*
* With -Wstop and warnings or errors of at least the requested
* level, do not produce output.
*/
goto cleanup;
/* If unset, allocate output dev now (if applicable). */
case OUTT_HTML:
break;
case OUTT_UTF8:
break;
case OUTT_LOCALE:
break;
case OUTT_ASCII:
break;
case OUTT_PDF:
break;
case OUTT_PS:
break;
default:
break;
}
case OUTT_HTML:
break;
case OUTT_TREE:
break;
case OUTT_MAN:
break;
case OUTT_PDF:
/* FALLTHROUGH */
case OUTT_ASCII:
/* FALLTHROUGH */
case OUTT_UTF8:
/* FALLTHROUGH */
case OUTT_LOCALE:
/* FALLTHROUGH */
case OUTT_PS:
break;
default:
break;
}
}
/* Execute the out device, if it exists. */
}
static enum mandoclevel
{
const char synb[] = "S\bSY\bYN\bNO\bOP\bPS\bSI\bIS\bS";
const char synr[] = "SYNOPSIS";
const char *syscall;
char *line;
int print;
syscall = "fdopen";
goto fail;
}
print = 0;
if (synopsis_only) {
if (print) {
goto done;
while (len &&
line++;
len--;
}
} else {
print = 1;
continue;
}
}
syscall = "write";
goto fail;
}
}
syscall = "fgetln";
goto fail;
}
done:
return(MANDOCLEVEL_OK);
fail:
return(MANDOCLEVEL_SYSERR);
}
static int
{
*options |= MPARSE_UTF8;
*options &= ~MPARSE_LATIN1;
*options |= MPARSE_LATIN1;
*options &= ~MPARSE_UTF8;
} else {
return(0);
}
return(1);
}
static int
{
/* nothing to do */;
*options |= MPARSE_MDOC;
/* nothing to do */;
*options |= MPARSE_MAN;
else {
return(0);
}
return(1);
}
static int
{
else {
return(0);
}
return(1);
}
static int
{
char *v, *o;
const char *toks[7];
toks[0] = "stop";
while (*arg) {
o = arg;
case 0:
break;
case 1:
/* FALLTHROUGH */
case 2:
break;
case 3:
break;
case 4:
break;
case 5:
break;
default:
progname, o);
return(0);
}
}
return(1);
}
static void
{
const char *mparse_msg;
if (line)
if (msg)
}
static pid_t
spawn_pager(void)
{
#define MAX_PAGER_ARGS 16
char *argv[MAX_PAGER_ARGS];
const char *pager;
char *cp;
int fildes[2];
int argc;
return(0);
}
case -1:
exit((int)MANDOCLEVEL_SYSERR);
case 0:
break;
default:
exit((int)MANDOCLEVEL_SYSERR);
}
return(pager_pid);
}
/* The child process becomes the pager. */
exit((int)MANDOCLEVEL_SYSERR);
}
/*
* Parse the pager command into words.
* Intentionally do not do anything fancy here.
*/
argc = 0;
break;
*cp++ = '\0';
while (*cp == ' ')
cp++;
if (*cp == '\0')
break;
}
/* Hand over to the pager. */
exit((int)MANDOCLEVEL_SYSERR);
}