SHELL(3)

NAME

shell - a ksh library interface

SYNOPSIS

#include        <shell.h>
Shopt_t;
Shscope_t;
int	sh_main(int argc, char *argv[], void(*fn)(int));
int	sh_init(int argc, char *argv);
Namval_t	*sh_addbuiltin(const char *name,int(*fn)(int,char*[],void*),void *arg);
Shopt_t	sh_isoption(Shopt_t options);
Shopt_t	sh_onoption(Shopt_t options);
Shopt_t	sh_offoption(Shopt_t options);
void	*sh_parse(Sfio_t *sp, int flags);
int 	sh_trap(const char *string, int mode);
int 	sh_eval(Sfio_t *sp,int mode);
int 	sh_fun(Namval_t *funnode, Namval_t *varnode, char *argv[]);
int 	sh_funscope(int argc,char *argv[],int(*fn)(void*),void *arg,int flags);
Shscope_t	*sh_getscope(int index,int whence);
Shscope_t	*sh_setscope(Shscope_t *scope);
int	(*sh_fdnotify(int(*fn)(int,int)))(int,int);
char	*sh_fmtq(const char *string);
void	*sh_waitnotify(int(*fn)(int,long,int));
void 	sh_delay(double sec);
Sfio_t 	*sh_fd2sfio(int  fd);
Namval_t	*sh_bltin_table(void);
int	sh_sigcheck(void);

DESCRIPTION The Shell library is a set of functions used for writing extensions to ksh or writing commands that embed shell command processing. The include file <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 access(), close(), dup(), exit(), fcntl(), lseek(), open(), pipe(), read(), and write() and all standard I/O functions so that it must be used with all functions intened to be compiled as built-in commands. The <shell.h> header includes <ast.h> which in turn includes the standard include files, <stddef.h>, <stdlib.h>, <stdarg.h>, <limits.h>, <stdio.h>, <string.h>, <unistd.h>, <sys/types.h>, <fcntl.h>, and <locale.h>. The <shell.h> header also includes the <sfio.h>, <stak.h>, and <error.h> headers.

Programs can extend ksh by loading dynamic libraries at run time using the builtin command, or by building separate commands that link the shell as a library at compile time. The sh_main() function is used to build a new version if ksh with extended capabilities. The user writes a main() function that calls sh_main() with the argc and argv arguments from main and a function argument fn. The function fn will be invoked with argument 0 after ksh has done initialization, but before ksh has processed any start up files or executed any commands. The function fn will be invoked with a non-zero argument before ksh begins to execute a script that has been invoked by name since ksh cleans up memory and long jumps back to the beginning of the shell in this case.

The sh_init() function can be used for applications that want to embed shell script processing capabilities but are not intended to behave like a shell. This function needs to be invoiked before any of the other functions are invoked. The arguments argc and argv are the number of arguments and the vector of arguments as supplied by the shell.

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 main() function of a program. The third argument is a pointer that can be associated with each built-in. The sh_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 when it is invoked. If, fn is non-NULL the built-in command is added or replaced. Otherwise, the given built-in command 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 /, 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, sh_addbuiltin() function returns a pointer to the node corresponding to the built-in on success and NULL if it is unable to add or replace the built-in. When deleting a built-in, NULL is returns on success or if not found, and the node pointer is returned if the built-in cannot be deleted.

The functions sh_onoption(), sh_offoption(), sh_isoption() are used to set, unset, and test for shell options respectively. These functions take an integral type Shopt_t that holds any subset of the following flags:

    SH_ALLEXPORT: The NV_EXPORT attribute is given to each variable whose name is an identifier when a value is assigned.

    SH_BGNICE: Each background process is run at a lower priority.

    SH_ERREXIT: Causes a non-interactive shell to exit when a command, other than a conditional command, returns non-zero.

    SH_EMACS: The emacs editing mode.

    SH_GMACS: Same as the emacs editing mode except for the behavior of CONTROL-T.

    SH_HISTORY: Indicates that the history file has been created and that commands can be logged.

    SH_IGNOREEOF: Do not treat end-of-file as exit.

    SH_INTERACTIVE:

    Set for interactive shells. Do not set or unset this option. SH_MARKDIRS: A / is added to the end of each directory generated by pathname expansion.

    SH_MONITOR: Indicates that the monitor option is enabled for job control.

    SH_NOCLOBBER: The > redirection will fail if the file exists. Each file created with > will have the O_EXCL bit set as described in <fcntl.h>

    SH_NOGLOB: Do not perform pathname expansion.

    SH_NOLOG: Do not save function definitions in the history file.

    SH_NOTIFY: Cause a message to be generated as soon as each background job completes.

    SH_NOUNSET: Cause the shell to fail with an error of an unset variable is referenced.

    SH_PRIVILEGED:

    SH_VERBOSE: Cause each line to be echoed as it is read by the parser.

    SH_XTRACE: Cause each command to be displayed after all expansions, but before execution.

    SH_VI: The vi edit mode.

    SH_VIRAW: Read character at a time rather than line at a time when in vi edit mode.

The sh_trap() function can be used to compile and execute a string or file. A value of 0 for mode indicates that name refers to a string. A value of 1 for mode indicates that name is an Sfio_t* to an open stream. A value of 2 for mode indicates that name points to a parse tree that has been returned by sh_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 sh_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 sh_parse() function takes 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 sh_trap(). The compilation flags can be zero or more of the following:

    SH_NL: Treat new-lines as ;.

    SH_EOF: An end of file causes syntax error. By default it will be treated as a new-line.

ksh executes each function defined with the function reserved word in a separate scope. The Shscope_t type provides an interface to some of the information that is available on each scope. The structure contains the following public members:
      Sh_scope_t      *par_scope;
      int             argc;
      char            **argv;
      char            *cmdname;
      Hashtab_t       *var_tree;
The sh_getscope() function can be used to the the scope information associated with existing scope. Scopes are numbered from 0 for the global scope up to the current scope level. The whence argument uses the symbolic constants associated with lseek() to indicate whether the Iscope argument is absolute, relative to the current scope, or relative to the topmost scope. Thesh_setscope() function can be used to make a a known scope the current scope. It returns a pointer to the old current scope.

The sh_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-NULL, then this function is invoked with argc, argv, and arg as arguments.

The sh_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 NULL terminated list of arguments that are passed to the function.

By default, ksh 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 sh_sigcheck(). If a signal is pending, sh_sigcheck() will exit the function you are calling and return to the point where the most recent built-in was invoked, or where sh_eval() or sh_trap() was called.

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

The sh_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 sh_fmtq may be returned on the current stack, so that it must be saved or copied.

The sh_fdnotify() function causes the function fn to be called whenever the shell duplicates or closes a file. It is provided for extentions 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 SH_FDCLOSE when a file has been closed. The previously installed sh_fdnotify() function pointer is returned.

The sh_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 -1 if the shell is waiting for a process to complete. The second is a timeout in milliseconds. A value of -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 >0 if there is input on the file descriptor, and a value <0 if the timeout is reached or a signal has occured. A value of 0 indicates that the function has returned without processing and that the shell should wait for input or process completion. The previous installed sh_waitnotify() function pointer is returned.

The sh_fd2sfio() function returns a pointer to the Sfio stream corresponding to file descriptor number ffd. If no stream exists, one is created.

SEE ALSO Error(3) Nval(3) Sfio(3) Stak(3) lseek(2)

AUTHOR David G. Korn (dgk@research.att.com).