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-2010, 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 <sys/stat.h>
37
#include <sys/signal.h>
38
#include <setjmp.h>
39
40
#include "openaxiom-c-macros.h"
41
#include "debug.h"
42
#include "halloc.h"
43
#include "sockio.h"
44
#include "hyper.h"
45
#include "group.h"
46
#include "parse.h"
47
#include "bsdsignal.h"
48
#include "cfuns.h"
49
#include "sockio.h"
50
51
using namespace OpenAxiom;
52
53
extern char **input_file_list;
54
extern int input_file_count;
55
extern int make_patch_files;
56
extern int kill_spad;
57
extern jmp_buf jmpbuf;
58
59
static void make_input_file_list(void );
60
static char * make_input_file_name(char * buf , char * filename);
61
static char * make_paste_file_name(char * buf , char * filename);
62
static void make_the_input_file(UnloadedPage * page);
63
static void make_input_file_from_page(HyperDocPage * page);
64
static int inListAndNewer(char * inputFile , char * htFile);
65
static void print_paste(FILE*, char* , char*, const char*, int);
66
static void print_graph_paste(FILE*, char*, char* , const char*, int);
67
static void send_command(char * command , int com_type);
68
69
#define MaxInputFiles 256
70
char *active_file_list[MaxInputFiles];
71
int num_active_files = 0;
72
char *inactive_file_list[MaxInputFiles];
73
int num_inactive_files = 0;
74
int include_bf = 0;
75
char buf_for_record_commands[256];
76
77
78
79
void
80
make_record()
81
{
82
int i;
83
for (i=0;i<input_file_count;i++){
84
send_lisp_command("(|clearCmdCompletely|)");
85
send_lisp_command("(setq |$testingSystem| T)");
86
send_lisp_command("(setq |$printLoadMsgs| NIL)");
87
send_lisp_command("(setq |$BreakMode| '|resume|)");
88
sprintf(buf_for_record_commands,"(|inputFile2RecordFile| '\"%s\")",input_file_list[i]);
89
fprintf(stderr,"%s\n",buf_for_record_commands);
90
send_lisp_command(buf_for_record_commands);
91
}
92
if (kill_spad){
93
i = connect_spad();
94
if (i != NotConnected && i != SpadBusy)
95
send_int(spad_socket, KillLispSystem);
96
}
97
98
}
99
100
void
101
verify_record()
102
{
103
int i;
104
for (i=0;i<input_file_count;i++){
105
send_lisp_command("(|clearCmdCompletely|)");
106
send_lisp_command("(setq |$testingSystem| T)");
107
send_lisp_command("(setq |$printLoadMsgs| NIL)");
108
send_lisp_command("(setq |$BreakMode| '|resume|)");
109
sprintf(buf_for_record_commands,"(|verifyRecordFile| '\"%s\")",input_file_list[i]);
110
fprintf(stderr,"%s\n",buf_for_record_commands);
111
send_lisp_command(buf_for_record_commands);
112
}
113
if (kill_spad) {
114
i = connect_spad();
115
if (i != NotConnected && i != SpadBusy)
116
send_int(spad_socket, KillLispSystem);
117
}
118
}
119
120
121
void
122
ht2_input()
123
{
124
HashTable *table;
125
HashEntry *entry;
126
int i;
127
128
bsdSignal(SIGUSR2, SIG_IGN,RestartSystemCalls);
129
gWindow = alloc_hd_window();
130
init_group_stack();
131
table = gWindow->fPageHashTable;
132
make_input_file_list();
133
for (i = 0; i < table->size; i++)
134
for (entry = table->table[i]; entry != NULL; entry = entry->next)
135
make_the_input_file((UnloadedPage *) entry->data);
136
if (kill_spad){
137
i = connect_spad();
138
if (i != NotConnected && i != SpadBusy)
139
send_int(spad_socket, KillLispSystem);
140
}
141
}
142
143
static char *
144
make_input_file_name(char *buf, char *filename)
145
{
146
char *b, *c;
147
148
strcpy(buf, filename);
149
for (b = buf + strlen(buf) - 1; b != buf && *b != '/'; b--);
150
if (b != buf)
151
b = b + 1;
152
for (c = b; *c != '.' || c[1] != 'h' || c[2] != 't'; c++);
153
strcpy(c, ".input");
154
return b;
155
}
156
157
static char *
158
make_paste_file_name(char *buf, char *filename)
159
{
160
char *b, *c;
161
162
strcpy(buf, filename);
163
for (b = buf + strlen(buf) - 1; b != buf && *b != '/'; b--);
164
if (b != buf)
165
b = b + 1;
166
for (c = b; *c != '.' || c[1] != 'h' || c[2] != 't'; c++);
167
strcpy(c, ".pht");
168
return b;
169
}
170
171
static void
172
make_the_input_file(UnloadedPage *page)
173
{
174
char buf[1024], *b;
175
176
if (!page->fpos.name)
177
return;
178
b = make_input_file_name(buf, page->fpos.name);
179
if (inListAndNewer(b, page->fpos.name)) {
180
printf("parsing: %s\n", page->name);
181
if (setjmp(jmpbuf)) {
182
printf("Syntax error!\n");
183
}
184
else {
185
load_page((HyperDocPage *)page);
186
make_input_file_from_page(gWindow->page);
187
}
188
}
189
}
190
191
int example_number;
192
193
static void
194
make_input_file_from_page(HyperDocPage *page)
195
{
196
TextNode *node;
197
int starting_file = 1,/* i,*/ /*len,*/ ret_val;
198
char *buf, buf2[1024], buf3[1024];
199
char *b, *c, *com;
200
FILE *file = NULL;
201
FILE *pfile = NULL;
202
static HyperDocPage *op = NULL;
203
204
if (op == page)
205
return;
206
op = page;
207
if (page == NULL)
208
return;
209
b = make_input_file_name(buf2, page->filename);
210
c = make_paste_file_name(buf3, page->filename);
211
if (inListAndNewer(b, page->filename)) {
212
/* open and prepare the input file */
213
file = fopen(b, "a");
214
if (file == NULL) {
215
fprintf(stderr, "couldn't open output file %s\n", b);
216
exit(-1);
217
}
218
fprintf(file, "\n-- Input for page %s\n", page->name);
219
fprintf(file, ")clear all\n\n");
220
221
for (node = page->scrolling; node != NULL; node = node->next)
222
if (node->type == openaxiom_Spadcommand_token
223
|| node->type == openaxiom_Spadgraph_token
224
|| node->type == openaxiom_Spadsrc_token) {
225
if (starting_file) {
226
example_number = 1;
227
if (make_patch_files) {
228
send_lisp_command("(|clearCmdAll|)");
229
send_lisp_command("(|resetWorkspaceVariables|)");
230
send_lisp_command("(setq $linelength 55)");
231
send_lisp_command("(|setOutputCharacters| '(default))");
232
send_lisp_command("(setq |$printLoadMsgs| NIL)");
233
send_lisp_command("(setq |$UserLevel| '|development|)");
234
send_lisp_command("(verbos 0)");
235
}
236
if (make_patch_files) {
237
pfile = fopen(c, "a");
238
if (pfile == NULL) {
239
fprintf(stderr, "couldn't open output file %s\n", c);
240
exit(-1);
241
}
242
}
243
starting_file = 0;
244
}
245
else
246
example_number++;
247
buf = print_to_string(node->next);
248
com = alloc_string(buf);
249
fprintf(file, "%s\n", buf);
250
fflush(file);
251
fprintf(stderr, "writing:\t%s\n", buf);
252
include_bf = 1;
253
buf = print_to_string(node->next);
254
include_bf = 0;
255
if (make_patch_files) {
256
if (node->type == openaxiom_Spadcommand_token
257
|| node->type == openaxiom_Spadsrc_token)
258
print_paste(pfile, com, buf, page->name, node->type);
259
else
260
print_graph_paste(pfile, com, buf, page->name, node->type);
261
}
262
}
263
if (!starting_file && make_patch_files) {
264
ret_val = fclose(pfile);
265
if (ret_val == -1) {
266
fprintf(stderr, "couldn't close file %s\n", b);
267
exit(-1);
268
}
269
}
270
ret_val = fclose(file);
271
if (ret_val == -1) {
272
fprintf(stderr, "couldn't close file %s\n", b);
273
exit(-1);
274
}
275
}
276
}
277
278
char *
279
strCopy(char *s)
280
{
281
char *b = halloc(strlen(s) + 1,"String");
282
283
strcpy(b, s);
284
return b;
285
}
286
287
static int
288
inListAndNewer(char *inputFile, char *htFile)
289
{
290
int ret_val, found = 0, i;
291
struct stat htBuf, inputBuf;
292
293
for (i = 0; i < num_active_files; i++) {
294
if (strcmp(active_file_list[i], inputFile) == 0) {
295
found = 1;
296
break;
297
}
298
}
299
if (found)
300
return 1;
301
found = 0;
302
for (i = 0; i < num_inactive_files; i++)
303
if (strcmp(inactive_file_list[i], inputFile) == 0) {
304
found = 1;
305
break;
306
}
307
if (found)
308
return 0;
309
found = 0;
310
for (i = 0; i < input_file_count; i++)
311
if (strcmp(input_file_list[i], inputFile) == 0) {
312
found = 1;
313
break;
314
}
315
if (!found) {
316
inactive_file_list[num_inactive_files++] = strCopy(inputFile);
317
return 0;
318
}
319
ret_val = stat(inputFile, &inputBuf);
320
if (ret_val == -1) {
321
active_file_list[num_active_files++] = input_file_list[i];
322
printf("making %s\n", inputFile);
323
return 1;
324
}
325
ret_val = stat(htFile, &htBuf);
326
if (ret_val == -1) {
327
inactive_file_list[num_inactive_files++] = strCopy(inputFile);
328
return 0;
329
}
330
ret_val = htBuf.st_mtime > inputBuf.st_mtime;
331
ret_val = 1;
332
if (ret_val) {
333
active_file_list[num_active_files++] = input_file_list[i];
334
printf("making %s\n", inputFile);
335
unlink(inputFile);
336
}
337
else
338
inactive_file_list[num_inactive_files++] = input_file_list[i];
339
return ret_val;
340
}
341
342
static void
343
make_input_file_list()
344
{
345
int i;
346
char buf[256], *name;
347
348
for (i = 0; i < input_file_count; i++) {
349
name = make_input_file_name(buf, input_file_list[i]);
350
input_file_list[i] = (char *) halloc(strlen(name) + 1,"Input Filename");
351
strcpy(input_file_list[i], name);
352
}
353
}
354
355
void
356
print_paste_line(FILE *pfile,char *str)
357
{
358
char *free = "\\free", *bound = "\\bound", *f = free, *b = bound;
359
int justSaw = 0;
360
361
for (; *str; str++) {
362
if (*f == '\0')
363
justSaw = 2;
364
if (*b == '\0')
365
justSaw = 2;
366
if (*b == *str)
367
b++;
368
else
369
b = bound;
370
if (*f == *str)
371
f++;
372
else
373
f = free;
374
if (*str == '%' || *str == '{' || *str == '}' || *str == '#') {
375
if (*str == '{' && justSaw)
376
justSaw--;
377
else if (*str == '}' && justSaw)
378
justSaw--;
379
else
380
putc('\\', pfile);
381
}
382
putc(*str, pfile);
383
}
384
}
385
386
387
388
void
389
get_spad_output(FILE *pfile,char *command,int com_type)
390
{
391
int n, i;
392
char buf[1024];
393
394
send_command(command, com_type);
395
n = get_int(spad_socket);
396
for (i = 0; i < n; i++) {
397
get_string_buf(spad_socket, buf, 1024);
398
fprintf(pfile, "%s\n", buf);
399
}
400
unescape_string(command);
401
}
402
403
/*
404
* THEMOS says: There is a problem here in that we issue the (|close|) and
405
* then go on. If this is the last command ,we will soon send a SIGTERM and
406
* the whole thing will collapse maybe BEFORE the writing out has finished.
407
* Fix: Call a Lisp function that checks (with \axiomOp{key} ps and grep) the
408
* health of the viewport. We do this after the (|close|).
409
*/
410
void
411
get_graph_output(char* command, const char* pagename, int com_type)
412
{
413
int n, i;
414
char buf[1024];
415
416
send_command(command, com_type);
417
n = get_int(spad_socket);
418
for (i = 0; i < n; i++) {
419
get_string_buf(spad_socket, buf, 1024);
420
}
421
unescape_string(command);
422
sprintf(buf, "(|processInteractive| '(|write| |%s| \"%s%d\" \"image\") NIL)", "%",
423
pagename, example_number);
424
send_lisp_command(buf);
425
send_lisp_command("(|setViewportProcess|)");
426
send_lisp_command("(|processInteractive| '(|close| (|%%| -3)) NIL)");
427
send_lisp_command("(|waitForViewport|)");
428
get_int(spad_socket);
429
}
430
static void
431
send_command(char *command,int com_type)
432
{
433
char buf[1024];
434
435
if (com_type != openaxiom_Spadsrc_token) {
436
escape_string(command);
437
sprintf(buf, "(|parseAndEvalToHypertex| '\"%s\")", command);
438
send_lisp_command(buf);
439
}
440
else {
441
FILE *f;
442
char name[512], str[512]/*, *c*/;
443
444
sprintf(name, "/tmp/hyper%s.input", oa_getenv("SPADNUM"));
445
f = fopen(name, "w");
446
if (f == NULL) {
447
fprintf(stderr, "Can't open temporary input file %s\n", name);
448
return;
449
}
450
fprintf(f, "%s", command);
451
fclose(f);
452
sprintf(str, "(|parseAndEvalToHypertex| '\")read %s\")", name);
453
send_lisp_command(str);
454
}
455
}
456
457
static void
458
print_paste(FILE* pfile, char* realcom, char* command,
459
const char* pagename, int com_type)
460
{
461
fprintf(pfile, "\\begin{patch}{%sPatch%d}\n", pagename, example_number);
462
fprintf(pfile, "\\begin{paste}{%sFull%d}{%sEmpty%d}\n",
463
pagename, example_number, pagename, example_number);
464
fprintf(pfile, "\\pastebutton{%sFull%d}{\\hidepaste}\n",
465
pagename, example_number);
466
fprintf(pfile, "\\tab{5}\\spadcommand{");
467
print_paste_line(pfile, command);
468
fprintf(pfile, "}\n");
469
fprintf(pfile, "\\indentrel{3}\\begin{verbatim}\n");
470
get_spad_output(pfile, realcom, com_type);
471
fprintf(pfile, "\\end{verbatim}\n");
472
fprintf(pfile, "\\indentrel{-3}\\end{paste}\\end{patch}\n\n");
473
474
fprintf(pfile, "\\begin{patch}{%sEmpty%d}\n", pagename, example_number);
475
fprintf(pfile, "\\begin{paste}{%sEmpty%d}{%sPatch%d}\n",
476
pagename, example_number, pagename, example_number);
477
fprintf(pfile, "\\pastebutton{%sEmpty%d}{\\showpaste}\n",
478
pagename, example_number);
479
fprintf(pfile, "\\tab{5}\\spadcommand{");
480
print_paste_line(pfile, command);
481
fprintf(pfile, "}\n");
482
fprintf(pfile, "\\end{paste}\\end{patch}\n\n");
483
fflush(pfile);
484
}
485
static void
486
print_graph_paste(FILE* pfile, char* realcom, char* command,
487
const char* pagename, int com_type)
488
{
489
fprintf(pfile, "\\begin{patch}{%sPatch%d}\n", pagename, example_number);
490
fprintf(pfile, "\\begin{paste}{%sFull%d}{%sEmpty%d}\n",
491
pagename, example_number, pagename, example_number);
492
fprintf(pfile, "\\pastebutton{%sFull%d}{\\hidepaste}\n",
493
pagename, example_number);
494
fprintf(pfile, "\\tab{5}\\spadgraph{");
495
print_paste_line(pfile, command);
496
fprintf(pfile, "}\n");
497
fprintf(pfile, "\\center{\\unixcommand{\\inputimage{\\env{AXIOM}/share/viewports/%s%d.VIEW/image}}{viewAlone\\space{1} \\env{AXIOM}/share/viewports/%s%d}}\n", pagename, example_number, pagename, example_number);
498
get_graph_output(realcom, pagename, com_type);
499
fprintf(pfile, "\\end{paste}\\end{patch}\n\n");
500
501
fprintf(pfile, "\\begin{patch}{%sEmpty%d}\n", pagename, example_number);
502
fprintf(pfile, "\\begin{paste}{%sEmpty%d}{%sPatch%d}\n",
503
pagename, example_number, pagename, example_number);
504
fprintf(pfile, "\\pastebutton{%sEmpty%d}{\\showpaste}\n",
505
pagename, example_number);
506
fprintf(pfile, "\\tab{5}\\spadgraph{");
507
print_paste_line(pfile, command);
508
fprintf(pfile, "}\n");
509
fprintf(pfile, "\\end{paste}\\end{patch}\n\n");
510
fflush(pfile);
511
}
512
513