git: c82c2fb1ae91 - stable/13 - usr.bin/calendar: do not treat // in text as comment

From: Stefan Eßer <se_at_FreeBSD.org>
Date: Fri, 04 Mar 2022 19:48:12 UTC
The branch stable/13 has been updated by se:

URL: https://cgit.FreeBSD.org/src/commit/?id=c82c2fb1ae91c5cddc1096c2141b694f2bd3cdce

commit c82c2fb1ae91c5cddc1096c2141b694f2bd3cdce
Author:     Stefan Eßer <se@FreeBSD.org>
AuthorDate: 2021-06-07 13:46:24 +0000
Commit:     Stefan Eßer <se@FreeBSD.org>
CommitDate: 2022-03-04 19:41:02 +0000

    usr.bin/calendar: do not treat // in text as comment
    
    The C++-style comment marker "//" has been added with the rewrite of
    the preprocessor features. Since this character sequence occurs in
    ULRS, the reminder of the URL was considered a comment and stripped
    from the calendar line.
    
    Change parsing of "//" to only start a comment at the begin of a line
    or when preceeded by a white-space character.
    
    (cherry picked from commit 35b8fd0b699a20f71d5636069347b243eb336979)
---
 usr.bin/calendar/io.c                      | 22 ++++++++++++++--------
 usr.bin/calendar/tests/calendar.comment    |  3 ++-
 usr.bin/calendar/tests/regress.comment.out |  2 ++
 3 files changed, 18 insertions(+), 9 deletions(-)

diff --git a/usr.bin/calendar/io.c b/usr.bin/calendar/io.c
index 24966399c179..5afcb1a33314 100644
--- a/usr.bin/calendar/io.c
+++ b/usr.bin/calendar/io.c
@@ -405,7 +405,7 @@ cal_parse(FILE *in, FILE *out)
 {
 	char *mylocale = NULL;
 	char *line = NULL;
-	char *buf;
+	char *buf, *bufp;
 	size_t linecap = 0;
 	ssize_t linelen;
 	ssize_t l;
@@ -443,21 +443,27 @@ cal_parse(FILE *in, FILE *out)
 			}
 		}
 		if (!incomment) {
+			bufp = buf;
 			do {
-				c = strstr(buf, "//");
-				cc = strstr(buf, "/*");
+				c = strstr(bufp, "//");
+				cc = strstr(bufp, "/*");
 				if (c != NULL && (cc == NULL || c - cc < 0)) {
-					/* single line comment */
-					*c = '\0';
-					linelen = c - buf;
-					break;
+					bufp = c + 2;
+					/* ignore "//" within string to allow it in an URL */
+					if (c == buf || isspace(c[-1])) {
+						/* single line comment */
+						*c = '\0';
+						linelen = c - buf;
+						break;
+					}
 				} else if (cc != NULL) {
 					c = strstr(cc + 2, "*/");
-					if (c != NULL) {
+					if (c != NULL) { // 'a /* b */ c' -- cc=2, c=7+2
 						/* multi-line comment ending on same line */
 						c += 2;
 						memmove(cc, c, buf + linelen + 1 - c);
 						linelen -= c - cc;
+						bufp = cc;
 					} else {
 						/* multi-line comment */
 						*cc = '\0';
diff --git a/usr.bin/calendar/tests/calendar.comment b/usr.bin/calendar/tests/calendar.comment
index 6af037ec7e9d..837e5af9e89e 100644
--- a/usr.bin/calendar/tests/calendar.comment
+++ b/usr.bin/calendar/tests/calendar.comment
@@ -7,4 +7,5 @@
 1/* comment */ 6	jan 6
 1 7	jan 7 // /* comment */ comment
 1 1/* comment */1	jan /* comment */11 // comment
-
+1 12	http://localhost.local/
+1 13	http://localhost.local/ // URL with additional comment
diff --git a/usr.bin/calendar/tests/regress.comment.out b/usr.bin/calendar/tests/regress.comment.out
index 1ba3d8cc640e..fb58fd29ff05 100644
--- a/usr.bin/calendar/tests/regress.comment.out
+++ b/usr.bin/calendar/tests/regress.comment.out
@@ -6,3 +6,5 @@ Jan  5 	jan 5
 Jan  6 	jan 6
 Jan  7 	jan 7
 Jan 11 	jan 11
+Jan 12 	http://localhost.local/
+Jan 13 	http://localhost.local/