-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathjconsole.c
154 lines (139 loc) · 3 KB
/
jconsole.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
/* Copyright 1990-2011, Jsoftware Inc. All rights reserved. */
/* License in license.txt. */
/* J console */
/* #define READLINE for Unix readline support */
#ifdef _WIN32
#include <windows.h>
#include <io.h>
#else
#define _isatty isatty
#define _fileno fileno
#endif
#include "stdio.h"
#include <unistd.h>
#include <readline/readline.h>
#include <readline/history.h>
#include <signal.h>
#include "j.h"
#include "jeload.h"
static char **adadbreak;
static void sigint(int k){**adadbreak+=1;signal(SIGINT,sigint);}
static char input[30000];
/* J calls for keyboard input (debug suspension and 1!:1[1) */
/* we call to get next input */
#ifdef READLINE
/* readlin.h */
int read_history(const char *);
int write_history(const char *);
char* readline(const char *);
int hist=1;
char histfile[256];
void rlexit(int c){ if(!hist&&histfile[0]) write_history(histfile);}
char* Jinput_rl(char* prompt)
{
static char* line=0;
if(hist)
{
char* s;
hist=0;
histfile[0]=0;
s=getenv("HOME");
if(s)
{
strcpy(histfile,s);
strcat(histfile,"/.jhistory");
using_history();
read_history(histfile);
}
}
if(line) free(line); /* free last input */
line = readline(prompt);
if(!line) return "2!:55''"; /* ^d eof */
if(*line) add_history(line);
return line;
}
#endif
char* Jinput_stdio(char* prompt)
{
fputs(prompt,stdout);
fflush(stdout); /* windows emacs */
if(!fgets(input, sizeof(input), stdin))
{
#ifdef _WIN32
/* ctrl+c gets here for win */
if(!_isatty(_fileno(stdin))) return "2!:55''";
fputs("\n",stdout);
fflush(stdout);
**adadbreak+=1;
#else
/* unix eof without readline */
return "2!:55''";
#endif
}
return input;
}
char* _stdcall Jinput(J jt,char* prompt){
#ifdef READLINE
if(isatty(0)){
return Jinput_rl(prompt);
} else
#endif
return Jinput_stdio(prompt);
}
/* J calls for output */
void _stdcall Joutput(J jt,int type, char* s)
{
if(MTYOEXIT==type)
{
#ifdef READLINE
rlexit((int)(I)s);
#endif
exit((int)(I)s);
}
fputs(s,stdout);
fflush(stdout);
}
void addargv(int argc, char* argv[], C* d)
{
C *p,*q; I i;
p=d+strlen(d);
for(i=0;i<argc;++i)
{
if(sizeof(input)<(100+strlen(d)+2*strlen(argv[i]))) exit(100);
if(1==argc){*p++=',';*p++='<';}
if(i)*p++=';';
*p++='\'';
q=argv[i];
while(*q)
{
*p++=*q++;
if('\''==*(p-1))*p++='\'';
}
*p++='\'';
}
*p=0;
}
J jt;
int main(int argc, char* argv[])
{
void* callbacks[] = {Joutput,0,Jinput,0,(void*)SMCON}; int type;
jepath(argv[0]); // get path to JFE folder
jt=jeload(callbacks);
if(!jt){char m[1000]; jefail(m), fputs(m,stdout); exit(1);}
adadbreak=(char**)jt; // first address in jt is address of breakdata
signal(SIGINT,sigint);
#ifdef READLINE
char* rl_readline_name="jconsole"; /* argv[0] varies too much*/
#endif
if(argc==2&&!strcmp(argv[1],"-jprofile"))
type=3;
else if(argc>2&&!strcmp(argv[1],"-jprofile"))
type=1;
else
type=0;
addargv(argc,argv,input+strlen(input));
jefirst(type,input);
while(1){jedo(Jinput(jt," "));}
jefree();
return 0;
}