blob: fa22bf75b8fb812de3291defa1ee04a98254d344 [file] [log] [blame]
Jari Aaltobb706242000-03-17 21:46:59 +00001/*
2 * print -- loadable ksh-93 style print builtin
3 */
4
Jari Aalto31859422009-01-12 13:36:28 +00005/*
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 Aalto7117c2d2002-07-17 14:10:11 +000023#ifdef HAVE_CONFIG_H
24# include <config.h>
25#endif
26
Jari Aaltocce855b1998-04-17 19:52:44 +000027#include "bashtypes.h"
Jari Aaltoccc6cda1996-12-23 17:02:34 +000028
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 Aalto31859422009-01-12 13:36:28 +000038#include "builtext.h"
39#include "common.h"
Jari Aaltoccc6cda1996-12-23 17:02:34 +000040
41#if !defined (errno)
Ricardo Cerqueiraa02fbff2013-07-25 22:35:34 +010042#include <errno.h>
Jari Aaltoccc6cda1996-12-23 17:02:34 +000043#endif
44
Jari Aaltoccc6cda1996-12-23 17:02:34 +000045int print_builtin ();
Jari Aaltoccc6cda1996-12-23 17:02:34 +000046static int printargs ();
47
48static FILE *ofp;
49
Jari Aaltoccc6cda1996-12-23 17:02:34 +000050extern char *this_command_name;
51
Jari Aaltoccc6cda1996-12-23 17:02:34 +000052static char *print_doc[] = {
Jari Aalto31859422009-01-12 13:36:28 +000053 "Display arguments.",
54 "",
Jari Aaltoccc6cda1996-12-23 17:02:34 +000055 "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
61struct 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
74int
75print_builtin (list)
76 WORD_LIST *list;
77{
78 int c, r, nflag, raw, ofd, sflag;
Jari Aalto7117c2d2002-07-17 14:10:11 +000079 intmax_t lfd;
Jari Aaltoccc6cda1996-12-23 17:02:34 +000080 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 Aalto7117c2d2002-07-17 14:10:11 +0000113 if (all_digits (list_optarg) && legal_number (list_optarg, &lfd) && lfd == (int)lfd)
114 ofd = lfd;
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000115 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
131opt_end:
132 list = loptend;
133
134 ofp = (ofd == 1) ? stdout : fdopen (dup (ofd), "w");
135
136 if (pfmt)
137 {
Jari Aaltocce855b1998-04-17 19:52:44 +0000138 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 Aaltoccc6cda1996-12-23 17:02:34 +0000147 }
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 Aaltocce855b1998-04-17 19:52:44 +0000171static int
172printargs (list, ofp)
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000173 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 Aaltobb706242000-03-17 21:46:59 +0000182 ostr = ansicstr (l->word->word, strlen (l->word->word), 0, &sawc, (int *)0);
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000183 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}