2 * Copyright (C) 1984-2022 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
= NULL
;
38 static int num_metachars
= 0;
39 static int size_metachars
= 0;
45 "usage: lessecho [-ox] [-cx] [-pn] [-dn] [-mx] [-nn] [-ex] [-fn] [-a] file ...\n");
49 pr_version(VOID_PARAM
)
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')
148 if (num_metachars
+1 >= size_metachars
)
151 size_metachars
= (size_metachars
> 0) ? size_metachars
*2 : 16;
152 p
= (char *) malloc(size_metachars
);
154 pr_error("Cannot allocate memory");
156 if (metachars
!= NULL
)
158 strcpy(p
, metachars
);
163 metachars
[num_metachars
++] = ch
;
164 metachars
[num_metachars
] = '\0';
171 return (metachars
!= NULL
&& strchr(metachars
, ch
) != NULL
);
180 for ( ; *s
!= '\0'; s
++)
202 if (*arg
!= '-' || no_more_options
)
213 closequote
= lstrtol(++arg
, 0, &s
);
215 pr_error("Missing number after -d");
218 if (strcmp(++arg
, "-") == 0)
224 meta_escape_buf
[0] = lstrtol(++arg
, 0, &s
);
225 meta_escape_buf
[1] = '\0';
226 meta_escape
= meta_escape_buf
;
228 pr_error("Missing number after -f");
234 openquote
= lstrtol(++arg
, 0, &s
);
236 pr_error("Missing number after -p");
239 add_metachar(*++arg
);
242 add_metachar(lstrtol(++arg
, 0, &s
));
244 pr_error("Missing number after -n");
255 if (strcmp(arg
, "version") == 0)
260 if (strcmp(arg
, "help") == 0)
265 pr_error("Invalid option after --");
267 pr_error("Invalid option letter");
275 for (s
= arg
; *s
!= '\0'; s
++)
283 if (quote_all
|| (has_meta
&& strlen(meta_escape
) == 0))
284 printf("%c%s%c", openquote
, arg
, closequote
);
287 for (s
= arg
; *s
!= '\0'; s
++)
290 printf("%s", meta_escape
);