home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 213a.lha / Sozobon-C / jas / main.c.orig < prev    next >
Text File  |  1996-02-14  |  3KB  |  179 lines

  1.  
  2. /*
  3.  * Copyright (c) 1988 by Sozobon, Limited.  Author: Joseph M Treat
  4.  *
  5.  * Permission is granted to anyone to use this software for any purpose
  6.  * on any computer system, and to redistribute it freely, with the
  7.  * following restrictions:
  8.  * 1) No charge may be made other than reasonable charges for reproduction.
  9.  * 2) Modified versions must be clearly marked as such.
  10.  * 3) The authors are not responsible for any harmful consequences
  11.  *    of using this software, even if they result from defects in it.
  12.  */
  13.  
  14. /*
  15.  * Version 1.1 contains fixes for referencing the usp, sr and ccr registers
  16.  * and not exiting if the -V switch is specified. format specifications for 
  17.  * move sr, ?? and usp moves were incorrect -- fixed. added aliasing for 
  18.  * immediate operations to sr and ccr.
  19.  */
  20.  
  21. #include "jas.h"
  22.  
  23. #define VERSION    1
  24. #define RELEASE 1
  25.  
  26. jmp_buf err_buf;
  27.  
  28. extern int sawerror; 
  29.  
  30. char *ofile = (char *) 0;
  31. char *ifile = (char *) 0;
  32. int Optimize = 1;
  33. int Lflag = 0;
  34. extern FILE *yyin;
  35.  
  36. main(argc,argv)
  37.     int argc;
  38.     char *argv[];
  39. {
  40.     extern FILE *freopen();
  41.  
  42.     if ( setjmp( err_buf ) ) {
  43.         unlink( ofile );
  44.         exit( 1 );
  45.     }
  46.  
  47.     setflags( argc, argv );
  48.  
  49.     if ( freopen( ifile, "r", stdin ) == (FILE *) NULL )
  50.         error( 0, "can't open source file for reading" );
  51.  
  52.     if ( freopen( ofile, "bw", stdout ) == (FILE *) NULL )
  53.         error( 0, "can't open object file for writing" );
  54.  
  55.     yyin = stdin;
  56.  
  57.     aspass1();
  58.  
  59.     if ( sawerror )
  60.         unlink( ofile );
  61.  
  62.     exit( sawerror );
  63. }
  64.  
  65. setflags(ac,av)
  66.     int ac;
  67.     char *av[];
  68. {
  69.     int errflag = 0, i;
  70.     int Vflag = 0;
  71.  
  72.     for ( i = 1; i < ac; i++ ) {
  73.         if ( *av[i] == '-' ) {
  74.             switch ( av[i][1] ) {
  75.             case 'o':
  76.                 ofile = av[++i];
  77.                 break;
  78.             case 'N':
  79.                 Optimize = 0;
  80.                 break;
  81.             case 'V':
  82.                 Vflag = 1;
  83.                 break;
  84.             case 'L':
  85.                 Lflag = 1;
  86.                 if ( av[i][2] )
  87.                     Lflag = av[i][2] - '0';
  88.                 break;
  89.             case 's':
  90.                 i++;
  91.             case 'l':
  92.             case 'u':
  93.                 break;
  94.             default:
  95.                 errflag = 1;
  96.                 break;
  97.             }
  98.         } else if (! ifile ) {
  99.             ifile = av[i];
  100.         } else {
  101.             errflag = 1;
  102.         }
  103.     }
  104.  
  105.     if ( Vflag ) {
  106.         fprintf( stderr, "Sozobon Assembler, Version %d.%d\n",
  107.                             VERSION, RELEASE );
  108.         fprintf( stderr, "Copyright (c) 1988 by Sozobon, Limited\n" );
  109.     }
  110.  
  111.     if (! ifile)
  112.         errflag = 1;
  113.  
  114.     if ( errflag ) {
  115.         fprintf( stderr, "usage: as [-N] source [-o object]\n" );
  116.         exit( 1 );
  117.     }
  118.  
  119.     if (! ofile ) {
  120.         char buf[32];
  121.         char *ip, *op;
  122.  
  123.         for ( op = buf, ip = ifile; *op++ = *ip; ip++ ) {
  124.             if ( *ip == '/' )
  125.                 op = buf;
  126.         }
  127.  
  128.         if ( op[-2] == 's' && op[-3] == '.' ) {
  129.             op[-2] = 'o';
  130.         } else {
  131.             fprintf(stderr, "usage: as [-N] source [-o object]\n" );
  132.             exit( 1 );
  133.         }
  134.         ofile = STRCPY(buf);
  135.     }
  136. }
  137.  
  138. error(n, s)
  139.     int n;
  140.     char *s;
  141. {
  142.     warn( n, s );
  143.     longjmp( err_buf, 1 );
  144. }
  145.  
  146. warn(n, s)
  147.     int n;
  148.     char *s;
  149. {
  150.     fprintf(stderr,"jas: ");
  151.     if ( n )
  152.         fprintf(stderr,"line %d: ", n);
  153.     fprintf(stderr,"( %s )\n", s);
  154.     sawerror = 1;
  155. }
  156.  
  157. char *
  158. allocate(size)
  159.     unsigned size;
  160. {
  161.     register char *loc;
  162.     extern char *calloc();
  163.  
  164.     loc = calloc( size, 1 );
  165.     if ( loc ) {
  166.         return loc;
  167.     }
  168.     error( 0, "out of memory" );
  169.     return (char *) NULL;
  170. }
  171.  
  172. output( buffer, size, nitems )
  173.     char *buffer;
  174.     int size, nitems; 
  175. {
  176.     if ( fwrite( buffer, size, nitems, stdout ) != nitems )
  177.         error( 0, "trouble writing object file" );
  178. }
  179.