root/tools/gen_conf_lua.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. read_line
  2. get_str
  3. usage
  4. main

   1 #include <stdlib.h>
   2 #include <stdio.h>
   3 #include <stdint.h>
   4 #include <string.h>
   5 #include <unistd.h>
   6 #include <time.h>
   7 #include <stdarg.h>
   8 
   9 //------------------------------------------------------------------------------------------------------------
  10 
  11 // File parsing
  12 
  13 static int read_line(FILE *f, char *buf)
  14 {
  15     int eof = 0;
  16     int len = 0;
  17     while (1)
  18     {
  19         if (fread(buf,1,1,f) != 1) { eof = 1; break; }
  20         if ((*buf == 0x0A) || (*buf == 0x0D)) break;
  21         len++; 
  22         buf++;
  23     }
  24     *buf = 0;
  25     return (eof == 0) || (len > 0);
  26 }
  27 
  28 static char* get_str(char *s, char *d)
  29 {
  30     while ((*s == ' ') || (*s == '\t') || (*s == ',')) s++;
  31     while (*s && (*s != ' ') && (*s != '\t') && (*s != ',') && (*s != '=') && (*s != ')'))
  32     {
  33         *d++ = *s++;
  34     }
  35         while (*s && (*s != ',') && (*s != '=') && (*s != ')'))
  36         {
  37                 if (*s == '+')
  38                 {
  39                         *d++ = *s++;
  40                         while ((*s == ' ') || (*s == '\t') || (*s == ',')) s++;
  41                         while (*s && (*s != ' ') && (*s != '\t') && (*s != ',') && (*s != '=') && (*s != ')'))
  42                         {
  43                                 *d++ = *s++;
  44                         }
  45                 }
  46                 else s++;
  47         }
  48     *d = 0;
  49     return s;
  50 }
  51 
  52 //------------------------------------------------------------------------------------------------------------
  53 
  54 void usage(char *err)
  55 {
  56     fprintf(stderr,"gen_conf_lua conf.c conf_name start_value - Error = %s\n",err);
  57     exit(1);
  58 }
  59 
  60 int main(int argc, char **argv)
  61 {
  62     if (argc != 4)
  63         usage("args");
  64 
  65     int start = strtoul(argv[3], NULL, 0);
  66 
  67     FILE *f = fopen(argv[1], "rb");
  68 
  69     if (f == NULL) usage("failed to open conf.c file");
  70 
  71     char conf_name[200];
  72     sprintf(conf_name, " %s[]", argv[2]);
  73 
  74     char line[1000];
  75     char nm[200];
  76     char val[200];
  77     int lastid = 0;
  78     int firstid = 0;
  79     char *s;
  80     int found = 0;
  81 
  82     printf("--[[\nGENERATED %s TABLE\n--]]\nreturn {\n _config_id=%d,\n", argv[2], start);
  83 
  84     while (read_line(f,line))
  85     {
  86         if (found)
  87         {
  88             s = strstr(line, "};");
  89             if (s != 0) break;
  90 
  91             int off = 10;
  92             s = strstr(line, "CONF_INFO(");
  93             if (s == 0) { off = 11; s = strstr(line, "CONF_INFO2("); }
  94             if (s == 0) { off = 11; s = strstr(line, "CONF_INFOP("); }
  95             if (s != 0)
  96             {
  97                 char *c = strstr(line, "//");
  98                 if ((c == 0) || (c > s))
  99                 {
 100                     s = get_str(s+off,val);
 101                     get_str(s,nm);
 102                     if (strcmp(nm+5,"script_allow_lua_native_calls") != 0)  // Exclude enabling native calls
 103                     {
 104                         lastid = atoi(val) + start;
 105                         if (firstid == 0)
 106                         {
 107                             firstid = lastid;
 108                             printf(" _first_entry=%d,\n", firstid);
 109                         }
 110                         printf(" %s=%d,\n", nm+5, lastid);
 111                     }
 112                 }
 113             }
 114         }
 115         else
 116         {
 117             s = strstr(line, conf_name);
 118             if (s != 0) found = 1;
 119         }
 120     }
 121     fclose(f);
 122     printf(" _last_entry=%d\n}\n", lastid);
 123 
 124     return 0;
 125 }
 126 
 127 //------------------------------------------------------------------------------------------------------------

/* [<][>][^][v][top][bottom][index][help] */