comparison rvinterf/old/libg23/fmtfunc.c @ 945:4c601097d23f

rvinterf: retire libg23 to old subdir
author Mychaela Falconia <falcon@freecalypso.org>
date Wed, 24 May 2023 07:22:32 +0000
parents rvinterf/libg23/fmtfunc.c@e7502631a0f9
children
comparison
equal deleted inserted replaced
944:441004febe35 945:4c601097d23f
1 /*
2 * This libg23 module exports functions for formatting 3 different kinds
3 * of GPF packets into human-readable form: traces, system primitives
4 * and protocol stack primitives.
5 *
6 * GPF packets passed to these functions for decoding MUST have already
7 * been verified to be well-formed for their respective type.
8 */
9
10 #include <sys/types.h>
11 #include <ctype.h>
12 #include <stdio.h>
13 #include <string.h>
14 #include <strings.h>
15
16 static int
17 entity_name_well_formed(p)
18 char *p;
19 {
20 int i, len;
21
22 if (!isupper(p[0]))
23 return(0);
24 for (i = 0; i < 4; i++)
25 if (!isalnum(p[i]))
26 break;
27 len = i;
28 for (; i < 4; i++)
29 if (p[i] != ' ')
30 return(0);
31 return(len);
32 }
33
34 static void
35 print_entity_name(raw, outp)
36 char *raw, **outp;
37 {
38 int len;
39
40 len = entity_name_well_formed(raw);
41 if (len) {
42 sprintf(*outp, "%.*s", len, raw);
43 *outp += len;
44 } else {
45 sprintf(*outp, "\"%.4s\"", raw);
46 *outp += 6;
47 }
48 }
49
50 static void
51 print_common_hdr(rxpkt, outp, typestr)
52 u_char *rxpkt;
53 char **outp, *typestr;
54 {
55 sprintf(*outp, "GPF %s id=%02X ts=%02X%02X%02X%02X ", typestr,
56 rxpkt[1], rxpkt[7], rxpkt[6], rxpkt[5], rxpkt[4]);
57 *outp = index(*outp, '\0');
58 print_entity_name(rxpkt + 8, outp);
59 *(*outp)++ = '-';
60 *(*outp)++ = '>';
61 print_entity_name(rxpkt + 12, outp);
62 *(*outp)++ = ' ';
63 }
64
65 static void
66 format_text(rxpkt, rxpkt_len, start_off, outp)
67 u_char *rxpkt;
68 char *outp;
69 {
70 int i, c;
71
72 *outp++ = '\"';
73 for (i = start_off; i < rxpkt_len; i++) {
74 c = rxpkt[i];
75 if (c & 0x80) {
76 *outp++ = 'M';
77 *outp++ = '-';
78 c &= 0x7F;
79 }
80 if (c < 0x20) {
81 *outp++ = '^';
82 *outp++ = c + '@';
83 } else if (c == 0x7F) {
84 *outp++ = '^';
85 *outp++ = '?';
86 } else
87 *outp++ = c;
88 }
89 *outp++ = '\"';
90 *outp = '\0';
91 }
92
93 static void
94 format_compressed_trace(rxpkt, rxpkt_len, start_off, outp)
95 u_char *rxpkt;
96 char *outp;
97 {
98 int i;
99
100 i = start_off + 1;
101 sprintf(outp, "%d", rxpkt[i+1] << 8 | rxpkt[i]);
102 outp = index(outp, '\0');
103 i += 4;
104 for (; i < rxpkt_len; i++) {
105 sprintf(outp, " %02X", rxpkt[i]);
106 outp += 3;
107 }
108 *outp = '\0';
109 }
110
111 void
112 format_g23_trace(rxpkt, rxpkt_len, outbuf)
113 u_char *rxpkt;
114 char *outbuf;
115 {
116 char *outp = outbuf;
117 int i;
118
119 print_common_hdr(rxpkt, &outp, "trace");
120 i = 16;
121 if (rxpkt[i] < 0x20) {
122 sprintf(outp, "tc=%02X ", rxpkt[i]);
123 outp += 6;
124 i++;
125 }
126 if (rxpkt_len - i >= 5 && rxpkt[i] == '%' &&
127 !rxpkt[i+3] && !rxpkt[i+4])
128 format_compressed_trace(rxpkt, rxpkt_len, i, outp);
129 else
130 format_text(rxpkt, rxpkt_len, i, outp);
131 }
132
133 void
134 format_g23_sysprim(rxpkt, rxpkt_len, outbuf)
135 u_char *rxpkt;
136 char *outbuf;
137 {
138 char *outp = outbuf;
139 int i;
140
141 print_common_hdr(rxpkt, &outp, "sysprim");
142 format_text(rxpkt, rxpkt_len, 16, outp);
143 }
144
145 void
146 format_g23_psprim(rxpkt, rxpkt_len, outbuf)
147 u_char *rxpkt;
148 char *outbuf;
149 {
150 char *outp = outbuf;
151 int i;
152
153 print_common_hdr(rxpkt, &outp, "PSprim");
154 /* original destination */
155 *outp++ = '(';
156 print_entity_name(rxpkt + 16, &outp);
157 *outp++ = ')';
158 /* opcode */
159 sprintf(outp, " %02X%02X%02X%02X",
160 rxpkt[23], rxpkt[22], rxpkt[21], rxpkt[20]);
161 outp += 9;
162 for (i = 24; i < rxpkt_len; i++) {
163 sprintf(outp, " %02X", rxpkt[i]);
164 outp += 3;
165 }
166 *outp = '\0';
167 }