1 /*@ S-nail - a mail user agent derived from Berkeley Mail.
2 *@ Commands: conditional constructs.
4 * Copyright (c) 2014 - 2016 Steffen (Daode) Nurpmeso <steffen@sdaoden.eu>.
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
; /* BUT: .ic_argv MUST be terminated! */
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
? " " : n_empty
));
70 n_err(_(" Expression: %s\n"), s
.s
);
72 str_concat_cpa(&s
, icp
->ic_argv
, (*icp
->ic_argv
!= NULL
? " " : n_empty
));
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 size_t i
= strlen(cp
);
130 if(i
> 0 && cp
[i
- 1] == '}')
131 cp
= savestrbuf(++cp
, i
-= 2);
137 else if((lhv
= vok_vlook(cp
)) == NULL
)
140 /* Single argument, "implicit boolean" form? */
147 /* Three argument comparison form required, check syntax */
148 emsg
= N_("unrecognized condition");
149 if (argc
== 2 || (c
= op
[0]) == '\0')
152 if (c
!= '<' && c
!= '>')
154 } else if (op
[2] != '\0')
156 else if (c
== '<' || c
== '>') {
159 } else if (c
== '=' || c
== '!') {
160 if (op
[1] != '=' && op
[1] != '@'
169 /* The right hand side may also be a variable, more syntax checking */
170 emsg
= N_("invalid right hand side");
171 if ((rhv
= argv
[2]) == NULL
/* Can't happen */)
176 else if(*rhv
== '{'){
177 size_t i
= strlen(rhv
);
179 if(i
> 0 && rhv
[i
- 1] == '}')
180 rhv
= savestrbuf(++rhv
, i
-= 2);
188 else if((rhv
= vok_vlook(cp
= rhv
)) == NULL
)
192 /* A null value is treated as the empty string */
195 lhv
= n_UNCONST(n_empty
);
197 rhv
= n_UNCONST(n_empty
);
203 if (regcomp(&re
, rhv
, REG_EXTENDED
| REG_ICASE
| REG_NOSUB
)) {
204 emsg
= N_("invalid regular expression");
208 rv
= (regexec(&re
, lhv
, 0,NULL
, 0) == REG_NOMATCH
) ^ (c
== '=');
214 else if (op
[1] == '@')
215 rv
= (asccasestr(lhv
, rhv
) == NULL
) ^ (c
== '=');
217 /* Try to interpret as integers, prefer those, then */
221 sli2
= strtol(rhv
, &eptr
, 0);
222 if (*rhv
!= '\0' && *eptr
== '\0') {
223 sli1
= strtol((cp
= lhv
), &eptr
, 0);
224 if (*cp
!= '\0' && *eptr
== '\0') {
228 case '=': rv
= (sli1
== 0); break;
229 case '!': rv
= (sli1
!= 0); break;
230 case '<': rv
= (op
[1] == '\0') ? sli1
< 0 : sli1
<= 0; break;
231 case '>': rv
= (op
[1] == '\0') ? sli1
> 0 : sli1
>= 0; break;
237 /* It is not an integer, perform string comparison */
238 sli1
= asccasecmp(lhv
, rhv
);
241 case '=': rv
= (sli1
== 0); break;
242 case '!': rv
= (sli1
!= 0); break;
243 case '<': rv
= (op
[1] == '\0') ? sli1
< 0 : sli1
<= 0; break;
244 case '>': rv
= (op
[1] == '\0') ? sli1
> 0 : sli1
>= 0; break;
258 _if_group(struct if_cmd
*icp
, size_t level
, bool_t noop
)
260 char const *emsg
= NULL
, *arg0
, * const *argv
, * const *argv_max_save
;
262 char unary
= '\0', c
;
268 _CANNOT_UNARY
= _NEED_LIST
,
269 _CANNOT_OBRACK
= _NEED_LIST
,
270 _CANNOT_CBRACK
= _FIRST
,
271 _CANNOT_LIST
= _FIRST
,
272 _CANNOT_COND
= _NEED_LIST
278 arg0
= *(argv
= icp
->ic_argv
);
280 if (!(state
& _END_OK
)) {
281 emsg
= N_("missing expression (premature end)");
286 break; /* goto jleave; */
289 switch ((c
= *arg0
)) {
294 if (state
& _CANNOT_UNARY
) {
295 emsg
= N_("cannot use a unary operator here");
299 unary
= (unary
!= '\0') ? '\0' : c
;
300 state
&= ~(_FIRST
| _END_OK
);
301 icp
->ic_argv
= ++argv
;
310 if (state
& _CANNOT_OBRACK
) {
311 emsg
= N_("cannot open a group here");
315 icp
->ic_argv
= ++argv
;
316 if ((xrv
= _if_group(icp
, level
+ 1, noop
)) < 0) {
320 rv
= (unary
!= '\0') ? !xrv
: xrv
;
323 state
&= ~(_FIRST
| _END_OK
);
324 state
|= (level
== 0 ? _END_OK
: 0) | _NEED_LIST
;
327 if (state
& _CANNOT_CBRACK
) {
328 emsg
= N_("cannot use closing bracket here");
333 emsg
= N_("no open groups to be closed here");
337 icp
->ic_argv
= ++argv
;
340 goto jleave
;/* break;break; */
345 if (c
!= arg0
[1] || arg0
[2] != '\0')
348 if (state
& _CANNOT_LIST
) {
349 emsg
= N_("cannot use a AND-OR list here");
353 noop
= ((c
== '&') ^ (rv
== TRU1
));
354 state
&= ~(_FIRST
| _END_OK
| _NEED_LIST
);
355 icp
->ic_argv
= ++argv
;
360 if (state
& _CANNOT_COND
) {
361 emsg
= N_("cannot use a `if' condition here");
366 if ((arg0
= argv
[i
]) == NULL
)
369 if (c
== '!' && arg0
[1] == '\0')
371 if ((c
== '[' || c
== ']') && arg0
[1] == '\0')
373 if ((c
== '&' || c
== '|') && c
== arg0
[1] && arg0
[2] == '\0')
377 emsg
= N_("empty conditional group");
381 argv_max_save
= icp
->ic_argv_max
;
382 icp
->ic_argv_max
= argv
+ i
;
383 if ((xrv
= _if_test(icp
, noop
)) < 0) {
387 rv
= (unary
!= '\0') ? !xrv
: xrv
;
388 icp
->ic_argv_max
= argv_max_save
;
390 icp
->ic_argv
= (argv
+= i
);
393 state
|= _END_OK
| _NEED_LIST
;
403 emsg
= N_("invalid grouping");
404 _if_error(icp
, V_(emsg
), arg0
);
413 char const * const *argv
;
414 struct cond_stack
*csp
;
419 csp
= smalloc(sizeof *csp
);
420 csp
->c_outer
= _cond_stack
;
422 csp
->c_noop
= condstack_isskip();
432 /* For heaven's sake, support comments _today_ TODO wyshlist.. */
433 for (argc
= 0, argv
= v
; argv
[argc
] != NULL
; ++argc
)
434 if(argv
[argc
][0] == '#'){
435 char const **nav
= salloc(sizeof(char*) * (argc
+ 1));
438 for(i
= 0; i
< argc
; ++i
)
444 ic
.ic_argv_base
= ic
.ic_argv
= argv
;
445 ic
.ic_argv_max
= &argv
[argc
];
446 xrv
= _if_group(&ic
, 0, FAL0
);
449 csp
->c_go
= (bool_t
)xrv
;
452 csp
->c_error
= csp
->c_noop
= TRU1
;
463 struct cond_stack
*csp
;
467 if ((csp
= _cond_stack
) == NULL
|| csp
->c_else
) {
468 n_err(_("`elif' without matching `if'\n"));
470 } else if (!csp
->c_error
) {
471 csp
->c_go
= !csp
->c_go
;
473 _cond_stack
->c_outer
= csp
->c_outer
;
488 if (_cond_stack
== NULL
|| _cond_stack
->c_else
) {
489 n_err(_("`else' without matching `if'\n"));
492 _cond_stack
->c_else
= TRU1
;
493 _cond_stack
->c_go
= !_cond_stack
->c_go
;
503 struct cond_stack
*csp
;
508 if ((csp
= _cond_stack
) == NULL
) {
509 n_err(_("`endif' without matching `if'\n"));
512 _cond_stack
= csp
->c_outer
;
521 condstack_isskip(void)
526 rv
= (_cond_stack
!= NULL
&& (_cond_stack
->c_noop
|| !_cond_stack
->c_go
));
532 condstack_release(void)
544 condstack_take(void *self
)
546 struct cond_stack
*csp
;
550 if (!(rv
= ((csp
= _cond_stack
) == NULL
)))
552 _cond_stack
= csp
->c_outer
;
554 } while ((csp
= _cond_stack
) != NULL
);