home *** CD-ROM | disk | FTP | other *** search
/ Crawly Crypt Collection 1 / crawlyvol1.bin / program / compiler / nasm20b / nasm_src / lib65.c < prev    next >
C/C++ Source or Header  |  1993-01-19  |  8KB  |  311 lines

  1. /* ---------------------------------------------------------------------- */
  2. /*                   Copyright (C) 1992 by Natürlich!                     */
  3. /*                      This file is copyrighted!                         */
  4. /*                Refer to the documentation for details.                 */
  5. /* ---------------------------------------------------------------------- */
  6. #define LIBRARIAN 1
  7. #include "defines.h"
  8. #include "nasm.h"
  9. #include <stdio.h>
  10. #include <string.h>
  11. #include "debug.h"
  12. #include "object.h"
  13. #include "lib.h"
  14. #if STATISTICS
  15. #include <time.h>
  16. #endif
  17. #include OSBIND
  18. #include NMALLOC_H
  19.  
  20. char          *currfile,
  21.               outfile[ 256];
  22. static char   *infile[ MAXFILES],
  23.               linkfile[ 256],
  24. #if OS == TOS
  25. _usage[] =
  26. "Usage: nlib65 [-{tw}][-{aducev}][-b batch] <-l library> <object files>\n",
  27. #else
  28. _usage[] =
  29. "Usage: nlib65 [-w][-{aducev}][-b batch] <-l library> <object files>\n",
  30. # if PORTED
  31. portnoy[] =
  32. {
  33. #  if INCOMPATIBLE
  34.    'W','a','r','n','i','n','g',':',' ','O','b','j','e','c','t',' ',
  35.    'f','i','l','e',' ',
  36.    'i','n','c','o','m','p','a','t','i','b','i','l','t','y','.',' ',
  37. #  endif
  38.    'P','o','r','t','e','d',' ','b','y',' ','t','h','e',' ',
  39.    'v','a','l','i','a','n','t',' ',PORTER,'\n',0
  40. },
  41. # endif
  42. #endif
  43. notice[] =
  44. {
  45.    'N','l','i','b','6','5',' ',' ','v',VERSION+'0','.',
  46.    LIBREVISION/10+'0',LIBREVISION%10+'0',' ',
  47.    'b','y',' ',AUTHOR,' ',' ','C','o','p','y','r','i','g','h','t',' ',
  48.    '(','c',')',' ','1','9','9','2',' ',ORGANIZATION,'\r','\n',0
  49. };
  50.  
  51. int           operation,
  52.               verbose,
  53.               laber,
  54. #if OS == TOS
  55.               tossable,
  56. #endif
  57.               emptyfile,
  58.               what_the_fuck,        /* cited from "Everybody wants some" */
  59.               fdout,
  60.               nfiles,
  61. #if STATISTICS              
  62.               tok_remain,
  63. #endif
  64.               maxerrors = 20;
  65.  
  66. extern int  freshflag,
  67.             errors;
  68.  
  69.  
  70. void main( argc, argv)
  71. int   argc;
  72. char  **argv;
  73. {
  74. #if STATISTICS
  75.    clock_t        t_start, t_finish, t_result, t_tmp;
  76. #endif
  77.    int            i = 0;
  78.  
  79.    ENTER("main");
  80. #if STATISTICS
  81.    t_start = clock();
  82. #endif
  83.  
  84. #if DEBUG
  85.    if( argc > 1)
  86.       printf("Argc=%d  argv[1]=\"%s\"\n", argc, argv[1]);
  87. #endif
  88.  
  89.    while( ++i < argc)
  90.       if( *argv[i] == '-')
  91.          switch( Xtolower( argv[i][1]))
  92.          {
  93.  
  94.             case 'l' :
  95.                if( ++i == argc)
  96.             default  :
  97.                   goto usage;
  98.                strcpy( outfile, argv[i]);
  99.                break;
  100.  
  101.             case 'a' :
  102.                operation = OP_ADD;
  103.                break;
  104.  
  105.             case 'u' :
  106.                operation = OP_UPDATE;
  107.                break;
  108.  
  109.             case 'd' :
  110.                operation = OP_DELETE;
  111.                break;
  112.  
  113.             case 'c' :
  114.                operation = OP_CREATE;  /* not for the faint of heart */
  115.                break;
  116.  
  117.             case 'e' :
  118.                operation = OP_EXTRACT;
  119.                break;
  120.  
  121.             case 'v' :
  122.                verbose   = argv[i][2];
  123.                operation = OP_LIST;
  124.                break;
  125.  
  126. #if OS == TOS
  127.             case 't' :
  128.                tossable = ! tossable;
  129.                break;
  130. #endif
  131.  
  132. #ifdef __DATE__
  133.             case ':' :
  134.                fputs( __DATE__, stderr);
  135. # ifdef __TIME__
  136.                fprintf( stderr, " %s", __TIME__);
  137. # endif                              
  138.                putc( '\n', stderr);
  139.                break;
  140. #endif               
  141.             case 'w' :
  142.                what_the_fuck = 1;
  143.                break;
  144.  
  145.             case '_' :
  146.                laber = 1;
  147.                break;
  148.  
  149.             case 'b' :
  150.             {
  151.                register char  c;
  152.                FILE           *fp, *fopen();
  153.                static char    x[256];
  154.  
  155.                if( ++i == argc)
  156.                   goto usage;
  157.                strcpy( linkfile, argv[i]);
  158.                complete( linkfile, ".bth", 0);
  159.                if( ! (fp = fopen( linkfile, "r")))
  160.                   nferror("Opening the linkfile failed");
  161.                do
  162.                   switch( c = getc( fp))
  163.                   {
  164.                      case '#'  :
  165.                         while( (c = getc( fp)) != '\n' && c != EOF);
  166.                         break;
  167.  
  168.                      default   :
  169.                      {
  170.                         register int   i = 0;
  171.  
  172.                         do
  173.                            x[i++] = c;
  174.                         while( (c = getc( fp)) != ' '  && c != '\t' &&
  175.                                c != '\r' && c != '\n' && c != EOF && i < 255);
  176.                         x[i++] = 0;
  177.                         infile[ nfiles++] = strcpy( (char *) nmalloc( (long) i), x);
  178.                      }
  179.                      case ' '  :
  180.                      case '\t' :
  181.                      case '\r' :
  182.                      case '\n' :
  183.                      case EOF  : ;
  184.                   }
  185.                while( c != EOF);
  186.                fclose( fp);
  187.             }
  188.          }
  189.       else
  190.          infile[nfiles++] = argv[i];
  191.  
  192.    if( (! infile[0] && operation < OP_LIST) || ! outfile[0])
  193.       goto usage;
  194.  
  195. #if OS == MSDOS
  196.    _fmode = O_BINARY;
  197. #endif
  198.    Cconws( notice);
  199. #if ! VERSION
  200.    Cconws("unfinished - unstable - unsupported -- untested. Don't use it!\r\n");
  201.    version0();
  202. #endif
  203. #if PORTED
  204.    Cconws( portnoy);
  205. #endif
  206.    complete( currfile = outfile, ".l65", 0);
  207.    pro_init();
  208.  
  209.    if( operation == OP_CREATE)
  210.    {
  211.       if( (i = (int) Fopen( outfile, OPEN_W)) >= 0)
  212.       {
  213.          Fclose( i);
  214.          nwarning("Killed an old real existing library");
  215.       }
  216.       goto doit;
  217.    }
  218.    if( ! lload( currfile = outfile))
  219.       if( operation == OP_ADD)
  220.       {
  221. doit:
  222.          if( (i = (int) Fkreate( currfile, 0x664)) < 0)
  223.             ngferror( i, "Couldn't create library file");
  224.          Fclose( i);
  225.          emptyfile = 1;
  226.       }
  227.       else
  228.          nferror("Library not found");
  229.  
  230.    if( operation == OP_LIST)
  231.    {
  232.       if( nfiles)
  233.          nwarning("Object files ignored");
  234.       list( verbose);
  235.    }
  236.    else
  237.       for( i = 0; i != nfiles; i++)
  238.       {
  239.          IMESS("Libbing now \"%s\"", (unsigned long) infile[i], 4);
  240.          switch( operation)
  241.          {
  242.             case OP_UPDATE :
  243.                delete( infile[ i], 1);
  244.             case OP_CREATE :
  245.             case OP_ADD    :
  246.                oload( currfile = infile[i]);
  247.                sym_lib();
  248.                file_lib( currfile);
  249.                wrapup();
  250.                break;
  251.  
  252.             case OP_EXTRACT :
  253.                extract( infile[ i]);
  254.                break;
  255.  
  256.             case OP_DELETE  :
  257.                delete( infile[ i], 0);
  258.          }
  259.       }
  260.    pro_exit();
  261.  
  262.    if( ! errors || what_the_fuck)
  263.    {
  264.       extern word  gindex, findex;
  265.  
  266.       if( ! (findex && gindex))
  267.          nferror("Library would be empty");
  268.       if( (fdout = (int) Fopen( currfile = outfile, OPEN_W)) < 0)
  269.          nferror("Can't open library file");
  270.       write_results( fdout);     /* clobbers internal structures !! */
  271.    }
  272.  
  273.    MESS("OK JUST ABOUT DONE");
  274. #if STATISTICS
  275.    t_finish = clock();
  276.    if( t_start > t_finish)
  277.       t_tmp = t_start - t_finish;
  278.    else
  279.       t_tmp = t_finish - t_start;
  280.    t_result = (t_finish - t_start) / (int) CLK_TCK;
  281.    finalstats(1);
  282.    printf("Link took %ld.%lds\n", t_result,
  283.                            (t_tmp - (t_result * (int) CLK_TCK)) >> 1 );
  284.    stats();
  285. #endif
  286.    ALEAVE();
  287.    nexit( 0);
  288.  
  289. usage:
  290.    fputs( _usage, ESTREAM);
  291.    fputs( "\
  292. \t-a         : add to library\n\
  293. \t-u         : update libraray\n\
  294. \t-d         : delete from library\n\
  295. \t-c         : create library from scratch\n\
  296. \t-e         : extract from library\n\
  297. \t-v         : verbose\n\
  298. \t-w         : write output file anyway (W.T.F.)\n\
  299. \t-_         : talkative\n\
  300. \t-b  <batch>: take input files from batchfile\n\
  301. \t-l  <lib>  : library to use\n", ESTREAM);
  302. #if OS == TOS
  303.         fputs( "\
  304. \t-t         : wait for keypress before exit\n", ESTREAM);
  305. #endif
  306.    ALEAVE();
  307.    nexit( 1);
  308. }
  309.  
  310.  
  311.