home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 351.lha / ontrap / umain.c < prev    next >
C/C++ Source or Header  |  1990-02-28  |  4KB  |  155 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.  
  17. #define isspace(c)      ((c == ' ')||(c == '\t') || (c == '\n'))
  18.  
  19. #ifndef TINY
  20. extern int _fmode,_iomode;
  21. extern int (*_ONBREAK)();
  22. extern int CXBRK();
  23. /*JVP*/
  24. extern int (*_ONTRAP)();
  25. extern int CXTRAP();
  26. #endif
  27.  
  28. extern struct UFB _ufbs[];
  29. int argc;                       /* arg count */
  30. char **targv, *argv[MAXARG];     /* arg pointers */
  31.  
  32. #define MAXWINDOW 40
  33. extern struct WBStartup *WBenchMsg;
  34.  
  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.  
  63. /*
  64. *
  65. * Build argument pointer list
  66. *
  67. */
  68. while (argc < MAXARG)
  69.         {
  70.         while (isspace(*line))  line++;
  71.         if (*line == '\0')      break;
  72.         pargv = &argv[argc++];
  73.         if (*line == QUOTE)
  74.                 {
  75.                 *pargv = ++line;  /* ptr inside quoted string */
  76.                 while ((*line != '\0') && (*line != QUOTE)) line++;
  77.                 if (*line == '\0')  _exit(1);
  78.                 else                *line++ = '\0';  /* terminate arg */
  79.                 }
  80.         else            /* non-quoted arg */
  81.                 {       
  82.                 *pargv = line;
  83.                 while ((*line != '\0') && (!isspace(*line))) line++;
  84.                 if (*line == '\0')  break;
  85.                 else                *line++ = '\0';  /* terminate arg */
  86.                 }
  87.         }  /* while */
  88. targv = (argc == 0) ? (char **)WBenchMsg : (char **)&argv[0];
  89.  
  90.  
  91. /*
  92. *
  93. * Open standard files
  94. *
  95. */
  96. #ifndef TINY
  97.  
  98. if (argc == 0)          /* running under workbench      */
  99.         {
  100.         strcpy(window, "con:10/10/320/80/");
  101.         strncat(window, WBenchMsg->sm_ArgList->wa_Name,MAXWINDOW);
  102.         _ufbs[0].ufbfh = Open(window,MODE_NEWFILE);
  103.         _ufbs[1].ufbfh = _ufbs[0].ufbfh;
  104.         _ufbs[1].ufbflg = UFB_NC;
  105.         _ufbs[2].ufbfh = _ufbs[0].ufbfh;
  106.         _ufbs[2].ufbflg = UFB_NC;
  107.         handle = (struct FileHandle *)(_ufbs[0].ufbfh << 2);
  108.         process = (struct Process *)FindTask(0);
  109.         process->pr_ConsoleTask = (APTR)handle->fh_Type;
  110.         x = 0;
  111.         }
  112. else                    /* running under CLI            */
  113.         {
  114.         _ufbs[0].ufbfh = Input();
  115.         _ufbs[1].ufbfh = Output();
  116.         _ufbs[2].ufbfh = Open("*", MODE_OLDFILE);
  117.         x = UFB_NC;                     /* do not close CLI defaults    */
  118.         }
  119.  
  120. _ufbs[0].ufbflg |= UFB_RA | O_RAW | x;
  121. _ufbs[1].ufbflg |= UFB_WA | O_RAW | x;
  122. _ufbs[2].ufbflg |= UFB_RA | UFB_WA | O_RAW;
  123.  
  124. x = (_fmode) ? 0 : _IOXLAT;
  125. stdin->_file = 0;
  126. stdin->_flag = _IOREAD | x;
  127. stdout->_file = 1;
  128. stdout->_flag = _IOWRT | x;
  129. stderr->_file = 2;
  130. stderr->_flag = _IORW | x;
  131.  
  132. /*      establish control-c handler */
  133.  
  134. _ONBREAK = CXBRK;
  135.  
  136. /*JVP   establish trap handler */
  137.  
  138. _ONTRAP = CXTRAP;
  139.  
  140. #endif
  141.  
  142. /*
  143. *
  144. * Call user's main program
  145. *
  146. */
  147.  
  148. main(argc,targv);              /* call main function */
  149. #ifndef TINY
  150. exit(0);
  151. #else
  152. _exit(0);
  153. #endif
  154. }
  155.