Commit | Line | Data |
---|---|---|
5134bfa7 | 1 | @c $Id: intro.texi,v 1.7 2001-05-06 01:49:56 ttn Exp $ |
38a93523 NJ |
2 | |
3 | @page | |
4 | @node What is Guile? | |
5 | @chapter What is Guile? | |
6 | ||
7 | Guile is an interpreter for the Scheme programming language, packaged | |
8 | for use in a wide variety of environments. Guile implements Scheme as | |
9 | described in the | |
10 | @tex | |
11 | Revised$^5$ | |
12 | @end tex | |
13 | @ifinfo | |
14 | Revised^5 | |
15 | @end ifinfo | |
16 | Report on the Algorithmic Language Scheme (usually known as R5RS), | |
17 | providing clean and general data and control structures. Guile goes | |
18 | beyond the rather austere language presented in R5RS, extending it with | |
19 | a module system, full access to POSIX system calls, networking support, | |
20 | multiple threads, dynamic linking, a foreign function call interface, | |
21 | powerful string processing, and many other features needed for | |
22 | programming in the real world. | |
23 | ||
24 | Like a shell, Guile can run interactively, reading expressions from the | |
25 | user, evaluating them, and displaying the results, or as a script | |
26 | interpreter, reading and executing Scheme code from a file. However, | |
27 | Guile is also packaged as an object library, allowing other applications | |
28 | to easily incorporate a complete Scheme interpreter. An application can | |
29 | use Guile as an extension language, a clean and powerful configuration | |
30 | language, or as multi-purpose ``glue'', connecting primitives provided | |
31 | by the application. It is easy to call Scheme code from C code and vice | |
32 | versa, giving the application designer full control of how and when to | |
33 | invoke the interpreter. Applications can add new functions, data types, | |
34 | control structures, and even syntax to Guile, creating a domain-specific | |
35 | language tailored to the task at hand, but based on a robust language | |
36 | design. | |
37 | ||
38 | Guile's module system allows one to break up a large program into | |
39 | manageable sections with well-defined interfaces between them. Modules | |
40 | may contain a mixture of interpreted and compiled code; Guile can use | |
41 | either static or dynamic linking to incorporate compiled code. Modules | |
42 | also encourage developers to package up useful collections of routines | |
43 | for general distribution; as of this writing, one can find Emacs | |
44 | interfaces, database access routines, compilers, GUI toolkit interfaces, | |
45 | and HTTP client functions, among others. | |
46 | ||
47 | In the future, we hope to expand Guile to support other languages like | |
48 | Tcl and Perl by translating them to Scheme code. This means that users | |
49 | can program applications which use Guile in the language of their | |
50 | choice, rather than having the tastes of the application's author | |
51 | imposed on them. | |
52 | ||
53 | ||
54 | @page | |
55 | @node Whirlwind Tour | |
56 | @chapter A Whirlwind Tour | |
57 | ||
58 | This chapter presents a quick tour of all the ways that Guile can be | |
59 | used. | |
60 | ||
61 | @menu | |
5134bfa7 TTN |
62 | * Running Guile Interactively:: |
63 | * Guile Scripts:: | |
64 | * Linking Programs With Guile:: | |
65 | * Writing Extensions for Guile:: | |
66 | * Guile Modules:: | |
38a93523 NJ |
67 | @end menu |
68 | ||
69 | ||
70 | @node Running Guile Interactively | |
71 | @section Running Guile Interactively | |
72 | ||
73 | In its simplest form, Guile acts as an interactive interpreter for the | |
74 | Scheme programming language, reading and evaluating Scheme expressions | |
75 | the user enters from the terminal. Here is a sample interaction between | |
76 | Guile and a user; the user's input appears after the @code{$} and | |
77 | @code{guile>} prompts: | |
78 | ||
79 | @example | |
80 | $ guile | |
81 | guile> (+ 1 2 3) ; add some numbers | |
82 | 6 | |
83 | guile> (define (factorial n) ; define a function | |
84 | (if (zero? n) 1 (* n (factorial (- n 1))))) | |
85 | guile> (factorial 20) | |
86 | 2432902008176640000 | |
87 | guile> (getpwnam "jimb") ; find my entry in /etc/passwd | |
88 | #("jimb" ".0krIpK2VqNbU" 4008 10 "Jim Blandy" "/u/jimb" | |
89 | "/usr/local/bin/bash") | |
90 | guile> @kbd{C-d} | |
91 | $ | |
92 | @end example | |
93 | ||
94 | @c [[When we get a fancier read-eval-print loop, with features for bouncing | |
95 | @c around among modules, referring to the value of the last expression, | |
96 | @c etc. then this section will get longer.]] | |
97 | ||
98 | ||
99 | @node Guile Scripts | |
100 | @section Guile Scripts | |
101 | ||
102 | Like AWK, Perl, or any shell, Guile can interpret script files. A Guile | |
103 | script is simply a file of Scheme code with some extra information at | |
104 | the beginning which tells the operating system how to invoke Guile, and | |
105 | then tells Guile how to handle the Scheme code. | |
106 | ||
107 | Before we present the details, here is a trivial Guile script: | |
108 | ||
109 | @example | |
110 | #!/usr/local/bin/guile -s | |
111 | !# | |
112 | (display "Hello, world!") | |
113 | (newline) | |
114 | @end example | |
115 | ||
116 | @menu | |
117 | * The Top of a Script File:: How to start a Guile script. | |
118 | * Scripting Examples:: Simple Guile scripts, explained. | |
119 | @end menu | |
120 | ||
121 | ||
122 | @node The Top of a Script File | |
123 | @subsection The Top of a Script File | |
124 | ||
125 | The first line of a Guile script must tell the operating system to use | |
126 | Guile to evaluate the script, and then tell Guile how to go about doing | |
127 | that. Here is the simplest case: | |
128 | ||
129 | @itemize @bullet | |
130 | ||
131 | @item | |
132 | The first two characters of the file must be @samp{#!}. | |
133 | ||
134 | The operating system interprets this to mean that the rest of the line | |
135 | is the name of an executable that can interpret the script. Guile, | |
136 | however, interprets these characters as the beginning of a multi-line | |
137 | comment, terminated by the characters @samp{!#} on a line by themselves. | |
138 | (This is an extension to the syntax described in R5RS, added to support | |
139 | shell scripts.) | |
140 | ||
141 | @item | |
142 | Immediately after those two characters must come the full pathname to | |
143 | the Guile interpreter. On most systems, this would be | |
144 | @samp{/usr/local/bin/guile}. | |
145 | ||
146 | @item | |
147 | Then must come a space, followed by a command-line argument to pass to | |
148 | Guile; this should be @samp{-s}. This switch tells Guile to run a | |
149 | script, instead of soliciting the user for input from the terminal. | |
150 | There are more elaborate things one can do here; see @ref{The Meta | |
151 | Switch}. | |
152 | ||
153 | @item | |
154 | Follow this with a newline. | |
155 | ||
156 | @item | |
157 | The second line of the script should contain only the characters | |
158 | @samp{!#} --- just like the top of the file, but reversed. The | |
159 | operating system never reads this far, but Guile treats this as the end | |
160 | of the comment begun on the first line by the @samp{#!} characters. | |
161 | ||
162 | @item | |
163 | The rest of the file should be a Scheme program. | |
164 | ||
165 | @end itemize | |
166 | ||
167 | Guile reads the program, evaluating expressions in the order that they | |
168 | appear. Upon reaching the end of the file, Guile exits. | |
169 | ||
170 | The function @code{command-line} returns the name of the script file and | |
171 | any command-line arguments passed by the user, as a list of strings. | |
172 | ||
173 | For example, consider the following script file: | |
174 | @example | |
175 | #!/usr/local/bin/guile -s | |
176 | !# | |
177 | (write (command-line)) | |
178 | (newline) | |
179 | @end example | |
180 | ||
181 | If you put that text in a file called @file{foo} in the current | |
182 | directory, then you could make it executable and try it out like this: | |
183 | @example | |
184 | $ chmod a+x foo | |
185 | $ ./foo | |
186 | ("./foo") | |
187 | $ ./foo bar baz | |
188 | ("./foo" "bar" "baz") | |
5134bfa7 | 189 | $ |
38a93523 NJ |
190 | @end example |
191 | ||
192 | As another example, here is a simple replacement for the POSIX | |
193 | @code{echo} command: | |
194 | @example | |
195 | #!/usr/local/bin/guile -s | |
196 | !# | |
197 | (for-each (lambda (s) (display s) (display " ")) | |
198 | (cdr (command-line))) | |
199 | (newline) | |
200 | @end example | |
201 | ||
202 | @deffn procedure command-line | |
203 | @deffnx primitive program-arguments | |
204 | Return a list of the command-line arguments passed to the currently | |
205 | running program. If the program invoked Guile with the @samp{-s}, | |
206 | @samp{-c} or @samp{--} switches, these procedures ignore everything up | |
207 | to and including those switches. | |
208 | @end deffn | |
209 | ||
210 | ||
211 | @node Scripting Examples | |
212 | @subsection Scripting Examples | |
213 | ||
214 | To start with, here are some examples of invoking Guile directly: | |
215 | ||
216 | @table @code | |
217 | ||
218 | @item guile -- a b c | |
219 | Run Guile interactively; @code{(command-line)} will return @* | |
220 | @code{("/usr/local/bin/guile" "a" "b" "c")}. | |
221 | ||
222 | @item guile -s /u/jimb/ex2 a b c | |
223 | Load the file @file{/u/jimb/ex2}; @code{(command-line)} will return @* | |
224 | @code{("/u/jimb/ex2" "a" "b" "c")}. | |
225 | ||
226 | @item guile -c '(write %load-path) (newline)' | |
227 | Write the value of the variable @code{%load-path}, print a newline, | |
228 | and exit. | |
229 | ||
230 | @item guile -e main -s /u/jimb/ex4 foo | |
231 | Load the file @file{/u/jimb/ex4}, and then call the function | |
232 | @code{main}, passing it the list @code{("/u/jimb/ex4" "foo")}. | |
233 | ||
234 | @item guile -l first -ds -l last -s script | |
235 | Load the files @file{first}, @file{script}, and @file{last}, in that | |
236 | order. The @code{-ds} switch says when to process the @code{-s} | |
237 | switch. For a more motivated example, see the scripts below. | |
238 | ||
239 | @end table | |
240 | ||
241 | ||
242 | Here is a very simple Guile script: | |
243 | @example | |
244 | #!/usr/local/bin/guile -s | |
245 | !# | |
246 | (display "Hello, world!") | |
247 | (newline) | |
248 | @end example | |
249 | The first line marks the file as a Guile script. When the user invokes | |
250 | it, the system runs @file{/usr/local/bin/guile} to interpret the script, | |
251 | passing @code{-s}, the script's filename, and any arguments given to the | |
252 | script as command-line arguments. When Guile sees @code{-s | |
253 | @var{script}}, it loads @var{script}. Thus, running this program | |
254 | produces the output: | |
255 | @example | |
256 | Hello, world! | |
257 | @end example | |
258 | ||
259 | Here is a script which prints the factorial of its argument: | |
260 | @example | |
261 | #!/usr/local/bin/guile -s | |
262 | !# | |
263 | (define (fact n) | |
264 | (if (zero? n) 1 | |
265 | (* n (fact (- n 1))))) | |
266 | ||
267 | (display (fact (string->number (cadr (command-line))))) | |
268 | (newline) | |
269 | @end example | |
270 | In action: | |
271 | @example | |
272 | $ fact 5 | |
273 | 120 | |
274 | $ | |
275 | @end example | |
276 | ||
277 | However, suppose we want to use the definition of @code{fact} in this | |
278 | file from another script. We can't simply @code{load} the script file, | |
279 | and then use @code{fact}'s definition, because the script will try to | |
280 | compute and display a factorial when we load it. To avoid this problem, | |
281 | we might write the script this way: | |
282 | ||
283 | @example | |
284 | #!/usr/local/bin/guile \ | |
285 | -e main -s | |
286 | !# | |
287 | (define (fact n) | |
288 | (if (zero? n) 1 | |
289 | (* n (fact (- n 1))))) | |
290 | ||
291 | (define (main args) | |
292 | (display (fact (string->number (cadr args)))) | |
293 | (newline)) | |
294 | @end example | |
295 | This version packages the actions the script should perform in a | |
296 | function, @code{main}. This allows us to load the file purely for its | |
297 | definitions, without any extraneous computation taking place. Then we | |
298 | used the meta switch @code{\} and the entry point switch @code{-e} to | |
299 | tell Guile to call @code{main} after loading the script. | |
300 | @example | |
301 | $ fact 50 | |
302 | 30414093201713378043612608166064768844377641568960512000000000000 | |
303 | @end example | |
304 | ||
305 | Suppose that we now want to write a script which computes the | |
306 | @code{choose} function: given a set of @var{m} distinct objects, | |
307 | @code{(choose @var{n} @var{m})} is the number of distinct subsets | |
308 | containing @var{n} objects each. It's easy to write @code{choose} given | |
309 | @code{fact}, so we might write the script this way: | |
310 | @example | |
311 | #!/usr/local/bin/guile \ | |
312 | -l fact -e main -s | |
313 | !# | |
314 | (define (choose n m) | |
315 | (/ (fact m) (* (fact (- m n)) (fact n)))) | |
316 | ||
317 | (define (main args) | |
318 | (let ((n (string->number (cadr args))) | |
319 | (m (string->number (caddr args)))) | |
320 | (display (choose n m)) | |
321 | (newline))) | |
322 | @end example | |
323 | ||
324 | The command-line arguments here tell Guile to first load the file | |
325 | @file{fact}, and then run the script, with @code{main} as the entry | |
326 | point. In other words, the @code{choose} script can use definitions | |
327 | made in the @code{fact} script. Here are some sample runs: | |
328 | @example | |
329 | $ choose 0 4 | |
330 | 1 | |
331 | $ choose 1 4 | |
332 | 4 | |
333 | $ choose 2 4 | |
334 | 6 | |
335 | $ choose 3 4 | |
336 | 4 | |
337 | $ choose 4 4 | |
338 | 1 | |
339 | $ choose 50 100 | |
340 | 100891344545564193334812497256 | |
341 | @end example | |
342 | ||
343 | ||
344 | @node Linking Programs With Guile | |
345 | @section Linking Programs With Guile | |
346 | ||
347 | The Guile interpreter is available as an object library, to be linked | |
348 | into applications using Scheme as a configuration or extension | |
349 | language. This chapter covers the mechanics of linking your program | |
350 | with Guile on a typical POSIX system. | |
351 | ||
352 | Parts III and IV of this manual describe the C functions Guile provides. | |
353 | Furthermore, any Scheme function described in this manual as a | |
354 | ``Primitive'' is also callable from C; see @ref{Scheme Primitives}. | |
355 | ||
356 | The header file @code{<libguile.h>} provides declarations for all of | |
357 | Guile's functions and constants. You should @code{#include} it at the | |
358 | head of any C source file that uses identifiers described in this | |
ac3e3f5b MV |
359 | manual. Once you've compiled your source files, you need to link them |
360 | against the Guile object code library, @code{libguile}. | |
361 | ||
362 | On most systems, you should not need to do tell the compiler and linker | |
363 | explicitely where they can find @file{libguile.h} and @file{libguile}. | |
364 | When Guile has been installed in a peculiar way, or when you are on a | |
365 | peculiar system, things might not be so easy and you might need to pass | |
366 | additional @code{-I} or @code{-L} options to the compiler. Guile | |
367 | provides the utility program @code{guile-config} to help you find the | |
368 | right values for these options. You would typically run | |
369 | @code{guile-config} during the configuration phase of your program and | |
370 | use the obtained information in the Makefile. | |
38a93523 NJ |
371 | |
372 | @menu | |
373 | * Guile Initialization Functions:: What to call first. | |
374 | * A Sample Guile Main Program:: Sources and makefiles. | |
375 | @end menu | |
376 | ||
377 | ||
378 | @node Guile Initialization Functions | |
379 | @subsection Guile Initialization Functions | |
380 | ||
ac3e3f5b MV |
381 | To initialize Guile, you can use one of two functions. The first, |
382 | @code{scm_boot_guile}, is the most portable way to initialize Guile. It | |
383 | should be used whenever you have control over the main function of your | |
384 | program because it never returns. The second function, | |
385 | @code{scm_init_guile}, does return and can thus be used in more | |
386 | situations. However, @code{scm_init_guile} is not as widely available | |
387 | as @code{scm_boot_guile} because it needs to rely on non-portable code | |
388 | to find the stack bounds. When Guile does not know how to find these | |
389 | bounds on your system, it will not provide @code{scm_init_guile}. | |
390 | ||
391 | When you can tolerate the limits of @code{scm_boot_guile}, you should | |
392 | use it in favor of @code{scm_init_guile} since that will make your | |
393 | program more portable. | |
38a93523 NJ |
394 | |
395 | @deftypefun void scm_boot_guile (int @var{argc}, char **@var{argv}, void (*@var{main_func}) (), void *@var{closure}) | |
396 | Initialize the Guile Scheme interpreter. Then call @var{main_func}, | |
397 | passing it @var{closure}, @var{argc}, and @var{argv}. @var{main_func} | |
398 | should do all the work of the program (initializing other packages, | |
399 | defining application-specific functions, reading user input, and so on) | |
400 | before returning. When @var{main_func} returns, call @code{exit (0)}; | |
401 | @code{scm_boot_guile} never returns. If you want some other exit value, | |
402 | have @var{main_func} call exit itself. | |
403 | ||
404 | @code{scm_boot_guile} arranges for the Scheme @code{command-line} | |
405 | function to return the strings given by @var{argc} and @var{argv}. If | |
406 | @var{main_func} modifies @var{argc} or @var{argv}, it should call | |
407 | @code{scm_set_program_arguments} with the final list, so Scheme code | |
408 | will know which arguments have been processed. | |
409 | ||
38a93523 | 410 | Why must the caller do all the real work from @var{main_func}? Guile's |
ac3e3f5b MV |
411 | garbage collector scans the stack to find all local variables that |
412 | reference Scheme objects. To do this, it needs to know the bounds of | |
413 | the stack that might contain such references. Because there is no | |
414 | protable way in C to find the base of the stack, @code{scm_boot_guile} | |
415 | assumes that all references are above its own stack frame. If you try | |
416 | to manipulate Scheme objects after this function returns, it's the luck | |
417 | of the draw whether Guile's storage manager will be able to find the | |
418 | objects you allocate. So, @code{scm_boot_guile} function exits, rather | |
419 | than returning, to discourage you from making that mistake. | |
420 | ||
421 | See @code{scm_init_guile}, below, for a function that can find the real | |
422 | base of the stack, but not in a portable way. | |
423 | @end deftypefun | |
424 | ||
425 | @deftypefun void scm_init_guile () | |
426 | Initialize the Guile Scheme interpreter. | |
427 | ||
428 | In contrast to @code{scm_boot_guile}, this function knows how to find | |
429 | the true base of the stack and thus does not need to usurp the control | |
430 | flow of your program. However, since finding the stack base can not be | |
431 | done portably, this function might not be available in all installations | |
432 | of Guile. If you can, you should use @code{scm_boot_guile} instead. | |
433 | ||
434 | Note that @code{scm_init_guile} does not inform Guile about the command | |
435 | line arguments that should be returned by the Scheme function | |
436 | @code{comamnd-line}. You can use @code{scm_set_program_arguments} to do | |
437 | this. | |
38a93523 NJ |
438 | @end deftypefun |
439 | ||
440 | One common way to use Guile is to write a set of C functions which | |
441 | perform some useful task, make them callable from Scheme, and then link | |
442 | the program with Guile. This yields a Scheme interpreter just like | |
443 | @code{guile}, but augmented with extra functions for some specific | |
444 | application --- a special-purpose scripting language. | |
445 | ||
446 | In this situation, the application should probably process its | |
447 | command-line arguments in the same manner as the stock Guile | |
448 | interpreter. To make that straightforward, Guile provides this | |
449 | function: | |
450 | ||
451 | @deftypefun void scm_shell (int @var{argc}, char **@var{argv}) | |
452 | Process command-line arguments in the manner of the @code{guile} | |
453 | executable. This includes loading the normal Guile initialization | |
454 | files, interacting with the user or running any scripts or expressions | |
455 | specified by @code{-s} or @code{-e} options, and then exiting. | |
456 | @xref{Invoking Guile}, for more details. | |
457 | ||
458 | Since this function does not return, you must do all | |
459 | application-specific initialization before calling this function. | |
38a93523 NJ |
460 | @end deftypefun |
461 | ||
462 | ||
463 | @node A Sample Guile Main Program | |
464 | @subsection A Sample Guile Main Program | |
465 | ||
466 | Here is @file{simple-guile.c}, source code for a @code{main} and an | |
467 | @code{inner_main} function that will produce a complete Guile | |
468 | interpreter. | |
469 | ||
470 | @example | |
471 | /* simple-guile.c --- how to start up the Guile | |
472 | interpreter from C code. */ | |
473 | ||
474 | /* Get declarations for all the scm_ functions. */ | |
475 | #include <libguile.h> | |
476 | ||
477 | static void | |
478 | inner_main (void *closure, int argc, char **argv) | |
479 | @{ | |
480 | /* module initializations would go here */ | |
481 | scm_shell (argc, argv); | |
482 | @} | |
483 | ||
484 | int | |
485 | main (int argc, char **argv) | |
486 | @{ | |
487 | scm_boot_guile (argc, argv, inner_main, 0); | |
488 | return 0; /* never reached */ | |
489 | @} | |
490 | @end example | |
491 | ||
492 | The @code{main} function calls @code{scm_boot_guile} to initialize | |
493 | Guile, passing it @code{inner_main}. Once @code{scm_boot_guile} is | |
494 | ready, it invokes @code{inner_main}, which calls @code{scm_shell} to | |
495 | process the command-line arguments in the usual way. | |
496 | ||
ac3e3f5b MV |
497 | Here is a Makefile which you can use to compile the above program. It |
498 | uses @code{guile-config} to learn about the necessary compiler and | |
499 | linker flags. | |
38a93523 NJ |
500 | @example |
501 | # Use GCC, if you have it installed. | |
502 | CC=gcc | |
503 | ||
ac3e3f5b MV |
504 | # Tell the C compiler where to find <libguile.h> |
505 | CFLAGS=`guile-config compile` | |
38a93523 | 506 | |
ac3e3f5b MV |
507 | # Tell the linker what libraries to use and where to find them. |
508 | LIBS=`guile-config link` | |
38a93523 NJ |
509 | |
510 | simple-guile: simple-guile.o | |
ac3e3f5b MV |
511 | $@{CC@} simple-guile.o $@{LIBS@} -o simple-guile |
512 | ||
38a93523 NJ |
513 | simple-guile.o: simple-guile.c |
514 | $@{CC@} -c $@{CFLAGS@} simple-guile.c | |
515 | @end example | |
516 | ||
517 | If you are using the GNU Autoconf package to make your application more | |
518 | portable, Autoconf will settle many of the details in the Makefile above | |
519 | automatically, making it much simpler and more portable; we recommend | |
ac3e3f5b MV |
520 | using Autoconf with Guile. Guile also provides the @code{GUILE_FLAGS} |
521 | macro for autoconf that performs all necessary checks. Here is a | |
522 | @file{configure.in} file for @code{simple-guile} that uses this macro. | |
523 | Autoconf can use as this file as template to generate a @code{configure} | |
524 | script. In order for Autoconf to find the @code{GUILE_FLAGS} macro, you | |
525 | will need to run @code{aclocal} first. This is not really Guile | |
526 | specific, so you should refer to the Autoconf documentation REFFIXME | |
527 | when in doubt. | |
38a93523 NJ |
528 | @example |
529 | AC_INIT(simple-guile.c) | |
530 | ||
531 | # Find a C compiler. | |
532 | AC_PROG_CC | |
533 | ||
ac3e3f5b MV |
534 | # Check for Guile |
535 | GUILE_FLAGS | |
38a93523 NJ |
536 | |
537 | # Generate a Makefile, based on the results. | |
538 | AC_OUTPUT(Makefile) | |
539 | @end example | |
540 | ||
541 | Here is a @code{Makefile.in} template, from which the @code{configure} | |
542 | script produces a Makefile customized for the host system: | |
543 | @example | |
544 | # The configure script fills in these values. | |
545 | CC=@@CC@@ | |
ac3e3f5b MV |
546 | CFLAGS=@@GUILE_CFLAGS@@ |
547 | LIBS=@@GUILE_LDFLAGS@@ | |
38a93523 NJ |
548 | |
549 | simple-guile: simple-guile.o | |
ac3e3f5b | 550 | $@{CC@} simple-guile.o $@{LIBS@} -o simple-guile |
38a93523 NJ |
551 | simple-guile.o: simple-guile.c |
552 | $@{CC@} -c $@{CFLAGS@} simple-guile.c | |
553 | @end example | |
554 | ||
555 | The developer should use Autoconf to generate the @file{configure} | |
556 | script from the @file{configure.in} template, and distribute | |
557 | @file{configure} with the application. Here's how a user might go about | |
558 | building the application: | |
559 | ||
560 | @example | |
561 | $ ls | |
562 | Makefile.in configure* configure.in simple-guile.c | |
563 | $ ./configure | |
564 | creating cache ./config.cache | |
ac3e3f5b | 565 | checking for gcc... (cached) gcc |
38a93523 NJ |
566 | checking whether the C compiler (gcc ) works... yes |
567 | checking whether the C compiler (gcc ) is a cross-compiler... no | |
ac3e3f5b MV |
568 | checking whether we are using GNU C... (cached) yes |
569 | checking whether gcc accepts -g... (cached) yes | |
570 | checking for Guile... yes | |
38a93523 NJ |
571 | creating ./config.status |
572 | creating Makefile | |
573 | $ make | |
ac3e3f5b MV |
574 | gcc -c -I/usr/local/include simple-guile.c |
575 | gcc simple-guile.o -L/usr/local/lib -lguile -lqthreads -lpthread -lm -o simple-guile | |
38a93523 NJ |
576 | $ ./simple-guile |
577 | guile> (+ 1 2 3) | |
578 | 6 | |
579 | guile> (getpwnam "jimb") | |
580 | #("jimb" "83Z7d75W2tyJQ" 4008 10 "Jim Blandy" "/u/jimb" | |
581 | "/usr/local/bin/bash") | |
582 | guile> (exit) | |
5134bfa7 | 583 | $ |
38a93523 NJ |
584 | @end example |
585 | ||
ac3e3f5b MV |
586 | @node Writing Extensions for Guile |
587 | @section Writing Extensions for Guile | |
588 | ||
589 | The previous sections have briefly explained how to write programs that | |
590 | make use of an embedded Guile interpreter. But sometimes, all you want | |
591 | to do is make new primitive procedures and data types available to the | |
592 | Scheme programmer. Writing a new version of @code{guile} is | |
593 | inconvenient in this case and it would in fact make the life of the | |
8d009ee4 | 594 | users of your new features needlessly hard. |
ac3e3f5b MV |
595 | |
596 | @c [[ the following is probably a bit longwinded ]] | |
597 | ||
598 | For example, suppose that there is a program @code{guile-db} that is a | |
599 | version of Guile with additional features for accessing a database. | |
600 | People who want to write Scheme programs that use these features would | |
601 | have to use @code{guile-db} instead of the usual @code{guile} program. | |
602 | Now suppose that there is also a program @code{guile-gtk} that extends | |
603 | Guile with access to the popular Gtk+ toolkit for graphical user | |
604 | interfaces. People who want to write GUIs in Scheme would have to use | |
605 | @code{guile-gtk}. Now, what happens when you want to write a Scheme | |
606 | application that uses a GUI to let the user accessa a database? You | |
607 | would have to write a @emph{third} program that incorporates both the | |
608 | database stuff and the GUI stuff. This might not be easy (because | |
609 | @code{guile-gtk} might be a quite obscure program, say) and taking this | |
610 | example further makes it easy to see that this approach can not work in | |
611 | practice. | |
612 | ||
613 | It would have been much better if both the database features and the GUI | |
614 | feature had been provided as libraries that can just be linked with | |
615 | @code{guile}. Guile makes it easy to do just this, and we encourage you | |
616 | to make your extensions to Guile available as libraries whenever | |
617 | possible. | |
618 | ||
619 | You write the new primitive procedures and data types in the normal | |
620 | fashion, and link them into a shared library instead of into a | |
621 | standalone program. The shared library can then be loaded dynamically | |
622 | by Guile. | |
623 | ||
624 | @menu | |
5134bfa7 | 625 | * A Sample Guile Extension:: |
ac3e3f5b MV |
626 | @end menu |
627 | ||
628 | @node A Sample Guile Extension | |
629 | @subsection A Sample Guile Extension | |
630 | ||
631 | This section explains how to make the Bessel functions of the C library | |
632 | available to Scheme. First we need to write the appropriate glue code | |
633 | to convert the arguments and return values of the functions from Scheme | |
634 | to C and back. Additionally, we need a function that will add them to | |
635 | the set of Guile primitives. Because this is just an example, we will | |
636 | only implement this for the @code{j0} function, tho. | |
637 | ||
638 | Consider the following file @file{bessel.c}. | |
639 | ||
640 | @smallexample | |
641 | #include <math.h> | |
642 | #include <libguile.h> | |
643 | ||
644 | SCM | |
645 | j0_wrapper (SCM x) | |
646 | @{ | |
647 | return scm_make_real (j0 (scm_num2dbl (x, "j0"))); | |
648 | @} | |
649 | ||
650 | void | |
651 | init_bessel () | |
652 | @{ | |
653 | scm_make_gsubr ("j0", 1, 0, 0, j0_wrapper); | |
654 | @} | |
655 | @end smallexample | |
656 | ||
657 | This C source file needs to be compiled into a shared library. Here is | |
658 | how to do it on GNU/Linux: | |
659 | ||
660 | @smallexample | |
661 | gcc -shared -o libguile-bessel.so -fPIC bessel.c | |
662 | @end smallexample | |
663 | ||
664 | For creating shared libraries portably, we recommend the use of | |
665 | @code{GNU Libtool}. | |
666 | ||
667 | A shared library can be loaded into a running Guile process with | |
668 | @code{dynamic-link}. After it has been linked you can call its exported | |
669 | functions via @code{dynamic-call}. For our example, we are going to | |
670 | call the function @code{init_bessel} which will make @code{j0_wrapper} | |
671 | available to Scheme programs with the name @code{j0}. Note that we do | |
672 | not specify a filename extension such as @file{.so} when invoking | |
673 | @code{dynamic-link}. The right extension for the host platform will be | |
674 | provided automatically. | |
675 | ||
676 | @smalllisp | |
677 | (define bessel-lib (dynamic-link "libguile-bessel")) | |
678 | (dynamic-call "init_bessel" bessel-lib) | |
679 | (j0 2) | |
680 | @result{} 0.223890779141236 | |
681 | @end smalllisp | |
682 | ||
683 | For this to work, @code{dynamic-link} must be able to find | |
684 | @file{libguile-bessel}, of course. It will look in the places that are | |
685 | usual for your operating system, and it will additionally look into the | |
686 | directories listed in the @code{LTDL_LIBRRAY_PATH} environment variable. | |
687 | ||
688 | To see how these Guile extensions via shared libraries relate to the | |
689 | module system, see below REFFIXME. | |
38a93523 | 690 | |
95a62aed MV |
691 | @node Guile Modules |
692 | @section Guile Modules | |
38a93523 | 693 | |
ac3e3f5b | 694 | Guile has support for dividing a program into @dfn{modules}. By using |
95a62aed MV |
695 | modules, you can group related code together and manage the |
696 | composition of complete programs from largely independent parts. | |
ac3e3f5b MV |
697 | |
698 | (The module system is in flux, and will likely look very different in | |
699 | the future. Feel free to use the existing system anyway. Guile will | |
700 | provide reasonable backwards compatability.) | |
38a93523 | 701 | |
95a62aed | 702 | @menu |
5134bfa7 TTN |
703 | * Using Guile Modules:: |
704 | * Writing New Modules:: | |
705 | * Modules and Extensions:: | |
95a62aed MV |
706 | @end menu |
707 | ||
708 | @node Using Guile Modules | |
709 | @subsection Using Existing Modules | |
710 | ||
2da0d971 MG |
711 | Guile comes with a lot of useful modules, for example for string |
712 | processing or command line parsing. Additionally, there exist many | |
713 | Guile modules written by other Guile hackers, but which have to be | |
714 | installed manually. | |
715 | ||
716 | Existing modules have to be placed in places where Guile looks for them | |
5134bfa7 TTN |
717 | by default or in colon-separated directories in the environment variable |
718 | @code{GUILE_LOAD_PATH}. When this variable is set, those directories | |
719 | are searched first, then the the default. The following command | |
720 | shows the complete list of directories searched: | |
721 | ||
722 | @smallexample | |
723 | guile -c '(for-each write-line %load-path)' | |
724 | @end smallexample | |
2da0d971 MG |
725 | |
726 | Suppose you want to use the procedures and variables exported by the | |
727 | module @code{(ice-9 popen)}, which provides the means for communicating | |
728 | with other processes over pipes. Add the following line to your | |
729 | currently running Guile REPL or the top of you script file. | |
730 | ||
731 | @lisp | |
732 | (use-modules (ice-9 popen)) | |
733 | @end lisp | |
734 | ||
735 | This will load the module and make the procedures exported by | |
736 | @code{(ice-9 popen)} automatically available. The next step could be to | |
737 | open a pipe to @file{ls} and read the contents of the current directory, | |
738 | one line at a time. | |
739 | ||
740 | @lisp | |
741 | (define p (open-input-pipe "ls -l")) | |
742 | (read-line p) | |
743 | @result{} | |
744 | "total 30" | |
745 | (read-line p) | |
746 | @result{} | |
747 | "drwxr-sr-x 2 mgrabmue mgrabmue 1024 Mar 29 19:57 CVS" | |
748 | @end lisp | |
749 | ||
750 | More details of module usage can be found in (REFFIXME). | |
751 | ||
95a62aed MV |
752 | |
753 | @node Writing New Modules | |
754 | @subsection Writing New Modules | |
755 | ||
2da0d971 MG |
756 | Of course it is possible to write modules yourself. Using modules for |
757 | structuring your programs makes them more readable and lets you | |
758 | distribute them more easily. Also, explicitly defining the procedures | |
759 | and variables which are exported from a module adds documentation to the | |
760 | source and specifies the interface a module provides. | |
761 | ||
762 | In Guile, you can create new modules and switch to exisiting modules in | |
763 | order to add bindings to them using the syntactic form | |
764 | @code{define-module}. | |
765 | ||
766 | @lisp | |
767 | (define-module (foo bar)) | |
768 | ||
769 | (define (frob x) x) | |
770 | @end lisp | |
771 | ||
772 | Will create the module @code{(foo bar)}.@footnote{It is only convention | |
773 | that the module names in this section have two elements. One or more | |
774 | than two elements are perfectly fine, such as @code{(foo)} or @code{(foo | |
775 | bar braz)}} All definitions following this statement will add bindings | |
776 | to the module @code{(foo bar)}, and these bindings will not be visible | |
777 | outside of the module. To make the bindings accessible to other | |
778 | modules, you have to export them explicitly using one of the following | |
779 | means: | |
780 | ||
781 | @itemize @bullet | |
782 | @item | |
783 | Export them with the @code{export} form: | |
784 | @lisp | |
785 | (export frob) | |
786 | @end lisp | |
787 | ||
788 | @item | |
789 | Include them into the @code{define-module} form with the keyword | |
790 | @code{export}: | |
791 | @lisp | |
792 | (define-module (foo bar) | |
793 | #:export (frob)) | |
794 | @end lisp | |
795 | ||
796 | @item | |
797 | Change the definition of @code{frob} to use @code{define-public}, which | |
798 | is a combination of @code{define} and @code{export}. | |
799 | @lisp | |
800 | (define-public (frob x) x) | |
801 | @end lisp | |
802 | @end itemize | |
803 | ||
804 | After exporting, other modules can access the exported items simply by | |
805 | using @code{use-modules} to load the module @code{(foo bar)}. | |
806 | ||
95a62aed MV |
807 | |
808 | @node Modules and Extensions | |
809 | @subsection Modules and Extensions | |
810 | ||
811 | In addition to Scheme code you can also put new procedures and other | |
812 | named features that are provided by an extension into a module. | |
813 | ||
814 | You do this by writing a small Scheme file that defines the module. | |
815 | That Scheme file in turn invokes @code{dynamic-link} and | |
816 | @code{dynamic-call} as explained above to make the extension | |
817 | available. | |
818 | ||
819 | Suppose we want to put the Bessel function @code{j0} from the example | |
820 | extension into a module called @code{(math bessel)}. We would have to | |
821 | write a Scheme file with this contents | |
822 | ||
823 | @smallexample | |
824 | (define-module (math bessel)) | |
825 | ||
826 | (dynamic-call "init_bessel" (dynamic-link "libguile-bessel")) | |
827 | @end smallexample | |
828 | ||
a4318577 | 829 | The file should of course be saved in the right place for autoloading, |
95a62aed | 830 | for example as @file{/usr/local/share/guile/math/bessel.scm}. |
38a93523 NJ |
831 | |
832 | @page | |
833 | @node Reporting Bugs | |
834 | @chapter Reporting Bugs | |
835 | ||
836 | Any problems with the installation should be reported to | |
837 | @email{bug-guile@@gnu.org}. | |
838 | ||
2da0d971 | 839 | Whenever you have found a bug in Guile you are encouraged to report it |
5134bfa7 TTN |
840 | to the Guile developers, so they can fix it. They may also be able to |
841 | suggest workarounds when it is not possible for you to apply the bugfix | |
842 | or install a new version of Guile yourself. | |
2da0d971 MG |
843 | |
844 | Before sending in bug reports, please check with the following list that | |
845 | you really have found a bug. | |
846 | ||
847 | @itemize @bullet | |
848 | @item | |
849 | Whenever documentation and actual behaviour differ, you have certainly | |
850 | found a bug, either in the documentation or in the program. | |
851 | ||
852 | @item | |
853 | When Guile crashes, it is a bug. | |
854 | ||
855 | @item | |
856 | When Guile hangs or takes forever to complete a task, it is a bug. | |
857 | ||
858 | @item | |
859 | When calculations produce wrong results, it is a bug. | |
860 | ||
861 | @item | |
862 | When Guile signals an error for valid Scheme programs, it is a bug. | |
863 | ||
864 | @item | |
2da0d971 MG |
865 | When Guile does not signal an error for invalid Scheme programs, it may |
866 | be a bug, unless this is explicitly documented. | |
867 | ||
868 | @item | |
869 | When some part of the documentation is not clear and does not make sense | |
870 | to you even after re--reading the section, it is a bug. | |
871 | @end itemize | |
872 | ||
873 | When you write a bug report, please make sure to include as much of the | |
874 | information described below in the report. If you can't figure out some | |
875 | of the items, it is not a problem, but the more information we get, the | |
5134bfa7 | 876 | more likely we can diagnose and fix the bug. |
2da0d971 MG |
877 | |
878 | @itemize @bullet | |
879 | @item | |
880 | The version number of Guile. Without this, we won't know whether there | |
881 | is any point in looking for the bug in the current version of Guile. | |
882 | ||
883 | You can get the version number by invoking the command | |
884 | ||
885 | @example | |
886 | $ guile --version | |
887 | Guile 1.4.1 | |
888 | Copyright (c) 1995, 1996, 1997, 2000 Free Software Foundation | |
889 | Guile may be distributed under the terms of the GNU General Public Licence; | |
890 | certain other uses are permitted as well. For details, see the file | |
891 | `COPYING', which is included in the Guile distribution. | |
892 | There is no warranty, to the extent permitted by law. | |
893 | @end example | |
894 | ||
895 | @item | |
896 | The type of machine you are using, and the operating system name and | |
897 | version number. On GNU systems, you can get it with @file{uname}. | |
898 | ||
899 | @example | |
900 | $ uname -a | |
901 | Linux tortoise 2.2.17 #1 Thu Dec 21 17:29:05 CET 2000 i586 unknown | |
902 | @end example | |
903 | ||
904 | @item | |
905 | The operands given to the @file{configure} command when Guile was | |
5134bfa7 TTN |
906 | installed. It's often useful to augment this with the output of the |
907 | command @code{guile-config info}. | |
2da0d971 MG |
908 | |
909 | @item | |
910 | A complete list of any modifications you have made to the Guile source. | |
911 | (We may not have time to investigate the bug unless it happens in an | |
912 | unmodified Guile. But if you've made modifications and you don't tell | |
913 | us, you are sending us on a wild goose chase.) | |
914 | ||
915 | Be precise about these changes. A description in English is not | |
916 | enough--send a context diff for them. | |
917 | ||
918 | Adding files of your own, or porting to another machine, is a | |
919 | modification of the source. | |
920 | ||
921 | @item | |
922 | Details of any other deviations from the standard procedure for | |
923 | installing Guile. | |
924 | ||
925 | @item | |
926 | The complete text of any source files needed to reproduce the bug. | |
927 | ||
928 | If you can tell us a way to cause the problem without loading any source | |
929 | files, please do so. This makes it much easier to debug. If you do | |
930 | need files, make sure you arrange for us to see their exact contents. | |
931 | ||
932 | @item | |
933 | The precise Guile invocation command line we need to type to reproduce | |
934 | the bug. | |
935 | ||
936 | @item | |
937 | A description of what behavior you observe that you believe is | |
938 | incorrect. For example, "The Guile process gets a fatal signal," or, | |
939 | "The resulting output is as follows, which I think is wrong." | |
940 | ||
941 | Of course, if the bug is that Guile gets a fatal signal, then one can't | |
942 | miss it. But if the bug is incorrect results, the maintainer might fail | |
943 | to notice what is wrong. Why leave it to chance? | |
944 | ||
945 | If the manifestation of the bug is an Guile error message, it is | |
946 | important to report the precise text of the error message, and a | |
947 | backtrace showing how the Scheme program arrived at the error. | |
948 | ||
949 | This can be done using the procedure @code{backtrace} in the REPL. | |
950 | ||
951 | @item | |
952 | Check whether any programs you have loaded into Guile, including your | |
5134bfa7 | 953 | @file{.guile} file, set any variables that may affect the functioning of |
2da0d971 | 954 | Guile. Also, see whether the problem happens in a freshly started Guile |
5134bfa7 TTN |
955 | without loading your @file{.guile} file (start Guile with the @code{-q} |
956 | switch to prevent loading the init file). If the problem does | |
957 | @emph{not} occur then, you must report the precise contents of any | |
958 | programs that you must load into Guile in order to cause the problem to | |
959 | occur. | |
2da0d971 MG |
960 | |
961 | @item | |
962 | If the problem does depend on an init file or other Lisp programs that | |
963 | are not part of the standard Guile distribution, then you should make | |
964 | sure it is not a bug in those programs by complaining to their | |
965 | maintainers first. After they verify that they are using Guile in a way | |
966 | that is supposed to work, they should report the bug. | |
967 | ||
968 | @item | |
969 | If you wish to mention something in the Guile source, show the line of | |
970 | code with a few lines of context. Don't just give a line number. | |
971 | ||
972 | The line numbers in the development sources don't match those in your | |
973 | sources. It would take extra work for the maintainers to determine what | |
974 | code is in your version at a given line number, and we could not be | |
975 | certain. | |
976 | ||
977 | @item | |
978 | Additional information from a C debugger such as GDB might enable | |
979 | someone to find a problem on a machine which he does not have available. | |
980 | If you don't know how to use GDB, please read the GDB manual--it is not | |
981 | very long, and using GDB is easy. You can find the GDB distribution, | |
982 | including the GDB manual in online form, in most of the same places you | |
983 | can find the Guile distribution. To run Guile under GDB, you should | |
5134bfa7 TTN |
984 | switch to the @file{libguile} subdirectory in which Guile was compiled, then |
985 | do @code{gdb .libs/guile}. | |
986 | @c fixme: libguile/.libs is for libtool-enabled systems -- what about rest? | |
2da0d971 MG |
987 | |
988 | However, you need to think when you collect the additional information | |
989 | if you want it to show what causes the bug. | |
990 | ||
991 | For example, many people send just a backtrace, but that is not very | |
992 | useful by itself. A simple backtrace with arguments often conveys | |
993 | little about what is happening inside Guile, because most of the | |
994 | arguments listed in the backtrace are pointers to Scheme objects. The | |
995 | numeric values of these pointers have no significance whatever; all that | |
996 | matters is the contents of the objects they point to (and most of the | |
997 | contents are themselves pointers). | |
998 | @end itemize | |
999 | ||
38a93523 NJ |
1000 | |
1001 | ||
1002 | @c Local Variables: | |
1003 | @c TeX-master: "guile.texi" | |
1004 | @c End: |