CVS commit: src/games/cgram
Module Name:src Committed By: rillig Date: Sun Feb 21 13:35:37 UTC 2021 Modified Files: src/games/cgram: cgram.c Log Message: cgram: fix Home key for cursor navigation Reported by liweitianux via GitHub. To generate a diff of this commit: cvs rdiff -u -r1.2 -r1.3 src/games/cgram/cgram.c Please note that diffs are not public domain; they are subject to the copyright notices on the relevant files. Modified files: Index: src/games/cgram/cgram.c diff -u src/games/cgram/cgram.c:1.2 src/games/cgram/cgram.c:1.3 --- src/games/cgram/cgram.c:1.2 Thu Jul 2 19:11:01 2020 +++ src/games/cgram/cgram.c Sun Feb 21 13:35:37 2021 @@ -241,7 +241,7 @@ static void loop(void) { int ch = getch(); switch (ch) { case 1: /* ^A */ - case KEY_BEG: + case KEY_HOME: curx=0; break; case 2: /* ^B */
CVS commit: src/games/cgram
Module Name:src Committed By: rillig Date: Sun Feb 21 15:41:04 UTC 2021 Modified Files: src/games/cgram: cgram.c Log Message: cgram: indent With manual corrections afterwards: - indent removes empty lines between statement and declaration. This had been a syntax error in C90, since C99 this is common style and should be accepted by indent. - indent didn't format the first line of main for whatever reason, did that manually. - indent removed the empty line above the '/' separators. It should have left these empty lines as-is. To generate a diff of this commit: cvs rdiff -u -r1.3 -r1.4 src/games/cgram/cgram.c Please note that diffs are not public domain; they are subject to the copyright notices on the relevant files. Modified files: Index: src/games/cgram/cgram.c diff -u src/games/cgram/cgram.c:1.3 src/games/cgram/cgram.c:1.4 --- src/games/cgram/cgram.c:1.3 Sun Feb 21 13:35:37 2021 +++ src/games/cgram/cgram.c Sun Feb 21 15:41:04 2021 @@ -1,3 +1,5 @@ +/* $NetBSD */ + /*- * Copyright (c) 2013 The NetBSD Foundation, Inc. * All rights reserved. @@ -39,40 +41,48 @@ -static char *xstrdup(const char *s) { - char *ret; - - ret = malloc(strlen(s) + 1); - if (ret == NULL) { - errx(1, "Out of memory"); - } - strcpy(ret, s); - return ret; +static char * +xstrdup(const char *s) +{ + char *ret; + + ret = malloc(strlen(s) + 1); + if (ret == NULL) { + errx(1, "Out of memory"); + } + strcpy(ret, s); + return ret; } struct stringarray { - char **v; - int num; + char **v; + int num; }; -static void stringarray_init(struct stringarray *a) { - a->v = NULL; - a->num = 0; -} - -static void stringarray_cleanup(struct stringarray *a) { - free(a->v); -} - -static void stringarray_add(struct stringarray *a, const char *s) { - a->v = realloc(a->v, (a->num + 1) * sizeof(a->v[0])); - if (a->v == NULL) { - errx(1, "Out of memory"); - } - a->v[a->num] = xstrdup(s); - a->num++; +static void +stringarray_init(struct stringarray *a) +{ + a->v = NULL; + a->num = 0; +} + +static void +stringarray_cleanup(struct stringarray *a) +{ + free(a->v); +} + +static void +stringarray_add(struct stringarray *a, const char *s) +{ + a->v = realloc(a->v, (a->num + 1) * sizeof(a->v[0])); + if (a->v == NULL) { + errx(1, "Out of memory"); + } + a->v[a->num] = xstrdup(s); + a->num++; } @@ -84,265 +94,279 @@ static int scrolldown; static unsigned curx; static int cury; -static void readquote(void) { - FILE *f = popen(_PATH_FORTUNE, "r"); - if (!f) { - err(1, "%s", _PATH_FORTUNE); - } - - char buf[128], buf2[8*sizeof(buf)]; - while (fgets(buf, sizeof(buf), f)) { - char *s = strrchr(buf, '\n'); - assert(s); - assert(strlen(s)==1); - *s = 0; - - int i,j; - for (i=j=0; buf[i]; i++) { - if (buf[i]=='\t') { - buf2[j++] = ' '; - while (j%8) buf2[j++] = ' '; - } - else if (buf[i]=='\b') { - if (j>0) j--; - } - else { - buf2[j++] = buf[i]; - } - } - buf2[j] = 0; - - stringarray_add(&lines, buf2); - stringarray_add(&sollines, buf2); - } - - pclose(f); -} - -static void encode(void) { - int key[26]; - for (int i=0; i<26; i++) key[i] = i; - for (int i=26; i>1; i--) { - int c = random() % i; - int t = key[i-1]; - key[i-1] = key[c]; - key[c] = t; - } - - for (int y=0; y=0 && cury= strlen(lines.v[cury])) { - beep(); - return -1; - } - - int och = lines.v[cury][curx]; - if (!isalpha((unsigned char)och)) { - beep(); - return -1; - } - - int loch = tolower((unsigned char)och); - int uoch = toupper((unsigned char)och); - int lch = tolower((unsigned char)ch); - int uch = toupper((unsigned char)ch); - - for (int y=0; y 0) + j--; + } else { +buf2[j++] = buf[i]; + } + } + buf2[j] = 0; + + stringarray_add(&lines, buf2); + stringarray_add(&sollines, buf2); + } + + pclose(f); +} + +static void +encode(void) +{ + int key[26]; + for (int i = 0; i < 26; i++) + key[i] = i; + for (int i = 26; i > 1; i--) { + int c = random() % i; + int t = key[i - 1]; + key[i - 1] = key[c]; + key[c] = t; + } + + for (int y = 0; y < lines.num; y++) { + for (unsigned x = 0; lines.v[y][x]; x++) { + if (islower((unsigned char)lines.v[y][x])) { +int q = lines.v[y][x] - 'a'; +lines.v[y][x] = 'a' + key[q]; + } + if (isupper((unsigned char)lines.v[y][x])) { +int q = lines.v[y][x] - 'A'; +lines.v[y][x] = 'A' + key[q]; + } + } + } +} + +static int +substitute(int ch) +{ + assert(cury >= 0 && cury < lines.num); + if (curx >= strlen(lines.v[cury])) { + beep(); + return -1; + } + + int och = lines.v[cury][curx]; + if (!isalpha((unsigned char)och)) { + beep(); + return -1; + } + + int loch = tolower((unsigned char)och)
CVS commit: src/games/cgram
Module Name:src Committed By: rillig Date: Sun Feb 21 15:42:21 UTC 2021 Modified Files: src/games/cgram: cgram.c Log Message: cgram: sort includes To generate a diff of this commit: cvs rdiff -u -r1.4 -r1.5 src/games/cgram/cgram.c Please note that diffs are not public domain; they are subject to the copyright notices on the relevant files. Modified files: Index: src/games/cgram/cgram.c diff -u src/games/cgram/cgram.c:1.4 src/games/cgram/cgram.c:1.5 --- src/games/cgram/cgram.c:1.4 Sun Feb 21 15:41:04 2021 +++ src/games/cgram/cgram.c Sun Feb 21 15:42:21 2021 @@ -29,14 +29,15 @@ * POSSIBILITY OF SUCH DAMAGE. */ +#include +#include +#include +#include #include -#include #include -#include +#include #include -#include -#include -#include + #include "pathnames.h"
CVS commit: src/games/cgram
Module Name:src Committed By: rillig Date: Sun Feb 21 16:05:44 UTC 2021 Modified Files: src/games/cgram: Makefile cgram.c Log Message: cgram: adjust style to survive lint's strict bool mode The biggest change is the return type of substitute(). Before, 0 meant success, now false means failure. No functional change. To generate a diff of this commit: cvs rdiff -u -r1.1 -r1.2 src/games/cgram/Makefile cvs rdiff -u -r1.5 -r1.6 src/games/cgram/cgram.c Please note that diffs are not public domain; they are subject to the copyright notices on the relevant files. Modified files: Index: src/games/cgram/Makefile diff -u src/games/cgram/Makefile:1.1 src/games/cgram/Makefile:1.2 --- src/games/cgram/Makefile:1.1 Sun Aug 4 05:42:47 2013 +++ src/games/cgram/Makefile Sun Feb 21 16:05:44 2021 @@ -1,10 +1,11 @@ -# $NetBSD: Makefile,v 1.1 2013/08/04 05:42:47 dholland Exp $ +# $NetBSD: Makefile,v 1.2 2021/02/21 16:05:44 rillig Exp $ -PROG=cgram -DPADD=${LIBCURSES} ${LIBTERMINFO} -LDADD=-lcurses -lterminfo -SRCS=cgram.c -MAN=cgram.6 -HIDEGAME=hidegame +PROG= cgram +DPADD= ${LIBCURSES} ${LIBTERMINFO} +LDADD= -lcurses -lterminfo +SRCS= cgram.c +MAN= cgram.6 +HIDEGAME= hidegame +LINTFLAGS+= -T .include Index: src/games/cgram/cgram.c diff -u src/games/cgram/cgram.c:1.5 src/games/cgram/cgram.c:1.6 --- src/games/cgram/cgram.c:1.5 Sun Feb 21 15:42:21 2021 +++ src/games/cgram/cgram.c Sun Feb 21 16:05:44 2021 @@ -33,6 +33,7 @@ #include #include #include +#include #include #include #include @@ -99,22 +100,22 @@ static void readquote(void) { FILE *f = popen(_PATH_FORTUNE, "r"); - if (!f) { + if (f == NULL) { err(1, "%s", _PATH_FORTUNE); } char buf[128], buf2[8 * sizeof(buf)]; - while (fgets(buf, sizeof(buf), f)) { + while (fgets(buf, sizeof buf, f) != NULL) { char *s = strrchr(buf, '\n'); - assert(s); + assert(s != NULL); assert(strlen(s) == 1); - *s = 0; + *s = '\0'; int i, j; - for (i = j = 0; buf[i]; i++) { + for (i = j = 0; buf[i] != '\0'; i++) { if (buf[i] == '\t') { buf2[j++] = ' '; -while (j % 8) +while (j % 8 != 0) buf2[j++] = ' '; } else if (buf[i] == '\b') { if (j > 0) @@ -123,7 +124,7 @@ readquote(void) buf2[j++] = buf[i]; } } - buf2[j] = 0; + buf2[j] = '\0'; stringarray_add(&lines, buf2); stringarray_add(&sollines, buf2); @@ -146,7 +147,7 @@ encode(void) } for (int y = 0; y < lines.num; y++) { - for (unsigned x = 0; lines.v[y][x]; x++) { + for (unsigned x = 0; lines.v[y][x] != '\0'; x++) { if (islower((unsigned char)lines.v[y][x])) { int q = lines.v[y][x] - 'a'; lines.v[y][x] = 'a' + key[q]; @@ -159,19 +160,19 @@ encode(void) } } -static int +static bool substitute(int ch) { assert(cury >= 0 && cury < lines.num); if (curx >= strlen(lines.v[cury])) { beep(); - return -1; + return false; } int och = lines.v[cury][curx]; if (!isalpha((unsigned char)och)) { beep(); - return -1; + return false; } int loch = tolower((unsigned char)och); @@ -180,7 +181,7 @@ substitute(int ch) int uch = toupper((unsigned char)ch); for (int y = 0; y < lines.num; y++) { - for (unsigned x = 0; lines.v[y][x]; x++) { + for (unsigned x = 0; lines.v[y][x] != '\0'; x++) { if (lines.v[y][x] == loch) { lines.v[y][x] = lch; } else if (lines.v[y][x] == uoch) { @@ -192,7 +193,7 @@ substitute(int ch) } } } - return 0; + return true; } @@ -206,7 +207,7 @@ redraw(void) move(i, 0); int ln = i + scrolldown; if (ln < lines.num) { - for (unsigned j = 0; lines.v[i][j]; j++) { + for (unsigned j = 0; lines.v[i][j] != '\0'; j++) { int ch = lines.v[i][j]; if (ch != sollines.v[i][j] && isalpha((unsigned char)ch)) { @@ -325,7 +326,7 @@ loop(void) break; default: if (isalpha(ch)) { -if (!substitute(ch)) { +if (substitute(ch)) { if (curx < strlen(lines.v[cury])) { curx++; } @@ -364,7 +365,7 @@ main(void) encode(); opencurses(); - keypad(stdscr, TRUE); + keypad(stdscr, true); loop(); closecurses();
CVS commit: src/games/cgram
Module Name:src Committed By: rillig Date: Sun Feb 21 16:37:26 UTC 2021 Modified Files: src/games/cgram: cgram.c Log Message: cgram: fix undefined behavior when pressing function keys The key codes for function keys are outside of the "range representable by an unsigned char". This resulted in parts of the screen being cleaned. To generate a diff of this commit: cvs rdiff -u -r1.6 -r1.7 src/games/cgram/cgram.c Please note that diffs are not public domain; they are subject to the copyright notices on the relevant files. Modified files: Index: src/games/cgram/cgram.c diff -u src/games/cgram/cgram.c:1.6 src/games/cgram/cgram.c:1.7 --- src/games/cgram/cgram.c:1.6 Sun Feb 21 16:05:44 2021 +++ src/games/cgram/cgram.c Sun Feb 21 16:37:26 2021 @@ -325,7 +325,7 @@ loop(void) done = true; break; default: - if (isalpha(ch)) { + if (isascii(ch) && isalpha(ch)) { if (substitute(ch)) { if (curx < strlen(lines.v[cury])) { curx++;
CVS commit: src/games/cgram
Module Name:src Committed By: rillig Date: Sun Feb 21 16:50:58 UTC 2021 Modified Files: src/games/cgram: cgram.c Log Message: cgram: consistently use char for characters Having to convert back and forth between char, unsigned char and int is confusing. Just stay with char, until the support for wide characters is added. No functional change. To generate a diff of this commit: cvs rdiff -u -r1.7 -r1.8 src/games/cgram/cgram.c Please note that diffs are not public domain; they are subject to the copyright notices on the relevant files. Modified files: Index: src/games/cgram/cgram.c diff -u src/games/cgram/cgram.c:1.7 src/games/cgram/cgram.c:1.8 --- src/games/cgram/cgram.c:1.7 Sun Feb 21 16:37:26 2021 +++ src/games/cgram/cgram.c Sun Feb 21 16:50:57 2021 @@ -56,6 +56,36 @@ xstrdup(const char *s) return ret; } +static char +ch_toupper(char ch) +{ + return (char)toupper((unsigned char)ch); +} + +static char +ch_tolower(char ch) +{ + return (char)tolower((unsigned char)ch); +} + +static bool +ch_isalpha(char ch) +{ + return isalpha((unsigned char)ch); +} + +static bool +ch_islower(char ch) +{ + return islower((unsigned char)ch); +} + +static bool +ch_isupper(char ch) +{ + return isupper((unsigned char)ch); +} + struct stringarray { @@ -148,11 +178,11 @@ encode(void) for (int y = 0; y < lines.num; y++) { for (unsigned x = 0; lines.v[y][x] != '\0'; x++) { - if (islower((unsigned char)lines.v[y][x])) { + if (ch_islower(lines.v[y][x])) { int q = lines.v[y][x] - 'a'; lines.v[y][x] = 'a' + key[q]; } - if (isupper((unsigned char)lines.v[y][x])) { + if (ch_isupper(lines.v[y][x])) { int q = lines.v[y][x] - 'A'; lines.v[y][x] = 'A' + key[q]; } @@ -169,16 +199,16 @@ substitute(int ch) return false; } - int och = lines.v[cury][curx]; - if (!isalpha((unsigned char)och)) { + char och = lines.v[cury][curx]; + if (!ch_isalpha(och)) { beep(); return false; } - int loch = tolower((unsigned char)och); - int uoch = toupper((unsigned char)och); - int lch = tolower((unsigned char)ch); - int uch = toupper((unsigned char)ch); + char loch = ch_tolower(och); + char uoch = ch_toupper(och); + char lch = ch_tolower(ch); + char uch = ch_toupper(ch); for (int y = 0; y < lines.num; y++) { for (unsigned x = 0; lines.v[y][x] != '\0'; x++) { @@ -208,14 +238,13 @@ redraw(void) int ln = i + scrolldown; if (ln < lines.num) { for (unsigned j = 0; lines.v[i][j] != '\0'; j++) { -int ch = lines.v[i][j]; -if (ch != sollines.v[i][j] && -isalpha((unsigned char)ch)) { +char ch = lines.v[i][j]; +if (ch != sollines.v[i][j] && ch_isalpha(ch)) { won = false; } bool bold = false; if (hinting && ch == sollines.v[i][j] && -isalpha((unsigned char)ch)) { +ch_isalpha(ch)) { bold = true; attron(A_BOLD); }
CVS commit: src/games/cgram
Module Name:src Committed By: rillig Date: Sun Feb 21 17:16:00 UTC 2021 Modified Files: src/games/cgram: Makefile cgram.c Log Message: cgram: WARNS=6, use int for all coordinates Eliminate some frequently occurring subexpressions during substitution. No functional change, assuming that fortune(6) always spits out less than 2 gigacharacters of text. To generate a diff of this commit: cvs rdiff -u -r1.2 -r1.3 src/games/cgram/Makefile cvs rdiff -u -r1.8 -r1.9 src/games/cgram/cgram.c Please note that diffs are not public domain; they are subject to the copyright notices on the relevant files. Modified files: Index: src/games/cgram/Makefile diff -u src/games/cgram/Makefile:1.2 src/games/cgram/Makefile:1.3 --- src/games/cgram/Makefile:1.2 Sun Feb 21 16:05:44 2021 +++ src/games/cgram/Makefile Sun Feb 21 17:16:00 2021 @@ -1,4 +1,4 @@ -# $NetBSD: Makefile,v 1.2 2021/02/21 16:05:44 rillig Exp $ +# $NetBSD: Makefile,v 1.3 2021/02/21 17:16:00 rillig Exp $ PROG= cgram DPADD= ${LIBCURSES} ${LIBTERMINFO} @@ -7,5 +7,6 @@ SRCS= cgram.c MAN= cgram.6 HIDEGAME= hidegame LINTFLAGS+= -T +WARNS= 6 .include Index: src/games/cgram/cgram.c diff -u src/games/cgram/cgram.c:1.8 src/games/cgram/cgram.c:1.9 --- src/games/cgram/cgram.c:1.8 Sun Feb 21 16:50:57 2021 +++ src/games/cgram/cgram.c Sun Feb 21 17:16:00 2021 @@ -1,4 +1,4 @@ -/* $NetBSD */ +/* $NetBSD: cgram.c,v 1.9 2021/02/21 17:16:00 rillig Exp $ */ /*- * Copyright (c) 2013 The NetBSD Foundation, Inc. @@ -90,7 +90,7 @@ ch_isupper(char ch) struct stringarray { char **v; - int num; + size_t num; }; static void @@ -123,7 +123,7 @@ static struct stringarray lines; static struct stringarray sollines; static bool hinting; static int scrolldown; -static unsigned curx; +static int curx; static int cury; static void @@ -167,34 +167,32 @@ static void encode(void) { int key[26]; + for (int i = 0; i < 26; i++) key[i] = i; + for (int i = 26; i > 1; i--) { - int c = random() % i; + int c = (int)(random() % i); int t = key[i - 1]; key[i - 1] = key[c]; key[c] = t; } - for (int y = 0; y < lines.num; y++) { - for (unsigned x = 0; lines.v[y][x] != '\0'; x++) { - if (ch_islower(lines.v[y][x])) { -int q = lines.v[y][x] - 'a'; -lines.v[y][x] = 'a' + key[q]; - } - if (ch_isupper(lines.v[y][x])) { -int q = lines.v[y][x] - 'A'; -lines.v[y][x] = 'A' + key[q]; - } + for (int y = 0; y < (int)lines.num; y++) { + for (char *p = lines.v[y]; *p != '\0'; p++) { + if (ch_islower(*p)) +*p = (char)('a' + key[*p - 'a']); + if (ch_isupper(*p)) +*p = (char)('A' + key[*p - 'A']); } } } static bool -substitute(int ch) +substitute(char ch) { - assert(cury >= 0 && cury < lines.num); - if (curx >= strlen(lines.v[cury])) { + assert(cury >= 0 && cury < (int)lines.num); + if (curx >= (int)strlen(lines.v[cury])) { beep(); return false; } @@ -210,17 +208,16 @@ substitute(int ch) char lch = ch_tolower(ch); char uch = ch_toupper(ch); - for (int y = 0; y < lines.num; y++) { - for (unsigned x = 0; lines.v[y][x] != '\0'; x++) { - if (lines.v[y][x] == loch) { -lines.v[y][x] = lch; - } else if (lines.v[y][x] == uoch) { -lines.v[y][x] = uch; - } else if (lines.v[y][x] == lch) { -lines.v[y][x] = loch; - } else if (lines.v[y][x] == uch) { -lines.v[y][x] = uoch; - } + for (int y = 0; y < (int)lines.num; y++) { + for (char *p = lines.v[y]; *p != '\0'; p++) { + if (*p == loch) +*p = lch; + else if (*p == uoch) +*p = uch; + else if (*p == lch) +*p = loch; + else if (*p == uch) +*p = uoch; } } return true; @@ -236,7 +233,7 @@ redraw(void) for (int i = 0; i < LINES - 1; i++) { move(i, 0); int ln = i + scrolldown; - if (ln < lines.num) { + if (ln < (int)lines.num) { for (unsigned j = 0; lines.v[i][j] != '\0'; j++) { char ch = lines.v[i][j]; if (ch != sollines.v[i][j] && ch_isalpha(ch)) { @@ -304,18 +301,18 @@ loop(void) curx--; } else if (cury > 0) { cury--; -curx = strlen(lines.v[cury]); +curx = (int)strlen(lines.v[cury]); } break; case 5: /* ^E */ case KEY_END: - curx = strlen(lines.v[cury]); + curx = (int)strlen(lines.v[cury]); break; case 6: /* ^F */ case KEY_RIGHT: - if (curx < strlen(lines.v[cury])) { + if (curx < (int)strlen(lines.v[cury])) { curx++; - } else if (cury < lines.num - 1) { + } else if (cury < (int)lines.num - 1) { cury++; curx = 0; } @@ -325,11 +322,11 @@ loop(void) break; case 14: /* ^N */ case KEY_DOWN: - if (cury < lines.num - 1) { + if (cury < (int)lines.num - 1) { cury++; } - if (curx > strlen(lines.v[cury])) { -curx = strlen(lines.v[cury]); + if (curx > (int)strlen(lines.v[cury])) { +curx = (int)strlen(lines.v[cury]); } if (scrolldown < cury - (LINES - 2)) { scrolldown = cury - (LINES - 2); @@ -340,8 +337,8 @@ loop(void) if (cury > 0) { cury--;
CVS commit: src/games/cgram
Module Name:src Committed By: rillig Date: Sun Feb 21 20:33:43 UTC 2021 Modified Files: src/games/cgram: cgram.c Log Message: cgram: rewrite completely, fixing bugs and style Fixed bugs: Do not consider the puzzle solved if all letters in the visible area are substituted correctly. To be properly solved, the whole puzzle must be solved, even those parts that are currently off-screen. Never place the cursor at the very right edge of the screen since that does not work well with some terminals. The maximum valid x coordinate is COLS - 1. Add horizontal scrolling. Make all coordinate handling symmetric in regard to the horizontal and vertical axes. Previously, lines longer than 80 characters could not be seen on the screen. Improvements: Remove the arbitrary limit of 128 characters per line. Even if fortune(6) may never generate such long lines, the code is easy enough to adapt to other sources. Properly clean up the allocated memory. Previously, only the string arrays were freed but not the strings themselves. Stylistic: Add RCS ID. Fix ctype functions in lint's strict bool mode. Avoid excessive calls to strlen whenever the cursor moves. Given that the whole screen is redrawn every time a key is pressed, this is an unnecessary optimization, but the code smelled nevertheless. To generate a diff of this commit: cvs rdiff -u -r1.9 -r1.10 src/games/cgram/cgram.c Please note that diffs are not public domain; they are subject to the copyright notices on the relevant files. Modified files: Index: src/games/cgram/cgram.c diff -u src/games/cgram/cgram.c:1.9 src/games/cgram/cgram.c:1.10 --- src/games/cgram/cgram.c:1.9 Sun Feb 21 17:16:00 2021 +++ src/games/cgram/cgram.c Sun Feb 21 20:33:42 2021 @@ -1,11 +1,11 @@ -/* $NetBSD: cgram.c,v 1.9 2021/02/21 17:16:00 rillig Exp $ */ +/* $NetBSD: cgram.c,v 1.10 2021/02/21 20:33:42 rillig Exp $ */ /*- - * Copyright (c) 2013 The NetBSD Foundation, Inc. + * Copyright (c) 2013, 2021 The NetBSD Foundation, Inc. * All rights reserved. * * This code is derived from software contributed to The NetBSD Foundation - * by David A. Holland. + * by David A. Holland and Roland Illig. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions @@ -29,6 +29,11 @@ * POSSIBILITY OF SUCH DAMAGE. */ +#include +#if defined(__RCSID) && !defined(lint) +__RCSID("$NetBSD: cgram.c,v 1.10 2021/02/21 20:33:42 rillig Exp $"); +#endif + #include #include #include @@ -43,19 +48,6 @@ -static char * -xstrdup(const char *s) -{ - char *ret; - - ret = malloc(strlen(s) + 1); - if (ret == NULL) { - errx(1, "Out of memory"); - } - strcpy(ret, s); - return ret; -} - static char ch_toupper(char ch) { @@ -71,29 +63,74 @@ ch_tolower(char ch) static bool ch_isalpha(char ch) { - return isalpha((unsigned char)ch); + return isalpha((unsigned char)ch) != 0; } static bool ch_islower(char ch) { - return islower((unsigned char)ch); + return islower((unsigned char)ch) != 0; } static bool ch_isupper(char ch) { - return isupper((unsigned char)ch); + return isupper((unsigned char)ch) != 0; +} + +static int +imax(int a, int b) +{ + return a > b ? a : b; +} + +static int +imin(int a, int b) +{ + return a < b ? a : b; } +struct string { + char *s; + size_t len; + size_t cap; +}; + struct stringarray { - char **v; + struct string *v; size_t num; }; static void +string_init(struct string *s) +{ + s->s = NULL; + s->len = 0; + s->cap = 0; +} + +static void +string_add(struct string *s, char ch) +{ + if (s->len >= s->cap) { + s->cap = 2 * s->cap + 16; + s->s = realloc(s->s, s->cap); + if (s->s == NULL) + errx(1, "Out of memory"); + } + s->s[s->len++] = ch; +} + +static void +string_finish(struct string *s) +{ + string_add(s, '\0'); + s->len--; +} + +static void stringarray_init(struct stringarray *a) { a->v = NULL; @@ -103,18 +140,33 @@ stringarray_init(struct stringarray *a) static void stringarray_cleanup(struct stringarray *a) { + for (size_t i = 0; i < a->num; i++) + free(a->v[i].s); free(a->v); } static void -stringarray_add(struct stringarray *a, const char *s) +stringarray_add(struct stringarray *a, struct string *s) { - a->v = realloc(a->v, (a->num + 1) * sizeof(a->v[0])); - if (a->v == NULL) { + size_t num = a->num++; + a->v = realloc(a->v, a->num * sizeof a->v[0]); + if (a->v == NULL) errx(1, "Out of memory"); + a->v[num] = *s; +} + +static void +stringarray_dup(struct stringarray *dst, const struct stringarray *src) +{ + assert(dst->num == 0); + for (size_t i = 0; i < src->num; i++) { + struct string str; + string_init(&str); + for (const char *p = src->v[i].s; *p != '\0'; p++) + string_add(&str, *p); + string_finish(&str); + stringarray_add(dst, &str); } - a->v[a->num] = xstrdup(s); - a-
CVS commit: src/games/cgram
Module Name:src Committed By: rillig Date: Sun Feb 21 22:21:56 UTC 2021 Modified Files: src/games/cgram: cgram.c Log Message: cgram: allow navigation with KEY_PPAGE and KEY_NPAGE To generate a diff of this commit: cvs rdiff -u -r1.10 -r1.11 src/games/cgram/cgram.c Please note that diffs are not public domain; they are subject to the copyright notices on the relevant files. Modified files: Index: src/games/cgram/cgram.c diff -u src/games/cgram/cgram.c:1.10 src/games/cgram/cgram.c:1.11 --- src/games/cgram/cgram.c:1.10 Sun Feb 21 20:33:42 2021 +++ src/games/cgram/cgram.c Sun Feb 21 22:21:56 2021 @@ -1,4 +1,4 @@ -/* $NetBSD: cgram.c,v 1.10 2021/02/21 20:33:42 rillig Exp $ */ +/* $NetBSD: cgram.c,v 1.11 2021/02/21 22:21:56 rillig Exp $ */ /*- * Copyright (c) 2013, 2021 The NetBSD Foundation, Inc. @@ -31,7 +31,7 @@ #include #if defined(__RCSID) && !defined(lint) -__RCSID("$NetBSD: cgram.c,v 1.10 2021/02/21 20:33:42 rillig Exp $"); +__RCSID("$NetBSD: cgram.c,v 1.11 2021/02/21 22:21:56 rillig Exp $"); #endif #include @@ -361,8 +361,8 @@ closecurses(void) static void saturate_cursor(void) { - assert(cursor_y >= 0); - assert(cursor_y <= cur_max_y()); + cursor_y = imax(cursor_y, 0); + cursor_y = imin(cursor_y, cur_max_y()); assert(cursor_x >= 0); cursor_x = imin(cursor_x, cur_max_x()); @@ -445,13 +445,17 @@ handle_key(void) break; case 14: /* ^N */ case KEY_DOWN: - if (cursor_y < cur_max_y()) - cursor_y++; + cursor_y++; break; case 16: /* ^P */ case KEY_UP: - if (cursor_y > 0) - cursor_y--; + cursor_y--; + break; + case KEY_PPAGE: + cursor_y -= LINES - 2; + break; + case KEY_NPAGE: + cursor_y += LINES - 2; break; case '*': hinting = !hinting;
CVS commit: src/games/cgram
Module Name:src Committed By: rillig Date: Mon Feb 22 16:28:20 UTC 2021 Modified Files: src/games/cgram: cgram.c Log Message: cgram: properly handle input errors On both NetBSD and Cygwin, a missing /usr/bin/fortune would previously continue since popen does not return an error (as /bin/sh is found and can be executed), so the next chance to catch an error is pclose. At that point, the shell has already printed an informative error message about what happened (or what didn't happen), so that cgram does not need to print an error by itself. To generate a diff of this commit: cvs rdiff -u -r1.11 -r1.12 src/games/cgram/cgram.c Please note that diffs are not public domain; they are subject to the copyright notices on the relevant files. Modified files: Index: src/games/cgram/cgram.c diff -u src/games/cgram/cgram.c:1.11 src/games/cgram/cgram.c:1.12 --- src/games/cgram/cgram.c:1.11 Sun Feb 21 22:21:56 2021 +++ src/games/cgram/cgram.c Mon Feb 22 16:28:20 2021 @@ -1,4 +1,4 @@ -/* $NetBSD: cgram.c,v 1.11 2021/02/21 22:21:56 rillig Exp $ */ +/* $NetBSD: cgram.c,v 1.12 2021/02/22 16:28:20 rillig Exp $ */ /*- * Copyright (c) 2013, 2021 The NetBSD Foundation, Inc. @@ -31,7 +31,7 @@ #include #if defined(__RCSID) && !defined(lint) -__RCSID("$NetBSD: cgram.c,v 1.11 2021/02/21 22:21:56 rillig Exp $"); +__RCSID("$NetBSD: cgram.c,v 1.12 2021/02/22 16:28:20 rillig Exp $"); #endif #include @@ -227,7 +227,8 @@ readquote(void) for (int i = 0; i < extent_y; i++) extent_x = imax(extent_x, (int)lines.v[i].len); - pclose(f); + if (pclose(f) != 0) + exit(1); /* error message must come from child process */ } static void
CVS commit: src/games/cgram
Module Name:src Committed By: rillig Date: Mon Feb 22 17:36:42 UTC 2021 Modified Files: src/games/cgram: cgram.c Log Message: cgram: add advanced cursor movement with tab, shift+tab, return To generate a diff of this commit: cvs rdiff -u -r1.12 -r1.13 src/games/cgram/cgram.c Please note that diffs are not public domain; they are subject to the copyright notices on the relevant files. Modified files: Index: src/games/cgram/cgram.c diff -u src/games/cgram/cgram.c:1.12 src/games/cgram/cgram.c:1.13 --- src/games/cgram/cgram.c:1.12 Mon Feb 22 16:28:20 2021 +++ src/games/cgram/cgram.c Mon Feb 22 17:36:42 2021 @@ -1,4 +1,4 @@ -/* $NetBSD: cgram.c,v 1.12 2021/02/22 16:28:20 rillig Exp $ */ +/* $NetBSD: cgram.c,v 1.13 2021/02/22 17:36:42 rillig Exp $ */ /*- * Copyright (c) 2013, 2021 The NetBSD Foundation, Inc. @@ -31,7 +31,7 @@ #include #if defined(__RCSID) && !defined(lint) -__RCSID("$NetBSD: cgram.c,v 1.12 2021/02/22 16:28:20 rillig Exp $"); +__RCSID("$NetBSD: cgram.c,v 1.13 2021/02/22 17:36:42 rillig Exp $"); #endif #include @@ -73,6 +73,12 @@ ch_islower(char ch) } static bool +ch_isspace(char ch) +{ + return isspace((unsigned char)ch) != 0; +} + +static bool ch_isupper(char ch) { return isupper((unsigned char)ch) != 0; @@ -193,6 +199,23 @@ cur_max_y(void) return extent_y - 1; } +static char +char_left_of_cursor(void) +{ + if (cursor_x > 0) + return lines.v[cursor_y].s[cursor_x - 1]; + assert(cursor_y > 0); + return '\n'; /* eol of previous line */ +} + +static char +char_at_cursor(void) +{ + if (cursor_x == cur_max_x()) + return '\n'; + return lines.v[cursor_y].s[cursor_x]; +} + static void readquote(void) { @@ -266,7 +289,7 @@ substitute(char ch) return false; } - char och = lines.v[cursor_y].s[cursor_x]; + char och = char_at_cursor(); if (!ch_isalpha(och)) { beep(); return false; @@ -292,8 +315,6 @@ substitute(char ch) return true; } - - static bool is_solved(void) { @@ -303,6 +324,8 @@ is_solved(void) return true; } + + static void redraw(void) { @@ -342,21 +365,6 @@ redraw(void) refresh(); } -static void -opencurses(void) -{ - initscr(); - cbreak(); - noecho(); - keypad(stdscr, true); -} - -static void -closecurses(void) -{ - endwin(); -} - static void @@ -383,6 +391,72 @@ scroll_into_view(void) offset_y = cursor_y - (LINES - 2); } +static bool +can_go_left(void) +{ + return cursor_y > 0 || + (cursor_y == 0 && cursor_x > 0); +} + +static bool +can_go_right(void) +{ + return cursor_y < cur_max_y() || + (cursor_y == cur_max_y() && cursor_x < cur_max_x()); +} + +static void +go_to_prev_line(void) +{ + cursor_y--; + cursor_x = cur_max_x(); +} + +static void +go_to_next_line(void) +{ + cursor_x = 0; + cursor_y++; +} + +static void +go_left(void) +{ + if (cursor_x > 0) + cursor_x--; + else if (cursor_y > 0) + go_to_prev_line(); +} + +static void +go_right(void) +{ + if (cursor_x < cur_max_x()) + cursor_x++; + else if (cursor_y < cur_max_y()) + go_to_next_line(); +} + +static void +go_to_prev_word(void) +{ + while (can_go_left() && ch_isspace(char_left_of_cursor())) + go_left(); + + while (can_go_left() && !ch_isspace(char_left_of_cursor())) + go_left(); +} + +static void +go_to_next_word(void) +{ + while (can_go_right() && !ch_isspace(char_at_cursor())) + go_right(); + + while (can_go_right() && ch_isspace(char_at_cursor())) + go_right(); +} + static void handle_char_input(int ch) { @@ -390,23 +464,13 @@ handle_char_input(int ch) if (substitute((char)ch)) { if (cursor_x < cur_max_x()) cursor_x++; - if (cursor_x == cur_max_x() && - cursor_y < cur_max_y()) { -cursor_x = 0; -cursor_y++; - } - } - } else if (cursor_x < cur_max_x() && - ch == lines.v[cursor_y].s[cursor_x]) { - cursor_x++; - if (cursor_x == cur_max_x() && - cursor_y < cur_max_y()) { - cursor_x = 0; - cursor_y++; + if (cursor_x == cur_max_x()) +go_to_next_line(); } - } else { + } else if (ch == char_at_cursor()) + go_right(); + else beep(); - } } static bool @@ -421,12 +485,7 @@ handle_key(void) break; case 2: /* ^B */ case KEY_LEFT: - if (cursor_x > 0) { - cursor_x--; - } else if (cursor_y > 0) { - cursor_y--; - cursor_x = cur_max_x(); - } + go_left(); break; case 5: /* ^E */ case KEY_END: @@ -434,12 +493,16 @@ handle_key(void) break; case 6: /* ^F */ case KEY_RIGHT: - if (cursor_x < cur_max_x()) { - cursor_x++; - } else if (cursor_y < cur_max_y()) { - cursor_y++; - cursor_x = 0; - } + go_right(); + break; + case '\t': + go_to_next_word(); + break; + case KEY_BTAB: + go_to_prev_word(); + break; + case '\n': + go_to_next_line(); break; case 12: /* ^L */ clear(); @@ -478,7 +541,11 @@ init(void) srandom((unsigned int)time(NUL
CVS commit: src/games/cgram
Module Name:src Committed By: rillig Date: Mon Feb 22 19:34:07 UTC 2021 Modified Files: src/games/cgram: cgram.c Log Message: cgram: fix assertion when substituting at the end of the longest line Reported by Weitian LI via GitHub. Contrary to the patch suggested in [1], still allow the cursor to be placed to the very right of the text in a line since that is the usual behavior of text editors. Split the function substitute() into two parts: one that handles the curses-specific part of checking whether a substitution is possible at the current cursor position, and one that performs the actual substitution. Only the latter is kept in the code section for the string manipulation functions, the other is moved to the section for curses code. Having all the curses code in one place reduces the places that call beep(). Previously, as well as now, there is a single beep per invalid key before, but that was not obvious from the previous code. [1]: https://github.com/DragonFlyBSD/DragonFlyBSD/commit/18d09f18cf4c To generate a diff of this commit: cvs rdiff -u -r1.13 -r1.14 src/games/cgram/cgram.c Please note that diffs are not public domain; they are subject to the copyright notices on the relevant files. Modified files: Index: src/games/cgram/cgram.c diff -u src/games/cgram/cgram.c:1.13 src/games/cgram/cgram.c:1.14 --- src/games/cgram/cgram.c:1.13 Mon Feb 22 17:36:42 2021 +++ src/games/cgram/cgram.c Mon Feb 22 19:34:07 2021 @@ -1,4 +1,4 @@ -/* $NetBSD: cgram.c,v 1.13 2021/02/22 17:36:42 rillig Exp $ */ +/* $NetBSD: cgram.c,v 1.14 2021/02/22 19:34:07 rillig Exp $ */ /*- * Copyright (c) 2013, 2021 The NetBSD Foundation, Inc. @@ -31,7 +31,7 @@ #include #if defined(__RCSID) && !defined(lint) -__RCSID("$NetBSD: cgram.c,v 1.13 2021/02/22 17:36:42 rillig Exp $"); +__RCSID("$NetBSD: cgram.c,v 1.14 2021/02/22 19:34:07 rillig Exp $"); #endif #include @@ -279,40 +279,26 @@ encode(void) } } -static bool -substitute(char ch) +static void +substitute(char a, char b) { - assert(cursor_x >= 0 && cursor_x < extent_x); - assert(cursor_y >= 0 && cursor_y < extent_y); - if (cursor_x >= cur_max_x()) { - beep(); - return false; - } - - char och = char_at_cursor(); - if (!ch_isalpha(och)) { - beep(); - return false; - } - - char loch = ch_tolower(och); - char uoch = ch_toupper(och); - char lch = ch_tolower(ch); - char uch = ch_toupper(ch); + char la = ch_tolower(a); + char ua = ch_toupper(a); + char lb = ch_tolower(b); + char ub = ch_toupper(b); for (int y = 0; y < (int)lines.num; y++) { for (char *p = lines.v[y].s; *p != '\0'; p++) { - if (*p == loch) -*p = lch; - else if (*p == uoch) -*p = uch; - else if (*p == lch) -*p = loch; - else if (*p == uch) -*p = uoch; + if (*p == la) +*p = lb; + else if (*p == ua) +*p = ub; + else if (*p == lb) +*p = la; + else if (*p == ub) +*p = ua; } } - return true; } static bool @@ -457,19 +443,24 @@ go_to_next_word(void) go_right(); } +static bool +can_substitute_here(int ch) +{ + return isascii(ch) && + ch_isalpha((char)ch) && + cursor_x < cur_max_x() && + ch_isalpha(char_at_cursor()); +} + static void handle_char_input(int ch) { - if (isascii(ch) && ch_isalpha((char)ch)) { - if (substitute((char)ch)) { - if (cursor_x < cur_max_x()) -cursor_x++; - if (cursor_x == cur_max_x()) -go_to_next_line(); - } - } else if (ch == char_at_cursor()) + if (ch == char_at_cursor()) + go_right(); + else if (can_substitute_here(ch)) { + substitute(char_at_cursor(), (char)ch); go_right(); - else + } else beep(); }
CVS commit: src/games/cgram
Module Name:src Committed By: rillig Date: Fri Feb 26 14:55:37 UTC 2021 Modified Files: src/games/cgram: cgram.c Log Message: cgram: make the 'solved' stand out more Suggested by Weitian LI via [1]. [1] https://github.com/NetBSD/src/commit/efec6410b2b5a736 To generate a diff of this commit: cvs rdiff -u -r1.14 -r1.15 src/games/cgram/cgram.c Please note that diffs are not public domain; they are subject to the copyright notices on the relevant files. Modified files: Index: src/games/cgram/cgram.c diff -u src/games/cgram/cgram.c:1.14 src/games/cgram/cgram.c:1.15 --- src/games/cgram/cgram.c:1.14 Mon Feb 22 19:34:07 2021 +++ src/games/cgram/cgram.c Fri Feb 26 14:55:37 2021 @@ -1,4 +1,4 @@ -/* $NetBSD: cgram.c,v 1.14 2021/02/22 19:34:07 rillig Exp $ */ +/* $NetBSD: cgram.c,v 1.15 2021/02/26 14:55:37 rillig Exp $ */ /*- * Copyright (c) 2013, 2021 The NetBSD Foundation, Inc. @@ -31,7 +31,7 @@ #include #if defined(__RCSID) && !defined(lint) -__RCSID("$NetBSD: cgram.c,v 1.14 2021/02/22 19:34:07 rillig Exp $"); +__RCSID("$NetBSD: cgram.c,v 1.15 2021/02/26 14:55:37 rillig Exp $"); #endif #include @@ -342,8 +342,12 @@ redraw(void) } move(LINES - 1, 0); - if (is_solved()) - addstr("*solved* "); + if (is_solved()) { + attron(A_BOLD | A_STANDOUT); + addstr("*solved*"); + attroff(A_BOLD | A_STANDOUT); + addch(' '); + } addstr("~ to quit, * to cheat, ^pnfb to move"); move(cursor_y - offset_y, cursor_x - offset_x);
CVS commit: src/games/cgram
Module Name:src Committed By: rillig Date: Fri Feb 26 15:15:34 UTC 2021 Modified Files: src/games/cgram: cgram.c Log Message: cgram: place the 'solved' marker nearer to the text This way, it cannot be overlooked as easily as before. It also doesn't change the position of the keyboard help text anymore. Only if there is not enough space, the 'solved' marker is put into the bottom line. To generate a diff of this commit: cvs rdiff -u -r1.15 -r1.16 src/games/cgram/cgram.c Please note that diffs are not public domain; they are subject to the copyright notices on the relevant files. Modified files: Index: src/games/cgram/cgram.c diff -u src/games/cgram/cgram.c:1.15 src/games/cgram/cgram.c:1.16 --- src/games/cgram/cgram.c:1.15 Fri Feb 26 14:55:37 2021 +++ src/games/cgram/cgram.c Fri Feb 26 15:15:34 2021 @@ -1,4 +1,4 @@ -/* $NetBSD: cgram.c,v 1.15 2021/02/26 14:55:37 rillig Exp $ */ +/* $NetBSD: cgram.c,v 1.16 2021/02/26 15:15:34 rillig Exp $ */ /*- * Copyright (c) 2013, 2021 The NetBSD Foundation, Inc. @@ -31,7 +31,7 @@ #include #if defined(__RCSID) && !defined(lint) -__RCSID("$NetBSD: cgram.c,v 1.15 2021/02/26 14:55:37 rillig Exp $"); +__RCSID("$NetBSD: cgram.c,v 1.16 2021/02/26 15:15:34 rillig Exp $"); #endif #include @@ -342,13 +342,17 @@ redraw(void) } move(LINES - 1, 0); + addstr("~ to quit, * to cheat, ^pnfb to move"); + if (is_solved()) { + if (extent_y + 1 - offset_y < LINES - 2) + move(extent_y + 1 - offset_y, 0); + else + addch(' '); attron(A_BOLD | A_STANDOUT); addstr("*solved*"); attroff(A_BOLD | A_STANDOUT); - addch(' '); } - addstr("~ to quit, * to cheat, ^pnfb to move"); move(cursor_y - offset_y, cursor_x - offset_x);
CVS commit: src/games/cgram
Module Name:src Committed By: rillig Date: Fri Feb 26 15:18:40 UTC 2021 Modified Files: src/games/cgram: cgram.c Log Message: cgram: don't beep if the window is resized To generate a diff of this commit: cvs rdiff -u -r1.16 -r1.17 src/games/cgram/cgram.c Please note that diffs are not public domain; they are subject to the copyright notices on the relevant files. Modified files: Index: src/games/cgram/cgram.c diff -u src/games/cgram/cgram.c:1.16 src/games/cgram/cgram.c:1.17 --- src/games/cgram/cgram.c:1.16 Fri Feb 26 15:15:34 2021 +++ src/games/cgram/cgram.c Fri Feb 26 15:18:40 2021 @@ -1,4 +1,4 @@ -/* $NetBSD: cgram.c,v 1.16 2021/02/26 15:15:34 rillig Exp $ */ +/* $NetBSD: cgram.c,v 1.17 2021/02/26 15:18:40 rillig Exp $ */ /*- * Copyright (c) 2013, 2021 The NetBSD Foundation, Inc. @@ -31,7 +31,7 @@ #include #if defined(__RCSID) && !defined(lint) -__RCSID("$NetBSD: cgram.c,v 1.16 2021/02/26 15:15:34 rillig Exp $"); +__RCSID("$NetBSD: cgram.c,v 1.17 2021/02/26 15:18:40 rillig Exp $"); #endif #include @@ -525,6 +525,8 @@ handle_key(void) break; case '~': return false; + case KEY_RESIZE: + break; default: handle_char_input(ch); break;
CVS commit: src/games/cgram
Module Name:src Committed By: dholland Date: Fri May 28 03:55:08 UTC 2021 Modified Files: src/games/cgram: cgram.c Log Message: Restore return value of main, arbitrarily thrown away in -r1.4. To generate a diff of this commit: cvs rdiff -u -r1.23 -r1.24 src/games/cgram/cgram.c Please note that diffs are not public domain; they are subject to the copyright notices on the relevant files. Modified files: Index: src/games/cgram/cgram.c diff -u src/games/cgram/cgram.c:1.23 src/games/cgram/cgram.c:1.24 --- src/games/cgram/cgram.c:1.23 Sat May 1 20:29:23 2021 +++ src/games/cgram/cgram.c Fri May 28 03:55:08 2021 @@ -1,4 +1,4 @@ -/* $NetBSD: cgram.c,v 1.23 2021/05/01 20:29:23 rillig Exp $ */ +/* $NetBSD: cgram.c,v 1.24 2021/05/28 03:55:08 dholland Exp $ */ /*- * Copyright (c) 2013, 2021 The NetBSD Foundation, Inc. @@ -31,7 +31,7 @@ #include #if defined(__RCSID) && !defined(lint) -__RCSID("$NetBSD: cgram.c,v 1.23 2021/05/01 20:29:23 rillig Exp $"); +__RCSID("$NetBSD: cgram.c,v 1.24 2021/05/28 03:55:08 dholland Exp $"); #endif #include @@ -612,4 +612,5 @@ main(int argc, char *argv[]) init(argc > 1 ? argv[1] : NULL); loop(); done(); + return 0; }
CVS commit: src/games/cgram
Module Name:src Committed By: dholland Date: Fri May 28 03:55:46 UTC 2021 Modified Files: src/games/cgram: cgram.c Log Message: I don't see any code I wrote left in here. To generate a diff of this commit: cvs rdiff -u -r1.24 -r1.25 src/games/cgram/cgram.c Please note that diffs are not public domain; they are subject to the copyright notices on the relevant files. Modified files: Index: src/games/cgram/cgram.c diff -u src/games/cgram/cgram.c:1.24 src/games/cgram/cgram.c:1.25 --- src/games/cgram/cgram.c:1.24 Fri May 28 03:55:08 2021 +++ src/games/cgram/cgram.c Fri May 28 03:55:45 2021 @@ -1,11 +1,11 @@ -/* $NetBSD: cgram.c,v 1.24 2021/05/28 03:55:08 dholland Exp $ */ +/* $NetBSD: cgram.c,v 1.25 2021/05/28 03:55:45 dholland Exp $ */ /*- * Copyright (c) 2013, 2021 The NetBSD Foundation, Inc. * All rights reserved. * * This code is derived from software contributed to The NetBSD Foundation - * by David A. Holland and Roland Illig. + * by Roland Illig. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions @@ -31,7 +31,7 @@ #include #if defined(__RCSID) && !defined(lint) -__RCSID("$NetBSD: cgram.c,v 1.24 2021/05/28 03:55:08 dholland Exp $"); +__RCSID("$NetBSD: cgram.c,v 1.25 2021/05/28 03:55:45 dholland Exp $"); #endif #include
CVS commit: src/games/cgram
Module Name:src Committed By: wiz Date: Thu Apr 22 14:57:36 UTC 2021 Modified Files: src/games/cgram: cgram.6 cgram.c Log Message: cgram: allow providing an input file instead of the random fortune To generate a diff of this commit: cvs rdiff -u -r1.2 -r1.3 src/games/cgram/cgram.6 cvs rdiff -u -r1.17 -r1.18 src/games/cgram/cgram.c Please note that diffs are not public domain; they are subject to the copyright notices on the relevant files. Modified files: Index: src/games/cgram/cgram.6 diff -u src/games/cgram/cgram.6:1.2 src/games/cgram/cgram.6:1.3 --- src/games/cgram/cgram.6:1.2 Sun Aug 4 07:55:09 2013 +++ src/games/cgram/cgram.6 Thu Apr 22 14:57:36 2021 @@ -1,4 +1,4 @@ -.\" $NetBSD: cgram.6,v 1.2 2013/08/04 07:55:09 wiz Exp $ +.\" $NetBSD: cgram.6,v 1.3 2021/04/22 14:57:36 wiz Exp $ .\" .\" Copyright (c) 2004, 2013 The NetBSD Foundation, Inc. .\" All rights reserved. @@ -27,21 +27,24 @@ .\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE .\" POSSIBILITY OF SUCH DAMAGE. .\" -.Dd August 3, 2013 +.Dd April 22, 2021 .Dt CGRAM 6 .Os .Sh NAME .Nm cgram .Nd solve Sunday-paper cryptograms .Sh SYNOPSIS -.Nm +.Nm Op Ar file .Sh DESCRIPTION .Nm is a curses-based widget for solving Sunday-paper-type cryptograms based on substitution ciphers. -A random cleartext is chosen using -.Xr fortune 6 -and a random substitution key is generated. +If +.Ar file +is given, it is used as cleartext, otherwise a random cleartext is +chosen using +.Xr fortune 6 ; +either way, a random substitution key is generated. .Pp The ciphertext is displayed. Typing a letter changes the key so that the letter under the cursor Index: src/games/cgram/cgram.c diff -u src/games/cgram/cgram.c:1.17 src/games/cgram/cgram.c:1.18 --- src/games/cgram/cgram.c:1.17 Fri Feb 26 15:18:40 2021 +++ src/games/cgram/cgram.c Thu Apr 22 14:57:36 2021 @@ -1,4 +1,4 @@ -/* $NetBSD: cgram.c,v 1.17 2021/02/26 15:18:40 rillig Exp $ */ +/* $NetBSD: cgram.c,v 1.18 2021/04/22 14:57:36 wiz Exp $ */ /*- * Copyright (c) 2013, 2021 The NetBSD Foundation, Inc. @@ -31,7 +31,7 @@ #include #if defined(__RCSID) && !defined(lint) -__RCSID("$NetBSD: cgram.c,v 1.17 2021/02/26 15:18:40 rillig Exp $"); +__RCSID("$NetBSD: cgram.c,v 1.18 2021/04/22 14:57:36 wiz Exp $"); #endif #include @@ -216,13 +216,7 @@ char_at_cursor(void) return lines.v[cursor_y].s[cursor_x]; } -static void -readquote(void) -{ - FILE *f = popen(_PATH_FORTUNE, "r"); - if (f == NULL) - err(1, "%s", _PATH_FORTUNE); - +static void getquote(FILE *f) { struct string line; string_init(&line); @@ -249,6 +243,30 @@ readquote(void) extent_y = (int)lines.num; for (int i = 0; i < extent_y; i++) extent_x = imax(extent_x, (int)lines.v[i].len); +} + +static void +readfile(const char *name) +{ + FILE *f = fopen(name, "r"); + if (f == NULL) + err(1, "%s", name); + + getquote(f); + + if (fclose(f) != 0) + exit(1); +} + + +static void +readquote(void) +{ + FILE *f = popen(_PATH_FORTUNE, "r"); + if (f == NULL) + err(1, "%s", _PATH_FORTUNE); + + getquote(f); if (pclose(f) != 0) exit(1); /* error message must come from child process */ @@ -535,12 +553,16 @@ handle_key(void) } static void -init(void) +init(const char *filename) { stringarray_init(&lines); stringarray_init(&sollines); srandom((unsigned int)time(NULL)); - readquote(); + if (filename) { + readfile(filename); + } else { + readquote(); + } encode(); initscr(); @@ -573,9 +595,9 @@ clean_up(void) int -main(void) +main(int argc, char *argv[]) { - init(); + init(argc > 1 ? argv[1] : NULL); loop(); clean_up(); }
CVS commit: src/games/cgram
Module Name:src Committed By: rillig Date: Sun Apr 25 20:14:29 UTC 2021 Modified Files: src/games/cgram: cgram.c Log Message: cgram: conform to lint's strict bool mode, KNF To generate a diff of this commit: cvs rdiff -u -r1.18 -r1.19 src/games/cgram/cgram.c Please note that diffs are not public domain; they are subject to the copyright notices on the relevant files. Modified files: Index: src/games/cgram/cgram.c diff -u src/games/cgram/cgram.c:1.18 src/games/cgram/cgram.c:1.19 --- src/games/cgram/cgram.c:1.18 Thu Apr 22 14:57:36 2021 +++ src/games/cgram/cgram.c Sun Apr 25 20:14:29 2021 @@ -1,4 +1,4 @@ -/* $NetBSD: cgram.c,v 1.18 2021/04/22 14:57:36 wiz Exp $ */ +/* $NetBSD: cgram.c,v 1.19 2021/04/25 20:14:29 rillig Exp $ */ /*- * Copyright (c) 2013, 2021 The NetBSD Foundation, Inc. @@ -31,7 +31,7 @@ #include #if defined(__RCSID) && !defined(lint) -__RCSID("$NetBSD: cgram.c,v 1.18 2021/04/22 14:57:36 wiz Exp $"); +__RCSID("$NetBSD: cgram.c,v 1.19 2021/04/25 20:14:29 rillig Exp $"); #endif #include @@ -216,7 +216,9 @@ char_at_cursor(void) return lines.v[cursor_y].s[cursor_x]; } -static void getquote(FILE *f) { +static void +getquote(FILE *f) +{ struct string line; string_init(&line); @@ -255,7 +257,7 @@ readfile(const char *name) getquote(f); if (fclose(f) != 0) - exit(1); + err(1, "%s", name); } @@ -558,7 +560,7 @@ init(const char *filename) stringarray_init(&lines); stringarray_init(&sollines); srandom((unsigned int)time(NULL)); - if (filename) { + if (filename != NULL) { readfile(filename); } else { readquote();
CVS commit: src/games/cgram
Module Name:src Committed By: rillig Date: Sun Apr 25 20:19:19 UTC 2021 Modified Files: src/games/cgram: cgram.c Log Message: cgram: provide usage on incorrect invocation To generate a diff of this commit: cvs rdiff -u -r1.19 -r1.20 src/games/cgram/cgram.c Please note that diffs are not public domain; they are subject to the copyright notices on the relevant files. Modified files: Index: src/games/cgram/cgram.c diff -u src/games/cgram/cgram.c:1.19 src/games/cgram/cgram.c:1.20 --- src/games/cgram/cgram.c:1.19 Sun Apr 25 20:14:29 2021 +++ src/games/cgram/cgram.c Sun Apr 25 20:19:19 2021 @@ -1,4 +1,4 @@ -/* $NetBSD: cgram.c,v 1.19 2021/04/25 20:14:29 rillig Exp $ */ +/* $NetBSD: cgram.c,v 1.20 2021/04/25 20:19:19 rillig Exp $ */ /*- * Copyright (c) 2013, 2021 The NetBSD Foundation, Inc. @@ -31,7 +31,7 @@ #include #if defined(__RCSID) && !defined(lint) -__RCSID("$NetBSD: cgram.c,v 1.19 2021/04/25 20:14:29 rillig Exp $"); +__RCSID("$NetBSD: cgram.c,v 1.20 2021/04/25 20:19:19 rillig Exp $"); #endif #include @@ -594,11 +594,23 @@ clean_up(void) stringarray_cleanup(&lines); } - + +static void __dead +usage(void) +{ + + fprintf(stderr, "usage: %s [file]\n", getprogname()); + exit(1); +} int main(int argc, char *argv[]) { + + setprogname(argv[0]); + if (argc != 1 && argc != 2) + usage(); + init(argc > 1 ? argv[1] : NULL); loop(); clean_up();
CVS commit: src/games/cgram
Module Name:src Committed By: rillig Date: Sun Apr 25 20:38:03 UTC 2021 Modified Files: src/games/cgram: cgram.c Log Message: cgram: use ASCII-only implementation of functions The function 'encode' already assumes that all letter characters are contiguous and that there are only 26 letters of each case. At the moment, cgram cannot handle UTF-8 anyway since it reads the input byte-wise, assuming that each byte is exacty one character. To generate a diff of this commit: cvs rdiff -u -r1.20 -r1.21 src/games/cgram/cgram.c Please note that diffs are not public domain; they are subject to the copyright notices on the relevant files. Modified files: Index: src/games/cgram/cgram.c diff -u src/games/cgram/cgram.c:1.20 src/games/cgram/cgram.c:1.21 --- src/games/cgram/cgram.c:1.20 Sun Apr 25 20:19:19 2021 +++ src/games/cgram/cgram.c Sun Apr 25 20:38:03 2021 @@ -1,4 +1,4 @@ -/* $NetBSD: cgram.c,v 1.20 2021/04/25 20:19:19 rillig Exp $ */ +/* $NetBSD: cgram.c,v 1.21 2021/04/25 20:38:03 rillig Exp $ */ /*- * Copyright (c) 2013, 2021 The NetBSD Foundation, Inc. @@ -31,7 +31,7 @@ #include #if defined(__RCSID) && !defined(lint) -__RCSID("$NetBSD: cgram.c,v 1.20 2021/04/25 20:19:19 rillig Exp $"); +__RCSID("$NetBSD: cgram.c,v 1.21 2021/04/25 20:38:03 rillig Exp $"); #endif #include @@ -46,42 +46,41 @@ __RCSID("$NetBSD: cgram.c,v 1.20 2021/04 #include "pathnames.h" - -static char -ch_toupper(char ch) +static bool +ch_isspace(char ch) { - return (char)toupper((unsigned char)ch); + return isspace((unsigned char)ch) != 0; } -static char -ch_tolower(char ch) +static bool +ch_islower(char ch) { - return (char)tolower((unsigned char)ch); + return ch >= 'a' && ch <= 'z'; } static bool -ch_isalpha(char ch) +ch_isupper(char ch) { - return isalpha((unsigned char)ch) != 0; + return ch >= 'A' && ch <= 'Z'; } static bool -ch_islower(char ch) +ch_isalpha(char ch) { - return islower((unsigned char)ch) != 0; + return ch_islower(ch) || ch_isupper(ch); } -static bool -ch_isspace(char ch) +static char +ch_toupper(char ch) { - return isspace((unsigned char)ch) != 0; + return ch_islower(ch) ? (char)(ch - 'a' + 'A') : ch; } -static bool -ch_isupper(char ch) +static char +ch_tolower(char ch) { - return isupper((unsigned char)ch) != 0; + return ch_isupper(ch) ? (char)(ch - 'A' + 'a') : ch; } static int
CVS commit: src/games/cgram
Module Name:src Committed By: rillig Date: Thu Apr 29 20:17:20 UTC 2021 Modified Files: src/games/cgram: cgram.6 cgram.c Log Message: cgram: in hinting mode, show all correct characters in bold face For almost solved cryptograms, this makes the punctuation stand out less, helping to focus on the missing letters. To generate a diff of this commit: cvs rdiff -u -r1.3 -r1.4 src/games/cgram/cgram.6 cvs rdiff -u -r1.21 -r1.22 src/games/cgram/cgram.c Please note that diffs are not public domain; they are subject to the copyright notices on the relevant files. Modified files: Index: src/games/cgram/cgram.6 diff -u src/games/cgram/cgram.6:1.3 src/games/cgram/cgram.6:1.4 --- src/games/cgram/cgram.6:1.3 Thu Apr 22 14:57:36 2021 +++ src/games/cgram/cgram.6 Thu Apr 29 20:17:20 2021 @@ -1,4 +1,4 @@ -.\" $NetBSD: cgram.6,v 1.3 2021/04/22 14:57:36 wiz Exp $ +.\" $NetBSD: cgram.6,v 1.4 2021/04/29 20:17:20 rillig Exp $ .\" .\" Copyright (c) 2004, 2013 The NetBSD Foundation, Inc. .\" All rights reserved. @@ -27,7 +27,7 @@ .\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE .\" POSSIBILITY OF SUCH DAMAGE. .\" -.Dd April 22, 2021 +.Dd April 29, 2021 .Dt CGRAM 6 .Os .Sh NAME @@ -55,7 +55,7 @@ Enter a tilde to quit. Press asterisk .Pq * -to enter an easier mode where correct letters are displayed in +to enter an easier mode where correct characters are displayed in boldface. .Sh SEE ALSO .Xr caesar 6 Index: src/games/cgram/cgram.c diff -u src/games/cgram/cgram.c:1.21 src/games/cgram/cgram.c:1.22 --- src/games/cgram/cgram.c:1.21 Sun Apr 25 20:38:03 2021 +++ src/games/cgram/cgram.c Thu Apr 29 20:17:20 2021 @@ -1,4 +1,4 @@ -/* $NetBSD: cgram.c,v 1.21 2021/04/25 20:38:03 rillig Exp $ */ +/* $NetBSD: cgram.c,v 1.22 2021/04/29 20:17:20 rillig Exp $ */ /*- * Copyright (c) 2013, 2021 The NetBSD Foundation, Inc. @@ -31,7 +31,7 @@ #include #if defined(__RCSID) && !defined(lint) -__RCSID("$NetBSD: cgram.c,v 1.21 2021/04/25 20:38:03 rillig Exp $"); +__RCSID("$NetBSD: cgram.c,v 1.22 2021/04/29 20:17:20 rillig Exp $"); #endif #include @@ -348,8 +348,7 @@ redraw(void) for (int x = 0; x < max_x; x++) { char ch = line[offset_x + x]; bool bold = hinting && - ch == solline[offset_x + x] && - ch_isalpha(ch); + (ch == solline[offset_x + x] || !ch_isalpha(ch)); if (bold) attron(A_BOLD);
CVS commit: src/games/cgram
Module Name:src Committed By: rillig Date: Sat May 1 20:29:23 UTC 2021 Modified Files: src/games/cgram: cgram.c Log Message: cgram: rename local functions The word 'cleanup' should have been named clean_up all along, but 'done' is even shorter. No functional change. To generate a diff of this commit: cvs rdiff -u -r1.22 -r1.23 src/games/cgram/cgram.c Please note that diffs are not public domain; they are subject to the copyright notices on the relevant files. Modified files: Index: src/games/cgram/cgram.c diff -u src/games/cgram/cgram.c:1.22 src/games/cgram/cgram.c:1.23 --- src/games/cgram/cgram.c:1.22 Thu Apr 29 20:17:20 2021 +++ src/games/cgram/cgram.c Sat May 1 20:29:23 2021 @@ -1,4 +1,4 @@ -/* $NetBSD: cgram.c,v 1.22 2021/04/29 20:17:20 rillig Exp $ */ +/* $NetBSD: cgram.c,v 1.23 2021/05/01 20:29:23 rillig Exp $ */ /*- * Copyright (c) 2013, 2021 The NetBSD Foundation, Inc. @@ -31,7 +31,7 @@ #include #if defined(__RCSID) && !defined(lint) -__RCSID("$NetBSD: cgram.c,v 1.22 2021/04/29 20:17:20 rillig Exp $"); +__RCSID("$NetBSD: cgram.c,v 1.23 2021/05/01 20:29:23 rillig Exp $"); #endif #include @@ -143,7 +143,7 @@ stringarray_init(struct stringarray *a) } static void -stringarray_cleanup(struct stringarray *a) +stringarray_done(struct stringarray *a) { for (size_t i = 0; i < a->num; i++) free(a->v[i].s); @@ -584,12 +584,12 @@ loop(void) } static void -clean_up(void) +done(void) { endwin(); - stringarray_cleanup(&sollines); - stringarray_cleanup(&lines); + stringarray_done(&sollines); + stringarray_done(&lines); } @@ -611,5 +611,5 @@ main(int argc, char *argv[]) init(argc > 1 ? argv[1] : NULL); loop(); - clean_up(); + done(); }
CVS commit: src/games/cgram
Module Name:src Committed By: dholland Date: Sun Aug 4 05:42:47 UTC 2013 Added Files: src/games/cgram: Makefile cgram.6 cgram.c pathnames.h Log Message: Add a curses gizmo for solving Sunday-paper-type cryptograms based on substitution ciphers. It gets the cleartext from fortune. I wrote this some years ago for my own amusement; a couple people have suggested that I should import it. Approved only by groo, so I'm going to wait a couple days to hook it to the build in case anyone demands it be removed again... To generate a diff of this commit: cvs rdiff -u -r0 -r1.1 src/games/cgram/Makefile src/games/cgram/cgram.6 \ src/games/cgram/cgram.c src/games/cgram/pathnames.h Please note that diffs are not public domain; they are subject to the copyright notices on the relevant files. Added files: Index: src/games/cgram/Makefile diff -u /dev/null src/games/cgram/Makefile:1.1 --- /dev/null Sun Aug 4 05:42:47 2013 +++ src/games/cgram/Makefile Sun Aug 4 05:42:47 2013 @@ -0,0 +1,10 @@ +# $NetBSD: Makefile,v 1.1 2013/08/04 05:42:47 dholland Exp $ + +PROG=cgram +DPADD=${LIBCURSES} ${LIBTERMINFO} +LDADD=-lcurses -lterminfo +SRCS=cgram.c +MAN=cgram.6 +HIDEGAME=hidegame + +.include Index: src/games/cgram/cgram.6 diff -u /dev/null src/games/cgram/cgram.6:1.1 --- /dev/null Sun Aug 4 05:42:47 2013 +++ src/games/cgram/cgram.6 Sun Aug 4 05:42:47 2013 @@ -0,0 +1,64 @@ +.\" +.\" Copyright (c) 2004, 2013 The NetBSD Foundation, Inc. +.\" All rights reserved. +.\" +.\" This code is derived from software contributed to The NetBSD Foundation +.\" by David A. Holland. +.\" +.\" Redistribution and use in source and binary forms, with or without +.\" modification, are permitted provided that the following conditions +.\" are met: +.\" 1. Redistributions of source code must retain the above copyright +.\"notice, this list of conditions and the following disclaimer. +.\" 2. Redistributions in binary form must reproduce the above copyright +.\"notice, this list of conditions and the following disclaimer in the +.\"documentation and/or other materials provided with the distribution. +.\" +.\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS +.\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED +.\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR +.\" PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS +.\" BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +.\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +.\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +.\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +.\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +.\" POSSIBILITY OF SUCH DAMAGE. +.\" +.Dd August 3, 2013 +.Dt CGRAM 6 +.Os +.Sh NAME +.Nm cgram +.Nd solve Sunday-paper cryptograms +.Sh SYNOPSIS +.Nm +.Sh DESCRIPTION +.Nm +is a curses-based widget for solving Sunday-paper-type cryptograms +based on substitution ciphers. +A random cleartext is chosen using +.Xr fortune 6 +and a random substitution key is generated. +.Pp +The ciphertext is displayed. +Typing a letter changes the key so that the letter under the cursor +maps to the newly typed letter, and updates the display accordingly. +Use Emacs-type cursor commands to move around. +Enter a tilde +.Pq ~ +to quit. +Press asterisk +.Pq * +to enter an easier mode where correct letters are displayed in +boldface. +.Sh SEE ALSO +.Xr caesar 6 +.Sh HISTORY +.Nm +was written circa 2004. +It was imported into +.Nx +in 2013 and first appeared in +.Nx 7.0 . Index: src/games/cgram/cgram.c diff -u /dev/null src/games/cgram/cgram.c:1.1 --- /dev/null Sun Aug 4 05:42:47 2013 +++ src/games/cgram/cgram.c Sun Aug 4 05:42:47 2013 @@ -0,0 +1,344 @@ +/*- + * Copyright (c) 2013 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by David A. Holland. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + *notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + *notice, this list of conditions and the following disclaimer in the + *documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
CVS commit: src/games/cgram
Module Name:src Committed By: wiz Date: Sun Aug 4 07:55:09 UTC 2013 Modified Files: src/games/cgram: cgram.6 Log Message: Add RCS Id. To generate a diff of this commit: cvs rdiff -u -r1.1 -r1.2 src/games/cgram/cgram.6 Please note that diffs are not public domain; they are subject to the copyright notices on the relevant files. Modified files: Index: src/games/cgram/cgram.6 diff -u src/games/cgram/cgram.6:1.1 src/games/cgram/cgram.6:1.2 --- src/games/cgram/cgram.6:1.1 Sun Aug 4 05:42:47 2013 +++ src/games/cgram/cgram.6 Sun Aug 4 07:55:09 2013 @@ -1,3 +1,4 @@ +.\" $NetBSD: cgram.6,v 1.2 2013/08/04 07:55:09 wiz Exp $ .\" .\" Copyright (c) 2004, 2013 The NetBSD Foundation, Inc. .\" All rights reserved.
CVS commit: src/games/cgram
Module Name:src Committed By: rillig Date: Thu Jul 2 19:11:01 UTC 2020 Modified Files: src/games/cgram: cgram.c Log Message: cgram(6): use standard cursor keys, use standard shuffle algorithm The previous shuffle algorithm asked for 100 random numbers, on average. The new algorithm asks exactly for 26 random numbers. Curses predefines numeric constants for keys, and there is no apparent reason not to use these standard keys for cursor movement. To generate a diff of this commit: cvs rdiff -u -r1.1 -r1.2 src/games/cgram/cgram.c Please note that diffs are not public domain; they are subject to the copyright notices on the relevant files. Modified files: Index: src/games/cgram/cgram.c diff -u src/games/cgram/cgram.c:1.1 src/games/cgram/cgram.c:1.2 --- src/games/cgram/cgram.c:1.1 Sun Aug 4 05:42:47 2013 +++ src/games/cgram/cgram.c Thu Jul 2 19:11:01 2020 @@ -120,16 +120,13 @@ static void readquote(void) { } static void encode(void) { - int used[26]; - for (int i=0; i<26; i++) used[i] = 0; - int key[26]; - int keypos=0; - while (keypos < 26) { - int c = random()%26; - if (used[c]) continue; - key[keypos++] = c; - used[c] = 1; + for (int i=0; i<26; i++) key[i] = i; + for (int i=26; i>1; i--) { + int c = random() % i; + int t = key[i-1]; + key[i-1] = key[c]; + key[c] = t; } for (int y=0; y 0) { curx--; } @@ -256,9 +255,11 @@ static void loop(void) { } break; case 5: /* ^E */ + case KEY_END: curx = strlen(lines.v[cury]); break; case 6: /* ^F */ + case KEY_RIGHT: if (curx < strlen(lines.v[cury])) { curx++; } @@ -271,6 +272,7 @@ static void loop(void) { clear(); break; case 14: /* ^N */ + case KEY_DOWN: if (cury < lines.num-1) { cury++; } @@ -282,6 +284,7 @@ static void loop(void) { } break; case 16: /* ^P */ + case KEY_UP: if (cury > 0) { cury--; } @@ -335,6 +338,7 @@ int main(void) { encode(); opencurses(); + keypad(stdscr, TRUE); loop(); closecurses();