Generalize and straigthen handling of the soft-exit-status *!*
[s-mailx.git] / cmd_misc.c
blobefeaac2feb00f8618da8e71e010446dd105bc386
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_shell(void *v)
115 sigset_t mask;
116 char const *cp;
117 NYD_ENTER;
119 cp = a_cmisc_bangexp(v);
121 sigemptyset(&mask);
122 run_command(ok_vlook(SHELL), &mask, COMMAND_FD_PASS, COMMAND_FD_PASS, "-c",
123 cp, NULL, NULL);
124 fprintf(n_stdout, "!\n");
125 NYD_LEAVE;
126 return 0;
129 FL int
130 c_dosh(void *v)
132 NYD_ENTER;
133 n_UNUSED(v);
135 run_command(ok_vlook(SHELL), 0, COMMAND_FD_PASS, COMMAND_FD_PASS, NULL,
136 NULL, NULL, NULL);
137 putc('\n', n_stdout);
138 NYD_LEAVE;
139 return 0;
142 FL int
143 c_cwd(void *v){
144 struct n_string s_b, *sp;
145 size_t l;
146 char const *varname;
147 NYD_ENTER;
149 sp = n_string_creat_auto(&s_b);
150 varname = (n_pstate & n_PS_ARGMOD_VPUT) ? *(char const**)v : NULL;
151 l = PATH_MAX;
153 for(;; l += PATH_MAX){
154 sp = n_string_resize(n_string_trunc(sp, 0), l);
156 if(getcwd(sp->s_dat, sp->s_len) == NULL){
157 int e;
159 e = errno;
160 if(e == ERANGE)
161 continue;
162 n_perr(_("Failed to getcwd(3)"), e);
163 v = NULL;
164 break;
167 if(varname != NULL){
168 if(n_var_vset(varname, (uintptr_t)sp->s_dat))
169 n_pstate_var__em = n_0;
170 else
171 v = NULL;
172 }else{
173 l = strlen(sp->s_dat);
174 sp = n_string_trunc(sp, l);
175 if(fwrite(sp->s_dat, 1, sp->s_len, n_stdout) == sp->s_len &&
176 putc('\n', n_stdout) != EOF)
177 n_pstate_var__em = n_0;
178 else
179 v = NULL;
181 break;
183 NYD_LEAVE;
184 return (v == NULL);
187 FL int
188 c_chdir(void *v)
190 char **arglist = v;
191 char const *cp;
192 NYD_ENTER;
194 if (*arglist == NULL)
195 cp = ok_vlook(HOME);
196 else if ((cp = fexpand(*arglist, FEXP_LOCAL | FEXP_NOPROTO)) == NULL)
197 goto jleave;
198 if (chdir(cp) == -1) {
199 n_perr(cp, 0);
200 cp = NULL;
202 jleave:
203 NYD_LEAVE;
204 return (cp == NULL);
207 FL int
208 c_echo(void *v){
209 int rv;
210 NYD_ENTER;
212 rv = a_cmisc_echo(v, n_stdout, TRU1);
213 NYD_LEAVE;
214 return rv;
217 FL int
218 c_echoerr(void *v){
219 int rv;
220 NYD_ENTER;
222 rv = a_cmisc_echo(v, n_stderr, TRU1);
223 NYD_LEAVE;
224 return rv;
227 FL int
228 c_echon(void *v){
229 int rv;
230 NYD_ENTER;
232 rv = a_cmisc_echo(v, n_stdout, FAL0);
233 NYD_LEAVE;
234 return rv;
237 FL int
238 c_echoerrn(void *v){
239 int rv;
240 NYD_ENTER;
242 rv = a_cmisc_echo(v, n_stderr, FAL0);
243 NYD_LEAVE;
244 return rv;
247 /* s-it-mode */