Home | History | Annotate | Line # | Download | only in cgram
cgram.c revision 1.22
      1  1.22    rillig /* $NetBSD: cgram.c,v 1.22 2021/04/29 20:17:20 rillig Exp $ */
      2   1.4    rillig 
      3   1.1  dholland /*-
      4  1.10    rillig  * Copyright (c) 2013, 2021 The NetBSD Foundation, Inc.
      5   1.1  dholland  * All rights reserved.
      6   1.1  dholland  *
      7   1.1  dholland  * This code is derived from software contributed to The NetBSD Foundation
      8  1.10    rillig  * by David A. Holland and Roland Illig.
      9   1.1  dholland  *
     10   1.1  dholland  * Redistribution and use in source and binary forms, with or without
     11   1.1  dholland  * modification, are permitted provided that the following conditions
     12   1.1  dholland  * are met:
     13   1.1  dholland  * 1. Redistributions of source code must retain the above copyright
     14   1.1  dholland  *    notice, this list of conditions and the following disclaimer.
     15   1.1  dholland  * 2. Redistributions in binary form must reproduce the above copyright
     16   1.1  dholland  *    notice, this list of conditions and the following disclaimer in the
     17   1.1  dholland  *    documentation and/or other materials provided with the distribution.
     18   1.1  dholland  *
     19   1.1  dholland  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20   1.1  dholland  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21   1.1  dholland  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22   1.1  dholland  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23   1.1  dholland  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24   1.1  dholland  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25   1.1  dholland  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26   1.1  dholland  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27   1.1  dholland  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28   1.1  dholland  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29   1.1  dholland  * POSSIBILITY OF SUCH DAMAGE.
     30   1.1  dholland  */
     31   1.1  dholland 
     32  1.10    rillig #include <sys/cdefs.h>
     33  1.10    rillig #if defined(__RCSID) && !defined(lint)
     34  1.22    rillig __RCSID("$NetBSD: cgram.c,v 1.22 2021/04/29 20:17:20 rillig Exp $");
     35  1.10    rillig #endif
     36  1.10    rillig 
     37   1.5    rillig #include <assert.h>
     38   1.5    rillig #include <ctype.h>
     39   1.5    rillig #include <curses.h>
     40   1.5    rillig #include <err.h>
     41   1.6    rillig #include <stdbool.h>
     42   1.1  dholland #include <stdio.h>
     43   1.5    rillig #include <stdlib.h>
     44   1.1  dholland #include <string.h>
     45   1.1  dholland #include <time.h>
     46   1.5    rillig 
     47   1.1  dholland #include "pathnames.h"
     48   1.1  dholland 
     49   1.1  dholland 
     50  1.21    rillig static bool
     51  1.21    rillig ch_isspace(char ch)
     52   1.8    rillig {
     53  1.21    rillig 	return isspace((unsigned char)ch) != 0;
     54   1.8    rillig }
     55   1.8    rillig 
     56  1.21    rillig static bool
     57  1.21    rillig ch_islower(char ch)
     58   1.8    rillig {
     59  1.21    rillig 	return ch >= 'a' && ch <= 'z';
     60   1.8    rillig }
     61   1.8    rillig 
     62   1.8    rillig static bool
     63  1.21    rillig ch_isupper(char ch)
     64   1.8    rillig {
     65  1.21    rillig 	return ch >= 'A' && ch <= 'Z';
     66   1.8    rillig }
     67   1.8    rillig 
     68   1.8    rillig static bool
     69  1.21    rillig ch_isalpha(char ch)
     70   1.8    rillig {
     71  1.21    rillig 	return ch_islower(ch) || ch_isupper(ch);
     72   1.8    rillig }
     73   1.8    rillig 
     74  1.21    rillig static char
     75  1.21    rillig ch_toupper(char ch)
     76  1.13    rillig {
     77  1.21    rillig 	return ch_islower(ch) ? (char)(ch - 'a' + 'A') : ch;
     78  1.13    rillig }
     79  1.13    rillig 
     80  1.21    rillig static char
     81  1.21    rillig ch_tolower(char ch)
     82   1.8    rillig {
     83  1.21    rillig 	return ch_isupper(ch) ? (char)(ch - 'A' + 'a') : ch;
     84  1.10    rillig }
     85  1.10    rillig 
     86  1.10    rillig static int
     87  1.10    rillig imax(int a, int b)
     88  1.10    rillig {
     89  1.10    rillig 	return a > b ? a : b;
     90  1.10    rillig }
     91  1.10    rillig 
     92  1.10    rillig static int
     93  1.10    rillig imin(int a, int b)
     94  1.10    rillig {
     95  1.10    rillig 	return a < b ? a : b;
     96   1.8    rillig }
     97   1.8    rillig 
     98   1.1  dholland ////////////////////////////////////////////////////////////
     99   1.1  dholland 
    100  1.10    rillig struct string {
    101  1.10    rillig 	char *s;
    102  1.10    rillig 	size_t len;
    103  1.10    rillig 	size_t cap;
    104  1.10    rillig };
    105  1.10    rillig 
    106   1.1  dholland struct stringarray {
    107  1.10    rillig 	struct string *v;
    108   1.9    rillig 	size_t num;
    109   1.1  dholland };
    110   1.1  dholland 
    111   1.4    rillig static void
    112  1.10    rillig string_init(struct string *s)
    113  1.10    rillig {
    114  1.10    rillig 	s->s = NULL;
    115  1.10    rillig 	s->len = 0;
    116  1.10    rillig 	s->cap = 0;
    117  1.10    rillig }
    118  1.10    rillig 
    119  1.10    rillig static void
    120  1.10    rillig string_add(struct string *s, char ch)
    121  1.10    rillig {
    122  1.10    rillig 	if (s->len >= s->cap) {
    123  1.10    rillig 		s->cap = 2 * s->cap + 16;
    124  1.10    rillig 		s->s = realloc(s->s, s->cap);
    125  1.10    rillig 		if (s->s == NULL)
    126  1.10    rillig 			errx(1, "Out of memory");
    127  1.10    rillig 	}
    128  1.10    rillig 	s->s[s->len++] = ch;
    129  1.10    rillig }
    130  1.10    rillig 
    131  1.10    rillig static void
    132  1.10    rillig string_finish(struct string *s)
    133  1.10    rillig {
    134  1.10    rillig 	string_add(s, '\0');
    135  1.10    rillig 	s->len--;
    136  1.10    rillig }
    137  1.10    rillig 
    138  1.10    rillig static void
    139   1.4    rillig stringarray_init(struct stringarray *a)
    140   1.4    rillig {
    141   1.4    rillig 	a->v = NULL;
    142   1.4    rillig 	a->num = 0;
    143   1.4    rillig }
    144   1.4    rillig 
    145   1.4    rillig static void
    146   1.4    rillig stringarray_cleanup(struct stringarray *a)
    147   1.4    rillig {
    148  1.10    rillig 	for (size_t i = 0; i < a->num; i++)
    149  1.10    rillig 		free(a->v[i].s);
    150   1.4    rillig 	free(a->v);
    151   1.4    rillig }
    152   1.4    rillig 
    153   1.4    rillig static void
    154  1.10    rillig stringarray_add(struct stringarray *a, struct string *s)
    155   1.4    rillig {
    156  1.10    rillig 	size_t num = a->num++;
    157  1.10    rillig 	a->v = realloc(a->v, a->num * sizeof a->v[0]);
    158  1.10    rillig 	if (a->v == NULL)
    159   1.4    rillig 		errx(1, "Out of memory");
    160  1.10    rillig 	a->v[num] = *s;
    161  1.10    rillig }
    162  1.10    rillig 
    163  1.10    rillig static void
    164  1.10    rillig stringarray_dup(struct stringarray *dst, const struct stringarray *src)
    165  1.10    rillig {
    166  1.10    rillig 	assert(dst->num == 0);
    167  1.10    rillig 	for (size_t i = 0; i < src->num; i++) {
    168  1.10    rillig 		struct string str;
    169  1.10    rillig 		string_init(&str);
    170  1.10    rillig 		for (const char *p = src->v[i].s; *p != '\0'; p++)
    171  1.10    rillig 			string_add(&str, *p);
    172  1.10    rillig 		string_finish(&str);
    173  1.10    rillig 		stringarray_add(dst, &str);
    174   1.4    rillig 	}
    175   1.1  dholland }
    176   1.1  dholland 
    177   1.1  dholland ////////////////////////////////////////////////////////////
    178   1.1  dholland 
    179   1.1  dholland static struct stringarray lines;
    180   1.1  dholland static struct stringarray sollines;
    181   1.1  dholland static bool hinting;
    182  1.10    rillig static int extent_x;
    183  1.10    rillig static int extent_y;
    184  1.10    rillig static int offset_x;
    185  1.10    rillig static int offset_y;
    186  1.10    rillig static int cursor_x;
    187  1.10    rillig static int cursor_y;
    188  1.10    rillig 
    189  1.10    rillig static int
    190  1.10    rillig cur_max_x(void)
    191  1.10    rillig {
    192  1.10    rillig 	return (int)lines.v[cursor_y].len;
    193  1.10    rillig }
    194  1.10    rillig 
    195  1.10    rillig static int
    196  1.10    rillig cur_max_y(void)
    197  1.10    rillig {
    198  1.10    rillig 	return extent_y - 1;
    199  1.10    rillig }
    200   1.1  dholland 
    201  1.13    rillig static char
    202  1.13    rillig char_left_of_cursor(void)
    203  1.13    rillig {
    204  1.13    rillig 	if (cursor_x > 0)
    205  1.13    rillig 		return lines.v[cursor_y].s[cursor_x - 1];
    206  1.13    rillig 	assert(cursor_y > 0);
    207  1.13    rillig 	return '\n'; /* eol of previous line */
    208  1.13    rillig }
    209  1.13    rillig 
    210  1.13    rillig static char
    211  1.13    rillig char_at_cursor(void)
    212  1.13    rillig {
    213  1.13    rillig 	if (cursor_x == cur_max_x())
    214  1.13    rillig 		return '\n';
    215  1.13    rillig 	return lines.v[cursor_y].s[cursor_x];
    216  1.13    rillig }
    217  1.13    rillig 
    218  1.19    rillig static void
    219  1.19    rillig getquote(FILE *f)
    220  1.19    rillig {
    221  1.10    rillig 	struct string line;
    222  1.10    rillig 	string_init(&line);
    223  1.10    rillig 
    224  1.10    rillig 	int ch;
    225  1.10    rillig 	while ((ch = fgetc(f)) != EOF) {
    226  1.10    rillig 		if (ch == '\n') {
    227  1.10    rillig 			string_finish(&line);
    228  1.10    rillig 			stringarray_add(&lines, &line);
    229  1.10    rillig 			string_init(&line);
    230  1.10    rillig 		} else if (ch == '\t') {
    231  1.10    rillig 			string_add(&line, ' ');
    232  1.10    rillig 			while (line.len % 8 != 0)
    233  1.10    rillig 				string_add(&line, ' ');
    234  1.10    rillig 		} else if (ch == '\b') {
    235  1.10    rillig 			if (line.len > 0)
    236  1.10    rillig 				line.len--;
    237  1.10    rillig 		} else {
    238  1.10    rillig 			string_add(&line, (char)ch);
    239  1.10    rillig 		}
    240   1.4    rillig 	}
    241   1.4    rillig 
    242  1.10    rillig 	stringarray_dup(&sollines, &lines);
    243   1.4    rillig 
    244  1.10    rillig 	extent_y = (int)lines.num;
    245  1.10    rillig 	for (int i = 0; i < extent_y; i++)
    246  1.10    rillig 		extent_x = imax(extent_x, (int)lines.v[i].len);
    247  1.18       wiz }
    248  1.18       wiz 
    249  1.18       wiz static void
    250  1.18       wiz readfile(const char *name)
    251  1.18       wiz {
    252  1.18       wiz 	FILE *f = fopen(name, "r");
    253  1.18       wiz 	if (f == NULL)
    254  1.18       wiz 		err(1, "%s", name);
    255  1.18       wiz 
    256  1.18       wiz 	getquote(f);
    257  1.18       wiz 
    258  1.18       wiz 	if (fclose(f) != 0)
    259  1.19    rillig 		err(1, "%s", name);
    260  1.18       wiz }
    261  1.18       wiz 
    262  1.18       wiz 
    263  1.18       wiz static void
    264  1.18       wiz readquote(void)
    265  1.18       wiz {
    266  1.18       wiz 	FILE *f = popen(_PATH_FORTUNE, "r");
    267  1.18       wiz 	if (f == NULL)
    268  1.18       wiz 		err(1, "%s", _PATH_FORTUNE);
    269  1.18       wiz 
    270  1.18       wiz 	getquote(f);
    271   1.4    rillig 
    272  1.12    rillig 	if (pclose(f) != 0)
    273  1.12    rillig 		exit(1); /* error message must come from child process */
    274   1.4    rillig }
    275   1.4    rillig 
    276   1.4    rillig static void
    277   1.4    rillig encode(void)
    278   1.4    rillig {
    279   1.4    rillig 	int key[26];
    280   1.9    rillig 
    281   1.4    rillig 	for (int i = 0; i < 26; i++)
    282   1.4    rillig 		key[i] = i;
    283   1.9    rillig 
    284   1.4    rillig 	for (int i = 26; i > 1; i--) {
    285   1.9    rillig 		int c = (int)(random() % i);
    286   1.4    rillig 		int t = key[i - 1];
    287   1.4    rillig 		key[i - 1] = key[c];
    288   1.4    rillig 		key[c] = t;
    289   1.4    rillig 	}
    290   1.4    rillig 
    291  1.10    rillig 	for (int y = 0; y < extent_y; y++) {
    292  1.10    rillig 		for (char *p = lines.v[y].s; *p != '\0'; p++) {
    293   1.9    rillig 			if (ch_islower(*p))
    294   1.9    rillig 				*p = (char)('a' + key[*p - 'a']);
    295   1.9    rillig 			if (ch_isupper(*p))
    296   1.9    rillig 				*p = (char)('A' + key[*p - 'A']);
    297   1.4    rillig 		}
    298   1.4    rillig 	}
    299   1.4    rillig }
    300   1.4    rillig 
    301  1.14    rillig static void
    302  1.14    rillig substitute(char a, char b)
    303   1.4    rillig {
    304  1.14    rillig 	char la = ch_tolower(a);
    305  1.14    rillig 	char ua = ch_toupper(a);
    306  1.14    rillig 	char lb = ch_tolower(b);
    307  1.14    rillig 	char ub = ch_toupper(b);
    308   1.4    rillig 
    309   1.9    rillig 	for (int y = 0; y < (int)lines.num; y++) {
    310  1.10    rillig 		for (char *p = lines.v[y].s; *p != '\0'; p++) {
    311  1.14    rillig 			if (*p == la)
    312  1.14    rillig 				*p = lb;
    313  1.14    rillig 			else if (*p == ua)
    314  1.14    rillig 				*p = ub;
    315  1.14    rillig 			else if (*p == lb)
    316  1.14    rillig 				*p = la;
    317  1.14    rillig 			else if (*p == ub)
    318  1.14    rillig 				*p = ua;
    319   1.4    rillig 		}
    320   1.4    rillig 	}
    321   1.1  dholland }
    322   1.1  dholland 
    323  1.10    rillig static bool
    324  1.10    rillig is_solved(void)
    325  1.10    rillig {
    326  1.10    rillig 	for (size_t i = 0; i < lines.num; i++)
    327  1.10    rillig 		if (strcmp(lines.v[i].s, sollines.v[i].s) != 0)
    328  1.10    rillig 			return false;
    329  1.10    rillig 	return true;
    330  1.10    rillig }
    331  1.10    rillig 
    332  1.13    rillig ////////////////////////////////////////////////////////////
    333  1.13    rillig 
    334   1.4    rillig static void
    335   1.4    rillig redraw(void)
    336   1.4    rillig {
    337   1.4    rillig 	erase();
    338  1.10    rillig 
    339  1.10    rillig 	int max_y = imin(LINES - 1, extent_y - offset_y);
    340  1.10    rillig 	for (int y = 0; y < max_y; y++) {
    341  1.10    rillig 		move(y, 0);
    342  1.10    rillig 
    343  1.10    rillig 		int len = (int)lines.v[offset_y + y].len;
    344  1.10    rillig 		int max_x = imin(COLS - 1, len - offset_x);
    345  1.10    rillig 		const char *line = lines.v[offset_y + y].s;
    346  1.10    rillig 		const char *solline = sollines.v[offset_y + y].s;
    347  1.10    rillig 
    348  1.10    rillig 		for (int x = 0; x < max_x; x++) {
    349  1.10    rillig 			char ch = line[offset_x + x];
    350  1.10    rillig 			bool bold = hinting &&
    351  1.22    rillig 			    (ch == solline[offset_x + x] || !ch_isalpha(ch));
    352  1.10    rillig 
    353  1.10    rillig 			if (bold)
    354  1.10    rillig 				attron(A_BOLD);
    355  1.10    rillig 			addch(ch);
    356  1.10    rillig 			if (bold)
    357  1.10    rillig 				attroff(A_BOLD);
    358   1.4    rillig 		}
    359   1.4    rillig 		clrtoeol();
    360   1.4    rillig 	}
    361   1.4    rillig 
    362   1.4    rillig 	move(LINES - 1, 0);
    363  1.16    rillig 	addstr("~ to quit, * to cheat, ^pnfb to move");
    364  1.16    rillig 
    365  1.15    rillig 	if (is_solved()) {
    366  1.16    rillig 		if (extent_y + 1 - offset_y < LINES - 2)
    367  1.16    rillig 			move(extent_y + 1 - offset_y, 0);
    368  1.16    rillig 		else
    369  1.16    rillig 			addch(' ');
    370  1.15    rillig 		attron(A_BOLD | A_STANDOUT);
    371  1.15    rillig 		addstr("*solved*");
    372  1.15    rillig 		attroff(A_BOLD | A_STANDOUT);
    373  1.15    rillig 	}
    374   1.4    rillig 
    375  1.10    rillig 	move(cursor_y - offset_y, cursor_x - offset_x);
    376   1.4    rillig 
    377   1.4    rillig 	refresh();
    378   1.4    rillig }
    379   1.4    rillig 
    380   1.1  dholland ////////////////////////////////////////////////////////////
    381   1.1  dholland 
    382   1.4    rillig static void
    383  1.10    rillig saturate_cursor(void)
    384  1.10    rillig {
    385  1.11    rillig 	cursor_y = imax(cursor_y, 0);
    386  1.11    rillig 	cursor_y = imin(cursor_y, cur_max_y());
    387  1.10    rillig 
    388  1.10    rillig 	assert(cursor_x >= 0);
    389  1.10    rillig 	cursor_x = imin(cursor_x, cur_max_x());
    390  1.10    rillig }
    391  1.10    rillig 
    392  1.10    rillig static void
    393  1.10    rillig scroll_into_view(void)
    394  1.10    rillig {
    395  1.10    rillig 	if (cursor_x < offset_x)
    396  1.10    rillig 		offset_x = cursor_x;
    397  1.10    rillig 	if (cursor_x > offset_x + COLS - 1)
    398  1.10    rillig 		offset_x = cursor_x - (COLS - 1);
    399  1.10    rillig 
    400  1.10    rillig 	if (cursor_y < offset_y)
    401  1.10    rillig 		offset_y = cursor_y;
    402  1.10    rillig 	if (cursor_y > offset_y + LINES - 2)
    403  1.10    rillig 		offset_y = cursor_y - (LINES - 2);
    404  1.10    rillig }
    405  1.10    rillig 
    406  1.13    rillig static bool
    407  1.13    rillig can_go_left(void)
    408  1.13    rillig {
    409  1.13    rillig 	return cursor_y > 0 ||
    410  1.13    rillig 	    (cursor_y == 0 && cursor_x > 0);
    411  1.13    rillig }
    412  1.13    rillig 
    413  1.13    rillig static bool
    414  1.13    rillig can_go_right(void)
    415  1.13    rillig {
    416  1.13    rillig 	return cursor_y < cur_max_y() ||
    417  1.13    rillig 	    (cursor_y == cur_max_y() && cursor_x < cur_max_x());
    418  1.13    rillig }
    419  1.13    rillig 
    420  1.13    rillig static void
    421  1.13    rillig go_to_prev_line(void)
    422  1.13    rillig {
    423  1.13    rillig 	cursor_y--;
    424  1.13    rillig 	cursor_x = cur_max_x();
    425  1.13    rillig }
    426  1.13    rillig 
    427  1.13    rillig static void
    428  1.13    rillig go_to_next_line(void)
    429  1.13    rillig {
    430  1.13    rillig 	cursor_x = 0;
    431  1.13    rillig 	cursor_y++;
    432  1.13    rillig }
    433  1.13    rillig 
    434  1.13    rillig static void
    435  1.13    rillig go_left(void)
    436  1.13    rillig {
    437  1.13    rillig 	if (cursor_x > 0)
    438  1.13    rillig 		cursor_x--;
    439  1.13    rillig 	else if (cursor_y > 0)
    440  1.13    rillig 		go_to_prev_line();
    441  1.13    rillig }
    442  1.13    rillig 
    443  1.13    rillig static void
    444  1.13    rillig go_right(void)
    445  1.13    rillig {
    446  1.13    rillig 	if (cursor_x < cur_max_x())
    447  1.13    rillig 		cursor_x++;
    448  1.13    rillig 	else if (cursor_y < cur_max_y())
    449  1.13    rillig 		go_to_next_line();
    450  1.13    rillig }
    451  1.13    rillig 
    452  1.13    rillig static void
    453  1.13    rillig go_to_prev_word(void)
    454  1.13    rillig {
    455  1.13    rillig 	while (can_go_left() && ch_isspace(char_left_of_cursor()))
    456  1.13    rillig 		go_left();
    457  1.13    rillig 
    458  1.13    rillig 	while (can_go_left() && !ch_isspace(char_left_of_cursor()))
    459  1.13    rillig 		go_left();
    460  1.13    rillig }
    461  1.13    rillig 
    462  1.13    rillig static void
    463  1.13    rillig go_to_next_word(void)
    464  1.13    rillig {
    465  1.13    rillig 	while (can_go_right() && !ch_isspace(char_at_cursor()))
    466  1.13    rillig 		go_right();
    467  1.13    rillig 
    468  1.13    rillig 	while (can_go_right() && ch_isspace(char_at_cursor()))
    469  1.13    rillig 		go_right();
    470  1.13    rillig }
    471  1.13    rillig 
    472  1.14    rillig static bool
    473  1.14    rillig can_substitute_here(int ch)
    474  1.14    rillig {
    475  1.14    rillig 	return isascii(ch) &&
    476  1.14    rillig 	    ch_isalpha((char)ch) &&
    477  1.14    rillig 	    cursor_x < cur_max_x() &&
    478  1.14    rillig 	    ch_isalpha(char_at_cursor());
    479  1.14    rillig }
    480  1.14    rillig 
    481  1.10    rillig static void
    482  1.10    rillig handle_char_input(int ch)
    483   1.4    rillig {
    484  1.14    rillig 	if (ch == char_at_cursor())
    485  1.14    rillig 		go_right();
    486  1.14    rillig 	else if (can_substitute_here(ch)) {
    487  1.14    rillig 		substitute(char_at_cursor(), (char)ch);
    488  1.13    rillig 		go_right();
    489  1.14    rillig 	} else
    490  1.10    rillig 		beep();
    491   1.1  dholland }
    492   1.1  dholland 
    493  1.10    rillig static bool
    494  1.10    rillig handle_key(void)
    495  1.10    rillig {
    496  1.10    rillig 	int ch = getch();
    497  1.10    rillig 
    498  1.10    rillig 	switch (ch) {
    499  1.10    rillig 	case 1:			/* ^A */
    500  1.10    rillig 	case KEY_HOME:
    501  1.10    rillig 		cursor_x = 0;
    502  1.10    rillig 		break;
    503  1.10    rillig 	case 2:			/* ^B */
    504  1.10    rillig 	case KEY_LEFT:
    505  1.13    rillig 		go_left();
    506  1.10    rillig 		break;
    507  1.10    rillig 	case 5:			/* ^E */
    508  1.10    rillig 	case KEY_END:
    509  1.10    rillig 		cursor_x = cur_max_x();
    510  1.10    rillig 		break;
    511  1.10    rillig 	case 6:			/* ^F */
    512  1.10    rillig 	case KEY_RIGHT:
    513  1.13    rillig 		go_right();
    514  1.13    rillig 		break;
    515  1.13    rillig 	case '\t':
    516  1.13    rillig 		go_to_next_word();
    517  1.13    rillig 		break;
    518  1.13    rillig 	case KEY_BTAB:
    519  1.13    rillig 		go_to_prev_word();
    520  1.13    rillig 		break;
    521  1.13    rillig 	case '\n':
    522  1.13    rillig 		go_to_next_line();
    523  1.10    rillig 		break;
    524  1.10    rillig 	case 12:		/* ^L */
    525  1.10    rillig 		clear();
    526  1.10    rillig 		break;
    527  1.10    rillig 	case 14:		/* ^N */
    528  1.10    rillig 	case KEY_DOWN:
    529  1.11    rillig 		cursor_y++;
    530  1.10    rillig 		break;
    531  1.10    rillig 	case 16:		/* ^P */
    532  1.10    rillig 	case KEY_UP:
    533  1.11    rillig 		cursor_y--;
    534  1.11    rillig 		break;
    535  1.11    rillig 	case KEY_PPAGE:
    536  1.11    rillig 		cursor_y -= LINES - 2;
    537  1.11    rillig 		break;
    538  1.11    rillig 	case KEY_NPAGE:
    539  1.11    rillig 		cursor_y += LINES - 2;
    540  1.10    rillig 		break;
    541  1.10    rillig 	case '*':
    542  1.10    rillig 		hinting = !hinting;
    543  1.10    rillig 		break;
    544  1.10    rillig 	case '~':
    545  1.10    rillig 		return false;
    546  1.17    rillig 	case KEY_RESIZE:
    547  1.17    rillig 		break;
    548  1.10    rillig 	default:
    549  1.10    rillig 		handle_char_input(ch);
    550  1.10    rillig 		break;
    551  1.10    rillig 	}
    552  1.10    rillig 	return true;
    553  1.10    rillig }
    554   1.1  dholland 
    555  1.10    rillig static void
    556  1.18       wiz init(const char *filename)
    557   1.4    rillig {
    558   1.4    rillig 	stringarray_init(&lines);
    559   1.4    rillig 	stringarray_init(&sollines);
    560   1.9    rillig 	srandom((unsigned int)time(NULL));
    561  1.19    rillig 	if (filename != NULL) {
    562  1.18       wiz 	    readfile(filename);
    563  1.18       wiz 	} else {
    564  1.18       wiz 	    readquote();
    565  1.18       wiz 	}
    566   1.4    rillig 	encode();
    567  1.13    rillig 
    568  1.13    rillig 	initscr();
    569  1.13    rillig 	cbreak();
    570  1.13    rillig 	noecho();
    571  1.13    rillig 	keypad(stdscr, true);
    572  1.10    rillig }
    573   1.4    rillig 
    574  1.10    rillig static void
    575  1.10    rillig loop(void)
    576  1.10    rillig {
    577  1.10    rillig 	for (;;) {
    578  1.10    rillig 		redraw();
    579  1.10    rillig 		if (!handle_key())
    580  1.10    rillig 			break;
    581  1.10    rillig 		saturate_cursor();
    582  1.10    rillig 		scroll_into_view();
    583  1.10    rillig 	}
    584  1.10    rillig }
    585   1.4    rillig 
    586  1.10    rillig static void
    587  1.10    rillig clean_up(void)
    588  1.10    rillig {
    589  1.13    rillig 	endwin();
    590  1.13    rillig 
    591   1.4    rillig 	stringarray_cleanup(&sollines);
    592   1.4    rillig 	stringarray_cleanup(&lines);
    593   1.1  dholland }
    594  1.10    rillig 
    595  1.20    rillig 
    596  1.20    rillig static void __dead
    597  1.20    rillig usage(void)
    598  1.20    rillig {
    599  1.20    rillig 
    600  1.20    rillig 	fprintf(stderr, "usage: %s [file]\n", getprogname());
    601  1.20    rillig 	exit(1);
    602  1.20    rillig }
    603  1.10    rillig 
    604  1.10    rillig int
    605  1.18       wiz main(int argc, char *argv[])
    606  1.10    rillig {
    607  1.20    rillig 
    608  1.20    rillig 	setprogname(argv[0]);
    609  1.20    rillig 	if (argc != 1 && argc != 2)
    610  1.20    rillig 		usage();
    611  1.20    rillig 
    612  1.18       wiz 	init(argc > 1 ? argv[1] : NULL);
    613  1.10    rillig 	loop();
    614  1.10    rillig 	clean_up();
    615  1.10    rillig }
    616