enhance.c revision 7c478bd95313f5f23a4c958a745db2134aa03244
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License, Version 1.0 only
* (the "License"). You may not use this file except in compliance
* with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2004 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#pragma ident "%Z%%M% %I% %E% SMI"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <signal.h>
#include <locale.h>
#include <unistd.h>
#include <termios.h>
#ifdef HAVE_SELECT
#ifdef HAVE_SYS_SELECT_H
#endif
#endif
#include <fcntl.h>
#include <dirent.h>
#if HAVE_SYSV_PTY
#include <stropts.h> /* System-V stream I/O */
#endif
#include "libtecla.h"
/*
* Pseudo-terminal devices are found in the following directory.
*/
#define PTY_DEV_DIR "/dev/"
/*
* Pseudo-terminal controller device file names start with the following
* prefix.
*/
#define PTY_CNTRL "pty"
/*
* Pseudo-terminal slave device file names start with the following
* prefix.
*/
#define PTY_SLAVE "tty"
/*
* Specify the maximum suffix length for the control and slave device
* names.
*/
#define PTY_MAX_SUFFIX 10
/*
* Set the maximum length of the master and slave terminal device filenames,
* including space for a terminating '\0'.
*/
+ PTY_MAX_SUFFIX + 1)
/*
* Set the maximum length of an input line.
*/
#define PTY_MAX_LINE 4096
/*
* Set the size of the buffer used for accumulating bytes written by the
* user's terminal to its stdout.
*/
#define PTY_MAX_READ 1000
/*
* Set the amount of memory used to record history.
*/
#define PTY_HIST_SIZE 10000
/*
* Set the timeout delay used to check for quickly arriving
* sequential output from the application.
*/
static GL_FD_EVENT_FN(pty_read_from_program);
static void pty_child_exited(int sig);
/*.......................................................................
* Run a program with enhanced terminal editing facilities.
*
* Usage:
* enhance program [args...]
*/
{
int status; /* The return statuses of the parent and child functions */
/* pseudo-terminal. */
char *prog; /* The name of the program (ie. argv[0]) */
/*
* Check the arguments.
*/
if(argc < 2) {
return 1;
};
/*
* Get the name of the program.
*/
/*
* If the user has the LC_CTYPE or LC_ALL environment variables set,
* enable display of characters corresponding to the specified locale.
*/
/*
* If the program is taking its input from a pipe or a file, or
* sending its output to something other than a terminal, run the
* program without tecla.
*/
_exit(1);
};
};
/*
* Open the master side of a pseudo-terminal pair, and return
* the corresponding file descriptor and the filename of the
* slave end of the pseudo-terminal.
*/
return 1;
/*
* Set up a signal handler to watch for the child process exiting.
*/
/*
* The above signal handler sends the parent process a SIGINT signal.
* This signal is caught by gl_get_line(), which resets the terminal
* settings, and if the application signal handler for this signal
* doesn't abort the process, gl_get_line() returns NULL with errno
* set to EINTR. Arrange to ignore the signal, so that gl_get_line()
* returns and we have a chance to cleanup.
*/
/*
* We will read user input in one process, and run the user's program
* in a child process.
*/
if(pid < 0) {
return 1;
};
/*
* Are we the parent?
*/
if(pid!=0) {
} else {
} else {
status = 1;
};
};
return status;
}
/*.......................................................................
* Open the master side of a pseudo-terminal pair, and return
* the corresponding file descriptor and the filename of the
* slave end of the pseudo-terminal.
*
* prog const char * The name of this program.
* cntrl int * The file descriptor of the pseudo-terminal
* controller device will be assigned tp *cntrl.
* slave_name char * The file-name of the pseudo-terminal slave device
* will be recorded in slave_name[], which must have
* at least PTY_MAX_NAME elements.
* Output:
* return int 0 - OK.
* 1 - Error.
*/
{
/*
* Mark the controller device as not opened yet.
*/
*cntrl = -1;
/*
* On systems with the Sys-V pseudo-terminal interface, we don't
* and if there is a free master terminal device, we are given a file
* descriptor connected to it.
*/
#if HAVE_SYSV_PTY
if(*cntrl >= 0) {
/*
* Get the filename of the slave side of the pseudo-terminal.
*/
if(name) {
return 1;
};
/*
* If unable to get the slave name, discard the controller file descriptor,
* ready to try a search instead.
*/
} else {
*cntrl = -1;
};
} else {
#endif
/*
* we open one master terminal after another, until one that isn't
* in use by another program is found.
*
* Open the devices directory.
*/
if(!dir) {
return 1;
};
/*
* Look for pseudo-terminal controller device files in the devices
* directory.
*/
/*
* Get the common extension of the control and slave filenames.
*/
continue;
/*
* Attempt to open the control file.
*/
if(*cntrl < 0)
continue;
/*
* Attempt to open the matching slave file.
*/
};
};
#if HAVE_SYSV_PTY
};
#endif
/*
* Did we fail to find a pseudo-terminal pair that we could open?
*/
if(*cntrl < 0) {
return 1;
};
/*
* System V systems require the program that opens the master to
* grant access to the slave side of the pseudo-terminal.
*/
#ifdef HAVE_SYSV_PTY
return 1;
};
#endif
/*
* Success.
*/
return 0;
}
/*.......................................................................
* Open the slave end of a pseudo-terminal.
*
* Input:
* prog const char * The name of this program.
* slave_name char * The filename of the slave device.
* Output:
* return int The file descriptor of the successfully opened
* slave device, or < 0 on error.
*/
{
int fd; /* The file descriptor of the slave device */
/*
* Place the process in its own process group. In system-V based
* OS's, this ensures that when the pseudo-terminal is opened, it
* becomes the controlling terminal of the process.
*/
if(setsid() < 0) {
return -1;
};
/*
* Attempt to open the specified device.
*/
if(fd < 0) {
return -1;
};
/*
* On system-V streams based systems, we need to push the stream modules
* that implement pseudo-terminal and termio interfaces. At least on
* Solaris, which pushes these automatically when a slave is opened,
* this is redundant, so ignore errors when pushing the modules.
*/
#if HAVE_SYSV_PTY
/*
* On BSD based systems other than SunOS 4.x, the following makes the
* pseudo-terminal the controlling terminal of the child process.
* According to the pseudo-terminal example code in Steven's
* Advanced programming in the unix environment, the !defined(CIBAUD)
* part of the clause prevents this from being used under SunOS. Since
* I only have his code with me, and won't have access to the book,
* I don't know why this is necessary.
*/
return -1;
};
#endif
return fd;
}
/*.......................................................................
* Read input from the controlling terminal of the program, using
* gl_get_line(), and feed it to the user's program running in a child
* process, via the controller side of the pseudo-terminal. Also pass
* data received from the user's program via the conroller end of
* the pseudo-terminal, to stdout.
*
* Input:
* prog const char * The name of this program.
* cntrl int The file descriptor of the controller end of the
* pseudo-terminal.
* Output:
* return int 0 - OK.
* 1 - Error.
*/
{
char *line; /* An input line read from the user */
/*
* Allocate the gl_get_line() resource object.
*/
if(!gl)
/*
* Allocate a buffer to use to accumulate bytes read from the
* pseudo-terminal.
*/
if(!rbuff)
rbuff[0] = '\0';
/*
* Register an event handler to watch for data appearing from the
* user's program on the controller end of the pseudo terminal.
*/
/*
* Read input lines from the user and pass them on to the user's program,
* by writing to the controller end of the pseudo-terminal.
*/
rbuff[0] = '\0';
};
}
/*.......................................................................
* This is a private return function of pty_parent(), used to release
* dynamically allocated resources, close the controller end of the
* pseudo-terminal, and wait for the child to exit. It returns the
* exit status of the child process, unless the caller reports an
* error itself, in which case the caller's error status is returned.
*
* Input:
* waserr int True if the caller is calling this function because
* an error occured.
* cntrl int The file descriptor of the controller end of the
* pseudo-terminal.
* gl GetLine * The resource object of gl_get_line().
* rbuff char * The buffer used to accumulate bytes read from
* the pseudo-terminal.
* Output:
* return int The desired exit status of the program.
*/
{
int status; /* The return status of the child process */
/*
* Close the controller end of the terminal.
*/
/*
* Delete the resource object.
*/
/*
* Delete the read buffer.
*/
if(rbuff)
/*
* Wait for the user's program to end.
*/
/*
* Return either our error status, or the return status of the child
* program.
*/
}
/*.......................................................................
* Run the user's program, with its stdin and stdout connected to the
* slave end of the psuedo-terminal.
*
* Input:
* prog const char * The name of this program.
* slave int The file descriptor of the slave end of the
* pseudo terminal.
* argv char *[] The argument vector to pass to the user's program,
* where argv[0] is the name of the user's program,
* and the last argument is followed by a pointer
* to NULL.
* Output:
* return int If this function returns at all, an error must
* have occured when trying to overlay the process
* with the user's program. In this case 1 is
* returned.
*/
{
/*
* We need to stop the pseudo-terminal from echoing everything that we send it.
*/
return 1;
};
return 1;
};
};
/*
* Arrange for stdin, stdout and stderr to be connected to the slave device,
* ignoring errors that imply that either stdin or stdout is closed.
*/
;
;
;
/*
* Run the user's program.
*/
_exit(1);
};
return 0; /* This should never be reached */
}
/*.......................................................................
* This is the event-handler that is called by gl_get_line() whenever
* there is tet waiting to be read from the user's program, via the
* controller end of the pseudo-terminal. See libtecla.h for details
* about its arguments.
*/
static GL_FD_EVENT_FN(pty_read_from_program)
{
char *nlptr; /* A pointer to the last newline in the accumulated string */
char *crptr; /* A pointer to the last '\r' in the accumulated string */
char *nextp; /* A pointer to the next unprocessed character */
/*
* Get the read buffer in which we are accumulating a line to be
* forwarded to stdout.
*/
/*
* New data may arrive while we are processing the current read, and
* it is more efficient to display this here than to keep returning to
* gl_get_line() and have it display the latest prefix as a prompt,
* followed by the current input line, so we loop, delaying a bit at
* the end of each iteration to check for more data arriving from
* the application, before finally returning to gl_get_line() when
* no more input is available.
*/
do {
/*
* Get the current length of the output string.
*/
/*
* Read the text from the program.
*/
if(nnew < 0)
return GLFD_ABORT;
/*
* Nul terminate the accumulated string.
*/
/*
* Find the last newline and last carriage return in the buffer, if any.
*/
/*
* We want to output up to just before the last newline or carriage
* return. If there are no newlines of carriage returns in the line,
* and the buffer is full, then we should output the whole line. In
* all cases a new output line will be started after the latest text
* has been output. The intention is to leave any incomplete line
* in the buffer, for (perhaps temporary) use as the current prompt.
*/
if(nlptr) {
} else if(crptr) {
} else if(len >= PTY_MAX_READ) {
} else {
};
/*
* Do we have any text to output yet?
*/
if(nextp) {
/*
* If there was already some text in rbuff before this function
* was called, then it will have been used as a prompt. Arrange
* to rewrite this prefix, plus the new suffix, by moving back to
* the start of the line.
*/
if(len > 0)
/*
* Write everything up to the last newline to stdout.
*/
/*
* Start a new line.
*/
/*
* Skip trailing carriage returns and newlines.
*/
nextp++;
/*
* Move any unwritten text following the newline, to the start of the
* buffer.
*/
};
/*
* Make the incomplete line in the output buffer the current prompt.
*/
return GLFD_REFRESH;
}
/*.......................................................................
* Write a given string to a specified file descriptor.
*
* Input:
* fd int The file descriptor to write to.
* string const char * The string to write (of at least 'n' characters).
* n int The number of characters to write.
* Output:
* return int 0 - OK.
* 1 - Error.
*/
{
int ndone = 0; /* The number of characters written so far */
/*
* Do as many writes as are needed to write the whole string.
*/
while(ndone < n) {
if(nnew > 0)
return 1;
};
return 0;
}
/*.......................................................................
* This is the signal handler that is called when the child process
* that is running the user's program exits for any reason. It closes
* the slave end of the terminal, so that gl_get_line() in the parent
* process sees an end of file.
*/
static void pty_child_exited(int sig)
{
}
/*.......................................................................
* Return non-zero after a given amount of time if there is data waiting
* to be read from a given file descriptor.
*
* Input:
* fd int The descriptor to watch.
* usec long The number of micro-seconds to wait for input to
* arrive before giving up.
* Output:
* return int 0 - No data is waiting to be read (or select isn't
* available).
* 1 - Data is waiting to be read.
*/
{
#if HAVE_SELECT
#else
return 0;
#endif
}