-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.c
752 lines (713 loc) · 14.1 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
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
/* Main-level NOS program:
* initialization
* keyboard processing
* generic user commands
*
* Copyright 1986-1996 Phil Karn, KA9Q
*/
#include "top.h"
#include "config.h"
#include <time.h>
#include <ctype.h>
/* Including system stdio for rename() */
#include <stdio.h>
#if defined(__TURBOC__) && defined(MSDOS)
#include <io.h>
#include <conio.h>
#endif
#ifdef UNIX
#include <unistd.h>
#endif
#include "lib/std/stdio.h"
#include "global.h"
#include <stdarg.h>
#include "net/core/mbuf.h"
#include "core/timer.h"
#include "core/proc.h"
#include "net/core/iface.h"
#include "core/tty.h"
#include "core/session.h"
#include "hardware.h"
#include "core/usock.h"
#include "core/socket.h"
#include "lib/util/cmdparse.h"
#include "commands.h"
#include "core/daemon.h"
#include "core/devparam.h"
#include "net/dns/domain.h"
#include "files.h"
#include "main.h"
#include "core/trace.h"
#include "core/display.h"
#include "lib/std/errno.h"
#if !defined(MSDOS) && !defined(UNIX) /* PC and CURSES use F-10 key */
static char Escape = 0x1d; /* default escape character is ^] */
#endif
char Badhost[] = "Unknown host %s\n";
char *Hostname;
char Nospace[] = "No space!!\n"; /* Generic malloc fail message */
struct proc *Cmdpp;
struct proc *Display;
char *Cmdline; /* Copy of most recent command line */
int main_exit = FALSE; /* from main program (flag) */
static char Prompt[] = "net> ";
static kFILE *Logfp;
static time_t StartTime; /* time that NOS was started */
static int Verbose;
static int keychar(int c);
static void pass(char *,int len);
static void passchar(int c);
static void helpsub(struct cmds *cmds);
int
main(int argc,char *argv[])
{
kFILE *fp;
struct daemon *tp;
int c;
char cmdbuf[256];
long hinit = 102400;
StartTime = time(&StartTime);
while((c = kgetopt(argc,argv,"f:s:d:bvh:")) != kEOF){
switch(c){
case 'h': /* Heap initialization */
hinit = atol(koptarg);
break;
case 'f': /* Number of files */
Nfiles = atoi(koptarg);
break;
case 's': /* Number of sockets */
Nsock = atoi(koptarg);
break;
case 'd': /* Root directory for various files */
initroot(koptarg);
break;
#ifdef __TURBOC__
case 'b': /* Use BIOS for screen output */
directvideo = 0;
break;
#endif
case 'v':
Verbose = 1;
break;
}
}
kinit();
ioinit(hinit);
sockinit();
Cmdpp = mainproc("cmdintrp");
Sessions = (struct session **)callocw(Nsessions,sizeof(struct session *));
Command = Lastcurr = newsession("command interpreter",COMMAND,1);
Display = newproc("display",350,display,0,NULL,NULL,0);
kprintf("KA9Q NOS version %s (%s)\n",Version, KA9Q_VERSION_STRING);
#ifdef CPU386
kprintf("Compiled for 386/486 CPU\n");
#endif
kprintf("Copyright 1986-1996 by Phil Karn, KA9Q\n");
#if defined(MSDOS) || defined(CPU386)
if(Verbose){
kprintf("cs = %lx ds = %lx ss = %lx\n",_go32_my_cs(),_go32_my_ds(),
_go32_my_ss());
}
#endif
usercvt();
/* Start background Daemons */
for(tp=Daemons;;tp++){
if(tp->name == NULL)
break;
newproc(tp->name,tp->stksize,tp->fp,0,NULL,NULL,0);
}
Encap.txproc = newproc("encap tx",512,if_tx,0,&Encap,NULL,0);
if(koptind < argc){
/* Read startup file named on command line */
if((fp = kfopen(argv[koptind],READ_TEXT)) == NULL){
kprintf("Can't read config file %s",argv[koptind]);
kperror("");
}
} else {
fp = kfopen(Startup,READ_TEXT);
}
if(fp != NULL){
while(kfgets(cmdbuf,sizeof(cmdbuf),fp) != NULL){
rip(cmdbuf);
Cmdline = strdup(cmdbuf);
if(Verbose)
kprintf("%s\n",Cmdline);
if(cmdparse(Cmds,cmdbuf,NULL) != 0){
kprintf("input line: %s\n",Cmdline);
}
FREE(Cmdline);
}
kfclose(fp);
}
/* Now loop forever, processing commands */
for(;;){
kprintf(Prompt);
kfflush(kstdout);
if(kfgets(cmdbuf,sizeof(cmdbuf),kstdin) != NULL){
rip(cmdbuf);
FREE(Cmdline);
Cmdline = strdup(cmdbuf);
(void)cmdparse(Cmds,cmdbuf,Lastcurr);
}
}
}
/* Keyboard input process */
void
keyboard(
int i,
void *v1,
void *v2
){
int c;
int j;
/* Keyboard process loop */
loop:
c = kbread();
#if defined(MSDOS) || defined(UNIX)
if(c >= 256){
/* Pass all special characters to app upcall */
if(Current->ctlproc != NULL && (c = (*Current->ctlproc)(c)) == 0)
goto loop; /* Upcall took them */
c -= 256;
if(Current->scrollmode == SCROLL_INBAND){
/* In inband scroll mode, pass escape sequences
* for cursor control keys. Otherwise fall thru
*/
switch(c){
case CURSHOM:
pass("\033O\0",3);
goto loop;
case CURSUP:
pass("\033OA",3);
goto loop;
case PAGEUP:
pass("\033[5~",4);
goto loop;
case CURSEND:
pass("\033OU",3);
goto loop;
case CURSDWN:
pass("\033OB",3);
goto loop;
case PAGEDWN:
pass("\033[6~",4);
goto loop;
case CURSRIGHT:
pass("\033OC",3);
goto loop;
case CURSLEFT:
pass("\033OD",3);
goto loop;
}
}
/* In local scroll mode, we can get here with cursor
* control keys
*/
switch(c){
case CURSHOM:
dhome(Current->output->ptr);
break;
case CURSUP:
dcursup(Current->output->ptr);
break;
case PAGEUP:
dpgup(Current->output->ptr);
break;
case CURSEND:
dend(Current->output->ptr);
break;
case CURSDWN:
dcursdown(Current->output->ptr);
break;
case PAGEDWN:
dpgdown(Current->output->ptr);
break;
case F10: /* F-10 (go to command mode) */
if(Current != Command){
/* Save current tty mode and set cooked */
Lastcurr = Current;
Current = Command;
alert(Display,1);
}
break;
case F9: /* F-9 (resume last current session) */
if(Current == Command && Lastcurr != NULL){
Current = Lastcurr;
alert(Display,1);
}
break;
case F8: /* F-8 (next session) */
for(j = Current->index+1;j != Current->index;j++){
if(j >= Nsessions)
j = 0;
if(Sessions[j] != NULL){
Current = Sessions[j];
alert(Display,1);
break;
}
}
break;
case F7: /* F-7 (prev session) */
for(j = Current->index-1;j != Current->index;j--){
if(j == -1)
j = Nsessions-1;
if(Sessions[j] != NULL){
Current = Sessions[j];
alert(Display,1);
break;
}
}
break;
case F6: /* Toggle scroll mode */
if(Current == NULL)
break;
Current->scrollmode =
Current->scrollmode == SCROLL_INBAND ?
SCROLL_LOCAL : SCROLL_INBAND;
dscrollmode(Current->output->ptr,Current->scrollmode);
break;
case F5: /* Kick current session */
if(Current != NULL)
dokick(0,NULL,Current);
break;
#ifdef MSDOS
case AF1:
case AF2:
case AF3:
case AF4:
case AF5:
case AF6:
case AF7:
case AF8:
case AF9:
case AF10: /* Alt-F1 thru Alt-F10 */
c -= AF1;
if(c < Nsessions && Sessions[c] != NULL){
Current = Sessions[c];
alert(Display,1);
}
break;
case AF11: /* Alt-F11 or Alt-F12 */
case AF12:
c -= (AF11 - 11);
if(c < Nsessions && Sessions[c] != NULL){
Current = Sessions[c];
alert(Display,1);
}
break;
#endif
default: /* else ignore */
break;
}
goto loop;
}
#else
if(c == Escape && Escape != 0 && Current != Command){
/* Save current tty mode and set cooked */
Lastcurr = Current;
Current = Command;
alert(Display,1);
goto loop;
}
#endif
passchar(c);
goto loop;
}
static void
pass(char *s,int len)
{
while(len-- != 0)
passchar(*s++);
}
static void
passchar(int c)
{
int cnt;
/* If a normal-character upcall exists, give it the character.
* if the upcall returns 0, don't pass it to the regular tty editor
*/
if(Current->inproc != NULL && (*Current->inproc)(c) == 0)
return;
/* Ordinary ASCII character, hand to tty editor */
if((cnt = ttydriv(Current,(char)c)) != 0){
/* Input ready to hand to process */
kfwrite(Current->ttystate.line,1,cnt,Current->input);
kfflush(Current->input);
}
}
/* Standard commands called from main */
int
dorepeat(
int argc,
char *argv[],
void *p
){
int32 interval;
int ret;
struct session *sp;
if(isdigit(argv[1][0])){
interval = atol(argv[1]);
argc--;
argv++;
} else {
interval = MSPTICK;
}
if((sp = newsession(Cmdline,REPEAT,1)) == NULL){
kprintf("Too many sessions\n");
return 1;
}
sp->inproc = keychar; /* Intercept ^C */
/* Set enough buffering to handle an entire screen so it'll get
* displayed in one quick update when we flush
*/
ksetvbuf(sp->output,NULL,_kIOFBF,2048);
while(sp->inproc == keychar){ /* ^C will clear sp->inproc */
kprintf("%c[2J",ESC); /* Clear screen */
ret = subcmd(Cmds,argc,argv,p);
kfflush(sp->output);
if(ret != 0 || ppause(interval) == -1)
break;
}
keywait(NULL,1);
freesession(&sp);
return 0;
}
static int
keychar(int c)
{
if(c != CTLC)
return 1; /* Ignore all but ^C */
kfprintf(Current->output,"^C\n");
alert(Current->proc,kEABORT);
Current->inproc = NULL;
return 0;
}
int
dodelete(
int argc,
char *argv[],
void *p
){
int i;
for(i=1;i < argc; i++){
if(unlink(argv[i]) == -1){
kprintf("Can't delete %s",argv[i]);
kperror("");
}
}
return 0;
}
int
dorename(
int argc,
char *argv[],
void *p
){
if(rename(argv[1],argv[2]) == -1){
kprintf("Can't rename %s",argv[1]);
kperror("");
}
return 0;
}
int
doexit(
int argc,
char *argv[],
void *p
){
int i;
time_t StopTime;
struct session *sp;
StopTime = time(&StopTime);
main_exit = TRUE; /* let everyone know we're out of here */
/* Alert each session task that we're aborting */
for(i=0;i<Nsessions;i++){
if((sp = Sessions[i]) == NULL)
continue;
alert(sp->proc,kEABORT);
alert(sp->proc1,kEABORT);
alert(sp->proc2,kEABORT);
}
reset_all();
if(Dfile_updater != NULL)
alert(Dfile_updater,0); /* don't wait for timeout */
for(i=0;i<100;i++)
kwait(NULL); /* Allow tasks to complete */
shuttrace();
logmsg(-1,"NOS was stopped at %s", ctime(&StopTime));
if(Logfp){
kfclose(Logfp);
Logfp = NULL;
}
#if defined(__TURBOC__)
clrscr();
#endif
iostop();
exit(0);
return 0; /* To satisfy lint */
}
int
doreboot(
int argc,
char *argv[],
void *p
){
time_t StopTime;
StopTime = time(&StopTime);
logmsg(-1,"NOS reboot at %s",ctime(&StopTime));
ppause(1000L);
iostop();
sysreset(); /* no return */
return 0; /* to satisfy lint */
}
int
dohostname(
int argc,
char *argv[],
void *p
){
if(argc < 2)
kprintf("%s\n",Hostname);
else {
struct iface *ifp;
char *name;
if((ifp = if_lookup(argv[1])) != NULL){
if((name = resolve_a(ifp->addr, FALSE)) == NULL){
kprintf("Interface address not resolved\n");
return 1;
} else {
FREE(Hostname);
Hostname = name;
/* remove trailing dot */
if ( Hostname[strlen(Hostname)] == '.' ) {
Hostname[strlen(Hostname)] = '\0';
}
kprintf("Hostname set to %s\n", name );
}
} else {
FREE(Hostname);
Hostname = strdup(argv[1]);
}
}
return 0;
}
int
dolog(
int argc,
char *argv[],
void *p
){
static char *logname;
if(argc < 2){
if(Logfp)
kprintf("Logging to %s\n",logname);
else
kprintf("Logging off\n");
return 0;
}
if(Logfp){
logmsg(-1,"NOS log closed");
kfclose(Logfp);
Logfp = NULL;
FREE(logname);
}
if(strcmp(argv[1],"stop") != 0){
logname = strdup(argv[1]);
Logfp = kfopen(logname,APPEND_TEXT);
logmsg(-1,"NOS was started at %s", ctime(&StartTime));
}
return 0;
}
int
dohelp(
int argc,
char *argv[],
void *p
){
helpsub(Cmds);
return 0;
}
int
dorhelp(
int argc,
char *argv[],
void *p
){
helpsub(Remcmds);
return 0;
}
static void
helpsub(struct cmds *cmds)
{
struct cmds *cmdp;
int i;
char buf[66];
kprintf("Main commands:\n");
memset(buf,' ',sizeof(buf));
buf[64] = '\n';
buf[65] = '\0';
for(i=0,cmdp = cmds;cmdp->name != NULL;cmdp++,i = (i+1)%4){
strncpy(&buf[i*16],cmdp->name,strlen(cmdp->name));
if(i == 3){
kprintf(buf);
memset(buf,' ',sizeof(buf));
buf[64] = '\n';
buf[65] = '\0';
}
}
if(i != 0)
kprintf(buf);
}
/* Attach an interface
* Syntax: attach <hw type> <I/O address> <vector> <mode> <label> <bufsize> [<speed>]
*/
int
doattach(
int argc,
char *argv[],
void *p
){
return subcmd(Attab,argc,argv,p);
}
/* Manipulate I/O device parameters */
int
doparam(
int argc,
char *argv[],
void *p
){
struct iface *ifp;
int param;
int32 val;
if((ifp = if_lookup(argv[1])) == NULL){
kprintf("Interface \"%s\" unknown\n",argv[1]);
return 1;
}
if(ifp->ioctl == NULL){
kprintf("Not supported\n");
return 1;
}
if(argc < 3){
for(param=1;param<=16;param++){
val = (*ifp->ioctl)(ifp,param,FALSE,0L);
if(val != -1)
kprintf("%s: %ld\n",parmname(param),val);
}
return 0;
}
if((param = devparam(argv[2])) == -1){
kprintf("Unknown parameter %s\n",argv[2]);
return 1;
}
if(argc < 4){
/* Read specific parameter */
val = (*ifp->ioctl)(ifp,param,FALSE,0L);
if(val == -1){
kprintf("Parameter %s not supported\n",argv[2]);
} else {
kprintf("%s: %ld\n",parmname(param),val);
}
return 0;
}
/* Set parameter */
(*ifp->ioctl)(ifp,param,TRUE,atol(argv[3]));
return 0;
}
#if !defined(MSDOS) && !defined(UNIX)
int
doescape(
int argc,
char *argv[],
void *p
){
if(argc < 2)
kprintf("0x%x\n",Escape);
else
Escape = *argv[1];
return 0;
}
#endif /* MSDOS */
#if !defined(UNIX)
/* Execute a command with output piped to more */
int
dopage(
int argc,
char *argv[],
void *p
){
kFILE *fp;
kFILE *outsav;
fp = ktmpfile();
outsav = kstdout;
kstdout = fp;
subcmd(Cmds,argc,argv,p);
kstdout = outsav;
newproc("view",512,view,0,(void *)fp,NULL,0); /* View closes fp */
return 0;
}
#endif
/* Set kernel process debug flag */
int
dodebug(
int argc,
char *argv[],
void *p
){
setbool(&Kdebug,"kernel debug",argc,argv);
return 0;
}
/* Set temp file wipe-on-close flag */
int
dowipe(
int argc,
char *argv[],
void *p
){
setbool(&_clrtmp,"tmp file wiping",argc,argv);
return 0;
}
/* No-op command */
int
donothing(
int argc,
char *argv[],
void *p
){
return 0;
}
/* Log messages of the form
* Tue Jan 31 00:00:00 1987 44.64.0.7:1003 open FTP
*/
void
logmsg(int s,char *fmt, ...)
{
va_list ap;
char *cp;
time_t t;
int i;
struct ksockaddr fsocket;
#ifdef MSDOS
int fd;
#endif
if(Logfp == NULL)
return;
time(&t);
cp = ctime(&t);
rip(cp);
i = SOCKSIZE;
kfprintf(Logfp,"%s",cp);
if(kgetpeername(s,&fsocket,&i) != -1)
kfprintf(Logfp," %s",psocket(&fsocket));
kfprintf(Logfp," - ");
va_start(ap,fmt);
kvfprintf(Logfp,fmt,ap);
va_end(ap);
kfprintf(Logfp,"\n");
kfflush(Logfp);
#ifdef MSDOS
/* MS-DOS doesn't really flush files until they're closed */
fd = kfileno(Logfp);
if((fd = dup(fd)) != -1)
kclose(fd);
#endif
}