1 | /*
|
---|
2 | =============================================================================
|
---|
3 | getline.c -- get a line from stdin
|
---|
4 | Version 1 -- 1988-05-03 -- D.N. Lynx Crowe
|
---|
5 | Does line editing. Acts like gets().
|
---|
6 | =============================================================================
|
---|
7 | */
|
---|
8 |
|
---|
9 | #include "stdio.h"
|
---|
10 | #include "ascii.h"
|
---|
11 | #include "stddefs.h"
|
---|
12 |
|
---|
13 | char *
|
---|
14 | getline(buf, nb)
|
---|
15 | char *buf; /* buffer pointer */
|
---|
16 | int nb; /* buffer limit */
|
---|
17 | {
|
---|
18 | register char *bp;
|
---|
19 | register int c;
|
---|
20 | register int bc;
|
---|
21 |
|
---|
22 | bc = 0; /* number of characters currently in buffer */
|
---|
23 | bp = buf; /* current buffer pointer */
|
---|
24 | *bp = '\0'; /* initial null into buffer */
|
---|
25 |
|
---|
26 | while (bc < nb) { /* read up to limit */
|
---|
27 |
|
---|
28 | c = getchar();
|
---|
29 |
|
---|
30 | switch (c) {
|
---|
31 |
|
---|
32 | case A_BS:
|
---|
33 | case A_DEL:
|
---|
34 |
|
---|
35 | if (bc EQ 0) {
|
---|
36 |
|
---|
37 | putchar(A_BEL); /* complain -- nothing there */
|
---|
38 | break;
|
---|
39 |
|
---|
40 | } else {
|
---|
41 |
|
---|
42 | bc--; /* decrement byte count */
|
---|
43 | bp--; /* back-up the pointer */
|
---|
44 | *bp = '\0'; /* terminate the string */
|
---|
45 |
|
---|
46 | putchar(A_BS); /* erase character from screen */
|
---|
47 | putchar(' ');
|
---|
48 | putchar(A_BS);
|
---|
49 |
|
---|
50 | break;
|
---|
51 | }
|
---|
52 |
|
---|
53 | case A_CR:
|
---|
54 | case A_LF:
|
---|
55 |
|
---|
56 | *bp = '\0'; /* terminate line with null */
|
---|
57 | return(buf); /* return -- CR or LF hit */
|
---|
58 |
|
---|
59 | /* |
---|
60 | */
|
---|
61 |
|
---|
62 | case CTL('X'):
|
---|
63 |
|
---|
64 | *buf = '\0'; /* clear the buffer */
|
---|
65 |
|
---|
66 | while (bc--) {
|
---|
67 |
|
---|
68 | putchar(A_BS);
|
---|
69 | putchar(' ');
|
---|
70 | putchar(A_BS);
|
---|
71 | }
|
---|
72 |
|
---|
73 | bc = 0; /* reset byte count */
|
---|
74 | bp = buf; /* reset buffer pointer */
|
---|
75 | break;
|
---|
76 |
|
---|
77 | case EOF:
|
---|
78 |
|
---|
79 | return(NULL);
|
---|
80 |
|
---|
81 | default:
|
---|
82 |
|
---|
83 | *bp++ = (char)c; /* put character in buffer */
|
---|
84 | *bp = '\0'; /* terminate line with null */
|
---|
85 | putchar(c); /* echo the character */
|
---|
86 | bc++; /* update character count */
|
---|
87 | }
|
---|
88 | }
|
---|
89 |
|
---|
90 | buf[nb - 1] = '\0'; /* terminate buffer */
|
---|
91 | return(buf); /* buffer full error */
|
---|
92 | }
|
---|