2 * Copyright (C) 1984-2024 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;
41 static void pr_usage(void)
44 "usage: lessecho [-ox] [-cx] [-pn] [-dn] [-mx] [-nn] [-ex] [-fn] [-a] file ...\n");
47 static void pr_version(void)
53 for (p
= version
; *p
!= ' '; p
++)
56 for (p
++; *p
!= '$' && *p
!= ' ' && *p
!= '\0'; p
++)
62 static void pr_error(char *s
)
64 fprintf(stderr
, "%s\n", s
);
68 static long lstrtol(char *s
, char **pend
, int radix
)
74 /* Skip leading white space. */
75 while (*s
== ' ' || *s
== '\t')
78 /* Check for a leading + or -. */
88 /* Determine radix if caller does not specify. */
107 /* Parse the digits of the number. */
110 if (*s
>= '0' && *s
<= '9')
112 else if (*s
>= 'a' && *s
<= 'f')
114 else if (*s
>= 'A' && *s
<= 'F')
126 /* Skip trailing white space. */
127 while (*s
== ' ' || *s
== '\t')
136 static void add_metachar(char ch
)
138 if (num_metachars
+1 >= size_metachars
)
141 size_metachars
= (size_metachars
> 0) ? size_metachars
*2 : 16;
142 p
= (char *) malloc((size_t) size_metachars
);
144 pr_error("Cannot allocate memory");
146 if (metachars
!= NULL
)
148 strcpy(p
, metachars
);
153 metachars
[num_metachars
++] = ch
;
154 metachars
[num_metachars
] = '\0';
157 static int is_metachar(int ch
)
159 return (metachars
!= NULL
&& strchr(metachars
, ch
) != NULL
);
163 char * strchr(char *s
, char c
)
165 for ( ; *s
!= '\0'; s
++)
174 int main(int argc
, char *argv
[])
184 if (*arg
!= '-' || no_more_options
)
195 closequote
= (char) lstrtol(++arg
, &s
, 0);
197 pr_error("Missing number after -d");
200 if (strcmp(++arg
, "-") == 0)
206 meta_escape_buf
[0] = (char) lstrtol(++arg
, &s
, 0);
207 meta_escape_buf
[1] = '\0';
208 meta_escape
= meta_escape_buf
;
210 pr_error("Missing number after -f");
216 openquote
= (char) lstrtol(++arg
, &s
, 0);
218 pr_error("Missing number after -p");
221 add_metachar(*++arg
);
224 add_metachar((char) lstrtol(++arg
, &s
, 0));
226 pr_error("Missing number after -n");
237 if (strcmp(arg
, "version") == 0)
242 if (strcmp(arg
, "help") == 0)
247 pr_error("Invalid option after --");
250 pr_error("Invalid option letter");
258 for (s
= arg
; *s
!= '\0'; s
++)
266 if (quote_all
|| (has_meta
&& strlen(meta_escape
) == 0))
267 printf("%c%s%c", openquote
, arg
, closequote
);
270 for (s
= arg
; *s
!= '\0'; s
++)
273 printf("%s", meta_escape
);