Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download

open-axiom repository from github

24005 views
1
/*
2
Copyright (c) 1991-2002, The Numerical ALgorithms Group Ltd.
3
All rights reserved.
4
Copyright (C) 2007-2013 Gabriel Dos Reis.
5
All rights reserved.
6
7
Redistribution and use in source and binary forms, with or without
8
modification, are permitted provided that the following conditions are
9
met:
10
11
- Redistributions of source code must retain the above copyright
12
notice, this list of conditions and the following disclaimer.
13
14
- Redistributions in binary form must reproduce the above copyright
15
notice, this list of conditions and the following disclaimer in
16
the documentation and/or other materials provided with the
17
distribution.
18
19
- Neither the name of The Numerical ALgorithms Group Ltd. nor the
20
names of its contributors may be used to endorse or promote products
21
derived from this software without specific prior written permission.
22
23
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
24
IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
25
TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
26
PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
27
OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
28
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
29
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34
*/
35
36
#include "openaxiom-c-macros.h"
37
#include <unistd.h>
38
#include <stdlib.h>
39
#include <stdio.h>
40
#include <string.h>
41
#include <fcntl.h>
42
#include <sys/stat.h>
43
#include <sys/types.h>
44
#include <sys/wait.h>
45
46
#include "cfuns.h"
47
#include "edible.h"
48
#include "bsdsignal.h"
49
50
51
#include "fnct_key.h"
52
#include "prt.h"
53
#include "edin.h"
54
55
/* FIXME: Remove this in complete rewrite */
56
using namespace OpenAxiom;
57
58
/** Some constants for functio key defs ****/
59
#define DELAYED 0
60
#define IMMEDIATE 1
61
#define SPECIAL 2
62
63
64
/** Here is the structure for storing bound pf-keys ***/
65
fkey function_key[13]; /** Strings which replace function
66
keys when a key is hit ***/
67
68
static const char *defaulteditor = "clefedit";
69
char editorfilename[100];
70
71
72
73
/*
74
* The following function environment variable clef editor. The command
75
* should be the one that the user wishes to have execed
76
*/
77
78
void
79
set_editor_key(void)
80
{
81
int pid;
82
83
sprintf(editorfilename, "/tmp/clef%d", pid = oa_getpid());
84
85
if (function_key[12].str == NULL) {
86
(function_key[12]).type = SPECIAL;
87
(function_key[12]).str = defaulteditor;
88
}
89
}
90
91
92
93
94
/*** This routine id used to find the users function key mappings. It
95
simply searches the users HOME directory for a file called ".clef".
96
If found it gets the key bindings from within
97
*****/
98
99
void
100
define_function_keys(void)
101
{
102
char *HOME, path[1024], string[1024];
103
int key;
104
int fd;
105
char type;
106
107
/** lets initialize the key pointers **/
108
for (key = 0; key < 13; key++)
109
(function_key[key]).str = NULL;
110
/** see if the user has a .clef file ***/
111
HOME = oa_getenv("HOME");
112
sprintf(path, "%s/.clef", HOME);
113
if ((fd = open(path, O_RDONLY)) == -1) {
114
return;
115
}
116
else {
117
/*** If so, then get the key bindings **/
118
while ((key = get_key(fd, &type))) {
119
get_str(fd, string);
120
switch (type) {
121
case 'D':
122
if (key == 12) {
123
fprintf(stderr,
124
"Clef Error: PF12 can only be of type E in .clef\n");
125
fprintf(stderr, "Line will be ignored\n");
126
type = -1;
127
}
128
else {
129
(function_key[key]).type = DELAYED;
130
}
131
break;
132
case 'F':
133
if (key == 12) {
134
fprintf(stderr,
135
"Clef Error: PF12 can only be of type E in .clef\n");
136
fprintf(stderr, "Line will be ignored\n");
137
type = -1;
138
}
139
else {
140
(function_key[key]).type = IMMEDIATE;
141
}
142
break;
143
case 'E':
144
if (key != 12) {
145
fprintf(stderr,
146
"Clef Error: PF12 can only be of type E in .clef\n");
147
fprintf(stderr, "Line will be ignored\n");
148
type = -1;
149
}
150
else {
151
(function_key[key]).type = SPECIAL;
152
}
153
break;
154
}
155
if (type != -1)
156
(function_key[key]).str = strdup(string);
157
}
158
}
159
160
/*
161
* Now set the editor function key
162
*/
163
set_editor_key();
164
}
165
166
167
#define defof(c) ((c == 'F' || c == 'D' || c == 'E')?(1):(0))
168
169
int
170
get_key(int fd,char * ty)
171
{
172
173
/*
174
* Determines the key number being mapped, and whether it is immediate or
175
* delay. It reurns the key value, and modifies the parameter type
176
*/
177
char keynum[1024];
178
int nr;
179
180
nr = read(fd, keynum, 3);
181
if (nr != -1 && nr != 0) {
182
if (!defof(keynum[0])) {
183
return 0;
184
}
185
else {
186
*ty = keynum[0];
187
keynum[3] = '\0';
188
return (atoi(&keynum[1]));
189
}
190
}
191
else
192
return 0;
193
}
194
195
int
196
get_str(int fd,char * string)
197
{
198
/** Gets the key mapping being bound **/
199
char c;
200
int count = 0;
201
char *trace = string;
202
203
read(fd, &c, 1);
204
while (c == ' ')
205
read(fd, &c, 1);
206
while (c != '\n') {
207
count++;
208
*trace++ = c;
209
if (read(fd, &c, 1) == 0)
210
break;
211
}
212
*trace = '\0';
213
return count;
214
}
215
216
void
217
null_fnct(int sig)
218
{
219
return;
220
}
221
222
void
223
handle_function_key(int key,int chann)
224
{
225
/** this procedure simply adds the string specified by the function key
226
to the buffer ****/
227
int count, fd;
228
int amount = strlen(function_key[key].str);
229
int id;
230
231
/*** This procedure takes the character at in_buff[num_proc] and adds
232
it to the buffer. It first checks to see if we should be inserting
233
or overwriting, and then does the appropriate thing *******/
234
235
switch ((function_key[key]).type) {
236
case IMMEDIATE:
237
if (INS_MODE) {
238
forwardcopy(&buff[curr_pntr + amount],
239
&buff[curr_pntr],
240
buff_pntr - curr_pntr);
241
forwardflag_cpy(&buff_flag[curr_pntr + amount],
242
&buff_flag[curr_pntr],
243
buff_pntr - curr_pntr);
244
for (count = 0; count < amount; count++) {
245
buff[curr_pntr + count] = (function_key[key].str)[count];
246
buff_flag[curr_pntr + count] = '1';
247
}
248
ins_print(curr_pntr, amount + 1);
249
buff_pntr = buff_pntr + amount;
250
}
251
else {
252
for (count = 0; count < amount; count++) {
253
buff[curr_pntr + count] = (function_key[key].str)[count];
254
buff_flag[curr_pntr + count] = '1';
255
myputchar((function_key[key].str)[count]);
256
}
257
}
258
num_proc = num_proc + 6;
259
curr_pntr = curr_pntr + amount;
260
buff_pntr = buff_pntr + amount;
261
send_function_to_child();
262
break;
263
case DELAYED:
264
if (INS_MODE) {
265
forwardcopy(&buff[curr_pntr + amount],
266
&buff[curr_pntr],
267
buff_pntr - curr_pntr);
268
forwardflag_cpy(&buff_flag[curr_pntr + amount],
269
&buff_flag[curr_pntr],
270
buff_pntr - curr_pntr);
271
for (count = 0; count < amount; count++) {
272
buff[curr_pntr + count] = (function_key[key].str)[count];
273
buff_flag[curr_pntr + count] = '1';
274
}
275
ins_print(curr_pntr, amount + 1);
276
buff_pntr = buff_pntr + amount;
277
}
278
else {
279
for (count = 0; count < amount; count++) {
280
buff[curr_pntr + count] = (function_key[key].str)[count];
281
buff_flag[curr_pntr + count] = '1';
282
myputchar((function_key[key].str)[count]);
283
}
284
}
285
num_proc = num_proc + 6;
286
curr_pntr = curr_pntr + amount;
287
buff_pntr = buff_pntr + amount;
288
fflush(stdout);
289
break;
290
case SPECIAL:
291
/* fprintf(stderr, "Here I am \n"); */
292
if (access(editorfilename, F_OK) < 0) {
293
fd = open(editorfilename, O_RDWR | O_CREAT, 0666);
294
write(fd, buff, buff_pntr);
295
back_up(buff_pntr);
296
close(fd);
297
}
298
else {
299
if (buff_pntr > 0) {
300
fd = open(editorfilename, O_RDWR | O_TRUNC);
301
write(fd, buff, buff_pntr);
302
back_up(buff_pntr);
303
close(fd);
304
}
305
}
306
bsdSignal(OPENAXIOM_SIGCHLD, null_fnct,RestartSystemCalls);
307
switch (id = fork()) {
308
case -1:
309
perror("Special key");
310
break;
311
case 0:
312
execlp((function_key[12]).str,
313
(function_key[12]).str,
314
editorfilename, (char*) NULL);
315
perror("Returned from exec");
316
exit(0);
317
318
}
319
while (wait((int *) 0) < 0);
320
/** now I should read that file and send all it stuff thru the
321
reader *****/
322
fd = open(editorfilename, O_RDWR);
323
if (fd == -1) {
324
perror("Opening temp file");
325
exit(-1);
326
}
327
num_proc += 6;
328
329
/** reinitialize the buffer ***/
330
init_flag(buff_flag, buff_pntr);
331
init_buff(buff, buff_pntr);
332
/** reinitialize my buffer pointers **/
333
buff_pntr = curr_pntr = 0;
334
/** reset the ring pointer **/
335
current = NULL;
336
ECHOIT = 0;
337
while ((num_read = read(fd, in_buff, MAXLINE))) {
338
do_reading();
339
}
340
close(fd);
341
break;
342
}
343
return;
344
345
}
346
347