File: shell.sl

package info (click to toggle)
jed 0.98.7-14
  • links: PTS
  • area: main
  • in suites: slink
  • size: 3,088 kB
  • ctags: 3,851
  • sloc: ansic: 29,315; makefile: 257; sh: 248
file content (279 lines) | stat: -rw-r--r-- 6,504 bytes parent folder | download | duplicates (2)
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
% -*- SLang -*-		shell.sl
% 
% 
%% system specific routines

variable Shell_Last_Shell_Command = Null_String;
variable Shell_Prompt;
#ifdef UNIX
Shell_Prompt = "% ";
#endif
#ifdef MSDOS OS2
Shell_Prompt = "> ";
#endif
#ifdef VMS
Shell_Prompt = "$ ";
#endif


define shell_set_output_buffer ()
{
   variable dir, file, name, flags;
   
   (,dir,,) = getbuf_info ();
   if ( change_default_dir (dir) ) 
     error ("Unable to chdir!");
   
   pop2buf ("*shell-output*"); 
   erase_buffer ();
   
   (file,,name, flags) = getbuf_info ();
   setbuf_info (file, dir,name, flags);
}

define shell_perform_cmd (cmd)
{
   variable status;
   
   shell_set_output_buffer ();
   
   status = run_shell_cmd (cmd);
   bob ();
   
   vmessage ("Exit Status: %d", status);
}


   
   
#ifdef VMS
define run_shell_cmd (cmd)
{
   variable cfile, file = "_jed_shell.cmd_";
   variable tmpdir;
   tmpdir = getenv ("SYS$SCRATCH");
   if (tmpdir == NULL) tmpdir = "SYS$LOGIN:";
   file = dircat (tmpdir, file);

   cfile = expand_jedlib_file ("vms_shell.com");
   !if ( strlen (cfile) ) error ("Unable to open vms_shell.com");

   flush ("starting process ...");
   () = system ((sprintf ("@%s/output=%s \"%s\"", cfile, file, cmd)));
   () = insert_file (file);
   delete_file (file);		       %  value returned
}

define do_shell_cmd ()
{
   variable cmd, dir;

   cmd = read_mini ("Shell Command:", Null_String, Shell_Last_Shell_Command);
   !if ( strlen (cmd) ) return;
   Shell_Last_Shell_Command = cmd;
   shell_perform_cmd (Shell_Last_Shell_Command);
}
#endif VMS

#ifdef MSDOS
% pass "(cmd) 2>&1" contructs to the system as "(cmd) > tmpfile 2>&1"
define run_shell_cmd (cmd)
{
   variable msg = "Executing shell cmd...";
   variable tmp, dir;

   dir = getenv ("TMP");
   if (dir == NULL) dir = "";

   if (2 != file_status (dir))
     (,dir,,) = getbuf_info ();	% get directory
   
   tmp = dircat (dir, "_jed_shl.cmd");
   
   flush (msg);
   
%%    if ( is_substr (cmd, "2>&1") )
%%      both;
%%   	argv = extract_element (cmd, 1, ' ');

   if (system (sprintf ("%s &> %s", cmd, tmp)) < 0)
     error ("system failed.");
   () = insert_file (tmp);
   flush (strcat (msg, "done"));
   delete_file (tmp);		       %  value returned
}

define do_shell_cmd ()
{
   variable cmd, dir, flags, file, name;
   
   cmd = read_mini ("Shell Command:", Null_String, Shell_Last_Shell_Command);
   !if ( strlen (cmd) ) return;
   Shell_Last_Shell_Command = cmd;
   shell_perform_cmd (Shell_Last_Shell_Command);
}
#endif MSDOS

#ifdef UNIX OS2
define do_shell_cmd ()
{
   variable cmd, dir, msg = "opening pipe ...";
   cmd = read_mini ("Shell Command:", Null_String, Shell_Last_Shell_Command);
   !if ( strlen (cmd) ) return;
   Shell_Last_Shell_Command = cmd;

   shell_set_output_buffer ();
   flush (msg);

   shell_cmd (sprintf (
#ifdef OS2
		       "(%s) 2>&1",
#else
		       "(%s) 2>&1 < /dev/null",
#endif
		       Shell_Last_Shell_Command));
   bob ();
   flush (strcat (msg, "done"));
}
#endif UNIX OS2

define shell ()
{
   variable dir, buf = "*shell*";

   !if ( keymap_p (buf) )
     {
	make_keymap (buf);
	definekey ("shell_input", "^M", buf);
     }

   (,dir,,) = getbuf_info ();		% get directory
   if ( change_default_dir (dir) ) error ("Unable to chdir!");

   pop2buf (buf);
   use_keymap (buf);

   vinsert ("\n(%s)\n%s", dir, Shell_Prompt, 2);

   % no backup (0x100), no save ~(0x80), no undo ~(0x20), no autosave ~(0x2)
   % unmodified ~(0x1)
   getbuf_info ();
   setbuf_info ((() | 0x100) & ~(0xA3));
}

%!% rudimentary `builtin' shell commands:
%!%	`cd [dir]'	change the default directory
%!%	`exit'		exit the subshell
%!%	`pwd'		Print Working Directory
%!%
%!% functions to eliminate some jed/shell vs. real shell problems
%!%	`clear'		erase the *shell* buffer
%!%	`e'		simulate ^X^F keybinding
%!%	`jed'		simulate ^X^F keybinding
%!%
%!%
%!% returns one of the following on the stack
%!%	Null_String	- builtin dispatched, no prompt
%!%	"pwd"		- builtin dispatched, give prompt
%!%	cmd		- use shell to execute CMD
define shell_builtin (cmd)
{
   variable argv, dir, buf, flag, pwd = "pwd";
   variable cmd1 = strcompress (cmd, " \t");

   (,dir,buf,flag) = getbuf_info ();		% cwd info

   argv = extract_element (cmd, 0, ' ');	% parse cmd
   switch (argv)
   % simple "aliases"
     {case pwd: argv; }			% 'pwd'='pwd' (no args)
#ifdef UNIX
     {case "dir": 	% 'dir'='ls -Al' [dir [dir]]
	() = str_replace (cmd, argv, "ls -Al");
     }
#endif
     {case "clear": erase_buffer; pwd;}	% 'clear'=erase_buffer()
     {case "exit":			% 'exit'
	set_buffer_modified_flag (0);
	delbuf (buf);
	Null_String;
     }
     {case "cd":			% 'cd' [dir]
	argv = extract_element (cmd1, 1, ' ');
	if (argv == NULL)
#ifdef MSDOS OS2
	  return pwd;
#else
	argv = "~/";
#endif
	argv = expand_filename (argv);
	if ( change_default_dir (argv) ) insert ("Unable to chdir!\n");
	else setbuf_info (Null_String, argv, buf, flag);
	pwd;			% only "pwd" pending
     }
     {case "jed" or case "e":	% jed
	argv = extract_element (cmd1, 1, ' ');
	if (argv != NULL)
	  () = find_file (expand_filename (dircat (dir, argv)));
	Null_String;		% nothing pending
     }
     {cmd;}		% default = failure, cmd still pending
}

define shell_input ()
{
   variable cmd, dir, tmp;

   bol (); skip_chars (Shell_Prompt); skip_white ();
   push_mark_eol ();
   cmd = bufsubstr ();
   eob ();
   bol (); skip_chars (Shell_Prompt); skip_white ();
   !if ( looking_at (cmd) ) insert (cmd);
   eol (); newline ();
   update (0);			% Update now so user see that things are ok

   if ( strlen (cmd) )
     {
	cmd = shell_builtin (cmd);
	!if ( strlen (cmd) )
	  {
	     update (0);
	     return;
	  }

	(,dir,,) = getbuf_info ();	% get directory
	if ( change_default_dir (dir) ) error ("Unable to chdir!");

	if ( strcmp (cmd, "pwd") )	% not "pwd"
	  {
#ifdef VMS
	     tmp = make_tmp_file ("sys$login:_jed_shell_.");
	     variable file = expand_jedlib_file ("vms_shell.com");
	     !if ( strlen (file) ) error ("Unable to find vms_shell.com");
	     () = system (sprintf ("@%s/output= %s %s", file, tmp, cmd));
	     () = insert_file (tmp);
	     () = delete_file (tmp);
#else
#ifdef MSDOS
	     shell_cmd (cmd);
#else
	     shell_cmd (sprintf ("(%s) 2>&1", cmd));
#endif
#endif
	  }
	vinsert ("\n(%s)\n%s", dir, Shell_Prompt, 2);
     }
   else
     {
	!if ( bolp () ) newline ();
	insert (Shell_Prompt);
     }
   set_buffer_modified_flag (0);

#ifdef OS2
   update (1);			% Update due to problems with Borland C
#endif
}

%%%%%%%%%%%%%%%%%%%%%%%%%%