Debian Codegraph @

expand.c

raw symbols SCIP
1 /*** expand.c *****************************************************************
2 **
3 ** This file is part of BibTool.
4 ** It is distributed under the GNU General Public License.
5 ** See the file COPYING for details.
6 **
7 ** (c) 1996-2020 Gerd Neugebauer
8 **
9 ** Net: [email protected]
10 **
11 ** This program is free software; you can redistribute it and/or modify
12 ** it under the terms of the GNU General Public License as published by
13 ** the Free Software Foundation; either version 2, or (at your option)
14 ** any later version.
15 **
16 ** This program is distributed in the hope that it will be useful,
17 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ** GNU General Public License for more details.
20 **
21 ** You should have received a copy of the GNU General Public License
22 ** along with this program; if not, write to the Free Software
23 ** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 **
25 **-----------------------------------------------------------------------------
26 ** Description:
27 ** This module contains functions to expand macros as they are
28 ** appearing in right hand sides of equations. This can be used
29 ** to get rid of additional macro definitions.
30 **
31 ******************************************************************************/
32
33 #include <bibtool/general.h>
34 #include <bibtool/error.h>
35 #include <bibtool/expand.h>
36 #include <bibtool/symbols.h>
37 #include <bibtool/macros.h>
38 #include <bibtool/sbuffer.h>
39
40 /*****************************************************************************/
41 /* Internal Programs */
42 /*===========================================================================*/
43
44 #ifdef __STDC__
45 #define _ARG(A) A
46 #else
47 #define _ARG(A) ()
48 #endif
49 Symbol expand_rhs _ARG((Symbol s,Symbol pre,Symbol post,DB db, bool lowercase));/* expand.c*/
50 static bool expand _ARG((String s,StringBuffer *sb,int brace,int first,String q_open,String q_close,DB db));/* expand.c*/
51 static void expand__ _ARG((String s,StringBuffer *sb,String q_open,String q_close,DB db));/* expand.c*/
52
53 /*****************************************************************************/
54 /* External Programs */
55 /*===========================================================================*/
56
57 /*---------------------------------------------------------------------------*/
58
59 /*-----------------------------------------------------------------------------
60 ** Function: expand_rhs()
61 ** Purpose: Expand the right hand side of an item. Each macro
62 ** which is defined in this database is replaced by its
63 ** value. The result is kept in a static variable until
64 ** the next invocation of this function overwrites it.
65 ** Arguments:
66 ** s String to expand
67 ** pre This is the opening brace character to be used. For
68 ** \BibTeX{} the valid values are |{| or |"|. This value
69 ** has to match to |post|.
70 ** post This is the closing brace character to be used. For
71 ** \BibTeX{} the valid values are |}| or |"|. This value
72 ** has to match to |pre|.
73 ** db Database containing the macros.
74 ** lowercase
75 ** Returns: A pointer to the expanded string. This value is kept in a
76 ** static variable of this function and will be overwritten with
77 ** the next invocation.
78 **___________________________________________________ */
79 Symbol expand_rhs(sym,pre,post,db,lowercase) /* */
80 Symbol sym; /* */
81 Symbol pre; /* */
82 Symbol post; /* */
83 DB db; /* */
84 bool lowercase; /* */
85 { static StringBuffer *sb = NULL; /* */
86 String s; /* */
87 /* */
88 DebugPrint1("expand_rhs"); /* */
89 if ( sb == NULL && (sb = sbopen()) == NULL ) /* */
90 { OUT_OF_MEMORY("string expansion");} /* */
91 /* */
92 DebugPrint2("Expanding ",SymbolValue(sym)); /* */
93 /* */
94 sbrewind(sb); /* */
95 expand__(SymbolValue(sym), /* */
96 sb, /* */
97 SymbolValue(pre), /* */
98 SymbolValue(post), /* */
99 db); /* */
100 s = (String)sbflush(sb); /* */
101 return symbol(lowercase ? lower(s): s); /* */
102 } /*------------------------*/
103
104 #define PUTS(S,SB) (void)sbputs((char*)S,SB)
105 #define PUTC(C,SB) (void)sbputchar(C,SB)
106
107 /*-----------------------------------------------------------------------------
108 ** Function*: expand__()
109 ** Purpose: Wrapper for expand().
110 ** Things have to be cleaned up afterwards.
111 ** Arguments:
112 ** s the string
113 ** sb the string buffer
114 ** q_open the open characters
115 ** q_close the close characters
116 ** db the database
117 ** Returns: nothing
118 **___________________________________________________ */
119 static void expand__(s, sb, q_open, q_close, db) /* */
120 register String s; /* */
121 StringBuffer *sb; /* */
122 String q_open; /* */
123 String q_close; /* */
124 DB db; /* */
125 { /* */
126 if (!expand(s,sb,true,true,q_open,q_close,db)) /* */
127 { PUTS(q_close, sb); } /* */
128 else if (sbtell(sb) == 0) /* */
129 { PUTS(q_open, sb); /* */
130 PUTS(q_close, sb); /* */
131 } /* */
132 } /*------------------------*/
133
134 /*-----------------------------------------------------------------------------
135 ** Function*: expand()
136 ** Purpose:
137 **
138 **
139 ** Arguments:
140 ** s String to apply expansion to.
141 ** sb String buffer which will receive the output.
142 ** brace Boolean. Determine if braces should be used.
143 ** The strings of the braces are in q_open and q_close.
144 ** first In multi parts determine if this is the first part.
145 ** q_open Open delimiter. This is an open brace or a double quote.
146 ** q_close Close delimiter. This is a close brace or a double quote.
147 ** Returns:
148 **___________________________________________________ */
149 static bool expand(s, sb, brace, first, q_open, q_close, db)/* */
150 register String s; /* specification */
151 StringBuffer *sb; /* output device */
152 bool brace; /* is a brace needed? */
153 bool first; /* is this the first part?*/
154 String q_open; /* open delimiter */
155 String q_close; /* close delimiter */
156 DB db; /* */
157 { /* */
158 while ( *s ) /* */
159 { /* */
160 switch ( *s ) /* */
161 { case '\0': break; /* Just in case. */
162 case '"': /* Start a string part. */
163 DebugPrint2("Start String:", s); /* */
164 if ( *++s == '"' ) { ++s; break; } /* Ignore the empty string*/
165 if ( brace ) /* */
166 { if ( !first ) { PUTS(" # ",sb); } /* */
167 PUTS(q_open,sb); /* */
168 brace = false; /* */
169 } /* */
170 first = false; /* */
171 /* */
172 for ( ; *s && *s != '"'; ++s ) /* Until the end is found */
173 { PUTC(*s,sb); /* transfer character. */
174 if ( *s == '\\' && *(s+1) ) /* \ is for quoting. */
175 { ++s; PUTC(*s,sb); } /* */
176 } /* */
177 if ( *s ) ++s; /* Advance if not end */
178 break; /* */
179 case '{': /* Start a block. */
180 DebugPrint2("Start block:",s); /* */
181 if ( *++s == '}' ) { ++s; break; } /* Ignore empty block. */
182 if ( brace ) /* */
183 { if ( !first ) { PUTS(" # ",sb); } /* */
184 PUTS(q_open,sb); /* */
185 brace = false; /* */
186 } /* */
187 first = false; /* */
188 /* */
189 { register int level = 1; /* Initialize brace count */
190 for ( ; *s && level > 0; ++s ) /* Until level 0 or end */
191 { switch ( *s ) /* */
192 { case '\\': /* \ is for quoting. */
193 PUTC(*s,sb); /* */
194 if (*++s) { PUTC(*s,sb); } /* */
195 break; /* */
196 case '{': /* */
197 ++level; /* */
198 PUTC(*s,sb); /* */
199 break; /* */
200 case '}': /* */
201 if ( --level > 0 ) PUTC(*s,sb); /* */
202 break; /* */
203 default: /* */
204 PUTC(*s,sb); /* */
205 } /* */
206 } /* */
207 } /* */
208 break; /* */
209 case '#': ++s; break; /* Silently assume #. */
210 case '0': case '1': case '2': case '3': case '4':/* Start a number */
211 case '5': case '6': case '7': case '8': case '9':/* */
212 DebugPrint2("Start number:",s); /* */
213 /* */
214 if ( brace ) /* */
215 { if ( !first ) { PUTS(" # ",sb); } /* */
216 PUTS(q_open,sb); /* */
217 brace = false; /* */
218 } /* */
219 first = false; /* */
220 /* */
221 do /* */
222 { PUTC(*s,sb); /* */
223 ++s; /* */
224 } while ( is_digit(*s) ); /* */
225 break; /* */
226 default: /* */
227 if ( is_space(*s) ) ++s; /* Ignore spaces. */
228 else if (!is_allowed(*s)) /* */
229 { PUTC(*s, sb); /* */
230 s++; /* */
231 } /* */
232 else /* Only macros are left. */
233 { String t = (String)s; /* */
234 Symbol val; /* */
235 Symbol sym = sym_extract(&t, false); /* */
236 /* */
237 DebugPrint2("Start symbol: ", s); /* */
238 val = db_string(db, sym, 1); /* */
239 if ( val ) /* */
240 { brace = expand(SymbolValue(val), /* */
241 sb, /* */
242 brace, /* */
243 first, /* */
244 q_open, /* */
245 q_close, /* */
246 db); } /* */
247 else /* */
248 { if ( !brace ) PUTS(q_close, sb); /* */
249 if ( !first ) PUTS(" # ", sb); /* */
250 PUTS(sym, sb); /* */
251 brace = true; /* */
252 first = false; /* */
253 } /* */
254 s = t; /* */
255 } /* */
256 } /* */
257 } /* */
258 return brace; /* */
259 } /*------------------------*/