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

  1. /* ---------------------------------------------------------------------- */
  2. /*                   Copyright (C) 1992 by Natürlich!                     */
  3. /*                      This file is copyrighted!                         */
  4. /*                Refer to the documentation for details.                 */
  5. /* ---------------------------------------------------------------------- */
  6. /* rev. history   1.0 --                                      */
  7. /*                1.1 --                                      */
  8. /*                1.2 -- removed NIL crashes on faulty MAC65  */
  9. /*                1.3 -- -c option searches directory         */
  10. /*                1.4 -- faulty M65 files are treated kinder  */
  11. /*                1.5 -- no real changes                      */
  12. /* ---------------------------------------------------------- */
  13.  
  14. #include "defines.h"
  15. #if __NSTDC__ && __TURBOC__
  16. #pragma warn -pia   /* Don't want those if( foo = bar) warnings */
  17. #endif
  18.  
  19. #include <stdio.h>
  20. #include <string.h>
  21. #include <stddef.h>
  22. #include <stdlib.h>
  23. #include <setjmp.h>
  24. #define __BIG_GENERATOR__
  25. #include OSBIND
  26. #include "code.h"
  27.  
  28. #define COMMENTPAD  24
  29. #define MACROPAD    06
  30. #define INSTRPAD    06
  31. #define DATAPAD     12
  32.  
  33. #if OS == TOS
  34. struct    
  35. {
  36.     char    reserved[ 21];
  37.     byte    attribs;
  38.     word    time,
  39.             date;
  40.     lword   len;
  41.     char    name[ 14];
  42. } dir;
  43. #endif
  44.  
  45. void  convert(),
  46.       nflush(),
  47.       io(),
  48.       label(),
  49.       nput(),
  50.       nprint(),
  51.       comment(),
  52.       padto();
  53.  
  54. struct
  55. {
  56.    word  fefe,
  57.          len;
  58. } header;
  59.  
  60. #ifdef NIL
  61. #undef NIL
  62. #endif
  63. #define NIL (char *) 0
  64.  
  65. #define TAB1SIZE 95
  66.  
  67. char  *table1[] =
  68. {
  69.    "error -",  ".if",      ".else",    ".endif",   ".macro",
  70.    ".endm",    ".title",   NIL,        ".page",    ".word",
  71.    ".error",   ".byte",    ".sbyte",   ".dbyte",   ".end",
  72.    ".opt",     ".tab",     ".include", ".ds",      ".org",
  73.    ".equ",     "bra",      "trb",      "tsb",      ".float",
  74.    ".cbyte",   "",         ".local",   ".set",     "*=",
  75.    "=",        ".=",       "jsr",      "jmp",      "dec",
  76.    "inc",      "ldx",      "ldy",      "stx",      "sty",
  77.    "cpx",      "cpy",      "bit",      "brk",      "clc",
  78.    "cld",      "cli",      "clv",      "dex",      "dey",
  79.    "inx",      "iny",      "nop",      "pha",      "php",
  80.    "pla",      "plp",      "rti",      "rts",      "sec",
  81.    "sed",      "sei",      "tax",      "tay",      "tsx",
  82.    "txa",      "txs",      "tya",      "bcc",      "bcs",
  83.    "beq",      "bmi",      "bne",      "bpl",      "bvc",
  84.    "bvs",      "ora",      "and",      "eor",      "adc",
  85.    "sta",      "lda",      "cmp",      "sbc",      "asl",
  86.    "rol",      "lsr",      "ror",      NIL,        "stz",
  87.    "dea",      "ina",      "phx",      "phy",      "plx",
  88.    "ply"
  89. };
  90.  
  91.  
  92. static char    ref[] = ".ref ",  and[] = ".and ", not[]= ".not ",
  93.                or[]  = ".or ",   def[] = ".def ";
  94.                
  95. #define TAB2SIZE  78
  96.  
  97. char  otable2[] =     /* 0 - 77 */
  98. {
  99.  0, 0, 0, 0, 0, 0, 0, 0,
  100.  0, 0, 0, 1, 1, 1, 0, 0,
  101.  0, 0, 1, 1, 1, 1, 1, 0,
  102.  1, 1, 1, 1, 1, 1, 1, 1,
  103.  1, 0, 0, 0, 1, 1, 0, 1,
  104.  0, 0, 0, 0, 0, 0, 0, 1,
  105.  1, 1, 1, 1, 1, 1, 1, 1,
  106.  1, 1, 1, 0, 0, 1, 1, 1,
  107.  1, 1, 1, 1, 1, 1, 1, 1,
  108.  1, 1, 1, 1, 1, 1
  109. };
  110.  
  111. char  *table2[] =     /* 0 - 77 */
  112. {
  113.    NIL,     NIL,     NIL,     NIL,     NIL,     NIL,     NIL,     NIL,
  114.    NIL,     NIL,     NIL,     "%$",    "%",     "*",     " ",     " ",     
  115.    "a",     "q",     "+",     "-",     "*",     "/",     "&",     NIL,     
  116.    "=",     "<=",    ">=",    "<>",    ">",     "<",     "-",     "[",     
  117.    "]",     or,      and,     not,     "!",     "^",     ref,     "\\",    
  118.    NIL,     NIL,     NIL,     NIL,     "Edit",  "What!", "Text:", ref,     
  119.    def,     not,     and,     or,      "<",     ">",     ",x)",   "),y",   
  120.    ",y",    ",x",    ")",     NIL,     NIL,     ",",     "#",     "a",     
  121.    "(",     "\"",    "$",     NIL,     NIL,     "no ",   "obj ",  "err ",  
  122.    "eject ","list ", "xref ", "mlist ","clist ","num "
  123. };
  124.  
  125.  
  126. #define tolower( c)  ((c) >= 'A' && (c) <= 'Z'  ? (c) + 32 : (c))
  127.  
  128. #if OS == TOS
  129. int   tossable;
  130. #endif
  131.  
  132. char  *currfile = "[none]";
  133. jmp_buf  errjmp;
  134.  
  135.  
  136. static char    tiny[8],
  137.                infile[ 256], 
  138.                outfile[256];
  139.  
  140. word           __x;
  141. byte huge      *p;
  142. long           len;
  143. int            fp,
  144.                forceit,
  145.                delete;
  146. FILE           *fq;
  147.  
  148.  
  149.  
  150. void nferror( s)
  151. char  *s;
  152. {
  153.    fprintf( stderr, "demac65: \"%s\" Fatal error - %s\n", currfile, s);
  154.    exit(1);
  155. }
  156.  
  157.  
  158. void nerror( s)
  159. char  *s;
  160. {
  161.    fprintf( stderr, "demac65: \"%s\" Error - %s\n", currfile, s);
  162.    Fclose( fp);
  163.    if( outfile[ 0])
  164.       fclose( fq);
  165.    longjmp( errjmp, 1);
  166. }
  167.  
  168. unsigned int   lines;
  169.  
  170. void nwarning( s)
  171. char  *s;
  172. {
  173.    fprintf( stderr, "demac65: \"%s\" ca. line %d, Warning - %s\n", 
  174.                currfile, lines + 1, s);
  175. }
  176.  
  177. void  complete( filename, ext, force)
  178. char  *filename, *ext;
  179. {
  180.    char  *x;
  181.  
  182.    if( x = strrchr( filename, '.'))
  183.    {
  184.       if( strchr( x, DIRSLASH) > x)
  185.          goto dis;
  186.       if( force)
  187.          while( *++x = *++ext);
  188.    }
  189.    else
  190. dis:
  191.       strcat( filename, ext);
  192. }
  193.  
  194.  
  195. int   chk_ext( filename, ext)
  196. char  *filename, *ext;
  197. {
  198.    char  *x;
  199.  
  200.    if( x = strrchr( filename, '.'))
  201.       if( strchr( x, DIRSLASH) <= x)
  202.          return( strcmp( x + 1, ext));
  203.    return( 1);
  204. }
  205.  
  206.  
  207. int   main( argc, argv)
  208. int    argc;
  209. char   *argv[];
  210. {
  211.    int          i = 0;
  212.  
  213.    fq = stdout;          /* done here for some bogus Amiga CC */
  214.    while( ++i < argc)
  215.    {
  216.       if( *argv[i] == '-')
  217.          switch( argv[i][1])
  218.          {
  219. #if OS == TOS
  220.             case 'T' :
  221.             case 't' :
  222.                tossable = ! tossable;
  223.                break;
  224. #endif
  225.             case 'D' :
  226.             case 'd' :
  227.                delete  = ! delete;
  228.                break;
  229.                
  230.             case 'C' :
  231.             case 'c' :
  232.                forceit = ! forceit;
  233.                break;
  234.                
  235. #ifdef __DATE__
  236.             case ':' :
  237.                fputs( __DATE__, stderr);
  238. # ifdef __TIME__
  239.                fprintf( stderr, " %s", __TIME__);
  240. # endif                              
  241.                putc( '\n', stderr);
  242.                break;
  243. #endif               
  244.             case 'V' :
  245.             case 'v' :
  246.                fprintf( stderr, "demac65 v1.5 (c) 1992 by Natuerlich! -- detokenizes MAC65 files\n");
  247.                break;
  248.          }
  249.       else
  250.          if( ! infile[0])
  251.             strcpy( infile, argv[i]);
  252.          else
  253.             if( ! outfile[0])
  254.                strcpy( outfile, argv[i]);
  255.             else
  256.                goto usage;
  257.    }
  258.    if( ! infile[0])
  259.       goto usage;
  260.  
  261.    if( ! setjmp( errjmp))
  262.    {
  263.       if( forceit)
  264.       {
  265.          strcpy( outfile, infile);
  266.          complete( outfile, ".s65", 1);
  267. #if OS == TOS
  268.          if( strchr( infile, '*'))        /* doesn't work well but works */
  269.          {
  270.             Fsetdta( (void *) &dir);
  271.             if( Fsfirst( infile, 0))
  272.                nferror( "No MaTcH");      /* waste user lifetime */
  273.             do
  274.                if( chk_ext( dir.name, "S65"))
  275.                {    
  276.                   strcpy( outfile, dir.name);
  277.                   complete( outfile, ".s65", 1);
  278.                   if( ! setjmp( errjmp))
  279.                      io( dir.name, outfile);
  280.               }
  281.             while( ! Fsnext());
  282.             return( 0);
  283.          }
  284. #endif
  285.       }
  286.       io( infile, outfile);
  287.       return( 0);
  288.    }
  289.    return( 1);
  290.  
  291. usage:
  292. #if OS == TOS
  293.    fprintf( stderr, "usage: demac65 [-{td}]{-c <filemask>|[-c] <infile> \
  294. [outfile]}\n");
  295.    fputs( 
  296. "\t-c <filemask> : convert 1 or more files, f.e. -c '*.*' converts all\n", 
  297.    stderr);
  298. #else
  299.    fprintf( stderr, "usage: demac65 [-{cd}] <infile> [outfile]\n");
  300. #endif
  301.    
  302.    fputs( "\
  303. \t-c <infile>   : convert <infile> to <infile>.S65\n\
  304. \t-d            : delete after conversion\n\
  305. \t-v            : version\n", stderr);
  306. #if OS == TOS
  307.    fputs( "\t-t            : wait for keypress before exiting\n", stderr);
  308.    keypress();
  309. #endif
  310.    return(1);
  311. }   
  312.  
  313. void  io( in, out)
  314. char  *in, *out;
  315. {
  316.    static char    buf[256];
  317.    
  318.    currfile = in;
  319.    if( (fp = (int) Fopen( in, 0)) < 0)
  320.    {
  321.       sprintf( buf, "Couldn't open source file \"%s\"", in);
  322.       nferror( buf);
  323.    }
  324.    if( Fread( fp, 4L, &header) < 4)
  325.       nerror("File trunctated");
  326.    if( header.fefe != 0xFEFE)
  327.       nerror("Not a MAC65 token file");
  328.    if( len = dpeek( &header.len))
  329.    {
  330.       if( ! (p = (byte huge *) Malloc( len)))
  331.          nferror("Out of memory");
  332.       if( Fread( fp, len, p) < len)
  333.          nerror("Source file truncated");
  334.       if( out[0] && ! (fq = fopen( out, "w")))
  335.       {
  336.          sprintf( buf, "Couldn't create output file \"%s\"", out);
  337.          nferror( buf);
  338.       }
  339.       convert();
  340.       if( delete && ! unlink( in))
  341.          fprintf( stderr, "Deleted \"%s\"\n", in);
  342.       if( out[0])
  343.          fclose( fq);
  344.    }
  345.    Fclose( fp);
  346. }
  347.  
  348. int   indent, 
  349.       pad, 
  350.       i, 
  351.       inquotes;
  352. byte  c, d;
  353.  
  354.  
  355. void convert()
  356. {
  357.    lines = 1;
  358.    indent = inquotes = 0;
  359.    while( len)
  360.    {
  361.       pad = 0;         /* start from left */
  362.       p  += 2;         /* ignore lineno   */
  363.  
  364.       i = (word) *p++ - 3;  /* line length */
  365.       len -= 3 + i;     /* minus lineno + linelength + linebyte */
  366.  
  367.       while( i)
  368.       {
  369.          i--;
  370.          c = *p & 0x7F;
  371.          if( c != *p++)
  372.          {
  373.             label( 0);
  374.             nput( ' ');
  375.             continue;
  376.          }
  377.       
  378.          switch( c)
  379.          {
  380.             case   0 :
  381.                nprint( table1[0]);     /* am i lazy... */
  382.                comment();
  383.                break;
  384.                
  385.             case   7 :
  386.                padto(MACROPAD);         
  387.                c = *p++ & 0x7F;
  388.                i--;
  389.                label(1);
  390.                nput(' ');
  391.                if( xconvert())
  392.                   goto salvage;
  393.                break;
  394.  
  395.             case  88 :
  396.                comment();
  397.                break;
  398.  
  399.             default  :
  400.                d = c;
  401.                if( d == 2 || d == 3 || d == 5) /* .ELSE .ENDIF .ENDM*/
  402.                   indent -= 3;
  403.                padto( INSTRPAD);
  404.                if( c >= TAB1SIZE)
  405.                {
  406.                   nerror("Got an totally unknown instr. token");
  407. salvage:
  408.                   p += i;
  409.                   i = 0;
  410.                   break;
  411.                }
  412.                else
  413.                   nprint( table1[ c]);
  414.                nput(' ');
  415.                if( xconvert())
  416.                   goto salvage;
  417.                if( d == 1 || d == 2 || d == 4)
  418.                   indent += 3;
  419.          } 
  420.       }
  421.       nput( '\n');
  422.       lines++;
  423.    }
  424. }
  425.  
  426. xconvert()
  427. {
  428.    inquotes = 0;
  429.    if( i)
  430.       padto( DATAPAD);
  431.    while( i)
  432.    {
  433.       i--;
  434.       c = *p & 0x7F;
  435.       if( c != *p++)
  436.       {
  437.          label( inquotes);
  438.          continue;
  439.       }
  440.       
  441.       switch( c)
  442.       {
  443.          case  1 :
  444.          case  3 :
  445.          case  5 :
  446.             sprintf( tiny, "$%X", ((word) p[1] << 8) | (word) *p);
  447.             p += 2; 
  448.             i -= 2; 
  449.             nprint( tiny); 
  450.             break;
  451.                
  452.          case  0 :
  453.          case  2 :
  454.          case  4 :
  455.          case  6 :
  456.             sprintf( tiny, "$%X", (word) *p++);
  457.             i--; 
  458.             nprint( tiny); 
  459.             break;
  460.                
  461.          case  7 :
  462.          case  9 :
  463.             sprintf( tiny, "%u", ((word) p[1] << 8) |  *p);
  464.             p += 2; 
  465.             i -= 2; 
  466.             nprint( tiny); 
  467.             break;
  468.             
  469.          case  8 :
  470.             sprintf( tiny, "%u", *p++);
  471.             i--; 
  472.             nprint( tiny); 
  473.             break;
  474.            
  475.          case 10 :
  476.             nput('\''); 
  477.             nput( (int) *p++); 
  478.             i--; 
  479.             break;
  480.    
  481.          case 59 :
  482.             padto( COMMENTPAD);
  483.             if( *p != ';')
  484.                nput( ';');
  485.             comment(); 
  486.             break;
  487.                            
  488.          case 65 :
  489.             inquotes = ! inquotes;
  490.             
  491.          default  :
  492.          {
  493.             if( c >= TAB2SIZE)
  494.             {
  495.                nerror("Got an undetokenizable token");
  496.                return( 1);
  497.             }
  498.             else
  499.             {
  500.                if( ! otable2[ c])
  501.                   nwarning("Unusual token");
  502.                nprint( table2[ c]);
  503.             }
  504.          }
  505.       } 
  506.    }
  507.    return( 0);
  508. }   
  509.  
  510. /* Vot = 0 lower case */
  511. void label( vot) 
  512.    char  v;
  513.    int   x = c; 
  514.  
  515.    i -= c;
  516.    while( x) 
  517.    {
  518.       x--;
  519.       v = *p++;
  520.       nput( (int) (vot ? v : tolower(v))); 
  521.    }
  522. }
  523.  
  524. void comment()
  525. {
  526.    while( i)
  527.    {
  528.       i--;
  529.       nput( (int) *p++);
  530.    }
  531. }
  532.  
  533.  
  534. void padto(to) 
  535. int  to; 
  536. {  
  537.    to += indent;
  538.    while( pad < to) 
  539.       nput(' ');    
  540. }
  541.    
  542.  
  543. void nput( c)
  544. int  c;
  545. {
  546.    putc( c, fq);
  547.    pad++;
  548. }
  549.  
  550.  
  551. void nprint( s)
  552. char  *s;
  553. {
  554.    int   i;
  555.    
  556.    if( s)
  557.    {
  558.       i = (int) strlen( s);
  559.    
  560.       if( fputs( s, fq))
  561.          nferror("Disk full ?");
  562.       pad += i;
  563.    }
  564.    else
  565.       nerror("Got an undetokenizable token");
  566. }
  567.  
  568. keypress()
  569. {
  570. #if OS == TOS
  571.    if( tossable)
  572.    {
  573.       while( Bconstat( 2))
  574.          Bconin( 2);
  575.       Bconin( 2);
  576.    }
  577. #endif
  578. }
  579.  
  580.