1 #define _DEFAULT_SOURCE /* for getopt, sigaction, usleep */
18 /* stores a function pointer for every takeable action; called by game loop */
19 int (*action
[NUM_PLACES
][10])(int,int,int) = {
21 /* 1 2 3 4 5 6 7 stk wst fnd*/
22 /* 1 */ { t2f
, t2t
, t2t
, t2t
, t2t
, t2t
, t2t
, nop
, nop
, t2f
},
23 /* 2 */ { t2t
, t2f
, t2t
, t2t
, t2t
, t2t
, t2t
, nop
, nop
, t2f
},
24 /* 3 */ { t2t
, t2t
, t2f
, t2t
, t2t
, t2t
, t2t
, nop
, nop
, t2f
},
25 /* 4 */ { t2t
, t2t
, t2t
, t2f
, t2t
, t2t
, t2t
, nop
, nop
, t2f
},
26 /* 5 */ { t2t
, t2t
, t2t
, t2t
, t2f
, t2t
, t2t
, nop
, nop
, t2f
},
27 /* 6 */ { t2t
, t2t
, t2t
, t2t
, t2t
, t2f
, t2t
, nop
, nop
, t2f
},
28 /* 7 */ { t2t
, t2t
, t2t
, t2t
, t2t
, t2t
, t2f
, nop
, nop
, t2f
},
29 /*stk*/ { nop
, nop
, nop
, nop
, nop
, nop
, nop
, nop
, s2w
, nop
},
30 /*wst*/ { w2t
, w2t
, w2t
, w2t
, w2t
, w2t
, w2t
, w2s
, w2f
, w2f
},
31 /*fnd*/ { f2t
, f2t
, f2t
, f2t
, f2t
, f2t
, f2t
, nop
, nop
, nop
},
33 /* 1 2 3 4 5 6 7 8 9 10*/
34 /* 1 */ { t2f
, t2t
, t2t
, t2t
, t2t
, t2t
, t2t
, t2t
, t2t
, t2t
},
35 /* 2 */ { t2t
, t2f
, t2t
, t2t
, t2t
, t2t
, t2t
, t2t
, t2t
, t2t
},
36 /* 3 */ { t2t
, t2t
, t2f
, t2t
, t2t
, t2t
, t2t
, t2t
, t2t
, t2t
},
37 /* 4 */ { t2t
, t2t
, t2t
, t2f
, t2t
, t2t
, t2t
, t2t
, t2t
, t2t
},
38 /* 5 */ { t2t
, t2t
, t2t
, t2t
, t2f
, t2t
, t2t
, t2t
, t2t
, t2t
},
39 /* 6 */ { t2t
, t2t
, t2t
, t2t
, t2t
, t2f
, t2t
, t2t
, t2t
, t2t
},
40 /* 7 */ { t2t
, t2t
, t2t
, t2t
, t2t
, t2t
, t2f
, t2t
, t2t
, t2t
},
41 /* 8 */ { t2t
, t2t
, t2t
, t2t
, t2t
, t2t
, t2t
, t2f
, t2t
, t2t
},
42 /* 9 */ { t2t
, t2t
, t2t
, t2t
, t2t
, t2t
, t2t
, t2t
, t2f
, t2t
},
43 /*10 */ { t2t
, t2t
, t2t
, t2t
, t2t
, t2t
, t2t
, t2t
, t2t
, t2f
},
44 /*stk*/ { s2t
, s2t
, s2t
, s2t
, s2t
, s2t
, s2t
, s2t
, s2t
, s2t
},
49 // argv parsing, game loops, cleanup {{{
50 int main(int argc
, char** argv
) {
51 /* opinionated defaults: */
52 op
.s
= &unicode_large_color
;
58 opterr
= 0; /* don't print message on unrecognized option */
59 while ((optget
= getopt (argc
, argv
, "+:hs:vbcm")) != -1) {
62 case 's': /* number of suits */
64 case '1': op
.m
= EASY
; break;
65 case '2': op
.m
= MEDIUM
; break;
66 case '4': op
.m
= NORMAL
; break;
70 case 'b': op
.s
= &unicode_large_mono
; break;
71 case 'c': op
.s
= &unicode_large_color
; break;
72 case 'm': op
.s
= &unicode_small_mono
; break; /* "mini" */
73 case 'h': default: goto error
;
75 fprintf (stderr
, SHORTHELP LONGHELP KEYHELP
, argv
[0]);
83 signal_handler(SIGWINCH
); /* initialize window size */
89 case GAME_NEW
: goto newgame
;
91 print_table(NO_HI
, NO_HI
);
93 if (getch(NULL
)=='q') return 0;
95 case GAME_QUIT
: return 0;
101 long seed
= time(NULL
);
108 switch (get_cmd(&from
, &to
, &opt
)) {
110 ret
= action
[from
][to
](from
,to
,opt
);
111 if (ret
== ERR
) /* try again with from/to swapped: */
112 ret
= action
[to
][from
](to
,from
,opt
);
115 case ERR
: visbell(); break;
116 case WON
: return GAME_WON
;
122 case ERR
: visbell(); break;
123 case WON
: return GAME_WON
;
126 case CMD_HINT
: break;//TODO: show a possible (and sensible) move. if possible, involve active cursor
127 case CMD_UNDO
: undo_pop(f
.u
); break;
128 case CMD_INVAL
: visbell(); break;
129 case CMD_NEW
: return GAME_NEW
;
130 case CMD_AGAIN
: goto restart
;
131 case CMD_QUIT
: return GAME_QUIT
;
142 // card games helper functions {{{
143 #define get_suit(card) \
144 ((card-1) % NUM_SUITS)
145 #define get_rank(card) \
146 ((card-1) / NUM_SUITS)
147 #define get_color(card) \
148 ((get_suit(card) ^ get_suit(card)>>1) & 1)
150 #define is_tableu(where) (where <= TAB_MAX)
152 int find_top(card_t
* pile
) {
154 for(i
=PILE_SIZE
-1; i
>=0 && !pile
[i
]; i
--);
157 int first_movable(card_t
* pile
) {
159 for (;pile
[i
] && !is_movable(pile
, i
); i
++);
162 int turn_over(card_t
* pile
) {
163 int top
= find_top(pile
);
169 int check_won(void) {
170 for (int pile
= 0; pile
< NUM_DECKS
*NUM_SUITS
; pile
++)
171 if (f
.f
[pile
][NUM_RANKS
-1] == NO_CARD
) return 0;
175 int rank_next (card_t a
, card_t b
) {
176 return get_rank(a
) == get_rank(b
)-1;
178 int is_consecutive (card_t
* pile
, int pos
) {
179 if (pos
+1 >= PILE_SIZE
) return 1; /* card is last */
180 if (pile
[pos
+1] == NO_CARD
) return 1; /* card is first */
183 /* ranks consecutive? */
184 if (!rank_next(pile
[pos
+1], pile
[pos
])) return 0;
185 /* color opposite? */
186 if (get_color(pile
[pos
+1]) == get_color(pile
[pos
])) return 0;
188 /* ranks consecutive? */
189 if (!rank_next(pile
[pos
+1], pile
[pos
])) return 0;
191 if (get_suit(pile
[pos
+1]) != get_suit(pile
[pos
])) return 0;
197 int is_movable(card_t
* pile
, int n
) {
199 return(pile
[n
] > NO_CARD
); /*non-movable cards don't exist in klondike*/
201 int top
= find_top(pile
);
202 for (int i
= top
; i
>= 0; i
--) {
203 if (pile
[i
] <= NO_CARD
) return 0; /*no card or card face down?*/
204 if (!is_consecutive(pile
, i
)) return 0;
205 if (i
== n
) return 1; /* card reached, must be movable */
212 // takeable actions {{{
214 card_t
stack_take(void) { /*NOTE: assert(f.w >= 0) */
215 card_t card
= f
.s
[f
.w
];
216 /* move stack one over, so there are no gaps in it: */
217 for (int i
= f
.w
; i
< f
.z
-1; i
++)
220 f
.w
--; /* make previous card visible again */
223 int t2f(int from
, int to
, int opt
) { /* tableu to foundation */
224 (void) to
; (void) opt
; /* don't need */
225 int top_from
= find_top(f
.t
[from
]);
226 to
= get_suit(f
.t
[from
][top_from
]);
227 int top_to
= find_top(f
.f
[to
]);
228 if ((top_to
< 0 && get_rank(f
.t
[from
][top_from
]) == RANK_A
)
229 || (top_to
>= 0 && rank_next(f
.f
[to
][top_to
],f
.t
[from
][top_from
]))) {
230 f
.f
[to
][top_to
+1] = f
.t
[from
][top_from
];
231 f
.t
[from
][top_from
] = NO_CARD
;
232 undo_push(from
, FOUNDATION
, to
,
233 turn_over(f
.t
[from
]));
234 if (check_won()) return WON
;
238 int w2f(int from
, int to
, int opt
) { /* waste to foundation */
239 (void) from
; (void) to
; (void) opt
; /* don't need */
240 if (f
.w
< 0) return ERR
;
241 to
= get_suit(f
.s
[f
.w
]);
242 int top_to
= find_top(f
.f
[to
]);
243 if ((top_to
< 0 && get_rank(f
.s
[f
.w
]) == RANK_A
)
244 || (top_to
>= 0 && rank_next(f
.f
[to
][top_to
], f
.s
[f
.w
]))) {
245 undo_push(WASTE
, FOUNDATION
, f
.w
| to
<<16, 0);//ugly encoding :|
246 f
.f
[to
][top_to
+1] = stack_take();
247 if (check_won()) return WON
;
252 int s2w(int from
, int to
, int opt
) { /* stock to waste */
253 (void) from
; (void) to
; (void) opt
; /* don't need */
254 if (f
.z
== 0) return ERR
;
256 if (f
.w
== f
.z
) f
.w
= -1;
259 int w2s(int from
, int to
, int opt
) { /* waste to stock (undo stock to waste) */
260 (void) from
; (void) to
; (void) opt
; /* don't need */
261 if (f
.z
== 0) return ERR
;
263 if (f
.w
< -1) f
.w
= f
.z
-1;
266 int f2t(int from
, int to
, int opt
) { /* foundation to tableu */
267 (void) from
; /* don't need */
268 int top_to
= find_top(f
.t
[to
]);
270 int top_from
= find_top(f
.f
[from
]);
272 if ((get_color(f
.t
[to
][top_to
]) != get_color(f
.f
[from
][top_from
]))
273 && (rank_next(f
.f
[from
][top_from
], f
.t
[to
][top_to
]))) {
274 f
.t
[to
][top_to
+1] = f
.f
[from
][top_from
];
275 f
.f
[from
][top_from
] = NO_CARD
;
276 undo_push(FOUNDATION
, to
, from
, 0);
280 int w2t(int from
, int to
, int opt
) { /* waste to tableu */
281 (void) from
; (void) opt
; /* don't need */
282 int top_to
= find_top(f
.t
[to
]);
283 if (((get_color(f
.t
[to
][top_to
]) != get_color(f
.s
[f
.w
]))
284 && (rank_next(f
.s
[f
.w
], f
.t
[to
][top_to
])))
285 || (top_to
< 0 && get_rank(f
.s
[f
.w
]) == RANK_K
)) {
286 undo_push(WASTE
, to
, f
.w
, 0);
287 f
.t
[to
][top_to
+1] = stack_take();
291 int t2t(int from
, int to
, int opt
) { /* tableu to tableu */
292 (void) opt
; /* don't need */
293 int top_to
= find_top(f
.t
[to
]);
294 int top_from
= find_top(f
.t
[from
]);
295 int count
= 0; //NOTE: could probably be factored out
296 for (int i
= top_from
; i
>=0; i
--) {
297 if (((get_color(f
.t
[to
][top_to
]) != get_color(f
.t
[from
][i
]))
298 && (rank_next(f
.t
[from
][i
], f
.t
[to
][top_to
]))
299 && f
.t
[from
][i
] > NO_CARD
) /* card face up? */
300 || (top_to
< 0 && get_rank(f
.t
[from
][i
]) == RANK_K
)) {
301 /* move cards [i..top_from] to their destination */
302 for (;i
<= top_from
; i
++) {
304 f
.t
[to
][top_to
] = f
.t
[from
][i
];
305 f
.t
[from
][i
] = NO_CARD
;
308 undo_push(from
, to
, count
,
309 turn_over(f
.t
[from
]));
313 return ERR
; /* no such move possible */
316 int remove_if_complete (int pileno
) { //cleanup!
317 card_t
* pile
= f
.t
[pileno
];
318 /* test if K...A complete; move to foundation if so */
319 int top_from
= find_top(pile
);
320 if (get_rank(pile
[top_from
]) != RANK_A
) return 0;
321 for (int i
= top_from
; i
>=0; i
--) {
322 if (!is_consecutive (pile
, i
)) return 0;
323 if (i
+RANK_K
== top_from
/* if ace to king: remove it */
324 && get_rank(pile
[top_from
-RANK_K
]) == RANK_K
) {
325 for(int i
=top_from
, j
=0; i
>top_from
-NUM_RANKS
; i
--,j
++){
326 f
.f
[f
.w
][j
] = pile
[i
];
329 undo_push(pileno
, FOUNDATION
, f
.w
,
338 int t2t(int from
, int to
, int opt
) { //in dire need of cleanup
339 int top_from
= find_top(f
.t
[from
]);
340 int top_to
= find_top(f
.t
[to
]);
341 int empty_to
= (top_to
< 0)? opt
: -1; /* empty pile? */
342 int count
= 0; //NOTE: could probably be factored out
344 for (int i
= top_from
; i
>= 0; i
--) {
345 if (!is_consecutive(f
.t
[from
], i
)) break;
347 /* is consecutive OR to empty pile and rank ok? */
348 if (rank_next(f
.t
[from
][i
], f
.t
[to
][top_to
])
349 || (empty_to
>= RANK_A
&& get_rank(f
.t
[from
][i
]) == empty_to
)) {
350 for (;i
<= top_from
; i
++) {
352 f
.t
[to
][top_to
] = f
.t
[from
][i
];
353 f
.t
[from
][i
] = NO_CARD
;
356 undo_push(from
, to
, count
,
357 turn_over(f
.t
[from
]));
358 remove_if_complete(to
);
359 if (check_won()) return WON
;
364 return ERR
; /* no such move possible */
366 int s2t(int from
, int to
, int opt
) {
367 (void) from
; (void) to
; (void) opt
; /* don't need */
368 if (f
.z
<= 0) return ERR
; /* stack out of cards */
369 for (int pile
= 0; pile
< NUM_PILES
; pile
++)
370 if (f
.t
[pile
][0]==NO_CARD
) return ERR
; /*no piles may be empty*/
371 for (int pile
= 0; pile
< NUM_PILES
; pile
++) {
372 f
.t
[pile
][find_top(f
.t
[pile
])+1] = f
.s
[--f
.z
];
373 remove_if_complete(pile
);
374 if (check_won()) return WON
;
376 undo_push(STOCK
, TABLEU
, 1, 0);/*NOTE: puts 1 card on each tableu pile*/
379 int t2f(int from
, int to
, int opt
) {
380 (void) to
; (void) opt
; /* don't need */
381 /* manually retrigger remove_if_complete() (e.g. after undo_pop) */
382 return remove_if_complete(from
)?OK
:ERR
;
385 //TODO: generalize prediction engine for CMD_HINT
387 #define would_complete(pile) 0
389 #define would_complete(pile) \
390 (get_rank(f.t[pile][r[pile].top]) == RANK_A \
391 && get_rank(f.t[to][bottom_to]) == RANK_K)
393 #define would_turn(pile) \
394 (f.t[pile][r[pile].pos-1] < 0)
395 #define would_empty(pile) \
399 int top_to
= find_top(f
.t
[to
]);
401 int bottom_to
= first_movable(f
.t
[to
]);
405 if (to
== FOUNDATION
) {
407 for (int i
= 0; i
<= TAB_MAX
; i
++)
408 switch ((i
?t2f
:w2f
)(i
-1, FOUNDATION
, 0)) {
409 case WON
: return WON
;
410 case OK
: status
= OK
;
416 if (top_to
< 0) { /* move a king to empty pile: */
417 for (int i
= 0; i
< TAB_MAX
; i
++) {
418 if (f
.t
[i
][0] < 0) /* i.e. would turn? */
419 if (t2t(i
, to
, 0) == OK
) return OK
;
421 return w2t(WASTE
, to
, 0);
426 int ok
:1; /* card to move in pile? */
427 int above
; /* number of movable cards above */
428 int below
; /* number of cards below ours */
429 int pos
; /* where the card to move is in the pile */
430 int top
; /* find_top() */
431 } r
[NUM_PILES
] = {{0}};
432 int complete
= 0;/* SPIDER: true if any pile would complete a stack */
433 int turn
= 0; /* SPIDER: true if any pile would turn_over */
434 int empty
= 0; /* true if any pile would become empty */
436 /* 1. rate each pile: */
439 for (int pile
= 0; pile
< NUM_PILES
; pile
++) {
440 if (pile
== to
) continue;
441 int top
= find_top(f
.t
[pile
]);
442 int bottom
= first_movable(f
.t
[pile
]);
443 r
[pile
].pos
= bottom
; /* need for would_empty */
445 if (top
< 0) continue; /* no cards to move */
446 if (would_empty(pile
)) continue; /* doesn't help */
449 r
[pile
].above
= 0; /* always take as many as possible */
450 r
[pile
].below
= top
- bottom
;
452 complete
|= would_complete(pile
); /* never happens */
453 turn
|= would_turn(pile
);
454 empty
|= would_empty(pile
);
458 for (int pile
= 0; pile
< NUM_PILES
; pile
++) {
459 r
[pile
].top
= r
[pile
].pos
= find_top(f
.t
[pile
]);
460 /* backtrack until we find a compatible-to-'to'-pile card: */
461 while (r
[pile
].pos
>= 0 && is_movable(f
.t
[pile
], r
[pile
].pos
)) {
462 int rankdiff
= get_rank(f
.t
[pile
][r
[pile
].pos
])
463 - get_rank(f
.t
[to
][top_to
]);
464 if (rankdiff
>= 0) break; /* past our card */
465 if (rankdiff
== -1 /* rank matches */
467 && get_color(f
.t
[pile
][r
[pile
].pos
]) /* color OK */
468 != get_color(f
.t
[to
][top_to
])
470 && get_suit(f
.t
[pile
][r
[pile
].pos
]) /* color OK */
471 == get_suit(f
.t
[to
][top_to
])
475 complete
|= would_complete(pile
);
476 turn
|= would_turn(pile
);
477 empty
|= would_empty(pile
);
478 for (int i
= r
[pile
].pos
; i
>= 0; i
--)
479 if (is_movable(f
.t
[pile
], i
-1))
489 /* 2. find optimal pile: (optimized for spider) */
490 //todo: in spider, prefer longest piles if above==0 (faster completions)
492 for (int pile
= 0, above
= 99, below
= 99; pile
< NUM_PILES
; pile
++) {
493 if (!r
[pile
].ok
) continue;
494 /* don't bother if another pile would be better: prefer ... */
495 /* ... to complete a stack: */
496 if (!would_complete(pile
) && complete
) continue;
497 /* ... emptying piles: */
498 if (!would_empty(pile
) && empty
&& !complete
) continue;
499 /* ... to turn_over: */
500 if (!would_turn(pile
) && turn
&& !complete
&& !empty
) continue;
501 /* ... not to rip apart too many cards: */
502 if (r
[pile
].above
> above
) continue;
503 /* if tied, prefer ... */
504 if (r
[pile
].above
== above
505 /* ... larger pile if destination is empty */
506 && (top_to
< 0? r
[pile
].below
< below
507 /* ... shorter pile otherwise */
508 : r
[pile
].below
> below
))
512 above
= r
[pile
].above
;
513 below
= r
[pile
].below
;
516 /* 3. move cards over and return: */
518 /* prefer waste if it wouldn't turn_over: */
519 if (!turn
&& w2t(WASTE
, to
, 0) == OK
)
521 if (from
< 0) /* nothing found */
523 return t2t(from
, to
, 0);
525 if (from
< 0) /* nothing found */
527 int bottom
= first_movable(f
.t
[from
]);
528 return t2t(from
, to
, get_rank(f
.t
[from
][bottom
]));
533 #undef would_complete
534 int nop(int from
, int to
, int opt
) { (void)from
;(void)to
;(void)opt
;return ERR
; }
537 // keyboard input handling {{{
538 // cursor functions{{{
540 void cursor_left (struct cursor
* cursor
) {
541 if (is_tableu(cursor
->pile
)) {
542 if (cursor
->pile
> 0) cursor
->pile
--;
544 } else { /* stock/waste/foundation*/
545 switch (cursor
->pile
) {
546 case WASTE
: cursor
->pile
= STOCK
; cursor
->opt
= 0; break;
548 if (cursor
->opt
<= 0)
549 cursor
->pile
= WASTE
;
555 void cursor_down (struct cursor
* cursor
) {
556 if (!is_tableu(cursor
->pile
)) {
557 switch (cursor
->pile
) {
558 case STOCK
: cursor
->pile
= TAB_1
; break;
559 case WASTE
: cursor
->pile
= TAB_2
; break;
561 cursor
->pile
= TAB_4
+ cursor
->opt
;
566 void cursor_up (struct cursor
* cursor
) {
567 if (is_tableu(cursor
->pile
)) {
568 switch (cursor
->pile
) { //ugly :|
569 case TAB_1
: cursor
->pile
= STOCK
; break;
570 case TAB_2
: cursor
->pile
= WASTE
; break;
571 case TAB_3
: cursor
->pile
= WASTE
; break;
572 case TAB_4
: case TAB_5
: case TAB_6
: case TAB_7
:
573 cursor
->opt
=cursor
->pile
-TAB_4
;
574 cursor
->pile
= FOUNDATION
;
579 void cursor_right (struct cursor
* cursor
) {
580 if (is_tableu(cursor
->pile
)) {
581 if (cursor
->pile
< TAB_MAX
) cursor
->pile
++;
583 switch (cursor
->pile
) {
584 case STOCK
: cursor
->pile
= WASTE
; break;
585 case WASTE
: cursor
->pile
= FOUNDATION
;cursor
->opt
= 0; break;
587 if (cursor
->opt
< NUM_SUITS
-1)
593 /*NOTE: one can't highlight the stock due to me being too lazy to implement it*/
594 void cursor_left (struct cursor
* cursor
) {
595 if (cursor
->pile
> 0) cursor
->pile
--;
598 void cursor_down (struct cursor
* cursor
) {
599 int first
= first_movable(f
.t
[cursor
->pile
]);
600 int top
= find_top(f
.t
[cursor
->pile
]);
601 if (first
+ cursor
->opt
< top
)
604 void cursor_up (struct cursor
* cursor
) {
605 if (cursor
->opt
> 0) cursor
->opt
--;
607 void cursor_right (struct cursor
* cursor
) {
608 if (cursor
->pile
< TAB_MAX
) cursor
->pile
++;
612 void cursor_to (struct cursor
* cursor
, int pile
) {
616 int set_mouse(int pile
, int* main
, int* opt
) {
617 if (pile
< 0) return 1;
620 if (pile
>= FOUNDATION
)
622 *opt
= pile
- FOUNDATION
;
625 //TODO: set opt if to field is empty (maybe not good to do there)
630 int get_cmd (int* from
, int* to
, int* opt
) {
632 unsigned char mouse
[6] = {0}; /* must clear [3]! */
633 struct cursor inactive
= {-1,-1};
634 static struct cursor active
= {0,0};
635 active
.opt
= 0; /* always reset offset, but keep pile */
638 from_l
: print_table(&active
, &inactive
);
642 /* direct addressing: */
643 case '1': *from
= TAB_1
; break;
644 case '2': *from
= TAB_2
; break;
645 case '3': *from
= TAB_3
; break;
646 case '4': *from
= TAB_4
; break;
647 case '5': *from
= TAB_5
; break;
648 case '6': *from
= TAB_6
; break;
649 case '7': *from
= TAB_7
; break;
651 case '8': *from
= TAB_8
; break;
652 case '9': *from
= TAB_9
; break;
653 case '0': *from
= TAB_10
;break;
654 #elif defined KLONDIKE
655 case '9': *from
= WASTE
; break;
656 case '0': *from
= FOUNDATION
; break;
657 case '8': /* fallthrough */
659 case '\n': *from
= STOCK
; break;
660 /* cursor keys addressing: */
662 case 'h': cursor_left (&active
); goto from_l
;
664 case 'j': cursor_down (&active
); goto from_l
;
666 case 'k': cursor_up (&active
); goto from_l
;
668 case 'l': cursor_right(&active
); goto from_l
;
670 case 'H': cursor_to(&active
,TAB_1
); goto from_l
; /* leftmost tableu */
672 case 'L': cursor_to(&active
,TAB_MAX
);goto from_l
; /* rigthmost tableu */
674 case 'M': cursor_to(&active
,TAB_MAX
/2); goto from_l
; /* center tableu */
675 case ' ': /* continue with second cursor */
678 *opt
= active
.opt
; /* when FOUNDATION */
682 /* mouse addressing: */
684 case MOUSE_RIGHT
: return CMD_NONE
;
686 if (set_mouse(term2pile(mouse
), from
, opt
))
692 fprintf (stderr
, ":");
693 raw_mode(0); /* turn on echo */
694 fgets (buf
, 256, stdin
);
697 case 'q': return CMD_QUIT
;
698 case 'n': return CMD_NEW
;
699 case 'r': return CMD_AGAIN
;
700 default: return CMD_INVAL
;
705 if (*to
== FOUNDATION
) return CMD_JOIN
;
707 if (*to
> TAB_MAX
) return CMD_INVAL
;
709 case 'K': /* fallthrough */
710 case '?': return CMD_HINT
;
711 case 'u': return CMD_UNDO
;
712 case EOF
: return CMD_NONE
; /* sent by SIGCONT */
713 default: return CMD_INVAL
;
715 inactive
.pile
= *from
; /* for direct addressing highlighting */
716 if (is_tableu(*from
) && f
.t
[*from
][0] == NO_CARD
) return CMD_INVAL
;
718 if (*from
== STOCK
) {
724 to_l
: print_table(&active
, &inactive
);
729 case 'h': cursor_left (&active
); goto to_l
;
731 case 'j': cursor_down (&active
); goto to_l
;
733 case 'k': cursor_up (&active
); goto to_l
;
735 case 'l': cursor_right(&active
); goto to_l
;
737 case 'H': cursor_to(&active
,TAB_1
); goto to_l
;
739 case 'L': cursor_to(&active
,TAB_MAX
); goto to_l
;
741 case 'M': cursor_to(&active
,TAB_MAX
/2); goto to_l
;
742 case 'J': /* fallthrough; just join selected pile */
745 break; /* continues with the foundation/empty tableu check */
747 case MOUSE_RIGHT
: return CMD_NONE
;
749 if (set_mouse(term2pile(mouse
), to
, opt
))
752 case 'K': /* fallthrough */
753 case '?': return CMD_HINT
;
754 case 'u': return CMD_NONE
; /* cancel selection */
755 case EOF
: return CMD_NONE
; /* sent by SIGCONT */
757 if (t
< '0' || t
> '9') return CMD_INVAL
;
770 if (*from
== FOUNDATION
) {
771 int top
= find_top(f
.t
[*to
]);
772 if (top
< 0) return CMD_INVAL
;
773 int color
= get_color(f
.t
[*to
][top
]);
774 int choice_1
= 1-color
; /* selects piles of */
775 int choice_2
= 2+color
; /* the opposite color */
776 int top_c1
= find_top(f
.f
[choice_1
]);
777 int top_c2
= find_top(f
.f
[choice_2
]);
779 switch ((rank_next(f
.f
[choice_1
][top_c1
], f
.t
[*to
][top
])
780 && top_c1
>= 0 ) << 0
781 |(rank_next(f
.f
[choice_2
][top_c2
], f
.t
[*to
][top
])
782 && top_c2
>= 0 ) << 1) {
783 case ( 1<<0): *opt
= choice_1
; break; /* choice_1 only */
784 case (1<<1 ): *opt
= choice_2
; break; /* choice_2 only */
785 case (1<<1 | 1<<0): /* both, ask user which to pick from */
786 printf ("take from (1-4): "); fflush (stdout
);
787 *opt
= getch(NULL
) - '1';
788 if (*opt
< 0 || *opt
> 3) return CMD_INVAL
;
790 default: return CMD_INVAL
; /* none matched */
792 /* `opt` is the foundation index (0..3) */
795 /* moving to empty tableu? */
796 if (is_tableu(*to
) && f
.t
[*to
][0] == NO_CARD
) {
797 int bottom
= first_movable(f
.t
[*from
]);
798 if (inactive
.opt
>= 0) { /*if from was cursor addressed: */
799 *opt
= get_rank(f
.t
[*from
][bottom
+ inactive
.opt
]);
802 int top
= find_top(f
.t
[*from
]);
803 if (top
< 0) return CMD_INVAL
;
804 if (top
>= 0 && !is_movable(f
.t
[*from
], top
-1)) {
805 *opt
= get_rank(f
.t
[*from
][top
]);
806 } else { /* only ask the user if it's unclear: */
807 printf ("\rup to ([a23456789xjqk] or space/return): ");
810 case ' ': *opt
= get_rank(f
.t
[*from
][top
]); break;
811 case'\n': *opt
= get_rank(f
.t
[*from
][bottom
]); break;
812 case 'a': case 'A': *opt
= RANK_A
; break;
813 case '0': /* fallthrough */
814 case 'x': case 'X': *opt
= RANK_X
; break;
815 case 'j': case 'J': *opt
= RANK_J
; break;
816 case 'q': case 'Q': *opt
= RANK_Q
; break;
817 case 'k': case 'K': *opt
= RANK_K
; break;
818 default: *opt
-= '1';
820 if (*opt
< RANK_A
|| *opt
> RANK_K
) return ERR
;
822 /* `opt` is the rank of the highest card to move */
828 int getctrlseq(unsigned char* buf
) {
836 int offset
= 0x20; /* never sends control chars as data */
837 while ((c
= getchar()) != EOF
) {
841 case '\033': state
=ESC_SENT
; break;
847 case '[': state
=CSI_SENT
; break;
848 default: return KEY_INVAL
;
853 case 'A': return KEY_UP
;
854 case 'B': return KEY_DOWN
;
855 case 'C': return KEY_RIGHT
;
856 case 'D': return KEY_LEFT
;
857 /*NOTE: home/end send ^[[x~ . no support for modifiers*/
858 case 'H': return KEY_HOME
;
859 case 'F': return KEY_END
;
860 case '2': getchar(); return KEY_INS
;
861 case '5': getchar(); return KEY_PGUP
;
862 case '6': getchar(); return KEY_PGDN
;
863 case 'M': state
=MOUSE_EVENT
; break;
864 default: return KEY_INVAL
;
868 if (buf
== NULL
) return KEY_INVAL
;
870 buf
[1] = getchar() - offset
;
871 buf
[2] = getchar() - offset
;
879 int term2pile(unsigned char *mouse
) {
880 int line
= (mouse
[2]-1);
881 int column
= (mouse
[1]-1) / op
.s
->width
;
883 if (line
< op
.s
->height
) { /* first line */
886 case 0: return STOCK
;
887 case 1: return WASTE
;
888 case 2: return -1; /* spacer */
889 case 3: return FOUNDATION
+0;
890 case 4: return FOUNDATION
+1;
891 case 5: return FOUNDATION
+2;
892 case 6: return FOUNDATION
+3;
895 if (column
< 3) return STOCK
;
898 } else if (line
> op
.s
->height
) { /* tableu */
899 if (column
<= TAB_MAX
) return column
;
903 int wait_mouse_up(unsigned char* mouse
) {
904 struct cursor cur
= {-1,-1};
906 /* note: if dragged [3]==1 and second position is in mouse[0,4,5] */
908 /* display a cursor while mouse button is pushed: */
909 int pile
= term2pile(mouse
);
912 if (pile
>= FOUNDATION
) {
913 cur
.pile
= FOUNDATION
;
914 cur
.opt
= pile
-FOUNDATION
;
917 print_table(&cur
, NO_HI
); //TODO: should not overwrite inactive cursor!
920 if (getctrlseq (mouse
+3) == MOUSE_ANY
) {
921 /* ignore mouse wheel events: */
922 if (mouse
[3] & 0x40) continue;
924 else if((mouse
[3]&3) == 3) level
--; /* release event */
925 else level
++; /* another button pressed */
929 int success
= mouse
[1] == mouse
[4] && mouse
[2] == mouse
[5];
936 int getch(unsigned char* buf
) {
937 /* returns a character, EOF, or constant for an escape/control sequence - NOT
938 compatible with the ncurses implementation of same name */
941 /* mouse was dragged; return 'ungetted' previous destination */
943 /* keep original [0], as [3] only contains release event */
948 action
= getctrlseq(buf
);
953 if (buf
[0] > 3) break; /* ignore wheel events */
958 case 0: return MOUSE_LEFT
;
959 case 1: return MOUSE_MIDDLE
;
960 case 2: return MOUSE_RIGHT
;
968 // shuffling and dealing {{{
969 void deal(long seed
) {
970 f
= (const struct playfield
){0}; /* clear playfield */
971 card_t deck
[DECK_SIZE
*NUM_DECKS
];
972 int avail
= DECK_SIZE
*NUM_DECKS
;
973 for (int i
= 0; i
< DECK_SIZE
*NUM_DECKS
; i
++) deck
[i
] = (i
%DECK_SIZE
)+1;
975 if (op
.m
!= NORMAL
) for (int i
= 0; i
< DECK_SIZE
*NUM_DECKS
; i
++) {
976 if (op
.m
== MEDIUM
) deck
[i
] = 1+((deck
[i
]-1) | 2);
977 if (op
.m
== EASY
) deck
[i
] = 1+((deck
[i
]-1) | 2 | 1);
978 /* the 1+ -1 dance gets rid of the offset created by NO_CARD */
982 for (int i
= DECK_SIZE
*NUM_DECKS
-1; i
> 0; i
--) { /* fisher-yates */
983 int j
= rand() % (i
+1);
984 if (j
-i
) deck
[i
]^=deck
[j
],deck
[j
]^=deck
[i
],deck
[i
]^=deck
[j
];
988 for (int i
= 0; i
< NUM_PILES
; i
++) {
990 int closed
= i
; /* pile n has n closed cards, then 1 open */
992 int closed
= i
<4?5:4; /* pile 1-4 have 5, 5-10 have 4 closed */
994 /* face down cards are negated: */
995 for (int j
= 0; j
< closed
; j
++) f
.t
[i
][j
] = -deck
[--avail
];
996 f
.t
[i
][closed
] = deck
[--avail
]; /* the face-up card */
998 /* rest of the cards to the stock; NOTE: assert(avail==50) for spider */
999 for (f
.z
= 0; avail
; f
.z
++) f
.s
[f
.z
] = deck
[--avail
];
1001 f
.w
= -1; /* @start: nothing on waste */
1002 #elif defined SPIDER
1003 f
.w
= 0; /* number of used foundations */
1006 f
.u
= &undo_sentinel
;
1010 // screen drawing routines {{{
1011 void print_hi(int invert
, int grey_bg
, int bold
, char* str
) {
1012 if (bold
&& op
.s
== &unicode_large_color
){ //awful hack for bold + faint
1013 int offset
= str
[3]==017?16:str
[4]==017?17:0;
1014 printf ("%s%s%s""%.*s%s%s""%s%s%s",
1015 "\033[1m", invert
?"\033[7m":"", grey_bg
?"\033[100m":"",
1016 offset
, str
, bold
?"\033[1m":"", str
+offset
,
1017 grey_bg
?"\033[49m":"", invert
?"\033[27m":"","\033[22m");
1020 printf ("%s%s%s%s%s%s%s",
1021 bold
?"\033[1m":"", invert
?"\033[7m":"", grey_bg
?"\033[100m":"",
1023 grey_bg
?"\033[49m":"", invert
?"\033[27m":"",bold
?"\033[22m":"");
1025 void print_table(const struct cursor
* active
, const struct cursor
* inactive
) {
1026 printf("\033[2J\033[H"); /* clear screen, reset cursor */
1028 /* print stock, waste and foundation: */
1029 for (int line
= 0; line
< op
.s
->height
; line
++) {
1031 print_hi (active
->pile
== STOCK
, inactive
->pile
== STOCK
, 1, (
1032 (f
.w
< f
.z
-1)?op
.s
->facedown
1033 :op
.s
->placeholder
)[line
]);
1035 print_hi (active
->pile
== WASTE
, inactive
->pile
== WASTE
, 1, (
1036 /* NOTE: cast, because f.w sometimes is (short)-1 !? */
1037 ((short)f
.w
>= 0)?op
.s
->card
[f
.s
[f
.w
]]
1038 :op
.s
->placeholder
)[line
]);
1039 printf ("%s", op
.s
->card
[NO_CARD
][line
]); /* spacer */
1041 for (int pile
= 0; pile
< NUM_SUITS
; pile
++) {
1042 int card
= find_top(f
.f
[pile
]);
1043 print_hi (active
->pile
==FOUNDATION
&& active
->opt
==pile
,
1044 inactive
->pile
==FOUNDATION
&& (
1045 /* cursor addr. || direct addr. */
1046 inactive
->opt
==pile
|| inactive
->opt
< 0
1048 (card
< 0)?op
.s
->placeholder
[line
]
1049 :op
.s
->card
[f
.f
[pile
][card
]][line
]);
1054 #elif defined SPIDER
1055 int fdone
; for (fdone
= NUM_DECKS
*NUM_SUITS
; fdone
; fdone
--)
1056 if (f
.f
[fdone
-1][RANK_K
]) break; /*number of completed stacks*/
1057 int spacer_from
= f
.z
?(f
.z
/10-1) * op
.s
->halfwidth
[0] + op
.s
->width
:0;
1058 int spacer_to
= NUM_PILES
*op
.s
->width
-
1059 ((fdone
?(fdone
-1) * op
.s
->halfwidth
[1]:0)+op
.s
->width
);
1060 for (int line
= 0; line
< op
.s
->height
; line
++) {
1061 /* available stock: */
1062 for (int i
= f
.z
/10; i
; i
--) {
1063 if (i
==1) printf ("%s", op
.s
->facedown
[line
]);
1064 else printf ("%s", op
.s
->halfstack
[line
]);
1067 for (int i
= spacer_from
; i
< spacer_to
; i
++) printf (" ");
1068 /* foundation (overlapping): */
1069 for (int i
= NUM_DECKS
*NUM_SUITS
-1, half
= 0; i
>= 0; i
--) {
1070 int overlap
= half
? op
.s
->halfcard
[line
]: 0;
1071 if (f
.f
[i
][RANK_K
]) printf ("%.*s", op
.s
->halfwidth
[2],
1072 op
.s
->card
[f
.f
[i
][RANK_K
]][line
]+overlap
),
1080 #define DO_HI(cursor) (cursor->pile == pile && (movable || empty))
1081 #define TOP_HI(c) 1 /* can't select partial stacks in KLONDIKE */
1083 #elif defined SPIDER
1084 int offset
[NUM_PILES
]={1,1,1,1,1,1,1,1,1,1}; // :|
1085 #define DO_HI(cursor) (cursor->pile == pile && (movable || empty) \
1086 && offset[pile] > cursor->opt)
1087 #define TOP_HI(cursor) (cursor->pile == pile && movable \
1088 && offset[pile]-1 == cursor->opt)
1089 #define INC_OFFSET if (movable) offset[pile]++
1091 /* print tableu piles: */
1092 int row
[NUM_PILES
] = {0};
1093 int line
[NUM_PILES
]= {0};
1094 int label
[NUM_PILES
]={0};
1096 int did_placeholders
= 0;
1099 for (int pile
= 0; pile
< NUM_PILES
; pile
++) {
1100 card_t card
= f
.t
[pile
][row
[pile
]];
1101 card_t next
= f
.t
[pile
][row
[pile
]+1];
1102 int movable
= is_movable(f
.t
[pile
], row
[pile
]);
1103 int empty
= !card
&& row
[pile
] == 0;
1105 print_hi (DO_HI(active
), DO_HI(inactive
), movable
, (
1106 (!card
&& row
[pile
] == 0)?op
.s
->placeholder
1107 :(card
<0)?op
.s
->facedown
1111 int extreme_overlap
= ( 3 /* spacer, labels, status */
1112 + 2 * op
.s
->height
/* stock, top tableu card */
1113 + find_top(f
.t
[pile
]) * op
.s
->overlap
) >op
.w
[0];
1114 /* normal overlap: */
1115 if (++line
[pile
] >= (next
?op
.s
->overlap
:op
.s
->height
)
1116 /* extreme overlap on closed cards: */
1117 || (extreme_overlap
&&
1119 f
.t
[pile
][row
[pile
]] < 0 &&
1120 f
.t
[pile
][row
[pile
]+1] <0)
1121 /* extreme overlap on sequences: */
1122 || (extreme_overlap
&&
1123 !TOP_HI(active
) && /*always show top selected card*/
1124 line
[pile
] >= 1 && row
[pile
] > 0 &&
1125 f
.t
[pile
][row
[pile
]-1] > NO_CARD
&&
1126 is_consecutive (f
.t
[pile
], row
[pile
]) &&
1127 is_consecutive (f
.t
[pile
], row
[pile
]-1) &&
1128 f
.t
[pile
][row
[pile
]+1] != NO_CARD
)
1134 /* tableu labels: */
1135 if(!card
&& !label
[pile
] && row
[pile
]>0&&line
[pile
]>0) {
1137 printf ("\b\b%d ", (pile
+1) % 10); //XXX: hack
1139 line_had_card
|= !!card
;
1140 did_placeholders
|= row
[pile
] > 0;
1143 } while (line_had_card
|| !did_placeholders
);
1146 void visbell (void) {
1147 printf ("\033[?5h"); fflush (stdout
);
1149 printf ("\033[?5l"); fflush (stdout
);
1151 void win_anim(void) {
1152 printf ("\033[?25l"); /* hide cursor */
1154 /* set cursor to random location */
1155 int row
= 1+rand()%(24-op
.s
->width
);
1156 int col
= 1+rand()%(80-op
.s
->height
);
1158 /* draw random card */
1159 int face
= 1 + rand() % 52;
1160 for (int l
= 0; l
< op
.s
->height
; l
++) {
1161 printf ("\033[%d;%dH", row
+l
, col
);
1162 printf ("%s", op
.s
->card
[face
][l
]);
1166 /* exit on keypress */
1167 struct pollfd p
= {STDIN_FILENO
, POLLIN
, 0};
1168 if (poll (&p
, 1, 80)) goto fin
;
1171 printf ("\033[?25h"); /* show cursor */
1177 void undo_push (int _f
, int t
, int n
, int o
) {
1178 struct undo
* new = malloc(sizeof(struct undo
));
1188 void undo_pop (struct undo
* u
) {
1189 if (u
== &undo_sentinel
) return;
1192 if (u
->f
== FOUNDATION
) {
1193 /* foundation -> tableu */
1194 int top_f
= find_top(f
.f
[u
->n
]);
1195 int top_t
= find_top(f
.t
[u
->t
]);
1196 f
.f
[u
->n
][top_f
+1] = f
.t
[u
->t
][top_t
];
1197 f
.t
[u
->t
][top_t
] = NO_CARD
;
1198 } else if (u
->f
== WASTE
&& u
->t
== FOUNDATION
) {
1199 /* waste -> foundation */
1200 /* split u->n into wst and fnd: */
1201 int wst
= u
->n
& 0xffff;
1202 int fnd
= u
->n
>> 16;
1203 /* move stock cards one position up to make room: */
1204 for (int i
= f
.z
; i
>= wst
; i
--) f
.s
[i
+1] = f
.s
[i
];
1205 /* move one card from foundation to waste: */
1206 int top
= find_top(f
.f
[fnd
]);
1207 f
.s
[wst
] = f
.f
[fnd
][top
];
1208 f
.f
[fnd
][top
] = NO_CARD
;
1211 } else if (u
->f
== WASTE
) {
1212 /* waste -> tableu */
1213 /* move stock cards one position up to make room: */
1214 for (int i
= f
.z
; i
>= u
->n
; i
--) f
.s
[i
+1] = f
.s
[i
];
1215 /* move one card from tableu to waste: */
1216 int top
= find_top(f
.t
[u
->t
]);
1217 f
.s
[u
->n
] = f
.t
[u
->t
][top
];
1218 f
.t
[u
->t
][top
] = NO_CARD
;
1221 } else if (u
->t
== FOUNDATION
) {
1222 /* tableu -> foundation */
1223 int top_f
= find_top(f
.t
[u
->f
]);
1224 int top_t
= find_top(f
.f
[u
->n
]);
1225 /* close topcard if previous action caused turn_over(): */
1226 if (u
->o
) f
.t
[u
->f
][top_f
] *= -1;
1227 /* move one card from foundation to tableu: */
1228 f
.t
[u
->f
][top_f
+1] = f
.f
[u
->n
][top_t
];
1229 f
.f
[u
->n
][top_t
] = NO_CARD
;
1231 /* tableu -> tableu */
1232 int top_f
= find_top(f
.t
[u
->f
]);
1233 int top_t
= find_top(f
.t
[u
->t
]);
1234 /* close topcard if previous action caused turn_over(): */
1235 if (u
->o
) f
.t
[u
->f
][top_f
] *= -1;
1236 /* move n cards from tableu[f] to tableu[t]: */
1237 for (int i
= 0; i
< u
->n
; i
++) {
1238 f
.t
[u
->f
][top_f
+u
->n
-i
] = f
.t
[u
->t
][top_t
-i
];
1239 f
.t
[u
->t
][top_t
-i
] = NO_CARD
;
1242 #elif defined SPIDER
1243 if (u
->f
== STOCK
) {
1244 /* stock -> tableu */
1245 /*remove a card from each pile and put it back onto the stock:*/
1246 for (int pile
= NUM_PILES
-1; pile
>= 0; pile
--) {
1247 int top
= find_top(f
.t
[pile
]);
1248 f
.s
[f
.z
++] = f
.t
[pile
][top
];
1249 f
.t
[pile
][top
] = NO_CARD
;
1251 } else if (u
->t
== FOUNDATION
) {
1252 /* tableu -> foundation */
1253 int top
= find_top(f
.t
[u
->f
]);
1254 /* close topcard if previous action caused turn_over(): */
1255 if (u
->o
) f
.t
[u
->f
][top
] *= -1;
1256 /* append cards from foundation to tableu */
1257 for (int i
= RANK_K
; i
>= RANK_A
; i
--) {
1258 f
.t
[u
->f
][++top
] = f
.f
[u
->n
][i
];
1259 f
.f
[u
->n
][i
] = NO_CARD
;
1261 f
.w
--; /* decrement complete-foundation-counter */
1264 /* tableu -> tableu */
1265 int top_f
= find_top(f
.t
[u
->f
]);
1266 int top_t
= find_top(f
.t
[u
->t
]);
1267 /* close topcard if previous action caused turn_over(): */
1268 if (u
->o
) f
.t
[u
->f
][top_f
] *= -1;
1269 /* move n cards from tableu[f] to tableu[t]: */
1270 for (int i
= 0; i
< u
->n
; i
++) {
1271 f
.t
[u
->f
][top_f
+u
->n
-i
] = f
.t
[u
->t
][top_t
-i
];
1272 f
.t
[u
->t
][top_t
-i
] = NO_CARD
;
1281 void free_undo (struct undo
* u
) {
1282 while (u
&& u
!= &undo_sentinel
) {
1290 // initialization stuff {{{
1291 void screen_setup (int enable
) {
1294 printf ("\033[s\033[?47h"); /* save cursor, alternate screen */
1295 printf ("\033[H\033[J"); /* reset cursor, clear screen */
1296 printf ("\033[?1000h"); /* enable mouse */
1298 printf ("\033[?1000l"); /* disable mouse */
1299 printf ("\033[?47l\033[u"); /* primary screen, restore cursor */
1304 void raw_mode(int enable
) {
1305 static struct termios saved_term_mode
;
1306 struct termios raw_term_mode
;
1309 if (saved_term_mode
.c_lflag
== 0)/*don't overwrite stored mode*/
1310 tcgetattr(STDIN_FILENO
, &saved_term_mode
);
1311 raw_term_mode
= saved_term_mode
;
1312 raw_term_mode
.c_lflag
&= ~(ICANON
| ECHO
);
1313 raw_term_mode
.c_cc
[VMIN
] = 1 ;
1314 raw_term_mode
.c_cc
[VTIME
] = 0;
1315 tcsetattr(STDIN_FILENO
, TCSAFLUSH
, &raw_term_mode
);
1317 tcsetattr(STDIN_FILENO
, TCSAFLUSH
, &saved_term_mode
);
1321 void signal_handler (int signum
) {
1326 signal(SIGTSTP
, SIG_DFL
); /* NOTE: assumes SysV semantics! */
1331 print_table(NO_HI
, NO_HI
);
1333 case SIGINT
: //TODO: don't exit; just warn like vim does
1336 ioctl(STDOUT_FILENO
, TIOCGWINSZ
, &w
);
1342 void signal_setup(void) {
1343 struct sigaction saction
;
1345 saction
.sa_handler
= signal_handler
;
1346 sigemptyset(&saction
.sa_mask
);
1347 saction
.sa_flags
= 0;
1348 if (sigaction(SIGTSTP
, &saction
, NULL
) < 0) {
1352 if (sigaction(SIGCONT
, &saction
, NULL
) < 0) {
1356 if (sigaction(SIGINT
, &saction
, NULL
) < 0) {
1360 if (sigaction(SIGWINCH
, &saction
, NULL
) < 0) {
1361 perror ("SIGWINCH");
1367 //vim: foldmethod=marker