oldfmt.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /* Copyright (C) 2000 Free Software Foundation, Inc.
  2. *
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation; either version 2, or (at your option)
  6. * any later version.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this software; see the file COPYING. If not, write to
  15. * the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  16. * Boston, MA 02111-1307 USA
  17. *
  18. * As a special exception, the Free Software Foundation gives permission
  19. * for additional uses of the text contained in its release of GUILE.
  20. *
  21. * The exception is that, if you link the GUILE library with other files
  22. * to produce an executable, this does not by itself cause the
  23. * resulting executable to be covered by the GNU General Public License.
  24. * Your use of that executable is in no way restricted on account of
  25. * linking the GUILE library code into it.
  26. *
  27. * This exception does not however invalidate any other reasons why
  28. * the executable file might be covered by the GNU General Public License.
  29. *
  30. * This exception applies only to the code released by the
  31. * Free Software Foundation under the name GUILE. If you copy
  32. * code from other Free Software Foundation releases into a copy of
  33. * GUILE, as the General Public License permits, the exception does
  34. * not apply to the code that you add in this way. To avoid misleading
  35. * anyone as to the status of such modified files, you must delete
  36. * this exception notice from them.
  37. *
  38. * If you write modifications of your own for GUILE, it is your choice
  39. * whether to permit this exception to apply to your modifications.
  40. * If you do not wish that, delete this exception notice.
  41. */
  42. /* From NEWS:
  43. *
  44. * * New primitive: `simple-format', affects `scm-error', scm_display_error, & scm_error message strings
  45. *
  46. * (ice-9 boot) makes `format' an alias for `simple-format' until possibly
  47. * extended by the more sophisticated version in (ice-9 format)
  48. *
  49. * (simple-format port message . args)
  50. * Write MESSAGE to DESTINATION, defaulting to `current-output-port'.
  51. * MESSAGE can contain ~A (was %s) and ~S (was %S) escapes. When printed,
  52. * the escapes are replaced with corresponding members of ARGS:
  53. * ~A formats using `display' and ~S formats using `write'.
  54. * If DESTINATION is #t, then use the `current-output-port',
  55. * if DESTINATION is #f, then return a string containing the formatted text.
  56. * Does not add a trailing newline."
  57. *
  58. * The two C procedures: scm_display_error and scm_error, as well as the
  59. * primitive `scm-error', now use scm_format to do their work. This means
  60. * that the message strings of all code must be updated to use ~A where %s
  61. * was used before, and ~S where %S was used before.
  62. *
  63. * During the period when there still are a lot of old Guiles out there,
  64. * you might want to support both old and new versions of Guile.
  65. *
  66. * There are basically two methods to achieve this. Both methods use
  67. * autoconf. Put
  68. *
  69. * AC_CHECK_FUNCS(scm_simple_format)
  70. *
  71. * in your configure.in.
  72. *
  73. * Method 1: Use the string concatenation features of ANSI C's
  74. * preprocessor.
  75. *
  76. * In C:
  77. *
  78. * #ifdef HAVE_SCM_SIMPLE_FORMAT
  79. * #define FMT_S "~S"
  80. * #else
  81. * #define FMT_S "%S"
  82. * #endif
  83. *
  84. * Then represent each of your error messages using a preprocessor macro:
  85. *
  86. * #define E_SPIDER_ERROR "There's a spider in your " ## FMT_S ## "!!!"
  87. *
  88. * In Scheme:
  89. *
  90. * (define fmt-s (if (defined? 'simple-format) "~S" "%S"))
  91. * (define make-message string-append)
  92. *
  93. * (define e-spider-error
  94. * (make-message "There's a spider in your " fmt-s "!!!"))
  95. *
  96. * Method 2: Use the oldfmt function found in doc/oldfmt.c.
  97. *
  98. * In C:
  99. *
  100. * scm_misc_error ("picnic", scm_c_oldfmt0 ("There's a spider in your ~S!!!"),
  101. * ...);
  102. *
  103. * In Scheme:
  104. *
  105. * (scm-error 'misc-error "picnic" (oldfmt "There's a spider in your ~S!!!")
  106. * ...)
  107. *
  108. */
  109. /*
  110. * Take a format string FROM adhering to the new standard format (~A and ~S
  111. * as placeholders) of length N and return a string which is adapted
  112. * to the format used by the Guile interpreter which you are running.
  113. *
  114. * On successive calls with similar strings but different storage, the
  115. * same string with same storage is returned. This is necessary since
  116. * the existence of a garbage collector in the system may cause the same
  117. * format string to be represented with different storage at different
  118. * calls.
  119. */
  120. char *
  121. scm_c_oldfmt (char *from, int n)
  122. {
  123. #ifdef HAVE_SCM_SIMPLE_FORMAT
  124. return from;
  125. #else
  126. static struct { int n; char *from; char *to; } *strings;
  127. static int size = 0;
  128. static int n_strings = 0;
  129. char *to;
  130. int i;
  131. for (i = 0; i < n_strings; ++i)
  132. if (n == strings[i].n && strncmp (from, strings[i].from, n) == 0)
  133. return strings[i].to;
  134. if (n_strings == size)
  135. {
  136. if (size == 0)
  137. {
  138. size = 10;
  139. strings = scm_must_malloc (size * sizeof (*strings), s_oldfmt);
  140. }
  141. else
  142. {
  143. int oldsize = size;
  144. size = 3 * oldsize / 2;
  145. strings = scm_must_realloc (strings,
  146. oldsize * sizeof (*strings),
  147. size * sizeof (*strings),
  148. s_oldfmt);
  149. }
  150. }
  151. strings[n_strings].n = n;
  152. strings[n_strings].from = strncpy (scm_must_malloc (n, s_oldfmt), from, n);
  153. to = strings[n_strings].to = scm_must_malloc (n + 1, s_oldfmt);
  154. n_strings++;
  155. for (i = 0; i < n; ++i)
  156. {
  157. if (from[i] == '~' && ++i < n)
  158. {
  159. if (from[i] == 'A')
  160. {
  161. to[i - 1] = '%';
  162. to[i] = 's';
  163. }
  164. else if (from[i] == 'S')
  165. {
  166. to[i - 1] = '%';
  167. to[i] = 'S';
  168. }
  169. else
  170. {
  171. to[i - 1] = '~';
  172. to[i] = from[i];
  173. }
  174. continue;
  175. }
  176. to[i] = from[i];
  177. }
  178. to[i] = '\0';
  179. return to;
  180. #endif
  181. }
  182. char *
  183. scm_c_oldfmt0 (char *s)
  184. {
  185. #ifdef HAVE_SCM_SIMPLE_FORMAT
  186. return s;
  187. #else
  188. return scm_c_oldfmt (s, strlen (s));
  189. #endif
  190. }
  191. SCM_PROC (s_oldfmt, "oldfmt", 1, 0, 0, scm_oldfmt);
  192. SCM
  193. scm_oldfmt (SCM s)
  194. {
  195. #ifdef HAVE_SCM_SIMPLE_FORMAT
  196. return s;
  197. #else
  198. int n;
  199. SCM_ASSERT (SCM_NIMP (s) && SCM_STRINGP (s), s, 1, s_oldfmt);
  200. n = SCM_LENGTH (s);
  201. return scm_return_first (scm_makfromstr (scm_c_oldfmt (SCM_ROCHARS (s), n),
  202. n,
  203. 0),
  204. s);
  205. #endif
  206. }