Skip to content

Commit

Permalink
7712 mandoc -Tlint does always exit with error code 0
Browse files Browse the repository at this point in the history
Reviewed by: Dan McDonald <danmcd@omniti.com>
Reviewed by: Toomas Soome <tsoome@me.com>
Approved by: Robert Mustacchi <rm@joyent.com>
  • Loading branch information
Yuri Pankov authored and rmustacc committed Jan 10, 2017
1 parent 28b83f2 commit a593473
Show file tree
Hide file tree
Showing 9 changed files with 107 additions and 94 deletions.
101 changes: 58 additions & 43 deletions usr/src/cmd/mandoc/main.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
/* $Id: main.c,v 1.269 2016/07/12 05:18:38 kristaps Exp $ */

/* $Id: main.c,v 1.273.2.3 2017/01/09 02:27:58 schwarze Exp $ */
/*
* Copyright (c) 2008-2012 Kristaps Dzonsons <kristaps@bsd.lv>
* Copyright (c) 2010-2012, 2014-2016 Ingo Schwarze <schwarze@openbsd.org>
* Copyright (c) 2010-2012, 2014-2017 Ingo Schwarze <schwarze@openbsd.org>
* Copyright (c) 2010 Joerg Sonnenberger <joerg@netbsd.org>
*
* Permission to use, copy, modify, and distribute this software for any
Expand Down Expand Up @@ -51,12 +52,6 @@
#include "manconf.h"
#include "mansearch.h"

#if !defined(__GNUC__) || (__GNUC__ < 2)
# if !defined(lint)
# define __attribute__(x)
# endif
#endif /* !defined(__GNUC__) || (__GNUC__ < 2) */

enum outmode {
OUTMODE_DEF = 0,
OUTMODE_FLN,
Expand Down Expand Up @@ -87,6 +82,11 @@ struct curparse {
struct manoutput *outopts; /* output options */
};


#if HAVE_SQLITE3
int mandocdb(int, char *[]);
#endif

static int fs_lookup(const struct manpaths *,
size_t ipath, const char *,
const char *, const char *,
Expand All @@ -95,12 +95,10 @@ static void fs_search(const struct mansearch *,
const struct manpaths *, int, char**,
struct manpage **, size_t *);
static int koptions(int *, char *);
#if HAVE_SQLITE3
int mandocdb(int, char**);
#endif
static int moptions(int *, char *);
static void mmsg(enum mandocerr, enum mandoclevel,
const char *, int, int, const char *);
static void outdata_alloc(struct curparse *);
static void parse(struct curparse *, int, const char *);
static void passthrough(const char *, int, int);
static pid_t spawn_pager(struct tag_files *);
Expand Down Expand Up @@ -486,8 +484,11 @@ main(int argc, char *argv[])
passthrough(resp->file, fd,
conf.output.synopsisonly);

if (argc > 1 && curp.outtype <= OUTT_UTF8)
if (argc > 1 && curp.outtype <= OUTT_UTF8) {
if (curp.outdata == NULL)
outdata_alloc(&curp);
terminal_sepline(curp.outdata);
}
} else if (rc < MANDOCLEVEL_ERROR)
rc = MANDOCLEVEL_ERROR;

Expand Down Expand Up @@ -747,32 +748,8 @@ parse(struct curparse *curp, int fd, const char *file)
if (rctmp != MANDOCLEVEL_OK && curp->wstop)
return;

/* If unset, allocate output dev now (if applicable). */

if (curp->outdata == NULL) {
switch (curp->outtype) {
case OUTT_HTML:
curp->outdata = html_alloc(curp->outopts);
break;
case OUTT_UTF8:
curp->outdata = utf8_alloc(curp->outopts);
break;
case OUTT_LOCALE:
curp->outdata = locale_alloc(curp->outopts);
break;
case OUTT_ASCII:
curp->outdata = ascii_alloc(curp->outopts);
break;
case OUTT_PDF:
curp->outdata = pdf_alloc(curp->outopts);
break;
case OUTT_PS:
curp->outdata = ps_alloc(curp->outopts);
break;
default:
break;
}
}
if (curp->outdata == NULL)
outdata_alloc(curp);

mparse_result(curp->mp, &man, NULL);

Expand Down Expand Up @@ -826,6 +803,34 @@ parse(struct curparse *curp, int fd, const char *file)
break;
}
}
mparse_updaterc(curp->mp, &rc);
}

static void
outdata_alloc(struct curparse *curp)
{
switch (curp->outtype) {
case OUTT_HTML:
curp->outdata = html_alloc(curp->outopts);
break;
case OUTT_UTF8:
curp->outdata = utf8_alloc(curp->outopts);
break;
case OUTT_LOCALE:
curp->outdata = locale_alloc(curp->outopts);
break;
case OUTT_ASCII:
curp->outdata = ascii_alloc(curp->outopts);
break;
case OUTT_PDF:
curp->outdata = pdf_alloc(curp->outopts);
break;
case OUTT_PS:
curp->outdata = ps_alloc(curp->outopts);
break;
default:
break;
}
}

static void
Expand All @@ -838,36 +843,46 @@ passthrough(const char *file, int fd, int synopsis_only)
const char *syscall;
char *line, *cp;
size_t linesz;
ssize_t len, written;
int print;

line = NULL;
linesz = 0;

if (fflush(stdout) == EOF) {
syscall = "fflush";
goto fail;
}

if ((stream = fdopen(fd, "r")) == NULL) {
close(fd);
syscall = "fdopen";
goto fail;
}

print = 0;
while (getline(&line, &linesz, stream) != -1) {
while ((len = getline(&line, &linesz, stream)) != -1) {
cp = line;
if (synopsis_only) {
if (print) {
if ( ! isspace((unsigned char)*cp))
goto done;
while (isspace((unsigned char)*cp))
while (isspace((unsigned char)*cp)) {
cp++;
len--;
}
} else {
if (strcmp(cp, synb) == 0 ||
strcmp(cp, synr) == 0)
print = 1;
continue;
}
}
if (fputs(cp, stdout)) {
for (; len > 0; len -= written) {
if ((written = write(STDOUT_FILENO, cp, len)) != -1)
continue;
fclose(stream);
syscall = "fputs";
syscall = "write";
goto fail;
}
}
Expand Down Expand Up @@ -978,7 +993,7 @@ woptions(struct curparse *curp, char *arg)

while (*arg) {
o = arg;
switch (getsubopt(&arg, UNCONST(toks), &v)) {
switch (getsubopt(&arg, (char * const *)toks, &v)) {
case 0:
curp->wstop = 1;
break;
Expand Down
11 changes: 7 additions & 4 deletions usr/src/cmd/mandoc/mandoc.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* $Id: mandoc.h,v 1.209 2016/01/08 02:53:13 schwarze Exp $ */
/* $Id: mandoc.h,v 1.213 2017/01/09 01:37:03 schwarze Exp $ */
/*
* Copyright (c) 2010, 2011, 2014 Kristaps Dzonsons <kristaps@bsd.lv>
* Copyright (c) 2010-2016 Ingo Schwarze <schwarze@openbsd.org>
* Copyright (c) 2010-2017 Ingo Schwarze <schwarze@openbsd.org>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
Expand Down Expand Up @@ -65,10 +65,11 @@ enum mandocerr {
MANDOCERR_DOC_EMPTY, /* no document body */
MANDOCERR_SEC_BEFORE, /* content before first section header: macro */
MANDOCERR_NAMESEC_FIRST, /* first section is not NAME: Sh title */
MANDOCERR_NAMESEC_NONM, /* NAME section without name */
MANDOCERR_NAMESEC_NONM, /* NAME section without Nm before Nd */
MANDOCERR_NAMESEC_NOND, /* NAME section without description */
MANDOCERR_NAMESEC_ND, /* description not at the end of NAME */
MANDOCERR_NAMESEC_BAD, /* bad NAME section content: macro */
MANDOCERR_NAMESEC_PUNCT, /* missing comma before name: Nm name */
MANDOCERR_ND_EMPTY, /* missing description line, using "" */
MANDOCERR_SEC_ORDER, /* sections out of conventional order: Sh title */
MANDOCERR_SEC_REP, /* duplicate section title: Sh title */
Expand Down Expand Up @@ -98,7 +99,7 @@ enum mandocerr {
MANDOCERR_ARG_EMPTY, /* empty argument, using 0n: macro arg */
MANDOCERR_BD_NOTYPE, /* missing display type, using -ragged: Bd */
MANDOCERR_BL_LATETYPE, /* list type is not the first argument: Bl arg */
MANDOCERR_BL_NOWIDTH, /* missing -width in -tag list, using 8n */
MANDOCERR_BL_NOWIDTH, /* missing -width in -tag list, using 6n */
MANDOCERR_EX_NONAME, /* missing utility name, using "": Ex */
MANDOCERR_FO_NOHEAD, /* missing function name, using "": Fo */
MANDOCERR_IT_NOHEAD, /* empty head in list item: Bl -type It */
Expand All @@ -107,6 +108,7 @@ enum mandocerr {
MANDOCERR_BF_BADFONT, /* unknown font type, using \fR: Bf font */
MANDOCERR_PF_SKIP, /* nothing follows prefix: Pf arg */
MANDOCERR_RS_EMPTY, /* empty reference block: Rs */
MANDOCERR_XR_NOSEC, /* missing section argument: Xr arg */
MANDOCERR_ARG_STD, /* missing -std argument, adding it: macro */
MANDOCERR_OP_EMPTY, /* missing option string, using "": OP */
MANDOCERR_UR_NOHEAD, /* missing resource identifier, using "": UR */
Expand Down Expand Up @@ -433,3 +435,4 @@ void mparse_result(struct mparse *,
const char *mparse_getkeep(const struct mparse *);
const char *mparse_strerror(enum mandocerr);
const char *mparse_strlevel(enum mandoclevel);
void mparse_updaterc(struct mparse *, enum mandoclevel *);
61 changes: 28 additions & 33 deletions usr/src/cmd/mandoc/read.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* $Id: read.c,v 1.149 2016/07/10 13:34:30 schwarze Exp $ */
/* $Id: read.c,v 1.150.2.5 2017/01/09 02:25:53 schwarze Exp $ */
/*
* Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
* Copyright (c) 2010-2016 Ingo Schwarze <schwarze@openbsd.org>
* Copyright (c) 2010-2017 Ingo Schwarze <schwarze@openbsd.org>
* Copyright (c) 2010, 2012 Joerg Sonnenberger <joerg@netbsd.org>
*
* Permission to use, copy, modify, and distribute this software for any
Expand Down Expand Up @@ -110,10 +110,11 @@ static const char * const mandocerrs[MANDOCERR_MAX] = {
"no document body",
"content before first section header",
"first section is not \"NAME\"",
"NAME section without name",
"NAME section without Nm before Nd",
"NAME section without description",
"description not at the end of NAME",
"bad NAME section content",
"missing comma before name",
"missing description line, using \"\"",
"sections out of conventional order",
"duplicate section title",
Expand Down Expand Up @@ -143,7 +144,7 @@ static const char * const mandocerrs[MANDOCERR_MAX] = {
"empty argument, using 0n",
"missing display type, using -ragged",
"list type is not the first argument",
"missing -width in -tag list, using 8n",
"missing -width in -tag list, using 6n",
"missing utility name, using \"\"",
"missing function name, using \"\"",
"empty head in list item",
Expand All @@ -152,6 +153,7 @@ static const char * const mandocerrs[MANDOCERR_MAX] = {
"unknown font type, using \\fR",
"nothing follows prefix",
"empty reference block",
"missing section argument",
"missing -std argument, adding it",
"missing option string, using \"\"",
"missing resource identifier, using \"\"",
Expand Down Expand Up @@ -291,13 +293,6 @@ choose_parser(struct mparse *curp)
}
}

if (curp->man == NULL) {
curp->man = roff_man_alloc(curp->roff, curp, curp->defos,
curp->options & MPARSE_QUICK ? 1 : 0);
curp->man->macroset = MACROSET_MAN;
curp->man->first->tok = TOKEN_NONE;
}

if (format == MPARSE_MDOC) {
mdoc_hash_init();
curp->man->macroset = MACROSET_MDOC;
Expand All @@ -324,6 +319,7 @@ mparse_buf_r(struct mparse *curp, struct buf blk, size_t i, int start)
const char *save_file;
char *cp;
size_t pos; /* byte number in the ln buffer */
size_t j; /* auxiliary byte number in the blk buffer */
enum rofferr rr;
int of;
int lnn; /* line number in the real file */
Expand Down Expand Up @@ -429,14 +425,21 @@ mparse_buf_r(struct mparse *curp, struct buf blk, size_t i, int start)
}

if ('"' == blk.buf[i + 1] || '#' == blk.buf[i + 1]) {
j = i;
i += 2;
/* Comment, skip to end of line */
for (; i < blk.sz; ++i) {
if ('\n' == blk.buf[i]) {
++i;
++lnn;
break;
}
if (blk.buf[i] != '\n')
continue;
if (blk.buf[i - 1] == ' ' ||
blk.buf[i - 1] == '\t')
mandoc_msg(
MANDOCERR_SPACE_EOL,
curp, curp->line,
pos + i-1 - j, NULL);
++i;
++lnn;
break;
}

/* Backout trailing whitespaces */
Expand Down Expand Up @@ -562,15 +565,7 @@ mparse_buf_r(struct mparse *curp, struct buf blk, size_t i, int start)
break;
}

/*
* If input parsers have not been allocated, do so now.
* We keep these instanced between parsers, but set them
* locally per parse routine since we can use different
* parsers with each one.
*/

if (curp->man == NULL ||
curp->man->macroset == MACROSET_NONE)
if (curp->man->macroset == MACROSET_NONE)
choose_parser(curp);

/*
Expand Down Expand Up @@ -683,10 +678,6 @@ read_whole_file(struct mparse *curp, const char *file, int fd,
static void
mparse_end(struct mparse *curp)
{

if (curp->man == NULL && curp->sodest == NULL)
curp->man = roff_man_alloc(curp->roff, curp, curp->defos,
curp->options & MPARSE_QUICK ? 1 : 0);
if (curp->man->macroset == MACROSET_NONE)
curp->man->macroset = MACROSET_MAN;
if (curp->man->macroset == MACROSET_MDOC)
Expand Down Expand Up @@ -842,11 +833,8 @@ mparse_alloc(int options, enum mandoclevel wlevel, mandocmsg mmsg,
void
mparse_reset(struct mparse *curp)
{

roff_reset(curp->roff);

if (curp->man != NULL)
roff_man_reset(curp->man);
roff_man_reset(curp->man);
if (curp->secondary)
curp->secondary->sz = 0;

Expand Down Expand Up @@ -884,6 +872,13 @@ mparse_result(struct mparse *curp, struct roff_man **man,
*man = curp->man;
}

void
mparse_updaterc(struct mparse *curp, enum mandoclevel *rc)
{
if (curp->file_status > *rc)
*rc = curp->file_status;
}

void
mandoc_vmsg(enum mandocerr t, struct mparse *m,
int ln, int pos, const char *fmt, ...)
Expand Down
Loading

0 comments on commit a593473

Please sign in to comment.