home *** CD-ROM | disk | FTP | other *** search
/ PC Plus SuperCD (UK) 1999 May / pcp151c.iso / misc / src / install / pci-probing / mappcinames.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-01-06  |  4.0 KB  |  213 lines

  1. /* ultra stupid program cause I cant right perl/awk/sed/bash */
  2. #include <stdio.h>
  3. #include <strings.h>
  4. #include <unistd.h>
  5. #include <stdlib.h>
  6.  
  7. #define MAX_MAP 1000
  8.  
  9. struct mapping {
  10.     char *name;
  11.     char *module;
  12. };
  13.  
  14. struct mapping map[MAX_MAP];
  15.  
  16. static void trim( char *s ) {
  17.     char *f, *l, *p, *q;
  18.  
  19.     if (!(*s))
  20.     return;
  21.     
  22.     for (f=s; *f && isspace(*f); f++) ;
  23.  
  24.     if (!*f) {
  25.     *s = '\0';
  26.     return;
  27.     }
  28.     
  29.     for (l=f+strlen(f)-1; isspace(*l) ; l--)
  30.     *l = '\0';
  31.     
  32.     q = s, p = f;
  33.     while (*p)
  34.     *q++ = *p++;
  35.     
  36.     *q = '\0';
  37. }
  38.  
  39. void readmap( int *mi, struct mapping *map, FILE *f ) {
  40.     char cur[250];
  41.     char *name, *module, *n, *split, *end;
  42.  
  43.     
  44.     /* read in the wonderous mapping files */
  45.     while (1) {
  46.     end = fgets(cur, 100, f);
  47.     if (!end)
  48.         break;
  49.     
  50.     if (*cur) {
  51.         cur[strlen(cur)-1] = 0; /* blow lf */
  52.         split = strrchr(cur, '|');
  53.         if (!split)
  54.         continue;
  55.  
  56.         module = split+1;
  57.         name   = cur;
  58.         cur[split-cur-1] = 0;
  59.         trim(module);
  60.         trim(name);
  61.         n=module;
  62.         /* look for a space or a "string", whichever is first */
  63.         while (*n) {
  64.         for (split = n; *split &&
  65.              *split != '"' && *split != ' ';split++);
  66.         if (!*split)
  67.             break;
  68.         if (*split == '"') {
  69.             n=split+1;
  70.             for (split++; *split && *split != '"'; split++);
  71.         }
  72.  
  73.         if (!*split)
  74.             break;
  75.         
  76.         *split = 0;
  77.         map[*mi].name   = strdup(name);
  78.         map[*mi].module = strdup(n);
  79.         trim(map[*mi].module);
  80.         (*mi)++;
  81.         n=split+1;
  82.         }
  83.         /* get the last one now */
  84.         if (*n) {
  85.         map[*mi].name   = strdup(name);
  86.         map[*mi].module = strdup(n);
  87.         trim(map[*mi].module);
  88.         (*mi)++;
  89.         }
  90.     }
  91.     }
  92. }
  93.  
  94.  
  95.  
  96. int main() {
  97.  
  98.     FILE *mapscsi, *mapeth, *mapvideo;
  99.     char cur[250], next[250], tmpstr[250];
  100.     int  mi, done, seen, i, len, done2, first;
  101.     char *end;
  102.     char *quote;
  103.     char *strp, *strq;
  104.     char *name, *mapped;
  105.     
  106.     if ((mapscsi=fopen("map-scsi.lst", "r"))==NULL) {
  107.     fprintf(stderr,"Cant find scsi mapping file\n");
  108.     exit(1);
  109.     }
  110.     
  111.     if ((mapeth=fopen("map-eth.lst", "r"))==NULL) {
  112.     fprintf(stderr,"Cant find eth mapping file\n");
  113.     exit(1);
  114.     }
  115.  
  116.     if ((mapvideo=fopen("map-video.lst", "r"))==NULL) {
  117.     fprintf(stderr,"Cant find video mapping file\n");
  118.     exit(1);
  119.     }
  120.  
  121.     mi = 0;
  122.     readmap(&mi, map, mapscsi);
  123.     readmap(&mi, map, mapeth);
  124.     readmap(&mi, map, mapvideo);
  125.     
  126.     done = 0;
  127.     seen = 0;
  128.     end = fgets(cur, 100, stdin);
  129.     if (!end)
  130.     done = 1;
  131.     while (!done) {
  132.     end = fgets(next, 100, stdin);
  133.     if (!end) {
  134.         fputs(cur, stdout);
  135.         break;
  136.     }
  137.  
  138.     /* see if this is start of the pci_module_maps */
  139.     if (!strcmp(cur, "struct pci_module_map scsi_pci_ids[] = {\n")) {
  140.         seen = 1;
  141.         fputs(cur, stdout);
  142.         strcpy(cur,next);
  143.         continue;
  144.     }
  145.  
  146.     /* seen if current line needs any mapping */
  147.     /* we have to have seen the start above   */
  148.     /* and current line needs to have a '"'   */
  149.     /* character on it.                       */
  150.     if (seen && (quote=strchr(cur, '\"'))) {
  151.         /* get name */
  152.         end = strrchr(cur, '\"');
  153.         name = malloc(end-quote+5);
  154.         memcpy(name, quote+1, end-quote-1);
  155.         name[end-quote-1] = 0;
  156.  
  157.         /* find a mapping */
  158.         first = 1;
  159.         i=0;
  160.         while ( 1 ) {
  161.         for (; i<mi && strcmp(name, map[i].name); i++);
  162.  
  163.         if (i>=mi) {
  164.             if (!first) 
  165.             break;
  166.             else
  167.             mapped = "UNKNOWN";
  168.         } else {
  169.             mapped = map[i].module;
  170.         }
  171.         
  172.         first = 0;
  173.         
  174.         /* insert into current string */
  175.         strp = strrchr(cur, ')');
  176.         memcpy(tmpstr, cur, (strp-cur)+1);
  177.         tmpstr[(strp-cur)+1] = 0;
  178.         strcat(tmpstr, ", ( \"");
  179.         strcat(tmpstr, mapped);
  180.         strcat(tmpstr, "\" )");
  181.         strcat(tmpstr, strp+1);
  182.         fputs(tmpstr, stdout);
  183.         i++;
  184.         }
  185.  
  186.         /* move on */
  187.         free(name);
  188.         strcpy(cur,next);
  189.         continue;
  190.     }
  191.  
  192.          
  193.         
  194.     /* ok, is it end of a pci_module_map */
  195.     if (!strcmp(next,"};\n") && seen) {
  196.         if (strrchr(cur, ',') == (cur+strlen(cur)-1)) {
  197.         cur[strlen(cur)-2] = '\n';
  198.         cur[strlen(cur)-1] = 0;
  199.         }
  200.         fputs(cur, stdout);
  201.         strcpy(cur,next);
  202.         continue;
  203.     }
  204.  
  205.     /* otherwise just print it */
  206.     fputs(cur, stdout);
  207.     strcpy(cur,next);
  208.     }
  209. }
  210.  
  211.         
  212.     
  213.