2 * Copyright (C) 1984-2014 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.
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.
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[64] = "";
38 static int num_metachars = 0;
44 "usage: lessecho [-ox] [-cx] [-pn] [-dn] [-mx] [-nn] [-ex] [-fn] [-a] file ...\n");
54 for (p = version; *p != ' '; p++)
57 for (p++; *p != '$' && *p != ' ' && *p != '\0'; p++)
67 fprintf(stderr, "%s\n", s);
72 lstrtol(s, radix, pend)
81 /* Skip leading white space. */
82 while (*s == ' ' || *s == '\t')
85 /* Check for a leading + or -. */
95 /* Determine radix if caller does not specify. */
114 /* Parse the digits of the number. */
117 if (*s >= '0' && *s <= '9')
119 else if (*s >= 'a' && *s <= 'f')
121 else if (*s >= 'A' && *s <= 'F')
133 /* Skip trailing white space. */
134 while (*s == ' ' || *s == '\t')
150 for ( ; *s != '\0'; s++)
172 if (*arg != '-' || no_more_options)
183 closequote = lstrtol(++arg, 0, &s);
185 pr_error("Missing number after -d");
188 if (strcmp(++arg, "-") == 0)
194 meta_escape_buf[0] = lstrtol(++arg, 0, &s);
195 meta_escape = meta_escape_buf;
197 pr_error("Missing number after -f");
203 openquote = lstrtol(++arg, 0, &s);
205 pr_error("Missing number after -p");
208 metachars[num_metachars++] = *++arg;
209 metachars[num_metachars] = '\0';
212 metachars[num_metachars++] = lstrtol(++arg, 0, &s);
214 pr_error("Missing number after -n");
215 metachars[num_metachars] = '\0';
226 if (strcmp(arg, "version") == 0)
231 if (strcmp(arg, "help") == 0)
236 pr_error("Invalid option after --");
238 pr_error("Invalid option letter");
246 for (s = arg; *s != '\0'; s++)
248 if (strchr(metachars, *s) != NULL)
254 if (quote_all || (has_meta && strlen(meta_escape) == 0))
255 printf("%c%s%c", openquote, arg, closequote);
258 for (s = arg; *s != '\0'; s++)
260 if (strchr(metachars, *s) != NULL)
261 printf("%s", meta_escape);