home *** CD-ROM | disk | FTP | other *** search
/ AMIGA PD 1 / AMIGA-PD-1.iso / Programme_zum_Heft / Programmieren / Kurztests / Barfly / umain.c < prev    next >
C/C++ Source or Header  |  1991-12-17  |  4KB  |  175 lines

  1. /*      _main.c         Copyright (C) 1985  Lattice, Inc.       */
  2.  
  3. #include <stdio.h>
  4. #include <fcntl.h>
  5. #include <ios1.h>
  6. #include <string.h>
  7. #include <stdlib.h>
  8. #include <workbench/startup.h>
  9. #include <libraries/dos.h>
  10. #include <libraries/dosextens.h>
  11. #include <proto/dos.h>
  12. #include <proto/exec.h>
  13.  
  14. #define MAXARG 32              /* maximum command line arguments */
  15. #define QUOTE  '"'
  16. #define ESCAPE '*'
  17. #define ESC '\027'
  18. #define NL '\n'
  19.  
  20. #define isspace(c)      ((c == ' ')||(c == '\t') || (c == '\n'))
  21.  
  22. #ifndef TINY
  23. extern int _fmode,_iomode;
  24. extern int (*_ONBREAK)();
  25. extern int CXBRK();
  26. #endif
  27.  
  28. extern struct UFB _ufbs[];
  29. static int argc;                       /* arg count */
  30. static char **targv, *argv[MAXARG];     /* arg pointers */
  31. static void badarg(char *program);
  32.  
  33. #define MAXWINDOW 40
  34. extern struct WBStartup *WBenchMsg;
  35. /**
  36. *
  37. * name         _main - process command line, open files, and call "main"
  38. *
  39. * synopsis     _main(line);
  40. *              char *line;     ptr to command line that caused execution
  41. *
  42. * description   This function performs the standard pre-processing for
  43. *               the main module of a C program.  It accepts a command
  44. *               line of the form
  45. *
  46. *                       pgmname arg1 arg2 ...
  47. *
  48. *               and builds a list of pointers to each argument.  The first
  49. *               pointer is to the program name.  For some environments, the
  50. *               standard I/O files are also opened, using file names that
  51. *               were set up by the OS interface module XCMAIN.
  52. *
  53. **/
  54. void _main(line)
  55. register char *line;
  56. {
  57.    register char **pargv;
  58.    register int x;
  59.    struct Process *process;
  60.    struct FileHandle *handle;
  61.    static char window[MAXWINDOW+18];
  62.     char *argbuf;
  63.  
  64. /*
  65. *
  66. * Build argument pointer list
  67. *
  68. */
  69.    
  70.    while (argc < MAXARG)
  71.    {
  72.         while (isspace(*line))  line++;
  73.         if (*line == '\0')      break;
  74.         pargv = &argv[argc++];
  75.         if (*line == QUOTE)
  76.         {
  77.             argbuf = *pargv = ++line;  /* ptr inside quoted string */
  78.                 while (*line != QUOTE)
  79.                 {
  80.                    if (*line == ESCAPE)
  81.                     {
  82.                           line++;
  83.                         switch (*line)
  84.                         {
  85.                             case 'E':
  86.                                 *argbuf++ = ESC;
  87.                                 break;
  88.                             case 'N':
  89.                                 *argbuf++ = NL;
  90.                                 break;
  91.                             default:
  92.                                 *argbuf++ = *line;
  93.                         }
  94.                         line++;
  95.                     }
  96.                    else
  97.                    {
  98.                      *argbuf++ = *line++;
  99.                    }
  100.               }
  101.               line++;
  102.               *argbuf++ = '\0';    /* terminate arg */
  103.         }
  104.         else            /* non-quoted arg */
  105.         {       
  106.             *pargv = line;
  107.             while ((*line != '\0') && (!isspace(*line))) line++;
  108.             if (*line == '\0')  break;
  109.             else                *line++ = '\0';  /* terminate arg */
  110.         }
  111.    }  /* while */
  112.    targv = (argc == 0) ? (char **)WBenchMsg : (char **)&argv[0];
  113.  
  114.  
  115. /*
  116. *
  117. * Open standard files
  118. *
  119. */
  120. #ifndef TINY
  121.  
  122.    if (argc == 0)          /* running under workbench      */
  123.    {
  124.         strcpy(window, "con:10/10/320/80/");
  125.         strncat(window, WBenchMsg->sm_ArgList->wa_Name,MAXWINDOW);
  126.         _ufbs[0].ufbfh = Open(window,MODE_NEWFILE);
  127.         _ufbs[1].ufbfh = _ufbs[0].ufbfh;
  128.         _ufbs[1].ufbflg = UFB_NC;
  129.         _ufbs[2].ufbfh = _ufbs[0].ufbfh;
  130.         _ufbs[2].ufbflg = UFB_NC;
  131.         handle = (struct FileHandle *)(_ufbs[0].ufbfh << 2);
  132.         process = (struct Process *)FindTask(0);
  133.         process->pr_ConsoleTask = (APTR)handle->fh_Type;
  134.         x = 0;
  135.    }
  136.    else                    /* running under CLI            */
  137.    {
  138.         _ufbs[0].ufbfh = Input();
  139.         _ufbs[1].ufbfh = Output();
  140.         _ufbs[2].ufbfh = Open("*", MODE_OLDFILE);
  141.         x = UFB_NC;                     /* do not close CLI defaults    */
  142.    }
  143.  
  144.    _ufbs[0].ufbflg |= UFB_RA | O_RAW | x;
  145.    _ufbs[1].ufbflg |= UFB_WA | O_RAW | x;
  146.    _ufbs[2].ufbflg |= UFB_RA | UFB_WA | O_RAW;
  147.  
  148.    x = (_fmode) ? 0 : _IOXLAT;
  149.    stdin->_file = 0;
  150.    stdin->_flag = _IOREAD | x;   
  151.    stdout->_file = 1;
  152.    stdout->_flag = _IOWRT | x;
  153.    stderr->_file = 2;
  154.    stderr->_flag = _IORW | x;
  155.  
  156. /*      establish control-c handler */
  157.  
  158. _ONBREAK = CXBRK;
  159.  
  160. #endif
  161.  
  162. /*
  163. *
  164. * Call user's main program
  165. *
  166. */
  167.  
  168.    main(argc,targv);              /* call main function */
  169. #ifndef TINY
  170.    exit(0);
  171. #else
  172. _  exit(0);
  173. #endif
  174. }
  175.