blob: 6a87fc319902afbddfaf16118c6a6235e77bb076 [file] [log] [blame]
Erik Andersene49d5ec2000-02-08 19:58:47 +00001/* vi: set sw=4 ts=4: */
Eric Andersencc8ed391999-10-05 16:24:54 +00002/* printf - format and print data
3 Copyright (C) 90, 91, 92, 93, 94, 95, 1996 Free Software Foundation, Inc.
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2, or (at your option)
8 any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software Foundation,
17 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
18
19/* Usage: printf format [argument...]
20
21 A front end to the printf function that lets it be used from the shell.
22
23 Backslash escapes:
24
25 \" = double quote
26 \\ = backslash
27 \a = alert (bell)
28 \b = backspace
29 \c = produce no further output
30 \f = form feed
31 \n = new line
32 \r = carriage return
33 \t = horizontal tab
34 \v = vertical tab
35 \0ooo = octal number (ooo is 0 to 3 digits)
36 \xhhh = hexadecimal number (hhh is 1 to 3 digits)
37
38 Additional directive:
39
40 %b = print an argument string, interpreting backslash escapes
41
42 The `format' argument is re-used as many times as necessary
43 to convert all of the given arguments.
44
45 David MacKenzie <djm@gnu.ai.mit.edu> */
Erik Andersene49d5ec2000-02-08 19:58:47 +000046
Eric Andersencc8ed391999-10-05 16:24:54 +000047
48// 19990508 Busy Boxed! Dave Cinege
49
Eric Andersenb0e9a701999-10-18 22:28:26 +000050#include "internal.h"
Eric Andersencc8ed391999-10-05 16:24:54 +000051#include <unistd.h>
52#include <stdio.h>
53#include <sys/types.h>
Eric Andersencc8ed391999-10-05 16:24:54 +000054#include <sys/stat.h>
55#include <string.h>
56#include <errno.h>
57#include <stdlib.h>
58#include <fcntl.h>
59#include <ctype.h>
Eric Andersencc8ed391999-10-05 16:24:54 +000060
61
62#ifndef S_IFMT
63# define S_IFMT 0170000
64#endif
65#if !defined(S_ISBLK) && defined(S_IFBLK)
66# define S_ISBLK(m) (((m) & S_IFMT) == S_IFBLK)
67#endif
68#if !defined(S_ISCHR) && defined(S_IFCHR)
69# define S_ISCHR(m) (((m) & S_IFMT) == S_IFCHR)
70#endif
71#if !defined(S_ISDIR) && defined(S_IFDIR)
72# define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
73#endif
74#if !defined(S_ISREG) && defined(S_IFREG)
75# define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
76#endif
77#if !defined(S_ISFIFO) && defined(S_IFIFO)
78# define S_ISFIFO(m) (((m) & S_IFMT) == S_IFIFO)
79#endif
80#if !defined(S_ISLNK) && defined(S_IFLNK)
81# define S_ISLNK(m) (((m) & S_IFMT) == S_IFLNK)
82#endif
83#if !defined(S_ISSOCK) && defined(S_IFSOCK)
84# define S_ISSOCK(m) (((m) & S_IFMT) == S_IFSOCK)
85#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +000086#if !defined(S_ISMPB) && defined(S_IFMPB) /* V7 */
Eric Andersencc8ed391999-10-05 16:24:54 +000087# define S_ISMPB(m) (((m) & S_IFMT) == S_IFMPB)
88# define S_ISMPC(m) (((m) & S_IFMT) == S_IFMPC)
89#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +000090#if !defined(S_ISNWK) && defined(S_IFNWK) /* HP/UX */
Eric Andersencc8ed391999-10-05 16:24:54 +000091# define S_ISNWK(m) (((m) & S_IFMT) == S_IFNWK)
92#endif
93
94#define IN_CTYPE_DOMAIN(c) 1
95
96#ifdef isblank
97# define ISBLANK(c) (IN_CTYPE_DOMAIN (c) && isblank (c))
98#else
99# define ISBLANK(c) ((c) == ' ' || (c) == '\t')
100#endif
101#ifdef isgraph
102# define ISGRAPH(c) (IN_CTYPE_DOMAIN (c) && isgraph (c))
103#else
104# define ISGRAPH(c) (IN_CTYPE_DOMAIN (c) && isprint (c) && !isspace (c))
105#endif
106
107#define ISPRINT(c) (IN_CTYPE_DOMAIN (c) && isprint (c))
108#define ISALNUM(c) (IN_CTYPE_DOMAIN (c) && isalnum (c))
109#define ISALPHA(c) (IN_CTYPE_DOMAIN (c) && isalpha (c))
110#define ISCNTRL(c) (IN_CTYPE_DOMAIN (c) && iscntrl (c))
111#define ISLOWER(c) (IN_CTYPE_DOMAIN (c) && islower (c))
112#define ISPUNCT(c) (IN_CTYPE_DOMAIN (c) && ispunct (c))
113#define ISSPACE(c) (IN_CTYPE_DOMAIN (c) && isspace (c))
114#define ISUPPER(c) (IN_CTYPE_DOMAIN (c) && isupper (c))
115#define ISXDIGIT(c) (IN_CTYPE_DOMAIN (c) && isxdigit (c))
116#define ISDIGIT_LOCALE(c) (IN_CTYPE_DOMAIN (c) && isdigit (c))
117#define ISDIGIT(c) ((unsigned) (c) - '0' <= 9)
118
119#define isodigit(c) ((c) >= '0' && (c) <= '7')
120#define hextobin(c) ((c)>='a'&&(c)<='f' ? (c)-'a'+10 : (c)>='A'&&(c)<='F' ? (c)-'A'+10 : (c)-'0')
121#define octtobin(c) ((c) - '0')
122
Erik Andersene49d5ec2000-02-08 19:58:47 +0000123static double xstrtod __P((char *s));
124static int print_esc __P((char *escstart));
125static int print_formatted __P((char *format, int argc, char **argv));
126static long xstrtol __P((char *s));
127static unsigned long xstrtoul __P((char *s));
128static void print_direc
129__P(
130
131 (char *start, size_t length, int field_width, int precision,
132 char *argument));
133static void print_esc_char __P((int c));
134static void print_esc_string __P((char *str));
135static void verify __P((char *s, char *end));
Eric Andersencc8ed391999-10-05 16:24:54 +0000136
137/* The value to return to the calling program. */
138static int exit_status;
139
Erik Andersene49d5ec2000-02-08 19:58:47 +0000140int printf_main(int argc, char **argv)
Eric Andersencc8ed391999-10-05 16:24:54 +0000141{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000142 char *format;
143 int args_used;
Eric Andersencc8ed391999-10-05 16:24:54 +0000144
Erik Andersene49d5ec2000-02-08 19:58:47 +0000145 exit_status = 0;
146 if (argc <= 1 || **(argv + 1) == '-') {
147 usage(printf_usage);
148 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000149
Erik Andersene49d5ec2000-02-08 19:58:47 +0000150 format = argv[1];
151 argc -= 2;
152 argv += 2;
Eric Andersencc8ed391999-10-05 16:24:54 +0000153
Erik Andersene49d5ec2000-02-08 19:58:47 +0000154 do {
155 args_used = print_formatted(format, argc, argv);
156 argc -= args_used;
157 argv += args_used;
158 }
159 while (args_used > 0 && argc > 0);
Eric Andersencc8ed391999-10-05 16:24:54 +0000160
161/*
162 if (argc > 0)
163 fprintf(stderr, "excess args ignored");
164*/
165
Eric Andersenb7362e32000-06-19 17:52:17 +0000166 return(exit_status);
Eric Andersencc8ed391999-10-05 16:24:54 +0000167}
168
169/* Print the text in FORMAT, using ARGV (with ARGC elements) for
170 arguments to any `%' directives.
171 Return the number of elements of ARGV used. */
172
Erik Andersene49d5ec2000-02-08 19:58:47 +0000173static int print_formatted(char *format, int argc, char **argv)
Eric Andersencc8ed391999-10-05 16:24:54 +0000174{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000175 int save_argc = argc; /* Preserve original value. */
176 char *f; /* Pointer into `format'. */
177 char *direc_start; /* Start of % directive. */
178 size_t direc_length; /* Length of % directive. */
179 int field_width; /* Arg to first '*', or -1 if none. */
180 int precision; /* Arg to second '*', or -1 if none. */
Eric Andersencc8ed391999-10-05 16:24:54 +0000181
Erik Andersene49d5ec2000-02-08 19:58:47 +0000182 for (f = format; *f; ++f) {
183 switch (*f) {
184 case '%':
185 direc_start = f++;
186 direc_length = 1;
187 field_width = precision = -1;
188 if (*f == '%') {
189 putchar('%');
190 break;
191 }
192 if (*f == 'b') {
193 if (argc > 0) {
194 print_esc_string(*argv);
195 ++argv;
196 --argc;
197 }
198 break;
199 }
200 if (strchr("-+ #", *f)) {
201 ++f;
202 ++direc_length;
203 }
204 if (*f == '*') {
205 ++f;
206 ++direc_length;
207 if (argc > 0) {
208 field_width = xstrtoul(*argv);
209 ++argv;
210 --argc;
211 } else
212 field_width = 0;
213 } else
214 while (ISDIGIT(*f)) {
215 ++f;
216 ++direc_length;
217 }
218 if (*f == '.') {
219 ++f;
220 ++direc_length;
221 if (*f == '*') {
222 ++f;
223 ++direc_length;
224 if (argc > 0) {
225 precision = xstrtoul(*argv);
226 ++argv;
227 --argc;
228 } else
229 precision = 0;
230 } else
231 while (ISDIGIT(*f)) {
232 ++f;
233 ++direc_length;
234 }
235 }
236 if (*f == 'l' || *f == 'L' || *f == 'h') {
237 ++f;
238 ++direc_length;
239 }
240 /*
241 if (!strchr ("diouxXfeEgGcs", *f))
242 fprintf(stderr, "%%%c: invalid directive", *f);
243 */
244 ++direc_length;
245 if (argc > 0) {
246 print_direc(direc_start, direc_length, field_width,
247 precision, *argv);
248 ++argv;
249 --argc;
250 } else
251 print_direc(direc_start, direc_length, field_width,
252 precision, "");
253 break;
Eric Andersencc8ed391999-10-05 16:24:54 +0000254
Erik Andersene49d5ec2000-02-08 19:58:47 +0000255 case '\\':
256 f += print_esc(f);
257 break;
Eric Andersencc8ed391999-10-05 16:24:54 +0000258
Erik Andersene49d5ec2000-02-08 19:58:47 +0000259 default:
260 putchar(*f);
261 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000262 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000263
Erik Andersene49d5ec2000-02-08 19:58:47 +0000264 return save_argc - argc;
Eric Andersencc8ed391999-10-05 16:24:54 +0000265}
266
267/* Print a \ escape sequence starting at ESCSTART.
268 Return the number of characters in the escape sequence
269 besides the backslash. */
270
Erik Andersene49d5ec2000-02-08 19:58:47 +0000271static int print_esc(char *escstart)
Eric Andersencc8ed391999-10-05 16:24:54 +0000272{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000273 register char *p = escstart + 1;
274 int esc_value = 0; /* Value of \nnn escape. */
275 int esc_length; /* Length of \nnn escape. */
Eric Andersencc8ed391999-10-05 16:24:54 +0000276
Erik Andersene49d5ec2000-02-08 19:58:47 +0000277 /* \0ooo and \xhhh escapes have maximum length of 3 chars. */
278 if (*p == 'x') {
279 for (esc_length = 0, ++p;
280 esc_length < 3 && ISXDIGIT(*p); ++esc_length, ++p)
281 esc_value = esc_value * 16 + hextobin(*p);
Eric Andersencc8ed391999-10-05 16:24:54 +0000282/* if (esc_length == 0)
283 fprintf(stderr, "missing hex in esc");
284*/
Erik Andersene49d5ec2000-02-08 19:58:47 +0000285 putchar(esc_value);
286 } else if (*p == '0') {
287 for (esc_length = 0, ++p;
288 esc_length < 3 && isodigit(*p); ++esc_length, ++p)
289 esc_value = esc_value * 8 + octtobin(*p);
290 putchar(esc_value);
291 } else if (strchr("\"\\abcfnrtv", *p))
292 print_esc_char(*p++);
Eric Andersencc8ed391999-10-05 16:24:54 +0000293/* else
294 fprintf(stderr, "\\%c: invalid esc", *p);
295*/
Erik Andersene49d5ec2000-02-08 19:58:47 +0000296 return p - escstart - 1;
Eric Andersencc8ed391999-10-05 16:24:54 +0000297}
298
299/* Output a single-character \ escape. */
300
Erik Andersene49d5ec2000-02-08 19:58:47 +0000301static void print_esc_char(int c)
Eric Andersencc8ed391999-10-05 16:24:54 +0000302{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000303 switch (c) {
304 case 'a': /* Alert. */
305 putchar(7);
306 break;
307 case 'b': /* Backspace. */
308 putchar(8);
309 break;
310 case 'c': /* Cancel the rest of the output. */
311 exit(0);
312 break;
313 case 'f': /* Form feed. */
314 putchar(12);
315 break;
316 case 'n': /* New line. */
317 putchar(10);
318 break;
319 case 'r': /* Carriage return. */
320 putchar(13);
321 break;
322 case 't': /* Horizontal tab. */
323 putchar(9);
324 break;
325 case 'v': /* Vertical tab. */
326 putchar(11);
327 break;
328 default:
329 putchar(c);
330 break;
331 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000332}
333
334/* Print string STR, evaluating \ escapes. */
335
Erik Andersene49d5ec2000-02-08 19:58:47 +0000336static void print_esc_string(char *str)
Eric Andersencc8ed391999-10-05 16:24:54 +0000337{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000338 for (; *str; str++)
339 if (*str == '\\')
340 str += print_esc(str);
341 else
342 putchar(*str);
Eric Andersencc8ed391999-10-05 16:24:54 +0000343}
344
345static void
Erik Andersene49d5ec2000-02-08 19:58:47 +0000346print_direc(char *start, size_t length, int field_width, int precision,
347 char *argument)
Eric Andersencc8ed391999-10-05 16:24:54 +0000348{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000349 char *p; /* Null-terminated copy of % directive. */
Eric Andersencc8ed391999-10-05 16:24:54 +0000350
Erik Andersene49d5ec2000-02-08 19:58:47 +0000351 p = xmalloc((unsigned) (length + 1));
352 strncpy(p, start, length);
353 p[length] = 0;
Eric Andersencc8ed391999-10-05 16:24:54 +0000354
Erik Andersene49d5ec2000-02-08 19:58:47 +0000355 switch (p[length - 1]) {
356 case 'd':
357 case 'i':
358 if (field_width < 0) {
359 if (precision < 0)
360 printf(p, xstrtol(argument));
361 else
362 printf(p, precision, xstrtol(argument));
363 } else {
364 if (precision < 0)
365 printf(p, field_width, xstrtol(argument));
366 else
367 printf(p, field_width, precision, xstrtol(argument));
368 }
369 break;
Eric Andersencc8ed391999-10-05 16:24:54 +0000370
Erik Andersene49d5ec2000-02-08 19:58:47 +0000371 case 'o':
372 case 'u':
373 case 'x':
374 case 'X':
375 if (field_width < 0) {
376 if (precision < 0)
377 printf(p, xstrtoul(argument));
378 else
379 printf(p, precision, xstrtoul(argument));
380 } else {
381 if (precision < 0)
382 printf(p, field_width, xstrtoul(argument));
383 else
384 printf(p, field_width, precision, xstrtoul(argument));
385 }
386 break;
Eric Andersencc8ed391999-10-05 16:24:54 +0000387
Erik Andersene49d5ec2000-02-08 19:58:47 +0000388 case 'f':
389 case 'e':
390 case 'E':
391 case 'g':
392 case 'G':
393 if (field_width < 0) {
394 if (precision < 0)
395 printf(p, xstrtod(argument));
396 else
397 printf(p, precision, xstrtod(argument));
398 } else {
399 if (precision < 0)
400 printf(p, field_width, xstrtod(argument));
401 else
402 printf(p, field_width, precision, xstrtod(argument));
403 }
404 break;
Eric Andersencc8ed391999-10-05 16:24:54 +0000405
Erik Andersene49d5ec2000-02-08 19:58:47 +0000406 case 'c':
407 printf(p, *argument);
408 break;
Eric Andersencc8ed391999-10-05 16:24:54 +0000409
Erik Andersene49d5ec2000-02-08 19:58:47 +0000410 case 's':
411 if (field_width < 0) {
412 if (precision < 0)
413 printf(p, argument);
414 else
415 printf(p, precision, argument);
416 } else {
417 if (precision < 0)
418 printf(p, field_width, argument);
419 else
420 printf(p, field_width, precision, argument);
421 }
422 break;
Eric Andersencc8ed391999-10-05 16:24:54 +0000423 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000424
Erik Andersene49d5ec2000-02-08 19:58:47 +0000425 free(p);
Eric Andersencc8ed391999-10-05 16:24:54 +0000426}
427
Erik Andersene49d5ec2000-02-08 19:58:47 +0000428static unsigned long xstrtoul(char *s)
Eric Andersencc8ed391999-10-05 16:24:54 +0000429{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000430 char *end;
431 unsigned long val;
Eric Andersencc8ed391999-10-05 16:24:54 +0000432
Erik Andersene49d5ec2000-02-08 19:58:47 +0000433 errno = 0;
434 val = strtoul(s, &end, 0);
435 verify(s, end);
436 return val;
Eric Andersencc8ed391999-10-05 16:24:54 +0000437}
438
Erik Andersene49d5ec2000-02-08 19:58:47 +0000439static long xstrtol(char *s)
Eric Andersencc8ed391999-10-05 16:24:54 +0000440{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000441 char *end;
442 long val;
Eric Andersencc8ed391999-10-05 16:24:54 +0000443
Erik Andersene49d5ec2000-02-08 19:58:47 +0000444 errno = 0;
445 val = strtol(s, &end, 0);
446 verify(s, end);
447 return val;
Eric Andersencc8ed391999-10-05 16:24:54 +0000448}
449
Erik Andersene49d5ec2000-02-08 19:58:47 +0000450static double xstrtod(char *s)
Eric Andersencc8ed391999-10-05 16:24:54 +0000451{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000452 char *end;
453 double val;
Eric Andersencc8ed391999-10-05 16:24:54 +0000454
Erik Andersene49d5ec2000-02-08 19:58:47 +0000455 errno = 0;
456 val = strtod(s, &end);
457 verify(s, end);
458 return val;
Eric Andersencc8ed391999-10-05 16:24:54 +0000459}
460
Erik Andersene49d5ec2000-02-08 19:58:47 +0000461static void verify(char *s, char *end)
Eric Andersencc8ed391999-10-05 16:24:54 +0000462{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000463 if (errno) {
464 fprintf(stderr, "%s", s);
465 exit_status = 1;
466 } else if (*end) {
467 /*
468 if (s == end)
469 fprintf(stderr, "%s: expected numeric", s);
470 else
471 fprintf(stderr, "%s: not completely converted", s);
472 */
473 exit_status = 1;
474 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000475}