~ubuntu-branches/ubuntu/karmic/axiom/karmic

« back to all changes in this revision

Viewing changes to src/hyper/macro.pamphlet

  • Committer: Bazaar Package Importer
  • Author(s):
  • Date: 2005-02-21 17:08:37 UTC
  • mfrom: (1.2.1 upstream) (2.1.1 hoary)
  • Revision ID: james.westby@ubuntu.com-20050221170837-34vm4j33v4t9hsk4
Tags: 20050201-1
* New upstream release
* Bug fix: "axiom graphics missing?", thanks to Daniel Lakeland (Closes:
  #277692).
* Bug fix: "axiom: Feb 2005 release for sarge would be nice", thanks to
  Balbir Thomas (Closes: #295000).

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
\documentclass{article}
 
2
\usepackage{axiom}
 
3
\begin{document}
 
4
\title{\$SPAD/src/macro}
 
5
\author{The Axiom Team}
 
6
\maketitle
 
7
\begin{abstract}
 
8
\end{abstract}
 
9
\eject
 
10
\tableofcontents
 
11
\eject
 
12
\section{macro.c}
 
13
<<macro.c>>=
 
14
#define _MACRO_C
 
15
#include "useproto.h"
 
16
#include "debug.h"
 
17
 
 
18
#include "parse.h"
 
19
#include "parse_aux.h"
 
20
#include "mem.h"
 
21
 
 
22
#include "all_hyper_proto.H1"
 
23
 
 
24
 
 
25
/* #define DEBUG 1 */
 
26
extern FILE *cfile;
 
27
 
 
28
 
 
29
/*
 
30
 * This routine keeps scanning until it reaches it pops off 1 more
 
31
 * right brace then left brace
 
32
 */
 
33
void
 
34
#ifdef _NO_PROTO
 
35
scan_HyperDoc()
 
36
#else
 
37
scan_HyperDoc(void)
 
38
#endif
 
39
{
 
40
    HDWindow *twin = gWindow;
 
41
    int ret_val;
 
42
    int number_of_left_braces = 1;
 
43
 
 
44
    gWindow = NULL;
 
45
    while (number_of_left_braces) {
 
46
        ret_val = get_token();
 
47
        if (ret_val == EOF && number_of_left_braces) {
 
48
            fprintf(stderr, "Scan_Hypertex: Unexpected End of File\n");
 
49
            longjmp(jmpbuf, 1);
 
50
        }
 
51
        switch (token.type) {
 
52
          case Page:
 
53
            fprintf(stderr, "scan_HyperDoc: Unexpected Page Declaration\n");
 
54
            break;
 
55
          case NewCommand:
 
56
            fprintf(stderr, "scan_HyperDoc: Unexpected Macro Declaration\n");
 
57
            break;
 
58
          case Lbrace:
 
59
            number_of_left_braces++;
 
60
            break;
 
61
          case Endpatch:
 
62
          case Rbrace:
 
63
            number_of_left_braces--;
 
64
            break;
 
65
          default:
 
66
            break;
 
67
        }
 
68
    }
 
69
    gWindow = twin;
 
70
}
 
71
 
 
72
int
 
73
#ifdef _NO_PROTO
 
74
number(str)
 
75
    char *str;
 
76
#else
 
77
number(char *str)
 
78
#endif
 
79
{
 
80
    char *t = str;
 
81
 
 
82
    while (*t)
 
83
        if (!isdigit(*t++))
 
84
            return 0;
 
85
    return 1;
 
86
}
 
87
 
 
88
/* Parse a given macro given the pointer to the unlaoded macro ** */
 
89
 
 
90
static char *
 
91
#ifndef _NO_PROTO
 
92
load_macro(MacroStore *macro)
 
93
#else
 
94
load_macro(macro)
 
95
MacroStore *macro;
 
96
#endif
 
97
{
 
98
    int ret_val;
 
99
    long start_fpos;
 
100
    int size = 0;
 
101
    char *trace;
 
102
    char *macro_buff;
 
103
 
 
104
    save_scanner_state();
 
105
    cfile = find_fp(macro->fpos);
 
106
 
 
107
 
 
108
    init_scanner();
 
109
 
 
110
    /** First thing I should do is make sure that the name is correct ***/
 
111
    get_expected_token(NewCommand);
 
112
    get_expected_token(Lbrace);
 
113
    get_expected_token(Macro);
 
114
    if (strcmp(token.id, macro->name)) {
 
115
        /** WOW, Somehow I had the location of the wrong macro **/
 
116
        fprintf(stderr, "Expected macro name %s got insted %s in load_macro\n",
 
117
                macro->name, token.id);
 
118
        longjmp(jmpbuf, 1);
 
119
    }
 
120
    get_expected_token(Rbrace);
 
121
 
 
122
    /** Next I should check to see if I have any parameters **/
 
123
    get_token();
 
124
    if (token.type == Lsquarebrace) {
 
125
        /** The person is telling me the number of macros he is going to use **/
 
126
        get_expected_token(Word);
 
127
        if (!number(token.id)) {
 
128
            fprintf(stderr, "load_macro: Expected A Value Instead Got %s\n",
 
129
                    token.id);
 
130
            longjmp(jmpbuf, 1);
 
131
        }
 
132
        /** if it is a number, then I should store it in the parameter number
 
133
          member of the macro structure **/
 
134
        macro->number_parameters = atoi(token.id);
 
135
#ifdef DEBUG
 
136
        fprintf(stderr,
 
137
              "The number of parameters is %d\n", macro->number_parameters);
 
138
#endif
 
139
        get_expected_token(Rsquarebrace);
 
140
        get_token();
 
141
    }
 
142
    else
 
143
        macro->number_parameters = 0;
 
144
 
 
145
    /*** Now I should be able to check the token, and insure that I have read
 
146
      a leftbrace, then the string will follow                    ****/
 
147
    if (token.type != Lbrace) {
 
148
        /** The macro is not in a group, uh oh **/
 
149
        fprintf(stderr, "load_macro:Expected a Left Brace got type %d\n",
 
150
                token.type);
 
151
        longjmp(jmpbuf, 1);
 
152
    }
 
153
    start_fpos = fpos;
 
154
    scan_HyperDoc();
 
155
    ret_val = fseek(cfile, macro->fpos.pos + start_fpos, 0);
 
156
    size = fpos - start_fpos;
 
157
    macro_buff = (char *) halloc((size + 1) * sizeof(char), "Macro_buf");
 
158
    for (size = 0, trace = macro_buff; size < fpos - (start_fpos) - 1; size++)
 
159
        *trace++ = getc(cfile);
 
160
    *trace = '\0';
 
161
    macro->loaded = 1;
 
162
    restore_scanner_state();
 
163
    return macro_buff;
 
164
}
 
165
 
 
166
 
 
167
/** Here are the functions and declarations for the parameter stack **/
 
168
ParameterList parameters = NULL;
 
169
 
 
170
ParameterList
 
171
#ifdef _NO_PROTO
 
172
init_parameter_elem(number)
 
173
    int number;
 
174
#else
 
175
init_parameter_elem(int number)
 
176
#endif
 
177
{
 
178
    ParameterList new;
 
179
    int count;
 
180
 
 
181
    /** allocate the space neeeded **/
 
182
    new = (ParameterList) halloc(sizeof(struct parameter_list_type),
 
183
                                 "ParameterList");
 
184
    /** now allocate the memeory  for the pointers to the  parameters **/
 
185
    if (number) {
 
186
        new->list = (char **) halloc(number * sizeof(char *), "Parameter List");
 
187
 
 
188
        /** initialize my pointers **/
 
189
        for (count = 0; count < number; count++)
 
190
            (new->list)[count] = NULL;
 
191
    }
 
192
    new->number = number;
 
193
    return new;
 
194
}
 
195
 
 
196
int
 
197
#ifdef _NO_PROTO
 
198
push_parameters(new)
 
199
    ParameterList new;
 
200
#else
 
201
push_parameters(ParameterList new)
 
202
#endif
 
203
{
 
204
 
 
205
    if (new == NULL) {
 
206
        fprintf(stderr, "Tried pushing a null list onto the parameter stack\n");
 
207
        longjmp(jmpbuf, 1);
 
208
    }
 
209
 
 
210
    new->next = parameters;
 
211
    parameters = new;
 
212
    return 1;
 
213
}
 
214
int
 
215
#ifdef _NO_PROTO
 
216
pop_parameters()
 
217
#else
 
218
pop_parameters(void)
 
219
#endif
 
220
{
 
221
    /** Simply pops the top of the parameter list, being good and freeing
 
222
      all the memory **/
 
223
    ParameterList old;
 
224
    int count;
 
225
 
 
226
    if (!parameters) {
 
227
        return 0;
 
228
    }
 
229
 
 
230
    old = parameters;
 
231
    parameters = old->next;
 
232
 
 
233
    /** Free the parameter text and pointers **/
 
234
    if (old->number >0) {
 
235
        for (count = 0; count < old->number; count++)
 
236
                if ( (old->list)[count] )  free((char *) (old->list)[count]);
 
237
        free(old->list);
 
238
        }
 
239
 
 
240
    free(old);                  /** free the parameter **/
 
241
 
 
242
    return 1;
 
243
}
 
244
 
 
245
int
 
246
#ifdef _NO_PROTO
 
247
parse_macro()
 
248
#else
 
249
parse_macro(void)
 
250
#endif
 
251
{
 
252
 
 
253
    /*
 
254
     * This routine loads a macro if needed, and then parses it from the
 
255
     * string
 
256
     */
 
257
    MacroStore *macro;
 
258
    int s;
 
259
 
 
260
    curr_node->type = Macro;
 
261
    curr_node->space = token.id[-1];
 
262
    curr_node->next = alloc_node();
 
263
    curr_node = curr_node->next;
 
264
    macro = (MacroStore *) hash_find(gWindow->fMacroHashTable, token.id);
 
265
    if (macro != NULL) {
 
266
        if (!macro->loaded)
 
267
            macro->macro_string = load_macro(macro);
 
268
        get_parameter_strings(macro->number_parameters, macro->name);
 
269
        parse_from_string(macro->macro_string);
 
270
        if (gEndedPage) {
 
271
            s = curr_node->type;
 
272
            curr_node->type = Endmacro;
 
273
            curr_node->next = alloc_node();
 
274
            curr_node = curr_node->next;
 
275
            curr_node->type = s;
 
276
        }
 
277
        else
 
278
            curr_node->type = Endmacro;
 
279
        if (pop_parameters())
 
280
            return 1;
 
281
        else {
 
282
            fprintf(stderr,
 
283
                    "parse_macro: Tried to pop an empty paramter stack\n");
 
284
            longjmp(jmpbuf, 1);
 
285
        }
 
286
    }
 
287
    else {
 
288
        fprintf(stderr, "parse_macro: Unknown keyword %s\n", token.id);
 
289
        longjmp(jmpbuf, 1);
 
290
    }
 
291
}
 
292
 
 
293
#define numeric(c) ((c >= '0' && c <= '9')?1:0)
 
294
 
 
295
static void
 
296
#ifdef _NO_PROTO
 
297
get_parameter_strings(number, macro_name)
 
298
    int number;
 
299
    char *macro_name;
 
300
#else
 
301
get_parameter_strings(int number,char * macro_name)
 
302
#endif
 
303
{
 
304
    static char buffer[4096];
 
305
    char *buffer_pntr;
 
306
    int count;
 
307
    int lbrace_counter;
 
308
    char c;
 
309
    int size;
 
310
    ParameterList new = init_parameter_elem(number);
 
311
    int pnum;
 
312
    char pnum_chars[5];
 
313
    int pc;
 
314
 
 
315
    if (!number) {              /* nothing to be done */
 
316
        push_parameters(new);
 
317
        return;
 
318
    }
 
319
    for (count = 0; count < number; count++) {
 
320
        get_token();
 
321
        if (token.type != Lbrace) {
 
322
            /** The macro is not in a group, uh oh **/
 
323
            fprintf(stderr, "Wrong number of arguments to the macro %s\n",
 
324
                    macro_name);
 
325
            jump();
 
326
        }
 
327
        for (lbrace_counter = 1, buffer_pntr = buffer;
 
328
             lbrace_counter;) {
 
329
            switch (c = get_char()) {
 
330
              case EOF:
 
331
                fprintf(stderr, "GetParameterStrings: Unexpected EOF\n");
 
332
                longjmp(jmpbuf, 1);
 
333
              case '}':
 
334
                lbrace_counter--;
 
335
                if (lbrace_counter)
 
336
                    *buffer_pntr++ = c;
 
337
                break;
 
338
              case '{':
 
339
                lbrace_counter++;
 
340
                *buffer_pntr++ = c;
 
341
                break;
 
342
              case '#':
 
343
                /* uh oh, I have a paramter reference inside a paramter */
 
344
                /* get the number */
 
345
                if (parameters == NULL) {
 
346
                    *buffer_pntr++ = c;
 
347
                    break;
 
348
                }
 
349
                if (
 
350
                    ((buffer_pntr > buffer + 1) &&
 
351
                     *(buffer_pntr - 1) == '\\' &&
 
352
                     *(buffer_pntr - 2) != '\\') ||
 
353
                    ((buffer_pntr > buffer) &&
 
354
                     *(buffer_pntr - 1) == '\\')) {
 
355
                    /* I had a \# */
 
356
                    *buffer_pntr++ = c;
 
357
                }
 
358
                else {
 
359
                    c = get_char();
 
360
                    for (pc = 0; numeric(c); pc++) {
 
361
                        pnum_chars[pc] = c;
 
362
                        c = get_char();
 
363
                    }
 
364
                    unget_char(c);
 
365
                    pnum_chars[pc] = '\0';
 
366
                    pnum = atoi(pnum_chars);
 
367
                    pc = 0;
 
368
                    /* Now copy the paramter */
 
369
                    while ((parameters->list)[pnum - 1][pc] != '\0')
 
370
                        *buffer_pntr++ = (parameters->list)[pnum - 1][pc++];
 
371
                }
 
372
                break;
 
373
              default:
 
374
                *buffer_pntr++ = c;
 
375
                break;
 
376
            }
 
377
        }
 
378
        *buffer_pntr = '\0';
 
379
        /*** Now add it to the current parameter list **/
 
380
        size = strlen(buffer) + 1;
 
381
        new->list[count] = (char *) halloc(size, "Parameter Strings");
 
382
        strcpy(new->list[count], buffer);
 
383
    }
 
384
    push_parameters(new);
 
385
    return ;
 
386
}
 
387
void
 
388
#ifdef _NO_PROTO
 
389
parse_parameters()
 
390
#else
 
391
parse_parameters(void)
 
392
#endif
 
393
{
 
394
    int value;
 
395
 
 
396
    if (!number(token.id)) {
 
397
        fprintf(stderr,
 
398
                "Parse_parameter: Error Expected a number, got %s instead\n", token.id);
 
399
        longjmp(jmpbuf, 1);
 
400
    }
 
401
 
 
402
    if ((value = atoi(token.id)) > parameters->number) {
 
403
        /** had a bad parameter number **/
 
404
        fprintf(stderr,
 
405
                "Parse_parameter: Had a bad parameter number %d\n", value);
 
406
        longjmp(jmpbuf, 1);
 
407
    }
 
408
 
 
409
    parse_from_string((parameters->list)[value - 1]);
 
410
    curr_node->type = Endparameter;
 
411
    return;
 
412
}
 
413
@
 
414
\section{License}
 
415
<<license>>=
 
416
/*
 
417
Copyright (c) 1991-2002, The Numerical ALgorithms Group Ltd.
 
418
All rights reserved.
 
419
 
 
420
Redistribution and use in source and binary forms, with or without
 
421
modification, are permitted provided that the following conditions are
 
422
met:
 
423
 
 
424
    - Redistributions of source code must retain the above copyright
 
425
      notice, this list of conditions and the following disclaimer.
 
426
 
 
427
    - Redistributions in binary form must reproduce the above copyright
 
428
      notice, this list of conditions and the following disclaimer in
 
429
      the documentation and/or other materials provided with the
 
430
      distribution.
 
431
 
 
432
    - Neither the name of The Numerical ALgorithms Group Ltd. nor the
 
433
      names of its contributors may be used to endorse or promote products
 
434
      derived from this software without specific prior written permission.
 
435
 
 
436
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
 
437
IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 
438
TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
 
439
PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
 
440
OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 
441
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 
442
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 
443
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 
444
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 
445
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 
446
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 
447
*/
 
448
 
 
449
@
 
450
<<*>>=
 
451
<<license>>
 
452
<<macro.c>>
 
453
 
454
\eject
 
455
\begin{thebibliography}{99}
 
456
\bibitem{1} nothing
 
457
\end{thebibliography}
 
458
\end{document}
 
459
 
 
460
 
 
461
 
 
462