Subversion Repositories shark

Rev

Blame | Last modification | View Log | RSS feed


#include <stdio.h>
#include <string.h>
#include <unistd.h>

#include "hconf.h"

static void visitcase(struct menuentry *menuent);
static void visitmenu(struct menuentry *menuent);

static void (*writevar)(struct variable *);

static void visitcase(struct menuentry *menuent)
{
  struct _case *list;
  list=menuent->x._case.list;
  while (list!=NULL) {
    if (!strcmp(list->value,menuent->x._case.var->value)) {      
      visitmenu(list->list);
      break;
    }
    list=list->next;
  }
  return;
}

static void visitmenu(struct menuentry *menuent)
{
  struct menuentry *ptr;

  ptr=menuent;
  while (ptr!=NULL) {
    switch(ptr->type) {

    case CASEENTRY: visitcase(ptr);break;
    case SUBMENUENTRY: visitmenu(ptr->x.submenu.menu->entries); break;

    case BOOLENTRY: writevar(ptr->x.bool.var); break;
    case TRISTATEENTRY: writevar(ptr->x.tristate.var); break;
    case RANGEENTRY: writevar(ptr->x.range.var); break;
    case CHOICEENTRY: writevar(ptr->x.choice.act->var); break;
    }

    ptr=ptr->next;
  }

  return;
}

/*
 *
 *
 *
 */


static FILE *fout;
static FILE *ftable[MAXFILETABLE];

static void write_hvar(struct variable *var)
{
  FILE *myfout;
  int t;

  if (var->fileindex!=NOFILEINDEX) myfout=ftable[var->fileindex];
  else myfout=fout;

  t=var->type;
  var->type=PRINTEDVAR;  

  switch (t) {
  case TRISTATEVAR:
    if (*var->value=='m')
      { fprintf(myfout,"#define %s 0\n",var->name); return; }
  case BOOLVAR:
    if (*var->value=='y')
      { fprintf(myfout,"#define %s 1\n",var->name); return; }
    fprintf(myfout,"#undef %s\n",var->name);
    return;
  case CHOICEVAR:
    fprintf(myfout,"#define %s 1\n",var->name);
    return;
  case RANGEVAR:
    fprintf(myfout,"#define %s %s\n",var->name,var->value);
    return;
  }
  return;
}

#if 0
static void write_makvar(struct variable *var)
{
  return;
}
#endif

/* -- */

char hmessage[]=
"/*\n"
" * This file is generated by a script\n"
" *\n"
" * (run 'make config' to change)\n"
" */\n"
"\n";

static int write_h(char *filename)
{
  int i;
 
  fout=fopen(filename,"wt");
  if (fout==NULL) return -1;
  fprintf(fout,"%s",hmessage);
  for (i=0;i<maxfileindex;i++) {
    ftable[i]=fopen(filetable[i],"wt");
    if (ftable[i]==NULL) return -3;
    fprintf(ftable[i],"%s",hmessage);    
  }

  writevar=write_hvar;
  visitmenu(mainmenu->entries);
 
  fprintf(fout,"\n");
  for (i=0;i<maxfileindex;i++) fprintf(ftable[i],"\n");
   
  for (i=0;i<nextvariable;i++)
    if (vartable[i].type!=PRINTEDVAR)
      fprintf(fout,"/* var %s %s */\n",vartable[i].name,vartable[i].value);
         
  fclose(fout);
  for (i=0;i<maxfileindex;i++) fclose(ftable[i]);
  return 0;
}

static int write_mak(char *filename)
{
  char buffer[256];
  int i;
  off_t pos;

  /* truncate file if necessary... */
  pos=0;
  fout=fopen(filename,"rt");
  if (fout!=NULL) {
    while (fgets(buffer,sizeof(buffer),fout)!=NULL)
      if (!strcmp(buffer,"# by a script (do not edit by hand)\n")) {
        pos=ftell(fout);
        break;
      }
    fclose(fout);
    if (pos) truncate(filename,pos);
  }

  /* open file */
  fout=fopen(filename,"at");
  if (fout==NULL) return -1;

  /* write all vars */
  if (!pos) {
    fprintf(fout,"\n#\n# the following lines are generated\n");
    fprintf(fout,"# by a script (do not edit by hand)\n#\n\n");
  }
  else fprintf(fout,"#\n\n");

  for (i=0;i<nextvariable;i++)
    fprintf(fout,"%s=%s\n",vartable[i].name,vartable[i].value);

  /* close file */
  fclose(fout);
  return 0;
}

/* -- */

int writeconfig(char *srcfilename)
{
  char buffer[256];
  char *ptr;

  ptr=strstr(srcfilename,".in");  
  if (ptr!=NULL) *ptr='\0';
  else {
    ptr=strchr(srcfilename,'.');
    if (ptr!=NULL) *ptr='\0';
  }

  strcpy(buffer,srcfilename);
  strcat(buffer,".h");
  write_h(buffer);

  return 0;
}

int writeconfigmak(char *srcfilename)
{
  char buffer[256];
  char *ptr;

  ptr=strstr(srcfilename,".in");  
  if (ptr!=NULL) *ptr='\0';
  else {
    ptr=strchr(srcfilename,'.');
    if (ptr!=NULL) *ptr='\0';
  }

  strcpy(buffer,srcfilename);
  strcat(buffer,".mak");
  write_mak(buffer);

  return 0;
}