2 * Copyright (C) 1984-2015 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++)
66 fprintf(stderr, "%s\n", s);
71 lstrtol(char *s, int radix, char **pend)
77 /* Skip leading white space. */
78 while (*s == ' ' || *s == '\t')
81 /* Check for a leading + or -. */
91 /* Determine radix if caller does not specify. */
110 /* Parse the digits of the number. */
113 if (*s >= '0' && *s <= '9')
115 else if (*s >= 'a' && *s <= 'f')
117 else if (*s >= 'A' && *s <= 'F')
129 /* Skip trailing white space. */
130 while (*s == ' ' || *s == '\t')
146 for ( ; *s != '\0'; s++)
168 if (*arg != '-' || no_more_options)
179 closequote = lstrtol(++arg, 0, &s);
181 pr_error("Missing number after -d");
184 if (strcmp(++arg, "-") == 0)
190 meta_escape_buf[0] = lstrtol(++arg, 0, &s);
191 meta_escape = meta_escape_buf;
193 pr_error("Missing number after -f");
199 openquote = lstrtol(++arg, 0, &s);
201 pr_error("Missing number after -p");
204 metachars[num_metachars++] = *++arg;
205 metachars[num_metachars] = '\0';
208 metachars[num_metachars++] = lstrtol(++arg, 0, &s);
210 pr_error("Missing number after -n");
211 metachars[num_metachars] = '\0';
222 if (strcmp(arg, "version") == 0)
227 if (strcmp(arg, "help") == 0)
232 pr_error("Invalid option after --");
234 pr_error("Invalid option letter");
242 for (s = arg; *s != '\0'; s++)
244 if (strchr(metachars, *s) != NULL)
250 if (quote_all || (has_meta && strlen(meta_escape) == 0))
251 printf("%c%s%c", openquote, arg, closequote);
254 for (s = arg; *s != '\0'; s++)
256 if (strchr(metachars, *s) != NULL)
257 printf("%s", meta_escape);