home *** CD-ROM | disk | FTP | other *** search
/ Aminet 18 / aminetcdnumber181997.iso / Aminet / dev / gcc / ixemulsrc.lha / ixemul / general / vis.c < prev    next >
C/C++ Source or Header  |  1996-12-11  |  4KB  |  158 lines

  1. /*-
  2.  * Copyright (c) 1989 The Regents of the University of California.
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms are permitted provided
  6.  * that: (1) source distributions retain this entire copyright notice and
  7.  * comment, and (2) distributions including binaries display the following
  8.  * acknowledgement:  ``This product includes software developed by the
  9.  * University of California, Berkeley and its contributors'' in the
  10.  * documentation or other materials provided with the distribution and in
  11.  * all advertising materials mentioning features or use of this software.
  12.  * Neither the name of the University nor the names of its contributors may
  13.  * be used to endorse or promote products derived from this software without
  14.  * specific prior written permission.
  15.  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
  16.  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
  17.  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  18.  */
  19.  
  20. #if defined(LIBC_SCCS) && !defined(lint)
  21. static char sccsid[] = "@(#)vis.c    5.3 (Berkeley) 6/26/90";
  22. #endif /* LIBC_SCCS and not lint */
  23.  
  24. #define _KERNEL
  25. #include "ixemul.h"
  26.  
  27. #include <ctype.h>
  28. #include <vis.h>
  29.  
  30. #define    isoctal(c)    (((u_char)(c)) >= '0' && ((u_char)(c)) <= '7')
  31.  
  32. /*
  33.  * vis - visually encode characters
  34.  */
  35. char *
  36. vis(char *dst, char c, int flag, char nextc)
  37. {
  38.     if ((isascii(c) && isgraph(c)) ||
  39.        ((flag & VIS_SP) == 0 && c == ' ') ||
  40.        ((flag & VIS_TAB) == 0 && c == '\t') ||
  41.        ((flag & VIS_NL) == 0 && c == '\n') ||
  42.        ((flag & VIS_SAFE) && (c == '\b' || c == '\007' || c == '\r'))) {
  43.         *dst++ = c;
  44.         if (c == '\\' && (flag & VIS_NOSLASH) == 0)
  45.             *dst++ = '\\';
  46.         *dst = '\0';
  47.         return (dst);
  48.     }
  49.  
  50.     if (flag & VIS_CSTYLE) {
  51.         switch(c) {
  52.         case '\n':
  53.             *dst++ = '\\';
  54.             *dst++ = 'n';
  55.             goto done;
  56.         case '\r':
  57.             *dst++ = '\\';
  58.             *dst++ = 'r';
  59.             goto done;
  60.         case '\b':
  61.             *dst++ = '\\';
  62.             *dst++ = 'b';
  63.             goto done;
  64.         case '\a':
  65.             *dst++ = '\\';
  66.             *dst++ = 'a';
  67.             goto done;
  68.         case '\v':
  69.             *dst++ = '\\';
  70.             *dst++ = 'v';
  71.             goto done;
  72.         case '\t':
  73.             *dst++ = '\\';
  74.             *dst++ = 't';
  75.             goto done;
  76.         case '\f':
  77.             *dst++ = '\\';
  78.             *dst++ = 'f';
  79.             goto done;
  80.         case ' ':
  81.             *dst++ = '\\';
  82.             *dst++ = 's';
  83.             goto done;
  84.         case '\0':
  85.             *dst++ = '\\';
  86.             *dst++ = '0';
  87.             if (isoctal(nextc)) {
  88.                 *dst++ = '0';
  89.                 *dst++ = '0';
  90.             }
  91.             goto done;
  92.         }
  93.     }
  94.     if (((c & 0177) == ' ') || (flag & VIS_OCTAL)) {    
  95.         *dst++ = '\\';
  96.         *dst++ = ((u_char)c >> 6 & 07) + '0';
  97.         *dst++ = ((u_char)c >> 3 & 07) + '0';
  98.         *dst++ = ((u_char)c & 07) + '0';
  99.         goto done;
  100.     }
  101.     if ((flag & VIS_NOSLASH) == 0)
  102.         *dst++ = '\\';
  103.     if (c & 0200) {
  104.         c &= 0177;
  105.         *dst++ = 'M';
  106.     }
  107.     if (iscntrl(c)) {
  108.         *dst++ = '^';
  109.         if (c == 0177)
  110.             *dst++ = '?';
  111.         else
  112.             *dst++ = c + '@';
  113.     } else {
  114.         *dst++ = '-';
  115.         *dst++ = c;
  116.     }
  117. done:
  118.     *dst = '\0';
  119.     return (dst);
  120. }
  121.  
  122. /*
  123.  * strvis, strvisx - visually encode characters from src into dst
  124.  *    
  125.  *    Dst must be 4 times the size of src to account for possible
  126.  *    expansion.  The length of dst, not including the trailing NULL,
  127.  *    is returned. 
  128.  *
  129.  *    Strvisx encodes exactly len bytes from src into dst.
  130.  *    This is useful for encoding a block of data.
  131.  */
  132. int
  133. strvis(char *dst, const char *src, int flag)
  134. {
  135.     register char c;
  136.     char *start = dst;
  137.  
  138.     for (; (c = *src); src++)
  139.         dst = vis(dst, c, flag, *(src+1));
  140.  
  141.     return (dst - start);
  142. }
  143.  
  144. int
  145. strvisx(char *dst, const char *src, size_t len, int flag)
  146. {
  147.     char *start = dst;
  148.  
  149.     while (len > 1) {
  150.         dst = vis(dst, *src, flag, *(src+1));
  151.         len--;
  152.     }
  153.     if (len)
  154.         dst = vis(dst, *src, flag, '\0');
  155.  
  156.     return (dst - start);
  157. }
  158.