Added all of the MH sources, including RCS files, in
[mmh] / docs / historical / mh-6.8.5 / miscellany / less-177 / cmdbuf.c
1 /*
2  * Functions which manipulate the command buffer.
3  * Used only by command() and related functions.
4  */
5
6 #include "less.h"
7
8 extern int erase_char, kill_char;
9 extern int sc_width;
10
11 static char cmdbuf[120];        /* Buffer for holding a multi-char command */
12 static int cmd_col;             /* Current column of the multi-char command */
13 static char *cp;                /* Pointer into cmdbuf */
14
15 /*
16  * Reset command buffer (to empty).
17  */
18         public void
19 cmd_reset()
20 {
21         cp = cmdbuf;
22         *cp = '\0';
23         cmd_col = 0;
24 }
25
26 /*
27  * How many characters are in the command buffer?
28  */
29         public int
30 len_cmdbuf()
31 {
32         return (cp - cmdbuf);
33 }
34
35 /*
36  * Backspace in the command buffer.
37  */
38         public int
39 cmd_erase()
40 {
41         register char *s;
42
43         if (cp == cmdbuf)
44                 /*
45                  * Backspace past beginning of the string:
46                  * this usually means abort the command.
47                  */
48                 return (1);
49
50         --cp;
51         if (*cp == ESC)
52                 s = "ESC";
53         else
54                 s = prchar(*cp);
55         while (*s++ != '\0')
56         {
57                 backspace();
58                 cmd_col--;
59         }
60         *cp = '\0';
61         return (0);
62 }
63
64 /*
65  * Process a single character of a multi-character command, such as
66  * a number, or the pattern of a search command.
67  */
68         public int
69 cmd_char(c)
70         int c;
71 {
72         char *s;
73
74         if (c == erase_char)
75         {
76                 if (cmd_erase())
77                         return (1);
78         } else if (c == kill_char)
79         {
80                 /* {{ Could do this faster, but who cares? }} */
81                 while (cmd_erase() == 0)
82                         ;
83         } else if (cp >= &cmdbuf[sizeof(cmdbuf)-1])
84         {
85                 /*
86                  * No room in the command buffer.
87                  */
88                 bell();
89         } else if (cmd_col >= sc_width-4)
90         {
91                 /*
92                  * No room on the screen.
93                  * {{ Could get fancy here; maybe shift the displayed
94                  *    line and make room for more chars, like ksh. }}
95                  */
96                 bell();
97         } else
98         {
99                 /*
100                  * Append the character to the string.
101                  */
102                 *cp++ = c;
103                 *cp = '\0';
104                 if (c == ESC)
105                         s = "ESC";
106                 else
107                         s = prchar(c);
108                 putstr(s);
109                 cmd_col += strlen(s);
110         }
111         return (0);
112 }
113
114 /*
115  * Return the number currently in the command buffer.
116  */
117         public int
118 cmd_int()
119 {
120         return (atoi(cmdbuf));
121 }
122
123 /*
124  * Display a string, usually as a prompt for input into the command buffer.
125  */
126         public void
127 cmd_putstr(s)
128         char *s;
129 {
130         putstr(s);
131         cmd_col += strlen(s);
132 }
133
134 /*
135  * Return a pointer to the command buffer.
136  */
137         public char *
138 get_cmdbuf()
139 {
140         return (cmdbuf);
141 }