1 /*@ S-nail - a mail user agent derived from Berkeley Mail.
2 *@ Commands: conditional constructs.
4 * Copyright (c) 2014 - 2018 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
25 #define a_CCND_IF_ISSKIP() \
26 (n_go_data->gdc_ifcond != NULL &&\
27 (((struct a_ccnd_if_node*)n_go_data->gdc_ifcond)->cin_noop ||\
28 !((struct a_ccnd_if_node*)n_go_data->gdc_ifcond)->cin_go))
30 struct a_ccnd_if_node
{
31 struct a_ccnd_if_node
*cin_outer
;
32 bool_t cin_error
; /* Bad expression, skip entire if..endif */
33 bool_t cin_noop
; /* Outer stack !cin_go, entirely no-op */
34 bool_t cin_go
; /* Green light */
35 bool_t cin_else
; /* In `else' clause */
40 char const * const *cic_argv_base
;
41 char const * const *cic_argv_max
; /* BUT: .cic_argv MUST be terminated! */
42 char const * const *cic_argv
;
46 static void a_ccnd_oif_error(struct a_ccnd_if_ctx
const *cicp
,
47 char const *msg_or_null
, char const *nearby_or_null
);
49 /* noop and (1) don't work for real, only syntax-check and
50 * (2) non-error return is ignored */
51 static si8_t
a_ccnd_oif_test(struct a_ccnd_if_ctx
*cicp
, bool_t noop
);
52 static si8_t
a_ccnd_oif_group(struct a_ccnd_if_ctx
*cicp
, size_t level
,
55 /* Shared `if' / `elif' implementation */
56 static int a_ccnd_if(void *v
, bool_t iselif
);
59 a_ccnd_oif_error(struct a_ccnd_if_ctx
const *cicp
, char const *msg_or_null
,
60 char const *nearby_or_null
){
64 if(msg_or_null
== NULL
)
65 msg_or_null
= _("invalid expression syntax");
67 if(nearby_or_null
!= NULL
)
68 n_err(_("`if' conditional: %s -- near: %s\n"),
69 msg_or_null
, nearby_or_null
);
71 n_err(_("`if' conditional: %s\n"), msg_or_null
);
73 if((n_psonce
& n_PSO_INTERACTIVE
) || (n_poption
& n_PO_D_V
)){
74 str_concat_cpa(&s
, cicp
->cic_argv_base
,
75 (*cicp
->cic_argv_base
!= NULL
? " " : n_empty
));
76 n_err(_(" Expression: %s\n"), s
.s
);
78 str_concat_cpa(&s
, cicp
->cic_argv
,
79 (*cicp
->cic_argv
!= NULL
? " " : n_empty
));
80 n_err(_(" Stopped at: %s\n"), s
.s
);
86 a_ccnd_oif_test(struct a_ccnd_if_ctx
*cicp
, bool_t noop
){
87 char const *emsg
, * const *argv
, *cp
, *lhv
, *op
, *rhv
;
95 argv
= cicp
->cic_argv
;
96 argc
= PTR2SIZE(cicp
->cic_argv_max
- cicp
->cic_argv
);
102 }else if(cp
[1] == '\0')
113 a_ccnd_oif_error(cicp
, emsg
, cp
);
119 switch((rv
= n_boolify(cp
, UIZ_MAX
, TRUM1
))){
124 emsg
= N_("Expected a boolean");
129 rv
= !(n_psonce
& n_PSO_SENDMODE
);
132 rv
= ((n_psonce
& n_PSO_SENDMODE
) != 0);
135 if(!asccasecmp(cp
, "true")) /* Beware! */
138 rv
= ((n_psonce
& n_PSO_INTERACTIVE
) != 0);
146 /* Look up the value in question, we need it anyway */
148 size_t i
= strlen(cp
);
150 if(i
> 0 && cp
[i
- 1] == '}')
151 cp
= savestrbuf(++cp
, i
-= 2);
158 lhv
= n_var_vlook(cp
, TRU1
);
160 /* Single argument, "implicit boolean" form? */
167 /* Three argument comparison form required, check syntax */
168 emsg
= N_("unrecognized condition");
169 if(argc
== 2 || (c
= op
[0]) == '\0')
172 /* May be modifier */
186 if(c
!= '<' && c
!= '>')
188 }else if(c
!= '-' && op
[2] != '\0')
190 else if(c
== '<' || c
== '>'){
193 }else if(c
== '=' || c
== '!'){
194 if(op
[1] != '=' && op
[1] != '%' && op
[1] != '@'
201 if(op
[1] == '\0' || op
[2] == '\0' || op
[3] != '\0')
206 }else if(op
[1] == 'g' || op
[1] == 'l'){
207 if(op
[2] != 'e' && op
[2] != 't')
209 }else if(op
[1] == 'n'){
217 /* The right hand side may also be a variable, more syntax checking */
218 emsg
= N_("invalid right hand side");
219 if((rhv
= argv
[2]) == NULL
/* Can't happen */)
224 else if(*rhv
== '{'){
225 size_t i
= strlen(rhv
);
227 if(i
> 0 && rhv
[i
- 1] == '}')
228 rhv
= savestrbuf(++rhv
, i
-= 2);
237 rhv
= n_var_vlook(cp
= rhv
, TRU1
);
240 /* A null value is treated as the empty string */
243 lhv
= n_UNCONST(n_empty
);
245 rhv
= n_UNCONST(n_empty
);
252 if((s
= regcomp(&re
, rhv
, REG_EXTENDED
| REG_NOSUB
|
253 (flags
& a_ICASE
? REG_ICASE
: 0))) != 0){
254 emsg
= savecat(_("invalid regular expression: "),
255 n_regex_err_to_doc(NULL
, s
));
259 rv
= (regexec(&re
, lhv
, 0,NULL
, 0) == REG_NOMATCH
) ^ (c
== '=');
265 else if(op
[1] == '%' || op
[1] == '@'){
267 n_OBSOLETE("`if'++: \"=@\" and \"!@\" became \"=%\" and \"!%\"");
268 rv
= ((flags
& a_ICASE
? asccasestr(lhv
, rhv
) : strstr(lhv
, rhv
)
269 ) == NULL
) ^ (c
== '=');
277 if((n_idec_si64_cp(&lhvi
, lhv
, 0, NULL
278 ) & (n_IDEC_STATE_EMASK
| n_IDEC_STATE_CONSUMED
)
279 ) != n_IDEC_STATE_CONSUMED
|| (n_idec_si64_cp(&rhvi
, rhv
, 0, NULL
280 ) & (n_IDEC_STATE_EMASK
| n_IDEC_STATE_CONSUMED
)
281 ) != n_IDEC_STATE_CONSUMED
){
282 emsg
= N_("integer expression expected");
289 case 'e': rv
= (lhvi
== 0); break;
290 case 'n': rv
= (lhvi
!= 0); break;
291 case 'l': rv
= (op
[2] == 't') ? lhvi
< 0 : lhvi
<= 0; break;
292 case 'g': rv
= (op
[2] == 't') ? lhvi
> 0 : lhvi
>= 0; break;
298 scmp
= (flags
& a_ICASE
) ? asccasecmp(lhv
, rhv
) : strcmp(lhv
, rhv
);
301 case '=': rv
= (scmp
== 0); break;
302 case '!': rv
= (scmp
!= 0); break;
303 case '<': rv
= (op
[1] == '\0') ? scmp
< 0 : scmp
<= 0; break;
304 case '>': rv
= (op
[1] == '\0') ? scmp
> 0 : scmp
>= 0; break;
318 a_ccnd_oif_group(struct a_ccnd_if_ctx
*cicp
, size_t level
, bool_t noop
){
319 char const *emsg
, *arg0
, * const *argv
, * const *argv_max_save
;
327 a_CANNOT_UNARY
= a_NEED_LIST
,
328 a_CANNOT_OBRACK
= a_NEED_LIST
,
329 a_CANNOT_CBRACK
= a_FIRST
,
330 a_CANNOT_LIST
= a_FIRST
,
331 a_CANNOT_COND
= a_NEED_LIST
342 arg0
= *(argv
= cicp
->cic_argv
);
344 if(!(state
& a_END_OK
)){
345 emsg
= N_("missing expression (premature end)");
350 break; /* goto jleave; */
358 if(state
& a_CANNOT_UNARY
){
359 emsg
= N_("cannot use a unary operator here");
363 unary
= (unary
!= '\0') ? '\0' : c
;
364 state
&= ~(a_FIRST
| a_END_OK
);
365 cicp
->cic_argv
= ++argv
;
374 if(state
& a_CANNOT_OBRACK
){
375 emsg
= N_("cannot open a group here");
379 cicp
->cic_argv
= ++argv
;
380 if((xrv
= a_ccnd_oif_group(cicp
, level
+ 1, noop
)) < 0){
384 rv
= (unary
!= '\0') ? !xrv
: xrv
;
387 state
&= ~(a_FIRST
| a_END_OK
);
388 state
|= (level
== 0 ? a_END_OK
: 0) | a_NEED_LIST
;
391 if(state
& a_CANNOT_CBRACK
){
392 emsg
= N_("cannot use closing bracket here");
397 emsg
= N_("no open groups to be closed here");
401 cicp
->cic_argv
= ++argv
;
404 goto jleave
;/* break;break; */
409 if(c
!= arg0
[1] || arg0
[2] != '\0')
412 if(state
& a_CANNOT_LIST
){
413 emsg
= N_("cannot use a AND-OR list here");
417 noop
= ((c
== '&') ^ (rv
== TRU1
));
418 state
&= ~(a_FIRST
| a_END_OK
| a_NEED_LIST
);
419 cicp
->cic_argv
= ++argv
;
424 if(state
& a_CANNOT_COND
){
425 emsg
= N_("cannot use a `if' condition here");
430 if((arg0
= argv
[i
]) == NULL
)
433 if(c
== '!' && arg0
[1] == '\0')
435 if((c
== '[' || c
== ']') && arg0
[1] == '\0')
437 if((c
== '&' || c
== '|') && c
== arg0
[1] && arg0
[2] == '\0')
441 emsg
= N_("empty conditional group");
445 argv_max_save
= cicp
->cic_argv_max
;
446 cicp
->cic_argv_max
= argv
+ i
;
447 if((xrv
= a_ccnd_oif_test(cicp
, noop
)) < 0){
451 rv
= (unary
!= '\0') ? !xrv
: xrv
;
452 cicp
->cic_argv_max
= argv_max_save
;
454 cicp
->cic_argv
= (argv
+= i
);
457 state
|= a_END_OK
| a_NEED_LIST
;
467 emsg
= N_("invalid grouping");
468 a_ccnd_oif_error(cicp
, V_(emsg
), arg0
);
474 a_ccnd_if(void *v
, bool_t iselif
){
475 struct a_ccnd_if_ctx cic
;
476 char const * const *argv
;
479 struct a_ccnd_if_node
*cinp
;
483 cinp
= smalloc(sizeof *cinp
);
484 cinp
->cin_outer
= n_go_data
->gdc_ifcond
;
486 cinp
= n_go_data
->gdc_ifcond
;
487 assert(cinp
!= NULL
);
489 cinp
->cin_error
= FAL0
;
490 cinp
->cin_noop
= a_CCND_IF_ISSKIP();
492 cinp
->cin_else
= FAL0
;
494 n_go_data
->gdc_ifcond
= cinp
;
501 /* For heaven's sake, support comments _today_ TODO wyshlist.. */
502 for(argc
= 0, argv
= v
; argv
[argc
] != NULL
; ++argc
)
503 if(argv
[argc
][0] == '#'){
504 char const **nav
= salloc(sizeof(char*) * (argc
+ 1));
507 for(i
= 0; i
< argc
; ++i
)
513 cic
.cic_argv_base
= cic
.cic_argv
= argv
;
514 cic
.cic_argv_max
= &argv
[argc
];
515 xrv
= a_ccnd_oif_group(&cic
, 0, FAL0
);
518 cinp
->cin_go
= (bool_t
)xrv
;
521 cinp
->cin_error
= cinp
->cin_noop
= TRU1
;
534 rv
= a_ccnd_if(v
, FAL0
);
541 struct a_ccnd_if_node
*cinp
;
545 if((cinp
= n_go_data
->gdc_ifcond
) == NULL
|| cinp
->cin_else
){
546 n_err(_("`elif' without a matching `if'\n"));
548 }else if(!cinp
->cin_error
){
549 cinp
->cin_go
= !cinp
->cin_go
; /* Cause right _IF_ISSKIP() result */
550 rv
= a_ccnd_if(v
, TRU1
);
560 struct a_ccnd_if_node
*cinp
;
564 if((cinp
= n_go_data
->gdc_ifcond
) == NULL
|| cinp
->cin_else
){
565 n_err(_("`else' without a matching `if'\n"));
568 cinp
->cin_else
= TRU1
;
569 cinp
->cin_go
= !cinp
->cin_go
;
579 struct a_ccnd_if_node
*cinp
;
583 if((cinp
= n_go_data
->gdc_ifcond
) == NULL
){
584 n_err(_("`endif' without a matching `if'\n"));
587 n_go_data
->gdc_ifcond
= cinp
->cin_outer
;
596 n_cnd_if_isskip(void){
600 rv
= a_CCND_IF_ISSKIP();
606 n_cnd_if_stack_del(struct n_go_data_ctx
*gdcp
){
607 struct a_ccnd_if_node
*vp
, *cinp
;
610 vp
= gdcp
->gdc_ifcond
;
611 gdcp
->gdc_ifcond
= NULL
;
613 while((cinp
= vp
) != NULL
){
614 vp
= cinp
->cin_outer
;