2 @node Scheme Primitives
3 @c @chapter Writing Scheme primitives in C
4 @c - according to the menu in guile.texi - NJ 2001/1/26
5 @chapter Relationship between Scheme and C functions
7 @c Chapter contents contributed by Thien-Thi Nguyen <ttn@gnu.org>.
9 Scheme procedures marked "primitive functions" have a regular interface
10 when calling from C, reflected in two areas: the name of a C function, and
11 the convention for passing non-required arguments to this function.
13 @c Although the vast majority of functions support these relationships,
14 @c there are some exceptions.
17 * Transforming Scheme name to C name::
18 * Structuring argument lists for C functions::
19 @c * Exceptions to the regularity::
22 @node Transforming Scheme name to C name
23 @section Transforming Scheme name to C name
25 Normally, the name of a C function can be derived given its Scheme name,
26 using some simple textual transformations:
31 Replace @code{-} (hyphen) with @code{_} (underscore).
34 Replace @code{?} (question mark) with "_p".
37 Replace @code{!} (exclamation point) with "_x".
40 Replace internal @code{->} with "_to_".
43 Replace @code{<=} (less than or equal) with "_leq".
46 Replace @code{>=} (greater than or equal) with "_geq".
49 Replace @code{<} (less than) with "_less".
52 Replace @code{>} (greater than) with "_gr".
55 Replace @code{@@} with "at". [Omit?]
58 Prefix with "gh_" (or "scm_" if you are ignoring the gh interface).
61 [Anything else? --ttn, 2000/01/16 15:17:28]
65 Here is an Emacs Lisp command that prompts for a Scheme function name and
66 inserts the corresponding C function name into the buffer.
69 (defun insert-scheme-to-C (name &optional use-gh)
70 "Transforms Scheme NAME, a string, to its C counterpart, and inserts it.
71 Prefix arg non-nil means use \"gh_\" prefix, otherwise use \"scm_\" prefix."
72 (interactive "sScheme name: \nP")
73 (let ((transforms '(("-" . "_")
83 (let ((trigger (concat "\\(.*\\)"
84 (regexp-quote (caar transforms))
86 (sub (cdar transforms))
88 (while (setq m (string-match trigger name))
89 (setq name (concat (match-string 1 name)
91 (match-string 2 name)))))
92 (setq transforms (cdr transforms))))
93 (insert (if use-gh "gh_" "scm_") name))
96 @node Structuring argument lists for C functions
97 @section Structuring argument lists for C functions
99 The C function's arguments will be all of the Scheme procedure's
100 argumements, both required and optional; if the Scheme procedure takes a
101 ``rest'' argument, that will be a final argument to the C function. The
102 C function's arguments, as well as its return type, will be @code{SCM}.
104 @c @node Exceptions to the regularity
105 @c @section Exceptions to the regularity
107 @c There are some exceptions to the regular structure described above.
112 @chapter Using and Extending Ports in C
115 * C Port Interface:: Using ports from C.
116 * Port Implementation:: How to implement a new port type in C.
120 @node C Port Interface
121 @section C Port Interface
123 This section describes how to use Scheme ports from C.
125 @subsection Port basics
127 There are two main data structures. A port type object (ptob) is of
128 type @code{scm_ptob_descriptor}. A port instance is of type
129 @code{scm_port}. Given an @code{SCM} variable which points to a port,
130 the corresponding C port object can be obtained using the
131 @code{SCM_PTAB_ENTRY} macro. The ptob can be obtained by using
132 @code{SCM_PTOBNUM} to give an index into the @code{scm_ptobs}
135 @subsection Port buffers
137 An input port always has a read buffer and an output port always has a
138 write buffer. However the size of these buffers is not guaranteed to be
139 more than one byte (e.g., the @code{shortbuf} field in @code{scm_port}
140 which is used when no other buffer is allocated). The way in which the
141 buffers are allocated depends on the implementation of the ptob. For
142 example in the case of an fport, buffers may be allocated with malloc
143 when the port is created, but in the case of an strport the underlying
144 string is used as the buffer.
146 @subsection The @code{rw_random} flag
148 Special treatment is required for ports which can be seeked at random.
149 Before various operations, such as seeking the port or changing from
150 input to output on a bidirectional port or vice versa, the port
151 implemention must be given a chance to update its state. The write
152 buffer is updated by calling the @code{flush} ptob procedure and the
153 input buffer is updated by calling the @code{end_input} ptob procedure.
154 In the case of an fport, @code{flush} causes buffered output to be
155 written to the file descriptor, while @code{end_input} causes the
156 descriptor position to be adjusted to account for buffered input which
159 The special treatment must be performed if the @code{rw_random} flag in
160 the port is non-zero.
162 @subsection The @code{rw_active} variable
164 The @code{rw_active} variable in the port is only used if
165 @code{rw_random} is set. It's defined as an enum with the following
170 the read buffer may have unread data.
173 the write buffer may have unwritten data.
175 @item SCM_PORT_NEITHER
176 neither the write nor the read buffer has data.
179 @subsection Reading from a port.
181 To read from a port, it's possible to either call existing libguile
182 procedures such as @code{scm_getc} and @code{scm_read_line} or to read
183 data from the read buffer directly. Reading from the buffer involves
188 Flush output on the port, if @code{rw_active} is @code{SCM_PORT_WRITE}.
191 Fill the read buffer, if it's empty, using @code{scm_fill_input}.
193 @item Read the data from the buffer and update the read position in
194 the buffer. Steps 2) and 3) may be repeated as many times as required.
196 @item Set rw_active to @code{SCM_PORT_READ} if @code{rw_random} is set.
198 @item update the port's line and column counts.
201 @subsection Writing to a port.
203 To write data to a port, calling @code{scm_lfwrite} should be sufficient for
204 most purposes. This takes care of the following steps:
208 End input on the port, if @code{rw_active} is @code{SCM_PORT_READ}.
211 Pass the data to the ptob implementation using the @code{write} ptob
212 procedure. The advantage of using the ptob @code{write} instead of
213 manipulating the write buffer directly is that it allows the data to be
214 written in one operation even if the port is using the single-byte
218 Set @code{rw_active} to @code{SCM_PORT_WRITE} if @code{rw_random}
223 @node Port Implementation
224 @section Port Implementation
226 This section describes how to implement a new port type in C.
228 As described in the previous section, a port type object (ptob) is
229 a structure of type @code{scm_ptob_descriptor}. A ptob is created by
230 calling @code{scm_make_port_type}.
232 All of the elements of the ptob, apart from @code{name}, are procedures
233 which collectively implement the port behaviour. Creating a new port
234 type mostly involves writing these procedures.
236 @code{scm_make_port_type} initialises three elements of the structure
237 (@code{name}, @code{fill_input} and @code{write}) from its arguments.
238 The remaining elements are initialised with default values and can be
239 set later if required.
243 A pointer to a NUL terminated string: the name of the port type. This
244 is the only element of @code{scm_ptob_descriptor} which is not
245 a procedure. Set via the first argument to @code{scm_make_port_type}.
248 Called during garbage collection to mark any SCM objects that a port
249 object may contain. It doesn't need to be set unless the port has
250 @code{SCM} components. Set using @code{scm_set_port_mark}.
253 Called when the port is collected during gc. It
254 should free any resources used by the port.
255 Set using @code{scm_set_port_free}.
258 Called when @code{write} is called on the port object, to print a
259 port description. e.g., for an fport it may produce something like:
260 @code{#<input: /etc/passwd 3>}. Set using @code{scm_set_port_print}.
263 Not used at present. Set using @code{scm_set_port_equalp}.
266 Called when the port is closed, unless it was collected during gc. It
267 should free any resources used by the port.
268 Set using @code{scm_set_port_close}.
271 Accept data which is to be written using the port. The port implementation
272 may choose to buffer the data instead of processing it directly.
273 Set via the third argument to @code{scm_make_port_type}.
276 Complete the processing of buffered output data. Reset the value of
277 @code{rw_active} to @code{SCM_PORT_NEITHER}.
278 Set using @code{scm_set_port_flush}.
281 Perform any synchronisation required when switching from input to output
282 on the port. Reset the value of @code{rw_active} to @code{SCM_PORT_NEITHER}.
283 Set using @code{scm_set_port_end_input}.
286 Read new data into the read buffer and return the first character. It
287 can be assumed that the read buffer is empty when this procedure is called.
288 Set via the second argument to @code{scm_make_port_type}.
291 Return a lower bound on the number of bytes that could be read from the
292 port without blocking. It can be assumed that the current state of
293 @code{rw_active} is @code{SCM_PORT_NEITHER}.
294 Set using @code{scm_set_port_input_waiting}.
297 Set the current position of the port. The procedure can not make
298 any assumptions about the value of @code{rw_active} when it's
299 called. It can reset the buffers first if desired by using something
303 if (pt->rw_active == SCM_PORT_READ)
304 scm_end_input (object);
305 else if (pt->rw_active == SCM_PORT_WRITE)
306 ptob->flush (object);
309 However note that this will have the side effect of discarding any data
310 in the unread-char buffer, in addition to any side effects from the
311 @code{end_input} and @code{flush} ptob procedures. This is undesirable
312 when seek is called to measure the current position of the port, i.e.,
313 @code{(seek p 0 SEEK_CUR)}. The libguile fport and string port
314 implementations take care to avoid this problem.
316 The procedure is set using @code{scm_set_port_seek}.
319 Truncate the port data to be specified length. It can be assumed that the
320 current state of @code{rw_active} is @code{SCM_PORT_NEITHER}.
321 Set using @code{scm_set_port_truncate}.
326 @node Handling Errors
327 @chapter How to Handle Errors in C Code
329 Error handling is based on catch and throw. Errors are always thrown with
330 a key and four arguments:
334 key: a symbol which indicates the type of error. The symbols used
335 by libguile are listed below.
338 subr: the name of the procedure from which the error is thrown, or #f.
341 message: a string (possibly language and system dependent) describing the
342 error. The tokens %s and %S can be embedded within the message: they
343 will be replaced with members of the args list when the message is
344 printed. %s indicates an argument printed using "display", while %S
345 indicates an argument printed using "write". message can also be #f,
346 to allow it to be derived from the key by the error handler (may be
347 useful if the key is to be thrown from both C and Scheme).
350 args: a list of arguments to be used to expand %s and %S tokens in message.
351 Can also be #f if no arguments are required.
354 rest: a list of any additional objects required. e.g., when the key is
355 'system-error, this contains the C errno value. Can also be #f if no
356 additional objects are required.
359 In addition to catch and throw, the following Scheme facilities are
364 (scm-error key subr message args rest): throw an error, with arguments
368 (error msg arg ...) Throw an error using the key 'misc-error. The error
369 message is created by displaying msg and writing the args.
372 The following are the error keys defined by libguile and the situations
373 in which they are used:
377 error-signal: thrown after receiving an unhandled fatal signal such as
378 SIGSEV, SIGBUS, SIGFPE etc. The "rest" argument in the throw contains
379 the coded signal number (at present this is not the same as the usual
383 system-error: thrown after the operating system indicates an error
384 condition. The "rest" argument in the throw contains the errno value.
387 numerical-overflow: numerical overflow.
390 out-of-range: the arguments to a procedure do not fall within the
394 wrong-type-arg: an argument to a procedure has the wrong thpe.
397 wrong-number-of-args: a procedure was called with the wrong number of
401 memory-allocation-error: memory allocation error.
404 stack-overflow: stack overflow error.
407 regex-error: errors generated by the regular expression library.
410 misc-error: other errors.
416 SCM scm_error (SCM key, char *subr, char *message, SCM args, SCM rest)
418 Throws an error, after converting the char * arguments to Scheme strings.
419 subr is the Scheme name of the procedure, NULL is converted to #f.
420 Likewise a NULL message is converted to #f.
422 The following procedures invoke scm_error with various error keys and
423 arguments. The first three call scm_error with the system-error key
424 and automatically supply errno in the "rest" argument: scm_syserror
425 generates messages using strerror, scm_sysmissing is used when
426 facilities are not available. Care should be taken that the errno
427 value is not reset (e.g. due to an interrupt).
431 void scm_syserror (char *subr);
433 void scm_syserror_msg (char *subr, char *message, SCM args);
435 void scm_sysmissing (char *subr);
437 void scm_num_overflow (char *subr);
439 void scm_out_of_range (char *subr, SCM bad_value);
441 void scm_wrong_num_args (SCM proc);
443 void scm_wrong_type_arg (char *subr, int pos, SCM bad_value);
445 void scm_memory_error (char *subr);
447 static void scm_regex_error (char *subr, int code); (only used in rgx.c).
450 Exception handlers can also be installed from C, using
451 scm_internal_catch, scm_lazy_catch, or scm_stack_catch from
452 libguile/throw.c. These have not yet been documented, however the
453 source contains some useful comments.