Subversion Repositories shark

Rev

Rev 2 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
2 pj 1
/*
2
 *
3
 *
4
 */
5
 
6
#include <stdio.h>
7
#include <stdlib.h>
8
#include <string.h>
9
#include <ctype.h>
10
 
11
#include "hconf.h"
12
 
13
struct variable vartable[MAXVARIABLES];
14
long varhelp[MAXVARIABLES];
15
struct menu menutable[MAXMENUS];
16
 
17
#if 0
18
#define printd(fmt,args...) fprintf(stderr,fmt,##args)
19
#else
20
#define printd(fmt,args...)
21
#endif
22
 
23
struct menu *mainmenu=NULL;
24
char *caption=NULL;
25
 
26
/*
27
 *
28
 */
29
 
30
int copystring(char **dstptr, char *src)
31
{
32
  int sz;
33
  if (*dstptr!=NULL) free(*dstptr);
34
  sz=strlen(src)+1;
35
  *dstptr=(char*)malloc(sz);
36
  if (*dstptr==NULL) return -100;
37
  strcpy(*dstptr,src);
38
  return 0;
39
}
40
 
41
/*
42
 *
43
 */
44
 
45
int nextvariable=0;
46
 
47
struct variable *findvar(char *s)
48
{
49
  int i;
50
  i=0;
51
  while (i<nextvariable) {
52
    if (!strcmp(vartable[i].name,s)) return vartable+i;
53
    i++;
54
  }
55
  return NULL;
56
}
57
 
58
struct variable *newvar(char *s, int type)
59
{
60
  /* safety test */
61
  if (findvar(s)!=NULL) return NULL;
62
 
63
  if (nextvariable>=MAXVARIABLES) return NULL;
64
 
65
  vartable[nextvariable].name=NULL;
66
  copystring(&vartable[nextvariable].name,s);
67
  vartable[nextvariable].type=type;
68
  vartable[nextvariable].value=NULL;
69
  vartable[nextvariable].flags=0;
70
 
71
  return vartable+nextvariable++;
72
}
73
 
74
/* -- */
75
 
76
int nextmenu=0;
77
 
78
struct menu *findmenu(char *s)
79
{
80
  int i;
81
  i=0;
82
  while (i<nextmenu) {
83
    if (!strcmp(menutable[i].name,s)) return menutable+i;
84
    i++;
85
  }
86
  return NULL;
87
}
88
 
89
struct menu *newmenu(char *s)
90
{
91
  if (nextmenu>=MAXMENUS) return NULL;
92
 
93
  menutable[nextmenu].name=NULL;
94
  copystring(&menutable[nextmenu].name,s);
95
  menutable[nextmenu].title=NULL;
96
  menutable[nextmenu].entries=NULL;
97
 
98
  return menutable+nextmenu++;
99
}
100
 
101
/*
102
 *
103
 *
104
 *
105
 */
106
 
107
struct _case *newcase(struct menuentry *menuent)
108
{
109
  struct _case *c;
110
  struct _case *n;
111
  c=malloc(sizeof(struct _case));
112
  if (c==NULL) return NULL;
113
  c->next=NULL;
114
  c->value=NULL;
115
  c->list=NULL;
116
  if (menuent->x._case.list==NULL) {
117
    menuent->x._case.list=c;
118
    return c;
119
  }
120
  n=menuent->x._case.list;
121
  for (;;) {
122
    if (n->next==NULL) {
123
      n->next=c;
124
      return c;
125
    }
126
    n=n->next;
127
  }
128
}
129
 
130
struct choice *newchoice(struct menuentry *menuent)
131
{
132
  struct choice *c;
133
  struct choice *n;
134
  c=malloc(sizeof(struct choice));
135
  if (c==NULL) return NULL;
136
  c->next=NULL;
137
  c->text=NULL;
138
  c->var=NULL;
139
  if (menuent->x.choice.list==NULL) {
140
    menuent->x.choice.list=c;
141
    return c;
142
  }
143
  n=menuent->x.choice.list;
144
  for (;;) {
145
    if (n->next==NULL) {
146
      n->next=c;
147
      return c;
148
    }
149
    n=n->next;
150
  }
151
}
152
 
153
struct menuentry *newmenuentry(struct menuentry **ptr)
154
{
155
  struct menuentry *n;
156
  struct menuentry *p;
157
  n=malloc(sizeof(struct menuentry));
158
  if (n==NULL) return NULL;
159
  n->next=NULL;
160
  n->type=0;
161
  if (*ptr==NULL) {
162
    *ptr=n;
163
    return n;
164
  }
165
  p=*ptr;
166
  for (;;) {
167
    if (p->next==NULL) {
168
      p->next=n;
169
      return n;
170
    }
171
    p=p->next;
172
  }
173
}
174
 
175
/*
176
 *
177
 *
178
 *
179
 */
180
 
181
/*
182
 *
183
 *
184
 *
185
 */
186
 
187
int parsebool(struct menuentry **ptr);
188
int parsetristate(struct menuentry **ptr);
189
int parserange(struct menuentry **ptr);
190
int parsesubmenu(struct menuentry **ptr);
191
int parsecase(struct menuentry **ptr);
192
int parsechoice(struct menuentry **ptr);
193
int parseseparator(struct menuentry **ptr);
194
 
195
int parsemenufield(struct menuentry **ptr)
196
{
197
  char *s;
198
 
199
  s=gettoken();
200
  if (s==NULL) return -999;
201
 
202
  if (!strcmp(s,"bool")) return parsebool(ptr);
203
  if (!strcmp(s,"tristate")) return parsetristate(ptr);
204
  if (!strcmp(s,"range")) return parserange(ptr);
205
  if (!strcmp(s,"submenu")) return parsesubmenu(ptr);
206
  if (!strcmp(s,"switch")) return parsecase(ptr);
207
  if (!strcmp(s,"choice")) return parsechoice(ptr);
208
  if (!strcmp(s,"separator")) return parseseparator(ptr);
209
 
210
  return -333;
211
}
212
 
213
/* --- */
214
 
215
int parsecase(struct menuentry **ptr)
216
{
217
  char *s;
218
  struct menuentry *ent;
219
  struct _case *cas=NULL;
220
  struct variable *v;
221
  int res;
222
 
223
  printd("start parseswitch()\n");
224
 
225
  ent=newmenuentry(ptr);
226
  if (ent==NULL) return -31;
227
  ent->type=CASEENTRY;
228
  ent->x._case.list=NULL;
229
 
230
  s=gettoken();
231
  if (s==NULL) return -32;
232
  v=findvar(s);
233
  if (v==NULL) return -36;
234
  ent->x._case.var=v;
235
  v->flags|=FLAG_M;
236
 
237
  for (;;) {
238
    s=gettoken();
239
    if (!strcmp(s,"endswitch")) break;
240
 
241
    if (!strcmp(s,"case")) {
242
      cas=newcase(ent);
243
      if (cas==NULL) return -34;
244
      s=gettoken();
245
      if (s==NULL) return -35;
246
      if (*s!='\"') return -35;
247
      cas->value=NULL;
248
      copystring(&cas->value,s+1);
249
      continue;
250
    }
251
 
252
    if (cas==NULL) return -39;
253
 
254
    ungettoken();
255
    res=parsemenufield(&cas->list);
256
    if (res) return res;
257
  }
258
 
259
  return 0;
260
}
261
 
262
int parsechoice(struct menuentry **ptr)
263
{
264
  struct menuentry *ent;
265
  struct choice *cho;
266
  struct variable *v;
267
  char *def;
268
  int defdone;
269
  char *s;
270
 
271
  printd("start parsechoice()\n");
272
 
273
  ent=newmenuentry(ptr);
274
  if (ent==NULL) return -31;
275
  ent->type=CHOICEENTRY;
276
  ent->x.choice.list=NULL;
277
  ent->x.choice.act=NULL;
278
  ent->x.choice.numchoices=0;
279
 
280
  s=gettoken();
281
  if (s==NULL) return -32;
282
  if (*s!='\"') return -33;
283
  ent->x.choice.text=NULL;
284
  copystring(&ent->x.choice.text,s+1);
285
 
286
  defdone=0;
287
  def=NULL;
288
  s=gettoken();
289
  if (s==NULL) return -39;
290
  if (*s!='\"') {
291
    copystring(&def,s);
292
  } else
293
    ungettoken();
294
 
295
  for (;;) {
296
    s=gettoken();
297
    if (s==NULL) return -34;
298
    if (!strcmp(s,"endchoice")) {
299
      if (def==NULL||!defdone) return -37;
300
      free(def);
301
      break;
302
    }
303
 
304
    if (*s!='\"') return -38;
305
 
306
    cho=newchoice(ent);
307
    ent->x.choice.numchoices++;
308
    if (cho==NULL) return -33;
309
    cho->text=NULL;
310
    copystring(&cho->text,s+1);
311
 
312
    s=gettoken();
313
    if (s==NULL) return -34;
314
    v=newvar(s,CHOICEVAR);
315
    if (v==NULL) return -37;
316
    cho->var=v;
317
    v->value=NULL;
318
    copystring(&v->value,"n");
319
 
320
    if (def==NULL) {
321
      ent->x.choice.act=cho;
322
      copystring(&def,s);
323
      defdone=1;
324
    } else if (!strcmp(def,s)) {
325
      ent->x.choice.act=cho;
326
      defdone=1;
327
    }
328
  }
329
 
330
  return 0;
331
}
332
 
333
int parsebool(struct menuentry **ptr)
334
{
335
  struct menuentry *ent;
336
  struct variable *var;
337
  char *s;
338
 
339
  printd("start parsebool()\n");
340
 
341
  ent=newmenuentry(ptr);
342
  if (ent==NULL) return -50;
343
  ent->type=BOOLENTRY;
344
 
345
  s=gettoken();
346
  if (s==NULL) return -51;
347
  if (*s!='\"') return -55;
348
  ent->x.bool.text=NULL;
349
  copystring(&ent->x.bool.text,s+1);
350
 
351
  s=gettoken();
352
  if (s==NULL) return -52;
353
  var=newvar(s,BOOLVAR);
354
  if (var==NULL) return -53;
355
  ent->x.bool.var=var;
356
 
357
  s=gettoken();
358
  if (*s!='\"') {
359
    ungettoken();
360
    var->value=(char *)malloc(2);
361
    var->value[0]='n';
362
    var->value[1]='\0';
363
    return 0;
364
  }
365
  if (*(s+1)=='\0') return -54;
366
  var->value=NULL;
367
  copystring(&var->value,s+1);
368
 
369
  return 0;
370
}
371
 
372
int parseseparator(struct menuentry **ptr)
373
{
374
  struct menuentry *ent;
375
 
376
  printd("start parseseparator()\n");
377
 
378
  ent=newmenuentry(ptr);
379
  if (ent==NULL) return -50;
380
  ent->type=SEPARATORENTRY;
381
 
382
  return 0;
383
}
384
 
385
int parserange(struct menuentry **ptr)
386
{
387
  struct menuentry *ent;
388
  struct variable *var;
389
  char *s;
390
  int v;
391
 
392
  printd("start parserange()\n");
393
 
394
  ent=newmenuentry(ptr);
395
  if (ent==NULL) return -60;
396
  ent->type=RANGEENTRY;
397
 
398
  s=gettoken();
399
  if (s==NULL) return -61;
400
  if (*s!='\"') return -65;
401
  ent->x.range.text=NULL;
402
  copystring(&ent->x.range.text,s+1);
403
 
404
  s=gettoken();
405
  if (s==NULL) return -62;
406
  var=newvar(s,RANGEVAR);
407
  if (var==NULL) return -63;
408
  ent->x.range.var=var;
409
 
410
  s=gettoken();
411
  if (s==NULL) return -66;
412
  ent->x.range.minval=atoi(s);
413
  if (ent->x.range.minval==0&&strcmp(s,"0")) return -67;
414
 
415
  s=gettoken();
416
  if (s==NULL) return -66;
417
  ent->x.range.maxval=atoi(s);
418
  if (ent->x.range.maxval==0&&strcmp(s,"0")) return -68;
419
 
420
  s=gettoken();
421
  if (s==NULL) return -69;
422
  v=atoi(s);
423
  if (v==0&&strcmp(s,"0")) {
424
    ungettoken();
425
    v=ent->x.range.minval;
426
  }
427
 
428
  {
429
    char buffer[256];
430
    sprintf(buffer,"%i",v);
431
    var->value=NULL;
432
    copystring(&var->value,buffer);
433
  }
434
 
435
  return 0;
436
}
437
 
438
int parsetristate(struct menuentry **ptr)
439
{
440
  struct menuentry *ent;
441
  struct variable *var;
442
  char *s;
443
 
444
  printd("start parsetristate()\n");
445
 
446
  ent=newmenuentry(ptr);
447
  if (ent==NULL) return -50;
448
  ent->type=TRISTATEENTRY;
449
 
450
  s=gettoken();
451
  if (s==NULL) return -51;
452
  if (*s!='\"') return -55;
453
  ent->x.tristate.text=NULL;
454
  copystring(&ent->x.tristate.text,s+1);
455
 
456
  s=gettoken();
457
  if (s==NULL) return -52;
458
  var=newvar(s,TRISTATEVAR);
459
  if (var==NULL) return -53;
460
  ent->x.tristate.var=var;
461
 
462
  s=gettoken();
463
  if (*s!='\"') {
464
    ungettoken();
465
    var->value=(char *)malloc(2);
466
    var->value[0]='n';
467
    var->value[1]='\0';
468
    return 0;
469
  }
470
  if (*(s+1)=='\0') return -54;
471
  var->value=NULL;
472
  copystring(&var->value,s+1);
473
 
474
  return 0;
475
}
476
 
477
int parsesubmenu(struct menuentry **ptr)
478
{
479
  struct menuentry *ent;
480
  char *s;
481
 
482
  printd("start parsesubmenu()\n");
483
 
484
  ent=newmenuentry(ptr);
485
  if (ent==NULL) return -40;
486
  ent->type=SUBMENUENTRY;
487
 
488
  s=gettoken();
489
  if (s==NULL) return -41;
490
  if (*s!='\"') return -43;
491
  ent->x.submenu.text=NULL;
492
  copystring(&ent->x.submenu.text,s+1);
493
 
494
  s=gettoken();
495
  if (s==NULL) return -42;
496
  ent->x.submenu.menuname=NULL;
497
  ent->x.submenu.menu=NULL;
498
  copystring(&ent->x.submenu.menuname,s);
499
 
500
  return 0;
501
}
502
 
503
int parsetitle(struct menu *ptr)
504
{
505
  char *s;
506
 
507
  printd("start parsetitle()\n");
508
 
509
  s=gettoken();
510
  if (s==NULL) return -71;
511
  ptr->title=NULL;
512
  copystring(&ptr->title,s+1);
513
  return 0;
514
}
515
 
516
int parsecaption(void)
517
{
518
  char *s;
519
 
520
  printd("start parsecaption()\n");
521
 
522
  s=gettoken();
523
  if (s==NULL) return -91;
524
  caption=NULL;
525
  copystring(&caption,s+1);
526
  return 0;
527
}
528
 
529
int parsemenu(void)
530
{
531
  struct menu *ptr;
532
  char *s;
533
  int ret;
534
 
535
  printd("start parsemenu()\n");
536
  ret=0;
537
 
538
  s=gettoken();
539
  if (s==NULL) return -12;
540
  ptr=newmenu(s);
541
  if (ptr==NULL) return -15;
542
 
543
  for (;;) {
544
    s=gettoken();
545
    if (!strcmp(s,"endmenu")) return 0;
546
 
547
    if (!strcmp(s,"title")) {
548
      ret=parsetitle(ptr);
549
      if (ret) return ret;
550
      continue;
551
    }
552
 
553
    ungettoken();
554
    ret=parsemenufield(&ptr->entries);
555
    if (ret) break;
556
  }
557
 
558
  return ret;
559
}
560
 
561
/*
562
 *
563
 */
564
 
565
int resolvmenuentries(struct menuentry *ptr)
566
{
567
  struct _case *ca;
568
  int res,i;
569
 
570
  while (ptr!=NULL) {
571
    switch(ptr->type) {
572
    case BOOLENTRY:
573
      break;
574
    case TRISTATEENTRY:
575
      break;
576
    case SUBMENUENTRY:
577
      ptr->x.submenu.menu=findmenu(ptr->x.submenu.menuname);
578
      if (ptr->x.submenu.menu==NULL) return -10;
579
      break;
580
    case CHOICEENTRY:
581
      break;
582
    case CASEENTRY:
583
      ca=ptr->x._case.list;
584
      i=1;
585
      while (ca!=NULL) {
586
	res=resolvmenuentries(ca->list);
587
	if (res) return res;
588
	ca=ca->next;
589
	i++;
590
      }
591
      break;
592
    }
593
    ptr=ptr->next;
594
  }
595
 
596
  return 0;
597
}
598
 
599
static int resolv(void)
600
{
601
  int res,i;
602
 
603
  i=0;
604
  while (i<nextmenu) {
605
    res=resolvmenuentries(menutable[i].entries);
606
    if (res) return res;
607
    i++;
608
  }
609
 
610
  return 0;
611
}
612
 
613
/*
614
 *
615
 */
616
 
617
int parse(void)
618
{
619
  char *s;
620
  int ret;
621
  ret=0;
622
  while ((s=gettoken())!=NULL) {
623
    if (!strcmp(s,"menu")) ret=parsemenu();
624
    else if (!strcmp(s,"caption")) ret=parsecaption();
625
    else ret=-1;
626
    if (ret) break;
627
  }
628
  return ret;
629
}
630
 
631
int parsehelp(char *filename)
632
{
633
  char buffer[256];
634
  FILE *fin;
635
  fin=fopen(filename,"rt");
636
  if (fin==NULL) return 0;
637
 
638
  fclose(fin);
639
}
640
 
641
int readconfigin(char *filename)
642
{
643
  char buffer[256];
644
  char *ptr;
645
  int res,i;
646
 
647
  for (i=0;i<MAXVARIABLES;i++)
648
    varhelp[i]=0;
649
 
650
  res=init_tokenizer(filename,0x01);
651
  if (res) return res;
652
 
653
  res=parse();
654
  if (res) return res;
655
 
656
  res=done_tokenizer();
657
  if (res) return res;
658
 
659
  res=resolv();
660
  if (res) return res;
661
 
662
  mainmenu=findmenu("main");
663
  if (mainmenu==NULL) return -97;
664
 
665
  /* -- */
666
 
667
  ptr=strstr(filename,".in");
668
  if (ptr!=NULL) *ptr='\0';
669
  else {
670
    ptr=strchr(filename,'.');
671
    if (ptr!=NULL) *ptr='\0';
672
  }
673
 
674
  strcpy(buffer,filename);
675
  strcat(buffer,".hlp");
676
 
677
  res=parsehelp(buffer);
678
  if (res) return res;
679
 
680
  return 0;
681
}
682
 
683
/*
684
 *
685
 *
686
 *
687
 */
688
 
689
void dumpvariables(void)
690
{
691
  int i;
692
 
693
  fprintf(stderr,"VARIABLES TABLE\n---------------\n\n");
694
  i=0;
695
  while (i<nextvariable) {
696
    fprintf(stderr,"name:  -%s-\n",vartable[i].name);
697
    if (vartable[i].value!=NULL)
698
      fprintf(stderr,"value: \"%s\"\n",vartable[i].value);
699
    else
700
      fprintf(stderr,"value: NO VALUE\n");
701
    fprintf(stderr,"flags: 0x%04x\n",vartable[i].flags);
702
    fputc('\n',stderr);
703
    i++;
704
  }
705
}
706
 
707
#if 0
708
 
709
void dumpmenuentrytype(struct menuentry *ptr,char *s)
710
{
711
  fprintf(stderr,"%s entry type  : ",s);
712
  switch(ptr->type) {
713
  case BOOLENTRY: fprintf(stderr,"bool\n"); break;
714
  case SUBMENUENTRY: fprintf(stderr,"submenu\n"); break;
715
  case TRISTATEENTRY: fprintf(stderr,"tristate\n"); break;
716
  case CHOICEENTRY: fprintf(stderr,"choice\n"); break;
717
  case CASEENTRY: fprintf(stderr,"case\n"); break;
718
  default: fprintf(stderr,"unknown\n"); break;
719
  }
720
}
721
 
722
void dumpmenuentries(struct menuentry *ptr,int x)
723
{
724
  struct choice *cho;
725
  struct _case *ca;
726
  char blanks[64];
727
  int i;
728
  memset(blanks,' ',sizeof(blanks));
729
  blanks[x-1]='\0';
730
  while (ptr!=NULL) {
731
    dumpmenuentrytype(ptr,blanks);
732
    switch(ptr->type) {
733
    case BOOLENTRY:
734
      fprintf(stderr,"%s text        : '%s'\n",blanks,ptr->x.bool.text);
735
      fprintf(stderr,"%s variable    : %s\n",blanks,ptr->x.bool.var->name);
736
      break;
737
    case TRISTATEENTRY:
738
      fprintf(stderr,"%s text        : '%s'\n",blanks,ptr->x.tristate.text);
739
      fprintf(stderr,"%s variable    : %s\n",blanks,ptr->x.tristate.var->name);
740
      break;
741
    case SUBMENUENTRY:
742
      fprintf(stderr,"%s text        : '%s'\n",blanks,ptr->x.submenu.text);
743
      fprintf(stderr,"%s submenu     : %s\n",blanks,ptr->x.submenu.menu->name);
744
      break;
745
    case CHOICEENTRY:
746
      fprintf(stderr,"%s text        : '%s'\n",blanks,ptr->x.choice.text);
747
      fprintf(stderr,"%s def variable: %s\n",blanks,ptr->x.choice.act->var->name);
748
      i=1;
749
      cho=ptr->x.choice.list;
750
      while (cho!=NULL) {
751
	fprintf(stderr,"%s text %i      : '%s'\n",blanks,i,cho->text);
752
	fprintf(stderr,"%s variable %i  : %s\n",blanks,i,cho->var->name);
753
	i++;
754
	cho=cho->next;
755
      }
756
      break;
757
    case CASEENTRY:
758
      fprintf(stderr,"%s variable    : %s\n",blanks,ptr->x._case.var->name);
759
      fputc('\n',stderr);
760
      ca=ptr->x._case.list;
761
      i=1;
762
      while (ca!=NULL) {
763
	fprintf(stderr,"%s value %i     : '%s'\n",blanks,i,ca->value);
764
	dumpmenuentries(ca->list,x+2);
765
	ca=ca->next;
766
	i++;
767
      }
768
      break;
769
    }
770
    fputc('\n',stderr);
771
    ptr=ptr->next;
772
  }
773
}
774
 
775
void dumpmenus(void)
776
{
777
  int i;
778
 
779
  fprintf(stderr,"MENUS TABLE\n-----------\n\n");
780
  i=0;
781
  while (i<nextmenu) {
782
    fprintf(stderr,"name:  %s\n",menutable[i].name);
783
    fprintf(stderr,"text:  '%s'\n",menutable[i].comment);
784
    fputc('\n',stderr);
785
    dumpmenuentries(menutable[i].entries,2);
786
    //fputc('\n',stderr);
787
    i++;
788
  }
789
}
790
#endif
791
 
792