1 /* printf - format and print data
2 Copyright (C) 1990-2002, 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 Foundation,
16 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, 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 \xhh = hexadecimal number (hhh is 1 to 2 digits)
36 \uhhhh = 16-bit Unicode character (hhhh is 4 digits)
37 \Uhhhhhhhh = 32-bit Unicode character (hhhhhhhh is 8 digits)
41 %b = print an argument string, interpreting backslash escapes
43 The `format' argument is re-used as many times as necessary
44 to convert all of the given arguments.
46 David MacKenzie <djm@gnu.ai.mit.edu> */
50 #include <sys/types.h>
54 #include "long-options.h"
57 #include "unicodeio.h"
59 /* The official name of this program (e.g., no `g' prefix). */
60 #define PROGRAM_NAME "printf"
62 #define AUTHORS "David MacKenzie"
67 unsigned long int strtoul ();
70 #define isodigit(c) ((c) >= '0' && (c) <= '7')
71 #define hextobin(c) ((c) >= 'a' && (c) <= 'f' ? (c) - 'a' + 10 : \
72 (c) >= 'A' && (c) <= 'F' ? (c) - 'A' + 10 : (c) - '0')
73 #define octtobin(c) ((c) - '0')
75 /* The value to return to the calling program. */
76 static int exit_status
;
78 /* Non-zero if the POSIXLY_CORRECT environment variable is set. */
79 static int posixly_correct
;
81 /* This message appears in N_() here rather than just in _() below because
82 the sole use would have been in a #define. */
83 static char *const cfcc_msg
=
84 N_("warning: %s: character(s) following character constant have been ignored");
86 /* The name this program was run with. */
93 fprintf (stderr
, _("Try `%s --help' for more information.\n"),
98 Usage: %s FORMAT [ARGUMENT]...\n\
101 program_name
, program_name
);
103 Print ARGUMENT(s) according to FORMAT.\n\
106 fputs (HELP_OPTION_DESCRIPTION
, stdout
);
107 fputs (VERSION_OPTION_DESCRIPTION
, stdout
);
110 FORMAT controls the output as in C printf. Interpreted sequences are:\n\
113 \\0NNN character with octal value NNN (0 to 3 digits)\n\
119 \\c produce no further output\n\
124 \\r carriage return\n\
125 \\t horizontal tab\n\
129 \\xNN byte with hexadecimal value NN (1 to 2 digits)\n\
131 \\uNNNN character with hexadecimal value NNNN (4 digits)\n\
132 \\UNNNNNNNN character with hexadecimal value NNNNNNNN (8 digits)\n\
136 %b ARGUMENT as a string with `\\' escapes interpreted\n\
138 and all C format specifications ending with one of diouxXfeEgGcs, with\n\
139 ARGUMENTs converted to proper type first. Variable widths are handled.\n\
141 printf (_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT
);
147 verify (const char *s
, const char *end
)
151 error (0, errno
, "%s", s
);
157 error (0, 0, _("%s: expected a numeric value"), s
);
159 error (0, 0, _("%s: value not completely converted"), s
);
164 #define STRTOX(TYPE, FUNC_NAME, LIB_FUNC_EXPR) \
172 if (*s == '\"' || *s == '\'') \
174 val = *(unsigned char *) ++s; \
175 /* If POSIXLY_CORRECT is not set, then give a warning that there \
176 are characters following the character constant and that GNU \
177 printf is ignoring those characters. If POSIXLY_CORRECT *is* \
178 set, then don't give the warning. */ \
179 if (*++s != 0 && !posixly_correct) \
180 error (0, 0, _(cfcc_msg), s); \
185 val = LIB_FUNC_EXPR; \
191 STRTOX (unsigned long int, xstrtoul, (strtoul (s, &end, 0)))
192 STRTOX (long int, xstrtol
, (strtol (s
, &end
, 0)))
193 STRTOX (double, xstrtod
, (strtod (s
, &end
)))
195 /* Output a single-character \ escape. */
198 print_esc_char (int c
)
202 case 'a': /* Alert. */
205 case 'b': /* Backspace. */
208 case 'c': /* Cancel the rest of the output. */
211 case 'f': /* Form feed. */
214 case 'n': /* New line. */
217 case 'r': /* Carriage return. */
220 case 't': /* Horizontal tab. */
223 case 'v': /* Vertical tab. */
232 /* Print a \ escape sequence starting at ESCSTART.
233 Return the number of characters in the escape sequence
234 besides the backslash. */
237 print_esc (const char *escstart
)
239 register const char *p
= escstart
+ 1;
240 int esc_value
= 0; /* Value of \nnn escape. */
241 int esc_length
; /* Length of \nnn escape. */
245 /* A hexadecimal \xhh escape sequence must have 1 or 2 hex. digits. */
246 for (esc_length
= 0, ++p
;
247 esc_length
< 2 && ISXDIGIT (*p
);
249 esc_value
= esc_value
* 16 + hextobin (*p
);
251 error (EXIT_FAILURE
, 0, _("missing hexadecimal number in escape"));
256 /* An octal \0ooo escape sequence has 0 to 3 octal digits
257 after the leading \0. */
258 for (esc_length
= 0, ++p
;
259 esc_length
< 3 && isodigit (*p
);
261 esc_value
= esc_value
* 8 + octtobin (*p
);
264 else if (strchr ("\"\\abcfnrtv", *p
))
265 print_esc_char (*p
++);
266 else if (!posixly_correct
&& (*p
== 'u' || *p
== 'U'))
269 unsigned int uni_value
;
272 for (esc_length
= (esc_char
== 'u' ? 4 : 8), ++p
;
277 error (EXIT_FAILURE
, 0, _("missing hexadecimal number in escape"));
278 uni_value
= uni_value
* 16 + hextobin (*p
);
281 /* A universal character name shall not specify a character short
282 identifier in the range 00000000 through 00000020, 0000007F through
283 0000009F, or 0000D800 through 0000DFFF inclusive. A universal
284 character name shall not designate a character in the required
286 if ((uni_value
>= 0x00 && uni_value
<= 0x9f
287 && uni_value
!= 0x24 && uni_value
!= 0x40 && uni_value
!= 0x60)
288 || (uni_value
>= 0xd800 && uni_value
<= 0xdfff))
289 error (EXIT_FAILURE
, 0, _("invalid universal character name \\%c%0*x"),
290 esc_char
, (esc_char
== 'u' ? 4 : 8), uni_value
);
292 print_unicode_char (stdout
, uni_value
, 0);
295 error (EXIT_FAILURE
, 0, _("\\%c: invalid escape"), *p
);
296 return p
- escstart
- 1;
299 /* Print string STR, evaluating \ escapes. */
302 print_esc_string (const char *str
)
306 str
+= print_esc (str
);
311 /* Output a % directive. START is the start of the directive,
312 LENGTH is its length, and ARGUMENT is its argument.
313 If FIELD_WIDTH or PRECISION is non-negative, they are args for
314 '*' values in those fields. */
317 print_direc (const char *start
, size_t length
, int field_width
,
318 int precision
, const char *argument
)
320 char *p
; /* Null-terminated copy of % directive. */
322 p
= xmalloc ((unsigned) (length
+ 1));
323 strncpy (p
, start
, length
);
326 switch (p
[length
- 1])
333 printf (p
, xstrtol (argument
));
335 printf (p
, precision
, xstrtol (argument
));
340 printf (p
, field_width
, xstrtol (argument
));
342 printf (p
, field_width
, precision
, xstrtol (argument
));
353 printf (p
, xstrtoul (argument
));
355 printf (p
, precision
, xstrtoul (argument
));
360 printf (p
, field_width
, xstrtoul (argument
));
362 printf (p
, field_width
, precision
, xstrtoul (argument
));
374 printf (p
, xstrtod (argument
));
376 printf (p
, precision
, xstrtod (argument
));
381 printf (p
, field_width
, xstrtod (argument
));
383 printf (p
, field_width
, precision
, xstrtod (argument
));
388 printf (p
, *argument
);
395 printf (p
, argument
);
397 printf (p
, precision
, argument
);
402 printf (p
, field_width
, argument
);
404 printf (p
, field_width
, precision
, argument
);
412 /* Print the text in FORMAT, using ARGV (with ARGC elements) for
413 arguments to any `%' directives.
414 Return the number of elements of ARGV used. */
417 print_formatted (const char *format
, int argc
, char **argv
)
419 int save_argc
= argc
; /* Preserve original value. */
420 const char *f
; /* Pointer into `format'. */
421 const char *direc_start
; /* Start of % directive. */
422 size_t direc_length
; /* Length of % directive. */
423 int field_width
; /* Arg to first '*', or -1 if none. */
424 int precision
; /* Arg to second '*', or -1 if none. */
426 for (f
= format
; *f
; ++f
)
433 field_width
= precision
= -1;
443 print_esc_string (*argv
);
449 if (strchr ("-+ #", *f
))
460 field_width
= xstrtoul (*argv
);
483 precision
= xstrtoul (*argv
);
497 if (*f
== 'l' || *f
== 'L' || *f
== 'h')
502 if (!strchr ("diouxXfeEgGcs", *f
))
503 error (EXIT_FAILURE
, 0, _("%%%c: invalid directive"), *f
);
507 print_direc (direc_start
, direc_length
, field_width
,
513 print_direc (direc_start
, direc_length
, field_width
,
526 return save_argc
- argc
;
530 main (int argc
, char **argv
)
535 program_name
= argv
[0];
536 setlocale (LC_ALL
, "");
537 bindtextdomain (PACKAGE
, LOCALEDIR
);
538 textdomain (PACKAGE
);
540 atexit (close_stdout
);
544 /* Don't recognize --help or --version if POSIXLY_CORRECT is set. */
545 posixly_correct
= (getenv ("POSIXLY_CORRECT") != NULL
);
546 if (!posixly_correct
)
547 parse_long_options (argc
, argv
, PROGRAM_NAME
, GNU_PACKAGE
, VERSION
,
550 /* The above handles --help and --version.
551 Since there is no other invocation of getopt, handle `--' here. */
552 if (1 < argc
&& STREQ (argv
[1], "--"))
560 fprintf (stderr
, _("Usage: %s format [argument...]\n"), program_name
);
570 args_used
= print_formatted (format
, argc
, argv
);
574 while (args_used
> 0 && argc
> 0);
578 _("warning: ignoring excess arguments, starting with `%s'"),