2 .TH SHELL 3 "28 Feb 2003"
6 \fBshell\fR \- a \f5ksh\fP library interface
9 .ta .8i 1.6i 2.4i 3.2i 4.0i 4.8i 5.6i
10 .SS "HEADERS/LIBRARIES"
32 int sh_main(int \fIargc\fP, char *\fIargv\fP[], Sh_init \fIfn\fP);
33 Shell_t *sh_init(int \fIargc\fP, char *\fIargv\fP[]);
34 Shell_t *sh_getinterp(void);
36 Namval_t *sh_addbuiltin(const char *\fIname\fP,Sh_bltin_f \fIfn\fP,void *\fIarg\fP);
38 unsigned int sh_isoption(int \fIoption\fP);
39 unsigned int sh_onoption(int \fIoption\fP);
40 unsigned int sh_offoption(int \fIoption\fP);
42 void *sh_parse(Shell_t *\fIshp\fP, Sfio_t *\fIsp\fP, int \fIflags\fP);
43 int sh_trap(const char *\fIstring\fP, int \fImode\fP);
44 int sh_run(int \fIargc\fP, char *\fIargv\fP[]);
45 int sh_eval(Sfio_t *\fIsp\fP,int \fImode\fP);
46 int sh_fun(Namval_t *\fIfunnode\fP, Namval_t *\fIvarnode\fP, char *\fIargv\fP[]);
47 int sh_funscope(int \fIargc\fP,char *\fIargv\fP[],int(*\fIfn\fP)(void*),void *\fIarg\fP,int \fIflags\fP);
48 Shscope_t *sh_getscope(int \fIindex\fP,int \fIwhence\fP);
49 Shscope_t *sh_setscope(Shscope_t *\fIscope\fP);
51 int (*sh_fdnotify(int(*\fIfn\fP)(int,int)))(int,int);
52 char *sh_fmtq(const char *\fIstring\fP);
53 void *sh_waitnotify(Shwait_f \fIfn\fP);
54 void sh_delay(double \fIsec\fP);
55 Sfio_t *sh_iogetiop(int \fIfd\fP, int \fImode\fP);
56 int sh_sigcheck(void);
62 The \fIShell\fP library is a set of functions used for
63 writing extensions to \f5ksh\fP or writing commands
64 that embed shell command processing.
65 The include file \f5<shell.h>\fP contains the type definitions,
66 function prototypes and symbolic constants defined by
67 this interface. It also defines replacement definitions for
68 the standard library functions
82 intended to be compiled as built-in commands.
84 The \f5<shell.h>\fP header includes \f5<ast.h>\fP which
85 in turn includes the standard include files, \f5<stddef.h>\fP,
86 \f5<stdlib.h>\fP, \f5<stdarg.h>\fP, \f5<limits.h>\fP,
87 \f5<stdio.h>\fP, \f5<string.h>\fP, \f5<unistd.h>\fP,
88 \f5<sys/types.h>\fP, \f5<fcntl.h>\fP, and \f5<locale.h>\fP.
89 The \f5<shell.h>\fP header also includes the headers
96 so that in most cases, programs only require the single
97 header \f5<shell.h>\fP.
99 Programs can use this library in one of the following ways:
103 To write builtin commands and/or other code that will be loaded
104 into the shell by loading dynamic libraries
105 at run time using the \f5builtin\fP(1) command.
106 In this case the shell will look for a function named \f5lib_init\fP
107 in your library and, if found, will execute this function with
108 two arguments. The first
109 argument will be an \f5int\P with value \f50\fP when the library is loaded.
110 The second argument will contain a pointer to a structure of type
112 In addition, for each argument named on the \f5builtin\fP
113 command line, it will look for a function named \f5b_\fP\fIname\fP\f5()\fP
114 in your library and will \fIname\fP as a built-in.
117 To build separate a separate command that uses the shell as a
118 library at compile or run time.
119 In this case the \f5sh_init()\fP function must be called to
120 initialize this library before any other commands in this library
122 The arguments \fIargc\fP and \fIargv\fP are the number
123 of arguments and the vector of arguments as supplied by the shell.
124 It returns a pointer the \f5Shell_t\fP.
127 To build a new version of \f5ksh\fP with extended capabilities,
128 for example \f5tksh\fP(1).
129 In this case, the user writes a \f5main()\fP function that
130 calls \f5sh_main()\fP with the \fIargc\fP and \fIargv\fP arguments
131 from \f5main\fP and pointer to function, \fIfn\fP as a third
132 argument.. The function \fIfn\fP will
133 be invoked with argument \f50\fP after \f5ksh\fP has done initialization,
134 but before \f5ksh\fP has processed any start up files or executed
135 any commands. The function \fIfn\fP
136 will be invoked with an argument of \f51\fP before \f5ksh\fP
137 begins to execute a script that has been invoked by name
138 since \f5ksh\fP cleans up memory and long jumps back to
139 the beginning of the shell in this case.
140 The function \fIfn\fP will be called with argument \f5-1\fP before
144 The \f5Shell_t\fP structure contains the following fields:
147 Shopt_t \fIoptions\fP; \fR/* set -o options */\fP
148 Dt_t *\fIvar_tree\fP; \fR/* shell variable dictionary */\fP
149 Dt_t *\fIfun_tree\fP; \fR/* shell function dictionary */\fP
150 Dt_t *\fIalias_tree\fP; \fR/* shell alias dictionary */\fP
151 Dt_t *\fIbltin_tree\fP; \fR/* shell built-in dictionary */\fP
152 Shscope_t *\fItopscope\fP; \fR/* pointer to top-level scope */\fP
153 char *\fIinfile_name\fP; \fR/* path name of current input file*/\fP
154 int \fIinlineno\fP; \fR/* line number of current input file*/\fP
155 int \fIexitval\fP; \fR/* most recent exit value*/\fP
158 This structure is returned by \f5sh_init()\fP but can also be retrieved
159 by a call to \f5sh_getinterp()\fP.
161 All built-in commands to the shell are invoked with
162 three arguments. The first two arguments give the
163 number of arguments and the argument list
164 and uses the same conventions as the \f5main()\fP function
165 of a program. The third argument is a pointer to a structure
166 of type \f5Shbltin_t\fP. This structure contains \f5shp\P which is a pointer
167 to the shell interpreter, and \f5ptr\fP which is a pointer that
168 can be associated with each built-in.
169 The \f5sh_addbuiltin()\fP function is used to add, replace or delete
171 It takes the name of the built-in, \fIname\fP, a pointer
172 to the function that implements the built-in, \fIfn\fP, and
173 a pointer that will be passed to the function in the \f5ptr\fP field when
175 If, \fIfn\fP is non-\f5NULL\fP the built-in command
176 is added or replaced. Otherwise, \f5sh_addbuiltin()\fP will
177 return a pointer to the built-in if it exists or \f5NULL\fP otherwise.
178 If \fIarg\fP is \f5(void*)1\fP the built-in will be deleted.
179 The \fIname\fP argument can be in the format of a pathname.
180 It cannot be the name of any of the special built-in commands.
181 If \fIname\fP contains a \f5/\fP, the built-in is the basename of
182 the pathname and the built-in will only be executed
183 if the given pathname is encountered when performing
185 When adding or replacing a built-in,
186 \f5sh_addbuiltin()\fP function returns a pointer to
187 the name-value pair corresponding to the built-in on success and \f5NULL\fP
188 if it is unable to add or replace the built-in.
189 When deleting a built-in, \f5NULL\fP is returned on success or
190 if not found, and the name-value pair pointer is returned if the built-in
193 The functions \f5sh_onoption()\fP, \f5sh_offoption()\fP, \f5sh_isoption()\fP
194 are used to set, unset, and test for shell options respectively.
195 The \fIoption\fP argument can be any one of the following:
198 The \f5NV_EXPORT\fP attribute is given to each variable whose
199 name is an identifier when a value is assigned.
202 Each background process is run at a lower priority.
205 Causes a non-interactive shell to exit when a command,
206 other than a conditional command, returns non-zero.
209 The emacs editing mode.
212 Same as the emacs editing mode except for the behavior of CONTROL-T.
215 Indicates that the history file has been created and that
216 commands can be logged.
219 Do not treat end-of-file as exit.
221 \f5SH_INTERACTIVE\fP:
223 Set for interactive shells.
224 Do not set or unset this option.
226 A \fB/\fP is added to the end of each directory generated by pathname
230 Indicates that the monitor option is enabled for job control.
233 The \fB>\fP redirection will fail if the file exists. Each file
234 created with \fB>\fP will have the \f5O_EXCL\fP bit set as described
238 Do not perform pathname expansion.
241 Do not save function definitions in the history file.
244 Cause a message to be generated as soon as each background job completes.
247 Cause the shell to fail with an error of an unset variable is
253 Cause each line to be echoed as it is read by the parser.
256 Cause each command to be displayed after all expansions, but
263 Read character at a time rather than line at a time when
267 The \f5sh_trap()\fP function can be used to compile and execute
269 A value of \f50\fP for \fImode\fP indicates that \fIname\fP
270 refers to a string. A value of \f51\fP for \fImode\fP
271 indicates that \fIname\fP is an \f5Sfio_t*\fP to an open stream.
272 A value of \f52\fP for \fImode\fP indicates that \fIname\fP
273 points to a parse tree that has been returned by \f5sh_parse()\fP.
274 The complete file associated with the string or file
275 is compiled and then executed so that aliases defined
276 within the string or file will not take effect until
277 the next command is executed.
279 The \f5sh_run()\fP function will run the command given by
280 by the argument list \fIargv\fP containing \fIargc\fP elements.
281 If \fIargv\fP\f5[0]\fP does not contain a \f5/\fP, it will
282 be checked to see if it is a built-in or function before
283 performing a path search.
285 The \f5sh_eval()\fP function executes a string or file
287 If \fImode\fP is non-zero and the history file has
288 been created, the stream defined by \fIsp\fP
289 will be appended to the history file as a command.
291 The \f5sh_parse()\fP function takes a pointer to the
292 shell interpreter \fIshp\fP, a pointer to a string or file stream
293 \fIsp\fP, and compilation flags, and returns a pointer
294 to a parse tree of the compiled stream. This pointer can
295 be used in subsequent calls to \f5sh_trap()\fP.
296 The compilation flags can be zero or more of the following:
299 Treat new-lines as \fB;\fP.
302 An end of file causes syntax error. By default it will
303 be treated as a new-line.
305 \f5ksh\fP executes each function defined with the \f5function\fP
306 reserved word in a separate scope. The \f5Shscope_t\fP type
307 provides an interface to some of the information that
308 is available on each scope. The structure contains
309 the following public members:
311 \f5Sh_scope_t *par_scope;\fP
315 \f5Dt_t *var_tree;\fP
317 The \f5sh_getscope()\fP function can be used to the the
318 scope information associated with existing scope.
319 Scopes are numbered from \f50\fP for the global scope
320 up to the current scope level. The \fIwhence\fP
321 argument uses the symbolic constants associated with \f5lseek()\fP
322 to indicate whether the \f5Iscope\fP argument is absolute,
323 relative to the current scope, or relative to the topmost scope.
324 The\f5sh_setscope()\fP function can be used to make a
325 a known scope the current scope. It returns a pointer to the
328 The \f5sh_funscope()\fP function can be used to run a function
329 in a new scope. The arguments \fIargc\fP and \fIargv\fP
330 are the number of arguments and the list of arguments
331 respectively. If \fIfn\fP is non-\f5NULL\fP, then
332 this function is invoked with \fIargc\fP, \fIargv\fP, and \fIarg\fP
335 The \f5sh_fun()\fP function can be called within a
336 discipline function or built-in extension to execute a
337 discipline function script.
338 The argument \fIfunnode\fP is a pointer to the shell function
339 or built-in to execute.
340 The argument \fIvarnode\fP is a pointer to the name
341 value pair that has defined this discipline.
342 The array \fIargv\fP is a \f5NULL\fP terminated list of
343 arguments that are passed to the function.
345 By default, \f5ksh\fP only records but does not act
346 on signals when running a built-in command.
347 If a built-in takes a substantial amount of time
348 to execute, then it should check for interrupts
349 periodically by calling \f5sh_sigcheck()\fP.
350 If a signal is pending, \f5sh_sigcheck()\fP will exit
351 the function you are calling and return to the point
352 where the most recent built-in was invoked, or where
353 \f5sh_eval()\fP or \f5sh_trap()\fP was called.
355 The \f5sh_delay()\fP function is used to cause the
356 shell to sleep for a period of time defined by \fIsec\fP.
358 The \f5sh_fmtq()\fP function can be used to convert a string
359 into a string that is quoted so that it can be reinput
360 to the shell. The quoted string returned by \f5sh_fmtq\fP
361 may be returned on the current stack, so that it
362 must be saved or copied.
364 The \f5sh_fdnotify()\fP function causes the function \fIfn\fP
365 to be called whenever the shell duplicates or closes a file.
366 It is provided for extensions that need to keep track of
367 file descriptors that could be changed by shell commands.
368 The function \fIfn\fP is called with two arguments.
369 The first argument is the original file descriptor. The
370 second argument is the new file descriptor for duplicating
371 files, and \f5SH_FDCLOSE\fP when a file has been closed.
372 The previously installed \f5sh_fdnotify()\fP function pointer
375 The \f5sh_waitnotify()\fP function causes the function \fIfn\fP
376 to be called whenever the shell is waiting for input from
377 a slow device or waiting for a process to complete.
378 This function can process events and run shell commands
379 until there is input, the timer is reached or a signal arises.
380 It is called with three arguments. The first is the file
381 descriptor from which the shell trying to read or \f5\-1\fP
382 if the shell is waiting for a process to complete.
383 The second is a timeout in milliseconds.
384 A value of \f5\-1\fP for the timeout means that
385 no timeout should be set.
386 The third argument is 0 for input file descriptors
387 and 1 for output file descriptor.
388 The function needs to return a value \f5>0\fP if there
389 is input on the file descriptor, and a value \f5<0\fP
390 if the timeout is reached or a signal has occurred.
391 A value of \f50\fP indicates
392 that the function has returned without processing and that the shell
393 should wait for input or process completion.
394 The previous installed \f5sh_waitnotify()\fP function pointer is returned.
396 The \f5sh_iogetiop()\fP function returns a pointer to the
397 Sfio stream corresponding to file descriptor number \fIfd\fP
398 and the given mode \fImode\fP. The mode can be either
399 \f5SF_READ\fP or \f5SF_WRITE\fP.
400 The \fIfd\fP argument can the number of an open file descriptor or
401 one of the following symbolic constants:
403 \f5SH_IOCOPROCESS\fP:
404 The stream corresponding to the most recent co-process.
407 The stream corresponding to the history file.
408 If no stream exists corresponding to \fIfd\fP or the stream
409 can not be accessed in the specified mode, \f5NULL\fP is returned.
421 David G. Korn (dgk@research.att.com).