Fix inserting multibyte characters from a macro.
[cboard.git] / src / rcfile.c
blobaacf67ff6a0a675ddb98b2b20661ddd3591f3162
1 /* vim:tw=78:ts=8:sw=4:set ft=c: */
2 /*
3 Copyright (C) 2002-2013 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
19 #ifdef HAVE_CONFIG_H
20 #include <config.h>
21 #endif
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <ctype.h>
27 #include <sys/types.h>
28 #include <sys/stat.h>
29 #include <errno.h>
30 #include <err.h>
32 #ifdef HAVE_STRINGS_H
33 #include <strings.h>
34 #endif
36 #include "common.h"
37 #include "conf.h"
38 #include "misc.h"
39 #include "colors.h"
40 #include "keys.h"
41 #include "rcfile.h"
42 #include "common.h"
44 static int attributes(const char *filename, int line, char *str)
46 char *tmp;
47 int attrs = 0;
49 while ((tmp = strsep(&str, ",")) != NULL) {
50 if (strcasecmp(tmp, "BOLD") == 0)
51 attrs |= A_BOLD;
52 else if (strcasecmp(tmp, "REVERSE") == 0)
53 attrs |= A_REVERSE;
54 else if (strcasecmp(tmp, "NONE") == 0)
55 attrs |= A_NORMAL;
56 else if (strcasecmp(tmp, "DIM") == 0)
57 attrs |= A_DIM;
58 else if (strcasecmp(tmp, "STANDOUT") == 0)
59 attrs |= A_STANDOUT;
60 else if (strcasecmp(tmp, "UNDERLINE") == 0)
61 attrs |= A_UNDERLINE;
62 else if (strcasecmp(tmp, "BLINK") == 0)
63 attrs |= A_BLINK;
64 else if (strcasecmp(tmp, "INVISIBLE") == 0)
65 attrs |= A_INVIS;
66 else
67 errx(EXIT_FAILURE, _("%s(%i): invalid attribute \"%s\""), filename,
68 line, tmp);
71 return attrs;
74 static short color_name(const char *filename, int line, const char *color)
76 if (strcasecmp(color, "BLACK") == 0)
77 return COLOR_BLACK;
78 else if (strcasecmp(color, "WHITE") == 0)
79 return COLOR_WHITE;
80 else if (strcasecmp(color, "GREEN") == 0)
81 return COLOR_GREEN;
82 else if (strcasecmp(color, "YELLOW") == 0)
83 return COLOR_YELLOW;
84 else if (strcasecmp(color, "MAGENTA") == 0)
85 return COLOR_MAGENTA;
86 else if (strcasecmp(color, "BLUE") == 0)
87 return COLOR_BLUE;
88 else if (strcasecmp(color, "RED") == 0)
89 return COLOR_RED;
90 else if (strcasecmp(color, "CYAN") == 0)
91 return COLOR_CYAN;
92 else if (strcasecmp(color, "-") == 0)
94 else
95 errx(EXIT_FAILURE, _("%s(%i): invalid color \"%s\""), filename, line,
96 color);
98 return -1;
101 static void parse_color(const char *filename, int line, const char *str,
102 struct color_s *c)
104 char fg[16], bg[16], attr[64], nattr[64];
105 int n;
107 if ((n = sscanf(str, "%[a-zA-Z-] %[a-zA-Z-] %[a-zA-Z,-] %[a-zA-Z,-]", fg, bg,
108 attr, nattr)) < 2)
109 errx(EXIT_FAILURE, _ ("%s(%i): parse error"), filename, line);
111 if ((n = color_name(filename, line, fg)) >= 0)
112 c->fg = n;
114 if ((n = color_name(filename, line, bg)) >= 0)
115 c->bg = n;
117 c->attrs = c->nattrs = 0;
119 if (n > 2)
120 c->attrs = attributes(filename, line, attr);
122 if (n > 3)
123 c->nattrs = attributes(filename, line, nattr);
126 static int on_or_off(const char *filename, int lines, const char *str)
128 if (strcasecmp(str, "on") == 0 || strcasecmp(str, "1") == 0 ||
129 strcasecmp(str, "yes") == 0 || strcasecmp(str, "true") == 0)
130 return 1;
132 if (strcasecmp(str, "off") == 0 || strcasecmp(str, "0") == 0 ||
133 strcasecmp(str, "no") == 0 || strcasecmp(str, "false") == 0)
134 return 0;
136 errx(EXIT_FAILURE, _("%s(%i): invalid value \"%s\""), filename, lines, str);
139 void copydatafile(const char *dst, const char *src)
141 FILE *fp, *ofp;
142 char buf[LINE_MAX], *s;
144 snprintf(buf, sizeof(buf), "%s/%s", DATA_PATH, src);
146 if ((fp = fopen(buf, "r")) == NULL) {
147 if (errno != ENOENT)
148 warn("%s", buf);
149 return;
152 if ((ofp = fopen(dst, "w+")) == NULL) {
153 fclose(fp);
154 warn("%s", dst);
155 return;
158 while ((s = fgets(buf, sizeof(buf), fp)) != NULL)
159 fprintf(ofp, "%s", s);
161 fclose(fp);
162 fclose(ofp);
165 static char *fancy_key_name(wint_t c)
167 static char buf[64] = {0};
168 char *p;
170 strncpy(buf, keyname(c) ? keyname (c) : key_name (c), sizeof(buf)-1);
171 p = buf;
173 if (*p == '^' && *(p + 1) == '[')
174 return _("Escape");
176 if (*p == '^' && *(p + 1) == 'J')
177 return _ ("Enter");
179 if (strncasecmp(p, "KEY_", 4) == 0) {
180 for (p = buf; *p; p++)
181 *p = tolower(*p);
183 p = buf;
184 p += 4;
186 if (*p == 'f') {
187 char *t = buf + 4;
189 p += 2;
191 while (isdigit(*p))
192 *++t = *p++;
194 *++t = 0;
195 p = buf + 4;
197 else if (strcmp(p, "ppage") == 0)
198 return _("PgUp");
199 else if (strcmp(p, "npage") == 0)
200 return _ ("PgDown");
202 *p = toupper(*p);
203 return p;
206 if (*p == ' ')
207 return _("Space");
209 return p;
212 void add_key_binding(struct key_s ***dst, key_func *func, wint_t c,
213 char *desc, int repeat)
215 int i = 0;
216 struct key_s **k = *dst;
218 if (k)
219 for (i = 0; k[i]; i++);
221 k = Realloc(k, (i + 2) * sizeof(struct key_s *));
222 k[i] = Calloc(1, sizeof(struct key_s));
223 k[i]->f = func;
224 k[i]->c = c;
225 k[i]->r = repeat;
226 k[i]->key = str_to_wchar (fancy_key_name(c));
228 if (desc)
229 k[i]->d = str_to_wchar(desc);
231 i++;
232 k[i] = NULL;
233 *dst = k;
236 void set_default_keys()
238 add_key_binding(&history_keys, do_history_jump_next, KEY_UP, _("history jump next"), 1);
239 add_key_binding(&history_keys, do_history_jump_prev, KEY_DOWN, _("history jump previous"), 1);
240 add_key_binding(&history_keys, do_history_next, KEY_RIGHT, _("next move"), 1);
241 add_key_binding(&history_keys, do_history_prev, KEY_LEFT, _("previous move"), 1);
242 add_key_binding(&history_keys, do_history_half_move_toggle, ' ', _("toggle half move (ply) stepping"), 0);
243 add_key_binding(&history_keys, do_history_rotate_board, 'r', _("rotate board"), 0);
244 add_key_binding(&history_keys, do_history_jump, 'j', _("jump to move number"), 1);
245 add_key_binding(&history_keys, do_history_find_new, '/', _("new move text expression"), 0);
246 add_key_binding(&history_keys, do_history_find_next, ']', _("find next move text expression"), 1);
247 add_key_binding(&history_keys, do_history_find_prev, '[', _("find previous move text expression"), 1);
248 add_key_binding(&history_keys, do_history_annotate, CTRL_KEY('a'), _("annotate the previous move"), 0);
249 add_key_binding(&history_keys, do_history_rav_next, '+', _("next variation of the previous move"), 0);
250 add_key_binding(&history_keys, do_history_rav_prev, '-', _("previous variation of the previous move"), 0);
251 add_key_binding(&history_keys, do_history_menu, 'M', _("move history tree"), 0);
252 add_key_binding(&history_keys, do_history_help, KEY_F(1), _("more help"), 0);
253 add_key_binding(&history_keys, do_history_help, CTRL_KEY('g'), NULL, 0);
254 add_key_binding(&history_keys, do_history_toggle, 'h', _("exit history mode"), 0);
256 add_key_binding(&edit_keys, do_edit_select, ' ', _("select piece for movement"), 0);
257 add_key_binding(&edit_keys, do_edit_commit, '\n', _("commit selected piece"), 0);
258 add_key_binding(&edit_keys, do_edit_cancel_selected, KEY_ESCAPE, _("cancel selected piece"), 0);
259 add_key_binding(&edit_keys, do_edit_delete, 'd', _("remove the piece under the cursor"), 0);
260 add_key_binding(&edit_keys, do_edit_insert, 'i', _("insert piece"), 0);
261 add_key_binding(&edit_keys, do_edit_toggle_castle, 'c', _("toggle castling availability"), 0);
262 add_key_binding(&edit_keys, do_edit_enpassant, 'p', _("toggle enpassant square"), 0);
263 add_key_binding(&edit_keys, do_edit_switch_turn, 'w', _("toggle turn"), 0);
264 add_key_binding(&edit_keys, do_edit_help, KEY_F(1), _("more help"), 0);
265 add_key_binding(&history_keys, do_edit_help, CTRL_KEY('g'), NULL, 0);
266 add_key_binding(&edit_keys, do_edit_exit, 'e', _("exit edit mode"), 0);
268 add_key_binding(&play_keys, do_play_select, ' ', _("select piece for movement"), 0);
269 add_key_binding(&play_keys, do_play_commit, '\n', _("commit selected piece"), 0);
270 add_key_binding(&play_keys, do_play_cancel_selected, KEY_ESCAPE, _("cancel selected piece"), 0);
271 add_key_binding(&play_keys, do_play_set_clock, 'C', _("set clock"), 0);
272 // add_key_binding(&play_keys, do_play_switch_turn, 'w', _("switch turn"), 0);
273 add_key_binding(&play_keys, do_play_undo, 'u', _("undo previous move"), 1);
274 add_key_binding(&play_keys, do_play_go, 'g', _("force the chess engine to make the next move"), 0);
275 add_key_binding(&play_keys, do_play_send_command, '|', _("send a command to the chess engine"), 0);
276 add_key_binding(&play_keys, do_play_toggle_eh_mode, 'w', _("toggle engine/human play"), 0);
277 add_key_binding(&play_keys, do_play_toggle_engine, 'E', _("toggle engine/engine play"), 0);
278 add_key_binding(&play_keys, do_play_toggle_human, 'H', _("toggle human/human play"), 0);
279 add_key_binding(&play_keys, do_play_toggle_pause, 'p', _("toggle pausing of this game"), 0);
280 add_key_binding(&play_keys, do_play_history_mode, 'h', _("enter history mode"), 0);
281 add_key_binding(&play_keys, do_play_edit_mode, 'e', _("enter edit mode"), 0);
282 add_key_binding(&play_keys, do_play_help, KEY_F(1), _("more help"), 0);
283 add_key_binding(&play_keys, do_play_help, CTRL_KEY('g'), NULL, 0);
285 add_key_binding(&global_keys, do_global_tag_edit, CTRL_KEY('t'), _("edit roster tags"), 0);
286 add_key_binding(&global_keys, do_global_tag_view, 't', _("view roster tags"), 0);
287 add_key_binding(&global_keys, do_global_find_new, '?', _("new find game expression"), 0);
288 add_key_binding(&global_keys, do_global_find_next, '}', _("find next game"), 1);
289 add_key_binding(&global_keys, do_global_find_prev, '{', _("find previous game"), 1);
290 add_key_binding(&global_keys, do_global_new_game, CTRL_KEY('n'), _("new game or round"), 0);
291 add_key_binding(&global_keys, do_global_new_all, CTRL_KEY('k'), _("new game from scratch"), 0);
292 add_key_binding(&global_keys, do_global_copy_game, CTRL_KEY('i'), _("copy current game"), 0);
293 add_key_binding(&global_keys, do_global_next_game, '>', _("next game"), 1);
294 add_key_binding(&global_keys, do_global_prev_game, '<', _("previous game"), 1);
295 add_key_binding(&global_keys, do_global_game_jump, 'J', _("jump to game"), 1);
296 add_key_binding(&global_keys, do_global_toggle_delete, 'X', _("toggle delete flag"), 1);
297 add_key_binding(&global_keys, do_global_delete_game, CTRL_KEY('X'), _("delete the current or flagged games"), 0);
298 add_key_binding(&global_keys, do_global_resume_game, CTRL_KEY('r'), _("load a PGN file"), 0);
299 add_key_binding(&global_keys, do_global_save_game, 's', _("save game"), 0);
300 add_key_binding(&global_keys, do_global_toggle_board_details, CTRL_KEY('d'), _("toggle board details"), 0);
301 add_key_binding(&global_keys, do_global_toggle_strict_castling, CTRL_KEY('p'), _("toggle strict castling"), 0);
302 add_key_binding(&global_keys, do_global_toggle_engine_window, 'W', _("toggle chess engine IO window"), 0);
303 #ifdef WITH_LIBPERL
304 add_key_binding(&global_keys, do_global_perl, CTRL_KEY('O'), _("Call PERL subroutine"), 0);
305 #endif
306 add_key_binding(&global_keys, do_global_about, KEY_F(10), _("version information"), 0);
307 add_key_binding(&global_keys, do_global_quit, 'Q', _("quit"), 0);
310 void set_config_defaults()
312 struct stat st;
314 config.pattern = strdup("*.[Pp][Gg][Nn]*");
315 config.engine_cmd = strdup("gnuchess --xboard");
316 config.engine_protocol = 1;
317 config.jumpcount = 5;
318 config.linegraphics = 1;
319 config.saveprompt = 1;
320 config.deleteprompt = 1;
321 config.validmoves = 1;
322 config.details = 1;
323 config.showattacks = 1;
324 config.coordsyleft = TRUE;
325 config.fmpolyglot = FALSE;
326 config.boardleft = TRUE;
327 config.exitdialogbox = TRUE;
328 config.enginecmdblacktag = TRUE;
330 set_default_colors();
332 if (stat(config.nagfile, &st) == -1) {
333 if (errno == ENOENT)
334 copydatafile(config.nagfile, "nag.data");
335 else
336 warn("%s", config.nagfile);
339 if (stat(config.ccfile, &st) == -1) {
340 if (errno == ENOENT)
341 copydatafile(config.ccfile, "cc.data");
342 else
343 warn("%s", config.ccfile);
347 static void update_key(struct key_s **dst, struct custom_key_s config_key,
348 wint_t c, char *desc)
350 int i;
352 for (i = 0; dst[i]; i++) {
353 if (dst[i]->f == config_key.func) {
354 dst[i]->c = c;
356 if (dst[i]->key)
357 free(dst[i]->key);
359 dst[i]->key = str_to_wchar(fancy_key_name(c));
360 if (desc) {
361 free(dst[i]->d);
362 dst[i]->d = str_to_wchar(desc);
365 goto done;
369 add_key_binding(&dst, config_key.func, c,
370 (desc) ? desc : _("no description"), config_key.r);
372 done:
373 return;
376 static struct known_key_s
378 wint_t c;
379 char *name;
380 } known_keys[] = {
381 { KEY_UP, "up" },
382 { KEY_DOWN, "down" },
383 { KEY_LEFT, "left" },
384 { KEY_RIGHT, "right" },
385 { KEY_HOME, "home" },
386 { KEY_END, "end" },
387 { KEY_DC, "delete" },
388 { KEY_PPAGE, "pgup" },
389 { KEY_NPAGE, "pgdn" },
390 { KEY_IC, "insert" },
391 { ' ', "space" },
392 { KEY_ESCAPE, "escape" },
393 { '\n', "enter" },
394 { 0, NULL }
397 static wint_t parse_key(const char *filename, int lines, wchar_t **key)
399 wchar_t *p = *key;
400 wchar_t *orig = *key;
401 wint_t c = 0;
403 if (!key || !wcslen (*key))
404 return 0;
406 if (*p == '\"') {
407 if (orig[wcslen(orig) - 1] != '\"')
408 errx(EXIT_FAILURE, _("%s(%i): unbalanced quotes"), filename, lines);
410 p++;
411 orig[wcslen(orig) - 1] = 0;
414 if (*p == '<') {
415 int i;
417 p++;
419 for (i = 0; known_keys[i].name; i++) {
420 wchar_t *wc = str_to_wchar (known_keys[i].name);
422 if (!wcsncasecmp (p, wc, wcslen (wc))) {
423 c = known_keys[i].c;
424 p += strlen (known_keys[i].name);
425 free (wc);
426 break;
429 free (wc);
432 if (!c) {
433 if (*p == '^') {
434 p++;
435 c = CTRL_KEY(*p++);
437 else if (*p == 'F' || *p == 'f') {
438 char *str = wchar_to_str (++p);
440 c = KEY_F(atoi(str));
441 p += integer_len(atoi(str));
442 free (str);
446 if (!c)
447 c = *p++;
449 if (*p++ != '>')
450 errx(EXIT_FAILURE, _("%s(%i): parse error \"%ls\""), filename,
451 lines, orig);
453 else if (*p == '\\') {
454 p++;
455 c = *p++;
457 else
458 c = *p++;
460 orig += wcslen(orig) - wcslen(p);
461 *key = orig;
462 return c;
465 static void parse_key_binding(const char *filename, int lines, char *val)
467 char mode[64], func[64];
468 wchar_t desc[64] = {0};
469 wchar_t key[64];
470 int n;
471 int m = 0;
472 wint_t c = 0;
473 int f;
474 int i;
475 wchar_t *p;
476 char *str;
478 n = sscanf(val, "%s %ls %s %63lc", mode, key, func, desc);
480 if (n < 3)
481 errx(EXIT_FAILURE, _ ("%s(%i): too few arguments"), filename, lines);
483 if (strcasecmp(mode, "history") == 0)
484 m = MODE_HISTORY;
485 else if (strcasecmp(mode, "play") == 0)
486 m = MODE_PLAY;
487 else if (strcasecmp(mode, "edit") == 0)
488 m = MODE_EDIT;
489 else
490 errx(EXIT_FAILURE, _ ("%s(%i): invalid game mode \"%s\""), filename,
491 lines, mode);
493 p = key;
494 c = parse_key(filename, lines, &p);
496 if (m != -1) {
497 for (i = 0; global_keys[i]; i++) {
498 if (global_keys[i]->c == c)
499 errx(EXIT_FAILURE,
500 _("%s(%i): key \"%ls\" conflicts with a global key"),
501 filename, lines, key);
505 for (f = -2, i = 0; config_keys[i].name; i++) {
506 if (strcmp(config_keys[i].name, func) == 0 &&
507 config_keys[i].mode == m) {
508 f = i;
509 break;
513 if (f == -2)
514 errx(EXIT_FAILURE, _("%s(%i): invalid command \"%s\""), filename,
515 lines, func);
517 str = wchar_to_str (desc);
519 switch (m) {
520 case MODE_PLAY:
521 update_key(play_keys, config_keys[f], c, (n > 3) ? str : NULL);
522 break;
523 case MODE_HISTORY:
524 update_key(history_keys, config_keys[f], c, (n > 3) ? str : NULL);
525 break;
526 case MODE_EDIT:
527 update_key(edit_keys, config_keys[f], c, (n > 3) ? str : NULL);
528 break;
529 default:
530 update_key(global_keys, config_keys[f], c, (n > 3) ? str : NULL);
531 break;
534 free (str);
537 static void parse_macro(const char *filename, int lines, char *val)
539 char mode[16];
540 wchar_t keys[2048] = {0}, key[8];
541 int n;
542 int m;
543 wint_t c;
544 int i = 0;
545 wchar_t *p;
547 n = sscanf(val, "%s %ls %2047lc", mode, key, keys);
549 if (n != 3)
550 errx(EXIT_FAILURE, _("%s(%i): too few arguments"), filename, lines);
552 if (strcasecmp(mode, "history") == 0)
553 m = MODE_HISTORY;
554 else if (strcasecmp(mode, "play") == 0)
555 m = MODE_PLAY;
556 else if (strcasecmp(mode, "edit") == 0)
557 m = MODE_EDIT;
558 else if (strcasecmp(mode, "any") == 0)
559 m = -1;
560 else
561 errx(EXIT_FAILURE, _ ("%s(%i): invalid game mode \"%s\""), filename,
562 lines, mode);
564 p = key;
565 c = parse_key(filename, lines, &p);
567 if (macros)
568 for (i = 0; macros[i]; i++);
570 macros = Realloc(macros, (i + 2) * sizeof(struct macro_s));
571 macros[i] = Calloc(1, sizeof(struct macro_s));
572 macros[i]->c = c;
573 macros[i]->mode = m;
574 p = keys;
576 while ((c = parse_key(filename, lines, &p)) != 0) {
577 macros[i]->keys = Realloc(macros[i]->keys, (macros[i]->total + 2) *
578 sizeof(wint_t));
579 macros[i]->keys[macros[i]->total++] = c;
582 macros[++i] = NULL;
585 void parse_rcfile(const char *filename)
587 FILE *fp;
588 char *line, buf[LINE_MAX];
589 int lines = 0;
590 char *altengine = NULL;
591 int init = 0;
592 int k = 0;
593 int c;
595 if ((fp = fopen(filename, "r")) == NULL)
596 err(EXIT_FAILURE, "%s", filename);
598 while ((line = fgets(buf, sizeof(buf), fp)) != NULL) {
599 int n;
600 char var[30] = {0}, val[LINE_MAX - sizeof(var) - 1] = {0};
601 char token[MAX_PGN_LINE_LEN + 1] = {0};
602 char value[MAX_PGN_LINE_LEN + 1] = {0};
603 char *p;
605 lines++;
606 line = trim(line);
608 if (!line[0] || line[0] == '#')
609 continue;
611 if ((n = sscanf(line, "%s %[^\n]", var, val)) != 2)
612 errx(EXIT_FAILURE, _("%s(%i): parse error %i"), filename, lines,n);
614 p = strdup(trim(val));
615 strncpy(val, p, sizeof(val)-1);
616 free(p);
617 p = strdup(trim(var));
618 strncpy(var, p, sizeof(var)-1);
619 free(p);
621 if (strcmp(var, "jump_count") == 0) {
622 if (!isinteger(val))
623 errx(EXIT_FAILURE, _("%s(%i): value is not an integer"), filename,
624 lines);
626 config.jumpcount = atoi(val);
628 else if (strcmp(var, "engine_init") == 0) {
629 config.einit = Realloc(config.einit, (init + 2) * sizeof(char *));
630 config.einit[init++] = strdup(val);
631 config.einit[init] = NULL;
633 else if (strcmp(var, "pattern") == 0) {
634 free(config.pattern);
635 config.pattern = strdup(val);
637 else if (strcmp(var, "mpl") == 0) {
638 if (!isinteger(val))
639 errx(EXIT_FAILURE, _("%s(%i): value is not an integer"),
640 filename, lines);
641 pgn_config_set(PGN_MPL, atoi(val));
643 else if (strcmp(var, "stop_on_error") == 0)
644 pgn_config_set(PGN_STOP_ON_ERROR, on_or_off(filename, lines, val));
645 else if (strcmp(var, "tag") == 0) {
646 if ((n = sscanf(val, "%s %s ", token, value)) < 1 || n > 2)
647 errx(EXIT_FAILURE, _ ("%s(%i): invalid value \"%s\""), filename,
648 lines, val);
650 if (n == 1)
651 value[0] = 0;
652 else {
653 p = val + strlen(token);
654 strncpy(value, p, sizeof(value)-1);
657 for (n = 0; n < strlen(token); n++) {
658 if (!isalnum(token[n]) && token[n] != '_')
659 errx(EXIT_FAILURE,
660 _ ("%s(%i): token names must match 0-9A-Za-z_."),
661 filename, lines);
664 token[0] = toupper(token[0]);
665 pgn_tag_add(&config.tag, token, value);
667 else if (strcmp(var, "save_directory") == 0)
668 config.savedirectory = strdup(val);
669 else if (strcmp(var, "line_graphics") == 0)
670 config.linegraphics = on_or_off(filename, lines, val);
671 else if (strcmp(var, "save_prompt") == 0)
672 config.saveprompt = on_or_off(filename, lines, val);
673 else if (strcmp(var, "delete_prompt") == 0)
674 config.deleteprompt = on_or_off(filename, lines, val);
675 else if (strcmp(var, "valid_moves") == 0)
676 config.validmoves = on_or_off(filename, lines, val);
677 else if (strcmp(var, "board_details") == 0)
678 config.details = on_or_off(filename, lines, val);
679 else if (strcmp(var, "show_attacks") == 0)
680 config.showattacks = on_or_off(filename, lines, val);
681 else if (strcmp(var, "coords_y_left") == 0 )
682 config.coordsyleft = on_or_off(filename, lines, val);
683 else if (strcmp(var, "fm_polyglot") == 0 )
684 config.fmpolyglot = on_or_off(filename, lines, val);
685 else if (strcmp(var, "board_left") == 0 )
686 config.boardleft = on_or_off(filename, lines, val);
687 else if (strcmp(var, "exit_dialog_box") == 0 )
688 config.exitdialogbox = on_or_off(filename, lines, val);
689 else if (strcmp(var, "engine_cmd_blacktag") == 0 )
690 config.enginecmdblacktag = on_or_off(filename, lines, val);
691 else if (strcmp(var, "strict_castling") == 0)
692 pgn_config_set(PGN_STRICT_CASTLING, on_or_off(filename, lines, val));
693 else if (strcmp(var, "engine_cmd") == 0)
694 altengine = strdup(val);
695 else if (strcmp(var, "engine_protocol") == 0) {
696 if (!isinteger(val))
697 errx(EXIT_FAILURE, _ ("%s(%i): value is not an integer"),
698 filename, lines);
700 config.engine_protocol = atoi(val);
702 if (config.engine_protocol != 1 && config.engine_protocol != 2)
703 errx(EXIT_FAILURE, _ ("%s(%i): invalid value"), filename, lines);
705 else if (strcmp(var, "utf8_pieces") == 0)
706 config.utf8_pieces = on_or_off (filename, lines, val);
707 else if (strcmp(var, "color_board_window") == 0)
708 parse_color(filename, lines, val, &config.color[CONF_BDWINDOW]);
709 else if (strcmp(var, "color_board_selected") == 0)
710 parse_color(filename, lines, val, &config.color[CONF_BSELECTED]);
711 else if (strcmp(var, "color_board_white_moves") == 0)
712 parse_color(filename, lines, val, &config.color[CONF_BMOVESW]);
713 else if (strcmp(var, "color_board_black_moves") == 0)
714 parse_color(filename, lines, val, &config.color[CONF_BMOVESB]);
715 else if (strcmp(var, "color_board_count") == 0)
716 parse_color(filename, lines, val, &config.color[CONF_BCOUNT]);
717 else if (strcmp(var, "color_board_cursor") == 0)
718 parse_color(filename, lines, val, &config.color[CONF_BCURSOR]);
719 else if (strcmp(var, "color_board_black") == 0)
720 parse_color(filename, lines, val, &config.color[CONF_BBLACK]);
721 else if (strcmp(var, "color_board_white") == 0)
722 parse_color(filename, lines, val, &config.color[CONF_BWHITE]);
723 else if (strcmp(var, "color_board_graphics") == 0)
724 parse_color(filename, lines, val, &config.color[CONF_BGRAPHICS]);
725 else if (strcmp(var, "color_board_coords") == 0)
726 parse_color(filename, lines, val, &config.color[CONF_BCOORDS]);
727 else if (strcmp(var, "color_board_castling") == 0)
728 parse_color(filename, lines, val, &config.color[CONF_BCASTLING]);
729 else if (strcmp(var, "color_board_enpassant") == 0)
730 parse_color(filename, lines, val, &config.color[CONF_BENPASSANT]);
731 else if (strcmp(var, "color_board_attack") == 0)
732 parse_color(filename, lines, val, &config.color[CONF_BATTACK]);
733 else if (strcmp(var, "color_status_window") == 0)
734 parse_color(filename, lines, val, &config.color[CONF_SWINDOW]);
735 else if (strcmp(var, "color_status_title") == 0)
736 parse_color(filename, lines, val, &config.color[CONF_STITLE]);
737 else if (strcmp(var, "color_status_border") == 0)
738 parse_color(filename, lines, val, &config.color[CONF_SBORDER]);
739 else if (strcmp(var, "color_status_notify") == 0)
740 parse_color(filename, lines, val, &config.color[CONF_SNOTIFY]);
741 else if (strcmp(var, "color_status_engine") == 0)
742 parse_color(filename, lines, val, &config.color[CONF_SENGINE]);
743 else if (strcmp(var, "color_tag_window") == 0)
744 parse_color(filename, lines, val, &config.color[CONF_TWINDOW]);
745 else if (strcmp(var, "color_tag_title") == 0)
746 parse_color(filename, lines, val, &config.color[CONF_TTITLE]);
747 else if (strcmp(var, "color_tag_border") == 0)
748 parse_color(filename, lines, val, &config.color[CONF_TBORDER]);
749 else if (strcmp(var, "color_history_window") == 0)
750 parse_color(filename, lines, val, &config.color[CONF_HWINDOW]);
751 else if (strcmp(var, "color_history_title") == 0)
752 parse_color(filename, lines, val, &config.color[CONF_HTITLE]);
753 else if (strcmp(var, "color_history_border") == 0)
754 parse_color(filename, lines, val, &config.color[CONF_HBORDER]);
755 else if (strcmp(var, "color_message_window") == 0)
756 parse_color(filename, lines, val, &config.color[CONF_MWINDOW]);
757 else if (strcmp(var, "color_message_title") == 0)
758 parse_color(filename, lines, val, &config.color[CONF_MTITLE]);
759 else if (strcmp(var, "color_message_border") == 0)
760 parse_color(filename, lines, val, &config.color[CONF_MBORDER]);
761 else if (strcmp(var, "color_message_prompt") == 0)
762 parse_color(filename, lines, val, &config.color[CONF_MPROMPT]);
763 else if (strcmp(var, "color_input_window") == 0)
764 parse_color(filename, lines, val, &config.color[CONF_IWINDOW]);
765 else if (strcmp(var, "color_input_title") == 0)
766 parse_color(filename, lines, val, &config.color[CONF_ITITLE]);
767 else if (strcmp(var, "color_input_border") == 0)
768 parse_color(filename, lines, val, &config.color[CONF_IBORDER]);
769 else if (strcmp(var, "color_input_prompt") == 0)
770 parse_color(filename, lines, val, &config.color[CONF_IPROMPT]);
771 else if (strcmp(var, "color_menu") == 0)
772 parse_color(filename, lines, val, &config.color[CONF_MENU]);
773 else if (strcmp(var, "color_menu_selected") == 0)
774 parse_color(filename, lines, val, &config.color[CONF_MENUS]);
775 else if (strcmp(var, "color_menu_highlight") == 0)
776 parse_color(filename, lines, val, &config.color[CONF_MENUH]);
777 else if (strcmp(var, "color_menu_graphics") == 0)
778 parse_color(filename, lines, val,
779 &config.color[CONF_HISTORY_MENU_LG]);
780 else if (strcmp(var, "bind") == 0)
781 parse_key_binding(filename, lines, val);
782 else if (strcmp(var, "macro") == 0)
783 parse_macro(filename, lines, val);
784 else if (strcmp(var, "cbind") == 0) {
785 config.keys = Realloc(config.keys, (k + 2) *
786 sizeof(struct config_key_s *));
787 config.keys[k] = Calloc(1, sizeof(struct config_key_s));
788 p = val;
789 n = 0;
791 while (*p && !isspace(*p))
792 p++, n++;
794 c = *p;
795 *p = 0;
796 p -= n;
798 if (strcasecmp(p, "none") == 0)
799 config.keys[k]->type = KEY_DEFAULT;
800 else if (strcasecmp(p, "repeat") == 0)
801 config.keys[k]->type = KEY_REPEAT;
802 else if (strcasecmp(p, "set") == 0)
803 config.keys[k]->type = KEY_SET;
804 else
805 errx(EXIT_FAILURE, _ ("%s(%i): invalid value \"%s\""), filename,
806 lines, p);
808 p = val + n;
809 *p = c;
811 while (*p && isspace(*p))
812 p++;
814 config.keys[k]->c = *p++;
816 while (isspace(*p))
817 p++;
819 config.keys[k++]->str = str_to_wchar (p);
820 config.keys[k] = NULL;
822 else
823 errx(EXIT_FAILURE, _ ("%s(%i): invalid parameter \"%s\""), filename,
824 lines, var);
827 fclose(fp);
829 if (altengine) {
830 free(config.engine_cmd);
831 config.engine_cmd = NULL;
832 config.engine_cmd = altengine;
835 if (config.enginecmdblacktag)
836 pgn_tag_add(&config.tag, "Black", config.engine_cmd);