File: spconf.c

package info (click to toggle)
spectemu 0.94a-19
  • links: PTS, VCS
  • area: contrib
  • in suites: stretch
  • size: 1,300 kB
  • ctags: 3,206
  • sloc: ansic: 15,421; asm: 5,160; sh: 1,533; cpp: 377; makefile: 173
file content (737 lines) | stat: -rw-r--r-- 16,799 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
/* 
 * Copyright (C) 1996-1998 Szeredi Miklos
 * Email: mszeredi@inf.bme.hu
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version. See the file COPYING. 
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 */

#include "spconf_p.h"
#include "spver.h"
#include "misc.h"
#include "interf.h"
#include "spscr_p.h"
#include "spkey.h"

#include "snapshot.h"   /* for SN_Z80  and SN_SNA  */
#include "tapefile.h"   /* for TAP_TAP and TAP_TZX */

#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>

extern const char *spcf_keynames_ascii[];
extern const char *spcf_keynames_misc[];

char *spcf_init_snapshot = NULL;
int   spcf_init_snapshot_type;
char *spcf_init_tapefile = NULL;
int   spcf_init_tapefile_type;

#define MAXLINELEN 512
static int linectr;
static FILE *conffp;
static const char *conffile;

static void strip_spaces(char **sp)
{
  char *s;
  
  s = *sp;
  while(isspace(*s)) s++;
  *sp = s;

  return;
}

int spcf_parse_conf_line(char *line, char **attrp, char **valp)
{
  char *s;

  for(s = line; *s; s++) 
    if(*s == '#') {
      *s = '\0';
      break;
    }
  for(s--; s >= line && isspace(*s); s--) *s = '\0';

  s = line;

  if(!*s) return 0;

  strip_spaces(&s);

  if(!isalpha(*s)) return -1;
  *attrp = s;

  for(; *s; s++) 
    if(!isalnum(*s) && *s != '_' && *s != '-') break;
  if(!*s) return -1;

  if(isspace(*s)) {
    *s='\0';
    s++;
    strip_spaces(&s);
  }
  if(*s != '=') return -1;
  *s='\0';  
  s++;
  strip_spaces(&s);
  
  if(!*s) return -1;
  *valp = s;
  
  return 1;
}

static int read_conf_line(char **attrp, char **valp)
{
  char *res;
  int ret;

  static char line[MAXLINELEN];

  do {
    linectr++;

    res = fgets(line, MAXLINELEN, conffp);
    if(res == NULL) return 0;

    if(line[0] && line[strlen(line)-1] != '\n') {
      int c;
      
      c = getc(conffp);
      if(c == EOF) 
	fprintf(stderr, "%s: File does not end with a newline\n", conffile);
      else 
	fprintf(stderr, "%s:%i: Line too long\n", conffile ,linectr);
      
      while(c != EOF && c != '\n') c = getc(conffp);
      ret = -1;
    }
    else {
      ret = spcf_parse_conf_line(line, attrp, valp);
      if(ret < 0) fprintf(stderr, "%s:%i: Parse error\n", conffile, linectr);
    }
  } while(ret <= 0);

  return 1;
}

static const char *vgamodes[] =  
{ "320x240", "320x200", NULL };

static const char *keybtypes[] = 
{ "extended", "spectrum", "compat", "custom", NULL };

static const char *curstypes[] = 
{ "shifted", "raw", "joystick", NULL };

static const char *colrtypes[] = 
{ "normal", "grayscale", "custom", NULL };

static const char *modifkeys[] = 
{ "none", "shift", "lock", "control", "alt", "mod2", "mod3", "mod4", "mod5", 
    NULL };


struct sp_options spcf_options[] = {
  {"frameSkip",       SA_INT,  &showframe,          NULL,      1 },
  {"rr",              SA_INT,  &showframe,          NULL,      0 },
  {"scale",           SA_INT,  &scrmul,             NULL,      1 },
  {"privateMap",      SA_BOOL, &privatemap,         NULL,      1 },
  {"mitShm",          SA_BOOL, &use_shm,            NULL,      1 },
  {"vgaMode",         SA_ENUM, &small_screen,       vgamodes,  1 },
  {"sound",           SA_BOOL, &sound_on,           NULL,      1 },
  {"soundDelay",      SA_INT,  &bufframes,          NULL,      1 },
  {"soundDevice",     SA_STR,  &sound_dev_name,     NULL,      1 },
  {"audioDev",        SA_STR,  &sound_dev_name,     NULL,      0 },
  {"soundSampleRate", SA_INT,  &sound_sample_rate,  NULL,      1 },
  {"soundAutoclose",  SA_BOOL, &sound_to_autoclose, NULL,      1 },
  {"soundDspSetfrag", SA_BOOL, &sound_dsp_setfrag,  NULL,      0 },
  {"keyboardType",    SA_ENUM, &keyboard_type,      keybtypes, 1 },
  {"cursorType",      SA_ENUM, &cursor_type,        curstypes, 1 },
  {"allowAscii",      SA_BOOL, &spkb_allow_ascii,   NULL,      0 },
  {"trueShift",       SA_ENUM, &spkb_trueshift,     modifkeys, 0 },
  {"funcShift",       SA_ENUM, &spkb_funcshift,     modifkeys, 0 },
  {"colorType",       SA_ENUM, &color_type,         colrtypes, 1 },
  {"pauseOnIconify",  SA_BOOL, &pause_on_iconify,   NULL,      1 },
  {"vgaPauseBg",      SA_BOOL, &vga_pause_bg,       NULL,      1 },
  {"quickLoad",       SA_BOOL, &sp_quick_load,      NULL,      1 },
  {"autoStop",        SA_BOOL, &spt_auto_stop,      NULL,      1 },
  {"loadImmed",       SA_BOOL, &load_immed,         NULL,      1 },
  {"pause",           SA_BOOL, &sp_paused,          NULL,      1 },

  {NULL, 0, NULL, NULL, 0 }
};

static int file_type = -1;
static int file_subtype;

struct ext_type {
  const char *ext;
  int type;
  int subtype;
};

static struct ext_type extensions[] = {
  {"z80", FT_SNAPSHOT, SN_Z80},
  {"sna", FT_SNAPSHOT, SN_SNA},
  {"tzx", FT_TAPEFILE, TAP_TZX},
  {"tap", FT_TAPEFILE, TAP_TAP},

  {NULL, 0, 0}
};

int spcf_find_file_type(char *filename, int *ftp, int *ftsubp)
{
  int i;
  int found;

  if(*ftp >= 0 && *ftsubp >= 0) return 1;

  found = 0;
  
  for(i = 0; extensions[i].ext != NULL; i++) 
    if((*ftp < 0 || *ftp == extensions[i].type) &&
       (*ftsubp < 0 || *ftsubp == extensions[i].subtype) &&
       check_ext(filename, extensions[i].ext)) {
      found = 1;
      *ftp = extensions[i].type;
      *ftsubp = extensions[i].subtype;
      break;
    }

  if(!found) for(i = 0; extensions[i].ext != NULL; i++) 
    if((*ftp < 0 || *ftp == extensions[i].type) &&
       (*ftsubp < 0 || *ftsubp == extensions[i].subtype) &&
       try_extension(filename, extensions[i].ext)) {
      found = 1;
      *ftp = extensions[i].type;
      *ftsubp = extensions[i].subtype;
      break;
    }
  
  return found;
}

static int find_extension(const char *ext)
{
  int i;

  for(i = 0; extensions[i].ext != NULL; i++)
    if(strcmp(extensions[i].ext, ext) == 0) return i;

  return -1;
}

static const char *spco[] = {
  "help",
  "version",
  NULL
};

#define OPT_HELP     0
#define OPT_VERSION  1

static char *progname;
static int atcol;
#define MAXCOL 70
#define STTAB  "   "

static void putopt(const char *opt)
{
  if(atcol + strlen(opt) + 2 >= MAXCOL) {
    fprintf(stderr, "\n");
    atcol = 0;
  }
  if(!atcol) fprintf(stderr, STTAB);
  
  fprintf(stderr, "%s, ", opt);
  atcol += strlen(opt) + 2;
}

static void makeopt(char *optstr, const char *instr)
{
  *optstr++ = '-';
  for(; *instr; instr++) {
    if(isupper(*instr)) {
      *optstr++ = '-';
      *optstr++ = tolower(*instr);
    }
    else *optstr++ = *instr;
  }
  *optstr = '\0';
}

static void usage(void)
{
  char optionstr[128];
  int i, j;
  
  fprintf(stderr, "usage: %s [options] [[filetype] filename]\n\n",
	  progname);
  fprintf(stderr, "The following file types can be specified:\n");

  atcol = 0;
  
  for(i = 0; extensions[i].ext != NULL; i++) {
    makeopt(optionstr, extensions[i].ext);
    putopt(optionstr);
  }
  fprintf(stderr, "\n\n");
 
  fprintf(stderr, "The following options are available:\n");

  atcol = 0;

  for(i = 0; spco[i] != NULL; i++) {
    makeopt(optionstr, spco[i]);
    putopt(optionstr);
  }
  
  for(i = 0; spcf_options[i].option != NULL; i++) 
    if(spcf_options[i].disp) switch(spcf_options[i].argtype) {
    case SA_BOOL:
/*
      makeopt(optionstr, spcf_options[i].option);
      putopt(optionstr);
*/
      strcpy(optionstr, "[-no]");
      makeopt(optionstr+5, spcf_options[i].option);
      putopt(optionstr);
      break;

    case SA_INT:
      makeopt(optionstr, spcf_options[i].option);
      strcat(optionstr, " NUM");
      putopt(optionstr);
      break;

    case SA_ENUM:
      makeopt(optionstr, spcf_options[i].option);
      strcat(optionstr, " <");
      for(j = 0; spcf_options[i].enums[j] != NULL; j++) {
	strcat(optionstr, spcf_options[i].enums[j]);
	strcat(optionstr, " ");
      }
      optionstr[strlen(optionstr)-1] = '>';
      putopt(optionstr);
      break;
      
    case SA_STR:
      makeopt(optionstr, spcf_options[i].option);      
      strcat(optionstr, " NAME");
      putopt(optionstr);
      break;
    }
  
  fprintf(stderr, "\n");
}

static void process_option(int ix, int pre)
{
  pre = pre;

  switch(ix) {
  case OPT_HELP:
    usage();
    exit(0);
    break;
    
  case OPT_VERSION:
    fprintf(stderr, "%s\n", SPECTEMU_VERSION);
    exit(0);
    break;
  }
}



#define MAXATTRLEN 32

static int match_attr(const char *attr, int onlybool)
{
  int i;
  int nu;
  static char compa[MAXATTRLEN];
  
  nu = 0;
  for(i = 0; *attr && i < MAXATTRLEN - 1; attr++) {
    if(*attr == '-') {
      if(nu) return -1;
      nu = 1;
    }
    else if(nu) nu = 0, compa[i++] = toupper(*attr);
    else compa[i++] = tolower(*attr);
  }
  if(nu) return -1;
  compa[i] = '\0';

  for(i = 0; spcf_options[i].option != NULL; i++) 
    if((!onlybool || spcf_options[i].argtype == SA_BOOL) &&
       strcmp(spcf_options[i].option, compa) == 0) return i;
  
  return -1;
}

static int match_numbered(const char *attr, const char *beg, int maxnum)
{
  size_t blen;
  int num;

  blen = strlen(beg);
  if(strncmp(attr, beg, blen) == 0) {
    const char *ns;
    int nd;

    ns = attr+blen;
    nd = 0;
    for(; *ns; nd++, ns++) if(!isdigit(*ns)) return -1;

    if(nd > 0 && nd < 6 && 
       sscanf(attr+blen, "%d", &num) == 1 && num >= 0 && num <= maxnum) 
      return num;
  }
  return -1;
}

int spcf_match_keydef(const char *attr, const char *beg)
{
  size_t blen;
  
  blen = strlen(beg);
  
  if(strncmp(attr, beg, blen) == 0) {
    int i;
    const char *kn;
    
    kn = attr+blen;
    
    for(i = 32; i < 127; i++) 
      if(strcmp(kn, spcf_keynames_ascii[i-32]) == 0) return i;
    
    for(i = 0; i < 256; i++)
      if(spcf_keynames_misc[i] != NULL &&
	 strcmp(kn, spcf_keynames_misc[i]) == 0) return i+0x100;
  }
  return -1;
}


static int match_option(const char *attr)
{
  int i;

  for(i = 0; spco[i] != NULL; i++)
    if(strcmp(spco[i], attr) == 0) return i;
  
  return -1;
}

static int eqcstr(const char *s, const char *t)
{
  for(; *s || *t; s++, t++) if(tolower(*s) != *t) return 0;
  return 1;
}

static int istrue(const char *s)
{
  if(eqcstr(s, "true") ||
     eqcstr(s, "yes")  ||
     eqcstr(s, "on")   ||
     eqcstr(s, "1")) return 1;
  
  return 0;
}

static int isfalse(const char *s)
{
  if(eqcstr(s, "false") ||
     eqcstr(s, "no")  ||
     eqcstr(s, "off")   ||
     eqcstr(s, "0")) return 1;
  
  return 0;
}

void spcf_set_val(int ix, const char *val, const char *name, int ctr, 
		  int fatal)
{
  int res;
  long lv;
  int i;
  const char **es;
  int quiet;
  int *ip;
  char **cp;

  ip = (int *) spcf_options[ix].argvalp;
  cp = (char **) spcf_options[ix].argvalp;

  if(name == NULL) quiet = 1;
  else quiet = 0;

  switch(spcf_options[ix].argtype) {
  case SA_INT:
    res = sscanf(val, "%ld", &lv);
    if(res == 1) *ip = (int) lv;
    else if(!quiet) {
      fprintf(stderr, 
	      "%s:%i: Illegal value '%s', expected integer\n", 
	      name, ctr, val);
      if(fatal) exit(1);
    }
    break;
    
  case SA_BOOL:
    res = 0;
    if(istrue(val)) lv = 1, res = 1;
    else if(isfalse(val)) lv = 0, res = 1;
    else if(!quiet) {
      fprintf(stderr, 
	      "%s:%i: Illegal value '%s', expected "
	      "'true' or 'false'\n",
	      name, ctr, val);
      if(fatal) exit(1);
    }
    if(res) *ip = (int) lv;
    break;
    
  case SA_ENUM:
    res = 0;
    es = spcf_options[ix].enums;
    for(i = 0; es[i] != NULL; i++) if(strcmp(val, es[i]) == 0) {
      res = 1;
      break;
    }
    if(res) *ip = i;
    else if(!quiet) {
      fprintf(stderr, "%s:%i: Illegal value '%s', expected ", 
	      name, ctr, val);
      for(i = 0; es[i] != NULL;) {
	fprintf(stderr, "'%s'", es[i]);
	if(es[++i] != NULL) fprintf(stderr, " or ");
      }
      fprintf(stderr, "\n");
      if(fatal) exit(1);
    }
    break;
    
  case SA_STR:
    *cp = make_string(*cp, val);
    break;
  }
}

void spcf_set_color(int ix, const char *val, const char *name, int ctr, 
		    int fatal)
{
  int r, g, b;
  int res;
  int quiet;
  
  if(name == NULL) quiet = 1;
  else quiet = 0;

  res = sscanf(val, "%d %d %d", &r, &g, &b);
  if(res != 3 || r < 0 || r > 63 || g < 0 || g > 63 || b < 0 || b > 63) {
    if(!quiet) {
      fprintf(stderr, "%s:%i: Illegal rgb values: '%s'\n", name, ctr, val);
      if(fatal) exit(1);
    }
  }
  else {
    custom_colors[ix].r = r;
    custom_colors[ix].g = g;
    custom_colors[ix].b = b;
  }
}

#define MAXSPKEYNAME 32

void spcf_set_key(int ix, const char *val, const char *name, int ctr,
		  int fatal)
{
  int quiet;
  char spkeyname[MAXSPKEYNAME+1];
  int len;
  
  if(name == NULL) quiet = 1;
  else quiet = 0;

  if(!spkey_new_custom(ix)) {
    if(!quiet) {
      fprintf(stderr, "%s:%i: Custom key table full\n", name, ctr);
      if(fatal) exit(1);
    }
    return;
  }
  
  while(*val) {
    for(len = 0; val[len] && !isspace(val[len]); len++);
    if(len > MAXSPKEYNAME) len = MAXSPKEYNAME;
    strncpy(spkeyname, val, (size_t) len);
    spkeyname[len] = '\0';
    
    if(!spkey_add_custom(spkeyname)) {
      if(!quiet) {
	fprintf(stderr, "%s:%i: Illegal key name: '%s'\n", name, ctr, 
		spkeyname);
	if(fatal) exit(1);
      }
    }
    val += len;
    while(isspace(*val)) val++;
  }
  
  
}

void spcf_pre_check_options(int argc, char *argv[])
{
  progname = get_base_name(argv[0]);
  
  if(argc > 1 && argv[1][0] == '-') {
    int ix;

    ix = match_option(argv[1]+1);
    if(ix >= 0) process_option(ix, 1);
  }
}

void spcf_read_command_line(int argc, char *argv[])
{
  int a;
  int ix;
  int bval;

  for(a = 1; a < argc; a++) {
    if(argv[a][0] == '-') {
      if(file_type >= 0) {
	fprintf(stderr, "File name expected after option '%s'\n", argv[a-1]);
	exit(1);
      }
      bval = 1;
      if(strncmp(argv[a]+1, "no-", 3) == 0) {
	bval = 0;
	ix = match_attr(argv[a]+4, 1);
      }
      else ix = match_attr(argv[a]+1, 0);

      if(ix >= 0) {
	if(spcf_options[ix].argtype == SA_BOOL) 
	  *((int *) spcf_options[ix].argvalp) = (int) bval;
	else {
	  if(a+1 < argc) a++, spcf_set_val(ix, argv[a], "Command line", a, 1);
	  else {
	    fprintf(stderr, "Option '%s', argument expected\n",
		    argv[a]);
	    exit(1);
	  }
	}
      }
      else {
	ix = match_option(argv[a]+1);
	
	if(ix >= 0) process_option(ix, 0);
	else {
	  ix = find_extension(argv[a]+1);
	  
	  if(ix >= 0) {
	    if(a + 1 == argc) {
	      fprintf(stderr, 
		      "File name expected after option '%s'\n", argv[a]);
	      exit(1);
	    }

	    file_type = extensions[ix].type;
	    file_subtype = extensions[ix].subtype;
	  }
	  else {
	    fprintf(stderr, "Option '%s' not recognized\n", argv[a]);
	    fprintf(stderr, "Use '-help' to see valid options\n");
	    exit(1);
	  }
	}
      }
    }
    else { /* Does not start with '-' */
      strncpy(filenamebuf, argv[a], MAXFILENAME - 10);
      filenamebuf[MAXFILENAME-10] = '\0';
      
      if(file_type < 0) file_subtype = -1;
      
      if(!spcf_find_file_type(filenamebuf, &file_type, &file_subtype)) {
	fprintf(stderr, "Can't determine file type of '%s'\n", filenamebuf);
	exit(1);
      }
      
      if(file_type == FT_SNAPSHOT) {
	spcf_init_snapshot = make_string(spcf_init_snapshot, filenamebuf);
	spcf_init_snapshot_type = file_subtype;
      }
      else if(file_type == FT_TAPEFILE) {
	spcf_init_tapefile = make_string(spcf_init_tapefile, filenamebuf);
	spcf_init_tapefile_type = file_subtype;
      }
      
      file_type = -1;
    }
  }
}

int spcf_read_conf_file(const char *filename)
{
  int res;
  char *attr, *val;

  conffile = filename;
  
  conffp = fopen(conffile, "rt");
  if(conffp == NULL) {
    fprintf(stderr, "Could not open file %s: %s\n", 
	    conffile, strerror(errno));
    return -1;
  }
  linectr = 0;
  
  for(;;) {
    int ix;

    res = read_conf_line(&attr, &val);
    if(res <= 0) break;

    ix = match_attr(attr, 0);
    if(ix >= 0) spcf_set_val(ix, val, conffile, linectr, 0);
    else {
      ix = match_numbered(attr, "color", COLORNUM-1);
      if(ix >= 0) spcf_set_color(ix, val, conffile, linectr, 0);
      else {
	ix = spcf_match_keydef(attr, "Key_");
	if(ix >= 0) spcf_set_key(ix, val, conffile, linectr, 0);
	else fprintf(stderr, "%s:%i: Unknown attribute '%s'\n", 
		     conffile, linectr, attr);
      }
    }
  }

  fclose(conffp);

  return res;
}