1 /* $NetBSD: lessecho.c,v 1.6 2006/10/26 01:33:08 mrg Exp $ */
4 * Copyright (C) 1984-2004 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 about less, or for information on how to
10 * contact the author, see the README file.
15 * lessecho [-ox] [-cx] [-pn] [-dn] [-a] file ...
16 * Simply echos its filename arguments on standard output.
17 * But any argument containing spaces is enclosed in quotes.
19 * -ox Specifies "x" to be the open quote character.
20 * -cx Specifies "x" to be the close quote character.
21 * -pn Specifies "n" to be the open quote character, as an integer.
22 * -dn Specifies "n" to be the close quote character, as an integer.
23 * -mx Specifies "x" to be a metachar.
24 * -nn Specifies "n" to be a metachar, as an integer.
25 * -ex Specifies "x" to be the escape char for metachars.
26 * -fn Specifies "x" to be the escape char for metachars, as an integer.
27 * -a Specifies that all arguments are to be quoted.
28 * The default is that only arguments containing spaces are quoted.
33 static char *version
= "$Revision: 1.6 $";
35 static int quote_all
= 0;
36 static char openquote
= '"';
37 static char closequote
= '"';
38 static char *meta_escape
= "\\";
39 static char meta_escape_buf
[2];
40 static char metachars
[64] = "";
41 static int num_metachars
= 0;
43 static void pr_usage
__P((void));
44 static void pr_version
__P((void));
45 static void pr_error
__P((char *));
46 static long lstrtol
__P((char *, int, char **));
52 "usage: lessecho [-ox] [-cx] [-pn] [-dn] [-mx] [-nn] [-ex] [-fn] [-a] file ...\n");
62 for (p
= version
; *p
!= ' '; p
++)
65 for (p
++; *p
!= '$' && *p
!= ' ' && *p
!= '\0'; p
++)
75 fprintf(stderr
, "%s\n", s
);
80 lstrtol(s
, radix
, pend
)
89 /* Skip leading white space. */
90 while (*s
== ' ' || *s
== '\t')
93 /* Check for a leading + or -. */
103 /* Determine radix if caller does not specify. */
122 /* Parse the digits of the number. */
125 if (*s
>= '0' && *s
<= '9')
127 else if (*s
>= 'a' && *s
<= 'f')
129 else if (*s
>= 'A' && *s
<= 'F')
141 /* Skip trailing white space. */
142 while (*s
== ' ' || *s
== '\t')
158 for ( ; *s
!= '\0'; s
++)
180 if (*arg
!= '-' || no_more_options
)
191 closequote
= lstrtol(++arg
, 0, &s
);
193 pr_error("Missing number after -d");
196 if (strcmp(++arg
, "-") == 0)
202 meta_escape_buf
[0] = lstrtol(++arg
, 0, &s
);
203 meta_escape
= meta_escape_buf
;
205 pr_error("Missing number after -f");
211 openquote
= lstrtol(++arg
, 0, &s
);
213 pr_error("Missing number after -p");
216 metachars
[num_metachars
++] = *++arg
;
217 metachars
[num_metachars
] = '\0';
220 metachars
[num_metachars
++] = lstrtol(++arg
, 0, &s
);
222 pr_error("Missing number after -n");
223 metachars
[num_metachars
] = '\0';
234 if (strcmp(arg
, "version") == 0)
239 if (strcmp(arg
, "help") == 0)
244 pr_error("Invalid option after --");
246 pr_error("Invalid option letter");
254 for (s
= arg
; *s
!= '\0'; s
++)
256 if (strchr(metachars
, *s
) != NULL
)
262 if (quote_all
|| (has_meta
&& strlen(meta_escape
) == 0))
263 printf("%c%s%c", openquote
, arg
, closequote
);
266 for (s
= arg
; *s
!= '\0'; s
++)
268 if (strchr(metachars
, *s
) != NULL
)
269 printf("%s", meta_escape
);