-
Notifications
You must be signed in to change notification settings - Fork 1
/
editgets.c
371 lines (338 loc) · 12.4 KB
/
editgets.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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
/* editgets.c - line input w/editing */
/* this code is released to the public domain */
/* written by Jon Burchmore */
/* modifications & enhancements by Bob Stout */
/* This is as close to ANSI compliant C that I could come, but it was made */
/* on an IBM compatable computer, so I designed it for that platform. */
/* If you're porting it to another computer type, please note how the IBM */
/* passes enhanced keys. First, it sends an ASCIIZ (character 0), then a */
/* standard character. Anyway, what the switch() statement does is check */
/* to see if there WAS a zero sent, and if there wasn't, it just "falls" */
/* through to the default, which handles normal characters. */
/* The conio header file provides the getch() function, which returns a */
/* single character from the KEYBOARD, not stdin, and waits if it must. */
/* It is be possible to re-write this function for a computer besides an */
/* IBM PC. */
/* It would be possible to check the variable insert, and if it's on, make */
/* the cursor large, and if it's off, make the cursor small, but my primary */
/* goal is portability, not fancy add-ons */
/* Pardon the lack of comments. I'm a coder, not an author. Besides, if */
/* you can't understand this, DON'T USE IT! (Words to live by) */
#include <conio.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#undef min
#define min(x,y) (((x) <= (y)) ? (x) : (y))
#define NUL 0x00
#define ESC 0x1B
#define LEFT 0x4B
#define RIGHT 0x4D
#define HOME 0x47
#define END 0x4F
#define INSERT 0x52
#define DELETE 0x53
#define BACKSPC 0x08
#define ENTER 0x0D
#define CTLEND 0x75
#define CTLHOME 0x77
#define CTLRT 0x74
#define CTLLFT 0x73
/*
** Password mode - '*' is echoed for all characters, only ESC, ENTER,
** BACKSPC, and CTLHOME are active.
*/
int password_mode = 0;
/*
** Aruments: 1) Buffer to receive string
** 2) Size of buffer
** 3) Default string
*/
int jgets(char *s, int maxlen, char *string)
{
char temp[500];
int insert = 1, done = 0, pos, len, i, j, c, zeroflag;
if (NULL == string)
string = "";
if (0 != (pos = len = strlen(string)))
strncpy(temp, string, min(len, maxlen));
for (i = 0; i < maxlen; ++i)
{
if (NUL == *string)
putchar('_');
else putchar(*string++);
}
for (i = 0; i < (maxlen - len); ++i)
putchar(BACKSPC);
while (!done)
{
zeroflag = 0;
if ((c = getch()) == 0)
{
zeroflag = 1;
c = getch();
}
switch (c)
{
case ESC :
if (len == 0)
break;
if (pos != len)
{
for (i = pos; i < len; i++)
putch('_');
for (i = len; i >= 0; i--)
{
putch(BACKSPC);
putch('_');
putch(BACKSPC);
}
pos = len = 0;
break;
}
case LEFT :
if (zeroflag)
{
if (password_mode)
break;
if (pos == 0)
break;
pos--;
putch(BACKSPC);
break;
}
case RIGHT :
if (zeroflag)
{
if (password_mode)
break;
if (pos == len)
break;
if (pos != maxlen)
{
putch(temp[pos]);
pos++;
}
break;
}
case HOME :
if (zeroflag)
{
if (password_mode)
break;
while (pos-- > 0)
putch(BACKSPC);
pos = 0;
break;
}
case END :
if (zeroflag)
{
if (password_mode)
break;
while (pos < len)
putch(temp[pos++]);
break;
}
case INSERT :
if (zeroflag)
{
if (password_mode)
break;
insert = (!(insert));
break;
}
case DELETE :
if (zeroflag)
{
if (password_mode)
break;
if (pos == len)
break;
for (i = pos; i < len; i++)
temp[i] = temp[i + 1];
len--;
for (i = pos; i < len; i++)
putch(temp[i]);
putch('_');
for (i = len + 1; i > pos; i--)
putch(BACKSPC);
break;
}
case BACKSPC :
if (c == BACKSPC)
{
if (pos == 0)
break;
if (pos != len)
{
for (i = pos - 1; i < len; i++)
temp[i] = temp[i + 1];
pos--;
len--;
putch(BACKSPC);
for (i = pos; i < len; i++)
putch(temp[i]);
putch('_');
for (i = len; i >= pos; i--)
putch(BACKSPC);
}
else
{
putch(BACKSPC);
putch('_');
putch(BACKSPC);
pos = --len;
}
break;
}
case ENTER :
if (c == ENTER)
{
done = 1;
break;
}
case CTLEND :
if (zeroflag)
{
if (password_mode)
break;
for (i = pos; i < len; ++i)
putch('_');
for (i = pos; i < len; ++i)
putch(BACKSPC);
len = pos;
break;
}
case CTLHOME :
if (zeroflag)
{
if (pos == 0)
break;
if (pos != len)
{
while (0 != pos)
{
for (i = pos - 1; i < len; i++)
temp[i] = temp[i + 1];
pos--;
len--;
putch(BACKSPC);
for (i = pos; i < len; i++)
putch(temp[i]);
putch('_');
for (i = len; i >= pos; i--)
putch(BACKSPC);
}
}
else
{
while (0 != pos)
{
putch(BACKSPC);
putch('_');
putch(BACKSPC);
pos = --len;
}
}
break;
}
case CTLRT :
if (zeroflag)
{
if (password_mode)
break;
do
{
if (pos == len)
break;
if (pos != maxlen)
{
putch(temp[pos]);
pos++;
}
} while (isspace(temp[pos]));
do
{
if (pos == len)
break;
if (pos != maxlen)
{
putch(temp[pos]);
pos++;
}
} while (!isspace(temp[pos]));
break;
}
case CTLLFT :
if (zeroflag)
{
if (password_mode)
break;
do
{
if (pos == 0)
break;
pos--;
putch(BACKSPC);
} while (isspace(temp[pos]));
do
{
if (pos == 0)
break;
pos--;
putch(BACKSPC);
} while (!isspace(temp[pos]));
break;
}
default :
if (zeroflag)
break;
if (c == 0 || pos == maxlen)
break;
if ((!(insert)) || pos == len)
{
temp[pos++] = (char)c;
if (pos > len) len++;
if (password_mode)
putch('*');
else putch(c);
}
else
{
if (len == maxlen)
break;
for (i = len++; i >= pos; i--)
temp[i + 1] = temp[i];
temp[pos++] = (char)c;
if (password_mode)
putch('*');
else putch(c);
for (i = pos; i < len; i++)
putch(temp[i]);
for (i = len; i > pos; i--)
putch(BACKSPC);
}
}
}
temp[len] = '\0';
strcpy(s, temp);
return len;
}
#ifdef TEST
void main(void)
{
char mystring[60];
memset(mystring, 0, 60);
fputs("Enter any string: ", stdout);
jgets(mystring, 60, "This is a test");
puts("");
printf("editgets() returned:\n%s\n", mystring);
password_mode = 1;
memset(mystring, 0, 60);
fputs("Enter any password: ", stdout);
jgets(mystring, 50, NULL);
puts("");
printf("editgets() returned:\n%s\n", mystring);
}
#endif