usr.sbin/makefs/hammer2: Remove redundant hammer2_inode_modify()
[dragonfly.git] / contrib / less / lessecho.c
blob5cd660db50ba8747ac34e94843474a2c82c35a46
1 /*
2 * Copyright (C) 1984-2022 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, see the README file.
8 */
12 * lessecho [-ox] [-cx] [-pn] [-dn] [-a] file ...
13 * Simply echos its filename arguments on standard output.
14 * But any argument containing spaces is enclosed in quotes.
16 * -ox Specifies "x" to be the open quote character.
17 * -cx Specifies "x" to be the close quote character.
18 * -pn Specifies "n" to be the open quote character, as an integer.
19 * -dn Specifies "n" to be the close quote character, as an integer.
20 * -mx Specifies "x" to be a metachar.
21 * -nn Specifies "n" to be a metachar, as an integer.
22 * -ex Specifies "x" to be the escape char for metachars.
23 * -fn Specifies "x" to be the escape char for metachars, as an integer.
24 * -a Specifies that all arguments are to be quoted.
25 * The default is that only arguments containing spaces are quoted.
28 #include "less.h"
30 static char *version = "$Revision: 1.15 $";
32 static int quote_all = 0;
33 static char openquote = '"';
34 static char closequote = '"';
35 static char *meta_escape = "\\";
36 static char meta_escape_buf[2];
37 static char* metachars = NULL;
38 static int num_metachars = 0;
39 static int size_metachars = 0;
41 static void
42 pr_usage(VOID_PARAM)
44 fprintf(stderr,
45 "usage: lessecho [-ox] [-cx] [-pn] [-dn] [-mx] [-nn] [-ex] [-fn] [-a] file ...\n");
48 static void
49 pr_version(VOID_PARAM)
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);
144 static void
145 add_metachar(ch)
146 int ch;
148 if (num_metachars+1 >= size_metachars)
150 char *p;
151 size_metachars = (size_metachars > 0) ? size_metachars*2 : 16;
152 p = (char *) malloc(size_metachars);
153 if (p == NULL)
154 pr_error("Cannot allocate memory");
156 if (metachars != NULL)
158 strcpy(p, metachars);
159 free(metachars);
161 metachars = p;
163 metachars[num_metachars++] = ch;
164 metachars[num_metachars] = '\0';
167 static int
168 is_metachar(ch)
169 int ch;
171 return (metachars != NULL && strchr(metachars, ch) != NULL);
174 #if !HAVE_STRCHR
175 char *
176 strchr(s, c)
177 char *s;
178 int c;
180 for ( ; *s != '\0'; s++)
181 if (*s == c)
182 return (s);
183 if (c == '\0')
184 return (s);
185 return (NULL);
187 #endif
190 main(argc, argv)
191 int argc;
192 char *argv[];
194 char *arg;
195 char *s;
196 int no_more_options;
198 no_more_options = 0;
199 while (--argc > 0)
201 arg = *++argv;
202 if (*arg != '-' || no_more_options)
203 break;
204 switch (*++arg)
206 case 'a':
207 quote_all = 1;
208 break;
209 case 'c':
210 closequote = *++arg;
211 break;
212 case 'd':
213 closequote = lstrtol(++arg, 0, &s);
214 if (s == arg)
215 pr_error("Missing number after -d");
216 break;
217 case 'e':
218 if (strcmp(++arg, "-") == 0)
219 meta_escape = "";
220 else
221 meta_escape = arg;
222 break;
223 case 'f':
224 meta_escape_buf[0] = lstrtol(++arg, 0, &s);
225 meta_escape_buf[1] = '\0';
226 meta_escape = meta_escape_buf;
227 if (s == arg)
228 pr_error("Missing number after -f");
229 break;
230 case 'o':
231 openquote = *++arg;
232 break;
233 case 'p':
234 openquote = lstrtol(++arg, 0, &s);
235 if (s == arg)
236 pr_error("Missing number after -p");
237 break;
238 case 'm':
239 add_metachar(*++arg);
240 break;
241 case 'n':
242 add_metachar(lstrtol(++arg, 0, &s));
243 if (s == arg)
244 pr_error("Missing number after -n");
245 break;
246 case '?':
247 pr_usage();
248 return (0);
249 case '-':
250 if (*++arg == '\0')
252 no_more_options = 1;
253 break;
255 if (strcmp(arg, "version") == 0)
257 pr_version();
258 return (0);
260 if (strcmp(arg, "help") == 0)
262 pr_usage();
263 return (0);
265 pr_error("Invalid option after --");
266 default:
267 pr_error("Invalid option letter");
271 while (argc-- > 0)
273 int has_meta = 0;
274 arg = *argv++;
275 for (s = arg; *s != '\0'; s++)
277 if (is_metachar(*s))
279 has_meta = 1;
280 break;
283 if (quote_all || (has_meta && strlen(meta_escape) == 0))
284 printf("%c%s%c", openquote, arg, closequote);
285 else
287 for (s = arg; *s != '\0'; s++)
289 if (is_metachar(*s))
290 printf("%s", meta_escape);
291 printf("%c", *s);
294 if (argc > 0)
295 printf(" ");
296 else
297 printf("\n");
299 return (0);