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