From owner-svn-src-head@freebsd.org Sat Oct 31 15:11:24 2020 Return-Path: Delivered-To: svn-src-head@mailman.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.nyi.freebsd.org (Postfix) with ESMTP id B667A44E62B; Sat, 31 Oct 2020 15:11:24 +0000 (UTC) (envelope-from se@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "Let's Encrypt Authority X3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4CNjLh4RHSz4YSr; Sat, 31 Oct 2020 15:11:24 +0000 (UTC) (envelope-from se@FreeBSD.org) Received: from repo.freebsd.org (repo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:0]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 7BB5A962C; Sat, 31 Oct 2020 15:11:24 +0000 (UTC) (envelope-from se@FreeBSD.org) Received: from repo.freebsd.org ([127.0.1.37]) by repo.freebsd.org (8.15.2/8.15.2) with ESMTP id 09VFBOsa087527; Sat, 31 Oct 2020 15:11:24 GMT (envelope-from se@FreeBSD.org) Received: (from se@localhost) by repo.freebsd.org (8.15.2/8.15.2/Submit) id 09VFBOnE087526; Sat, 31 Oct 2020 15:11:24 GMT (envelope-from se@FreeBSD.org) Message-Id: <202010311511.09VFBOnE087526@repo.freebsd.org> X-Authentication-Warning: repo.freebsd.org: se set sender to se@FreeBSD.org using -f From: =?UTF-8?Q?Stefan_E=c3=9fer?= Date: Sat, 31 Oct 2020 15:11:24 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org Subject: svn commit: r367207 - head/usr.bin/calendar X-SVN-Group: head X-SVN-Commit-Author: se X-SVN-Commit-Paths: head/usr.bin/calendar X-SVN-Commit-Revision: 367207 X-SVN-Commit-Repository: base MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: svn-src-head@freebsd.org X-Mailman-Version: 2.1.33 Precedence: list List-Id: SVN commit messages for the src tree for head/-current List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sat, 31 Oct 2020 15:11:24 -0000 Author: se Date: Sat Oct 31 15:11:24 2020 New Revision: 367207 URL: https://svnweb.freebsd.org/changeset/base/367207 Log: Improve calendar file parsing and consistency tests Add line number information to more warning and error messages. Detect #else and #endif without corresponing #ifdef/#ifndef as error. Detect missing #endif at end of file and print warning but continue. Support for #undef has been added to reverse the effect of a prior #define. It is no error if the argument value has not been defined before. These changes may cause error aborts on malformed input files (e.g. with spurious #else or #endif), but no such errors exist in the calendar files in the FreeBSD base system and the calendar-data port and all tests pass. More tests will be added in a follow-up commit to detect regressions that might affect the newly added features. This commit ends a series of updates that enhance the pre-processor and make it behave much more like prior versions of the calendar progarm that called cpp to pre-process the data files. MFC after: 3 days Relnotes: yes Modified: head/usr.bin/calendar/io.c Modified: head/usr.bin/calendar/io.c ============================================================================== --- head/usr.bin/calendar/io.c Sat Oct 31 14:59:39 2020 (r367206) +++ head/usr.bin/calendar/io.c Sat Oct 31 15:11:24 2020 (r367207) @@ -153,7 +153,7 @@ cal_fopen(const char *file) warnx(format " in %s/%s/%s line %d", arg1, cal_home, cal_dir, cal_file, cal_line) static int -token(char *line, FILE *out, int *skip) +token(char *line, FILE *out, int *skip, int *unskip) { char *walk, c, a; const char *this_cal_home; @@ -164,6 +164,13 @@ token(char *line, FILE *out, int *skip) if (strncmp(line, "endif", 5) == 0) { if (*skip > 0) --*skip; + else if (*unskip > 0) + --*unskip; + else { + WARN0("#endif without prior #ifdef or #ifndef"); + return (T_ERR); + } + return (T_OK); } @@ -178,7 +185,9 @@ token(char *line, FILE *out, int *skip) if (*skip != 0 || definitions == NULL || sl_find(definitions, walk) == NULL) ++*skip; - + else + ++*unskip; + return (T_OK); } @@ -193,6 +202,8 @@ token(char *line, FILE *out, int *skip) if (*skip != 0 || (definitions != NULL && sl_find(definitions, walk) != NULL)) ++*skip; + else + ++*unskip; return (T_OK); } @@ -206,10 +217,18 @@ token(char *line, FILE *out, int *skip) return (T_ERR); } - if (*skip == 0) + if (*unskip == 0) { + if (*skip == 0) { + WARN0("#else without prior #ifdef or #ifndef"); + return (T_ERR); + } else if (*skip == 1) { + *skip = 0; + *unskip = 1; + } + } else if (*unskip == 1) { *skip = 1; - else if (*skip == 1) - *skip = 0; + *unskip = 0; + } return (T_OK); } @@ -267,10 +286,28 @@ token(char *line, FILE *out, int *skip) return (T_ERR); } - sl_add(definitions, strdup(walk)); + if (sl_find(definitions, walk) == NULL) + sl_add(definitions, strdup(walk)); return (T_OK); } + if (strncmp(line, "undef", 5) == 0) { + if (definitions != NULL) { + walk = line + 5; + trimlr(&walk); + + if (*walk == '\0') { + WARN0("Expecting arguments after #undef"); + return (T_ERR); + } + + walk = sl_find(definitions, walk); + if (walk != NULL) + walk[0] = '\0'; + } + return (T_OK); + } + return (T_PROCESS); } @@ -299,6 +336,7 @@ cal_parse(FILE *in, FILE *out) int day[MAXCOUNT]; int year[MAXCOUNT]; int skip = 0; + int unskip = 0; char dbuf[80]; char *pp, p; struct tm tm; @@ -369,7 +407,7 @@ cal_parse(FILE *in, FILE *out) continue; if (buf == line && *buf == '#') { - switch (token(buf+1, out, &skip)) { + switch (token(buf+1, out, &skip, &unskip)) { case T_ERR: free(line); return (1); @@ -448,8 +486,7 @@ cal_parse(FILE *in, FILE *out) if (count < 0) { /* Show error status based on return value */ if (debug) - fprintf(stderr, "Ignored: \"%s\" in %s/%s/%s line %d\n", - buf, cal_home, cal_dir, cal_file, cal_line); + WARN1("Ignored: \"%s\"", buf); if (count == -1) continue; count = -count + 1; @@ -469,12 +506,15 @@ cal_parse(FILE *in, FILE *out) (void)strftime(dbuf, sizeof(dbuf), d_first ? "%e %b" : "%b %e", &tm); if (debug) - fprintf(stderr, "got \"%s\" in %s/%s/%s line %d\n", - pp, cal_home, cal_dir, cal_file, cal_line); + WARN1("got \"%s\"", pp); events[i] = event_add(year[i], month[i], day[i], dbuf, ((flags &= F_VARIABLE) != 0) ? 1 : 0, pp, extradata[i]); } + } + while (skip-- > 0 || unskip-- > 0) { + cal_line++; + WARN0("Missing #endif assumed"); } free(line);