oldfmt.c 5.2 KB

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