home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 5 Edit / 05-Edit.zip / elvis184.zip / src / amiga.c < prev    next >
C/C++ Source or Header  |  1992-05-08  |  5KB  |  235 lines

  1. /* amiga.c */
  2.  
  3. /*-
  4.  *    Mike Rieser                 Dale Rahn
  5.  *    2410 Happy Hollow Rd. Apt D-10        540 Vine St.
  6.  *    West Lafayette, IN 47906         West Lafayette, IN 47906
  7.  *    riesermc@mentor.cc.purdue.edu        rahn@sage.cc.purdue.edu
  8.  */
  9.  
  10. #include <string.h>
  11. #include <fcntl.h>
  12. #include <exec/memory.h>
  13. #include <dos/dostags.h>    /* for system() and rpipe() */
  14. #include <dos/var.h>
  15. #include <clib/exec_protos.h>
  16. #include <clib/dos_protos.h>
  17.  
  18. #if AZTEC_C
  19. #include <pragmas/exec_lib.h>
  20. #include <pragmas/dos_lib.h>
  21. #else
  22. #include <pragmas/exec.h>
  23. #include <pragmas/dos.h>
  24. #endif
  25.  
  26. #include "config.h"
  27.  
  28. static struct TagItem systags[]=
  29. {
  30.     {SYS_Input, 0L},
  31.     {SYS_Output, 0L},
  32.     {SYS_UserShell, TRUE},
  33.     {SYS_Asynch, TRUE},
  34.     {TAG_DONE}
  35. };
  36.  
  37. #define ENVSIZE 1028
  38. #define FILE_SEP ' '
  39.  
  40. #ifdef LATTICE
  41. /* INDENT OFF */
  42. int  CXBRK(void) { return 0; }        /* Disable Lattice CTRL/C handling */
  43. int  chkabort(void) { return 0; }
  44. #elif AZTEC_C
  45. long  Chk_Abort(void) { return 0; }    /* Disable Aztec CTRL/C handling */
  46. /* INDENT ON */
  47. #endif
  48.  
  49.  
  50. /*
  51.  * getpid() - This is only used to make tmpfiles.
  52.  */
  53. int
  54. getpid()
  55. {
  56.     return (int) FindTask(0L);
  57. }
  58.  
  59.  
  60. /*
  61.  * rpipe() - gets a `cmd' to run and a `file descriptor' to use as its stdin.
  62.  */
  63. int
  64. rpipe(UBYTE * cmd, int fd)
  65. {
  66.     BPTR         fdFH = 0,    /* FileHandle of passed file descriptor */
  67.                  outPH = 0,    /* Pipe (File) Handle for child to write to. */
  68.                  inPH = 0,    /* Pipe (File) Handle for child to read from. */
  69.                  lock = 0;
  70.     int          fdr = 0;
  71.     char         pname[32], *pc;
  72.     extern char  o_shell[];
  73.  
  74.     if (isOldDOS())
  75.     return -1;
  76.  
  77.     /*-
  78.      * Sorry, I'm playing with an AZTEC internal here:
  79.      * _devtab[fd].fd is Aztec's FileHandle for the file descriptor `fd'.
  80.      * 
  81.      * HINT: For your compiler, look in your compiler's fcntl.h.
  82.      */
  83.  
  84.     switch (fd)
  85.     {
  86.     case 0:
  87.     inPH = Open((UBYTE *) "*", MODE_READWRITE);
  88.     break;
  89.     default:
  90.  
  91. #ifdef    AZTEC_C
  92.     fdFH = _devtab[fd].fd;        /* Grab FileHandle from fd */
  93. #elif    _DCC
  94.     fdFH = fdtofh(fd);        /* DCC does it right! */
  95. #else
  96.     return -1;            /* Sorry, can't help you. */
  97. #endif
  98.  
  99.     /*
  100.      * Get a FileHandle to use for the child's stdin.
  101.      * The only reason we Dup is because we'll run the child ASynch,
  102.      * and it will close its Input and Output on exit.
  103.      */
  104.     lock = DupLockFromFH(fdFH);
  105.     if (!lock)
  106.         return -1;
  107.     inPH = OpenFromLock(lock);
  108.     }
  109.  
  110.     if (!inPH)
  111.     {
  112.     if (lock)
  113.         UnLock(lock);
  114.     return -1;
  115.     }
  116.  
  117.     /*
  118.      * Get a pipe to use for the child's stdout, which we will read from.
  119.      */
  120.     strcpy(pname, "PIPE:ElvisXXX.XXX");
  121.     pc = mktemp(pname);            /* Get a unique PIPE: */
  122.     if (!*pc)
  123.     {
  124.         Close(inPH);
  125.     return -1;            /* Failure. */
  126.     }
  127.  
  128.     /*
  129.      * Get a FileHandle to use for the child's stdout.
  130.      */
  131.     if ((BPTR) 0 == (outPH = Open((UBYTE *) pc, MODE_NEWFILE)))
  132.     {
  133.     Close(inPH);
  134.     return -1;            /* Failure. */
  135.     }
  136.  
  137.     /* Get a file descriptor to return to the calling function */
  138.     if ((fdr = open(pc, O_RDONLY)) < 0)
  139.     {
  140.     Close(inPH);
  141.     Close(outPH);
  142.     return -1;            /* Failure. */
  143.     }
  144.  
  145.     /* exec the cmd */
  146.     systags[0].ti_Data = inPH;        /* Input FileHandle for child */
  147.     systags[1].ti_Data = outPH;        /* Output FileHandle for child */
  148.     systags[2].ti_Data = (long) o_shell;/* which shell to use */
  149.  
  150.     if (System((UBYTE *) cmd, systags))
  151.     {
  152.     close(fdr);
  153.     return -1;            /* Failure. */
  154.     }
  155.  
  156.     return fdr;                /* Success! */
  157. }
  158.  
  159.  
  160. /*
  161.  * This is supposed to wait till the child process is done. Unfortunately, I'm
  162.  * not sure how to do that.
  163.  */
  164. int
  165. rpclose(int fd)
  166. {
  167.     close(fd);
  168.     return 0;
  169. }
  170.  
  171.  
  172. int
  173. sleep(unsigned seconds)
  174. {
  175.     if (seconds) 
  176.      Delay(seconds * TICKS_PER_SECOND);
  177.     return 0;
  178. }
  179.  
  180.  
  181. int
  182. system(UBYTE * command)
  183. {
  184.     return (isOldDOS()) ? -1 : System((UBYTE *) command, TAG_DONE);
  185. }
  186.  
  187.  
  188. #ifdef AZTEC_C
  189.  
  190. /*
  191.  * Aztec's library getenv() doesn't allow for Environment variables larger than
  192.  * 256 bytes.  It also doesn't check the local environment, which would make
  193.  * elvis running on an AUX: port more useful.
  194.  */
  195. char        *
  196. getenv(char *var)
  197. {
  198.     static char *buf;
  199.  
  200.     buf = (char *) malloc(sizeof(*buf) * (ENVSIZE + 1));
  201.     if ((char *) 0 == buf)
  202.     {
  203.     return 0;
  204.     }
  205.     if (isOldDOS())
  206.     {
  207.     int          bytes;
  208.     BPTR         fh;
  209.  
  210.     strcpy(buf, "env:");
  211.     strcat(buf, var);
  212.     fh = Open((UBYTE *) buf, MODE_OLDFILE);
  213.     if ((BPTR) 0 == fh)
  214.     {
  215.         _free_(buf);
  216.         return (char *) 0;        /* return null for not defined */
  217.     }
  218.     bytes = Read(fh, (UBYTE *) buf, ENVSIZE);
  219.     Close(fh);
  220.     if (bytes == -1)
  221.     {
  222.         _free_(buf);
  223.         return (char *) 0;        /* return null for not defined */
  224.     }
  225.     buf[bytes] = '\000';
  226.     } else if (-1 == GetVar((UBYTE *) var, (UBYTE *) buf, ENVSIZE, GVF_BINARY_VAR))
  227.     {                    /* no varible defined, free memory */
  228.     _free_(buf);
  229.     return (char *) 0;        /* return null for not defined */
  230.     }
  231.     return (char *) buf;
  232. }
  233.  
  234. #endif
  235.