Updated to reflect changes to the guile-snarf tool.
[bpt/guile.git] / doc / ref / tools.texi
CommitLineData
abae3014 1@page
fc17ed5b
TTN
2@node Miscellaneous Tools
3@chapter Miscellaneous Tools
4
5Programming is more fun with a good tools. This chapter describes snarfing
6tools, and the @code{guile-tools} program which can be used to invoke the rest
7of the tools (which are self-documenting). Some of these are used in Guile
8development, too. Imagine that!
9
10@menu
11* Snarfing:: Grepping the source in various ways.
12* Executable Modules:: Modules callable via guile-tools.
13@end menu
14
15@c ---------------------------------------------------------------------------
16@node Snarfing
17@section Snarfing
18@cindex snarfing
19
20Because it's easier to maintain documentation, code, and other metainfo in one
21source file than in many files, there have evolved many methods for grepping
22source to lift and separate these kinds of info, in the process generating
23docs or fragments of source or what have you. This is known generally as
24@dfn{snarfing}, which comes from the verb ``to snarf'', here meaning ``to
25unceremoniously extract information from a somewhat unwilling source.''
26
27This section documents the installed program @code{guile-snarf} which does
28@dfn{init snarfing}, and also touches upon guile's doc snarfing process which
29is not yet finalized (i.e., doc snarfing programs are not installed at this
30time).
31
32@menu
33* Init Snarfing with guile-snarf:: Exposing C subrs and friends to Scheme.
34* Doc Snarfing:: Generating GDFv2 or texi from source.
35@end menu
36
37@c ---------------------------------------------------------------------------
38@node Init Snarfing with guile-snarf
39@subsection Init Snarfing with guile-snarf
40@c NOTE: This node and two subnodes are adapted from ../sources/snarf.texi.
41@cindex snarfing, init
42@cindex primitive functions
43@cindex subrs, defining
44
45When writing C code for use with Guile, you typically define a set of C
46functions, and then make some of them visible to the Scheme world by
47calling the @code{scm_c_define_gsubr} function; a C function published in
48this way is called a @dfn{subr}. If you have many subrs to publish, it
49can sometimes be annoying to keep the list of calls to
50@code{scm_c_define_gsubr} in sync with the list of function definitions.
51Frequently, a programmer will define a new subr in C, recompile the
52application, and then discover that the Scheme interpreter cannot see
53the subr, because of a missed call to @code{scm_c_define_gsubr}.
54
55Guile provides the @code{guile-snarf} command to manage this problem.
56Using this tool, you can keep all the information needed to define the
57subr alongside the function definition itself; @code{guile-snarf} will
58extract this information from your source code, and automatically
59generate a file of calls to @code{scm_c_define_gsubr} which you can
60@code{#include} into an initialization function.
61
62@menu
63* How guile-snarf works:: Using @code{guile-snarf}, with example.
64* Macros guile-snarf recognizes:: How to mark up code for @code{guile-snarf}.
65@end menu
66
67@c ---------------------------------------------------------------------------
68@node How guile-snarf works
69@subsubsection How guile-snarf works
70@cindex guile-snarf invocation
71@cindex guile-snarf example
72
b5bb4262 73Usage: guile-snarf [-d | -D] [-o OUTFILE] INFILE [CPP-OPTIONS ...]
fc17ed5b 74
d5e1d82d
TTN
75What @code{guile-snarf} does:
76
77Process INFILE using the C pre-processor and some other programs.
033aa07c
MV
78Write output to a file named OUTFILE or to the standard output when no
79OUTFILE has been specified or when OUTFILE is @code{-}. When writing
80to a file, ignore lines from the input matching the following grep(1)
81regular expression:
d5e1d82d
TTN
82
83@example
84 ^#include ".*OUTFILE"
85@end example
86
87If there are errors during processing, delete OUTFILE and exit with
88non-zero status.
89
b5bb4262
TTN
90Optional arg "-d" means grep INFILE for deprecated macros and
91issue a warning if any are found. Alternatively, "-D" means
92do the same thing but signal error and exit with non-zero status.
d5e1d82d
TTN
93
94If env var CPP is set, use its value instead of the C pre-processor
a2448fb1 95determined at Guile configure-time.
d5e1d82d 96
033aa07c
MV
97During snarfing, the pre-processor macro @code{SCM_MAGIC_SNARFER} is
98defined.
99
d5e1d82d 100@xref{Macros guile-snarf recognizes}, for a list of the special (some would
b5bb4262 101say magic) cpp macros you can use, including the list of deprecated macros.
fc17ed5b
TTN
102
103For example, here is how you might define a new subr called
104@code{clear-image}, implemented by the C function @code{clear_image}:
105
106@example
107@group
108#include <libguile.h>
109
110SCM_DEFINE (clear_image, "clear-image", 1, 0, 0,
111 (SCM image_smob),
112 "Clear the image.")
113#define FUNC_NAME s_clear_image
114@{
115 /* C code to clear the image... */
116@}
117#undef FUNC_NAME
118
119void
120init_image_type ()
121@{
122#include "image-type.x"
123@}
124@end group
125@end example
126
127The @code{SCM_DEFINE} declaration says that the C function
128@code{clear_image} implements a Scheme subr called @code{clear-image},
129which takes one required argument (type @code{SCM} named
130@code{image_smob}), no optional arguments, and no tail argument.
131@xref{Doc Snarfing}, for info on the docstring.
132
133This works in concert with @code{FUNC_NAME} to also define a static
134array of characters named @code{s_clear_image}, initialized to the
135string "clear-image". The body of @code{clear_image} may use the array
136in error messages, instead of writing out the literal string; this may
137save string space on some systems.
138
139Assuming the text above lives in a file named @file{image-type.c}, you will
140need to execute the following command to prepare this file for compilation:
141
142@example
d5e1d82d 143guile-snarf image-type.c
fc17ed5b
TTN
144@end example
145
146This scans @file{image-type.c} for @code{SCM_DEFINE}
147declarations, and writes to @file{image-type.x} the output:
148
149@example
150scm_c_define_gsubr (s_clear_image, 1, 0, 0, (SCM (*)() ) clear_image);
151@end example
152
153When compiled normally, @code{SCM_DEFINE} is a macro which expands to a
154declaration of the @code{s_clear_image} string.
155
156Note that the output file name matches the @code{#include} from the
157input file. Also, you still need to provide all the same information
158you would if you were using @code{scm_c_define_gsubr} yourself, but you
159can place the information near the function definition itself, so it is
160less likely to become incorrect or out-of-date.
161
162If you have many files that @code{guile-snarf} must process, you should
d5e1d82d 163consider using a fragment like the following in your Makefile:
fc17ed5b
TTN
164
165@example
d5e1d82d 166snarfcppopts = $(DEFS) $(INCLUDES) $(CPPFLAGS) $(CFLAGS)
fc17ed5b
TTN
167.SUFFIXES: .x
168.c.x:
d5e1d82d 169 guile-snarf -o $@ $< $(snarfcppopts)
fc17ed5b
TTN
170@end example
171
172This tells make to run @code{guile-snarf} to produce each needed
173@file{.x} file from the corresponding @file{.c} file.
174
d5e1d82d
TTN
175Aside from the required argument INFILE, @code{guile-snarf} passes its
176command-line arguments directly to the C preprocessor, which it uses to
177extract the information it needs from the source code. this means you can pass
178normal compilation flags to @code{guile-snarf} to define preprocessor symbols,
179add header file directories, and so on.
fc17ed5b
TTN
180
181@c ---------------------------------------------------------------------------
182@node Macros guile-snarf recognizes
183@subsubsection Macros guile-snarf recognizes
184@cindex guile-snarf recognized macros
b5bb4262 185@cindex guile-snarf deprecated macros
fc17ed5b
TTN
186
187Here are the macros you can use in your source code from which
188@code{guile-snarf} can construct initialization code:
189
190@example
191/* procedures */
192SCM_DEFINE (FNAME, PRIMNAME, REQ, OPT, VAR, ARGLIST, DOCSTRING)
193
194SCM_PROC (RANAME, STR, REQ, OPT, VAR, CFN)
195SCM_REGISTER_PROC (RANAME, STR, REQ, OPT, VAR, CFN)
196
197SCM_GPROC (RANAME, STR, REQ, OPT, VAR, CFN, GF)
198
199/* everything else */
200SCM_SYMBOL (c_name, scheme_name)
201SCM_GLOBAL_SYMBOL (c_name, scheme_name)
202
203SCM_KEYWORD (c_name, scheme_name)
204SCM_GLOBAL_KEYWORD (c_name, scheme_name)
205
206SCM_VARIABLE (c_name, scheme_name)
207SCM_GLOBAL_VARIABLE (c_name, scheme_name)
208
209SCM_VARIABLE_INIT (c_name, scheme_name, init_val)
210SCM_GLOBAL_VARIABLE_INIT (c_name, scheme_name, init_val)
211@end example
212
213@c i like things dense, but maybe someone else will reformat this
214@c into an easier-to-read list. also, all-upcase to me is a form
215@c of quoting, so @var{} is not necessary there. --ttn
216REQ and OPT are numbers indicating required and optional argument
217counts, respectively; VAR is a number that, if non-zero, means the
218function will accept any remaining arguments as a list; DOCSTRING is a
219string (use @code{\n\} at eol for multi-line); FNAME is a C-language
220identifier, CFN and GF and @var{c_name} likewise; PRIMNAME is a string
221denoting the name available to Scheme code, STR and @var{scheme_name}
222likewise; RANAME is the name of the static string (must match that
223declared by the associated definition of cpp macro @var{FUNC_NAME});
224ARGLIST is an argument list (in parentheses); and lastly, @var{init_val}
225is a expression suitable for initializing a new variable.
226
227For procedures, you can use @code{SCM_DEFINE} for most purposes. Use
228@code{SCM_PROC} along with @code{SCM_REGISTER_PROC} when you don't want
229to be bothered with docstrings. Use @code{SCM_GPROC} for generic
230functions (@pxref{GOOPS,,,goops}). All procedures are declared
231@code{static} with return type @code{SCM}.
232
233For everything else, use the appropriate macro (@code{SCM_SYMBOL} for
234symbols, and so on). The "_GLOBAL_" variants omit @code{static}
235declaration.
236
237All these macros should be used at top-level, outside function bodies.
238Also, it's a good idea to define @var{FUNC_NAME} immediately after using
239@code{SCM_DEFINE} (and similar), and then the function body, and then
240@code{#undef FUNC_NAME}.
241
b5bb4262 242Here is the list of deprecated macros:
d5e1d82d 243
b5bb4262 244@c reminder: sync w/ libguile/guile-snarf.in var `deprecated_list'
d5e1d82d 245@example
b5bb4262
TTN
246 SCM_CONST_LONG
247 SCM_VCELL
248 SCM_VCELL_INIT
249 SCM_GLOBAL_VCELL
250 SCM_GLOBAL_VCELL_INIT
d5e1d82d
TTN
251@end example
252
b5bb4262
TTN
253Some versions of guile (and guile-snarf) will continue to recognize them but
254at some point they will no longer work. You can pass either @code{-d} or
255@code{-D} option to have guile-snarf warn or signal error, respectively, if
256any of these are found in the input file.
257
258@xref{How guile-snarf works}, and also libguile source, for examples.
259@xref{Subrs}, for details on argument passing and how to write C
260functions.
d5e1d82d 261
fc17ed5b
TTN
262@c ---------------------------------------------------------------------------
263@node Doc Snarfing
264@subsection Doc Snarfing
265
266In addition to init snarfing (@pxref{Init Snarfing with guile-snarf}),
267the libguile sources are also subject to doc snarfing, by programs that
268are included in the distribution (but not installed at this time). The
269output is the file @file{guile-procedures.txt} which is installed, and
270subsequently used by module @code{(ice-9 documentation)}.
271
272Here is a list of what does what according to @file{libguile/Makefile.am}:
273
274@itemize
275@item guile-snarf-docs runs cpp defining SCM_MAGIC_SNARF_DOCS
276@item guile_filter_doc_snarfage parses guile-snarf-docs output to produce .doc
277@item ../scripts/snarf-check-and-output-texi makes guile.texi
278@item ../scripts/snarf-check-and-output-texi makes guile-procedures.txt
279@item guile-func-name-check checks source snarf-syntax integrity (optional?)
280@item guile-doc-snarf calls guile-snarf-docs (to make .doc) and guile-snarf
281@end itemize
282
283Note that for guile-1.4, a completely different approach was used! All this
284is rather byzantine, so for now @emph{NO} doc snarfing programs are installed.
285
286[fixme: Document further once doc snarfing is tamed somewhat. --ttn]
287
288@c ---------------------------------------------------------------------------
abae3014 289@node Executable Modules
fc17ed5b 290@section Executable Modules
abae3014
TTN
291@cindex guile-tools
292@cindex modules, executable
293@cindex executable modules
294@cindex scripts
295
296When Guile is installed, in addition to the @code{(ice-9 FOO)} modules,
297a set of @dfn{executable modules} @code{(scripts BAR)} is also installed.
298Each is a regular Scheme module that has some additional packaging so
299that it can be called as a program in its own right, from the shell. For this
300reason, we sometimes use the term @dfn{script} in this context to mean the
301same thing.
302
fc17ed5b
TTN
303@c wow look at this hole^! variable-width font users eat your heart out.
304
abae3014
TTN
305As a convenience, the @code{guile-tools} wrapper program is installed along w/
306@code{guile}; it knows where a particular module is installed and calls it
307passing its args to the program. The result is that you need not augment your
308PATH. Usage is straightforward:
309
310@example
311guile-tools --help
312guile-tools --version
313guile-tools [OPTION] PROGRAM [ARGS ...]
314
315If PROGRAM is "list" or omitted, display contents of scripts dir, otherwise
316PROGRAM is run w/ ARGS. Options (only one of which may be used at a time):
317 --scriptsdir DIR -- Look in DIR for scripts
318 --guileversion VERS -- Look in $pkgdatadir/VERS/scripts for scripts
319 --source -- Display PROGRAM source (ignore ARGS) to stdout
320@end example
321
322The modules are self-documenting. For example, to see the documentation for
323@code{lint}, use one (or both) of the shell commands:
324
325@example
326guile-tools display-commentary '(scripts lint)'
327guile-tools --source lint
328@end example
329
fc17ed5b 330The rest of this section describes the packaging that goes into creating an
abae3014
TTN
331executable module. Feel free to skip to the next chapter.
332
fc17ed5b 333@subsection Writing Executable Modules
abae3014
TTN
334
335@c adapted from scripts/README
336
337See template file @code{PROGRAM} for a quick start.
338
339Programs must follow the @dfn{executable module} convention, documented here:
340
341@itemize
342
343@item
344The file name must not end in ".scm".
345
346@item
347The file must be executable (chmod +x).
348
349@item
350The module name must be "(scripts PROGRAM)". A procedure named PROGRAM w/
351signature "(PROGRAM . args)" must be exported. Basically, use some variant
352of the form:
353
354@example
fc17ed5b
TTN
355(define-module (scripts PROGRAM)
356 :export (PROGRAM))
abae3014
TTN
357@end example
358
359Feel free to export other definitions useful in the module context.
360
361@item
362There must be the alias:
363
364@example
fc17ed5b 365(define main PROGRAM)
abae3014
TTN
366@end example
367
368However, `main' must NOT be exported.
369
370@item
371The beginning of the file must use the following invocation sequence:
372
373@example
fc17ed5b
TTN
374#!/bin/sh
375main='(module-ref (resolve-module '\''(scripts PROGRAM)) '\'main')'
376exec $@{GUILE-guile@} -l $0 -c "(apply $main (cdr (command-line)))" "$@@"
377!#
abae3014
TTN
378@end example
379
380@end itemize
381
382Following these conventions allows the program file to be used as module
383@code{(scripts PROGRAM)} in addition to as a standalone executable. Please
384also include a helpful Commentary section w/ some usage info.
385
386@c tools.texi ends here