]>
Commit | Line | Data |
---|---|---|
cce855bc | 1 | #include "bashtypes.h" |
ccc6cda3 JA |
2 | |
3 | #include <errno.h> | |
4 | #include <limits.h> | |
5 | #include <stdio.h> | |
6 | ||
7 | #include "bashansi.h" | |
8 | #include "shell.h" | |
9 | #include "builtins.h" | |
10 | #include "stdc.h" | |
11 | #include "bashgetopt.h" | |
12 | ||
13 | #if !defined (errno) | |
14 | extern int errno; | |
15 | #endif | |
16 | ||
ccc6cda3 | 17 | int print_builtin (); |
ccc6cda3 JA |
18 | static int printargs (); |
19 | ||
20 | static FILE *ofp; | |
21 | ||
22 | extern char *ansicstr (); | |
ccc6cda3 JA |
23 | |
24 | extern char *this_command_name; | |
25 | ||
ccc6cda3 JA |
26 | static char *print_doc[] = { |
27 | "Output the arguments. The -f option means to use the argument as a", | |
28 | "format string as would be supplied to printf(1). The rest of the", | |
29 | "options are as in ksh.", | |
30 | (char *)NULL | |
31 | }; | |
32 | ||
33 | struct builtin print_struct = { | |
34 | "print", | |
35 | print_builtin, | |
36 | BUILTIN_ENABLED, | |
37 | print_doc, | |
38 | "print [-Rnprs] [-u unit] [-f format] [arguments]", | |
39 | (char *)0 | |
40 | }; | |
41 | ||
42 | #ifndef ISOPTION | |
43 | #define ISOPTION(s, c) (s[0] == '-' && s[2] == '\0' && s[1] == c) | |
44 | #endif | |
45 | ||
46 | int | |
47 | print_builtin (list) | |
48 | WORD_LIST *list; | |
49 | { | |
50 | int c, r, nflag, raw, ofd, sflag; | |
51 | char **v, *pfmt, *arg; | |
52 | WORD_LIST *l; | |
53 | ||
54 | nflag = raw = sflag = 0; | |
55 | ofd = 1; | |
56 | pfmt = 0; | |
57 | ||
58 | reset_internal_getopt (); | |
59 | while ((c = internal_getopt (list, "Rnprsu:f:")) != -1) | |
60 | { | |
61 | switch (c) | |
62 | { | |
63 | case 'R': | |
64 | raw = 2; | |
65 | loptend = lcurrent; | |
66 | if (loptend && ISOPTION (loptend->word->word, 'n')) | |
67 | { | |
68 | loptend = loptend->next; | |
69 | nflag = 1; | |
70 | } | |
71 | goto opt_end; | |
72 | case 'r': | |
73 | raw = 1; | |
74 | break; | |
75 | case 'n': | |
76 | nflag = 1; | |
77 | break; | |
78 | case 's': | |
79 | sflag = 1; | |
80 | break; | |
81 | case 'p': | |
82 | break; /* NOP */ | |
83 | case 'u': | |
84 | if (all_digits (list_optarg)) | |
85 | ofd = atoi (list_optarg); | |
86 | else | |
87 | { | |
88 | for (l = list; l->next && l->next != lcurrent; l = l->next); | |
89 | lcurrent = loptend = l; | |
90 | goto opt_end; | |
91 | } | |
92 | break; | |
93 | case 'f': | |
94 | pfmt = list_optarg; | |
95 | break; | |
96 | default: | |
97 | builtin_usage (); | |
98 | return (EX_USAGE); | |
99 | } | |
100 | } | |
101 | ||
102 | opt_end: | |
103 | list = loptend; | |
104 | ||
105 | ofp = (ofd == 1) ? stdout : fdopen (dup (ofd), "w"); | |
106 | ||
107 | if (pfmt) | |
108 | { | |
cce855bc JA |
109 | WORD_DESC *w; |
110 | WORD_LIST *nlist; | |
111 | ||
112 | w = make_word (pfmt); | |
113 | nlist = make_word_list (w, list); | |
114 | r = printf_builtin (nlist); | |
115 | nlist->next = (WORD_LIST *)NULL; | |
116 | dispose_words (nlist); | |
117 | return (r); | |
ccc6cda3 JA |
118 | } |
119 | ||
120 | if (raw) | |
121 | { | |
122 | for (l = list; l; l = l->next) | |
123 | { | |
124 | fprintf (ofp, "%s", l->word->word); | |
125 | if (l->next) | |
126 | fprintf (ofp, " "); | |
127 | } | |
128 | if (nflag == 0) | |
129 | fprintf (ofp, "\n"); | |
130 | fflush (ofp); | |
131 | return (0); | |
132 | } | |
133 | ||
134 | r = printargs (list, ofp); | |
135 | if (r && nflag == 0) | |
136 | fprintf (ofp, "\n"); | |
137 | if (ofd != 1) | |
138 | fclose (ofp); | |
139 | return 0; | |
140 | } | |
141 | ||
cce855bc JA |
142 | static int |
143 | printargs (list, ofp) | |
ccc6cda3 JA |
144 | WORD_LIST *list; |
145 | FILE *ofp; | |
146 | { | |
147 | WORD_LIST *l; | |
148 | char *ostr; | |
149 | int sawc; | |
150 | ||
151 | for (sawc = 0, l = list; l; l = l->next) | |
152 | { | |
cce855bc | 153 | ostr = ansicstr (l->word->word, strlen (l->word->word), &sawc, (int *)0); |
ccc6cda3 JA |
154 | fprintf (ofp, "%s", ostr); |
155 | free (ostr); | |
156 | if (sawc) | |
157 | return (0); | |
158 | if (l->next) | |
159 | fprintf (ofp, " "); | |
160 | } | |
161 | return (1); | |
162 | } | |
163 |