File: readline.c

package info (click to toggle)
scilab 2.6-4
  • links: PTS
  • area: non-free
  • in suites: woody
  • size: 54,632 kB
  • ctags: 40,267
  • sloc: ansic: 267,851; fortran: 166,549; sh: 10,005; makefile: 4,119; tcl: 1,070; cpp: 233; csh: 143; asm: 135; perl: 130; java: 39
file content (1139 lines) | stat: -rw-r--r-- 25,259 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
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139


/*******************************************
 * Original source : GNUPLOT - readline.c 
 * modified for Scilab 
 *******************************************
 *
 * Copyright (C) 1986 - 1993   Thomas Williams, Colin Kelley
 *
 * Permission to use, copy, and distribute this software and its
 * documentation for any purpose with or without fee is hereby granted, 
 * provided that the above copyright notice appear in all copies and 
 * that both that copyright notice and this permission notice appear 
 * in supporting documentation.
 *
 * Permission to modify the software is granted, but not the right to
 * distribute the modified code.  Modifications are to be distributed 
 * as patches to released version.
 *  
 * This software is provided "as is" without express or implied warranty.
 * 
 *
 * AUTHORS
 *
 *   Original Software:
 *     Tom Tkacik
 *   Msdos port and some enhancements:
 *     Gershon Elber and many others.
 *   Scilab port 
 *     Jean-Philippe Chancelier 
 */

/***********************************************************
 * a small portable version of GNU's readline 
 * this is not the BASH or GNU EMACS version of READLINE due to Copyleft restrictions 
 * do not need any terminal capabilities except backspace, 
 * and space overwrites a character 
 * NANO-EMACS line editing facility 
 * printable characters print as themselves (insert not overwrite) 
 * ^A moves to the beginning of the line 
 * ^B moves back a single character 
 * ^E moves to the end of the line 
 * ^F moves forward a single character 
 * ^K kills from current position to the end of line 
 * ^P moves back through history 
 * ^N moves forward through history 
 * ^H and DEL delete the previous character 
 * ^D deletes the current character, or EOF if line is empty 
 * ^L/^R redraw line in case it gets trashed 
 * ^U kills the entire line 
 * ^W kills last word 
 * LF and CR return the entire line regardless of the cursor postition 
 * EOF with an empty line returns (char *)NULL 
 * all other characters are ignored 
 ***********************************************************/

#include <stdio.h>
#include <ctype.h>
#include <signal.h>

#ifdef __CYGWIN32__
#include <unistd.h>		/* for isatty */
#define ISATTY
/* #define SGTTY */
#endif

#if !defined(MSDOS) && !defined(ATARI) && !defined(_Windows) && !defined(DOS386)

extern char *alloc (unsigned long size, char *message);
/*
 * Set up structures using the proper include file
 */
/** 
  XXXX : 
  For cygwin (__CYGWIN32__) 
  [1] the version with SGTTY uncommented works 
  [2] the version with SGTTY commented only works with a patched version 
  of cygwin.dll ( termio patch ) 
  In fact : in the future the sun/zzledt version will also certainly work 
**/

#if defined(_IBMR2) || defined(alliant)
#define SGTTY
#endif

/*  submitted by Francois.Dagorn@cicb.fr */
#ifdef SGTTY
#ifdef __CYGWIN32__
#include <sgtty.h>
static struct sgttyb orig_termio, rl_termio;
/* define terminal control characters */
static struct tchars s_tchars;
static struct ltchars s_ltchars;
#else
#include <sgtty.h>
static struct sgttyb orig_termio, rl_termio;
/* define terminal control characters */
static struct tchars s_tchars;
#define VERASE    0
#define VEOF      1
#define VKILL     2
#ifdef TIOCGLTC			/* available only with the 'new' line discipline */
static struct ltchars s_ltchars;
#define VWERASE   3
#define VREPRINT  4
#define VSUSP     5
#endif /* TIOCGLTC */
#define NCCS      6
#endif /* __CYGWIN32__ */

#else /* SGTTY */

/* SIGTSTP defines job control */
/* if there is job control then we need termios.h instead of termio.h */
/* (Are there any systems with job control that use termio.h?  I hope not.) */
#ifdef SIGTSTP
#define TERMIOS
#include <termios.h>
/* Added by Robert Eckardt, RobertE@beta.TP2.Ruhr-Uni-Bochum.de */
#ifdef ISC22
#ifndef ONOCR			/* taken from sys/termio.h */
#define ONOCR 0000020		/* true at least for ISC 2.2 */
#endif
#ifndef IUCLC
#define IUCLC 0001000
#endif
#endif /* ISC22 */

static struct termios orig_termio, rl_termio;
#else
#include <termio.h>
static struct termio orig_termio, rl_termio;
/* termio defines NCC instead of NCCS */
#define NCCS    NCC
#endif /* SIGTSTP */
#endif /* SGTTY */

/* ULTRIX defines VRPRNT instead of VREPRINT */
#ifdef VRPRNT
#define VREPRINT VRPRNT
#endif

/* define characters to use with our input character handler */
static char term_chars[NCCS];

static int term_set = 0;	/* =1 if rl_termio set */

#define special_getc() ansi_getc()
static char ansi_getc ();

#else /* !MSDOS && !ATARI && !_Windows */

#ifdef _Windows
#ifndef STRICT
#define STRICT
#endif
#include <windows.h>
#ifdef USE_CONSOLE
#include "wtextc.h"
#else
#include "wtext.h"
#endif
#include "wgnuplib.h"
#include "wcommon.h"

extern TW textwin;
#define TEXTUSER 0xf1
#define TEXTGNUPLOT 0xf0
#define special_getc() msdos_getch()
static char msdos_getch ();
#endif

#if defined(MSDOS) || defined(DOS386)
/* MSDOS specific stuff */
#ifdef DJGPP
#include <pc.h>
#endif
#ifdef __EMX__
#include <conio.h>
#endif
#define special_getc() msdos_getch()
static char msdos_getch ();
#endif /* MSDOS */

#ifdef ATARI
#include <stdlib.h>
#ifdef __PUREC__
#include <tos.h>
#else
#include <osbind.h>
#endif
#define special_getc() tos_getch()
static char tos_getch ();
#endif

#endif /* !MSDOS && !ATARI && !_Windows */

#include <string.h>

#define MAXBUF	1024
#define BACKSPACE 0x08		/* ^H */
#define SPACE	' '

struct hist
  {
    char *line;
    struct hist *prev;
    struct hist *next;
  };

static struct hist *history = NULL;	/* no history yet */
static struct hist *cur_entry = NULL;

static char cur_line[MAXBUF];	/* current contents of the line */
static int cur_pos = 0;		/* current position of the cursor */
static int max_pos = 0;		/* maximum character position */

#ifdef USE_CONSOLE
void add_history_nw (char *line);
#else
void add_history_win (char *line);
#endif

static void fix_line ();
static void redraw_line ();
static void clear_line ();
static void clear_eoline ();
static void copy_line ();
static void set_termio ();
static void reset_termio ();

/* user_putc and user_puts should be used in the place of
 * fputc(ch,stdout) and fputs(str,stdout) for all output
 * of user typed characters.  This allows MS-Windows to 
 * display user input in a different color. */

static int 
user_putc (ch)
     int ch;
{
  int rv;
#ifdef _Windows
#ifndef USE_CONSOLE
  TextAttr (&textwin, TEXTUSER);
#endif
#endif
  rv = fputc (ch, stdout);
#ifdef _Windows
#ifndef USE_CONSOLE
  TextAttr (&textwin, TEXTGNUPLOT);
#endif
#endif
  return rv;
}

static int 
user_puts (str)
     char *str;
{
  int rv;
#ifdef _Windows
#ifndef USE_CONSOLE
  TextAttr (&textwin, TEXTUSER);
#endif
#endif
  rv = fputs (str, stdout);
#ifdef _Windows
#ifndef USE_CONSOLE
  TextAttr (&textwin, TEXTGNUPLOT);
#endif
#endif
  return rv;
}

/* This function provides a centralized non-destructive backspace capability */
/* M. Castro */

static void 
backspace ()
{
  user_putc (BACKSPACE);
}


/** TERMIOS : are not properly implemented in cygwin **/
/** so we use a temp function without them for scilab -nw **/
/** XXXXXXXXXXXXXXXXX  **/
/** #define OK_TO_USE_TERMIO **/

#define OK_TO_USE_TERMIO

/** 
  isatty(fileno(f)) : returns an eroneous result if 
  we have compiled cygwin with --subsystem windows 
  **/

#ifdef ISATTY
/** we use isatty with cygwin **/
#define isterm(f) isatty(fileno(f))
#else
/** other windows compiler + mingwin32 **/
#define isterm(f) ((f==stdin && InteractiveMode()==0)|| f==stdout || f==stderr)
#endif

/***************************************************
 * reads one line when stdin is not a tty 
 * the input characters are not echoed 
 ***************************************************/

static int 
NotTTyRead (char *prompt, char *buffer, int buf_size, int *eof)
{
  static int firstentry = 0, tty;
  if (firstentry == 0)
    {
      tty = isterm (stdin);
      firstentry++;
    }
  if (!tty)
    {
/** We are reading a file ==> no prompts : XXXXX to test **/
      fputs ("-->", stdout);
      /* read a line into the buffer, but not too* big */
      *eof = (fgets (buffer, buf_size, stdin) == NULL);
      /* remove newline character if there */
      return (1);
    }
  return (0);
}

/***************************************************
 * reads one line 
 * a pointer to an allocated zone (alloc) where
 *   the read characters are stored is returned 
 ***************************************************/

#ifdef USE_CONSOLE
char *
readline_nw (char *prompt)
#else
char *
readline_win (char *prompt)
#endif
{
  unsigned char cur_char;
  char *new_line;
  /* unsigned char *new_line; */
  int eof;
  if (NotTTyRead (prompt, cur_line, MAXBUF, &eof) == 1)
    {
      if (eof == 1)
	{
/** coded line to return eof **/
	  cur_line[0] = -1;
	  cur_line[1] = '\0';
	}
      new_line = (char *) alloc ((unsigned long)
				 (strlen (cur_line) + 1), "history");
      strcpy (new_line, cur_line);
      return (new_line);
    }
  /* set the termio so we can do our own input processing */
  set_termio ();

  /* print the prompt */
  fputs (prompt, stdout);
  cur_line[0] = '\0';
  cur_pos = 0;
  max_pos = 0;
  cur_entry = NULL;

  /* get characters */
  for (;;)
    {
      cur_char = special_getc ();
#ifdef OS2
      /* for emx: remap scan codes for cursor keys */
      if (cur_char == 0)
	{
	  cur_char = getc (stdin);
	  switch (cur_char)
	    {
	    case 75:		/* left, map to ^B */
	      cur_char = 2;
	      break;
	    case 77:		/* right, map to ^F */
	      cur_char = 6;
	      break;
	    case 115:		/* ctrl left */
	    case 71:		/* home, map to ^A */
	      cur_char = 1;
	      break;
	    case 116:		/* ctrl right */
	    case 79:		/* end, map to ^E */
	      cur_char = 5;
	      break;
	    case 72:		/* up, map to ^P */
	      cur_char = 16;
	      break;
	    case 80:		/* down, map to ^N */
	      cur_char = 14;
	      break;
	    case 83:		/* delete, map to ^D */
	      cur_char = 4;
	      break;
	    default:		/* ignore */
	      cur_char = 0;
	      continue;
	    }
	}
#endif /*OS2 */
      if ((isprint (cur_char)
#if defined(ATARI) || defined(_Windows) || defined(MSDOS) || defined(DOS386)
      /* this should be used for all 8bit ASCII machines, I guess */
	   || ((unsigned char) cur_char > 0x7f)
#endif
	  ) && max_pos < MAXBUF - 1)
	{
	  int i;
	  for (i = max_pos; i > cur_pos; i--)
	    {
	      cur_line[i] = cur_line[i - 1];
	    }
	  user_putc (cur_char);
	  cur_line[cur_pos] = cur_char;
	  cur_pos += 1;
	  max_pos += 1;
	  if (cur_pos < max_pos)
	    fix_line ();
	  cur_line[max_pos] = '\0';

	  /* else interpret unix terminal driver characters */
#ifdef VERASE
	}
      else if (cur_char == term_chars[VERASE])
	{			/* DEL? */
	  if (cur_pos > 0)
	    {
	      int i;
	      cur_pos -= 1;
	      backspace ();
	      for (i = cur_pos; i < max_pos; i++)
		cur_line[i] = cur_line[i + 1];
	      max_pos -= 1;
	      fix_line ();
	    }
#endif /* VERASE */
#ifdef VEOF
	}
      else if (cur_char == term_chars[VEOF])
	{			/* ^D? */
	  if (max_pos == 0)
	    {
	      reset_termio ();
	      return ((char *) NULL);
	    }
	  if ((cur_pos < max_pos) && (cur_char == 004))
	    {			/* ^D */
	      int i;
	      for (i = cur_pos; i < max_pos; i++)
		cur_line[i] = cur_line[i + 1];
	      max_pos -= 1;
	      fix_line ();
	    }
#endif /* VEOF */
#ifdef VKILL
	}
      else if (cur_char == term_chars[VKILL])
	{			/* ^U? */
	  clear_line (prompt);
#endif /* VKILL */
#ifdef VWERASE
	}
      else if (cur_char == term_chars[VWERASE])
	{			/* ^W? */
	  while ((cur_pos > 0) &&
		 (cur_line[cur_pos - 1] == SPACE))
	    {
	      cur_pos -= 1;
	      backspace ();
	    }
	  while ((cur_pos > 0) &&
		 (cur_line[cur_pos - 1] != SPACE))
	    {
	      cur_pos -= 1;
	      backspace ();
	    }
	  clear_eoline ();
	  max_pos = cur_pos;
#endif /* VWERASE */
#ifdef VREPRINT
	}
      else if (cur_char == term_chars[VREPRINT])
	{			/* ^R? */
	  putc ('\n', stdout);	/* go to a fresh line */
	  redraw_line (prompt);
#endif /* VREPRINT */
#ifdef VSUSP
	}
      else if (cur_char == term_chars[VSUSP])
	{
	  reset_termio ();
	  kill (0, SIGTSTP);

	  /* process stops here */

	  set_termio ();
	  /* print the prompt */
	  redraw_line (prompt);
#endif /* VSUSP */
	}
      else
	{
	  /* do normal editing commands */
	  /* some of these are also done above */
	  int i;
	  switch (cur_char)
	    {
	    case 255:		/* jpc eof quand on fait un pipe */
	      new_line = (char *) alloc ((unsigned long) 2,
					 "history");
	      new_line[0] = -1;
	      new_line[1] = '\0';
	      reset_termio ();
	      return (new_line);
	    case EOF:
	      reset_termio ();
	      return ((char *) NULL);
	    case 001:		/* ^A */
	      while (cur_pos > 0)
		{
		  cur_pos -= 1;
		  backspace ();
		}
	      break;
	    case 002:		/* ^B */
	      if (cur_pos > 0)
		{
		  cur_pos -= 1;
		  backspace ();
		}
	      break;
	    case 005:		/* ^E */
	      while (cur_pos < max_pos)
		{
		  user_putc (cur_line[cur_pos]);
		  cur_pos += 1;
		}
	      break;
	    case 006:		/* ^F */
	      if (cur_pos < max_pos)
		{
		  user_putc (cur_line[cur_pos]);
		  cur_pos += 1;
		}
	      break;
	    case 013:		/* ^K */
	      clear_eoline ();
	      max_pos = cur_pos;
	      break;
	    case 020:		/* ^P */
	      if (history != NULL)
		{
		  if (cur_entry == NULL)
		    {
		      cur_entry = history;
		      clear_line (prompt);
		      copy_line (cur_entry->line);
		    }
		  else if (cur_entry->prev != NULL)
		    {
		      cur_entry = cur_entry->prev;
		      clear_line (prompt);
		      copy_line (cur_entry->line);
		    }
		}
	      break;
	    case 016:		/* ^N */
	      if (cur_entry != NULL)
		{
		  cur_entry = cur_entry->next;
		  clear_line (prompt);
		  if (cur_entry != NULL)
		    copy_line (cur_entry->line);
		  else
		    cur_pos = max_pos = 0;
		}
	      break;
	    case 014:		/* ^L */
	    case 022:		/* ^R */
	      putc ('\n', stdout);	/* go to a fresh line */
	      redraw_line (prompt);
	      break;
	    case 0177:		/* DEL */
	    case 010:		/* ^H */
	      if (cur_pos > 0)
		{
		  cur_pos -= 1;
		  backspace ();
		  for (i = cur_pos; i < max_pos; i++)
		    cur_line[i] = cur_line[i + 1];
		  max_pos -= 1;
		  fix_line ();
		}
	      break;
	    case 004:		/* ^D */
	      if (max_pos == 0)
		{
		  reset_termio ();
		  return ((char *) NULL);
		}
	      if (cur_pos < max_pos)
		{
		  for (i = cur_pos; i < max_pos; i++)
		    cur_line[i] = cur_line[i + 1];
		  max_pos -= 1;
		  fix_line ();
		}
	      break;
	    case 025:		/* ^U */
	      clear_line (prompt);
	      break;
	    case 027:		/* ^W */
	      while ((cur_pos > 0) &&
		     (cur_line[cur_pos - 1] == SPACE))
		{
		  cur_pos -= 1;
		  backspace ();
		}
	      while ((cur_pos > 0) &&
		     (cur_line[cur_pos - 1] != SPACE))
		{
		  cur_pos -= 1;
		  backspace ();
		}
	      clear_eoline ();
	      max_pos = cur_pos;
	      break;
	    case '\n':		/* ^J */
	    case '\r':		/* ^M */
	      cur_line[max_pos + 1] = '\0';
	      putc ('\n', stdout);
	      new_line = (char *) alloc ((unsigned long) (strlen (cur_line) + 1),
					 "history");
	      strcpy (new_line, cur_line);
	      reset_termio ();
	      return (new_line);
	    default:
	      break;
	    }
	}
    }
}

/* fix up the line from cur_pos to max_pos */
/* do not need any terminal capabilities except backspace, */
/* and space overwrites a character */

static void 
fix_line ()
{
  int i;

  /* write tail of string */
  for (i = cur_pos; i < max_pos; i++)
    user_putc (cur_line[i]);

  /* write a space at the end of the line in case we deleted one */
  user_putc (SPACE);

  /* backup to original position */
  for (i = max_pos + 1; i > cur_pos; i--)
    backspace ();

}

/* redraw the entire line, putting the cursor where it belongs */
static void
redraw_line (prompt)
     char *prompt;
{
  int i;

  fputs (prompt, stdout);
  user_puts (cur_line);

  /* put the cursor where it belongs */
  for (i = max_pos; i > cur_pos; i--)
    backspace ();
}

/* clear cur_line and the screen line */
static void
clear_line (prompt)
     char *prompt;
{
  int i;
  for (i = 0; i < max_pos; i++)
    cur_line[i] = '\0';

  for (i = cur_pos; i > 0; i--)
    backspace ();

  for (i = 0; i < max_pos; i++)
    putc (SPACE, stdout);

  putc ('\r', stdout);
  fputs (prompt, stdout);

  cur_pos = 0;
  max_pos = 0;
}

/* clear to end of line and the screen end of line */
static void
clear_eoline (prompt)
     char *prompt;
{
  int i;
  for (i = cur_pos; i < max_pos; i++)
    cur_line[i] = '\0';

  for (i = cur_pos; i < max_pos; i++)
    putc (SPACE, stdout);
  for (i = cur_pos; i < max_pos; i++)
    backspace ();
}

/* copy line to cur_line, draw it and set cur_pos and max_pos */
static void
copy_line (line)
     char *line;
{
  strcpy (cur_line, line);
  user_puts (cur_line);
  cur_pos = max_pos = strlen (cur_line);
}

/* add line to the history */

#ifdef USE_CONSOLE
void 
add_history_nw (char *line)
#else
void 
add_history_win (char *line)
#endif
{
  struct hist *entry;
  entry = (struct hist *) alloc ((unsigned long) sizeof (struct hist), "history");
  entry->line = alloc ((unsigned long) (strlen (line) + 1), "history");
  strcpy (entry->line, line);

  entry->prev = history;
  entry->next = NULL;
  if (history != NULL)
    {
      history->next = entry;
    }
  history = entry;
}

#if !defined(MSDOS) && !defined(_Windows) && !defined(DOS386)

/* Convert ANSI arrow keys to control characters */
static char
ansi_getc ()
{
  char c = getc (stdin);
  if (c == 033)
    {
      c = getc (stdin);		/* check for CSI */
      if (c == '[')
	{
	  c = getc (stdin);	/* get command character */
	  switch (c)
	    {
	    case 'D':		/* left arrow key */
	      c = 002;
	      break;
	    case 'C':		/* right arrow key */
	      c = 006;
	      break;
	    case 'A':		/* up arrow key */
	      c = 020;
	      break;
	    case 'B':		/* down arrow key */
	      c = 016;
	      break;
	    }
	}
    }
  return c;
}
#endif

#if defined(MSDOS) || defined(_Windows) || defined(DOS386)

/* Convert Arrow keystrokes to Control characters: */
static char
msdos_getch ()
{
#ifdef DJGPP
  char c;
  int ch = getkey ();
  c = (ch & 0xff00) ? 0 : ch & 0xff;
#else
  char c = _getch ();
#endif
  if (c == 3)
    {
/** control-C : we return a \n for stopping line processing **/
      SignalCtrC ();
      return ((int) '\n');
    }
  if (c == 0)
    {
#ifdef DJGPP
      c = ch & 0xff;
#else
      c = _getch ();		/* Get the extended code. */
#endif
      switch (c)
	{
	case 75:		/* Left Arrow. */
	  c = 002;
	  break;
	case 77:		/* Right Arrow. */
	  c = 006;
	  break;
	case 72:		/* Up Arrow. */
	  c = 020;
	  break;
	case 80:		/* Down Arrow. */
	  c = 016;
	  break;
	case 115:		/* Ctl Left Arrow. */
	case 71:		/* Home */
	  c = 001;
	  break;
	case 116:		/* Ctl Right Arrow. */
	case 79:		/* End */
	  c = 005;
	  break;
	case 83:		/* Delete */
	  c = 004;
	  break;
	default:
	  c = 0;
	  break;
	}
    }
  else if (c == 033)
    {				/* ESC */
      c = 025;
    }
  return c;
}

#endif /* MSDOS */

#ifdef ATARI

/* Convert Arrow keystrokes to Control characters: TOS version */

/* the volatile could be necessary to keep gcc from reordering 
   the two Super calls
 */
#define CONTERM ((/*volatile*/ char *)0x484L)

static void
remove_conterm ()
{
  void *ssp = (void *) Super (0L);
  *CONTERM &= ~0x8;
  Super (ssp);
}

static char
tos_getch ()
{
  long rawkey;
  char c;
  int scan_code;
  void *ssp;
  static int init = 1;
  static int in_help = 0;

  if (in_help)
    {
      switch (in_help)
	{
	case 1:
	case 5:
	  in_help++;
	  return 'e';
	case 2:
	case 6:
	  in_help++;
	  return 'l';
	case 3:
	case 7:
	  in_help++;
	  return 'p';
	case 4:
	  in_help = 0;
	  return 0x0d;
	case 8:
	  in_help = 0;
	  return ' ';
	}
    }

  if (init)
    {
      ssp = (void *) Super (0L);
      if (!(*CONTERM & 0x8))
	{
	  *CONTERM |= 0x8;
	}
      else
	{
	  init = 0;
	}
      (void) Super (ssp);
      if (init)
	{
	  atexit (remove_conterm);
	  init = 0;
	}
    }

  (void) Cursconf (1, 0);	/* cursor on */
  rawkey = Cnecin ();
  c = (char) rawkey;
  scan_code = ((int) (rawkey >> 16)) & 0xff;	/* get the scancode */
  if (rawkey & 0x07000000)
    scan_code |= 0x80;		/* shift or control */

  switch (scan_code)
    {
    case 0x62:			/* HELP         */
      if (max_pos == 0)
	{
	  in_help = 1;
	  return 'h';
	}
      else
	{
	  return 0;
	}
    case 0xe2:			/* shift HELP   */
      if (max_pos == 0)
	{
	  in_help = 5;
	  return 'h';
	}
      else
	{
	  return 0;
	}
    case 0x48:			/* Up Arrow */
      return 0x10;		/* ^P */
    case 0x50:			/* Down Arrow */
      return 0x0e;		/* ^N */
    case 0x4b:			/* Left Arrow */
      return 0x02;		/* ^B */
    case 0x4d:			/* Right Arrow */
      return 0x06;		/* ^F */
    case 0xcb:			/* Shift Left Arrow */
    case 0xf3:			/* Ctrl Left Arrow (TOS-bug ?) */
    case 0x47:			/* Home */
      return 0x01;		/* ^A */
    case 0xcd:			/* Shift Right Arrow */
    case 0xf4:			/* Ctrl Right Arrow (TOS-bug ?) */
    case 0xc7:			/* Shift Home */
    case 0xf7:			/* Crtl Home */
      return 0x05;		/* ^E */
    case 0x61:			/* Undo - redraw line */
      return 0x0c;		/* ^L */
    default:
      if (c == 0x1b)
	return 0x15;		/* ESC becomes ^U */
      if (c == 0x7f)
	return 0x04;		/* Del becomes ^D */
      break;
    }

  return c;
}

#endif /* ATARI */

/* set termio so we can do our own input processing */
static void
set_termio ()
{
#if defined(OK_TO_USE_TERMIO)
#if !defined(MSDOS) && !defined(ATARI) && !defined(_Windows) && !defined(DOS386)
  /* set termio so we can do our own input processing */
  /* and save the old terminal modes so we can reset them later */
  if (term_set == 0)
    {
      /*
       * Get terminal modes.
       */
#ifdef SGTTY
      ioctl (0, TIOCGETP, &orig_termio);
#else /* SGTTY */
#ifdef TERMIOS
#ifdef TCGETS
      ioctl (0, TCGETS, &orig_termio);
#else
      tcgetattr (0, &orig_termio);
#endif /* TCGETS */
#else
      ioctl (0, TCGETA, &orig_termio);
#endif /* TERMIOS */
#endif /* SGTTY */

      /*
       * Save terminal modes
       */
      rl_termio = orig_termio;

      /*
       * Set the modes to the way we want them
       *  and save our input special characters
       */
#ifdef SGTTY
      /* XXXXX rl_termio.sg_flags |= CBREAK;
         rl_termio.sg_flags &= ~(ECHO|XTABS); */
      rl_termio.sg_flags &= ~(ECHO);
      ioctl (0, TIOCSETN, &rl_termio);

      ioctl (0, TIOCGETC, &s_tchars);
      term_chars[VERASE] = orig_termio.sg_erase;
      term_chars[VEOF] = s_tchars.t_eofc;
      term_chars[VKILL] = orig_termio.sg_kill;
#ifdef TIOCGLTC
      ioctl (0, TIOCGLTC, &s_ltchars);
      term_chars[VWERASE] = s_ltchars.t_werasc;
      term_chars[VREPRINT] = s_ltchars.t_rprntc;
      term_chars[VSUSP] = s_ltchars.t_suspc;

      /* disable suspending process on ^Z */
      s_ltchars.t_suspc = 0;
      ioctl (0, TIOCSLTC, &s_ltchars);
#endif /* TIOCGLTC */
#else /* SGTTY */
      rl_termio.c_iflag &= ~(BRKINT | PARMRK | INPCK | IUCLC | IXON | IXOFF);
      rl_termio.c_iflag |= (IGNBRK | IGNPAR);

      /* rl_termio.c_oflag &= ~(ONOCR); Costas Sphocleous Irvine,CA */

      rl_termio.c_lflag &= ~(ICANON | ECHO | ECHOE | ECHOK | ECHONL | NOFLSH);
#ifdef OS2
      /* for emx: remove default terminal processing */
      rl_termio.c_lflag &= ~(IDEFAULT);
#endif /* OS2 */
      rl_termio.c_lflag |= (ISIG);
      rl_termio.c_cc[VMIN] = 1;
      rl_termio.c_cc[VTIME] = 0;

#ifndef VWERASE
#define VWERASE 3
#endif
      term_chars[VERASE] = orig_termio.c_cc[VERASE];
      term_chars[VEOF] = orig_termio.c_cc[VEOF];
      term_chars[VKILL] = orig_termio.c_cc[VKILL];
#ifdef TERMIOS
      term_chars[VWERASE] = orig_termio.c_cc[VWERASE];
#ifdef VREPRINT
      term_chars[VREPRINT] = orig_termio.c_cc[VREPRINT];
#else
#ifdef VRPRNT
      term_chars[VRPRNT] = orig_termio.c_cc[VRPRNT];
#endif
#endif
      term_chars[VSUSP] = orig_termio.c_cc[VSUSP];

      /* disable suspending process on ^Z */
      rl_termio.c_cc[VSUSP] = 0;
#endif /* TERMIOS */
#endif /* SGTTY */

      /*
       * Set the new terminal modes.
       */
#ifdef SGTTY
      ioctl (0, TIOCSLTC, &s_ltchars);
#else
#ifdef TERMIOS
#ifdef TCSETSW
      ioctl (0, TCSETSW, &rl_termio);
#else
      tcsetattr (0, TCSADRAIN, &rl_termio);
#endif /* TCSETSW */
#else
      ioctl (0, TCSETAW, &rl_termio);
#endif /* TERMIOS */
#endif /* SGTTY */
      term_set = 1;
    }
#endif /* !MSDOS && !ATARI && !defined(_Windows) */
#endif /* #if defined(OK_TO_USE_TERMIO) */
}

static void
reset_termio ()
{
#if defined(OK_TO_USE_TERMIO)
#if !defined(MSDOS) && !defined(ATARI) && !defined(_Windows) && !defined(DOS386)
  /* reset saved terminal modes */
  if (term_set == 1)
    {
#ifdef SGTTY
      ioctl (0, TIOCSETN, &orig_termio);
#ifdef TIOCGLTC
      /* enable suspending process on ^Z */
      s_ltchars.t_suspc = term_chars[VSUSP];
      ioctl (0, TIOCSLTC, &s_ltchars);
#endif /* TIOCGLTC */
#else /* SGTTY */
#ifdef TERMIOS
#ifdef TCSETSW
      ioctl (0, TCSETSW, &orig_termio);
#else
      tcsetattr (0, TCSADRAIN, &orig_termio);
#endif /* TCSETSW */
#else
      ioctl (0, TCSETAW, &orig_termio);
#endif /* TERMIOS */
#endif /* SGTTY */
      term_set = 0;
    }
#endif /* !MSDOS && !ATARI && !_Windows */
#endif /* #if defined(OK_TO_USE_TERMIO) */
}