1 /* printf - format and print data
2 Copyright (C) 1990-2000, 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 \xhhh = hexadecimal number (hhh is 1 to 3 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, and xgettext doesn't look for
83 strings in cpp directives. */
84 static char *const cfcc_msg
=
85 N_("warning: %s: character(s) following character constant have been ignored");
87 /* The name this program was run with. */
94 fprintf (stderr
, _("Try `%s --help' for more information.\n"),
99 Usage: %s FORMAT [ARGUMENT]...\n\
102 program_name
, program_name
);
104 Print ARGUMENT(s) according to FORMAT.\n\
106 --help display this help and exit\n\
107 --version output version information and exit\n\
109 FORMAT controls the output as in C printf. Interpreted sequences are:\n\
112 \\0NNN character with octal value NNN (0 to 3 digits)\n\
116 \\c produce no further output\n\
119 \\r carriage return\n\
120 \\t horizontal tab\n\
122 \\xNNN byte with hexadecimal value NNN (1 to 3 digits)\n\
124 \\uNNNN character with hexadecimal value NNNN (4 digits)\n\
125 \\UNNNNNNNN character with hexadecimal value NNNNNNNN (8 digits)\n\
127 %%b ARGUMENT as a string with `\\' escapes interpreted\n\
129 and all C format specifications ending with one of diouxXfeEgGcs, with\n\
130 ARGUMENTs converted to proper type first. Variable widths are handled.\n\
132 puts (_("\nReport bugs to <bug-sh-utils@gnu.org>."));
138 verify (const char *s
, const char *end
)
142 error (0, errno
, "%s", s
);
148 error (0, 0, _("%s: expected a numeric value"), s
);
150 error (0, 0, _("%s: value not completely converted"), s
);
155 #define STRTOX(TYPE, FUNC_NAME, LIB_FUNC_EXPR) \
163 if (*s == '\"' || *s == '\'') \
165 val = *(unsigned char *) ++s; \
166 /* If POSIXLY_CORRECT is not set, then give a warning that there \
167 are characters following the character constant and that GNU \
168 printf is ignoring those characters. If POSIXLY_CORRECT *is* \
169 set, then don't give the warning. */ \
170 if (*++s != 0 && !posixly_correct) \
171 error (0, 0, _(cfcc_msg), s); \
176 val = LIB_FUNC_EXPR; \
182 STRTOX (unsigned long int, xstrtoul, (strtoul (s, &end, 0)))
183 STRTOX (long int, xstrtol
, (strtol (s
, &end
, 0)))
184 STRTOX (double, xstrtod
, (strtod (s
, &end
)))
186 /* Output a single-character \ escape. */
189 print_esc_char (int c
)
193 case 'a': /* Alert. */
196 case 'b': /* Backspace. */
199 case 'c': /* Cancel the rest of the output. */
202 case 'f': /* Form feed. */
205 case 'n': /* New line. */
208 case 'r': /* Carriage return. */
211 case 't': /* Horizontal tab. */
214 case 'v': /* Vertical tab. */
223 /* Print a \ escape sequence starting at ESCSTART.
224 Return the number of characters in the escape sequence
225 besides the backslash. */
228 print_esc (const char *escstart
)
230 register const char *p
= escstart
+ 1;
231 int esc_value
= 0; /* Value of \nnn escape. */
232 int esc_length
; /* Length of \nnn escape. */
234 /* \0ooo and \xhhh escapes have maximum length of 3 chars. */
237 for (esc_length
= 0, ++p
;
238 esc_length
< 3 && ISXDIGIT (*p
);
240 esc_value
= esc_value
* 16 + hextobin (*p
);
242 error (1, 0, _("missing hexadecimal number in escape"));
247 for (esc_length
= 0, ++p
;
248 esc_length
< 3 && isodigit (*p
);
250 esc_value
= esc_value
* 8 + octtobin (*p
);
253 else if (strchr ("\"\\abcfnrtv", *p
))
254 print_esc_char (*p
++);
255 else if (!posixly_correct
&& (*p
== 'u' || *p
== 'U'))
258 unsigned int uni_value
;
261 for (esc_length
= (esc_char
== 'u' ? 4 : 8), ++p
;
266 error (1, 0, _("missing hexadecimal number in escape"));
267 uni_value
= uni_value
* 16 + hextobin (*p
);
270 /* A universal character name shall not specify a character short
271 identifier in the range 00000000 through 00000020, 0000007F through
272 0000009F, or 0000D800 through 0000DFFF inclusive. A universal
273 character name shall not designate a character in the required
275 if ((uni_value
>= 0x00 && uni_value
<= 0x9f
276 && uni_value
!= 0x24 && uni_value
!= 0x40 && uni_value
!= 0x60)
277 || (uni_value
>= 0xd800 && uni_value
<= 0xdfff))
278 error (1, 0, _("invalid universal character name \\%c%0*x"),
279 esc_char
, (esc_char
== 'u' ? 4 : 8), uni_value
);
281 print_unicode_char (stdout
, uni_value
);
284 error (1, 0, _("\\%c: invalid escape"), *p
);
285 return p
- escstart
- 1;
288 /* Print string STR, evaluating \ escapes. */
291 print_esc_string (const char *str
)
295 str
+= print_esc (str
);
300 /* Output a % directive. START is the start of the directive,
301 LENGTH is its length, and ARGUMENT is its argument.
302 If FIELD_WIDTH or PRECISION is non-negative, they are args for
303 '*' values in those fields. */
306 print_direc (const char *start
, size_t length
, int field_width
,
307 int precision
, const char *argument
)
309 char *p
; /* Null-terminated copy of % directive. */
311 p
= xmalloc ((unsigned) (length
+ 1));
312 strncpy (p
, start
, length
);
315 switch (p
[length
- 1])
322 printf (p
, xstrtol (argument
));
324 printf (p
, precision
, xstrtol (argument
));
329 printf (p
, field_width
, xstrtol (argument
));
331 printf (p
, field_width
, precision
, xstrtol (argument
));
342 printf (p
, xstrtoul (argument
));
344 printf (p
, precision
, xstrtoul (argument
));
349 printf (p
, field_width
, xstrtoul (argument
));
351 printf (p
, field_width
, precision
, xstrtoul (argument
));
363 printf (p
, xstrtod (argument
));
365 printf (p
, precision
, xstrtod (argument
));
370 printf (p
, field_width
, xstrtod (argument
));
372 printf (p
, field_width
, precision
, xstrtod (argument
));
377 printf (p
, *argument
);
384 printf (p
, argument
);
386 printf (p
, precision
, argument
);
391 printf (p
, field_width
, argument
);
393 printf (p
, field_width
, precision
, argument
);
401 /* Print the text in FORMAT, using ARGV (with ARGC elements) for
402 arguments to any `%' directives.
403 Return the number of elements of ARGV used. */
406 print_formatted (const char *format
, int argc
, char **argv
)
408 int save_argc
= argc
; /* Preserve original value. */
409 const char *f
; /* Pointer into `format'. */
410 const char *direc_start
; /* Start of % directive. */
411 size_t direc_length
; /* Length of % directive. */
412 int field_width
; /* Arg to first '*', or -1 if none. */
413 int precision
; /* Arg to second '*', or -1 if none. */
415 for (f
= format
; *f
; ++f
)
422 field_width
= precision
= -1;
432 print_esc_string (*argv
);
438 if (strchr ("-+ #", *f
))
449 field_width
= xstrtoul (*argv
);
472 precision
= xstrtoul (*argv
);
486 if (*f
== 'l' || *f
== 'L' || *f
== 'h')
491 if (!strchr ("diouxXfeEgGcs", *f
))
492 error (1, 0, _("%%%c: invalid directive"), *f
);
496 print_direc (direc_start
, direc_length
, field_width
,
502 print_direc (direc_start
, direc_length
, field_width
,
515 return save_argc
- argc
;
519 main (int argc
, char **argv
)
524 program_name
= argv
[0];
525 setlocale (LC_ALL
, "");
526 bindtextdomain (PACKAGE
, LOCALEDIR
);
527 textdomain (PACKAGE
);
529 atexit (close_stdout
);
533 /* Don't recognize --help or --version if POSIXLY_CORRECT is set. */
534 posixly_correct
= (getenv ("POSIXLY_CORRECT") != NULL
);
535 if (!posixly_correct
)
536 parse_long_options (argc
, argv
, PROGRAM_NAME
, GNU_PACKAGE
, VERSION
,
541 fprintf (stderr
, _("Usage: %s format [argument...]\n"), program_name
);
551 args_used
= print_formatted (format
, argc
, argv
);
555 while (args_used
> 0 && argc
> 0);
558 error (0, 0, _("warning: excess arguments have been ignored"));