kernel - Refactor the lwkt_token code, making it faster
[dragonfly.git] / contrib / less / lessecho.c
blobbf6f19d78ccf1a53af2ee382ae659f1f76b34798
1 /*
2 * Copyright (C) 1984-2009 Mark Nudelman
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.
7 * For more information about less, or for information on how to
8 * contact the author, see the README file.
9 */
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.
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 * -mx Specifies "x" to be a metachar.
22 * -nn Specifies "n" to be a metachar, as an integer.
23 * -ex Specifies "x" to be the escape char for metachars.
24 * -fn Specifies "x" to be the escape char for metachars, as an integer.
25 * -a Specifies that all arguments are to be quoted.
26 * The default is that only arguments containing spaces are quoted.
29 #include "less.h"
31 static char *version = "$Revision: 1.13 $";
33 static int quote_all = 0;
34 static char openquote = '"';
35 static char closequote = '"';
36 static char *meta_escape = "\\";
37 static char meta_escape_buf[2];
38 static char metachars[64] = "";
39 static int num_metachars = 0;
41 static void
42 pr_usage()
44 fprintf(stderr,
45 "usage: lessecho [-ox] [-cx] [-pn] [-dn] [-mx] [-nn] [-ex] [-fn] [-a] file ...\n");
48 static void
49 pr_version()
51 char *p;
52 char buf[10];
53 char *pbuf = buf;
55 for (p = version; *p != ' '; p++)
56 if (*p == '\0')
57 return;
58 for (p++; *p != '$' && *p != ' ' && *p != '\0'; p++)
59 *pbuf++ = *p;
60 *pbuf = '\0';
61 printf("%s\n", buf);
64 static void
65 pr_error(s)
66 char *s;
68 fprintf(stderr, "%s\n", s);
69 exit(1);
72 static long
73 lstrtol(s, radix, pend)
74 char *s;
75 int radix;
76 char **pend;
78 int v;
79 int neg = 0;
80 long n = 0;
82 /* Skip leading white space. */
83 while (*s == ' ' || *s == '\t')
84 s++;
86 /* Check for a leading + or -. */
87 if (*s == '-')
89 neg = 1;
90 s++;
91 } else if (*s == '+')
93 s++;
96 /* Determine radix if caller does not specify. */
97 if (radix == 0)
99 radix = 10;
100 if (*s == '0')
102 switch (*++s)
104 case 'x':
105 radix = 16;
106 s++;
107 break;
108 default:
109 radix = 8;
110 break;
115 /* Parse the digits of the number. */
116 for (;;)
118 if (*s >= '0' && *s <= '9')
119 v = *s - '0';
120 else if (*s >= 'a' && *s <= 'f')
121 v = *s - 'a' + 10;
122 else if (*s >= 'A' && *s <= 'F')
123 v = *s - 'A' + 10;
124 else
125 break;
126 if (v >= radix)
127 break;
128 n = n * radix + v;
129 s++;
132 if (pend != NULL)
134 /* Skip trailing white space. */
135 while (*s == ' ' || *s == '\t')
136 s++;
137 *pend = s;
139 if (neg)
140 return (-n);
141 return (n);
145 #if !HAVE_STRCHR
146 char *
147 strchr(s, c)
148 char *s;
149 int c;
151 for ( ; *s != '\0'; s++)
152 if (*s == c)
153 return (s);
154 if (c == '\0')
155 return (s);
156 return (NULL);
158 #endif
161 main(argc, argv)
162 int argc;
163 char *argv[];
165 char *arg;
166 char *s;
167 int no_more_options;
169 no_more_options = 0;
170 while (--argc > 0)
172 arg = *++argv;
173 if (*arg != '-' || no_more_options)
174 break;
175 switch (*++arg)
177 case 'a':
178 quote_all = 1;
179 break;
180 case 'c':
181 closequote = *++arg;
182 break;
183 case 'd':
184 closequote = lstrtol(++arg, 0, &s);
185 if (s == arg)
186 pr_error("Missing number after -d");
187 break;
188 case 'e':
189 if (strcmp(++arg, "-") == 0)
190 meta_escape = "";
191 else
192 meta_escape = arg;
193 break;
194 case 'f':
195 meta_escape_buf[0] = lstrtol(++arg, 0, &s);
196 meta_escape = meta_escape_buf;
197 if (s == arg)
198 pr_error("Missing number after -f");
199 break;
200 case 'o':
201 openquote = *++arg;
202 break;
203 case 'p':
204 openquote = lstrtol(++arg, 0, &s);
205 if (s == arg)
206 pr_error("Missing number after -p");
207 break;
208 case 'm':
209 metachars[num_metachars++] = *++arg;
210 metachars[num_metachars] = '\0';
211 break;
212 case 'n':
213 metachars[num_metachars++] = lstrtol(++arg, 0, &s);
214 if (s == arg)
215 pr_error("Missing number after -n");
216 metachars[num_metachars] = '\0';
217 break;
218 case '?':
219 pr_usage();
220 return (0);
221 case '-':
222 if (*++arg == '\0')
224 no_more_options = 1;
225 break;
227 if (strcmp(arg, "version") == 0)
229 pr_version();
230 return (0);
232 if (strcmp(arg, "help") == 0)
234 pr_usage();
235 return (0);
237 pr_error("Invalid option after --");
238 default:
239 pr_error("Invalid option letter");
243 while (argc-- > 0)
245 int has_meta = 0;
246 arg = *argv++;
247 for (s = arg; *s != '\0'; s++)
249 if (strchr(metachars, *s) != NULL)
251 has_meta = 1;
252 break;
255 if (quote_all || (has_meta && strlen(meta_escape) == 0))
256 printf("%c%s%c", openquote, arg, closequote);
257 else
259 for (s = arg; *s != '\0'; s++)
261 if (strchr(metachars, *s) != NULL)
262 printf("%s", meta_escape);
263 printf("%c", *s);
266 if (argc > 0)
267 printf(" ");
268 else
269 printf("\n");
271 return (0);