home *** CD-ROM | disk | FTP | other *** search
/ rtsi.com / 2014.01.www.rtsi.com.tar / www.rtsi.com / OS9 / OSK / CMDS / less_332.lzh / less_332 / lessecho.c < prev    next >
C/C++ Source or Header  |  1998-03-03  |  5KB  |  242 lines

  1. /*
  2.  * Copyright (c) 1984,1985,1989,1994,1995,1996  Mark Nudelman
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms, with or without
  6.  * modification, are permitted provided that the following conditions
  7.  * are met:
  8.  * 1. Redistributions of source code must retain the above copyright
  9.  *    notice, this list of conditions and the following disclaimer.
  10.  * 2. Redistributions in binary form must reproduce the above copyright
  11.  *    notice in the documentation and/or other materials provided with 
  12.  *    the distribution.
  13.  *
  14.  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY
  15.  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  16.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 
  17.  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE
  18.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
  19.  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 
  20.  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 
  21.  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
  22.  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 
  23.  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN 
  24.  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25.  */
  26.  
  27. /*
  28.  * lessecho [-ox] [-cx] [-pn] [-dn] [-a] file ...
  29.  * Simply echos its filename arguments on standard output.
  30.  * But any argument containing spaces is enclosed in quotes.
  31.  *
  32.  * -ox    Specifies "x" to be the open quote character.
  33.  * -cx    Specifies "x" to be the close quote character.
  34.  * -pn    Specifies "n" to be the open quote character, as an integer.
  35.  * -dn    Specifies "n" to be the close quote character, as an integer.
  36.  * -a    Specifies that all arguments are to be quoted.
  37.  *    The default is that only arguments containing spaces are quoted.
  38.  */
  39.  
  40. #include "less.h"
  41.  
  42. static char *version = "$Revision: 1.3 $";
  43.  
  44. static int quote_all = 0;
  45. static char openquote = '"';
  46. static char closequote = '"';
  47.  
  48.     static void
  49. pr_usage()
  50. {
  51.     fprintf(stderr,
  52.         "usage: lessecho [-ox] [-cx] [-pn] [-dn] [-a] file ...\n");
  53. }
  54.  
  55.     static void
  56. pr_version()
  57. {
  58.     char *p;
  59.     char buf[10];
  60.     char *pbuf = buf;
  61.  
  62.     for (p = version;  *p != ' ';  p++)
  63.         if (*p == '\0')
  64.             return;
  65.     for (p++;  *p != '$' && *p != ' ' && *p != '\0';  p++)
  66.         *pbuf++ = *p;
  67.     *pbuf = '\0';
  68.     printf("%s\n", buf);
  69. }
  70.  
  71.     static void
  72. pr_error(s)
  73.     char *s;
  74. {
  75.     fprintf(stderr, "%s\n", s);
  76.     exit(1);
  77. }
  78.  
  79.     static long
  80. lstrtol(s, radix, pend)
  81.     char *s;
  82.     int radix;
  83.     char **pend;
  84. {
  85.     int v;
  86.     int neg = 0;
  87.     long n = 0;
  88.  
  89.     /* Skip leading white space. */
  90.     while (*s == ' ' || *s == '\t')
  91.         s++;
  92.  
  93.     /* Check for a leading + or -. */
  94.     if (*s == '-')
  95.     {
  96.         neg = 1;
  97.         s++;
  98.     } else if (*s == '+')
  99.     {
  100.         s++;
  101.     }
  102.  
  103.     /* Determine radix if caller does not specify. */
  104.     if (radix == 0)
  105.     {
  106.         radix = 10;
  107.         if (*s == '0')
  108.         {
  109.             switch (*++s)
  110.             {
  111.             case 'x':
  112.                 radix = 16;
  113.                 s++;
  114.                 break;
  115.             default:
  116.                 radix = 8;
  117.                 break;
  118.             }
  119.         }
  120.     }
  121.  
  122.     /* Parse the digits of the number. */
  123.     for (;;)
  124.     {
  125.         if (*s >= '0' && *s <= '9')
  126.             v = *s - '0';
  127.         else if (*s >= 'a' && *s <= 'f')
  128.             v = *s - 'a' + 10;
  129.         else if (*s >= 'A' && *s <= 'F')
  130.             v = *s - 'A' + 10;
  131.         else
  132.             break;
  133.         if (v >= radix)
  134.             break;
  135.         n = n * radix + v;
  136.         s++;
  137.     }
  138.  
  139.     if (pend != NULL)
  140.     {
  141.         /* Skip trailing white space. */
  142.         while (*s == ' ' || *s == '\t')
  143.             s++;
  144.         *pend = s;
  145.     }
  146.     if (neg)
  147.         return (-n);
  148.     return (n);
  149. }
  150.  
  151.  
  152. #if !HAVE_STRCHR
  153.     char *
  154. strchr(s, c)
  155.     char *s;
  156.     int c;
  157. {
  158.     for ( ;  *s != '\0';  s++)
  159.         if (*s == c)
  160.             return (s);
  161.     if (c == '\0')
  162.         return (s);
  163.     return (NULL);
  164. }
  165. #endif
  166.  
  167.     int
  168. main(argc, argv)
  169.     int argc;
  170.     char *argv[];
  171. {
  172.     char *arg;
  173.     char *s;
  174.     int no_more_options;
  175.  
  176.     no_more_options = 0;
  177.     while (--argc > 0)
  178.     {
  179.         arg = *++argv;
  180.         if (*arg != '-' || no_more_options)
  181.             break;
  182.         switch (*++arg)
  183.         {
  184.         case 'a':
  185.             quote_all = 1;
  186.             break;
  187.         case 'o':
  188.             openquote = *++arg;
  189.             break;
  190.         case 'c':
  191.             closequote = *++arg;
  192.             break;
  193.         case 'p':
  194.             openquote = lstrtol(++arg, 0, &s);
  195.             if (s == arg)
  196.                 pr_error("Missing number after -O");
  197.             break;
  198.         case 'd':
  199.             closequote = lstrtol(++arg, 0, &s);
  200.             if (s == arg)
  201.                 pr_error("Missing number after -C");
  202.             break;
  203.         case '?':
  204.             pr_usage();
  205.             return (0);
  206.         case '-':
  207.             if (*++arg == '\0')
  208.             {
  209.                 no_more_options = 1;
  210.                 break;
  211.             }
  212.             if (strcmp(arg, "version") == 0)
  213.             {
  214.                 pr_version();
  215.                 return (0);
  216.             }
  217.             if (strcmp(arg, "help") == 0)
  218.             {
  219.                 pr_usage();
  220.                 return (0);
  221.             }
  222.             pr_error("Invalid option after --");
  223.         default:
  224.             pr_error("Invalid option letter");
  225.         }
  226.     }
  227.  
  228.     while (argc-- > 0)
  229.     {
  230.         arg = *argv++;
  231.         if (quote_all || strchr(arg, ' ') != NULL)
  232.             printf("%c%s%c", openquote, arg, closequote);
  233.         else
  234.             printf("%s", arg);
  235.         if (argc > 0)
  236.             printf(" ");
  237.         else
  238.             printf("\n");
  239.     }
  240.     return (0);
  241. }
  242.