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 - 2017 Steffen (Daode) Nurpmeso <steffen@sdaoden.eu>.
8 * Copyright (c) 1980, 1993
9 * The Regents of the University of California. All rights reserved.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 #define n_FILE cmd_misc
38 #ifndef HAVE_AMALGAMATION
42 /* Expand the shell escape by expanding unescaped !'s into the last issued
43 * command where possible */
44 static char const *a_cmisc_bangexp(char const *cp
);
46 /* c_n?echo(), c_n?echoerr() */
47 static int a_cmisc_echo(void *vp
, FILE *fp
, bool_t donl
);
50 a_cmisc_bangexp(char const *cp
){
51 static struct str last_bang
;
53 struct n_string xbang
, *bang
;
63 for(bang
= n_string_creat(&xbang
); (c
= *cp
++) != '\0';){
66 bang
= n_string_push_buf(bang
, last_bang
.s
, last_bang
.l
);
69 if(c
== '\\' && *cp
== '!'){
74 bang
= n_string_push_c(bang
, c
);
78 if(last_bang
.s
!= NULL
)
80 last_bang
.s
= n_string_cp(bang
);
81 last_bang
.l
= bang
->s_len
;
82 bang
= n_string_drop_ownership(bang
);
87 fprintf(n_stdout
, "!%s\n", cp
);
94 a_cmisc_echo(void *vp
, FILE *fp
, bool_t donl
){
95 char const **argv
, **ap
, *cp
;
98 for(ap
= argv
= vp
; *ap
!= NULL
; ++ap
){
101 if((cp
= fexpand(*ap
, FEXP_NSHORTCUT
| FEXP_NVAR
)) == NULL
)
120 if((n_idec_uiz_cp(&sec
, argv
[0], 0, NULL
) &
121 (n_IDEC_STATE_EMASK
| n_IDEC_STATE_CONSUMED
)
122 ) != n_IDEC_STATE_CONSUMED
)
127 else if((n_idec_uiz_cp(&msec
, argv
[1], 0, NULL
) &
128 (n_IDEC_STATE_EMASK
| n_IDEC_STATE_CONSUMED
)
129 ) != n_IDEC_STATE_CONSUMED
)
132 if(UIZ_MAX
/ n_DATE_MILLISSEC
< sec
)
134 sec
*= n_DATE_MILLISSEC
;
136 if(UIZ_MAX
- sec
< msec
)
140 n_pstate_err_no
= (n_msleep(msec
, (argv
[2] == NULL
)) > 0)
141 ? n_ERR_INTR
: n_ERR_NONE
;
144 return (argv
== NULL
);
146 n_err(_("`sleep': argument(s) overflow(s) datatype\n"));
147 n_pstate_err_no
= n_ERR_OVERFLOW
;
151 n_err(_("Synopsis: sleep: <seconds> [<milliseconds>] [uninterruptible]\n"));
152 n_pstate_err_no
= n_ERR_INVAL
;
164 cp
= a_cmisc_bangexp(v
);
167 n_child_run(ok_vlook(SHELL
), &mask
, n_CHILD_FD_PASS
, n_CHILD_FD_PASS
,
168 "-c", cp
, NULL
, NULL
);
169 fprintf(n_stdout
, "!\n");
170 /* Line buffered fflush(n_stdout); */
181 n_child_run(ok_vlook(SHELL
), 0, n_CHILD_FD_PASS
, n_CHILD_FD_PASS
, NULL
,
183 putc('\n', n_stdout
);
184 /* Line buffered fflush(n_stdout); */
191 struct n_string s_b
, *sp
;
196 sp
= n_string_creat_auto(&s_b
);
197 varname
= (n_pstate
& n_PS_ARGMOD_VPUT
) ? *(char const**)v
: NULL
;
200 for(;; l
+= PATH_MAX
){
201 sp
= n_string_resize(n_string_trunc(sp
, 0), l
);
203 if(getcwd(sp
->s_dat
, sp
->s_len
) == NULL
){
209 n_perr(_("Failed to getcwd(3)"), e
);
215 if(!n_var_vset(varname
, (uintptr_t)sp
->s_dat
))
218 l
= strlen(sp
->s_dat
);
219 sp
= n_string_trunc(sp
, l
);
220 if(fwrite(sp
->s_dat
, 1, sp
->s_len
, n_stdout
) == sp
->s_len
&&
221 putc('\n', n_stdout
) == EOF
)
237 if (*arglist
== NULL
)
239 else if ((cp
= fexpand(*arglist
, FEXP_LOCAL
| FEXP_NOPROTO
)) == NULL
)
241 if (chdir(cp
) == -1) {
255 rv
= a_cmisc_echo(v
, n_stdout
, TRU1
);
265 rv
= a_cmisc_echo(v
, n_stderr
, TRU1
);
275 rv
= a_cmisc_echo(v
, n_stdout
, FAL0
);
285 rv
= a_cmisc_echo(v
, n_stderr
, FAL0
);