.fp 5 CW
SHELL 3 "28 Feb 2003" AST
NAME
shell - a \f5ksh library interface
SYNOPSIS
#include <shell.h>
libshell.a -lshell
"DATA TYPES"
Shell_t;
Shopt_t;
Shscope_t;
Shbltin_t;
Shbltin_f;
Shinit_f;
Shwait_f;
"FUNCTIONS"
int sh_main(int argc, char *argv[], Sh_init fn);
Shell_t *sh_init(int argc, char *argv[]);
Shell_t *sh_getinterp(void);

Namval_t *sh_addbuiltin((Shell_t *shp,const char *name,Sh_bltin_f fn,void *arg);

unsigned int sh_isoption((Shell_t *shp,int option);
unsigned int sh_onoption((Shell_t *shp,int option);
unsigned int sh_offoption((Shell_t *shp,int option);

void *sh_parse(Shell_t *shp, Sfio_t *sp, int flags);
int sh_trap((Shell_t *shp,const char *string, int mode);
int sh_run((Shell_t *shp,int argc, char *argv[]);
int sh_eval(Shell_t *shp,Sfio_t *shp,int mode);
int sh_fun((Shell_t *shp,Namval_t *funnode, Namval_t *varnode, char *argv[]);
int sh_funscope((Shell_t *shp,int argc,char *argv[],int(*fn)(void*),void *arg,int flags);
Shscope_t *sh_getscope((Shell_t *shp,int index,int whence);
Shscope_t *sh_setscope((Shell_t *shp, Shscope_t *scope);

int (*sh_fdnotify(int(*fn)(int,int)))(int,int);
char *sh_fmtq(const char *string);
void *sh_waitnotify(Shwait_f fn);
void sh_delay(double sec);
Sfio_t *sh_iogetiop(int fd, int mode);
int sh_sigcheck(void);
DESCRIPTION
The Shell library is a set of functions used for writing extensions to \f5ksh or writing commands that embed shell command processing. The include file \f5<shell.h> contains the type definitions, function prototypes and symbolic constants defined by this interface. It also defines replacement definitions for the standard library functions \f5access(), \f5close(), \f5dup(), \f5exit(), \f5fcntl(), \f5lseek(), \f5open(), \f5pipe(), \f5read(), and \f5write() that must be used with all code intended to be compiled as built-in commands.

The \f5<shell.h> header includes \f5<ast.h> which in turn includes the standard include files, \f5<stddef.h>, \f5<stdlib.h>, \f5<stdarg.h>, \f5<limits.h>, \f5<stdio.h>, \f5<string.h>, \f5<unistd.h>, \f5<sys/types.h>, \f5<fcntl.h>, and \f5<locale.h>. The \f5<shell.h> header also includes the headers \f5<cdt.h>, \f5<cmd.h>, \f5<sfio.h>, \f5<nval.h>, \f5<stk.h>, and \f5<error.h> so that in most cases, programs only require the single header \f5<shell.h>.

Programs can use this library in one of the following ways:

0

1 To write builtin commands and/or other code that will be loaded into the shell by loading dynamic libraries at run time using the \f5builtin(1) command. In this case the shell will look for a function named \f5lib_init in your library and, if found, will execute this function with two arguments. The first argument will be an \f5int with value \f50 when the library is loaded. The second argument will contain a pointer to a structure of type \f5Shbltin_t. In addition, for each argument named on the \f5builtin command line, it will look for a function named \f5b_name\f5() in your library and will name as a built-in.

2 To build separate a separate command that uses the shell as a library at compile or run time. In this case the \f5sh_init() function must be called to initialize this library before any other commands in this library are invoked. The arguments argc and argv are the number of arguments and the vector of arguments as supplied by the shell. It returns a pointer the \f5Shell_t.

3 To build a new version of \f5ksh with extended capabilities, for example \f5tksh(1). In this case, the user writes a \f5main() function that calls \f5sh_main() with the argc and argv arguments from \f5main and pointer to function, fn as a third argument.. The function fn will be invoked with argument \f50 after \f5ksh has done initialization, but before \f5ksh has processed any start up files or executed any commands. The function fn will be invoked with an argument of \f51 before \f5ksh begins to execute a script that has been invoked by name since \f5ksh cleans up memory and long jumps back to the beginning of the shell in this case. The function fn will be called with argument \f5-1 before the shell exits.

The \f5Shell_t structure contains the following fields:

 Shopt_t options; /* set -o options */
 Dt_t *var_tree; /* shell variable dictionary */
 Dt_t *fun_tree; /* shell function dictionary */
 Dt_t *alias_tree; /* shell alias dictionary */
 Dt_t *bltin_tree; /* shell built-in dictionary */
 Shscope_t *topscope; /* pointer to top-level scope */
 char *infile_name; /* path name of current input file*/
 int inlineno; /* line number of current input file*/
 int exitval; /* most recent exit value*/
This structure is returned by \f5sh_init() but can also be retrieved by a call to \f5sh_getinterp().

All built-in commands to the shell are invoked with three arguments. The first two arguments give the number of arguments and the argument list and uses the same conventions as the \f5main() function of a program. The third argument is a pointer to a structure of type \f5Shbltin_t. This structure contains \f5shp which is a pointer to the shell interpreter, and \f5ptr which is a pointer that can be associated with each built-in. The \f5sh_addbuiltin() function is used to add, replace or delete built-in commands. It takes the name of the built-in, name, a pointer to the function that implements the built-in, fn, and a pointer that will be passed to the function in the \f5ptr field when it is invoked. If, fn is non-\f5NULL the built-in command is added or replaced. Otherwise, \f5sh_addbuiltin() will return a pointer to the built-in if it exists or \f5NULL otherwise. If arg is \f5(void*)1 the built-in will be deleted. The name argument can be in the format of a pathname. It cannot be the name of any of the special built-in commands. If name contains a \f5/, the built-in is the basename of the pathname and the built-in will only be executed if the given pathname is encountered when performing a path search. When adding or replacing a built-in, \f5sh_addbuiltin() function returns a pointer to the name-value pair corresponding to the built-in on success and \f5NULL if it is unable to add or replace the built-in. When deleting a built-in, \f5NULL is returned on success or if not found, and the name-value pair pointer is returned if the built-in cannot be deleted.

The functions \f5sh_onoption(), \f5sh_offoption(), \f5sh_isoption() are used to set, unset, and test for shell options respectively. The option argument can be any one of the following:

\f5SH_ALLEXPORT: The \f5NV_EXPORT attribute is given to each variable whose name is an identifier when a value is assigned.
\f5SH_BGNICE: Each background process is run at a lower priority.
\f5SH_ERREXIT: Causes a non-interactive shell to exit when a command, other than a conditional command, returns non-zero.
\f5SH_EMACS: The emacs editing mode.
\f5SH_GMACS: Same as the emacs editing mode except for the behavior of CONTROL-T.
\f5SH_HISTORY: Indicates that the history file has been created and that commands can be logged.
\f5SH_IGNOREEOF: Do not treat end-of-file as exit.
\f5SH_INTERACTIVE:
Set for interactive shells. Do not set or unset this option. \f5SH_MARKDIRS: A / is added to the end of each directory generated by pathname expansion.
\f5SH_MONITOR: Indicates that the monitor option is enabled for job control.
\f5SH_NOCLOBBER: The > redirection will fail if the file exists. Each file created with > will have the \f5O_EXCL bit set as described in \f5<fcntl.h>
\f5SH_NOGLOB: Do not perform pathname expansion.
\f5SH_NOLOG: Do not save function definitions in the history file.
\f5SH_NOTIFY: Cause a message to be generated as soon as each background job completes.
\f5SH_NOUNSET: Cause the shell to fail with an error of an unset variable is referenced.
\f5SH_PRIVILEGED:
\f5SH_VERBOSE: Cause each line to be echoed as it is read by the parser.
\f5SH_XTRACE: Cause each command to be displayed after all expansions, but before execution.
\f5SH_VI: The vi edit mode.
\f5SH_VIRAW: Read character at a time rather than line at a time when in vi edit mode.

The \f5sh_trap() function can be used to compile and execute a string or file. A value of \f50 for mode indicates that name refers to a string. A value of \f51 for mode indicates that name is an \f5Sfio_t* to an open stream. A value of \f52 for mode indicates that name points to a parse tree that has been returned by \f5sh_parse(). The complete file associated with the string or file is compiled and then executed so that aliases defined within the string or file will not take effect until the next command is executed.

The \f5sh_run() function will run the command given by by the argument list argv containing argc elements. If argv\f5[0] does not contain a \f5/, it will be checked to see if it is a built-in or function before performing a path search.

The \f5sh_eval() function executes a string or file stream sp. If mode is non-zero and the history file has been created, the stream defined by sp will be appended to the history file as a command.

The \f5sh_parse() function takes a pointer to the shell interpreter shp, a pointer to a string or file stream sp, and compilation flags, and returns a pointer to a parse tree of the compiled stream. This pointer can be used in subsequent calls to \f5sh_trap(). The compilation flags can be zero or more of the following:

\f5SH_NL: Treat new-lines as ;.
\f5SH_EOF: An end of file causes syntax error. By default it will be treated as a new-line.

\f5ksh executes each function defined with the \f5function reserved word in a separate scope. The \f5Shscope_t type provides an interface to some of the information that is available on each scope. The structure contains the following public members:

 \f5Sh_scope_t *par_scope;
 \f5int argc;
 \f5char **argv;
 \f5char *cmdname;
 \f5Dt_t *var_tree;
The \f5sh_getscope() function can be used to the the scope information associated with existing scope. Scopes are numbered from \f50 for the global scope up to the current scope level. The whence argument uses the symbolic constants associated with \f5lseek() to indicate whether the \f5Iscope argument is absolute, relative to the current scope, or relative to the topmost scope. The\f5sh_setscope() function can be used to make a a known scope the current scope. It returns a pointer to the old current scope.

The \f5sh_funscope() function can be used to run a function in a new scope. The arguments argc and argv are the number of arguments and the list of arguments respectively. If fn is non-\f5NULL, then this function is invoked with argc, argv, and arg as arguments.

The \f5sh_fun() function can be called within a discipline function or built-in extension to execute a discipline function script. The argument funnode is a pointer to the shell function or built-in to execute. The argument varnode is a pointer to the name value pair that has defined this discipline. The array argv is a \f5NULL terminated list of arguments that are passed to the function.

By default, \f5ksh only records but does not act on signals when running a built-in command. If a built-in takes a substantial amount of time to execute, then it should check for interrupts periodically by calling \f5sh_sigcheck(). If a signal is pending, \f5sh_sigcheck() will exit the function you are calling and return to the point where the most recent built-in was invoked, or where \f5sh_eval() or \f5sh_trap() was called.

The \f5sh_delay() function is used to cause the shell to sleep for a period of time defined by sec.

The \f5sh_fmtq() function can be used to convert a string into a string that is quoted so that it can be reinput to the shell. The quoted string returned by \f5sh_fmtq may be returned on the current stack, so that it must be saved or copied.

The \f5sh_fdnotify() function causes the function fn to be called whenever the shell duplicates or closes a file. It is provided for extensions that need to keep track of file descriptors that could be changed by shell commands. The function fn is called with two arguments. The first argument is the original file descriptor. The second argument is the new file descriptor for duplicating files, and \f5SH_FDCLOSE when a file has been closed. The previously installed \f5sh_fdnotify() function pointer is returned.

The \f5sh_waitnotify() function causes the function fn to be called whenever the shell is waiting for input from a slow device or waiting for a process to complete. This function can process events and run shell commands until there is input, the timer is reached or a signal arises. It is called with three arguments. The first is the file descriptor from which the shell trying to read or \f5-1 if the shell is waiting for a process to complete. The second is a timeout in milliseconds. A value of \f5-1 for the timeout means that no timeout should be set. The third argument is 0 for input file descriptors and 1 for output file descriptor. The function needs to return a value \f5>0 if there is input on the file descriptor, and a value \f5<0 if the timeout is reached or a signal has occurred. A value of \f50 indicates that the function has returned without processing and that the shell should wait for input or process completion. The previous installed \f5sh_waitnotify() function pointer is returned.

The \f5sh_iogetiop() function returns a pointer to the Sfio stream corresponding to file descriptor number fd and the given mode mode. The mode can be either \f5SF_READ or \f5SF_WRITE. The fd argument can the number of an open file descriptor or one of the following symbolic constants:

\f5SH_IOCOPROCESS: The stream corresponding to the most recent co-process.
\f5SH_IOHISTFILE: The stream corresponding to the history file. If no stream exists corresponding to fd or the stream can not be accessed in the specified mode, \f5NULL is returned.
SEE ALSO
builtin(1) cdt(3) error(3) nval(3) sfio(3) stk(3) tksh(1)
AUTHOR
David G. Korn (dgk@research.att.com).