1 /* printf - format and print data
2 Copyright (C) 1990, 1991 Free Software Foundation, Inc.
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2, or (at your option)
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
18 /* Usage: printf format [argument...]
20 A front end to the printf function that lets it be used from the shell.
28 \c = produce no further output
34 \0ooo = octal number (ooo is 0 to 3 digits)
35 \xhhh = hexadecimal number (hhh is 1 to 3 digits)
39 %b = print an argument string, interpreting backslash escapes
41 The `format' argument is re-used as many times as necessary
42 to convert all of the given arguments.
44 David MacKenzie <djm@gnu.ai.mit.edu> */
47 #if defined (CONFIG_BROKETS)
48 /* We use <config.h> instead of "config.h" so that a compilation
49 using -I. -I$srcdir will use ./config.h rather than $srcdir/config.h
50 (which it would do because it found this file in $srcdir). */
59 #include <sys/types.h>
64 #include "long-option.h"
66 #if !defined (isascii) || defined (STDC_HEADERS)
71 #define ISDIGIT(c) (isascii (c) && isdigit (c))
72 #define ISXDIGIT(c) (isascii (c) && isxdigit (c))
77 unsigned long strtoul ();
80 #define isodigit(c) ((c) >= '0' && (c) <= '7')
81 #define hextobin(c) ((c)>='a'&&(c)<='f' ? (c)-'a'+10 : (c)>='A'&&(c)<='F' ? (c)-'A'+10 : (c)-'0')
82 #define octtobin(c) ((c) - '0')
87 static double xstrtod ();
88 static int print_esc ();
89 static int print_formatted ();
90 static long xstrtol ();
91 static unsigned long xstrtoul ();
92 static void print_direc ();
93 static void print_esc_char ();
94 static void print_esc_string ();
95 static void verify ();
97 /* The value to return to the calling program. */
98 static int exit_status;
100 /* The name this program was run with. */
107 fprintf (status == 0 ? stdout : stderr, "\
108 Usage: %s FORMAT [ARGUMENT]...\n\
111 program_name, program_name);
114 fprintf (stderr, "Try `%s --help' for more information.\n",
120 --help display this help and exit\n\
121 --version output version information and exit\n\
123 FORMAT controls the output as in C printf. Interpreted sequences are:\n\
126 \\0NNN character with octal value NNN (0 to 3 digits)\n\
130 \\c produce no further output\n\
133 \\r carriage return\n\
134 \\t horizontal tab\n\
136 \\xNNN character with hexadecimal value NNN (1 to 3 digits)\n\
139 %%b ARGUMENT as a string with `\\' escapes interpreted\n\
141 and all C format specifications ending with one of diouxXfeEgGcs, with\n\
142 ARGUMENTs converted to proper type first. Variable widths are handled.\n\
156 program_name = argv[0];
159 parse_long_options (argc, argv, usage);
163 fprintf (stderr, "Usage: %s format [argument...]\n", program_name);
173 args_used = print_formatted (format, argc, argv);
177 while (args_used > 0 && argc > 0);
182 /* Print the text in FORMAT, using ARGV (with ARGC elements) for
183 arguments to any `%' directives.
184 Return the number of elements of ARGV used. */
187 print_formatted (format, argc, argv)
192 int save_argc = argc; /* Preserve original value. */
193 char *f; /* Pointer into `format'. */
194 char *direc_start; /* Start of % directive. */
195 int direc_length; /* Length of % directive. */
196 int field_width; /* Arg to first '*', or -1 if none. */
197 int precision; /* Arg to second '*', or -1 if none. */
199 for (f = format; *f; ++f)
206 field_width = precision = -1;
216 print_esc_string (*argv);
222 if (index ("-+ #", *f))
233 field_width = xstrtoul (*argv);
256 precision = xstrtoul (*argv);
270 if (*f == 'l' || *f == 'L' || *f == 'h')
275 if (!index ("diouxXfeEgGcs", *f))
276 error (1, 0, "%%%c: invalid directive", *f);
280 print_direc (direc_start, direc_length, field_width,
286 print_direc (direc_start, direc_length, field_width,
299 return save_argc - argc;
302 /* Print a \ escape sequence starting at ESCSTART.
303 Return the number of characters in the escape sequence
304 besides the backslash. */
310 register char *p = escstart + 1;
311 int esc_value = 0; /* Value of \nnn escape. */
312 int esc_length; /* Length of \nnn escape. */
314 /* \0ooo and \xhhh escapes have maximum length of 3 chars. */
317 for (esc_length = 0, ++p;
318 esc_length < 3 && ISXDIGIT (*p);
320 esc_value = esc_value * 16 + hextobin (*p);
322 error (1, 0, "missing hexadecimal number in escape");
327 for (esc_length = 0, ++p;
328 esc_length < 3 && isodigit (*p);
330 esc_value = esc_value * 8 + octtobin (*p);
333 else if (index ("\"\\abcfnrtv", *p))
334 print_esc_char (*p++);
336 error (1, 0, "\\%c: invalid escape", *p);
337 return p - escstart - 1;
340 /* Output a single-character \ escape. */
348 case 'a': /* Alert. */
351 case 'b': /* Backspace. */
354 case 'c': /* Cancel the rest of the output. */
357 case 'f': /* Form feed. */
360 case 'n': /* New line. */
363 case 'r': /* Carriage return. */
366 case 't': /* Horizontal tab. */
369 case 'v': /* Vertical tab. */
378 /* Print string STR, evaluating \ escapes. */
381 print_esc_string (str)
386 str += print_esc (str);
391 /* Output a % directive. START is the start of the directive,
392 LENGTH is its length, and ARGUMENT is its argument.
393 If FIELD_WIDTH or PRECISION is non-negative, they are args for
394 '*' values in those fields. */
397 print_direc (start, length, field_width, precision, argument)
404 char *p; /* Null-terminated copy of % directive. */
406 p = xmalloc ((unsigned) (length + 1));
407 strncpy (p, start, length);
410 switch (p[length - 1])
417 printf (p, xstrtol (argument));
419 printf (p, precision, xstrtol (argument));
424 printf (p, field_width, xstrtol (argument));
426 printf (p, field_width, precision, xstrtol (argument));
437 printf (p, xstrtoul (argument));
439 printf (p, precision, xstrtoul (argument));
444 printf (p, field_width, xstrtoul (argument));
446 printf (p, field_width, precision, xstrtoul (argument));
458 printf (p, xstrtod (argument));
460 printf (p, precision, xstrtod (argument));
465 printf (p, field_width, xstrtod (argument));
467 printf (p, field_width, precision, xstrtod (argument));
472 printf (p, *argument);
479 printf (p, argument);
481 printf (p, precision, argument);
486 printf (p, field_width, argument);
488 printf (p, field_width, precision, argument);
504 val = strtoul (s, &end, 0);
517 val = strtol (s, &end, 0);
530 val = strtod (s, &end);
541 error (0, errno, "%s", s);
547 error (0, 0, "%s: expected a numeric value", s);
549 error (0, 0, "%s: value not completely converted", s);