oldfmt.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /* Copyright (C) 2000,2001, 2006, 2008 Free Software Foundation, Inc.
  2. *
  3. * This library is free software; you can redistribute it and/or
  4. * modify it under the terms of the GNU Lesser General Public
  5. * License as published by the Free Software Foundation; either
  6. * version 2.1 of the License, or (at your option) any later version.
  7. *
  8. * This library 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 GNU
  11. * Lesser General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU Lesser General Public
  14. * License along with this library; if not, write to the Free Software
  15. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  16. */
  17. /* From NEWS:
  18. *
  19. * * New primitive: `simple-format', affects `scm-error', scm_display_error, & scm_error message strings
  20. *
  21. * (ice-9 boot) makes `format' an alias for `simple-format' until possibly
  22. * extended by the more sophisticated version in (ice-9 format)
  23. *
  24. * (simple-format port message . args)
  25. * Write MESSAGE to DESTINATION, defaulting to `current-output-port'.
  26. * MESSAGE can contain ~A (was %s) and ~S (was %S) escapes. When printed,
  27. * the escapes are replaced with corresponding members of ARGS:
  28. * ~A formats using `display' and ~S formats using `write'.
  29. * If DESTINATION is #t, then use the `current-output-port',
  30. * if DESTINATION is #f, then return a string containing the formatted text.
  31. * Does not add a trailing newline."
  32. *
  33. * The two C procedures: scm_display_error and scm_error, as well as the
  34. * primitive `scm-error', now use scm_format to do their work. This means
  35. * that the message strings of all code must be updated to use ~A where %s
  36. * was used before, and ~S where %S was used before.
  37. *
  38. * During the period when there still are a lot of old Guiles out there,
  39. * you might want to support both old and new versions of Guile.
  40. *
  41. * There are basically two methods to achieve this. Both methods use
  42. * autoconf. Put
  43. *
  44. * AC_CHECK_FUNCS(scm_simple_format)
  45. *
  46. * in your configure.in.
  47. *
  48. * Method 1: Use the string concatenation features of ANSI C's
  49. * preprocessor.
  50. *
  51. * In C:
  52. *
  53. * #ifdef HAVE_SCM_SIMPLE_FORMAT
  54. * #define FMT_S "~S"
  55. * #else
  56. * #define FMT_S "%S"
  57. * #endif
  58. *
  59. * Then represent each of your error messages using a preprocessor macro:
  60. *
  61. * #define E_SPIDER_ERROR "There's a spider in your " ## FMT_S ## "!!!"
  62. *
  63. * In Scheme:
  64. *
  65. * (define fmt-s (if (defined? 'simple-format) "~S" "%S"))
  66. * (define make-message string-append)
  67. *
  68. * (define e-spider-error
  69. * (make-message "There's a spider in your " fmt-s "!!!"))
  70. *
  71. * Method 2: Use the oldfmt function found in doc/oldfmt.c.
  72. *
  73. * In C:
  74. *
  75. * scm_misc_error ("picnic", scm_c_oldfmt0 ("There's a spider in your ~S!!!"),
  76. * ...);
  77. *
  78. * In Scheme:
  79. *
  80. * (scm-error 'misc-error "picnic" (oldfmt "There's a spider in your ~S!!!")
  81. * ...)
  82. *
  83. */
  84. /*
  85. * Take a format string FROM adhering to the new standard format (~A and ~S
  86. * as placeholders) of length N and return a string which is adapted
  87. * to the format used by the Guile interpreter which you are running.
  88. *
  89. * On successive calls with similar strings but different storage, the
  90. * same string with same storage is returned. This is necessary since
  91. * the existence of a garbage collector in the system may cause the same
  92. * format string to be represented with different storage at different
  93. * calls.
  94. */
  95. char *
  96. scm_c_oldfmt (char *from, int n)
  97. {
  98. #ifdef HAVE_SCM_SIMPLE_FORMAT
  99. return from;
  100. #else
  101. static struct { int n; char *from; char *to; } *strings;
  102. static int size = 0;
  103. static int n_strings = 0;
  104. char *to;
  105. int i;
  106. for (i = 0; i < n_strings; ++i)
  107. if (n == strings[i].n && strncmp (from, strings[i].from, n) == 0)
  108. return strings[i].to;
  109. if (n_strings == size)
  110. {
  111. if (size == 0)
  112. {
  113. size = 10;
  114. strings = scm_must_malloc (size * sizeof (*strings), s_oldfmt);
  115. }
  116. else
  117. {
  118. int oldsize = size;
  119. size = 3 * oldsize / 2;
  120. strings = scm_must_realloc (strings,
  121. oldsize * sizeof (*strings),
  122. size * sizeof (*strings),
  123. s_oldfmt);
  124. }
  125. }
  126. strings[n_strings].n = n;
  127. strings[n_strings].from = strncpy (scm_must_malloc (n, s_oldfmt), from, n);
  128. to = strings[n_strings].to = scm_must_malloc (n + 1, s_oldfmt);
  129. n_strings++;
  130. for (i = 0; i < n; ++i)
  131. {
  132. if (from[i] == '~' && ++i < n)
  133. {
  134. if (from[i] == 'A')
  135. {
  136. to[i - 1] = '%';
  137. to[i] = 's';
  138. }
  139. else if (from[i] == 'S')
  140. {
  141. to[i - 1] = '%';
  142. to[i] = 'S';
  143. }
  144. else
  145. {
  146. to[i - 1] = '~';
  147. to[i] = from[i];
  148. }
  149. continue;
  150. }
  151. to[i] = from[i];
  152. }
  153. to[i] = '\0';
  154. return to;
  155. #endif
  156. }
  157. char *
  158. scm_c_oldfmt0 (char *s)
  159. {
  160. #ifdef HAVE_SCM_SIMPLE_FORMAT
  161. return s;
  162. #else
  163. return scm_c_oldfmt (s, strlen (s));
  164. #endif
  165. }
  166. SCM_PROC (s_oldfmt, "oldfmt", 1, 0, 0, scm_oldfmt);
  167. SCM
  168. scm_oldfmt (SCM s)
  169. {
  170. #ifdef HAVE_SCM_SIMPLE_FORMAT
  171. return s;
  172. #else
  173. int n;
  174. SCM_ASSERT (SCM_NIMP (s) && SCM_STRINGP (s), s, 1, s_oldfmt);
  175. n = SCM_LENGTH (s);
  176. return scm_return_first (scm_mem2string (scm_c_oldfmt (SCM_ROCHARS (s), n),
  177. n),
  178. s);
  179. #endif
  180. }