src/postprocess.c

filter: Statementstmt 83.4% 96 / 115 Branchbran 76.6% 72 / 94 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 80.3% 168 / 209 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« src/parser.c ↑ Summary src/string_util.c »
1 /* ------------------------------------------------------------------------
2 @NAME : postprocess.c
3 @DESCRIPTION: Post-processing of entries once parsing is complete:
4 expanding macros, pasting substrings together, converting
5 numbers to strings and collapsing whitespace.
6 -------------------------------------------------------------------------- */
7  
8 #include <stdlib.h>
9 #include <string.h>
10 #include "bt_internal.h"
11  
12 /* ------------------------------------------------------------------------
13 @NAME : bt_postprocess_string()
14 @DESCRIPTION: Returns a new copy of s with carriage returns removed and,
15 if BTO_COLLAPSE is set, whitespace collapsed according to
16 BibTeX rules: leading and trailing spaces removed, and runs
17 of spaces reduced to one. (The lexer has already turned
18 newlines inside strings into spaces.)
19 -------------------------------------------------------------------------- */
20 char *
21 744 bt_postprocess_string (const char *s, btshort options)
22 {
23 744 boolean collapse = (options & BTO_COLLAPSE) != 0;
24 bt_sbuf out;
25 boolean prev_space;
26  
27 744 ▶ if (s == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (s == NULL) 18 726 744
28 18 return NULL;
29  
30 726 bt_sbuf_init (&out);
31 726 bt_sbuf_grow (&out, strlen (s));
32 726 prev_space = collapse; /* drops leading spaces */
33  
34 18668 ▶ for (; *s; s++)
Branch: 1 branch
BranchTrueFalseTotal
for (; *s; s++) 17942 726 18668
35 {
36 17942 ▶ if (*s == '\r') /* no Ctrl-Ms in the output */
Branch: 1 branch
BranchTrueFalseTotal
if (*s == '\r') /* no Ctrl-Ms in the output */ 0 17942 17942
37 0 continue;
38 17942 ▶ if (collapse && *s == ' ')
Branch: 2 branches
BranchTrueFalseTotal
if (collapse && *s == ' ') 17031 911 17942
if (collapse && *s == ' ') 2001 15030 17031
39 {
40 2001 ▶ if (prev_space)
Branch: 1 branch
BranchTrueFalseTotal
if (prev_space) 79 1922 2001
41 79 continue;
42 1922 prev_space = TRUE;
43 }
44 else
45 15941 prev_space = FALSE;
46 17863 bt_sbuf_putc (&out, *s);
47 }
48  
49 726 ▶ if (collapse && out.len > 0 && out.s[out.len - 1] == ' ')
Branch: 3 branches
BranchTrueFalseTotal
if (collapse && out.len > 0 && out.s[out.len - 1] == ' ') 630 96 726
if (collapse && out.len > 0 && out.s[out.len - 1] == ' ') 630 0 630
if (collapse && out.len > 0 && out.s[out.len - 1] == ' ') 6 624 630
50 6 out.s[--out.len] = 0;
51 726 return bt_sbuf_steal (&out);
52 }
53  
54  
55 /* ------------------------------------------------------------------------
56 @NAME : bt_postprocess_value()
57 @DESCRIPTION: Post-processes a series of simple values (strings, numbers,
58 macros) joined by '#'. If BTO_PASTE is set and there is
59 more than one simple value, they are pasted into a single
60 string (which requires macro expansion and number
61 conversion); otherwise each simple value is processed on
62 its own.
63  
64 If `replace' is true, the AST is updated in place (pasted
65 values are collapsed into the first node) and NULL is
66 returned. Otherwise the AST is untouched and the processed
67 string (of the last simple value, or of the pasted whole)
68 is returned; the caller must free() it.
69 -------------------------------------------------------------------------- */
70 char *
71 694 bt_postprocess_value (bt_macros *macros, bt_errors *errs,
72 AST *value, btshort options, boolean replace)
73 {
74 AST * sv;
75 boolean pasting;
76 btshort string_opts;
77 bt_sbuf paste;
78 694 char * result = NULL;
79  
80 694 ▶ if (value == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (value == NULL) 10 684 694
81 10 return NULL;
82 684 ▶ if (value->nodetype != BTAST_STRING &&
Branch: 1 branch
BranchTrueFalseTotal
if (value->nodetype != BTAST_STRING && 42 642 684
83 42 ▶ value->nodetype != BTAST_NUMBER &&
Branch: 1 branch
BranchTrueFalseTotal
value->nodetype != BTAST_NUMBER && 20 22 42
84 20 ▶ value->nodetype != BTAST_MACRO)
Branch: 1 branch
BranchTrueFalseTotal
value->nodetype != BTAST_MACRO) 0 20 20
85 {
86 0 bt_report (errs, BTERR_USAGEERR, NULL, -1, NULL, -1,
87 "bt_postprocess_value: invalid AST node (not a value)");
88 0 return NULL;
89 }
90  
91 684 ▶ pasting = (options & BTO_PASTE) && value->right;
Branch: 2 branches
BranchTrueFalseTotal
pasting = (options & BTO_PASTE) && value->right; 683 1 684
pasting = (options & BTO_PASTE) && value->right; 34 649 683
92  
93 /* Never collapse whitespace in the pieces of a pasted string -- only
94 * in the whole thing, once it has been put together. */
95 684 ▶ string_opts = pasting ? (options & ~BTO_COLLAPSE) : options;
Branch: 1 branch
BranchTrueFalseTotal
string_opts = pasting ? (options & ~BTO_COLLAPSE) : options; 34 650 684
96  
97 684 ▶ if (pasting && ! (options & (BTO_CONVERT | BTO_EXPAND)))
Branch: 2 branches
BranchTrueFalseTotal
if (pasting && ! (options & (BTO_CONVERT | BTO_EXPAND))) 34 650 684
if (pasting && ! (options & (BTO_CONVERT | BTO_EXPAND))) 0 34 34
98 {
99 0 bt_report (errs, BTERR_USAGEERR, NULL, -1, NULL, -1,
100 "bt_postprocess_value(): must convert numbers and expand "
101 "macros when pasting substrings");
102 0 return NULL;
103 }
104  
105 684 bt_sbuf_init (&paste);
106  
107 1419 ▶ for (sv = value; sv; sv = sv->right)
Branch: 1 branch
BranchTrueFalseTotal
for (sv = value; sv; sv = sv->right) 735 684 1419
108 {
109 735 char * tmp = NULL;
110 735 boolean free_tmp = FALSE;
111  
112 735 ▶ if (sv->nodetype == BTAST_MACRO && (options & BTO_EXPAND))
Branch: 2 branches
BranchTrueFalseTotal
if (sv->nodetype == BTAST_MACRO && (options & BTO_EXPAND)) 44 691 735
if (sv->nodetype == BTAST_MACRO && (options & BTO_EXPAND)) 43 1 44
113 43 {
114 43 const char * text = bt_macro_text (macros, errs, sv->text,
115 sv->filename, sv->line);
116 43 tmp = bt_postprocess_string (text, string_opts);
117 43 free_tmp = TRUE;
118 43 ▶ if (replace)
Branch: 1 branch
BranchTrueFalseTotal
if (replace) 43 0 43
119 {
120 43 sv->nodetype = BTAST_STRING;
121 43 free (sv->text);
122 43 sv->text = tmp; /* NULL if the macro is undefined */
123 43 free_tmp = FALSE;
124 }
125 }
126 692 ▶ else if (sv->nodetype == BTAST_STRING && sv->text)
Branch: 2 branches
BranchTrueFalseTotal
else if (sv->nodetype == BTAST_STRING && sv->text) 667 25 692
else if (sv->nodetype == BTAST_STRING && sv->text) 667 0 667
127 {
128 667 tmp = bt_postprocess_string (sv->text, string_opts);
129 667 ▶ if (replace)
Branch: 1 branch
BranchTrueFalseTotal
if (replace) 667 0 667
130 {
131 667 free (sv->text);
132 667 sv->text = tmp;
133 }
134 else
135 0 free_tmp = TRUE;
136 }
137 25 ▶ else if (sv->nodetype == BTAST_NUMBER)
Branch: 1 branch
BranchTrueFalseTotal
else if (sv->nodetype == BTAST_NUMBER) 24 1 25
138 {
139 24 ▶ if (replace && (options & BTO_CONVERT))
Branch: 2 branches
BranchTrueFalseTotal
if (replace && (options & BTO_CONVERT)) 24 0 24
if (replace && (options & BTO_CONVERT)) 24 0 24
140 24 sv->nodetype = BTAST_STRING;
141 24 tmp = sv->text;
142 }
143  
144 735 ▶ if (pasting)
Branch: 1 branch
BranchTrueFalseTotal
if (pasting) 84 651 735
145 84 bt_sbuf_puts (&paste, tmp);
146 651 ▶ else if (!replace)
Branch: 1 branch
BranchTrueFalseTotal
else if (!replace) 0 651 651
147 {
148 0 free (result);
149 0 ▶ result = bt_xstrdup (tmp ? tmp : "");
Branch: 1 branch
BranchTrueFalseTotal
result = bt_xstrdup (tmp ? tmp : ""); 0 0 0
150 }
151 735 ▶ if (free_tmp)
Branch: 1 branch
BranchTrueFalseTotal
if (free_tmp) 0 735 735
152 0 free (tmp);
153 }
154  
155 684 ▶ if (pasting)
Branch: 1 branch
BranchTrueFalseTotal
if (pasting) 34 650 684
156 {
157 34 ▶ char * whole = bt_postprocess_string (paste.s ? paste.s : "", options);
Branch: 1 branch
BranchTrueFalseTotal
char * whole = bt_postprocess_string (paste.s ? paste.s : "", options); 34 0 34
158  
159 34 bt_sbuf_free (&paste);
160 34 ▶ if (!replace)
Branch: 1 branch
BranchTrueFalseTotal
if (!replace) 0 34 34
161 0 return whole;
162  
163 34 bt_free_ast (value->right); /* free from 2nd simple value on */
164 34 value->right = NULL;
165 34 free (value->text);
166 34 value->text = whole;
167 34 value->nodetype = BTAST_STRING;
168 34 return NULL;
169 }
170  
171 650 return result;
172 }
173  
174  
175 /* ------------------------------------------------------------------------
176 @NAME : bt_postprocess_field()
177 @DESCRIPTION: Lowercases the field name and post-processes its value.
178 -------------------------------------------------------------------------- */
179 char *
180 689 bt_postprocess_field (bt_macros *macros, bt_errors *errs,
181 AST *field, btshort options, boolean replace)
182 {
183 689 ▶ if (field == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (field == NULL) 0 689 689
184 0 return NULL;
185 689 ▶ if (field->nodetype != BTAST_FIELD)
Branch: 1 branch
BranchTrueFalseTotal
if (field->nodetype != BTAST_FIELD) 0 689 689
186 {
187 0 bt_report (errs, BTERR_USAGEERR, NULL, -1, NULL, -1,
188 "bt_postprocess_field: invalid AST node (not a field)");
189 0 return NULL;
190 }
191  
192 689 ▶ if (replace)
Branch: 1 branch
BranchTrueFalseTotal
if (replace) 689 0 689
193 {
194 689 char * lc = bt_uni_map_string (field->text, BT_CASE_LOWER);
195 689 free (field->text);
196 689 field->text = lc;
197 }
198 689 return bt_postprocess_value (macros, errs, field->down, options, replace);
199 }
200  
201  
202 /* ------------------------------------------------------------------------
203 @NAME : bt_postprocess_entry()
204 @DESCRIPTION: Post-processes all the fields of an entry, and (unless
205 BTO_NOSTORE is set) stores the macros defined by an @string
206 entry.
207 -------------------------------------------------------------------------- */
208 void
209 167 bt_postprocess_entry (bt_macros *macros, bt_errors *errs,
210 AST *top, btshort options)
211 {
212 AST * cur;
213  
214 167 ▶ if (top == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (top == NULL) 0 167 167
215 0 return;
216 167 ▶ if (top->nodetype != BTAST_ENTRY)
Branch: 1 branch
BranchTrueFalseTotal
if (top->nodetype != BTAST_ENTRY) 0 167 167
217 {
218 0 bt_report (errs, BTERR_USAGEERR, NULL, -1, NULL, -1,
219 "bt_postprocess_entry: invalid node type (not entry root)");
220 0 return;
221 }
222  
223 167 ▶ if (top->text)
Branch: 1 branch
BranchTrueFalseTotal
if (top->text) 167 0 167
224 {
225 167 char * lc = bt_uni_map_string (top->text, BT_CASE_LOWER);
226 167 free (top->text);
227 167 top->text = lc;
228 }
229  
230 167 cur = top->down;
231 167 ▶ if (cur && cur->nodetype == BTAST_KEY)
Branch: 2 branches
BranchTrueFalseTotal
if (cur && cur->nodetype == BTAST_KEY) 165 2 167
if (cur && cur->nodetype == BTAST_KEY) 142 23 165
232 142 cur = cur->right;
233 167 ▶ if (cur == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (cur == NULL) 4 163 167
234 4 return;
235  
236 163 switch (top->metatype)
237 {
238 158 case BTE_REGULAR:
239 case BTE_MACRODEF:
240 847 ▶ for (; cur; cur = cur->right)
Branch: 1 branch
BranchTrueFalseTotal
for (; cur; cur = cur->right) 689 158 847
241 {
242 689 bt_postprocess_field (macros, errs, cur, options, TRUE);
243 689 ▶ if (top->metatype == BTE_MACRODEF && ! (options & BTO_NOSTORE))
Branch: 2 branches
BranchTrueFalseTotal
if (top->metatype == BTE_MACRODEF && ! (options & BTO_NOSTORE)) 21 668 689
if (top->metatype == BTE_MACRODEF && ! (options & BTO_NOSTORE)) 21 0 21
244 21 bt_add_macro_value (macros, errs, cur, options);
245 }
246 158 break;
247 5 case BTE_COMMENT:
248 case BTE_PREAMBLE:
249 5 bt_postprocess_value (macros, errs, cur, options, TRUE);
250 5 break;
251 0 default:
252 0 bt_report (errs, BTERR_INTERNAL, NULL, -1, NULL, -1,
253 "bt_postprocess_entry: unknown entry metatype (%d)",
254 0 (int) top->metatype);
255 }
256 }
« src/parser.c ↑ Summary src/string_util.c »