|
|
1.1 root 1: /*
2: * strdlen calculates the length of the displayed string.
3: * Should understand escape sequences and cntrl characters.
4: * for most terminals.
5: */
6: #include <string.h>
7:
8: #define ESCAPE '\033'
9: #define NORMAL 1
10: #define ST_ESCAPE 2
11: #define LOOK_ALPHA 3
12: #define LOOK_EQUAL 4
13: #define ALPHA_GRAVE 5
14:
15: int strdlen(string)
16: char *string;
17: {
18: register char *pcChar;
19: register int iStrLen = 0;
20: int iState;
21:
22: for (pcChar = string, iState = NORMAL; *pcChar; *pcChar++) {
23: switch(iState) {
24: case NORMAL:
25: if (*pcChar == ESCAPE) {
26: iState = ST_ESCAPE;
27: continue;
28: }
29: if (iscntrl((int) *pcChar))
30: continue;
31: break;
32: case ST_ESCAPE:
33: if (*pcChar == ESCAPE) {
34: iState = NORMAL;
35: break;
36: }
37: switch(*pcChar) {
38: case '[':
39: iState = LOOK_ALPHA;
40: continue;
41: case '>':
42: iState = LOOK_EQUAL;
43: continue;
44: case '`':
45: iState = NORMAL;
46: continue;
47: default:
48: if (isalpha((int) *pcChar))
49: iState = NORMAL;
50: else
51: if (isdigit((int) *pcChar))
52: iState = ALPHA_GRAVE;
53: else
54: iState = LOOK_ALPHA;
55: continue;
56: }
57: case ALPHA_GRAVE:
58: if (isalpha((int) *pcChar)) {
59: iState = NORMAL;
60: break;
61: }
62: continue;
63: case LOOK_ALPHA:
64: if (isalpha((int) *pcChar) || *pcChar == '`' ||
65: *pcChar == '@')
66: iState = NORMAL;
67: continue;
68: case LOOK_EQUAL:
69: iState = NORMAL;
70: if (*pcChar == '=')
71: continue;
72: break;
73: }
74: iStrLen++;
75: }
76: return(iStrLen);
77: }
78:
79: #if TEST
80: static void
81: checkit(s)
82: char *s;
83: {
84: static int testNo;
85: int i;
86:
87: testNo++;
88: if (3 != (i = strdlen(s)))
89: printf("%d = test %d\n", i, testNo);
90: }
91:
92: main()
93: {
94: /* Test ESCAPE */
95: checkit("\0338abc");
96: checkit("\033[12mabc");
97: checkit("\033>abc");
98: checkit("\033>=abc");
99: checkit("\033Dabc");
100: checkit("\033`abc");
101: checkit("\03312`abc");
102: checkit("\033[@abc");
103: checkit("\033[12ma\033[11mbc\033[11m");
104: /* Test CTRL */
105: checkit("\001ab\002c");
106: }
107: #endif
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.