2012-07-23 23:31:53 +02:00
|
|
|
/*
|
2023-05-01 10:22:19 +02:00
|
|
|
* Copyright (C) 1984-2023 Mark Nudelman
|
2012-07-23 23:31:53 +02:00
|
|
|
*
|
|
|
|
* You may distribute under the terms of either the GNU General Public
|
|
|
|
* License or the Less License, as specified in the README file.
|
|
|
|
*
|
|
|
|
* For more information, see the README file.
|
|
|
|
*/
|
2000-05-22 11:53:22 +02:00
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* lessecho [-ox] [-cx] [-pn] [-dn] [-a] file ...
|
|
|
|
* Simply echos its filename arguments on standard output.
|
|
|
|
* But any argument containing spaces is enclosed in quotes.
|
|
|
|
*
|
2021-04-19 03:15:19 +02:00
|
|
|
* -ox Specifies "x" to be the open quote character.
|
|
|
|
* -cx Specifies "x" to be the close quote character.
|
|
|
|
* -pn Specifies "n" to be the open quote character, as an integer.
|
|
|
|
* -dn Specifies "n" to be the close quote character, as an integer.
|
2004-04-17 09:16:34 +02:00
|
|
|
* -mx Specifies "x" to be a metachar.
|
|
|
|
* -nn Specifies "n" to be a metachar, as an integer.
|
|
|
|
* -ex Specifies "x" to be the escape char for metachars.
|
|
|
|
* -fn Specifies "x" to be the escape char for metachars, as an integer.
|
2021-04-19 03:15:19 +02:00
|
|
|
* -a Specifies that all arguments are to be quoted.
|
|
|
|
* The default is that only arguments containing spaces are quoted.
|
2000-05-22 11:53:22 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "less.h"
|
|
|
|
|
2012-06-26 22:38:53 +02:00
|
|
|
static char *version = "$Revision: 1.15 $";
|
2000-05-22 11:53:22 +02:00
|
|
|
|
|
|
|
static int quote_all = 0;
|
|
|
|
static char openquote = '"';
|
|
|
|
static char closequote = '"';
|
2004-04-17 09:16:34 +02:00
|
|
|
static char *meta_escape = "\\";
|
|
|
|
static char meta_escape_buf[2];
|
2022-09-06 01:57:47 +02:00
|
|
|
static char* metachars = NULL;
|
2004-04-17 09:16:34 +02:00
|
|
|
static int num_metachars = 0;
|
2022-09-06 01:57:47 +02:00
|
|
|
static int size_metachars = 0;
|
2000-05-22 11:53:22 +02:00
|
|
|
|
2023-05-01 10:22:19 +02:00
|
|
|
static void pr_usage(void)
|
2000-05-22 11:53:22 +02:00
|
|
|
{
|
|
|
|
fprintf(stderr,
|
2004-04-17 09:16:34 +02:00
|
|
|
"usage: lessecho [-ox] [-cx] [-pn] [-dn] [-mx] [-nn] [-ex] [-fn] [-a] file ...\n");
|
2000-05-22 11:53:22 +02:00
|
|
|
}
|
|
|
|
|
2023-05-01 10:22:19 +02:00
|
|
|
static void pr_version(void)
|
2000-05-22 11:53:22 +02:00
|
|
|
{
|
|
|
|
char *p;
|
|
|
|
char buf[10];
|
|
|
|
char *pbuf = buf;
|
|
|
|
|
|
|
|
for (p = version; *p != ' '; p++)
|
|
|
|
if (*p == '\0')
|
|
|
|
return;
|
|
|
|
for (p++; *p != '$' && *p != ' ' && *p != '\0'; p++)
|
|
|
|
*pbuf++ = *p;
|
|
|
|
*pbuf = '\0';
|
|
|
|
printf("%s\n", buf);
|
|
|
|
}
|
|
|
|
|
2023-05-01 10:22:19 +02:00
|
|
|
static void pr_error(char *s)
|
2000-05-22 11:53:22 +02:00
|
|
|
{
|
|
|
|
fprintf(stderr, "%s\n", s);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2023-05-01 10:22:19 +02:00
|
|
|
static long lstrtol(char *s, char **pend, int radix)
|
2000-05-22 11:53:22 +02:00
|
|
|
{
|
|
|
|
int v;
|
|
|
|
int neg = 0;
|
|
|
|
long n = 0;
|
|
|
|
|
|
|
|
/* Skip leading white space. */
|
|
|
|
while (*s == ' ' || *s == '\t')
|
|
|
|
s++;
|
|
|
|
|
|
|
|
/* Check for a leading + or -. */
|
|
|
|
if (*s == '-')
|
|
|
|
{
|
|
|
|
neg = 1;
|
|
|
|
s++;
|
|
|
|
} else if (*s == '+')
|
|
|
|
{
|
|
|
|
s++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Determine radix if caller does not specify. */
|
|
|
|
if (radix == 0)
|
|
|
|
{
|
|
|
|
radix = 10;
|
|
|
|
if (*s == '0')
|
|
|
|
{
|
|
|
|
switch (*++s)
|
|
|
|
{
|
|
|
|
case 'x':
|
|
|
|
radix = 16;
|
|
|
|
s++;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
radix = 8;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Parse the digits of the number. */
|
|
|
|
for (;;)
|
|
|
|
{
|
|
|
|
if (*s >= '0' && *s <= '9')
|
|
|
|
v = *s - '0';
|
|
|
|
else if (*s >= 'a' && *s <= 'f')
|
|
|
|
v = *s - 'a' + 10;
|
|
|
|
else if (*s >= 'A' && *s <= 'F')
|
|
|
|
v = *s - 'A' + 10;
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
if (v >= radix)
|
|
|
|
break;
|
|
|
|
n = n * radix + v;
|
|
|
|
s++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pend != NULL)
|
|
|
|
{
|
|
|
|
/* Skip trailing white space. */
|
|
|
|
while (*s == ' ' || *s == '\t')
|
|
|
|
s++;
|
|
|
|
*pend = s;
|
|
|
|
}
|
|
|
|
if (neg)
|
|
|
|
return (-n);
|
|
|
|
return (n);
|
|
|
|
}
|
|
|
|
|
2023-05-01 10:22:19 +02:00
|
|
|
static void add_metachar(int ch)
|
2022-09-06 01:57:47 +02:00
|
|
|
{
|
|
|
|
if (num_metachars+1 >= size_metachars)
|
|
|
|
{
|
|
|
|
char *p;
|
|
|
|
size_metachars = (size_metachars > 0) ? size_metachars*2 : 16;
|
|
|
|
p = (char *) malloc(size_metachars);
|
|
|
|
if (p == NULL)
|
|
|
|
pr_error("Cannot allocate memory");
|
|
|
|
|
|
|
|
if (metachars != NULL)
|
|
|
|
{
|
|
|
|
strcpy(p, metachars);
|
|
|
|
free(metachars);
|
|
|
|
}
|
|
|
|
metachars = p;
|
|
|
|
}
|
|
|
|
metachars[num_metachars++] = ch;
|
|
|
|
metachars[num_metachars] = '\0';
|
|
|
|
}
|
|
|
|
|
2023-05-01 10:22:19 +02:00
|
|
|
static int is_metachar(int ch)
|
2022-09-06 01:57:47 +02:00
|
|
|
{
|
|
|
|
return (metachars != NULL && strchr(metachars, ch) != NULL);
|
|
|
|
}
|
2000-05-22 11:53:22 +02:00
|
|
|
|
|
|
|
#if !HAVE_STRCHR
|
2023-05-01 10:22:19 +02:00
|
|
|
char * strchr(char *s, char c)
|
2000-05-22 11:53:22 +02:00
|
|
|
{
|
|
|
|
for ( ; *s != '\0'; s++)
|
|
|
|
if (*s == c)
|
|
|
|
return (s);
|
|
|
|
if (c == '\0')
|
|
|
|
return (s);
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2023-05-01 10:22:19 +02:00
|
|
|
int main(int argc, char *argv[])
|
2000-05-22 11:53:22 +02:00
|
|
|
{
|
|
|
|
char *arg;
|
|
|
|
char *s;
|
|
|
|
int no_more_options;
|
|
|
|
|
|
|
|
no_more_options = 0;
|
|
|
|
while (--argc > 0)
|
|
|
|
{
|
|
|
|
arg = *++argv;
|
|
|
|
if (*arg != '-' || no_more_options)
|
|
|
|
break;
|
|
|
|
switch (*++arg)
|
|
|
|
{
|
|
|
|
case 'a':
|
|
|
|
quote_all = 1;
|
|
|
|
break;
|
|
|
|
case 'c':
|
|
|
|
closequote = *++arg;
|
|
|
|
break;
|
2004-04-17 09:16:34 +02:00
|
|
|
case 'd':
|
2023-05-01 10:22:19 +02:00
|
|
|
closequote = lstrtol(++arg, &s, 0);
|
2004-04-17 09:16:34 +02:00
|
|
|
if (s == arg)
|
|
|
|
pr_error("Missing number after -d");
|
|
|
|
break;
|
|
|
|
case 'e':
|
|
|
|
if (strcmp(++arg, "-") == 0)
|
|
|
|
meta_escape = "";
|
|
|
|
else
|
|
|
|
meta_escape = arg;
|
|
|
|
break;
|
|
|
|
case 'f':
|
2023-05-01 10:22:19 +02:00
|
|
|
meta_escape_buf[0] = lstrtol(++arg, &s, 0);
|
2022-09-06 01:57:47 +02:00
|
|
|
meta_escape_buf[1] = '\0';
|
2004-04-17 09:16:34 +02:00
|
|
|
meta_escape = meta_escape_buf;
|
|
|
|
if (s == arg)
|
|
|
|
pr_error("Missing number after -f");
|
|
|
|
break;
|
|
|
|
case 'o':
|
|
|
|
openquote = *++arg;
|
|
|
|
break;
|
2000-05-22 11:53:22 +02:00
|
|
|
case 'p':
|
2023-05-01 10:22:19 +02:00
|
|
|
openquote = lstrtol(++arg, &s, 0);
|
2000-05-22 11:53:22 +02:00
|
|
|
if (s == arg)
|
2004-04-17 09:16:34 +02:00
|
|
|
pr_error("Missing number after -p");
|
2000-05-22 11:53:22 +02:00
|
|
|
break;
|
2004-04-17 09:16:34 +02:00
|
|
|
case 'm':
|
2022-09-06 01:57:47 +02:00
|
|
|
add_metachar(*++arg);
|
2004-04-17 09:16:34 +02:00
|
|
|
break;
|
|
|
|
case 'n':
|
2023-05-01 10:22:19 +02:00
|
|
|
add_metachar(lstrtol(++arg, &s, 0));
|
2000-05-22 11:53:22 +02:00
|
|
|
if (s == arg)
|
2004-04-17 09:16:34 +02:00
|
|
|
pr_error("Missing number after -n");
|
2000-05-22 11:53:22 +02:00
|
|
|
break;
|
|
|
|
case '?':
|
|
|
|
pr_usage();
|
|
|
|
return (0);
|
|
|
|
case '-':
|
|
|
|
if (*++arg == '\0')
|
|
|
|
{
|
|
|
|
no_more_options = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (strcmp(arg, "version") == 0)
|
|
|
|
{
|
|
|
|
pr_version();
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
if (strcmp(arg, "help") == 0)
|
|
|
|
{
|
|
|
|
pr_usage();
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
pr_error("Invalid option after --");
|
|
|
|
default:
|
|
|
|
pr_error("Invalid option letter");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
while (argc-- > 0)
|
|
|
|
{
|
2004-04-17 09:16:34 +02:00
|
|
|
int has_meta = 0;
|
2000-05-22 11:53:22 +02:00
|
|
|
arg = *argv++;
|
2004-04-17 09:16:34 +02:00
|
|
|
for (s = arg; *s != '\0'; s++)
|
|
|
|
{
|
2022-09-06 01:57:47 +02:00
|
|
|
if (is_metachar(*s))
|
2004-04-17 09:16:34 +02:00
|
|
|
{
|
|
|
|
has_meta = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (quote_all || (has_meta && strlen(meta_escape) == 0))
|
2000-05-22 11:53:22 +02:00
|
|
|
printf("%c%s%c", openquote, arg, closequote);
|
2004-04-17 09:16:34 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
for (s = arg; *s != '\0'; s++)
|
|
|
|
{
|
2022-09-06 01:57:47 +02:00
|
|
|
if (is_metachar(*s))
|
2004-04-17 09:16:34 +02:00
|
|
|
printf("%s", meta_escape);
|
|
|
|
printf("%c", *s);
|
|
|
|
}
|
|
|
|
}
|
2000-05-22 11:53:22 +02:00
|
|
|
if (argc > 0)
|
|
|
|
printf(" ");
|
|
|
|
else
|
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
return (0);
|
|
|
|
}
|