1 /*@ S-nail - a mail user agent derived from Berkeley Mail.
2 *@ Commands: conditional constructs.
4 * Copyright (c) 2014 - 2015 Steffen (Daode) Nurpmeso <sdaoden@users.sf.net>.
6 * Permission to use, copy, modify, and/or distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 #define n_FILE cmd_cnd
21 #ifndef HAVE_AMALGAMATION
26 struct cond_stack
*c_outer
;
27 bool_t c_error
; /* Bad expression, skip entire if..endif */
28 bool_t c_noop
; /* Outer stack !c_go, entirely no-op */
29 bool_t c_go
; /* Green light */
30 bool_t c_else
; /* In `else' clause */
35 char const * const *ic_argv_base
;
36 char const * const *ic_argv_max
;
37 char const * const *ic_argv
;
40 static struct cond_stack
*_cond_stack
;
43 static void _if_error(struct if_cmd
const *icp
, char const *msg_or_null
,
44 char const *nearby_or_null
);
46 /* noop and (1) don't work for real, only syntax-check and
47 * (2) non-error return is ignored */
48 static si8_t
_if_test(struct if_cmd
*icp
, bool_t noop
);
49 static si8_t
_if_group(struct if_cmd
*icp
, size_t level
, bool_t noop
);
52 _if_error(struct if_cmd
const *icp
, char const *msg_or_null
,
53 char const *nearby_or_null
)
58 if (msg_or_null
== NULL
)
59 msg_or_null
= _("invalid expression syntax");
61 if (nearby_or_null
!= NULL
)
62 n_err(_("`if' conditional: %s -- near \"%s\"\n"),
63 msg_or_null
, nearby_or_null
);
65 n_err(_("`if' conditional: %s\n"), msg_or_null
);
67 if (options
& (OPT_INTERACTIVE
| OPT_D_V
)) {
68 str_concat_cpa(&s
, icp
->ic_argv_base
,
69 (*icp
->ic_argv_base
!= NULL
? " " : ""));
70 n_err(_(" Expression: %s\n"), s
.s
);
72 str_concat_cpa(&s
, icp
->ic_argv
, (*icp
->ic_argv
!= NULL
? " " : ""));
73 n_err(_(" Stopped at: %s\n"), s
.s
);
80 _if_test(struct if_cmd
*icp
, bool_t noop
)
82 char const *emsg
= NULL
, * const *argv
, *cp
, *lhv
, *op
, *rhv
;
89 argc
= PTR2SIZE(icp
->ic_argv_max
- icp
->ic_argv
);
95 } else if (cp
[1] == '\0')
99 _if_error(icp
, emsg
, cp
);
105 switch (boolify(cp
, UIZ_MAX
, -1)) {
106 case 0: rv
= FAL0
; break;
107 case 1: rv
= TRU1
; break;
109 emsg
= N_("Expected a boolean");
114 rv
= !(options
& OPT_SENDMODE
);
117 rv
= ((options
& OPT_SENDMODE
) != 0);
120 if (!asccasecmp(cp
, "true")) /* Beware! */
123 rv
= ((options
& OPT_TTYIN
) != 0);
126 /* Look up the value in question, we need it anyway */
128 lhv
= noop
? NULL
: vok_vlook(cp
);
130 /* Single argument, "implicit boolean" form? */
137 /* Three argument comparison form required, check syntax */
138 emsg
= N_("unrecognized condition");
139 if (argc
== 2 || (c
= op
[0]) == '\0')
142 if (c
!= '<' && c
!= '>')
144 } else if (op
[2] != '\0')
146 else if (c
== '<' || c
== '>') {
149 } else if (c
== '=' || c
== '!') {
150 if (op
[1] != '=' && op
[1] != '@'
159 /* The right hand side may also be a variable, more syntax checking */
160 emsg
= N_("invalid right hand side");
161 if ((rhv
= argv
[2]) == NULL
/* Can't happen */)
166 rhv
= noop
? NULL
: vok_vlook(rhv
);
169 /* A null value is treated as the empty string */
180 if (regcomp(&re
, rhv
, REG_EXTENDED
| REG_ICASE
| REG_NOSUB
)) {
181 emsg
= N_("invalid regular expression");
185 rv
= (regexec(&re
, lhv
, 0,NULL
, 0) == REG_NOMATCH
) ^ (c
== '=');
191 else if (op
[1] == '@')
192 rv
= (asccasestr(lhv
, rhv
) == NULL
) ^ (c
== '=');
194 /* Try to interpret as integers, prefer those, then */
198 sli2
= strtol(rhv
, &eptr
, 0);
199 if (*rhv
!= '\0' && *eptr
== '\0') {
200 sli1
= strtol((cp
= lhv
), &eptr
, 0);
201 if (*cp
!= '\0' && *eptr
== '\0') {
205 case '=': rv
= (sli1
== 0); break;
206 case '!': rv
= (sli1
!= 0); break;
207 case '<': rv
= (op
[1] == '\0') ? sli1
< 0 : sli1
<= 0; break;
208 case '>': rv
= (op
[1] == '\0') ? sli1
> 0 : sli1
>= 0; break;
214 /* It is not an integer, perform string comparison */
215 sli1
= asccasecmp(lhv
, rhv
);
218 case '=': rv
= (sli1
== 0); break;
219 case '!': rv
= (sli1
!= 0); break;
220 case '<': rv
= (op
[1] == '\0') ? sli1
< 0 : sli1
<= 0; break;
221 case '>': rv
= (op
[1] == '\0') ? sli1
> 0 : sli1
>= 0; break;
235 _if_group(struct if_cmd
*icp
, size_t level
, bool_t noop
)
237 char const *emsg
= NULL
, *arg0
, * const *argv
, * const *argv_max_save
;
239 char unary
= '\0', c
;
245 _CANNOT_UNARY
= _NEED_LIST
,
246 _CANNOT_OBRACK
= _NEED_LIST
,
247 _CANNOT_CBRACK
= _FIRST
,
248 _CANNOT_LIST
= _FIRST
,
249 _CANNOT_COND
= _NEED_LIST
255 arg0
= *(argv
= icp
->ic_argv
);
257 if (!(state
& _END_OK
)) {
258 emsg
= N_("missing expression (premature end)");
263 break; /* goto jleave; */
266 switch ((c
= *arg0
)) {
271 if (state
& _CANNOT_UNARY
) {
272 emsg
= N_("cannot use a unary operator here");
276 unary
= (unary
!= '\0') ? '\0' : c
;
277 state
&= ~(_FIRST
| _END_OK
);
278 icp
->ic_argv
= ++argv
;
287 if (state
& _CANNOT_OBRACK
) {
288 emsg
= N_("cannot open a group here");
292 icp
->ic_argv
= ++argv
;
293 if ((xrv
= _if_group(icp
, level
+ 1, noop
)) < 0) {
297 rv
= (unary
!= '\0') ? !xrv
: xrv
;
300 state
&= ~(_FIRST
| _END_OK
);
301 state
|= (level
== 0 ? _END_OK
: 0) | _NEED_LIST
;
304 if (state
& _CANNOT_CBRACK
) {
305 emsg
= N_("cannot use closing bracket here");
310 emsg
= N_("no open groups to be closed here");
314 icp
->ic_argv
= ++argv
;
317 goto jleave
;/* break;break; */
322 if (c
!= arg0
[1] || arg0
[2] != '\0')
325 if (state
& _CANNOT_LIST
) {
326 emsg
= N_("cannot use a AND-OR list here");
330 noop
= ((c
== '&') ^ (rv
== TRU1
));
331 state
&= ~(_FIRST
| _END_OK
| _NEED_LIST
);
332 icp
->ic_argv
= ++argv
;
337 if (state
& _CANNOT_COND
) {
338 emsg
= N_("cannot use a `if' condition here");
343 if ((arg0
= argv
[i
]) == NULL
)
346 if (c
== '!' && arg0
[1] == '\0')
348 if ((c
== '[' || c
== ']') && arg0
[1] == '\0')
350 if ((c
== '&' || c
== '|') && c
== arg0
[1] && arg0
[2] == '\0')
354 emsg
= N_("empty conditional group");
358 argv_max_save
= icp
->ic_argv_max
;
359 icp
->ic_argv_max
= argv
+ i
;
360 if ((xrv
= _if_test(icp
, noop
)) < 0) {
364 rv
= (unary
!= '\0') ? !xrv
: xrv
;
365 icp
->ic_argv_max
= argv_max_save
;
367 icp
->ic_argv
= (argv
+= i
);
370 state
|= _END_OK
| _NEED_LIST
;
380 emsg
= N_("invalid grouping");
381 _if_error(icp
, V_(emsg
), arg0
);
390 struct cond_stack
*csp
;
393 char const * const *argv
= v
;
396 csp
= smalloc(sizeof *csp
);
397 csp
->c_outer
= _cond_stack
;
399 csp
->c_noop
= condstack_isskip();
409 for (argc
= 0; argv
[argc
] != NULL
; ++argc
)
411 assert(argc
> 0); /* (Minimum argument count for command) */
412 ic
.ic_argv_base
= ic
.ic_argv
= argv
;
413 ic
.ic_argv_max
= argv
+ argc
;
414 xrv
= _if_group(&ic
, 0, FAL0
);
417 csp
->c_go
= (bool_t
)xrv
;
420 csp
->c_error
= csp
->c_noop
= TRU1
;
429 struct cond_stack
*csp
;
433 if ((csp
= _cond_stack
) == NULL
|| csp
->c_else
) {
434 n_err(_("`elif' without matching `if'\n"));
436 } else if (!csp
->c_error
) {
437 csp
->c_go
= !csp
->c_go
;
439 _cond_stack
->c_outer
= csp
->c_outer
;
454 if (_cond_stack
== NULL
|| _cond_stack
->c_else
) {
455 n_err(_("`else' without matching `if'\n"));
458 _cond_stack
->c_else
= TRU1
;
459 _cond_stack
->c_go
= !_cond_stack
->c_go
;
469 struct cond_stack
*csp
;
474 if ((csp
= _cond_stack
) == NULL
) {
475 n_err(_("`endif' without matching `if'\n"));
478 _cond_stack
= csp
->c_outer
;
487 condstack_isskip(void)
492 rv
= (_cond_stack
!= NULL
&& (_cond_stack
->c_noop
|| !_cond_stack
->c_go
));
498 condstack_release(void)
510 condstack_take(void *self
)
512 struct cond_stack
*csp
;
516 if (!(rv
= ((csp
= _cond_stack
) == NULL
)))
518 _cond_stack
= csp
->c_outer
;
520 } while ((csp
= _cond_stack
) != NULL
);