home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 15 / AACD15.ISO / AACD / Programming / Python2 / Python20_source / Amiga_Misc / ParseSetup.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-11-10  |  5.3 KB  |  302 lines

  1. /******************************************************************
  2.  
  3.     Processor for Modules/Setup.in & Modules/Config.c.in,
  4.     to generate appropriate Modules/Config.c file.
  5.     Part of AmigaPython source distribution.
  6.  
  7.     Sorry but this program cannot be written in Python!
  8.     (because people building Python need this program,
  9.     and don't yet have a Python executable ofcourse).
  10.  
  11.  
  12.     Written June 1998 by Irmen de Jong. (irmen@bigfoot.com)
  13.  
  14. ******************************************************************/
  15.  
  16. #include <stdlib.h>
  17. #include <stdio.h>
  18. #include <string.h>
  19.  
  20. #define SETUPFILENAME "Setup"
  21. #define CONFIGFILENAME_IN "config.c.in"
  22. #define CONFIGFILENAME_OUT "config.c"
  23.  
  24. typedef enum { L_SKIP, L_MODULE, L_DEFINE, L_OPTION } lineType;
  25.  
  26. struct Node
  27. {
  28.     char *string;
  29.     int line;
  30.     struct Node *next;
  31. };
  32.  
  33. static struct Node *moduleList = NULL;
  34. static struct Node *moduleTail = NULL;
  35. static struct Node *defineList = NULL;
  36. static struct Node *defineTail = NULL;
  37.  
  38. static struct Node *protoList = NULL;
  39. static struct Node *protoTail = NULL;
  40. static struct Node *configentryList = NULL;
  41. static struct Node *configentryTail = NULL;
  42.  
  43. static int lineNumber;
  44.  
  45. static char *setup_filename=SETUPFILENAME;
  46. static char *configin_filename=CONFIGFILENAME_IN;
  47. static char *configout_filename=CONFIGFILENAME_OUT;
  48.  
  49.  
  50. /// Utility functions
  51. static void addTail(struct Node **listhead, struct Node **listtail, struct Node *node)
  52. {
  53.     node->next = NULL;
  54.     if(*listhead)
  55.     {
  56.         (*listtail)->next=node;
  57.     }
  58.     else
  59.     {
  60.         (*listhead)=node;
  61.     }
  62.     (*listtail)=node;
  63. }
  64.  
  65. static void addStrToList(struct Node **listHead, struct Node **listTail, const char *string)
  66. {
  67.     struct Node *node;
  68.  
  69.     if(node=malloc(sizeof(struct Node)))
  70.     {
  71.         if(node->string = strdup(string))
  72.         {
  73.             node->line = lineNumber;
  74.             addTail(listHead,listTail,node);
  75.             return;
  76.         }
  77.         free(node);
  78.     }
  79.  
  80.     puts("NO MEM");
  81. }
  82.  
  83.  
  84. static lineType classifyLine(char *string)
  85. {
  86.     switch (string[0])
  87.     {
  88.         case '#':
  89.         case '\0':
  90.         case '\n':
  91.                 return L_SKIP;
  92.         case '*':
  93.                 return L_OPTION;
  94.         default:
  95.             {
  96.                 if(strchr(string,'='))
  97.                     return L_DEFINE;
  98.                 else
  99.                     return L_MODULE;
  100.             }
  101.     }
  102. }
  103.  
  104. static void stripNL(char *string)
  105. {
  106.     int l;
  107.  
  108.     l=strlen(string)-1;
  109.     if(l>=0)
  110.     {
  111.         if(string[l]=='\n')
  112.             string[l]='\0';
  113.     }
  114. }
  115.  
  116. static void stripComment(char *moduleline)
  117. {
  118.     char *pos;
  119.  
  120.     pos=strchr(moduleline,'#');
  121.     if(pos)
  122.     {
  123.         // strip comment and preceding tabs/spaces
  124.         *pos=' ';
  125.         while(pos>=moduleline && *pos==' ' || *pos=='\t')
  126.         {
  127.             *pos='\0';
  128.             pos--;
  129.         }
  130.     }
  131. }
  132. ///
  133.  
  134. /// Process functions
  135. static int processModules(void)
  136. {
  137.     struct Node *node;
  138.     char *moduleName;
  139.     char moduleSources[BUFSIZ];
  140.     char buf[BUFSIZ];
  141.  
  142.     moduleSources[0]='\0';
  143.     node=moduleList;
  144.     while(node)
  145.     {
  146.         char *ptr;
  147.         moduleSources[0]='\0';
  148.         ptr = strtok(node->string," \t");
  149.         if(ptr)
  150.         {
  151.             moduleName=ptr;
  152.             ptr=strtok(NULL," \t");
  153.             while(ptr)
  154.             {
  155.                 int i;
  156.                 i=strlen(ptr);
  157.                 // if it is a .c file, add it to the sources string
  158.                 // otherwise, break the loop
  159.                 if(i>2 && (ptr[i-2]=='.' && (ptr[i-1]=='c' || ptr[i-1]=='C')))
  160.                 {
  161.                     strcat(moduleSources," ");
  162.                     strcat(moduleSources,ptr);
  163.                     ptr=strtok(NULL," \t");
  164.                 }
  165.                 else break;
  166.             }
  167.  
  168.             // generate prototype
  169.             sprintf(buf,"void init%s(void);\n",moduleName);
  170.             addStrToList(&protoList, &protoTail, buf);
  171.             // generate configentry
  172.             sprintf(buf,"\t{ \"%s\", init%s },\n", moduleName, moduleName);
  173.             addStrToList(&configentryList, &configentryTail, buf);
  174.         }
  175.  
  176.         node=node->next;
  177.     }
  178.  
  179.     // Read config and add lines
  180.  
  181.     // open CONFIGFILENAME_IN as source
  182.     // open CONFIGFILENAME_OUT as destination
  183.     // copy lines until MARKER 1
  184.     // write all protos:
  185. /*
  186.     node=protoList;
  187.     while(node)
  188.     {
  189.         fprintf(outfile,node->string);
  190.         node=node->next;
  191.     }
  192. */
  193.     // copy lines until MARKER 2
  194.     // write all configentries:
  195. /*
  196.     node=configentryList;
  197.     while(node)
  198.     {
  199.         fprintf(outfile,node->string);
  200.         node=node->next;
  201.     }
  202. */
  203.     // copy remaining lines
  204.  
  205.     return 1;
  206. }
  207.  
  208. static int processDefines(void)
  209. {
  210.     struct Node *node;
  211.  
  212.     node=defineList;
  213.     while(node)
  214.     {
  215.         printf("%3d:DEFINE: %s\n",node->line,node->string);
  216.         node=node->next;
  217.     }
  218.  
  219.     return 1;
  220. }
  221. ///
  222.  
  223. /// Main
  224. int main(int argc, char **argv)
  225. {
  226.     FILE *fh;
  227.     int RC=10;
  228.     char readbuf[BUFSIZ];
  229.  
  230.     argc--; argv++;
  231.     if(argc>0)
  232.     {
  233.         setup_filename = *argv;
  234.         argc--; argv++;
  235.     }
  236.     if(argc>0)
  237.     {
  238.         configin_filename = *argv;
  239.         argc--; argv++;
  240.     }
  241.     if(argc>0)
  242.     {
  243.         configout_filename = *argv;
  244.         argc--; argv++;
  245.     }
  246.  
  247.     printf("Files: Setup=%s, Config in=%s, Config out=%s\n",
  248.         setup_filename,configin_filename,configout_filename);
  249.  
  250.     if(fh=fopen(setup_filename,"r"))
  251.     {
  252.         int skip=0;
  253.  
  254.         lineNumber=1;
  255.  
  256.         while(fgets(readbuf,BUFSIZ,fh)!=NULL)
  257.         {
  258.             stripNL(readbuf);
  259.             switch (classifyLine(readbuf))
  260.             {
  261.             case L_SKIP:
  262.                 // skip this line
  263.                 break;
  264.             case L_MODULE:
  265.                 if(!skip)
  266.                 {
  267.                     stripComment(readbuf);
  268.                     addStrToList(&moduleList, &moduleTail, readbuf);
  269.                 }
  270.                 break;
  271.             case L_DEFINE:
  272.                 if(!skip)
  273.                 {
  274.                     addStrToList(&defineList, &defineTail, readbuf);
  275.                 }
  276.                 break;
  277.             case L_OPTION:
  278.                 if(strcmp(readbuf,"*static*")==NULL) skip=0;
  279.                 if(strcmp(readbuf,"*doconfig*")==NULL) skip=0;
  280.                 if(strcmp(readbuf,"*shared*")==NULL) skip=1;
  281.                 if(strcmp(readbuf,"*noconfig*")==NULL) skip=1;
  282.                 break;
  283.             }
  284.  
  285.             lineNumber++;
  286.         }
  287.  
  288.         fclose(fh);
  289.  
  290.         if(processDefines() && processModules())
  291.             RC=0;
  292.     }
  293.     else
  294.     {
  295.         printf("Cannot read `%s'\n",setup_filename);
  296.     }
  297.  
  298.     return RC;
  299. }
  300. ///
  301.  
  302.