root/tools/stubs_load.c

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

DEFINITIONS

This source file includes following definitions.
  1. new_stub_values
  2. read_line
  3. get_str
  4. add_sig
  5. find_sig
  6. find_sig_val
  7. find_sig_val_by_type
  8. find_match
  9. load_funcs
  10. load_stubs_file
  11. load_stubs
  12. load_stubs_min
  13. load_modemap
  14. load_platform
  15. load_propcases
  16. load_a_makefile
  17. load_makefile

   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 #include "stubs_load.h"
  10 
  11 //------------------------------------------------------------------------------------------------------------
  12 
  13 // Functions for loading stubs and other source files, storing and searching for values contained within.
  14 
  15 //------------------------------------------------------------------------------------------------------------
  16 
  17 stub_values* new_stub_values()
  18 {
  19     stub_values *p = malloc(sizeof(stub_values));
  20     p->stubs = 0;
  21     p->stubs_min = 0;
  22     p->modemap = 0;
  23     p->makevals = 0;
  24     p->min_focus_len = 0;
  25     p->max_focus_len = 0;
  26     p->propcases = 0;
  27     p->propset = 0;
  28     return p;
  29 }
  30 
  31 //------------------------------------------------------------------------------------------------------------
  32 
  33 // File parsing
  34 
  35 static int read_line(FILE *f, char *buf)
  36 {
  37     int eof = 0;
  38     int len = 0;
  39     while (1)
  40     {
  41         if (fread(buf,1,1,f) != 1) { eof = 1; break; }
  42         if ((*buf == 0x0A) || (*buf == 0x0D)) break;
  43         len++; 
  44         buf++;
  45     }
  46     *buf = 0;
  47     return (eof == 0) || (len > 0);
  48 }
  49 
  50 static char* get_str(char *s, char *d)
  51 {
  52     while ((*s == ' ') || (*s == '\t') || (*s == ',')) s++;
  53     while (*s && (*s != ' ') && (*s != '\t') && (*s != ',') && (*s != '=') && (*s != ')'))
  54     {
  55         *d++ = *s++;
  56     }
  57         while (*s && (*s != ',') && (*s != '=') && (*s != ')'))
  58         {
  59                 if (*s == '+')
  60                 {
  61                         *d++ = *s++;
  62                         while ((*s == ' ') || (*s == '\t') || (*s == ',')) s++;
  63                         while (*s && (*s != ' ') && (*s != '\t') && (*s != ',') && (*s != '=') && (*s != ')'))
  64                         {
  65                                 *d++ = *s++;
  66                         }
  67                 }
  68                 else s++;
  69         }
  70     *d = 0;
  71     return s;
  72 }
  73 
  74 // Add a new value to the list
  75 static osig* add_sig(char *nm, char *val, osig **hdr, int is_comment)
  76 {
  77     osig *p = malloc(sizeof(osig));
  78     strcpy(p->nm, nm);
  79         strcpy(p->sval, val);
  80     p->pct = 0;
  81     p->is_comment = is_comment;
  82     p->type = 0;
  83     p->nxt = *hdr;
  84     *hdr = p;
  85 
  86         uint32_t v = 0, n = 0;
  87         if ((strncmp(val,"0x",2) == 0) || (strncmp(val,"0X",2) == 0))
  88         {
  89                 while (val)
  90                 {
  91                         sscanf(val,"%x",&n);
  92                         v += n;
  93                         val = strchr(val,'+');
  94                         if (val) val++;
  95                 }
  96         }
  97         else
  98         {
  99                 sscanf(val,"%d",&v);
 100         }
 101 
 102         p->val = v;
 103     return p;
 104 }
 105 
 106 //------------------------------------------------------------------------------------------------------------
 107 
 108 // Find an entry by name
 109 osig* find_sig(osig* p, const char *nm)
 110 {
 111     while (p)
 112     {
 113         if (strcmp(p->nm, nm) == 0) return p;
 114         p = p->nxt;
 115     }
 116     return 0;
 117 }
 118 
 119 // Find an entry by value
 120 osig* find_sig_val(osig* p, uint32_t val)
 121 {
 122     while (p)
 123     {
 124         if (p->val == val) return p;
 125         p = p->nxt;
 126     }
 127     return 0;
 128 }
 129 
 130 // Find an entry by value
 131 osig* find_sig_val_by_type(osig* p, uint32_t val, int typ)
 132 {
 133     while (p)
 134     {
 135         if ((p->val == val) && (p->type == typ)) return p;
 136         p = p->nxt;
 137     }
 138     return 0;
 139 }
 140 
 141 // Find a match (in case of multiple entries)
 142 // returns first entry matching name & value, 
 143 // or first entry matching name only (if value does not match),
 144 // or 0 if no name match
 145 osig* find_match(osig *p, const char *nm, uint32_t val)
 146 {
 147     p = find_sig(p, nm);
 148     osig *f = p;
 149     while (p)
 150     {
 151         if (p->val == val)
 152             return p;
 153         p = find_sig(p->nxt, nm);
 154     }
 155     return f;
 156 }
 157 
 158 //------------------------------------------------------------------------------------------------------------
 159 
 160 // Load a specified file.
 161 // Add new entries the list pointer to by 'hdr'
 162 void load_funcs(stub_values *sv, char *name)
 163 {
 164     FILE *f = fopen(name, "rb");
 165 
 166     if (f == NULL) return;
 167 
 168     char line[500];
 169     char nm[100];
 170     char val[100];
 171     char *s;
 172 
 173     while (read_line(f,line))
 174     {
 175         s = get_str(line,val);
 176         get_str(s,nm);
 177         osig *p = add_sig(nm, val, &sv->stubs, 1);
 178         p->type = TYPE_NHSTUB;
 179         continue;
 180     }
 181     fclose(f);
 182 }
 183 
 184 // Load a specified file.
 185 // If 'exclude_comments' is non-zero then commented lines are ignored.
 186 // Add new entries the list pointer to by 'hdr'
 187 static void load_stubs_file(char *name, int exclude_comments, osig **hdr)
 188 {
 189     FILE *f = fopen(name, "rb");
 190 
 191     if (f == NULL) return;
 192 
 193     char line[500];
 194     char nm[100];
 195     char val[100];
 196     char *s;
 197 
 198     while (read_line(f,line))
 199     {
 200         int typ = TYPE_NHSTUB;
 201         int off = 7;
 202         s = strstr(line, "NHSTUB(");
 203         if (s == 0) { off = 8; s = strstr(line, "NHSTUB2("); } // note may want to flag dif from NHSTUB
 204         if (s == 0) { off = 7; s = strstr(line, "IGNORE("); typ = TYPE_IGNORE; }
 205         if (s == 0) { off = 6; s = strstr(line, "NSTUB("); }
 206         if (s == 0) { off = 4; s = strstr(line, "DEF("); typ = TYPE_DEF; }
 207         if (s == 0) { off = 10; s = strstr(line, "DEF_CONST("); typ = TYPE_CONST; }
 208         if (s != 0)
 209         {
 210             char *c = strstr(line, "//");
 211             if ((exclude_comments == 0) || (c == 0) || (c > s))
 212             {
 213                 s = get_str(s+off,nm);
 214                 if (typ != TYPE_IGNORE)
 215                     get_str(s,val);
 216                 else
 217                     val[0] = 0;
 218                 osig *p = add_sig(nm, val, hdr, ((c != 0) && (c <= s)) ? 1 : 0);
 219                 p->type = typ;
 220                 continue;
 221             }
 222         }
 223     }
 224     fclose(f);
 225 }
 226 
 227 // Load a specified stubs file.
 228 void load_stubs(stub_values *sv, char *name, int exclude_comments)
 229 {
 230     load_stubs_file(name, exclude_comments, &sv->stubs);
 231 }
 232 
 233 // Load the 'stubs_min.S' file
 234 void load_stubs_min(stub_values *sv)
 235 {
 236     load_stubs_file("stubs_min.S", 1, &sv->stubs_min);
 237 }
 238 
 239 // Load the MODEMAP from the shooting.c source file
 240 void load_modemap(stub_values *sv)
 241 {
 242     FILE *f = fopen("../../shooting.c", "rb");
 243 
 244     if (f == NULL) return;
 245 
 246     char line[500];
 247     char nm[100];
 248     char val[12];
 249         int found_modemap = 0;
 250     char *s;
 251 
 252     while (read_line(f,line))
 253     {
 254                 if (found_modemap)
 255                 {
 256                         s = strstr(line, "};");
 257                         if (s != 0) return;
 258                         s = strstr(line, "MODE_");
 259                         if (s != 0)
 260                         {
 261                                 char *c = strstr(line, "//");
 262                                 if ((c == 0) || (c > s))
 263                                 {
 264                                         s = get_str(s,nm);
 265                                         get_str(s,val);
 266                                         add_sig(nm, val, &sv->modemap, 0);
 267                                 }
 268                         }
 269                 }
 270                 else
 271                 {
 272                         s = strstr(line, "modemap[");
 273                         if (s != 0) found_modemap = 1;
 274                 }
 275     }
 276 }
 277 
 278 // Load the DNG lens info from the 'platform_camera.h' file.
 279 void load_platform(stub_values *sv)
 280 {
 281     FILE *f = fopen("../../platform_camera.h", "rb");
 282 
 283     if (f == NULL) return;
 284 
 285     char line[500];
 286     char val[12];
 287     char div[12];
 288     int v, d;
 289     char *s;
 290 
 291     while (read_line(f,line))
 292     {
 293                 s = strstr(line, "CAM_DNG_LENS_INFO");
 294                 if (s != 0)
 295                 {
 296                         char *c = strstr(line, "//");
 297                         if ((c == 0) || (c > s))
 298                         {
 299                 s = strstr(line,"{")+1;
 300                                 s = get_str(s,val);
 301                                 s = get_str(s,div);
 302                 v = atoi(val);
 303                 d = atoi(div);
 304                 sv->min_focus_len = (v * 1000) / d;
 305                                 s = get_str(s,val);
 306                                 s = get_str(s,div);
 307                 v = atoi(val);
 308                 d = atoi(div);
 309                 sv->max_focus_len = (v * 1000) / d;
 310                         }
 311                 }
 312         s = strstr(line, "CAM_PROPSET");
 313         if (s != 0)
 314         {
 315             char *c = strstr(line, "//");
 316             if ((c == 0) || (c > s))
 317             {
 318                 s = s + strlen("CAM_PROPSET");
 319                 s = get_str(s,val);
 320                 v = atoi(val);
 321                 sv->propset = v;
 322             }
 323         }
 324     }
 325     fclose(f);
 326 }
 327 
 328 // Load procase values from propcaseN.h.
 329 void load_propcases(stub_values *sv,char *fn)
 330 {
 331     FILE *f = fopen(fn, "rb");
 332 
 333     if (f == NULL) return;
 334 
 335     char line[500];
 336     char *s;
 337 
 338     while (read_line(f,line))
 339     {
 340         s = strstr(line, "#define");
 341         if (s == 0)
 342         {
 343             continue;
 344         }
 345         char *c = strstr(line, "//");
 346         if (c && c < s)
 347         {
 348             continue;
 349         }
 350         char *nm = strtok(s+strlen("#define")," \t");
 351         if(!nm) 
 352         {
 353             continue;
 354         }
 355         if(strncmp(nm,"PROPCASE_",9) != 0)
 356         {
 357             continue;
 358         }
 359         char *val = strtok(NULL," \t");
 360         if(!val) 
 361         {
 362             continue;
 363         }
 364 
 365         add_sig(nm, val, &sv->propcases, 0);
 366     }
 367     fclose(f);
 368 }
 369 
 370 // Load the build values from the makefile.inc source file
 371 void load_a_makefile(stub_values *sv, char *fn)
 372 {
 373     FILE *f = fopen(fn, "rb");
 374 
 375     if (f == NULL) return;
 376 
 377     char line[500];
 378     char nm[100];
 379     char val[100];
 380     char *s;
 381 
 382     while (read_line(f,line))
 383     {
 384                 s = strstr(line, "=");
 385                 if (s != 0)
 386                 {
 387             char *c = strstr(line, "#");
 388                         if ((c == 0) || (c > s))
 389                         {
 390                 if (c) *c = 0;
 391                                 s = get_str(line,nm);
 392                                 get_str(s+1,val);
 393                                 add_sig(nm, val, &sv->makevals, 0);
 394                         }
 395                 }
 396     }
 397     fclose(f);
 398 }
 399 
 400 void load_makefile(stub_values *sv)
 401 {
 402     load_a_makefile(sv, "../../makefile.inc"); // new in CHDK 1.4
 403     load_a_makefile(sv, "makefile.inc");
 404 }
 405 
 406 //------------------------------------------------------------------------------------------------------------

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