File: zzledt.c

package info (click to toggle)
scilab 2.4-1
  • links: PTS
  • area: non-free
  • in suites: potato, slink
  • size: 55,196 kB
  • ctags: 38,019
  • sloc: ansic: 231,970; fortran: 148,976; tcl: 7,099; makefile: 4,585; sh: 2,978; csh: 154; cpp: 101; asm: 39; sed: 5
file content (1108 lines) | stat: -rw-r--r-- 30,664 bytes parent folder | download
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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
/* Copyright INRIA */
/***********************************************************************
 * zzledt.c - last line editing routine
 *
 * $Id: zzledt.c,v 10.2 1993/03/08 16:11:01 a Exp $
 * $Log: zzledt.c,v $
 *
 * Revision 10.1  1993/02/11  14:34:03  mga
 * Initial Version
 *
 * Copyright (c) mitchell and gauthier assoc, inc 1990
 * All rights reserved
 *
 **********************************************************************/

#include <string.h>

#ifndef WIN32 
/** The win32 version is defined in the wsci directory **/


#ifdef __STDC__
#include <stdlib.h>
#endif

int gchar_no_echo();
extern int  Xorgetchar();
char Sci_Prompt[10];

#ifdef aix
#define ATTUNIX
#endif
#ifdef aix370
#define ATTUNIX
#endif
#ifdef cdcu
#define ATTUNIX
#endif
#ifdef cray
#define B42UNIX
#endif
#ifdef hpux
#define ATTUNIX
#endif
#ifdef sgi
#define ATTUNIX
#endif
#ifdef sun
#define ATTUNIX
#define TERMCAP
#endif
#ifdef ultrix
#define B42UNIX
#define TERMCAP
#endif
#ifdef  __alpha
#define B42UNIX
#endif
#ifdef linux
#define ATTUNIX
#define TERMCAP
#endif

#include <stdio.h>
#include <ctype.h>
#include "../machine.h"

#ifndef HAVE_TERMCAP
#undef TERMCAP
#endif

#ifdef B42UNIX

#define KEYPAD 

#include <sys/file.h>
#include <sgtty.h>

static short save_sg_flags;
static struct sgttyb arg;
static struct tchars arg1;
#endif

#ifdef ATTUNIX

#define KEYPAD 

#include <termio.h>

static struct termio save_term;
static struct termio arg;
#endif

#ifndef MAX
#define MAX(a, b) (a > b ? a : b)
#endif

#define EXCL                  0x0021
#define UP_ARROW              0x0148
#define DOWN_ARROW            0x0150
#define LEFT_ARROW            0x014b
#define RIGHT_ARROW           0x014d
#define SEARCH_BACKWARD       0x015e
#define SEARCH_FORWARD        0x0160
#define HOME                  0x0001  /* control-A */
#define ENDS                  0x0005  /* control-E */
#define LDEL                  0x0015  /* control-U */
#define INS                   0x0014  /* control-T */
#define DEL                   0x007f
#define BS                    0x0008  /* control-H */
#define CR                    0x000d
#define LF                    0x000a
#define BEL                   0x0007
#define CTRL_B                0x0002  /* back a character */
#define CTRL_C                0x0003  /* redo line */
#define CTRL_D                0x0004  /* delete next char */
#define CTRL_F                0x0006  /* forward a character */
#define CTRL_K                0x000b  /* delete to end of line */
#define CTRL_L                0x000c  /* delete to end of line */
#define CTRL_N                0x000e  /* next line */
#define CTRL_P                0x0010  /* previous line */
#define CTRL_Y                0x0019  /* paste */

#define NUL '\0'
#define TRUE 1
#define FALSE 0
#define TAB_SKIP 8
#define NO_SAVED_LINES 100
#define WK_BUF_SIZE 520
#define SV_BUF_SIZE 5000

static int cbreak_crmod = 1;/* crmod on */
static int fd=0;              /* file number for standard in */

#define N_SEQS       6      /* number of special sequences */
#define MAX_SEQ_LEN  10     /* max chars in special termcap seqs */
#define ESC          0x01b
                            /* declare and define initial sequences. They
                             * may be overwritten by termcap entries */
static char seqs[N_SEQS][MAX_SEQ_LEN] = {
   { 0x1b, 0x5b, 0x41, 0x00 },   /* up arrow */
   { 0x1b, 0x5b, 0x42, 0x00 },   /* down arrow */
   { 0x1b, 0x5b, 0x44, 0x00 },   /* left arrow */
   { 0x1b, 0x5b, 0x43, 0x00 },    /* right arrow */
   { 0x1b, 0x3c, 0x00, 0x00 },   /* search backward*/
   { 0x1b, 0x3e, 0x00, 0x00 }    /* search forward */
};
static int key_map[] = {UP_ARROW, DOWN_ARROW, LEFT_ARROW, RIGHT_ARROW, SEARCH_BACKWARD, SEARCH_FORWARD};


#ifdef TERMCAP
                            /* termcap capability string for arrow up,
                             * down, left, and right */
static char *tc_capabilities[] = { "ku", "kd", "kl", "kr", "" };
static char strings[128];   /* place to store output strings from termcap
                             * file */
static char *KS;            /* enable keypad */
static char *KE;            /* disable keypad */
static char *CE;            /* clear to end of line */
static char *BC;            /* backspace */
static char *IM;            /* start insert mode */
static char *IC;            /* insert character */
static char *EI;            /* end insert mode */
static char *CL;            /* clear screen */
#endif

static char sv_buf[SV_BUF_SIZE];
static char yank_buf[WK_BUF_SIZE + 1];
static char *sv_lines[NO_SAVED_LINES] = { NULL, NULL };
static char *sv_buf_point = sv_buf;
static char tosearch[SV_BUF_SIZE] = "";/* place to store search string */
static int last_line = 1;
static int insert_flag = 1;
static int init_flag = TRUE;
static int tty;
static    int cur_line_number = 0;

static void move_right(), move_left(), display_string();
static void get_line(), save_line(), backspace(), erase_nchar();
static void enable_keypad_mode(), disable_keypad_mode();
static void init_io(), set_cbreak(), set_crmod();
static void strip_blank();
static int  lines_equal();
static int  translate();
static int  search_line_backward(),search_line_forward();
void  set_echo_mode(),set_is_reading();
int   get_echo_mode();
/***********************************************************************
 * line editor
 **********************************************************************/
extern void
C2F(zzledt)(buffer, buf_size, len_line, eof, dummy1)
/**********************************************************************/
char *buffer;
int *buf_size;
int *len_line;
int *eof;
long int dummy1;                /* added by FORTRAN to give buffer length */
{
   int line_index = 0;
   int line_number;
   int cursor_max = 0;
   int cursor = 0;
   int yank_len,i;
   int keystroke;
   int character_count;
   char wk_buf[WK_BUF_SIZE + 1];

   if(init_flag) {
      init_io();
      init_flag = FALSE;
   }

   set_is_reading(TRUE);

   /* if not an interactive terminal */
   if(!tty) {
     /* read a line into the buffer, but not too
      * big */
     /** fputs("[file]",stdout); **/
     fputs("-->",stdout);
     *eof = (fgets(buffer, *buf_size, stdin) == NULL);
     *len_line = strlen(buffer);
     /* remove newline character if there */
     if(buffer[*len_line - 1] == '\n')
       (*len_line)--;
     return;
   }

#ifdef KEYPAD 
   set_cbreak();
   enable_keypad_mode();
#endif

                            /* write prompt */
   printf(Sci_Prompt);
                            /* empty work buffer */
    wk_buf[0] = NUL;

                            /* main loop to read keyboard input */
   while(1) {
                            /* get next keystroke (no echo) */
      keystroke = gchar_no_echo();
 
      if(iscntrl(keystroke) || keystroke > 0x0100 ) {

                            /* stroke is line editing command */
         switch(keystroke) {

         case UP_ARROW:
         case DOWN_ARROW:
	 case CTRL_P:
	 case CTRL_N:

            if(keystroke == UP_ARROW || keystroke == CTRL_P) {
                            /* pick out previous line, if any */
               if(line_index < last_line) line_index++;
            }
            else {
                            /* pick out following line, if any */
               if(line_index > 0) line_index--;
            }
                            /* get a new line from the save buffer */
            get_line(line_index, wk_buf);
                            /* backspace to beginning of line */
            backspace(cursor);
                            /* copy to screen */
            display_string(wk_buf);
                            /* cursor set at end of line */
            cursor = strlen(wk_buf);
                            /* erase extra characters left over,
			     * if any */
            erase_nchar(MAX(0, cursor_max - cursor));
            cursor_max = cursor;
            break;

         case LEFT_ARROW:
	 case CTRL_B:
                            /* if room to move left */
            if(cursor > 0) {
               cursor--;
               backspace(1);
            }
	    else {
	       putchar(BEL);
            }
            break;

         case RIGHT_ARROW:
	 case CTRL_F:
                            /* if room to move right */
            if(cursor < cursor_max) {
               putchar(wk_buf[cursor++]);
            }
	    else {
	       putchar(BEL);
            }
            break;

         case HOME:
                            /* move to start of line */
            backspace(cursor);
            cursor = 0;
            break;

         case ENDS:
                            /* move to end of line */
            while(cursor < cursor_max) {
               putchar(wk_buf[cursor++]);
            }
            break;

         case INS:
                            /* toggle insert/overwrite flag */
            insert_flag = !insert_flag;
            break;
		    
	 case CTRL_C:
	    /* we must never be here */
	    break;

	 case CTRL_D:
	                    /* delete next character*/
            if(cursor == cursor_max) {
			    /* reminder that backing up over edge */
	       putchar(BEL);
               break;
	    }
            move_left(&wk_buf[cursor]);
            cursor_max--;
			    /* and write rest of line to end */
            display_string(&wk_buf[cursor]);
			    /* erase extra character now at end. */
	    erase_nchar(1);
			    /* backspace to proper cursor position */
            backspace(cursor_max - cursor);
            break;

         case DEL:
         case BS:
                            /* backspace with delete */
            if(cursor == 0) {
			    /* reminder that backing up over edge */
	       putchar(BEL);
               break;
	    }
                            /* move string in work, one left from
			     * cursor */
            move_left(&wk_buf[cursor - 1]);
            cursor_max--;
            cursor--;
	    backspace(1);
			    /* and write rest of line to end */
            display_string(&wk_buf[cursor]);
			    /* erase extra character now at end. */
	    erase_nchar(1);
			    /* backspace to proper cursor position */
            backspace(cursor_max - cursor);
            break;

	 case CTRL_K:
	                    /* delete to end of line*/
            if(cursor == cursor_max) {
			    /* reminder that backing up over edge */
	       putchar(BEL);
               break;
	    }
			    /* erase  character  at end. */
	    erase_nchar(cursor_max - cursor);
                            /* store cutted part */
            strcpy(yank_buf,&wk_buf[cursor]);
			    /* backspace to proper cursor position */
            wk_buf[cursor] = NUL;
            cursor_max = cursor;
            break;

	  case CTRL_Y:
	    yank_len=strlen(yank_buf);
            if(yank_len!=0 ){
              if (cursor==cursor_max) {
		strcpy(&wk_buf[cursor],yank_buf);
		display_string(&wk_buf[cursor]);
		cursor = cursor_max + yank_len;
		cursor_max = cursor;

	      }
	      else {
		for(i = 0; i <= cursor_max-cursor; i++)
		  wk_buf[cursor_max+yank_len-i]=wk_buf[cursor_max-i];
                wk_buf[cursor_max+yank_len]=NUL;
		strncpy(&wk_buf[cursor],yank_buf,yank_len);
		erase_nchar(cursor_max - cursor);
		display_string(&wk_buf[cursor]);
		backspace(cursor_max-cursor);
                cursor_max=cursor_max+yank_len;
                cursor=cursor+yank_len;
	      }

	    }
            break;

#ifdef TERMCAP
         case CTRL_L:

            fputs(CL,stdout);
            wk_buf[0]=NUL;
	    goto exit;
#endif

         case LDEL:
                            /* clear line buffer */
            backspace(cursor);
            erase_nchar(cursor_max);
            wk_buf[0] = NUL;
            cursor = cursor_max = 0;
            break;

         case CR:
         case LF:
	    cur_line_number = 0;
                            /* carrage return indicates line is ok;
                             * first strip any trailing blanks */
            strip_blank(wk_buf);
            if (wk_buf[0]==EXCL) {
              strcpy(tosearch,wk_buf);
              line_number = search_line_backward(tosearch);
	      if(line_number == 0) {
		putchar(BEL);
	      }
              else {
		cur_line_number=line_number;
		strcpy(wk_buf,sv_lines[line_number]);
		backspace(cursor);
		/* copy to screen */
		display_string(wk_buf);
		/* cursor set at end of line */
		cursor = strlen(wk_buf);
		/* erase extra characters left over,
		 * if any */
		erase_nchar(MAX(0, cursor_max - cursor));
		cursor_max = cursor;
	      }
	      break;
	    }
	    if(get_echo_mode()==1)   {
		/* if this line differs from last line */
		if(lines_equal(wk_buf, 1) == FALSE) {
		    /* put in save buffer */
		    save_line(wk_buf);
		}
	    }
            goto exit;

	  case SEARCH_BACKWARD:
            if(tosearch[0]!=EXCL) {
	      putchar(BEL);
	    }
	    else {
              line_number = search_line_backward(tosearch);
	      if(line_number == 0) {
		putchar(BEL);
	      }
              else {
		cur_line_number=line_number;
		strcpy(wk_buf,sv_lines[line_number]);
		backspace(cursor);
		/* copy to screen */
		display_string(wk_buf);
		/* cursor set at end of line */
		cursor = strlen(wk_buf);
		/* erase extra characters left over,
		 * if any */
		erase_nchar(MAX(0, cursor_max - cursor));
		cursor_max = cursor;
	      }
	    }
	    break;

	  case SEARCH_FORWARD:
            if(tosearch[0]!=EXCL) {
	      putchar(BEL);
	    }
	    else {
              line_number = search_line_forward(tosearch);
	      if(line_number == 0) {
		putchar(BEL);
	      }
              else {
		cur_line_number=line_number;
		strcpy(wk_buf,sv_lines[line_number]);
		backspace(cursor);
		/* copy to screen */
		display_string(wk_buf);
		/* cursor set at end of line */
		cursor = strlen(wk_buf);
		/* erase extra characters left over,
		 * if any */
		erase_nchar(MAX(0, cursor_max - cursor));
		cursor_max = cursor;
	      }
	    }
	    break;

         default:
	    putchar(BEL);
            break;

         }

      } 
      else {
                            /* alpha/numeric keystroke.
                             * substitute blank fill for tab char */
         if(keystroke == '\t') {
            keystroke = ' ';
            character_count = TAB_SKIP - (cursor%TAB_SKIP);
            if(character_count == 0)
               character_count = TAB_SKIP;
         }
         else {
	   if(keystroke == EOF) {
	     character_count = 0;
	   }
	   else {
	     character_count = 1;
	   }
         }
         while(character_count--) {
	     if(get_echo_mode()==0) {
		 wk_buf[cursor] = keystroke;
		 cursor++;
	     }
	     else {
		 if(insert_flag) {
		     /* insert mode, move rest of line right and
		      * add character at cursor */
		     move_right(&wk_buf[cursor], WK_BUF_SIZE - cursor);
		     /* bump max cursor but not over buffer
		      * size */
		     cursor_max = (++cursor_max > WK_BUF_SIZE)
			 ?WK_BUF_SIZE : cursor_max;
		     /* if cursor at end of line, backspace so
		      * that new character overwrites last one */
		     if(cursor == WK_BUF_SIZE) {
			 cursor--;
			 backspace(1);
		     }
		     wk_buf[cursor] = keystroke;
		     display_string(&wk_buf[cursor]);
		     cursor++;
		     backspace(cursor_max - cursor);
               
		 } 
		 else {
		     /* overstrike mode */
		     if(cursor == WK_BUF_SIZE) {
			 cursor--;
			 backspace(1);
		     }
		     wk_buf[cursor] = keystroke;
		     putchar(keystroke);
		     if(cursor < WK_BUF_SIZE - 1) {
			 cursor++;
			 cursor_max = MAX(cursor_max, cursor);
		     }
		     else {
			 backspace(1);
		     }
		 }
	     }
         }
     }
  }

 exit:
   /* copy to return buffer */
   if(get_echo_mode()==0)  
	   {
	       *len_line=cursor;
	       strncpy(buffer,wk_buf,*buf_size);
	       set_echo_mode(TRUE);
	       wk_buf[0] = NUL;
	   }
   else {
       strcpy(buffer, wk_buf);
       *len_line = strlen(wk_buf);
       putchar('\r'); /** jpc avril 97 **/
       putchar('\n');
   }
#ifdef KEYPAD
   set_crmod();
   disable_keypad_mode();
#endif
   *eof = FALSE;
   set_is_reading(FALSE);
   return;

}

/***********************************************************************
 * move_right - move source string to one address larger (right)
 **********************************************************************/
static void
move_right(source, max_chars)
/**********************************************************************/
char *source;
int  max_chars;
{
   char *p;

   p = source;              /* point to beginning */
   while(max_chars-- && *p++) /* increment until max chars or string
			       * end */
      ;
   *p = NUL;                /* force new string end */
   while(--p > source) {    /* move from rightmost edge to right */
      *p = *(p - 1);
   }
}

/***********************************************************************
 * move_left - move source string to one address less (left)
 **********************************************************************/
static void
move_left(source)
/**********************************************************************/
char *source;
{
   do {                     /* move from left edge to left */
      *source = *(source + 1);
   } while(*source++ != NUL);
}

/***********************************************************************
 * display_string - display string starting at current cursor position
 **********************************************************************/

static void
display_string(string)
     char *string;
{
   while(*string != NUL) {
      putchar(*string++);
   }
}
/***********************************************************************
 * get_line()
 **********************************************************************/

static void
get_line(line_index, source)
     int  line_index;
     char *source;
{
   char *p;
                            /* pointer to line in save buffer */
   p = sv_lines[line_index];
                            /* if NUL, it-s an empty line */
   if(p == NULL) {
      *source = NUL;
      return;
   }
   while(*p != NUL) {
                            /* move each character from save buffer */
      *source++ = *p++;
                            /* if at end of save buffer, wrap */
      if(p - sv_buf == SV_BUF_SIZE) p = sv_buf;
   }
                            /* add terminator to line */
   *source = NUL;
}
/***********************************************************************
 * save_line - save edited line in next position in save buffer
 **********************************************************************/
static void
save_line(source)
/**********************************************************************/
char *source;
{
   int i;
   int length;
                            /* if empty line, don-t save it */
   if(*source == NUL) return;
                            /* bump saved line count but not over max */
   if(last_line < NO_SAVED_LINES - 1) last_line++;
                            /* point last to nothing */
   sv_lines[last_line] = NULL;
                            /* move up rest to make space at slot one */
   for(i = last_line - 1; i > 1; i--) {
      sv_lines[i] = sv_lines[i - 1];
   }
                            /* point slot one to where new line
			     * is going */
   sv_lines[1] = sv_buf_point;
                            /* move active buffer to save with wrap */
   for(length = 0; *source != NUL; length++) {
      *sv_buf_point = *source++;
                            /* bump save buffer pointer and if at end of
                             * buffer, wrap back to zero */
      if((++sv_buf_point - sv_buf) == SV_BUF_SIZE)
         sv_buf_point = sv_buf;
   }
                            /* terminate string in save buffer and leave
			     * pointer ready for next time */
   *sv_buf_point++ = NUL;
                            /* check if any lines point into new line */
   for(i = last_line; i > 1; i--) {
                            /* if not pointing anywhere */
      if(sv_lines[i] == NULL)
         continue;
                            /* if pointing into new line */
      if((sv_lines[i] - sv_lines[1] + SV_BUF_SIZE)%SV_BUF_SIZE
						<= length + 1) {
         sv_lines[i] = NULL;
			    /* and reduce last saved line */
	 last_line = i;
      }
   }
}
/***********************************************************************
 * backspace - move cursor n char to the left
 **********************************************************************/
static void
backspace(n)
/**********************************************************************/
int n;
{
   if(n < 1)
      return;

   while(n--)
#ifdef TERMCAP
   if(BC) {                 /* if control-H won-t work */
      fputs(BC, stdout);
   }
   else {                   /* otherwise just use a normal control-H */
      putchar('\010');
   }
#else
      putchar('\010');
#endif

}

/***********************************************************************
 * erase n characters to right and back up cursor
 **********************************************************************/
static void
erase_nchar(n)
/**********************************************************************/
int n;
{
   int i;                   /* fill field with blanks */
   for(i = 0; i < n; i++) {
      putchar(' ');
   }
   backspace(n);            /* and back up over blanks just written */
}
/***********************************************************************
 * lines_equal(source, line_index)
 **********************************************************************/
static int
lines_equal(source, line_index)
/**********************************************************************/
char *source;
int  line_index;
{
   char *p;

                            /* if empty line, matches anything */
   if(*source == NUL) return(TRUE);
                            /* point to line start in save buffer */
   p = sv_lines[line_index];
                            /* if line empty, report not equal */
   if(p == NULL) return(FALSE);
   while(*source != NUL) {
                            /* if not equal, return false */
      if(*source++ != *p++) {
         return(FALSE);
      }
                            /* if outside buffer, wrap to beginning */
      if(p - sv_buf == SV_BUF_SIZE) {
         p = sv_buf;
      }
   }
   return (*p == NUL) ? TRUE : FALSE;
}
/***********************************************************************
 * strip_blank(source) - strip trailing blanks by inserting NUL-s
 **********************************************************************/
static void
strip_blank(source)
/**********************************************************************/
char *source;
{
   char *p;

   p = source;
                            /* look for end of string */
   while(*p != NUL) {
      p++;
   }
   while(p != source) {
      p--;
      if(*p != ' ') break;
      *p = NUL;
   }
}
/***********************************************************************
 * get sungle character with no echo
 **********************************************************************/
int gchar_no_echo()
/**********************************************************************/
{
   int i;
   extern int ioctl();
                            /* get next character, gotten in cbreak mode
                             * so no wait for <cr> */
   i = Xorgetchar();
                            /* if more than one character */
   if(i == ESC) {
      /* translate control code sequences to codes over 100 hex */
      i = translate(i);
   }
   return(i);
}

/***********************************************************************
 * set CBREAK mode and switch off echo and disable CR action!
 **********************************************************************/
static void
set_cbreak()
/**********************************************************************/
{
   extern int ioctl();

                            /* switch to CBREAK mode without flushing
                             * line buffer */

#ifdef B42UNIX
   arg.sg_flags |= CBREAK;
   arg.sg_flags &= ~ECHO; 
   arg.sg_flags &= ~CRMOD;
   ioctl(fd, TIOCSETN, &arg);
#endif

#ifdef ATTUNIX
   arg.c_lflag &= ~ICANON;
   arg.c_lflag &= ~ECHO;
   arg.c_iflag &= ~ICRNL;
   arg.c_oflag &= ~OPOST;
   arg.c_cc [VEOF] = 1;
   arg.c_cc [VEOL] = 2;
   ioctl(fd, TCSETAW, &arg);
#endif

   cbreak_crmod = 0;

   return;
}

/***********************************************************************
 * reset to original mode 
 **********************************************************************/
static void
set_crmod()

/**********************************************************************/
{
   extern int ioctl();
                            /* reset to original mode (CRMOD) */

#ifdef B42UNIX
   arg.sg_flags = save_sg_flags;
   ioctl(fd, TIOCSETN, &arg);
   arg1.t_intrc = 3;
   ioctl(fd, TIOCSETC, &arg1);
#endif

#ifdef ATTUNIX
   ioctl(fd, TCSETAW, &save_term);
#endif
   cbreak_crmod = 1;

   return;
}

/***********************************************************************
 * translate escape sequences
 **********************************************************************/
static int
translate(ichar)
/**********************************************************************/
int ichar;
{
   int i, j, not_done;
   char *pstr[N_SEQS];      /* points to each sequence as it progresses */

                            /* initialize pointer array */
   for(i = 0; i < N_SEQS; i++)
      pstr[i] = &seqs[i][0];
                            /* examine all pstrings one char at a time */
   for(j=0; j++ < MAX_SEQ_LEN;) {

      not_done = 0;
      for(i = 0; i < N_SEQS; i++) {
                            /* if matches next character, this sequence */
         if(ichar == *pstr[i]) {
                            /* if next character, this sequence, null */
            if(!*(++pstr[i]))
                            /* return sequence mapped to integer */
               return(key_map[i]);
            else
                            /* flag not done with this sequence yet */
               not_done = 1;
         }
      }
                            /* if any sequence not finished yet */
      if(not_done) {
            ichar = Xorgetchar();
      }
      else {
                            /* hopefully at first character */
         break;
      }
   }

   return(ichar);
}

/************************************************************************
 * initialise the io sequences
 ***********************************************************************/
static void
init_io()
/***********************************************************************/
{
   int  tgetent();
   char *getenv();
   char *tgetstr();
   char tc_buf[1024];       /* holds termcap buffer */
   char *area;
   char erase_char;
   int i;
   extern int ioctl();

                            /* check standard for interactive */
   fd=fileno(stdin);
   tty = isatty(fileno(stdin));
   if (tty == 0) return;

#ifdef B42UNIX
   ioctl(fd,TIOCGETP,&arg);
   save_sg_flags = arg.sg_flags;
   ioctl(fd,TIOCGETC,&arg1);

   erase_char = arg.sg_erase;
#endif

#ifdef ATTUNIX
   ioctl(fd, TCGETA, &arg);
   ioctl(fd, TCGETA, &save_term);

   erase_char = save_term.c_cc [VERASE];
#endif

#ifdef TERMCAP
                            /* get termcap translations */
   if(tgetent(tc_buf, getenv("TERM")) == 1) {
                            /* loop thru zero terminated list of input
                             * capabilities */
      for(i = 0; *tc_capabilities[i]; i++) {
         area = &seqs[i][0];
         tgetstr(tc_capabilities[i], &area);
      }
      area = strings;       /* point to place where strings are to
			     * be stored */
      KS = tgetstr("ks", &area);
      KE = tgetstr("ke", &area);
      CE = tgetstr("ce", &area);
      BC = tgetstr("bc", &area);
      IM = tgetstr("im", &area);
      IC = tgetstr("ic", &area);
      EI = tgetstr("ei", &area);
      CL = tgetstr("cl", &area);
   }
#endif
}
#ifdef TERMCAP
/************************************************************************
 * enable keypad mode if using termcap
 ***********************************************************************/
static void
enable_keypad_mode()
/***********************************************************************/
{
                            /* enable keypad transmit mode */
   if(KS && *KS)
      fputs(KS, stdout);
}
/************************************************************************
 * disable the keypad mode if using termcap
 ***********************************************************************/
static void
disable_keypad_mode()
/***********************************************************************/
{
                            /* disable keypad transmit mode */
   if(KE && *KE)
      fputs(KE, stdout);
}
#else 
/************************************************************************
 *wee need references to thoses function if using KEYPAD but not Having TERMCAP
 ***********************************************************************/
static void
enable_keypad_mode(){}

static void
disable_keypad_mode(){}
#endif

 
/***********************************************************************
 * search_line_backward(source)
 **********************************************************************/
static int
search_line_backward(source)
/**********************************************************************/
char *source;
{
   int  line_index,ok,length;
   char *p,*p1;
   
   length=strlen(source)-1;
   ok=0;
   if (length == 0) {
     return(0);
   }

   p1 = source;
   p1++;
   for(line_index = cur_line_number+1 ; line_index < last_line; line_index++) {

     p = sv_lines[line_index];
     if(strncmp(p1,p,length) == 0) {
       ok = 1;
       break;
     }
   }

   if(ok==1) {
/*     strcpy(source,p);*/
     return(line_index);
   }
   else {
     return(0);
   }
}

/***********************************************************************
 * search_line_forward(source)
 **********************************************************************/
static int
search_line_forward(source)
/**********************************************************************/
char *source;
{
   int  line_index,ok,length;
   char *p,*p1;
   
   length=strlen(source)-1;
   ok=0;
   if (length == 0) {
     return(0);
   }

   p1 = source;
   p1++;
   for(line_index = cur_line_number-1 ; line_index > 1; line_index--) {

     p = sv_lines[line_index];
     if(strncmp(p1,p,length) == 0) {
       ok = 1;
       break;
     }
   }

   if(ok==1) {
     return(line_index);
   }
   else {
     return(0);
   }
}


void C2F(setprlev)(pause)
     int *pause;
{
  if ( *pause == 0 ) 
    sprintf(Sci_Prompt,"-->");
  else 
    sprintf(Sci_Prompt,"-%d->",*pause);
}

     
#endif /** WIN32 **/