File: menu-method.cc

package info (click to toggle)
menu 2.1.5-3
  • links: PTS
  • area: main
  • in suites: potato
  • size: 1,476 kB
  • ctags: 1,251
  • sloc: cpp: 6,222; ansic: 2,306; sh: 453; makefile: 296; sed: 93
file content (1187 lines) | stat: -rw-r--r-- 31,180 bytes parent folder | download | duplicates (2)
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
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
#include <strstream.h>
#include <iostream.h>
#include <fstream.h>
#include <set.h>
#include <list.h>
#include <getopt.h>
#include <ctype.h>
#include <unistd.h>
#include <stdlib.h>
#include <values.h>
#include <sys/types.h>
#include <dirent.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/stat.h>

#include "menu-method.h"
#include "menu-tree.h"

int show_time=0, verbose=0, dodebug=0;

map <String, func *, less<String> > func_data;

menuentry menu;
configinfo *config;
supportedinfo *supported;
int linenumber=-123456;

ofstream *genoutputfile=NULL;
set<String> outputnames; //all names of files ever written to


struct option long_options[] = { 
  { "showtime", no_argument, &show_time, 1 }, 
  { "verbose", no_argument, &verbose, 1 }, 
  { "debug", no_argument, &dodebug, 1 }, 
  { "help", no_argument, NULL, 'h' }, 
  { "stdin", no_argument, NULL, 'f' }, 
  { NULL, 0, NULL, 0 } };

void usage(){
  cerr<<"menu-method: generate window-manager 'rc' files (or html docs)"<<endl
      <<"  menu-method gets the menuentries from standard in."<<endl
      <<"  Options to menu-method: "<<endl
      <<"     -h --help : this message"<<endl
      <<"     -d --debug: show debug info"<<endl
      <<"     -showtime : show timeing information"<<endl;
  exit(1);
}

func_def::~func_def(){
}

void store_func(func *f){
  func_data[f->name()]=f;
}
void add_functions(){
  store_func(new prefix_func);
  store_func(new ifroot_func);

  store_func(new print_func);
  store_func(new add_func);
  store_func(new sub_func);
  store_func(new mult_func);
  store_func(new div_func);
  store_func(new ifempty_func);
  store_func(new ifnempty_func);
  store_func(new iffile_func);
  store_func(new ifelsefile_func);
  store_func(new ifelse_func);
  store_func(new catfile_func);

  store_func(new ifeq_func);
  store_func(new ifneq_func);
  store_func(new ifeqelse_func);

  store_func(new cond_surr_func);
  store_func(new esc_func);
  store_func(new escwith_func);
  store_func(new escfirst_func);
  store_func(new tolower_func);  
  store_func(new toupper_func);
  store_func(new replacewith_func);
  store_func(new nstring_func);  
  store_func(new cppesc_func);
  store_func(new parent_func);
  store_func(new basename_func);
  store_func(new entrycount_func);
  store_func(new entryindex_func);
  store_func(new firstentry_func);
  store_func(new lastentry_func);
  store_func(new level_func);

  store_func(new rcfile_func);
  store_func(new examplercfile_func);
  store_func(new mainmenutitle_func);
  store_func(new rootsection_func);
  store_func(new rootprefix_func);
  store_func(new userprefix_func);
  store_func(new treewalk_func);
  store_func(new postoutput_func);
  store_func(new preoutput_func);
}


bool empty_String(const String &s){
  if(s.length()&&(s!=String("none")))
    return false;
  else
    return true;
}


cat_str *get_eq_cat_str(parsestream &i){
  i.skip_space();
  i.skip_char('=');
  return new cat_str(i);
}

int check_dir(String s){
  String t;
  unsigned int i;
  if(dodebug)
    cerr<<"CHECK_DIR: "<<s<<endl;
  while(s.length()){
    i=s.find('/',1);
    if(i<s.length()){
      t=s.substr(0,i+1);
      for(; ((unsigned int)i<s.length()) && (s[i]=='/'); i++);
      s=s.substr(s.begin()+i,s.end());
    } else {
      t=s;
      s="";
    }
    if(chdir(t.c_str())<0){
      if(dodebug)
	cerr<<"MKDIR "<<t<<endl;
      if(mkdir(t.c_str(),0755))
	throw dir_createerror(t);
      if(chdir(t.c_str()))
	throw dir_createerror(t);
    } else {
      if(dodebug)
	cerr<<" dir "<<t<<" already exists "<<endl;
    }
  }
  return !s.length();
}

void closegenoutput(){
  set<String>::iterator i;

  // OK, this will look clumsy in strace output, especially if there's
  // only output file: first we close, and immediately afterwards we
  // open and write again. But I don't see an easy and general way
  // to get round that.
  if(genoutputfile){
    delete genoutputfile;
    genoutputfile=NULL;
  }
  for(i=outputnames.begin(); i!=outputnames.end(); i++){
    ofstream f((*i).c_str(), ios::app);
    f<<(config->postoutput());
  }
}
void genoutput(const String &s,
	       map<String, String, less<String> > &v){
  
  static String lastname="////";
  String name;
  String dir;

  name=config->prefix()+"/"+config->genmenu->soutput(v);
  //if(dodebug)
  //  cerr<<"GENOUTPUT: name="<<name<<endl;
  if(name!=lastname){
    if(genoutputfile)
      delete genoutputfile;
    if(outputnames.find(name)==outputnames.end()){
      outputnames.insert(name);
      check_dir(String_parent(name));
      // after opening a file with ios::trunc, all writes seem to fail!
      //genoutputfile=new ofstream(name.c_str(), ios::trunc);
      // So, I do it this way instead:
      unlink(name.c_str());
      genoutputfile=new ofstream(name.c_str());
      (*genoutputfile)<<config->preoutput();    
    } else
      genoutputfile=new ofstream(name.c_str(),ios::app);      
    lastname=name;
  }
  (*genoutputfile)<<s;
}

/////////////////////////////////////////////////////
//  Construction:
//

str::~str(){
}
cat_str::cat_str(parsestream &i){
  char c;
  c=i.get_char();
  i.put_back(c);
  try{
    while(1){
      i.skip_space();
      c=i.get_char();
      i.put_back(c);
      if(isalpha(c))
	v.push_back(new func_str(i));
      else if(c=='\"') 
	v.push_back(new const_str(i));
      else if(c=='$') 
	v.push_back(new var_str(i));
      else if(c==',')
	break;
      else if(c==')')
	break;
      else if(c=='\0')
	break;
      else
	throw char_unexpected(&i, c);
    }
  }catch(endofline p){};
}

const_str::const_str(parsestream &i){
  data=i.get_Stringconst();
}

var_str::var_str(parsestream &i){
  i.skip_char('$');
  var_name=i.get_name();
}

func_str::func_str(parsestream &i){
  char c;
  String name;
  map <String, func *, less<String> >::iterator j;

  name=i.get_name();
  j=func_data.find(name);
  if(j==func_data.end()){
    throw unknown_function(&i);
  } else
    f=(*j).second;

  i.skip_space();
  i.skip_char('(');
  do{
    i.skip_space();
    c=i.get_char();
    if(c==')')
      break;
    i.put_back(c);
    args.push_back(new cat_str(i));
    i.skip_space();
  } while((c=i.get_char())&&(c==','));
  i.put_back(c);
  i.skip_char(')');
  if(args.size()!=(unsigned int)f->nargs())
    throw narg_mismatch(&i, name);
}


/////////////////////////////////////////////////////
//  Output routines
//

ostream &const_str::output(ostream &o, 
			   map<String, String, less<String> > &/*menuentry*/){
  //  cout<<"IN CONST_STR: data="<<data<<endl;
  return o<<data;
}

String cat_str::soutput(map<String, String, less<String> > &menuentry){
  char buf[MAX_BUF];
  vector <str * >::iterator i;
  ostrstream s(buf,sizeof(buf));
  
  //cout<<"CONFIG->rfc="<<config->rcff()<<endl;
  
  for(i=v.begin();i!=v.end();i++){
    //char Duf[MAX_BUF];
    //ostrstream D(Duf,sizeof(buf));
    (*i)->output(s,menuentry);
    //(*i)->output(D,menuentry);
    //D<<ends;
    //cout<<"DDD "<<Duf<<endl;
  }
  s<<ends;
  
  return String(buf);
}
ostream &cat_str::output(ostream &o, 
			 map<String, String, less<String> > &menuentry){
  o<<soutput(menuentry);
  return o;
}
void cat_str::output(map<String, String, less<String> > &menuentry){
  genoutput(soutput(menuentry),menuentry);
}

ostream &var_str::output(ostream &o,
			 map<String, String, less<String> > &menuentry){
  return o<<menuentry[var_name];
}
ostream &func_str::output(ostream &o,
			 map<String, String, less<String> > &menuentry){
  return f->output(o,args,menuentry);
}

/////////////////////////////////////////////////////
//  Debug routines
//
ostream &const_str::debuginfo(ostream &o){
  return o<<"CONST_STR: "<<data<<endl;
}
ostream &cat_str::debuginfo(ostream &o){
  vector<str *>::iterator i;
  o<<"CAT_STR: "<<endl;
  for(i=v.begin();i!=v.end();i++){
    (*i)->debuginfo(o);
  }
  return o;
}
ostream &var_str::debuginfo(ostream &o){
  return o<<"VAR_STR: "<<var_name<<endl;
}
ostream &func_str::debuginfo(ostream &o){
  o<<"FUNC_STR: "<<f->name()<<" ("<<endl;
  vector<cat_str *>::iterator i;  
  for(i=args.begin();i!=args.end();i++){
    o<<", ";
    (*i)->debuginfo(o);
  }
  return o<<")"<<endl;
}


/////////////////////////////////////////////////////
//  Function definitions:
//

ostream &prefix_func::output(ostream &o, vector<cat_str *> &,
			     map<String, String, less<String> > &){

  return o<<config->prefix();
}
ostream &ifroot_func::output(ostream &o, vector<cat_str *> & args,
			     map<String, String, less<String> > &menuentry){
  if(getuid())
    args[1]->output(o,menuentry);
  else
    args[0]->output(o,menuentry);

  return o;
}

ostream &print_func::output(ostream &o, vector<cat_str *> &args,
			    map<String, String, less<String> > &menuentry){

  String s=args[0]->soutput(menuentry);

  if(empty_String(s)){
    cerr<<"Zero-size argument to print function";
    throw informed_fatal();
  }
  return o<<s;
}
ostream &ifempty_func::output(ostream &o, vector<cat_str *> &args,
			      map<String, String, less<String> > &menuentry){

  String s=args[0]->soutput(menuentry);

  if(empty_String(s))
    args[1]->output(o,menuentry);
  return o;
}
ostream &ifnempty_func::output(ostream &o, vector<cat_str *> &args,
			       map<String, String, less<String> > &menuentry){

  String s=args[0]->soutput(menuentry);
  if(!empty_String(s))
    args[1]->output(o,menuentry);
  return o;
}
ostream &iffile_func::output(ostream &o, vector<cat_str *> &args,
			     map<String, String, less<String> > &menuentry){

  String s=args[0]->soutput(menuentry);
  ifstream f(s.c_str());
  if(f)
    args[1]->output(o,menuentry);
  return o;
}
ostream &ifelsefile_func::output(ostream &o, vector<cat_str *> &args,
			     map<String, String, less<String> > &menuentry){

  String s=args[0]->soutput(menuentry);
  ifstream f(s.c_str());
  if(f)
    args[1]->output(o,menuentry);
  else
    args[2]->output(o,menuentry);
  return o;
}
ostream &catfile_func::output(ostream &o, vector<cat_str *> &args,
			     map<String, String, less<String> > &menuentry){

  String s=args[0]->soutput(menuentry);
  ifstream f(s.c_str());
  char c;

  while(f && f.get(c))
    o<<c;
  return o;
}

ostream &esc_func::output(ostream &o, vector<cat_str *> &args,
			  map<String, String, less<String> > &menuentry){

  return o<<escape_String(args[0]->soutput(menuentry),
			  args[1]->soutput(menuentry));
}
ostream &escwith_func::output(ostream &o, vector<cat_str *> &args,
			      map<String, String, less<String> > &menuentry){

  return o<<escapewith_String(args[0]->soutput(menuentry),
			      args[1]->soutput(menuentry),
			      args[2]->soutput(menuentry));
}
ostream &escfirst_func::output(ostream &o, vector<cat_str *> &args,
			      map<String, String, less<String> > &menuentry){

  String s=args[0]->soutput(menuentry);
  String esc=args[1]->soutput(menuentry);
  String t;
  int  i;

  for(i=0;(unsigned int)i!=s.length();i++){
    if(esc.length() && esc.contains(s[i])){
      t=s.substr(0,i);
      t+=args[2]->soutput(menuentry);
      t+=s.substr(s.begin()+i,s.end());
      break;
    }
    t+=s[i];
  }
  return o<<t;
}
ostream &tolower_func::output(ostream &o, vector<cat_str *> &args,
			      map<String, String, less<String> > &menuentry){

  return o<<tolower_String(args[0]->soutput(menuentry));
}
ostream &toupper_func::output(ostream &o, vector<cat_str *> &args,
				  map<String, String, less<String> > &menuentry){

  return o<<toupper_String(args[0]->soutput(menuentry));
}
ostream &replacewith_func::output(ostream &o, vector<cat_str *> &args,
				  map<String, String, less<String> > &menuentry){

  return o<<replacewith_String(args[0]->soutput(menuentry),
			       args[1]->soutput(menuentry),
			       args[2]->soutput(menuentry));
}
ostream &nstring_func::output(ostream &o, vector<cat_str *> &args,
			    map<String, String, less<String> > &menuentry){
  int count= Stringtoi(args[0]->soutput(menuentry));
  int i;

  for(i=0; i<count; i++)
    o<<args[1]->soutput(menuentry);

  return o;
}
ostream &cppesc_func::output(ostream &o, vector<cat_str *> &args,
			      map<String, String, less<String> > &menuentry){

  return o<<cppesc_String(args[0]->soutput(menuentry));
}

ostream &add_func::output(ostream &o, vector<cat_str *> &args,
			  map<String, String, less<String> > &menuentry){

  int x=Stringtoi(args[0]->soutput(menuentry));
  int y=Stringtoi(args[1]->soutput(menuentry));

  return o<<itoString(x+y);
}
ostream &sub_func::output(ostream &o, vector<cat_str *> &args,
			  map<String, String, less<String> > &menuentry){

  int x=Stringtoi(args[0]->soutput(menuentry));
  int y=Stringtoi(args[1]->soutput(menuentry));

  return o<<itoString(x-y);
}
ostream &mult_func::output(ostream &o, vector<cat_str *> &args,
			  map<String, String, less<String> > &menuentry){

  int x=Stringtoi(args[0]->soutput(menuentry));
  int y=Stringtoi(args[1]->soutput(menuentry));

  return o<<itoString(x*y);
}
ostream &div_func::output(ostream &o, vector<cat_str *> &args,
			  map<String, String, less<String> > &menuentry){

  int x=Stringtoi(args[0]->soutput(menuentry));
  int y=Stringtoi(args[1]->soutput(menuentry));
  
  if(y)
    return o<<itoString(x/y);
  else
    return o<<"0";
}

ostream &ifelse_func::output(ostream &o, vector<cat_str *> &args,
			     map<String, String, less<String> > &menuentry){

  String s=args[0]->soutput(menuentry);

  (!empty_String(s)) ?
    args[1]->output(o,menuentry)
    :
    args[2]->output(o,menuentry);
  return o;
}
ostream &ifeq_func::output(ostream &o, vector<cat_str *> &args,
			   map<String, String, less<String> > &menuentry){

  String s=args[0]->soutput(menuentry);
  String t=args[1]->soutput(menuentry);
  if(s==t)
    args[2]->output(o,menuentry);
  
  return o;
}
ostream &ifneq_func::output(ostream &o, vector<cat_str *> &args,
			   map<String, String, less<String> > &menuentry){

  String s=args[0]->soutput(menuentry);
  String t=args[1]->soutput(menuentry);
  if(!(s==t))
    args[2]->output(o,menuentry);
  
  return o;
}
ostream &ifeqelse_func::output(ostream &o, vector<cat_str *> &args,
			   map<String, String, less<String> > &menuentry){

  String s=args[0]->soutput(menuentry);
  String t=args[1]->soutput(menuentry);
  if(s==t)
    args[2]->output(o,menuentry);
  else
    args[3]->output(o,menuentry);

  return o;
}

ostream &cond_surr_func::output(ostream &o, vector<cat_str *> &args,
				map<String, String, less<String> > &menuentry){

  String s=args[0]->soutput(menuentry);

  if(!empty_String(s)){
    args[1]->output(o,menuentry);
    args[0]->output(o,menuentry);
    args[2]->output(o,menuentry);
  }
  return o;
}

ostream &parent_func::output(ostream &o, vector<cat_str *> &args,
			     map<String, String, less<String> > &menuentry){

  String s=args[0]->soutput(menuentry);

  return o<<String_parent(s);
}

ostream &basename_func::output(ostream &o, vector<cat_str *> &args,
			       map<String, String, less<String> > &menuentry){

  String s=args[0]->soutput(menuentry);
  
  return o<<String_basename(s);
}
ostream &entrycount_func::output(ostream &o, vector<cat_str *> &/*args*/,
				 map<String, String, less<String> > &menuentry){
  return o<<menuentry[PRIVATE_ENTRYCOUNT_VAR];
}
ostream &entryindex_func::output(ostream &o, vector<cat_str *> &/*args*/,
				 map<String, String, less<String> > &menuentry){
  return o<<menuentry[PRIVATE_ENTRYINDEX_VAR];
}
ostream &firstentry_func::output(ostream &o, vector<cat_str *> &args,
				 map<String, String, less<String> > &menuentry){
  int index=Stringtoi(menuentry[PRIVATE_ENTRYINDEX_VAR]);

  if(index == 0)
    args[0]->output(o,menuentry);
  return o;
}

ostream &lastentry_func::output(ostream &o, vector<cat_str *> &args,
				 map<String, String, less<String> > &menuentry){

  int index=Stringtoi(menuentry[PRIVATE_ENTRYINDEX_VAR]);
  int count=Stringtoi(menuentry[PRIVATE_ENTRYCOUNT_VAR]);

  if(index+1 == count)
    args[0]->output(o,menuentry);
  return o;
}


ostream &level_func::output(ostream &o, vector<cat_str *> &/*args*/,
				 map<String, String, less<String> > &menuentry){
  return o<<menuentry[PRIVATE_LEVEL_VAR];
}


ostream &rcfile_func::output(ostream &o, vector<cat_str *> &/*args*/,
				 map<String, String, less<String> > &menuentry){
  return o<<config->rcfile();
}
ostream &examplercfile_func::output(ostream &o, vector<cat_str *> &/*args*/,
				 map<String, String, less<String> > &menuentry){
  return o<<config->examplercfile();
}
ostream &mainmenutitle_func::output(ostream &o, vector<cat_str *> &/*args*/,
				 map<String, String, less<String> > &menuentry){
  return o<<config->mainmenutitle();
}
ostream &rootsection_func::output(ostream &o, vector<cat_str *> &/*args*/,
				 map<String, String, less<String> > &menuentry){
  return o<<config->rootsection();
}
ostream &rootprefix_func::output(ostream &o, vector<cat_str *> &/*args*/,
				 map<String, String, less<String> > &menuentry){
  return o<<config->rootprefix();
}
ostream &userprefix_func::output(ostream &o, vector<cat_str *> &/*args*/,
				 map<String, String, less<String> > &menuentry){
  return o<<config->userprefix();
}
ostream &treewalk_func::output(ostream &o, vector<cat_str *> &/*args*/,
				 map<String, String, less<String> > &menuentry){
  return o<<config->treewalk();
}
ostream &postoutput_func::output(ostream &o, vector<cat_str *> &/*args*/,
				 map<String, String, less<String> > &menuentry){
  return o<<config->postoutput();
}
ostream &preoutput_func::output(ostream &o, vector<cat_str *> &/*args*/,
				map<String, String, less<String> > &menuentry){
  return o<<config->preoutput();
}


/////////////////////////////////////////////////////
//  "defined" function (macro).
//

ostream &func_def::output(ostream &o, vector<cat_str *> &args,
			  map<String, String, less<String> > &menuentry){

  String t;
  unsigned int i;
  map<String, String, less<String> > local_menuentry=menuentry;

  for(i=0; i<args_name.size(); i++)
    local_menuentry[args_name[i]]=args[i]->soutput(menuentry);

  f->output(o,local_menuentry);
  return o;
}
func_def::func_def(parsestream &i){
  char c;

  n=i.get_name();
  i.skip_space();
  i.skip_char('(');
  do{
    i.skip_space();
    c=i.get_char();
    if(c==')')
      break;
    i.put_back(c);
    i.skip_char('$');
    args_name.push_back(i.get_name());
    i.skip_space();
  } while((c=i.get_char())&&(c==','));
  i.put_back(c);
  i.skip_char(')');
  i.skip_space();
  i.skip_char('=');
  f=new cat_str(i);
}

/////////////////////////////////////////////////////
//  Supported stuff
//

supportedinfo::supportedinfo(parsestream &i){
  linenumber=1;
  int prec=0;
  while(1){
    try{
      String name;
      i.skip_space();
      name=i.get_name();
      if(name==String("endsupported"))
	return;
      name=upcase(name);
      if(dodebug)
	cerr<<"SUPPORTED_CONSTRUCT: name="<<name<<endl;
      
      supinf inf;
      inf.c=get_eq_cat_str(i);
      inf.prec=prec++;
      sup[name]=inf;
      if(dodebug)
	sup[name].c->debuginfo(cerr);
      i.skip_line();   //read away the final newline
    }
    catch(endofline d){}
  }
}

void supportedinfo::subst(map<String, String, less<String> > vars){

  map<String, String, less<String> >::iterator i;
  map<String, supinf, less<String> >::iterator j;

  if((i=vars.find(NEEDS_VAR))==vars.end()){
    cerr<<"Undefined "NEEDS_VAR" variable in menuentries"<<endl;
    throw informed_fatal();
  }
  if((j=sup.find(upcase((*i).second)))==sup.end()){
    cerr<<"Unknown "NEEDS_VAR"=\""<<(*i).second<<"\""<<endl;
    throw informed_fatal();
  }
  genoutput((*j).second.c->soutput(vars), vars);
}
int supportedinfo::prec(String &name){
  map<String, supinf, less<String> >::iterator i;
  int p;
  if((i=sup.find(upcase(name)))==sup.end())
    p=MAXINT;
  else
    p=(*i).second.prec;
  //if(dodebug)
  //  cerr<<"PREC: name="<<upcase(name)<<"="<<p<<endl;
  return p;
}

ostream &supportedinfo::debuginfo(ostream &o){
  map<String, supinf, less<String> >::iterator i;
  for(i=sup.begin();i!=sup.end();i++){
    o<<"SUPPORTED:** name="<<(*i).first<<", prec="
	<<(*i).second.prec<<" Def="<<endl;
    (*i).second.c->debuginfo(o);
  }
  return o;
}
/////////////////////////////////////////////////////
//  forcetree stuff
//

void read_forcetree(parsestream &i){
  Regex r("[a-zA-Z/-_ ]");

  while(1){
    try{
      String name;
      StrVec v;
      menuentry *m;

      i.skip_space();
      name=i.get_name(r);
      if(!name.size())
	throw ident_expected(&i);
      if(name==String("endforcetree"))
	return;
      
      break_slashes(name,v);
      m=new menuentry;
      m->forced=true;
      m->vars[TITLE_VAR]=v[v.size()-1];
      menu.add_menuentry_ptr(v,m);  
      i.skip_line();
    }
    catch(endofline d){}
  }
  
}
/////////////////////////////////////////////////////
//  configinfo
//
configinfo::configinfo(parsestream &i){
  String errmsg("Parse error: String constant expected");
  linenumber=1;
  treew="c(m)";
  sort=prerun=preruntest=postrun=genmenu=
    hkexclude=startmenu=endmenu=submenutitle=NULL;
  mainmt="Debian";
  onlyrunasroot=onlyrunasuser=false;

  hint_optimize=false;
  hint_nentry=6;
  hint_topnentry=5;
  hint_mixedpenalty=15;
  hint_minhintfreq=0.1;
  hint_mlpenalty=2000;
  hint_max_ntry=4;
  hint_max_iter_hint=5;
  hint_debug=false;

  //minhintfreq=0.1;
  //hintdebug=false;
  //n_entry_opt=6;
  //hint_toplevel_nopt=5;
  //hint_mlpenalty=20;
  //keep_sections=true;

  preout="#Automatically generated file. Do not edit (see /usr/doc/menu/html/index.html)\n\n";
  postout="";
  roots="/Debian";
  try{
    while(1){
        String name;
        name=i.get_name();
	if(name==String("supported")){
	  i.skip_line();
	  supported=new supportedinfo(i);
	} else if(name==String("forcetree")){
	  i.skip_line();
	  read_forcetree(i);
	} 
	else if(name==String("function"))
	  store_func(new func_def(i));
	else if(name==String("startmenu"))
	  startmenu=get_eq_cat_str(i);
	else if(name==String("endmenu"))
	  endmenu=get_eq_cat_str(i);
	else if(name==String("submenutitle"))
	  submenutitle=get_eq_cat_str(i);
	else if(name==String("hotkeyexclude"))
	  hkexclude=get_eq_cat_str(i);
	else if(name==String("genmenu"))
	  genmenu=get_eq_cat_str(i);	
	else if(name==String("postrun"))
	  postrun=get_eq_cat_str(i);	
	else if(name==String("prerun"))
	  prerun=get_eq_cat_str(i);	
	else if(name==String("preruntest"))
	  preruntest=get_eq_cat_str(i);	
	else if(name==String("onlyrunasroot"))
	  onlyrunasroot=i.get_eq_boolean();	
	else if(name==String("onlyrunasuser"))
	  onlyrunasuser=i.get_eq_boolean();	
	else if(name==String("sort"))
	  sort=get_eq_cat_str(i);

	else if(name==String("compat")){
	  compt=i.get_eq_Stringconst();
	  if(compt==String("menu-1"))
	    i.seteolmode(parsestream::eol_newline);
	  else if(compt==String("menu-2"))
	    i.seteolmode(parsestream::eol_semicolon);
	  else 
	    throw unknown_compat(&i, compt);
	}
	else if(name==String("rcfile"))
	  rcf=i.get_eq_Stringconst();
	else if(name==String("examplercfile"))
	  exrcf=i.get_eq_Stringconst();
	else if(name==String("mainmenutitle"))
	  mainmt=i.get_eq_Stringconst();
	else if(name==String("rootsection"))
	  roots=i.get_eq_Stringconst();
	else if(name==String("rootprefix"))
	  rootpref=i.get_eq_Stringconst();
	else if(name==String("userprefix"))
	  userpref=i.get_eq_Stringconst();
	else if(name==String("treewalk"))
	  treew=i.get_eq_Stringconst();
	else if(name==String("postoutput"))
	  postout=i.get_eq_Stringconst();
	else if(name==String("preoutput"))
	  preout=i.get_eq_Stringconst();
	else if(name==String("command")){
	  system((i.get_eq_Stringconst()).c_str());
	  exit(0);
	}
	else if(name==String("hotkeycase")){
	  String s=i.get_eq_Stringconst();
	  if(s==String("sensitive"))
	    hotkeycase=1;
	  else if (s==String("insensitive"))
	    hotkeycase=0;
	  else {
	    cerr<<"install-menus hotkeycase can only be {,in}sensitive"<<endl;
	    throw informed_fatal();
	  }
	} 
	else if(name==String("hint_optimize"))
	  hint_optimize=i.get_eq_boolean();
	else if(name==String("hint_nentry"))
	  hint_nentry=i.get_eq_double();
	else if(name==String("hint_topnentry"))
	  hint_topnentry=i.get_eq_integer();
	else if(name==String("hint_mixedpenalty"))
	  hint_mixedpenalty=i.get_eq_double();
	else if(name==String("hint_minhintfreq"))
	  hint_minhintfreq=i.get_eq_double();
	else if(name==String("hint_mlpenalty"))
	  hint_mlpenalty=i.get_eq_double();
	else if(name==String("hint_max_ntry")){
	  hint_max_ntry=i.get_eq_integer();
	  if(hint_max_ntry < 1)
	    hint_max_ntry=1;
	}
	else if(name==String("hint_max_iter_hint"))
	  hint_max_iter_hint=i.get_eq_integer();
	else if(name==String("hint_debug"))
	  hint_debug=i.get_eq_boolean();
	else
	  throw unknown_ident(&i);
      i.skip_line();//read away final newline
    }
  }
  catch(endoffile){}
  
  check_config();
}
void configinfo::check_config(){
  if(!(genmenu && startmenu && endmenu)){
    cerr<<"At least one of genmenu, startmenu, endmenu"<<endl
	<<"is undefined in the config file. All of these have to be "<<endl
	<<"defined (although they may be equal to \"\")"<<endl;
    throw informed_fatal();
  }
}
String configinfo::prefix(){
  String s;

  if(getuid())
    return String(getenv("HOME"))+"/"+userprefix();
  else
    return rootpref;
}

ostream &configinfo::debuginfo(ostream &o){
    o<<"Using compatibility with:"<<compt<<endl
     <<"mainmenutitle   : "<<mainmt   <<endl
     <<"rootsection     : "<<roots    <<endl
     <<"rcfile          : "<<rcf      <<endl
     <<"examplercfile   : "<<exrcf    <<endl
     <<"root-prefix     : "<<rootpref <<endl
     <<"user-prefix     : "<<userpref <<endl
     <<"startmenu       : "<<endl;
    if(startmenu)
      startmenu->debuginfo(o);
    o<<"endmenu         : "<<endl;
    if(endmenu)
      endmenu->debuginfo(o);
    o<<"genmenu         : "<<endl;
    if(genmenu)
      genmenu->debuginfo(o);
    o<<"submenutitle    : "<<endl;
    if(submenutitle)
      submenutitle->debuginfo(o);
  o<<"mainmenutitle   : "<<mainmt   <<endl
   <<"treewalk        : "<<treew    <<endl;
  return o;
}

/////////////////////////////////////////////////////
//  Misc
//

map <String, String, less<String> > read_vars(parsestream &i){
  map <String, String, less<String> > m;
  try{
    String name, val;
    while(1){
      name=i.get_name();
      val=i.get_eq_Stringconst();
      m[name]=val;
    } 
  }
  catch(endofline p){};
  return m;
}

void check_vars(parsestream &i,
		map <String, String, less<String> > &m){
  vector <String> need;
  map <String, String, less<String> >::iterator j;
  unsigned int k;

  need.push_back(SECTION_VAR);
  need.push_back(TITLE_VAR);
  need.push_back(NEEDS_VAR);

  for(k=0;k<need.size();k++){
    j=m.find(need[k]);
    if((j==m.end())||((*j).second==String("")))
      throw missing_tag(&i,need[k]);
  }
}
void read_input(parsestream &i){
  String s;
  try{
    while(1){
      map <String, String, less<String> > m;
      StrVec sec_vec;
      
      m=read_vars(i);

      // check presence of section,title,needs vars. Later we will blindly
      // assume they are defined.
      check_vars(i,m);

      break_slashes(m[SECTION_VAR],sec_vec);
      //if(m.find(SORT_VAR)!=m.end())
      //	sec_vec.push_back(m[SORT_VAR]+":"+m[TITLE_VAR]);
      //else
      sec_vec.push_back(m[TITLE_VAR]);	  
      if(supported->prec(m[NEEDS_VAR])!=MAXINT)
	menu.add_entry(sec_vec,m);
      i.skip_line();   //read away the final newline
    }
  }
  catch(endoffile p){}
}


void includemenus(String o, String i,
                  String rep, String m){
  //copy filename i to filename o, replacing the line
  //rep with menu-file m
  char buf[MAX_BUF];
  char c;
  bool changed=false;
  ifstream fi(i.c_str());
  ifstream fm(m.c_str());
  
  if(!fi){
    cerr<<"Cannot open file "<<i<<endl; throw informed_fatal();}
  if(!fm){
    cerr<<"Cannot open file "<<m<<endl; throw informed_fatal();}

  ofstream fo(o.c_str());

  if(!fo){
    cerr<<"Cannot open file "<<o<<endl; throw informed_fatal();}

  {
    while(fi.get(buf,sizeof(buf))){
      if(String(buf).contains(rep,0)){
	while(fm.get(buf,sizeof(buf))){
	  fo<<buf<<endl;
	  fm.get(c);
	}
	changed=true;
      }
      else
	fo<<buf<<endl;
      fi.get(c);
    }
  }
  if(!changed)
    cerr<<"Warning: String \""<<rep
	<<"\" didn't occur in example file "<<i<<endl;
}

int main(int argc, char **argv){
  int c, option_index;
  char script[MAX_BUF];

  String menudefname;
  
  try{
    if(argv[1])
      strcpy(script, argv[1]);
    else{
      cerr<<"install-menu: First parameter must be name of script"<<endl;
      throw informed_fatal();
    }

    while(1){
      c = getopt_long (argc, argv, "fdvh", long_options, &option_index);
      if(c==-1) 
	break;
      switch(c){
      case '?': 
	cerr<<"Try  --help for more information.\n"<<endl;
	throw informed_fatal();
      case 'd': dodebug=1; break;
      case 'v': verbose=1;break;
      case 'h': usage(); break;
      case 'f':break;
      }
    } while (c!=-1);

    add_functions();
    parsestream ps(script);
    if(!ps.good()){
      cerr<<"Cannot open script "<<script<< " for reading"<<endl;
      throw informed_fatal();
    }
    config=new configinfo(ps);
    if(dodebug){
      config->debuginfo(cerr);
      supported->debuginfo(cerr);
    }
    if(config->prerun)
      system((config->prerun->soutput(menu.vars)).c_str());
    if(config->preruntest){
      bool r;
      r=system((config->preruntest->soutput(menu.vars)).c_str());
      if(r)
	return r;
    }
    if(config->onlyrunasroot)
      if(getuid())
	return 0;
    if(config->onlyrunasuser)
      if(!getuid())
	return 0;
    parsestream psscript(cin);
    menu.vars[TITLE_VAR]=config->mainmenutitle();

    read_input(psscript);
    if(config->hint_optimize){ 
      StrVec v;
      menu.process_hints(v);
    };
    menu.postprocess(1,0, config->rootsection());
    menu.output();
    closegenoutput();
    if(config->rcfile().length()&&config->examplercfile().length())
      includemenus(config->prefix()+"/"+config->rcfile(),
		   config->prefix()+"/"+config->examplercfile(),
		   "include-menu-defs",
		   config->prefix()+"/"+config->genmenu->soutput(menu.vars));
    if(config->postrun)
      system((config->postrun->soutput(menu.vars)).c_str());
    exit(0);
  }
  //yes, I know this _should_ be handled in one
  //"derived" exception handler, but gcc doesn't like those
  // (I'm getting internal compiler errors now quite often
  // already, and I remember having them even more frequenly
  // with the -frtty stuff -- _I_WANT_gcc-2.8_!).
  catch(endoffile p){        p.report(cerr); }
  catch(endofline p){        p.report(cerr); }
  catch(char_expected p){    p.report(cerr); }
  catch(char_unexpected p){  p.report(cerr); }
  catch(unexpectedtrailing p){p.report(cerr);}
  catch(boolean_expected p){ p.report(cerr); }
  catch(ident_expected p){   p.report(cerr); }
  catch(narg_mismatch p){    p.report(cerr); }
  catch(unknown_function p){ p.report(cerr); }
  catch(unknown_ident p){    p.report(cerr); }
  catch(ferror_open p){      p.report(cerr); }
  catch(missing_tag p){      p.report(cerr); }
  catch(unknown_compat p){   p.report(cerr); }

  catch(dir_createerror d){
    cerr<<": Cannot create directory "<<d.name<<endl;
  }
  catch(informed_fatal){};

  cerr<<script<<": Aborting"<<endl;

  exit(1);
}