16
#include "trailer.h"
17
18
typedef enum { FIELD_STR, FIELD_ULONG, FIELD_TIME } cmp_type;
19
+typedef enum { COMPARE_EQUAL, COMPARE_UNEQUAL, COMPARE_NONE } cmp_status;
20
21
struct align {
22
align_type position;
24
};
25
26
struct if_then_else {
27
+ cmp_status cmp_status;
28
+ const char *str;
29
unsigned int then_atom_seen : 1,
30
else_atom_seen : 1,
31
condition_satisfied : 1;
53
enum { C_BARE, C_BODY, C_BODY_DEP, C_LINES, C_SIG, C_SUB, C_TRAILERS } option;
54
unsigned int nlines;
55
} contents;
56
+ struct {
57
+ cmp_status cmp_status;
58
+ const char *str;
59
+ } if_then_else;
60
enum { O_FULL, O_SHORT } objectname;
61
} u;
62
} *used_atom;
186
string_list_clear(¶ms, 0);
187
}
188
189
+static void if_atom_parser(struct used_atom *atom, const char *arg)
190
+{
191
+ if (!arg) {
192
+ atom->u.if_then_else.cmp_status = COMPARE_NONE;
193
+ return;
194
+ } else if (skip_prefix(arg, "equals=", &atom->u.if_then_else.str)) {
195
+ atom->u.if_then_else.cmp_status = COMPARE_EQUAL;
196
+ } else if (skip_prefix(arg, "notequals=", &atom->u.if_then_else.str)) {
197
+ atom->u.if_then_else.cmp_status = COMPARE_UNEQUAL;
198
+ } else {
199
+ die(_("unrecognized %%(if) argument: %s"), arg);
200
+ }
201
+}
202
+
203
+
204
static struct {
205
const char *name;
206
cmp_type cmp_type;
242
{ "color", FIELD_STR, color_atom_parser },
243
{ "align", FIELD_STR, align_atom_parser },
244
{ "end" },
223
- { "if" },
245
+ { "if", FIELD_STR, if_atom_parser },
246
{ "then" },
247
{ "else" },
248
};
444
struct ref_formatting_stack *new;
445
struct if_then_else *if_then_else = xcalloc(sizeof(struct if_then_else), 1);
446
447
+ if_then_else->str = atomv->atom->u.if_then_else.str;
448
+ if_then_else->cmp_status = atomv->atom->u.if_then_else.cmp_status;
449
+
450
push_stack_element(&state->stack);
451
new = state->stack;
452
new->at_end = if_then_else_handler;
478
die(_("format: %%(then) atom used after %%(else)"));
479
if_then_else->then_atom_seen = 1;
480
/*
456
- * If there exists non-empty string between the 'if' and
457
- * 'then' atom then the 'if' condition is satisfied.
481
+ * If the 'equals' or 'notequals' attribute is used then
482
+ * perform the required comparison. If not, only non-empty
483
+ * strings satisfy the 'if' condition.
484
*/
459
- if (cur->output.len && !is_empty(cur->output.buf))
485
+ if (if_then_else->cmp_status == COMPARE_EQUAL) {
486
+ if (!strcmp(if_then_else->str, cur->output.buf))
487
+ if_then_else->condition_satisfied = 1;
488
+ } else if (if_then_else->cmp_status == COMPARE_UNEQUAL) {
489
+ if (strcmp(if_then_else->str, cur->output.buf))
490
+ if_then_else->condition_satisfied = 1;
491
+ } else if (cur->output.len && !is_empty(cur->output.buf))
492
if_then_else->condition_satisfied = 1;
493
strbuf_reset(&cur->output);
494
}
1190
} else if (!strcmp(name, "end")) {
1191
v->handler = end_atom_handler;
1192
continue;
1161
- } else if (!strcmp(name, "if")) {
1193
+ } else if (starts_with(name, "if")) {
1194
+ const char *s;
1195
+
1196
+ if (skip_prefix(name, "if:", &s))
1197
+ v->s = xstrdup(s);
1198
v->handler = if_atom_handler;
1199
continue;
1200
} else if (!strcmp(name, "then")) {