-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrlgetc.c
155 lines (125 loc) · 2.86 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
#include "rlgetc.h"
#include <stdlib.h>
#include <unistd.h>
#include <term.h>
#include <readline/readline.h>
#include <readline/history.h>
#define MAXHIST 1024 /* max history lines to save */
#define HISTORY ".posthist"
#define POSTRC ".postrc"
extern char *getwd();
extern char *xmalloc();
char * stripwhite();
char *lineread = (char *) NULL;
int pushback = '\0';
#include <signal.h>
#include <setjmp.h>
// jmp_buf begin;
char *rl_gets();
/* *************************************************** */
/* some routines to implement command line history ... */
/* *************************************************** */
int xrlgetc(fd)
FILE *fd;
{
int c;
c=getc(fd);
/* printf("->%2.2x %c\n",c,c); */
return (c);
}
int xrl_ungetc(c,fd)
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 = xmalloc(strlen( (char *) s) + n);
strcpy(r, (char *) s);
return (r);
}
int rl_ungetc(c,fd)
int c;
FILE *fd;
{
/* printf("ungetting %2.2x %c\n",c,c); */
if (fd != stdin || !isatty(1) || !isatty(0)) {
return(ungetc(c,fd));
} else {
pushback=c;
return(1);
}
}
/* Read a string, and return a pointer to it. Returns NULL on EOF. */
char * rl_gets (prompt)
char *prompt;
{
char *s;
int debug=0;
/* If the buffer has already been allocated, return the memory
to the free pool. */
if (lineread) {
free (lineread);
lineread = (char *) NULL;
}
/* Get a line from the user. */
if (debug) printf("entering readline\n");
lineread = readline(prompt);
if (lineread == NULL) {
return(NULL);
}
fflush(stdout);
/* 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,2);
strcat(s,"\n");
free (lineread);
lineread = s;
return (lineread);
}
void rl_init()
{
/* Allow conditional parsing of the ~/.inputrc file. */
rl_readline_name = POSTRC;
// stifle_history(MAXHIST); /* maximum number of history lines */
read_history(HISTORY);
}
/* note: "rl_getc" is an internal readline function... Do NOT rename */
/* this function from "rlgetc" to "rl_getc" or all hell will break loose */
int rlgetc(fd)
FILE *fd;
{
int c;
static char *lp = NULL;
if (fd != stdin || !isatty(1) || !isatty(0)) {
c=getc(fd);
} else {
if (pushback != '\0') {
c=pushback;
pushback= '\0';
} else if(lp != NULL && *lp != '\0') {
c=*(lp++);
} else {
if (isatty(1) && isatty(0)) {
lineread=rl_gets(":");
} else {
lineread=rl_gets("");
}
if (lineread == NULL) {
c=EOF;
} else {
lp = lineread;
c=*(lp++);
}
}
}
return (c);
}