File: execute.c

package info (click to toggle)
nn 6.7.3-16.1
  • links: PTS
  • area: main
  • in suites: forky, sid
  • size: 3,688 kB
  • sloc: ansic: 32,039; sh: 1,491; awk: 138; makefile: 80
file content (210 lines) | stat: -rw-r--r-- 3,725 bytes parent folder | download | duplicates (9)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/*
 *	(c) Copyright 1990, Kim Fabricius Storm.  All rights reserved.
 *      Copyright (c) 1996-2005 Michael T Pins.  All rights reserved.
 *
 *	UNIX command execution.
 */

#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include <signal.h>
#include <errno.h>
#include <sys/wait.h>
#include <string.h>
#include "config.h"
#include "global.h"
#include "execute.h"
#include "folder.h"
#include "nn_term.h"


/* execute.c */

static int      shell_check(void);


int             shell_restrictions = 0;	/* disable shell escapes */

static char    *init_shell = SHELL;
char           *user_shell;
char           *exec_chdir_to = NULL;

extern int      errno;


static int
shell_check(void)
{
    if (shell_restrictions) {
	msg("Restricted operation - not allowed");
	return -1;
    }
    return 0;
}

void
init_execute(void)
{
    if ((user_shell = getenv("SHELL")) == NULL)
	user_shell = SHELL;
}

int
execute(char *path, char **args, int toggle_visual)
{
    int             was_raw, pid, i, status;
    sig_type(*quit) (), (*intr) (), (*tstp) ();

    was_raw = toggle_visual ? visual_off() : unset_raw();

    while ((pid = fork()) == -1)
	sleep(1);

    if (pid == 0) {
	for (i = 3; i < 20; i++)
	    close(i);

	if (exec_chdir_to != NULL)
	    chdir(exec_chdir_to);

	if (execvp(path, args)) {
	    msg("%s: %s", path, strerror(errno));
	    user_delay(2);
	    exit(20);
	}
    }
    quit = signal(SIGQUIT, SIG_IGN);
    intr = signal(SIGINT, SIG_IGN);

#ifdef HAVE_JOBCONTROL
    tstp = signal(SIGTSTP, SIG_DFL);
#endif

    while ((i = wait(&status)) != pid && (i != -1 || errno == EINTR));

    signal(SIGQUIT, quit);
    signal(SIGINT, intr);

#ifdef HAVE_JOBCONTROL
    signal(SIGTSTP, tstp);
#endif

    if (toggle_visual) {
	visual_on();
	if (toggle_visual == 2)
	    s_redraw++;
    }
    if (was_raw)
	nn_raw();

    return (status & 0xff) ? 0x100 : (status >> 8);
}


int
shell_escape(void)
{
    static char     command[FILENAME] = "";
    char           *cmd;
    int             first = 1;

    if (shell_check())
	return 0;

    for (;;) {
	prompt("\1!\1");
	cmd = get_s(command, NONE, NONE, NULL_FCT);
	if (cmd == NULL)
	    return !first;

	if (*cmd == NUL) {
	    if (first)
		run_shell((char *) NULL, 1, 0);
	    return 1;
	}
	strcpy(command, cmd);

	if (run_shell(command, first, 0) < 0)
	    return !first;
	first = 0;
	prompt_line = -1;
    }
}


static char    *exec_sh_args[] = {
    "nnsh",
    (char *) NULL,		/* "-c" or "-i" */
    (char *) NULL,		/* cmdstring */
    (char *) NULL
};

int
run_shell(char *command, int clear, int init_sh)

 /*
  * clear	-2 => no command output (:!!command) - keep visual, output
  * before command: -1 => none, 0 => CR/NL, 1 => clear
  */
 /* init_sh	0 => use user_shell, else use init_shell */
{
    char            cmdstring[512];

    if (shell_check())
	return -1;

    if (command != NULL) {
	if (!expand_file_name(cmdstring, command, 1))
	    return -1;
	exec_sh_args[1] = "-c";
	exec_sh_args[2] = cmdstring;
    } else {
	exec_sh_args[1] = "-i";
	exec_sh_args[2] = NULL;
    }

    if (clear > 0) {
	clrdisp();
	fl;
    } else if (clear == 0) {
	tputc(CR);
	tputc(NL);
    }
    return execute(init_sh ? init_shell : user_shell,
		   exec_sh_args, clear == -2 ? 0 : 1);
}

#ifndef HAVE_JOBCONTROL
static char    *exec_suspend_args[] = {
    "nnsh",
    "-i",
    (char *) NULL
};

#endif

int
suspend_nn(void)
{
    int             was_raw;

    if (shell_check())
	return 0;

    gotoxy(0, Lines - 1);
    clrline();

#ifdef HAVE_JOBCONTROL
    was_raw = visual_off();
    kill(process_id, SIGSTOP);
    visual_on();
    s_redraw++;
    if (was_raw)
	nn_raw();
#else
    execute(user_shell, exec_suspend_args, 2);
#endif

    return 1;
}