]> git.gir.st - VIper.git/blob - viper.c
rename to 'viper', new makefile
[VIper.git] / viper.c
1 /*******************************************************************************
2 viper 0.2
3 By Tobias Girstmair, 2018 - 2019
4
5 ./viper 40x25
6 (see ./viper -h for full list of options)
7
8 KEYBINDINGS: - hjkl to move
9 - p to pause and resume
10 - r to restart
11 - q to quit
12 - (see `./viper -h' for all keybindings)
13
14 GNU GPL v3, see LICENSE or https://www.gnu.org/licenses/gpl-3.0.txt
15 *******************************************************************************/
16
17
18 #define _POSIX_C_SOURCE 2 /*for getopt and sigaction in c99, sigsetjmp*/
19 #include <setjmp.h>
20 #include <signal.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <sys/ioctl.h>
25 #include <sys/time.h>
26 #include <termios.h>
27 #include <time.h>
28 #include <unistd.h>
29
30 #include "viper.h"
31 #include "schemes.h"
32
33 #define MIN(a,b) (a>b?b:a)
34 #define MAX(a,b) (a>b?a:b)
35 #define CLAMP(a,m,M) (a<m?m:(a>M?M:a))
36 #define printm(n, s) for (int _loop = 0; _loop < n; _loop++) fputs (s, stdout)
37 #define print(str) fputs (str?str:"", stdout)
38 #define CTRL_ 0x1F &
39
40 #define BORDER(v,h) (g.b.t&1<<BONUS_WRAP?op.sch->permeable:op.sch->border) \
41 [BORDER_ ## v][BORDER_ ## h]
42 #define OPPOSITE(dir) ( \
43 dir == EAST ? WEST : \
44 dir == WEST ? EAST : \
45 dir == NORTH ? SOUTH : \
46 dir == SOUTH ? NORTH : -1)
47
48 #define COL_OFFSET 1
49 #define LINE_OFFSET 1
50 #define LINES_AFTER 1
51 #define CW op.sch->cell_width
52 #define DW op.sch->display_width
53
54 #define SPEEDUP_AFTER 100 /* increment speed every n points */
55 #define BONUS_INTERVAL 90 /* how often a bonus item is spawned */
56 #define BONUS_DURATION 15 /* how long one can catch the bonus */
57 #define BONUS_WARN 5 /* how long before the removal to warn */
58 #define BONUS_STOP_NUM 10 /* how many steps the time freezes */
59
60 struct game {
61 int w; /* field width */
62 int h; /* field height */
63 int d; /* direction the snake is looking */
64 int p; /* score */
65 float v; /* velocity in moves per second */
66 time_t t; /* time of game start */
67 struct snake* s; /* snek */
68 struct item* i; /* items (food, boni) */
69 struct bonus {
70 int t; /* bonus type (bitmask based on enum bonus_value) */
71 int s; /* steps left of time freeze bonus */
72 time_t n; /* time of next bonus item spawn */
73 time_t w; /* time of wall-wrap end */
74 } b; /* bonus-related values */
75 struct directions {
76 int h; /* write head */
77 int n; /* number of elements */
78 int c[16];
79 } k; /* ring buffer for direction events */
80 } g;
81
82 struct opt {
83 int l; /* initial snake length */
84 int s; /* initial snake speed */
85 struct scheme* sch;
86 } op;
87
88 sigjmp_buf game_over;
89
90 int main (int argc, char** argv) {
91 /* defaults: */
92 g.w = 30;
93 g.h = 20;
94 op.l = 10;
95 op.s = 8;
96 op.sch = &unic0de;
97
98 int optget;
99 opterr = 0; /* don't print message on unrecognized option */
100 while ((optget = getopt (argc, argv, "+s:l:h")) != -1) {
101 switch (optget) {
102 case 's':
103 op.s = atof(optarg);
104 if (op.s < 1) {
105 fprintf (stderr, "speed must be >= 1\n");
106 return 1;
107 }
108 break;
109 case 'l':
110 op.l = atoi(optarg);
111 if (op.s < 2 || op.s > g.w*g.h-1) {
112 fprintf (stderr, "length must be >= 2 and < W*H\n");
113 return 1;
114 }
115 break;
116 case 'h':
117 default:
118 fprintf (stderr, SHORTHELP LONGHELP, argv[0]);
119 return !(optget=='h');
120 }
121 } if (optind < argc) { /* parse Fieldspec */
122 int n = sscanf (argv[optind], "%dx%d", &g.w, &g.h);
123
124 if (n < 2) {
125 fprintf(stderr,"FIELDSIZE is WxH (width 'x' height)\n");
126 return 1;
127 }
128 }
129
130 clamp_fieldsize();
131
132 srand(time(0));
133 signal_setup();
134 screen_setup(1);
135 atexit (*quit);
136
137 char* end_screen_msg = "";
138 restart:
139 switch (sigsetjmp(game_over, 1)) {
140 case GAME_INIT:
141 case GAME_START:
142 viper();
143 break; /* function doesn't return, but `-Wextra' complains */
144 case GAME_OVER:
145 end_screen_msg = " GAME OVER ";
146 goto end_screen;
147 case GAME_WON:
148 end_screen_msg = "CONGRATULATIONS!";
149 goto end_screen;
150 end_screen:
151 timer_setup(0);
152 show_playfield();
153 for (;;) switch (end_screen(end_screen_msg)) {
154 case 'r': goto restart;
155 case 'q': goto quit;
156 case CTRL_'L':
157 screen_setup(1);
158 show_playfield();
159 break;
160 default: continue;
161 }
162 case GAME_EXIT:
163 goto quit;
164 }
165
166 quit:
167 return 0;
168 }
169
170 int viper(void) {
171 init_snake();
172 show_playfield ();
173 g.d = EAST;
174 g.v = op.s;
175
176 timer_setup(1);
177 g.t = time(NULL);
178 g.p = 0;
179 g.k.n = 0;
180 g.b.n = time(NULL) + BONUS_INTERVAL;
181 g.b.s = 0;
182 g.b.t = 0;
183
184 spawn_item(FOOD, rand() % NUM_FOODS, NULL);
185
186 for(;;) {
187 switch (getctrlseq()) {
188 case 'h': case CTRSEQ_CURSOR_LEFT: append_movement(WEST); break;
189 case 'j': case CTRSEQ_CURSOR_DOWN: append_movement(SOUTH);break;
190 case 'k': case CTRSEQ_CURSOR_UP: append_movement(NORTH);break;
191 case 'l': case CTRSEQ_CURSOR_RIGHT:append_movement(EAST); break;
192 case 'p': pause_game(); break;
193 case 'r': siglongjmp(game_over, GAME_START);
194 case 'q': siglongjmp(game_over, GAME_EXIT);
195 case'\\': {static int _;spawn_item(BONUS,_++%NUM_BONI,NULL);}break;
196 case CTRL_'L':
197 screen_setup(1);
198 show_playfield();
199 break;
200 case 0x02: /* STX; gets sent when returning from SIGALRM */
201 continue;
202 }
203
204 if (g.b.s) { /* highjack keyboard function for time freeze */
205 snake_advance();
206 if (!--g.b.s) timer_setup(1);/*turn back on afterwards*/
207 }
208 }
209
210 }
211
212 #define pop_dir() (g.k.n? g.k.c[(16+g.k.h-g.k.n--)%16] : NONE)
213 void snake_advance (void) {
214 int respawn = 0;
215 struct item* i; /*temporary item (defined here to respawn at the end) */
216 int new_dir = pop_dir();
217 /* switch direction if new one is in the buffer and it won't kill us: */
218 if (new_dir && g.d != OPPOSITE(new_dir)) g.d = new_dir;
219
220 int new_row = g.s->r +(g.d==SOUTH) -(g.d==NORTH);
221 int new_col = g.s->c +(g.d==EAST) -(g.d==WEST);
222
223 /* detect food hit and spawn a new food */
224 for (i = g.i; i; i = i->next) {
225 if (i->r == new_row && i->c == new_col) {
226 consume_item (i);
227
228 switch (i->t) {
229 case FOOD: respawn = 1; break;
230 case BONUS: remove_bonus(i); break;
231 }
232 break;
233 }
234 }
235
236 if (g.b.t & 1<<BONUS_WRAP) {
237 if (new_row >= g.h) new_row = 0;
238 if (new_col >= g.w) new_col = 0;
239 if (new_row < 0) new_row = g.h-1;
240 if (new_col < 0) new_col = g.w-1;
241 } else {
242 if (new_row >= g.h || new_col >= g.w || new_row < 0 || new_col < 0)
243 siglongjmp(game_over, GAME_OVER);
244 }
245
246 struct snake* new_head;
247 struct snake* new_tail; /* former second-to-last element */
248 for (new_tail = g.s; new_tail->next->next; new_tail = new_tail->next)
249 /*use the opportunity of looping to check if we eat ourselves:*/
250 if(new_tail->next->r == new_row && new_tail->next->c == new_col)
251 siglongjmp(game_over, GAME_OVER);
252 int old_tail[2] = {new_tail->next->r, new_tail->next->c};/*gets erased*/
253 new_head = new_tail->next; /* reuse element instead of malloc() */
254 new_tail->next = NULL;
255
256 new_head->r = new_row;
257 new_head->c = new_col;
258 new_head->next = g.s;
259
260 g.s = new_head;
261
262 /* bonus mode(s) still active? */
263 if (g.b.t&1<<BONUS_WRAP && g.b.w < time(NULL)) {
264 g.b.t &= ~(1<<BONUS_WRAP);
265 show_playfield();
266 }
267
268 if (respawn) spawn_item(FOOD, rand() % NUM_FOODS, i);
269 draw_sprites (old_tail[0], old_tail[1]);
270 }
271
272 void spawn_item (int type, int value, struct item* p_item) {
273 int row, col;
274 char occupied[g.w][g.h]; int snake_len = 0;
275 memset(*occupied, 0, g.w*g.h);
276 try_again:
277 row = rand() % g.h;
278 col = rand() % g.w;
279 if (snake_len >= g.w*g.h-1) siglongjmp(game_over, GAME_WON);
280 /* loop through snake to check if we aren't on it */
281 if (occupied[row][col]) goto try_again; /* shortcut */
282 for (struct snake* s = g.s; s; s = s->next)
283 if (s->r == row && s->c == col) {
284 occupied[s->r][s->c] = 1; snake_len++;
285 goto try_again;
286 }
287
288 /* if we got a item buffer reuse it, otherwise create a new one: */
289 struct item* new_item;
290 if (p_item) new_item = p_item;
291 else new_item = malloc (sizeof(struct item));
292
293 new_item->r = row;
294 new_item->c = col;
295 new_item->t = type;
296 new_item->v = value;
297 new_item->s = time(0);
298 if (g.i) g.i->prev = new_item;
299 new_item->next = g.i;
300 new_item->prev = NULL;
301
302 g.i = new_item;
303 }
304
305 void consume_item (struct item* i) {
306 int old_score = g.p;
307
308 switch (i->t) {
309 case FOOD:
310 switch (i->v) {
311 case FOOD_PEAR:
312 case FOOD_WMELON:
313 case FOOD_BANANA:
314 case FOOD_KIWI:
315 g.p += 5;
316 break;
317 case FOOD_APPLER:
318 case FOOD_CHERRY:
319 g.p += 10;
320 break;
321 case FOOD_AVOCADO:
322 g.p += 20;
323 break;
324 }
325 snake_append(&g.s, -1, -1);
326 break; /* will be reused as the head before it is drawn */
327 case BONUS:
328 switch (i->v) {
329 case BONUS_SNIP:
330 for (int i = 5; i && g.s->next->next; i--) {
331 struct snake* p = g.s;
332 while (p->next->next) p = p->next;
333 free (p->next);
334 p->next = NULL;
335 }
336 show_playfield();
337 break;
338 case BONUS_GROW:
339 for (int i = 5; i; i--) snake_append(&g.s, -1, -1);
340 break;
341 case BONUS_SLOW:
342 if (g.v > 1) g.v--;
343 timer_setup(1);
344 break;
345 case BONUS_FAST:
346 g.v++;
347 timer_setup(1);
348 break;
349 case BONUS_WRAP:
350 g.b.w = time(NULL)+30;
351 g.b.t |= 1<<BONUS_WRAP;
352 show_playfield();
353 break;
354 case BONUS_STOP:
355 g.b.s = BONUS_STOP_NUM; /* stop the time for 5 steps */
356 timer_setup(0);
357 break;
358 }
359 g.b.n = time(NULL) + BONUS_INTERVAL; /*next bonus in x seconds*/
360 break;
361 }
362
363 if (i->next) i->next->prev = i->prev;
364 if (i->prev) i->prev->next = i->next;
365 else g.i = i->next;
366
367 /* snake speedup every 100 points: */
368 if (g.p/SPEEDUP_AFTER - old_score/SPEEDUP_AFTER) g.v++, timer_setup(1);
369 }
370
371 void spawn_bonus(void) {
372 for (struct item* i = g.i; i; i = i->next)
373 if (i->t == BONUS) { /* bonus already there */
374 if (((i->s+BONUS_DURATION)-time(NULL)) < 0) {
375 remove_bonus(i); /* remove if over lifetime */
376 }
377 return;
378 }
379 if (g.b.n < time(NULL)) { /* time to spawn item: */
380 spawn_item(BONUS, rand() % NUM_BONI, NULL);
381 g.b.n = time(NULL) + BONUS_INTERVAL + BONUS_DURATION;
382 }
383 }
384
385 void show_playfield (void) {
386 move_ph (0,0);
387
388 /* top border */
389 print(BORDER(T,L));
390 printm (g.w, BORDER(T,C));
391 printf ("%s\n", BORDER(T,R));
392
393 /* main area */
394 for (int row = 0; row < g.h; row++)
395 printf ("%s%*s%s\n", BORDER(C,L), CW*g.w, "", BORDER(C,R));
396
397 /* bottom border */
398 print(BORDER(B,L));
399 printm (g.w, BORDER(B,C));
400 print (BORDER(B,R));
401
402 draw_sprites (-1,-1);
403 }
404
405 void draw_sprites (int erase_r, int erase_c) {
406 /* erase old tail, if any */
407 if (erase_r >= 0 && erase_c >= 0) {
408 move_ph (erase_r+LINE_OFFSET, erase_c*CW+COL_OFFSET);
409 printm (CW, " ");
410 }
411
412 /* print snake */
413 struct snake* last = NULL;
414 int color = 2;
415 for (struct snake* s = g.s; s; s = s->next) {
416 if (s->r < 0 || s->c < 0) continue; /*can't draw outside term.*/
417 move_ph (s->r+LINE_OFFSET, s->c*CW+COL_OFFSET);
418
419 int predecessor = (last==NULL)?NONE:
420 (last->r < s->r) ? NORTH:
421 (last->r > s->r) ? SOUTH:
422 (last->c > s->c) ? EAST:
423 (last->c < s->c) ? WEST:NONE;
424 int successor = (s->next == NULL)?NONE:
425 (s->next->r < s->r) ? NORTH:
426 (s->next->r > s->r) ? SOUTH:
427 (s->next->c > s->c) ? EAST:
428 (s->next->c < s->c) ? WEST:NONE;
429
430 printf ("\033[%sm", op.sch->color[color]);
431 print (op.sch->snake[predecessor][successor]);
432 printf ("\033[0m");
433 last = s;
434 color = !color;
435 }
436
437 /* print item queue */
438 for (struct item* i = g.i; i; i = i->next) {
439 move_ph (i->r+LINE_OFFSET, i->c*CW+COL_OFFSET);
440 if (i->t == FOOD) print(op.sch->food[i->v]);
441 else if (i->t == BONUS) {
442 if (i->s + BONUS_DURATION-BONUS_WARN < time(NULL))
443 printf("\033[5m%s\033[25m", op.sch->boni[i->v]);
444 else print(op.sch->boni[i->v]);
445 }
446 }
447
448 /* print score */
449 int score_width = g.p > 9999?6:4;
450 move_ph (0, (g.w*CW-score_width)/2-COL_OFFSET);
451 printf ("%s %0*d %s", BORDER(S,L), score_width, g.p, BORDER(S,R));
452 }
453
454 void pause_game (void) {
455 time_t pause_start = time(NULL);
456 time_t pause_diff;
457 timer_setup(0);
458
459 move_ph (g.h/2+LINE_OFFSET, g.w*CW/2);
460 printf ("\033[5;7m PAUSE \033[0m"); /* blinking reverse text */
461 if (getchar() == 'q')
462 siglongjmp(game_over, GAME_EXIT);
463
464 /* update all timers, so bonus items don't get out of sync: */
465 pause_diff = time(NULL) - pause_start;
466 g.b.n += pause_diff;
467 for (struct item* i = g.i; i; i = i->next)
468 i->s += pause_diff;
469
470 show_playfield();
471 timer_setup(1);
472 }
473
474 #define MOVE_POPUP(WIDTH, LINE) \
475 move_ph(g.h/2+LINE_OFFSET-1+LINE,(g.w*DW-WIDTH)/2)
476 int end_screen(char* message) {
477 int msg_w = strlen(message);
478 MOVE_POPUP(msg_w, -1);
479 print(BORDER(T,L));
480 printm ((msg_w+2)/DW, BORDER(T,C));
481 print (BORDER(T,R));
482
483 MOVE_POPUP(msg_w, 0);
484 printf("%s %s %s", BORDER(C,L), message, BORDER(C,R));
485 MOVE_POPUP(msg_w, 1);
486 printf("%s `r' restart%*s%s", BORDER(C,L), msg_w-10, "", BORDER(C,R));
487 MOVE_POPUP(msg_w, 2);
488 printf("%s `q' quit%*s%s", BORDER(C,L), msg_w-7, "", BORDER(C,R));
489
490 MOVE_POPUP(msg_w, 3);
491 print(BORDER(B,L));
492 printm ((msg_w+2)/DW, BORDER(B,C));
493 print (BORDER(B,R));
494 fflush(stdout);
495
496 return getctrlseq();
497 }
498
499 void snake_append (struct snake** s, int row, int col) {
500 struct snake* new = malloc (sizeof(struct snake));
501 new->r = row;
502 new->c = col;
503 new->next = NULL;
504
505 if (*s) {
506 struct snake* p = *s;
507 while (p->next) p = p->next;
508 p->next = new;
509 } else {
510 *s = new;
511 }
512 }
513
514 void remove_bonus (struct item* i) {
515 if (i->next) i->next->prev = i->prev;
516 if (i->prev) i->prev->next = i->next;
517 else g.i = i->next;
518
519 draw_sprites (i->r, i->c); /* overwrite sprite */
520
521 free(i);
522 }
523
524 #define free_ll(head) do{ \
525 while (head) { \
526 void* tmp = head; \
527 head = head->next; \
528 free(tmp); \
529 } \
530 head = NULL; \
531 }while(0)
532
533 void init_snake() {
534 free_ll(g.s);
535 free_ll(g.i);
536
537 int direction = WEST;
538 int r = g.h/2;
539 int c = g.w/2;
540 int min_r = 0;
541 int max_r = g.h-1;
542 int min_c = 0;
543 int max_c = g.w-1;
544 for (int i = 0; i < op.l; i++) {
545 switch (direction) {
546 case NORTH: r--; if(r <= min_r){direction=EAST; min_r++;}break;
547 case EAST: c++; if(c >= max_c){direction=SOUTH;max_c--;}break;
548 case SOUTH: r++; if(r >= max_r){direction=WEST; max_r--;}break;
549 case WEST: c--; if(c <= min_c){direction=NORTH;min_c++;}break;
550 }
551
552 snake_append(&g.s, r, c);
553 }
554 }
555
556 void quit (void) {
557 screen_setup(0);
558 free_ll(g.s);
559 free_ll(g.i);
560 }
561
562 enum esc_states {
563 START,
564 ESC_SENT,
565 CSI_SENT,
566 MOUSE_EVENT,
567 };
568 int getctrlseq (void) {
569 int c;
570 int state = START;
571 while ((c = getchar()) != EOF) {
572 switch (state) {
573 case START:
574 switch (c) {
575 case '\033': state=ESC_SENT; break;
576 default: return c;
577 }
578 break;
579 case ESC_SENT:
580 switch (c) {
581 case '[': state=CSI_SENT; break;
582 default: return CTRSEQ_INVALID;
583 }
584 break;
585 case CSI_SENT:
586 switch (c) {
587 case 'A': return CTRSEQ_CURSOR_UP;
588 case 'B': return CTRSEQ_CURSOR_DOWN;
589 case 'C': return CTRSEQ_CURSOR_RIGHT;
590 case 'D': return CTRSEQ_CURSOR_LEFT;
591 default: return CTRSEQ_INVALID;
592 }
593 break;
594 default:
595 return CTRSEQ_INVALID;
596 }
597 }
598 return 2;
599 }
600
601 void append_movement (int dir) {
602 if (g.k.n > 15) return; /* no space in buffer */
603 if (g.k.n && g.k.c[(16+g.k.h-1)%16] == dir)
604 return; /* don't add the same direction twice */
605
606 g.k.c[g.k.h] = dir;
607 g.k.n++;
608 g.k.h = (g.k.h+1) % 16;
609 }
610
611 void move_ph (int line, int col) {
612 /* move printhead to zero-indexed position */
613 printf ("\033[%d;%dH", line+1, col+1);
614 }
615
616 void clamp_fieldsize (void) {
617 /* clamp field size to terminal size and mouse maximum: */
618 struct winsize w;
619 ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
620
621 if (g.w < 10) g.w = 10;
622 if (g.h < 10) g.h = 10;
623
624 if (COL_OFFSET + g.w*CW + COL_OFFSET > w.ws_col)
625 g.w = (w.ws_col - 2*COL_OFFSET) / DW;
626 if (LINE_OFFSET + g.h + LINES_AFTER > w.ws_row)
627 g.h = w.ws_row - (LINE_OFFSET+LINES_AFTER);
628 }
629
630 void timer_setup (int enable) {
631 static struct itimerval tbuf;
632 tbuf.it_interval.tv_sec = 0;
633 tbuf.it_interval.tv_usec = (1000000/g.v)-1; /*WARN: 1 <= g.v <= 999999*/
634
635 if (enable) {
636 tbuf.it_value.tv_sec = tbuf.it_interval.tv_sec;
637 tbuf.it_value.tv_usec = tbuf.it_interval.tv_usec;
638 } else {
639 tbuf.it_value.tv_sec = 0;
640 tbuf.it_value.tv_usec = 0;
641 }
642
643 if ( setitimer(ITIMER_REAL, &tbuf, NULL) == -1 ) {
644 perror("setitimer");
645 exit(1);
646 }
647
648 }
649
650 void signal_setup (void) {
651 struct sigaction saction;
652
653 saction.sa_handler = signal_handler;
654 sigemptyset(&saction.sa_mask);
655 saction.sa_flags = 0;
656 if (sigaction(SIGALRM, &saction, NULL) < 0 ) {
657 perror("SIGALRM");
658 exit(1);
659 }
660
661 if (sigaction(SIGINT, &saction, NULL) < 0 ) {
662 perror ("SIGINT");
663 exit (1);
664 }
665
666 if (sigaction(SIGTSTP, &saction, NULL) < 0 ) {
667 perror ("SIGTSTP");
668 exit (1);
669 }
670
671 if (sigaction(SIGCONT, &saction, NULL) < 0 ) {
672 perror ("SIGCONT");
673 exit (1);
674 }
675 }
676
677 void signal_handler (int signum) {
678 switch (signum) {
679 case SIGALRM:
680 snake_advance();
681 spawn_bonus();
682 break;
683 case SIGINT:
684 exit(128+SIGINT);
685 case SIGTSTP:
686 screen_setup(0);
687 signal(SIGTSTP, SIG_DFL); /* NOTE: assumes SysV semantics! */
688 raise(SIGTSTP);
689 break;
690 case SIGCONT:
691 screen_setup(1);
692 show_playfield();
693 }
694 }
695
696 void screen_setup (int enable) {
697 if (enable) {
698 raw_mode(1);
699 printf ("\033[s\033[?47h"); /* save cursor, alternate screen */
700 printf ("\033[H\033[J"); /* reset cursor, clear screen */
701 printf ("\033[?25l"); /* hide cursor */
702 } else {
703 printf ("\033[?25h"); /* show cursor */
704 printf ("\033[?47l\033[u"); /* primary screen, restore cursor */
705 raw_mode(0);
706 }
707 }
708
709 /* http://users.csc.calpoly.edu/~phatalsk/357/lectures/code/sigalrm.c */
710 void raw_mode(int enable) {
711 static struct termios saved_term_mode;
712 struct termios raw_term_mode;
713
714 if (enable) {
715 if (saved_term_mode.c_lflag == 0)/*don't overwrite stored mode*/
716 tcgetattr(STDIN_FILENO, &saved_term_mode);
717 raw_term_mode = saved_term_mode;
718 raw_term_mode.c_lflag &= ~(ICANON | ECHO);
719 raw_term_mode.c_cc[VMIN] = 1 ;
720 raw_term_mode.c_cc[VTIME] = 0;
721 tcsetattr(STDIN_FILENO, TCSAFLUSH, &raw_term_mode);
722 } else {
723 tcsetattr(STDIN_FILENO, TCSAFLUSH, &saved_term_mode);
724 }
725 }
Imprint / Impressum