/[pinfo]/pinfo/src/readlinewrapper.c
ViewVC logotype

Contents of /pinfo/src/readlinewrapper.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.1 - (show annotations) (download)
Tue Feb 22 16:24:56 2005 UTC (8 years, 3 months ago) by bas
Branch: MAIN
CVS Tags: PINFO_0_6_8, HEAD
File MIME type: text/plain
Import of last release 0.6.8
1 #include "common_includes.h"
2
3 RCSID ("$Id: readlinewrapper.c,v 1.1 2005/02/22 16:24:56 bas Exp $")
4
5 char **rlhistory = 0;
6 int rlhistorylen = 0;
7 int rlhistorypos = 0;
8
9 #define KEY_BCKSPC 8
10
11 char *
12 readlinewrapper (char *prompt)
13 {
14 char *buf = xmalloc (1024); /* initial buffer for the read line */
15 int origx, origy; /* start coords of input line */
16 int cursor = 0; /* cursor position in input string */
17 int key = 0, i; /* key - a variable for getch() */
18 buf[0] = 0; /* initial value of line - "" */
19 addstr (prompt); /* print prompt */
20 getyx (stdscr, origy, origx); /* get origx,origy coordinates */
21 noecho (); /* turn off echoing chars by getch() */
22 mvhline (origy, origx, ' ', maxx - origx); /* create input line bar */
23
24 rlhistorylen++; /* history entry for this line */
25 rlhistorypos = rlhistorylen; /* move history pos to current entry */
26 if (!rlhistory)
27 rlhistory = xmalloc (sizeof (char *)); /* alloc memory for
28 this entry */
29 else
30 rlhistory = xrealloc (rlhistory, sizeof (char *) * rlhistorylen);
31 rlhistory[rlhistorylen - 1] = xmalloc (1024);
32 strcpy (rlhistory[rlhistorylen - 1], buf); /* and copy there the current
33 value of input line */
34 if(CallReadlineHistory)
35 ungetch(KEY_UP); /* call history to be present */
36
37 while (key != '\n')
38 {
39 key = getch (); /* read key */
40 switch (key)
41 {
42 case KEY_LEFT: /* move cursor left */
43 if (cursor > 0)
44 cursor--;
45 break;
46 case KEY_RIGHT: /* move cursor right */
47 if (cursor < strlen (buf))
48 cursor++;
49 break;
50 case KEY_END:
51 cursor = strlen (buf);
52 break;
53 case KEY_BCKSPC: /* handle backspace: copy all */
54 case KEY_BACKSPACE: /* chars starting from curpos */
55 if (cursor > 0) /* - 1 from buf[n+1] to buf */
56 {
57 for (i = cursor - 1; buf[i] != 0; i++)
58 buf[i] = buf[i + 1];
59 cursor--;
60 }
61 break;
62 case KEY_DC: /* handle delete key. As above */
63 if (cursor <= strlen (buf) - 1)
64 {
65 for (i = cursor; buf[i] != 0; i++)
66 buf[i] = buf[i + 1];
67 }
68 break;
69 case KEY_UP: /* backwards-history call */
70 if (rlhistorylen) /* if there is history */
71 if (rlhistorypos > 1) /* and we have */
72 { /* where to move */
73 rlhistorypos--; /* decrement history position */
74 if (rlhistorypos == rlhistorylen - 1) /*
75 * if the previous
76 * pos was the input
77 * line
78 */
79 strcpy (rlhistory[rlhistorylen - 1], buf); /*
80 * save it's
81 * value to
82 * history
83 */
84 strcpy (buf, rlhistory[rlhistorypos - 1]); /*
85 * recall
86 * value from
87 * history to
88 * input buf
89 */
90 }
91 if (cursor > strlen (buf))
92 cursor = strlen (buf);
93 break;
94 case KEY_DOWN: /* forwards-history call */
95 if (rlhistorylen)
96 if (rlhistorypos < rlhistorylen)
97 {
98 rlhistorypos++;
99 strcpy (buf, rlhistory[rlhistorypos - 1]);
100 }
101 if (cursor > strlen (buf))
102 cursor = strlen (buf);
103 break;
104 /* eliminate nonprintable chars */
105 case '\n':
106 case KEY_PPAGE:
107 case KEY_NPAGE:
108 case KEY_F (1):
109 case KEY_F (2):
110 case KEY_F (3):
111 case KEY_F (4):
112 case KEY_F (5):
113 case KEY_F (6):
114 case KEY_F (7):
115 case KEY_F (8):
116 case KEY_F (9):
117 case KEY_F (10):
118 break;
119 default:
120 if (key >= 32)
121 {
122 if (strlen (buf + cursor)) /* if the cursor is */
123 { /* not at the last pos */
124 char *tmp = 0;
125 tmp = xmalloc (strlen (buf + cursor) + 1);
126 strcpy (tmp, buf + cursor);
127 buf[cursor] = key;
128 buf[cursor + 1] = 0;
129 strcat (&buf[cursor + 1], tmp);
130 xfree (tmp);
131 cursor++;
132 }
133 else
134 {
135 buf[cursor + 1] = 0;
136 buf[cursor] = key;
137 cursor++;
138 }
139 }
140 }
141 move (origy, origx);
142 for (i = origx; i < maxx; i++)
143 addch (' ');
144 move (origy, origx);
145 addstr (buf);
146 move (origy, origx + cursor);
147
148 }
149 strcpy (rlhistory[rlhistorylen - 1], buf);
150 if (strlen (buf))
151 {
152 rlhistory[rlhistorylen - 1] = xrealloc (rlhistory[rlhistorylen - 1],
153 strlen (rlhistory[rlhistorylen - 1]) + 1);
154 }
155 else
156 {
157 xfree (rlhistory[rlhistorylen - 1]);
158 rlhistorylen--;
159 rlhistorypos = rlhistorylen;
160 }
161 buf = xrealloc (buf, strlen (buf) + 1);
162 return buf;
163 }

  ViewVC Help
Powered by ViewVC 1.1.5