Home | History | Annotate | Line # | Download | only in cgram
cgram.c revision 1.26
      1  1.26       nia /* $NetBSD: cgram.c,v 1.26 2021/10/29 11:45:39 nia 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.25  dholland  * by 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.26       nia __RCSID("$NetBSD: cgram.c,v 1.26 2021/10/29 11:45:39 nia 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.23    rillig stringarray_done(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.26       nia 	if (reallocarr(&a->v, a->num, sizeof(a->v[0])) != 0)
    158   1.4    rillig 		errx(1, "Out of memory");
    159  1.10    rillig 	a->v[num] = *s;
    160  1.10    rillig }
    161  1.10    rillig 
    162  1.10    rillig static void
    163  1.10    rillig stringarray_dup(struct stringarray *dst, const struct stringarray *src)
    164  1.10    rillig {
    165  1.10    rillig 	assert(dst->num == 0);
    166  1.10    rillig 	for (size_t i = 0; i < src->num; i++) {
    167  1.10    rillig 		struct string str;
    168  1.10    rillig 		string_init(&str);
    169  1.10    rillig 		for (const char *p = src->v[i].s; *p != '\0'; p++)
    170  1.10    rillig 			string_add(&str, *p);
    171  1.10    rillig 		string_finish(&str);
    172  1.10    rillig 		stringarray_add(dst, &str);
    173   1.4    rillig 	}
    174   1.1  dholland }
    175   1.1  dholland 
    176   1.1  dholland ////////////////////////////////////////////////////////////
    177   1.1  dholland 
    178   1.1  dholland static struct stringarray lines;
    179   1.1  dholland static struct stringarray sollines;
    180   1.1  dholland static bool hinting;
    181  1.10    rillig static int extent_x;
    182  1.10    rillig static int extent_y;
    183  1.10    rillig static int offset_x;
    184  1.10    rillig static int offset_y;
    185  1.10    rillig static int cursor_x;
    186  1.10    rillig static int cursor_y;
    187  1.10    rillig 
    188  1.10    rillig static int
    189  1.10    rillig cur_max_x(void)
    190  1.10    rillig {
    191  1.10    rillig 	return (int)lines.v[cursor_y].len;
    192  1.10    rillig }
    193  1.10    rillig 
    194  1.10    rillig static int
    195  1.10    rillig cur_max_y(void)
    196  1.10    rillig {
    197  1.10    rillig 	return extent_y - 1;
    198  1.10    rillig }
    199   1.1  dholland 
    200  1.13    rillig static char
    201  1.13    rillig char_left_of_cursor(void)
    202  1.13    rillig {
    203  1.13    rillig 	if (cursor_x > 0)
    204  1.13    rillig 		return lines.v[cursor_y].s[cursor_x - 1];
    205  1.13    rillig 	assert(cursor_y > 0);
    206  1.13    rillig 	return '\n'; /* eol of previous line */
    207  1.13    rillig }
    208  1.13    rillig 
    209  1.13    rillig static char
    210  1.13    rillig char_at_cursor(void)
    211  1.13    rillig {
    212  1.13    rillig 	if (cursor_x == cur_max_x())
    213  1.13    rillig 		return '\n';
    214  1.13    rillig 	return lines.v[cursor_y].s[cursor_x];
    215  1.13    rillig }
    216  1.13    rillig 
    217  1.19    rillig static void
    218  1.19    rillig getquote(FILE *f)
    219  1.19    rillig {
    220  1.10    rillig 	struct string line;
    221  1.10    rillig 	string_init(&line);
    222  1.10    rillig 
    223  1.10    rillig 	int ch;
    224  1.10    rillig 	while ((ch = fgetc(f)) != EOF) {
    225  1.10    rillig 		if (ch == '\n') {
    226  1.10    rillig 			string_finish(&line);
    227  1.10    rillig 			stringarray_add(&lines, &line);
    228  1.10    rillig 			string_init(&line);
    229  1.10    rillig 		} else if (ch == '\t') {
    230  1.10    rillig 			string_add(&line, ' ');
    231  1.10    rillig 			while (line.len % 8 != 0)
    232  1.10    rillig 				string_add(&line, ' ');
    233  1.10    rillig 		} else if (ch == '\b') {
    234  1.10    rillig 			if (line.len > 0)
    235  1.10    rillig 				line.len--;
    236  1.10    rillig 		} else {
    237  1.10    rillig 			string_add(&line, (char)ch);
    238  1.10    rillig 		}
    239   1.4    rillig 	}
    240   1.4    rillig 
    241  1.10    rillig 	stringarray_dup(&sollines, &lines);
    242   1.4    rillig 
    243  1.10    rillig 	extent_y = (int)lines.num;
    244  1.10    rillig 	for (int i = 0; i < extent_y; i++)
    245  1.10    rillig 		extent_x = imax(extent_x, (int)lines.v[i].len);
    246  1.18       wiz }
    247  1.18       wiz 
    248  1.18       wiz static void
    249  1.18       wiz readfile(const char *name)
    250  1.18       wiz {
    251  1.18       wiz 	FILE *f = fopen(name, "r");
    252  1.18       wiz 	if (f == NULL)
    253  1.18       wiz 		err(1, "%s", name);
    254  1.18       wiz 
    255  1.18       wiz 	getquote(f);
    256  1.18       wiz 
    257  1.18       wiz 	if (fclose(f) != 0)
    258  1.19    rillig 		err(1, "%s", name);
    259  1.18       wiz }
    260  1.18       wiz 
    261  1.18       wiz 
    262  1.18       wiz static void
    263  1.18       wiz readquote(void)
    264  1.18       wiz {
    265  1.18       wiz 	FILE *f = popen(_PATH_FORTUNE, "r");
    266  1.18       wiz 	if (f == NULL)
    267  1.18       wiz 		err(1, "%s", _PATH_FORTUNE);
    268  1.18       wiz 
    269  1.18       wiz 	getquote(f);
    270   1.4    rillig 
    271  1.12    rillig 	if (pclose(f) != 0)
    272  1.12    rillig 		exit(1); /* error message must come from child process */
    273   1.4    rillig }
    274   1.4    rillig 
    275   1.4    rillig static void
    276   1.4    rillig encode(void)
    277   1.4    rillig {
    278   1.4    rillig 	int key[26];
    279   1.9    rillig 
    280   1.4    rillig 	for (int i = 0; i < 26; i++)
    281   1.4    rillig 		key[i] = i;
    282   1.9    rillig 
    283   1.4    rillig 	for (int i = 26; i > 1; i--) {
    284   1.9    rillig 		int c = (int)(random() % i);
    285   1.4    rillig 		int t = key[i - 1];
    286   1.4    rillig 		key[i - 1] = key[c];
    287   1.4    rillig 		key[c] = t;
    288   1.4    rillig 	}
    289   1.4    rillig 
    290  1.10    rillig 	for (int y = 0; y < extent_y; y++) {
    291  1.10    rillig 		for (char *p = lines.v[y].s; *p != '\0'; p++) {
    292   1.9    rillig 			if (ch_islower(*p))
    293   1.9    rillig 				*p = (char)('a' + key[*p - 'a']);
    294   1.9    rillig 			if (ch_isupper(*p))
    295   1.9    rillig 				*p = (char)('A' + key[*p - 'A']);
    296   1.4    rillig 		}
    297   1.4    rillig 	}
    298   1.4    rillig }
    299   1.4    rillig 
    300  1.14    rillig static void
    301  1.14    rillig substitute(char a, char b)
    302   1.4    rillig {
    303  1.14    rillig 	char la = ch_tolower(a);
    304  1.14    rillig 	char ua = ch_toupper(a);
    305  1.14    rillig 	char lb = ch_tolower(b);
    306  1.14    rillig 	char ub = ch_toupper(b);
    307   1.4    rillig 
    308   1.9    rillig 	for (int y = 0; y < (int)lines.num; y++) {
    309  1.10    rillig 		for (char *p = lines.v[y].s; *p != '\0'; p++) {
    310  1.14    rillig 			if (*p == la)
    311  1.14    rillig 				*p = lb;
    312  1.14    rillig 			else if (*p == ua)
    313  1.14    rillig 				*p = ub;
    314  1.14    rillig 			else if (*p == lb)
    315  1.14    rillig 				*p = la;
    316  1.14    rillig 			else if (*p == ub)
    317  1.14    rillig 				*p = ua;
    318   1.4    rillig 		}
    319   1.4    rillig 	}
    320   1.1  dholland }
    321   1.1  dholland 
    322  1.10    rillig static bool
    323  1.10    rillig is_solved(void)
    324  1.10    rillig {
    325  1.10    rillig 	for (size_t i = 0; i < lines.num; i++)
    326  1.10    rillig 		if (strcmp(lines.v[i].s, sollines.v[i].s) != 0)
    327  1.10    rillig 			return false;
    328  1.10    rillig 	return true;
    329  1.10    rillig }
    330  1.10    rillig 
    331  1.13    rillig ////////////////////////////////////////////////////////////
    332  1.13    rillig 
    333   1.4    rillig static void
    334   1.4    rillig redraw(void)
    335   1.4    rillig {
    336   1.4    rillig 	erase();
    337  1.10    rillig 
    338  1.10    rillig 	int max_y = imin(LINES - 1, extent_y - offset_y);
    339  1.10    rillig 	for (int y = 0; y < max_y; y++) {
    340  1.10    rillig 		move(y, 0);
    341  1.10    rillig 
    342  1.10    rillig 		int len = (int)lines.v[offset_y + y].len;
    343  1.10    rillig 		int max_x = imin(COLS - 1, len - offset_x);
    344  1.10    rillig 		const char *line = lines.v[offset_y + y].s;
    345  1.10    rillig 		const char *solline = sollines.v[offset_y + y].s;
    346  1.10    rillig 
    347  1.10    rillig 		for (int x = 0; x < max_x; x++) {
    348  1.10    rillig 			char ch = line[offset_x + x];
    349  1.10    rillig 			bool bold = hinting &&
    350  1.22    rillig 			    (ch == solline[offset_x + x] || !ch_isalpha(ch));
    351  1.10    rillig 
    352  1.10    rillig 			if (bold)
    353  1.10    rillig 				attron(A_BOLD);
    354  1.10    rillig 			addch(ch);
    355  1.10    rillig 			if (bold)
    356  1.10    rillig 				attroff(A_BOLD);
    357   1.4    rillig 		}
    358   1.4    rillig 		clrtoeol();
    359   1.4    rillig 	}
    360   1.4    rillig 
    361   1.4    rillig 	move(LINES - 1, 0);
    362  1.16    rillig 	addstr("~ to quit, * to cheat, ^pnfb to move");
    363  1.16    rillig 
    364  1.15    rillig 	if (is_solved()) {
    365  1.16    rillig 		if (extent_y + 1 - offset_y < LINES - 2)
    366  1.16    rillig 			move(extent_y + 1 - offset_y, 0);
    367  1.16    rillig 		else
    368  1.16    rillig 			addch(' ');
    369  1.15    rillig 		attron(A_BOLD | A_STANDOUT);
    370  1.15    rillig 		addstr("*solved*");
    371  1.15    rillig 		attroff(A_BOLD | A_STANDOUT);
    372  1.15    rillig 	}
    373   1.4    rillig 
    374  1.10    rillig 	move(cursor_y - offset_y, cursor_x - offset_x);
    375   1.4    rillig 
    376   1.4    rillig 	refresh();
    377   1.4    rillig }
    378   1.4    rillig 
    379   1.1  dholland ////////////////////////////////////////////////////////////
    380   1.1  dholland 
    381   1.4    rillig static void
    382  1.10    rillig saturate_cursor(void)
    383  1.10    rillig {
    384  1.11    rillig 	cursor_y = imax(cursor_y, 0);
    385  1.11    rillig 	cursor_y = imin(cursor_y, cur_max_y());
    386  1.10    rillig 
    387  1.10    rillig 	assert(cursor_x >= 0);
    388  1.10    rillig 	cursor_x = imin(cursor_x, cur_max_x());
    389  1.10    rillig }
    390  1.10    rillig 
    391  1.10    rillig static void
    392  1.10    rillig scroll_into_view(void)
    393  1.10    rillig {
    394  1.10    rillig 	if (cursor_x < offset_x)
    395  1.10    rillig 		offset_x = cursor_x;
    396  1.10    rillig 	if (cursor_x > offset_x + COLS - 1)
    397  1.10    rillig 		offset_x = cursor_x - (COLS - 1);
    398  1.10    rillig 
    399  1.10    rillig 	if (cursor_y < offset_y)
    400  1.10    rillig 		offset_y = cursor_y;
    401  1.10    rillig 	if (cursor_y > offset_y + LINES - 2)
    402  1.10    rillig 		offset_y = cursor_y - (LINES - 2);
    403  1.10    rillig }
    404  1.10    rillig 
    405  1.13    rillig static bool
    406  1.13    rillig can_go_left(void)
    407  1.13    rillig {
    408  1.13    rillig 	return cursor_y > 0 ||
    409  1.13    rillig 	    (cursor_y == 0 && cursor_x > 0);
    410  1.13    rillig }
    411  1.13    rillig 
    412  1.13    rillig static bool
    413  1.13    rillig can_go_right(void)
    414  1.13    rillig {
    415  1.13    rillig 	return cursor_y < cur_max_y() ||
    416  1.13    rillig 	    (cursor_y == cur_max_y() && cursor_x < cur_max_x());
    417  1.13    rillig }
    418  1.13    rillig 
    419  1.13    rillig static void
    420  1.13    rillig go_to_prev_line(void)
    421  1.13    rillig {
    422  1.13    rillig 	cursor_y--;
    423  1.13    rillig 	cursor_x = cur_max_x();
    424  1.13    rillig }
    425  1.13    rillig 
    426  1.13    rillig static void
    427  1.13    rillig go_to_next_line(void)
    428  1.13    rillig {
    429  1.13    rillig 	cursor_x = 0;
    430  1.13    rillig 	cursor_y++;
    431  1.13    rillig }
    432  1.13    rillig 
    433  1.13    rillig static void
    434  1.13    rillig go_left(void)
    435  1.13    rillig {
    436  1.13    rillig 	if (cursor_x > 0)
    437  1.13    rillig 		cursor_x--;
    438  1.13    rillig 	else if (cursor_y > 0)
    439  1.13    rillig 		go_to_prev_line();
    440  1.13    rillig }
    441  1.13    rillig 
    442  1.13    rillig static void
    443  1.13    rillig go_right(void)
    444  1.13    rillig {
    445  1.13    rillig 	if (cursor_x < cur_max_x())
    446  1.13    rillig 		cursor_x++;
    447  1.13    rillig 	else if (cursor_y < cur_max_y())
    448  1.13    rillig 		go_to_next_line();
    449  1.13    rillig }
    450  1.13    rillig 
    451  1.13    rillig static void
    452  1.13    rillig go_to_prev_word(void)
    453  1.13    rillig {
    454  1.13    rillig 	while (can_go_left() && ch_isspace(char_left_of_cursor()))
    455  1.13    rillig 		go_left();
    456  1.13    rillig 
    457  1.13    rillig 	while (can_go_left() && !ch_isspace(char_left_of_cursor()))
    458  1.13    rillig 		go_left();
    459  1.13    rillig }
    460  1.13    rillig 
    461  1.13    rillig static void
    462  1.13    rillig go_to_next_word(void)
    463  1.13    rillig {
    464  1.13    rillig 	while (can_go_right() && !ch_isspace(char_at_cursor()))
    465  1.13    rillig 		go_right();
    466  1.13    rillig 
    467  1.13    rillig 	while (can_go_right() && ch_isspace(char_at_cursor()))
    468  1.13    rillig 		go_right();
    469  1.13    rillig }
    470  1.13    rillig 
    471  1.14    rillig static bool
    472  1.14    rillig can_substitute_here(int ch)
    473  1.14    rillig {
    474  1.14    rillig 	return isascii(ch) &&
    475  1.14    rillig 	    ch_isalpha((char)ch) &&
    476  1.14    rillig 	    cursor_x < cur_max_x() &&
    477  1.14    rillig 	    ch_isalpha(char_at_cursor());
    478  1.14    rillig }
    479  1.14    rillig 
    480  1.10    rillig static void
    481  1.10    rillig handle_char_input(int ch)
    482   1.4    rillig {
    483  1.14    rillig 	if (ch == char_at_cursor())
    484  1.14    rillig 		go_right();
    485  1.14    rillig 	else if (can_substitute_here(ch)) {
    486  1.14    rillig 		substitute(char_at_cursor(), (char)ch);
    487  1.13    rillig 		go_right();
    488  1.14    rillig 	} else
    489  1.10    rillig 		beep();
    490   1.1  dholland }
    491   1.1  dholland 
    492  1.10    rillig static bool
    493  1.10    rillig handle_key(void)
    494  1.10    rillig {
    495  1.10    rillig 	int ch = getch();
    496  1.10    rillig 
    497  1.10    rillig 	switch (ch) {
    498  1.10    rillig 	case 1:			/* ^A */
    499  1.10    rillig 	case KEY_HOME:
    500  1.10    rillig 		cursor_x = 0;
    501  1.10    rillig 		break;
    502  1.10    rillig 	case 2:			/* ^B */
    503  1.10    rillig 	case KEY_LEFT:
    504  1.13    rillig 		go_left();
    505  1.10    rillig 		break;
    506  1.10    rillig 	case 5:			/* ^E */
    507  1.10    rillig 	case KEY_END:
    508  1.10    rillig 		cursor_x = cur_max_x();
    509  1.10    rillig 		break;
    510  1.10    rillig 	case 6:			/* ^F */
    511  1.10    rillig 	case KEY_RIGHT:
    512  1.13    rillig 		go_right();
    513  1.13    rillig 		break;
    514  1.13    rillig 	case '\t':
    515  1.13    rillig 		go_to_next_word();
    516  1.13    rillig 		break;
    517  1.13    rillig 	case KEY_BTAB:
    518  1.13    rillig 		go_to_prev_word();
    519  1.13    rillig 		break;
    520  1.13    rillig 	case '\n':
    521  1.13    rillig 		go_to_next_line();
    522  1.10    rillig 		break;
    523  1.10    rillig 	case 12:		/* ^L */
    524  1.10    rillig 		clear();
    525  1.10    rillig 		break;
    526  1.10    rillig 	case 14:		/* ^N */
    527  1.10    rillig 	case KEY_DOWN:
    528  1.11    rillig 		cursor_y++;
    529  1.10    rillig 		break;
    530  1.10    rillig 	case 16:		/* ^P */
    531  1.10    rillig 	case KEY_UP:
    532  1.11    rillig 		cursor_y--;
    533  1.11    rillig 		break;
    534  1.11    rillig 	case KEY_PPAGE:
    535  1.11    rillig 		cursor_y -= LINES - 2;
    536  1.11    rillig 		break;
    537  1.11    rillig 	case KEY_NPAGE:
    538  1.11    rillig 		cursor_y += LINES - 2;
    539  1.10    rillig 		break;
    540  1.10    rillig 	case '*':
    541  1.10    rillig 		hinting = !hinting;
    542  1.10    rillig 		break;
    543  1.10    rillig 	case '~':
    544  1.10    rillig 		return false;
    545  1.17    rillig 	case KEY_RESIZE:
    546  1.17    rillig 		break;
    547  1.10    rillig 	default:
    548  1.10    rillig 		handle_char_input(ch);
    549  1.10    rillig 		break;
    550  1.10    rillig 	}
    551  1.10    rillig 	return true;
    552  1.10    rillig }
    553   1.1  dholland 
    554  1.10    rillig static void
    555  1.18       wiz init(const char *filename)
    556   1.4    rillig {
    557   1.4    rillig 	stringarray_init(&lines);
    558   1.4    rillig 	stringarray_init(&sollines);
    559   1.9    rillig 	srandom((unsigned int)time(NULL));
    560  1.19    rillig 	if (filename != NULL) {
    561  1.18       wiz 	    readfile(filename);
    562  1.18       wiz 	} else {
    563  1.18       wiz 	    readquote();
    564  1.18       wiz 	}
    565   1.4    rillig 	encode();
    566  1.13    rillig 
    567  1.13    rillig 	initscr();
    568  1.13    rillig 	cbreak();
    569  1.13    rillig 	noecho();
    570  1.13    rillig 	keypad(stdscr, true);
    571  1.10    rillig }
    572   1.4    rillig 
    573  1.10    rillig static void
    574  1.10    rillig loop(void)
    575  1.10    rillig {
    576  1.10    rillig 	for (;;) {
    577  1.10    rillig 		redraw();
    578  1.10    rillig 		if (!handle_key())
    579  1.10    rillig 			break;
    580  1.10    rillig 		saturate_cursor();
    581  1.10    rillig 		scroll_into_view();
    582  1.10    rillig 	}
    583  1.10    rillig }
    584   1.4    rillig 
    585  1.10    rillig static void
    586  1.23    rillig done(void)
    587  1.10    rillig {
    588  1.13    rillig 	endwin();
    589  1.13    rillig 
    590  1.23    rillig 	stringarray_done(&sollines);
    591  1.23    rillig 	stringarray_done(&lines);
    592   1.1  dholland }
    593  1.10    rillig 
    594  1.20    rillig 
    595  1.20    rillig static void __dead
    596  1.20    rillig usage(void)
    597  1.20    rillig {
    598  1.20    rillig 
    599  1.20    rillig 	fprintf(stderr, "usage: %s [file]\n", getprogname());
    600  1.20    rillig 	exit(1);
    601  1.20    rillig }
    602  1.10    rillig 
    603  1.10    rillig int
    604  1.18       wiz main(int argc, char *argv[])
    605  1.10    rillig {
    606  1.20    rillig 
    607  1.20    rillig 	setprogname(argv[0]);
    608  1.20    rillig 	if (argc != 1 && argc != 2)
    609  1.20    rillig 		usage();
    610  1.20    rillig 
    611  1.18       wiz 	init(argc > 1 ? argv[1] : NULL);
    612  1.10    rillig 	loop();
    613  1.23    rillig 	done();
    614  1.24  dholland 	return 0;
    615  1.10    rillig }
    616