home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / magazine / drdobbs / 1987 / 12 / naro / readcfg.c < prev    next >
Text File  |  1987-12-21  |  19KB  |  197 lines

  1. #include <stdio.h>                                                                           
  2. #include <stdlib.h>                                                                          
  3. #include <string.h>                                                                          
  4.                                                                                              
  5. #include "loc.h"                                                                             
  6. #include "externs.h"                                                                         
  7.                                                                                              
  8. int   process_class_keyword(), process_order_keyword(), process_null() ;                     
  9. int   process_rom_keyword(), process_dup_keyword(), process_comment() ;                      
  10.                                                                                              
  11.                                                                                              
  12. static   struct   CFG_COMMANDS   {                                                           
  13.          char  *cmd ;                                                                        
  14.          int   (*command)() ;                                                                
  15.       }  cfg_cmds[] = {                                                                      
  16.             "CLASS",    process_class_keyword,                                               
  17.             "ORDER",    process_order_keyword,                                               
  18.             "ROM",      process_rom_keyword,                                                 
  19.             "DUP",      process_dup_keyword,                                                 
  20.             ";",        process_comment                                                      
  21.          } ;                                                                                 
  22.                                                                                              
  23.                                                                                              
  24. int   process_locate_file(seg_list)                                                          
  25. SEG_DESCRIPTOR *seg_list ;                                                                   
  26. {                                                                                            
  27.    int   i, error = OK;                                                                      
  28.    char  *tok, *buf ;                                                                        
  29.                                                                                              
  30.    /*                                                                                        
  31.       This function reads the configuration file and performs the parsing                    
  32.       and control transfer to routines which perform the desired action.                     
  33.    */                                                                                        
  34.                                                                                              
  35.    /* Allocate some memory for the line buffer */                                            
  36.    if ((buf = malloc(256)) == NULL)   {                                                      
  37.       perror(__FILE__) ;                                                                     
  38.       exit(1) ;                                                                              
  39.    }                                                                                         
  40.                                                                                              
  41.    /* Read and categorize a token from the configuration file */                             
  42.    while (fgets(buf, 256, config_file) != NULL)   {                                          
  43.       /* Extract the first token (read the next line if none is found */                     
  44.       if ((tok = strtok(buf, " \t\n")) == NULL)                                              
  45.          continue ;                                                                          
  46.                                                                                              
  47.       for (i = 0; i < dim(cfg_cmds); i++)   {                                                
  48.          if (stricmp(cfg_cmds[i].cmd, tok) == 0)   {                                         
  49.             error = (*cfg_cmds[i].command)() ;                                               
  50.             break ;                                                                          
  51.          }                                                                                   
  52.       }                                                                                      
  53.                                                                                              
  54.       if (i == dim(cfg_cmds))   {                                                            
  55.          fprintf(stderr, "Illegal input - <%s>\n", tok) ;                                    
  56.          exit(1) ;                                                                           
  57.       }                                                                                      
  58.    }                                                                                         
  59.    free(buf) ;                                                                               
  60.    return   error ;                                                                          
  61. }                                                                                            
  62.                                                                                              
  63.                                                                                              
  64. int   process_comment()                                                                      
  65. {                                                                                            
  66.    return   OK ;                                                                             
  67. }                                                                                            
  68.                                                                                              
  69.                                                                                              
  70. int   process_class_keyword()                                                                
  71. {                                                                                            
  72.    char  *tok, name[32], *p ;                                                                
  73.    unsigned int   seg ;                                                                      
  74.                                                                                              
  75.    /*                                                                                        
  76.       This function parses the remainder of the CLASS directive.                             
  77.    */                                                                                        
  78.                                                                                              
  79.    /* Read the class name */                                                                 
  80.    strcpy(name, strupr(strtok(NULL, " \t\n="))) ;                                            
  81.                                                                                              
  82.    /* Verify that an equal sign is present */                                                
  83.    if (strcmp((tok = strtok(NULL, " \t\n")), "=") != 0)   {                                  
  84.       fprintf(stderr, "\"=\" expected instead found <%s>\n", tok) ;                          
  85.       return   ERROR ;                                                                       
  86.    }                                                                                         
  87.                                                                                              
  88.    /* Read the segment number for the class */                                               
  89.    tok = strtok(NULL, " \t\n") ;                                                             
  90.    seg = (unsigned int) strtol(tok, &p, 0) ;                                                 
  91.    if (*p)   {                                                                               
  92.       fprintf(stderr, "Unrecognized token <%s>\n", p) ;                                      
  93.       return   ERROR ;                                                                       
  94.    }                                                                                         
  95.                                                                                              
  96.    /* Assign the physical segment number to the specified class */                           
  97.    if (assign_physical_segment(name, seg) == ERROR)   {                                      
  98.       fprintf(stderr, "Undefined class <%s>\n", name);                                       
  99.       return   ERROR;                                                                        
  100.    }                                                                                         
  101.                                                                                              
  102.    return   OK ;                                                                             
  103. }                                                                                            
  104.                                                                                              
  105.                                                                                              
  106. int   process_order_keyword()                                                                
  107. {                                                                                            
  108.    char  *tok, pclass[32], class[32] ;                                                       
  109.    unsigned int   next_seg ;                                                                 
  110.    BOOLEAN  found = FALSE ;                                                                  
  111.                                                                                              
  112.                                                                                              
  113.    /*                                                                                        
  114.       This function processes the ORDER directive.                                           
  115.    */                                                                                        
  116.                                                                                              
  117.    /* Read the leading class name from the command */                                        
  118.    strcpy(pclass, strupr((tok = strtok(NULL, " \t\n")))) ;                                   
  119.                                                                                              
  120.    /* Process the remaining class names in the command */                                    
  121.    while ((tok = strtok(NULL, " \t\n")) != NULL)   {                                         
  122.       if (*tok == ';')                                                                       
  123.          break ;                                                                             
  124.                                                                                              
  125.       found = TRUE ;                                                                         
  126.       strcpy(class, strupr(tok)) ;                                                           
  127.                                                                                              
  128.       /* Compute the segment address for this class to be made                               
  129.          contiguous with the previous class */                                               
  130.       if (get_next_segment(pclass, class, &next_seg) == ERROR)   {                           
  131.          fprintf(stderr, "Undefined class <%s>\n", pclass) ;                                 
  132.          return   ERROR ;                                                                    
  133.       }                                                                                      
  134.                                                                                              
  135.       /* Assign the computed segment number to the class */                                  
  136.       if (assign_physical_segment(class, next_seg) == ERROR)   {                             
  137.          fprintf(stderr, "Undefined class <%s>\n", class) ;                                  
  138.          return   ERROR ;                                                                    
  139.       }                                                                                      
  140.                                                                                              
  141.       /* Setup to process the next class */                                                  
  142.       strcpy(pclass, class) ;                                                                
  143.    }                                                                                         
  144.                                                                                              
  145.    return   (found == FALSE) ? ERROR : OK ;                                                  
  146. }                                                                                            
  147.                                                                                              
  148.                                                                                              
  149. int   process_dup_keyword()                                                                  
  150. {                                                                                            
  151.    char  *tok, old_class[32], new_class[32] ;                                                
  152.                                                                                              
  153.                                                                                              
  154.    /*                                                                                        
  155.       This function is responsible for processing the DUP directive.                         
  156.    */                                                                                        
  157.                                                                                              
  158.    /* Read the existing class name */                                                        
  159.    strcpy(old_class, strupr((tok = strtok(NULL, " \t\n")))) ;                                
  160.                                                                                              
  161.    /* Read the name of the class to be created */                                            
  162.    strcpy(new_class, strupr((tok = strtok(NULL, " \t\n")))) ;                                
  163.                                                                                              
  164.    /* Duplicate the existing class */                                                        
  165.    if (dup_class(old_class, new_class) == ERROR)   {                                         
  166.       fprintf(stderr, "Undefined class <%s>\n", old_class);                                  
  167.       return   ERROR ;                                                                       
  168.    }                                                                                         
  169.                                                                                              
  170.    return   OK ;                                                                             
  171. }                                                                                            
  172.                                                                                              
  173.                                                                                              
  174. int   process_rom_keyword()                                                                  
  175. {                                                                                            
  176.    char  *tok, class[32] ;                                                                   
  177.                                                                                              
  178.    /*                                                                                        
  179.       This function processes the ROM keyword and marks all specified                        
  180.       classes as ROMable.                                                                    
  181.    */                                                                                        
  182.                                                                                              
  183.    /* Read all of the tokens on the line */                                                  
  184.    while ((tok = strtok(NULL, " \t\n")) != NULL)   {                                         
  185.       if (*tok == ';')                                                                       
  186.          break ;                                                                             
  187.                                                                                              
  188.       strcpy(class, strupr(tok)) ;                                                           
  189.                                                                                              
  190.       if (rom_class(class) == ERROR)   {                                                     
  191.          fprintf(stderr, "Undefined class <%s>\n", class) ;                                  
  192.          return   ERROR ;                                                                    
  193.       }                                                                                      
  194.    }                                                                                         
  195.                                                                                              
  196.    return   OK ;                                                                             
  197. }