home *** CD-ROM | disk | FTP | other *** search
/ The CDPD Public Domain Collection for CDTV 3 / CDPDIII.bin / pd / programming / gnuc / gen_library / rcs / vis.c,v < prev    next >
Encoding:
Text File  |  1992-07-04  |  3.7 KB  |  183 lines

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