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
75 static char *str_etc(const char *str
, int maxlen
, int rev
)
77 int len
= strlen(str
);
78 static char buf
[80], *p
= buf
;
81 strncpy(buf
, str
, sizeof(buf
));
90 for (i
= 0; i
< maxlen
+ 3; i
++)
91 *p
++ = buf
[(len
- maxlen
) + i
+ 3];
106 void update_cursor(GAME g
, int idx
)
110 int t
= pgn_history_total(g
.hp
);
111 struct userdata_s
*d
= g
.data
;
114 * If not deincremented then r and c would be the next move.
118 if (idx
> t
|| idx
< 0 || !t
|| !g
.hp
[idx
]->move
) {
119 d
->c_row
= 2, d
->c_col
= 5;
132 d
->c_row
= (g
.turn
== WHITE
) ? 1 : 8;
141 d
->c_row
= ROWTOINT(*p
--);
142 d
->c_col
= COLTOINT(*p
);
145 static int init_nag()
151 if ((fp
= fopen(config
.nagfile
, "r")) == NULL
) {
152 cmessage(ERROR
, ANYKEY
, "%s: %s", config
.nagfile
, strerror(errno
));
156 nags
= Realloc(nags
, 2 * sizeof(char *));
161 if (fscanf(fp
, " %[^\n] ", line
) == 1) {
162 nags
= Realloc(nags
, (i
+ 2) * sizeof(char *));
163 nags
[i
++] = strdup(line
);
170 void set_menu_vars(int c
, int rows
, int items
, int *item
, int *top
)
172 int selected
= *item
;
177 selected
= toppos
= 0;
181 toppos
= items
- rows
+ 1;
184 if (selected
- 1 < 0) {
187 toppos
= selected
- rows
+ 1;
192 if (toppos
&& selected
<= toppos
)
197 if (selected
+ 1 > items
)
198 selected
= toppos
= 0;
202 if (selected
- toppos
>= rows
)
212 toppos
= selected
- rows
+ 1;
220 if (selected
> items
)
223 toppos
= selected
- rows
+ 1;
229 toppos
= selected
- rows
+ 1;
240 int test_nag_selected(unsigned char nag
[], int s
)
244 for (i
= 0; i
< MAX_PGN_NAG
; i
++) {
252 char *history_edit_nag(void *arg
)
259 HISTORY
*anno
= (HISTORY
*)arg
;
264 unsigned char nag
[MAX_PGN_NAG
] = {0};
265 char menubuf
[64] = {0}, *mp
= menubuf
;
272 for (i
= 1, n
= 0; nags
[i
]; i
++) {
281 rows
= (total
+ 4 > (LINES
/ 5) * 4) ? (LINES
/ 5) * 4 : total
+ 4;
283 win
= newwin(rows
, cols
, CALCPOSY(rows
), CALCPOSX(cols
));
284 panel
= new_panel(win
);
290 memcpy(&nag
, &anno
->nag
, sizeof(nag
));
292 for (i
= 0; i
< MAX_PGN_NAG
; i
++) {
303 draw_window_title(win
, NAG_EDIT_TITLE
, cols
, CP_INPUT_TITLE
,
306 for (i
= toppos
, c
= 2; i
< total
&& c
< rows
- 2; i
++, c
++) {
308 wattron(win
, CP_MENU_HIGHLIGHT
);
309 mvwprintw(win
, c
, 1, "%s", (nags
[i
]) ? nags
[i
] : "none");
310 wattroff(win
, CP_MENU_HIGHLIGHT
);
314 if (test_nag_selected(nag
, i
) != -1) {
315 wattron(win
, CP_MENU_SELECTED
);
316 mvwprintw(win
, c
, 1, "%s", (nags
[i
]) ? nags
[i
] : "none");
317 wattroff(win
, CP_MENU_SELECTED
);
321 mvwprintw(win
, c
, 1, "%s", (nags
[i
]) ? nags
[i
] : "none");
324 snprintf(buf
, sizeof(buf
), "NAG %i of %i (%i of %i selected) %s",
325 selected
+ 1, total
, itemcount
, MAX_PGN_NAG
, NAG_EDIT_PROMPT
);
326 draw_prompt(win
, rows
- 2, cols
, buf
, CP_INPUT_PROMPT
);
336 help(NAG_EDIT_HELP
, ANYKEY
, naghelp
);
344 set_menu_vars(c
, rows
- 4, total
- 1, &selected
, &toppos
);
350 for (i
= 0; i
< MAX_PGN_NAG
; i
++)
357 if ((found
= test_nag_selected(nag
, selected
)) != -1) {
362 if (itemcount
+ 1 > MAX_PGN_NAG
)
365 for (i
= 0; i
< MAX_PGN_NAG
; i
++) {
383 if (strlen(menubuf
) + 1 > sizeof(menubuf
) - 1) {
392 for (i
= 0; i
< total
; i
++) {
396 if (strncasecmp(menubuf
, nags
[i
], strlen(menubuf
)) == 0) {
407 set_menu_vars(c
, rows
- 4, total
- 1, &selected
, &toppos
);
413 memcpy(&anno
->nag
, &nag
, sizeof(nag
));
419 static void view_nag(void *arg
)
421 HISTORY
*h
= (HISTORY
*)arg
;
423 char line
[LINE_MAX
] = {0};
426 snprintf(buf
, sizeof(buf
), "Viewing NAG for \"%s\"", h
->move
);
433 for (i
= 0; i
< MAX_PGN_NAG
; i
++) {
437 strncat(line
, nags
[h
->nag
[i
]], sizeof(line
));
438 strncat(line
, "\n", sizeof(line
));
441 line
[strlen(line
) - 1] = 0;
442 message(buf
, ANYKEY
, "%s", line
);
445 void view_annotation(HISTORY h
)
447 char buf
[strlen(h
.move
) + strlen(ANNOTATION_VIEW_TITLE
) + 4];
448 int nag
= 0, comment
= 0;
450 if (h
.comment
&& h
.comment
[0])
456 if (!nag
&& !comment
)
459 snprintf(buf
, sizeof(buf
), "%s \"%s\"", ANNOTATION_VIEW_TITLE
, h
.move
);
462 show_message(buf
, (nag
) ? "Any other key to continue" : ANYKEY
,
463 (nag
) ? "Press 'n' to view NAG" : NULL
,
464 (nag
) ? view_nag
: NULL
, (nag
) ? (void *)&h
: NULL
,
465 (nag
) ? 'n' : 0, "%s", h
.comment
);
467 show_message(buf
, "Any other key to continue", "Press 'n' to view NAG",
468 view_nag
, (void *)&h
, 'n', "%s", "No annotations for this move");
471 static void cleanup(WINDOW
*win
, PANEL
*panel
, struct file_s
*files
)
476 for (i
= 0; files
[i
].name
; i
++) {
489 static int sort_files(const void *a
, const void *b
)
491 const struct file_s
*aa
= a
;
492 const struct file_s
*bb
= b
;
494 return strcmp(aa
->name
, bb
->name
);
497 char *file_browser(void *arg
)
499 char pattern
[FILENAME_MAX
];
500 static char path
[FILENAME_MAX
];
501 static char file
[FILENAME_MAX
];
504 int cursor
= curs_set(0);
505 char menubuf
[64] = {0}, *mp
= menubuf
;
508 if (config
.savedirectory
) {
509 if ((p
= word_expand(config
.savedirectory
)) == NULL
)
512 strncpy(path
, p
, sizeof(path
));
514 if (access(path
, R_OK
) == -1) {
515 cmessage(ERROR
, ANYKEY
, "%s: %s", path
, strerror(errno
));
516 getcwd(path
, sizeof(path
));
520 getcwd(path
, sizeof(path
));
525 * First find directories (including hidden) in the working directory.
526 * Then apply the config.pattern to regular files.
528 if ((p
= word_split_append(path
, '/', ".* *")) == NULL
)
531 strncpy(pattern
, p
, sizeof(pattern
));
540 int len
= strlen(path
);
543 struct file_s
*files
= NULL
;
549 if (wordexp(pattern
, &w
, x
) != 0) {
550 cmessage(ERROR
, ANYKEY
, "Error in pattern\n%s", pattern
);
554 for (i
= 0; i
< w
.we_wordc
; i
++) {
559 if (stat(w
.we_wordv
[i
], &st
) == -1)
562 if ((p
= strrchr(w
.we_wordv
[i
], '/')) != NULL
)
568 if (!S_ISDIR(st
.st_mode
))
571 if (p
[0] == '.' && p
[1] == 0)
575 if (S_ISDIR(st
.st_mode
))
580 files
= Realloc(files
, (n
+ 2) * sizeof(struct file_s
));
581 files
[n
].path
= strdup(w
.we_wordv
[i
]);
582 files
[n
].name
= Malloc(len
);
583 strncpy(files
[n
].name
, p
, len
);
585 if (S_ISDIR(st
.st_mode
))
586 files
[n
].name
[len
- 2] = '/';
588 tp
= localtime(&st
.st_mtime
);
589 strftime(tbuf
, sizeof(tbuf
), "%b %d %T", tp
);
590 snprintf(sbuf
, sizeof(sbuf
), "%9i %s", (int)st
.st_size
, tbuf
);
591 files
[n
].st
= strdup(sbuf
);
592 memset(&files
[++n
], '\0', sizeof(struct file_s
));
598 if ((p
= word_split_append(path
, '/', config
.pattern
)) == NULL
)
601 strncpy(pattern
, p
, sizeof(pattern
));
607 qsort(files
, n
, sizeof(struct file_s
), sort_files
);
609 for (i
= x
= nlen
= 0; i
< n
; i
++) {
610 if (strlen(files
[i
].name
) > nlen
)
611 nlen
= strlen(files
[i
].name
);
613 if (x
< nlen
+ strlen(files
[i
].st
))
614 x
= nlen
+ strlen(files
[i
].st
);
619 if (cols
< strlen(path
))
622 if (cols
< strlen(HELP_PROMPT
))
623 cols
= strlen(HELP_PROMPT
);
629 rows
= (n
+ 4 > (LINES
/ 5) * 4) ? (LINES
/ 5) * 4 : n
+ 4;
631 win
= newwin(rows
, cols
, CALCPOSY(rows
) - 2, CALCPOSX(cols
));
633 panel
= new_panel(win
);
634 draw_window_title(win
, path
, cols
, CP_INPUT_TITLE
, CP_INPUT_BORDER
);
635 draw_prompt(win
, rows
- 2, cols
, HELP_PROMPT
, CP_INPUT_PROMPT
);
644 for (i
= toppos
, c
= 2; i
< n
&& c
< rows
- 2; i
++, c
++) {
646 wattron(win
, CP_MENU_HIGHLIGHT
);
647 mvwprintw(win
, c
, 1, "%-*s %-*s", nlen
, files
[i
].name
,
648 cols
- nlen
- 2 - 1, files
[i
].st
);
649 wattroff(win
, CP_MENU_HIGHLIGHT
);
653 mvwprintw(win
, c
, 1, "%-*s %-*s", nlen
, files
[i
].name
,
654 cols
- nlen
- 2 - 1, files
[i
].st
);
667 set_menu_vars(c
, rows
- 4, n
- 1, &selected
, &toppos
);
675 cleanup(win
, panel
, files
);
680 help(BROWSER_HELP
, ANYKEY
, file_browser_help
);
683 strncpy(path
, "~", sizeof(path
));
684 cleanup(win
, panel
, files
);
688 if ((tmp
= get_input_str_clear(BROWSER_CHDIR_TITLE
, NULL
))
692 if (tmp
[strlen(tmp
) - 1] == '/')
693 tmp
[strlen(tmp
) - 1] = 0;
695 strncpy(path
, tmp
, sizeof(path
));
696 cleanup(win
, panel
, files
);
700 if (strlen(menubuf
) + 1 > sizeof(menubuf
) - 1) {
709 for (i
= 0; i
< n
; i
++) {
710 if (strncasecmp(menubuf
, files
[i
].name
,
711 strlen(menubuf
)) == 0) {
722 set_menu_vars(c
, rows
- 4, n
- 1, &selected
, &toppos
);
730 strncpy(file
, files
[selected
].path
, sizeof(file
));
731 cleanup(win
, panel
, files
);
733 if (stat(file
, &st
) == -1) {
734 cmessage(ERROR
, ANYKEY
, "%s\n%s", file
, strerror(errno
));
738 if (S_ISDIR(st
.st_mode
)) {
739 p
= file
+ strlen(file
) - 2;
741 if (strcmp(p
, "..") == 0) {
742 p
= file
+ strlen(file
) - 3;
745 if ((p
= strrchr(file
, '/')) != NULL
)
746 file
[strlen(file
) - strlen(p
)] = 0;
749 strncpy(path
, file
, sizeof(path
));
753 if (S_ISREG(st
.st_mode
))
756 cmessage(ERROR
, ANYKEY
, "%s\n%s", file
, E_NOTAREGFILE
);
761 return (*file
) ? file
: NULL
;
764 static int init_country_codes()
767 char line
[LINE_MAX
], *s
;
770 if ((fp
= fopen(config
.ccfile
, "r")) == NULL
) {
771 cmessage(ERROR
, ANYKEY
, "%s: %s", config
.ccfile
, strerror(errno
));
775 while ((s
= fgets(line
, sizeof(line
), fp
)) != NULL
) {
778 if ((tmp
= strsep(&s
, " ")) == NULL
)
787 ccodes
= Realloc(ccodes
, (cindex
+ 2) * sizeof(struct country_codes
));
788 strncpy(ccodes
[cindex
].code
, tmp
, sizeof(ccodes
[cindex
].code
));
789 strncpy(ccodes
[cindex
].country
, s
, sizeof(ccodes
[cindex
].country
));
793 memset(&ccodes
[cindex
], '\0', sizeof(struct country_codes
));
799 char *country_codes(void *arg
)
810 char menubuf
[64] = {0}, *mp
= menubuf
;
813 if (init_country_codes())
817 for (i
= n
= 0; ccodes
[i
].code
&& ccodes
[i
].code
[0]; i
++) {
818 n
= strlen(ccodes
[i
].code
) + strlen(ccodes
[i
].country
);
827 if (cols
< strlen(HELP_PROMPT
) + 21)
828 cols
= strlen(HELP_PROMPT
) + 21;
836 rows
= (i
+ 4 > (LINES
/ 5) * 4) ? (LINES
/ 5) * 4 : i
+ 4;
837 win
= newwin(rows
, cols
, CALCPOSY(rows
) - 2, CALCPOSX(cols
));
838 panel
= new_panel(win
);
852 draw_window_title(win
, CC_TITLE
, cols
, CP_INPUT_TITLE
, CP_INPUT_BORDER
);
854 for (i
= toppos
, c
= 2; i
< total
&& c
< rows
- 2; i
++, c
++) {
856 wattron(win
, CP_MENU_HIGHLIGHT
);
857 mvwprintw(win
, c
, 1, "%3s %s", ccodes
[i
].code
,
859 wattroff(win
, CP_MENU_HIGHLIGHT
);
863 mvwprintw(win
, c
, 1, "%3s %s", ccodes
[i
].code
, ccodes
[i
].country
);
866 snprintf(buf
, sizeof(buf
), "%s %i %s %i %s", MENU_ITEM_STR
,
867 selected
+ 1, N_OF_N_STR
, total
, HELP_PROMPT
);
868 draw_prompt(win
, rows
- 2, cols
, buf
, CP_INPUT_PROMPT
);
874 help(CC_KEY_HELP
, ANYKEY
, cc_help
);
882 set_menu_vars(c
, rows
- 4, total
- 1, &selected
, &toppos
);
887 tmp
= ccodes
[selected
].code
;
895 if (strlen(menubuf
) + 1 > sizeof(menubuf
) - 1) {
904 for (i
= 0; i
< total
; i
++) {
905 if (strncasecmp(menubuf
, ccodes
[i
].code
,
906 strlen(menubuf
)) == 0) {
917 set_menu_vars(c
, rows
- 4, n
- 1, &selected
, &toppos
);
928 static void add_custom_tags(TAG
***t
)
931 int total
= pgn_tag_total(config
.tag
);
936 for (i
= 0; i
< total
; i
++)
937 pgn_tag_add(t
, config
.tag
[i
]->name
, config
.tag
[i
]->value
);
942 TAG
**edit_tags(GAME g
, BOARD b
, int edit
)
951 char menubuf
[64] = {0}, *mp
= menubuf
;
953 /* Edit the backup copy, not the original in case the save fails. */
954 len
= pgn_tag_total(g
.tag
);
956 for (n
= 0; n
< len
; n
++)
957 pgn_tag_add(&data
, g
.tag
[n
]->name
, g
.tag
[n
]->value
);
959 data_index
= pgn_tag_total(data
);
970 data_index
= pgn_tag_total(data
);
972 for (i
= cols
= 0, n
= 4; i
< data_index
; i
++) {
973 n
= strlen(data
[i
]->name
);
979 n
+= strlen(data
[i
]->value
);
981 n
+= strlen(UNKNOWN
);
992 /* +14 for the extra prompt info. */
993 if (cols
< strlen(HELP_PROMPT
) + 14 + 2)
994 cols
= strlen(HELP_PROMPT
) + 14 + 2;
996 rows
= (data_index
+ 4 > (LINES
/ 5) * 4) ? (LINES
/ 5) * 4 :
999 win
= newwin(rows
, cols
, CALCPOSY(rows
), CALCPOSX(cols
));
1000 panel
= new_panel(win
);
1005 wbkgd(win
, CP_MENU
);
1006 draw_window_title(win
, (edit
) ? TAG_EDIT_TITLE
: TAG_VIEW_TITLE
,
1007 cols
, (edit
) ? CP_INPUT_TITLE
: CP_MESSAGE_TITLE
,
1008 (edit
) ? CP_INPUT_BORDER
: CP_MESSAGE_BORDER
);
1010 if (selected
>= data_index
- 1)
1011 selected
= data_index
- 1;
1015 TAG
**tmppgn
= NULL
;
1016 char *newtag
= NULL
;
1018 for (i
= toppos
, c
= 2; i
< data_index
&& c
< rows
- 2; i
++, c
++) {
1019 if (i
== selected
) {
1020 wattron(win
, CP_MENU_HIGHLIGHT
);
1021 mvwprintw(win
, c
, 1, "%*s: %-*s", nlen
, data
[i
]->name
,
1022 cols
- nlen
- 2 - 2, (data
[i
]->value
&&
1023 data
[i
]->value
[0]) ? data
[i
]->value
: UNKNOWN
);
1024 wattroff(win
, CP_MENU_HIGHLIGHT
);
1028 mvwprintw(win
, c
, 1, "%*s: %-*s", nlen
, data
[i
]->name
,
1029 cols
- nlen
- 2 - 2, (data
[i
]->value
&&
1030 data
[i
]->value
[0]) ? data
[i
]->value
: UNKNOWN
);
1033 snprintf(buf
, sizeof(buf
), "%s %i %s %i %s", MENU_TAG_STR
,
1034 selected
+ 1, N_OF_N_STR
, data_index
, HELP_PROMPT
);
1035 draw_prompt(win
, rows
- 2, cols
, buf
,
1036 (edit
) ? CP_INPUT_PROMPT
: CP_MESSAGE_PROMPT
);
1045 add_custom_tags(&data
);
1046 selected
= data_index
- 1;
1047 toppos
= data_index
- (rows
- 4);
1052 help(TAG_EDIT_HELP
, ANYKEY
, pgn_edit_help
);
1054 help(TAG_VIEW_HELP
, ANYKEY
, pgn_info_help
);
1060 if (selected
<= 6) {
1061 cmessage(NULL
, ANYKEY
, "%s", E_REMOVE_STR
);
1065 data_index
= pgn_tag_total(data
);
1067 for (i
= 0; i
< data_index
; i
++) {
1071 pgn_tag_add(&tmppgn
, data
[i
]->name
, data
[i
]->value
);
1077 for (i
= 0; tmppgn
[i
]; i
++)
1078 pgn_tag_add(&data
, tmppgn
[i
]->name
, tmppgn
[i
]->value
);
1080 pgn_tag_free(tmppgn
);
1082 if (selected
>= data_index
)
1083 selected
= data_index
- 1;
1085 if (selected
> rows
- 5)
1086 toppos
= selected
- (rows
- 5);
1088 toppos
-= (toppos
) ? 1 : 0;
1096 if ((newtag
= get_input(TAG_NEW_TITLE
, NULL
, 1, 1, NULL
,
1097 NULL
, NULL
, 0, FIELD_TYPE_PGN_TAG_NAME
))
1101 newtag
[0] = toupper(newtag
[0]);
1103 if (strlen(newtag
) > MAX_VALUE_WIDTH
- 6 -
1104 strlen(PRESS_ENTER
)) {
1105 cmessage(ERROR
, ANYKEY
, "%s", E_TAG_NAMETOOLONG
);
1109 for (i
= 0; i
< data_index
; i
++) {
1110 if (strcasecmp(data
[i
]->name
, newtag
) == 0) {
1116 pgn_tag_add(&data
, newtag
, NULL
);
1117 data_index
= pgn_tag_total(data
);
1118 selected
= data_index
- 1;
1119 set_menu_vars(c
, rows
- 4, data_index
- 1, &selected
,
1129 set_menu_vars(c
, rows
- 4, data_index
- 1, &selected
,
1138 pgn_tag_add(&data
, "FEN", pgn_game_to_fen(g
, b
));
1139 data_index
= pgn_tag_total(data
);
1140 selected
= data_index
- 1;
1141 set_menu_vars(c
, rows
- 4, data_index
- 1, &selected
,
1154 if (strlen(menubuf
) + 1 > sizeof(menubuf
) - 1) {
1163 for (i
= 0; i
< data_index
; i
++) {
1164 if (strncasecmp(menubuf
, data
[i
]->name
, strlen(menubuf
))
1171 if (n
== selected
) {
1176 set_menu_vars(c
, rows
- 4, data_index
- 1, &selected
,
1183 nlen
= strlen(data
[selected
]->name
) + 2;
1184 nlen
+= (edit
) ? strlen(TAG_EDIT_TAG_TITLE
) : strlen(TAG_VIEW_TAG_TITLE
);
1186 if (nlen
> MAX_VALUE_WIDTH
)
1187 snprintf(buf
, sizeof(buf
), "%s", data
[selected
]->name
);
1189 snprintf(buf
, sizeof(buf
), "%s \"%s\"",
1190 (edit
) ? TAG_EDIT_TAG_TITLE
: TAG_VIEW_TAG_TITLE
,
1191 data
[selected
]->name
);
1194 if (!data
[selected
]->value
)
1197 cmessage(buf
, ANYKEY
, "%s", data
[selected
]->value
);
1201 if (strcmp(data
[selected
]->name
, "Date") == 0) {
1202 tmp
= get_input(buf
, data
[selected
]->value
, 0, 0, NULL
, NULL
, NULL
,
1203 0, FIELD_TYPE_PGN_DATE
);
1206 if (strptime(tmp
, PGN_TIME_FORMAT
, &tp
) == NULL
) {
1207 cmessage(ERROR
, ANYKEY
, "%s", E_TAG_DATE_FMT
);
1214 else if (strcmp(data
[selected
]->name
, "Site") == 0) {
1215 tmp
= get_input(buf
, data
[selected
]->value
, 1, 1, CC_PROMPT
,
1216 country_codes
, NULL
, CTRL('t'), -1);
1221 else if (strcmp(data
[selected
]->name
, "Round") == 0) {
1222 tmp
= get_input(buf
, NULL
, 1, 1, NULL
, NULL
, NULL
, 0,
1223 FIELD_TYPE_PGN_ROUND
);
1232 else if (strcmp(data
[selected
]->name
, "Result") == 0) {
1233 tmp
= get_input(buf
, data
[selected
]->value
, 1, 1, NULL
, NULL
, NULL
,
1240 tmp
= (data
[selected
]->value
) ? data
[selected
]->value
: NULL
;
1241 tmp
= get_input(buf
, tmp
, 0, 0, NULL
, NULL
, NULL
, 0, -1);
1244 len
= (tmp
) ? strlen(tmp
) + 1 : 1;
1245 data
[selected
]->value
= Realloc(data
[selected
]->value
, len
);
1246 strncpy(data
[selected
]->value
, (tmp
) ? tmp
: "", len
);
1264 /* If the saveindex argument is -1, all games will be saved. Otherwise it's a
1265 * game index number.
1267 int save_pgn(const char *filename
, int saveindex
)
1272 char buf
[FILENAME_MAX
];
1275 char *command
= NULL
;
1276 int saveindex_max
= (saveindex
== -1) ? gtotal
: saveindex
+ 1;
1278 if (filename
[0] != '/' && config
.savedirectory
) {
1279 if (stat(config
.savedirectory
, &st
) == -1) {
1280 if (errno
== ENOENT
) {
1281 if (mkdir(config
.savedirectory
, 0755) == -1) {
1282 cmessage(ERROR
, ANYKEY
, "%s: %s", config
.savedirectory
,
1288 cmessage(ERROR
, ANYKEY
, "%s: %s", config
.savedirectory
,
1294 stat(config
.savedirectory
, &st
);
1296 if (!S_ISDIR(st
.st_mode
)) {
1297 cmessage(ERROR
, ANYKEY
, "%s: %s", config
.savedirectory
, E_NOTADIR
);
1301 snprintf(buf
, sizeof(buf
), "%s/%s", config
.savedirectory
, filename
);
1305 if (access(filename
, W_OK
) == 0) {
1306 c
= cmessage(NULL
, GAME_SAVE_OVERWRITE_PROMPT
,
1307 "%s \"%s\"", E_FILEEXISTS
, filename
);
1311 if (pgn_is_compressed(filename
) == E_PGN_OK
) {
1312 cmessage(NULL
, ANYKEY
, "%s", E_SAVE_COMPRESS
);
1329 if ((fp
= popen(command
, "w")) == NULL
) {
1330 cmessage(ERROR
, ANYKEY
, "%s: %s", filename
, strerror(errno
));
1335 if ((fp
= fopen(filename
, mode
)) == NULL
) {
1336 cmessage(ERROR
, ANYKEY
, "%s: %s", filename
, strerror(errno
));
1341 for (i
= (saveindex
== -1) ? 0 : saveindex
; i
< saveindex_max
; i
++)
1342 pgn_write(fp
, game
[i
]);
1349 if (saveindex
== -1)
1350 strncpy(loadfile
, filename
, sizeof(loadfile
));
1355 char *random_agony(GAME g
)
1359 char line
[LINE_MAX
];
1361 if (n
== -1 || !config
.agony
|| !curses_initialized
||
1362 (g
.mode
== MODE_HISTORY
&& !config
.historyagony
))
1366 if ((fp
= fopen(config
.agonyfile
, "r")) == NULL
) {
1368 cmessage(ERROR
, ANYKEY
, "%s: %s", config
.agonyfile
, strerror(errno
));
1373 if (fscanf(fp
, " %[^\n] ", line
) == 1) {
1374 agony
= Realloc(agony
, (n
+ 2) * sizeof(char *));
1375 agony
[n
++] = strdup(trim(line
));
1382 if (agony
[0] == NULL
|| !n
) {
1388 return agony
[random() % n
];
1391 static int castling_state(GAME
*g
, BOARD b
, int row
, int col
, int piece
, int mod
)
1393 if (pgn_piece_to_int(piece
) == ROOK
&& col
== 7
1395 (TEST_FLAG(g
->flags
, GF_WK_CASTLE
) || mod
) &&
1396 pgn_piece_to_int(b
[7][4].icon
) == KING
&& isupper(piece
)) {
1398 TOGGLE_FLAG(g
->flags
, GF_WK_CASTLE
);
1401 else if (pgn_piece_to_int(piece
) == ROOK
&& col
== 0
1403 (TEST_FLAG(g
->flags
, GF_WQ_CASTLE
) || mod
) &&
1404 pgn_piece_to_int(b
[7][4].icon
) == KING
&& isupper(piece
)) {
1406 TOGGLE_FLAG(g
->flags
, GF_WQ_CASTLE
);
1409 else if (pgn_piece_to_int(piece
) == ROOK
&& col
== 7
1411 (TEST_FLAG(g
->flags
, GF_BK_CASTLE
) || mod
) &&
1412 pgn_piece_to_int(b
[0][4].icon
) == KING
&& islower(piece
)) {
1414 TOGGLE_FLAG(g
->flags
, GF_BK_CASTLE
);
1417 else if (pgn_piece_to_int(piece
) == ROOK
&& col
== 0
1419 (TEST_FLAG(g
->flags
, GF_BQ_CASTLE
) || mod
) &&
1420 pgn_piece_to_int(b
[0][4].icon
) == KING
&& islower(piece
)) {
1422 TOGGLE_FLAG(g
->flags
, GF_BQ_CASTLE
);
1425 else if (pgn_piece_to_int(piece
) == KING
&& col
== 4
1427 (mod
|| (pgn_piece_to_int(b
[7][7].icon
) == ROOK
&&
1428 TEST_FLAG(g
->flags
, GF_WK_CASTLE
))
1430 (pgn_piece_to_int(b
[7][0].icon
) == ROOK
&&
1431 TEST_FLAG(g
->flags
, GF_WQ_CASTLE
))) && isupper(piece
)) {
1433 if (TEST_FLAG(g
->flags
, GF_WK_CASTLE
) ||
1434 TEST_FLAG(g
->flags
, GF_WQ_CASTLE
))
1435 CLEAR_FLAG(g
->flags
, GF_WK_CASTLE
|GF_WQ_CASTLE
);
1437 SET_FLAG(g
->flags
, GF_WK_CASTLE
|GF_WQ_CASTLE
);
1441 else if (pgn_piece_to_int(piece
) == KING
&& col
== 4
1443 (mod
|| (pgn_piece_to_int(b
[0][7].icon
) == ROOK
&&
1444 TEST_FLAG(g
->flags
, GF_BK_CASTLE
))
1446 (pgn_piece_to_int(b
[0][0].icon
) == ROOK
&&
1447 TEST_FLAG(g
->flags
, GF_BQ_CASTLE
))) && islower(piece
)) {
1449 if (TEST_FLAG(g
->flags
, GF_BK_CASTLE
) ||
1450 TEST_FLAG(g
->flags
, GF_BQ_CASTLE
))
1451 CLEAR_FLAG(g
->flags
, GF_BK_CASTLE
|GF_BQ_CASTLE
);
1453 SET_FLAG(g
->flags
, GF_BK_CASTLE
|GF_BQ_CASTLE
);
1461 static void draw_board(GAME
*g
, int details
)
1464 int bcol
= 0, brow
= 0;
1465 int maxy
= BOARD_HEIGHT
, maxx
= BOARD_WIDTH
;
1466 int ncols
= 0, offset
= 1;
1467 unsigned coords_y
= 8;
1468 struct userdata_s
*d
= g
->data
;
1470 if (g
->mode
!= MODE_PLAY
&& g
->mode
!= MODE_EDIT
)
1471 update_cursor(*g
, g
->hindex
);
1473 for (row
= 0; row
< maxy
; row
++) {
1476 for (col
= 0; col
< maxx
; col
++) {
1479 unsigned char piece
;
1481 if (row
== 0 || row
== maxy
- 2) {
1483 mvwaddch(boardw
, row
, col
,
1484 LINE_GRAPHIC((row
) ?
1485 ACS_LLCORNER
| CP_BOARD_GRAPHICS
:
1486 ACS_ULCORNER
| CP_BOARD_GRAPHICS
));
1487 else if (col
== maxx
- 2)
1488 mvwaddch(boardw
, row
, col
,
1489 LINE_GRAPHIC((row
) ?
1490 ACS_LRCORNER
| CP_BOARD_GRAPHICS
:
1491 ACS_URCORNER
| CP_BOARD_GRAPHICS
));
1492 else if (!(col
% 4))
1493 mvwaddch(boardw
, row
, col
,
1494 LINE_GRAPHIC((row
) ?
1495 ACS_BTEE
| CP_BOARD_GRAPHICS
:
1496 ACS_TTEE
| CP_BOARD_GRAPHICS
));
1498 if (col
!= maxx
- 1)
1499 mvwaddch(boardw
, row
, col
,
1500 LINE_GRAPHIC(ACS_HLINE
| CP_BOARD_GRAPHICS
));
1506 if ((row
% 2) && col
== maxx
- 1 && coords_y
) {
1507 wattron(boardw
, CP_BOARD_COORDS
);
1508 mvwprintw(boardw
, row
, col
, "%d", coords_y
--);
1509 wattroff(boardw
, CP_BOARD_COORDS
);
1513 if ((col
== 0 || col
== maxx
- 2) && row
!= maxy
- 1) {
1515 mvwaddch(boardw
, row
, col
,
1516 LINE_GRAPHIC((col
) ?
1517 ACS_RTEE
| CP_BOARD_GRAPHICS
:
1518 ACS_LTEE
| CP_BOARD_GRAPHICS
));
1520 mvwaddch(boardw
, row
, col
,
1521 LINE_GRAPHIC(ACS_VLINE
| CP_BOARD_GRAPHICS
));
1526 if ((row
% 2) && !(col
% 4) && row
!= maxy
- 1) {
1527 mvwaddch(boardw
, row
, col
,
1528 LINE_GRAPHIC(ACS_VLINE
| CP_BOARD_GRAPHICS
));
1532 if (!(col
% 4) && row
!= maxy
- 1) {
1533 mvwaddch(boardw
, row
, col
,
1534 LINE_GRAPHIC(ACS_PLUS
| CP_BOARD_GRAPHICS
));
1545 if (((ncols
% 2) && !(offset
% 2)) || (!(ncols
% 2)
1551 if (config
.validmoves
&& d
->b
[brow
][bcol
].valid
) {
1552 attrs
= (attrwhich
== WHITE
) ? CP_BOARD_MOVES_WHITE
:
1553 CP_BOARD_MOVES_BLACK
;
1556 attrs
= (attrwhich
== WHITE
) ? CP_BOARD_WHITE
:
1559 if (row
== ROWTOMATRIX(d
->c_row
) && col
==
1560 COLTOMATRIX(d
->c_col
)) {
1561 attrs
= CP_BOARD_CURSOR
;
1564 if (row
== ROWTOMATRIX(d
->sp
.srow
) &&
1565 col
== COLTOMATRIX(d
->sp
.scol
)) {
1566 attrs
= CP_BOARD_SELECTED
;
1569 if (row
== maxy
- 1)
1572 mvwaddch(boardw
, row
, col
, ' ' | attrs
);
1574 if (row
== maxy
- 1)
1575 waddch(boardw
, x_grid_chars
[bcol
] | CP_BOARD_COORDS
);
1577 if (details
&& d
->b
[row
/ 2][bcol
].enpassant
)
1580 piece
= d
->b
[row
/ 2][bcol
].icon
;
1582 if (details
&& castling_state(g
, d
->b
, brow
, bcol
,
1586 if (g
->side
== WHITE
&& isupper(piece
))
1588 else if (g
->side
== BLACK
&& islower(piece
))
1591 waddch(boardw
, (pgn_piece_to_int(piece
) != OPEN_SQUARE
) ? piece
| attrs
: ' ' | attrs
);
1593 CLEAR_FLAG(attrs
, A_BOLD
);
1594 CLEAR_FLAG(attrs
, A_REVERSE
);
1597 waddch(boardw
, ' ' | attrs
);
1603 if (col
!= maxx
- 1)
1604 mvwaddch(boardw
, row
, col
,
1605 LINE_GRAPHIC(ACS_HLINE
| CP_BOARD_GRAPHICS
));
1613 void invalid_move(int n
, const char *m
)
1615 if (curses_initialized
)
1616 cmessage(ERROR
, ANYKEY
, "%s \"%s\" (round #%i)", E_INVALID_MOVE
, m
, n
);
1618 warnx("%s: %s \"%s\" (round #%i)", loadfile
, E_INVALID_MOVE
, m
, n
);
1621 /* Convert the selected piece to SAN format and validate it. */
1622 static char *board_to_san(GAME
*g
, BOARD b
)
1624 static char str
[MAX_SAN_MOVE_LEN
+ 1], *p
;
1627 struct userdata_s
*d
= g
->data
;
1629 snprintf(str
, sizeof(str
), "%c%i%c%i", x_grid_chars
[d
->sp
.scol
- 1],
1630 d
->sp
.srow
, x_grid_chars
[d
->sp
.col
- 1], d
->sp
.row
);
1633 piece
= pgn_piece_to_int(b
[ROWTOBOARD(d
->sp
.srow
)][COLTOBOARD(d
->sp
.scol
)].icon
);
1635 if (piece
== PAWN
&& ((d
->sp
.row
== 8 && g
->turn
== WHITE
) ||
1636 (d
->sp
.row
== 1 && g
->turn
== BLACK
))) {
1637 promo
= cmessage(PROMOTION_TITLE
, PROMOTION_PROMPT
, PROMOTION_TEXT
);
1639 if (pgn_piece_to_int(promo
) == -1)
1642 p
= str
+ strlen(str
);
1643 *p
++ = toupper(promo
);
1649 if (TEST_FLAG(d
->flags
, CF_HUMAN
)) {
1650 if (pgn_parse_move(g
, b
, &p
) != E_PGN_OK
) {
1651 invalid_move(gindex
+ 1, p
);
1658 if (pgn_validate_move(g
, b
, &p
) != E_PGN_OK
) {
1659 invalid_move(gindex
+ 1, p
);
1666 static int move_to_engine(GAME
*g
, BOARD b
)
1669 struct userdata_s
*d
= g
->data
;
1671 if ((p
= board_to_san(g
, b
)) == NULL
)
1674 d
->sp
.srow
= d
->sp
.scol
= d
->sp
.icon
= 0;
1676 if (TEST_FLAG(d
->flags
, CF_HUMAN
)) {
1677 pgn_history_add(g
, p
);
1679 SET_FLAG(g
->flags
, GF_MODIFIED
);
1684 add_engine_command(g
, ENGINE_THINKING
, "%s\n", p
);
1688 static void update_clock(int n
, int *h
, int *m
, int *s
)
1691 *m
= (n
% 3600) / 60;
1692 *s
= (n
% 3600) % 60;
1697 void update_status_window(GAME g
)
1701 char tmp
[15], *engine
, *mode
;
1707 struct userdata_s
*d
= g
.data
;
1709 getmaxyx(statusw
, maxy
, maxx
);
1717 if (TEST_FLAG(g
.flags
, GF_DELETE
)) {
1723 if (TEST_FLAG(g
.flags
, GF_PERROR
)) {
1733 if (TEST_FLAG(g
.flags
, GF_MODIFIED
)) {
1748 mvwprintw(statusw
, 2, 1, "%*s %-*s", 7, STATUS_FILE_STR
, w
,
1749 (loadfile
[0]) ? str_etc(loadfile
, w
, 1) : UNAVAILABLE
);
1750 snprintf(buf
, len
, "%i %s %i %s", gindex
+ 1, N_OF_N_STR
, gtotal
,
1752 mvwprintw(statusw
, 3, 1, "%*s %-*s", 7, STATUS_GAME_STR
, w
, buf
);
1756 mode
= MODE_HISTORY_STR
;
1759 mode
= MODE_EDIT_STR
;
1762 mode
= MODE_PLAY_STR
;
1769 snprintf(buf
, len
- 1, "%*s %s", 7, STATUS_MODE_STR
, mode
);
1771 if (g
.mode
== MODE_PLAY
) {
1772 if (TEST_FLAG(d
->flags
, CF_HUMAN
))
1773 strncat(buf
, " (human/human)", len
- 1);
1774 else if (TEST_FLAG(d
->flags
, CF_ENGINE_LOOP
))
1775 strncat(buf
, " (engine/engine)", len
- 1);
1777 strncat(buf
, " (human/engine)", len
- 1);
1780 mvwprintw(statusw
, 4, 1, "%-*s", len
, buf
);
1783 switch (d
->engine
->status
) {
1784 case ENGINE_THINKING
:
1785 engine
= ENGINE_PONDER_STR
;
1788 engine
= ENGINE_READY_STR
;
1790 case ENGINE_INITIALIZING
:
1791 engine
= ENGINE_INITIALIZING_STR
;
1793 case ENGINE_OFFLINE
:
1794 engine
= ENGINE_OFFLINE_STR
;
1802 engine
= ENGINE_OFFLINE_STR
;
1804 mvwprintw(statusw
, 5, 1, "%*s %-*s", 7, STATUS_ENGINE_STR
, w
, " ");
1805 wattron(statusw
, CP_STATUS_ENGINE
);
1806 mvwaddstr(statusw
, 5, 9, engine
);
1807 wattroff(statusw
, CP_STATUS_ENGINE
);
1809 mvwprintw(statusw
, 6, 1, "%*s %-*s", 7, STATUS_TURN_STR
, w
,
1810 (g
.turn
== WHITE
) ? WHITE_STR
: BLACK_STR
);
1812 strncpy(tmp
, WHITE_STR
, sizeof(tmp
));
1813 tmp
[0] = toupper(tmp
[0]);
1814 update_clock(g
.moveclock
, &h
, &m
, &s
);
1815 snprintf(buf
, len
, "%.2i:%.2i:%.2i", h
, m
, s
);
1816 mvwprintw(statusw
, 7, 1, "%*s: %-*s", 6, tmp
, w
, buf
);
1818 strncpy(tmp
, BLACK_STR
, sizeof(tmp
));
1819 tmp
[0] = toupper(tmp
[0]);
1820 update_clock(g
.moveclock
, &h
, &m
, &s
);
1821 snprintf(buf
, len
, "%.2i:%.2i:%.2i", h
, m
, s
);
1822 mvwprintw(statusw
, 8, 1, "%*s: %-*s", 6, tmp
, w
, buf
);
1825 for (i
= 1; i
< maxx
- 4; i
++)
1826 mvwprintw(statusw
, maxy
- 2, i
, " ");
1829 status
.notify
= strdup(GAME_HELP_PROMPT
);
1831 wattron(statusw
, CP_STATUS_NOTIFY
);
1832 mvwprintw(statusw
, maxy
- 2, CENTERX(maxx
, status
.notify
), "%s",
1834 wattroff(statusw
, CP_STATUS_NOTIFY
);
1837 void update_history_window(GAME g
)
1839 char buf
[HISTORY_WIDTH
- 1];
1842 int t
= pgn_history_total(g
.hp
);
1844 n
= (g
.hindex
+ 1) / 2;
1847 total
= (t
+ 1) / 2;
1852 snprintf(buf
, sizeof(buf
), "%u %s %u%s", n
, N_OF_N_STR
, total
,
1853 (movestep
== 1) ? HISTORY_PLY_STEP
: "");
1855 strncpy(buf
, UNAVAILABLE
, sizeof(buf
));
1857 mvwprintw(historyw
, 2, 1, "%*s %-*s", 10, HISTORY_MOVE_STR
,
1858 HISTORY_WIDTH
- 13, buf
);
1860 h
= pgn_history_by_n(g
.hp
, g
.hindex
);
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
, 3, 1, "%s %-*s", HISTORY_MOVE_NEXT_STR
,
1883 HISTORY_WIDTH
- 13, buf
);
1885 h
= pgn_history_by_n(g
.hp
, game
[gindex
].hindex
- 1);
1886 snprintf(buf
, sizeof(buf
), "%s", (h
&& h
->move
) ? h
->move
: UNAVAILABLE
);
1889 if (h
&& ((h
->comment
) || h
->nag
[0])) {
1890 strncat(buf
, " (V", sizeof(buf
));
1895 strncat(buf
, (n
) ? ",+" : " (+", sizeof(buf
));
1900 strncat(buf
, (n
) ? ",-" : " (-", sizeof(buf
));
1905 strncat(buf
, ")", sizeof(buf
));
1907 mvwprintw(historyw
, 4, 1, "%s %-*s", HISTORY_MOVE_PREV_STR
,
1908 HISTORY_WIDTH
- 13, buf
);
1911 void update_tag_window(TAG
**t
)
1914 int w
= TAG_WIDTH
- 10;
1916 for (i
= 0; i
< 7; i
++)
1917 mvwprintw(tagw
, (i
+ 2), 1, "%*s: %-*s", 6, t
[i
]->name
, w
, t
[i
]->value
);
1920 void draw_prompt(WINDOW
*win
, int y
, int width
, const char *str
, chtype attr
)
1926 for (i
= 1; i
< width
- 1; i
++)
1927 mvwaddch(win
, y
, i
, ' ');
1929 mvwprintw(win
, y
, CENTERX(width
, str
), "%s", str
);
1930 wattroff(win
, attr
);
1933 void draw_window_title(WINDOW
*win
, const char *title
, int width
, chtype attr
,
1941 for (i
= 1; i
< width
- 1; i
++)
1942 mvwaddch(win
, 1, i
, ' ');
1944 mvwprintw(win
, 1, CENTERX(width
, title
), "%s", title
);
1945 wattroff(win
, attr
);
1948 wattron(win
, battr
);
1949 box(win
, ACS_VLINE
, ACS_HLINE
);
1950 wattroff(win
, battr
);
1953 void append_enginebuf(char *line
)
1958 for (i
= 0; enginebuf
[i
]; i
++);
1960 if (i
>= LINES
- 3) {
1963 for (i
= 0; enginebuf
[i
+1]; i
++)
1964 enginebuf
[i
] = enginebuf
[i
+1];
1966 enginebuf
[i
] = strdup(line
);
1969 enginebuf
= Realloc(enginebuf
, (i
+ 2) * sizeof(char *));
1970 enginebuf
[i
++] = strdup(line
);
1971 enginebuf
[i
] = NULL
;
1975 void update_engine_window()
1982 wmove(enginew
, 0, 0);
1986 for (i
= 0; enginebuf
[i
]; i
++)
1987 mvwprintw(enginew
, i
+ 2, 1, "%s", enginebuf
[i
]);
1990 draw_window_title(enginew
, "Engine IO Window", COLS
, CP_MESSAGE_TITLE
,
1994 void toggle_engine_window()
1997 enginew
= newwin(LINES
, COLS
, 0, 0);
1998 enginep
= new_panel(enginew
);
1999 draw_window_title(enginew
, "Engine IO Window", COLS
, CP_MESSAGE_TITLE
,
2001 hide_panel(enginep
);
2004 if (panel_hidden(enginep
)) {
2005 update_engine_window();
2010 hide_panel(enginep
);
2021 void update_all(GAME g
)
2023 update_status_window(g
);
2024 update_history_window(g
);
2025 update_tag_window(g
.tag
);
2026 update_engine_window();
2029 static void game_next_prev(GAME g
, int n
, int count
)
2035 if (gindex
+ count
> gtotal
- 1) {
2037 gindex
= gtotal
- 1;
2045 if (gindex
- count
< 0) {
2049 gindex
= gtotal
- 1;
2056 static void delete_game(int which
)
2062 for (i
= 0; i
< gtotal
; i
++) {
2063 if (i
== which
|| TEST_FLAG(game
[i
].flags
, GF_DELETE
)) {
2068 g
= Realloc(g
, (gi
+ 1) * sizeof(GAME
));
2069 memcpy(&g
[gi
], &game
[i
], sizeof(GAME
));
2070 g
[gi
].tag
= game
[i
].tag
;
2071 g
[gi
].history
= game
[i
].history
;
2072 g
[gi
].hp
= game
[i
].hp
;
2080 if (which
+ 1 >= gtotal
)
2081 gindex
= gtotal
- 1;
2086 gindex
= gtotal
- 1;
2088 game
[gindex
].hp
= game
[gindex
].history
;
2091 static int find_move_exp(GAME g
, const char *str
, int init
, int which
,
2097 static int firstrun
= 1;
2106 if ((ret
= regcomp(&r
, str
, REG_EXTENDED
|REG_NOSUB
)) != 0) {
2107 regerror(ret
, &r
, errbuf
, sizeof(errbuf
));
2108 cmessage(E_REGCOMP_TITLE
, ANYKEY
, "%s", errbuf
);
2115 incr
= (which
== 0) ? -1 : 1;
2117 for (i
= g
.hindex
+ incr
- 1, found
= 0; ; i
+= incr
) {
2118 if (i
== g
.hindex
- 1)
2121 if (i
>= pgn_history_total(g
.hp
))
2124 i
= pgn_history_total(g
.hp
) - 1;
2127 ret
= regexec(&r
, g
.hp
[i
]->move
, 0, 0, 0);
2130 if (count
== ++found
) {
2135 if (ret
!= REG_NOMATCH
) {
2136 regerror(ret
, &r
, errbuf
, sizeof(errbuf
));
2137 cmessage(E_REGEXEC_TITLE
, ANYKEY
, "%s", errbuf
);
2146 static int toggle_delete_flag(int n
)
2150 TOGGLE_FLAG(game
[n
].flags
, GF_DELETE
);
2152 update_all(game
[gindex
]);
2154 for (i
= x
= 0; i
< gtotal
; i
++) {
2155 if (TEST_FLAG(game
[i
].flags
, GF_DELETE
))
2160 cmessage(NULL
, ANYKEY
, "%s", E_DELETE_GAME
);
2161 CLEAR_FLAG(game
[n
].flags
, GF_DELETE
);
2168 static void edit_save_tags(GAME
*g
)
2171 struct userdata_s
*d
= g
->data
;
2173 if ((t
= edit_tags(*g
, d
->b
, 1)) == NULL
)
2176 pgn_tag_free(g
->tag
);
2178 SET_FLAG(g
->flags
, GF_MODIFIED
);
2179 pgn_tag_sort(g
->tag
);
2182 static int find_game_exp(char *str
, int which
, int count
)
2184 char *nstr
= NULL
, *exp
= NULL
;
2188 char buf
[255], *tmp
;
2191 int incr
= (which
== 0) ? -(1) : 1;
2193 strncpy(buf
, str
, sizeof(buf
));
2196 if (strstr(tmp
, ":") != NULL
) {
2197 nstr
= strsep(&tmp
, ":");
2199 if ((ret
= regcomp(&nexp
, nstr
,
2200 REG_ICASE
|REG_EXTENDED
|REG_NOSUB
)) != 0) {
2201 regerror(ret
, &nexp
, errbuf
, sizeof(errbuf
));
2202 cmessage(E_REGCOMP_TITLE
, ANYKEY
, "%s", errbuf
);
2213 if ((ret
= regcomp(&vexp
, exp
, REG_EXTENDED
|REG_NOSUB
)) != 0) {
2214 regerror(ret
, &vexp
, errbuf
, sizeof(errbuf
));
2215 cmessage(E_REGCOMP_TITLE
, ANYKEY
, "%s", errbuf
);
2222 for (g
= gindex
+ incr
, found
= 0; ; g
+= incr
) {
2233 for (t
= 0; game
[g
].tag
[t
]; t
++) {
2235 if (regexec(&nexp
, game
[g
].tag
[t
]->name
, 0, 0, 0) == 0) {
2236 if (regexec(&vexp
, game
[g
].tag
[t
]->value
, 0, 0, 0) == 0) {
2237 if (count
== ++found
) {
2245 if (regexec(&vexp
, game
[g
].tag
[t
]->value
, 0, 0, 0) == 0) {
2246 if (count
== ++found
) {
2268 * Updates the notification line in the status window then refreshes the
2271 void update_status_notify(GAME g
, char *fmt
, ...)
2274 #ifdef HAVE_VASPRINTF
2281 if (status
.notify
) {
2282 free(status
.notify
);
2283 status
.notify
= NULL
;
2285 if (curses_initialized
)
2286 update_status_window(g
);
2293 #ifdef HAVE_VASPRINTF
2294 vasprintf(&line
, fmt
, ap
);
2296 vsnprintf(line
, sizeof(line
), fmt
, ap
);
2301 free(status
.notify
);
2303 status
.notify
= strdup(line
);
2305 #ifdef HAVE_VASPRINTF
2308 if (curses_initialized
)
2309 update_status_window(g
);
2312 int rav_next_prev(GAME
*g
, BOARD b
, int n
)
2316 if (g
->hp
[g
->hindex
]->rav
== NULL
)
2319 g
->rav
= Realloc(g
->rav
, (g
->ravlevel
+ 1) * sizeof(RAV
));
2320 g
->rav
[g
->ravlevel
].hp
= g
->hp
;
2321 g
->rav
[g
->ravlevel
].flags
= g
->flags
;
2322 g
->rav
[g
->ravlevel
].fen
= strdup(pgn_game_to_fen(*g
, b
));
2323 g
->rav
[g
->ravlevel
].hindex
= g
->hindex
;
2324 g
->hp
= g
->hp
[g
->hindex
]->rav
;
2330 if (g
->ravlevel
- 1 < 0)
2335 pgn_board_init_fen(g
, b
, g
->rav
[g
->ravlevel
].fen
);
2336 free(g
->rav
[g
->ravlevel
].fen
);
2337 g
->hp
= g
->rav
[g
->ravlevel
].hp
;
2338 g
->flags
= g
->rav
[g
->ravlevel
].flags
;
2339 g
->hindex
= g
->rav
[g
->ravlevel
].hindex
;
2343 static void draw_window_decor()
2345 move_panel(historyp
, LINES
- HISTORY_HEIGHT
, COLS
- HISTORY_WIDTH
);
2346 move_panel(boardp
, 0, COLS
- BOARD_WIDTH
);
2347 wbkgd(boardw
, CP_BOARD_WINDOW
);
2348 wbkgd(statusw
, CP_STATUS_WINDOW
);
2349 draw_window_title(statusw
, STATUS_WINDOW_TITLE
, STATUS_WIDTH
,
2350 CP_STATUS_TITLE
, CP_STATUS_BORDER
);
2351 wbkgd(tagw
, CP_TAG_WINDOW
);
2352 draw_window_title(tagw
, TAG_WINDOW_TITLE
, TAG_WIDTH
, CP_TAG_TITLE
,
2354 wbkgd(historyw
, CP_HISTORY_WINDOW
);
2355 draw_window_title(historyw
, HISTORY_WINDOW_TITLE
, HISTORY_WIDTH
,
2356 CP_HISTORY_TITLE
, CP_HISTORY_BORDER
);
2359 static void do_window_resize()
2361 if (LINES
< 24 || COLS
< 80)
2364 resizeterm(LINES
, COLS
);
2365 wresize(historyw
, HISTORY_HEIGHT
, HISTORY_WIDTH
);
2366 wresize(statusw
, STATUS_HEIGHT
, STATUS_WIDTH
);
2367 wresize(tagw
, TAG_HEIGHT
, TAG_WIDTH
);
2368 wmove(historyw
, 0, 0);
2369 wclrtobot(historyw
);
2372 wmove(statusw
, 0, 0);
2374 draw_window_decor();
2375 update_all(game
[gindex
]);
2378 static void historymode_keys(chtype
);
2379 static int playmode_keys(chtype c
)
2381 // More keys in MODE_EDIT share keys with MODE_PLAY than don't.
2382 int editmode
= (game
[gindex
].mode
== MODE_EDIT
) ? 1 : 0;
2386 struct userdata_s
*d
= game
[gindex
].data
;
2390 TOGGLE_FLAG(d
->flags
, CF_HUMAN
);
2392 if (!TEST_FLAG(d
->flags
, CF_HUMAN
) &&
2393 pgn_history_total(game
[gindex
].hp
)) {
2394 pgn_tag_add(&game
[gindex
].tag
, "FEN",
2395 pgn_game_to_fen(game
[gindex
], d
->b
));
2396 x
= pgn_tag_find(game
[gindex
].tag
, "FEN");
2398 if (start_chess_engine(&game
[gindex
]) <= 0) {
2399 add_engine_command(&game
[gindex
], ENGINE_READY
,
2400 "setboard %s\n", game
[gindex
].tag
[x
]->value
);
2401 d
->engine
->status
= ENGINE_READY
;
2405 CLEAR_FLAG(d
->flags
, CF_ENGINE_LOOP
);
2408 d
->engine
->status
= ENGINE_READY
;
2410 update_all(game
[gindex
]);
2416 TOGGLE_FLAG(d
->flags
, CF_ENGINE_LOOP
);
2417 CLEAR_FLAG(d
->flags
, CF_HUMAN
);
2419 if (d
->engine
&& !TEST_FLAG(d
->flags
, CF_ENGINE_LOOP
)) {
2420 pgn_board_update(&game
[gindex
], d
->b
,
2421 pgn_history_total(game
[gindex
].hp
));
2422 add_engine_command(&game
[gindex
], ENGINE_READY
,
2423 "setboard %s\n", pgn_game_to_fen(game
[gindex
], d
->b
));
2426 update_all(game
[gindex
]);
2432 if (d
->engine
->status
== ENGINE_OFFLINE
)
2435 x
= d
->engine
->status
;
2437 if ((tmp
= get_input_str_clear(ENGINE_CMD_TITLE
, NULL
)) != NULL
)
2438 send_to_engine(&game
[gindex
], ENGINE_READY
, "%s\n", tmp
);
2439 d
->engine
->status
= x
;
2443 pushkey
= keycount
= 0;
2444 update_status_notify(game
[gindex
], NULL
);
2446 if (!editmode
&& !TEST_FLAG(d
->flags
, CF_HUMAN
) &&
2447 (!d
->engine
|| d
->engine
->status
== ENGINE_THINKING
)) {
2455 d
->sp
.row
= d
->c_row
;
2456 d
->sp
.col
= d
->c_col
;
2459 p
= d
->b
[ROWTOBOARD(d
->sp
.srow
)][COLTOBOARD(d
->sp
.scol
)].icon
;
2460 d
->b
[ROWTOBOARD(d
->sp
.row
)][COLTOBOARD(d
->sp
.col
)].icon
= p
;
2461 d
->b
[ROWTOBOARD(d
->sp
.srow
)][COLTOBOARD(d
->sp
.scol
)].icon
=
2462 pgn_int_to_piece(game
[gindex
].turn
, OPEN_SQUARE
);
2463 d
->sp
.icon
= d
->sp
.srow
= d
->sp
.scol
= 0;
2467 if (move_to_engine(&game
[gindex
], d
->b
)) {
2468 if (config
.validmoves
)
2469 pgn_reset_valid_moves(d
->b
);
2471 if (TEST_FLAG(game
[gindex
].flags
, GF_GAMEOVER
)) {
2472 CLEAR_FLAG(game
[gindex
].flags
, GF_GAMEOVER
);
2473 SET_FLAG(game
[gindex
].flags
, GF_MODIFIED
);
2479 if (!TEST_FLAG(d
->flags
, CF_HUMAN
) && (!d
->engine
||
2480 d
->engine
->status
== ENGINE_OFFLINE
) && !editmode
) {
2481 if (start_chess_engine(&game
[gindex
])) {
2486 x
= pgn_tag_find(game
[gindex
].tag
, "FEN");
2487 w
= pgn_tag_find(game
[gindex
].tag
, "SetUp");
2489 if ((w
>= 0 && x
>= 0 && atoi(game
[gindex
].tag
[w
]->value
) == 1)
2490 || (x
>= 0 && w
== -1)) {
2491 add_engine_command(&game
[gindex
], ENGINE_READY
,
2492 "setboard %s\n", game
[gindex
].tag
[x
]->value
);
2496 if (d
->sp
.icon
|| (!editmode
&& d
->engine
&&
2497 d
->engine
->status
== ENGINE_THINKING
)) {
2502 d
->sp
.icon
= mvwinch(boardw
, ROWTOMATRIX(d
->c_row
),
2503 COLTOMATRIX(d
->c_col
)+1) & A_CHARTEXT
;
2505 if (d
->sp
.icon
== ' ') {
2510 if (!editmode
&& ((islower(d
->sp
.icon
) && game
[gindex
].turn
!= BLACK
)
2511 || (isupper(d
->sp
.icon
) && game
[gindex
].turn
!= WHITE
))) {
2512 if (pgn_history_total(game
[gindex
].hp
)) {
2513 message(NULL
, ANYKEY
, "%s", E_SELECT_TURN
);
2518 add_engine_command(&game
[gindex
], ENGINE_READY
, "black\n");
2519 pgn_switch_turn(&game
[gindex
]);
2521 if (game
[gindex
].side
!= BLACK
)
2522 pgn_switch_side(&game
[gindex
]);
2526 d
->sp
.srow
= d
->c_row
;
2527 d
->sp
.scol
= d
->c_col
;
2529 if (!editmode
&& config
.validmoves
)
2530 pgn_find_valid_moves(game
[gindex
], d
->b
, d
->sp
.scol
, d
->sp
.srow
);
2535 pgn_switch_side(&game
[gindex
]);
2536 add_engine_command(&game
[gindex
], -1,
2537 (game
[gindex
].side
== WHITE
) ? "white\n" : "black\n");
2538 update_status_window(game
[gindex
]);
2541 if (!pgn_history_total(game
[gindex
].hp
))
2544 if (d
->engine
&& d
->engine
->status
== ENGINE_READY
) {
2545 add_engine_command(&game
[gindex
], ENGINE_READY
, "remove\n");
2546 d
->engine
->status
= ENGINE_READY
;
2549 game
[gindex
].hindex
-= 2;
2550 pgn_history_free(game
[gindex
].hp
, game
[gindex
].hindex
);
2551 game
[gindex
].hindex
= pgn_history_total(game
[gindex
].hp
);
2552 pgn_board_update(&game
[gindex
], d
->b
, game
[gindex
].hindex
);
2553 update_history_window(game
[gindex
]);
2556 historymode_keys(c
);
2559 board_details
= (board_details
) ? 0 : 1;
2562 paused
= (paused
) ? 0 : 1;
2565 if (!d
->engine
|| d
->engine
->status
== ENGINE_OFFLINE
)
2566 start_chess_engine(&game
[gindex
]);
2568 add_engine_command(&game
[gindex
], ENGINE_THINKING
, "go\n");
2575 for (x
= 0; config
.keys
[x
]; x
++) {
2576 if (config
.keys
[x
]->c
== c
) {
2577 switch (config
.keys
[x
]->type
) {
2579 add_engine_command(&game
[gindex
], -1, "%s\n",
2580 config
.keys
[x
]->str
);
2586 add_engine_command(&game
[gindex
], -1,
2587 "%s %i\n", config
.keys
[x
]->str
, keycount
);
2594 for (w
= 0; w
< keycount
; w
++)
2595 add_engine_command(&game
[gindex
], -1,
2596 "%s\n", config
.keys
[x
]->str
);
2603 update_status_notify(game
[gindex
], NULL
);
2611 static void editmode_keys(chtype c
)
2613 struct userdata_s
*d
= game
[gindex
].data
;
2623 d
->b
[ROWTOBOARD(d
->sp
.srow
)][COLTOBOARD(d
->sp
.scol
)].icon
=
2624 pgn_int_to_piece(game
[gindex
].turn
, OPEN_SQUARE
);
2626 d
->b
[ROWTOBOARD(d
->c_row
)][COLTOBOARD(d
->c_col
)].icon
=
2627 pgn_int_to_piece(game
[gindex
].turn
, OPEN_SQUARE
);
2629 d
->sp
.icon
= d
->sp
.srow
= d
->sp
.scol
= 0;
2632 pgn_switch_turn(&game
[gindex
]);
2633 update_all(game
[gindex
]);
2636 castling_state(&game
[gindex
], d
->b
, ROWTOBOARD(d
->c_row
),
2637 COLTOBOARD(d
->c_col
),
2638 d
->b
[ROWTOBOARD(d
->c_row
)][COLTOBOARD(d
->c_col
)].icon
, 1);
2641 c
= message(GAME_EDIT_TITLE
, GAME_EDIT_PROMPT
, "%s",
2644 if (pgn_piece_to_int(c
) == -1)
2647 d
->b
[ROWTOBOARD(d
->c_row
)][COLTOBOARD(d
->c_col
)].icon
= c
;
2650 if (d
->c_row
== 6 || d
->c_row
== 3) {
2651 pgn_reset_enpassant(d
->b
);
2652 d
->b
[ROWTOBOARD(d
->c_row
)][COLTOBOARD(d
->c_col
)].enpassant
= 1;
2660 static void historymode_keys(chtype c
)
2664 static char moveexp
[255] = {0};
2665 struct userdata_s
*d
= game
[gindex
].data
;
2669 board_details
= (board_details
) ? 0 : 1;
2672 movestep
= (movestep
== 1) ? 2 : 1;
2673 update_history_window(game
[gindex
]);
2676 pgn_history_next(&game
[gindex
], d
->b
, (keycount
> 0) ?
2677 config
.jumpcount
* keycount
* movestep
:
2678 config
.jumpcount
* movestep
);
2679 update_all(game
[gindex
]);
2682 pgn_history_prev(&game
[gindex
], d
->b
, (keycount
) ?
2683 config
.jumpcount
* keycount
* movestep
:
2684 config
.jumpcount
* movestep
);
2685 update_all(game
[gindex
]);
2688 pgn_history_prev(&game
[gindex
], d
->b
, (keycount
) ?
2689 keycount
* movestep
: movestep
);
2690 update_all(game
[gindex
]);
2693 pgn_history_next(&game
[gindex
], d
->b
, (keycount
) ?
2694 keycount
* movestep
: movestep
);
2695 update_all(game
[gindex
]);
2698 n
= game
[gindex
].hindex
;
2700 if (n
&& game
[gindex
].hp
[n
- 1]->move
)
2706 snprintf(buf
, COLS
- 1, "%s \"%s\"", ANNOTATION_EDIT_TITLE
,
2707 game
[gindex
].hp
[n
]->move
);
2709 tmp
= get_input(buf
, game
[gindex
].hp
[n
]->comment
, 0, 0, NAG_PROMPT
,
2710 history_edit_nag
, (void *)game
[gindex
].hp
[n
], CTRL('T'),
2714 if (!tmp
&& (!game
[gindex
].hp
[n
]->comment
||
2715 !*game
[gindex
].hp
[n
]->comment
))
2717 else if (tmp
&& game
[gindex
].hp
[n
]->comment
) {
2718 if (strcmp(tmp
, game
[gindex
].hp
[n
]->comment
) == 0)
2722 len
= (tmp
) ? strlen(tmp
) + 1 : 1;
2723 game
[gindex
].hp
[n
]->comment
= Realloc(game
[gindex
].hp
[n
]->comment
,
2725 strncpy(game
[gindex
].hp
[n
]->comment
, (tmp
) ? tmp
: "", len
);
2726 SET_FLAG(game
[gindex
].flags
, GF_MODIFIED
);
2727 update_all(game
[gindex
]);
2732 if (pgn_history_total(game
[gindex
].hp
) < 2)
2737 if (!*moveexp
|| c
== '/') {
2738 if ((tmp
= get_input(FIND_REGEXP
, moveexp
, 1, 1, NULL
, NULL
, NULL
, 0, -1)) == NULL
)
2741 strncpy(moveexp
, tmp
, sizeof(moveexp
));
2745 if ((n
= find_move_exp(game
[gindex
], moveexp
, n
,
2746 (c
== '[') ? 0 : 1, (keycount
) ? keycount
: 1))
2750 game
[gindex
].hindex
= n
;
2751 pgn_board_update(&game
[gindex
], d
->b
, game
[gindex
].hindex
);
2752 update_all(game
[gindex
]);
2755 view_annotation(*game
[gindex
].hp
[game
[gindex
].hindex
]);
2758 if (game
[gindex
].hindex
- 1 >= 0)
2759 view_annotation(*game
[gindex
].hp
[game
[gindex
].hindex
- 1]);
2763 rav_next_prev(&game
[gindex
], d
->b
, (c
== '-') ? 0 : 1);
2764 update_all(game
[gindex
]);
2767 if (pgn_history_total(game
[gindex
].hp
) < 2)
2770 /* FIXME field validation
2771 if ((tmp = get_input(GAME_HISTORY_JUMP_TITLE, NULL, 1, 1,
2772 NULL, NULL, NULL, 0, FIELD_TYPE_INTEGER, 1, 0,
2773 game[gindex].htotal)) == NULL)
2778 if ((tmp
= get_input(GAME_HISTORY_JUMP_TITLE
, NULL
, 1, 1,
2779 NULL
, NULL
, NULL
, 0, -1)) == NULL
)
2782 if (!isinteger(tmp
))
2790 if (n
< 0 || n
> (pgn_history_total(game
[gindex
].hp
) / 2))
2793 game
[gindex
].hindex
= (n
) ? n
* 2 - 1 : n
* 2;
2794 pgn_board_update(&game
[gindex
], d
->b
,
2795 game
[gindex
].hindex
);
2796 update_all(game
[gindex
]);
2803 static void free_userdata()
2807 for (i
= 0; i
< gtotal
; i
++) {
2808 struct userdata_s
*d
;
2814 stop_engine(&game
[i
]);
2819 game
[i
].data
= NULL
;
2824 void update_loading_window()
2827 loadingw
= newwin(3, COLS
/ 2, CALCPOSY(3), CALCPOSX(COLS
/ 2));
2828 loadingp
= new_panel(loadingw
);
2829 wbkgd(loadingw
, CP_MESSAGE_WINDOW
);
2832 wmove(loadingw
, 0, 0);
2833 wclrtobot(loadingw
);
2834 wattron(loadingw
, CP_MESSAGE_BORDER
);
2835 box(loadingw
, ACS_VLINE
, ACS_HLINE
);
2836 wattroff(loadingw
, CP_MESSAGE_BORDER
);
2837 mvwprintw(loadingw
, 1, CENTER_INT((COLS
/ 2),
2838 11 + strlen(itoa(gtotal
))), "Loading... %i", gtotal
);
2843 void init_userdata()
2847 for (i
= 0; i
< gtotal
; i
++) {
2848 struct userdata_s
*d
= NULL
;
2850 d
= Calloc(1, sizeof(struct userdata_s
));
2853 pgn_board_init(d
->b
);
2857 void fix_marks(int *start
, int *end
)
2861 *start
= (*start
< 0) ? 0 : *start
;
2862 *end
= (*end
< 0) ? 0 : *end
;
2864 if (*start
> *end
) {
2870 *end
= (*end
> gtotal
) ? gtotal
: *end
;
2873 // Global and other keys.
2874 static int globalkeys(chtype c
)
2876 static char gameexp
[255] = {0};
2880 char tfile
[FILENAME_MAX
];
2881 struct userdata_s
*d
= game
[gindex
].data
;
2885 toggle_engine_window();
2888 cmessage("ABOUT", ANYKEY
, "%s (%s)\nUsing %s with %i colors "
2889 "and %i color pairs\nCopyright 2002-2006 %s",
2890 PACKAGE_STRING
, pgn_version(), curses_version(), COLORS
,
2891 COLOR_PAIRS
, PACKAGE_BUGREPORT
);
2894 if (game
[gindex
].mode
!= MODE_HISTORY
) {
2895 if (!pgn_history_total(game
[gindex
].hp
) ||
2896 (d
->engine
&& d
->engine
->status
== ENGINE_THINKING
))
2899 game
[gindex
].mode
= MODE_HISTORY
;
2900 pgn_board_update(&game
[gindex
], d
->b
, pgn_history_total(game
[gindex
].hp
));
2901 update_all(game
[gindex
]);
2905 // FIXME Resuming from previous history could append to a RAV.
2906 if (game
[gindex
].hindex
!= pgn_history_total(game
[gindex
].hp
)) {
2908 if ((c
= message(NULL
, YESNO
, "%s",
2909 GAME_RESUME_HISTORY_TEXT
)) != 'y')
2914 if (TEST_FLAG(game
[gindex
].flags
, GF_GAMEOVER
))
2918 if (!TEST_FLAG(d
->flags
, CF_HUMAN
)) {
2920 if (start_chess_engine(&game
[gindex
]))
2924 add_engine_command(&game
[gindex
], ENGINE_READY
, "setboard %s\n",
2925 pgn_game_to_fen(game
[gindex
], d
->b
));
2929 game
[gindex
].mode
= MODE_PLAY
;
2930 update_all(game
[gindex
]);
2934 game_next_prev(game
[gindex
], (c
== '>') ? 1 : 0, (keycount
) ?
2936 d
= game
[gindex
].data
;
2940 markend
= markstart
+ delete_count
;
2944 markend
= markstart
- delete_count
;
2945 delete_count
= -1; // to fix gindex in the other direction
2949 fix_marks(&markstart
, &markend
);
2952 if (game
[gindex
].mode
!= MODE_EDIT
)
2953 pgn_board_update(&game
[gindex
], d
->b
, pgn_history_total(game
[gindex
].hp
));
2955 update_status_notify(game
[gindex
], NULL
);
2956 update_all(game
[gindex
]);
2964 if (!*gameexp
|| c
== '?') {
2965 if ((tmp
= get_input(GAME_FIND_EXPRESSION_TITLE
, gameexp
,
2966 1, 1, GAME_FIND_EXPRESSION_PROMPT
, NULL
,
2967 NULL
, 0, -1)) == NULL
)
2970 strncpy(gameexp
, tmp
, sizeof(gameexp
));
2973 if ((n
= find_game_exp(gameexp
, (c
== '{') ? 0 : 1, (keycount
)
2979 d
= game
[gindex
].data
;
2981 if (pgn_history_total(game
[gindex
].hp
))
2982 game
[gindex
].mode
= MODE_HISTORY
;
2984 pgn_board_update(&game
[gindex
], d
->b
, pgn_history_total(game
[gindex
].hp
));
2985 update_all(game
[gindex
]);
2991 /* FIXME field validation
2992 if ((tmp = get_input(GAME_JUMP_TITLE, NULL, 1, 1, NULL, NULL,
2993 NULL, 0, FIELD_TYPE_INTEGER, 1, 1, gtotal))
2999 if ((tmp
= get_input(GAME_JUMP_TITLE
, NULL
, 1, 1, NULL
,
3000 NULL
, NULL
, 0, -1)) == NULL
)
3003 if (!isinteger(tmp
))
3011 if (--i
> gtotal
- 1 || i
< 0)
3015 d
= game
[gindex
].data
;
3016 pgn_board_update(&game
[gindex
], d
->b
, pgn_history_total(game
[gindex
].hp
));
3017 update_status_notify(game
[gindex
], NULL
);
3018 update_all(game
[gindex
]);
3026 if (keycount
&& delete_count
== 0) {
3028 delete_count
= keycount
;
3029 update_status_notify(game
[gindex
], "%s (delete)",
3034 if (markstart
>= 0 && markend
>= 0) {
3035 for (i
= markstart
; i
< markend
; i
++) {
3036 if (toggle_delete_flag(i
)) {
3037 update_all(game
[gindex
]);
3042 gindex
= (delete_count
< 0) ? markstart
: i
- 1;
3043 update_all(game
[gindex
]);
3046 if (toggle_delete_flag(gindex
))
3050 markstart
= markend
= -1;
3052 update_status_window(game
[gindex
]);
3056 cmessage(NULL
, ANYKEY
, "%s", E_DELETE_GAME
);
3062 for (i
= n
= 0; i
< gtotal
; i
++) {
3063 if (TEST_FLAG(game
[i
].flags
, GF_DELETE
))
3068 tmp
= GAME_DELETE_GAME_TEXT
;
3071 cmessage(NULL
, ANYKEY
, "%s", E_DELETE_GAME
);
3075 tmp
= GAME_DELETE_ALL_TEXT
;
3078 if (config
.deleteprompt
) {
3079 if ((c
= cmessage(NULL
, YESNO
, "%s", tmp
)) != 'y')
3083 delete_game((!n
) ? gindex
: -1);
3085 if (pgn_history_total(game
[gindex
].hp
))
3086 game
[gindex
].mode
= MODE_HISTORY
;
3088 d
= game
[gindex
].data
;
3089 pgn_board_update(&game
[gindex
], d
->b
, pgn_history_total(game
[gindex
].hp
));
3090 update_all(game
[gindex
]);
3093 edit_save_tags(&game
[gindex
]);
3094 update_all(game
[gindex
]);
3097 edit_tags(game
[gindex
], d
->b
, 0);
3100 if ((tmp
= get_input(GAME_LOAD_TITLE
, NULL
, 1, 1,
3101 BROWSER_PROMPT
, file_browser
, NULL
, '\t',
3105 if ((tmp
= word_expand(tmp
)) == NULL
)
3108 if ((fp
= pgn_open(tmp
)) == NULL
) {
3109 cmessage(ERROR
, ANYKEY
, "%s\n%s", tmp
, strerror(errno
));
3115 if (pgn_parse(fp
)) {
3117 update_all(game
[gindex
]);
3121 del_panel(loadingp
);
3126 strncpy(loadfile
, tmp
, sizeof(loadfile
));
3128 if (pgn_history_total(game
[gindex
].hp
))
3129 game
[gindex
].mode
= MODE_HISTORY
;
3131 d
= game
[gindex
].data
;
3132 pgn_board_update(&game
[gindex
], d
->b
, pgn_history_total(game
[gindex
].hp
));
3133 update_all(game
[gindex
]);
3140 n
= message(NULL
, GAME_SAVE_MULTI_PROMPT
, "%s",
3141 GAME_SAVE_MULTI_TEXT
);
3148 update_status_notify(game
[gindex
], "%s", NOTIFY_SAVE_ABORTED
);
3153 if ((tmp
= get_input(GAME_SAVE_TITLE
, loadfile
, 1, 1,
3154 BROWSER_PROMPT
, file_browser
, NULL
,
3155 '\t', -1)) == NULL
) {
3156 update_status_notify(game
[gindex
], "%s", NOTIFY_SAVE_ABORTED
);
3160 if ((tmp
= word_expand(tmp
)) == NULL
)
3163 if (pgn_is_compressed(tmp
)) {
3164 p
= tmp
+ strlen(tmp
) - 1;
3166 if (*p
!= 'n' || *(p
-1) != 'g' || *(p
-2) != 'p' ||
3168 snprintf(tfile
, sizeof(tfile
), "%s.pgn", tmp
);
3173 if ((p
= strchr(tmp
, '.')) != NULL
) {
3174 if (strcmp(p
, ".pgn") != 0) {
3175 snprintf(tfile
, sizeof(tfile
), "%s.pgn", tmp
);
3180 snprintf(tfile
, sizeof(tfile
), "%s.pgn", tmp
);
3185 if (save_pgn(tmp
, i
)) {
3186 update_status_notify(game
[gindex
], "%s", NOTIFY_SAVE_FAILED
);
3190 update_status_notify(game
[gindex
], "%s", NOTIFY_SAVED
);
3191 update_all(game
[gindex
]);
3196 switch (game
[gindex
].mode
) {
3198 c
= help(GAME_HELP_PLAY_TITLE
, ANYKEY
, playhelp
);
3201 c
= help(GAME_HELP_HISTORY_TITLE
, ANYKEY
, historyhelp
);
3204 c
= help(GAME_HELP_EDIT_TITLE
, ANYKEY
, edithelp
);
3210 while (c
== KEY_F(1)) {
3211 c
= help(GAME_HELP_INDEX_TITLE
, GAME_HELP_INDEX_PROMPT
,
3216 c
= help(GAME_HELP_HISTORY_TITLE
, ANYKEY
, historyhelp
);
3219 c
= help(GAME_HELP_PLAY_TITLE
, ANYKEY
, playhelp
);
3222 c
= help(GAME_HELP_EDIT_TITLE
, ANYKEY
, edithelp
);
3225 c
= help(GAME_HELP_GAME_TITLE
, ANYKEY
, gamehelp
);
3236 if (cmessage(NULL
, YESNO
, "%s", GAME_NEW_PROMPT
) != 'y')
3242 add_custom_tags(&game
[gindex
].tag
);
3243 d
= Calloc(1, sizeof(struct userdata_s
));
3244 pgn_board_init(d
->b
);
3245 game
[gindex
].data
= d
;
3250 add_custom_tags(&game
[gindex
].tag
);
3251 d
= Calloc(1, sizeof(struct userdata_s
));
3252 pgn_board_init(d
->b
);
3253 game
[gindex
].data
= d
;
3256 game
[gindex
].mode
= MODE_PLAY
;
3257 d
->c_row
= (game
[gindex
].side
== WHITE
) ? 2 : 7;
3259 update_status_notify(game
[gindex
], NULL
);
3260 update_all(game
[gindex
]);
3264 keypad(boardw
, TRUE
);
3268 d
->sp
.icon
= d
->sp
.srow
= d
->sp
.scol
= 0;
3269 markend
= markstart
= 0;
3273 update_status_notify(game
[gindex
], NULL
);
3276 if (config
.validmoves
)
3277 pgn_reset_valid_moves(d
->b
);
3284 keycount
= keycount
* 10 + n
;
3288 update_status_notify(game
[gindex
], "Repeat %i", keycount
);
3291 if (game
[gindex
].mode
== MODE_HISTORY
)
3295 d
->c_row
+= keycount
;
3306 if (game
[gindex
].mode
== MODE_HISTORY
)
3310 d
->c_row
-= keycount
;
3312 update_status_notify(game
[gindex
], NULL
);
3322 if (game
[gindex
].mode
== MODE_HISTORY
)
3326 d
->c_col
-= keycount
;
3337 if (game
[gindex
].mode
== MODE_HISTORY
)
3341 d
->c_col
+= keycount
;
3352 if (game
[gindex
].mode
!= MODE_EDIT
&& game
[gindex
].mode
!=
3356 // Don't edit a running game (for now).
3357 if (pgn_history_total(game
[gindex
].hp
))
3360 if (game
[gindex
].mode
!= MODE_EDIT
) {
3361 pgn_board_init_fen(&game
[gindex
], d
->b
, NULL
);
3363 game
[gindex
].mode
= MODE_EDIT
;
3364 update_all(game
[gindex
]);
3369 pgn_tag_add(&game
[gindex
].tag
, "FEN",
3370 pgn_game_to_fen(game
[gindex
], d
->b
));
3371 pgn_tag_add(&game
[gindex
].tag
, "SetUp", "1");
3372 pgn_tag_sort(game
[gindex
].tag
);
3373 game
[gindex
].mode
= MODE_PLAY
;
3374 update_all(game
[gindex
]);
3384 message("DEBUG BOARD", ANYKEY
, "%s", debug_board(d
->b
));
3397 int error_recover
= 0;
3398 struct userdata_s
*d
= game
[gindex
].data
;
3400 d
->c_row
= 2, d
->c_col
= 5;
3401 gindex
= gtotal
- 1;
3403 if (pgn_history_total(game
[gindex
].hp
))
3404 game
[gindex
].mode
= MODE_HISTORY
;
3406 game
[gindex
].mode
= MODE_PLAY
;
3408 if (game
[gindex
].mode
== MODE_HISTORY
) {
3409 pgn_board_update(&game
[gindex
], d
->b
,
3410 pgn_history_total(game
[gindex
].hp
));
3413 update_status_notify(game
[gindex
], "%s", GAME_HELP_PROMPT
);
3415 paused
= 1; //FIXME clock
3417 update_all(game
[gindex
]);
3418 update_tag_window(game
[gindex
].tag
);
3419 wtimeout(boardw
, 70);
3424 char fdbuf
[8192] = {0};
3426 struct timeval tv
= {0, 0};
3431 for (i
= 0; i
< gtotal
; i
++) {
3435 if (d
->engine
->fd
[ENGINE_IN_FD
] > 2) {
3436 if (d
->engine
->fd
[ENGINE_IN_FD
] > n
)
3437 n
= d
->engine
->fd
[ENGINE_IN_FD
];
3439 FD_SET(d
->engine
->fd
[ENGINE_IN_FD
], &rfds
);
3445 if ((n
= select(n
+ 1, &rfds
, NULL
, NULL
, &tv
)) > 0) {
3446 for (i
= 0; i
< gtotal
; i
++) {
3450 if (FD_ISSET(d
->engine
->fd
[ENGINE_IN_FD
], &rfds
)) {
3451 len
= read(d
->engine
->fd
[ENGINE_IN_FD
], fdbuf
,
3455 if (errno
!= EAGAIN
) {
3456 cmessage(ERROR
, ANYKEY
, "Engine read(): %s",
3458 waitpid(d
->engine
->pid
, &n
, 0);
3466 parse_engine_output(&game
[i
], fdbuf
);
3474 cmessage(ERROR
, ANYKEY
, "select(): %s", strerror(errno
));
3479 for (i
= 0; i
< gtotal
; i
++) {
3482 if (d
->engine
&& d
->engine
->queue
)
3483 send_engine_command(&game
[i
]);
3486 if (TEST_FLAG(game
[gindex
].flags
, GF_GAMEOVER
) && game
[gindex
].mode
3488 game
[gindex
].mode
= MODE_HISTORY
;
3490 d
= game
[gindex
].data
;
3492 draw_board(&game
[gindex
], board_details
);
3493 update_all(game
[gindex
]);
3494 wmove(boardw
, ROWTOMATRIX(d
->c_row
), COLTOMATRIX(d
->c_col
));
3504 if ((c
= wgetch(boardw
)) == ERR
)
3508 if (!keycount
&& status
.notify
)
3509 update_status_notify(game
[gindex
], NULL
);
3511 if ((n
= globalkeys(c
)) == 1) {
3518 switch (game
[gindex
].mode
) {
3523 if (playmode_keys(c
))
3527 historymode_keys(c
);
3537 void usage(const char *pn
, int ret
)
3539 fprintf((ret
) ? stderr
: stdout
, "%s",
3540 "Usage: cboard [-hvE] [-VtRS] [-p <file>]\n"
3541 " -p Load PGN file.\n"
3542 " -V Validate a game file.\n"
3543 " -S Validate and output a PGN formatted game.\n"
3544 " -R Like -S but write a reduced PGN formatted game.\n"
3545 " -t Also write custom PGN tags from config file.\n"
3546 " -E Stop processing on file parsing error (overrides config).\n"
3547 " -v Version information.\n"
3548 " -h This help text.\n");
3559 free(config
.engine_cmd
);
3560 free(config
.pattern
);
3563 for (i
= 0; config
.einit
[i
]; i
++)
3564 free(config
.einit
[i
]);
3570 pgn_tag_free(config
.tag
);
3572 if (curses_initialized
) {
3574 del_panel(historyp
);
3587 for (i
= 0; enginebuf
[i
]; i
++)
3598 void catch_signal(int which
)
3603 if (which
== SIGPIPE
&& quit
)
3606 if (which
== SIGPIPE
)
3607 cmessage(NULL
, ANYKEY
, "%s", E_BROKEN_PIPE
);
3617 keypad(boardw
, TRUE
);
3623 if (curses_initialized
) {
3624 update_loading_window(game
[gindex
]);
3628 fprintf(stderr
, "Loading... %i\r", gtotal
);
3636 static void set_defaults()
3639 set_config_defaults();
3642 int main(int argc
, char *argv
[])
3646 char buf
[FILENAME_MAX
];
3647 char datadir
[FILENAME_MAX
];
3648 int ret
= EXIT_SUCCESS
;
3649 int validate_only
= 0, validate_and_write
= 0;
3650 int write_custom_tags
= 0;
3654 if ((config
.pwd
= getpwuid(getuid())) == NULL
)
3655 err(EXIT_FAILURE
, "getpwuid()");
3657 snprintf(datadir
, sizeof(datadir
), "%s/.cboard", config
.pwd
->pw_dir
);
3658 snprintf(buf
, sizeof(buf
), "%s/cc.data", datadir
);
3659 config
.ccfile
= strdup(buf
);
3660 snprintf(buf
, sizeof(buf
), "%s/nag.data", datadir
);
3661 config
.nagfile
= strdup(buf
);
3662 snprintf(buf
, sizeof(buf
), "%s/agony.data", datadir
);
3663 config
.agonyfile
= strdup(buf
);
3664 snprintf(buf
, sizeof(buf
), "%s/config", datadir
);
3665 config
.configfile
= strdup(buf
);
3667 if (stat(datadir
, &st
) == -1) {
3668 if (errno
== ENOENT
) {
3669 if (mkdir(datadir
, 0755) == -1)
3670 err(EXIT_FAILURE
, "%s", datadir
);
3673 err(EXIT_FAILURE
, "%s", datadir
);
3678 if (!S_ISDIR(st
.st_mode
))
3679 errx(EXIT_FAILURE
, "%s: %s", datadir
, E_NOTADIR
);
3683 while ((opt
= getopt(argc
, argv
, "EVtSRhp:v")) != -1) {
3686 write_custom_tags
= 1;
3689 pgn_config_set(PGN_STOP_ON_ERROR
, 1);
3692 pgn_config_set(PGN_REDUCED
, 1);
3694 validate_and_write
= 1;
3699 printf("%s (%s)\n%s\n", PACKAGE_STRING
, curses_version(),
3703 filetype
= PGN_FILE
;
3704 strncpy(loadfile
, optarg
, sizeof(loadfile
));
3708 usage(argv
[0], EXIT_SUCCESS
);
3712 if ((validate_only
|| validate_and_write
) && !*loadfile
)
3713 usage(argv
[0], EXIT_FAILURE
);
3715 if (access(config
.configfile
, R_OK
) == 0)
3716 parse_rcfile(config
.configfile
);
3718 signal(SIGPIPE
, catch_signal
);
3719 signal(SIGCONT
, catch_signal
);
3720 signal(SIGSTOP
, catch_signal
);
3721 signal(SIGINT
, catch_signal
);
3722 signal(SIGUSR1
, catch_signal
);
3728 if ((fp
= pgn_open(loadfile
)) == NULL
)
3729 err(EXIT_FAILURE
, "%s", loadfile
);
3731 ret
= pgn_parse(fp
);
3734 //ret = parse_fen_file(loadfile);
3736 case EPD_FILE
: // Not implemented.
3739 // No file specified. Empty game.
3740 ret
= pgn_parse(NULL
);
3741 add_custom_tags(&game
[gindex
].tag
);
3745 if (validate_only
|| validate_and_write
) {
3746 if (validate_and_write
) {
3747 for (i
= 0; i
< gtotal
; i
++) {
3748 if (write_custom_tags
)
3749 add_custom_tags(&game
[i
].tag
);
3751 pgn_write(stdout
, game
[i
]);
3763 if (initscr() == NULL
)
3764 errx(EXIT_FAILURE
, "%s", E_INITCURSES
);
3766 curses_initialized
= 1;
3768 if (LINES
< 24 || COLS
< 80) {
3770 errx(EXIT_FAILURE
, "Need at least an 80x24 terminal.");
3773 if (has_colors() == TRUE
&& start_color() == OK
)
3776 boardw
= newwin(BOARD_HEIGHT
, BOARD_WIDTH
, 0, COLS
- BOARD_WIDTH
);
3777 boardp
= new_panel(boardw
);
3778 historyw
= newwin(HISTORY_HEIGHT
, HISTORY_WIDTH
, LINES
- HISTORY_HEIGHT
,
3779 COLS
- HISTORY_WIDTH
);
3780 historyp
= new_panel(historyw
);
3781 statusw
= newwin(STATUS_HEIGHT
, STATUS_WIDTH
, LINES
- STATUS_HEIGHT
, 0);
3782 statusp
= new_panel(statusw
);
3783 tagw
= newwin(TAG_HEIGHT
, TAG_WIDTH
, 0, 0);
3784 tagp
= new_panel(tagw
);
3785 keypad(boardw
, TRUE
);
3786 // leaveok(boardw, TRUE);
3787 leaveok(tagw
, TRUE
);
3788 leaveok(statusw
, TRUE
);
3789 leaveok(historyw
, TRUE
);
3793 draw_window_decor();