1 /* Copyright (C) 2000,2001 Free Software Foundation, Inc.
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)
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.
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
18 * As a special exception, the Free Software Foundation gives permission
19 * for additional uses of the text contained in its release of GUILE.
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.
27 * This exception does not however invalidate any other reasons why
28 * the executable file might be covered by the GNU General Public License.
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.
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.
47 * * New primitive: `simple-format', affects `scm-error', scm_display_error, & scm_error message strings
49 * (ice-9 boot) makes `format' an alias for `simple-format' until possibly
50 * extended by the more sophisticated version in (ice-9 format)
52 * (simple-format port message . args)
53 * Write MESSAGE to DESTINATION, defaulting to `current-output-port'.
54 * MESSAGE can contain ~A (was %s) and ~S (was %S) escapes. When printed,
55 * the escapes are replaced with corresponding members of ARGS:
56 * ~A formats using `display' and ~S formats using `write'.
57 * If DESTINATION is #t, then use the `current-output-port',
58 * if DESTINATION is #f, then return a string containing the formatted text.
59 * Does not add a trailing newline."
61 * The two C procedures: scm_display_error and scm_error, as well as the
62 * primitive `scm-error', now use scm_format to do their work. This means
63 * that the message strings of all code must be updated to use ~A where %s
64 * was used before, and ~S where %S was used before.
66 * During the period when there still are a lot of old Guiles out there,
67 * you might want to support both old and new versions of Guile.
69 * There are basically two methods to achieve this. Both methods use
72 * AC_CHECK_FUNCS(scm_simple_format)
74 * in your configure.in.
76 * Method 1: Use the string concatenation features of ANSI C's
81 * #ifdef HAVE_SCM_SIMPLE_FORMAT
87 * Then represent each of your error messages using a preprocessor macro:
89 * #define E_SPIDER_ERROR "There's a spider in your " ## FMT_S ## "!!!"
93 * (define fmt-s (if (defined? 'simple-format) "~S" "%S"))
94 * (define make-message string-append)
96 * (define e-spider-error
97 * (make-message "There's a spider in your " fmt-s "!!!"))
99 * Method 2: Use the oldfmt function found in doc/oldfmt.c.
103 * scm_misc_error ("picnic", scm_c_oldfmt0 ("There's a spider in your ~S!!!"),
108 * (scm-error 'misc-error "picnic" (oldfmt "There's a spider in your ~S!!!")
114 * Take a format string FROM adhering to the new standard format (~A and ~S
115 * as placeholders) of length N and return a string which is adapted
116 * to the format used by the Guile interpreter which you are running.
118 * On successive calls with similar strings but different storage, the
119 * same string with same storage is returned. This is necessary since
120 * the existence of a garbage collector in the system may cause the same
121 * format string to be represented with different storage at different
126 scm_c_oldfmt (char *from
, int n
)
128 #ifdef HAVE_SCM_SIMPLE_FORMAT
131 static struct { int n
; char *from
; char *to
; } *strings
;
133 static int n_strings
= 0;
137 for (i
= 0; i
< n_strings
; ++i
)
138 if (n
== strings
[i
].n
&& strncmp (from
, strings
[i
].from
, n
) == 0)
139 return strings
[i
].to
;
141 if (n_strings
== size
)
146 strings
= scm_must_malloc (size
* sizeof (*strings
), s_oldfmt
);
151 size
= 3 * oldsize
/ 2;
152 strings
= scm_must_realloc (strings
,
153 oldsize
* sizeof (*strings
),
154 size
* sizeof (*strings
),
159 strings
[n_strings
].n
= n
;
160 strings
[n_strings
].from
= strncpy (scm_must_malloc (n
, s_oldfmt
), from
, n
);
161 to
= strings
[n_strings
].to
= scm_must_malloc (n
+ 1, s_oldfmt
);
164 for (i
= 0; i
< n
; ++i
)
166 if (from
[i
] == '~' && ++i
< n
)
173 else if (from
[i
] == 'S')
194 scm_c_oldfmt0 (char *s
)
196 #ifdef HAVE_SCM_SIMPLE_FORMAT
199 return scm_c_oldfmt (s
, strlen (s
));
203 SCM_PROC (s_oldfmt
, "oldfmt", 1, 0, 0, scm_oldfmt
);
208 #ifdef HAVE_SCM_SIMPLE_FORMAT
212 SCM_ASSERT (SCM_NIMP (s
) && SCM_STRINGP (s
), s
, 1, s_oldfmt
);
214 return scm_return_first (scm_mem2string (scm_c_oldfmt (SCM_ROCHARS (s
), n
),