1 /* $NetBSD: cgram.c,v 1.17 2021/02/26 15:18:40 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 isspace((unsigned char)ch) != 0;
79 return isupper((unsigned char)ch) != 0;
94 ////////////////////////////////////////////////////////////
108 string_init(struct string *s)
116 string_add(struct string *s, char ch)
118 if (s->len >= s->cap) {
119 s->cap = 2 * s->cap + 16;
120 s->s = realloc(s->s, s->cap);
122 errx(1, "Out of memory");
128 string_finish(struct string *s)
135 stringarray_init(struct stringarray *a)
142 stringarray_cleanup(struct stringarray *a)
144 for (size_t i = 0; i < a->num; i++)
150 stringarray_add(struct stringarray *a, struct string *s)
152 size_t num = a->num++;
153 a->v = realloc(a->v, a->num * sizeof a->v[0]);
155 errx(1, "Out of memory");
160 stringarray_dup(struct stringarray *dst, const struct stringarray *src)
162 assert(dst->num == 0);
163 for (size_t i = 0; i < src->num; i++) {
166 for (const char *p = src->v[i].s; *p != '\0'; p++)
167 string_add(&str, *p);
169 stringarray_add(dst, &str);
173 ////////////////////////////////////////////////////////////
175 static struct stringarray lines;
176 static struct stringarray sollines;
188 return (int)lines.v[cursor_y].len;
198 char_left_of_cursor(void)
201 return lines.v[cursor_y].s[cursor_x - 1];
202 assert(cursor_y > 0);
203 return '\n'; /* eol of previous line */
209 if (cursor_x == cur_max_x())
211 return lines.v[cursor_y].s[cursor_x];
217 FILE *f = popen(_PATH_FORTUNE, "r");
219 err(1, "%s", _PATH_FORTUNE);
225 while ((ch = fgetc(f)) != EOF) {
227 string_finish(&line);
228 stringarray_add(&lines, &line);
230 } else if (ch == '\t') {
231 string_add(&line, ' ');
232 while (line.len % 8 != 0)
233 string_add(&line, ' ');
234 } else if (ch == '\b') {
238 string_add(&line, (char)ch);
242 stringarray_dup(&sollines, &lines);
244 extent_y = (int)lines.num;
245 for (int i = 0; i < extent_y; i++)
246 extent_x = imax(extent_x, (int)lines.v[i].len);
249 exit(1); /* error message must come from child process */
257 for (int i = 0; i < 26; i++)
260 for (int i = 26; i > 1; i--) {
261 int c = (int)(random() % i);
267 for (int y = 0; y < extent_y; y++) {
268 for (char *p = lines.v[y].s; *p != '\0'; p++) {
270 *p = (char)('a' + key[*p - 'a']);
272 *p = (char)('A' + key[*p - 'A']);
278 substitute(char a, char b)
280 char la = ch_tolower(a);
281 char ua = ch_toupper(a);
282 char lb = ch_tolower(b);
283 char ub = ch_toupper(b);
285 for (int y = 0; y < (int)lines.num; y++) {
286 for (char *p = lines.v[y].s; *p != '\0'; p++) {
302 for (size_t i = 0; i < lines.num; i++)
303 if (strcmp(lines.v[i].s, sollines.v[i].s) != 0)
308 ////////////////////////////////////////////////////////////
315 int max_y = imin(LINES - 1, extent_y - offset_y);
316 for (int y = 0; y < max_y; y++) {
319 int len = (int)lines.v[offset_y + y].len;
320 int max_x = imin(COLS - 1, len - offset_x);
321 const char *line = lines.v[offset_y + y].s;
322 const char *solline = sollines.v[offset_y + y].s;
324 for (int x = 0; x < max_x; x++) {
325 char ch = line[offset_x + x];
326 bool bold = hinting &&
327 ch == solline[offset_x + x] &&
340 addstr("~ to quit, * to cheat, ^pnfb to move");
343 if (extent_y + 1 - offset_y < LINES - 2)
344 move(extent_y + 1 - offset_y, 0);
347 attron(A_BOLD | A_STANDOUT);
349 attroff(A_BOLD | A_STANDOUT);
352 move(cursor_y - offset_y, cursor_x - offset_x);
357 ////////////////////////////////////////////////////////////
360 saturate_cursor(void)
362 cursor_y = imax(cursor_y, 0);
363 cursor_y = imin(cursor_y, cur_max_y());
365 assert(cursor_x >= 0);
366 cursor_x = imin(cursor_x, cur_max_x());
370 scroll_into_view(void)
372 if (cursor_x < offset_x)
374 if (cursor_x > offset_x + COLS - 1)
375 offset_x = cursor_x - (COLS - 1);
377 if (cursor_y < offset_y)
379 if (cursor_y > offset_y + LINES - 2)
380 offset_y = cursor_y - (LINES - 2);
386 return cursor_y > 0 ||
387 (cursor_y == 0 && cursor_x > 0);
393 return cursor_y < cur_max_y() ||
394 (cursor_y == cur_max_y() && cursor_x < cur_max_x());
398 go_to_prev_line(void)
401 cursor_x = cur_max_x();
405 go_to_next_line(void)
416 else if (cursor_y > 0)
423 if (cursor_x < cur_max_x())
425 else if (cursor_y < cur_max_y())
430 go_to_prev_word(void)
432 while (can_go_left() && ch_isspace(char_left_of_cursor()))
435 while (can_go_left() && !ch_isspace(char_left_of_cursor()))
440 go_to_next_word(void)
442 while (can_go_right() && !ch_isspace(char_at_cursor()))
445 while (can_go_right() && ch_isspace(char_at_cursor()))
450 can_substitute_here(int ch)
452 return isascii(ch) &&
453 ch_isalpha((char)ch) &&
454 cursor_x < cur_max_x() &&
455 ch_isalpha(char_at_cursor());
459 handle_char_input(int ch)
461 if (ch == char_at_cursor())
463 else if (can_substitute_here(ch)) {
464 substitute(char_at_cursor(), (char)ch);
486 cursor_x = cur_max_x();
513 cursor_y -= LINES - 2;
516 cursor_y += LINES - 2;
526 handle_char_input(ch);
535 stringarray_init(&lines);
536 stringarray_init(&sollines);
537 srandom((unsigned int)time(NULL));
544 keypad(stdscr, true);
564 stringarray_cleanup(&sollines);
565 stringarray_cleanup(&lines);
568 ////////////////////////////////////////////////////////////