1 /*
2 Copyright (c) 1991-2002, The Numerical ALgorithms Group Ltd.
3 All rights reserved.
4 
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are
7 met:
8 
9     - Redistributions of source code must retain the above copyright
10       notice, this list of conditions and the following disclaimer.
11 
12     - Redistributions in binary form must reproduce the above copyright
13      notice, this list of conditions and the following disclaimer in
14       the documentation and/or other materials provided with the
15       distribution.
16 
17     - Neither the name of The Numerical ALgorithms Group Ltd. nor the
18       names of its contributors may be used to endorse or promote products
19       derived from this software without specific prior written permission.
20 
21 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
22 IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
23 TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
24 PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
25 OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
26 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
27 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
28 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
29 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 */
33 
34 #include "fricas_c_macros.h"
35 #include <string.h>
36 #include <stdio.h>
37 #include <sys/types.h>
38 #include "edible.h"
39 
40 #include "prt.H1"
41 #include "edin.H1"
42 
43 void
myputchar(char c)44 myputchar(char c)
45 {
46     if (ECHOIT)
47         putchar(c);
48     return;
49 }
50 
51 void
clear_buff(void)52 clear_buff(void)
53 {
54     int count;
55 
56     /*** called when spadbuf gives me a line incase there is something already
57       on the line ****/
58     if (buff_pntr > 0) {
59         /*** backup to the beginning of the line ***/
60         for (count = curr_pntr; count > 0; count--)
61             myputchar(_BKSPC);
62         /** blank over the line      ***/
63         for (count = 0; count < buff_pntr; count++) {
64             myputchar(_BLANK);
65         }
66         /** back up again ***/
67         for (count = buff_pntr; count > 0; count--)
68             myputchar(_BKSPC);
69         init_buff(buff, buff_pntr);
70         init_flag(buff_flag, buff_pntr);
71         curr_pntr = buff_pntr = 0;
72     }
73 }
74 
75 
76 void
move_end(void)77 move_end(void)
78 {
79 
80     /** Moves cursor to the end of the line ***/
81     if (curr_pntr == buff_pntr) {
82         putchar(_BELL);
83     }
84     else {
85         for (; curr_pntr < buff_pntr;) {
86             myputchar(buff[curr_pntr++]);
87         }
88     }
89     fflush(stdout);
90 }
91 
92 void
move_home(void)93 move_home(void)
94 {
95 
96     /*** Moves the cursor to the front of the line ***/
97     if (curr_pntr > 0) {
98         for (; curr_pntr > 0;) {
99             myputchar(_BKSPC);
100             curr_pntr--;
101         }
102     }
103     else {
104         putchar(_BELL);
105     }
106     fflush(stdout);
107 
108 }
109 
110 void
move_fore_word(void)111 move_fore_word(void)
112 {
113     /** move the cursor to the next blank space  **/
114     if (curr_pntr != buff_pntr) {
115         myputchar(buff[curr_pntr]);
116         curr_pntr++;
117         while (curr_pntr < buff_pntr && buff[curr_pntr] != ' ') {
118             myputchar(buff[curr_pntr]);
119             curr_pntr++;
120         }
121     }
122     else
123         putchar(_BELL);
124     fflush(stdout);
125     return;
126 }
127 
128 void
move_back_word(void)129 move_back_word(void)
130 {
131     /*** moves the cursor to the last blank space ***/
132     if (curr_pntr > 0) {
133         myputchar(_BKSPC);
134         curr_pntr--;
135         while (curr_pntr > 0 && buff[curr_pntr - 1] != ' ') {
136             myputchar(_BKSPC);
137             curr_pntr--;
138         }
139 
140     }
141     else
142         putchar(_BELL);
143     fflush(stdout);
144     return;
145 }
146 
147 void
delete_current_char(void)148 delete_current_char(void)
149 {
150     /**  deletes the char currently above the current_pntr, if it can be **/
151     if (curr_pntr != buff_pntr) {
152         if (buff_flag[curr_pntr] == 1 || buff_flag[curr_pntr] == 0) {
153             myputchar(_BLANK);
154             myputchar(_BKSPC);
155             memmove(&buff[curr_pntr],
156                     &buff[curr_pntr + 1],
157                     buff_pntr - curr_pntr - 1);
158             flagcpy(&buff_flag[curr_pntr],
159                     &buff_flag[curr_pntr + 1]);
160             buff_pntr--;
161             del_print(curr_pntr, 1);
162         }
163         else {
164             /** lets delete two of the little buggers **/
165             myputchar(_BLANK);
166             myputchar(_BLANK);
167             myputchar(_BKSPC);
168             myputchar(_BKSPC);
169             memmove(&buff[curr_pntr],
170                     &buff[curr_pntr + 2],
171                     buff_pntr - curr_pntr - 2);
172             flagcpy(&buff_flag[curr_pntr],
173                     &buff_flag[curr_pntr + 2]);
174             buff_pntr -= 2;
175             del_print(curr_pntr, 2);
176         }
177     }
178     else {
179         putchar(_BELL);
180         fflush(stdout);
181     }
182     num_proc = num_proc + 3;
183 }
184 
185 void
delete_to_end_of_line(void)186 delete_to_end_of_line(void)
187 {
188     int count;
189 
190     /*** deletes from the curr_pntr to the end of line ***/
191 
192     if (curr_pntr == buff_pntr)
193         return;                 /** There is nothing to do **/
194 
195     /** blank over the end of the line      ***/
196     for (count = curr_pntr; count < buff_pntr; count++) {
197         myputchar(_BLANK);
198     }
199     /** back up again ***/
200     for (count = buff_pntr; count > curr_pntr; count--)
201         myputchar(_BKSPC);
202 
203     buff_pntr = curr_pntr;
204     fflush(stdout);
205     return;
206 
207 }
208 
209 void
delete_line(void)210 delete_line(void)
211 {
212     int count;
213 
214     /*** deletes the entire line                                 *****/
215 
216     if (buff_pntr == 0)
217         return;                 /** There is nothing to do **/
218 
219     /** first I have to back up to the beginning of the line     ****/
220     for (count = curr_pntr; count > 0; count--)
221         myputchar(_BKSPC);
222 
223     /** blank over the end of the line      ***/
224     for (count = 0; count < buff_pntr; count++) {
225         myputchar(_BLANK);
226     }
227     /** back up again ***/
228     for (count = buff_pntr; count > 0; count--)
229         myputchar(_BKSPC);
230 
231     /* Also clear the buffer */
232     init_buff(buff, buff_pntr);
233     init_flag(buff_flag, buff_pntr);
234     buff_pntr = curr_pntr = 0;
235 
236     fflush(stdout);
237     return;
238 
239 }
240 
241 void
printbuff(int start,int num)242 printbuff(int start,int  num)
243 {
244     int trace;
245 
246     for (trace = start; trace < start + num; trace++)
247         if (buff[trace] != '\0')
248             myputchar(buff[trace]);
249     fflush(stdout);
250 }
251 
252 void
del_print(int start,int num)253 del_print(int start, int num)
254 {
255     int count;
256 
257     /*** move the rest of the string     ***/
258     for (count = start; count < buff_pntr; count++) {
259         myputchar(buff[count]);
260     }
261     /** now blank out the number of chars we are supposed to ***/
262     for (count = 0; count < num; count++)
263         myputchar(_BLANK);
264     /*** Now back up  ***/
265     for (count = buff_pntr + num; count > start; count--)
266         myputchar(_BKSPC);
267     fflush(stdout);
268 }
269 
270 
271 void
ins_print(int start,int num)272 ins_print(int start,int  num)
273 {
274     int count;
275 
276     /** write the rest of the word ***/
277     for (count = start; count < buff_pntr + num; count++) {
278         myputchar(buff[count]);
279     }
280     /** now back up to where we should be ***/
281     for (count = buff_pntr; count > start; count--)
282         myputchar(_BKSPC);
283     fflush(stdout);
284 }
285 
286 void
reprint(int start)287 reprint(int start)
288 {
289     /**  simply reprints a single character **/
290     if (buff[start] == '\0')
291         myputchar(_BLANK);
292     else
293         myputchar(buff[start]);
294     myputchar(_BKSPC);
295     fflush(stdout);
296     return;
297 }
298 
299 void
back_up(int num_chars)300 back_up(int num_chars)
301 {
302     int cnt;
303 
304     for (cnt = 0; cnt < num_chars; cnt++)
305         myputchar(_BKSPC);
306     for (cnt = 0; cnt < num_chars; cnt++)
307         myputchar(_BLANK);
308     for (cnt = 0; cnt < num_chars; cnt++)
309         myputchar(_BKSPC);
310     fflush(stdout);
311 
312 }
313 
314 void
back_it_up(int num_chars)315 back_it_up(int num_chars)
316 {
317     int cnt;
318 
319     for (cnt = 0; cnt < num_chars; cnt++)
320         myputchar(_BKSPC);
321     fflush(stdout);
322 }
323 
324 
325 void
print_whole_buff(void)326 print_whole_buff(void)
327 {
328     int trace;
329 
330     for (trace = 0; trace < buff_pntr; trace++)
331         if (buff[trace] != '\0')
332             myputchar(buff[trace]);
333     fflush(stdout);
334 }
335 
336 void
move_ahead(void)337 move_ahead(void)
338 {
339     /*** simply moves the pointer ahead a single word ***/
340     if (curr_pntr == buff_pntr) {
341         putchar(_BELL);
342     }
343     else {
344         if (buff_flag[curr_pntr] == 2) {
345             myputchar(buff[curr_pntr++]);
346         }
347         myputchar(buff[curr_pntr++]);
348     }
349     fflush(stdout);
350 }
351 
352 void
move_back(void)353 move_back(void)
354 {
355     /** simply moves the cursor back one position **/
356     if (curr_pntr == 0) {
357         putchar(_BELL);
358     }
359     else {
360         if (!buff_flag[curr_pntr - 1]) {
361             myputchar(_BKSPC);
362             curr_pntr--;
363         }
364         myputchar(_BKSPC);
365         curr_pntr--;
366     }
367     fflush(stdout);
368 }
369 
370 void
back_over_current_char(void)371 back_over_current_char(void)
372 {
373     /*** simply backs over the character behind the cursor ***/
374     if (curr_pntr == 0) {
375         putchar(_BELL);
376     }
377     else {
378         if (!buff_flag[curr_pntr - 1]) {
379             myputchar(_BKSPC);
380             myputchar(_BKSPC);
381             myputchar(_BLANK);
382             myputchar(_BLANK);
383             myputchar(_BKSPC);
384             myputchar(_BKSPC);
385             memmove(&buff[curr_pntr - 2],
386                    &buff[curr_pntr], buff_pntr - curr_pntr);
387             flagcpy(&buff_flag[curr_pntr - 2],
388                     &buff_flag[curr_pntr]);
389             buff_pntr -= 2;
390             curr_pntr -= 2;
391             del_print(curr_pntr, 2);
392         }
393         else {
394             myputchar(_BKSPC);
395             myputchar(_BLANK);
396             myputchar(_BKSPC);
397             memmove(&buff[curr_pntr - 1],
398                    &buff[curr_pntr], buff_pntr - curr_pntr);
399             flagcpy(&buff_flag[curr_pntr - 1],
400                     &buff_flag[curr_pntr]);
401             curr_pntr--;
402             buff_pntr--;
403             del_print(curr_pntr, 1);
404         }
405     }
406     fflush(stdout);
407     return;
408 }
409