home *** CD-ROM | disk | FTP | other *** search
/ Amiga ISO Collection / AmigaUtilCD2.iso / Programming / C / SASC6571.LZX / source / _main.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-12-24  |  4.1 KB  |  162 lines

  1. /***
  2. *
  3. *          Copyright © 1992 SAS Institute, Inc.
  4. *
  5. * name             __main - process command line, open files, and call main()
  6. *
  7. * synopsis         __main(line);
  8. *                  char *line;     ptr to command line that caused execution
  9. *
  10. * description      This function performs the standard pre-processing for
  11. *                  the main module of a C program.  It accepts a command
  12. *                  line of the form
  13. *
  14. *                       pgmname arg1 arg2 ...
  15. *
  16. *                  and builds a list of pointers to each argument.  The first
  17. *                  pointer is to the program name.  For some environments, the
  18. *                  standard I/O files are also opened, using file names that
  19. *                  were set up by the OS interface module XCMAIN.
  20. *
  21. ***/
  22.  
  23. #include <stdio.h>
  24. #include <string.h>
  25. #include <stdlib.h>
  26. #include <constructor.h>
  27. #include <workbench/startup.h>
  28. #include <libraries/dos.h>
  29. #include <libraries/dosextens.h>
  30. #include <proto/dos.h>
  31. #include <proto/exec.h>
  32. #include <exec/memory.h>
  33.  
  34. #define QUOTE       '"'
  35. #define ESCAPE '*'
  36. #define ESC '\x1b'
  37. #define NL '\n'
  38.  
  39. #define isspace(c)      ((c == ' ')||(c == '\t') || (c == '\n'))
  40.  
  41. extern char __stdiowin[];
  42. extern char __stdiov37[];
  43.  
  44. extern struct WBStartup *_WBenchMsg;
  45. int main(int, void *);
  46.  
  47. static int argc;                   /* arg count */
  48. static char **targv, **argv;       /* arg pointers */
  49.  
  50. void __stdargs __main(line)
  51.     char *line;
  52. {
  53.     char *argbuf;
  54.     int ret;
  55.     int i;
  56.  
  57. /***
  58. *     First count the number of arguments
  59. ***/
  60.    argbuf = line;
  61.    for (argc = 0; ; argc++)
  62.    {
  63.         while (isspace(*line))  line++;
  64.         if (*line == '\0')      break;
  65.         if (*line == QUOTE)
  66.         {
  67.             line++;
  68.             while (*line != QUOTE && *line != 0)
  69.             {
  70.                if (*line == ESCAPE)
  71.                {
  72.                   line++;
  73.                   if (*line == 0) break;
  74.                }
  75.                line++;
  76.             }
  77.             if (*line) line++;
  78.         }
  79.         else            /* non-quoted arg */
  80.         {       
  81.             while ((*line != '\0') && (!isspace(*line))) line++;
  82.             if (*line == '\0')  break;
  83.         }
  84.    }
  85.  
  86.    if (argc)
  87.    {
  88.       argv = AllocMem((argc+1) * sizeof(char *), MEMF_CLEAR);
  89.       if (argv == NULL)
  90.          exit(20);
  91.          
  92.       /***
  93.       *     Build argument pointer list
  94.       ***/
  95.       i = 0;
  96.       line = argbuf;
  97.       while (1)
  98.       {
  99.            while (isspace(*line))  line++;
  100.            if (*line == '\0')      break;
  101.            if (*line == QUOTE)
  102.            {
  103.                argbuf = argv[i++] = ++line;  /* ptr inside quoted string */
  104.                while (*line != QUOTE && *line != 0)
  105.                {
  106.                   if (*line == ESCAPE)
  107.                   {
  108.                      line++;
  109.                      switch (*line)
  110.                      {
  111.                         case '\0':
  112.                            *argbuf = 0;
  113.                            goto linedone;
  114.                         case 'E':
  115.                            *argbuf++ = ESC;
  116.                            break;
  117.                         case 'N':
  118.                            *argbuf++ = NL;
  119.                            break;
  120.                         default:
  121.                            *argbuf++ = *line;
  122.                      }
  123.                      line++;
  124.                   }
  125.                   else
  126.                   {
  127.                     *argbuf++ = *line++;
  128.                   }
  129.                }
  130.                if (*line) line++;
  131.                *argbuf++ = '\0'; /* terminate arg */
  132.            }
  133.            else            /* non-quoted arg */
  134.            {       
  135.                argv[i++] = line;
  136.                while ((*line != '\0') && (!isspace(*line))) line++;
  137.                if (*line == '\0')  break;
  138.                else                *line++ = '\0';  /* terminate arg */
  139.            }
  140.       }  /* while */
  141.    }
  142. linedone:
  143.  
  144.     targv = (argc == 0) ? (char **) _WBenchMsg : (char **) &argv[0];
  145.  
  146.  
  147. /***
  148. *     Call user's main program
  149. ***/
  150.  
  151.     ret = main(argc, targv);                /* call main function */
  152.  
  153.     exit(ret);
  154. }
  155.  
  156.  
  157. MEMCLEANUP_DESTRUCTOR(argcleanup)
  158. {
  159.     if (argc && argv)
  160.        FreeMem(argv, (argc+1) * sizeof(char *));
  161. }
  162.