Bump S-nail v14.9.0-pre4, 2017-04-13
[s-mailx.git] / cmd-misc.c
blob72741d6d5887ee821f1c562df00a52f82e504768
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>.
6 */
7 /*
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
13 * are met:
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
33 * SUCH DAMAGE.
35 #undef n_FILE
36 #define n_FILE cmd_misc
38 #ifndef HAVE_AMALGAMATION
39 # include "nail.h"
40 #endif
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);
49 static char const *
50 a_cmisc_bangexp(char const *cp){
51 static struct str last_bang;
53 struct n_string xbang, *bang;
54 char c;
55 bool_t changed;
56 NYD_ENTER;
58 if(!ok_blook(bang))
59 goto jleave;
61 changed = FAL0;
63 for(bang = n_string_creat(&xbang); (c = *cp++) != '\0';){
64 if(c == '!'){
65 if(last_bang.l > 0)
66 bang = n_string_push_buf(bang, last_bang.s, last_bang.l);
67 changed = TRU1;
68 }else{
69 if(c == '\\' && *cp == '!'){
70 ++cp;
71 c = '!';
72 changed = TRU1;
74 bang = n_string_push_c(bang, c);
78 if(last_bang.s != NULL)
79 free(last_bang.s);
80 last_bang.s = n_string_cp(bang);
81 last_bang.l = bang->s_len;
82 bang = n_string_drop_ownership(bang);
83 n_string_gut(bang);
85 cp = last_bang.s;
86 if(changed)
87 fprintf(n_stdout, "!%s\n", cp);
88 jleave:
89 NYD_LEAVE;
90 return cp;
93 static int
94 a_cmisc_echo(void *vp, FILE *fp, bool_t donl){
95 char const **argv, **ap, *cp;
96 NYD2_ENTER;
98 for(ap = argv = vp; *ap != NULL; ++ap){
99 if(ap != argv)
100 putc(' ', fp);
101 if((cp = fexpand(*ap, FEXP_NSHORTCUT | FEXP_NVAR)) == NULL)
102 cp = *ap;
103 fputs(cp, fp);
105 if(donl)
106 putc('\n', fp);
107 fflush(fp);
108 NYD2_LEAVE;
109 return 0;
112 FL int
113 c_sleep(void *v){
114 uiz_t sec, msec;
115 char **argv;
116 NYD_ENTER;
118 argv = v;
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)
123 goto jesyn;
125 if(argv[1] == NULL)
126 msec = 0;
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)
130 goto jesyn;
132 if(UIZ_MAX / n_DATE_MILLISSEC < sec)
133 goto jeover;
134 sec *= n_DATE_MILLISSEC;
136 if(UIZ_MAX - sec < msec)
137 goto jeover;
138 msec += sec;
140 n_pstate_err_no = (n_msleep(msec, (argv[2] == NULL)) > 0)
141 ? n_ERR_INTR : n_ERR_NONE;
142 jleave:
143 NYD_LEAVE;
144 return (argv == NULL);
145 jeover:
146 n_err(_("`sleep': argument(s) overflow(s) datatype\n"));
147 n_pstate_err_no = n_ERR_OVERFLOW;
148 argv = NULL;
149 goto jleave;
150 jesyn:
151 n_err(_("Synopsis: sleep: <seconds> [<milliseconds>] [uninterruptible]\n"));
152 n_pstate_err_no = n_ERR_INVAL;
153 argv = NULL;
154 goto jleave;
157 FL int
158 c_shell(void *v)
160 sigset_t mask;
161 char const *cp;
162 NYD_ENTER;
164 cp = a_cmisc_bangexp(v);
166 sigemptyset(&mask);
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); */
171 NYD_LEAVE;
172 return 0;
175 FL int
176 c_dosh(void *v)
178 NYD_ENTER;
179 n_UNUSED(v);
181 n_child_run(ok_vlook(SHELL), 0, n_CHILD_FD_PASS, n_CHILD_FD_PASS, NULL,
182 NULL, NULL, NULL);
183 putc('\n', n_stdout);
184 /* Line buffered fflush(n_stdout); */
185 NYD_LEAVE;
186 return 0;
189 FL int
190 c_cwd(void *v){
191 struct n_string s_b, *sp;
192 size_t l;
193 char const *varname;
194 NYD_ENTER;
196 sp = n_string_creat_auto(&s_b);
197 varname = (n_pstate & n_PS_ARGMOD_VPUT) ? *(char const**)v : NULL;
198 l = PATH_MAX;
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){
204 int e;
206 e = n_err_no;
207 if(e == n_ERR_RANGE)
208 continue;
209 n_perr(_("Failed to getcwd(3)"), e);
210 v = NULL;
211 break;
214 if(varname != NULL){
215 if(!n_var_vset(varname, (uintptr_t)sp->s_dat))
216 v = NULL;
217 }else{
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)
222 v = NULL;
224 break;
226 NYD_LEAVE;
227 return (v == NULL);
230 FL int
231 c_chdir(void *v)
233 char **arglist = v;
234 char const *cp;
235 NYD_ENTER;
237 if (*arglist == NULL)
238 cp = ok_vlook(HOME);
239 else if ((cp = fexpand(*arglist, FEXP_LOCAL | FEXP_NOPROTO)) == NULL)
240 goto jleave;
241 if (chdir(cp) == -1) {
242 n_perr(cp, 0);
243 cp = NULL;
245 jleave:
246 NYD_LEAVE;
247 return (cp == NULL);
250 FL int
251 c_echo(void *v){
252 int rv;
253 NYD_ENTER;
255 rv = a_cmisc_echo(v, n_stdout, TRU1);
256 NYD_LEAVE;
257 return rv;
260 FL int
261 c_echoerr(void *v){
262 int rv;
263 NYD_ENTER;
265 rv = a_cmisc_echo(v, n_stderr, TRU1);
266 NYD_LEAVE;
267 return rv;
270 FL int
271 c_echon(void *v){
272 int rv;
273 NYD_ENTER;
275 rv = a_cmisc_echo(v, n_stdout, FAL0);
276 NYD_LEAVE;
277 return rv;
280 FL int
281 c_echoerrn(void *v){
282 int rv;
283 NYD_ENTER;
285 rv = a_cmisc_echo(v, n_stderr, FAL0);
286 NYD_LEAVE;
287 return rv;
290 /* s-it-mode */