home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / INFO / CROSSASM / 68ASMSIM.ZIP / asmsrc / main.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-04-09  |  6.4 KB  |  288 lines

  1. /***********************************************************************
  2.  *
  3.  *        MAIN.C
  4.  *        Main Module for 68000 Assembler
  5.  *
  6.  *    Function: main()
  7.  *        Parses the command line, opens the input file and
  8.  *        output files, and calls processFile() to perform the
  9.  *        assembly, then closes all files. 
  10.  *
  11.  *     Usage: main(argc, argv);
  12.  *        int argc;
  13.  *        char *argv[];
  14.  *
  15.  *      Author: Paul McKee
  16.  *        ECE492    North Carolina State University
  17.  *
  18.  *        Date:    12/13/86
  19.  *
  20.  ************************************************************************/
  21.  
  22.  
  23. #include <stdio.h>
  24. #include <ctype.h>
  25. #include "asm.h"
  26.  
  27.  
  28. extern FILE *inFile;    /* Input file */
  29. extern FILE *listFile;    /* Listing file */
  30. extern FILE *objFile;    /* Object file */
  31. extern char line[256];    /* Source line */
  32. extern int errorCount, warningCount;    /* Number of errors and warnings */
  33.  
  34.  
  35. extern char listFlag;    /* True if a listing is desired */
  36. extern char objFlag;    /* True if an object code file is desired */
  37. extern char xrefFlag;    /* True if a cross-reference is desired */
  38. extern char cexFlag;    /* True is Constants are to be EXpanded */
  39.  
  40.  
  41. int    main(argc, argv)
  42. int argc;
  43. char *argv[];
  44. {
  45. char fileName[64], outName[64], *p;
  46. int i;
  47.  
  48.     puts("68000 Assembler by PGM\n");
  49.     setFlags(argc, argv, &i);
  50.     /* Check whether a name was specified */
  51.     if (i >= argc) {
  52.         fputs("No input file specified\n\n", stderr);
  53.         help();
  54.         }
  55.     if (!strcmp("?", argv[i]))
  56.         help();
  57.  
  58.  
  59.     strcpy(fileName, argv[i]);
  60.     inFile = fopen(fileName, "r");
  61.     if (!inFile) {
  62.         fputs("Input file not found\n", stderr);
  63.         exit(0);
  64.         }
  65.  
  66.     /****************************************************************
  67.      *                                *
  68.      *    NOTE: The following filename manipulations are        *
  69.      *    intended for VMS filenames only. Other file naming    *
  70.      *    conventions may require changes here.            *
  71.      *                                *
  72.      *    VMS filenames have the form                *
  73.      *                                *
  74.      *        node::device:[dir1.dir2.dir3]filename.ext;vers    *
  75.      *                                *
  76.      *    For use here, we want the listing and object files    *
  77.      *    to be the most recent version in the default        *
  78.      *    directory, so we strip off the node, device, and    *
  79.      *    directory names, extension, and version number, then    *
  80.      *    append the appropriate file extension (.LIS or .H68)    *
  81.      *    for each file.                         *
  82.      *                                *
  83.      ****************************************************************/
  84.  
  85.     /* Process output file names in their own buffer */
  86.     strcpy(outName, fileName);
  87.  
  88.     /* Delete version number from output file names */
  89.     p = strchr(outName, ';');
  90.     if (p)
  91.         *p = '\0';
  92.  
  93.     /* Remove directory name from output file names */
  94.     p = outName + strlen(outName);
  95.     while (p >= outName && *p != ':' && *p != ']')
  96.         p--;
  97.     if (p >= outName)
  98.         strcpy(outName, ++p);
  99.     
  100.     /* Change extension to .LIS */
  101.     p = strchr(outName, '.');
  102.     if (!p)
  103.         p = outName + strlen(outName);
  104.  
  105.     if (listFlag) {
  106.         strcpy(p, ".LIS");
  107.         initList(outName);
  108.         }
  109.  
  110.     if (objFlag) {
  111.         strcpy(p, ".H68");
  112.         initObj(outName);
  113.     }
  114.  
  115.     /* Assemble the file */
  116.     processFile();
  117.  
  118.     /* Close files and print error and warning counts */
  119.     fclose(inFile);
  120.     if (listFlag) {
  121.         putc('\n', listFile);
  122.         if (errorCount > 0)
  123.             fprintf(listFile, "%d error%s detected\n", errorCount,
  124.                 (errorCount > 1) ? "s" : "");
  125.         else
  126.             fprintf(listFile, "No errors detected\n");
  127.         if (warningCount > 0)
  128.             fprintf(listFile, "%d warning%s generated\n", warningCount,
  129.                 (warningCount > 1) ? "s" : "");
  130.         else
  131.             fprintf(listFile, "No warnings generated\n");
  132.         fclose(listFile);
  133.         }
  134.     if (objFlag)
  135.         finishObj();
  136.     if (errorCount > 0)
  137.         fprintf(stderr, "%d error%s detected\n", errorCount,
  138.             (errorCount > 1) ? "s" : "");
  139.     else
  140.         fprintf(stderr, "No errors detected\n");
  141.     if (warningCount > 0)
  142.         fprintf(stderr, "%d warning%s generated\n", warningCount,
  143.             (warningCount > 1) ? "s" : "");
  144.     else
  145.         fprintf(stderr, "No warnings generated\n");
  146.  
  147.     return NORMAL;
  148.  
  149. }
  150.  
  151.  
  152. int    strcap(d, s)
  153. char *d, *s;
  154. {
  155. char capFlag;
  156.  
  157.     capFlag = TRUE;
  158.     while (*s) {
  159.         if (capFlag)
  160.             *d = toupper(*s);
  161.         else
  162.             *d = *s;
  163.         if (*s == '\'')
  164.             capFlag = (char) !capFlag;
  165.         d++;
  166.         s++;
  167.         }
  168.     *d = '\0';
  169.  
  170.     return NORMAL;
  171.  
  172. }
  173.  
  174.  
  175. char *skipSpace(p)
  176. char *p;
  177. {
  178.     while (isspace(*p))
  179.         p++;
  180.     return p;
  181. }
  182.  
  183.  
  184. int    setFlags(argc, argv, argi)
  185. int argc;
  186. char *argv[];
  187. int *argi;
  188. {
  189. int option;
  190.  
  191.     while ((option = getopt(argc, argv, "cln", argi)) != EOF) {
  192.         switch (option) {
  193.             case 'c' : cexFlag = TRUE; break;
  194.             case 'h' : help(); break;
  195.             case 'l' : listFlag = TRUE; break;
  196.             case 'n' : objFlag = FALSE; break;
  197.             case '?' : help(); break;
  198.             }
  199.         }
  200.  
  201.     return NORMAL;
  202.  
  203. }
  204.  
  205.  
  206. /**********************************************************************
  207.  *
  208.  *    Function getopt() scans the command line arguments passed
  209.  *    via argc and argv for options of the form "-x". It returns
  210.  *    the letters of the options, one per call, until all the
  211.  *    options have been returned; it then returns EOF. The argi
  212.  *    argument is set to the number of the argv string that
  213.  *    is currently being examined.
  214.  *
  215.  *********************************************************************/
  216.  
  217. int getopt(argc, argv, optstring, argi)
  218. int argc;
  219. char *argv[];
  220. char *optstring;
  221. int *argi;
  222. {
  223. static char *scan = NULL;    /* Scan pointer */
  224. static int optind = 0;        /* argv index */
  225.  
  226. char c;
  227. char *place;
  228. char *strchr();
  229.  
  230.     if (scan == NULL || *scan == '\0') {
  231.         if (optind == 0)
  232.             optind++;
  233.  
  234.         if (optind >= argc || argv[optind][0] != '-' || argv[optind][1] == '\0') {
  235.             *argi = optind;
  236.             return(EOF);
  237.             }
  238.         if (strcmp(argv[optind], "--")==0) {
  239.             optind++;
  240.             *argi = optind;
  241.             return(EOF);
  242.         }
  243.  
  244.         scan = argv[optind]+1;
  245.         optind++;
  246.     }
  247.  
  248.     c = *scan++;
  249.     place = strchr(optstring, c);
  250.  
  251.     if (place == NULL || c == ':') {
  252.         fprintf(stderr, "Unknown option -%c\n", c);
  253.         *argi = optind;
  254.         return('?');
  255.     }
  256.  
  257.     place++;
  258.     if (*place == ':') {
  259.         if (*scan != '\0')
  260.             scan = NULL;
  261.         else
  262.             optind++;
  263.     }
  264.  
  265.     *argi = optind;
  266.     return c;
  267. }
  268.  
  269.  
  270. /**********************************************************************
  271.  *
  272.  *    Function help() prints out a usage explanation if a bad
  273.  *    option is specified or no filename is given.
  274.  *
  275.  *********************************************************************/
  276.  
  277. int    help()
  278. {
  279.     puts("Usage: asm [-cln] infile.ext\n");
  280.     puts("Options: -c  Show full constant expansions for DC directives");
  281.     puts("         -l  Produce listing file (infile.lis)");
  282.     puts("         -n  Produce NO object file (infile.h68)");
  283.     exit(0);
  284.     return NORMAL;
  285. }
  286.  
  287.  
  288.