BSD 4_4 release
[unix-history] / usr / src / sys / luna68k / stand / getline.c-big
CommitLineData
ad787160
C
1/*
2 * Copyright (c) 1992 OMRON Corporation.
3 * Copyright (c) 1992 The Regents of the University of California.
4 * All rights reserved.
5 *
6 * This code is derived from software contributed to Berkeley by
7 * OMRON Corporation.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. All advertising materials mentioning features or use of this software
18 * must display the following acknowledgement:
19 * This product includes software developed by the University of
20 * California, Berkeley and its contributors.
21 * 4. Neither the name of the University nor the names of its contributors
22 * may be used to endorse or promote products derived from this software
23 * without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * SUCH DAMAGE.
36 *
37 * @(#)getline.c-big 8.1 (Berkeley) 6/10/93
38 */
39
40/*
41 * getline -- getline function with EMACS like key operation
42 * by A.Fujita, FEB-01-1992
43 */
44
45#define MAXLINE 80
46
47struct key_bind_table {
48 int (*func)();
49};
50
51char linebuff[MAXLINE];
52int lineleft;
53int lineright = MAXLINE - 1;
54int cursole;
55int endline;
56
57int
58nil_operation(c)
59 char c;
60{
61 return(1);
62}
63
64int
65self_insert(c)
66 char c;
67{
68 register int n;
69
70 if (cursole <= endline) {
71 linebuff[cursole++] = c;
72
73 cnputc(c);
74
75 for (n = endline; n < MAXLINE; n++)
76 cnputc(linebuff[n]);
77 for (n = endline + 1; n < MAXLINE; n++)
78 cnputc('\x08'); /* le: move the cursor left one column */
79 }
80
81 return(1);
82}
83
84/*
85int
86self_insert(c)
87 char c;
88{
89 register int n;
90
91 if (cursole <= endline) {
92 linebuff[cursole++] = c;
93
94 cnputc('\x1b');
95 cnputc('[');
96 cnputc('4');
97 cnputc('h');
98
99 cnputc(c);
100
101 cnputc('\x1b');
102 cnputc('[');
103 cnputc('4');
104 cnputc('l');
105 }
106
107 return(1);
108}
109 */
110
111int
112accept_line(c)
113 char c;
114{
115 cnputc('\r');
116 cnputc('\n');
117
118 return(0);
119}
120
121int
122beginning_of_line(c)
123 char c;
124{
125 while (cursole > lineleft) {
126 linebuff[endline--] = linebuff[--cursole];
127 cnputc('\x08'); /* le: move the cursor left one column */
128 }
129
130 return(1);
131}
132
133int
134end_of_line(c)
135 char c;
136{
137 while (endline < lineright) {
138 linebuff[cursole++] = linebuff[++endline];
139 cnputc('\x1b'); /* nd: move the cursor right one column */
140 cnputc('[');
141 cnputc('C');
142 }
143
144 return(1);
145}
146
147int
148forward_char(c)
149 char c;
150{
151 if (endline < lineright) {
152 linebuff[cursole++] = linebuff[++endline];
153 cnputc('\x1b'); /* nd: move the cursor right one column */
154 cnputc('[');
155 cnputc('C');
156 }
157
158 return(1);
159}
160
161int
162backward_char(c)
163 char c;
164{
165 if (cursole > lineleft) {
166 linebuff[endline--] = linebuff[--cursole];
167 cnputc('\x08');
168 }
169
170 return(1);
171}
172
173int
174delete_char(c)
175 char c;
176{
177 register int n;
178
179 if (cursole > lineleft || endline < lineright) {
180 endline++;
181 cnputc('\x1b'); /* dc: delete one character position at the cursor */
182 cnputc('[');
183 cnputc('P');
184 }
185}
186
187int
188backward_delete_char(c)
189 char c;
190{
191 if (cursole > lineleft) {
192 cursole--;
193 cnputc('\x08'); /* le: move the cursor left one column */
194 cnputc('\x1b'); /* dc: delete one character position at the cursor */
195 cnputc('[');
196 cnputc('P');
197 }
198
199 return(1);
200}
201
202int
203kill_line(c)
204{
205 register int n = lineright - endline;
206
207 while(endline < lineright) {
208 endline++;
209 cnputc('\x1b'); /* dc: delete one character position at the cursor */
210 cnputc('[');
211 cnputc('P');
212 }
213
214 return(1);
215}
216
217struct key_bind_table keybind[] = {
218{ nil_operation }, /* 0x00 NUL */
219{ beginning_of_line }, /* 0x01 SOH */
220{ backward_char }, /* 0x02 STX */
221{ nil_operation }, /* 0x03 ETX */
222{ delete_char }, /* 0x04 EOT */
223{ end_of_line }, /* 0x05 ENQ */
224{ forward_char }, /* 0x06 ACK */
225{ nil_operation }, /* 0x07 BEL */
226{ backward_delete_char }, /* 0x08 BS */
227{ nil_operation }, /* 0x09 HT */
228{ accept_line }, /* 0x0A NL */
229{ kill_line }, /* 0x0B VT */
230{ nil_operation }, /* 0x0C NP */
231{ accept_line }, /* 0x0D CR */
232{ nil_operation }, /* 0x0E SO */
233{ nil_operation }, /* 0x0F SI */
234{ nil_operation }, /* 0x10 DLE */
235{ nil_operation }, /* 0x11 DC1 */
236{ nil_operation }, /* 0x12 DC2 */
237{ nil_operation }, /* 0x13 DC3 */
238{ nil_operation }, /* 0x14 DC4 */
239{ nil_operation }, /* 0x15 NAK */
240{ nil_operation }, /* 0x16 SYN */
241{ nil_operation }, /* 0x17 ETB */
242{ nil_operation }, /* 0x18 CAN */
243{ nil_operation }, /* 0x19 EM */
244{ nil_operation }, /* 0x1A SUB */
245{ nil_operation }, /* 0x1B ESC */
246{ nil_operation }, /* 0x1C FS */
247{ nil_operation }, /* 0x1D GS */
248{ nil_operation }, /* 0x1E RS */
249{ nil_operation }, /* 0x1F US */
250{ self_insert }, /* 0x20 ( ) */
251{ self_insert }, /* 0x21 (!) */
252{ self_insert }, /* 0x22 (") */
253{ self_insert }, /* 0x23 (#) */
254{ self_insert }, /* 0x24 ($) */
255{ self_insert }, /* 0x25 (%) */
256{ self_insert }, /* 0x26 (&) */
257{ self_insert }, /* 0x27 (') */
258{ self_insert }, /* 0x28 (() */
259{ self_insert }, /* 0x29 ()) */
260{ self_insert }, /* 0x2A (*) */
261{ self_insert }, /* 0x2B (+) */
262{ self_insert }, /* 0x2C (,) */
263{ self_insert }, /* 0x2D (-) */
264{ self_insert }, /* 0x2E (.) */
265{ self_insert }, /* 0x2F (/) */
266{ self_insert }, /* 0x30 (0) */
267{ self_insert }, /* 0x31 (1) */
268{ self_insert }, /* 0x32 (2) */
269{ self_insert }, /* 0x33 (3) */
270{ self_insert }, /* 0x34 (4) */
271{ self_insert }, /* 0x35 (5) */
272{ self_insert }, /* 0x36 (6) */
273{ self_insert }, /* 0x37 (7) */
274{ self_insert }, /* 0x38 (8) */
275{ self_insert }, /* 0x39 (9) */
276{ self_insert }, /* 0x3A (:) */
277{ self_insert }, /* 0x3B (;) */
278{ self_insert }, /* 0x3C (<) */
279{ self_insert }, /* 0x3D (=) */
280{ self_insert }, /* 0x3E (>) */
281{ self_insert }, /* 0x3F (?) */
282{ self_insert }, /* 0x40 (@) */
283{ self_insert }, /* 0x41 (A) */
284{ self_insert }, /* 0x42 (B) */
285{ self_insert }, /* 0x43 (C) */
286{ self_insert }, /* 0x44 (D) */
287{ self_insert }, /* 0x45 (E) */
288{ self_insert }, /* 0x46 (F) */
289{ self_insert }, /* 0x47 (G) */
290{ self_insert }, /* 0x48 (H) */
291{ self_insert }, /* 0x49 (I) */
292{ self_insert }, /* 0x4A (J) */
293{ self_insert }, /* 0x4B (K) */
294{ self_insert }, /* 0x4C (L) */
295{ self_insert }, /* 0x4D (M) */
296{ self_insert }, /* 0x4E (N) */
297{ self_insert }, /* 0x4F (O) */
298{ self_insert }, /* 0x50 (P) */
299{ self_insert }, /* 0x51 (Q) */
300{ self_insert }, /* 0x52 (R) */
301{ self_insert }, /* 0x53 (S) */
302{ self_insert }, /* 0x54 (T) */
303{ self_insert }, /* 0x55 (U) */
304{ self_insert }, /* 0x56 (V) */
305{ self_insert }, /* 0x57 (W) */
306{ self_insert }, /* 0x58 (X) */
307{ self_insert }, /* 0x59 (W) */
308{ self_insert }, /* 0x5A (Z) */
309{ self_insert }, /* 0x5B ([) */
310{ self_insert }, /* 0x5C (\) */
311{ self_insert }, /* 0x5D (]) */
312{ self_insert }, /* 0x5E (^) */
313{ self_insert }, /* 0x5F (_) */
314{ self_insert }, /* 0x60 (`) */
315{ self_insert }, /* 0x61 (a) */
316{ self_insert }, /* 0x62 (b) */
317{ self_insert }, /* 0x63 (c) */
318{ self_insert }, /* 0x64 (d) */
319{ self_insert }, /* 0x65 (e) */
320{ self_insert }, /* 0x66 (f) */
321{ self_insert }, /* 0x67 (g) */
322{ self_insert }, /* 0x68 (h) */
323{ self_insert }, /* 0x69 (i) */
324{ self_insert }, /* 0x6A (j) */
325{ self_insert }, /* 0x6B (k) */
326{ self_insert }, /* 0x6C (l) */
327{ self_insert }, /* 0x6D (m) */
328{ self_insert }, /* 0x6E (n) */
329{ self_insert }, /* 0x6F (o) */
330{ self_insert }, /* 0x70 (p) */
331{ self_insert }, /* 0x71 (q) */
332{ self_insert }, /* 0x72 (r) */
333{ self_insert }, /* 0x73 (s) */
334{ self_insert }, /* 0x74 (t) */
335{ self_insert }, /* 0x75 (u) */
336{ self_insert }, /* 0x76 (v) */
337{ self_insert }, /* 0x77 (w) */
338{ self_insert }, /* 0x78 (x) */
339{ self_insert }, /* 0x79 (y) */
340{ self_insert }, /* 0x7A (z) */
341{ self_insert }, /* 0x7B ({) */
342{ self_insert }, /* 0x7C (|) */
343{ self_insert }, /* 0x7D (}) */
344{ self_insert }, /* 0x7E (~) */
345{ backward_delete_char }, /* 0x7F DEL */
346};
347
348int
349getline(prompt, buff)
350 char *prompt, *buff;
351{
352 register int c;
353
354 bzero(linebuff, MAXLINE);
355 cursole = lineleft = strlen(prompt);
356 endline = lineright = MAXLINE - 1;
357 printf("%s", prompt);
358
359 do {
360 c = cngetc();
361 c &= 0x7F;
362 } while((*keybind[c].func)(c));
363
364 bcopy(&linebuff[lineleft],
365 &buff[0], cursole - lineleft);
366 bcopy(&linebuff[endline+1],
367 &buff[cursole - lineleft], lineright - endline);
368
369 return(strlen(buff));
370}