7 #define NARGS 10 /* number of arguments */
8 #define NMACROS 512 /* number of macros */
9 #define NSRCDEP 512 /* maximum esrc_depth */
11 /* eqn input stream */
13 struct esrc
*prev
; /* previous buffer */
14 char *buf
; /* input buffer; NULL for stdin */
15 int pos
; /* current position in buf */
16 int unbuf
[LNLEN
]; /* push-back buffer */
18 char *args
[NARGS
]; /* macro arguments */
19 int call
; /* is a macro call */
22 static struct esrc esrc_stdin
; /* the default input stream */
23 static struct esrc
*esrc
= &esrc_stdin
;
24 static int lineno
= 1; /* current line number */
25 static int esrc_depth
; /* the length of esrc chain */
27 static char *src_strdup(char *s
)
29 char *d
= malloc(strlen(s
) + 1);
34 /* push buf in the input stream; this is a macro call if args is not NULL */
35 static void src_push(char *buf
, char **args
)
39 if (esrc_depth
> NSRCDEP
)
40 errdie("neateqn: macro recursion limit reached\n");
41 next
= malloc(sizeof(*next
));
42 memset(next
, 0, sizeof(*next
));
44 next
->buf
= src_strdup(buf
);
45 next
->call
= args
!= NULL
;
47 for (i
= 0; i
< NARGS
; i
++)
48 next
->args
[i
] = args
[i
] ? src_strdup(args
[i
]) : NULL
;
53 /* back to the previous esrc buffer */
54 static void src_pop(void)
56 struct esrc
*prev
= esrc
->prev
;
59 for (i
= 0; i
< NARGS
; i
++)
68 static int src_stdin(void)
76 /* read the next character */
81 return esrc
->unbuf
[--esrc
->uncnt
];
84 if (esrc
->buf
[esrc
->pos
])
85 return (unsigned char) esrc
->buf
[esrc
->pos
++];
95 esrc
->unbuf
[esrc
->uncnt
++] = c
;
103 void src_lineset(int n
)
113 static struct macro macros
[NMACROS
];
116 static int src_findmacro(char *name
)
119 for (i
= 0; i
< nmacros
; i
++)
120 if (!strcmp(macros
[i
].name
, name
))
125 /* return nonzero if name is a macro */
126 int src_macro(char *name
)
128 return src_findmacro(name
) >= 0;
132 void src_define(char *name
, char *def
)
134 int idx
= src_findmacro(name
);
135 if (idx
< 0 && nmacros
< NMACROS
)
138 strcpy(macros
[idx
].name
, name
);
139 free(macros
[idx
].def
);
140 macros
[idx
].def
= src_strdup(def
);
147 for (i
= 0; i
< nmacros
; i
++)
152 int src_expand(char *name
, char **args
)
154 int i
= src_findmacro(name
);
156 src_push(macros
[i
].def
, args
);
160 /* expand argument */
163 int call
= esrc
->call
;
164 if (call
&& esrc
->args
[i
- 1])
165 src_push(esrc
->args
[i
- 1], NULL
);
169 /* return one if not reading macros and their arguments */