home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / vol_300 / 363_01 / main.c < prev    next >
C/C++ Source or Header  |  1991-09-24  |  7KB  |  241 lines

  1. /***********************************************************************
  2.  *
  3.  *      MAIN.C
  4.  *      Main Module for 68020 Assembler
  5.  *
  6.  *    Function: main()
  7.  *      Parses the command line, opens the input file and output files, and 
  8.  *      calls processFile() to perform the assembly, then closes all files.
  9.  *
  10.  *   Usage: main(argc, argv);
  11.  *      int argc;
  12.  *      char *argv[];
  13.  *
  14.  *      Author: Paul McKee
  15.  *      ECE492    North Carolina State University, 12/13/86
  16.  *
  17.  *      Modified A.E. Romer. Version 1.0
  18.  *          17 March 1991:  ANSI functions, braces layout.
  19.  *          26 April 1991:  Default extension for input files added, VMS
  20.  *                          compatibility removed.
  21.  *
  22.  ************************************************************************/
  23.  
  24.  
  25. #include <stdio.h>
  26. #include <ctype.h>
  27. #include "asm.h"
  28.  
  29.  
  30. #define     FNAMSIZE        13          /* name = 8, dot = 1, ext = 3,
  31.                                          * plus terminating '\0' */
  32. #define     DEFAULT_IFX     ".ASM"      /* default input-file extension */
  33. #define     DEFAULT_OFX     ".H68"      /* default output-file extension */
  34. #define     DEFAULT_LFX     ".LIS"      /* default listing-file extension */
  35.  
  36. extern FILE *inFile;                                          /* Input file */
  37. extern FILE *listFile;                                      /* Listing file */
  38. extern FILE *objFile;                                        /* Object file */
  39. extern char line[256];                                       /* Source line */
  40. extern int errorCount, warningCount;       /* Number of errors and warnings */
  41.  
  42.  
  43. extern char listFlag;   /* True if a listing is desired */
  44. extern char objFlag;    /* True if an object code file is desired */
  45. extern char xrefFlag;   /* True if a cross-reference is desired */
  46. extern char cexFlag;    /* True is Constants are to be EXpanded */
  47. /**********************************************************************
  48.  *
  49.  *  Function getopt() scans the command line arguments passed
  50.  *  via argc and argv for options of the form "-x". It returns
  51.  *  the letters of the options, one per call, until all the
  52.  *  options have been returned; it then returns EOF. The argi
  53.  *  argument is set to the number of the argv string that
  54.  *  is currently being examined.
  55.  *
  56.  *********************************************************************/
  57.  
  58.  
  59. int getopt(int argc, char *argv[], char *optstring, int *argi)
  60.     {
  61.     static char *scan = NULL;   /* Scan pointer */
  62.     static int optind = 0;      /* argv index */
  63.  
  64.     char c;
  65.     char *place;
  66.  
  67.     if (scan == NULL || *scan == '\0')
  68.         {
  69.         if (optind == 0)
  70.             optind++;
  71.  
  72.         if (optind >= argc || argv[optind][0] != '-' ||
  73.                                                     argv[optind][1] == '\0')
  74.             {
  75.             *argi = optind;
  76.             return(EOF);
  77.             }
  78.         if (strcmp(argv[optind], "--")==0)
  79.             {
  80.             optind++;
  81.             *argi = optind;
  82.             return(EOF);
  83.             }
  84.  
  85.         scan = argv[optind]+1;
  86.         optind++;
  87.         }
  88.  
  89.     c = *scan++;
  90.     place = strchr(optstring, c);
  91.  
  92.     if (place == NULL || c == ':')
  93.         {
  94.         fprintf(stderr, "Unknown option -%c\n", c);
  95.         *argi = optind;
  96.         return('?');
  97.         }
  98.  
  99.     place++;
  100.     if (*place == ':')
  101.         {
  102.         if (*scan != '\0')
  103.             scan = NULL;
  104.         else
  105.             optind++;
  106.         }
  107.  
  108.     *argi = optind;
  109.     return c;
  110.     }
  111.  
  112.  
  113. /**********************************************************************
  114.  *
  115.  *  Function help() prints out a usage explanation if a bad
  116.  *  option is specified or no filename is given.
  117.  *
  118.  *********************************************************************/
  119.  
  120. int help()
  121.     {
  122.     puts("Usage: asm [-cln] infile[.ext]\nor:    asm -h\nor:    asm ?\n");
  123.     puts("Options: -c  Show full target code for lomg instructions");
  124.     puts("         -l  Produce listing file (infile.lis)");
  125.     puts("         -n  Produce NO object file (infile.h68)");
  126.     puts("         -h  Print (this) help message");
  127.     puts("          ?  Print (this) help message");
  128.     exit(0);
  129.     return NORMAL;
  130.     }
  131.  
  132.  
  133. int setFlags(int argc, char *argv[], int *argi)
  134.     {
  135.     int option;
  136.  
  137.     while ((option = getopt(argc, argv, "chln", argi)) != EOF)
  138.         {
  139.         switch (option)
  140.             {
  141.             case 'c' : cexFlag = TRUE; break;
  142.             case 'h' : help(); break;
  143.             case 'l' : listFlag = TRUE; break;
  144.             case 'n' : objFlag = FALSE; break;
  145.             case '?' : help(); break;
  146.             }
  147.         }
  148.  
  149.     return NORMAL;
  150.     }
  151.  
  152.  
  153. int main(int argc, char *argv[])
  154.     {
  155.     char fileName[FNAMSIZE], outName[FNAMSIZE], *p;
  156.     int i, j;
  157.  
  158.     puts("68020 Assembler by AER\n");
  159.     setFlags(argc, argv, &i);
  160.     /* Check whether a name was specified */
  161.     if (i >= argc)
  162.         {
  163.         fputs("No input file specified\n\n", stderr);
  164.         help();
  165.         }
  166.     if ((strcmp("?", argv[i])) == 0)
  167.         help();
  168.  
  169.     for (j = 0; j < FNAMSIZE; j++)
  170.         {
  171.         *(fileName + j) = *(argv[i] + j);
  172.         if (*(argv[i] + j) == '\0')
  173.             break;
  174.         }
  175.  
  176.     if (strchr(fileName, '.'))
  177.         ;                           /* do nothing if extension specified */
  178.     else
  179.         strcpy((fileName + strlen(fileName)), DEFAULT_IFX);
  180.                                     /* append default input-file extension */
  181.     inFile = fopen(fileName, "r");
  182.     if (!inFile)
  183.         {
  184.         fprintf(stderr, "Input file \"%s\" not found\n", fileName);
  185.         exit(0);
  186.         }
  187.  
  188.     /* Process output file names in their own buffer */
  189.     strcpy(outName, fileName);
  190.  
  191.     /* Replace extension in output file names */
  192.     p = strchr(outName, '.');
  193.  
  194.     if (listFlag)
  195.         {
  196.         strcpy(p, DEFAULT_LFX); /* append listing-file extension */
  197.         initList(outName);
  198.         }
  199.  
  200.     if (objFlag)
  201.         {
  202.         strcpy(p, DEFAULT_OFX); /* append output-file extension */
  203.         initObj(outName);
  204.         }
  205.  
  206.     /* Assemble the file */
  207.     processFile();
  208.  
  209.     /* Close files and print error and warning counts */
  210.     fclose(inFile);
  211.     if (listFlag)
  212.         {
  213.         putc('\n', listFile);
  214.         if (errorCount > 0)
  215.             fprintf(listFile, "%d error%s detected\n", errorCount,
  216.                                             (errorCount > 1) ? "s" : "");
  217.         else
  218.             fprintf(listFile, "No errors detected\n");
  219.         if (warningCount > 0)
  220.             fprintf(listFile, "%d warning%s generated\n", warningCount,
  221.                 (warningCount > 1) ? "s" : "");
  222.         else
  223.             fprintf(listFile, "No warnings generated\n");
  224.         fclose(listFile);
  225.         }
  226.     if (objFlag)
  227.         finishObj();
  228.     if (errorCount > 0)
  229.         fprintf(stderr, "%d error%s detected\n", errorCount,
  230.                                                 (errorCount > 1) ? "s" : "");
  231.     else
  232.         fprintf(stderr, "No errors detected\n");
  233.     if (warningCount > 0)
  234.         fprintf(stderr, "%d warning%s generated\n", warningCount,
  235.             (warningCount > 1) ? "s" : "");
  236.     else
  237.         fprintf(stderr, "No warnings generated\n");
  238.  
  239.     return NORMAL;
  240.     }
  241.