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 "openaxiom-c-macros.h"
37
/***
38
Contains all the code needed to parse input items,
39
InputString
40
SimpleBox
41
RadioBox.
42
****/
43
#include "debug.h"
44
#include "halloc.h"
45
#include "sockio.h"
46
#include "parse.h"
47
#include "lex.h"
48
#include "hyper.h"
49
50
static void insert_item(InputItem * item);
51
static void add_box_to_rb_list(char * name , InputBox * box);
52
static int check_others(InputBox * list);
53
54
/* create an unmapped input window for getting strings * */
55
extern int make_input_file;
56
57
HyperLink *
58
make_input_window(InputItem * item)
59
{
60
HyperLink *link;
61
XSetWindowAttributes at;
62
63
if (!make_input_file) {
64
link = (HyperLink *) halloc(sizeof(HyperLink), "HyperLink");
65
if (link == NULL) {
66
fprintf(stderr, "Ran out of memory allocating a hyper link!\n");
67
exit(-1);
68
}
69
at.cursor = gActiveCursor;
70
at.background_pixel = gInputBackgroundColor;
71
at.border_pixel = gActiveColor;
72
link->win = XCreateWindow(gXDisplay, gWindow->fDisplayedWindow, 0, 0, 100, 100, 0,
73
0, InputOutput, CopyFromParent,
74
CWCursor | CWBackPixel | CWBorderPixel, &at);
75
XSelectInput(gXDisplay, link->win, ButtonPressMask);
76
link->type = openaxiom_Inputstring_token;
77
link->x = link->y = 0;
78
/** This way when I click in an input window, I need only use reference
79
to get a pointer to the item ***/
80
link->reference.string = item;
81
hash_insert(gLinkHashTable,(char *) link,(char *) &link->win);
82
83
return link;
84
}
85
return 0;
86
}
87
88
/* create an unmapped input window for boxes */
89
HyperLink *
90
make_box_window(InputBox * box, int type)
91
{
92
HyperLink *link = 0;
93
XSetWindowAttributes at;
94
95
if (!make_input_file) {
96
link = (HyperLink *) halloc(sizeof(HyperLink), "Make_box_window");
97
if (link == NULL) {
98
fprintf(stderr, "Ran out of memory allocating a hyper link!\n");
99
exit(-1);
100
}
101
at.cursor = gActiveCursor;
102
at.background_pixel = gInputBackgroundColor;
103
link->win = XCreateWindow(gXDisplay, gWindow->fDisplayedWindow,
104
0, 0, 100, 100, 0,
105
0, InputOutput, CopyFromParent,
106
CWCursor | CWBackPixel, &at);
107
XSelectInput(gXDisplay, link->win, ButtonPressMask);
108
link->type = type;
109
link->x = link->y = 0;
110
/** This way when I click in an input window, I need only use reference
111
to get a pointer to the item ***/
112
link->reference.box = box;
113
hash_insert(gLinkHashTable, (char *)link,(char *) &link->win);
114
}
115
116
return link;
117
}
118
119
void
120
initialize_default(InputItem *item,char * buff)
121
{
122
LineStruct *newline;
123
LineStruct *curr_line;
124
int size = item->size;
125
int bp;
126
127
item->curr_line = item->lines = alloc_inputline(size);
128
curr_line = item->lines;
129
item->num_lines = 1;
130
curr_line->line_number = 1;
131
/* while I still have lines to fill */
132
for (bp = 0; *buff;) {
133
if (*buff == '\n') {
134
curr_line->len = bp;
135
curr_line->buffer[bp] = 0;
136
newline = alloc_inputline(size);
137
newline->line_number = ++(item->num_lines);
138
curr_line->next = newline;
139
newline->prev = curr_line;
140
curr_line = newline;
141
bp = 0;
142
buff++;
143
}
144
else if (bp == size) {
145
curr_line->len = size + 1;
146
curr_line->buffer[size] = '_';
147
curr_line->buffer[size + 1] = 0;
148
newline = alloc_inputline(size);
149
newline->line_number = ++(item->num_lines);
150
curr_line->next = newline;
151
newline->prev = curr_line;
152
bp = 0;
153
curr_line = newline;
154
}
155
else {
156
curr_line->buffer[bp++] = *buff++;
157
}
158
}
159
curr_line->buff_pntr = curr_line->len = bp;
160
item->curr_line = curr_line;
161
}
162
163
164
165
/* Parse the input string statement * */
166
void
167
parse_inputstring()
168
{
169
TextNode *input_node = curr_node;
170
char *name;
171
InputItem *item;
172
int size;
173
char *default_value;
174
175
gStringValueOk = 0;
176
177
/* first get the name */
178
input_node->type = token.type;
179
get_expected_token(openaxiom_Lbrace_token);
180
name = get_input_string();
181
input_node->data.text = alloc_string(name);
182
/* now get the width */
183
get_expected_token(openaxiom_Lbrace_token);
184
get_expected_token(openaxiom_Word_token);
185
get_expected_token(openaxiom_Rbrace_token);
186
size = atoi(token.id);
187
if (size < 0) {
188
fprintf(stderr, "Illegal size in Input string\n");
189
longjmp(jmpbuf, 1);
190
}
191
192
/* get the default value */
193
get_expected_token(openaxiom_Lbrace_token);
194
default_value = get_input_string();
195
196
/** now I need to malloc space for the input stuff **/
197
item = (InputItem *) halloc(sizeof(InputItem), "InputItem");
198
199
/* Now store all the string info */
200
item->name = (char *)
201
halloc((strlen(input_node->data.text) + 1) * (sizeof(char)),"parse_inputstring");
202
strcpy(item->name, input_node->data.text);
203
item->size = size;
204
item->entered = 0;
205
item->next = NULL;
206
initialize_default(item, default_value);
207
208
/** Now that I have all the structures made, lets make the window, and
209
add the item to the list ****/
210
211
input_node->link = make_input_window(item);
212
if (!make_input_file)
213
item->win = input_node->link->win; /* TTT */
214
insert_item(item);
215
gStringValueOk = 1;
216
curr_node = input_node;
217
return ;
218
}
219
220
void
221
parse_simplebox()
222
{
223
InputBox *box;
224
char *name;
225
short int picked = 0;
226
char *filename;
227
TextNode *input_box = curr_node;
228
229
gStringValueOk = 0;
230
231
/* set the type and space fields */
232
input_box->type = openaxiom_SimpleBox_token;
233
input_box->space = token.id[-1];
234
235
/* IS it selected? */
236
get_token();
237
if (token.type == openaxiom_Lsquarebrace_token) {
238
get_expected_token(openaxiom_Word_token);
239
if (!is_number(token.id)) {
240
fprintf(stderr,
241
"parse_simple_box: Expected a value not %s\n", token.id);
242
print_page_and_filename();
243
jump();
244
}
245
else if (!strcmp(token.id, "1"))
246
picked = 1;
247
else if (!strcmp(token.id, "0"))
248
picked = 0;
249
else {
250
fprintf(stderr, "parse_simple_box: Unexpected Value %s\n", token.id);
251
print_page_and_filename();
252
jump();
253
}
254
get_expected_token(openaxiom_Rsquarebrace_token);
255
get_token();
256
}
257
258
if (token.type != openaxiom_Lbrace_token) {
259
token_name(token.type);
260
fprintf(stderr, "parse_inputbox was expecting a { not a %s\n", ebuffer);
261
print_page_and_filename();
262
jump();
263
}
264
265
name = get_input_string();
266
if (gPageBeingParsed->box_hash && hash_find(gPageBeingParsed->box_hash, name)) {
267
fprintf(stderr, "Input box name %s is not unique \n", name);
268
print_page_and_filename();
269
jump();
270
}
271
272
box = alloc_inputbox();
273
box->name = alloc_string(name);
274
input_box->data.text = alloc_string(name);
275
box->picked = picked;
276
277
/* Get the filename for the selected and unselected bitmaps */
278
get_expected_token(openaxiom_Lbrace_token);
279
filename = get_input_string();
280
if (!make_input_file)
281
box->selected = insert_image_struct(filename);
282
get_expected_token(openaxiom_Lbrace_token);
283
filename = get_input_string();
284
if (!make_input_file) {
285
box->unselected = insert_image_struct(filename);
286
/* set the width and height for the maximaum of the two */
287
input_box->height = max(box->selected->height, box->unselected->height);
288
input_box->width = max(box->selected->width, box->unselected->width);
289
/* Make the window and stuff */
290
input_box->link = make_box_window(box, openaxiom_SimpleBox_token);
291
box->win = input_box->link->win;
292
293
/* Now add the box to the box_has table for this window */
294
if (gPageBeingParsed->box_hash == NULL) {
295
gPageBeingParsed->box_hash = (HashTable *) halloc(sizeof(HashTable),
296
"Box Hash");
297
hash_init(
298
gPageBeingParsed->box_hash,
299
BoxHashSize,
300
(EqualFunction) string_equal,
301
(HashcodeFunction) string_hash);
302
}
303
hash_insert(gPageBeingParsed->box_hash, (char *)box, box->name);
304
}
305
306
/* reset the curr_node and then return */
307
curr_node = input_box;
308
gStringValueOk = 1;
309
return;
310
}
311
void
312
parse_radiobox()
313
{
314
InputBox *box;
315
char *name;
316
char *group_name;
317
short int picked = 0;
318
TextNode *input_box = curr_node;
319
320
gStringValueOk = 0;
321
322
/* set the type and space fields */
323
input_box->type = openaxiom_Radiobox_token;
324
input_box->space = token.id[-1];
325
326
/* IS it selected? */
327
get_token();
328
if (token.type == openaxiom_Lsquarebrace_token) {
329
get_expected_token(openaxiom_Word_token);
330
if (!is_number(token.id)) {
331
fprintf(stderr,
332
"parse_simple_box: Expected a value not %s\n", token.id);
333
print_page_and_filename();
334
jump();
335
}
336
else if (!strcmp(token.id, "1"))
337
picked = 1;
338
else if (!strcmp(token.id, "0"))
339
picked = 0;
340
else {
341
fprintf(stderr, "parse_simple_box: Unexpected Value %s\n", token.id);
342
print_page_and_filename();
343
jump();
344
}
345
get_expected_token(openaxiom_Rsquarebrace_token);
346
get_token();
347
}
348
349
if (token.type != openaxiom_Lbrace_token) {
350
token_name(token.type);
351
fprintf(stderr, "parse_inputbox was expecting a { not a %s\n", ebuffer);
352
print_page_and_filename();
353
jump();
354
}
355
356
name = get_input_string();
357
if (gPageBeingParsed->box_hash && hash_find(gPageBeingParsed->box_hash, name)) {
358
fprintf(stderr, "Input box name %s is not unique \n", name);
359
print_page_and_filename();
360
jump();
361
}
362
363
box = alloc_inputbox();
364
box->name = alloc_string(name);
365
input_box->data.text = alloc_string(name);
366
box->picked = picked;
367
368
/* Now what I need to do is get the group name */
369
get_token();
370
if (token.type != openaxiom_Lbrace_token) {
371
token_name(token.type);
372
fprintf(stderr, "parse_inputbox was expecting a { not a %s\n", ebuffer);
373
print_page_and_filename();
374
jump();
375
}
376
group_name = get_input_string();
377
378
/*
379
* Now call a routine which searches the radio box list for the current
380
* group name, and if found adds this box to it
381
*/
382
add_box_to_rb_list(group_name, box);
383
384
input_box->width = box->rbs->width;
385
input_box->height = box->rbs->height;
386
/* Make the window and stuff */
387
input_box->link = make_box_window(box, openaxiom_Radiobox_token);
388
if (!make_input_file)
389
box->win = input_box->link->win; /* TTT */
390
391
392
/* Now add the box to the box_has table for this window */
393
if (gPageBeingParsed->box_hash == NULL) {
394
gPageBeingParsed->box_hash = (HashTable *) halloc(sizeof(HashTable),
395
"Box Hash");
396
hash_init(
397
gPageBeingParsed->box_hash,
398
BoxHashSize,
399
(EqualFunction) string_equal,
400
(HashcodeFunction) string_hash);
401
}
402
hash_insert(gPageBeingParsed->box_hash, (char *)box, box->name);
403
404
/* reset the curr_node and then return */
405
curr_node = input_box;
406
gStringValueOk = 1;
407
return;
408
}
409
static void
410
add_box_to_rb_list(char *name,InputBox *box)
411
{
412
RadioBoxes *trace = gPageBeingParsed->radio_boxes;
413
InputBox *list;
414
/*int found = 0;*/
415
416
while (trace != NULL && strcmp(trace->name, name))
417
trace = trace->next;
418
419
if (!trace) {
420
fprintf(stderr, "Tried to add a radio box to a non-existent group %s\n",
421
name);
422
print_page_and_filename();
423
jump();
424
}
425
426
/* now add the box to the list */
427
list = trace->boxes;
428
box->next = list;
429
trace->boxes = box;
430
431
if (box->picked && check_others(box->next)) {
432
fprintf(stderr, "Only a single radio button can be picked\n");
433
print_page_and_filename();
434
box->picked = 0;
435
}
436
box->selected = trace->selected;
437
box->unselected = trace->unselected;
438
box->rbs = trace;
439
440
return;
441
}
442
static int
443
check_others(InputBox *list)
444
{
445
InputBox *trace = list;
446
447
while (trace != NULL && !trace->picked)
448
trace = trace->next;
449
450
if (trace != NULL)
451
return 1;
452
else
453
return 0;
454
}
455
456
457
/* inserts an item into the current input list */
458
static void
459
insert_item(InputItem *item)
460
{
461
InputItem *trace = gPageBeingParsed->input_list;
462
463
if (gPageBeingParsed->current_item == NULL) {
464
gPageBeingParsed->current_item = item;
465
}
466
if (trace == NULL) {
467
/** Insert at the front of the list **/
468
gPageBeingParsed->input_list = item;
469
return;
470
}
471
else {
472
/** find the end of the list **/
473
while (trace->next != NULL)
474
trace = trace->next;
475
trace->next = item;
476
return;
477
}
478
}
479
480
InputItem *save_item;
481
482
void
483
init_paste_item(InputItem *item)
484
{
485
InputItem *trace = gPageBeingParsed->input_list;
486
487
if (!item) {
488
gPageBeingParsed->input_list = NULL;
489
gPageBeingParsed->current_item = NULL;
490
save_item = NULL;
491
}
492
else {
493
save_item = item->next;
494
trace->next = NULL;
495
}
496
}
497
void
498
repaste_item()
499
{
500
InputItem *trace;
501
502
if (save_item) {
503
for (trace = gPageBeingParsed->input_list; trace && trace->next != NULL;
504
trace = trace->next);
505
if (trace) {
506
trace->next = save_item;
507
}
508
else {
509
gWindow->page->input_list = save_item;
510
gWindow->page->current_item = save_item;
511
}
512
}
513
save_item = NULL;
514
}
515
516
InputItem *
517
current_item()
518
{
519
InputItem *trace = gPageBeingParsed->input_list;
520
521
if (trace) {
522
for (; trace->next != NULL; trace = trace->next);
523
return trace;
524
}
525
else
526
return NULL;
527
}
528
int
529
already_there(char *name)
530
{
531
RadioBoxes *trace = gPageBeingParsed->radio_boxes;
532
533
while (trace && strcmp(trace->name, name))
534
trace = trace->next;
535
536
if (trace)
537
return 1;
538
else
539
return 0;
540
}
541
void
542
parse_radioboxes()
543
{
544
TextNode *return_node = curr_node;
545
RadioBoxes *newrb;
546
char *fname;
547
548
/* I really don't need this node, it just sets up some parsing stuff */
549
return_node->type = openaxiom_Noop_token;
550
551
newrb = alloc_rbs();
552
553
get_token();
554
if (token.type != openaxiom_Lbrace_token) {
555
token_name(token.type);
556
fprintf(stderr, "\\radioboxes was expecting a name not %s\n", ebuffer);
557
print_page_and_filename();
558
jump();
559
}
560
561
newrb->name = alloc_string(get_input_string());
562
563
/* quick search for the name in the current list */
564
if (already_there(newrb->name)) {
565
free(newrb->name);
566
free(newrb);
567
fprintf(stderr, "Tried to redefine radioboxes %s\n", newrb->name);
568
print_page_and_filename();
569
jump();
570
}
571
/* now I have to get the selected and unslected bitmaps */
572
get_token();
573
if (token.type != openaxiom_Lbrace_token) {
574
token_name(token.type);
575
fprintf(stderr, "\\radioboxes was expecting a name not %s\n", ebuffer);
576
print_page_and_filename();
577
jump();
578
}
579
fname = get_input_string();
580
if (!make_input_file)
581
newrb->selected = insert_image_struct(fname);
582
583
get_token();
584
if (token.type != openaxiom_Lbrace_token) {
585
token_name(token.type);
586
fprintf(stderr, "\\radioboxes was expecting a name not %s\n", ebuffer);
587
print_page_and_filename();
588
jump();
589
}
590
fname = get_input_string();
591
if (!make_input_file) {
592
newrb->unselected = insert_image_struct(fname);
593
newrb->height = max(newrb->selected->height, newrb->unselected->height);
594
newrb->width = max(newrb->selected->width, newrb->unselected->width);
595
/* now add the thing to the current list of radio boxes */
596
}
597
newrb->next = gPageBeingParsed->radio_boxes;
598
gPageBeingParsed->radio_boxes = newrb;
599
600
curr_node = return_node;
601
return;
602
}
603
604