Back to home page

LXR

 
 

    


File indexing completed on 2025-05-11 08:24:19

0001 /*  $NetBSD: echo.c,v 1.12 2005/02/06 04:43:43 perry Exp $  */
0002 
0003 /*-
0004  * Copyright (c) 1991, 1993
0005  *  The Regents of the University of California.  All rights reserved.
0006  *
0007  * This code is derived from software contributed to Berkeley by
0008  * Kenneth Almquist.
0009  *
0010  * Redistribution and use in source and binary forms, with or without
0011  * modification, are permitted provided that the following conditions
0012  * are met:
0013  * 1. Redistributions of source code must retain the above copyright
0014  *    notice, this list of conditions and the following disclaimer.
0015  * 2. Redistributions in binary form must reproduce the above copyright
0016  *    notice, this list of conditions and the following disclaimer in the
0017  *    documentation and/or other materials provided with the distribution.
0018  * 3. Neither the name of the University nor the names of its contributors
0019  *    may be used to endorse or promote products derived from this software
0020  *    without specific prior written permission.
0021  *
0022  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
0023  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
0024  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
0025  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
0026  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
0027  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
0028  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
0029  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
0030  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
0031  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
0032  * SUCH DAMAGE.
0033  *
0034  *  @(#)echo.c  8.1 (Berkeley) 5/31/93
0035  */
0036 
0037 /*
0038  * Echo command.
0039  *
0040  * echo is steeped in tradition - several of them!
0041  * netbsd has supported 'echo [-n | -e] args' in spite of -e not being
0042  * documented anywhere.
0043  * Posix requires that -n be supported, output from strings containing
0044  * \ is implementation defined
0045  * The Single Unix Spec requires that \ escapes be treated as if -e
0046  * were set, but that -n not be treated as an option.
0047  * (ksh supports 'echo [-eEn] args', but not -- so that it is actually
0048  * impossible to actually output '-n')
0049  *
0050  * It is suggested that 'printf "%b" "string"' be used to get \ sequences
0051  * expanded.  printf is now a builtin of netbsd's sh and csh.
0052  */
0053 
0054 
0055 #ifdef HAVE_CONFIG_H
0056 #include "config.h"
0057 #endif
0058 
0059 #include <stdio.h>
0060 #include <unistd.h>
0061 #include <string.h>
0062 #include <errno.h>
0063 
0064 #include <rtems.h>
0065 #include <rtems/shell.h>
0066 #include "internal.h"
0067 
0068 
0069 
0070 static int rtems_shell_main_echo(
0071   int   argc,
0072   char *argv[]
0073 )
0074 {
0075   char **ap;
0076   char *p;
0077   char c;
0078   int count;
0079   int nflag = 0;
0080   int eflag = 0;
0081 
0082   ap = argv;
0083   if (argc)
0084     ap++;
0085 
0086   if ((p = *ap) != NULL) {
0087     if (!strcmp(p, "-n")) {
0088       nflag = 1;
0089       ap++;
0090     } else if (!strcmp(p, "-e")) {
0091       eflag = 1;
0092       ap++;
0093     }
0094   }
0095 
0096   while ((p = *ap++) != NULL) {
0097     while ((c = *p++) != '\0') {
0098       if (c == '\\' && eflag) {
0099         switch (*p++) {
0100         case 'a':  c = '\a';  break;  /* bell */
0101         case 'b':  c = '\b';  break;
0102         case 'c':  return 0;    /* exit */
0103         case 'e':  c =  033;  break;  /* escape */
0104         case 'f':  c = '\f';  break;
0105         case 'n':  c = '\n';  break;
0106         case 'r':  c = '\r';  break;
0107         case 't':  c = '\t';  break;
0108         case 'v':  c = '\v';  break;
0109         case '\\':  break;    /* c = '\\' */
0110         case '0':
0111           c = 0;
0112           count = 3;
0113           while (--count >= 0 && (unsigned)(*p - '0') < 8)
0114             c = (c << 3) + (*p++ - '0');
0115           break;
0116         default:
0117           /* Output the '/' and char following */
0118           p--;
0119           break;
0120         }
0121       }
0122       putchar(c);
0123     }
0124     if (*ap)
0125       putchar(' ');
0126   }
0127   if (! nflag)
0128     putchar('\n');
0129   return 0;
0130 }
0131 
0132 rtems_shell_cmd_t rtems_shell_ECHO_Command = {
0133   "echo",                        /* name */
0134   "echo [args]",                 /* usage */
0135   "misc",                        /* topic */
0136   rtems_shell_main_echo,         /* command */
0137   NULL,                          /* alias */
0138   NULL                           /* next */
0139 };