1 /*@ S-nail - a mail user agent derived from Berkeley Mail.
2 *@ Miscellaneous user commands, like `echo', `pwd', etc.
4 * Copyright (c) 2000-2004 Gunnar Ritter, Freiburg i. Br., Germany.
5 * Copyright (c) 2012 - 2018 Steffen (Daode) Nurpmeso <steffen@sdaoden.eu>.
6 * SPDX-License-Identifier: BSD-3-Clause
9 * Copyright (c) 1980, 1993
10 * The Regents of the University of California. All rights reserved.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 #define n_FILE cmd_misc
39 #ifndef HAVE_AMALGAMATION
43 #include <sys/utsname.h>
45 /* Expand the shell escape by expanding unescaped !'s into the last issued
46 * command where possible */
47 static char const *a_cmisc_bangexp(char const *cp
);
49 /* c_n?echo(), c_n?echoerr() */
50 static int a_cmisc_echo(void *vp
, FILE *fp
, bool_t donl
);
53 static bool_t
a_cmisc_read_set(char const *cp
, char const *value
);
56 static int a_cmisc_version_cmp(void const *s1
, void const *s2
);
59 a_cmisc_bangexp(char const *cp
){
60 static struct str last_bang
;
62 struct n_string xbang
, *bang
;
72 for(bang
= n_string_creat(&xbang
); (c
= *cp
++) != '\0';){
75 bang
= n_string_push_buf(bang
, last_bang
.s
, last_bang
.l
);
78 if(c
== '\\' && *cp
== '!'){
83 bang
= n_string_push_c(bang
, c
);
87 if(last_bang
.s
!= NULL
)
89 last_bang
.s
= n_string_cp(bang
);
90 last_bang
.l
= bang
->s_len
;
91 bang
= n_string_drop_ownership(bang
);
96 fprintf(n_stdout
, "!%s\n", cp
);
103 a_cmisc_echo(void *vp
, FILE *fp
, bool_t donl
){
104 struct n_string s
, *sp
;
107 char const **argv
, *varname
, **ap
, *cp
;
111 varname
= (n_pstate
& n_PS_ARGMOD_VPUT
) ? *argv
++ : NULL
;
112 sp
= n_string_reserve(n_string_creat_auto(&s
), 121/* XXX */);
114 doerr
= (fp
== n_stderr
&& (n_psonce
& n_PSO_INTERACTIVE
));
119 for(ap
= argv
; *ap
!= NULL
; ++ap
){
121 sp
= n_string_push_c(sp
, ' ');
122 if((cp
= fexpand(*ap
, FEXP_NSHORTCUT
| FEXP_NVAR
)) == NULL
)
124 sp
= n_string_push_cp(sp
, cp
);
127 sp
= n_string_push_c(sp
, '\n');
128 cp
= n_string_cp(sp
);
135 /* xxx Ensure *log-prefix* will be placed by n_err() for next msg */
137 cp
= n_string_cp(n_string_trunc(sp
, sp
->s_len
- 1));
138 n_err((donl
? "%s\n" : "%s"), cp
);
139 }else if(fputs(cp
, fp
) == EOF
)
141 if((rv
= (fflush(fp
) == EOF
)))
143 rv
|= ferror(fp
) ? 1 : 0;
145 }else if(!n_var_vset(varname
, (uintptr_t)cp
)){
146 n_pstate_err_no
= n_ERR_NOTSUP
;
149 n_pstate_err_no
= n_ERR_NONE
;
157 a_cmisc_read_set(char const *cp
, char const *value
){
161 if(!n_shexp_is_valid_varname(cp
))
162 value
= N_("not a valid variable name");
163 else if(!n_var_is_user_writable(cp
))
164 value
= N_("variable is read-only");
165 else if(!n_var_vset(cp
, (uintptr_t)value
))
166 value
= N_("failed to update variable value");
171 n_err("`read': %s: %s\n", V_(value
), n_shexp_quote_cp(cp
, FAL0
));
179 a_cmisc_version_cmp(void const *s1
, void const *s2
){
180 char const * const *cp1
, * const *cp2
;
186 rv
= strcmp(&(*cp1
)[1], &(*cp2
)[1]);
199 if((n_idec_uiz_cp(&sec
, argv
[0], 0, NULL
) &
200 (n_IDEC_STATE_EMASK
| n_IDEC_STATE_CONSUMED
)
201 ) != n_IDEC_STATE_CONSUMED
)
206 else if((n_idec_uiz_cp(&msec
, argv
[1], 0, NULL
) &
207 (n_IDEC_STATE_EMASK
| n_IDEC_STATE_CONSUMED
)
208 ) != n_IDEC_STATE_CONSUMED
)
211 if(UIZ_MAX
/ n_DATE_MILLISSEC
< sec
)
213 sec
*= n_DATE_MILLISSEC
;
215 if(UIZ_MAX
- sec
< msec
)
219 n_pstate_err_no
= (n_msleep(msec
, (argv
[2] == NULL
)) > 0)
220 ? n_ERR_INTR
: n_ERR_NONE
;
223 return (argv
== NULL
);
225 n_err(_("`sleep': argument(s) overflow(s) datatype\n"));
226 n_pstate_err_no
= n_ERR_OVERFLOW
;
230 n_err(_("Synopsis: sleep: <seconds> [<milliseconds>] [uninterruptible]\n"));
231 n_pstate_err_no
= n_ERR_INVAL
;
242 char const **argv
, *varname
, *varres
, *cp
;
245 n_pstate_err_no
= n_ERR_NONE
;
247 varname
= (n_pstate
& n_PS_ARGMOD_VPUT
) ? *argv
++ : NULL
;
251 if(varname
!= NULL
&&
252 (fp
= Ftmp(NULL
, "shell", OF_RDWR
| OF_UNLINK
| OF_REGISTER
)
254 n_pstate_err_no
= n_ERR_CANCELED
;
257 cp
= a_cmisc_bangexp(*argv
);
260 if(n_child_run(ok_vlook(SHELL
), &mask
,
261 n_CHILD_FD_PASS
, (fp
!= NULL
? fileno(fp
) : n_CHILD_FD_PASS
),
262 "-c", cp
, NULL
, NULL
, &rv
) < 0){
263 n_pstate_err_no
= n_err_no
;
266 rv
= WEXITSTATUS(rv
);
275 if(UICMP(64, l
, >=, UIZ_MAX
-42)){
276 n_pstate_err_no
= n_ERR_NOMEM
;
279 varres
= x
= n_autorec_alloc(l
+1);
281 for(; l
> 0 && (c
= getc(fp
)) != EOF
; --l
)
285 n_pstate_err_no
= n_err_no
;
286 varres
= n_empty
; /* xxx hmmm */
297 if(!n_var_vset(varname
, (uintptr_t)varres
)){
298 n_pstate_err_no
= n_ERR_NOTSUP
;
301 }else if(rv
>= 0 && (n_psonce
& n_PSO_INTERACTIVE
)){
302 fprintf(n_stdout
, "!\n");
303 /* Line buffered fflush(n_stdout); */
316 if(n_child_run(ok_vlook(SHELL
), 0, n_CHILD_FD_PASS
, n_CHILD_FD_PASS
, NULL
,
317 NULL
, NULL
, NULL
, &rv
) < 0){
318 n_pstate_err_no
= n_err_no
;
321 putc('\n', n_stdout
);
322 /* Line buffered fflush(n_stdout); */
323 n_pstate_err_no
= n_ERR_NONE
;
324 rv
= WEXITSTATUS(rv
);
332 struct n_string s_b
, *sp
;
337 sp
= n_string_creat_auto(&s_b
);
338 varname
= (n_pstate
& n_PS_ARGMOD_VPUT
) ? *(char const**)v
: NULL
;
341 for(;; l
+= PATH_MAX
){
342 sp
= n_string_resize(n_string_trunc(sp
, 0), l
);
344 if(getcwd(sp
->s_dat
, sp
->s_len
) == NULL
){
350 n_perr(_("Failed to getcwd(3)"), e
);
356 if(!n_var_vset(varname
, (uintptr_t)sp
->s_dat
))
359 l
= strlen(sp
->s_dat
);
360 sp
= n_string_trunc(sp
, l
);
361 if(fwrite(sp
->s_dat
, 1, sp
->s_len
, n_stdout
) == sp
->s_len
&&
362 putc('\n', n_stdout
) == EOF
)
378 if (*arglist
== NULL
)
380 else if ((cp
= fexpand(*arglist
, FEXP_LOCAL
| FEXP_NOPROTO
)) == NULL
)
382 if (chdir(cp
) == -1) {
396 rv
= a_cmisc_echo(v
, n_stdout
, TRU1
);
406 rv
= a_cmisc_echo(v
, n_stderr
, TRU1
);
416 rv
= a_cmisc_echo(v
, n_stdout
, FAL0
);
426 rv
= a_cmisc_echo(v
, n_stderr
, FAL0
);
432 c_read(void * volatile vp
){
435 struct n_string s
, *sp
;
439 char const *ifs
, **argv
, *cp
;
442 sp
= n_string_creat_auto(&s
);
443 sp
= n_string_reserve(sp
, 64 -1);
450 n_SIGMAN_ENTER_SWITCH(&sm
, n_SIGMAN_ALL
){
454 n_pstate_err_no
= n_ERR_INTR
;
459 n_pstate_err_no
= n_ERR_NONE
;
460 rv
= n_go_input(((n_pstate
& n_PS_COMPOSE_MODE
461 ? n_GO_INPUT_CTX_COMPOSE
: n_GO_INPUT_CTX_DEFAULT
) |
462 n_GO_INPUT_FORCE_STDIN
| n_GO_INPUT_NL_ESC
|
463 n_GO_INPUT_PROMPT_NONE
/* XXX POSIX: PS2: yes! */),
464 NULL
, &linebuf
, &linesize
, NULL
, NULL
);
466 if(!n_go_input_is_eof())
467 n_pstate_err_no
= n_ERR_BADF
;
470 if(n_go_input_is_eof()){
478 for(; *argv
!= NULL
; ++argv
){
479 if(trim
.l
== 0 || n_str_trim_ifs(&trim
, FAL0
)->l
== 0)
482 /* The last variable gets the remaining line less trailing IFS-WS */
485 sp
= n_string_assign_buf(sp
, trim
.s
, trim
.l
);
487 }else for(cp
= trim
.s
, i
= 1;; ++cp
, ++i
){
488 if(strchr(ifs
, *cp
) != NULL
){
489 sp
= n_string_assign_buf(sp
, trim
.s
, i
- 1);
498 if(!a_cmisc_read_set(*argv
, n_string_cp(sp
))){
499 n_pstate_err_no
= n_ERR_NOTSUP
;
506 /* Set the remains to the empty string */
507 for(; *argv
!= NULL
; ++argv
)
508 if(!a_cmisc_read_set(*argv
, n_empty
)){
509 n_pstate_err_no
= n_ERR_NOTSUP
;
514 n_sigman_cleanup_ping(&sm
);
519 n_sigman_leave(&sm
, n_SIGMAN_VIPSIGS_NTTYOUT
);
524 c_readall(void * vp
){ /* TODO 64-bit retval */
526 struct n_string s
, *sp
;
533 sp
= n_string_creat_auto(&s
);
534 sp
= n_string_reserve(sp
, 64 -1);
540 n_SIGMAN_ENTER_SWITCH(&sm
, n_SIGMAN_ALL
){
544 n_pstate_err_no
= n_ERR_INTR
;
549 n_pstate_err_no
= n_ERR_NONE
;
552 rv
= n_go_input(((n_pstate
& n_PS_COMPOSE_MODE
553 ? n_GO_INPUT_CTX_COMPOSE
: n_GO_INPUT_CTX_DEFAULT
) |
554 n_GO_INPUT_FORCE_STDIN
| /*n_GO_INPUT_NL_ESC |*/
555 n_GO_INPUT_PROMPT_NONE
),
556 NULL
, &linebuf
, &linesize
, NULL
, NULL
);
558 if(!n_go_input_is_eof()){
559 n_pstate_err_no
= n_ERR_BADF
;
567 if(n_pstate
& n_PS_READLINE_NL
)
568 linebuf
[rv
++] = '\n'; /* Replace NUL with it */
570 if(n_UNLIKELY(rv
== 0)){ /* xxx will not get*/
571 if(n_go_input_is_eof()){
578 }else if(n_LIKELY(UICMP(32, SI32_MAX
- sp
->s_len
, >, rv
)))
579 sp
= n_string_push_buf(sp
, linebuf
, rv
);
581 n_pstate_err_no
= n_ERR_OVERFLOW
;
587 if(!a_cmisc_read_set(argv
[0], n_string_cp(sp
))){
588 n_pstate_err_no
= n_ERR_NOTSUP
;
594 n_sigman_cleanup_ping(&sm
);
599 n_sigman_leave(&sm
, n_SIGMAN_VIPSIGS_NTTYOUT
);
606 struct n_string s
, *sp
= &s
;
609 char const *cp
, **arr
;
613 sp
= n_string_creat_auto(sp
);
614 sp
= n_string_book(sp
, 1024);
616 /* First two lines */
617 sp
= n_string_push_cp(sp
, n_uagent
);
618 sp
= n_string_push_c(sp
, ' ');
619 sp
= n_string_push_cp(sp
, ok_vlook(version
));
620 sp
= n_string_push_c(sp
, ',');
621 sp
= n_string_push_c(sp
, ' ');
622 sp
= n_string_push_cp(sp
, ok_vlook(version_date
));
623 sp
= n_string_push_c(sp
, ' ');
624 sp
= n_string_push_c(sp
, '(');
625 sp
= n_string_push_cp(sp
, _("build for "));
626 sp
= n_string_push_cp(sp
, ok_vlook(build_os
));
627 sp
= n_string_push_c(sp
, ')');
628 sp
= n_string_push_cp(sp
, _("\nFeatures included (+) or not (-):\n"));
630 /* Some lines with the features.
631 * *features* starts with dummy byte to avoid + -> *folder* expansions */
632 i
= strlen(cp
= &ok_vlook(features
)[1]) +1;
633 iop
= n_autorec_alloc(i
);
636 arr
= n_autorec_alloc(sizeof(cp
) * VAL_FEATURES_CNT
);
637 for(i
= 0; (cp
= n_strsep(&iop
, ',', TRU1
)) != NULL
; ++i
)
639 qsort(arr
, i
, sizeof(cp
), &a_cmisc_version_cmp
);
641 for(lnlen
= 0; i
-- > 0;){
645 if((lnlen
+= j
+ 1) > 72){
646 sp
= n_string_push_c(sp
, '\n');
649 sp
= n_string_push_c(sp
, ' ');
650 sp
= n_string_push_buf(sp
, cp
, j
);
652 sp
= n_string_push_c(sp
, '\n');
655 if(n_poption
& n_PO_VERB
){
656 sp
= n_string_push_cp(sp
, "Compile: ");
657 sp
= n_string_push_cp(sp
, ok_vlook(build_cc
));
658 sp
= n_string_push_cp(sp
, "\nLink: ");
659 sp
= n_string_push_cp(sp
, ok_vlook(build_ld
));
660 if(*(cp
= ok_vlook(build_rest
)) != '\0'){
661 sp
= n_string_push_cp(sp
, "\nRest: ");
662 sp
= n_string_push_cp(sp
, cp
);
664 sp
= n_string_push_c(sp
, '\n');
666 /* A trailing line with info of the running machine */
668 sp
= n_string_push_c(sp
, '@');
669 sp
= n_string_push_cp(sp
, ut
.sysname
);
670 sp
= n_string_push_c(sp
, ' ');
671 sp
= n_string_push_cp(sp
, ut
.release
);
672 sp
= n_string_push_c(sp
, ' ');
673 sp
= n_string_push_cp(sp
, ut
.version
);
674 sp
= n_string_push_c(sp
, ' ');
675 sp
= n_string_push_cp(sp
, ut
.machine
);
676 sp
= n_string_push_c(sp
, '\n');
680 cp
= n_string_cp(sp
);
682 if(n_pstate
& n_PS_ARGMOD_VPUT
){
683 if(n_var_vset(*(char const**)vp
, (uintptr_t)cp
))
688 if(fputs(cp
, n_stdout
) != EOF
)