]>
git.gir.st - minesVIiper.git/blob - mines.c
1 /*******************************************************************************
3 By Tobias Girstmair, 2015 - 2018
6 (see ./minesviiper -h for full list of options)
8 MOUSE MODE: - left click to open and choord
9 - right click to flag/unflag
10 VI MODE: - hjkl to move cursor left/down/up/right
11 - o/space to open and choord
13 - (see `./minesviiper -h' for all keybindings)
15 GNU GPL v3, see LICENSE or https://www.gnu.org/licenses/gpl-3.0.txt
16 *******************************************************************************/
19 #define _POSIX_C_SOURCE 2 /*for getopt, sigaction in c99*/
24 #include <sys/ioctl.h>
39 #define MIN(a,b) (a>b?b:a)
40 #define MAX(a,b) (a>b?a:b)
41 #define CLAMP(a,m,M) (a<m?m:(a>M?M:a))
42 #define printm(n, s) for (int _loop = 0; _loop < n; _loop++) fputs (s, stdout)
43 #define print(str) fputs (str?str:"", stdout)
44 #define EMOT(e) op.scheme->emoticons[EMOT_ ## e]
45 #define BORDER(l, c) op.scheme->border[B_ ## l][B_ ## c]
46 #define CW op.scheme->cell_width /* for brevity */
52 int main (int argc
, char** argv
) {
57 op
.scheme
= &symbols_mono
;
61 opterr
= 0; /* don't print message on unrecognized option */
62 while ((optget
= getopt (argc
, argv
, "+hnfqcdx")) != -1) {
64 case 'n': op
.mode
= NOFLAG
; break;
65 case 'f': op
.mode
= FLAG
; break; /*default*/
66 case 'q': op
.mode
= QUESM
; break;
67 case 'c': op
.scheme
= &symbols_col1
; break;
68 case 'd': op
.scheme
= &symbols_doublewidth
; break;
71 fprintf (stderr
, SHORTHELP LONGHELP
, argv
[0]);
72 return !(optget
=='h');
74 } if (optind
< argc
) { /* parse Fieldspec */
75 if (parse_fieldspec (argv
[optind
])) {
76 fprintf (stderr
, "FIELDSPEC: WxH[xM]"
77 " (width 'x' height 'x' mines)\n");
86 /* check boundaries */
87 if (f
.m
> (f
.w
-1) * (f
.h
-1)) {
88 move_ph (LINE_OFFSET
+f
.h
+LINES_AFTER
-1,0);
89 f
.m
= (f
.w
-1) * (f
.h
-1);
90 fprintf (stdout
, "too many mines. reduced to %d.\r\n", f
.m
); //TODO: doesn't show up
95 switch (minesviiper()) {
96 case GAME_NEW
: goto newgame
;
97 case GAME_WON
: g
.o
= GAME_WON
; break;
98 case GAME_LOST
:g
.o
= GAME_LOST
;break;
101 timer_setup(0); /* stop timer */
102 show_minefield (SHOWMINES
);
105 unsigned char mouse
[3];
106 gotaction
= getch(mouse
);
107 if (gotaction
==CTRSEQ_MOUSE_LEFT
&& clicked_emoticon(mouse
)) {
110 } else if (gotaction
== 'r') {
113 } else if (gotaction
== 'q') {
122 int minesviiper(void) {
123 f
.c
= alloc_array (f
.h
, f
.w
);
124 g
= (const struct game
){0}; /* reset all game-specific parameters */
125 for (int i
=26; i
; g
.m
[--i
].l
= -1);
127 show_minefield (NORMAL
);
132 action
= getch_wrapper();
135 if (g
.s
== MODE_OPEN
||
136 f
.c
[g
.p
[0]][g
.p
[1]].o
== OPENED
) {
137 switch (do_uncover(&(g
.n
))) {
138 case GAME_LOST
: return GAME_LOST
;
139 case GAME_WON
: return GAME_WON
;
141 } else if (g
.s
== MODE_FLAG
) {
142 flag_square (g
.p
[0], g
.p
[1]);
143 } else if (g
.s
== MODE_QUESM
) {
144 quesm_square (g
.p
[0], g
.p
[1]); }
147 g
.s
= (g
.s
+1)%(op
.mode
+1);
148 show_minefield (g
.c
?SHOWMINES
:NORMAL
);
150 case CTRSEQ_MOUSE_LEFT
:
152 switch (do_uncover(&(g
.n
))) {
153 case GAME_LOST
: return GAME_LOST
;
154 case GAME_WON
: return GAME_WON
;
157 case CTRSEQ_MOUSE_RIGHT
:
158 case 'i': flag_square (g
.p
[0], g
.p
[1]); break;
159 case '?':quesm_square (g
.p
[0], g
.p
[1]); break;
160 case CTRSEQ_CURSOR_LEFT
:
161 case 'h': move_hi (g
.p
[0], g
.p
[1]-1 ); break;
162 case CTRSEQ_CURSOR_DOWN
:
163 case 'j': move_hi (g
.p
[0]+1, g
.p
[1] ); break;
164 case CTRSEQ_CURSOR_UP
:
165 case 'k': move_hi (g
.p
[0]-1, g
.p
[1] ); break;
166 case CTRSEQ_CURSOR_RIGHT
:
167 case 'l': move_hi (g
.p
[0], g
.p
[1]+1 ); break;
168 case 'w': to_next_boundary (g
.p
[0], g
.p
[1], '>'); break;
169 case 'b': to_next_boundary (g
.p
[0], g
.p
[1], '<'); break;
170 case 'u': to_next_boundary (g
.p
[0], g
.p
[1], '^'); break;
171 case 'd': to_next_boundary (g
.p
[0], g
.p
[1], 'v'); break;
172 case '0': /* fallthrough */
173 case '^': move_hi (g
.p
[0], 0 ); break;
174 case '$': move_hi (g
.p
[0], f
.w
-1 ); break;
175 case 'g': move_hi (0, g
.p
[1] ); break;
176 case 'G': move_hi (f
.h
-1, g
.p
[1] ); break;
177 case 'z': move_hi (f
.h
/2, f
.w
/2); break;
179 action
= tolower(getch_wrapper());
180 if (action
< 'a' || action
> 'z') break;/*out of bound*/
181 g
.m
[action
-'a'].l
= g
.p
[0];
182 g
.m
[action
-'a'].c
= g
.p
[1];
184 case'\'': /* fallthrough */
186 action
= tolower(getch_wrapper());
187 if (action
< 'a' || action
> 'z' /* out of bound or */
188 || g
.m
[action
-'a'].l
== -1) break; /* unset */
189 move_hi (g
.m
[action
-'a'].l
, g
.m
[action
-'a'].c
);
191 case WRAPPER_EMOTICON
:
192 case 'r': /* start a new game */
197 case '\014': /* Ctrl-L -- redraw */
198 show_minefield (NORMAL
);
201 if (g
.n
== GAME_NEW
) break; /* must open a cell first */
202 show_minefield (g
.c
?NORMAL
:SHOWMINES
);
215 /* I haven't won as long as a cell exists, that
216 - I haven't opened, and
218 int everything_opened (void) {
219 for (int row
= 0; row
< f
.h
; row
++)
220 for (int col
= 0; col
< f
.w
; col
++)
221 if (f
.c
[row
][col
].o
== CLOSED
&&
222 f
.c
[row
][col
].m
== NO_MINE
) return 0;
226 int wait_mouse_up (int l
, int c
) {
227 unsigned char mouse2
[3];
232 move_ph (1, field2screen_c (f
.w
/2)-1); print (EMOT(OHH
));
234 if (!(l
< 0 || l
>= f
.h
|| c
< 0 || c
>= f
.w
)) {
235 /* show a pushed-in button if cursor is on minefield */
236 move_ph (l
+LINE_OFFSET
, field2screen_c(c
));
237 fputs (op
.scheme
->mouse_highlight
, stdout
);
241 if (getctrlseq (mouse2
) == CTRSEQ_MOUSE
) {
242 /* ignore mouse wheel events: */
243 if (mouse2
[0] & 0x40) continue;
245 else if (mouse2
[0]&3 == 3) level
--; /* release event */
246 else level
++; /* another button pressed */
250 move_ph (1, field2screen_c (f
.w
/2)-1); print (get_emoticon());
252 if (!(l
< 0 || l
>= f
.h
|| c
< 0 || c
>= f
.w
)) {
253 partial_show_minefield (l
, c
, NORMAL
);
255 c2
= screen2field_c(mouse2
[1]);
256 l2
= screen2field_l(mouse2
[2]);
257 return ((l2
== l
) && (c2
== c
));
260 int choord_square (int line
, int col
) {
261 for (int l
= MAX(line
-1, 0); l
<= MIN(line
+1, f
.h
-1); l
++) {
262 for (int c
= MAX(col
-1, 0); c
<= MIN(col
+1, f
.w
-1); c
++) {
263 if (f
.c
[l
][c
].f
!= FLAG
) {
264 if (uncover_square (l
, c
))
273 int uncover_square (int l
, int c
) {
274 f
.c
[l
][c
].o
= OPENED
;
275 f
.c
[l
][c
].f
= NOFLAG
; /*must not be QUESM, otherwise rendering issues*/
276 partial_show_minefield (l
, c
, NORMAL
);
279 f
.c
[l
][c
].m
= DEATH_MINE
;
283 /* check for chording */
284 if (f
.c
[l
][c
].n
== 0) {
285 for (int choord_l
= -1; choord_l
<= 1; choord_l
++) {
286 for (int choord_c
= -1; choord_c
<= 1; choord_c
++) {
287 int newl
= l
+ choord_l
;
288 int newc
= c
+ choord_c
;
289 if (newl
>= 0 && newl
< f
.h
&&
290 newc
>= 0 && newc
< f
.w
&&
291 f
.c
[newl
][newc
].o
== CLOSED
&&
292 uncover_square (newl
, newc
)) {
302 void flag_square (int l
, int c
) {
303 static char modechar
[] = {'*', '!', '?'};
305 if (f
.c
[l
][c
].o
!= CLOSED
) return;
306 /* cycle through flag/quesm/noflag: */
307 f
.c
[l
][c
].f
= (f
.c
[l
][c
].f
+ 1) % (op
.mode
+ 1);
308 if (f
.c
[l
][c
].f
==FLAG
) g
.f
++;
309 else if ((op
.mode
==FLAG
&& f
.c
[l
][c
].f
==NOFLAG
) ||
310 (op
.mode
==QUESM
&& f
.c
[l
][c
].f
==QUESM
)) g
.f
--;
311 partial_show_minefield (l
, c
, NORMAL
);
312 move_ph (1, op
.scheme
->cell_width
);
313 printf ("[%03d%c]", f
.m
- g
.f
, modechar
[g
.s
]);
316 void quesm_square (int l
, int c
) {
317 /* toggle question mark / none. won't turn flags into question marks.
318 unlike flag_square, this function doesn't respect `-q'. */
319 if (f
.c
[l
][c
].o
!= CLOSED
) return;
320 else if (f
.c
[l
][c
].f
== NOFLAG
) f
.c
[l
][c
].f
= QUESM
;
321 else if (f
.c
[l
][c
].f
== QUESM
) f
.c
[l
][c
].f
= NOFLAG
;
322 partial_show_minefield (l
, c
, NORMAL
);
325 int do_uncover (int* is_newgame
) {
326 if (*is_newgame
== GAME_NEW
) {
327 *is_newgame
= GAME_INPROGRESS
;
328 fill_minefield (g
.p
[0], g
.p
[1]);
332 if (f
.c
[g
.p
[0]][g
.p
[1]].f
== FLAG
) return GAME_INPROGRESS
;
333 if (f
.c
[g
.p
[0]][g
.p
[1]].o
== CLOSED
) {
334 if (uncover_square (g
.p
[0], g
.p
[1])) return GAME_LOST
;
335 } else if (get_neighbours (g
.p
[0], g
.p
[1], 1) == 0) {
336 if (choord_square (g
.p
[0], g
.p
[1])) return GAME_LOST
;
338 if (everything_opened()) return GAME_WON
;
340 return GAME_INPROGRESS
;
343 void fill_minefield (int l
, int c
) {
347 int line
= rand() % f
.h
;
348 int col
= rand() % f
.w
;
350 if (f
.c
[line
][col
].m
) {
351 /* skip if field already has a mine */
353 } else if ((line
== l
) && (col
== c
)) {
354 /* don't put a mine on the already opened (first click) field */
358 f
.c
[line
][col
].m
= STD_MINE
;
362 /* precalculate neighbours */
363 for (int l
=0; l
< f
.h
; l
++)
364 for (int c
=0; c
< f
.w
; c
++)
365 f
.c
[l
][c
].n
= get_neighbours (l
, c
, NORMAL
);
368 void move_ph (int line
, int col
) {
369 /* move printhead to zero-indexed position */
370 printf ("\033[%d;%dH", line
+1, col
+1);
373 void move_hi (int l
, int c
) {
374 /* move cursor and highlight to absolute coordinates */
376 partial_show_minefield (g
.p
[0], g
.p
[1], NORMAL
);
378 g
.p
[0] = CLAMP(l
, 0, f
.h
-1);
379 g
.p
[1] = CLAMP(c
, 0, f
.w
-1);
380 move_ph (g
.p
[0]+LINE_OFFSET
, field2screen_c(g
.p
[1]));
382 print("\033[7m"); /* reverse video */
383 partial_show_minefield (g
.p
[0], g
.p
[1], HIGHLIGHT
);
384 print("\033[0m"); /* un-invert */
387 /* to_next_boundary(): move into the supplied direction until a change in open-
388 state or flag-state is found and move there. falls back to BIG_MOVE. */
389 #define FIND_NEXT(X, L, C, L1, C1, MAX, OP) do {\
390 new_ ## X OP ## = BIG_MOVE;\
391 for (int i = X OP 1; i > 0 && i < f.MAX-1; i OP ## OP)\
392 if (((f.c[L ][C ].o<<2) + f.c[L ][C ].f) \
393 != ((f.c[L1][C1].o<<2) + f.c[L1][C1].f)) {\
398 void to_next_boundary (int l
, int c
, char direction
) {
402 case '>': FIND_NEXT(c
, l
, i
, l
, i
+1, w
, +); break;
403 case '<': FIND_NEXT(c
, l
, i
, l
, i
-1, w
, -); break;
404 case '^': FIND_NEXT(l
, i
, c
, i
-1, c
, h
, -); break;
405 case 'v': FIND_NEXT(l
, i
, c
, i
+1, c
, h
, +); break;
408 move_hi (new_l
, new_c
);
412 char* cell2schema (int l
, int c
, int mode
) {
413 struct minecell cell
= f
.c
[l
][c
];
415 if (mode
== SHOWMINES
) return (
416 cell
.f
== FLAG
&& cell
.m
? op
.scheme
->field_flagged
:
417 cell
.f
== FLAG
&& !cell
.m
? op
.scheme
->mine_wrongf
:
418 cell
.m
== STD_MINE
? op
.scheme
->mine_normal
:
419 cell
.m
== DEATH_MINE
? op
.scheme
->mine_death
:
420 cell
.o
== CLOSED
? op
.scheme
->field_closed
:
421 /*.......................*/ op
.scheme
->number
[f
.c
[l
][c
].n
]);
423 cell
.f
== FLAG
? op
.scheme
->field_flagged
:
424 cell
.f
== QUESM
? op
.scheme
->field_question
:
425 cell
.o
== CLOSED
? op
.scheme
->field_closed
:
426 cell
.m
== STD_MINE
? op
.scheme
->mine_normal
:
427 cell
.m
== DEATH_MINE
? op
.scheme
->mine_death
:
428 /*.......................*/ op
.scheme
->number
[f
.c
[l
][c
].n
]);
431 void partial_show_minefield (int l
, int c
, int mode
) {
432 move_ph (l
+LINE_OFFSET
, field2screen_c(c
));
434 print (cell2schema(l
, c
, mode
));
437 char* get_emoticon(void) {
438 return g
.o
==GAME_WON
? EMOT(WON
):
439 g
.o
==GAME_LOST
? EMOT(DEAD
):
443 /* https://zserge.com/blog/c-for-loop-tricks.html */
444 #define print_line(which) \
445 for (int _break = (printf("%s", BORDER(which,LEFT)), 1); _break; \
446 _break = 0, printf("%s\r\n", BORDER(which,RIGHT)))
447 #define print_border(which, width) \
448 print_line(which) printm (width, BORDER(which,MIDDLE))
449 void show_minefield (int mode
) {
450 int dtime
= difftime (time(NULL
), g
.t
)*!!g
.t
;
451 int half_spaces
= f
.w
*op
.scheme
->cell_width
/2;
452 int left_spaces
= MAX(0,half_spaces
-7-(f
.m
-g
.f
>999));
453 int right_spaces
= MAX(0,half_spaces
-6-(dtime
>999));
454 static char modechar
[] = {'*', '!', '?'};
458 print_border(TOP
, f
.w
);
460 printf("[%03d%c]%*s%s%*s[%03d]",
461 /* [ */ f
.m
- g
.f
, modechar
[g
.s
], /* ] */
462 left_spaces
,"", get_emoticon(), right_spaces
,"",
463 /* [ */ dtime
/* ] */);
465 print_border(DIVIDER
, f
.w
);
467 for (int l
= 0; l
< f
.h
; l
++) print_line(FIELD
)
468 printm (f
.w
, cell2schema(l
, _loop
, mode
));
469 print_border(BOTTOM
, f
.w
);
472 int get_neighbours (int line
, int col
, int reduced_mode
) {
473 /* counts mines surrounding a square
474 modes: 0=normal; 1=reduced */
478 for (int l
= MAX(line
-1, 0); l
<= MIN(line
+1, f
.h
-1); l
++) {
479 for (int c
= MAX(col
-1, 0); c
<= MIN(col
+1, f
.w
-1); c
++) {
480 if (!l
&& !c
) continue;
482 count
+= !!f
.c
[l
][c
].m
;
483 count
-= reduced_mode
* f
.c
[l
][c
].f
==FLAG
;
489 struct minecell
** alloc_array (int lines
, int cols
) {
490 struct minecell
** a
= malloc (lines
* sizeof(struct minecell
*));
491 if (a
== NULL
) return NULL
;
492 for (int l
= 0; l
< lines
; l
++) {
493 a
[l
] = calloc (cols
, sizeof(struct minecell
));
494 if (a
[l
] == NULL
) goto unalloc
;
499 for (int l
= 0; l
< lines
; l
++)
504 void free_field (void) {
505 if (f
.c
== NULL
) return;
506 for (int l
= 0; l
< f
.h
; l
++) {
514 int screen2field_l (int l
) {
515 return (l
-LINE_OFFSET
) - 1;
517 int screen2field_c (int c
) {
518 /* this depends on the cell width and only works when it is 1 or 2. */
519 return (c
-COL_OFFSET
+1 - 2*(CW
%2))/2 - CW
/2;
521 int field2screen_c (int c
) {
522 return (CW
*c
+COL_OFFSET
- (CW
%2));
524 int clicked_emoticon (unsigned char* mouse
) {
525 return (mouse
[2] == LINE_OFFSET
-1 && (
526 mouse
[1] == f
.w
+COL_OFFSET
||
527 mouse
[1] == f
.w
+COL_OFFSET
+1));
536 int getctrlseq (unsigned char* buf
) {
539 int offset
= 0x20; /* never sends control chars as data */
540 while ((c
= getchar()) != EOF
) {
544 case '\033': state
=ESC_SENT
; break;
550 case '[': state
=CSI_SENT
; break;
551 default: return CTRSEQ_INVALID
;
556 case 'A': return CTRSEQ_CURSOR_UP
;
557 case 'B': return CTRSEQ_CURSOR_DOWN
;
558 case 'C': return CTRSEQ_CURSOR_RIGHT
;
559 case 'D': return CTRSEQ_CURSOR_LEFT
;
560 case 'M': state
=MOUSE_EVENT
; break;
561 default: return CTRSEQ_INVALID
;
566 buf
[1] = getchar() - offset
;
567 buf
[2] = getchar() - offset
;
570 return CTRSEQ_INVALID
;
576 int getch(unsigned char* buf
) {
577 /* returns a character, EOF, or constant for an escape/control sequence - NOT
578 compatible with the ncurses implementation of same name */
579 int action
= getctrlseq(buf
);
583 l
= screen2field_l (buf
[2]);
584 c
= screen2field_c (buf
[1]);
586 if (buf
[0] > 3) break; /* ignore all but left/middle/right/up */
587 int success
= wait_mouse_up(l
, c
);
589 /* mouse moved while pressed: */
590 if (!success
) return CTRSEQ_INVALID
;
593 case 0: return CTRSEQ_MOUSE_LEFT
;
594 case 1: return CTRSEQ_MOUSE_MIDDLE
;
595 case 2: return CTRSEQ_MOUSE_RIGHT
;
602 int getch_wrapper (void) {
603 unsigned char mouse
[3];
604 int c
= getch(mouse
);
606 if (c
== CTRSEQ_MOUSE_LEFT
|| c
== CTRSEQ_MOUSE_RIGHT
) {
607 if (clicked_emoticon(mouse
))
608 return WRAPPER_EMOTICON
;
610 if (screen2field_c (mouse
[1]) < 0
611 || screen2field_c (mouse
[1]) >= f
.w
612 || screen2field_l (mouse
[2]) < 0
613 || screen2field_l (mouse
[2]) >= f
.h
)
614 return CTRSEQ_INVALID
;
616 g
.p
[0] = screen2field_l (mouse
[2]);
617 g
.p
[1] = screen2field_c (mouse
[1]);
619 return c
; /* CTRSEQ_MOUSE_LEFT || CTRSEQ_MOUSE_RIGHT */
625 int parse_fieldspec(char* str
) {
626 /* parses the FIELDSPEC (WxHxM); returns 1 on error */
627 int n
= sscanf (str
, "%dx%dx%d", &f
.w
, &f
.h
, &f
.m
);
629 ioctl(STDOUT_FILENO
, TIOCGWINSZ
, &w
);
631 /* clamp field size to terminal size and mouse maximum: */
632 if (COL_OFFSET
+ f
.w
*CW
+ COL_OFFSET
> w
.ws_col
)
633 f
.w
= w
.ws_col
/CW
- (COL_OFFSET
+COL_OFFSET
);
634 if (LINE_OFFSET
+ f
.h
+ LINES_AFTER
> w
.ws_row
)
635 f
.h
= w
.ws_row
- (LINE_OFFSET
+LINES_AFTER
);
636 if (COL_OFFSET
+ f
.w
*CW
> MOUSE_MAX
)
637 f
.w
= MOUSE_MAX
/CW
- COL_OFFSET
;
638 if (LINE_OFFSET
+ f
.h
> MOUSE_MAX
)
639 f
.h
= MOUSE_MAX
- LINE_OFFSET
;
642 return 1; /* error */
644 if (f
.w
< 30) f
.m
= f
.w
*f
.h
*.15625;
645 else f
.m
= f
.w
*f
.h
*.20625;
651 void timer_setup (int enable
) {
652 static struct itimerval tbuf
;
653 tbuf
.it_interval
.tv_sec
= 1;
654 tbuf
.it_interval
.tv_usec
= 0;
658 tbuf
.it_value
.tv_sec
= 1;
659 tbuf
.it_value
.tv_usec
= 0;
660 if (setitimer(ITIMER_REAL
, &tbuf
, NULL
) == -1) {
665 tbuf
.it_value
.tv_sec
= 0;
666 tbuf
.it_value
.tv_usec
= 0;
667 if ( setitimer(ITIMER_REAL
, &tbuf
, NULL
) == -1 ) {
675 void signal_setup (void) {
676 struct sigaction saction
;
678 saction
.sa_handler
= signal_handler
;
679 sigemptyset(&saction
.sa_mask
);
680 saction
.sa_flags
= 0;
681 if (sigaction(SIGALRM
, &saction
, NULL
) < 0 ) {
686 if (sigaction(SIGINT
, &saction
, NULL
) < 0 ) {
692 void signal_handler (int signum
) {
696 dtime
= difftime (time(NULL
), g
.t
);
697 move_ph (1, f
.w
*CW
-(CW
%2)-3-(dtime
>999));
698 printf ("[%03d]", g
.t
?dtime
:0);
705 void screen_setup (int enable
) {
708 printf ("\033[s\033[?47h"); /* save cursor, alternate screen */
709 printf ("\033[H\033[J"); /* reset cursor, clear screen */
710 printf ("\033[?1000h\033[?25l"); /* enable mouse, hide cursor */
711 print (op
.scheme
->init_seq
); /* swich charset, if necessary */
713 print (op
.scheme
->reset_seq
); /* reset charset, if necessary */
714 printf ("\033[?9l\033[?25h"); /* disable mouse, show cursor */
715 printf ("\033[?47l\033[u"); /* primary screen, restore cursor */
720 /* http://users.csc.calpoly.edu/~phatalsk/357/lectures/code/sigalrm.c */
721 void raw_mode(int enable
) {
722 static struct termios saved_term_mode
;
723 struct termios raw_term_mode
;
726 tcgetattr(STDIN_FILENO
, &saved_term_mode
);
727 raw_term_mode
= saved_term_mode
;
728 raw_term_mode
.c_lflag
&= ~(ICANON
| ECHO
);
729 raw_term_mode
.c_cc
[VMIN
] = 1 ;
730 raw_term_mode
.c_cc
[VTIME
] = 0;
731 tcsetattr(STDIN_FILENO
, TCSAFLUSH
, &raw_term_mode
);
733 tcsetattr(STDIN_FILENO
, TCSAFLUSH
, &saved_term_mode
);