gh.texi 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202
  1. @c -*-texinfo-*-
  2. @c This is part of the GNU Guile Reference Manual.
  3. @c Copyright (C) 1996, 1997, 2000, 2001, 2002, 2003, 2004
  4. @c Free Software Foundation, Inc.
  5. @c See the file guile.texi for copying conditions.
  6. @page
  7. @node GH
  8. @section GH: A Portable C to Scheme Interface
  9. @cindex libguile - gh
  10. @cindex gh
  11. @cindex gh - reference manual
  12. This chapter shows how to use the GH interface to call Guile from your
  13. application's C code, and to add new Scheme level procedures to Guile
  14. whose behaviour is specified by application specific code written in C.
  15. Note, however, that the GH interface is now deprecated, and developers
  16. are encouraged to switch to using the scm interface instead. Therefore,
  17. for each GH feature, this chapter also documents how to achieve
  18. the same result using the scm interface.
  19. @menu
  20. * GH deprecation:: Why the GH interface is now deprecated.
  21. * Transitioning away from GH::
  22. * GH preliminaries::
  23. * Data types and constants defined by GH::
  24. * Starting and controlling the interpreter::
  25. * Error messages::
  26. * Executing Scheme code::
  27. * Defining new Scheme procedures in C::
  28. * Converting data between C and Scheme::
  29. * Type predicates::
  30. * Equality predicates::
  31. * Memory allocation and garbage collection::
  32. * Calling Scheme procedures from C::
  33. @end menu
  34. @node GH deprecation
  35. @subsection Why the GH Interface is Now Deprecated
  36. Historically, the GH interface was the product of a practical problem
  37. and a neat idea. The practical problem was that the interface of the
  38. @code{scm_} functions with which Guile itself was written (inherited
  39. from Aubrey Jaffer's SCM) was so closely tied to the (rather arcane)
  40. details of the internal data representation that it was extremely
  41. difficult to write a Guile extension using these functions. The neat
  42. idea was to define a high level language extension interface in such a
  43. way that other extension language projects, not just Guile, would be
  44. able to provide an implementation of that interface; then applications
  45. using this interface could be compiled with whichever of the various
  46. available implementations they chose. So the GH interface was created,
  47. and advertised both as the recommended interface for application
  48. developers wishing to use Guile, and as a portable high level interface
  49. that could theoretically be implemented by other extension language
  50. projects.
  51. Time passed, and various things changed. Crucially, an enormous number
  52. of improvements were made to the @code{scm_} interface that Guile itself
  53. uses in its implementation, with the result that it is now both easy and
  54. comfortable to write a Guile extension with this interface. At the same
  55. time, the contents of the GH interface were somewhat neglected by the
  56. core Guile developers, such that some key operations --- such as smob
  57. creation and management --- are simply not possible using GH alone.
  58. Finally, the idea of multiple implementations of the GH interface did
  59. not really crystallize (apart, I believe, from a short lived
  60. implementation by the MzScheme project).
  61. For all these reasons, the Guile developers have decided to deprecate
  62. the GH interface --- which means that support for GH will be completely
  63. removed after the next few releases --- and to focus only on the
  64. @code{scm_} interface, with additions to ensure that it is as easy to
  65. use in all respects as GH was.
  66. It remains an open question whether a deep kind of interface portability
  67. would be useful for extension language-based applications, and it may
  68. still be an interesting project to attempt to define a corresponding
  69. GH-like interface, but the Guile developers no longer plan to try to do
  70. this as part of the core Guile project.
  71. @node Transitioning away from GH
  72. @subsection Transitioning away from GH
  73. The following table summarizes how to transition from the GH to the
  74. scm interface. The replacements that are recommended are not always
  75. completely equivalent to the GH functionality that they should
  76. replace. Therefore, you should read the reference documentation of
  77. the replacements carefully if you are not yet familiar with them.
  78. @table @asis
  79. @item Header file
  80. Use @code{#include <libguile.h>} instead of @code{#include
  81. <guile/gh.h>}.
  82. @item Compiling and Linking
  83. Use @code{guile-config} to pick up the flags required to compile C or
  84. C++ code that uses @code{libguile}, like so
  85. @smallexample
  86. $(CC) -o prog.o -c prog.c `guile-config compile`
  87. @end smallexample
  88. If you are using libtool to link your executables, just use
  89. @code{-lguile} in your link command. Libtool will expand this into
  90. the needed linker options automatically. If you are not using
  91. libtool, use the @code{guile-config} program to query the needed
  92. options explicitly. A linker command like
  93. @smallexample
  94. $(CC) -o prog prog.o `guile-config link`
  95. @end smallexample
  96. should be all that is needed. To link shared libraries that will be
  97. used as Guile Extensions, use libtool to control both the compilation
  98. and the link stage.
  99. @item The @code{SCM} type
  100. No change: the scm interface also uses this type to represent an
  101. arbitrary Scheme value.
  102. @item @code{SCM_BOOL_F} and @code{SCM_BOOL_T}
  103. No change.
  104. @item @code{SCM_UNSPECIFIED} and @code{SCM_UNDEFINED}
  105. No change.
  106. @item @code{gh_enter}
  107. Use @code{scm_boot_guile} instead, but note that @code{scm_boot_guile}
  108. has a slightly different calling convention from @code{gh_enter}:
  109. @code{scm_boot_guile}, and the main program function that you specify
  110. for @code{scm_boot_guile} to call, both take an additional @var{closure}
  111. parameter. @ref{Guile Initialization Functions} for more details.
  112. @item @code{gh_repl}
  113. Use @code{scm_shell} instead.
  114. @item @code{gh_init}
  115. Use @code{scm_init_guile} instead.
  116. @item @code{gh_catch}
  117. Use @code{scm_internal_catch} instead.
  118. @item @code{gh_eval_str}
  119. Use @code{scm_c_eval_string} instead.
  120. @item @code{gh_eval_str_with_catch}
  121. Use @code{scm_c_eval_string} together with @code{scm_internal_catch}
  122. instead.
  123. @item @code{gh_eval_str_with_standard_handler}
  124. Use @code{scm_c_eval_string} together with @code{scm_internal_catch}
  125. and @code{scm_handle_by_message_no_exit} instead.
  126. @item @code{gh_eval_str_with_stack_saving_handler}
  127. Use @code{scm_c_eval_string} together with
  128. @code{scm_internal_stack_catch} and
  129. @code{scm_handle_by_message_no_exit} instead.
  130. @item @code{gh_eval_file} or @code{gh_load}
  131. Use @code{scm_c_primitive_load} instead.
  132. @item @code{gh_eval_file_with_catch}
  133. Use @code{scm_c_primitive_load} together with
  134. @code{scm_internal_catch} instead.
  135. @item @code{gh_eval_file_with_standard_handler}
  136. Use @code{scm_c_primitive_load} together with
  137. @code{scm_internal_catch} and @code{scm_handle_by_message_no_exit}
  138. instead.
  139. @item @code{gh_new_procedure}
  140. @itemx @code{gh_new_procedure0_0}
  141. @itemx @code{gh_new_procedure0_1}
  142. @itemx @code{gh_new_procedure0_2}
  143. @itemx @code{gh_new_procedure1_0}
  144. @itemx @code{gh_new_procedure1_1}
  145. @itemx @code{gh_new_procedure1_2}
  146. @itemx @code{gh_new_procedure2_0}
  147. @itemx @code{gh_new_procedure2_1}
  148. @itemx @code{gh_new_procedure2_2}
  149. @itemx @code{gh_new_procedure3_0}
  150. @itemx @code{gh_new_procedure4_0}
  151. @itemx @code{gh_new_procedure5_0}
  152. Use @code{scm_c_define_gsubr} instead, but note that the arguments are
  153. in a different order: for @code{scm_c_define_gsubr} the C function
  154. pointer is the last argument. @ref{A Sample Guile Extension} for an
  155. example.
  156. @item @code{gh_defer_ints} and @code{gh_allow_ints}
  157. Use @code{SCM_CRITICAL_SECTION_START} and
  158. @code{SCM_CRITICAL_SECTION_END} instead. Note that these macros are
  159. used without parentheses, as in @code{SCM_DEFER_INTS;}.
  160. @item @code{gh_bool2scm}
  161. Use @code{scm_from_bool} instead.
  162. @item @code{gh_int2scm}
  163. Use @code{scm_from_int} instead.
  164. @item @code{gh_ulong2scm}
  165. Use @code{scm_from_ulong} instead.
  166. @item @code{gh_long2scm}
  167. Use @code{scm_from_long} instead.
  168. @item @code{gh_double2scm}
  169. Use @code{scm_make_real} instead.
  170. @item @code{gh_char2scm}
  171. Use @code{SCM_MAKE_CHAR} instead.
  172. @item @code{gh_str2scm}
  173. Use @code{scm_from_locale_stringn} instead.
  174. @item @code{gh_str02scm}
  175. Use @code{scm_from_locale_string} instead.
  176. @item @code{gh_set_substr}
  177. Use @code{scm_string_copy_x}.
  178. @item @code{gh_symbol2scm}
  179. Use @code{scm_from_locale_symbol} instead.
  180. @item @code{gh_ints2scm}
  181. @itemx @code{gh_doubles2scm}
  182. @itemx @code{gh_chars2byvect}
  183. @itemx @code{gh_shorts2svect}
  184. @itemx @code{gh_longs2ivect}
  185. @itemx @code{gh_ulongs2uvect}
  186. @itemx @code{gh_floats2fvect}
  187. @itemx @code{gh_doubles2dvect}
  188. Use the uniform numeric vector function, @xref{Uniform Numeric
  189. Vectors}.
  190. @item @code{gh_scm2bool}
  191. Use @code{scm_is_true} or @code{scm_to_bool} instead.
  192. @item @code{gh_scm2int}
  193. Use @code{scm_to_int} instead.
  194. @item @code{gh_scm2ulong}
  195. Use @code{scm_to_ulong} instead.
  196. @item @code{gh_scm2long}
  197. Use @code{scm_to_long} instead.
  198. @item @code{gh_scm2double}
  199. Use @code{scm_to_double} instead.
  200. @item @code{gh_scm2char}
  201. Use @code{scm_to_char} instead.
  202. @item @code{gh_scm2newstr}
  203. Use @code{scm_to_locale_string} or similar instead.
  204. @item @code{gh_get_substr}
  205. Use @code{scm_c_substring} together with @code{scm_to_locale_string}
  206. or similar instead.
  207. @item @code{gh_symbol2newstr}
  208. Use @code{scm_symbol_to_string} together with @code{scm_to_locale_string} or similar instead.
  209. @item @code{gh_scm2chars}
  210. Use @code{scm_from_locale_string} (or similar) or the uniform numeric
  211. vector functions (@pxref{Uniform Numeric Vectors}) instead.
  212. @item @code{gh_scm2shorts}
  213. @itemx @code{gh_scm2longs}
  214. @itemx @code{gh_scm2floats}
  215. @itemx @code{gh_scm2doubles}
  216. Use the uniform numeric vector function, @xref{Uniform Numeric
  217. Vectors}.
  218. @item @code{gh_boolean_p}
  219. Use @code{scm_is_bool} instead.
  220. @item @code{gh_symbol_p}
  221. Use @code{scm_is_symbol} instead.
  222. @item @code{gh_char_p}
  223. Replace @code{gh_char_p (@var{obj})} with
  224. @example
  225. scm_is_true (scm_char_p (@var{obj}))
  226. @end example
  227. @item @code{gh_vector_p}
  228. Replace @code{gh_vector_p (@var{obj})} with
  229. @example
  230. scm_is_true (scm_vector_p (@var{obj}))
  231. @end example
  232. @item @code{gh_pair_p}
  233. Replace @code{gh_pair_p (@var{obj})} with
  234. @example
  235. scm_is_true (scm_pair_p (@var{obj}))
  236. @end example
  237. @item @code{gh_number_p}
  238. Use @code{scm_is_number} instead.
  239. @item @code{gh_string_p}
  240. Use @code{scm_is_string} instead.
  241. @item @code{gh_procedure_p}
  242. Replace @code{gh_procedure_p (@var{obj})} by
  243. @example
  244. scm_is_true (scm_procedure_p (@var{obj}))
  245. @end example
  246. @item @code{gh_list_p}
  247. Replace @code{gh_list_p (@var{obj})} with
  248. @example
  249. scm_is_true (scm_list_p (@var{obj}))
  250. @end example
  251. @item @code{gh_inexact_p}
  252. Replace @code{gh_inexact_p (@var{obj})} with
  253. @example
  254. scm_is_true (scm_inexact_p (@var{obj}))
  255. @end example
  256. @item @code{gh_exact_p}
  257. Replace @code{gh_exact_p (@var{obj})} with
  258. @example
  259. scm_is_true (scm_exact_p (@var{obj}))
  260. @end example
  261. @item @code{gh_eq_p}
  262. Use @code{scm_is_eq} instead.
  263. @item @code{gh_eqv_p}
  264. Replace @code{gh_eqv_p (@var{x}, @var{y})} with
  265. @example
  266. scm_is_true (scm_eqv_p (@var{x}, @var{y}))
  267. @end example
  268. @item @code{gh_equal_p}
  269. Replace @code{gh_equal_p (@var{x}, @var{y})} with
  270. @example
  271. scm_is_true (scm_equal_p (@var{x}, @var{y}))
  272. @end example
  273. @item @code{gh_string_equal_p}
  274. Replace @code{gh_string_equal_p (@var{x}, @var{y})} with
  275. @example
  276. scm_is_true (scm_string_equal_p (@var{x}, @var{y}))
  277. @end example
  278. @item @code{gh_null_p}
  279. Use @code{scm_is_null} instead.
  280. @item @code{gh_not}
  281. Use @code{scm_not} instead.
  282. @item @code{gh_make_string}
  283. Use @code{scm_make_string} instead.
  284. @item @code{gh_string_length}
  285. Use @code{scm_string_length} instead.
  286. @item @code{gh_string_ref}
  287. Use @code{scm_string_ref} instead.
  288. @item @code{gh_string_set_x}
  289. Use @code{scm_string_set_x} instead.
  290. @item @code{gh_substring}
  291. Use @code{scm_substring} instead.
  292. @item @code{gh_string_append}
  293. Use @code{scm_string_append} instead.
  294. @item @code{gh_cons}
  295. Use @code{scm_cons} instead.
  296. @item @code{gh_car} and @code{gh_cdr}
  297. Use @code{scm_car} and @code{scm_cdr} instead.
  298. @item @code{gh_cxxr} and @code{gh_cxxxr}
  299. (Where each x is either @samp{a} or @samp{d}.) Use the corresponding
  300. @code{scm_cxxr} or @code{scm_cxxxr} function instead.
  301. @item @code{gh_set_car_x} and @code{gh_set_cdr_x}
  302. Use @code{scm_set_car_x} and @code{scm_set_cdr_x} instead.
  303. @item @code{gh_list}
  304. Use @code{scm_list_n} instead.
  305. @item @code{gh_length}
  306. Replace @code{gh_length (@var{lst})} with
  307. @example
  308. scm_to_size_t (scm_length (@var{lst}))
  309. @end example
  310. @item @code{gh_append}
  311. Use @code{scm_append} instead.
  312. @item @code{gh_append2}, @code{gh_append3}, @code{gh_append4}
  313. Replace @code{gh_append@var{N} (@var{l1}, @dots{}, @var{lN})} by
  314. @example
  315. scm_append (scm_list_n (@var{l1}, @dots{}, @var{lN}, SCM_UNDEFINED))
  316. @end example
  317. @item @code{gh_reverse}
  318. Use @code{scm_reverse} instead.
  319. @item @code{gh_list_tail} and @code{gh_list_ref}
  320. Use @code{scm_list_tail} and @code{scm_list_ref} instead.
  321. @item @code{gh_memq}, @code{gh_memv} and @code{gh_member}
  322. Use @code{scm_memq}, @code{scm_memv} and @code{scm_member} instead.
  323. @item @code{gh_assq}, @code{gh_assv} and @code{gh_assoc}
  324. Use @code{scm_assq}, @code{scm_assv} and @code{scm_assoc} instead.
  325. @item @code{gh_make_vector}
  326. Use @code{scm_make_vector} instead.
  327. @item @code{gh_vector} or @code{gh_list_to_vector}
  328. Use @code{scm_vector} instead.
  329. @item @code{gh_vector_ref} and @code{gh_vector_set_x}
  330. Use @code{scm_vector_ref} and @code{scm_vector_set_x} instead.
  331. @item @code{gh_vector_length}
  332. Use @code{scm_c_vector_length} instead.
  333. @item @code{gh_uniform_vector_length}
  334. Use @code{scm_c_uniform_vector_length} instead.
  335. @item @code{gh_uniform_vector_ref}
  336. Use @code{scm_c_uniform_vector_ref} instead.
  337. @item @code{gh_vector_to_list}
  338. Use @code{scm_vector_to_list} instead.
  339. @item @code{gh_apply}
  340. Use @code{scm_apply_0} instead.
  341. @item @code{gh_call0}
  342. @itemx @code{gh_call1}
  343. @itemx @code{gh_call2}
  344. @itemx @code{gh_call3}
  345. Use @code{scm_call_0}, @code{scm_call_1}, etc instead.
  346. @item @code{gh_display}
  347. @itemx @code{gh_write}
  348. @itemx @code{gh_newline}
  349. Use @code{scm_display (obj, scm_current_output_port ())} instead, etc.
  350. @item @code{gh_lookup}
  351. Use @code{scm_variable_ref (scm_c_lookup (name))} instead.
  352. @item @code{gh_module_lookup}
  353. Use @code{scm_variable_ref (scm_c_module_lookup (module, name))} instead.
  354. @end table
  355. @node GH preliminaries
  356. @subsection GH preliminaries
  357. To use gh, you must have the following toward the beginning of your C
  358. source:
  359. @smallexample
  360. #include <guile/gh.h>
  361. @end smallexample
  362. @cindex gh - headers
  363. When you link, you will have to add at least @code{-lguile} to the list
  364. of libraries. If you are using more of Guile than the basic Scheme
  365. interpreter, you will have to add more libraries.
  366. @cindex gh - linking
  367. @node Data types and constants defined by GH
  368. @subsection Data types and constants defined by GH
  369. The following C constants and data types are defined in gh:
  370. @code{SCM} is a C data type used to store all Scheme data, no matter what the
  371. Scheme type. Values are converted between C data types and the SCM type
  372. with utility functions described below (@pxref{Converting data between C
  373. and Scheme}). [FIXME: put in references to Jim's essay and so forth.]
  374. @defvr Constant SCM_BOOL_T
  375. @defvrx Constant SCM_BOOL_F
  376. The @emph{Scheme} values returned by many boolean procedures in
  377. libguile.
  378. This can cause confusion because they are different from 0 and 1. In
  379. testing a boolean function in libguile programming, you must always make
  380. sure that you check the spec: @code{gh_} and @code{scm_} functions will
  381. usually return @code{SCM_BOOL_T} and @code{SCM_BOOL_F}, but other C
  382. functions usually can be tested against 0 and 1, so programmers' fingers
  383. tend to just type @code{if (boolean_function()) @{ ... @}}
  384. @end defvr
  385. @defvr Constant SCM_UNSPECIFIED
  386. This is a SCM value that is not the same as any legal Scheme value. It
  387. is the value that a Scheme function returns when its specification says
  388. that its return value is unspecified.
  389. @end defvr
  390. @defvr Constant SCM_UNDEFINED
  391. This is another SCM value that is not the same as any legal Scheme
  392. value. It is the value used to mark variables that do not yet have a
  393. value, and it is also used in C to terminate functions with variable
  394. numbers of arguments, such as @code{gh_list()}.
  395. @end defvr
  396. @node Starting and controlling the interpreter
  397. @subsection Starting and controlling the interpreter
  398. @cindex libguile - start interpreter
  399. In almost every case, your first @code{gh_} call will be:
  400. @deftypefun void gh_enter (int @var{argc}, char *@var{argv}[], void (*@var{main_prog})())
  401. Starts up a Scheme interpreter with all the builtin Scheme primitives.
  402. @code{gh_enter()} never exits, and the user's code should all be in the
  403. @code{@var{main_prog}()} function. @code{argc} and @code{argv} will be
  404. passed to @var{main_prog}.
  405. @deftypefun void main_prog (int @var{argc}, char *@var{argv}[])
  406. This is the user's main program. It will be invoked by
  407. @code{gh_enter()} after Guile has been started up.
  408. @end deftypefun
  409. Note that you can use @code{gh_repl} inside @code{gh_enter} (in other
  410. words, inside the code for @code{main-prog}) if you want the program to
  411. be controlled by a Scheme read-eval-print loop.
  412. @end deftypefun
  413. @cindex read eval print loop -- from the gh_ interface
  414. @cindex REPL -- from the gh_ interface
  415. A convenience routine which enters the Guile interpreter with the
  416. standard Guile read-eval-print loop (@dfn{REPL}) is:
  417. @deftypefun void gh_repl (int @var{argc}, char *@var{argv}[])
  418. Enters the Scheme interpreter giving control to the Scheme REPL.
  419. Arguments are processed as if the Guile program @file{guile} were being
  420. invoked.
  421. Note that @code{gh_repl} should be used @emph{inside} @code{gh_enter},
  422. since any Guile interpreter calls are meaningless unless they happen in
  423. the context of the interpreter.
  424. Also note that when you use @code{gh_repl}, your program will be
  425. controlled by Guile's REPL (which is written in Scheme and has many
  426. useful features). Use straight C code inside @code{gh_enter} if you
  427. want to maintain execution control in your C program.
  428. @end deftypefun
  429. You will typically use @code{gh_enter} and @code{gh_repl} when you
  430. want a Guile interpreter enhanced by your own libraries, but otherwise
  431. quite normal. For example, to build a Guile--derived program that
  432. includes some random number routines @dfn{GSL} (GNU Scientific Library),
  433. you would write a C program that looks like this:
  434. @smallexample
  435. #include <guile/gh.h>
  436. #include <gsl_ran.h>
  437. /* random number suite */
  438. SCM gw_ran_seed(SCM s)
  439. @{
  440. gsl_ran_seed(gh_scm2int(s));
  441. return SCM_UNSPECIFIED;
  442. @}
  443. SCM gw_ran_random()
  444. @{
  445. SCM x;
  446. x = gh_ulong2scm(gsl_ran_random());
  447. return x;
  448. @}
  449. SCM gw_ran_uniform()
  450. @{
  451. SCM x;
  452. x = gh_double2scm(gsl_ran_uniform());
  453. return x;
  454. @}
  455. SCM gw_ran_max()
  456. @{
  457. return gh_double2scm(gsl_ran_max());
  458. @}
  459. void
  460. init_gsl()
  461. @{
  462. /* random number suite */
  463. gh_new_procedure("gsl-ran-seed", gw_ran_seed, 1, 0, 0);
  464. gh_new_procedure("gsl-ran-random", gw_ran_random, 0, 0, 0);
  465. gh_new_procedure("gsl-ran-uniform", gw_ran_uniform, 0, 0, 0);
  466. gh_new_procedure("gsl-ran-max", gw_ran_max, 0, 0, 0);
  467. @}
  468. void
  469. main_prog (int argc, char *argv[])
  470. @{
  471. init_gsl();
  472. gh_repl(argc, argv);
  473. @}
  474. int
  475. main (int argc, char *argv[])
  476. @{
  477. gh_enter (argc, argv, main_prog);
  478. @}
  479. @end smallexample
  480. Then, supposing the C program is in @file{guile-gsl.c}, you could
  481. compile it with @kbd{gcc -o guile-gsl guile-gsl.c -lguile -lgsl}.
  482. The resulting program @file{guile-gsl} would have new primitive
  483. procedures @code{gsl-ran-random}, @code{gsl-ran-gaussian} and so forth.
  484. @node Error messages
  485. @subsection Error messages
  486. @cindex libguile - error messages
  487. @cindex error messages in libguile
  488. [FIXME: need to fill this based on Jim's new mechanism]
  489. @node Executing Scheme code
  490. @subsection Executing Scheme code
  491. @cindex libguile - executing Scheme
  492. @cindex executing Scheme
  493. Once you have an interpreter running, you can ask it to evaluate Scheme
  494. code. There are two calls that implement this:
  495. @deftypefun SCM gh_eval_str (char *@var{scheme_code})
  496. This asks the interpreter to evaluate a single string of Scheme code,
  497. and returns the result of the last expression evaluated.
  498. Note that the line of code in @var{scheme_code} must be a well formed
  499. Scheme expression. If you have many lines of code before you balance
  500. parentheses, you must either concatenate them into one string, or use
  501. @code{gh_eval_file()}.
  502. @end deftypefun
  503. @deftypefun SCM gh_eval_file (char *@var{fname})
  504. @deftypefunx SCM gh_load (char *@var{fname})
  505. @code{gh_eval_file} is completely analogous to @code{gh_eval_str()},
  506. except that a whole file is evaluated instead of a string.
  507. @code{gh_eval_file} returns @code{SCM_UNSPECIFIED}.
  508. @code{gh_load} is identical to @code{gh_eval_file} (it's a macro that
  509. calls @code{gh_eval_file} on its argument). It is provided to start
  510. making the @code{gh_} interface match the R5RS Scheme procedures
  511. closely.
  512. @end deftypefun
  513. @node Defining new Scheme procedures in C
  514. @subsection Defining new Scheme procedures in C
  515. @cindex libguile - new procedures
  516. @cindex new procedures
  517. @cindex procedures, new
  518. @cindex new primitives
  519. @cindex primitives, new
  520. The real interface between C and Scheme comes when you can write new
  521. Scheme procedures in C. This is done through the routine
  522. @deftypefn {Libguile high} SCM gh_new_procedure (char *@var{proc_name}, SCM (*@var{fn})(), int @var{n_required_args}, int @var{n_optional_args}, int @var{restp})
  523. @code{gh_new_procedure} defines a new Scheme procedure. Its Scheme name
  524. will be @var{proc_name}, it will be implemented by the C function
  525. (*@var{fn})(), it will take at least @var{n_required_args} arguments,
  526. and at most @var{n_optional_args} extra arguments.
  527. When the @var{restp} parameter is 1, the procedure takes a final
  528. argument: a list of remaining parameters.
  529. @code{gh_new_procedure} returns an SCM value representing the procedure.
  530. The C function @var{fn} should have the form
  531. @deftypefn {Libguile high} SCM fn (SCM @var{req1}, SCM @var{req2}, ..., SCM @var{opt1}, SCM @var{opt2}, ..., SCM @var{rest_args})
  532. The arguments are all passed as SCM values, so the user will have to use
  533. the conversion functions to convert to standard C types.
  534. Examples of C functions used as new Scheme primitives can be found in
  535. the sample programs @code{learn0} and @code{learn1}.
  536. @end deftypefn
  537. @end deftypefn
  538. @strong{Rationale:} this is the correct way to define new Scheme
  539. procedures in C. The ugly mess of arguments is required because of how
  540. C handles procedures with variable numbers of arguments.
  541. @strong{NB:} what about documentation strings?
  542. @cartouche
  543. There are several important considerations to be made when writing the C
  544. routine @code{(*fn)()}.
  545. First of all the C routine has to return type @code{SCM}.
  546. Second, all arguments passed to the C function will be of type
  547. @code{SCM}.
  548. Third: the C routine is now subject to Scheme flow control, which means
  549. that it could be interrupted at any point, and then reentered. This
  550. means that you have to be very careful with operations such as
  551. allocating memory, modifying static data @dots{}
  552. Fourth: to get around the latter issue, you can use
  553. @code{GH_DEFER_INTS} and @code{GH_ALLOW_INTS}.
  554. @end cartouche
  555. @defmac GH_DEFER_INTS
  556. @defmacx GH_ALLOW_INTS
  557. These macros disable and re-enable Scheme's flow control. They
  558. @end defmac
  559. @c [??? have to do this right; maybe using subsections, or maybe creating a
  560. @c section called Flow control issues...]
  561. @c [??? Go into exhaustive detail with examples of the various possible
  562. @c combinations of required and optional args...]
  563. @node Converting data between C and Scheme
  564. @subsection Converting data between C and Scheme
  565. @cindex libguile - converting data
  566. @cindex data conversion
  567. @cindex converting data
  568. Guile provides mechanisms to convert data between C and Scheme. This
  569. allows new builtin procedures to understand their arguments (which are
  570. of type @code{SCM}) and return values of type @code{SCM}.
  571. @menu
  572. * C to Scheme::
  573. * Scheme to C::
  574. @end menu
  575. @node C to Scheme
  576. @subsubsection C to Scheme
  577. @deftypefun SCM gh_bool2scm (int @var{x})
  578. Returns @code{#f} if @var{x} is zero, @code{#t} otherwise.
  579. @end deftypefun
  580. @deftypefun SCM gh_ulong2scm (unsigned long @var{x})
  581. @deftypefunx SCM gh_long2scm (long @var{x})
  582. @deftypefunx SCM gh_double2scm (double @var{x})
  583. @deftypefunx SCM gh_char2scm (char @var{x})
  584. Returns a Scheme object with the value of the C quantity @var{x}.
  585. @end deftypefun
  586. @deftypefun SCM gh_str2scm (char *@var{s}, int @var{len})
  587. Returns a new Scheme string with the (not necessarily null-terminated) C
  588. array @var{s} data.
  589. @end deftypefun
  590. @deftypefun SCM gh_str02scm (char *@var{s})
  591. Returns a new Scheme string with the null-terminated C string @var{s}
  592. data.
  593. @end deftypefun
  594. @deftypefun SCM gh_set_substr (char *@var{src}, SCM @var{dst}, int @var{start}, int @var{len})
  595. Copy @var{len} characters at @var{src} into the @emph{existing} Scheme
  596. string @var{dst}, starting at @var{start}. @var{start} is an index into
  597. @var{dst}; zero means the beginning of the string.
  598. If @var{start} + @var{len} is off the end of @var{dst}, signal an
  599. out-of-range error.
  600. @end deftypefun
  601. @deftypefun SCM gh_symbol2scm (char *@var{name})
  602. Given a null-terminated string @var{name}, return the symbol with that
  603. name.
  604. @end deftypefun
  605. @deftypefun SCM gh_ints2scm (int *@var{dptr}, int @var{n})
  606. @deftypefunx SCM gh_doubles2scm (double *@var{dptr}, int @var{n})
  607. Make a scheme vector containing the @var{n} ints or doubles at memory
  608. location @var{dptr}.
  609. @end deftypefun
  610. @deftypefun SCM gh_chars2byvect (char *@var{dptr}, int @var{n})
  611. @deftypefunx SCM gh_shorts2svect (short *@var{dptr}, int @var{n})
  612. @deftypefunx SCM gh_longs2ivect (long *@var{dptr}, int @var{n})
  613. @deftypefunx SCM gh_ulongs2uvect (ulong *@var{dptr}, int @var{n})
  614. @deftypefunx SCM gh_floats2fvect (float *@var{dptr}, int @var{n})
  615. @deftypefunx SCM gh_doubles2dvect (double *@var{dptr}, int @var{n})
  616. Make a scheme uniform vector containing the @var{n} chars, shorts,
  617. longs, unsigned longs, floats or doubles at memory location @var{dptr}.
  618. @end deftypefun
  619. @node Scheme to C
  620. @subsubsection Scheme to C
  621. @deftypefun int gh_scm2bool (SCM @var{obj})
  622. @deftypefunx {unsigned long} gh_scm2ulong (SCM @var{obj})
  623. @deftypefunx long gh_scm2long (SCM @var{obj})
  624. @deftypefunx double gh_scm2double (SCM @var{obj})
  625. @deftypefunx int gh_scm2char (SCM @var{obj})
  626. These routines convert the Scheme object to the given C type.
  627. @end deftypefun
  628. @deftypefun {char *} gh_scm2newstr (SCM @var{str}, size_t *@var{lenp})
  629. Given a Scheme string @var{str}, return a pointer to a new copy of its
  630. contents, followed by a null byte. If @var{lenp} is non-null, set
  631. @code{*@var{lenp}} to the string's length.
  632. This function uses malloc to obtain storage for the copy; the caller is
  633. responsible for freeing it.
  634. Note that Scheme strings may contain arbitrary data, including null
  635. characters. This means that null termination is not a reliable way to
  636. determine the length of the returned value. However, the function
  637. always copies the complete contents of @var{str}, and sets @var{*lenp}
  638. to the true length of the string (when @var{lenp} is non-null).
  639. @end deftypefun
  640. @deftypefun void gh_get_substr (SCM str, char *return_str, int *lenp)
  641. Copy @var{len} characters at @var{start} from the Scheme string
  642. @var{src} to memory at @var{dst}. @var{start} is an index into
  643. @var{src}; zero means the beginning of the string. @var{dst} has
  644. already been allocated by the caller.
  645. If @var{start} + @var{len} is off the end of @var{src}, signal an
  646. out-of-range error.
  647. @end deftypefun
  648. @deftypefun {char *} gh_symbol2newstr (SCM @var{sym}, int *@var{lenp})
  649. Takes a Scheme symbol and returns a string of the form
  650. @code{"'symbol-name"}. If @var{lenp} is non-null, the string's length
  651. is returned in @code{*@var{lenp}}.
  652. This function uses malloc to obtain storage for the returned string; the
  653. caller is responsible for freeing it.
  654. @end deftypefun
  655. @deftypefun {char *} gh_scm2chars (SCM @var{vector}, chars *@var{result})
  656. @deftypefunx {short *} gh_scm2shorts (SCM @var{vector}, short *@var{result})
  657. @deftypefunx {long *} gh_scm2longs (SCM @var{vector}, long *@var{result})
  658. @deftypefunx {float *} gh_scm2floats (SCM @var{vector}, float *@var{result})
  659. @deftypefunx {double *} gh_scm2doubles (SCM @var{vector}, double *@var{result})
  660. Copy the numbers in @var{vector} to the array pointed to by @var{result}
  661. and return it. If @var{result} is NULL, allocate a double array large
  662. enough.
  663. @var{vector} can be an ordinary vector, a weak vector, or a signed or
  664. unsigned uniform vector of the same type as the result array. For
  665. chars, @var{vector} can be a string or substring. For floats and
  666. doubles, @var{vector} can contain a mix of inexact and integer values.
  667. If @var{vector} is of unsigned type and contains values too large to fit
  668. in the signed destination array, those values will be wrapped around,
  669. that is, data will be copied as if the destination array was unsigned.
  670. @end deftypefun
  671. @node Type predicates
  672. @subsection Type predicates
  673. These C functions mirror Scheme's type predicate procedures with one
  674. important difference. The C routines return C boolean values (0 and 1)
  675. instead of @code{SCM_BOOL_T} and @code{SCM_BOOL_F}.
  676. The Scheme notational convention of putting a @code{?} at the end of
  677. predicate procedure names is mirrored in C by placing @code{_p} at the
  678. end of the procedure. For example, @code{(pair? ...)} maps to
  679. @code{gh_pair_p(...)}.
  680. @deftypefun int gh_boolean_p (SCM @var{val})
  681. Returns 1 if @var{val} is a boolean, 0 otherwise.
  682. @end deftypefun
  683. @deftypefun int gh_symbol_p (SCM @var{val})
  684. Returns 1 if @var{val} is a symbol, 0 otherwise.
  685. @end deftypefun
  686. @deftypefun int gh_char_p (SCM @var{val})
  687. Returns 1 if @var{val} is a char, 0 otherwise.
  688. @end deftypefun
  689. @deftypefun int gh_vector_p (SCM @var{val})
  690. Returns 1 if @var{val} is a vector, 0 otherwise.
  691. @end deftypefun
  692. @deftypefun int gh_pair_p (SCM @var{val})
  693. Returns 1 if @var{val} is a pair, 0 otherwise.
  694. @end deftypefun
  695. @deftypefun int gh_procedure_p (SCM @var{val})
  696. Returns 1 if @var{val} is a procedure, 0 otherwise.
  697. @end deftypefun
  698. @deftypefun int gh_list_p (SCM @var{val})
  699. Returns 1 if @var{val} is a list, 0 otherwise.
  700. @end deftypefun
  701. @deftypefun int gh_inexact_p (SCM @var{val})
  702. Returns 1 if @var{val} is an inexact number, 0 otherwise.
  703. @end deftypefun
  704. @deftypefun int gh_exact_p (SCM @var{val})
  705. Returns 1 if @var{val} is an exact number, 0 otherwise.
  706. @end deftypefun
  707. @node Equality predicates
  708. @subsection Equality predicates
  709. These C functions mirror Scheme's equality predicate procedures with one
  710. important difference. The C routines return C boolean values (0 and 1)
  711. instead of @code{SCM_BOOL_T} and @code{SCM_BOOL_F}.
  712. The Scheme notational convention of putting a @code{?} at the end of
  713. predicate procedure names is mirrored in C by placing @code{_p} at the
  714. end of the procedure. For example, @code{(equal? ...)} maps to
  715. @code{gh_equal_p(...)}.
  716. @deftypefun int gh_eq_p (SCM x, SCM y)
  717. Returns 1 if @var{x} and @var{y} are equal in the sense of Scheme's
  718. @code{eq?} predicate, 0 otherwise.
  719. @end deftypefun
  720. @deftypefun int gh_eqv_p (SCM x, SCM y)
  721. Returns 1 if @var{x} and @var{y} are equal in the sense of Scheme's
  722. @code{eqv?} predicate, 0 otherwise.
  723. @end deftypefun
  724. @deftypefun int gh_equal_p (SCM x, SCM y)
  725. Returns 1 if @var{x} and @var{y} are equal in the sense of Scheme's
  726. @code{equal?} predicate, 0 otherwise.
  727. @end deftypefun
  728. @deftypefun int gh_string_equal_p (SCM @var{s1}, SCM @var{s2})
  729. Returns 1 if the strings @var{s1} and @var{s2} are equal, 0 otherwise.
  730. @end deftypefun
  731. @deftypefun int gh_null_p (SCM @var{l})
  732. Returns 1 if @var{l} is an empty list or pair; 0 otherwise.
  733. @end deftypefun
  734. @node Memory allocation and garbage collection
  735. @subsection Memory allocation and garbage collection
  736. @c [FIXME: flesh this out with some description of garbage collection in
  737. @c scm/guile]
  738. @c @deftypefun SCM gh_mkarray (int size)
  739. @c Allocate memory for a Scheme object in a garbage-collector-friendly
  740. @c manner.
  741. @c @end deftypefun
  742. @node Calling Scheme procedures from C
  743. @subsection Calling Scheme procedures from C
  744. Many of the Scheme primitives are available in the @code{gh_}
  745. interface; they take and return objects of type SCM, and one could
  746. basically use them to write C code that mimics Scheme code.
  747. I will list these routines here without much explanation, since what
  748. they do is the same as documented in @ref{Standard procedures, R5RS, ,
  749. r5rs, R5RS}. But I will point out that when a procedure takes a
  750. variable number of arguments (such as @code{gh_list}), you should pass
  751. the constant @var{SCM_UNDEFINED} from C to signify the end of the list.
  752. @deftypefun SCM gh_define (char *@var{name}, SCM @var{val})
  753. Corresponds to the Scheme @code{(define name val)}: it binds a value to
  754. the given name (which is a C string). Returns the new object.
  755. @end deftypefun
  756. @heading Pairs and lists
  757. @deftypefun SCM gh_cons (SCM @var{a}, SCM @var{b})
  758. @deftypefunx SCM gh_list (SCM l0, SCM l1, ... , SCM_UNDEFINED)
  759. These correspond to the Scheme @code{(cons a b)} and @code{(list l0 l1
  760. ...)} procedures. Note that @code{gh_list()} is a C macro that invokes
  761. @code{scm_list_n()}.
  762. @end deftypefun
  763. @deftypefun SCM gh_car (SCM @var{obj})
  764. @deftypefunx SCM gh_cdr (SCM @var{obj})
  765. @dots{}
  766. @deftypefunx SCM gh_c[ad][ad][ad][ad]r (SCM @var{obj})
  767. These correspond to the Scheme @code{(caadar ls)} procedures etc @dots{}
  768. @end deftypefun
  769. @deftypefun SCM gh_set_car_x (SCM @var{pair}, SCM @var{value})
  770. Modifies the CAR of @var{pair} to be @var{value}. This is equivalent to
  771. the Scheme procedure @code{(set-car! ...)}.
  772. @end deftypefun
  773. @deftypefun SCM gh_set_cdr_x (SCM @var{pair}, SCM @var{value})
  774. Modifies the CDR of @var{pair} to be @var{value}. This is equivalent to
  775. the Scheme procedure @code{(set-cdr! ...)}.
  776. @end deftypefun
  777. @deftypefun {unsigned long} gh_length (SCM @var{ls})
  778. Returns the length of the list.
  779. @end deftypefun
  780. @deftypefun SCM gh_append (SCM @var{args})
  781. @deftypefunx SCM gh_append2 (SCM @var{l1}, SCM @var{l2})
  782. @deftypefunx SCM gh_append3 (SCM @var{l1}, SCM @var{l2}, @var{l3})
  783. @deftypefunx SCM gh_append4 (SCM @var{l1}, SCM @var{l2}, @var{l3}, @var{l4})
  784. @code{gh_append()} takes @var{args}, which is a list of lists
  785. @code{(list1 list2 ...)}, and returns a list containing all the elements
  786. of the individual lists.
  787. A typical invocation of @code{gh_append()} to append 5 lists together
  788. would be
  789. @smallexample
  790. gh_append(gh_list(l1, l2, l3, l4, l5, SCM_UNDEFINED));
  791. @end smallexample
  792. The functions @code{gh_append2()}, @code{gh_append2()},
  793. @code{gh_append3()} and @code{gh_append4()} are convenience routines to
  794. make it easier for C programs to form the list of lists that goes as an
  795. argument to @code{gh_append()}.
  796. @end deftypefun
  797. @deftypefun SCM gh_reverse (SCM @var{ls})
  798. Returns a new list that has the same elements as @var{ls} but in the
  799. reverse order. Note that this is implemented as a macro which calls
  800. @code{scm_reverse()}.
  801. @end deftypefun
  802. @deftypefun SCM gh_list_tail (SCM @var{ls}, SCM @var{k})
  803. Returns the sublist of @var{ls} with the last @var{k} elements.
  804. @end deftypefun
  805. @deftypefun SCM gh_list_ref (SCM @var{ls}, SCM @var{k})
  806. Returns the @var{k}th element of the list @var{ls}.
  807. @end deftypefun
  808. @deftypefun SCM gh_memq (SCM @var{x}, SCM @var{ls})
  809. @deftypefunx SCM gh_memv (SCM @var{x}, SCM @var{ls})
  810. @deftypefunx SCM gh_member (SCM @var{x}, SCM @var{ls})
  811. These functions return the first sublist of @var{ls} whose CAR is
  812. @var{x}. They correspond to @code{(memq x ls)}, @code{(memv x ls)} and
  813. @code{(member x ls)}, and hence use (respectively) @code{eq?},
  814. @code{eqv?} and @code{equal?} to do comparisons.
  815. If @var{x} does not appear in @var{ls}, the value @code{SCM_BOOL_F} (not
  816. the empty list) is returned.
  817. Note that these functions are implemented as macros which call
  818. @code{scm_memq()}, @code{scm_memv()} and @code{scm_member()}
  819. respectively.
  820. @end deftypefun
  821. @deftypefun SCM gh_assq (SCM @var{x}, SCM @var{alist})
  822. @deftypefunx SCM gh_assv (SCM @var{x}, SCM @var{alist})
  823. @deftypefunx SCM gh_assoc (SCM @var{x}, SCM @var{alist})
  824. These functions search an @dfn{association list} (list of pairs)
  825. @var{alist} for the first pair whose CAR is @var{x}, and they return
  826. that pair.
  827. If no pair in @var{alist} has @var{x} as its CAR, the value
  828. @code{SCM_BOOL_F} (not the empty list) is returned.
  829. Note that these functions are implemented as macros which call
  830. @code{scm_assq()}, @code{scm_assv()} and @code{scm_assoc()}
  831. respectively.
  832. @end deftypefun
  833. @heading Symbols
  834. @c @deftypefun SCM gh_symbol (SCM str, SCM len)
  835. @c @deftypefunx SCM gh_tmp_symbol (SCM str, SCM len)
  836. @c Takes the given string @var{str} of length @var{len} and returns a
  837. @c symbol corresponding to that string.
  838. @c @end deftypefun
  839. @heading Vectors
  840. @deftypefun SCM gh_make_vector (SCM @var{n}, SCM @var{fill})
  841. @deftypefunx SCM gh_vector (SCM @var{ls})
  842. @deftypefunx SCM gh_vector_ref (SCM @var{v}, SCM @var{i})
  843. @deftypefunx SCM gh_vector_set (SCM @var{v}, SCM @var{i}, SCM @var{val})
  844. @deftypefunx {unsigned long} gh_vector_length (SCM @var{v})
  845. @deftypefunx SCM gh_list_to_vector (SCM @var{ls})
  846. These correspond to the Scheme @code{(make-vector n fill)},
  847. @code{(vector a b c ...)} @code{(vector-ref v i)} @code{(vector-set v i
  848. value)} @code{(vector-length v)} @code{(list->vector ls)} procedures.
  849. The correspondence is not perfect for @code{gh_vector}: this routine
  850. takes a list @var{ls} instead of the individual list elements, thus
  851. making it identical to @code{gh_list_to_vector}.
  852. There is also a difference in gh_vector_length: the value returned is a
  853. C @code{unsigned long} instead of an SCM object.
  854. @end deftypefun
  855. @heading Procedures
  856. @c @deftypefun SCM gh_make_subr (SCM (*@var{fn})(), int @var{req}, int @var{opt}, int @var{restp}, char *@var{sym})
  857. @c Make the C function @var{fn} available to Scheme programs. The function
  858. @c will be bound to the symbol @var{sym}. The arguments @var{req},
  859. @c @var{opt} and @var{restp} describe @var{fn}'s calling conventions. The
  860. @c function must take @var{req} required arguments and may take @var{opt}
  861. @c optional arguments. Any optional arguments which are not supplied by
  862. @c the caller will be bound to @var{SCM_UNSPECIFIED}. If @var{restp} is
  863. @c non-zero, it means that @var{fn} may be called with an arbitrary number
  864. @c of arguments, and that any extra arguments supplied by the caller will
  865. @c be passed to @var{fn} as a list. The @var{restp} argument is exactly
  866. @c like Scheme's @code{(lambda (arg1 arg2 . arglist))} calling convention.
  867. @c
  868. @c For example, the procedure @code{read-line}, which takes optional
  869. @c @var{port} and @var{handle-delim} arguments, would be declared like so:
  870. @c
  871. @c @example
  872. @c SCM scm_read_line (SCM port, SCM handle_delim);
  873. @c gh_make_subr (scm_read_line, 0, 2, 0, "read-line");
  874. @c @end example
  875. @c
  876. @c The @var{req} argument to @code{gh_make_subr} is 0 to indicate that
  877. @c there are no required arguments, so @code{read-line} may be called
  878. @c without any arguments at all. The @var{opt} argument is 2, to indicate
  879. @c that both the @var{port} and @var{handle_delim} arguments to
  880. @c @code{scm_read_line} are optional, and will be bound to
  881. @c @code{SCM_UNSPECIFIED} if the calling program does not supply them.
  882. @c Because the @var{restp} argument is 0, this function may not be called
  883. @c with more than two arguments.
  884. @c @end deftypefun
  885. @deftypefun SCM gh_apply (SCM proc, SCM args)
  886. Call the Scheme procedure @var{proc}, with the elements of @var{args} as
  887. arguments. @var{args} must be a proper list.
  888. @end deftypefun
  889. @deftypefun SCM gh_call0 (SCM proc)
  890. @deftypefunx SCM gh_call1 (SCM proc, SCM arg)
  891. @deftypefunx SCM gh_call2 (SCM proc, SCM arg1, SCM arg2)
  892. @deftypefunx SCM gh_call3 (SCM proc, SCM arg1, SCM arg2, SCM arg3)
  893. Call the Scheme procedure @var{proc} with no arguments
  894. (@code{gh_call0}), one argument (@code{gh_call1}), and so on. You can
  895. get the same effect by wrapping the arguments up into a list, and
  896. calling @code{gh_apply}; Guile provides these functions for convenience.
  897. @end deftypefun
  898. @deftypefun SCM gh_catch (SCM key, SCM thunk, SCM handler)
  899. @deftypefunx SCM gh_throw (SCM key, SCM args)
  900. Corresponds to the Scheme @code{catch} and @code{throw} procedures,
  901. which in Guile are provided as primitives.
  902. @end deftypefun
  903. @c [FIXME: must add the I/O section in gscm.h]
  904. @deftypefun SCM gh_is_eq (SCM a, SCM b)
  905. @deftypefunx SCM gh_is_eqv (SCM a, SCM b)
  906. @deftypefunx SCM gh_is_equal (SCM a, SCM b)
  907. These correspond to the Scheme @code{eq?}, @code{eqv?} and @code{equal?}
  908. predicates.
  909. @end deftypefun
  910. @deftypefun int gh_obj_length (SCM @var{obj})
  911. Returns the raw object length.
  912. @end deftypefun
  913. @heading Data lookup
  914. For now I just include Tim Pierce's comments from the @file{gh_data.c}
  915. file; it should be organized into a documentation of the two functions
  916. here.
  917. @smallexample
  918. /* Data lookups between C and Scheme
  919. Look up a symbol with a given name, and return the object to which
  920. it is bound. gh_lookup examines the Guile top level, and
  921. gh_module_lookup checks the module name space specified by the
  922. `vec' argument.
  923. The return value is the Scheme object to which SNAME is bound, or
  924. SCM_UNDEFINED if SNAME is not bound in the given context. [FIXME:
  925. should this be SCM_UNSPECIFIED? Can a symbol ever legitimately be
  926. bound to SCM_UNDEFINED or SCM_UNSPECIFIED? What is the difference?
  927. -twp] */
  928. @end smallexample