kill: add undocumented -L for compatibility
[coreutils.git] / src / printf.c
bloba244946318a9d6a3788216174428dc65e26cb306
1 /* printf - format and print data
2 Copyright (C) 1990-2015 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 3 of the License, or
7 (at your option) any later version.
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, see <http://www.gnu.org/licenses/>. */
17 /* Usage: printf format [argument...]
19 A front end to the printf function that lets it be used from the shell.
21 Backslash escapes:
23 \" = double quote
24 \\ = backslash
25 \a = alert (bell)
26 \b = backspace
27 \c = produce no further output
28 \e = escape
29 \f = form feed
30 \n = new line
31 \r = carriage return
32 \t = horizontal tab
33 \v = vertical tab
34 \ooo = octal number (ooo is 1 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)
39 Additional directive:
41 %b = print an argument string, interpreting backslash escapes,
42 except that octal escapes are of the form \0 or \0ooo.
44 %q = print an argument string in a format that can be
45 reused as shell input. Escaped characters used the proposed
46 POSIX $'' syntax supported by most shells.
48 The 'format' argument is re-used as many times as necessary
49 to convert all of the given arguments.
51 David MacKenzie <djm@gnu.ai.mit.edu> */
53 #include <config.h>
54 #include <stdio.h>
55 #include <sys/types.h>
57 #include "system.h"
58 #include "c-strtod.h"
59 #include "error.h"
60 #include "quote.h"
61 #include "unicodeio.h"
62 #include "xprintf.h"
64 /* The official name of this program (e.g., no 'g' prefix). */
65 #define PROGRAM_NAME "printf"
67 #define AUTHORS proper_name ("David MacKenzie")
69 #define isodigit(c) ((c) >= '0' && (c) <= '7')
70 #define hextobin(c) ((c) >= 'a' && (c) <= 'f' ? (c) - 'a' + 10 : \
71 (c) >= 'A' && (c) <= 'F' ? (c) - 'A' + 10 : (c) - '0')
72 #define octtobin(c) ((c) - '0')
74 /* The value to return to the calling program. */
75 static int exit_status;
77 /* True if the POSIXLY_CORRECT environment variable is set. */
78 static bool posixly_correct;
80 /* This message appears in N_() here rather than just in _() below because
81 the sole use would have been in a #define. */
82 static char const *const cfcc_msg =
83 N_("warning: %s: character(s) following character constant have been ignored");
85 void
86 usage (int status)
88 if (status != EXIT_SUCCESS)
89 emit_try_help ();
90 else
92 printf (_("\
93 Usage: %s FORMAT [ARGUMENT]...\n\
94 or: %s OPTION\n\
95 "),
96 program_name, program_name);
97 fputs (_("\
98 Print ARGUMENT(s) according to FORMAT, or execute according to OPTION:\n\
99 \n\
100 "), stdout);
101 fputs (HELP_OPTION_DESCRIPTION, stdout);
102 fputs (VERSION_OPTION_DESCRIPTION, stdout);
103 fputs (_("\
105 FORMAT controls the output as in C printf. Interpreted sequences are:\n\
107 \\\" double quote\n\
108 "), stdout);
109 fputs (_("\
110 \\\\ backslash\n\
111 \\a alert (BEL)\n\
112 \\b backspace\n\
113 \\c produce no further output\n\
114 \\e escape\n\
115 \\f form feed\n\
116 \\n new line\n\
117 \\r carriage return\n\
118 \\t horizontal tab\n\
119 \\v vertical tab\n\
120 "), stdout);
121 fputs (_("\
122 \\NNN byte with octal value NNN (1 to 3 digits)\n\
123 \\xHH byte with hexadecimal value HH (1 to 2 digits)\n\
124 \\uHHHH Unicode (ISO/IEC 10646) character with hex value HHHH (4 digits)\n\
125 \\UHHHHHHHH Unicode character with hex value HHHHHHHH (8 digits)\n\
126 "), stdout);
127 fputs (_("\
128 %% a single %\n\
129 %b ARGUMENT as a string with '\\' escapes interpreted,\n\
130 except that octal escapes are of the form \\0 or \\0NNN\n\
131 %q ARGUMENT is printed in a format that can be reused as shell input,\n\
132 escaping non-printable characters with the proposed POSIX $'' syntax.\
133 \n\n\
134 and all C format specifications ending with one of diouxXfeEgGcs, with\n\
135 ARGUMENTs converted to proper type first. Variable widths are handled.\n\
136 "), stdout);
137 printf (USAGE_BUILTIN_WARNING, PROGRAM_NAME);
138 emit_ancillary_info (PROGRAM_NAME);
140 exit (status);
143 static void
144 verify_numeric (const char *s, const char *end)
146 if (errno)
148 error (0, errno, "%s", quote (s));
149 exit_status = EXIT_FAILURE;
151 else if (*end)
153 if (s == end)
154 error (0, 0, _("%s: expected a numeric value"), quote (s));
155 else
156 error (0, 0, _("%s: value not completely converted"), quote (s));
157 exit_status = EXIT_FAILURE;
161 #define STRTOX(TYPE, FUNC_NAME, LIB_FUNC_EXPR) \
162 static TYPE \
163 FUNC_NAME (char const *s) \
165 char *end; \
166 TYPE val; \
168 if ((*s == '\"' || *s == '\'') && *(s + 1)) \
170 unsigned char ch = *++s; \
171 val = ch; \
172 /* If POSIXLY_CORRECT is not set, then give a warning that there \
173 are characters following the character constant and that GNU \
174 printf is ignoring those characters. If POSIXLY_CORRECT *is* \
175 set, then don't give the warning. */ \
176 if (*++s != 0 && !posixly_correct) \
177 error (0, 0, _(cfcc_msg), s); \
179 else \
181 errno = 0; \
182 val = (LIB_FUNC_EXPR); \
183 verify_numeric (s, end); \
185 return val; \
188 STRTOX (intmax_t, vstrtoimax, strtoimax (s, &end, 0))
189 STRTOX (uintmax_t, vstrtoumax, strtoumax (s, &end, 0))
190 STRTOX (long double, vstrtold, c_strtold (s, &end))
192 /* Output a single-character \ escape. */
194 static void
195 print_esc_char (char c)
197 switch (c)
199 case 'a': /* Alert. */
200 putchar ('\a');
201 break;
202 case 'b': /* Backspace. */
203 putchar ('\b');
204 break;
205 case 'c': /* Cancel the rest of the output. */
206 exit (EXIT_SUCCESS);
207 break;
208 case 'e': /* Escape. */
209 putchar ('\x1B');
210 break;
211 case 'f': /* Form feed. */
212 putchar ('\f');
213 break;
214 case 'n': /* New line. */
215 putchar ('\n');
216 break;
217 case 'r': /* Carriage return. */
218 putchar ('\r');
219 break;
220 case 't': /* Horizontal tab. */
221 putchar ('\t');
222 break;
223 case 'v': /* Vertical tab. */
224 putchar ('\v');
225 break;
226 default:
227 putchar (c);
228 break;
232 /* Print a \ escape sequence starting at ESCSTART.
233 Return the number of characters in the escape sequence
234 besides the backslash.
235 If OCTAL_0 is nonzero, octal escapes are of the form \0ooo, where o
236 is an octal digit; otherwise they are of the form \ooo. */
238 static int
239 print_esc (const char *escstart, bool octal_0)
241 const char *p = escstart + 1;
242 int esc_value = 0; /* Value of \nnn escape. */
243 int esc_length; /* Length of \nnn escape. */
245 if (*p == 'x')
247 /* A hexadecimal \xhh escape sequence must have 1 or 2 hex. digits. */
248 for (esc_length = 0, ++p;
249 esc_length < 2 && isxdigit (to_uchar (*p));
250 ++esc_length, ++p)
251 esc_value = esc_value * 16 + hextobin (*p);
252 if (esc_length == 0)
253 error (EXIT_FAILURE, 0, _("missing hexadecimal number in escape"));
254 putchar (esc_value);
256 else if (isodigit (*p))
258 /* Parse \0ooo (if octal_0 && *p == '0') or \ooo (otherwise).
259 Allow \ooo if octal_0 && *p != '0'; this is an undocumented
260 extension to POSIX that is compatible with Bash 2.05b. */
261 for (esc_length = 0, p += octal_0 && *p == '0';
262 esc_length < 3 && isodigit (*p);
263 ++esc_length, ++p)
264 esc_value = esc_value * 8 + octtobin (*p);
265 putchar (esc_value);
267 else if (*p && strchr ("\"\\abcefnrtv", *p))
268 print_esc_char (*p++);
269 else if (*p == 'u' || *p == 'U')
271 char esc_char = *p;
272 unsigned int uni_value;
274 uni_value = 0;
275 for (esc_length = (esc_char == 'u' ? 4 : 8), ++p;
276 esc_length > 0;
277 --esc_length, ++p)
279 if (! isxdigit (to_uchar (*p)))
280 error (EXIT_FAILURE, 0, _("missing hexadecimal number in escape"));
281 uni_value = uni_value * 16 + hextobin (*p);
284 /* A universal character name shall not specify a character short
285 identifier in the range 00000000 through 00000020, 0000007F through
286 0000009F, or 0000D800 through 0000DFFF inclusive. A universal
287 character name shall not designate a character in the required
288 character set. */
289 if ((uni_value <= 0x9f
290 && uni_value != 0x24 && uni_value != 0x40 && uni_value != 0x60)
291 || (uni_value >= 0xd800 && uni_value <= 0xdfff))
292 error (EXIT_FAILURE, 0, _("invalid universal character name \\%c%0*x"),
293 esc_char, (esc_char == 'u' ? 4 : 8), uni_value);
295 print_unicode_char (stdout, uni_value, 0);
297 else
299 putchar ('\\');
300 if (*p)
302 putchar (*p);
303 p++;
306 return p - escstart - 1;
309 /* Print string STR, evaluating \ escapes. */
311 static void
312 print_esc_string (const char *str)
314 for (; *str; str++)
315 if (*str == '\\')
316 str += print_esc (str, true);
317 else
318 putchar (*str);
321 /* Evaluate a printf conversion specification. START is the start of
322 the directive, LENGTH is its length, and CONVERSION specifies the
323 type of conversion. LENGTH does not include any length modifier or
324 the conversion specifier itself. FIELD_WIDTH and PRECISION are the
325 field width and precision for '*' values, if HAVE_FIELD_WIDTH and
326 HAVE_PRECISION are true, respectively. ARGUMENT is the argument to
327 be formatted. */
329 static void
330 print_direc (const char *start, size_t length, char conversion,
331 bool have_field_width, int field_width,
332 bool have_precision, int precision,
333 char const *argument)
335 char *p; /* Null-terminated copy of % directive. */
337 /* Create a null-terminated copy of the % directive, with an
338 intmax_t-wide length modifier substituted for any existing
339 integer length modifier. */
341 char *q;
342 char const *length_modifier;
343 size_t length_modifier_len;
345 switch (conversion)
347 case 'd': case 'i': case 'o': case 'u': case 'x': case 'X':
348 length_modifier = PRIdMAX;
349 length_modifier_len = sizeof PRIdMAX - 2;
350 break;
352 case 'a': case 'e': case 'f': case 'g':
353 case 'A': case 'E': case 'F': case 'G':
354 length_modifier = "L";
355 length_modifier_len = 1;
356 break;
358 default:
359 length_modifier = start; /* Any valid pointer will do. */
360 length_modifier_len = 0;
361 break;
364 p = xmalloc (length + length_modifier_len + 2);
365 q = mempcpy (p, start, length);
366 q = mempcpy (q, length_modifier, length_modifier_len);
367 *q++ = conversion;
368 *q = '\0';
371 switch (conversion)
373 case 'd':
374 case 'i':
376 intmax_t arg = vstrtoimax (argument);
377 if (!have_field_width)
379 if (!have_precision)
380 xprintf (p, arg);
381 else
382 xprintf (p, precision, arg);
384 else
386 if (!have_precision)
387 xprintf (p, field_width, arg);
388 else
389 xprintf (p, field_width, precision, arg);
392 break;
394 case 'o':
395 case 'u':
396 case 'x':
397 case 'X':
399 uintmax_t arg = vstrtoumax (argument);
400 if (!have_field_width)
402 if (!have_precision)
403 xprintf (p, arg);
404 else
405 xprintf (p, precision, arg);
407 else
409 if (!have_precision)
410 xprintf (p, field_width, arg);
411 else
412 xprintf (p, field_width, precision, arg);
415 break;
417 case 'a':
418 case 'A':
419 case 'e':
420 case 'E':
421 case 'f':
422 case 'F':
423 case 'g':
424 case 'G':
426 long double arg = vstrtold (argument);
427 if (!have_field_width)
429 if (!have_precision)
430 xprintf (p, arg);
431 else
432 xprintf (p, precision, arg);
434 else
436 if (!have_precision)
437 xprintf (p, field_width, arg);
438 else
439 xprintf (p, field_width, precision, arg);
442 break;
444 case 'c':
445 if (!have_field_width)
446 xprintf (p, *argument);
447 else
448 xprintf (p, field_width, *argument);
449 break;
451 case 's':
452 if (!have_field_width)
454 if (!have_precision)
455 xprintf (p, argument);
456 else
457 xprintf (p, precision, argument);
459 else
461 if (!have_precision)
462 xprintf (p, field_width, argument);
463 else
464 xprintf (p, field_width, precision, argument);
466 break;
469 free (p);
472 /* Print the text in FORMAT, using ARGV (with ARGC elements) for
473 arguments to any '%' directives.
474 Return the number of elements of ARGV used. */
476 static int
477 print_formatted (const char *format, int argc, char **argv)
479 int save_argc = argc; /* Preserve original value. */
480 const char *f; /* Pointer into 'format'. */
481 const char *direc_start; /* Start of % directive. */
482 size_t direc_length; /* Length of % directive. */
483 bool have_field_width; /* True if FIELD_WIDTH is valid. */
484 int field_width = 0; /* Arg to first '*'. */
485 bool have_precision; /* True if PRECISION is valid. */
486 int precision = 0; /* Arg to second '*'. */
487 char ok[UCHAR_MAX + 1]; /* ok['x'] is true if %x is allowed. */
489 for (f = format; *f; ++f)
491 switch (*f)
493 case '%':
494 direc_start = f++;
495 direc_length = 1;
496 have_field_width = have_precision = false;
497 if (*f == '%')
499 putchar ('%');
500 break;
502 if (*f == 'b')
504 /* FIXME: Field width and precision are not supported
505 for %b, even though POSIX requires it. */
506 if (argc > 0)
508 print_esc_string (*argv);
509 ++argv;
510 --argc;
512 break;
515 if (*f == 'q')
517 if (argc > 0)
519 fputs (quotearg_style (shell_escape_quoting_style, *argv),
520 stdout);
521 ++argv;
522 --argc;
524 break;
527 memset (ok, 0, sizeof ok);
528 ok['a'] = ok['A'] = ok['c'] = ok['d'] = ok['e'] = ok['E'] =
529 ok['f'] = ok['F'] = ok['g'] = ok['G'] = ok['i'] = ok['o'] =
530 ok['s'] = ok['u'] = ok['x'] = ok['X'] = 1;
532 for (;; f++, direc_length++)
533 switch (*f)
535 #if (__GLIBC__ == 2 && 2 <= __GLIBC_MINOR__) || 3 <= __GLIBC__
536 case 'I':
537 #endif
538 case '\'':
539 ok['a'] = ok['A'] = ok['c'] = ok['e'] = ok['E'] =
540 ok['o'] = ok['s'] = ok['x'] = ok['X'] = 0;
541 break;
542 case '-': case '+': case ' ':
543 break;
544 case '#':
545 ok['c'] = ok['d'] = ok['i'] = ok['s'] = ok['u'] = 0;
546 break;
547 case '0':
548 ok['c'] = ok['s'] = 0;
549 break;
550 default:
551 goto no_more_flag_characters;
553 no_more_flag_characters:
555 if (*f == '*')
557 ++f;
558 ++direc_length;
559 if (argc > 0)
561 intmax_t width = vstrtoimax (*argv);
562 if (INT_MIN <= width && width <= INT_MAX)
563 field_width = width;
564 else
565 error (EXIT_FAILURE, 0, _("invalid field width: %s"),
566 quote (*argv));
567 ++argv;
568 --argc;
570 else
571 field_width = 0;
572 have_field_width = true;
574 else
575 while (ISDIGIT (*f))
577 ++f;
578 ++direc_length;
580 if (*f == '.')
582 ++f;
583 ++direc_length;
584 ok['c'] = 0;
585 if (*f == '*')
587 ++f;
588 ++direc_length;
589 if (argc > 0)
591 intmax_t prec = vstrtoimax (*argv);
592 if (prec < 0)
594 /* A negative precision is taken as if the
595 precision were omitted, so -1 is safe
596 here even if prec < INT_MIN. */
597 precision = -1;
599 else if (INT_MAX < prec)
600 error (EXIT_FAILURE, 0, _("invalid precision: %s"),
601 quote (*argv));
602 else
603 precision = prec;
604 ++argv;
605 --argc;
607 else
608 precision = 0;
609 have_precision = true;
611 else
612 while (ISDIGIT (*f))
614 ++f;
615 ++direc_length;
619 while (*f == 'l' || *f == 'L' || *f == 'h'
620 || *f == 'j' || *f == 't' || *f == 'z')
621 ++f;
624 unsigned char conversion = *f;
625 if (! ok[conversion])
626 error (EXIT_FAILURE, 0,
627 _("%.*s: invalid conversion specification"),
628 (int) (f + 1 - direc_start), direc_start);
631 print_direc (direc_start, direc_length, *f,
632 have_field_width, field_width,
633 have_precision, precision,
634 (argc <= 0 ? "" : (argc--, *argv++)));
635 break;
637 case '\\':
638 f += print_esc (f, false);
639 break;
641 default:
642 putchar (*f);
646 return save_argc - argc;
650 main (int argc, char **argv)
652 char *format;
653 int args_used;
655 initialize_main (&argc, &argv);
656 set_program_name (argv[0]);
657 setlocale (LC_ALL, "");
658 bindtextdomain (PACKAGE, LOCALEDIR);
659 textdomain (PACKAGE);
661 atexit (close_stdout);
663 exit_status = EXIT_SUCCESS;
665 posixly_correct = (getenv ("POSIXLY_CORRECT") != NULL);
667 /* We directly parse options, rather than use parse_long_options, in
668 order to avoid accepting abbreviations. */
669 if (argc == 2)
671 if (STREQ (argv[1], "--help"))
672 usage (EXIT_SUCCESS);
674 if (STREQ (argv[1], "--version"))
676 version_etc (stdout, PROGRAM_NAME, PACKAGE_NAME, Version, AUTHORS,
677 (char *) NULL);
678 return EXIT_SUCCESS;
682 /* The above handles --help and --version.
683 Since there is no other invocation of getopt, handle '--' here. */
684 if (1 < argc && STREQ (argv[1], "--"))
686 --argc;
687 ++argv;
690 if (argc <= 1)
692 error (0, 0, _("missing operand"));
693 usage (EXIT_FAILURE);
696 format = argv[1];
697 argc -= 2;
698 argv += 2;
702 args_used = print_formatted (format, argc, argv);
703 argc -= args_used;
704 argv += args_used;
706 while (args_used > 0 && argc > 0);
708 if (argc > 0)
709 error (0, 0,
710 _("warning: ignoring excess arguments, starting with %s"),
711 quote (argv[0]));
713 return exit_status;