home *** CD-ROM | disk | FTP | other *** search
/ Crawly Crypt Collection 1 / crawlyvol1.bin / program / compiler / nasm20b / nasm_src / main65.c < prev    next >
Text File  |  1993-01-19  |  11KB  |  480 lines

  1. /* ---------------------------------------------------------------------- */
  2. /*                   Copyright (C) 1992 by Natürlich!                     */
  3. /*                      This file is copyrighted!                         */
  4. /*                Refer to the documentation for details.                 */
  5. /*           if you add to the options, remember NASM.TTP too             */
  6. /* ---------------------------------------------------------------------- */
  7. #include "defines.h"
  8. #include "nasm.h"
  9. #include OSBIND
  10. #include "debug.h"
  11. #include "buffer.h"
  12. #include <stdio.h>
  13. #include <string.h>
  14. #include <stdlib.h>
  15. #include <time.h>
  16. #if STATISTICS
  17. #include NMALLOC_H
  18. #endif
  19.  
  20. static char   infile[ 256],
  21.               outfile[ 256],
  22. #if OS == TOS
  23. _usage[] =
  24. "Usage: nasm65 [-{brstw}][-a xx][-e #][-o binary][-h includedir] <source>\n",
  25. #else
  26. _usage[] =
  27. "Usage: nasm65 [-{brsw}][-a xx][-e #][-o binary][-h includedir] <source>\n",
  28. # if PORTED
  29. portnote[] =
  30. {
  31. # if INCOMPATIBLE
  32.    'W','a','r','n','i','n','g',':',' ','O','b','j','e','c','t',' ',
  33.    'f','i','l','e',' ',
  34.    'i','n','c','o','m','p','a','t','i','b','i','l','t','y','.',' ',
  35. # endif
  36.    'P','o','r','t',' ','b','y',' ',PORTER,'\n',0
  37.  
  38. },
  39. # endif
  40. #endif
  41.  
  42. notice[] =
  43. {
  44.    'N','a','s','m','6','5',' ',' ','v',VERSION+'0','.',
  45.    ASMREVISION/10+'0',ASMREVISION%10+'0',' ',
  46.    'b','y',' ',AUTHOR,' ',' ','C','o','p','y','r','i','g','h','t',' ',
  47.    '(','c',')',' ','1','9','9','2',' ',ORGANIZATION,
  48. #if OS == TOS
  49.    '\r','\n',
  50. #else
  51.    '\n',
  52. #endif
  53.    0
  54. };
  55.  
  56. char          header[128];  
  57. word          alignment;
  58. int           runnable,
  59.               better,
  60.               show_open,
  61.               show_expansion,
  62.               show_mload,
  63. #if OS == TOS
  64.               tossable,
  65. # if WITHNOISY
  66.               noisytok,
  67. # endif
  68. #endif
  69.               pssst,
  70.               what_the_fuck,        /* cited from "Everybody wants some" */
  71.               fdout,
  72.               maxerrors = 30;
  73.  
  74. char          buf[ 128], *realname;
  75.  
  76. extern int  freshflag,
  77.             errors;
  78.  
  79.  
  80.  
  81. main( argc, argv)
  82. int   argc;
  83. char  **argv;
  84. {
  85. #if STATISTICS
  86.    clock_t        t_start, t_finish, t_result, t_tmp;
  87. #endif
  88.    char huge      *x;
  89.    int            foo;   /* portability */
  90.    int            i = 0;
  91.  
  92.    ENTER("main");
  93.  
  94. #if STATISTICS
  95.    t_start = clock();
  96. #endif
  97.    if( x = getenv( "INC65"))
  98.    {
  99.       strcpy( header, x);
  100.       fixheader();
  101.    }
  102. #if OS == TOS
  103.    else
  104.       tossable = 1;
  105. #endif
  106.  
  107. #if DEBUG
  108.    if( argc > 1)
  109.       printf("Argc=%d  argv[1]=\"%s\"\n", argc, argv[1]);
  110. #endif
  111.  
  112. #if VERSION                   /* need this up here for -D defs */
  113.    sexp_alloc();              /* reserve space for exprfast    */
  114.    str_alloc(1);              /* reserve space for yy_txt2val  */
  115. #endif
  116.    while( ++i < argc)
  117.    {
  118.       if( *argv[i] == '-')
  119.          switch( Xtolower( argv[i][1]))
  120.          {
  121.             case '_' :
  122.                if( ++i == argc)
  123.                   nierror("wrong arg given by CPP");
  124.                realname = argv[ i];
  125.                break;
  126.                
  127.             case 'a' :                                     
  128.                if( ++i == argc || ! sscanf( argv[i], "%X", &foo))
  129.                   goto usage;
  130.                if( (alignment = (word) foo) >= 0x100)
  131.                   nferror("Only alignment values less than $100");
  132.                {
  133.                   register word  i = alignment;
  134.  
  135.                   for(; i & 1; i >>= 1);
  136.                      if( i)                          
  137.                         nwarning("Alignment value  H I G H L Y  unusual");
  138.                }
  139.                break;
  140.  
  141.             case 'b' :
  142.                better = ! better;
  143.                break;
  144.             
  145.             
  146.             case 'd' :  /* maybe we should use the env. as well */
  147.                if( ++i < argc)              
  148.                {
  149.                   word  val = 0;
  150.                   char  *x, *y;
  151.                
  152.                   x = argv[i];
  153.                   upcase( x);
  154.                   if( y = strrchr( x, '='))
  155.                   {
  156.                      *y = 0;
  157.                      sscanf( y + 1, "%d", &val);
  158.                   }
  159.                   def_label( x, val);
  160.                }
  161.                else
  162.                   goto usage;
  163.                break;
  164.                      
  165.             case 'e' :
  166.                if( ++i == argc || ! sscanf( argv[i], "%d", &maxerrors))
  167.                   goto usage;
  168.                break;
  169.  
  170.             case 'h' :
  171.                if( ++i == argc)
  172.                   goto usage;
  173.                strcpy( header, argv[i]);
  174.                fixheader();
  175.                break;
  176.  
  177. #if OS == TOS
  178. # if WITHNOISY
  179.             case 'n' :
  180.                noisytok = ! noisytok;
  181.                break;
  182. # endif
  183.             case 't' :
  184.                tossable = ! tossable;
  185.                break;
  186. #else
  187.             case 't' :
  188.             case 'n' :
  189.                break;
  190. #endif
  191.  
  192.             case 'o' :
  193.                if( ++i == argc)
  194.             default  :
  195.                   goto usage;
  196.                strcpy( outfile, argv[i]);
  197.                break;
  198.  
  199.             case 'q' :
  200.                pssst = ! pssst;
  201.                break;
  202.  
  203.             case 'r' :
  204.                runnable = ! runnable;
  205.                break;
  206.  
  207.             case 's' :
  208.                show_expansion = ! show_expansion;
  209.                if( argv[ i][ 2])
  210.                   show_mload = ! show_mload;
  211.                break;
  212.  
  213. #ifdef __DATE__
  214.             case ':' :
  215.                fputs( __DATE__, stderr);
  216. # ifdef __TIME__
  217.                fprintf( stderr, " %s", __TIME__);
  218. # endif                              
  219.                putc( '\n', stderr);
  220.                break;
  221. #endif               
  222.             case 'v' :
  223.                if( Xtolower( argv[i][2]) != 'f' ||
  224.                    Xtolower( argv[i][3]) != 'l')
  225.                   goto usage;
  226.                {
  227.                   extern char is_where[];
  228.                   extern byte is_what[];
  229.  
  230.                   is_where[ '|'] = 1;
  231.                   is_what[ '|']  = 0x10;
  232. #if VERSION
  233.                   if( (rand() & 0x100)/*Random()*/ & 0x1)
  234.                      print_secret();
  235.                   else
  236.                      print2_secret();
  237. #endif
  238.                }
  239.                break;
  240.  
  241.             case 'w' :
  242.                what_the_fuck = ! what_the_fuck;
  243.                break;
  244.  
  245.             case 'x' :
  246.                show_open = ! show_open;
  247.          }
  248.       else
  249.       {
  250.          if( infile[0])
  251.             goto usage;
  252.          strcpy( infile, argv[i]);
  253.       }
  254.    }
  255.    if( ! infile[0])
  256.       goto usage;
  257.  
  258.    if( ! pssst)
  259.    {
  260.       Cconws( notice);
  261. #if PORTED
  262.       Cconws( portnote);
  263. #endif
  264.    }
  265.  
  266. #if OS == /* fucking */ MSDOS
  267.    _fmode = O_BINARY;
  268. #endif
  269.  
  270. #if ! VERSION
  271.    Cconws("unfinished - unstable - unsupported - untested. Don't use it!\r\n");
  272.    version0();
  273. #endif
  274.    MESS("Starting the INCLUDE");
  275.    include( infile, ".s65");
  276.    {
  277.       extern buffer  *bp;
  278.     
  279.       inc_line();
  280.    }
  281.    if( ! outfile[0])
  282.    {
  283.       strcpy( outfile, infile);
  284.       complete( outfile, runnable ? ".com" : ".o65", 1);
  285.    }
  286.    pro_init();                /* alloc dump space */
  287.    if( runnable)
  288.       def_label( "__RUN", 1);
  289.    def_label(" __NASM65", 1);
  290. #if YACCDEBUG
  291.    {
  292.       extern int   yydebug;
  293.  
  294.       yydebug = 1;
  295.    }
  296. #endif
  297.    yyparse();
  298.    MESS("we are back");
  299.    do_local();              /* clean up and dump unrefs */
  300.    err_undefs();
  301.    pro_exit();
  302. #if ! DEBUG
  303.    if( ! errors || what_the_fuck)
  304.    {
  305. #endif
  306.       if( (fdout = (int) Fkreate( outfile, 0x775)) < 0)
  307.          nferror("Can't create output file");
  308.       write_results( fdout);
  309. #if ! DEBUG
  310.    }
  311. #endif
  312. #if STATISTICS
  313.    MESS("OK JUST ABOUT DONE");
  314.    t_finish = clock();
  315.    if( t_start > t_finish)
  316.       t_tmp = t_start - t_finish;
  317.    else
  318.       t_tmp = t_finish - t_start;
  319.    t_result = (t_finish - t_start) / (int) CLK_TCK;
  320.    finalstats(1);
  321.    printf("Assembly took %ld.%lds\n", t_result,
  322.                            (t_tmp - (t_result * (word) CLK_TCK)) >> 1 );
  323.    stats();
  324. #endif
  325.    ALEAVE();
  326.    nexit( errors ? 1 : 0);
  327.  
  328. usage:
  329.    errors = 1;
  330.    fputs( _usage, ESTREAM);
  331.    fputs( "\
  332. \t-x           : list unresolved references\n\
  333. \t-w           : write output file anyway (W.T.F.)\n\
  334. \t-h <path>    : \"system\" include file directory\n\
  335. \t-o <file>    : output file specifier\n\
  336. \t-r           : create runnable binary file\n\
  337. \t-e <digit>   : set maximal number of errors before aborting\n\
  338. \t-s           : show calling of macros and include files\n\
  339. \t-d <l>[=<v>] : defines label <l>. optionally assigns <v> as value to it\n\
  340. \t-a <value>   : alignment value (1->even 255->page)\n\
  341. \t-q           : quiet flag\n\
  342. \t-b           : optimize small segments\n", ESTREAM);
  343. #if OS == TOS
  344.    fputs( "\
  345. \t-t           : wait for keypress before exit\n\
  346. \t-n           : noisy tokens\n", ESTREAM);
  347. #endif
  348.    ALEAVE();
  349.    nexit( 1);
  350. }
  351.  
  352.  
  353. void  fixheader()
  354. {
  355.    static char foo[] = { DIRSLASH, 0 };
  356.  
  357.    if( header[ strlen( header)] != DIRSLASH)
  358.       strcat( header, foo);
  359. #if DEBUG
  360.    printf("Current include path : \"%s\"\n", header);
  361. #endif
  362. }
  363.  
  364. void  yyerror()
  365. {
  366. }
  367.  
  368. void print_char( c)
  369. char  c;
  370. {
  371. #if OS != AMIGA || defined( __hp9000s300)
  372.    static clock_t  tick;
  373.  
  374.    Bconout( 2, (int) c);
  375.    tick = clock() + ((int) CLK_TCK >> 2);
  376.    while( tick > clock());
  377. #else
  378.    Bconout( 2, (int) c);
  379.    sleep( 1);
  380. #endif   
  381. }
  382.  
  383.  
  384. void print_secret()
  385. {
  386.    static char  vfl1[] = "Dqn~B~a⇩-$e①s① oRhTm⇧W}lEe^ _az(n/w8r ",
  387.                 vfl2[] = "①e⑥  l*u4W*iCsHBHcIuM ha|zfrtn}e╱ ①i9 ";
  388.  
  389.    char huge   *p = vfl1,
  390.                *q = vfl2;
  391.    do
  392.    {
  393.       print_char( *p++);
  394.       print_char( *++q);
  395.    }
  396.    while( *++q && *++p);
  397.    print_char('\r');
  398.    print_char('\n');
  399. }
  400.  
  401. #ifdef __hp9000s300
  402. # define UNIT   2
  403. # define KURZ   1
  404. # define MITTEL 2
  405. # define LANG   3
  406. #else
  407. # if OS == AMIGA
  408. #  undef CLK_TCK
  409. #  define CLK_TCK 50
  410. # endif
  411. # define UNIT   ((int) CLK_TCK >> 1)
  412. # define KURZ   ((int) CLK_TCK / 3)
  413. # define MITTEL ((int) CLK_TCK >> 1)
  414. # define LANG   ((int) CLK_TCK  - 1)
  415. #endif
  416.  
  417.  
  418. void takt( s, flag, len)
  419. char huge   *s;
  420. {
  421.    int   c;
  422.  
  423.    while( c = *s++)
  424.       Bconout( 2, c ^ 0x5F);
  425.    if( flag)
  426.       Bconout(2, ' ');
  427. #if ! defined( __hp9000s300) && OS != AMIGA
  428.    {
  429.       clock_t  tick;
  430.       
  431.       tick = clock() + len;
  432.       while( tick > clock());
  433.    }
  434. #else
  435.    sleep( len);
  436. #endif   
  437. }
  438.  
  439. static char  secret[] =
  440. {
  441.    0x1B, 0x3A, 0x31, 0,
  442.    0x1D, 0x33, 0x3E, 0x2A, 0,
  443.    0x72, 0x8,  0x3A, 0x36, 0x2C, 0x2C, 0,
  444.    0x1D, 0x30, 0,
  445.    0x3C, 0x37, 0x2A, 0x32, 0,
  446.    0x8,  0x3E, 0x33, 0,
  447.    0x25, 0x3A, 0x2D, 0,
  448.    0x2B, 0x3E, 0x31, 0,
  449.    0x25, 0x3A, 0x31, 0,
  450.    0x28, 0x36, 0x2D, 0x71, 0x71, 0x71, 0
  451. };
  452.  
  453. void print2_secret()
  454. {
  455.    char huge   *p = secret;
  456.  
  457.    takt( p,      1, KURZ);
  458.    takt( p += 4, 0, LANG);
  459.    takt( p += 5, 1, MITTEL);
  460.    takt( p += 7, 0, LANG);
  461.    takt( p += 3, 1, MITTEL);
  462.    takt( p += 5, 0, MITTEL);
  463.    takt( p += 4, 1, KURZ);
  464.    takt( p += 4, 0, MITTEL);
  465.    takt( p += 4, 1, KURZ);
  466.    takt( p + 4,  0, KURZ);
  467.    Bconout( 2, '\r');
  468.    Bconout( 2, '\n');
  469. }
  470.  
  471.  
  472. void upcase( s)
  473. register char  *s;
  474. {
  475.    register char  c;
  476.    
  477.    while( c = *s)
  478.       *s++ = toupper( c);
  479. }
  480.