(Fcompute_motion): Typecheck all arguments.
[emacs.git] / src / doprnt.c
blob303846124f4c6cbd1cd1f6bddb0d0dd4b65cbaae
1 /* Output like sprintf to a buffer of specified size.
2 Also takes args differently: pass one pointer to an array of strings
3 in addition to the format string which is separate.
4 Copyright (C) 1985 Free Software Foundation, Inc.
6 This file is part of GNU Emacs.
8 GNU Emacs is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 1, or (at your option)
11 any later version.
13 GNU Emacs is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GNU Emacs; see the file COPYING. If not, write to
20 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
23 #include <config.h>
24 #include <stdio.h>
25 #include <ctype.h>
27 /* Generate output from a format-spec FORMAT,
28 terminated at position FORMAT_END.
29 Output goes in BUFFER, which has room for BUFSIZE chars.
30 If the output does not fit, truncate it to fit.
31 Returns the number of characters stored into BUFFER.
32 ARGS points to the vector of arguments, and NARGS says how many.
33 A double counts as two arguments. */
35 doprnt (buffer, bufsize, format, format_end, nargs, args)
36 char *buffer;
37 register int bufsize;
38 char *format;
39 char *format_end;
40 int nargs;
41 char **args;
43 int cnt = 0; /* Number of arg to gobble next */
44 register char *fmt = format; /* Pointer into format string */
45 register char *bufptr = buffer; /* Pointer into output buffer.. */
47 /* Use this for sprintf unless we need something really big. */
48 char tembuf[100];
50 /* Size of sprintf_buffer. */
51 int size_allocated = 100;
53 /* Buffer to use for sprintf. Either tembuf or same as BIG_BUFFER. */
54 char *sprintf_buffer = tembuf;
56 /* Buffer we have got with malloc. */
57 char *big_buffer = 0;
59 register int tem;
60 char *string;
61 char fixed_buffer[20]; /* Default buffer for small formatting. */
62 char *fmtcpy;
63 int minlen;
64 int size; /* Field width factor; e.g., %90d */
66 if (format_end == 0)
67 format_end = format + strlen (format);
69 if ((format_end - format + 1) < sizeof (fixed_buffer))
70 fmtcpy = fixed_buffer;
71 else
72 fmtcpy = (char *) alloca (format_end - format + 1);
74 bufsize--;
76 /* Loop until end of format string or buffer full. */
77 while (fmt != format_end && bufsize > 0)
79 if (*fmt == '%') /* Check for a '%' character */
81 int size_bound;
83 fmt++;
84 /* Copy this one %-spec into fmtcpy. */
85 string = fmtcpy;
86 *string++ = '%';
87 while (1)
89 *string++ = *fmt;
90 if (! (*fmt >= '0' && *fmt <= '9')
91 && *fmt != '-' && *fmt != ' '&& *fmt != '.')
92 break;
93 fmt++;
95 *string = 0;
96 /* Get an idea of how much space we might need. */
97 size_bound = atoi (&fmtcpy[1]);
99 /* Avoid pitfall of negative "size" parameter ("%-200d"). */
100 if (size_bound < 0)
101 size_bound = -size_bound;
102 size_bound += 50;
104 /* Make sure we have that much. */
105 if (size_bound > size_allocated)
107 if (big_buffer)
108 big_buffer = (char *) xrealloc (big_buffer, size_bound);
109 else
110 big_buffer = (char *) xmalloc (size_bound);
111 sprintf_buffer = big_buffer;
112 size_allocated = size_bound;
114 minlen = 0;
115 switch (*fmt++)
117 default:
118 error ("Invalid format operation %%%c", fmt[-1]);
120 /* case 'b': */
121 case 'd':
122 case 'o':
123 case 'x':
124 if (cnt == nargs)
125 error ("Format string wants too many arguments");
126 sprintf (sprintf_buffer, fmtcpy, args[cnt++]);
127 /* Now copy into final output, truncating as nec. */
128 string = sprintf_buffer;
129 goto doit;
131 case 'f':
132 case 'e':
133 case 'g':
135 union { double d; char *half[2]; } u;
136 if (cnt + 1 == nargs)
137 error ("Format string wants too many arguments");
138 u.half[0] = args[cnt++];
139 u.half[1] = args[cnt++];
140 sprintf (sprintf_buffer, fmtcpy, u.d);
141 /* Now copy into final output, truncating as nec. */
142 string = sprintf_buffer;
143 goto doit;
146 case 'S':
147 string[-1] = 's';
148 case 's':
149 if (cnt == nargs)
150 error ("Format string wants too many arguments");
151 string = args[cnt++];
152 if (fmtcpy[1] != 's')
153 minlen = atoi (&fmtcpy[1]);
154 /* Copy string into final output, truncating if no room. */
155 doit:
156 tem = strlen (string);
157 if (minlen > 0)
159 while (minlen > tem && bufsize > 0)
161 *bufptr++ = ' ';
162 bufsize--;
163 minlen--;
165 minlen = 0;
167 if (tem > bufsize)
168 tem = bufsize;
169 strncpy (bufptr, string, tem);
170 bufptr += tem;
171 bufsize -= tem;
172 if (minlen < 0)
174 while (minlen < - tem && bufsize > 0)
176 *bufptr++ = ' ';
177 bufsize--;
178 minlen++;
180 minlen = 0;
182 continue;
184 case 'c':
185 if (cnt == nargs)
186 error ("Format string wants too many arguments");
187 *bufptr++ = (int) args[cnt++];
188 bufsize--;
189 continue;
191 case '%':
192 fmt--; /* Drop thru and this % will be treated as normal */
195 *bufptr++ = *fmt++; /* Just some characters; Copy 'em */
196 bufsize--;
199 /* If we had to malloc something, free it. */
200 if (big_buffer)
201 xfree (big_buffer);
203 *bufptr = 0; /* Make sure our string end with a '\0' */
204 return bufptr - buffer;