builtins.html 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
  2. <HTML>
  3. <HEAD>
  4. <META name="generator" content="mm2html (AT&amp;T Research) 2012-01-11">
  5. <TITLE> www/ksh/builtins.mm mm document </TITLE>
  6. <META name="author" content="gsf+dgk+kpv">
  7. <STYLE type="text/css">
  8. div.FI { padding-left:2em; text-indent:0em; }
  9. div.HI { padding-left:4em; text-indent:-2em; }
  10. dt { float:left; clear:both; }
  11. dd { margin-left:3em; }
  12. </STYLE>
  13. </HEAD>
  14. <BODY bgcolor=white link=slateblue vlink=teal >
  15. <TABLE border=0 align=center width=96%>
  16. <TBODY><TR><TD valign=top align=left>
  17. <!--INDEX--><!--/INDEX-->
  18. <P>
  19. <CENTER>
  20. <H3><CENTER><FONT color=red><FONT face=courier>Guidelines for writing <TT>ksh-93</TT> built-in commands</FONT></FONT></CENTER></H3>
  21. <BR>David G. Korn
  22. <P><I></I>
  23. </CENTER>
  24. <P>
  25. <CENTER><FONT color=red><FONT face=courier><H3 align=center><A name="Abstract">Abstract</A></H3></FONT></FONT></CENTER>
  26. One of the features of <TT>ksh93</TT>, the latest version of <TT>ksh</TT>,
  27. is the ability to add built-in commands at run time.
  28. This feature only works on operating systems that have the ability
  29. to load and link code into the current process at run time.
  30. Some examples of the systems that have this feature
  31. are Linux, System V Release 4, Solaris, Sun OS, HP-UX Release 8 and above,
  32. AIX 3.2 and above, and Microsoft Windows systems.
  33. <P>
  34. This memo describes how to write and compile programs
  35. that can be loaded into <TT>ksh</TT> at run time as built-in
  36. commands.
  37. <P>
  38. <P><HR><CENTER><FONT color=red><FONT face=courier><H3><A name="INTRODUCTION">INTRODUCTION</A></H3></FONT></FONT></CENTER>
  39. A built-in command is executed without creating a separate process.
  40. Instead, the command is invoked as a C function by <TT>ksh</TT>.
  41. If this function has no side effects in the shell process,
  42. then the behavior of this built-in is identical to that of
  43. the equivalent stand-alone command. The primary difference
  44. in this case is performance. The overhead of process creation
  45. is eliminated. For commands of short duration, the effect
  46. can be dramatic. For example, on SUN OS 4.1, the time to
  47. run <TT>wc</TT> on a small file of about 1000 bytes, runs
  48. about 50 times faster as a built-in command.
  49. <P>
  50. In addition, built-in commands may have side effects on the
  51. shell environment.
  52. This is usually done to extend the application domain for
  53. shell programming. For example, there is a group of X-windows extension
  54. built-ins that make heavy use of the shell variable namespace.
  55. These built-ins are added at run time and
  56. result in a windowing shell that can be used to write
  57. X-windows applications.
  58. <P>
  59. While there are definite advantages to adding built-in
  60. commands, there are some disadvantages as well.
  61. Since the built-in command and <TT>ksh</TT> share the same
  62. address space, a coding error in the built-in program
  63. may affect the behavior of <TT>ksh</TT>; perhaps causing
  64. it to core dump or hang.
  65. Debugging is also more complex since your code is now
  66. a part of a larger entity.
  67. The isolation provided by a separate process
  68. guarantees that all resources used by the command
  69. will be freed when the command completes.
  70. Resources used by a built-in must be meticulously maintained and freed.
  71. Also, since the address space of <TT>ksh</TT> will be larger when built-in are loaded,
  72. it may increase the time it takes <TT>ksh</TT> to fork() and
  73. exec() non-built-in commands.
  74. It makes no sense to add a built-in command that takes
  75. a long time to run or that is run only once, since the performance
  76. benefits will be negligible.
  77. Built-ins that have side effects in the current shell
  78. environment have the disadvantage of increasing the
  79. coupling between the built-in and <TT>ksh</TT>, making
  80. the overall system less modular and more monolithic.
  81. <P>
  82. Despite these drawbacks, in many cases extending
  83. <TT>ksh</TT> by adding built-in
  84. commands makes sense and allows reuse of the shell
  85. scripting ability in an application specific domain.
  86. This memo describes how to write <TT>ksh</TT> extensions.
  87. <P>
  88. <P><HR><CENTER><FONT color=red><FONT face=courier><H3><A name="WRITING BUILT-IN COMMANDS">WRITING BUILT-IN COMMANDS</A></H3></FONT></FONT></CENTER>
  89. There is a development kit available for writing <TT>ksh</TT>
  90. built-ins as part of the AST (AT&amp;T Software Technology) Toolkit.
  91. The development kit has three directories,
  92. <TT>include</TT>, <TT>lib</TT>, and <TT>bin</TT>.
  93. It is best to set the value of the environment variable
  94. <TT>PACKAGE_ast</TT> to the pathname of the directory
  95. containing the development kit.
  96. The <TT>include</TT> directory contains a sub-directory
  97. named <TT>ast</TT> that contains interface prototypes
  98. for functions that you can call from built-ins. The <TT>lib</TT>
  99. directory contains the <TT>ast</TT> library
  100. and a library named <TT>cmd</TT> that contains a version
  101. of several of the standard POSIX<FONT SIZE=-6>[1]</FONT>
  102. utilities that can be made run time built-ins.
  103. The <TT>lib/ksh</TT> directory contains shared libraries
  104. that implement other <TT>ksh</TT> built-ins.
  105. The <TT>bin</TT> directory contains build tools such as <TT>nmake</TT><FONT SIZE=-6>[2]</FONT>.
  106. To add built-ins at runtime, it is necessary to build a shared library
  107. containing one or more built-ins that you wish to add.
  108. The built-ins are then added by running <TT>builtin &#45;f</TT> <EM>shared_lib</EM>.
  109. Since the procedure for building share libraries is system dependent,
  110. it is best to use
  111. <TT>nmake</TT>
  112. using the sample nmake makefile below as a prototype.
  113. The AST Toolkit also contains some examples of built-in libraries under
  114. the <TT>src/cmd/kshlib</TT> directory.
  115. <P>
  116. There are two ways to code adding built-ins. One method is to replace
  117. the function <TT>main</TT> with a function
  118. <TT>b_</TT><EM>name</EM>, where <EM>name</EM> is the name
  119. of the built-in you wish to define.
  120. A built-in command has a calling convention similar to
  121. the <TT>main</TT> function of a program,
  122. <TT>int main(int argc, char *argv&#0091;&#0093;)</TT>.
  123. except that it takes a third argument of type <TT>Shbltin_t*</TT> which can
  124. be passed as <TT><FONT SIZE=-1>NULL</FONT></TT> if it is not used. The definition for
  125. <TT>Shbltin_t*</TT> is in <TT>&lt;ast/shcmd.h&gt;</TT>.
  126. Instead of <TT>exit</TT>, you need to use <TT>return</TT>
  127. to terminate your command.
  128. The return value will become the exit status of the command.
  129. The <TT>open</TT> built-in, installed in <TT>lib/ksh</TT> in the AST Toolkit, uses this method.
  130. The <TT>Shbltin_t</TT> structure contains a field named <TT>shp</TT> which is
  131. a pointer the the shell data that is needed for <TT>shell</TT> library callbacks.
  132. It also contains the fields, <TT>shrun</TT>, <TT>shtrap</TT>, <TT>shexit</TT>,
  133. and <TT>shbltin</TT>
  134. that are function pointers to the <TT>shell</TT> library functions <TT>sh_run</TT>, <TT>sh_trap</TT>
  135. <TT>sh_exit</TT>, and <TT>sh_addbuiltin</TT>, respectively. These functions
  136. can be invoked without the need for runtime symbol lookup when the
  137. shell is statically linked with <TT>libshell</TT>.
  138. <P>
  139. The alternative method is to create a function <TT>lib_init</TT> and
  140. use the <TT>Shbltin_t.shbltin()</TT> function to add one or more built-ins.
  141. The <TT>lib_init</TT> function will be called with two arguments. The
  142. first argument will be 0 when the library is loaded and the second
  143. argument will be of type <TT>Shbltin_t*</TT>.
  144. The <TT>dbm_t</TT> and <TT>dss</TT> shell built-ins use this method.
  145. <P>
  146. No matter which way you add built-ins you should add the line
  147. <TT>SHLIB(</TT><EM>identifier</EM><TT>)</TT> as the last line of one
  148. of the built-in source file, where <EM>identifier</EM> is any C identifier.
  149. This line provides version information to the shell <TT>builtin</TT> command
  150. that it uses to verify compatibility between the built-in and <TT>ksh</TT>
  151. implementation versions. <TT>builtin</TT> fails with a diagnostic on version
  152. mismatch. The diagnostic helps determine whether <TT>ksh</TT> is out of
  153. date and requires an upgrade or the built-in is out of date and requires
  154. recompilation.
  155. <P>
  156. The steps necessary to create and add a run time built-in are
  157. illustrated in the following simple example.
  158. Suppose you wish to add a built-in command named <TT>hello</TT>
  159. which requires one argument and prints the word hello followed
  160. by its argument. First, write the following program in the file
  161. <TT>hello.c</TT>:
  162. <DIV class=FI>
  163. <PRE>
  164. #include &lt;stdio.h&gt;
  165. int b_hello(int argc, char *argv&#0091;&#0093;, void *context)
  166. {
  167. if(argc != 2)
  168. {
  169. fprintf(stderr,"Usage: hello arg&#0092;n");
  170. return(2);
  171. }
  172. printf("hello %s&#0092;n",argv&#0091;1&#0093;);
  173. return(0);
  174. }
  175. SHLIB(hello)
  176. </DIV>
  177. </PRE>
  178. <P>
  179. Next, the program needs to be compiled.
  180. If you are building with AT&amp;T <TT>nmake</TT> use the following <TT>Makefile</TT>:
  181. <DIV class=FI>
  182. <PRE>
  183. :PACKAGE: --shared ast
  184. hello plugin=ksh :LIBRARY: hello.c
  185. </DIV>
  186. </PRE>
  187. and run <TT>nmake install</TT> to compile, link, and install the built-in shared library
  188. in <TT>lib/ksh/</TT> under <TT>PACKAGE_ast</TT>.
  189. If the built-in extension uses several <TT>.c</TT> files, list all of these on
  190. the <TT>:LIBRARY:</TT> line.
  191. <P>
  192. Otherwise you will have to compile <TT>hello.c</TT> with an option
  193. to pick up the AST include directory
  194. (since the AST <TT>&lt;stdio.h&gt;</TT> is required for <TT>ksh</TT> compatibility)
  195. and options required for generating shared libraries.
  196. For example, on Linux use this to compile:
  197. <DIV class=FI>
  198. <PRE>
  199. cc -fpic -I$PACKAGE_ast/include/ast -c hello.c
  200. </DIV>
  201. </PRE>
  202. and use the appropriate link line.
  203. It really is best to use <TT>nmake</TT> because the 2 line Makefile above
  204. will work on all systems that have <TT>ksh</TT> installed.
  205. <P>
  206. If you have several built-ins, it is desirable
  207. to build a shared library that contains them all.
  208. <P>
  209. The final step is using the built-in.
  210. This can be done with the <TT>ksh</TT> command <TT>builtin</TT>.
  211. To load the shared library <TT>libhello.so</TT> from the current directory
  212. and add the built-in <TT>hello</TT>, invoke the command,
  213. <DIV class=FI>
  214. <PRE>
  215. builtin -f ./libhello.so hello
  216. </DIV>
  217. </PRE>
  218. The shared library prefix (<TT>lib</TT> here) and suffix (<TT>.so</TT> here) be omitted;
  219. the shell will add an appropriate suffix
  220. for the system that it is loading from.
  221. If you install the shared library in <TT>lib/ksh/</TT>, where <TT>../lib/ksh/</TT> is
  222. a directory on <STRONG>$PATH</STRONG>, the command
  223. <DIV class=FI>
  224. <PRE>
  225. builtin -f hello hello
  226. </DIV>
  227. </PRE>
  228. will automatically find, load and install the built-in on any system.
  229. Once this command has been invoked, you can invoke <TT>hello</TT>
  230. as you do any other command.
  231. If you are using <TT>lib_init</TT> method to add built-ins then no arguments
  232. follow the <TT>&#45;f</TT> option.
  233. <P>
  234. It is often desirable to make a command <EM>built-in</EM>
  235. the first time that it is referenced. The first
  236. time <TT>hello</TT> is invoked, <TT>ksh</TT> should load and execute it,
  237. whereas for subsequent invocations <TT>ksh</TT> should just execute the built-in.
  238. This can be done by creating a file named <TT>hello</TT>
  239. with the following contents:
  240. <DIV class=FI>
  241. <PRE>
  242. function hello
  243. {
  244. unset -f hello
  245. builtin -f hello hello
  246. hello "$@"
  247. }
  248. </DIV>
  249. </PRE>
  250. This file <TT>hello</TT> needs to be placed in a directory that is
  251. in your <STRONG><FONT SIZE=-1>FPATH</FONT></STRONG> variable, and the built-in shared library
  252. should be installed in <TT>lib/ksh/</TT>, as described above.
  253. <P>
  254. <P><HR><CENTER><FONT color=red><FONT face=courier><H3><A name="CODING REQUIREMENTS AND CONVENTIONS">CODING REQUIREMENTS AND CONVENTIONS</A></H3></FONT></FONT></CENTER>
  255. As mentioned above, the entry point for built-ins must either be of
  256. the form <TT>b_</TT><EM>name</EM> or else be loaded from a function named
  257. <TT>lib_init</TT>.
  258. Your built-ins can call functions from the standard C library,
  259. the <TT>ast</TT> library, interface functions provided by <TT>ksh</TT>,
  260. and your own functions.
  261. You should avoid using any global symbols beginning with
  262. <STRONG>sh_</STRONG>,
  263. <STRONG>nv_</STRONG>,
  264. and
  265. <STRONG>ed_</STRONG>
  266. since these are used by <TT>ksh</TT> itself.
  267. <TT>#define</TT> constants in <TT>ksh</TT> interface
  268. files use symbols beginning with <TT>SH_</TT> and <TT>NV_</TT>,
  269. so avoid using names beginning with these too.
  270. <P>
  271. <H4><A name="Header Files">Header Files</A></H4>
  272. The development kit provides a portable interface
  273. to the C library and to libast.
  274. The header files in the development kit are compatible with
  275. K&amp;R C<FONT SIZE=-6>[3]</FONT>,
  276. ANSI-C<FONT SIZE=-6>[4]</FONT>,
  277. and C++<FONT SIZE=-6>[5]</FONT>.
  278. <P>
  279. The best thing to do is to include the header file <TT>&lt;shell.h&gt;</TT>.
  280. This header file causes the <TT>&lt;ast.h&gt;</TT> header, the
  281. <TT>&lt;error.h&gt;</TT> header and the <TT>&lt;stak.h&gt;</TT>
  282. header to be included as well as defining prototypes
  283. for functions that you can call to get shell
  284. services for your builtins.
  285. The header file <TT>&lt;ast.h&gt;</TT>
  286. provides prototypes for many <STRONG>libast</STRONG> functions
  287. and all the symbol and function definitions from the
  288. ANSI-C headers, <TT>&lt;stddef.h&gt;</TT>,
  289. <TT>&lt;stdlib.h&gt;</TT>, <TT>&lt;stdarg.h&gt;</TT>, <TT>&lt;limits.h&gt;</TT>,
  290. and <TT>&lt;string.h&gt;</TT>.
  291. It also provides all the symbols and definitions for the
  292. POSIX<FONT SIZE=-6>[6]</FONT>
  293. headers <TT>&lt;sys/types.h&gt;</TT>, <TT>&lt;fcntl.h&gt;</TT>, and
  294. <TT>&lt;unistd.h&gt;</TT>.
  295. You should include <TT>&lt;ast.h&gt;</TT> instead of one or more of
  296. these headers.
  297. The <TT>&lt;error.h&gt;</TT> header provides the interface to the error
  298. and option parsing routines defined below.
  299. The <TT>&lt;stak.h&gt;</TT> header provides the interface to the memory
  300. allocation routines described below.
  301. <P>
  302. Programs that want to use the information in <TT>&lt;sys/stat.h&gt;</TT>
  303. should include the file <TT>&lt;ls.h&gt;</TT> instead.
  304. This provides the complete POSIX interface to <TT>stat()</TT>
  305. related functions even on non-POSIX systems.
  306. <P>
  307. <P>
  308. <H4><A name="Input/Output">Input/Output</A></H4>
  309. <TT>ksh</TT> uses <STRONG>sfio</STRONG>,
  310. the Safe/Fast I/O library<FONT SIZE=-6>[7]</FONT>,
  311. to perform all I/O operations.
  312. The <STRONG>sfio</STRONG> library, which is part of <STRONG>libast</STRONG>,
  313. provides a superset of the functionality provided by the standard
  314. I/O library defined in ANSI-C.
  315. If none of the additional functionality is required,
  316. and if you are not familiar with <STRONG>sfio</STRONG> and
  317. you do not want to spend the time learning it,
  318. then you can use <TT>sfio</TT> via the <TT>stdio</TT> library
  319. interface. The development kit contains the header <TT>&lt;stdio.h&gt;</TT>
  320. which maps <TT>stdio</TT> calls to <TT>sfio</TT> calls.
  321. In most instances the mapping is done
  322. by macros or inline functions so that there is no overhead.
  323. The man page for the <TT>sfio</TT> library is in an Appendix.
  324. <P>
  325. However, there are some very nice extensions and
  326. performance improvements in <TT>sfio</TT>
  327. and if you plan any major extensions I recommend
  328. that you use it natively.
  329. <P>
  330. <H4><A name="Error Handling">Error Handling</A></H4>
  331. For error messages it is best to use the <TT>ast</TT> library
  332. function <TT>errormsg()</TT> rather that sending output to
  333. <TT>stderr</TT> or the equivalent <TT>sfstderr</TT> directly.
  334. Using <TT>errormsg()</TT> will make error message appear
  335. more uniform to the user.
  336. Furthermore, using <TT>errormsg()</TT> should make it easier
  337. to do error message translation for other locales
  338. in future versions of <TT>ksh</TT>.
  339. <P>
  340. The first argument to
  341. <TT>errormsg()</TT> specifies the dictionary in which the string
  342. will be searched for translation.
  343. The second argument to <TT>errormsg()</TT> contains that error type
  344. and value. The third argument is a <EM>printf</EM> style format
  345. and the remaining arguments are arguments to be printed
  346. as part of the message. A new-line is inserted at the
  347. end of each message and therefore, should not appear as
  348. part of the format string.
  349. The second argument should be one of the following:
  350. <DIV class=SH>
  351. <DL>
  352. <DT><TT>ERROR_exit(</TT><EM>n</EM><TT>)</TT>:<DD><BR>
  353. If <EM>n</EM> is not-zero, the builtin will exit value <EM>n</EM> after
  354. printing the message.
  355. <DT><TT>ERROR_system(</TT><EM>n</EM><TT>)</TT>:<DD><BR>
  356. Exit builtin with exit value <EM>n</EM> after printing the message.
  357. The message will display the message corresponding to <TT>errno</TT>
  358. enclosed within <TT>&#0091;&nbsp;&#0093;</TT> at the end of the message.
  359. <DT><TT>ERROR_usage(</TT><EM>n</EM><TT>)</TT>:<DD><BR>
  360. Will generate a usage message and exit. If <EM>n</EM> is non-zero,
  361. the exit value will be 2. Otherwise the exit value will be 0.
  362. <DT><TT>ERROR_debug(</TT><EM>n</EM><TT>)</TT>:<DD><BR>
  363. Will print a level <EM>n</EM> debugging message and will then continue.
  364. <DT><TT>ERROR_warn(</TT><EM>n</EM><TT>)</TT>:<DD><BR>
  365. Prints a warning message. <EM>n</EM> is ignored.
  366. </DL><P>
  367. <H4><A name="Option Parsing">Option Parsing</A></H4>
  368. The first thing that a built-in should do is to check
  369. the arguments for correctness and to print any usage
  370. messages on standard error.
  371. For consistency with the rest of <TT>ksh</TT>, it is best
  372. to use the <TT>libast</TT> functions <TT>optget()</TT> and
  373. <TT>optusage()</TT>for this
  374. purpose.
  375. The header <TT>&lt;error.h&gt;</TT> includes prototypes for
  376. these functions.
  377. The <TT>optget()</TT> function is similar to the
  378. System V C library function <TT>getopt()</TT>,
  379. but provides some additional capabilities.
  380. Built-ins that use <TT>optget()</TT> provide a more
  381. consistent user interface.
  382. <P>
  383. The <TT>optget()</TT> function is invoked as
  384. <DIV class=FI>
  385. <PRE>
  386. int optget(char *<EM>argv</EM>&#0091;&#0093;, const char *<EM>optstring</EM>)
  387. </DIV>
  388. </PRE>
  389. where <TT>argv</TT> is the argument list and <TT>optstring</TT>
  390. is a string that specifies the allowable arguments and
  391. additional information that is used to format <EM>usage</EM>
  392. messages.
  393. In fact a complete man page in <TT>troff</TT> or <TT>html</TT>
  394. can be generated by passing a usage string as described
  395. by the <TT>getopts</TT> command.
  396. Like <TT>getopt()</TT>,
  397. single letter options are represented by the letter itself,
  398. and options that take a string argument are followed by the <TT>:</TT>
  399. character.
  400. Option strings have the following special characters:
  401. <DIV class=SH>
  402. <DL>
  403. <DT><TT>:</TT><DD>
  404. Used after a letter option to indicate that the option
  405. takes an option argument.
  406. The variable <TT>opt_info.arg</TT> will point to this
  407. value after the given argument is encountered.
  408. <DT><TT>#</TT><DD>
  409. Used after a letter option to indicate that the option
  410. can only take a numerical value.
  411. The variable <TT>opt_info.num</TT> will contain this
  412. value after the given argument is encountered.
  413. <DT><TT>?</TT><DD>
  414. Used after a <TT>:</TT> or <TT>#</TT> (and after the optional <TT>?</TT>)
  415. to indicate the the
  416. preceding option argument is not required.
  417. <DT><TT>&#0091;</TT>...<TT>&#0093;</TT><DD><BR>
  418. After a <TT>:</TT> or <TT>#</TT>, the characters contained
  419. inside the brackets are used to identify the option
  420. argument when generating a <EM>usage</EM> message.
  421. <DT><EM>space</EM><DD><BR>
  422. The remainder of the string will only be used when generating
  423. usage messages.
  424. </DL>
  425. </DIV>
  426. <P>
  427. The <TT>optget()</TT> function returns the matching option letter if
  428. one of the legal option is matched.
  429. Otherwise, <TT>optget()</TT> returns
  430. <DIV class=SH>
  431. <DL>
  432. <DT><TT>':'</TT><DD>
  433. If there is an error. In this case the variable <TT>opt_info.arg</TT>
  434. contains the error string.
  435. <DT><TT>0</TT><DD>
  436. Indicates the end of options.
  437. The variable <TT>opt_info.index</TT> contains the number of arguments
  438. processed.
  439. <DT><TT>'?'</TT><DD>
  440. A usage message has been required.
  441. You normally call <TT>optusage()</TT> to generate and display
  442. the usage message.
  443. </DL>
  444. </DIV>
  445. <P>
  446. The following is an example of the option parsing portion
  447. of the <TT>wc</TT> utility.
  448. <DIV class=FI>
  449. <PRE>
  450. #include &lt;shell.h&gt;
  451. while(1) switch(n=optget(argv,"xf:&#0091;file&#0093;"))
  452. {
  453. case 'f':
  454. file = opt_info.arg;
  455. break;
  456. case ':':
  457. error(ERROR_exit(0), opt_info.arg);
  458. break;
  459. case '?':
  460. error(ERROR_usage(2), opt_info.arg);
  461. break;
  462. }
  463. </DIV>
  464. </PRE>
  465. <P>
  466. <H4><A name="Storage Management">Storage Management</A></H4>
  467. It is important that any memory used by your built-in
  468. be returned. Otherwise, if your built-in is called frequently,
  469. <TT>ksh</TT> will eventually run out of memory.
  470. You should avoid using <TT>malloc()</TT> for memory that must
  471. be freed before returning from you built-in, because by default,
  472. <TT>ksh</TT> will terminate you built-in in the event of an
  473. interrupt and the memory will not be freed.
  474. <P>
  475. The best way to allocate variable sized storage is
  476. through calls to the <STRONG>stak</STRONG> library
  477. which is included in <STRONG>libast</STRONG>
  478. and which is used extensively by <TT>ksh</TT> itself.
  479. Objects allocated with the <TT>stakalloc()</TT>
  480. function are freed when you function completes
  481. or aborts.
  482. The <STRONG>stak</STRONG> library provides a convenient way to
  483. build variable length strings and other objects dynamically.
  484. The man page for the <STRONG>stak</STRONG> library is contained
  485. in the Appendix.
  486. <P>
  487. Before <TT>ksh</TT> calls each built-in command, it saves
  488. the current stack location and restores it after
  489. it returns.
  490. It is not necessary to save and restore the stack
  491. location in the <TT>b_</TT> entry function,
  492. but you may want to write functions that use this stack
  493. are restore it when leaving the function.
  494. The following coding convention will do this in
  495. an efficient manner:
  496. <DIV class=FI>
  497. <PRE>
  498. <EM>yourfunction</EM>()
  499. {
  500. char *savebase;
  501. int saveoffset;
  502. if(saveoffset=staktell())
  503. savebase = stakfreeze(0);
  504. ...
  505. if(saveoffset)
  506. stakset(savebase,saveoffset);
  507. else
  508. stakseek(0);
  509. }
  510. </DIV>
  511. </PRE>
  512. <P>
  513. <P><HR><CENTER><FONT color=red><FONT face=courier><H3><A name="CALLING <TT>ksh</TT> SERVICES">CALLING <TT>ksh</TT> SERVICES</A></H3></FONT></FONT></CENTER>
  514. Some of the more interesting applications are those that extend
  515. the functionality of <TT>ksh</TT> in application specific directions.
  516. A prime example of this is the X-windows extension which adds
  517. builtins to create and delete widgets.
  518. The <STRONG>nval</STRONG> library is used to interface with the shell
  519. name space.
  520. The <STRONG>shell</STRONG> library is used to access other shell services.
  521. <P>
  522. <H4><A name="The nval library">The nval library</A></H4>
  523. A great deal of power is derived from the ability to use
  524. portions of the hierarchal variable namespace provided by <TT>ksh-93</TT>
  525. and turn these names into active objects.
  526. <P>
  527. The <STRONG>nval</STRONG> library is used to interface with shell
  528. variables.
  529. A man page for this file is provided in an Appendix.
  530. You need to include the header <TT>&lt;nval.h&gt;</TT>
  531. to access the functions defined in the <STRONG>nval</STRONG> library.
  532. All the functions provided by the <STRONG>nval</STRONG> library begin
  533. with the prefix <TT>nv_</TT>.
  534. Each shell variable is an object in an associative table
  535. that is referenced by name.
  536. The type <TT>Namval_t*</TT> is pointer to a shell variable.
  537. To operate on a shell variable, you first get a handle
  538. to the variable with the <TT>nv_open()</TT> function
  539. and then supply the handle returned as the first
  540. argument of the function that provides an operation
  541. on the variable.
  542. You must call <TT>nv_close()</TT> when you are finished
  543. using this handle so that the space can be freed once
  544. the value is unset.
  545. The two most frequent operations are to get the value of
  546. the variable, and to assign value to the variable.
  547. The <TT>nv_getval()</TT> returns a pointer the the
  548. value of the variable.
  549. In some cases the pointer returned is to a region that
  550. will be overwritten by the next <TT>nv_getval()</TT> call
  551. so that if the value isn't used immediately, it should
  552. be copied.
  553. Many variables can also generate a numeric value.
  554. The <TT>nv_getnum()</TT> function returns a numeric
  555. value for the given variable pointer, calling the
  556. arithmetic evaluator if necessary.
  557. <P>
  558. The <TT>nv_putval()</TT> function is used to assign a new
  559. value to a given variable.
  560. The second argument to <TT>putval()</TT> is the value
  561. to be assigned
  562. and the third argument is a <EM>flag</EM> which
  563. is used in interpreting the second argument.
  564. <P>
  565. Each shell variable can have one or more attributes.
  566. The <TT>nv_isattr()</TT> is used to test for the existence
  567. of one or more attributes.
  568. See the appendix for a complete list of attributes.
  569. <P>
  570. By default, each shell variable passively stores the string you
  571. give with with <TT>nv_putval()</TT>, and returns the value
  572. with <TT>getval()</TT>. However, it is possible to turn
  573. any node into an active entity by assigning functions
  574. to it that will be called whenever <TT>nv_putval()</TT>
  575. and/or <TT>nv_getval()</TT> is called.
  576. In fact there are up to five functions that can
  577. associated with each variable to override the
  578. default actions.
  579. The type <TT>Namfun_t</TT> is used to define these functions.
  580. Only those that are non-<TT>NULL</TT> override the
  581. default actions.
  582. To override the default actions, you must allocate an
  583. instance of <TT>Namfun_t</TT>, and then assign
  584. the functions that you wish to override.
  585. The <TT>putval()</TT>
  586. function is called by the <TT>nv_putval()</TT> function.
  587. A <TT>NULL</TT> for the <EM>value</EM> argument
  588. indicates a request to unset the variable.
  589. The <EM>type</EM> argument might contain the <TT>NV_INTEGER</TT>
  590. bit so you should be prepared to do a conversion if
  591. necessary.
  592. The <TT>getval()</TT>
  593. function is called by <TT>nv_getval()</TT>
  594. value and must return a string.
  595. The <TT>getnum()</TT>
  596. function is called by by the arithmetic evaluator
  597. and must return double.
  598. If omitted, then it will call <TT>nv_getval()</TT> and
  599. convert the result to a number.
  600. <P>
  601. The functionality of a variable can further be increased
  602. by adding discipline functions that
  603. can be associated with the variable.
  604. A discipline function allows a script that uses your
  605. variable to define functions whose name is
  606. <EM>varname</EM><TT>.</TT><EM>discname</EM>
  607. where <EM>varname</EM> is the name of the variable, and <EM>discname</EM>
  608. is the name of the discipline.
  609. When the user defines such a function, the <TT>settrap()</TT>
  610. function will be called with the name of the discipline and
  611. a pointer to the parse tree corresponding to the discipline
  612. function.
  613. The application determines when these functions are actually
  614. executed.
  615. By default, <TT>ksh</TT> defines <TT>get</TT>,
  616. <TT>set</TT>, and <TT>unset</TT> as discipline functions.
  617. <P>
  618. In addition, it is possible to provide a data area that
  619. will be passed as an argument to
  620. each of these functions whenever any of these functions are called.
  621. To have private data, you need to define and allocate a structure
  622. that looks like
  623. <DIV class=FI>
  624. <PRE>
  625. struct <EM>yours</EM>
  626. {
  627. Namfun_t fun;
  628. <EM>your_data_fields</EM>;
  629. };
  630. </DIV>
  631. </PRE>
  632. <P>
  633. <H4><A name="The shell library">The shell library</A></H4>
  634. There are several functions that are used by <TT>ksh</TT> itself
  635. that can also be called from built-in commands.
  636. The man page for these routines are in the Appendix.
  637. <P>
  638. The <TT>sh_addbuiltin()</TT> function can be used to add or delete
  639. builtin commands. It takes the name of the built-in, the
  640. address of the function that implements the built-in, and
  641. a <TT>void*</TT> pointer that will be passed to this function
  642. as the third argument whenever it is invoked.
  643. If the function address is <TT>NULL</TT>, the specified built-in
  644. will be deleted. However, special built-in functions cannot
  645. be deleted or modified.
  646. <P>
  647. The <TT>sh_fmtq()</TT> function takes a string and returns
  648. a string that is quoted as necessary so that it can
  649. be used as shell input.
  650. This function is used to implement the <TT>%q</TT> option
  651. of the shell built-in <TT>printf</TT> command.
  652. <P>
  653. The <TT>sh_parse()</TT> function returns a parse tree corresponding
  654. to a give file stream. The tree can be executed by supplying
  655. it as the first argument to
  656. the <TT>sh_trap()</TT> function and giving a value of <TT>1</TT> as the
  657. second argument.
  658. Alternatively, the <TT>sh_trap()</TT> function can parse and execute
  659. a string by passing the string as the first argument and giving <TT>0</TT>
  660. as the second argument.
  661. <P>
  662. The <TT>sh_isoption()</TT> function can be used to set to see whether one
  663. or more of the option settings is enabled.
  664. </DIV>
  665. <P><HR><CENTER><FONT color=red><FONT face=courier><H3><A name="References">References</A></H3></FONT></FONT></CENTER>
  666. <P>
  667. <DL compact>
  668. <DT>[1]<DD>
  669. <EM>POSIX &#45; Part 2: Shell and Utilities,</EM>
  670. IEEE Std 1003.2-1992, ISO/IEC 9945-2:1993.
  671. <DT>[2]<DD>
  672. Glenn Fowler,
  673. <EM>A Case for make</EM>,
  674. Software - Practice and Experience, Vol. 20 No. S1, pp. 30-46, June 1990.
  675. <DT>[3]<DD>
  676. Brian W. Kernighan and Dennis M. Ritchie,
  677. <EM>The C Programming Language</EM>,
  678. Prentice Hall, 1978.
  679. <DT>[4]<DD>
  680. American National Standard for Information Systems &#45; Programming
  681. Language &#45; C, ANSI X3.159-1989.
  682. <DT>[5]<DD>
  683. Bjarne Stroustroup,
  684. <EM>C++</EM>,
  685. Addison Wesley, xxxx
  686. <DT>[6]<DD>
  687. <EM>POSIX &#45; Part 1: System Application Program Interface,</EM>
  688. IEEE Std 1003.1-1990, ISO/IEC 9945-1:1990.
  689. <DT>[7]<DD>
  690. David Korn and Kiem-Phong Vo,
  691. <EM>SFIO - A Safe/Fast Input/Output library,</EM>
  692. Proceedings of the Summer Usenix,
  693. pp. , 1991.
  694. </DL>
  695. <P>
  696. <HR>
  697. <TABLE border=0 align=center width=96%>
  698. <TR>
  699. <TD align=left></TD>
  700. <TD align=center></TD>
  701. <TD align=right>March 13, 2012</TD>
  702. </TR>
  703. </TABLE>
  704. <P>
  705. </TD></TR></TBODY></TABLE>
  706. </BODY>
  707. </HTML>