-
Notifications
You must be signed in to change notification settings - Fork 4
/
rlgetc.c
260 lines (217 loc) · 5.26 KB
/
rlgetc.c
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
#include <stdio.h>
#include <readline/readline.h>
#include <readline/history.h>
#include "eprintf.h"
#include "db.h"
#include "xwin.h"
#include "token.h"
extern char *getwd();
extern char *xmalloc();
char * stripwhite();
static char *lineread = (char *) NULL;
double getdouble();
int lineno = 1;
#include <signal.h>
#include <setjmp.h>
jmp_buf begin;
int indef;
char *infile; /* input file name */
FILE *fin; /* input file pointer */
char **gargv; /* global argument list */
int gargc;
char *rl_gets();
#define MAXHIST 1024
#define HISTORY ".pighist"
/*
main() {
char *s;
while((s=rl_gets(":")) != NULL) {
if (strlen(s) != 1) {
printf("got %s",s);
}
}
exit(0);
}
*/
/* *************************************************** */
/* some routines to implement command line history ... */
/* *************************************************** */
char *prompt = (char *) NULL;
char *saveprompt = (char *) NULL;
char *rl_saveprompt() {
if (prompt != (char *) NULL) {
if (saveprompt != (char *) NULL) {
free(saveprompt);
}
saveprompt = (char *) estrdup(prompt);
} else {
saveprompt = (char *) estrdup(">");
}
return(saveprompt);
}
void rl_restoreprompt() {
if (saveprompt != (char *) NULL) {
if (prompt != (char *) NULL) {
free(prompt);
}
prompt = (char *) estrdup(saveprompt);
} else {
prompt = (char *) estrdup(">");
}
}
void rl_setprompt(char *str)
{
if (prompt != (char *) NULL) {
free(prompt);
}
prompt = (char *) estrdup(str);
}
/* prototype for working with a pure file input */
/* the real rlgetc() is a co-routine with procXevent */
/* and gets characters from both mouse and keyboard */
int xrlgetc(FILE *fd)
{
int c;
c=getc(fd);
// printf("->%2.2x %c\n",c,c);
return (c);
}
int xrl_ungetc(int c, FILE *fd)
{
/* printf("ungetting %2.2x %c\n",c,c); */
return ungetc(c,fd);
}
/* expand and duplicate a string with malloc */
char * expdupstr(char *s, int n)
{
char *r;
r = (char *) malloc(strlen( (char *) s) + n);
strcpy(r, (char *) s);
return (r);
}
int rl_ungetc(LEXER *lp, int c)
{
int debug=0;
if (debug) printf("ungetting %2.2x %c at location %d\n",c,c,lp->pbufp);
(lp->pushback)[lp->pbufp] = c;
lp->pbufp++;
return(1);
}
/* stuff a string back onto stdin in reverse order */
int rl_ungets(LEXER *lp, char *s)
{
int i;
for (i=strlen(s)-1; i>=0; i--) {
rl_ungetc(lp,s[i]);
}
return(1);
}
static FILE *pigrcfp = NULL;
int rl_readin_file(FILE *fp) {
pigrcfp = fp;
return(0);
}
void rmctrls(char *s) {
while (*s != '\0') {
if (!isprint((unsigned char)*s)) {
*s = ' ';
}
s++;
}
}
/* Read a string, and return a pointer to it. Returns NULL on EOF. */
char * rl_gets (char *prompt)
{
char *s;
char buf[1024];
/* If the buffer has already been allocated, return the memory
to the free pool. */
if (lineread) {
free (lineread);
lineread = (char *) NULL;
}
if (pigrcfp == NULL) { /* Get a line from the user. */
lineread = readline (prompt);
if (lineread == NULL) {
return(NULL);
}
/* If the line has any text in it, save it on the history. */
if (lineread && *lineread) {
add_history (lineread);
write_history(HISTORY);
}
/* add a newline to return string */
s = expdupstr(lineread,3);
strcat(s,"\n");
free (lineread);
lineread = s;
} else {
if (fgets(buf, 1023, pigrcfp) == NULL) {
pigrcfp=NULL;
s = expdupstr(";\n",3);
} else {
s = expdupstr(buf,3);
strcat(s,";\n");
}
rmctrls(s);
lineread = s;
}
return (lineread);
}
void initialize_readline()
{
/* Allow conditional parsing of an inputrc file. */
/* commented out to force readline to use the default */
/* .inputrc name */
/* rl_readline_name = (char *) estrdup(progname()); */
/* Now tell readline where to get characters
*
* This is how the multitasking operates between the text and
* graphical portions of the system: readline calls the Xevent
* loop to get characters through a select() call, and the Xevent
* loop returns characters to be processed. Once it starts up,
* the two processes act as co-routines, transferring control
* back and forth.
*/
rl_getc_function = procXevent;
/* Allow conditional parsing of the ~/.inputrc file. */
stifle_history(MAXHIST);
read_history(HISTORY);
}
int rlgetc(LEXER *lp)
{
int c;
static char *linep = NULL;
if (lp->pbufp != 0) { // pushback ?
lp->pbufp--;
c = lp->pushback[lp->pbufp];
} else if (lp->token_stream != stdin) { // non-interactive readin
c=getc(lp->token_stream);
} else if(linep != NULL && *linep != '\0') { // eating current line?
c=*(linep++);
} else { // get a new line
if (prompt != NULL) {
lineread=rl_gets(prompt);
} else {
lineread=rl_gets(":");
}
if (lineread == NULL) {
linep = NULL;
if (lp->token_stream == stdin) {
if (signal(SIGINT, SIG_IGN) == SIG_IGN) {
c=EOF;
} else {
printf("trapped EOF on input: use QUIT to exit...\n");
c='\n';
}
} else {
c=EOF;
}
} else {
linep = lineread;
c=*(linep++);
}
}
/* printf("->%2.2x %c\n",c,c); */
return (c);
}