home *** CD-ROM | disk | FTP | other *** search
/ High Voltage Shareware / high1.zip / high1 / DIR24 / BASH_112.ZIP / BASH-112.TAR / bash-1.12 / echo.c < prev    next >
C/C++ Source or Header  |  1993-02-18  |  3KB  |  127 lines

  1. /* echo.c, created from echo.def. */
  2. #include <stdio.h>
  3. #include "../shell.h"
  4.  
  5.  
  6.  
  7. #if defined (V9_ECHO)
  8. #  if defined (USG)
  9. #    define VALID_ECHO_OPTIONS "neE"
  10. #  else
  11. #    define VALID_ECHO_OPTIONS "ne"
  12. #  endif /* !USG */
  13. #else /* !V9_ECHO */
  14. #  define VALID_ECHO_OPTIONS "n"
  15. #endif /* !V9_ECHO */
  16.  
  17. /* Print the words in LIST to standard output.  If the first word is
  18.    `-n', then don't print a trailing newline.  We also support the
  19.    echo syntax from Version 9 unix systems. */
  20. echo_builtin (list)
  21.      WORD_LIST *list;
  22. {
  23.   int display_return = 1, do_v9 = 0;
  24.  
  25. /* System V machines already have a /bin/sh with a v9 behaviour.  We
  26.    give Bash the identical behaviour for these machines so that the
  27.    existing system shells won't barf. */
  28. #if defined (V9_ECHO) && defined (USG)
  29.     do_v9 = 1;
  30. #endif
  31.  
  32.   while (list && list->word->word[0] == '-')
  33.     {
  34.       register char *temp;
  35.       register int i;
  36.  
  37.       /* If it appears that we are handling options, then make sure that
  38.      all of the options specified are actually valid.  Otherwise, the
  39.      string should just be echoed. */
  40.       temp = &(list->word->word[1]);
  41.  
  42.       for (i = 0; temp[i]; i++)
  43.     {
  44.       if (rindex (VALID_ECHO_OPTIONS, temp[i]) == 0)
  45.         goto just_echo;
  46.     }
  47.  
  48.       if (!*temp)
  49.     goto just_echo;
  50.  
  51.       /* All of the options in TEMP are valid options to ECHO.
  52.      Handle them. */
  53.       while (*temp)
  54.     {
  55.       if (*temp == 'n')
  56.         display_return = 0;
  57. #if defined (V9_ECHO)
  58.       else if (*temp == 'e')
  59.         do_v9 = 1;
  60. #if defined (USG)
  61.       else if (*temp == 'E')
  62.         do_v9 = 0;
  63. #endif /* USG */
  64. #endif /* V9_ECHO */
  65.       else
  66.         goto just_echo;
  67.  
  68.       temp++;
  69.     }
  70.       list = list->next;
  71.     }
  72.  
  73. just_echo:
  74.  
  75.   if (list)
  76.     {
  77. #if defined (V9_ECHO)
  78.       if (do_v9)
  79.     {
  80.       while (list)
  81.         {
  82.           register char *s = list->word->word;
  83.           register int c;
  84.  
  85.           while (c = *s++)
  86.         {
  87.           if (c == '\\' && *s)
  88.             {
  89.               switch (c = *s++)
  90.             {
  91.             case 'a': c = '\007'; break;
  92.             case 'b': c = '\b'; break;
  93.             case 'c': display_return = 0; continue;
  94.             case 'f': c = '\f'; break;
  95.             case 'n': c = '\n'; break;
  96.             case 'r': c = '\r'; break;
  97.             case 't': c = '\t'; break;
  98.             case 'v': c = (int) 0x0B; break;
  99.             case '0': case '1': case '2': case '3':
  100.             case '4': case '5': case '6': case '7':
  101.               c -= '0';
  102.               if (*s >= '0' && *s <= '7')
  103.                 c = c * 8 + (*s++ - '0');
  104.               if (*s >= '0' && *s <= '7')
  105.                 c = c * 8 + (*s++ - '0');
  106.               break;
  107.             case '\\': break;
  108.             default:  putchar ('\\'); break;
  109.             }
  110.             }
  111.           putchar(c);
  112.         }
  113.           list = list->next;
  114.           if (list)
  115.         putchar(' ');
  116.         }
  117.     }
  118.       else
  119. #endif /* V9_ECHO */
  120.     print_word_list (list, " ");
  121.     }
  122.   if (display_return)
  123.     printf ("\n");
  124.   fflush (stdout);
  125.   return (EXECUTION_SUCCESS);
  126. }
  127.