| 1 |
@node Operating System Interface |
| 2 |
@section Operating System Interface |
| 3 |
|
| 4 |
@menu |
| 5 |
* Command line arguments:: |
| 6 |
* External processes:: |
| 7 |
* FIFO files (named pipes):: |
| 8 |
@c * Signals and interrupts:: |
| 9 |
* Operating System Interface Reference:: |
| 10 |
@end menu |
| 11 |
|
| 12 |
@cindex Command line processing |
| 13 |
@node Command line arguments |
| 14 |
@subsection Command line arguments |
| 15 |
|
| 16 |
@lspdef ext:*help-message* |
| 17 |
@deftypevar string ext:*help-message* |
| 18 |
Command line help message. Initial value is ECL help message. This |
| 19 |
variable contains the help message which is output when ECL is invoked |
| 20 |
with the @code{--help}. |
| 21 |
@end deftypevar |
| 22 |
|
| 23 |
@lspdef ext:*lisp-init-file-list* |
| 24 |
@deftypevar list-of-pathname-designators ext:*lisp-init-file-list* |
| 25 |
ECL initialization files. Initial value is @code{'("~/.ecl" |
| 26 |
"~/.eclrc")}. This variable contains the names of initialization files |
| 27 |
that are loaded by ECL or embedding programs. The loading of |
| 28 |
initialization files happens automatically in ECL unless invoked with |
| 29 |
the option @code{--norc}. Whether initialization files are loaded or |
| 30 |
not is controlled by the command line options rules, as described in |
| 31 |
@coderef{ext:process-command-args}. |
| 32 |
@end deftypevar |
| 33 |
|
| 34 |
@lspdef ext:+default-command-arg-rules+ |
| 35 |
@deftypevar list-of-lists ext:+default-command-arg-rules+ |
| 36 |
ECL command line options. This constant contains a list of rules for |
| 37 |
parsing the command line arguments. This list is made of all the |
| 38 |
options which ECL accepts by default. It can be passed as first |
| 39 |
argument to @coderef{ext:process-command-args}, and you can use it as a |
| 40 |
starting point to extend ECL. |
| 41 |
@end deftypevar |
| 42 |
|
| 43 |
@lspdef ext:command-args |
| 44 |
@defun ext:command-args |
| 45 |
Original list of command line arguments. This function returns the |
| 46 |
list of command line arguments passed to either ECL or the program it |
| 47 |
was embedded in. The output is a list of strings and it corresponds to |
| 48 |
the argv vector in a C program. Typically, the first argument is the |
| 49 |
name of the program as it was invoked. You should not count on the |
| 50 |
filename to be resolved. |
| 51 |
@end defun |
| 52 |
|
| 53 |
@lspdef ext:process-command-args |
| 54 |
@defun ext:process-command-args &key args rules |
| 55 |
|
| 56 |
@table @var |
| 57 |
@item args |
| 58 |
A list of strings. Defaults to the output of @coderef{ext:command-args}. |
| 59 |
@item rules |
| 60 |
A list of lists. Defaults to the value of |
| 61 |
@coderef{ext:+default-command-arg-rules+}. |
| 62 |
@end table |
| 63 |
|
| 64 |
This function processes the command line arguments passed to either |
| 65 |
ECL or the program that embeds it. It uses the list of rules rules, |
| 66 |
which has the following syntax: |
| 67 |
|
| 68 |
@code{(option-name nargs template [:stop | :noloadrc | :loadrc]*)} |
| 69 |
|
| 70 |
@table @code |
| 71 |
@item option-name |
| 72 |
A string with the option prefix as typed by the user. For instance |
| 73 |
@code{--help}, @code{-?}, @code{--compile}, etc. |
| 74 |
|
| 75 |
@item nargs |
| 76 |
A non-negative integer denoting the number of arguments taken by this |
| 77 |
option. |
| 78 |
|
| 79 |
@item template |
| 80 |
A lisp form, not evaluated, where numbers from 0 to nargs will be |
| 81 |
replaced by the corresponding option argument. |
| 82 |
|
| 83 |
@item :stop |
| 84 |
If present, parsing of arguments stops after this option is found and |
| 85 |
processed. The list of remaining arguments is passed to the |
| 86 |
rule. ECL's top-level uses this option with the @code{--} command line |
| 87 |
option to set @code{ext:*unprocessed-ecl-command-args*} to the list of |
| 88 |
remaining arguments. |
| 89 |
|
| 90 |
@item :noloadrc, :loadrc |
| 91 |
Determine whether the lisp initialization files in |
| 92 |
@coderef{ext:*lisp-init-file-list*} will be loaded before processing |
| 93 |
all forms. |
| 94 |
@end table |
| 95 |
|
| 96 |
@coderef{ext:process-command-args} works as follows. First of all, it |
| 97 |
parses all the command line arguments, except for the first one, which |
| 98 |
is assumed to contain the program name. Each of these arguments is |
| 99 |
matched against the rules, sequentially, until one of the patterns |
| 100 |
succeeds. |
| 101 |
|
| 102 |
A special name @code{*default*}, matches any unknown command line |
| 103 |
option. If there is no @code{*default*} rule and no match is found, an |
| 104 |
error is signaled. For each rule that succeeds, the function |
| 105 |
constructs a lisp statement using the template. |
| 106 |
|
| 107 |
After all arguments have been processed, |
| 108 |
@coderef{ext:process-command-args}, and there were no occurrences of |
| 109 |
@code{:noloadrc}, the first existing file listed in |
| 110 |
@coderef{ext:*lisp-init-file-list*} will be loaded. Finally, the list of |
| 111 |
lisp statements will be evaluated. |
| 112 |
@end defun |
| 113 |
|
| 114 |
@cindex Parsing arguments in standalone executable |
| 115 |
@exindex LS implementation |
| 116 |
|
| 117 |
The following piece of code implements the ls command using |
| 118 |
lisp. Instructions for building this program are found under |
| 119 |
@file{examples/cmdline/ls.lsp}. |
| 120 |
|
| 121 |
@lisp |
| 122 |
@verbatim |
| 123 |
(setq ext:*help-message* " |
| 124 |
ls [--help | -?] filename* |
| 125 |
Lists the file that match the given patterns. |
| 126 |
") |
| 127 |
|
| 128 |
(defun print-directory (pathnames) |
| 129 |
(format t "~{~A~%~}" |
| 130 |
(mapcar #'(lambda (x) (enough-namestring x (si::getcwd))) |
| 131 |
(mapcan #'directory (or pathnames '("*.*" "*/")))))) |
| 132 |
|
| 133 |
(defconstant +ls-rules+ |
| 134 |
'(("--help" 0 (progn (princ ext:*help-message* *standard-output*) (ext:quit 0))) |
| 135 |
("-?" 0 (progn (princ ext:*help-message* *standard-output*) (ext:quit 0))) |
| 136 |
("*DEFAULT*" 1 (print-directory 1) :stop))) |
| 137 |
|
| 138 |
(let ((ext:*lisp-init-file-list* NIL)) ; No initialization files |
| 139 |
(handler-case (ext:process-command-args :rules +ls-rules+) |
| 140 |
(error (c) |
| 141 |
(princ ext:*help-message* *error-output*) |
| 142 |
(ext:quit 1)))) |
| 143 |
(ext:quit 0) |
| 144 |
@end verbatim |
| 145 |
@end lisp |
| 146 |
|
| 147 |
@cindex External processes |
| 148 |
@node External processes |
| 149 |
@subsection External processes |
| 150 |
|
| 151 |
ECL provides several facilities for invoking and communicating with |
| 152 |
external processes. If one just wishes to execute some program, without |
| 153 |
caring for its output, then probably @coderef{ext:system} is the best |
| 154 |
function. In all other cases it is preferable to use |
| 155 |
@coderef{ext:run-program}, which opens pipes to communicate with the |
| 156 |
program and manipulate it while it runs on the background. |
| 157 |
|
| 158 |
External process is a structure created with @coderef{ext:run-program} |
| 159 |
(returned as third value). It is programmer responsibility, to call |
| 160 |
@coderef{ext:external-process-wait} on finished processes, however during |
| 161 |
garbage collection object will be finalized. |
| 162 |
|
| 163 |
@lspdef ext:external-process-pid |
| 164 |
@defun ext:external-process-pid process |
| 165 |
Returns process PID or @code{nil} if already finished. |
| 166 |
@end defun |
| 167 |
|
| 168 |
@lspdef ext:external-process-status |
| 169 |
@defun ext:external-process-status process |
| 170 |
Updates process status. @coderef{ext:external-process-status} calls |
| 171 |
@coderef{ext:external-process-wait} if process has not finished yet |
| 172 |
(non-blocking call). Returns two values: |
| 173 |
|
| 174 |
@code{status} - member of @code{(:abort :error :exited :signaled |
| 175 |
:stopped :resumed :running)} |
| 176 |
|
| 177 |
@code{code} - if process exited it is a returned value, if terminated |
| 178 |
it is a signal code. Otherwise NIL. |
| 179 |
@end defun |
| 180 |
|
| 181 |
@lspdef ext:external-process-wait |
| 182 |
@defun ext:external-process-wait process wait |
| 183 |
If the second argument is non-NIL, function blocks until external |
| 184 |
process is finished. Otherwise status is updated. Returns two values |
| 185 |
(see @coderef{ext:external-process-status}). |
| 186 |
@end defun |
| 187 |
|
| 188 |
@lspdef ext:terminate-process |
| 189 |
@defun ext:terminate-process process &optional force |
| 190 |
Terminates external process. May signal an error if the process has |
| 191 |
already finished. |
| 192 |
@end defun |
| 193 |
|
| 194 |
@lspdef ext:external-process-input |
| 195 |
@lspdef ext:external-process-output |
| 196 |
@lspdef ext:external-process-error-stream |
| 197 |
@defun ext:external-process-input process |
| 198 |
@defunx ext:external-process-output process |
| 199 |
@defunx ext:external-process-error-stream process |
| 200 |
Process stream accessors (read-only). |
| 201 |
@end defun |
| 202 |
|
| 203 |
@lspdef ext:run-program |
| 204 |
@defun ext:run-program command argv @ |
| 205 |
&key input output error wait environ @ |
| 206 |
if-input-does-not-exist if-output-exists if-error-exists @ |
| 207 |
external-format #+windows escape-arguments |
| 208 |
|
| 209 |
@coderef{ext:run-program} creates a new process specified by the |
| 210 |
@var{command} argument. @var{argv} are the standard arguments that can |
| 211 |
be passed to a program. For no arguments, use @code{nil} (which means |
| 212 |
that just the name of the program is passed as arg 0). |
| 213 |
|
| 214 |
@coderef{ext:run-program} will return three values - two-way stream for |
| 215 |
communication, return code or @code{nil} (if process is called |
| 216 |
asynchronously), and @code{ext:external-process} object holding |
| 217 |
process state. |
| 218 |
|
| 219 |
It is programmer responsibility to call |
| 220 |
@coderef{ext:external-process-wait} on finished process, however ECL |
| 221 |
associates @ref{Finalization, finalizer} with the object calling it |
| 222 |
when the object is garbage collected. If process didn't finish but is |
| 223 |
not referenced, finalizer will be invoked once more during next |
| 224 |
garbage collection. |
| 225 |
|
| 226 |
The @code{&key} arguments have the following meanings: |
| 227 |
|
| 228 |
@table @var |
| 229 |
@item input |
| 230 |
Either @code{t}, @code{nil}, a pathname, a string, a stream or |
| 231 |
@code{:stream}. If @code{t} the standard input for the current process |
| 232 |
is inherited. If @code{nil}, @code{/dev/null} is used. If a pathname (or |
| 233 |
a string), the file so specified is used. If a stream, all the input is |
| 234 |
read from that stream and sent to the subprocess. If @code{:stream}, the |
| 235 |
@coderef{ext:external-process-input} slot is filled in with a stream |
| 236 |
that sends its output to the process. Defaults to @code{:stream}. |
| 237 |
|
| 238 |
@item if-input-does-not-exist |
| 239 |
Can be one of: @code{:error} to generate an error @code{:create} to |
| 240 |
create an empty file @code{nil} (the default) to return nil from |
| 241 |
@coderef{ext:run-program} |
| 242 |
|
| 243 |
@item output |
| 244 |
Either @code{t}, @code{nil}, a pathname, a string, a stream, or |
| 245 |
@code{:stream}. If @code{t}, the standard output for the current process |
| 246 |
is inherited. If @code{nil}, @code{/dev/null} is used. If a pathname (or |
| 247 |
as string), the file so specified is used. If a stream, all the output |
| 248 |
from the process is written to this stream. If @code{:stream}, the |
| 249 |
@coderef{ext:external-process-output} slot is filled in with a stream |
| 250 |
that can be read to get the output. Defaults to @code{:stream}. |
| 251 |
|
| 252 |
@item if-output-exists |
| 253 |
Can be one of: @code{:error} (the default) to generate an error, |
| 254 |
@code{:supersede} to supersede the file with output from the program, |
| 255 |
@code{:append} to append output from the program to the file or |
| 256 |
@code{nil} to return @code{nil} from @coderef{ext:run-program}. |
| 257 |
|
| 258 |
@item error |
| 259 |
Same as @code{:output}, except that @code{:error} can also be |
| 260 |
specified as @code{:output} in which case all error output is routed |
| 261 |
to the same place as normal output. Defaults to @code{:output}. |
| 262 |
|
| 263 |
@item if-error-exists |
| 264 |
Same as @code{:if-output-exists}. |
| 265 |
|
| 266 |
@item wait |
| 267 |
If non-@code{nil} (default), wait until the created process finishes. If |
| 268 |
@code{nil}, continue running Lisp until the program finishes. |
| 269 |
|
| 270 |
@item environ |
| 271 |
A list of STRINGs describing the new Unix environment (as in "man |
| 272 |
environ"). The default is to copy the environment of the current |
| 273 |
process. To extend existing environment (instead of replacing it), |
| 274 |
use @code{:environ (append *my-env* (ext:environ))}. |
| 275 |
|
| 276 |
If non-@code{nil} @code{environ} argument is supplied, then first |
| 277 |
argument to @coderef{ext:run-program}, @var{command}, must be full path |
| 278 |
to the file. |
| 279 |
|
| 280 |
@item external-format |
| 281 |
The external-format to use for @code{:input}, @code{:output}, and |
| 282 |
@code{:error} STREAMs. |
| 283 |
@end table |
| 284 |
|
| 285 |
@strong{Windows specific options:} |
| 286 |
@table @var |
| 287 |
@item escape-arguments |
| 288 |
Controls escaping of the arguments passed to @code{CreateProcess}. |
| 289 |
@end table |
| 290 |
@end defun |
| 291 |
|
| 292 |
@cindex FIFO files (named pipes) |
| 293 |
@node FIFO files (named pipes) |
| 294 |
@subsection FIFO files (named pipes) |
| 295 |
|
| 296 |
Named pipe (known as fifo) may be created on UNIX with a shell command |
| 297 |
mkfifo. They can be opened in non-blocking mode by using @code{:nonblock |
| 298 |
t} option for @coderef{open}. @coderef{ext:file-kind} will return for |
| 299 |
such file @code{:fifo}. Since it is impossible to guess how many |
| 300 |
characters are available in this special file @code{file-length} |
| 301 |
function will return @code{nil}. |
| 302 |
|
| 303 |
@node Operating System Interface Reference |
| 304 |
@subsection Operating System Interface Reference |
| 305 |
|
| 306 |
@c environment routines |
| 307 |
|
| 308 |
@c Don't advertise argc and argv, we have command-args |
| 309 |
|
| 310 |
@c @defun ext:argc |
| 311 |
@c @end defun |
| 312 |
|
| 313 |
@c @defun ext:argv |
| 314 |
@c @end defun |
| 315 |
|
| 316 |
@lspdef ext:system |
| 317 |
@defun ext:system command |
| 318 |
Run shell command ignoring its output. Uses fork. |
| 319 |
@end defun |
| 320 |
|
| 321 |
@lspdef ext:make-pipe |
| 322 |
@defun ext:make-pipe |
| 323 |
Creates a pipe and wraps it in a two way stream. |
| 324 |
@end defun |
| 325 |
|
| 326 |
@lspdef ext:quit |
| 327 |
@defun ext:quit &optional exit-code kill-all-threads |
| 328 |
This function abruptly stops the execution of the program in which ECL |
| 329 |
is embedded. Depending on the platform, several other functions will |
| 330 |
be invoked to free resources, close loaded modules, etc. |
| 331 |
|
| 332 |
The exit code is the code seen by the parent process that invoked this |
| 333 |
program. Normally a code other than zero denotes an error. |
| 334 |
|
| 335 |
If @var{kill-all-threads} is non-@code{nil}, tries to gently kill and |
| 336 |
join with running threads. |
| 337 |
@end defun |
| 338 |
|
| 339 |
@lspdef ext:environ |
| 340 |
@lspdef ext:getenv |
| 341 |
@lspdef ext:setenv |
| 342 |
@defun ext:environ |
| 343 |
@defunx ext:getenv variable |
| 344 |
@defunx ext:setenv variable value |
| 345 |
Environment accessors. |
| 346 |
@end defun |
| 347 |
|
| 348 |
|
| 349 |
@c UNIX shell interface |
| 350 |
|
| 351 |
@lspdef ext:getpid |
| 352 |
@lspdef ext:getuid |
| 353 |
@lspdef ext:getcwd |
| 354 |
@lspdef ext:chdir |
| 355 |
@lspdef ext:file-kind |
| 356 |
@lspdef ext:copy-file |
| 357 |
@lspdef ext:chmod |
| 358 |
|
| 359 |
@defun ext:getpid |
| 360 |
@defunx ext:getuid |
| 361 |
@defunx ext:getcwd &optional (change-default-pathname-defaults NIL) |
| 362 |
@defunx ext:chdir directory &optional (change-default-pathname-defaults T) |
| 363 |
@defunx ext:file-kind filename follow-symlinks-p |
| 364 |
@defunx ext:copy-file filename destination-filename |
| 365 |
@defunx ext:chmod filename mode |
| 366 |
Common operating system functions. |
| 367 |
@end defun |
| 368 |
|
| 369 |
@c Internal UNIX shell interface |
| 370 |
|
| 371 |
@c @defun si:mkdir |
| 372 |
@c @defunx si:rmdir |
| 373 |
@c @defunx si:mkstemp |
| 374 |
@c @defunx si:copy-file |
| 375 |
@c @end defun |
| 376 |
|
| 377 |
@c @defun si:get-library-pathname |
| 378 |
@c @end defun |
| 379 |
|
| 380 |
@c @defun si:waitpid pid wait |
| 381 |
@c @defunx si:kill pid signal |
| 382 |
@c @end defun |
| 383 |
|
| 384 |
@c @defun si:run-program-inner command argv environ |
| 385 |
@c @end defun |