FreeCalypso > hg > freecalypso-tools
annotate rvinterf/libprint/back_esc.c @ 1009:4a153059abbb
doc/DUART28-boot-control: update for fc-linux-patch and fc-usbser-tools
author | Mychaela Falconia <falcon@freecalypso.org> |
---|---|
date | Tue, 12 Dec 2023 06:57:11 +0000 |
parents | 6a0aa8d36d06 |
children |
rev | line source |
---|---|
926
6a0aa8d36d06
rvinterf backslash escape: introduce libprint
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
1 /* |
6a0aa8d36d06
rvinterf backslash escape: introduce libprint
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
2 * The function implemented in this module prints a potentially-dirty string |
6a0aa8d36d06
rvinterf backslash escape: introduce libprint
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
3 * into a buffer with C-style backslash escapes, including doubling of any |
6a0aa8d36d06
rvinterf backslash escape: introduce libprint
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
4 * already-present backslashes. The output buffer must be sized for up to |
6a0aa8d36d06
rvinterf backslash escape: introduce libprint
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
5 * 4x expansion, plus the terminating NUL. |
6a0aa8d36d06
rvinterf backslash escape: introduce libprint
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
6 */ |
6a0aa8d36d06
rvinterf backslash escape: introduce libprint
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
7 |
6a0aa8d36d06
rvinterf backslash escape: introduce libprint
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
8 #include <sys/types.h> |
6a0aa8d36d06
rvinterf backslash escape: introduce libprint
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
9 #include <ctype.h> |
6a0aa8d36d06
rvinterf backslash escape: introduce libprint
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
10 #include <stdio.h> |
6a0aa8d36d06
rvinterf backslash escape: introduce libprint
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
11 |
6a0aa8d36d06
rvinterf backslash escape: introduce libprint
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
12 void |
6a0aa8d36d06
rvinterf backslash escape: introduce libprint
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
13 safe_print_trace(src, srclen, dest) |
6a0aa8d36d06
rvinterf backslash escape: introduce libprint
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
14 u_char *src; |
6a0aa8d36d06
rvinterf backslash escape: introduce libprint
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
15 char *dest; |
6a0aa8d36d06
rvinterf backslash escape: introduce libprint
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
16 { |
6a0aa8d36d06
rvinterf backslash escape: introduce libprint
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
17 int i, c; |
6a0aa8d36d06
rvinterf backslash escape: introduce libprint
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
18 char *dp; |
6a0aa8d36d06
rvinterf backslash escape: introduce libprint
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
19 |
6a0aa8d36d06
rvinterf backslash escape: introduce libprint
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
20 dp = dest; |
6a0aa8d36d06
rvinterf backslash escape: introduce libprint
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
21 for (i = 0; i < srclen; i++) { |
6a0aa8d36d06
rvinterf backslash escape: introduce libprint
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
22 c = src[i]; |
6a0aa8d36d06
rvinterf backslash escape: introduce libprint
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
23 switch (c) { |
6a0aa8d36d06
rvinterf backslash escape: introduce libprint
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
24 case '\\': |
6a0aa8d36d06
rvinterf backslash escape: introduce libprint
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
25 *dp++ = '\\'; |
6a0aa8d36d06
rvinterf backslash escape: introduce libprint
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
26 *dp++ = '\\'; |
6a0aa8d36d06
rvinterf backslash escape: introduce libprint
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
27 continue; |
6a0aa8d36d06
rvinterf backslash escape: introduce libprint
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
28 case '\r': |
6a0aa8d36d06
rvinterf backslash escape: introduce libprint
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
29 *dp++ = '\\'; |
6a0aa8d36d06
rvinterf backslash escape: introduce libprint
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
30 *dp++ = 'r'; |
6a0aa8d36d06
rvinterf backslash escape: introduce libprint
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
31 continue; |
6a0aa8d36d06
rvinterf backslash escape: introduce libprint
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
32 case '\n': |
6a0aa8d36d06
rvinterf backslash escape: introduce libprint
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
33 *dp++ = '\\'; |
6a0aa8d36d06
rvinterf backslash escape: introduce libprint
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
34 *dp++ = 'n'; |
6a0aa8d36d06
rvinterf backslash escape: introduce libprint
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
35 continue; |
6a0aa8d36d06
rvinterf backslash escape: introduce libprint
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
36 } |
6a0aa8d36d06
rvinterf backslash escape: introduce libprint
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
37 if (c >= ' ' && c <= '~') |
6a0aa8d36d06
rvinterf backslash escape: introduce libprint
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
38 *dp++ = c; |
6a0aa8d36d06
rvinterf backslash escape: introduce libprint
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
39 else if (c <= 7 && (i+1 == srclen || !isdigit(src[i+1]))) { |
6a0aa8d36d06
rvinterf backslash escape: introduce libprint
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
40 sprintf(dp, "\\%d", c); |
6a0aa8d36d06
rvinterf backslash escape: introduce libprint
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
41 dp += 2; |
6a0aa8d36d06
rvinterf backslash escape: introduce libprint
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
42 } else { |
6a0aa8d36d06
rvinterf backslash escape: introduce libprint
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
43 sprintf(dp, "\\x%02X", c); |
6a0aa8d36d06
rvinterf backslash escape: introduce libprint
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
44 dp += 4; |
6a0aa8d36d06
rvinterf backslash escape: introduce libprint
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
45 } |
6a0aa8d36d06
rvinterf backslash escape: introduce libprint
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
46 } |
6a0aa8d36d06
rvinterf backslash escape: introduce libprint
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
47 *dp = '\0'; |
6a0aa8d36d06
rvinterf backslash escape: introduce libprint
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
48 } |