$OpenBSD: vis.3,v 1.23 2005/08/28 19:51:27 millert Exp $ Copyright (c) 1989, 1991, 1993 The Regents of the University of California. All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the abov...
NAMEvis strvis strnvis strvisx - visually encode characters
SYNOPSISIn stdlib.h In vis.h (See libbsd(7) for include usage.) Ft char * Fn vis char *dst int c int flag int nextc Ft int Fn strvis char *dst const char *src int flag Ft int Fn strnvis char *dst const char *src size_t size int flag Ft int Fn strvisx char *dst const char *src size_t len int flag
DESCRIPTIONThe Fn vis function copies into Fa dst a string which represents the character Fa c . If Fa c needs no encoding, it is copied in unaltered. The string is NUL terminated and a pointer to the end of the string is returned. The maximum length of any encoding is four characters (not including the trailing NUL); thus, when encoding a set of characters into a buffer, the size of the buffer should be four times the number of characters encoded, plus one for the trailing NUL. The Fa flag parameter is used for altering the default range of characters considered for encoding and for altering the visual representation. The additional character, Fa nextc , is only used when selecting the VIS_CSTYLE encoding format (explained below).
The Fn strvis , Fn strnvis and Fn strvisx functions copy into Fa dst a visual representation of the string Fa src . The Fn strvis function encodes characters from Fa src up to the first NUL. The Fn strnvis function encodes characters from Fa src up to the first NUL or the end of Fa dst , as indicated by Fa size . The Fn strvisx function encodes exactly Fa len characters from Fa src (this is useful for encoding a block of data that may contain NULs). All three forms NUL terminate Fa dst , except for Fn strnvis when Fa size is zero, in which case Fa dst is not touched. For Fn strvis and Fn strvisx , the size of Fa dst must be four times the number of characters encoded from Fa src (plus one for the NUL). Fn strvis and Fn strvisx return the number of characters in Fa dst (not including the trailing NUL). Fn strnvis returns the length that Fa dst would become if it were of unlimited size (similar to snprintf(3) or strlcpy(3bsd)). This can be used to detect truncation but it also means that the return value of Fn strnvis must not be used without checking it against Fa size .
The encoding is a unique, invertible representation composed entirely of graphic characters; it can be decoded back into the original form using the unvis(3bsd) or strunvis(3bsd) functions.
There are two parameters that can be controlled: the range of characters that are encoded, and the type of representation used. By default, all non-graphic characters except space, tab, and newline are encoded (see isgraph(3)). The following flags alter this:
- Also encode magic characters recognized by glob(3) ( `*' `?' , `[' ) and `#'
- Also encode space.
- Also encode tab.
- Also encode newline.
- Synonym for VIS_SP | VIS_TAB | VIS_NL
- Only encode ``unsafe'' characters. These are control characters which may cause common terminals to perform unexpected functions. Currently this form allows space, tab, newline, backspace, bell, and return -- in addition to all graphic characters -- unencoded.
There are three forms of encoding. All forms use the backslash `\' character to introduce a special sequence; two backslashes are used to represent a real backslash. These are the visual formats:
to represent meta characters (characters with the 8th
bit set), and use a caret
to represent control characters (see
The following formats are used:
- Represents the control character `C' Spans characters `\000' through `\037' , and `\177' (as `\^?' ) .
- Represents character `C' with the 8th bit set. Spans characters `\241' through `\376'
- Represents control character `C' with the 8th bit set. Spans characters `\200' through `\237' , and `\377' (as `\M^?' ) .
- Represents ASCII space.
- Represents Meta-space.
Use C-style backslash sequences to represent standard non-printable
The following sequences are used to represent the indicated characters:
\a - BEL (007) \b - BS (010) \f - NP (014) \n - NL (012) \r - CR (015) \s - SP (040) \t - HT (011) \v - VT (013) \0 - NUL (000)
When using this format, the Fa nextc parameter is looked at to determine if a NUL character can be encoded as `\0' instead of `\000' If Fa nextc is an octal digit, the latter representation is used to avoid ambiguity.
- Use a three digit octal sequence. The form is `\ddd' where d represents an octal digit.
There is one additional flag, VIS_NOSLASH which inhibits the doubling of backslashes and the backslash before the default format (that is, control characters are represented by `^C' and meta characters as `M-C' ) . With this flag set, the encoding is ambiguous and non-invertible.