1 /* vim:tw=78:ts=8:sw=4:set ft=c: */
3 Copyright (C) 2002-2006 Ben Kibbey <bjk@luxsci.net>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include <sys/types.h>
25 #include <sys/socket.h>
39 #ifdef HAVE_SYS_WAIT_H
79 static char *str_etc(const char *str
, int maxlen
, int rev
)
81 int len
= strlen(str
);
82 static char buf
[80], *p
= buf
;
85 strncpy(buf
, str
, sizeof(buf
));
94 for (i
= 0; i
< maxlen
+ 3; i
++)
95 *p
++ = buf
[(len
- maxlen
) + i
+ 3];
111 void update_cursor(GAME g
, int idx
)
115 int t
= history_total(g
.hp
);
118 * If not deincremented then r and c would be the next move.
122 if (idx
> t
|| idx
< 0 || !t
|| !g
.hp
[idx
]->move
) {
123 c_row
= 2, c_col
= 5;
136 c_row
= (g
.turn
== WHITE
) ? 8 : 1;
145 c_row
= ROWTOINT(*p
--);
146 c_col
= COLTOINT(*p
);
149 static int init_nag()
155 if ((fp
= fopen(config
.nagfile
, "r")) == NULL
) {
156 cmessage(ERROR
, ANYKEY
, "%s: %s", config
.nagfile
, strerror(errno
));
161 if (fscanf(fp
, " %[^\n] ", line
) == 1) {
162 nags
= Realloc(nags
, (i
+ 2) * sizeof(struct nag_s
));
163 nags
[i
].line
= strdup(line
);
173 char *history_edit_nag(void *arg
)
177 ITEM
**mitems
= NULL
;
183 HISTORY
*anno
= (HISTORY
*)arg
;
191 mitems
= Realloc(mitems
, (i
+ 2) * sizeof(ITEM
));
192 mitems
[i
++] = new_item(NONE
, NULL
);
194 for (n
= 0; nags
[n
].line
; n
++, i
++) {
195 mitems
= Realloc(mitems
, (i
+ 2) * sizeof(ITEM
));
196 mitems
[i
] = new_item(nags
[n
].line
, NULL
);
200 menu
= new_menu(mitems
);
201 scale_menu(menu
, &rows
, &cols
);
203 win
= newwin(rows
+ 4, cols
+ 2, CALCPOSY(rows
) - 2, CALCPOSX(cols
));
204 set_menu_win(menu
, win
);
205 subw
= derwin(win
, rows
, cols
, 2, 1);
206 set_menu_sub(menu
, subw
);
207 set_menu_fore(menu
, A_REVERSE
);
208 set_menu_grey(menu
, A_NORMAL
);
209 set_menu_mark(menu
, NULL
);
210 set_menu_spacing(menu
, 0, 0, 0);
211 menu_opts_off(menu
, O_NONCYCLIC
|O_SHOWDESC
|O_ONEVALUE
);
213 panel
= new_panel(win
);
217 set_menu_pattern(menu
, mbuf
);
218 wbkgd(win
, CP_MESSAGE_WINDOW
);
219 draw_window_title(win
, NAG_EDIT_TITLE
, cols
+ 2, CP_HISTORY_TITLE
,
222 for (i
= 0; i
< MAX_PGN_NAG
; i
++) {
223 if (anno
->nag
[i
] && anno
->nag
[i
] <= item_count(menu
)) {
224 set_item_value(mitems
[anno
->nag
[i
]], TRUE
);
225 set_current_item(menu
, mitems
[anno
->nag
[i
]]);
235 wattron(win
, A_REVERSE
);
237 for (c
= 1; c
< (cols
+ 2) - 1; c
++)
238 mvwprintw(win
, rows
+ 2, c
, " ");
240 c
= item_index(current_item(menu
)) + 1;
242 snprintf(buf
, sizeof(buf
), "Item %i of %i (%i of %i selected) %s", c
,
243 item_count(menu
), itemcount
, MAX_PGN_NAG
, NAG_EDIT_PROMPT
);
244 draw_prompt(win
, rows
+ 2, cols
+ 2, buf
, CP_MESSAGE_PROMPT
);
246 wattroff(win
, A_REVERSE
);
249 for (i
= 0; mitems
[i
]; i
++)
250 set_item_value(mitems
[i
], FALSE
);
252 set_item_value(mitems
[0], TRUE
);
255 set_item_value(mitems
[0], FALSE
);
257 /* This nl() statement needs to be here because NL is recognized
258 * for some reason after the first selection.
268 help(NAG_EDIT_HELP
, ANYKEY
, naghelp
);
276 for (i
= item_index(current_item(menu
)) + 1; mitems
[i
]; i
++) {
277 if (item_value(mitems
[i
]) == TRUE
) {
284 for (i
= 0; mitems
[i
]; i
++) {
285 if (item_value(mitems
[i
]) == TRUE
) {
292 set_current_item(menu
, mitems
[found
]);
300 for (i
= item_index(current_item(menu
)) - 1; i
> 0; i
--) {
301 if (item_value(mitems
[i
]) == TRUE
) {
308 for (i
= item_count(menu
) - 1; i
> 0; i
--) {
309 if (item_value(mitems
[i
]) == TRUE
) {
316 set_current_item(menu
, mitems
[found
]);
319 menu_driver(menu
, REQ_FIRST_ITEM
);
322 menu_driver(menu
, REQ_LAST_ITEM
);
325 menu_driver(menu
, REQ_UP_ITEM
);
328 menu_driver(menu
, REQ_DOWN_ITEM
);
332 if (menu_driver(menu
, REQ_SCR_UPAGE
) == E_REQUEST_DENIED
)
333 menu_driver(menu
, REQ_FIRST_ITEM
);
337 if (menu_driver(menu
, REQ_SCR_DPAGE
) == E_REQUEST_DENIED
)
338 menu_driver(menu
, REQ_LAST_ITEM
);
341 if (item_index(current_item(menu
)) == 0 &&
342 item_value(current_item(menu
)) == FALSE
) {
347 if (item_value(current_item(menu
)) == TRUE
) {
348 set_item_value(current_item(menu
), FALSE
);
352 if (itemcount
+ 1 > MAX_PGN_NAG
)
355 set_item_value(current_item(menu
), TRUE
);
359 SET_FLAG(game
[gindex
].flags
, GF_MODIFIED
);
368 tmp
= menu_pattern(menu
);
370 if (tmp
&& tmp
[strlen(tmp
) - 1] != c
) {
371 menu_driver(menu
, REQ_CLEAR_PATTERN
);
372 menu_driver(menu
, c
);
375 if (menu_driver(menu
, REQ_NEXT_MATCH
) == E_NO_MATCH
)
376 menu_driver(menu
, c
);
384 for (i
= 0; i
< MAX_PGN_NAG
; i
++)
387 for (i
= 0, n
= 0; mitems
[i
] && n
< MAX_PGN_NAG
; i
++) {
388 if (item_value(mitems
[i
]) == TRUE
)
396 for (i
= 0; mitems
[i
]; i
++)
397 free_item(mitems
[i
]);
406 static void view_nag(void *arg
)
408 HISTORY
*h
= (HISTORY
*)arg
;
410 char line
[LINE_MAX
] = {0};
413 snprintf(buf
, sizeof(buf
), "Viewing NAG for \"%s\"", h
->move
);
420 for (i
= 0; i
< MAX_PGN_NAG
; i
++) {
424 strncat(line
, nags
[h
->nag
[i
] - 1].line
, sizeof(line
));
425 strncat(line
, "\n", sizeof(line
));
428 line
[strlen(line
) - 1] = 0;
429 message(buf
, ANYKEY
, "%s", line
);
432 void view_annotation(HISTORY h
)
434 char buf
[MAX_SAN_MOVE_LEN
+ strlen(ANNOTATION_VIEW_TITLE
) + 4];
435 int nag
= 0, comment
= 0;
437 if (h
.comment
&& h
.comment
[0])
443 if (!nag
&& !comment
)
446 snprintf(buf
, sizeof(buf
), "%s \"%s\"", ANNOTATION_VIEW_TITLE
, h
.move
);
449 show_message(buf
, (nag
) ? "Any other key to continue" : ANYKEY
,
450 (nag
) ? "Press 'n' to view NAG" : NULL
,
451 (nag
) ? view_nag
: NULL
, (nag
) ? (void *)&h
: NULL
,
452 (nag
) ? 'n' : 0, "%s", h
.comment
);
454 show_message(buf
, "Any other key to continue", "Press 'n' to view NAG",
455 view_nag
, (void *)&h
, 'n', "%s", "No annotations for this move");
458 static void cleanup(WINDOW
*win
, WINDOW
*subw
, PANEL
*panel
, MENU
*menu
,
459 ITEM
**items
, struct d_entries
*entries
)
466 for (i
= 0; items
[i
]; i
++)
472 for (i
= 0; entries
[i
].name
; i
++) {
473 free(entries
[i
].name
);
474 free(entries
[i
].fancy
);
485 static int sort_entries(const void *s1
, const void *s2
)
487 const struct d_entries
*ss1
= s1
;
488 const struct d_entries
*ss2
= s2
;
490 return strcmp(ss1
->name
, ss2
->name
);
493 char *browse_directory(void *arg
)
495 char *inputstr
= (char *)arg
;
496 int initkey
= (inputstr
) ? inputstr
[0] : 0;
497 char pattern
[FILENAME_MAX
];
498 static char path
[FILENAME_MAX
];
499 static char file
[FILENAME_MAX
];
504 if (config
.savedirectory
) {
505 if ((p
= word_expand(config
.savedirectory
)) == NULL
)
508 strncpy(path
, p
, sizeof(path
));
510 if (access(path
, R_OK
) == -1) {
511 cmessage(ERROR
, ANYKEY
, "%s: %s", path
, strerror(errno
));
512 getcwd(path
, sizeof(path
));
516 getcwd(path
, sizeof(path
));
521 * First find directories (including hidden) in the working directory.
522 * Then apply the config.pattern to regular files.
524 if ((p
= word_split_append(path
, '/', ".* *")) == NULL
)
527 strncpy(pattern
, p
, sizeof(pattern
));
532 ITEM
**mitems
= NULL
;
539 int len
= strlen(path
);
542 struct d_entries
*entries
= NULL
;
547 if (wordexp(pattern
, &w
, x
) != 0) {
548 cmessage(ERROR
, ANYKEY
, "Error in pattern\n%s", pattern
);
552 for (i
= 0; i
< w
.we_wordc
; i
++) {
556 if (stat(w
.we_wordv
[i
], &st
) == -1)
559 if ((p
= strrchr(w
.we_wordv
[i
], '/')) != NULL
)
565 if (!S_ISDIR(st
.st_mode
))
568 if (p
[0] == '.' && p
[1] == 0)
572 if (S_ISDIR(st
.st_mode
))
577 entries
= Realloc(entries
, (n
+ 2) * sizeof(struct d_entries
));
578 entries
[n
].name
= strdup(w
.we_wordv
[i
]);
579 entries
[n
].fancy
= Malloc(len
);
580 strncpy(entries
[n
].fancy
, p
, len
);
582 if (S_ISDIR(st
.st_mode
))
583 entries
[n
].fancy
[len
- 2] = '/';
585 tp
= localtime(&st
.st_mtime
);
586 strftime(tbuf
, sizeof(tbuf
), "%b %d %T", tp
);
588 snprintf(entries
[n
].desc
, sizeof(entries
[n
].desc
), "%-7i %s",
589 (int)st
.st_size
, tbuf
);
591 memset(&entries
[++n
], '\0', sizeof(struct d_entries
));
597 if ((p
= word_split_append(path
, '/', config
.pattern
)) == NULL
)
600 strncpy(pattern
, p
, sizeof(pattern
));
606 qsort(entries
, n
, sizeof(struct d_entries
), sort_entries
);
608 for (i
= 0; i
< n
; i
++) {
609 mitems
= Realloc(mitems
, (idx
+ 2) * sizeof(ITEM
));
610 mitems
[idx
++] = new_item(entries
[i
].fancy
, entries
[i
].desc
);
614 menu
= new_menu(mitems
);
615 scale_menu(menu
, &rows
, &cols
);
617 if (cols
< strlen(path
))
620 if (cols
< strlen(HELP_PROMPT
))
621 cols
= strlen(HELP_PROMPT
);
623 rows
= (LINES
/ 5) * 4;
626 win
= newwin(rows
+ 4, cols
, CALCPOSY(rows
) - 2, CALCPOSX(cols
));
627 set_menu_format(menu
, rows
, 0);
628 set_menu_win(menu
, win
);
629 subw
= derwin(win
, rows
, cols
- 2, 2, 1);
630 set_menu_sub(menu
, subw
);
631 set_menu_fore(menu
, A_REVERSE
);
632 set_menu_grey(menu
, A_NORMAL
);
633 set_menu_mark(menu
, NULL
);
634 set_menu_spacing(menu
, 2, 0, 0);
635 menu_opts_off(menu
, O_NONCYCLIC
);
637 panel
= new_panel(win
);
639 draw_window_title(win
, path
, cols
, CP_MESSAGE_TITLE
, CP_MESSAGE_BORDER
);
640 draw_prompt(win
, rows
+ 2, cols
, HELP_PROMPT
, CP_MESSAGE_PROMPT
);
645 set_menu_pattern(menu
, mbuf
);
647 if (isgraph(initkey
)) {
648 menu_driver(menu
, initkey
);
655 /* This nl() statement needs to be here because NL is recognized
656 * for some reason after the first selection.
665 menu_driver(menu
, REQ_SCR_UPAGE
);
670 menu_driver(menu
, REQ_SCR_DPAGE
);
673 menu_driver(menu
, REQ_UP_ITEM
);
676 menu_driver(menu
, REQ_DOWN_ITEM
);
679 selected
= item_index(current_item(menu
));
683 cleanup(win
, subw
, panel
, menu
, mitems
, entries
);
688 help(BROWSER_HELP
, ANYKEY
, file_browser_help
);
691 strncpy(path
, "~/", sizeof(path
));
692 cleanup(win
, subw
, panel
, menu
, mitems
, entries
);
696 if ((tmp
= get_input_str_clear(BROWSER_CHDIR_TITLE
, NULL
))
700 strncpy(path
, tmp
, sizeof(path
));
701 cleanup(win
, subw
, panel
, menu
, mitems
, entries
);
705 tmp
= menu_pattern(menu
);
707 if (tmp
&& tmp
[strlen(tmp
) - 1] != c
) {
708 menu_driver(menu
, REQ_CLEAR_PATTERN
);
709 menu_driver(menu
, c
);
712 if (menu_driver(menu
, REQ_NEXT_MATCH
) == E_NO_MATCH
)
713 menu_driver(menu
, c
);
721 strncpy(file
, entries
[selected
].name
, sizeof(file
));
722 cleanup(win
, subw
, panel
, menu
, mitems
, entries
);
724 if (stat(file
, &st
) == -1) {
725 cmessage(ERROR
, ANYKEY
, "%s\n%s", file
, strerror(errno
));
729 if (S_ISDIR(st
.st_mode
)) {
730 p
= file
+ strlen(file
) - 2;
732 if (strcmp(p
, "..") == 0) {
733 p
= file
+ strlen(file
) - 3;
736 if ((p
= strrchr(file
, '/')) != NULL
)
737 file
[strlen(file
) - strlen(p
)] = 0;
740 strncpy(path
, file
, sizeof(path
));
744 if (S_ISREG(st
.st_mode
))
747 cmessage(ERROR
, ANYKEY
, "%s\n%s", file
, E_NOTAREGFILE
);
751 return (*file
) ? file
: NULL
;
753 static int init_country_codes()
756 char line
[LINE_MAX
], *s
;
759 if ((fp
= fopen(config
.ccfile
, "r")) == NULL
) {
760 cmessage(ERROR
, ANYKEY
, "%s: %s", config
.ccfile
, strerror(errno
));
764 while ((s
= fgets(line
, sizeof(line
), fp
)) != NULL
) {
767 if ((tmp
= strsep(&s
, " ")) == NULL
)
776 ccodes
= Realloc(ccodes
, (cindex
+ 2) * sizeof(struct country_codes
));
777 strncpy(ccodes
[cindex
].code
, tmp
, sizeof(ccodes
[cindex
].code
));
778 strncpy(ccodes
[cindex
].country
, s
, sizeof(ccodes
[cindex
].country
));
782 memset(&ccodes
[cindex
], '\0', sizeof(struct country_codes
));
788 char *country_codes(void *arg
)
792 ITEM
**mitems
= NULL
;
800 if (init_country_codes())
804 for (n
= i
= 0; ccodes
[n
].code
[0]; n
++, i
++) {
805 mitems
= Realloc(mitems
, (i
+ 2) * sizeof(ITEM
));
806 mitems
[i
] = new_item(ccodes
[n
].country
, ccodes
[n
].code
);
810 menu
= new_menu(mitems
);
811 scale_menu(menu
, &rows
, &cols
);
813 if (cols
< strlen(HELP_PROMPT
) + 21)
814 cols
= strlen(HELP_PROMPT
) + 21;
816 win
= newwin(rows
+ 4, cols
+ 4, CALCPOSY(rows
) - 2, CALCPOSX(cols
));
817 set_menu_win(menu
, win
);
818 subw
= derwin(win
, rows
, cols
+ 2, 2, 1);
819 set_menu_sub(menu
, subw
);
820 set_menu_fore(menu
, A_REVERSE
);
821 set_menu_grey(menu
, A_NORMAL
);
822 set_menu_mark(menu
, NULL
);
823 set_menu_spacing(menu
, 0, 0, 0);
824 menu_opts_off(menu
, O_NONCYCLIC
);
826 panel
= new_panel(win
);
830 set_menu_pattern(menu
, mbuf
);
831 wbkgd(win
, CP_MESSAGE_WINDOW
);
832 draw_window_title(win
, CC_TITLE
, cols
+ 4, CP_MESSAGE_TITLE
,
839 wattron(win
, A_REVERSE
);
841 for (c
= 1; c
< (cols
+ 2) - 1; c
++)
842 mvwprintw(win
, rows
+ 2, c
, " ");
844 c
= item_index(current_item(menu
)) + 1;
846 snprintf(buf
, sizeof(buf
), "%s %i %s %i %s", MENU_ITEM_STR
, c
,
847 N_OF_N_STR
, item_count(menu
), HELP_PROMPT
);
848 draw_prompt(win
, rows
+ 2, cols
+ 2, buf
, CP_MESSAGE_PROMPT
);
850 wattroff(win
, A_REVERSE
);
852 /* This nl() statement needs to be here because NL is recognized
853 * for some reason after the first selection.
861 help(CC_KEY_HELP
, ANYKEY
, cc_help
);
864 menu_driver(menu
, REQ_FIRST_ITEM
);
867 menu_driver(menu
, REQ_LAST_ITEM
);
870 menu_driver(menu
, REQ_UP_ITEM
);
873 menu_driver(menu
, REQ_DOWN_ITEM
);
877 if (menu_driver(menu
, REQ_SCR_UPAGE
) == E_REQUEST_DENIED
)
878 menu_driver(menu
, REQ_FIRST_ITEM
);
883 if (menu_driver(menu
, REQ_SCR_DPAGE
) == E_REQUEST_DENIED
)
884 menu_driver(menu
, REQ_LAST_ITEM
);
887 tmp
= (char *)item_description(current_item(menu
));
895 tmp
= menu_pattern(menu
);
897 if (tmp
&& tmp
[strlen(tmp
) - 1] != c
) {
898 menu_driver(menu
, REQ_CLEAR_PATTERN
);
899 menu_driver(menu
, c
);
902 if (menu_driver(menu
, REQ_NEXT_MATCH
) == E_NO_MATCH
)
903 menu_driver(menu
, c
);
914 for (i
= 0; mitems
[i
]; i
++)
915 free_item(mitems
[i
]);
923 static void add_custom_tags(TAG
***t
)
930 for (i
= 0; config
.tag
[i
]; i
++)
931 pgn_add_tag(t
, config
.tag
[i
]->name
, config
.tag
[i
]->value
);
936 TAG
**edit_tags(GAME g
, BOARD b
, int edit
)
940 unsigned char data_index
= 0;
941 int n
, lastindex
= 0;
944 /* Edit the backup copy, not the original in case the save fails. */
945 for (n
= 0; g
.tag
[n
]; n
++)
946 pgn_add_tag(&data
, g
.tag
[n
]->name
, g
.tag
[n
]->value
);
948 data_index
= pgn_tag_total(data
);
953 ITEM
**mitems
= NULL
;
961 int nlen
= 0, vlen
= 0;
963 data_index
= pgn_tag_total(data
);
965 for (i
= 0; i
< data_index
; i
++) {
966 mitems
= Realloc(mitems
, (i
+ 2) * sizeof(ITEM
));
968 if (data
[i
]->value
) {
969 nlen
= strlen(data
[i
]->name
);
970 vlen
= strlen(data
[i
]->value
);
972 /* The +6 is for the menu padding. */
973 mitems
[i
] = new_item(data
[i
]->name
,
974 (nlen
+ vlen
+ 6 >= MAX_VALUE_WIDTH
)
975 ? PRESS_ENTER
: data
[i
]->value
);
978 mitems
[i
] = new_item(data
[i
]->name
, UNKNOWN
);
982 menu
= new_menu(mitems
);
983 scale_menu(menu
, &rows
, &cols
);
985 /* +14 for the extra prompt info. */
986 if (cols
< strlen(HELP_PROMPT
) + 14)
987 cols
= strlen(HELP_PROMPT
) + 14;
989 win
= newwin(rows
+ 4, cols
+ 4, CALCPOSY(rows
) - 2, CALCPOSX(cols
));
990 set_menu_win(menu
, win
);
991 subw
= derwin(win
, rows
, cols
+ 2, 2, 1);
992 set_menu_sub(menu
, subw
);
993 set_menu_fore(menu
, A_REVERSE
);
994 set_menu_grey(menu
, A_NORMAL
);
995 set_menu_mark(menu
, NULL
);
996 set_menu_pad(menu
, '-');
997 set_menu_spacing(menu
, 3, 0, 0);
998 menu_opts_off(menu
, O_NONCYCLIC
);
1000 panel
= new_panel(win
);
1005 set_menu_pattern(menu
, mbuf
);
1006 wbkgd(win
, CP_MESSAGE_WINDOW
);
1007 draw_window_title(win
, (edit
) ? TAG_EDIT_TITLE
: TAG_VIEW_TITLE
,
1008 cols
+ 4, CP_MESSAGE_TITLE
, CP_MESSAGE_BORDER
);
1012 TAG
**tmppgn
= NULL
;
1013 char *newtag
= NULL
;
1015 if (set_current_item(menu
, mitems
[lastindex
]) != E_OK
) {
1016 lastindex
= item_count(menu
) - 1;
1020 snprintf(buf
, sizeof(buf
), "%s %i %s %i %s", MENU_TAG_STR
,
1021 item_index(current_item(menu
)) + 1, N_OF_N_STR
,
1022 item_count(menu
), HELP_PROMPT
);
1023 draw_prompt(win
, rows
+ 2, cols
+ 4, buf
, CP_MESSAGE_PROMPT
);
1029 add_custom_tags(&data
);
1034 help(TAG_EDIT_HELP
, ANYKEY
, pgn_edit_help
);
1036 help(TAG_VIEW_HELP
, ANYKEY
, pgn_info_help
);
1042 selected
= item_index(current_item(menu
));
1044 if (selected
<= 6) {
1045 cmessage(NULL
, ANYKEY
, "%s", E_REMOVE_STR
);
1049 data_index
= pgn_tag_total(data
);
1051 for (i
= 0; i
< data_index
; i
++) {
1055 pgn_add_tag(&tmppgn
, data
[i
]->name
, data
[i
]->value
);
1061 for (i
= 0; tmppgn
[i
]; i
++)
1062 pgn_add_tag(&data
, tmppgn
[i
]->name
, tmppgn
[i
]->value
);
1064 pgn_tag_free(tmppgn
);
1071 if ((newtag
= get_input(TAG_NEW_TITLE
, NULL
, 1, 1, NULL
,
1072 NULL
, NULL
, 0, FIELD_TYPE_PGN_TAG_NAME
))
1076 newtag
[0] = toupper(newtag
[0]);
1078 if (strlen(newtag
) > MAX_VALUE_WIDTH
- 6 -
1079 strlen(PRESS_ENTER
)) {
1080 cmessage(ERROR
, ANYKEY
, "%s", E_TAG_NAMETOOLONG
);
1084 for (i
= 0; i
< data_index
; i
++) {
1085 if (strcasecmp(data
[i
]->name
, newtag
) == 0) {
1091 pgn_add_tag(&data
, newtag
, NULL
);
1092 data_index
= pgn_tag_total(data
);
1093 selected
= data_index
- 1;
1097 menu_driver(menu
, REQ_FIRST_ITEM
);
1100 menu_driver(menu
, REQ_LAST_ITEM
);
1106 pgn_add_tag(&data
, "FEN", pgn_game_to_fen(g
, b
));
1107 data_index
= pgn_tag_total(data
);
1108 selected
= data_index
- 1;
1113 if (menu_driver(menu
, REQ_SCR_DPAGE
) == E_REQUEST_DENIED
)
1114 menu_driver(menu
, REQ_LAST_ITEM
);
1118 if (menu_driver(menu
, REQ_SCR_UPAGE
) == E_REQUEST_DENIED
)
1119 menu_driver(menu
, REQ_FIRST_ITEM
);
1122 menu_driver(menu
, REQ_UP_ITEM
);
1125 menu_driver(menu
, REQ_DOWN_ITEM
);
1128 selected
= item_index(current_item(menu
));
1132 cleanup(win
, subw
, panel
, menu
, mitems
, NULL
);
1136 tmp
= menu_pattern(menu
);
1138 if (tmp
&& tmp
[strlen(tmp
) - 1] != c
) {
1139 menu_driver(menu
, REQ_CLEAR_PATTERN
);
1140 menu_driver(menu
, c
);
1143 if (menu_driver(menu
, REQ_NEXT_MATCH
) == E_NO_MATCH
)
1144 menu_driver(menu
, c
);
1150 lastindex
= item_index(current_item(menu
));
1154 lastindex
= selected
;
1155 nlen
= strlen(data
[selected
]->name
) + 3;
1156 nlen
+= (edit
) ? strlen(TAG_EDIT_TAG_TITLE
) : strlen(TAG_VIEW_TAG_TITLE
);
1158 if (nlen
> MAX_VALUE_WIDTH
)
1159 snprintf(buf
, sizeof(buf
), "%s", data
[selected
]->name
);
1161 snprintf(buf
, sizeof(buf
), "%s \"%s\"",
1162 (edit
) ? TAG_EDIT_TAG_TITLE
: TAG_VIEW_TAG_TITLE
,
1163 data
[selected
]->name
);
1166 if (strcmp(item_description(mitems
[selected
]), UNKNOWN
) == 0)
1169 cmessage(buf
, ANYKEY
, "%s", data
[selected
]->value
);
1173 if (strcmp(data
[selected
]->name
, "Date") == 0) {
1174 tmp
= get_input(buf
, data
[selected
]->value
, 0, 0, NULL
, NULL
, NULL
,
1175 0, FIELD_TYPE_PGN_DATE
);
1178 if (strptime(tmp
, PGN_TIME_FORMAT
, &tp
) == NULL
) {
1179 cmessage(ERROR
, ANYKEY
, "%s", E_TAG_DATE_FMT
);
1186 else if (strcmp(data
[selected
]->name
, "Site") == 0) {
1187 tmp
= get_input(buf
, data
[selected
]->value
, 1, 1, CC_PROMPT
,
1188 country_codes
, NULL
, CTRL('t'), -1);
1193 else if (strcmp(data
[selected
]->name
, "Round") == 0) {
1194 tmp
= get_input(buf
, NULL
, 1, 1, NULL
, NULL
, NULL
, 0,
1195 FIELD_TYPE_PGN_ROUND
);
1204 else if (strcmp(data
[selected
]->name
, "Result") == 0) {
1205 tmp
= get_input(buf
, data
[selected
]->value
, 1, 1, NULL
, NULL
, NULL
,
1212 if (item_description(mitems
[selected
]) &&
1213 strcmp(item_description(mitems
[selected
]), UNKNOWN
) == 0)
1216 tmp
= data
[selected
]->value
;
1218 tmp
= get_input(buf
, tmp
, 0, 0, NULL
, NULL
, NULL
, 0, -1);
1221 len
= (tmp
) ? strlen(tmp
) + 1 : 1;
1222 data
[selected
]->value
= Realloc(data
[selected
]->value
, len
);
1223 strncpy(data
[selected
]->value
, (tmp
) ? tmp
: "", len
);
1226 cleanup(win
, subw
, panel
, menu
, mitems
, NULL
);
1238 /* If the saveindex argument is -1, all games will be saved. Otherwise it's a
1239 * game index number.
1241 int save_pgn(const char *filename
, int isfifo
, int saveindex
)
1246 char buf
[FILENAME_MAX
];
1249 char *command
= NULL
;
1250 int saveindex_max
= (saveindex
== -1) ? gtotal
: saveindex
+ 1;
1252 if (filename
[0] != '/' && config
.savedirectory
&& !isfifo
) {
1253 if (stat(config
.savedirectory
, &st
) == -1) {
1254 if (errno
== ENOENT
) {
1255 if (mkdir(config
.savedirectory
, 0755) == -1) {
1256 cmessage(ERROR
, ANYKEY
, "%s: %s", config
.savedirectory
,
1262 cmessage(ERROR
, ANYKEY
, "%s: %s", config
.savedirectory
,
1268 stat(config
.savedirectory
, &st
);
1270 if (!S_ISDIR(st
.st_mode
)) {
1271 cmessage(ERROR
, ANYKEY
, "%s: %s", config
.savedirectory
, E_NOTADIR
);
1275 snprintf(buf
, sizeof(buf
), "%s/%s", config
.savedirectory
, filename
);
1279 /* This is a hack to resume an existing game when more than one game is
1280 * available. Also resuming a saved game and a game from history.
1282 // FIXME: may not need this when a FEN tag is supported (by the engine).
1286 if (access(filename
, W_OK
) == 0) {
1287 c
= cmessage(NULL
, GAME_SAVE_OVERWRITE_PROMPT
,
1288 "%s \"%s\"", E_FILEEXISTS
, filename
);
1292 if (pgn_is_compressed(filename
)) {
1293 cmessage(NULL
, ANYKEY
, "%s", E_SAVE_COMPRESS
);
1311 if ((fp
= popen(command
, "w")) == NULL
) {
1312 cmessage(ERROR
, ANYKEY
, "%s: %s", filename
, strerror(errno
));
1317 if ((fp
= fopen(filename
, mode
)) == NULL
) {
1318 cmessage(ERROR
, ANYKEY
, "%s: %s", filename
, strerror(errno
));
1324 pgn_write(fp
, game
[saveindex
]);
1326 for (i
= (saveindex
== -1) ? 0 : saveindex
; i
< saveindex_max
; i
++)
1327 pgn_write(fp
, game
[i
]);
1335 if (!isfifo
&& saveindex
== -1)
1336 strncpy(loadfile
, filename
, sizeof(loadfile
));
1341 char *random_agony(GAME g
)
1345 char line
[LINE_MAX
];
1347 if (n
== -1 || !config
.agony
|| !curses_initialized
||
1348 (g
.mode
== MODE_HISTORY
&& !config
.historyagony
))
1352 if ((fp
= fopen(config
.agonyfile
, "r")) == NULL
) {
1354 cmessage(ERROR
, ANYKEY
, "%s: %s", config
.agonyfile
, strerror(errno
));
1359 if (fscanf(fp
, " %[^\n] ", line
) == 1) {
1360 agony
= Realloc(agony
, (n
+ 2) * sizeof(char *));
1361 agony
[n
++] = strdup(trim(line
));
1368 if (agony
[0] == NULL
|| !n
) {
1374 return agony
[random() % n
];
1377 static int castling_state(GAME
*g
, BOARD b
, int row
, int col
, int piece
, int mod
)
1379 if (pgn_piece_to_int(piece
) == ROOK
&& col
== 7
1381 (TEST_FLAG(g
->flags
, GF_WK_CASTLE
) || mod
) &&
1382 pgn_piece_to_int(b
[7][4].icon
) == KING
&& isupper(piece
)) {
1384 TOGGLE_FLAG(g
->flags
, GF_WK_CASTLE
);
1387 else if (pgn_piece_to_int(piece
) == ROOK
&& col
== 0
1389 (TEST_FLAG(g
->flags
, GF_WQ_CASTLE
) || mod
) &&
1390 pgn_piece_to_int(b
[7][4].icon
) == KING
&& isupper(piece
)) {
1392 TOGGLE_FLAG(g
->flags
, GF_WQ_CASTLE
);
1395 else if (pgn_piece_to_int(piece
) == ROOK
&& col
== 7
1397 (TEST_FLAG(g
->flags
, GF_BK_CASTLE
) || mod
) &&
1398 pgn_piece_to_int(b
[0][4].icon
) == KING
&& islower(piece
)) {
1400 TOGGLE_FLAG(g
->flags
, GF_BK_CASTLE
);
1403 else if (pgn_piece_to_int(piece
) == ROOK
&& col
== 0
1405 (TEST_FLAG(g
->flags
, GF_BQ_CASTLE
) || mod
) &&
1406 pgn_piece_to_int(b
[0][4].icon
) == KING
&& islower(piece
)) {
1408 TOGGLE_FLAG(g
->flags
, GF_BQ_CASTLE
);
1411 else if (pgn_piece_to_int(piece
) == KING
&& col
== 4
1413 (mod
|| (pgn_piece_to_int(b
[7][7].icon
) == ROOK
&&
1414 TEST_FLAG(g
->flags
, GF_WK_CASTLE
))
1416 (pgn_piece_to_int(b
[7][0].icon
) == ROOK
&&
1417 TEST_FLAG(g
->flags
, GF_WQ_CASTLE
))) && isupper(piece
)) {
1419 if (TEST_FLAG(g
->flags
, GF_WK_CASTLE
) ||
1420 TEST_FLAG(g
->flags
, GF_WQ_CASTLE
))
1421 CLEAR_FLAG(g
->flags
, GF_WK_CASTLE
|GF_WQ_CASTLE
);
1423 SET_FLAG(g
->flags
, GF_WK_CASTLE
|GF_WQ_CASTLE
);
1427 else if (pgn_piece_to_int(piece
) == KING
&& col
== 4
1429 (mod
|| (pgn_piece_to_int(b
[0][7].icon
) == ROOK
&&
1430 TEST_FLAG(g
->flags
, GF_BK_CASTLE
))
1432 (pgn_piece_to_int(b
[0][0].icon
) == ROOK
&&
1433 TEST_FLAG(g
->flags
, GF_BQ_CASTLE
))) && islower(piece
)) {
1435 if (TEST_FLAG(g
->flags
, GF_BK_CASTLE
) ||
1436 TEST_FLAG(g
->flags
, GF_BQ_CASTLE
))
1437 CLEAR_FLAG(g
->flags
, GF_BK_CASTLE
|GF_BQ_CASTLE
);
1439 SET_FLAG(g
->flags
, GF_BK_CASTLE
|GF_BQ_CASTLE
);
1447 static void draw_board(GAME
*g
, int details
)
1450 int bcol
= 0, brow
= 0;
1451 int maxy
= BOARD_HEIGHT
, maxx
= BOARD_WIDTH
;
1452 int ncols
= 0, offset
= 1;
1453 unsigned coords_y
= 8;
1455 for (row
= 0; row
< maxy
; row
++) {
1458 for (col
= 0; col
< maxx
; col
++) {
1461 unsigned char piece
;
1463 if (row
== 0 || row
== maxy
- 2) {
1465 mvwaddch(boardw
, row
, col
,
1466 LINE_GRAPHIC((row
) ?
1467 ACS_LLCORNER
| CP_BOARD_GRAPHICS
:
1468 ACS_ULCORNER
| CP_BOARD_GRAPHICS
));
1469 else if (col
== maxx
- 2)
1470 mvwaddch(boardw
, row
, col
,
1471 LINE_GRAPHIC((row
) ?
1472 ACS_LRCORNER
| CP_BOARD_GRAPHICS
:
1473 ACS_URCORNER
| CP_BOARD_GRAPHICS
));
1474 else if (!(col
% 4))
1475 mvwaddch(boardw
, row
, col
,
1476 LINE_GRAPHIC((row
) ?
1477 ACS_BTEE
| CP_BOARD_GRAPHICS
:
1478 ACS_TTEE
| CP_BOARD_GRAPHICS
));
1480 if (col
!= maxx
- 1)
1481 mvwaddch(boardw
, row
, col
,
1482 LINE_GRAPHIC(ACS_HLINE
| CP_BOARD_GRAPHICS
));
1488 if ((row
% 2) && col
== maxx
- 1 && coords_y
) {
1489 wattron(boardw
, CP_BOARD_COORDS
);
1490 mvwprintw(boardw
, row
, col
, "%d", coords_y
--);
1491 wattroff(boardw
, CP_BOARD_COORDS
);
1495 if ((col
== 0 || col
== maxx
- 2) && row
!= maxy
- 1) {
1497 mvwaddch(boardw
, row
, col
,
1498 LINE_GRAPHIC((col
) ?
1499 ACS_RTEE
| CP_BOARD_GRAPHICS
:
1500 ACS_LTEE
| CP_BOARD_GRAPHICS
));
1502 mvwaddch(boardw
, row
, col
,
1503 LINE_GRAPHIC(ACS_VLINE
| CP_BOARD_GRAPHICS
));
1508 if ((row
% 2) && !(col
% 4) && row
!= maxy
- 1) {
1509 mvwaddch(boardw
, row
, col
,
1510 LINE_GRAPHIC(ACS_VLINE
| CP_BOARD_GRAPHICS
));
1514 if (!(col
% 4) && row
!= maxy
- 1) {
1515 mvwaddch(boardw
, row
, col
,
1516 LINE_GRAPHIC(ACS_PLUS
| CP_BOARD_GRAPHICS
));
1527 if (((ncols
% 2) && !(offset
% 2)) || (!(ncols
% 2)
1533 if (config
.validmoves
&& g
->b
[brow
][bcol
].valid
) {
1534 attrs
= (attrwhich
== WHITE
) ? CP_BOARD_MOVES_WHITE
:
1535 CP_BOARD_MOVES_BLACK
;
1538 attrs
= (attrwhich
== WHITE
) ? CP_BOARD_WHITE
:
1541 if (row
== ROWTOMATRIX(c_row
) && col
==
1542 COLTOMATRIX(c_col
)) {
1543 attrs
= CP_BOARD_CURSOR
;
1546 if (row
== ROWTOMATRIX(sp
.row
) &&
1547 col
== COLTOMATRIX(sp
.col
)) {
1548 attrs
= CP_BOARD_SELECTED
;
1551 if (row
== maxy
- 1)
1554 mvwaddch(boardw
, row
, col
, ' ' | attrs
);
1556 if (row
== maxy
- 1)
1557 waddch(boardw
, x_grid_chars
[bcol
] | CP_BOARD_COORDS
);
1559 if (details
&& g
->b
[row
/ 2][bcol
].enpassant
)
1562 piece
= g
->b
[row
/ 2][bcol
].icon
;
1564 if (details
&& castling_state(g
, g
->b
, brow
, bcol
,
1568 if (g
->side
== WHITE
&& isupper(piece
))
1570 else if (g
->side
== BLACK
&& islower(piece
))
1573 waddch(boardw
, (pgn_piece_to_int(piece
) != OPEN_SQUARE
) ? piece
| attrs
: ' ' | attrs
);
1575 CLEAR_FLAG(attrs
, A_BOLD
);
1576 CLEAR_FLAG(attrs
, A_REVERSE
);
1579 waddch(boardw
, ' ' | attrs
);
1585 if (col
!= maxx
- 1)
1586 mvwaddch(boardw
, row
, col
,
1587 LINE_GRAPHIC(ACS_HLINE
| CP_BOARD_GRAPHICS
));
1595 void invalid_move(int n
, const char *m
)
1597 if (curses_initialized
)
1598 cmessage(ERROR
, ANYKEY
, "%s \"%s\" (round #%i)", E_INVALID_MOVE
, m
, n
);
1600 warnx("%s: %s \"%s\" (round #%i)", loadfile
, E_INVALID_MOVE
, m
, n
);
1603 /* Convert the selected piece to SAN format and validate it. */
1604 static char *board_to_san(GAME
*g
, BOARD b
)
1606 static char str
[MAX_SAN_MOVE_LEN
+ 1], *p
;
1611 snprintf(str
, sizeof(str
), "%c%i%c%i", x_grid_chars
[sp
.col
- 1],
1612 sp
.row
, x_grid_chars
[sp
.destcol
- 1], sp
.destrow
);
1615 piece
= pgn_piece_to_int(b
[ROWTOBOARD(sp
.row
)][COLTOBOARD(sp
.col
)].icon
);
1617 if (piece
== PAWN
&& ((sp
.destrow
== 8 && g
->turn
== WHITE
) ||
1618 (sp
.destrow
== 1 && g
->turn
== BLACK
))) {
1619 promo
= cmessage(PROMOTION_TITLE
, PROMOTION_PROMPT
, PROMOTION_TEXT
);
1621 if (pgn_piece_to_int(promo
) == -1)
1624 p
= str
+ strlen(str
);
1625 *p
++ = toupper(promo
);
1629 memcpy(oldboard
, b
, sizeof(BOARD
));
1631 if ((p
= pgn_a2a4tosan(g
, b
, str
)) == NULL
) {
1632 cmessage(p
, ANYKEY
, "%s", E_A2A4_PARSE
);
1633 memcpy(b
, oldboard
, sizeof(BOARD
));
1637 if (pgn_validate_move(g
, b
, p
)) {
1638 invalid_move(gindex
+ 1, p
);
1639 memcpy(b
, oldboard
, sizeof(BOARD
));
1646 static int move_to_engine(GAME
*g
, BOARD b
)
1650 if ((p
= board_to_san(g
, b
)) == NULL
)
1653 sp
.row
= sp
.col
= sp
.icon
= 0;
1658 SET_FLAG(g
->flags
, GF_MODIFIED
);
1663 send_to_engine(g
, "%s\n", p
);
1667 static void update_clock(int n
, int *h
, int *m
, int *s
)
1670 *m
= (n
% 3600) / 60;
1671 *s
= (n
% 3600) % 60;
1676 void update_status_window(GAME g
)
1680 char tmp
[15], *engine
, *mode
;
1686 struct userdata_s
*d
= g
.data
;
1688 getmaxyx(statusw
, maxy
, maxx
);
1696 if (TEST_FLAG(g
.flags
, GF_DELETE
)) {
1702 if (TEST_FLAG(g
.flags
, GF_PERROR
)) {
1712 if (TEST_FLAG(g
.flags
, GF_MODIFIED
)) {
1727 mvwprintw(statusw
, 2, 1, "%*s %-*s", 7, STATUS_FILE_STR
, w
,
1728 (loadfile
[0]) ? str_etc(loadfile
, w
, 1) : UNAVAILABLE
);
1729 snprintf(buf
, len
, "%i %s %i %s", gindex
+ 1, N_OF_N_STR
, gtotal
,
1731 mvwprintw(statusw
, 3, 1, "%*s %-*s", 7, STATUS_GAME_STR
, w
, buf
);
1735 mode
= MODE_HISTORY_STR
;
1738 mode
= MODE_EDIT_STR
;
1741 mode
= MODE_PLAY_STR
;
1748 mvwprintw(statusw
, 4, 1, "%*s %-*s", 7, STATUS_MODE_STR
, w
, mode
);
1751 switch (d
->engine
->status
) {
1752 case ENGINE_THINKING
:
1753 engine
= ENGINE_THINKING_STR
;
1756 engine
= ENGINE_READY_STR
;
1758 case ENGINE_INITIALIZING
:
1759 engine
= ENGINE_INITIALIZING_STR
;
1761 case ENGINE_OFFLINE
:
1762 engine
= ENGINE_OFFLINE_STR
;
1770 engine
= ENGINE_OFFLINE_STR
;
1772 mvwprintw(statusw
, 5, 1, "%*s %-*s", 7, STATUS_ENGINE_STR
, w
, " ");
1773 wattron(statusw
, CP_STATUS_ENGINE
);
1775 if (TEST_FLAG(d
->flags
, CF_ENGINE_LOOP
)) {
1776 snprintf(buf
, len
- 1, "%s (loop)", engine
);
1777 mvwaddstr(statusw
, 5, 9, buf
);
1780 mvwaddstr(statusw
, 5, 9, engine
);
1782 wattroff(statusw
, CP_STATUS_ENGINE
);
1784 mvwprintw(statusw
, 6, 1, "%*s %-*s", 7, STATUS_TURN_STR
, w
,
1785 (g
.turn
== WHITE
) ? WHITE_STR
: BLACK_STR
);
1787 strncpy(tmp
, WHITE_STR
, sizeof(tmp
));
1788 tmp
[0] = toupper(tmp
[0]);
1789 update_clock(g
.moveclock
, &h
, &m
, &s
);
1790 snprintf(buf
, len
, "%.2i:%.2i:%.2i", h
, m
, s
);
1791 mvwprintw(statusw
, 7, 1, "%*s: %-*s", 6, tmp
, w
, buf
);
1793 strncpy(tmp
, BLACK_STR
, sizeof(tmp
));
1794 tmp
[0] = toupper(tmp
[0]);
1795 update_clock(g
.moveclock
, &h
, &m
, &s
);
1796 snprintf(buf
, len
, "%.2i:%.2i:%.2i", h
, m
, s
);
1797 mvwprintw(statusw
, 8, 1, "%*s: %-*s", 6, tmp
, w
, buf
);
1800 for (i
= 1; i
< maxx
- 4; i
++)
1801 mvwprintw(statusw
, maxy
- 2, i
, " ");
1804 status
.notify
= strdup(GAME_HELP_PROMPT
);
1806 wattron(statusw
, CP_STATUS_NOTIFY
);
1807 mvwprintw(statusw
, maxy
- 2, CENTERX(maxx
, status
.notify
), "%s",
1809 wattroff(statusw
, CP_STATUS_NOTIFY
);
1812 void update_history_window(GAME g
)
1814 char buf
[HISTORY_WIDTH
- 1];
1817 int t
= history_total(g
.hp
);
1819 n
= (g
.hindex
+ 1) / 2;
1822 total
= (t
+ 1) / 2;
1827 snprintf(buf
, sizeof(buf
), "%u %s %u%s", n
, N_OF_N_STR
, total
,
1828 (movestep
== 1) ? HISTORY_PLY_STEP
: "");
1830 strncpy(buf
, UNAVAILABLE
, sizeof(buf
));
1832 mvwprintw(historyw
, 2, 1, "%*s %-*s", 10, HISTORY_MOVE_STR
,
1833 HISTORY_WIDTH
- 13, buf
);
1835 h
= history_by_n(g
.hp
, g
.hindex
);
1836 snprintf(buf
, sizeof(buf
), "%s", (h
&& h
->move
) ? h
->move
: UNAVAILABLE
);
1839 if (h
&& ((h
->comment
) || h
->nag
[0])) {
1840 strncat(buf
, " (v", sizeof(buf
));
1845 strncat(buf
, (n
) ? ",+" : " (+", sizeof(buf
));
1850 strncat(buf
, (n
) ? ",-" : " (-", sizeof(buf
));
1855 strncat(buf
, ")", sizeof(buf
));
1857 mvwprintw(historyw
, 3, 1, "%s %-*s", HISTORY_MOVE_NEXT_STR
,
1858 HISTORY_WIDTH
- 13, buf
);
1860 h
= history_by_n(g
.hp
, game
[gindex
].hindex
- 1);
1861 snprintf(buf
, sizeof(buf
), "%s", (h
&& h
->move
) ? h
->move
: UNAVAILABLE
);
1864 if (h
&& ((h
->comment
) || h
->nag
[0])) {
1865 strncat(buf
, " (V", sizeof(buf
));
1870 strncat(buf
, (n
) ? ",+" : " (+", sizeof(buf
));
1875 strncat(buf
, (n
) ? ",-" : " (-", sizeof(buf
));
1880 strncat(buf
, ")", sizeof(buf
));
1882 mvwprintw(historyw
, 4, 1, "%s %-*s", HISTORY_MOVE_PREV_STR
,
1883 HISTORY_WIDTH
- 13, buf
);
1886 void update_tag_window(TAG
**t
)
1889 int w
= TAG_WIDTH
- 10;
1891 for (i
= 0; i
< 7; i
++)
1892 mvwprintw(tagw
, (i
+ 2), 1, "%*s: %-*s", 6, t
[i
]->name
, w
, t
[i
]->value
);
1895 void draw_prompt(WINDOW
*win
, int y
, int width
, const char *str
, chtype attr
)
1901 for (i
= 1; i
< width
- 1; i
++)
1902 mvwaddch(win
, y
, i
, ' ');
1904 mvwprintw(win
, y
, CENTERX(width
, str
), "%s", str
);
1905 wattroff(win
, attr
);
1908 void draw_window_title(WINDOW
*win
, const char *title
, int width
, chtype attr
,
1916 for (i
= 1; i
< width
- 1; i
++)
1917 mvwaddch(win
, 1, i
, ' ');
1919 mvwprintw(win
, 1, CENTERX(width
, title
), "%s", title
);
1920 wattroff(win
, attr
);
1923 wattron(win
, battr
);
1924 box(win
, ACS_VLINE
, ACS_HLINE
);
1925 wattroff(win
, battr
);
1934 void update_all(GAME g
)
1936 update_status_window(g
);
1937 update_history_window(g
);
1938 update_tag_window(g
.tag
);
1941 static void game_next_prev(GAME g
, int n
, int count
)
1947 if (gindex
+ count
> gtotal
- 1) {
1949 gindex
= gtotal
- 1;
1957 if (gindex
- count
< 0) {
1961 gindex
= gtotal
- 1;
1968 static void delete_game(int which
)
1974 for (i
= 0; i
< gtotal
; i
++) {
1975 if (i
== which
|| TEST_FLAG(game
[i
].flags
, GF_DELETE
)) {
1980 g
= Realloc(g
, (gi
+ 1) * sizeof(GAME
));
1981 memcpy(&g
[gi
], &game
[i
], sizeof(GAME
));
1982 g
[gi
].tag
= game
[i
].tag
;
1983 g
[gi
].history
= game
[i
].history
;
1984 g
[gi
].hp
= game
[i
].hp
;
1992 if (which
+ 1 >= gtotal
)
1993 gindex
= gtotal
- 1;
1998 gindex
= gtotal
- 1;
2000 game
[gindex
].hp
= game
[gindex
].history
;
2003 static int find_move_exp(GAME g
, const char *str
, int init
, int which
,
2009 static int firstrun
= 1;
2018 if ((ret
= regcomp(&r
, str
, REG_EXTENDED
|REG_NOSUB
)) != 0) {
2019 regerror(ret
, &r
, errbuf
, sizeof(errbuf
));
2020 cmessage(E_REGCOMP_TITLE
, ANYKEY
, "%s", errbuf
);
2027 incr
= (which
== 0) ? -1 : 1;
2029 for (i
= g
.hindex
+ incr
- 1, found
= 0; ; i
+= incr
) {
2030 if (i
== g
.hindex
- 1)
2033 if (i
>= history_total(g
.hp
))
2036 i
= history_total(g
.hp
) - 1;
2039 ret
= regexec(&r
, g
.hp
[i
]->move
, 0, 0, 0);
2042 if (count
== ++found
) {
2047 if (ret
!= REG_NOMATCH
) {
2048 regerror(ret
, &r
, errbuf
, sizeof(errbuf
));
2049 cmessage(E_REGEXEC_TITLE
, ANYKEY
, "%s", errbuf
);
2058 static int toggle_delete_flag(int n
)
2062 TOGGLE_FLAG(game
[n
].flags
, GF_DELETE
);
2064 for (i
= x
= 0; i
< gtotal
; i
++) {
2065 if (TEST_FLAG(game
[i
].flags
, GF_DELETE
))
2070 cmessage(NULL
, ANYKEY
, "%s", E_DELETE_GAME
);
2071 CLEAR_FLAG(game
[n
].flags
, GF_DELETE
);
2078 static void edit_save_tags(GAME
*g
)
2082 if ((t
= edit_tags(*g
, g
->b
, 1)) == NULL
)
2085 pgn_tag_free(g
->tag
);
2087 SET_FLAG(g
->flags
, GF_MODIFIED
);
2088 pgn_sort_tags(g
->tag
);
2091 static int find_game_exp(char *str
, int which
, int count
)
2093 char *nstr
= NULL
, *exp
= NULL
;
2097 char buf
[255], *tmp
;
2100 int incr
= (which
== 0) ? -(1) : 1;
2102 strncpy(buf
, str
, sizeof(buf
));
2105 if (strstr(tmp
, ":") != NULL
) {
2106 nstr
= strsep(&tmp
, ":");
2108 if ((ret
= regcomp(&nexp
, nstr
,
2109 REG_ICASE
|REG_EXTENDED
|REG_NOSUB
)) != 0) {
2110 regerror(ret
, &nexp
, errbuf
, sizeof(errbuf
));
2111 cmessage(E_REGCOMP_TITLE
, ANYKEY
, "%s", errbuf
);
2122 if ((ret
= regcomp(&vexp
, exp
, REG_EXTENDED
|REG_NOSUB
)) != 0) {
2123 regerror(ret
, &vexp
, errbuf
, sizeof(errbuf
));
2124 cmessage(E_REGCOMP_TITLE
, ANYKEY
, "%s", errbuf
);
2131 for (g
= gindex
+ incr
, found
= 0; ; g
+= incr
) {
2142 for (t
= 0; game
[g
].tag
[t
]; t
++) {
2144 if (regexec(&nexp
, game
[g
].tag
[t
]->name
, 0, 0, 0) == 0) {
2145 if (regexec(&vexp
, game
[g
].tag
[t
]->value
, 0, 0, 0) == 0) {
2146 if (count
== ++found
) {
2154 if (regexec(&vexp
, game
[g
].tag
[t
]->value
, 0, 0, 0) == 0) {
2155 if (count
== ++found
) {
2177 * Updates the notification line in the status window then refreshes the
2180 void update_status_notify(GAME g
, char *fmt
, ...)
2183 #ifdef HAVE_VASPRINTF
2190 if (status
.notify
) {
2191 free(status
.notify
);
2192 status
.notify
= NULL
;
2194 if (curses_initialized
)
2195 update_status_window(g
);
2202 #ifdef HAVE_VASPRINTF
2203 vasprintf(&line
, fmt
, ap
);
2205 vsnprintf(line
, sizeof(line
), fmt
, ap
);
2210 free(status
.notify
);
2212 status
.notify
= strdup(line
);
2214 #ifdef HAVE_VASPRINTF
2217 if (curses_initialized
)
2218 update_status_window(g
);
2221 static void switch_side(GAME
*g
)
2223 g
->side
= (g
->side
== WHITE
) ? BLACK
: WHITE
;
2226 int rav_next_prev(GAME
*g
, BOARD b
, int n
)
2230 if (g
->hp
[g
->hindex
]->rav
== NULL
)
2233 g
->rav
= Realloc(g
->rav
, (g
->ravlevel
+ 1) * sizeof(RAV
));
2234 g
->rav
[g
->ravlevel
].hp
= g
->hp
;
2235 g
->rav
[g
->ravlevel
].flags
= g
->flags
;
2236 g
->rav
[g
->ravlevel
].fen
= strdup(pgn_game_to_fen(*g
, b
));
2237 g
->rav
[g
->ravlevel
].hindex
= g
->hindex
;
2238 g
->hp
= g
->hp
[g
->hindex
]->rav
;
2244 if (g
->ravlevel
- 1 < 0)
2249 pgn_init_fen_board(g
, b
, g
->rav
[g
->ravlevel
].fen
);
2250 free(g
->rav
[g
->ravlevel
].fen
);
2251 g
->hp
= g
->rav
[g
->ravlevel
].hp
;
2252 g
->flags
= g
->rav
[g
->ravlevel
].flags
;
2253 g
->hindex
= g
->rav
[g
->ravlevel
].hindex
;
2257 static void draw_window_decor()
2259 move_panel(historyp
, LINES
- HISTORY_HEIGHT
, COLS
- HISTORY_WIDTH
);
2260 move_panel(boardp
, 0, COLS
- BOARD_WIDTH
);
2261 wbkgd(boardw
, CP_BOARD_WINDOW
);
2262 wbkgd(statusw
, CP_STATUS_WINDOW
);
2263 draw_window_title(statusw
, STATUS_WINDOW_TITLE
, STATUS_WIDTH
,
2264 CP_STATUS_TITLE
, CP_STATUS_BORDER
);
2265 wbkgd(tagw
, CP_TAG_WINDOW
);
2266 draw_window_title(tagw
, TAG_WINDOW_TITLE
, TAG_WIDTH
, CP_TAG_TITLE
,
2268 wbkgd(historyw
, CP_HISTORY_WINDOW
);
2269 draw_window_title(historyw
, HISTORY_WINDOW_TITLE
, HISTORY_WIDTH
,
2270 CP_HISTORY_TITLE
, CP_HISTORY_BORDER
);
2273 static void do_window_resize()
2275 if (LINES
< 24 || COLS
< 80)
2278 resizeterm(LINES
, COLS
);
2279 wresize(historyw
, HISTORY_HEIGHT
, HISTORY_WIDTH
);
2280 wresize(statusw
, STATUS_HEIGHT
, STATUS_WIDTH
);
2281 wresize(tagw
, TAG_HEIGHT
, TAG_WIDTH
);
2282 wmove(historyw
, 0, 0);
2283 wclrtobot(historyw
);
2286 wmove(statusw
, 0, 0);
2288 draw_window_decor();
2289 update_all(game
[gindex
]);
2292 static void historymode_keys(int);
2293 static void playmode_keys(int c
)
2295 // More keys in MODE_EDIT share keys with MODE_PLAY than don't.
2296 int editmode
= (game
[gindex
].mode
== MODE_EDIT
) ? 1 : 0;
2300 struct userdata_s
*d
= game
[gindex
].data
;
2307 TOGGLE_FLAG(d
->flags
, CF_ENGINE_LOOP
);
2308 update_all(game
[gindex
]);
2314 if (d
->engine
->status
== ENGINE_OFFLINE
)
2317 x
= d
->engine
->status
;
2319 if ((tmp
= get_input_str_clear(ENGINE_CMD_TITLE
, NULL
)) != NULL
)
2320 send_to_engine(&game
[gindex
], "%s\n", tmp
);
2321 d
->engine
->status
= x
;
2325 pushkey
= keycount
= 0;
2326 update_status_notify(game
[gindex
], NULL
);
2328 if (!noengine
&& (!d
->engine
||
2329 d
->engine
->status
== ENGINE_THINKING
)) {
2341 p
= game
[gindex
].b
[ROWTOBOARD(sp
.row
)][COLTOBOARD(sp
.col
)].icon
;
2342 game
[gindex
].b
[ROWTOBOARD(sp
.destrow
)][COLTOBOARD(sp
.destcol
)].icon
= p
;
2343 game
[gindex
].b
[ROWTOBOARD(sp
.row
)][COLTOBOARD(sp
.col
)].icon
= pgn_int_to_piece(game
[gindex
].turn
, OPEN_SQUARE
);
2344 sp
.icon
= sp
.row
= sp
.col
= 0;
2348 if (move_to_engine(&game
[gindex
], game
[gindex
].b
)) {
2349 if (config
.validmoves
)
2350 board_reset_valid_moves(game
[gindex
].b
);
2352 if (TEST_FLAG(game
[gindex
].flags
, GF_GAMEOVER
)) {
2353 CLEAR_FLAG(game
[gindex
].flags
, GF_GAMEOVER
);
2354 SET_FLAG(game
[gindex
].flags
, GF_MODIFIED
);
2360 if (!noengine
&& (!d
->engine
||
2361 d
->engine
->status
== ENGINE_OFFLINE
) && !editmode
) {
2362 if (start_chess_engine(&game
[gindex
]) < 0) {
2369 wtimeout(boardw
, 70);
2371 if (sp
.icon
|| (!editmode
&& d
->engine
&&
2372 d
->engine
->status
== ENGINE_THINKING
)) {
2377 sp
.icon
= mvwinch(boardw
, ROWTOMATRIX(c_row
),
2378 COLTOMATRIX(c_col
)+1) & A_CHARTEXT
;
2380 if (sp
.icon
== ' ') {
2385 if (!editmode
&& ((islower(sp
.icon
) && game
[gindex
].turn
!= BLACK
)
2386 || (isupper(sp
.icon
) && game
[gindex
].turn
!= WHITE
))) {
2387 message(NULL
, ANYKEY
, "%s", E_SELECT_TURN
);
2395 if (!editmode
&& config
.validmoves
)
2396 board_get_valid_moves(&game
[gindex
], game
[gindex
].b
,
2397 pgn_piece_to_int(sp
.icon
), sp
.row
, sp
.col
, &w
, &x
, &y
, &z
);
2402 send_to_engine(&game
[gindex
], "\nswitch\n");
2403 switch_side(&game
[gindex
]);
2404 update_status_window(game
[gindex
]);
2407 /* FIXME dies reading FIFO sometimes. */
2408 if (!history_total(game
[gindex
].hp
))
2411 history_previous(&game
[gindex
], game
[gindex
].b
, (keycount
) ? keycount
* 2 :
2415 if (status
.engine
== CRAFTY
)
2416 SEND_TO_ENGINE("read %s\n", config
.fifo
);
2418 SEND_TO_ENGINE("\npgnload %s\n", config
.fifo
);
2421 update_history_window(game
[gindex
]);
2424 historymode_keys(c
);
2427 board_details
= (board_details
) ? 0 : 1;
2430 paused
= (paused
) ? 0 : 1;
2433 if (!d
->engine
|| d
->engine
->status
== ENGINE_OFFLINE
)
2434 start_chess_engine(&game
[gindex
]);
2436 send_to_engine(&game
[gindex
], "go\n");
2443 static void editmode_keys(int c
)
2453 game
[gindex
].b
[ROWTOBOARD(sp
.row
)][COLTOBOARD(sp
.col
)].icon
= pgn_int_to_piece(game
[gindex
].turn
, OPEN_SQUARE
);
2455 game
[gindex
].b
[ROWTOBOARD(c_row
)][COLTOBOARD(c_col
)].icon
= pgn_int_to_piece(game
[gindex
].turn
, OPEN_SQUARE
);
2457 sp
.icon
= sp
.row
= sp
.col
= 0;
2460 pgn_switch_turn(&game
[gindex
]);
2461 switch_side(&game
[gindex
]);
2462 update_all(game
[gindex
]);
2465 castling_state(&game
[gindex
], game
[gindex
].b
, ROWTOBOARD(c_row
),
2467 game
[gindex
].b
[ROWTOBOARD(c_row
)][COLTOBOARD(c_col
)].icon
, 1);
2470 c
= message(GAME_EDIT_TITLE
, GAME_EDIT_PROMPT
, "%s",
2473 if (pgn_piece_to_int(c
) == -1)
2476 game
[gindex
].b
[ROWTOBOARD(c_row
)][COLTOBOARD(c_col
)].icon
= c
;
2479 if (c_row
== 6 || c_row
== 3) {
2480 pgn_reset_enpassant(game
[gindex
].b
);
2481 game
[gindex
].b
[ROWTOBOARD(c_row
)][COLTOBOARD(c_col
)].enpassant
= 1;
2489 static void historymode_keys(int c
)
2493 static char moveexp
[255] = {0};
2497 movestep
= (movestep
== 1) ? 2 : 1;
2498 update_history_window(game
[gindex
]);
2501 history_next(&game
[gindex
], game
[gindex
].b
, (keycount
> 0) ?
2502 config
.jumpcount
* keycount
* movestep
:
2503 config
.jumpcount
* movestep
);
2504 update_cursor(game
[gindex
], game
[gindex
].hindex
);
2505 update_all(game
[gindex
]);
2508 history_previous(&game
[gindex
], game
[gindex
].b
, (keycount
) ?
2509 config
.jumpcount
* keycount
* movestep
:
2510 config
.jumpcount
* movestep
);
2511 update_cursor(game
[gindex
], game
[gindex
].hindex
);
2512 update_all(game
[gindex
]);
2515 history_previous(&game
[gindex
], game
[gindex
].b
, (keycount
) ?
2516 keycount
* movestep
: movestep
);
2517 update_cursor(game
[gindex
], game
[gindex
].hindex
);
2518 update_all(game
[gindex
]);
2521 history_next(&game
[gindex
], game
[gindex
].b
, (keycount
) ?
2522 keycount
* movestep
: movestep
);
2523 update_cursor(game
[gindex
], game
[gindex
].hindex
);
2524 update_all(game
[gindex
]);
2527 n
= game
[gindex
].hindex
;
2529 if (n
&& game
[gindex
].hp
[n
- 1]->move
)
2535 snprintf(buf
, COLS
- 1, "%s \"%s\"", ANNOTATION_EDIT_TITLE
,
2536 game
[gindex
].hp
[n
]->move
);
2538 tmp
= get_input(buf
, game
[gindex
].hp
[n
]->comment
, 0, 0, NAG_PROMPT
,
2539 history_edit_nag
, (void *)game
[gindex
].hp
[n
], CTRL('T'),
2543 if (!tmp
&& (!game
[gindex
].hp
[n
]->comment
||
2544 !*game
[gindex
].hp
[n
]->comment
))
2546 else if (tmp
&& game
[gindex
].hp
[n
]->comment
) {
2547 if (strcmp(tmp
, game
[gindex
].hp
[n
]->comment
) == 0)
2551 len
= (tmp
) ? strlen(tmp
) + 1 : 1;
2552 game
[gindex
].hp
[n
]->comment
= Realloc(game
[gindex
].hp
[n
]->comment
,
2554 strncpy(game
[gindex
].hp
[n
]->comment
, (tmp
) ? tmp
: "", len
);
2555 SET_FLAG(game
[gindex
].flags
, GF_MODIFIED
);
2556 update_all(game
[gindex
]);
2561 if (history_total(game
[gindex
].hp
) < 2)
2566 if (!*moveexp
|| c
== '/') {
2567 if ((tmp
= get_input(FIND_REGEXP
, moveexp
, 1, 1, NULL
, NULL
, NULL
, 0, -1)) == NULL
)
2570 strncpy(moveexp
, tmp
, sizeof(moveexp
));
2574 if ((n
= find_move_exp(game
[gindex
], moveexp
, n
,
2575 (c
== '[') ? 0 : 1, (keycount
) ? keycount
: 1))
2579 game
[gindex
].hindex
= n
;
2580 history_update_board(&game
[gindex
], game
[gindex
].b
, game
[gindex
].hindex
);
2581 update_all(game
[gindex
]);
2582 update_cursor(game
[gindex
], game
[gindex
].hindex
);
2585 view_annotation(*game
[gindex
].hp
[game
[gindex
].hindex
]);
2588 if (game
[gindex
].hindex
- 1 >= 0)
2589 view_annotation(*game
[gindex
].hp
[game
[gindex
].hindex
- 1]);
2593 rav_next_prev(&game
[gindex
], game
[gindex
].b
, (c
== '-') ? 0 : 1);
2594 update_all(game
[gindex
]);
2597 if (history_total(game
[gindex
].hp
) < 2)
2600 /* FIXME field validation
2601 if ((tmp = get_input(GAME_HISTORY_JUMP_TITLE, NULL, 1, 1,
2602 NULL, NULL, NULL, 0, FIELD_TYPE_INTEGER, 1, 0,
2603 game[gindex].htotal)) == NULL)
2608 if ((tmp
= get_input(GAME_HISTORY_JUMP_TITLE
, NULL
, 1, 1,
2609 NULL
, NULL
, NULL
, 0, -1)) == NULL
)
2612 if (!isinteger(tmp
))
2620 if (n
< 0 || n
> (history_total(game
[gindex
].hp
) / 2))
2623 game
[gindex
].hindex
= (n
) ? n
* 2 - 1 : n
* 2;
2624 history_update_board(&game
[gindex
], game
[gindex
].b
,
2625 game
[gindex
].hindex
);
2626 update_all(game
[gindex
]);
2627 update_cursor(game
[gindex
], game
[gindex
].hindex
);
2634 static void cleanup_all_games()
2638 for (i
= 0; i
< gtotal
; i
++) {
2639 struct userdata_s
*d
;
2642 stop_engine(&game
[i
]);
2645 game
[i
].data
= NULL
;
2650 // Global and other keys.
2651 static int globalkeys(int c
)
2653 static char gameexp
[255] = {0};
2657 char tfile
[FILENAME_MAX
];
2658 struct userdata_s
*d
= game
[gindex
].data
;
2662 if (game
[gindex
].mode
!= MODE_HISTORY
) {
2663 if (!history_total(game
[gindex
].hp
) ||
2664 (d
->engine
&& d
->engine
->status
== ENGINE_THINKING
))
2667 game
[gindex
].mode
= MODE_HISTORY
;
2668 history_update_board(&game
[gindex
], game
[gindex
].b
, history_total(game
[gindex
].hp
));
2673 if (TEST_FLAG(game
[gindex
].flags
, GF_BLACK_OPENING
)) {
2674 cmessage(NULL
, ANYKEY
, "%s", E_RESUME_BLACK
);
2678 // FIXME Resuming from previous history could append to a RAV.
2679 if (game
[gindex
].hindex
!= history_total(game
[gindex
].hp
)) {
2681 if ((c
= message(NULL
, YESNO
, "%s",
2682 GAME_RESUME_HISTORY_TEXT
)) != 'y')
2687 if (TEST_FLAG(game
[gindex
].flags
, GF_GAMEOVER
))
2691 if (!noengine
&& (!d
->engine
||
2692 d
->engine
->status
== ENGINE_OFFLINE
)) {
2693 if (start_chess_engine(&game
[gindex
]) < 0)
2701 oldhistorytotal
= history_total(game
[gindex
].hp
);
2702 game
[gindex
].mode
= MODE_PLAY
;
2703 update_all(game
[gindex
]);
2707 game_next_prev(game
[gindex
], (c
== '>') ? 1 : 0, (keycount
) ?
2716 if (game
[gindex
].mode
!= MODE_EDIT
) {
2717 history_update_board(&game
[gindex
], game
[gindex
].b
, history_total(game
[gindex
].hp
));
2718 update_cursor(game
[gindex
], game
[gindex
].hindex
);
2720 update_all(game
[gindex
]);
2721 update_tag_window(game
[gindex
].tag
);
2723 // Not sure whether to keep these.
2724 case '!': c_row
= 1; return 1;
2725 case '@': c_row
= 2; return 1;
2726 case '#': c_row
= 3; return 1;
2727 case '$': c_row
= 4; return 1;
2728 case '%': c_row
= 5; return 1;
2729 case '^': c_row
= 6; return 1;
2730 case '&': c_row
= 7; return 1;
2731 case '*': c_row
= 8; return 1;
2732 case 'A': c_col
= 1; return 1;
2733 case 'B': c_col
= 2; return 1;
2734 case 'C': c_col
= 3; return 1;
2735 case 'D': c_col
= 4; return 1;
2736 case 'E': c_col
= 5; return 1;
2737 case 'F': c_col
= 6; return 1;
2738 case 'G': c_col
= 7; return 1;
2739 case 'H': c_col
= 8; return 1;
2746 if (!*gameexp
|| c
== '?') {
2747 if ((tmp
= get_input(GAME_FIND_EXPRESSION_TITLE
, gameexp
,
2748 1, 1, GAME_FIND_EXPRESSION_PROMPT
, NULL
,
2749 NULL
, 0, -1)) == NULL
)
2752 strncpy(gameexp
, tmp
, sizeof(gameexp
));
2755 if ((n
= find_game_exp(gameexp
, (c
== '{') ? 0 : 1, (keycount
)
2762 if (history_total(game
[gindex
].hp
))
2763 game
[gindex
].mode
= MODE_HISTORY
;
2765 history_update_board(&game
[gindex
], game
[gindex
].b
, history_total(game
[gindex
].hp
));
2766 update_all(game
[gindex
]);
2767 update_tag_window(game
[gindex
].tag
);
2773 /* FIXME field validation
2774 if ((tmp = get_input(GAME_JUMP_TITLE, NULL, 1, 1, NULL, NULL,
2775 NULL, 0, FIELD_TYPE_INTEGER, 1, 1, gtotal))
2781 if ((tmp
= get_input(GAME_JUMP_TITLE
, NULL
, 1, 1, NULL
,
2782 NULL
, NULL
, 0, -1)) == NULL
)
2785 if (!isinteger(tmp
))
2793 if (--i
> gtotal
- 1 || i
< 0)
2797 history_update_board(&game
[gindex
], game
[gindex
].b
, history_total(game
[gindex
].hp
));
2798 update_cursor(game
[gindex
], game
[gindex
].hindex
);
2799 update_all(game
[gindex
]);
2800 update_tag_window(game
[gindex
].tag
);
2808 if (keycount
&& !delete_count
) {
2811 update_status_notify(game
[gindex
], "%s (delete)",
2816 if (markstart
>= 0 && markend
>= 0) {
2817 if (markstart
> markend
) {
2819 markstart
= markend
;
2823 for (i
= markstart
; i
<= markend
; i
++) {
2824 if (toggle_delete_flag(i
))
2829 if (toggle_delete_flag(gindex
))
2833 markstart
= markend
= -1;
2834 update_status_window(game
[gindex
]);
2838 cmessage(NULL
, ANYKEY
, "%s", E_DELETE_GAME
);
2844 for (i
= n
= 0; i
< gtotal
; i
++) {
2845 if (TEST_FLAG(game
[i
].flags
, GF_DELETE
))
2850 tmp
= GAME_DELETE_GAME_TEXT
;
2853 cmessage(NULL
, ANYKEY
, "%s", E_DELETE_GAME
);
2857 tmp
= GAME_DELETE_ALL_TEXT
;
2860 if (config
.deleteprompt
) {
2861 if ((c
= cmessage(NULL
, YESNO
, "%s", tmp
)) != 'y')
2865 delete_game((!n
) ? gindex
: -1);
2867 if (history_total(game
[gindex
].hp
))
2868 game
[gindex
].mode
= MODE_HISTORY
;
2870 history_update_board(&game
[gindex
], game
[gindex
].b
, history_total(game
[gindex
].hp
));
2871 update_all(game
[gindex
]);
2872 update_tag_window(game
[gindex
].tag
);
2875 edit_save_tags(&game
[gindex
]);
2876 update_all(game
[gindex
]);
2877 update_tag_window(game
[gindex
].tag
);
2880 edit_tags(game
[gindex
], game
[gindex
].b
, 0);
2883 if ((tmp
= get_input(GAME_LOAD_TITLE
, NULL
, 1, 1,
2884 BROWSER_PROMPT
, browse_directory
, NULL
, '\t',
2888 if ((tmp
= word_expand(tmp
)) == NULL
)
2891 if ((fp
= pgn_open(tmp
)) == NULL
) {
2892 cmessage(ERROR
, ANYKEY
, "%s\n%s", tmp
, strerror(errno
));
2899 strncpy(loadfile
, tmp
, sizeof(loadfile
));
2901 if (history_total(game
[gindex
].hp
))
2902 game
[gindex
].mode
= MODE_HISTORY
;
2904 history_update_board(&game
[gindex
], game
[gindex
].b
, history_total(game
[gindex
].hp
));
2905 update_all(game
[gindex
]);
2906 update_tag_window(game
[gindex
].tag
);
2913 n
= message(NULL
, GAME_SAVE_MULTI_PROMPT
, "%s",
2914 GAME_SAVE_MULTI_TEXT
);
2921 update_status_notify(game
[gindex
], "%s", NOTIFY_SAVE_ABORTED
);
2926 if ((tmp
= get_input(GAME_SAVE_TITLE
, loadfile
, 1, 1,
2927 BROWSER_PROMPT
, browse_directory
, NULL
,
2928 '\t', -1)) == NULL
) {
2929 update_status_notify(game
[gindex
], "%s", NOTIFY_SAVE_ABORTED
);
2933 if ((tmp
= word_expand(tmp
)) == NULL
)
2936 if (pgn_is_compressed(tmp
)) {
2937 snprintf(tfile
, sizeof(tfile
), "%s.pgn", tmp
);
2941 if ((p
= strchr(tmp
, '.')) != NULL
) {
2942 if (strcmp(p
, ".pgn") != 0) {
2943 snprintf(tfile
, sizeof(tfile
), "%s.pgn", tmp
);
2948 snprintf(tfile
, sizeof(tfile
), "%s.pgn", tmp
);
2953 if (save_pgn(tmp
, 0, i
)) {
2954 update_status_notify(game
[gindex
], "%s", NOTIFY_SAVE_FAILED
);
2958 update_status_notify(game
[gindex
], "%s", NOTIFY_SAVED
);
2959 update_all(game
[gindex
]);
2965 n
= help(GAME_HELP_INDEX_TITLE
, GAME_HELP_INDEX_PROMPT
,
2970 help(GAME_HELP_HISTORY_TITLE
, ANYKEY
, historyhelp
);
2973 help(GAME_HELP_PLAY_TITLE
, ANYKEY
, playhelp
);
2976 help(GAME_HELP_EDIT_TITLE
, ANYKEY
, edithelp
);
2979 help(GAME_HELP_GAME_TITLE
, ANYKEY
, gamehelp
);
2991 if (cmessage(NULL
, YESNO
, "%s", GAME_NEW_PROMPT
) != 'y')
2997 add_custom_tags(&game
[gindex
].tag
);
2998 d
= Calloc(1, sizeof(struct userdata_s
));
2999 game
[gindex
].data
= d
;
3002 cleanup_all_games();
3004 add_custom_tags(&game
[gindex
].tag
);
3005 pgn_init_board(game
[gindex
].b
);
3006 d
= Calloc(1, sizeof(struct userdata_s
));
3007 game
[gindex
].data
= d
;
3010 game
[gindex
].mode
= MODE_PLAY
;
3011 c_row
= (game
[gindex
].side
== WHITE
) ? 2 : 7;
3013 update_status_notify(game
[gindex
], NULL
);
3014 update_all(game
[gindex
]);
3015 update_tag_window(game
[gindex
].tag
);
3019 keypad(boardw
, TRUE
);
3023 sp
.icon
= sp
.row
= sp
.col
= 0;
3024 markend
= markstart
= 0;
3028 update_status_notify(game
[gindex
], NULL
);
3031 if (config
.validmoves
)
3032 board_reset_valid_moves(game
[gindex
].b
);
3039 keycount
= keycount
* 10 + n
;
3043 update_status_notify(game
[gindex
], "Repeat %i", keycount
);
3046 if (game
[gindex
].mode
== MODE_HISTORY
)
3061 if (game
[gindex
].mode
== MODE_HISTORY
)
3067 update_status_notify(game
[gindex
], NULL
);
3077 if (game
[gindex
].mode
== MODE_HISTORY
)
3092 if (game
[gindex
].mode
== MODE_HISTORY
)
3107 if (game
[gindex
].mode
!= MODE_EDIT
&& game
[gindex
].mode
!=
3111 // Don't edit a running game (for now).
3112 if (history_total(game
[gindex
].hp
))
3115 if (game
[gindex
].mode
!= MODE_EDIT
) {
3116 pgn_init_fen_board(&game
[gindex
], game
[gindex
].b
, NULL
);
3118 game
[gindex
].mode
= MODE_EDIT
;
3119 update_all(game
[gindex
]);
3124 pgn_add_tag(&game
[gindex
].tag
, "FEN",
3125 pgn_game_to_fen(game
[gindex
], game
[gindex
].b
));
3126 pgn_add_tag(&game
[gindex
].tag
, "SetUp", "1");
3127 pgn_sort_tags(game
[gindex
].tag
);
3128 game
[gindex
].mode
= MODE_PLAY
;
3129 update_all(game
[gindex
]);
3139 message("DEBUG BOARD", ANYKEY
, "%s", debug_board(game
[gindex
].b
));
3152 int error_recover
= 0;
3154 c_row
= 2, c_col
= 5;
3155 gindex
= gtotal
- 1;
3157 if (history_total(game
[gindex
].hp
))
3158 game
[gindex
].mode
= MODE_HISTORY
;
3160 game
[gindex
].mode
= MODE_PLAY
;
3162 if (game
[gindex
].mode
== MODE_HISTORY
) {
3163 history_update_board(&game
[gindex
], game
[gindex
].b
,
3164 history_total(game
[gindex
].hp
));
3165 update_cursor(game
[gindex
], game
[gindex
].hindex
);
3168 update_status_notify(game
[gindex
], "%s", GAME_HELP_PROMPT
);
3170 paused
= 1; //FIXME clock
3172 update_all(game
[gindex
]);
3173 update_tag_window(game
[gindex
].tag
);
3178 char fdbuf
[8192] = {0};
3180 struct timeval tv
= {0, 0};
3182 struct userdata_s
*d
= NULL
;
3186 for (i
= 0; i
< gtotal
; i
++) {
3190 if (d
->engine
->fd
[ENGINE_IN_FD
] > 2) {
3191 if (d
->engine
->fd
[ENGINE_IN_FD
] > n
)
3192 n
= d
->engine
->fd
[ENGINE_IN_FD
];
3194 FD_SET(d
->engine
->fd
[ENGINE_IN_FD
], &rfds
);
3200 if ((n
= select(n
+ 1, &rfds
, NULL
, NULL
, &tv
)) > 0) {
3201 for (i
= 0; i
< gtotal
; i
++) {
3205 if (FD_ISSET(d
->engine
->fd
[ENGINE_IN_FD
], &rfds
)) {
3206 len
= read(d
->engine
->fd
[ENGINE_IN_FD
], fdbuf
,
3210 if (errno
!= EAGAIN
) {
3211 cmessage(ERROR
, ANYKEY
, "Engine read(): %s",
3213 waitpid(d
->engine
->pid
, &n
, 0);
3221 parse_engine_output(&game
[i
], fdbuf
);
3222 update_all(game
[gindex
]);
3231 cmessage(ERROR
, ANYKEY
, "select(): %s", strerror(errno
));
3239 draw_board(&game
[gindex
], board_details
);
3240 wmove(boardw
, ROWTOMATRIX(c_row
), COLTOMATRIX(c_col
));
3250 if ((c
= wgetch(boardw
)) == ERR
)
3254 if (!keycount
&& status
.notify
)
3255 update_status_notify(game
[gindex
], NULL
);
3258 if ((n
= globalkeys(c
)) == 1) {
3265 switch (game
[gindex
].mode
) {
3273 historymode_keys(c
);
3283 void usage(const char *pn
, int ret
)
3285 fprintf((ret
) ? stderr
: stdout
, "%s",
3286 "Usage: cboard [-hvNE] [-VtRS] [-p <file>]\n"
3287 " -p Load PGN file.\n"
3288 " -V Validate a game file.\n"
3289 " -S Validate and output a PGN formatted game.\n"
3290 " -R Like -S but write a reduced PGN formatted game.\n"
3291 " -t Also write custom PGN tags from config file.\n"
3292 " -N Don't enable the chess engine (two human players).\n"
3293 " -E Stop processing on file parsing error (overrides config).\n"
3294 " -v Version information.\n"
3295 " -h This help text.\n");
3302 cleanup_all_games();
3305 del_panel(historyp
);
3315 void catch_signal(int which
)
3320 if (which
== SIGPIPE
&& quit
)
3323 if (which
== SIGPIPE
)
3324 cmessage(NULL
, ANYKEY
, "%s", E_BROKEN_PIPE
);
3334 keypad(boardw
, TRUE
);
3344 static void set_defaults()
3347 set_config_defaults();
3350 int main(int argc
, char *argv
[])
3354 char buf
[FILENAME_MAX
];
3355 char datadir
[FILENAME_MAX
];
3356 int ret
= EXIT_SUCCESS
;
3357 int validate_only
= 0, validate_and_write
= 0, reduced
= 0;
3358 int write_custom_tags
= 0;
3362 if ((config
.pwd
= getpwuid(getuid())) == NULL
)
3363 err(EXIT_FAILURE
, "getpwuid()");
3365 snprintf(datadir
, sizeof(datadir
), "%s/.cboard", config
.pwd
->pw_dir
);
3366 snprintf(buf
, sizeof(buf
), "%s/cc.data", datadir
);
3367 config
.ccfile
= strdup(buf
);
3368 snprintf(buf
, sizeof(buf
), "%s/nag.data", datadir
);
3369 config
.nagfile
= strdup(buf
);
3370 snprintf(buf
, sizeof(buf
), "%s/agony.data", datadir
);
3371 config
.agonyfile
= strdup(buf
);
3372 snprintf(buf
, sizeof(buf
), "%s/config", datadir
);
3373 config
.configfile
= strdup(buf
);
3374 snprintf(buf
, sizeof(buf
), "%s/fifo", datadir
);
3375 config
.fifo
= strdup(buf
);
3377 if (stat(datadir
, &st
) == -1) {
3378 if (errno
== ENOENT
) {
3379 if (mkdir(datadir
, 0755) == -1)
3380 err(EXIT_FAILURE
, "%s", datadir
);
3383 err(EXIT_FAILURE
, "%s", datadir
);
3388 if (!S_ISDIR(st
.st_mode
))
3389 errx(EXIT_FAILURE
, "%s: %s", datadir
, E_NOTADIR
);
3391 if (access(config
.fifo
, R_OK
) == -1 && errno
== ENOENT
) {
3392 if (mkfifo(config
.fifo
, 0600) == -1)
3393 err(EXIT_FAILURE
, "%s", config
.fifo
);
3398 while ((opt
= getopt(argc
, argv
, "ENVtSRhp:v")) != -1) {
3401 write_custom_tags
= 1;
3404 config
.stoponerror
= 1;
3412 validate_and_write
= 1;
3417 printf("%s (%s)\n%s\n", PACKAGE_STRING
, curses_version(),
3421 filetype
= PGN_FILE
;
3422 strncpy(loadfile
, optarg
, sizeof(loadfile
));
3426 usage(argv
[0], EXIT_SUCCESS
);
3430 if ((validate_only
|| validate_and_write
) && !*loadfile
)
3431 usage(argv
[0], EXIT_FAILURE
);
3433 if (access(config
.configfile
, R_OK
) == 0)
3434 parse_rcfile(config
.configfile
);
3436 signal(SIGPIPE
, catch_signal
);
3437 signal(SIGCONT
, catch_signal
);
3438 signal(SIGSTOP
, catch_signal
);
3439 signal(SIGINT
, catch_signal
);
3445 if ((fp
= pgn_open(loadfile
)) == NULL
)
3446 err(EXIT_FAILURE
, "%s", loadfile
);
3448 ret
= pgn_parse(fp
);
3451 //ret = parse_fen_file(loadfile);
3453 case EPD_FILE
: // Not implemented.
3456 // No file specified. Empty game.
3457 ret
= pgn_parse(NULL
);
3458 add_custom_tags(&game
[gindex
].tag
);
3462 if (validate_only
|| validate_and_write
) {
3463 if (validate_and_write
) {
3464 for (i
= 0; i
< gtotal
; i
++) {
3465 if (write_custom_tags
)
3466 add_custom_tags(&game
[i
].tag
);
3468 pgn_write(stdout
, game
[i
]);
3478 for (i
= 0; i
< gtotal
; i
++) {
3479 struct userdata_s
*d
= NULL
;
3481 d
= Calloc(1, sizeof(struct userdata_s
));
3485 if (initscr() == NULL
)
3486 errx(EXIT_FAILURE
, "%s", E_INITCURSES
);
3488 curses_initialized
= 1;
3490 if (LINES
< 24 || COLS
< 80) {
3492 errx(EXIT_FAILURE
, "Need at least an 80x24 terminal.");
3495 if (has_colors() == TRUE
&& start_color() == OK
)
3498 boardw
= newwin(BOARD_HEIGHT
, BOARD_WIDTH
, 0, COLS
- BOARD_WIDTH
);
3499 boardp
= new_panel(boardw
);
3500 historyw
= newwin(HISTORY_HEIGHT
, HISTORY_WIDTH
, LINES
- HISTORY_HEIGHT
,
3501 COLS
- HISTORY_WIDTH
);
3502 historyp
= new_panel(historyw
);
3503 statusw
= newwin(STATUS_HEIGHT
, STATUS_WIDTH
, LINES
- STATUS_HEIGHT
, 0);
3504 statusp
= new_panel(statusw
);
3505 tagw
= newwin(TAG_HEIGHT
, TAG_WIDTH
, 0, 0);
3506 tagp
= new_panel(tagw
);
3507 keypad(boardw
, TRUE
);
3508 // leaveok(boardw, TRUE);
3509 leaveok(tagw
, TRUE
);
3510 leaveok(statusw
, TRUE
);
3511 leaveok(historyw
, TRUE
);
3515 draw_window_decor();