2 * Copyright (C) 1984-2007 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.
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.
31 static char *version
= "$Revision: 1.11 $";
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;
45 "usage: lessecho [-ox] [-cx] [-pn] [-dn] [-mx] [-nn] [-ex] [-fn] [-a] file ...\n");
55 for (p
= version
; *p
!= ' '; p
++)
58 for (p
++; *p
!= '$' && *p
!= ' ' && *p
!= '\0'; p
++)
68 fprintf(stderr
, "%s\n", s
);
73 lstrtol(s
, radix
, pend
)
82 /* Skip leading white space. */
83 while (*s
== ' ' || *s
== '\t')
86 /* Check for a leading + or -. */
96 /* Determine radix if caller does not specify. */
115 /* Parse the digits of the number. */
118 if (*s
>= '0' && *s
<= '9')
120 else if (*s
>= 'a' && *s
<= 'f')
122 else if (*s
>= 'A' && *s
<= 'F')
134 /* Skip trailing white space. */
135 while (*s
== ' ' || *s
== '\t')
151 for ( ; *s
!= '\0'; s
++)
173 if (*arg
!= '-' || no_more_options
)
184 closequote
= lstrtol(++arg
, 0, &s
);
186 pr_error("Missing number after -d");
189 if (strcmp(++arg
, "-") == 0)
195 meta_escape_buf
[0] = lstrtol(++arg
, 0, &s
);
196 meta_escape
= meta_escape_buf
;
198 pr_error("Missing number after -f");
204 openquote
= lstrtol(++arg
, 0, &s
);
206 pr_error("Missing number after -p");
209 metachars
[num_metachars
++] = *++arg
;
210 metachars
[num_metachars
] = '\0';
213 metachars
[num_metachars
++] = lstrtol(++arg
, 0, &s
);
215 pr_error("Missing number after -n");
216 metachars
[num_metachars
] = '\0';
227 if (strcmp(arg
, "version") == 0)
232 if (strcmp(arg
, "help") == 0)
237 pr_error("Invalid option after --");
239 pr_error("Invalid option letter");
247 for (s
= arg
; *s
!= '\0'; s
++)
249 if (strchr(metachars
, *s
) != NULL
)
255 if (quote_all
|| (has_meta
&& strlen(meta_escape
) == 0))
256 printf("%c%s%c", openquote
, arg
, closequote
);
259 for (s
= arg
; *s
!= '\0'; s
++)
261 if (strchr(metachars
, *s
) != NULL
)
262 printf("%s", meta_escape
);