Import from OpenBSD: -E flag

Add -E flag (make warnings fatal), following the behavior of GNU m4 1.4.9+

Help and direction millert@ espie@ anton@ deraadt@

ok espie@

Obtained from:	OpenBSD
This commit is contained in:
Baptiste Daroussin 2019-09-09 15:18:04 +00:00
parent 3e6b8ce30e
commit c560b67c5c
7 changed files with 72 additions and 21 deletions

View File

@ -1,4 +1,4 @@
/* $OpenBSD: eval.c,v 1.74 2015/02/05 12:59:57 millert Exp $ */ /* $OpenBSD: eval.c,v 1.75 2017/06/15 13:48:42 bcallah Exp $ */
/* $NetBSD: eval.c,v 1.7 1996/11/10 21:21:29 pk Exp $ */ /* $NetBSD: eval.c,v 1.7 1996/11/10 21:21:29 pk Exp $ */
/*- /*-
@ -275,6 +275,10 @@ expand_builtin(const char *argv[], int argc, int td)
warn("%s at line %lu: include(%s)", warn("%s at line %lu: include(%s)",
CURRENT_NAME, CURRENT_LINE, argv[2]); CURRENT_NAME, CURRENT_LINE, argv[2]);
exit_code = 1; exit_code = 1;
if (fatal_warns) {
killdiv();
exit(exit_code);
}
} else } else
err(1, "%s at line %lu: include(%s)", err(1, "%s at line %lu: include(%s)",
CURRENT_NAME, CURRENT_LINE, argv[2]); CURRENT_NAME, CURRENT_LINE, argv[2]);

View File

@ -1,4 +1,4 @@
/* $OpenBSD: extern.h,v 1.54 2014/05/12 19:11:19 espie Exp $ */ /* $OpenBSD: extern.h,v 1.55 2017/06/15 13:48:42 bcallah Exp $ */
/* $NetBSD: extern.h,v 1.3 1996/01/13 23:25:24 pk Exp $ */ /* $NetBSD: extern.h,v 1.3 1996/01/13 23:25:24 pk Exp $ */
/*- /*-
@ -61,6 +61,8 @@ extern void doesyscmd(const char *);
extern void getdivfile(const char *); extern void getdivfile(const char *);
extern void doformat(const char *[], int); extern void doformat(const char *[], int);
extern void m4_warnx(const char *, ...);
/* look.c */ /* look.c */
#define FLAG_UNTRACED 0 #define FLAG_UNTRACED 0
@ -178,4 +180,5 @@ extern int synch_lines; /* line synchronisation directives */
extern int mimic_gnu; /* behaves like gnu-m4 */ extern int mimic_gnu; /* behaves like gnu-m4 */
extern int prefix_builtins; /* prefix builtin macros with m4_ */ extern int prefix_builtins; /* prefix builtin macros with m4_ */
extern int error_warns; /* make warnings cause exit_code = 1 */
extern int fatal_warns; /* make warnings fatal */

View File

@ -1,4 +1,4 @@
/* $OpenBSD: gnum4.c,v 1.50 2015/04/29 00:13:26 millert Exp $ */ /* $OpenBSD: gnum4.c,v 1.51 2017/06/15 13:48:42 bcallah Exp $ */
/*- /*-
* SPDX-License-Identifier: BSD-2-Clause * SPDX-License-Identifier: BSD-2-Clause
@ -39,6 +39,7 @@ __FBSDID("$FreeBSD$");
#include <err.h> #include <err.h>
#include <paths.h> #include <paths.h>
#include <regex.h> #include <regex.h>
#include <stdarg.h>
#include <stddef.h> #include <stddef.h>
#include <stdlib.h> #include <stdlib.h>
#include <stdint.h> #include <stdint.h>
@ -259,11 +260,29 @@ exit_regerror(int er, regex_t *re, const char *source)
m4errx(1, "regular expression error in %s: %s.", source, errbuf); m4errx(1, "regular expression error in %s: %s.", source, errbuf);
} }
/* warnx() plus check to see if we need to change exit code or exit .
* -E flag functionality.
*/
void
m4_warnx(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
warnx(fmt, ap);
va_end(ap);
if (fatal_warns)
exit(1);
if (error_warns)
exit_code = 1;
}
static void static void
add_sub(int n, const char *string, regex_t *re, regmatch_t *pm) add_sub(int n, const char *string, regex_t *re, regmatch_t *pm)
{ {
if (n > (int)re->re_nsub) if (n > (int)re->re_nsub)
warnx("No subexpression %d", n); m4_warnx("No subexpression %d", n);
/* Subexpressions that did not match are /* Subexpressions that did not match are
* not an error. */ * not an error. */
else if (pm[n].rm_so != -1 && else if (pm[n].rm_so != -1 &&
@ -446,7 +465,7 @@ void
dopatsubst(const char *argv[], int argc) dopatsubst(const char *argv[], int argc)
{ {
if (argc <= 3) { if (argc <= 3) {
warnx("Too few arguments to patsubst"); m4_warnx("Too few arguments to patsubst");
return; return;
} }
/* special case: empty regexp */ /* special case: empty regexp */
@ -498,7 +517,7 @@ doregexp(const char *argv[], int argc)
const char *source; const char *source;
if (argc <= 3) { if (argc <= 3) {
warnx("Too few arguments to regexp"); m4_warnx("Too few arguments to regexp");
return; return;
} }
/* special gnu case */ /* special gnu case */

View File

@ -1,5 +1,5 @@
.\" $NetBSD: m4.1,v 1.23 2012/04/08 22:00:39 wiz Exp $ .\" $NetBSD: m4.1,v 1.23 2012/04/08 22:00:39 wiz Exp $
.\" @(#) $OpenBSD: m4.1,v 1.63 2015/09/14 20:06:58 schwarze Exp $ .\" @(#) $OpenBSD: m4.1,v 1.64 2017/06/15 13:48:42 bcallah Exp $
.\" .\"
.\" Copyright (c) 1989, 1993 .\" Copyright (c) 1989, 1993
.\" The Regents of the University of California. All rights reserved. .\" The Regents of the University of California. All rights reserved.
@ -33,7 +33,7 @@
.\" .\"
.\" $FreeBSD$ .\" $FreeBSD$
.\" .\"
.Dd $Mdocdate: September 14 2015 $ .Dd $Mdocdate: June 15 2017 $
.Dt M4 1 .Dt M4 1
.Os .Os
.Sh NAME .Sh NAME
@ -41,7 +41,7 @@
.Nd macro language processor .Nd macro language processor
.Sh SYNOPSIS .Sh SYNOPSIS
.Nm .Nm
.Op Fl gPs .Op Fl EgPs
.Oo .Oo
.Sm off .Sm off
.Fl D Ar name Op No = Ar value .Fl D Ar name Op No = Ar value
@ -130,6 +130,21 @@ turn on all options.
.Pp .Pp
By default, trace is set to By default, trace is set to
.Qq eq . .Qq eq .
.It Fl E
Set warnings to be fatal.
When a single
.Fl E
flag is specified, if warnings are issued, execution
continues but
.Nm
will exit with a non-zero exit status.
When multiple
.Fl E
flags are specified, execution will halt upon issuing the
first warning and
.Nm
will exit with a non-zero exit status.
This behaviour matches GNU-m4 1.4.9 and later.
.It Fl g .It Fl g
Activate GNU-m4 compatibility mode. Activate GNU-m4 compatibility mode.
In this mode, translit handles simple character In this mode, translit handles simple character
@ -437,7 +452,9 @@ Returns the current file's name.
.Pp .Pp
But note that the But note that the
.Ic m4exit .Ic m4exit
macro can modify the exit status. macro can modify the exit status, as can the
.Fl E
flag.
.Sh STANDARDS .Sh STANDARDS
The The
.Nm .Nm
@ -446,7 +463,7 @@ utility is compliant with the
specification. specification.
.Pp .Pp
The flags The flags
.Op Fl dgIPot .Op Fl dEgIPot
and the macros and the macros
.Ic builtin , .Ic builtin ,
.Ic esyscmd , .Ic esyscmd ,

View File

@ -1,4 +1,4 @@
/* $OpenBSD: main.c,v 1.86 2015/11/03 16:21:47 deraadt Exp $ */ /* $OpenBSD: main.c,v 1.87 2017/06/15 13:48:42 bcallah Exp $ */
/* $NetBSD: main.c,v 1.12 1997/02/08 23:54:49 cgd Exp $ */ /* $NetBSD: main.c,v 1.12 1997/02/08 23:54:49 cgd Exp $ */
/*- /*-
@ -81,6 +81,8 @@ char scommt[MAXCCHARS+1] = {SCOMMT}; /* start character for comment */
char ecommt[MAXCCHARS+1] = {ECOMMT}; /* end character for comment */ char ecommt[MAXCCHARS+1] = {ECOMMT}; /* end character for comment */
int synch_lines = 0; /* line synchronisation for C preprocessor */ int synch_lines = 0; /* line synchronisation for C preprocessor */
int prefix_builtins = 0; /* -P option to prefix builtin keywords */ int prefix_builtins = 0; /* -P option to prefix builtin keywords */
int error_warns = 0; /* -E option to make warnings exit_code = 1 */
int fatal_warns = 0; /* -E -E option to make warnings fatal */
struct keyblk { struct keyblk {
const char *knam; /* keyword name */ const char *knam; /* keyword name */
@ -186,7 +188,7 @@ main(int argc, char *argv[])
outfile = NULL; outfile = NULL;
resizedivs(MAXOUT); resizedivs(MAXOUT);
while ((c = getopt(argc, argv, "gst:d:D:U:o:I:P")) != -1) while ((c = getopt(argc, argv, "gst:d:D:EU:o:I:P")) != -1)
switch(c) { switch(c) {
case 'D': /* define something..*/ case 'D': /* define something..*/
@ -197,6 +199,12 @@ main(int argc, char *argv[])
*p++ = EOS; *p++ = EOS;
dodefine(optarg, p); dodefine(optarg, p);
break; break;
case 'E': /* like GNU m4 1.4.9+ */
if (error_warns == 0)
error_warns = 1;
else
fatal_warns = 1;
break;
case 'I': case 'I':
addtoincludepath(optarg); addtoincludepath(optarg);
break; break;

View File

@ -1,4 +1,4 @@
/* $OpenBSD: misc.c,v 1.46 2015/12/07 14:12:46 espie Exp $ */ /* $OpenBSD: misc.c,v 1.47 2017/06/15 13:48:42 bcallah Exp $ */
/* $NetBSD: misc.c,v 1.6 1995/09/28 05:37:41 tls Exp $ */ /* $NetBSD: misc.c,v 1.6 1995/09/28 05:37:41 tls Exp $ */
/*- /*-
@ -386,7 +386,7 @@ xstrdup(const char *s)
void void
usage(void) usage(void)
{ {
fprintf(stderr, "usage: m4 [-gPs] [-Dname[=value]] [-d flags] " fprintf(stderr, "usage: m4 [-EgPs] [-Dname[=value]] [-d flags] "
"[-I dirname] [-o filename]\n" "[-I dirname] [-o filename]\n"
"\t[-t macro] [-Uname] [file ...]\n"); "\t[-t macro] [-Uname] [file ...]\n");
exit(1); exit(1);

View File

@ -1,6 +1,6 @@
%option nounput noinput %option nounput noinput
%{ %{
/* $OpenBSD: tokenizer.l,v 1.8 2012/04/12 17:00:11 espie Exp $ */ /* $OpenBSD: tokenizer.l,v 1.9 2017/06/15 13:48:42 bcallah Exp $ */
/* /*
* Copyright (c) 2004 Marc Espie <espie@cvs.openbsd.org> * Copyright (c) 2004 Marc Espie <espie@cvs.openbsd.org>
* *
@ -25,6 +25,7 @@
#include <stdint.h> #include <stdint.h>
#include <limits.h> #include <limits.h>
extern void m4_warnx(const char *, ...);
extern int mimic_gnu; extern int mimic_gnu;
extern int32_t yylval; extern int32_t yylval;
@ -72,7 +73,7 @@ number(void)
l = strtol(yytext, NULL, 0); l = strtol(yytext, NULL, 0);
if (((l == LONG_MAX || l == LONG_MIN) && errno == ERANGE) || if (((l == LONG_MAX || l == LONG_MIN) && errno == ERANGE) ||
l > INT32_MAX || l < INT32_MIN) { l > INT32_MAX || l < INT32_MIN) {
fprintf(stderr, "m4: numeric overflow in expr: %s\n", yytext); m4_warnx("numeric overflow in expr: %s", yytext);
} }
return l; return l;
} }
@ -88,7 +89,7 @@ parse_radix(void)
l = 0; l = 0;
base = strtol(yytext+2, &next, 0); base = strtol(yytext+2, &next, 0);
if (base > 36 || next == NULL) { if (base > 36 || next == NULL) {
fprintf(stderr, "m4: error in number %s\n", yytext); m4_warnx("error in number %s", yytext);
} else { } else {
next++; next++;
while (*next != 0) { while (*next != 0) {
@ -101,8 +102,7 @@ parse_radix(void)
d = *next - 'A' + 10; d = *next - 'A' + 10;
} }
if (d >= base) { if (d >= base) {
fprintf(stderr, m4_warnx("error in number %s", yytext);
"m4: error in number %s\n", yytext);
return 0; return 0;
} }
l = base * l + d; l = base * l + d;