cgram.c revision 1.3 1 1.1 dholland /*-
2 1.1 dholland * Copyright (c) 2013 The NetBSD Foundation, Inc.
3 1.1 dholland * All rights reserved.
4 1.1 dholland *
5 1.1 dholland * This code is derived from software contributed to The NetBSD Foundation
6 1.1 dholland * by David A. Holland.
7 1.1 dholland *
8 1.1 dholland * Redistribution and use in source and binary forms, with or without
9 1.1 dholland * modification, are permitted provided that the following conditions
10 1.1 dholland * are met:
11 1.1 dholland * 1. Redistributions of source code must retain the above copyright
12 1.1 dholland * notice, this list of conditions and the following disclaimer.
13 1.1 dholland * 2. Redistributions in binary form must reproduce the above copyright
14 1.1 dholland * notice, this list of conditions and the following disclaimer in the
15 1.1 dholland * documentation and/or other materials provided with the distribution.
16 1.1 dholland *
17 1.1 dholland * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
18 1.1 dholland * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19 1.1 dholland * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20 1.1 dholland * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
21 1.1 dholland * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 1.1 dholland * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 1.1 dholland * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 1.1 dholland * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 1.1 dholland * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 1.1 dholland * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 1.1 dholland * POSSIBILITY OF SUCH DAMAGE.
28 1.1 dholland */
29 1.1 dholland
30 1.1 dholland #include <stdio.h>
31 1.1 dholland #include <string.h>
32 1.1 dholland #include <stdlib.h>
33 1.1 dholland #include <ctype.h>
34 1.1 dholland #include <time.h>
35 1.1 dholland #include <err.h>
36 1.1 dholland #include <assert.h>
37 1.1 dholland #include <curses.h>
38 1.1 dholland #include "pathnames.h"
39 1.1 dholland
40 1.1 dholland ////////////////////////////////////////////////////////////
41 1.1 dholland
42 1.1 dholland static char *xstrdup(const char *s) {
43 1.1 dholland char *ret;
44 1.1 dholland
45 1.1 dholland ret = malloc(strlen(s) + 1);
46 1.1 dholland if (ret == NULL) {
47 1.1 dholland errx(1, "Out of memory");
48 1.1 dholland }
49 1.1 dholland strcpy(ret, s);
50 1.1 dholland return ret;
51 1.1 dholland }
52 1.1 dholland
53 1.1 dholland ////////////////////////////////////////////////////////////
54 1.1 dholland
55 1.1 dholland struct stringarray {
56 1.1 dholland char **v;
57 1.1 dholland int num;
58 1.1 dholland };
59 1.1 dholland
60 1.1 dholland static void stringarray_init(struct stringarray *a) {
61 1.1 dholland a->v = NULL;
62 1.1 dholland a->num = 0;
63 1.1 dholland }
64 1.1 dholland
65 1.1 dholland static void stringarray_cleanup(struct stringarray *a) {
66 1.1 dholland free(a->v);
67 1.1 dholland }
68 1.1 dholland
69 1.1 dholland static void stringarray_add(struct stringarray *a, const char *s) {
70 1.1 dholland a->v = realloc(a->v, (a->num + 1) * sizeof(a->v[0]));
71 1.1 dholland if (a->v == NULL) {
72 1.1 dholland errx(1, "Out of memory");
73 1.1 dholland }
74 1.1 dholland a->v[a->num] = xstrdup(s);
75 1.1 dholland a->num++;
76 1.1 dholland }
77 1.1 dholland
78 1.1 dholland ////////////////////////////////////////////////////////////
79 1.1 dholland
80 1.1 dholland static struct stringarray lines;
81 1.1 dholland static struct stringarray sollines;
82 1.1 dholland static bool hinting;
83 1.1 dholland static int scrolldown;
84 1.1 dholland static unsigned curx;
85 1.1 dholland static int cury;
86 1.1 dholland
87 1.1 dholland static void readquote(void) {
88 1.1 dholland FILE *f = popen(_PATH_FORTUNE, "r");
89 1.1 dholland if (!f) {
90 1.1 dholland err(1, "%s", _PATH_FORTUNE);
91 1.1 dholland }
92 1.1 dholland
93 1.1 dholland char buf[128], buf2[8*sizeof(buf)];
94 1.1 dholland while (fgets(buf, sizeof(buf), f)) {
95 1.1 dholland char *s = strrchr(buf, '\n');
96 1.1 dholland assert(s);
97 1.1 dholland assert(strlen(s)==1);
98 1.1 dholland *s = 0;
99 1.1 dholland
100 1.1 dholland int i,j;
101 1.1 dholland for (i=j=0; buf[i]; i++) {
102 1.1 dholland if (buf[i]=='\t') {
103 1.1 dholland buf2[j++] = ' ';
104 1.1 dholland while (j%8) buf2[j++] = ' ';
105 1.1 dholland }
106 1.1 dholland else if (buf[i]=='\b') {
107 1.1 dholland if (j>0) j--;
108 1.1 dholland }
109 1.1 dholland else {
110 1.1 dholland buf2[j++] = buf[i];
111 1.1 dholland }
112 1.1 dholland }
113 1.1 dholland buf2[j] = 0;
114 1.1 dholland
115 1.1 dholland stringarray_add(&lines, buf2);
116 1.1 dholland stringarray_add(&sollines, buf2);
117 1.1 dholland }
118 1.1 dholland
119 1.1 dholland pclose(f);
120 1.1 dholland }
121 1.1 dholland
122 1.1 dholland static void encode(void) {
123 1.1 dholland int key[26];
124 1.2 rillig for (int i=0; i<26; i++) key[i] = i;
125 1.2 rillig for (int i=26; i>1; i--) {
126 1.2 rillig int c = random() % i;
127 1.2 rillig int t = key[i-1];
128 1.2 rillig key[i-1] = key[c];
129 1.2 rillig key[c] = t;
130 1.1 dholland }
131 1.1 dholland
132 1.1 dholland for (int y=0; y<lines.num; y++) {
133 1.1 dholland for (unsigned x=0; lines.v[y][x]; x++) {
134 1.1 dholland if (islower((unsigned char)lines.v[y][x])) {
135 1.1 dholland int q = lines.v[y][x]-'a';
136 1.1 dholland lines.v[y][x] = 'a'+key[q];
137 1.1 dholland }
138 1.1 dholland if (isupper((unsigned char)lines.v[y][x])) {
139 1.1 dholland int q = lines.v[y][x]-'A';
140 1.1 dholland lines.v[y][x] = 'A'+key[q];
141 1.1 dholland }
142 1.1 dholland }
143 1.1 dholland }
144 1.1 dholland }
145 1.1 dholland
146 1.1 dholland static int substitute(int ch) {
147 1.1 dholland assert(cury>=0 && cury<lines.num);
148 1.1 dholland if (curx >= strlen(lines.v[cury])) {
149 1.1 dholland beep();
150 1.1 dholland return -1;
151 1.1 dholland }
152 1.1 dholland
153 1.1 dholland int och = lines.v[cury][curx];
154 1.1 dholland if (!isalpha((unsigned char)och)) {
155 1.1 dholland beep();
156 1.1 dholland return -1;
157 1.1 dholland }
158 1.1 dholland
159 1.1 dholland int loch = tolower((unsigned char)och);
160 1.1 dholland int uoch = toupper((unsigned char)och);
161 1.1 dholland int lch = tolower((unsigned char)ch);
162 1.1 dholland int uch = toupper((unsigned char)ch);
163 1.1 dholland
164 1.1 dholland for (int y=0; y<lines.num; y++) {
165 1.1 dholland for (unsigned x=0; lines.v[y][x]; x++) {
166 1.1 dholland if (lines.v[y][x]==loch) {
167 1.1 dholland lines.v[y][x] = lch;
168 1.1 dholland }
169 1.1 dholland else if (lines.v[y][x]==uoch) {
170 1.1 dholland lines.v[y][x] = uch;
171 1.1 dholland }
172 1.1 dholland else if (lines.v[y][x]==lch) {
173 1.1 dholland lines.v[y][x] = loch;
174 1.1 dholland }
175 1.1 dholland else if (lines.v[y][x]==uch) {
176 1.1 dholland lines.v[y][x] = uoch;
177 1.1 dholland }
178 1.1 dholland }
179 1.1 dholland }
180 1.1 dholland return 0;
181 1.1 dholland }
182 1.1 dholland
183 1.1 dholland ////////////////////////////////////////////////////////////
184 1.1 dholland
185 1.1 dholland static void redraw(void) {
186 1.1 dholland erase();
187 1.1 dholland bool won = true;
188 1.1 dholland for (int i=0; i<LINES-1; i++) {
189 1.1 dholland move(i, 0);
190 1.1 dholland int ln = i+scrolldown;
191 1.1 dholland if (ln < lines.num) {
192 1.1 dholland for (unsigned j=0; lines.v[i][j]; j++) {
193 1.1 dholland int ch = lines.v[i][j];
194 1.1 dholland if (ch != sollines.v[i][j] && isalpha((unsigned char)ch)) {
195 1.1 dholland won = false;
196 1.1 dholland }
197 1.1 dholland bool bold=false;
198 1.1 dholland if (hinting && ch==sollines.v[i][j] &&
199 1.1 dholland isalpha((unsigned char)ch)) {
200 1.1 dholland bold = true;
201 1.1 dholland attron(A_BOLD);
202 1.1 dholland }
203 1.1 dholland addch(lines.v[i][j]);
204 1.1 dholland if (bold) {
205 1.1 dholland attroff(A_BOLD);
206 1.1 dholland }
207 1.1 dholland }
208 1.1 dholland }
209 1.1 dholland clrtoeol();
210 1.1 dholland }
211 1.1 dholland
212 1.1 dholland move(LINES-1, 0);
213 1.1 dholland if (won) {
214 1.1 dholland addstr("*solved* ");
215 1.1 dholland }
216 1.1 dholland addstr("~ to quit, * to cheat, ^pnfb to move");
217 1.1 dholland
218 1.1 dholland move(LINES-1, 0);
219 1.1 dholland
220 1.1 dholland move(cury-scrolldown, curx);
221 1.1 dholland
222 1.1 dholland refresh();
223 1.1 dholland }
224 1.1 dholland
225 1.1 dholland static void opencurses(void) {
226 1.1 dholland initscr();
227 1.1 dholland cbreak();
228 1.1 dholland noecho();
229 1.1 dholland }
230 1.1 dholland
231 1.1 dholland static void closecurses(void) {
232 1.1 dholland endwin();
233 1.1 dholland }
234 1.1 dholland
235 1.1 dholland ////////////////////////////////////////////////////////////
236 1.1 dholland
237 1.1 dholland static void loop(void) {
238 1.1 dholland bool done=false;
239 1.1 dholland while (!done) {
240 1.1 dholland redraw();
241 1.1 dholland int ch = getch();
242 1.1 dholland switch (ch) {
243 1.1 dholland case 1: /* ^A */
244 1.3 rillig case KEY_HOME:
245 1.1 dholland curx=0;
246 1.1 dholland break;
247 1.1 dholland case 2: /* ^B */
248 1.2 rillig case KEY_LEFT:
249 1.1 dholland if (curx > 0) {
250 1.1 dholland curx--;
251 1.1 dholland }
252 1.1 dholland else if (cury > 0) {
253 1.1 dholland cury--;
254 1.1 dholland curx = strlen(lines.v[cury]);
255 1.1 dholland }
256 1.1 dholland break;
257 1.1 dholland case 5: /* ^E */
258 1.2 rillig case KEY_END:
259 1.1 dholland curx = strlen(lines.v[cury]);
260 1.1 dholland break;
261 1.1 dholland case 6: /* ^F */
262 1.2 rillig case KEY_RIGHT:
263 1.1 dholland if (curx < strlen(lines.v[cury])) {
264 1.1 dholland curx++;
265 1.1 dholland }
266 1.1 dholland else if (cury < lines.num - 1) {
267 1.1 dholland cury++;
268 1.1 dholland curx = 0;
269 1.1 dholland }
270 1.1 dholland break;
271 1.1 dholland case 12: /* ^L */
272 1.1 dholland clear();
273 1.1 dholland break;
274 1.1 dholland case 14: /* ^N */
275 1.2 rillig case KEY_DOWN:
276 1.1 dholland if (cury < lines.num-1) {
277 1.1 dholland cury++;
278 1.1 dholland }
279 1.1 dholland if (curx > strlen(lines.v[cury])) {
280 1.1 dholland curx = strlen(lines.v[cury]);
281 1.1 dholland }
282 1.1 dholland if (scrolldown < cury - (LINES-2)) {
283 1.1 dholland scrolldown = cury - (LINES-2);
284 1.1 dholland }
285 1.1 dholland break;
286 1.1 dholland case 16: /* ^P */
287 1.2 rillig case KEY_UP:
288 1.1 dholland if (cury > 0) {
289 1.1 dholland cury--;
290 1.1 dholland }
291 1.1 dholland if (curx > strlen(lines.v[cury])) {
292 1.1 dholland curx = strlen(lines.v[cury]);
293 1.1 dholland }
294 1.1 dholland if (scrolldown > cury) {
295 1.1 dholland scrolldown = cury;
296 1.1 dholland }
297 1.1 dholland break;
298 1.1 dholland case '*':
299 1.1 dholland hinting = !hinting;
300 1.1 dholland break;
301 1.1 dholland case '~':
302 1.1 dholland done = true;
303 1.1 dholland break;
304 1.1 dholland default:
305 1.1 dholland if (isalpha(ch)) {
306 1.1 dholland if (!substitute(ch)) {
307 1.1 dholland if (curx < strlen(lines.v[cury])) {
308 1.1 dholland curx++;
309 1.1 dholland }
310 1.1 dholland if (curx==strlen(lines.v[cury]) && cury < lines.num-1) {
311 1.1 dholland curx=0;
312 1.1 dholland cury++;
313 1.1 dholland }
314 1.1 dholland }
315 1.1 dholland }
316 1.1 dholland else if (curx < strlen(lines.v[cury]) && ch==lines.v[cury][curx]) {
317 1.1 dholland curx++;
318 1.1 dholland if (curx==strlen(lines.v[cury]) && cury < lines.num-1) {
319 1.1 dholland curx=0;
320 1.1 dholland cury++;
321 1.1 dholland }
322 1.1 dholland }
323 1.1 dholland else {
324 1.1 dholland beep();
325 1.1 dholland }
326 1.1 dholland break;
327 1.1 dholland }
328 1.1 dholland }
329 1.1 dholland }
330 1.1 dholland
331 1.1 dholland ////////////////////////////////////////////////////////////
332 1.1 dholland
333 1.1 dholland int main(void) {
334 1.1 dholland stringarray_init(&lines);
335 1.1 dholland stringarray_init(&sollines);
336 1.1 dholland srandom(time(NULL));
337 1.1 dholland readquote();
338 1.1 dholland encode();
339 1.1 dholland opencurses();
340 1.1 dholland
341 1.2 rillig keypad(stdscr, TRUE);
342 1.1 dholland loop();
343 1.1 dholland
344 1.1 dholland closecurses();
345 1.1 dholland stringarray_cleanup(&sollines);
346 1.1 dholland stringarray_cleanup(&lines);
347 1.1 dholland return 0;
348 1.1 dholland }
349