lessecho.c revision 60786
160786Sps/* 260786Sps * Copyright (C) 1984-2000 Mark Nudelman 360786Sps * 460786Sps * You may distribute under the terms of either the GNU General Public 560786Sps * License or the Less License, as specified in the README file. 660786Sps * 760786Sps * For more information about less, or for information on how to 860786Sps * contact the author, see the README file. 960786Sps */ 1060786Sps 1160786Sps 1260786Sps/* 1360786Sps * lessecho [-ox] [-cx] [-pn] [-dn] [-a] file ... 1460786Sps * Simply echos its filename arguments on standard output. 1560786Sps * But any argument containing spaces is enclosed in quotes. 1660786Sps * 1760786Sps * -ox Specifies "x" to be the open quote character. 1860786Sps * -cx Specifies "x" to be the close quote character. 1960786Sps * -pn Specifies "n" to be the open quote character, as an integer. 2060786Sps * -dn Specifies "n" to be the close quote character, as an integer. 2160786Sps * -a Specifies that all arguments are to be quoted. 2260786Sps * The default is that only arguments containing spaces are quoted. 2360786Sps */ 2460786Sps 2560786Sps#include "less.h" 2660786Sps 2760786Spsstatic char *version = "$Revision: 1.6 $"; 2860786Sps 2960786Spsstatic int quote_all = 0; 3060786Spsstatic char openquote = '"'; 3160786Spsstatic char closequote = '"'; 3260786Sps 3360786Sps static void 3460786Spspr_usage() 3560786Sps{ 3660786Sps fprintf(stderr, 3760786Sps "usage: lessecho [-ox] [-cx] [-pn] [-dn] [-a] file ...\n"); 3860786Sps} 3960786Sps 4060786Sps static void 4160786Spspr_version() 4260786Sps{ 4360786Sps char *p; 4460786Sps char buf[10]; 4560786Sps char *pbuf = buf; 4660786Sps 4760786Sps for (p = version; *p != ' '; p++) 4860786Sps if (*p == '\0') 4960786Sps return; 5060786Sps for (p++; *p != '$' && *p != ' ' && *p != '\0'; p++) 5160786Sps *pbuf++ = *p; 5260786Sps *pbuf = '\0'; 5360786Sps printf("%s\n", buf); 5460786Sps} 5560786Sps 5660786Sps static void 5760786Spspr_error(s) 5860786Sps char *s; 5960786Sps{ 6060786Sps fprintf(stderr, "%s\n", s); 6160786Sps exit(1); 6260786Sps} 6360786Sps 6460786Sps static long 6560786Spslstrtol(s, radix, pend) 6660786Sps char *s; 6760786Sps int radix; 6860786Sps char **pend; 6960786Sps{ 7060786Sps int v; 7160786Sps int neg = 0; 7260786Sps long n = 0; 7360786Sps 7460786Sps /* Skip leading white space. */ 7560786Sps while (*s == ' ' || *s == '\t') 7660786Sps s++; 7760786Sps 7860786Sps /* Check for a leading + or -. */ 7960786Sps if (*s == '-') 8060786Sps { 8160786Sps neg = 1; 8260786Sps s++; 8360786Sps } else if (*s == '+') 8460786Sps { 8560786Sps s++; 8660786Sps } 8760786Sps 8860786Sps /* Determine radix if caller does not specify. */ 8960786Sps if (radix == 0) 9060786Sps { 9160786Sps radix = 10; 9260786Sps if (*s == '0') 9360786Sps { 9460786Sps switch (*++s) 9560786Sps { 9660786Sps case 'x': 9760786Sps radix = 16; 9860786Sps s++; 9960786Sps break; 10060786Sps default: 10160786Sps radix = 8; 10260786Sps break; 10360786Sps } 10460786Sps } 10560786Sps } 10660786Sps 10760786Sps /* Parse the digits of the number. */ 10860786Sps for (;;) 10960786Sps { 11060786Sps if (*s >= '0' && *s <= '9') 11160786Sps v = *s - '0'; 11260786Sps else if (*s >= 'a' && *s <= 'f') 11360786Sps v = *s - 'a' + 10; 11460786Sps else if (*s >= 'A' && *s <= 'F') 11560786Sps v = *s - 'A' + 10; 11660786Sps else 11760786Sps break; 11860786Sps if (v >= radix) 11960786Sps break; 12060786Sps n = n * radix + v; 12160786Sps s++; 12260786Sps } 12360786Sps 12460786Sps if (pend != NULL) 12560786Sps { 12660786Sps /* Skip trailing white space. */ 12760786Sps while (*s == ' ' || *s == '\t') 12860786Sps s++; 12960786Sps *pend = s; 13060786Sps } 13160786Sps if (neg) 13260786Sps return (-n); 13360786Sps return (n); 13460786Sps} 13560786Sps 13660786Sps 13760786Sps#if !HAVE_STRCHR 13860786Sps char * 13960786Spsstrchr(s, c) 14060786Sps char *s; 14160786Sps int c; 14260786Sps{ 14360786Sps for ( ; *s != '\0'; s++) 14460786Sps if (*s == c) 14560786Sps return (s); 14660786Sps if (c == '\0') 14760786Sps return (s); 14860786Sps return (NULL); 14960786Sps} 15060786Sps#endif 15160786Sps 15260786Sps int 15360786Spsmain(argc, argv) 15460786Sps int argc; 15560786Sps char *argv[]; 15660786Sps{ 15760786Sps char *arg; 15860786Sps char *s; 15960786Sps int no_more_options; 16060786Sps 16160786Sps no_more_options = 0; 16260786Sps while (--argc > 0) 16360786Sps { 16460786Sps arg = *++argv; 16560786Sps if (*arg != '-' || no_more_options) 16660786Sps break; 16760786Sps switch (*++arg) 16860786Sps { 16960786Sps case 'a': 17060786Sps quote_all = 1; 17160786Sps break; 17260786Sps case 'o': 17360786Sps openquote = *++arg; 17460786Sps break; 17560786Sps case 'c': 17660786Sps closequote = *++arg; 17760786Sps break; 17860786Sps case 'p': 17960786Sps openquote = lstrtol(++arg, 0, &s); 18060786Sps if (s == arg) 18160786Sps pr_error("Missing number after -O"); 18260786Sps break; 18360786Sps case 'd': 18460786Sps closequote = lstrtol(++arg, 0, &s); 18560786Sps if (s == arg) 18660786Sps pr_error("Missing number after -C"); 18760786Sps break; 18860786Sps case '?': 18960786Sps pr_usage(); 19060786Sps return (0); 19160786Sps case '-': 19260786Sps if (*++arg == '\0') 19360786Sps { 19460786Sps no_more_options = 1; 19560786Sps break; 19660786Sps } 19760786Sps if (strcmp(arg, "version") == 0) 19860786Sps { 19960786Sps pr_version(); 20060786Sps return (0); 20160786Sps } 20260786Sps if (strcmp(arg, "help") == 0) 20360786Sps { 20460786Sps pr_usage(); 20560786Sps return (0); 20660786Sps } 20760786Sps pr_error("Invalid option after --"); 20860786Sps default: 20960786Sps pr_error("Invalid option letter"); 21060786Sps } 21160786Sps } 21260786Sps 21360786Sps while (argc-- > 0) 21460786Sps { 21560786Sps arg = *argv++; 21660786Sps if (quote_all || strchr(arg, ' ') != NULL) 21760786Sps printf("%c%s%c", openquote, arg, closequote); 21860786Sps else 21960786Sps printf("%s", arg); 22060786Sps if (argc > 0) 22160786Sps printf(" "); 22260786Sps else 22360786Sps printf("\n"); 22460786Sps } 22560786Sps return (0); 22660786Sps} 227