diff options
Diffstat (limited to 'contrib/less/lessecho.c')
-rw-r--r-- | contrib/less/lessecho.c | 73 |
1 files changed, 42 insertions, 31 deletions
diff --git a/contrib/less/lessecho.c b/contrib/less/lessecho.c index bfb0f729a8b8..67bdf9c7130d 100644 --- a/contrib/less/lessecho.c +++ b/contrib/less/lessecho.c @@ -1,5 +1,5 @@ /* - * Copyright (C) 1984-2021 Mark Nudelman + * Copyright (C) 1984-2023 Mark Nudelman * * You may distribute under the terms of either the GNU General Public * License or the Less License, as specified in the README file. @@ -34,18 +34,17 @@ static char openquote = '"'; static char closequote = '"'; static char *meta_escape = "\\"; static char meta_escape_buf[2]; -static char metachars[64] = ""; +static char* metachars = NULL; static int num_metachars = 0; +static int size_metachars = 0; - static void -pr_usage(VOID_PARAM) +static void pr_usage(void) { fprintf(stderr, "usage: lessecho [-ox] [-cx] [-pn] [-dn] [-mx] [-nn] [-ex] [-fn] [-a] file ...\n"); } - static void -pr_version(VOID_PARAM) +static void pr_version(void) { char *p; char buf[10]; @@ -60,19 +59,13 @@ pr_version(VOID_PARAM) printf("%s\n", buf); } - static void -pr_error(s) - char *s; +static void pr_error(char *s) { fprintf(stderr, "%s\n", s); exit(1); } - static long -lstrtol(s, radix, pend) - char *s; - int radix; - char **pend; +static long lstrtol(char *s, char **pend, int radix) { int v; int neg = 0; @@ -140,12 +133,34 @@ lstrtol(s, radix, pend) return (n); } +static void add_metachar(int ch) +{ + 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'; +} + +static int is_metachar(int ch) +{ + return (metachars != NULL && strchr(metachars, ch) != NULL); +} #if !HAVE_STRCHR - char * -strchr(s, c) - char *s; - int c; +char * strchr(char *s, char c) { for ( ; *s != '\0'; s++) if (*s == c) @@ -156,10 +171,7 @@ strchr(s, c) } #endif - int -main(argc, argv) - int argc; - char *argv[]; +int main(int argc, char *argv[]) { char *arg; char *s; @@ -180,7 +192,7 @@ main(argc, argv) closequote = *++arg; break; case 'd': - closequote = lstrtol(++arg, 0, &s); + closequote = lstrtol(++arg, &s, 0); if (s == arg) pr_error("Missing number after -d"); break; @@ -191,7 +203,8 @@ main(argc, argv) meta_escape = arg; break; case 'f': - meta_escape_buf[0] = lstrtol(++arg, 0, &s); + meta_escape_buf[0] = lstrtol(++arg, &s, 0); + meta_escape_buf[1] = '\0'; meta_escape = meta_escape_buf; if (s == arg) pr_error("Missing number after -f"); @@ -200,19 +213,17 @@ main(argc, argv) openquote = *++arg; break; case 'p': - openquote = lstrtol(++arg, 0, &s); + openquote = lstrtol(++arg, &s, 0); if (s == arg) pr_error("Missing number after -p"); break; case 'm': - metachars[num_metachars++] = *++arg; - metachars[num_metachars] = '\0'; + add_metachar(*++arg); break; case 'n': - metachars[num_metachars++] = lstrtol(++arg, 0, &s); + add_metachar(lstrtol(++arg, &s, 0)); if (s == arg) pr_error("Missing number after -n"); - metachars[num_metachars] = '\0'; break; case '?': pr_usage(); @@ -245,7 +256,7 @@ main(argc, argv) arg = *argv++; for (s = arg; *s != '\0'; s++) { - if (strchr(metachars, *s) != NULL) + if (is_metachar(*s)) { has_meta = 1; break; @@ -257,7 +268,7 @@ main(argc, argv) { for (s = arg; *s != '\0'; s++) { - if (strchr(metachars, *s) != NULL) + if (is_metachar(*s)) printf("%s", meta_escape); printf("%c", *s); } |