static char *rcsid_Xttylib_c
= "$Header: Xttylib.c,v 10.7 86/04/22 15:18:10 jg Rel $";
/* This version has a single reverse-video argument instead of colors. It
really only works well on monochrome displays */
/* Library of routines to create a terminal emulator window
* Routines in this library are:
* CreateTTYWindow Creates a new instance of a terminal window
* DestroyTTYWindow Destroys a terminal window
* TTYPutString Puts a string in a terminal window
* TTYPutChar Puts a character in a terminal window
* TTYPrintf Does a printf in a terminal window
* TTYGetString Gets s string from a terminal window
* TTYGetChar Gets a char from a terminal window
* SetStdout Flushes stdout and assigns it to a window
* ResetStdout Resets stdout to its inital value
* The terminal window created responds to exactly the same character
* sequences as xterm (not surprising). Creating a window automatically
* These routines pass around a pointer to a TTYWindow:
* typedef struct _TTYWindow {
* Window w; The window id
* int pid; The pid of the subprocess xterm
* short file; The file id of the tty to read/write characters to/from
* The SetStdout routine is highly useful in conjunction with curses
* since curses always writes to stdout.
TTYWindow
*CreateTTYWindow(cols
, lines
, x
, y
, normFont
, boldFont
, bwidth
,
int lines
, cols
, x
, y
, bwidth
, reverse
;
char *normFont
, *boldFont
;
if ((t
= (TTYWindow
*) malloc(sizeof(TTYWindow
))) ==
if (Start_slave_xterm(t
, lines
, cols
, x
, y
, normFont
, boldFont
,
fprintf(stderr
, "Couldn't start slave xterm\n");
int Start_slave_xterm(t
, lines
, cols
, x
, y
, normFont
, boldFont
, bwidth
,
int lines
, cols
, x
, y
, bwidth
, reverse
;
char *normFont
, *boldFont
;
char Sbuf
[BUFSIZE
], sizeBuf
[BUFSIZE
], wdBuf
[BUFSIZE
],
if (boldFont
== NULL
) boldFont
= normFont
;
ttyMasterPty
= GetPty(ttyName
);
if (ttyMasterPty
== -1) return 0;
if ((t
->pid
= vfork()) < 0) return 0;
sprintf(Sbuf
, "-S%c%c%d", ttyName
[8], ttyName
[9], ttyMasterPty
);
sprintf(sizeBuf
, "=%dx%d+%d+%d", cols
, lines
, x
, y
);
sprintf(wdBuf
, "%d", bwidth
);
execlp("xterm", "xterm", Sbuf
, "-fn", normFont
, "-fb", boldFont
,
sizeBuf
, "-bw", wdBuf
, reverse
? "-rv" : (char *) 0,
if (!keepMasterOpen
) close(ttyMasterPty
);
/* Open the slave end of the pty */
ttyName
[5] = 't'; /* Change /dev/pty?? to /dev/tty?? */
t
->file
= open(ttyName
, O_RDWR
, 0777);
/* Couldn't open the tty--better get rid of the process */
/* Read the windowid from the pty */
while ((bytesRead
= read(t
->file
, inputBuffer
+ len
,
sizeof(Window
) - len
)) > 0) len
+= bytesRead
;
/* Flush the rest of the garbahge */
ioctl(t
->file
, TIOCFLUSH
, (struct sgttyb
*) NULL
);
/* the data consists of a binary window ID */
t
->w
= *(Window
*) inputBuffer
;
register int devindex
, letter
;
strcpy(name
, "/dev/ptyp0");
for (letter
= 0; letter
< 4; letter
++) {
name
[8] = "pqrs"[letter
];
for (devindex
= 0; devindex
< 16; devindex
++) {
name
[9] = "0123456789abcdef"[devindex
];
if ((fd
= open (name
, O_RDWR
)) >= 0) return fd
;
/* close the tty; this should cause the xterm to terminate with an I/O error */
write(t
->file
, str
, strlen(str
));
TTYPrintf(t
, format
, args
)
char buffer
[TTY_BUFSIZE
+1];
_strbuf
._flag
= _IOWRT
+_IOSTRG
;
_strbuf
._cnt
= TTY_BUFSIZE
;
_doprnt(format
, &args
, &_strbuf
);
_strbuf
._cnt
++; /* Be sure there's room for the \0 */
static initial_stdout
= -1;
if (initial_stdout
== -1) initial_stdout
= stdout
->_file
;
stdout
->_file
= initial_stdout
;
if (read(t
->file
, &c
, 1) > 0)
char *TTYGetString(t
, str
, n
)
while (--n
> 0 && read(t
->file
, cs
, 1) > 0) {
if (*cs
++ == '\n') break;
if (cs
== str
) return NULL
;