Home | History | Annotate | Line # | Download | only in cgram
cgram.c revision 1.1
      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 used[26];
    124  1.1  dholland    for (int i=0; i<26; i++) used[i] = 0;
    125  1.1  dholland 
    126  1.1  dholland    int key[26];
    127  1.1  dholland    int keypos=0;
    128  1.1  dholland    while (keypos < 26) {
    129  1.1  dholland       int c = random()%26;
    130  1.1  dholland       if (used[c]) continue;
    131  1.1  dholland       key[keypos++] = c;
    132  1.1  dholland       used[c] = 1;
    133  1.1  dholland    }
    134  1.1  dholland 
    135  1.1  dholland    for (int y=0; y<lines.num; y++) {
    136  1.1  dholland       for (unsigned x=0; lines.v[y][x]; x++) {
    137  1.1  dholland 	 if (islower((unsigned char)lines.v[y][x])) {
    138  1.1  dholland 	    int q = lines.v[y][x]-'a';
    139  1.1  dholland 	    lines.v[y][x] = 'a'+key[q];
    140  1.1  dholland 	 }
    141  1.1  dholland 	 if (isupper((unsigned char)lines.v[y][x])) {
    142  1.1  dholland 	    int q = lines.v[y][x]-'A';
    143  1.1  dholland 	    lines.v[y][x] = 'A'+key[q];
    144  1.1  dholland 	 }
    145  1.1  dholland       }
    146  1.1  dholland    }
    147  1.1  dholland }
    148  1.1  dholland 
    149  1.1  dholland static int substitute(int ch) {
    150  1.1  dholland    assert(cury>=0 && cury<lines.num);
    151  1.1  dholland    if (curx >= strlen(lines.v[cury])) {
    152  1.1  dholland       beep();
    153  1.1  dholland       return -1;
    154  1.1  dholland    }
    155  1.1  dholland 
    156  1.1  dholland    int och = lines.v[cury][curx];
    157  1.1  dholland    if (!isalpha((unsigned char)och)) {
    158  1.1  dholland       beep();
    159  1.1  dholland       return -1;
    160  1.1  dholland    }
    161  1.1  dholland 
    162  1.1  dholland    int loch = tolower((unsigned char)och);
    163  1.1  dholland    int uoch = toupper((unsigned char)och);
    164  1.1  dholland    int lch = tolower((unsigned char)ch);
    165  1.1  dholland    int uch = toupper((unsigned char)ch);
    166  1.1  dholland 
    167  1.1  dholland    for (int y=0; y<lines.num; y++) {
    168  1.1  dholland       for (unsigned x=0; lines.v[y][x]; x++) {
    169  1.1  dholland 	 if (lines.v[y][x]==loch) {
    170  1.1  dholland 	    lines.v[y][x] = lch;
    171  1.1  dholland 	 }
    172  1.1  dholland 	 else if (lines.v[y][x]==uoch) {
    173  1.1  dholland 	    lines.v[y][x] = uch;
    174  1.1  dholland 	 }
    175  1.1  dholland 	 else if (lines.v[y][x]==lch) {
    176  1.1  dholland 	    lines.v[y][x] = loch;
    177  1.1  dholland 	 }
    178  1.1  dholland 	 else if (lines.v[y][x]==uch) {
    179  1.1  dholland 	    lines.v[y][x] = uoch;
    180  1.1  dholland 	 }
    181  1.1  dholland       }
    182  1.1  dholland    }
    183  1.1  dholland    return 0;
    184  1.1  dholland }
    185  1.1  dholland 
    186  1.1  dholland ////////////////////////////////////////////////////////////
    187  1.1  dholland 
    188  1.1  dholland static void redraw(void) {
    189  1.1  dholland    erase();
    190  1.1  dholland    bool won = true;
    191  1.1  dholland    for (int i=0; i<LINES-1; i++) {
    192  1.1  dholland       move(i, 0);
    193  1.1  dholland       int ln = i+scrolldown;
    194  1.1  dholland       if (ln < lines.num) {
    195  1.1  dholland 	 for (unsigned j=0; lines.v[i][j]; j++) {
    196  1.1  dholland 	    int ch = lines.v[i][j];
    197  1.1  dholland 	    if (ch != sollines.v[i][j] && isalpha((unsigned char)ch)) {
    198  1.1  dholland 	       won = false;
    199  1.1  dholland 	    }
    200  1.1  dholland 	    bool bold=false;
    201  1.1  dholland 	    if (hinting && ch==sollines.v[i][j] &&
    202  1.1  dholland 		isalpha((unsigned char)ch)) {
    203  1.1  dholland 	       bold = true;
    204  1.1  dholland 	       attron(A_BOLD);
    205  1.1  dholland 	    }
    206  1.1  dholland 	    addch(lines.v[i][j]);
    207  1.1  dholland 	    if (bold) {
    208  1.1  dholland 	       attroff(A_BOLD);
    209  1.1  dholland 	    }
    210  1.1  dholland 	 }
    211  1.1  dholland       }
    212  1.1  dholland       clrtoeol();
    213  1.1  dholland    }
    214  1.1  dholland 
    215  1.1  dholland    move(LINES-1, 0);
    216  1.1  dholland    if (won) {
    217  1.1  dholland       addstr("*solved* ");
    218  1.1  dholland    }
    219  1.1  dholland    addstr("~ to quit, * to cheat, ^pnfb to move");
    220  1.1  dholland 
    221  1.1  dholland    move(LINES-1, 0);
    222  1.1  dholland 
    223  1.1  dholland    move(cury-scrolldown, curx);
    224  1.1  dholland 
    225  1.1  dholland    refresh();
    226  1.1  dholland }
    227  1.1  dholland 
    228  1.1  dholland static void opencurses(void) {
    229  1.1  dholland     initscr();
    230  1.1  dholland     cbreak();
    231  1.1  dholland     noecho();
    232  1.1  dholland }
    233  1.1  dholland 
    234  1.1  dholland static void closecurses(void) {
    235  1.1  dholland    endwin();
    236  1.1  dholland }
    237  1.1  dholland 
    238  1.1  dholland ////////////////////////////////////////////////////////////
    239  1.1  dholland 
    240  1.1  dholland static void loop(void) {
    241  1.1  dholland    bool done=false;
    242  1.1  dholland    while (!done) {
    243  1.1  dholland       redraw();
    244  1.1  dholland       int ch = getch();
    245  1.1  dholland       switch (ch) {
    246  1.1  dholland        case 1: /* ^A */
    247  1.1  dholland 	curx=0;
    248  1.1  dholland 	break;
    249  1.1  dholland        case 2: /* ^B */
    250  1.1  dholland 	if (curx > 0) {
    251  1.1  dholland 	   curx--;
    252  1.1  dholland 	}
    253  1.1  dholland 	else if (cury > 0) {
    254  1.1  dholland 	   cury--;
    255  1.1  dholland 	   curx = strlen(lines.v[cury]);
    256  1.1  dholland 	}
    257  1.1  dholland 	break;
    258  1.1  dholland        case 5: /* ^E */
    259  1.1  dholland 	curx = strlen(lines.v[cury]);
    260  1.1  dholland 	break;
    261  1.1  dholland        case 6: /* ^F */
    262  1.1  dholland 	if (curx < strlen(lines.v[cury])) {
    263  1.1  dholland 	   curx++;
    264  1.1  dholland 	}
    265  1.1  dholland 	else if (cury < lines.num - 1) {
    266  1.1  dholland 	   cury++;
    267  1.1  dholland 	   curx = 0;
    268  1.1  dholland 	}
    269  1.1  dholland 	break;
    270  1.1  dholland        case 12: /* ^L */
    271  1.1  dholland 	clear();
    272  1.1  dholland 	break;
    273  1.1  dholland        case 14: /* ^N */
    274  1.1  dholland 	if (cury < lines.num-1) {
    275  1.1  dholland 	   cury++;
    276  1.1  dholland 	}
    277  1.1  dholland 	if (curx > strlen(lines.v[cury])) {
    278  1.1  dholland 	   curx =  strlen(lines.v[cury]);
    279  1.1  dholland 	}
    280  1.1  dholland 	if (scrolldown < cury - (LINES-2)) {
    281  1.1  dholland 	   scrolldown = cury - (LINES-2);
    282  1.1  dholland 	}
    283  1.1  dholland 	break;
    284  1.1  dholland        case 16: /* ^P */
    285  1.1  dholland 	if (cury > 0) {
    286  1.1  dholland 	   cury--;
    287  1.1  dholland 	}
    288  1.1  dholland 	if (curx > strlen(lines.v[cury])) {
    289  1.1  dholland 	   curx = strlen(lines.v[cury]);
    290  1.1  dholland 	}
    291  1.1  dholland 	if (scrolldown > cury) {
    292  1.1  dholland 	   scrolldown = cury;
    293  1.1  dholland 	}
    294  1.1  dholland 	break;
    295  1.1  dholland        case '*':
    296  1.1  dholland 	hinting = !hinting;
    297  1.1  dholland 	break;
    298  1.1  dholland        case '~':
    299  1.1  dholland 	done = true;
    300  1.1  dholland 	break;
    301  1.1  dholland        default:
    302  1.1  dholland 	if (isalpha(ch)) {
    303  1.1  dholland 	   if (!substitute(ch)) {
    304  1.1  dholland 	      if (curx < strlen(lines.v[cury])) {
    305  1.1  dholland 		 curx++;
    306  1.1  dholland 	      }
    307  1.1  dholland 	      if (curx==strlen(lines.v[cury]) && cury < lines.num-1) {
    308  1.1  dholland 		 curx=0;
    309  1.1  dholland 		 cury++;
    310  1.1  dholland 	      }
    311  1.1  dholland 	   }
    312  1.1  dholland 	}
    313  1.1  dholland 	else if (curx < strlen(lines.v[cury]) && ch==lines.v[cury][curx]) {
    314  1.1  dholland 	   curx++;
    315  1.1  dholland 	   if (curx==strlen(lines.v[cury]) && cury < lines.num-1) {
    316  1.1  dholland 	      curx=0;
    317  1.1  dholland 	      cury++;
    318  1.1  dholland 	   }
    319  1.1  dholland 	}
    320  1.1  dholland 	else {
    321  1.1  dholland 	   beep();
    322  1.1  dholland 	}
    323  1.1  dholland 	break;
    324  1.1  dholland       }
    325  1.1  dholland    }
    326  1.1  dholland }
    327  1.1  dholland 
    328  1.1  dholland ////////////////////////////////////////////////////////////
    329  1.1  dholland 
    330  1.1  dholland int main(void) {
    331  1.1  dholland    stringarray_init(&lines);
    332  1.1  dholland    stringarray_init(&sollines);
    333  1.1  dholland    srandom(time(NULL));
    334  1.1  dholland    readquote();
    335  1.1  dholland    encode();
    336  1.1  dholland    opencurses();
    337  1.1  dholland 
    338  1.1  dholland    loop();
    339  1.1  dholland 
    340  1.1  dholland    closecurses();
    341  1.1  dholland    stringarray_cleanup(&sollines);
    342  1.1  dholland    stringarray_cleanup(&lines);
    343  1.1  dholland    return 0;
    344  1.1  dholland }
    345