Patch-ID: bash41-003
[bash.git] / examples / loadables / print.c
blobe17597b3b16f36ad1013d2d6bce5a9854518c637
1 /*
2 * print -- loadable ksh-93 style print builtin
3 */
5 /*
6 Copyright (C) 1999-2009 Free Software Foundation, Inc.
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.
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.
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/>.
23 #ifdef HAVE_CONFIG_H
24 # include <config.h>
25 #endif
27 #include "bashtypes.h"
29 #include <errno.h>
30 #include <limits.h>
31 #include <stdio.h>
33 #include "bashansi.h"
34 #include "shell.h"
35 #include "builtins.h"
36 #include "stdc.h"
37 #include "bashgetopt.h"
38 #include "builtext.h"
39 #include "common.h"
41 #if !defined (errno)
42 extern int errno;
43 #endif
45 int print_builtin ();
46 static int printargs ();
48 static FILE *ofp;
50 extern char *this_command_name;
52 static char *print_doc[] = {
53 "Display arguments.",
54 "",
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
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
70 #ifndef ISOPTION
71 #define ISOPTION(s, c) (s[0] == '-' && s[2] == '\0' && s[1] == c)
72 #endif
74 int
75 print_builtin (list)
76 WORD_LIST *list;
78 int c, r, nflag, raw, ofd, sflag;
79 intmax_t lfd;
80 char **v, *pfmt, *arg;
81 WORD_LIST *l;
83 nflag = raw = sflag = 0;
84 ofd = 1;
85 pfmt = 0;
87 reset_internal_getopt ();
88 while ((c = internal_getopt (list, "Rnprsu:f:")) != -1)
90 switch (c)
92 case 'R':
93 raw = 2;
94 loptend = lcurrent;
95 if (loptend && ISOPTION (loptend->word->word, 'n'))
97 loptend = loptend->next;
98 nflag = 1;
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':
113 if (all_digits (list_optarg) && legal_number (list_optarg, &lfd) && lfd == (int)lfd)
114 ofd = lfd;
115 else
117 for (l = list; l->next && l->next != lcurrent; l = l->next);
118 lcurrent = loptend = l;
119 goto opt_end;
121 break;
122 case 'f':
123 pfmt = list_optarg;
124 break;
125 default:
126 builtin_usage ();
127 return (EX_USAGE);
131 opt_end:
132 list = loptend;
134 ofp = (ofd == 1) ? stdout : fdopen (dup (ofd), "w");
136 if (pfmt)
138 WORD_DESC *w;
139 WORD_LIST *nlist;
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);
149 if (raw)
151 for (l = list; l; l = l->next)
153 fprintf (ofp, "%s", l->word->word);
154 if (l->next)
155 fprintf (ofp, " ");
157 if (nflag == 0)
158 fprintf (ofp, "\n");
159 fflush (ofp);
160 return (0);
163 r = printargs (list, ofp);
164 if (r && nflag == 0)
165 fprintf (ofp, "\n");
166 if (ofd != 1)
167 fclose (ofp);
168 return 0;
171 static int
172 printargs (list, ofp)
173 WORD_LIST *list;
174 FILE *ofp;
176 WORD_LIST *l;
177 char *ostr;
178 int sawc;
180 for (sawc = 0, l = list; l; l = l->next)
182 ostr = ansicstr (l->word->word, strlen (l->word->word), 0, &sawc, (int *)0);
183 fprintf (ofp, "%s", ostr);
184 free (ostr);
185 if (sawc)
186 return (0);
187 if (l->next)
188 fprintf (ofp, " ");
190 return (1);