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

  1. #include <stdio.h>                                                                           
  2. #include <stdlib.h>                                                                          
  3. #include <string.h>                                                                          
  4. #include <signal.h>                                                                          
  5.                                                                                              
  6. #include "loc.h"                                                                             
  7. #include "globals.h"                                                                         
  8. #include "externs.h"                                                                         
  9.                                                                                              
  10. /*                                                                                           
  11.    LOCATE *** MS-DOS ROM Utility                                                             
  12.    Copyright (C) 1987 Rick Naro.  All rights reserved.                                       
  13. */                                                                                           
  14.                                                                                              
  15. int   main(argc, argv)                                                                       
  16. int argc;                                                                                    
  17. char *argv[];                                                                                
  18. {                                                                                            
  19.    char  *s, *input_file ;                                                                   
  20.    unsigned char  *entry_point ;                                                             
  21.    int   i ;                                                                                 
  22.                                                                                              
  23.    /*                                                                                        
  24.       This is the root module of the locate utility and it controls the                      
  25.       sequencing of the entire location process.                                             
  26.    */                                                                                        
  27.                                                                                              
  28.    /* Install a Control-C interrupt handler */                                               
  29.    if (signal(SIGINT, break_handler) == (int(*)()) -1)   {                                   
  30.       fprintf(stderr, "Failure to install break handler\n") ;                                
  31.       abort() ;                                                                              
  32.    }                                                                                         
  33.                                                                                              
  34.    /* Build a command line string using argv[0] through argv[argc-1] */                      
  35.    command_line[0] = '\0' ;                                                                  
  36.    for (i = 0; i < argc; i++)   {                                                            
  37.       strcat(command_line, argv[i]) ;                                                        
  38.       strcat(command_line, " ") ;                                                            
  39.    }                                                                                         
  40.                                                                                              
  41.    /* Test if the user needs help in running this utility */                                 
  42.    if (argc == 1)                                                                            
  43.       help = TRUE ;                                                                          
  44.                                                                                              
  45.    config_fname[0] = abs_fname[0] = print_fname[0] = '\0' ;                                  
  46.                                                                                              
  47.    /* Process each argument in sequence until all are processed */                           
  48.    while (--argc > 0 && (*++argv)[0] == '-')   {                                             
  49.       for (s = argv[0] + 1;  *s != '\0';  s++)   {                                           
  50.          switch (*s)   {                                                                     
  51.             case 'b':                                                                        
  52.                boot_rec = TRUE ;                                                             
  53.                break ;                                                                       
  54.                                                                                              
  55.             case 'c':                                                                        
  56.                config = TRUE ;                                                               
  57.                if (*++s)                                                                     
  58.                   strcpy(config_fname, s) ;                                                  
  59.                *s-- = '\0' ;                                                                 
  60.                break ;                                                                       
  61.                                                                                              
  62.             case 'h':                                                                        
  63.                hex_name = TRUE ;                                                             
  64.                if (*++s)                                                                     
  65.                   strcpy(abs_fname, s) ;                                                     
  66.                *s-- = '\0' ;                                                                 
  67.                break ;                                                                       
  68.                                                                                              
  69.             default:                                                                         
  70.                help = TRUE ;                                                                 
  71.                argc = 0 ;                                                                    
  72.                break ;                                                                       
  73.          }                                                                                   
  74.       }                                                                                      
  75.    }                                                                                         
  76.    input_file = argv[0] ;                                                                    
  77.                                                                                              
  78.    if (help == TRUE)   {                                                                     
  79.       fprintf(stderr, "\nUsage is\n\n") ;                                                    
  80.       fprintf(stderr, "\tlocate switches exefile\n\n") ;                                     
  81.       fprintf(stderr, "The valid switches are:\n\n") ;                                       
  82.       fprintf(stderr, "\t%-14s create bootstrap record\n", "-b") ;                           
  83.       fprintf(stderr, "\t%-14s configuration filename\n", "-c[name]") ;                      
  84.       fprintf(stderr, "\t%-14s hex filename\n", "-h[name]") ;                                
  85.       exit(1) ;                                                                              
  86.    }                                                                                         
  87.                                                                                              
  88.    fprintf(stderr, "MS-DOS Locate Utility - Version 1.0\n") ;                                
  89.    fprintf(stderr, "Copyright (C) 1987 Rick Naro.  ") ;                                      
  90.    fprintf(stderr, "All rights reserved\n\n") ;                                              
  91.                                                                                              
  92.    /* Open and create the files used in the location process */                              
  93.    open_file_system(input_file) ;                                                            
  94.                                                                                              
  95.    /* Install the routine to shutdown the utility gracefully in the                          
  96.       event of an error. */                                                                  
  97.    onexit(close_file_system) ;                                                               
  98.                                                                                              
  99.    /* Build the segment descriptor list using the link map */                                
  100.    seg_list = build_seg_list();                                                              
  101.                                                                                              
  102.    /* Process the locate configuration file */                                               
  103.    if (process_locate_file(seg_list) == ERROR)   {                                           
  104.       fprintf(stderr, "Error(s) reading the locate map\n") ;                                 
  105.       exit(1) ;                                                                              
  106.    }                                                                                         
  107.                                                                                              
  108.    /* Convert any public symbols to their new physical addresses */                          
  109.    read_symbol_table(seg_list) ;                                                             
  110.                                                                                              
  111.    /* Read the load module and perform the segment fixups */                                 
  112.    entry_point = load_exe_file() ;                                                           
  113.                                                                                              
  114.    /* Add a bootstrap record if enabled on the command line */                               
  115.    if (boot_rec == TRUE)                                                                     
  116.       create_bootstrap(seg_list, entry_point) ;                                              
  117.                                                                                              
  118.    /* Output the load module in the specified format */                                      
  119.    output_hex_OMF(abs_file, seg_list, entry_point) ;                                         
  120.                                                                                              
  121.    /* Make the locate map containing the new segment assignments */                          
  122.    print_statistics(map_fname, print_fname, command_line, exe_fname, \                       
  123.       abs_fname, config_fname, entry_point) ;                                                
  124.                                                                                              
  125.    exit(0) ;                                                                                 
  126. }                                                                                            
  127.                                                                                              
  128.                                                                                              
  129. void  break_handler()                                                                        
  130. {                                                                                            
  131.    /*                                                                                        
  132.       The break handler is provided to catch Ctrl-C interrupts from the                      
  133.       user and perform a shutdown of the program in a graceful manner.                       
  134.    */                                                                                        
  135.                                                                                              
  136.    /* Set the user abort flag for the file system close routine */                           
  137.    user_abort = TRUE ;                                                                       
  138.    exit(1) ;                                                                                 
  139. }                                                                                            
  140.