| 1 |
<?xml version="1.0" encoding="utf-8"?> |
| 2 |
<!DOCTYPE book [ |
| 3 |
<!ENTITY % eclent SYSTEM "ecl.ent"> |
| 4 |
%eclent; |
| 5 |
]> |
| 6 |
<book xmlns="http://docbook.org/ns/docbook" version="5.0" xml:lang="en"><reference xml:id="os.dict"><title>OS Reference</title> |
| 7 |
|
| 8 |
<!-- ====================================================================== --> |
| 9 |
<!-- EXT:*HELP-MESSAGE* --> |
| 10 |
<!-- ====================================================================== --> |
| 11 |
|
| 12 |
<refentry xml:id="ref._help-message_"> |
| 13 |
<refnamediv> |
| 14 |
<refname><varname>ext:*help-message*</varname></refname> |
| 15 |
<refpurpose>Command line help message</refpurpose> |
| 16 |
</refnamediv> |
| 17 |
|
| 18 |
<refsynopsisdiv> |
| 19 |
<title>Variable</title> |
| 20 |
<variablelist> |
| 21 |
<varlistentry> |
| 22 |
<term>Type</term> |
| 23 |
<listitem><para>A string</para></listitem> |
| 24 |
</varlistentry> |
| 25 |
<varlistentry> |
| 26 |
<term>Initial value</term> |
| 27 |
<listitem><para>&ECL; help message</para></listitem> |
| 28 |
</varlistentry> |
| 29 |
</variablelist> |
| 30 |
</refsynopsisdiv> |
| 31 |
|
| 32 |
<refsect1> |
| 33 |
<title>Description</title> |
| 34 |
|
| 35 |
<para>This variable contains the help message which is output when &ECL; is |
| 36 |
invoked with the <option>--help</option>.</para> |
| 37 |
</refsect1> |
| 38 |
|
| 39 |
<refsect1> |
| 40 |
<title>Example</title> |
| 41 |
<para>See <xref linkend="ref.process-command-args"/>.</para> |
| 42 |
</refsect1> |
| 43 |
</refentry> |
| 44 |
|
| 45 |
<!-- ====================================================================== --> |
| 46 |
<!-- EXT:+LISP-INIT-FILES+ --> |
| 47 |
<!-- ====================================================================== --> |
| 48 |
|
| 49 |
<refentry xml:id="ref._lisp-init-file-list_"> |
| 50 |
<refnamediv> |
| 51 |
<refname><varname>ext:*lisp-init-file-list*</varname></refname> |
| 52 |
<refpurpose>&ECL; initialization files.</refpurpose> |
| 53 |
</refnamediv> |
| 54 |
|
| 55 |
<refsynopsisdiv> |
| 56 |
<title>Constant</title> |
| 57 |
<variablelist> |
| 58 |
<varlistentry> |
| 59 |
<term>Type</term> |
| 60 |
<listitem><para>A list of pathname designators.</para></listitem> |
| 61 |
</varlistentry> |
| 62 |
<varlistentry> |
| 63 |
<term>Initial value</term> |
| 64 |
<listitem><para><literal>'("~/.ecl" "~/.eclrc")</literal></para></listitem> |
| 65 |
</varlistentry> |
| 66 |
</variablelist> |
| 67 |
</refsynopsisdiv> |
| 68 |
|
| 69 |
<refsect1> |
| 70 |
<title>Description</title> |
| 71 |
|
| 72 |
<para>This variable contains the names of initialization files that are |
| 73 |
loaded by &ECL; or embedding programs. The loading of initialization files |
| 74 |
happens automatically in &ECL; unless invoked with the option |
| 75 |
<option>-norc</option>. Whether initialization files are loaded or not is |
| 76 |
controlled by the command line options rules, as described in <xref |
| 77 |
linkend="ref.process-command-args"/>.</para> |
| 78 |
</refsect1> |
| 79 |
|
| 80 |
<refsect1> |
| 81 |
<title>Example</title> |
| 82 |
<para>See <xref linkend="ref.process-command-args"/>.</para> |
| 83 |
</refsect1> |
| 84 |
</refentry> |
| 85 |
|
| 86 |
<!-- ====================================================================== --> |
| 87 |
<!-- EXT:+DEFAULT-COMMAND-ARG-RULES+ --> |
| 88 |
<!-- ====================================================================== --> |
| 89 |
|
| 90 |
<refentry xml:id="ref._default-command-arg-rules_"> |
| 91 |
<refnamediv> |
| 92 |
<refname><varname>ext:+default-command-arg-rules+</varname></refname> |
| 93 |
<refpurpose>&ECL; command line options</refpurpose> |
| 94 |
</refnamediv> |
| 95 |
|
| 96 |
<refsynopsisdiv> |
| 97 |
<title>Constant</title> |
| 98 |
<variablelist> |
| 99 |
<varlistentry> |
| 100 |
<term>Type</term> |
| 101 |
<listitem><para>A list of lists.</para></listitem> |
| 102 |
</varlistentry> |
| 103 |
</variablelist> |
| 104 |
</refsynopsisdiv> |
| 105 |
|
| 106 |
<refsect1> |
| 107 |
<title>Description</title> |
| 108 |
|
| 109 |
<para>This constant contains a list of rules for parsing the command line |
| 110 |
arguments. This list is made of all the options which &ECL; accepts by |
| 111 |
default. It can be passed as first argument to <xref |
| 112 |
linkend="ref.process-command-args"/>, and you can use it as a starting point |
| 113 |
to extend &ECL;.</para> |
| 114 |
</refsect1> |
| 115 |
|
| 116 |
<refsect1> |
| 117 |
<title>Example</title> |
| 118 |
<para>See <xref linkend="ref.process-command-args"/>.</para> |
| 119 |
</refsect1> |
| 120 |
</refentry> |
| 121 |
|
| 122 |
<!-- ====================================================================== --> |
| 123 |
<!-- EXT:COMMAND-ARGS --> |
| 124 |
<!-- ====================================================================== --> |
| 125 |
|
| 126 |
<refentry xml:id="ref.command-args"> |
| 127 |
<refnamediv> |
| 128 |
<refname><function>ext:command-args</function></refname> |
| 129 |
<refpurpose>Original list of command line arguments.</refpurpose> |
| 130 |
</refnamediv> |
| 131 |
|
| 132 |
<refsynopsisdiv> |
| 133 |
<title>Function</title> |
| 134 |
<funcsynopsis> |
| 135 |
<funcprototype> |
| 136 |
<funcdef>ext:command-args</funcdef> |
| 137 |
<void></void> |
| 138 |
</funcprototype> |
| 139 |
</funcsynopsis> |
| 140 |
<variablelist> |
| 141 |
<varlistentry> |
| 142 |
<term>returns</term> |
| 143 |
<listitem><para>A list of strings.</para></listitem> |
| 144 |
</varlistentry> |
| 145 |
</variablelist> |
| 146 |
</refsynopsisdiv> |
| 147 |
|
| 148 |
<refsect1> |
| 149 |
<title>Description</title> |
| 150 |
|
| 151 |
<para>This function returns the list of command line arguments passed to |
| 152 |
either &ECL; or the program it was embedded in. The output is a list of |
| 153 |
strings and it corresponds to the <varname>argv</varname> vector in a C |
| 154 |
program. Typically, the first argument is the name of the program as it was |
| 155 |
invoked. You should not count on ths filename to be resolved.</para> |
| 156 |
</refsect1> |
| 157 |
|
| 158 |
</refentry> |
| 159 |
|
| 160 |
<!-- ====================================================================== --> |
| 161 |
<!-- EXT:PROCESS-COMMAND-ARGS --> |
| 162 |
<!-- ====================================================================== --> |
| 163 |
|
| 164 |
<refentry xml:id="ref.process-command-args"> |
| 165 |
<refnamediv> |
| 166 |
<refname><function>ext:process-command-args</function></refname> |
| 167 |
<refpurpose>Process command line arguments.</refpurpose> |
| 168 |
</refnamediv> |
| 169 |
|
| 170 |
<refsynopsisdiv> |
| 171 |
<title>Function</title> |
| 172 |
<funcsynopsis> |
| 173 |
<funcprototype> |
| 174 |
<funcdef>ext:process-command-args</funcdef> |
| 175 |
<paramdef><parameter>&key;</parameter></paramdef> |
| 176 |
<paramdef><parameter>args</parameter></paramdef> |
| 177 |
<paramdef><parameter>rules</parameter></paramdef> |
| 178 |
</funcprototype> |
| 179 |
</funcsynopsis> |
| 180 |
<variablelist> |
| 181 |
<varlistentry> |
| 182 |
<term><replaceable>args</replaceable></term> |
| 183 |
<listitem><para>A list of strings. Defaults to the output of <xref |
| 184 |
linkend="ref.command-args"/></para></listitem> |
| 185 |
</varlistentry> |
| 186 |
<varlistentry> |
| 187 |
<term><replaceable>rules</replaceable></term> |
| 188 |
<listitem><para>A list of lists. Defaults to the value of <xref |
| 189 |
linkend="ref._default-command-arg-rules_"/></para></listitem> |
| 190 |
</varlistentry> |
| 191 |
</variablelist> |
| 192 |
</refsynopsisdiv> |
| 193 |
|
| 194 |
<refsect1> |
| 195 |
<title>Description</title> |
| 196 |
|
| 197 |
<para>This function processes the command line arguments passed to either |
| 198 |
&ECL; or the program that embeds it. It uses the list of rules |
| 199 |
<replaceable>rules</replaceable>, which has the following syntax: |
| 200 |
<synopsis>(option-name nargs template [:stop | :noloadrc | :loadrc]*)</synopsis> |
| 201 |
</para> |
| 202 |
<variablelist> |
| 203 |
<varlistentry> |
| 204 |
<term><replaceable>option-name</replaceable></term> |
| 205 |
<listitem><para>A string with the option prefix as typed by the user. For |
| 206 |
instance <option>--help</option>, <option>-?</option>, |
| 207 |
<option>--compile</option>, etc.</para></listitem> |
| 208 |
</varlistentry> |
| 209 |
<varlistentry> |
| 210 |
<term><replaceable>nargs</replaceable></term> |
| 211 |
<listitem><para>A nonnegative integer denoting the number of arguments |
| 212 |
taken by this option.</para></listitem> |
| 213 |
</varlistentry> |
| 214 |
<varlistentry> |
| 215 |
<term><replaceable>template</replaceable></term> |
| 216 |
<listitem><para>A lisp form, not evaluated, where numbers from 0 to |
| 217 |
<replaceable>nargs</replaceable> will be replaced by the corresponding |
| 218 |
option argument.</para></listitem> |
| 219 |
</varlistentry> |
| 220 |
<varlistentry> |
| 221 |
<term><symbol>:STOP</symbol></term> |
| 222 |
<listitem><para>If present, parsing of arguments stops after this option |
| 223 |
is found and processed. The list of remaining arguments is passed to |
| 224 |
the rule. &ECL;'s top-level uses this option with the <symbol>--</symbol> |
| 225 |
command line option to set <symbol>ext:*unprocessed-ecl-command-args*</symbol> |
| 226 |
to the list of remaining arguments.</para></listitem> |
| 227 |
</varlistentry> |
| 228 |
<varlistentry> |
| 229 |
<term><symbol>:NOLOADRC</symbol> and <symbol>:LOADRC</symbol></term> |
| 230 |
<listitem><para>Determine whether the lisp initalization file (<xref |
| 231 |
linkend="ref._lisp-init-file-list_"/>) will be loaded before processing |
| 232 |
all forms.</para></listitem> |
| 233 |
</varlistentry> |
| 234 |
</variablelist> |
| 235 |
|
| 236 |
<para><function>EXT:PROCESS-COMMAND-ARGS</function> works as follows. First |
| 237 |
of all, it parses all the command line arguments, except for the first one, |
| 238 |
which is assumed to contain the program name. Each of these arguments is |
| 239 |
matched against the rules, sequentially, until one of the patterns |
| 240 |
succeeeds.</para> |
| 241 |
|
| 242 |
<para>A special name <literal>"*DEFAULT*"</literal>, matches any unknown |
| 243 |
command line option. If there is no <literal>"*DEFAULT*"</literal> rule and |
| 244 |
no match is found, an error is signalled. For each rule that succeeds, the |
| 245 |
function constructs a lisp statement using the |
| 246 |
<replaceable>template</replaceable>.</para> |
| 247 |
|
| 248 |
<para>After all arguments have been processed, |
| 249 |
<function>EXT:PROCESS-COMMAND-ARGS</function>, and there were no occurences |
| 250 |
of <symbol>:NOLOADRC</symbol>, one of the files listed in <xref |
| 251 |
linkend="ref._lisp-init-file-list_"/> will be loaded. Finally, the list of |
| 252 |
lisp statements will be evaluated.</para> |
| 253 |
</refsect1> |
| 254 |
|
| 255 |
<refsect1> |
| 256 |
<title>Example</title> |
| 257 |
|
| 258 |
<para>The following piece of code implements the <command>ls</command> |
| 259 |
command using lisp.<footnote><para>Instructions for building this program |
| 260 |
are found under |
| 261 |
<filename>ecl/examples/cmdline/ls.lsp</filename></para></footnote></para> |
| 262 |
<!-- When changing, check ecl/examples/cmdline/ls.lsp --> |
| 263 |
<programlisting> |
| 264 |
(setq ext:*help-message* " |
| 265 |
ls [--help | -?] filename* |
| 266 |
Lists the file that match the given patterns. |
| 267 |
") |
| 268 |
|
| 269 |
(defun print-directory (pathnames) |
| 270 |
(format t "~{~A~%~}" |
| 271 |
(mapcar #'(lambda (x) (enough-namestring x (si::getcwd))) |
| 272 |
(mapcan #'directory (or pathnames '("*.*" "*/")))))) |
| 273 |
|
| 274 |
(defconstant +ls-rules+ |
| 275 |
'(("--help" 0 (progn (princ ext:*help-message* *standard-output*) (ext:quit 0))) |
| 276 |
("-?" 0 (progn (princ ext:*help-message* *standard-output*) (ext:quit 0))) |
| 277 |
("*DEFAULT*" 1 (print-directory 1) :stop))) |
| 278 |
|
| 279 |
(let ((ext:*lisp-init-file-list* NIL)) ; No initialization files |
| 280 |
(handler-case (ext:process-command-args :rules +ls-rules+) |
| 281 |
(error (c) |
| 282 |
(princ ext:*help-message* *error-output*) |
| 283 |
(ext:quit 1)))) |
| 284 |
(ext:quit 0) |
| 285 |
</programlisting> |
| 286 |
</refsect1> |
| 287 |
</refentry> |
| 288 |
|
| 289 |
<!-- ====================================================================== --> |
| 290 |
<!-- EXT:QUIT --> |
| 291 |
<!-- ====================================================================== --> |
| 292 |
|
| 293 |
<refentry xml:id="ref.quit"> |
| 294 |
<refnamediv> |
| 295 |
<refname><function>ext:quit</function></refname> |
| 296 |
<refpurpose>Exit &ECL;.</refpurpose> |
| 297 |
</refnamediv> |
| 298 |
|
| 299 |
<refsynopsisdiv> |
| 300 |
<title>Function</title> |
| 301 |
<funcsynopsis> |
| 302 |
<funcprototype> |
| 303 |
<funcdef>ext:quit</funcdef> |
| 304 |
<paramdef>&optional;</paramdef> |
| 305 |
<paramdef><parameter>exit-code</parameter></paramdef> |
| 306 |
</funcprototype> |
| 307 |
</funcsynopsis> |
| 308 |
<variablelist> |
| 309 |
<varlistentry> |
| 310 |
<term><replaceable>exit-code</replaceable></term> |
| 311 |
<listitem><para>An integer between 0 and 255</para></listitem> |
| 312 |
</varlistentry> |
| 313 |
</variablelist> |
| 314 |
</refsynopsisdiv> |
| 315 |
|
| 316 |
<refsect1> |
| 317 |
<title>Description</title> |
| 318 |
|
| 319 |
<para>This function abruptly stops the execution of the program in which |
| 320 |
&ECL; is embedded. Depending on the platform, several other functions will |
| 321 |
be invoked to free resources, close loaded modules, etc.</para> |
| 322 |
|
| 323 |
<para>The exit code is the code seen by the parent process that invoked |
| 324 |
this program. Normally a code other than zero denotes an error.</para> |
| 325 |
</refsect1> |
| 326 |
</refentry> |
| 327 |
|
| 328 |
<!-- ====================================================================== --> |
| 329 |
<!-- EXT:EXTERNAL-PROCESS --> |
| 330 |
<!-- ====================================================================== --> |
| 331 |
|
| 332 |
<refentry xml:id="ref.external-process"> |
| 333 |
<refnamediv> |
| 334 |
<refname><function>ext:external-process</function></refname> |
| 335 |
<refpurpose>Holds process state.</refpurpose> |
| 336 |
</refnamediv> |
| 337 |
|
| 338 |
<refsynopsisdiv> |
| 339 |
<title>Structure</title> |
| 340 |
<synopsis><type>ext:external-process</type></synopsis> |
| 341 |
<variablelist> |
| 342 |
<varlistentry> |
| 343 |
<term><replaceable>PID</replaceable></term> |
| 344 |
<listitem><para>process pid</para></listitem> |
| 345 |
</varlistentry> |
| 346 |
<varlistentry> |
| 347 |
<term><replaceable>INPUT</replaceable></term> |
| 348 |
<listitem><para>process input stream</para></listitem> |
| 349 |
</varlistentry> |
| 350 |
<varlistentry> |
| 351 |
<term><replaceable>OUTPUT</replaceable></term> |
| 352 |
<listitem><para>process output stream</para></listitem> |
| 353 |
</varlistentry> |
| 354 |
<varlistentry> |
| 355 |
<term><replaceable>ERROR-STREAM</replaceable></term> |
| 356 |
<listitem><para>process error stream</para></listitem> |
| 357 |
</varlistentry> |
| 358 |
<varlistentry> |
| 359 |
<term><replaceable>%STATUS</replaceable></term> <listitem><para>either |
| 360 |
<symbol>:RUNNING</symbol>, <symbol>:STOPPED</symbol>, |
| 361 |
<symbol>:SIGNALED</symbol>, <symbol>:EXITED</symbol></para></listitem>, |
| 362 |
<symbol>:ERROR</symbol> or <symbol>:ABORT</symbol> |
| 363 |
</varlistentry> |
| 364 |
<varlistentry> |
| 365 |
<term><replaceable>%CODE</replaceable></term> |
| 366 |
<listitem><para>exit code or nil (if still running)</para></listitem> |
| 367 |
</varlistentry> |
| 368 |
</variablelist> |
| 369 |
</refsynopsisdiv> |
| 370 |
|
| 371 |
<refsect1> |
| 372 |
<title>Description</title> |
| 373 |
<para>This structure is responsible for holding external process state - |
| 374 |
process id, communication streams, runtime status and exit code. It is |
| 375 |
returned as third value of <xref linkend="ref.run-program"/>.</para> |
| 376 |
</refsect1> |
| 377 |
</refentry> |
| 378 |
|
| 379 |
<!-- ====================================================================== --> |
| 380 |
<!-- EXT:EXTERNAL-PROCESS-STATUS --> |
| 381 |
<!-- ====================================================================== --> |
| 382 |
<refentry xml:id="ref.external-process-status"> |
| 383 |
<refnamediv> |
| 384 |
<refname><function>ext:external-process-status</function></refname> |
| 385 |
<refpurpose>Check status of <xref |
| 386 |
linkend="ref.external-process"/>.</refpurpose> |
| 387 |
</refnamediv> |
| 388 |
|
| 389 |
<refsynopsisdiv> |
| 390 |
<title>Function</title> |
| 391 |
<funcsynopsis> |
| 392 |
<funcprototype> |
| 393 |
<funcdef>ext:external-process-status</funcdef> |
| 394 |
<paramdef><parameter>external-process</parameter></paramdef> |
| 395 |
</funcprototype> |
| 396 |
</funcsynopsis> |
| 397 |
|
| 398 |
<refsect1> |
| 399 |
<title>Description</title> |
| 400 |
|
| 401 |
<para>Checks status of external-process. Returns it's current status, and |
| 402 |
if exited - <symbol>EXIT-CODE</symbol> as second value.</para> |
| 403 |
</refsect1> |
| 404 |
</refsynopsisdiv> |
| 405 |
</refentry> |
| 406 |
|
| 407 |
<!-- ====================================================================== |
| 408 |
--> |
| 409 |
<!-- EXT:RUN-PROGRAM --> |
| 410 |
<!-- ====================================================================== --> |
| 411 |
<refentry xml:id="ref.run-program"> |
| 412 |
<refnamediv> |
| 413 |
<refname><function>ext:run-program</function></refname> |
| 414 |
<refpurpose>Start and communicate with a child process.</refpurpose> |
| 415 |
</refnamediv> |
| 416 |
|
| 417 |
<refsynopsisdiv> |
| 418 |
<title>Function</title> |
| 419 |
<funcsynopsis> |
| 420 |
<funcprototype> |
| 421 |
<funcdef>ext:run-program</funcdef> |
| 422 |
<paramdef><parameter>command</parameter></paramdef> |
| 423 |
<paramdef><parameter>argv</parameter></paramdef> |
| 424 |
<paramdef>&key;</paramdef> |
| 425 |
<paramdef>input</paramdef> |
| 426 |
<paramdef>output</paramdef> |
| 427 |
<paramdef>error</paramdef> |
| 428 |
<paramdef>wait</paramdef> |
| 429 |
<paramdef>environ</paramdef> |
| 430 |
<paramdef>if-input-does-exist</paramdef> |
| 431 |
<paramdef>if-output-exists</paramdef> |
| 432 |
<paramdef>if-error-exists</paramdef> |
| 433 |
<paramdef>external-format</paramdef> |
| 434 |
</funcprototype> |
| 435 |
</funcsynopsis> |
| 436 |
<variablelist> |
| 437 |
<varlistentry> |
| 438 |
<term><replaceable>input</replaceable></term> |
| 439 |
<listitem><para>One of <symbol>:STREAM</symbol>, <symbol>T</symbol>, |
| 440 |
<symbol>NIL</symbol>, string or pathname - defaults to |
| 441 |
<symbol>:STREAM</symbol></para></listitem> |
| 442 |
</varlistentry> |
| 443 |
<varlistentry> |
| 444 |
<term><replaceable>output</replaceable></term> |
| 445 |
<listitem><para>One of <symbol>:STREAM</symbol>, <symbol>T</symbol>, |
| 446 |
<symbol>NIL</symbol>, string or pathname - defaults to |
| 447 |
<symbol>:STREAM</symbol></para></listitem> |
| 448 |
</varlistentry> |
| 449 |
<varlistentry> |
| 450 |
<term><replaceable>error</replaceable></term> |
| 451 |
<listitem><para>One of <symbol>:OUTPUT</symbol>, <symbol>:STREAM</symbol>, |
| 452 |
<symbol>T</symbol>, <symbol>NIL</symbol>, string or pathname - defaults to |
| 453 |
<symbol>:OUTPUT</symbol></para></listitem> |
| 454 |
</varlistentry> |
| 455 |
<varlistentry> |
| 456 |
<term><replaceable>wait</replaceable></term> |
| 457 |
<listitem><para>Boolean indicating if process should run |
| 458 |
synchronously, defaults to T</para></listitem> |
| 459 |
</varlistentry> |
| 460 |
<varlistentry> |
| 461 |
<term><replaceable>environ</replaceable></term> |
| 462 |
<listitem><para>List of strings containing environment bindings. Defaults |
| 463 |
to NIL (inheritance of lisp process environment).</para></listitem> |
| 464 |
</varlistentry> |
| 465 |
<varlistentry> |
| 466 |
<term><replaceable>if-input-does-not-exist</replaceable></term> |
| 467 |
<listitem><para>If <symbol>:INPUT</symbol> is pathname this symbol value |
| 468 |
is provided as <symbol>:IF-DOES-NOT-EXIST</symbol> for |
| 469 |
<symbol>CL_OPEN</symbol>. Defaults to |
| 470 |
<symbol>NIL</symbol></para></listitem> |
| 471 |
</varlistentry> |
| 472 |
<varlistentry> |
| 473 |
<term><replaceable>if-output-exists</replaceable></term> |
| 474 |
<listitem><para>If <symbol>:OUTPUT</symbol> is pathname this symbol value |
| 475 |
is provided as <symbol>:IF-EXISTS</symbol> for |
| 476 |
<symbol>CL_OPEN</symbol>. Defaults to |
| 477 |
<symbol>:ERROR</symbol></para></listitem> |
| 478 |
</varlistentry> |
| 479 |
<varlistentry> |
| 480 |
<term><replaceable>if-error-exists</replaceable></term> |
| 481 |
<listitem><para>If <symbol>:ERROR</symbol> is pathname this symbol value |
| 482 |
is provided as <symbol>:IF-EXISTS</symbol> for |
| 483 |
<symbol>CL_OPEN</symbol>. Defaults to |
| 484 |
<symbol>:ERROR</symbol></para></listitem> |
| 485 |
</varlistentry> |
| 486 |
<varlistentry> |
| 487 |
<term><replaceable>external-format</replaceable></term> |
| 488 |
<listitem><para>External format used for both streams and |
| 489 |
pathnames. Defaults to <symbol>:DEFAULT</symbol></para></listitem> |
| 490 |
</varlistentry> |
| 491 |
<varlistentry> |
| 492 |
<term>returns</term> |
| 493 |
<listitem><para>Two-way stream responsible for input/output with created |
| 494 |
process (created of <symbol>:OUTPUT</symbol> and |
| 495 |
<symbol>:INPUT</symbol>),</para></listitem> |
| 496 |
<listitem><para>Return code of spawned process. If process still runs then |
| 497 |
<symbol>NIL</symbol></para></listitem> |
| 498 |
<listitem><para><xref linkend="ref.external-process"/> structure holding |
| 499 |
process state.</para></listitem> |
| 500 |
</varlistentry> |
| 501 |
</variablelist> |
| 502 |
</refsynopsisdiv> |
| 503 |
|
| 504 |
<refsect1> |
| 505 |
<title>Description</title> |
| 506 |
|
| 507 |
<para>This function creates a external process by launching the program |
| 508 |
<replaceable>command</replaceable> with the arguments from the list |
| 509 |
<replaceable>argv</replaceable>.</para> |
| 510 |
|
| 511 |
<para>The arguments <replaceable>input</replaceable>, |
| 512 |
<replaceable>output</replaceable> and <replaceable>error</replaceable> are |
| 513 |
used to intercept the standard input, output and error streams of the |
| 514 |
program. A value of <symbol>:STREAM</symbol> means a lisp stream will be |
| 515 |
created to communicate with the child process. A value of |
| 516 |
<symbol>NIL</symbol> means that the data from this pipe will be |
| 517 |
discarded. The value of <symbol>T</symbol> means that the child process will |
| 518 |
use the parent's standard input, output or error channels. If value is |
| 519 |
pathname, then corresponding file will be used (strings are coerced to |
| 520 |
pathnames).</para> |
| 521 |
|
| 522 |
<para>For instance, if &ECL; writes to the console and you pass a value of |
| 523 |
<replaceable>output</replaceable> equal to <symbol>T</symbol>, the child |
| 524 |
process will also output to the console. Finally, the error messages of the |
| 525 |
child process are redirected to the same pipe as its standard output when |
| 526 |
<replaceable>error</replaceable> takes the value |
| 527 |
<symbol>:OUTPUT</symbol>.</para> |
| 528 |
|
| 529 |
<para>If the child process was succesfully launched, this function outputs a |
| 530 |
lisp stream to which we one may write, read or do both things, depending on |
| 531 |
the arguments <replaceable>input</replaceable> and |
| 532 |
<replaceable>output</replaceable>. If an error happened during the |
| 533 |
preparation of the child process (for instance the program was not found), |
| 534 |
this function returns <replaceable>NIL</replaceable>.</para> |
| 535 |
|
| 536 |
<para>Function returns three values - two-way stream for communication, |
| 537 |
return-code or nil depending if process is called asynchronously, and <xref |
| 538 |
linkend="ref.external-process"/> structure holding process state.</para> |
| 539 |
|
| 540 |
<para>The design of this function is inspired by the function of same name |
| 541 |
in &CMUCL; and &SBCL;.</para> |
| 542 |
</refsect1> |
| 543 |
|
| 544 |
<refsect1> |
| 545 |
<title>Example</title> |
| 546 |
|
| 547 |
<para>List all users in a Unix system. We use the <command>sed</command> |
| 548 |
command to parse the file with the list of users, removing comments and |
| 549 |
information other than the user names: |
| 550 |
<programlisting> |
| 551 |
(defun all-users (&optional (file "/etc/passwd")) |
| 552 |
(let ((s (ext:run-program "sed" |
| 553 |
(list "-e" "/^#.*$/d;/^[^:]*$/d;s,^\\([^:]*\\).*$,\\1,g" |
| 554 |
file) |
| 555 |
:input NIL :output :STREAM :error NIL))) |
| 556 |
(unless s |
| 557 |
(error "Unable to parse password file")) |
| 558 |
(loop for x = (read s NIL NIL) |
| 559 |
while x |
| 560 |
collect x))) |
| 561 |
</programlisting></para> |
| 562 |
|
| 563 |
<para>Make a directory. Redirect standard error output to the same as the |
| 564 |
output: |
| 565 |
<programlisting>(ext:run-program "mkdir" '("./tmp") :output :STREAM :error :OUTPUT) |
| 566 |
</programlisting> |
| 567 |
</para> |
| 568 |
|
| 569 |
<para>Same as before, but now both the output and the standard error are |
| 570 |
discarded |
| 571 |
<programlisting>(ext:run-program "mkdir" '("./tmp") :output NIL :error :OUTPUT) |
| 572 |
</programlisting> |
| 573 |
</para> |
| 574 |
</refsect1> |
| 575 |
|
| 576 |
<refsect1> |
| 577 |
<title>Limitations</title> |
| 578 |
|
| 579 |
<para>All streams passed to <function>ext:run-program</function> has to have |
| 580 |
underlying file handler. That means, that if gray streams are passed to |
| 581 |
function - it might signal an error. Such situation occurs, when for |
| 582 |
instance <symbol>:OUTPUT</symbol> value is <symbol>T</symbol> and |
| 583 |
<symbol>*standard-output*</symbol> is bound to gray stream (default if using |
| 584 |
slime and emacs).</para> |
| 585 |
</refsect1> |
| 586 |
</refentry> |
| 587 |
|
| 588 |
<!-- ====================================================================== --> |
| 589 |
<!-- EXT:SYSTEM --> |
| 590 |
<!-- ====================================================================== --> |
| 591 |
|
| 592 |
<refentry xml:id="ref.system"> |
| 593 |
<refnamediv> |
| 594 |
<refname><function>ext:system</function></refname> |
| 595 |
<refpurpose>Invoke a command using the shell.</refpurpose> |
| 596 |
</refnamediv> |
| 597 |
|
| 598 |
<refsynopsisdiv> |
| 599 |
<title>Function</title> |
| 600 |
<funcsynopsis> |
| 601 |
<funcprototype> |
| 602 |
<funcdef>ext:system</funcdef> |
| 603 |
<paramdef><parameter>command</parameter></paramdef> |
| 604 |
</funcprototype> |
| 605 |
</funcsynopsis> |
| 606 |
<variablelist> |
| 607 |
<varlistentry> |
| 608 |
<term><replaceable>command</replaceable></term> |
| 609 |
<listitem><para>A string</para></listitem> |
| 610 |
</varlistentry> |
| 611 |
<varlistentry> |
| 612 |
<term>returns</term> |
| 613 |
<listitem><para>An integer (0-255) with the exit code of the |
| 614 |
program</para></listitem> |
| 615 |
</varlistentry> |
| 616 |
</variablelist> |
| 617 |
</refsynopsisdiv> |
| 618 |
|
| 619 |
<refsect1> |
| 620 |
<title>Description</title> |
| 621 |
|
| 622 |
<para>This function executes a command in the shell. In Unix systems, |
| 623 |
typically the environment variable <symbol>SHELL</symbol> determines which |
| 624 |
program will be invoked, while in Windows <command>CMD.EXE</command> is |
| 625 |
used. The string may thus be any valid command that the shell accepts and |
| 626 |
in can contain higher level elements such as input/output |
| 627 |
redirection. Output isn't printed.</para> |
| 628 |
|
| 629 |
<para>As an example, the following function uses an external editor to |
| 630 |
modify a lisp file and, if successful, loads the changed sources:</para> |
| 631 |
<programlisting> |
| 632 |
(defun edit (filename) |
| 633 |
(let* ((editor #+windows "notepad.exe" |
| 634 |
#-windows "/usr/bin/emacs") |
| 635 |
(command (concatenate 'string editor " " filename))) |
| 636 |
(when (zerop (ext:system command)) |
| 637 |
(load filename)))) |
| 638 |
</programlisting> |
| 639 |
</refsect1> |
| 640 |
</refentry> |
| 641 |
</reference></book> |
| 642 |
<!-- Keep this comment at the end of the file |
| 643 |
Local variables: |
| 644 |
mode: nxml |
| 645 |
sgml-parent-document: "ecl.xml" |
| 646 |
sgml-indent-step: 1 |
| 647 |
nxml-child-indent: 1 |
| 648 |
nxml-outline-child-indent: 1 |
| 649 |
fill-column: 79 |
| 650 |
End: |
| 651 |
--> |