From 25ce8d30abd423289db903d3e113bec0a5a3fb77 Mon Sep 17 00:00:00 2001 From: Tony Finch Date: Fri, 29 Feb 2008 12:57:14 +0000 Subject: [PATCH] Allow #if defined SYM as well as #if defined(SYM). Fix an abort caused by files that have #endif and no newline on the last line (reported by joe@). Also fix a benign uninitialized variable bug. Update and tidy the copyright. --- usr.bin/unifdef/unifdef.c | 49 +++++++++++++++++++++++++-------------- 1 file changed, 31 insertions(+), 18 deletions(-) diff --git a/usr.bin/unifdef/unifdef.c b/usr.bin/unifdef/unifdef.c index dd2f23e08ff3..32bed03561e4 100644 --- a/usr.bin/unifdef/unifdef.c +++ b/usr.bin/unifdef/unifdef.c @@ -1,13 +1,5 @@ /* - * Copyright (c) 2002 - 2005 Tony Finch . All rights reserved. - * - * This code is derived from software contributed to Berkeley by Dave Yost. - * It was rewritten to support ANSI C by Tony Finch. The original version of - * unifdef carried the following copyright notice. None of its code remains - * in this version (though some of the names remain). - * - * Copyright (c) 1985, 1993 - * The Regents of the University of California. All rights reserved. + * Copyright (c) 2002 - 2008 Tony Finch * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions @@ -31,6 +23,14 @@ * SUCH DAMAGE. */ +/* + * This code is derived from software contributed to Berkeley by Dave Yost. + * It was rewritten to support ANSI C by Tony Finch. The original version + * of unifdef carried the 4-clause BSD copyright licence. None of its code + * remains in this version (though some of the names remain) so it now + * carries a more liberal licence. + */ + #include #ifndef lint @@ -42,7 +42,7 @@ static const char copyright[] = #ifdef __IDSTRING __IDSTRING(Berkeley, "@(#)unifdef.c 8.1 (Berkeley) 6/6/93"); __IDSTRING(NetBSD, "$NetBSD: unifdef.c,v 1.8 2000/07/03 02:51:36 matt Exp $"); -__IDSTRING(dotat, "$dotat: things/unifdef.c,v 1.171 2005/03/08 12:38:48 fanf2 Exp $"); +__IDSTRING(dotat, "$dotat: things/unifdef.c,v 1.176 2008/02/29 12:44:25 fanf2 Exp $"); #endif #endif /* not lint */ #ifdef __FBSDID @@ -586,9 +586,18 @@ getline(void) if (incomment) linestate = LS_DIRTY; } - /* skipcomment should have changed the state */ - if (linestate == LS_HASH) - abort(); /* bug */ + /* skipcomment normally changes the state, except + if the last line of the file lacks a newline */ + if (linestate == LS_HASH) { + size_t len = cp - tline; + if (fgets(tline + len, MAXLINE - len, input) != NULL) + abort(); /* bug */ + /* append the missing newline */ + tline[len+0] = '\n'; + tline[len+1] = '\0'; + cp++; + linestate = LS_START; + } } if (linestate == LS_DIRTY) { while (*cp != '\0') @@ -664,6 +673,7 @@ eval_unary(const struct ops *ops, int *valp, const char **cpp) const char *cp; char *ep; int sym; + bool defparen; cp = skipcomment(*cpp); if (*cp == '!') { @@ -687,16 +697,19 @@ eval_unary(const struct ops *ops, int *valp, const char **cpp) } else if (strncmp(cp, "defined", 7) == 0 && endsym(cp[7])) { cp = skipcomment(cp+7); debug("eval%d defined", ops - eval_ops); - if (*cp++ != '(') - return (LT_IF); - cp = skipcomment(cp); + if (*cp == '(') { + cp = skipcomment(cp+1); + defparen = true; + } else { + defparen = false; + } sym = findsym(cp); if (sym < 0) return (LT_IF); *valp = (value[sym] != NULL); cp = skipsym(cp); cp = skipcomment(cp); - if (*cp++ != ')') + if (defparen && *cp++ != ')') return (LT_IF); keepthis = false; } else if (!endsym(*cp)) { @@ -765,7 +778,7 @@ static Linetype ifeval(const char **cpp) { int ret; - int val; + int val = 0; debug("eval %s", *cpp); keepthis = killconsts ? false : true;