1 /* $NetBSD: lessecho.c,v 1.4 2013/09/04 19:44:21 tron Exp $ */
4 * Copyright (C) 1984-2012 Mark Nudelman
6 * You may distribute under the terms of either the GNU General Public
7 * License or the Less License, as specified in the README file.
9 * For more information, see the README file.
14 * lessecho [-ox] [-cx] [-pn] [-dn] [-a] file ...
15 * Simply echos its filename arguments on standard output.
16 * But any argument containing spaces is enclosed in quotes.
18 * -ox Specifies "x" to be the open quote character.
19 * -cx Specifies "x" to be the close quote character.
20 * -pn Specifies "n" to be the open quote character, as an integer.
21 * -dn Specifies "n" to be the close quote character, as an integer.
22 * -mx Specifies "x" to be a metachar.
23 * -nn Specifies "n" to be a metachar, as an integer.
24 * -ex Specifies "x" to be the escape char for metachars.
25 * -fn Specifies "x" to be the escape char for metachars, as an integer.
26 * -a Specifies that all arguments are to be quoted.
27 * The default is that only arguments containing spaces are quoted.
32 static char *version
= "Revision: 1.3";
34 static int quote_all
= 0;
35 static char openquote
= '"';
36 static char closequote
= '"';
37 static char *meta_escape
= "\\";
38 static char meta_escape_buf
[2];
39 static char metachars
[64] = "";
40 static int num_metachars
= 0;
42 static void pr_usage
__P((void));
43 static void pr_version
__P((void));
44 static void pr_error
__P((char *));
45 static long lstrtol
__P((char *, int, char **));
51 "usage: lessecho [-ox] [-cx] [-pn] [-dn] [-mx] [-nn] [-ex] [-fn] [-a] file ...\n");
61 for (p
= version
; *p
!= ' '; p
++)
64 for (p
++; *p
!= '$' && *p
!= ' ' && *p
!= '\0'; p
++)
74 fprintf(stderr
, "%s\n", s
);
79 lstrtol(s
, radix
, pend
)
88 /* Skip leading white space. */
89 while (*s
== ' ' || *s
== '\t')
92 /* Check for a leading + or -. */
102 /* Determine radix if caller does not specify. */
121 /* Parse the digits of the number. */
124 if (*s
>= '0' && *s
<= '9')
126 else if (*s
>= 'a' && *s
<= 'f')
128 else if (*s
>= 'A' && *s
<= 'F')
140 /* Skip trailing white space. */
141 while (*s
== ' ' || *s
== '\t')
157 for ( ; *s
!= '\0'; s
++)
179 if (*arg
!= '-' || no_more_options
)
190 closequote
= lstrtol(++arg
, 0, &s
);
192 pr_error("Missing number after -d");
195 if (strcmp(++arg
, "-") == 0)
201 meta_escape_buf
[0] = lstrtol(++arg
, 0, &s
);
202 meta_escape
= meta_escape_buf
;
204 pr_error("Missing number after -f");
210 openquote
= lstrtol(++arg
, 0, &s
);
212 pr_error("Missing number after -p");
215 metachars
[num_metachars
++] = *++arg
;
216 metachars
[num_metachars
] = '\0';
219 metachars
[num_metachars
++] = lstrtol(++arg
, 0, &s
);
221 pr_error("Missing number after -n");
222 metachars
[num_metachars
] = '\0';
233 if (strcmp(arg
, "version") == 0)
238 if (strcmp(arg
, "help") == 0)
243 pr_error("Invalid option after --");
245 pr_error("Invalid option letter");
253 for (s
= arg
; *s
!= '\0'; s
++)
255 if (strchr(metachars
, *s
) != NULL
)
261 if (quote_all
|| (has_meta
&& strlen(meta_escape
) == 0))
262 printf("%c%s%c", openquote
, arg
, closequote
);
265 for (s
= arg
; *s
!= '\0'; s
++)
267 if (strchr(metachars
, *s
) != NULL
)
268 printf("%s", meta_escape
);