diff --git a/usr.bin/calendar/calendar.1 b/usr.bin/calendar/calendar.1 index eeadc92152b7..8abaf2096ef1 100644 --- a/usr.bin/calendar/calendar.1 +++ b/usr.bin/calendar/calendar.1 @@ -28,7 +28,7 @@ .\" @(#)calendar.1 8.1 (Berkeley) 6/29/93 .\" $FreeBSD$ .\" -.Dd October 28, 2020 +.Dd November 4, 2020 .Dt CALENDAR 1 .Os .Sh NAME @@ -218,12 +218,14 @@ If the shared file is not referenced by a full pathname, searches in the current (or home) directory first, and then in the directory .Pa /usr/share/calendar . -Empty lines and lines protected by the C commenting syntax +Empty lines and text protected by the C commenting syntax .Pq Li /* ... */ +or +.Pq Li // are ignored. .Pp Some possible calendar entries ( characters highlighted by -\fB\et\fR sequence) +\fB\et\fR sequence): .Bd -unfilled -offset indent LANG=C Easter=Ostern diff --git a/usr.bin/calendar/calendar.h b/usr.bin/calendar/calendar.h index 2604568352b5..312ef1c2d4c9 100644 --- a/usr.bin/calendar/calendar.h +++ b/usr.bin/calendar/calendar.h @@ -122,7 +122,7 @@ extern int year1, year2; * - Use event_continue() to add more text to the last added event * - Use event_print_all() to display them in time chronological order */ -struct event *event_add(int, int, int, char *, int, char *, char *); +struct event *event_add(int, int, int, int, char *, char *); void event_continue(struct event *events, char *txt); void event_print_all(FILE *fp); struct event { @@ -189,7 +189,7 @@ int remember_yd(int y, int d, int *rm, int *rd); int first_dayofweek_of_year(int y); int first_dayofweek_of_month(int y, int m); int walkthrough_dates(struct event **e); -void addtodate(struct event *e, int year, int month, int day); +void addtodate(struct event *e); /* pom.c */ #define MAXMOONS 18 diff --git a/usr.bin/calendar/dates.c b/usr.bin/calendar/dates.c index 6ba4e3a4a6f2..740187c48463 100644 --- a/usr.bin/calendar/dates.c +++ b/usr.bin/calendar/dates.c @@ -444,12 +444,12 @@ find_day(int yy, int mm, int dd) } void -addtodate(struct event *e, int year, int month, int day) +addtodate(struct event *e) { struct cal_day *d; struct event *ee; - d = find_day(year, month, day); + d = find_day(e->year, e->month, e->day); ee = d->lastevent; if (ee != NULL) ee->next = e; diff --git a/usr.bin/calendar/events.c b/usr.bin/calendar/events.c index 39ede509e4db..9d4dce97e6e0 100644 --- a/usr.bin/calendar/events.c +++ b/usr.bin/calendar/events.c @@ -39,6 +39,7 @@ __FBSDID("$FreeBSD$"); #include #include #include +#include static iconv_t conv = (iconv_t)-1; static char *currentEncoding = NULL; @@ -150,8 +151,7 @@ convert(char *input) } struct event * -event_add(int year, int month, int day, char *date, int var, char *txt, - char *extra) +event_add(int year, int month, int day, int var, char *txt, char *extra) { struct event *e; @@ -159,25 +159,22 @@ event_add(int year, int month, int day, char *date, int var, char *txt, * Creating a new event: * - Create a new event * - Copy the machine readable day and month - * - Copy the human readable and language specific date * - Copy the text of the event */ e = (struct event *)calloc(1, sizeof(struct event)); if (e == NULL) errx(1, "event_add: cannot allocate memory"); + e->year = year; e->month = month; e->day = day; e->var = var; - e->date = convert(date); - if (e->date == NULL) - errx(1, "event_add: cannot allocate memory"); e->text = convert(txt); if (e->text == NULL) errx(1, "event_add: cannot allocate memory"); e->extra = NULL; if (extra != NULL && extra[0] != '\0') e->extra = convert(extra); - addtodate(e, year, month, day); + addtodate(e); return (e); } @@ -204,20 +201,37 @@ void event_print_all(FILE *fp) { struct event *e; + struct tm tm; + char dbuf[80]; + static int d_first; + const char *lang; + + lang = getenv("LANG"); + if (lang == NULL) + lang = "C"; + if (setlocale(LC_ALL, lang) == NULL) + (void)setlocale(LC_ALL, "C"); + d_first = (*nl_langinfo(D_MD_ORDER) == 'd'); while (walkthrough_dates(&e) != 0) { + if (e) { #ifdef DEBUG - if (e) fprintf(stderr, "event_print_all month: %d, day: %d\n", e->month, e->day); #endif + memset(&tm, 0, sizeof(struct tm)); + tm.tm_mday = e->day; + tm.tm_mon = e->month - 1; + tm.tm_year = e->year - 1900; + (void)strftime(dbuf, sizeof(dbuf), d_first ? "%e %b" : "%b %e", &tm); + } /* * Go through all events and print the text of the matching * dates */ while (e != NULL) { - (void)fprintf(fp, "%s%c%s%s%s%s\n", e->date, + (void)fprintf(fp, "%s%c%s%s%s%s\n", dbuf, e->var ? '*' : ' ', e->text, e->extra != NULL ? " (" : "", e->extra != NULL ? e->extra : "", diff --git a/usr.bin/calendar/io.c b/usr.bin/calendar/io.c index 27bd55db3619..e6a69581a342 100644 --- a/usr.bin/calendar/io.c +++ b/usr.bin/calendar/io.c @@ -50,7 +50,6 @@ __FBSDID("$FreeBSD$"); #include #include #include -#include #include #include #include @@ -159,7 +158,9 @@ cal_path(void) { static char buffer[MAXPATHLEN + 10]; - if (cal_dir[0] == '/') + if (cal_dir == NULL) + snprintf(buffer, sizeof(buffer), "%s", cal_file); + else if (cal_dir[0] == '/') snprintf(buffer, sizeof(buffer), "%s/%s", cal_dir, cal_file); else snprintf(buffer, sizeof(buffer), "%s/%s/%s", cal_home, cal_dir, cal_file); @@ -181,14 +182,14 @@ token(char *line, FILE *out, int *skip, int *unskip) int this_cal_line; if (strncmp(line, "endif", 5) == 0) { - if (*skip > 0) - --*skip; - else if (*unskip > 0) - --*unskip; - else { + if (*skip + *unskip == 0) { WARN0("#endif without prior #ifdef or #ifndef"); return (T_ERR); } + if (*skip > 0) + --*skip; + else + --*unskip; return (T_OK); } @@ -247,18 +248,17 @@ token(char *line, FILE *out, int *skip, int *unskip) WARN0("Expecting no arguments after #else"); return (T_ERR); } + if (*skip + *unskip == 0) { + WARN0("#else without prior #ifdef or #ifndef"); + return (T_ERR); + } - 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; - *unskip = 0; + if (*skip == 0) { + ++*skip; + --*unskip; + } else if (*skip == 1) { + --*skip; + ++*unskip; } return (T_OK); @@ -366,7 +366,6 @@ cal_parse(FILE *in, FILE *out) size_t linecap = 0; ssize_t linelen; ssize_t l; - static int d_first = -1; static int count = 0; int i; int month[MAXCOUNT]; @@ -374,19 +373,11 @@ cal_parse(FILE *in, FILE *out) int year[MAXCOUNT]; int skip = 0; int unskip = 0; - char dbuf[80]; char *pp, p; - struct tm tm; int flags; char *c, *cc; bool incomment = false; - /* Unused */ - tm.tm_sec = 0; - tm.tm_min = 0; - tm.tm_hour = 0; - tm.tm_wday = 0; - if (in == NULL) return (1); @@ -468,7 +459,7 @@ cal_parse(FILE *in, FILE *out) * and does not run iconv(), this variable has little use. */ if (strncmp(buf, "LANG=", 5) == 0) { - (void)setlocale(LC_CTYPE, buf + 5); + (void)setlocale(LC_ALL, buf + 5); #ifdef WITH_ICONV if (!doall) set_new_encoding(); @@ -532,18 +523,10 @@ cal_parse(FILE *in, FILE *out) while (pp[1] == '\t') pp++; - if (d_first < 0) - d_first = (*nl_langinfo(D_MD_ORDER) == 'd'); - for (i = 0; i < count; i++) { - tm.tm_mon = month[i] - 1; - tm.tm_mday = day[i]; - tm.tm_year = year[i] - 1900; - (void)strftime(dbuf, sizeof(dbuf), - d_first ? "%e %b" : "%b %e", &tm); if (debug) WARN1("got \"%s\"", pp); - events[i] = event_add(year[i], month[i], day[i], dbuf, + events[i] = event_add(year[i], month[i], day[i], ((flags &= F_VARIABLE) != 0) ? 1 : 0, pp, extradata[i]); } @@ -592,6 +575,7 @@ opencalin(void) FILE *fpin; /* open up calendar file */ + cal_file = calendarFile; if ((fpin = fopen(calendarFile, "r")) == NULL) { if (doall) { if (chdir(calendarHomes[0]) != 0) diff --git a/usr.bin/calendar/tests/Makefile b/usr.bin/calendar/tests/Makefile index db0c963db1bb..69c2d87a4f6f 100644 --- a/usr.bin/calendar/tests/Makefile +++ b/usr.bin/calendar/tests/Makefile @@ -2,10 +2,18 @@ PACKAGE= tests -TAP_TESTS_SH= legacy_test +TAP_TESTS_SH= comment_test cond_test legacy_test +TEST_METADATA.comment_test+= timeout="600" +TEST_METADATA.cond_test+= timeout="600" TEST_METADATA.legacy_test+= timeout="600" +${PACKAGE}FILES+= calendar.comment +${PACKAGE}FILES+= regress.comment.out + +${PACKAGE}FILES+= calendar.cond +${PACKAGE}FILES+= regress.cond.out + ${PACKAGE}FILES+= calendar.calibrate ${PACKAGE}FILES+= regress.a1.out ${PACKAGE}FILES+= regress.a2.out diff --git a/usr.bin/calendar/tests/calendar.comment b/usr.bin/calendar/tests/calendar.comment new file mode 100644 index 000000000000..6af037ec7e9d --- /dev/null +++ b/usr.bin/calendar/tests/calendar.comment @@ -0,0 +1,10 @@ +1 1 jan 1 // comment +1 2 jan 2 /* comment */ +1 3 jan /* comment */3 +1 4 /* comment*/jan 4 +// comment +/* comment */1 5 jan 5 +1/* comment */ 6 jan 6 +1 7 jan 7 // /* comment */ comment +1 1/* comment */1 jan /* comment */11 // comment + diff --git a/usr.bin/calendar/tests/calendar.cond b/usr.bin/calendar/tests/calendar.cond new file mode 100644 index 000000000000..98806e50be67 --- /dev/null +++ b/usr.bin/calendar/tests/calendar.cond @@ -0,0 +1,83 @@ +#define DEF1 + +1 1 jan 1 OK + +#ifdef DEF1 +1 2 jan 2 OK +#endif + +1 3 jan 3 OK + +#ifdef DEF2 +1 4 jan 4 NOT OK +#else +1 5 jan 5 OK +#endif + +#ifndef DEF2 +1 6 jan 6 OK +#else +1 7 jan 7 NOT OK +#endif + +#ifdef DEF1 +#ifndef DEF2 +1 8 jan 8 OK +#endif +#endif + +#ifdef DEF1 +#ifdef DEF2 +1 9 jan 9 NOT OK +#else +1 10 jan 10 OK +#endif +#else +1 11 jan 11 NOT OK +#endif + +#define DEF2 + +#ifndef DEF1 // skip = 1 +#ifndef DEF2 // skip = 2 +1 12 jan 12 NOT OK +#else // skip = 1 unskip = 0 +1 13 jan 13 NOT OK +#endif // skip = 0 unskip = 0 +#else // skip = 0 unskip = 1 +1 14 jan 14 OK +#endif // skip = 0 unskip = 0 + +#undef DEF1 + +#ifdef DEF1 // OFF +#ifdef DEF2 // ON +#ifdef DEF3 // OFF +1 15 jan 15 NOT OK +#else +1 16 jan 16 NOT OK +#endif // DEF3 +#else // DEF2 +#ifdef DEF3 +1 17 jan 17 NOT OK +#else +1 18 jan 18 NOT OK +#endif // DEF3 +#endif // DEF2 +#else // DEF1 +#ifdef DEF2 +#ifdef DEF3 +1 19 jan 19 NOT OK +#else +1 20 jan 20 OK +#endif // DEF3 +#else // DEF2 +#ifdef DEF3 +1 21 jan 21 NOT OK +#else +1 22 jan 22 NOT OK +#endif // DEF3 +#endif // DEF2 +#endif // DEF1 + +1 23 jan 23 OK \ No newline at end of file diff --git a/usr.bin/calendar/tests/comment.sh b/usr.bin/calendar/tests/comment.sh new file mode 100755 index 000000000000..adde5d755252 --- /dev/null +++ b/usr.bin/calendar/tests/comment.sh @@ -0,0 +1,14 @@ +# $FreeBSD$ + +CALENDAR_FILE="-f ${SRCDIR}/calendar.comment" +CALENDAR_BIN="calendar" + +CALENDAR="${CALENDAR_BIN} ${CALENDAR_FILE}" + +REGRESSION_START($1) + +echo 1..1 + +REGRESSION_TEST(`comment',`$CALENDAR -t 01.01.2020 -A 30') + +REGRESSION_END() diff --git a/usr.bin/calendar/tests/comment_test.sh b/usr.bin/calendar/tests/comment_test.sh new file mode 100755 index 000000000000..c44fe63c9aed --- /dev/null +++ b/usr.bin/calendar/tests/comment_test.sh @@ -0,0 +1,6 @@ +#!/bin/sh +# $FreeBSD$ + +SRCDIR="$(dirname "${0}")"; export SRCDIR + +m4 "${SRCDIR}/../regress.m4" "${SRCDIR}/comment.sh" | sh diff --git a/usr.bin/calendar/tests/cond.sh b/usr.bin/calendar/tests/cond.sh new file mode 100755 index 000000000000..420aaf1d7c5c --- /dev/null +++ b/usr.bin/calendar/tests/cond.sh @@ -0,0 +1,14 @@ +# $FreeBSD$ + +CALENDAR_FILE="-f ${SRCDIR}/calendar.cond" +CALENDAR_BIN="calendar" + +CALENDAR="${CALENDAR_BIN} ${CALENDAR_FILE}" + +REGRESSION_START($1) + +echo 1..1 + +REGRESSION_TEST(`cond',`$CALENDAR -t 01.01.2020 -A 30') + +REGRESSION_END() diff --git a/usr.bin/calendar/tests/cond_test.sh b/usr.bin/calendar/tests/cond_test.sh new file mode 100755 index 000000000000..f00509f3a27c --- /dev/null +++ b/usr.bin/calendar/tests/cond_test.sh @@ -0,0 +1,6 @@ +#!/bin/sh +# $FreeBSD$ + +SRCDIR="$(dirname "${0}")"; export SRCDIR + +m4 "${SRCDIR}/../regress.m4" "${SRCDIR}/cond.sh" | sh diff --git a/usr.bin/calendar/tests/regress.comment.out b/usr.bin/calendar/tests/regress.comment.out new file mode 100644 index 000000000000..1ba3d8cc640e --- /dev/null +++ b/usr.bin/calendar/tests/regress.comment.out @@ -0,0 +1,8 @@ +Jan 1 jan 1 +Jan 2 jan 2 +Jan 3 jan 3 +Jan 4 jan 4 +Jan 5 jan 5 +Jan 6 jan 6 +Jan 7 jan 7 +Jan 11 jan 11 diff --git a/usr.bin/calendar/tests/regress.cond.out b/usr.bin/calendar/tests/regress.cond.out new file mode 100644 index 000000000000..5f5c24829f0c --- /dev/null +++ b/usr.bin/calendar/tests/regress.cond.out @@ -0,0 +1,10 @@ +Jan 1 jan 1 OK +Jan 2 jan 2 OK +Jan 3 jan 3 OK +Jan 5 jan 5 OK +Jan 6 jan 6 OK +Jan 8 jan 8 OK +Jan 10 jan 10 OK +Jan 14 jan 14 OK +Jan 20 jan 20 OK +Jan 23 jan 23 OK