-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.c
452 lines (375 loc) · 8.52 KB
/
main.c
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
/************************************************************************
* *
* TUsh - The Telnet User Shell Simon Marsh 1992 *
* *
************************************************************************/
/*
This is the file main.c
*/
#include <stdlib.h>
#include <time.h>
#include <errno.h>
#include <fcntl.h>
#include <sys/types.h>
#include <signal.h>
#include <sys/param.h>
#include <ctype.h>
#include <malloc.h>
#include "config.h"
#ifndef SIGWINCH
#define SIGWINCH SIGWINDOW
#endif
extern void redraw_screen(),centre(),print(),handle_user(),newline(),
execute(),update(),init_term(),restore_term(),cls(),restart(),
window_change(),draw_input();
extern int wchange;
int quit=0;
shell *first_shell=0;
int no_of_shells=0;
shell *current_shell=0;
int key_buff_len=STANDARD_KEY_BUFF_LEN;
int key_history_len=STANDARD_KEY_HISTORY_LEN;
int screen_history_len=STANDARD_KEY_HISTORY_LEN;
int screen_buff_len=STANDARD_SCREEN_BUFF_LEN;
void *scratch;
int scratch_size=SCRATCH_SIZE;
char time_string[80]=STANDARD_TIME_STRING;
file site_list;
int error_type=0;
/*
some misc routines
*/
/* in case of problems ... */
handle_error(str)
{
printf("\n\nERROR - %s\n\n",str);
restore_all();
exit(-1);
}
/* print out the time in a nice format */
char *convert_time()
{
static char buff[80];
time_t t;
t=time(0);
strftime(buff,80,time_string,localtime(&t));
return buff;
}
/* change and print pathname routines */
void pwd(sh,str)
shell *sh;
char *str;
{
char pathname[MAXPATHLEN];
#ifdef SYSV
getcwd(pathname,MAXPATHLEN);
#else
getwd(pathname);
#endif
shell_print(sh,pathname);
}
void change_dir(sh,str)
shell *sh;
char *str;
{
if (!*str) {
strcpy(scratch,getenv("HOME"));
str=scratch;
}
if (*str=='~') {
sprintf(scratch,"%s%s",getenv("HOME"),str+1);
str=scratch;
}
if (chdir(str)) {
shell_print(sh,"Error trying to change directory");
switch(errno) {
case EACCES:
shell_print(sh,"Permission Denied.");
break;
case ENOENT:
shell_print(sh,"Path does not exist.");
break;
case ENOTDIR:
shell_print(sh,"Component not a directory.");
break;
}
}
pwd(sh,"");
}
/* handle file error if it happens */
void filio_error(sh)
shell *sh;
{
switch (errno) {
case ENOENT:
switch (error_type) {
case 0:
shell_print(sh,"No such file or directory.");
break;
case 1:
shell_print(sh,"No .tushrc file to run.");
break;
case 2:
shell_print(sh,"No .tush-sites file.");
break;
}
break;
case EACCES:
switch (error_type) {
case 0:
shell_print(sh,"Permission Denied.");
break;
case 1:
shell_print(sh,"Permission Denied, can't execute .tushrc");
break;
case 2:
shell_print(sh,"Permission Denied, can't read .tush-sites");
break;
}
case EEXIST:
shell_print(sh,"File already exists.");
break;
default:
sprintf(scratch,"%s Non-specific file error No. %d",SHELL_PRINT,errno);
print(sh,scratch);
break;
}
}
/* load a file */
file load_file(sh,filename)
shell *sh;
char *filename;
{
file f;
int d,e;
d=open(filename,O_RDONLY);
if (d<0) {
filio_error(sh);
f.where=0;
f.length=0;
return f;
}
f.length=lseek(d,0,2);
lseek(d,0,0);
f.where=(char *)malloc(f.length+1);
if (read(d,f.where,f.length)<0) {
filio_error(sh);
f.where=0;
f.length=0;
return f;
}
close(d);
return f;
}
/* routines for scripting */
void stop_script(sh,str)
shell *sh;
char *str;
{
if (sh->script_stream) {
sprintf(scratch,"%s Scripting stopped %s.\n",SHELL_PRINT,convert_time());
print(sh,scratch);
fclose(sh->script_stream);
sh->script_stream = 0;
}
else shell_print(sh,"Not scripting.");
}
void script(sh,str)
shell *sh;
char *str;
{
if (sh->script_stream) {
if (!fwrite(str,1,strlen(str),sh->script_stream)) {
fclose(sh->script_stream);
sh->script_stream = 0;
shell_print(sh,"Scripting stopped due to error.");
filio_error(sh);
}
}
}
void start_script(sh,str)
shell *sh;
char *str;
{
FILE *stream;
if (sh->script_stream) {
stop_script(sh,str);
return;
}
if (!*str) str=SCRIPT_FILE;
stream=fopen(str,"a");
if (!stream) {
filio_error(sh);
return;
}
fseek(stream,0,2);
sh->script_stream=stream;
sprintf(scratch,"%s Scripting started %s.\n",SHELL_PRINT,convert_time());
print(sh,scratch);
}
/* switches to a different shell */
void switch_shell(sh)
shell *sh;
{
current_shell=sh;
}
/* starts up a new shell, and switches to it */
shell *new_shell(str)
char *str;
{
shell *new,*empty;
new=(shell *)malloc(sizeof(shell));
no_of_shells++;
empty=first_shell;
if (!empty) first_shell=new;
else {
while(empty->next) empty=empty->next;
empty->next=new;
}
new->next=0;
new->key_buff=(char *)malloc(key_buff_len);
memset(new->key_buff,0,key_buff_len);
new->screen_buff=(char *)malloc(screen_buff_len);
memset(new->screen_buff,0,screen_buff_len);
new->key_history=(char *)malloc(key_history_len);
memset(new->key_history,0,key_history_len);
new->screen_history=(char *)malloc(screen_history_len);
memset(new->screen_history,0,screen_history_len);
new->flags=COMMAND|NO_UPDATE|WORD_WRAP|GA_PROMPT;
strncpy(new->prompt,STANDARD_PROMPT,PROMPT_LEN);
new->kb_pointer=new->key_buff;
new->sb_pointer=0;
new->kh_pointer=0;
new->kh_search=0;
new->sh_pointer=0;
new->script_stream = 0;
new->alias_list=0;
new->trigger_list=0;
new->max_word=10;
new->col=0;
switch_shell(new);
cls();
print(new,"TUsh - The Telnet User Shell\nv1.74 1992 - Simon Marsh\n\n");
sprintf(scratch,"%s/%s",getenv("HOME"),STARTUP_FILE);
error_type=1;
execute(new,scratch);
error_type=0;
command_processing(new,str);
delete_input(new);
new->flags &= ~NO_UPDATE;
return new;
}
/* shuts down a shell */
close_shell(s)
shell *s;
{
shell *p,*n;
n=s->next;
if (s!=first_shell) {
p=first_shell;
while(p->next!=s) p=p->next;
p->next=n;
}
else first_shell=n;
no_of_shells--;
if (s==current_shell) {
if (n) switch_shell(n);
else switch_shell(first_shell);
redraw_screen();
}
if (s->script_stream) stop_script(s,0);
if (s->flags&CONNECTED) close(s->sock_desc);
free((void *)s->key_buff);
free((void *)s->screen_buff);
free((void *)s->key_history);
free((void *)s->screen_history);
free((void *)s);
}
/* handle ^c */
void control_c()
{
current_shell->flags |= CTRL_C;
shell_print(current_shell,"^C Interrupt.");
}
/* broken pipes .. ignore */
void sigpipe() { return; }
/* start everything off */
void init_all()
{
int i;
char *scan;
#ifndef NO_FANCY_MALLOC
mallopt(M_MXFAST,MAX_SMALL_BLOCK);
#endif
scratch=malloc(SCRATCH_SIZE);
init_term();
signal(SIGPIPE,sigpipe);
signal(SIGWINCH,window_change);
signal(SIGCONT,restart);
signal(SIGINT,control_c);
sprintf(scratch,"%s/%s",getenv("HOME"),SITE_FILE);
error_type=2;
site_list=load_file(current_shell,scratch);
error_type=0;
scan=site_list.where;
for(i=0;i<site_list.length;i++,scan++)
if (isspace(*scan) && (*scan!='\n')) *scan=0;
new_shell("");
}
/* and restore it all afterwards */
restore_all()
{
while(first_shell)
close_shell(first_shell);
restore_term();
}
/* gotta have main() somewhere */
main(argc,argv)
int argc;
char *argv[];
{
int i;
fd_set set,*p;
char *compose,*arg;
shell *scan;
init_all();
compose=argv[0];
*compose++=toupper(*compose);
*compose=toupper(*compose);
p=&set;
for(i=1,compose=scratch;i<argc;i++) {
arg=argv[i];
while(*arg) *compose++ = *arg++;
*compose++=' ';
}
*compose=0;
strncpy(current_shell->key_buff,scratch,key_buff_len);
current_shell->flags |= NO_UPDATE;
newline();
while(!quit) {
FD_ZERO(p);
FD_SET(0,p);
for(scan=first_shell;scan;scan=scan->next) {
if (scan->flags&CONNECTED) FD_SET(scan->sock_desc,p);
}
if ((select(FD_SETSIZE,p,0,0,0) != -1)) {
for(scan=first_shell;scan;scan=scan->next) {
if ((scan->flags&CONNECTED) && (FD_ISSET(scan->sock_desc,p)))
read_in(scan);
}
if (FD_ISSET(0,p)) {
if (current_shell->flags&HALF_LINE) {
current_shell->flags&=~HALF_LINE;
current_shell->flags|=NO_UPDATE;
print(current_shell,"\n");
current_shell->flags&=~NO_UPDATE;
draw_input(current_shell);
}
handle_user();
}
}
if (wchange) restart();
}
restore_all();
printf("\n");
}