home *** CD-ROM | disk | FTP | other *** search
- /******************************************************************************
- *
- * xdbx - X Window System interface to dbx
- *
- * Copyright 1989 The University of Texas at Austin
- *
- * Author: Po Cheung
- * Date: March 10, 1989
- *
- * Permission to use, copy, modify, and distribute this software and
- * its documentation for any purpose and without fee is hereby granted,
- * provided that the above copyright notice appear in all copies and that
- * both that copyright notice and this permission notice appear in
- * supporting documentation. The University of Texas at Austin makes no
- * representations about the suitability of this software for any purpose.
- * It is provided "as is" without express or implied warranty.
- *
- ******************************************************************************/
-
-
- #include <sys/ioctl.h>
- #include "global.h"
-
- FILE *dbxfp = NULL; /* file pointer to dbx */
- int dbxpid; /* dbx process id */
- int dbxInputId; /* dbx input id */
-
- static char *pty = "/dev/pty??"; /* master side of pseudo-terminal */
- static char *tty = "/dev/tty??"; /* slave side of pseudo-terminal */
-
- /*
- * Xdbx talks to dbx through a pseudo terminal which is a pair of master
- * and slave devices: /dev/pty?? and /dev/tty??, where ?? goes from p0 to
- * qf (system dependent). The pty is opened for both read and write.
- */
- static int openMaster()
- {
- int i, master;
- char c;
-
- for (c='p'; c<='q'; c++) {
- pty[8] = c;
- for (i=0; i<16; i++) {
- pty[9] = "0123456789abcdef"[i];
- if ((master = open(pty, O_RDWR)) != -1)
- return (master);
- }
- }
- fprintf(stderr, "xdbx: all ptys in use: ptyp0 - ptyqf\n");
- exit(1);
- }
-
- static int openSlave()
- {
- int slave;
-
- tty[8] = pty[8];
- tty[9] = pty[9];
- if ((slave = open(tty, O_RDWR)) != -1) {
- return (slave);
- }
- else {
- fprintf(stderr, "open: cannot open slave pty %s", tty);
- exit(1);
- }
- }
-
- /* ARGSUSED */
- void calldbx(argc, argv)
- int argc;
- char *argv[];
- {
- int master; /* file descriptor of master pty */
- int slave; /* file descriptor to slave pty */
- int n = 0;
-
- master = openMaster();
- slave = openSlave();
-
- #ifndef BSD
- ioctl(master, TIOCPKT, &n); /* disable packet mode */
- #endif
-
- dbxpid = fork();
- if (dbxpid == -1) {
- perror("Cannot fork dbx process");
- exit(1);
- }
- else if (dbxpid) {
- /*
- * Parent : close the slave side of pty
- * close stdin and stdout
- * set the dbx file descriptor to nonblocking mode
- * open file pointer with read/write access to dbx
- * set unbuffered mode
- * register dbx input with X
- */
- close(slave);
- close(0);
- close(1);
- fcntl(master, F_SETFL, FNDELAY);
- dbxfp = fdopen(master, "r+");
- setbuf(dbxfp, NULL);
- dbxInputId = XtAddInput(master, XtInputReadMask, readDbx, NULL);
- }
- else {
- /*
- * Child : close master side of pty
- * redirect stdin, stdout, stderr of dbx to pty
- * unbuffer output data from dbx
- * exec dbx with arguments
- */
- close(master);
- dup2(slave, 0);
- dup2(slave, 1);
- dup2(slave, 2);
- if (slave > 2)
- close(slave);
- fcntl(1, F_SETFL, FAPPEND);
- setbuf(stdout, NULL);
- argv[0] = "dbx";
- execvp("dbx", argv);
- perror("Cannot call dbx");
- exit(1);
- }
- }
-