]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/less/lessecho.c
This commit was generated by cvs2svn to compensate for changes in r97403,
[FreeBSD/FreeBSD.git] / contrib / less / lessecho.c
1 /*
2  * Copyright (C) 1984-2000  Mark Nudelman
3  *
4  * You may distribute under the terms of either the GNU General Public
5  * License or the Less License, as specified in the README file.
6  *
7  * For more information about less, or for information on how to 
8  * contact the author, see the README file.
9  */
10
11
12 /*
13  * lessecho [-ox] [-cx] [-pn] [-dn] [-a] file ...
14  * Simply echos its filename arguments on standard output.
15  * But any argument containing spaces is enclosed in quotes.
16  *
17  * -ox  Specifies "x" to be the open quote character.
18  * -cx  Specifies "x" to be the close quote character.
19  * -pn  Specifies "n" to be the open quote character, as an integer.
20  * -dn  Specifies "n" to be the close quote character, as an integer.
21  * -a   Specifies that all arguments are to be quoted.
22  *      The default is that only arguments containing spaces are quoted.
23  */
24
25 #include "less.h"
26
27 static char *version = "$Revision: 1.6 $";
28
29 static int quote_all = 0;
30 static char openquote = '"';
31 static char closequote = '"';
32
33         static void
34 pr_usage()
35 {
36         fprintf(stderr,
37                 "usage: lessecho [-ox] [-cx] [-pn] [-dn] [-a] file ...\n");
38 }
39
40         static void
41 pr_version()
42 {
43         char *p;
44         char buf[10];
45         char *pbuf = buf;
46
47         for (p = version;  *p != ' ';  p++)
48                 if (*p == '\0')
49                         return;
50         for (p++;  *p != '$' && *p != ' ' && *p != '\0';  p++)
51                 *pbuf++ = *p;
52         *pbuf = '\0';
53         printf("%s\n", buf);
54 }
55
56         static void
57 pr_error(s)
58         char *s;
59 {
60         fprintf(stderr, "%s\n", s);
61         exit(1);
62 }
63
64         static long
65 lstrtol(s, radix, pend)
66         char *s;
67         int radix;
68         char **pend;
69 {
70         int v;
71         int neg = 0;
72         long n = 0;
73
74         /* Skip leading white space. */
75         while (*s == ' ' || *s == '\t')
76                 s++;
77
78         /* Check for a leading + or -. */
79         if (*s == '-')
80         {
81                 neg = 1;
82                 s++;
83         } else if (*s == '+')
84         {
85                 s++;
86         }
87
88         /* Determine radix if caller does not specify. */
89         if (radix == 0)
90         {
91                 radix = 10;
92                 if (*s == '0')
93                 {
94                         switch (*++s)
95                         {
96                         case 'x':
97                                 radix = 16;
98                                 s++;
99                                 break;
100                         default:
101                                 radix = 8;
102                                 break;
103                         }
104                 }
105         }
106
107         /* Parse the digits of the number. */
108         for (;;)
109         {
110                 if (*s >= '0' && *s <= '9')
111                         v = *s - '0';
112                 else if (*s >= 'a' && *s <= 'f')
113                         v = *s - 'a' + 10;
114                 else if (*s >= 'A' && *s <= 'F')
115                         v = *s - 'A' + 10;
116                 else
117                         break;
118                 if (v >= radix)
119                         break;
120                 n = n * radix + v;
121                 s++;
122         }
123
124         if (pend != NULL)
125         {
126                 /* Skip trailing white space. */
127                 while (*s == ' ' || *s == '\t')
128                         s++;
129                 *pend = s;
130         }
131         if (neg)
132                 return (-n);
133         return (n);
134 }
135
136
137 #if !HAVE_STRCHR
138         char *
139 strchr(s, c)
140         char *s;
141         int c;
142 {
143         for ( ;  *s != '\0';  s++)
144                 if (*s == c)
145                         return (s);
146         if (c == '\0')
147                 return (s);
148         return (NULL);
149 }
150 #endif
151
152         int
153 main(argc, argv)
154         int argc;
155         char *argv[];
156 {
157         char *arg;
158         char *s;
159         int no_more_options;
160
161         no_more_options = 0;
162         while (--argc > 0)
163         {
164                 arg = *++argv;
165                 if (*arg != '-' || no_more_options)
166                         break;
167                 switch (*++arg)
168                 {
169                 case 'a':
170                         quote_all = 1;
171                         break;
172                 case 'o':
173                         openquote = *++arg;
174                         break;
175                 case 'c':
176                         closequote = *++arg;
177                         break;
178                 case 'p':
179                         openquote = lstrtol(++arg, 0, &s);
180                         if (s == arg)
181                                 pr_error("Missing number after -O");
182                         break;
183                 case 'd':
184                         closequote = lstrtol(++arg, 0, &s);
185                         if (s == arg)
186                                 pr_error("Missing number after -C");
187                         break;
188                 case '?':
189                         pr_usage();
190                         return (0);
191                 case '-':
192                         if (*++arg == '\0')
193                         {
194                                 no_more_options = 1;
195                                 break;
196                         }
197                         if (strcmp(arg, "version") == 0)
198                         {
199                                 pr_version();
200                                 return (0);
201                         }
202                         if (strcmp(arg, "help") == 0)
203                         {
204                                 pr_usage();
205                                 return (0);
206                         }
207                         pr_error("Invalid option after --");
208                 default:
209                         pr_error("Invalid option letter");
210                 }
211         }
212
213         while (argc-- > 0)
214         {
215                 arg = *argv++;
216                 if (quote_all || strchr(arg, ' ') != NULL)
217                         printf("%c%s%c", openquote, arg, closequote);
218                 else
219                         printf("%s", arg);
220                 if (argc > 0)
221                         printf(" ");
222                 else
223                         printf("\n");
224         }
225         return (0);
226 }