1 /* $NetBSD: cgram.c,v 1.11 2021/02/21 22:21:56 rillig Exp $ */
4 * Copyright (c) 2013, 2021 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by David A. Holland and Roland Illig.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
42 #include "pathnames.h"
44 ////////////////////////////////////////////////////////////
49 return (char)toupper((unsigned char)ch);
55 return (char)tolower((unsigned char)ch);
61 return isalpha((unsigned char)ch) != 0;
67 return islower((unsigned char)ch) != 0;
73 return isupper((unsigned char)ch) != 0;
88 ////////////////////////////////////////////////////////////
102 string_init(struct string *s)
110 string_add(struct string *s, char ch)
112 if (s->len >= s->cap) {
113 s->cap = 2 * s->cap + 16;
114 s->s = realloc(s->s, s->cap);
116 errx(1, "Out of memory");
122 string_finish(struct string *s)
129 stringarray_init(struct stringarray *a)
136 stringarray_cleanup(struct stringarray *a)
138 for (size_t i = 0; i < a->num; i++)
144 stringarray_add(struct stringarray *a, struct string *s)
146 size_t num = a->num++;
147 a->v = realloc(a->v, a->num * sizeof a->v[0]);
149 errx(1, "Out of memory");
154 stringarray_dup(struct stringarray *dst, const struct stringarray *src)
156 assert(dst->num == 0);
157 for (size_t i = 0; i < src->num; i++) {
160 for (const char *p = src->v[i].s; *p != '\0'; p++)
161 string_add(&str, *p);
163 stringarray_add(dst, &str);
167 ////////////////////////////////////////////////////////////
169 static struct stringarray lines;
170 static struct stringarray sollines;
182 return (int)lines.v[cursor_y].len - 1;
194 FILE *f = popen(_PATH_FORTUNE, "r");
196 err(1, "%s", _PATH_FORTUNE);
202 while ((ch = fgetc(f)) != EOF) {
204 string_finish(&line);
205 stringarray_add(&lines, &line);
207 } else if (ch == '\t') {
208 string_add(&line, ' ');
209 while (line.len % 8 != 0)
210 string_add(&line, ' ');
211 } else if (ch == '\b') {
215 string_add(&line, (char)ch);
219 stringarray_dup(&sollines, &lines);
221 extent_y = (int)lines.num;
222 for (int i = 0; i < extent_y; i++)
223 extent_x = imax(extent_x, (int)lines.v[i].len);
233 for (int i = 0; i < 26; i++)
236 for (int i = 26; i > 1; i--) {
237 int c = (int)(random() % i);
243 for (int y = 0; y < extent_y; y++) {
244 for (char *p = lines.v[y].s; *p != '\0'; p++) {
246 *p = (char)('a' + key[*p - 'a']);
248 *p = (char)('A' + key[*p - 'A']);
256 assert(cursor_x >= 0 && cursor_x < extent_x);
257 assert(cursor_y >= 0 && cursor_y < extent_y);
258 if (cursor_x > cur_max_x()) {
263 char och = lines.v[cursor_y].s[cursor_x];
264 if (!ch_isalpha(och)) {
269 char loch = ch_tolower(och);
270 char uoch = ch_toupper(och);
271 char lch = ch_tolower(ch);
272 char uch = ch_toupper(ch);
274 for (int y = 0; y < (int)lines.num; y++) {
275 for (char *p = lines.v[y].s; *p != '\0'; p++) {
289 ////////////////////////////////////////////////////////////
294 for (size_t i = 0; i < lines.num; i++)
295 if (strcmp(lines.v[i].s, sollines.v[i].s) != 0)
305 int max_y = imin(LINES - 1, extent_y - offset_y);
306 for (int y = 0; y < max_y; y++) {
309 int len = (int)lines.v[offset_y + y].len;
310 int max_x = imin(COLS - 1, len - offset_x);
311 const char *line = lines.v[offset_y + y].s;
312 const char *solline = sollines.v[offset_y + y].s;
314 for (int x = 0; x < max_x; x++) {
315 char ch = line[offset_x + x];
316 bool bold = hinting &&
317 ch == solline[offset_x + x] &&
332 addstr("~ to quit, * to cheat, ^pnfb to move");
334 move(cursor_y - offset_y, cursor_x - offset_x);
345 keypad(stdscr, true);
354 ////////////////////////////////////////////////////////////
357 saturate_cursor(void)
359 cursor_y = imax(cursor_y, 0);
360 cursor_y = imin(cursor_y, cur_max_y());
362 assert(cursor_x >= 0);
363 cursor_x = imin(cursor_x, cur_max_x());
367 scroll_into_view(void)
369 if (cursor_x < offset_x)
371 if (cursor_x > offset_x + COLS - 1)
372 offset_x = cursor_x - (COLS - 1);
374 if (cursor_y < offset_y)
376 if (cursor_y > offset_y + LINES - 2)
377 offset_y = cursor_y - (LINES - 2);
381 handle_char_input(int ch)
383 if (isascii(ch) && ch_isalpha((char)ch)) {
384 if (substitute((char)ch)) {
385 if (cursor_x <= cur_max_x())
387 if (cursor_x > cur_max_x() &&
388 cursor_y < cur_max_y()) {
393 } else if (cursor_x <= cur_max_x() &&
394 ch == lines.v[cursor_y].s[cursor_x]) {
396 if (cursor_x > cur_max_x() &&
397 cursor_y < cur_max_y()) {
420 } else if (cursor_y > 0) {
422 cursor_x = cur_max_x();
427 cursor_x = cur_max_x();
431 if (cursor_x < cur_max_x()) {
433 } else if (cursor_y < cur_max_y()) {
450 cursor_y -= LINES - 2;
453 cursor_y += LINES - 2;
461 handle_char_input(ch);
470 stringarray_init(&lines);
471 stringarray_init(&sollines);
472 srandom((unsigned int)time(NULL));
494 stringarray_cleanup(&sollines);
495 stringarray_cleanup(&lines);
498 ////////////////////////////////////////////////////////////