Subversion Repositories shark

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
2 pj 1
 
2
#include <stdio.h>
3
#include <string.h>
4
#include <unistd.h>
5
 
6
#include "hconf.h"
7
 
8
static void visitcase(struct menuentry *menuent);
9
static void visitmenu(struct menuentry *menuent);
10
 
11
static void (*writevar)(struct variable *);
12
 
13
static void visitcase(struct menuentry *menuent)
14
{
15
  struct _case *list;
16
  list=menuent->x._case.list;
17
  while (list!=NULL) {
18
    if (!strcmp(list->value,menuent->x._case.var->value)) {      
19
      visitmenu(list->list);
20
      break;
21
    }
22
    list=list->next;
23
  }
24
  return;
25
}
26
 
27
static void visitmenu(struct menuentry *menuent)
28
{
29
  struct menuentry *ptr;
30
 
31
  ptr=menuent;
32
  while (ptr!=NULL) {
33
    switch(ptr->type) {
34
 
35
    case CASEENTRY: visitcase(ptr);break;
36
    case SUBMENUENTRY: visitmenu(ptr->x.submenu.menu->entries); break;
37
 
38
    case BOOLENTRY: writevar(ptr->x.bool.var); break;
39
    case TRISTATEENTRY: writevar(ptr->x.tristate.var); break;
40
    case RANGEENTRY: writevar(ptr->x.range.var); break;
41
    case CHOICEENTRY: writevar(ptr->x.choice.act->var); break;
42
    }
43
 
44
    ptr=ptr->next;
45
  }
46
 
47
  return;
48
}
49
 
50
/*
51
 *
52
 *
53
 *
54
 */
55
 
56
static FILE *fout;
57
static FILE *ftable[MAXFILETABLE];
58
 
59
static void write_hvar(struct variable *var)
60
{
61
  FILE *myfout;
62
  int t;
63
 
64
  if (var->fileindex!=NOFILEINDEX) myfout=ftable[var->fileindex];
65
  else myfout=fout;
66
 
67
  t=var->type;
68
  var->type=PRINTEDVAR;  
69
 
70
  switch (t) {
71
  case TRISTATEVAR:
72
    if (*var->value=='m')
73
      { fprintf(myfout,"#define %s 0\n",var->name); return; }
74
  case BOOLVAR:
75
    if (*var->value=='y')
76
      { fprintf(myfout,"#define %s 1\n",var->name); return; }
77
    fprintf(myfout,"#undef %s\n",var->name);
78
    return;
79
  case CHOICEVAR:
80
    fprintf(myfout,"#define %s 1\n",var->name);
81
    return;
82
  case RANGEVAR:
83
    fprintf(myfout,"#define %s %s\n",var->name,var->value);
84
    return;
85
  }
86
  return;
87
}
88
 
89
#if 0
90
static void write_makvar(struct variable *var)
91
{
92
  return;
93
}
94
#endif
95
 
96
/* -- */
97
 
98
char hmessage[]=
99
"/*\n"
100
" * This file is generated by a script\n"
101
" *\n"
102
" * (run 'make config' to change)\n"
103
" */\n"
104
"\n";
105
 
106
static int write_h(char *filename)
107
{
108
  int i;
109
 
110
  fout=fopen(filename,"wt");
111
  if (fout==NULL) return -1;
112
  fprintf(fout,"%s",hmessage);
113
  for (i=0;i<maxfileindex;i++) {
114
    ftable[i]=fopen(filetable[i],"wt");
115
    if (ftable[i]==NULL) return -3;
116
    fprintf(ftable[i],"%s",hmessage);    
117
  }
118
 
119
  writevar=write_hvar;
120
  visitmenu(mainmenu->entries);
121
 
122
  fprintf(fout,"\n");
123
  for (i=0;i<maxfileindex;i++) fprintf(ftable[i],"\n");
124
 
125
  for (i=0;i<nextvariable;i++)
126
    if (vartable[i].type!=PRINTEDVAR)
127
      fprintf(fout,"/* var %s %s */\n",vartable[i].name,vartable[i].value);
128
 
129
  fclose(fout);
130
  for (i=0;i<maxfileindex;i++) fclose(ftable[i]);
131
  return 0;
132
}
133
 
134
static int write_mak(char *filename)
135
{
136
  char buffer[256];
137
  int i;
138
  off_t pos;
139
 
140
  /* truncate file if necessary... */
141
  pos=0;
142
  fout=fopen(filename,"rt");
143
  if (fout!=NULL) {
144
    while (fgets(buffer,sizeof(buffer),fout)!=NULL)
145
      if (!strcmp(buffer,"# by a script (do not edit by hand)\n")) {
146
        pos=ftell(fout);
147
        break;
148
      }
149
    fclose(fout);
150
    if (pos) truncate(filename,pos);
151
  }
152
 
153
  /* open file */
154
  fout=fopen(filename,"at");
155
  if (fout==NULL) return -1;
156
 
157
  /* write all vars */
158
  if (!pos) {
159
    fprintf(fout,"\n#\n# the following lines are generated\n");
160
    fprintf(fout,"# by a script (do not edit by hand)\n#\n\n");
161
  }
162
  else fprintf(fout,"#\n\n");
163
 
164
  for (i=0;i<nextvariable;i++)
165
    fprintf(fout,"%s=%s\n",vartable[i].name,vartable[i].value);
166
 
167
  /* close file */
168
  fclose(fout);
169
  return 0;
170
}
171
 
172
/* -- */
173
 
174
int writeconfig(char *srcfilename)
175
{
176
  char buffer[256];
177
  char *ptr;
178
 
179
  ptr=strstr(srcfilename,".in");  
180
  if (ptr!=NULL) *ptr='\0';
181
  else {
182
    ptr=strchr(srcfilename,'.');
183
    if (ptr!=NULL) *ptr='\0';
184
  }
185
 
186
  strcpy(buffer,srcfilename);
187
  strcat(buffer,".h");
188
  write_h(buffer);
189
 
190
  return 0;
191
}
192
 
193
int writeconfigmak(char *srcfilename)
194
{
195
  char buffer[256];
196
  char *ptr;
197
 
198
  ptr=strstr(srcfilename,".in");  
199
  if (ptr!=NULL) *ptr='\0';
200
  else {
201
    ptr=strchr(srcfilename,'.');
202
    if (ptr!=NULL) *ptr='\0';
203
  }
204
 
205
  strcpy(buffer,srcfilename);
206
  strcat(buffer,".mak");
207
  write_mak(buffer);
208
 
209
  return 0;
210
}