-
Notifications
You must be signed in to change notification settings - Fork 3
/
tgoto.c
251 lines (237 loc) · 6.26 KB
/
tgoto.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
/************************************************************************
* *
* Copyright (c) 1982, Fred Fish *
* All Rights Reserved *
* *
* This software and/or documentation is released for public *
* distribution for personal, non-commercial use only. *
* Limited rights to use, modify, and redistribute are hereby *
* granted for non-commercial purposes, provided that all *
* copyright notices remain intact and all changes are clearly *
* documented. The author makes no warranty of any kind with *
* respect to this product and explicitly disclaims any implied *
* warranties of merchantability or fitness for any particular *
* purpose. *
* *
************************************************************************
*/
/*
* LIBRARY FUNCTION
*
* tgoto expand cursor addressing string from cm capability
*
* KEY WORDS
*
* termcap
*
* SYNOPSIS
*
* char *tgoto(cm,destcol,destline)
* char *cm;
* int destcol;
* int destline;
*
* DESCRIPTION
*
* Returns cursor addressing string, decoded from the cm
* capability string, to move cursor to column destcol on
* line destline.
*
* The following sequences uses one input argument, either
* line or column, and place the appropriate substitution
* in the output string:
*
* %d substitute decimal value (in ASCII)
* %2 like %d but forces field width to 2
* %3 like %d but forces field width to 3
* %. like %c
* %+x like %c but adds ASCII value of x
*
* The following sequences cause processing modifications
* but do not "use up" one of the arguments. If they
* act on an argument they act on the next one to
* be converted.
*
* %>xy if next value to be converted is
* greater than value of ASCII char x
* then add value of ASCII char y.
* %r reverse substitution of line
* and column (line is substituted
* first by default).
* %i causes input values destcol and
* destline to be incremented.
* %% gives single % character in output.
*
* BUGS
*
* Does not implement some of the more arcane sequences for
* radically weird terminals (specifically %n, %B, & %D).
* If you have one of these you deserve whatever happens.
*
*/
/*
* Miscellaneous stuff
*/
#include <stdio.h>
#include <string.h>
#define MAXARGS 2
static void process(void);
static const char *in; /* Internal copy of input string pointer */
static char *out; /* Pointer to output array */
static int args[MAXARGS]; /* Maximum number of args to convert */
static int pcount; /* Count of args processed */
static char output[64]; /* Converted string */
/*
* PSEUDO CODE
*
* Begin tgoto
* If no string to process then
* Return pointer to error string.
* Else
* Initialize pointer to input string.
* Initialize pointer to result string.
* First arg is line number by default.
* Second arg is col number by default.
* No arguments processed yet.
* While there is another character to process
* If character is a not a % character then
* Simply copy to output.
* Else
* Process the control sequence.
* End if
* End while
* TERMINATE STRING! (rde)
* Return pointer to static output string.
* End if
* End tgoto
*
*/
const char * atgoto(const char *cm, int destcol, int destline)
{
if (cm == NULL) {
return("OOPS");
} else {
in = cm;
out = output;
args[0] = destline;
args[1] = destcol;
pcount = 0;
while (*in != '\0') {
if (*in != '%') {
*out++ = *in++;
} else {
process();
}
}
*out = '\0'; /* rde 18-DEC-86: don't assume out was all zeros */
return(output);
}
}
/*
* INTERNAL FUNCTION
*
* process process the conversion/command sequence
*
* SYNOPSIS
*
* static process()
*
* DESCRIPTION
*
* Processes the sequence beginning with the % character.
* Directly manipulates the input string pointer, the
* output string pointer, and the arguments. Leaves
* the input string pointer pointing to the next character
* to be processed, and the output string pointer pointing
* to the next output location. If conversion of
* one of the numeric arguments occurs, then the pcount
* is incremented.
*
*/
/*
* PSEUDO CODE
*
* Begin process
* Skip over the % character.
* Switch on next character after %
* Case 'd':
* Process %d type conversion (variable width).
* Reinitialize output pointer.
* Break;
* Case '2':
* Process %d type conversion (width 2).
* Reinitialize output pointer.
* Break;
* Case '3':
* Process %d type conversion (width 3).
* Reinitialize output pointer.
* Break;
* Case '.'
* Process %c type conversion.
* Break;
* Case '+':
* Process %c type conversion with offset.
* Break;
* Case '>':
* Process argument modification.
* Break;
* Case 'r':
* Process argument reversal.
* Break;
* Case 'i':
* Increment argument values.
* Break;
* Case '%':
* Copy to output, incrementing pointers.
* Break;
* End switch
* End process
*
*/
static void process(void)
{
int temp;
in++;
switch(*in++) {
case 'd':
sprintf(out,"%d",args[pcount++]);
out = &output[strlen(output)];
break;
case '2':
sprintf(out,"%02d",args[pcount++]);
out += 2 ;
/*
out = &output[strlen(output)];
*/
break;
case '3':
sprintf(out,"%03d",args[pcount++]);
out = &output[strlen(output)];
break;
case '.':
*out++ = args[pcount++];
break;
case '+':
*out++ = args[pcount++] + *in++;
break;
case '>':
if (args[pcount] > *in++) {
args[pcount] += *in++;
} else {
in++;
}
break;
case 'r':
temp = args[pcount];
args[pcount] = args[pcount+1];
args[pcount+1] = temp;
break;
case 'i':
args[pcount]++;
args[pcount+1]++;
break;
case '%':
*out++ = '%';
break;
}
}