home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / x / volume3 / xdbx / part03 / calldbx.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-03-14  |  3.2 KB  |  127 lines

  1. /******************************************************************************
  2.  *
  3.  *  xdbx - X Window System interface to dbx
  4.  *
  5.  *  Copyright 1989 The University of Texas at Austin
  6.  *
  7.  *  Author:    Po Cheung
  8.  *  Date:    March 10, 1989
  9.  *
  10.  *  Permission to use, copy, modify, and distribute this software and
  11.  *  its documentation for any purpose and without fee is hereby granted,
  12.  *  provided that the above copyright notice appear in all copies and that
  13.  *  both that copyright notice and this permission notice appear in
  14.  *  supporting documentation.  The University of Texas at Austin makes no 
  15.  *  representations about the suitability of this software for any purpose.  
  16.  *  It is provided "as is" without express or implied warranty.
  17.  *
  18.  ******************************************************************************/
  19.  
  20.  
  21. #include    <sys/ioctl.h>
  22. #include    "global.h"
  23.  
  24. FILE               *dbxfp = NULL;        /* file pointer to dbx */
  25. int                dbxpid;            /* dbx process id */
  26. int                dbxInputId;        /* dbx input id */
  27.  
  28. static char     *pty = "/dev/pty??";    /* master side of pseudo-terminal */
  29. static char     *tty = "/dev/tty??";    /* slave side of pseudo-terminal */
  30.  
  31. /*
  32.  *  Xdbx talks to dbx through a pseudo terminal which is a pair of master
  33.  *  and slave devices: /dev/pty?? and /dev/tty??, where ?? goes from p0 to
  34.  *  qf (system dependent).  The pty is opened for both read and write.
  35.  */
  36. static int openMaster()
  37. {
  38.     int  i, master; 
  39.     char c;
  40.  
  41.     for (c='p'; c<='q'; c++) {
  42.     pty[8] = c;
  43.     for (i=0; i<16; i++) {
  44.         pty[9] = "0123456789abcdef"[i];
  45.         if ((master = open(pty, O_RDWR)) != -1) 
  46.         return (master); 
  47.     }
  48.     }
  49.     fprintf(stderr, "xdbx: all ptys in use: ptyp0 - ptyqf\n");
  50.     exit(1);
  51. }
  52.  
  53. static int openSlave()
  54. {
  55.     int slave;
  56.  
  57.     tty[8] = pty[8];
  58.     tty[9] = pty[9];
  59.     if ((slave = open(tty, O_RDWR)) != -1) {
  60.     return (slave);
  61.     }
  62.     else {
  63.     fprintf(stderr, "open: cannot open slave pty %s", tty);
  64.         exit(1);
  65.     }
  66. }
  67.  
  68. /* ARGSUSED */
  69. void calldbx(argc, argv)
  70. int argc;
  71. char *argv[];
  72. {
  73.     int  master;    /* file descriptor of master pty */
  74.     int  slave;     /* file descriptor to slave pty */
  75.     int  n = 0;
  76.  
  77.     master = openMaster();
  78.     slave = openSlave();
  79.  
  80. #ifndef BSD
  81.     ioctl(master, TIOCPKT, &n);        /* disable packet mode */
  82. #endif
  83.  
  84.     dbxpid = fork();
  85.     if (dbxpid == -1) {
  86.     perror("Cannot fork dbx process");
  87.     exit(1);
  88.     }
  89.     else if (dbxpid) { 
  90.     /* 
  91.      * Parent : close the slave side of pty
  92.      *        close stdin and stdout
  93.      *        set the dbx file descriptor to nonblocking mode
  94.      *        open file pointer with read/write access to dbx
  95.      *        set unbuffered mode
  96.      *        register dbx input with X
  97.      */
  98.     close(slave);
  99.     close(0);
  100.     close(1);
  101.     fcntl(master, F_SETFL, FNDELAY);
  102.         dbxfp = fdopen(master, "r+");
  103.     setbuf(dbxfp, NULL);
  104.     dbxInputId = XtAddInput(master, XtInputReadMask, readDbx, NULL);
  105.     }
  106.     else { 
  107.     /* 
  108.      * Child : close master side of pty
  109.      *        redirect stdin, stdout, stderr of dbx to pty
  110.      *       unbuffer output data from dbx
  111.      *       exec dbx with arguments
  112.      */
  113.     close(master);
  114.     dup2(slave, 0);
  115.     dup2(slave, 1);
  116.     dup2(slave, 2);
  117.     if (slave > 2)
  118.         close(slave);
  119.     fcntl(1, F_SETFL, FAPPEND);
  120.     setbuf(stdout, NULL);
  121.     argv[0] = "dbx";
  122.     execvp("dbx", argv);
  123.     perror("Cannot call dbx");
  124.     exit(1);
  125.     }
  126. }
  127.