Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 1 | /* |
| 2 | * print -- loadable ksh-93 style print builtin |
| 3 | */ |
| 4 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 5 | /* |
| 6 | Copyright (C) 1999-2009 Free Software Foundation, Inc. |
| 7 | |
| 8 | This file is part of GNU Bash. |
| 9 | Bash is free software: you can redistribute it and/or modify |
| 10 | it under the terms of the GNU General Public License as published by |
| 11 | the Free Software Foundation, either version 3 of the License, or |
| 12 | (at your option) any later version. |
| 13 | |
| 14 | Bash is distributed in the hope that it will be useful, |
| 15 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | GNU General Public License for more details. |
| 18 | |
| 19 | You should have received a copy of the GNU General Public License |
| 20 | along with Bash. If not, see <http://www.gnu.org/licenses/>. |
| 21 | */ |
| 22 | |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 23 | #ifdef HAVE_CONFIG_H |
| 24 | # include <config.h> |
| 25 | #endif |
| 26 | |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 27 | #include "bashtypes.h" |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 28 | |
| 29 | #include <errno.h> |
| 30 | #include <limits.h> |
| 31 | #include <stdio.h> |
| 32 | |
| 33 | #include "bashansi.h" |
| 34 | #include "shell.h" |
| 35 | #include "builtins.h" |
| 36 | #include "stdc.h" |
| 37 | #include "bashgetopt.h" |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 38 | #include "builtext.h" |
| 39 | #include "common.h" |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 40 | |
| 41 | #if !defined (errno) |
Ricardo Cerqueira | a02fbff | 2013-07-25 22:35:34 +0100 | [diff] [blame] | 42 | #include <errno.h> |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 43 | #endif |
| 44 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 45 | int print_builtin (); |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 46 | static int printargs (); |
| 47 | |
| 48 | static FILE *ofp; |
| 49 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 50 | extern char *this_command_name; |
| 51 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 52 | static char *print_doc[] = { |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 53 | "Display arguments.", |
| 54 | "", |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 55 | "Output the arguments. The -f option means to use the argument as a", |
| 56 | "format string as would be supplied to printf(1). The rest of the", |
| 57 | "options are as in ksh.", |
| 58 | (char *)NULL |
| 59 | }; |
| 60 | |
| 61 | struct builtin print_struct = { |
| 62 | "print", |
| 63 | print_builtin, |
| 64 | BUILTIN_ENABLED, |
| 65 | print_doc, |
| 66 | "print [-Rnprs] [-u unit] [-f format] [arguments]", |
| 67 | (char *)0 |
| 68 | }; |
| 69 | |
| 70 | #ifndef ISOPTION |
| 71 | #define ISOPTION(s, c) (s[0] == '-' && s[2] == '\0' && s[1] == c) |
| 72 | #endif |
| 73 | |
| 74 | int |
| 75 | print_builtin (list) |
| 76 | WORD_LIST *list; |
| 77 | { |
| 78 | int c, r, nflag, raw, ofd, sflag; |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 79 | intmax_t lfd; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 80 | char **v, *pfmt, *arg; |
| 81 | WORD_LIST *l; |
| 82 | |
| 83 | nflag = raw = sflag = 0; |
| 84 | ofd = 1; |
| 85 | pfmt = 0; |
| 86 | |
| 87 | reset_internal_getopt (); |
| 88 | while ((c = internal_getopt (list, "Rnprsu:f:")) != -1) |
| 89 | { |
| 90 | switch (c) |
| 91 | { |
| 92 | case 'R': |
| 93 | raw = 2; |
| 94 | loptend = lcurrent; |
| 95 | if (loptend && ISOPTION (loptend->word->word, 'n')) |
| 96 | { |
| 97 | loptend = loptend->next; |
| 98 | nflag = 1; |
| 99 | } |
| 100 | goto opt_end; |
| 101 | case 'r': |
| 102 | raw = 1; |
| 103 | break; |
| 104 | case 'n': |
| 105 | nflag = 1; |
| 106 | break; |
| 107 | case 's': |
| 108 | sflag = 1; |
| 109 | break; |
| 110 | case 'p': |
| 111 | break; /* NOP */ |
| 112 | case 'u': |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 113 | if (all_digits (list_optarg) && legal_number (list_optarg, &lfd) && lfd == (int)lfd) |
| 114 | ofd = lfd; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 115 | else |
| 116 | { |
| 117 | for (l = list; l->next && l->next != lcurrent; l = l->next); |
| 118 | lcurrent = loptend = l; |
| 119 | goto opt_end; |
| 120 | } |
| 121 | break; |
| 122 | case 'f': |
| 123 | pfmt = list_optarg; |
| 124 | break; |
| 125 | default: |
| 126 | builtin_usage (); |
| 127 | return (EX_USAGE); |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | opt_end: |
| 132 | list = loptend; |
| 133 | |
| 134 | ofp = (ofd == 1) ? stdout : fdopen (dup (ofd), "w"); |
| 135 | |
| 136 | if (pfmt) |
| 137 | { |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 138 | WORD_DESC *w; |
| 139 | WORD_LIST *nlist; |
| 140 | |
| 141 | w = make_word (pfmt); |
| 142 | nlist = make_word_list (w, list); |
| 143 | r = printf_builtin (nlist); |
| 144 | nlist->next = (WORD_LIST *)NULL; |
| 145 | dispose_words (nlist); |
| 146 | return (r); |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | if (raw) |
| 150 | { |
| 151 | for (l = list; l; l = l->next) |
| 152 | { |
| 153 | fprintf (ofp, "%s", l->word->word); |
| 154 | if (l->next) |
| 155 | fprintf (ofp, " "); |
| 156 | } |
| 157 | if (nflag == 0) |
| 158 | fprintf (ofp, "\n"); |
| 159 | fflush (ofp); |
| 160 | return (0); |
| 161 | } |
| 162 | |
| 163 | r = printargs (list, ofp); |
| 164 | if (r && nflag == 0) |
| 165 | fprintf (ofp, "\n"); |
| 166 | if (ofd != 1) |
| 167 | fclose (ofp); |
| 168 | return 0; |
| 169 | } |
| 170 | |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 171 | static int |
| 172 | printargs (list, ofp) |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 173 | WORD_LIST *list; |
| 174 | FILE *ofp; |
| 175 | { |
| 176 | WORD_LIST *l; |
| 177 | char *ostr; |
| 178 | int sawc; |
| 179 | |
| 180 | for (sawc = 0, l = list; l; l = l->next) |
| 181 | { |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 182 | ostr = ansicstr (l->word->word, strlen (l->word->word), 0, &sawc, (int *)0); |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 183 | fprintf (ofp, "%s", ostr); |
| 184 | free (ostr); |
| 185 | if (sawc) |
| 186 | return (0); |
| 187 | if (l->next) |
| 188 | fprintf (ofp, " "); |
| 189 | } |
| 190 | return (1); |
| 191 | } |