-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuser.c
More file actions
1065 lines (869 loc) · 21.6 KB
/
user.c
File metadata and controls
1065 lines (869 loc) · 21.6 KB
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
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
** SCCS ID: @(#)user.c 1.1 4/17/15
**
** File: ?
**
** Author: CSCI-452 class of 20145
**
** Contributor: Ernesto Soltero
** Nick Jenis
** Max Roth
**
** Description: User routines.
*/
#include "common.h"
#include "ulib.h"
#include "user.h"
#include "c_io.h"
/*
** USER PROCESSES
**
** Each is designed to test some facility of the OS; see the user.h
** header file for a description of which system calls are used by
** each user process.
**
** Output from user processes is always alphabetic. Uppercase
** characters are "expected" output; lowercase are "erroneous"
** output.
**
** More specific information about each user process can be found in
** the header comment for that function (below).
**
** To spawn a specific user process, uncomment its SPAWN_x
** definition in the user.h header file.
*/
/*
** Prototypes for all one-letter user main routines (even
** ones that may not exist, for completeness)
*/
void user_a( void ); void user_b( void ); void user_c( void );
void user_d( void ); void user_e( void ); void user_f( void );
void user_g( void ); void user_h( void ); void user_i( void );
void user_j( void ); void user_k( void ); void user_l( void );
void user_m( void ); void user_n( void ); void user_o( void );
void user_p( void ); void user_q( void ); void user_r( void );
void user_s( void ); void user_t( void ); void user_u( void );
void user_v( void ); void user_w( void ); void user_x( void );
void user_y( void ); void user_z( void );
void helloCommand( void );
/*
** Users A, B, and C are identical, except for the character they
** print out via write( FD_SIO,). Each prints its ID, then loops 30
** times delaying and printing, before exiting. They also verify
** the status return from write( FD_SIO,).
*/
void user_a( void ) {
int i, j, ch;
write( FD_CONSOLE, "User A running\n", 0 );
ch = write( FD_SIO, "A", 1 );
if( ch != 1 ) {
write( FD_CONSOLE, "User A, write 1\n", 0 );
}
for( i = 0; i < 30; ++i ) {
for( j = 0; j < DELAY_STD; ++j )
continue;
ch = write( FD_SIO, "A", 1 );
if( ch != 1 ) {
write( FD_CONSOLE, "User A, write 2\n", 0 );
}
}
write( FD_CONSOLE, "User A exiting\n", 0 );
exit();
ch = write( FD_SIO, "a", 1 ); /* shouldn't happen! */
if( ch != 1 ) {
write( FD_CONSOLE, "User A, write 3\n", 0 );
}
}
void user_b( void ) {
int i, j, ch;
write( FD_CONSOLE, "User B running\n", 0 );
ch = write( FD_SIO, "B", 1 );
if( ch != 1 ) {
write( FD_CONSOLE, "User B, write 1\n", 0 );
}
for( i = 0; i < 30; ++i ) {
for( j = 0; j < DELAY_STD; ++j )
continue;
ch = write( FD_SIO, "B", 1 );
if( ch != 1 ) {
write( FD_CONSOLE, "User B, write 2\n", 0 );
}
}
write( FD_CONSOLE, "User B exiting\n", 0 );
exit();
ch = write( FD_SIO, "b", 1 ); /* shouldn't happen! */
if( ch != 1 ) {
write( FD_CONSOLE, "User B, write 3\n", 0 );
}
}
void user_c( void ) {
int i, j, ch;
write( FD_CONSOLE, "User C running\n", 0 );
ch = write( FD_SIO, "C", 1 );
if( ch != 1 ) {
write( FD_CONSOLE, "User C, write 1\n", 0 );
}
for( i = 0; i < 30; ++i ) {
for( j = 0; j < DELAY_STD; ++j )
continue;
ch = write( FD_SIO, "C", 1 );
if( ch != 1 ) {
write( FD_CONSOLE, "User C, write 2\n", 0 );
}
}
write( FD_CONSOLE, "User C exiting\n", 0 );
exit();
ch = write( FD_SIO, "c", 1 ); /* shouldn't happen! */
if( ch != 1 ) {
write( FD_CONSOLE, "User B, write 3\n", 0 );
}
}
/*
** User D spawns user Z.
*/
void user_d( void ) {
int pid;
write( FD_CONSOLE, "User D running\n", 0 );
write( FD_SIO, "D", 1 );
pid = spawn( user_z );
if( pid < 0 ) {
write( FD_CONSOLE, "User D spawn() failed\n", 0 );
}
write( FD_SIO, "D", 1 );
write( FD_CONSOLE, "User D exiting\n", 0 );
exit();
}
/*
** Users E, F, and G test the sleep facility.
**
** User E sleeps for 10 seconds at a time.
*/
void user_e( void ) {
int i;
char buf[16];
write( FD_CONSOLE, "User E (pid ", 0 );
i = itos10( buf, get_process_info( INFO_PID, 0 ) );
write( FD_CONSOLE, buf, i );
write( FD_CONSOLE, ") running\n", 0 );
write( FD_SIO, "E", 1 );
for( i = 0; i < 5 ; ++i ) {
sleep( SECONDS_TO_MS(10) );
write( FD_SIO, "E", 1 );
}
write( FD_CONSOLE, "User E exiting\n", 0 );
exit();
}
/*
** User F sleeps for 5 seconds at a time.
*/
void user_f( void ) {
int i;
char buf[16];
write( FD_CONSOLE, "User F (pid ", 0 );
i = itos10( buf, get_process_info( INFO_PID, 0 ) );
write( FD_CONSOLE, buf, i );
write( FD_CONSOLE, ") running\n", 0 );
write( FD_SIO, "F", 1 );
for( i = 0; i < 5 ; ++i ) {
sleep( SECONDS_TO_MS(5) );
write( FD_SIO, "F", 1 );
}
write( FD_CONSOLE, "User F exiting\n", 0 );
exit();
}
/*
** User G sleeps for 15 seconds at a time.
*/
void user_g( void ) {
int i;
char buf[16];
write( FD_CONSOLE, "User G (pid ", 0 );
i = itos10( buf, get_process_info( INFO_PID, 0 ) );
write( FD_CONSOLE, buf, i );
write( FD_CONSOLE, ") running\n", 0 );
write( FD_SIO, "G", 1 );
for( i = 0; i < 5; ++i ) {
sleep( SECONDS_TO_MS(15) );
write( FD_SIO, "G", 1 );
}
write( FD_CONSOLE, "User G exiting\n", 0 );
exit();
}
/*
** User H is like A-C except it only loops 5 times and doesn't
** call exit().
*/
void user_h( void ) {
int i, j;
write( FD_CONSOLE, "User H running\n", 0 );
write( FD_SIO, "H", 1 );
for( i = 0; i < 5; ++i ) {
for( j = 0; j < DELAY_STD; ++j )
continue;
write( FD_SIO, "H", 1 );
}
write( FD_CONSOLE, "User H returning without exiting!\n", 0 );
}
/*
** User J tries to spawn 2*N_PROCS copies of user_y.
*/
void user_j( void ) {
int i;
int pid;
char buf[16];
write( FD_CONSOLE, "User J running\n", 0 );
write( FD_SIO, "J", 1 );
for( i = 0; i < N_PROCS * 2 ; ++i ) {
pid = spawn( user_y );
if( pid < 0 ) {
write( FD_SIO, "j", 1 );
write( FD_CONSOLE, "User J spawn() #", 0 );
pid = itos10( buf, i );
write( FD_CONSOLE, buf, pid );
write( FD_CONSOLE, " failed\n", 0 );
} else {
write( FD_SIO, "J", 1 );
}
}
write( FD_CONSOLE, "User J exiting\n", 0 );
exit();
}
/*
** User K prints, goes into a loop which runs three times, and exits.
** In the loop, it does a spawn of user_x, sleeps 30 seconds, and prints.
*/
void user_k( void ) {
int i;
int pid;
char buf[16];
write( FD_CONSOLE, "User K running\n", 0 );
write( FD_SIO, "K", 1 );
for( i = 0; i < 3 ; ++i ) {
pid = spawn( user_x );
if( pid < 0 ) {
write( FD_CONSOLE, "User K spawn() #", 0 );
pid = itos10( buf, i );
write( FD_CONSOLE, buf, pid );
write( FD_CONSOLE, " failed\n", 0 );
} else {
sleep( SECONDS_TO_MS(30) );
write( FD_SIO, "K", 1 );
}
}
write( FD_CONSOLE, "User K exiting\n", 0 );
exit();
}
/*
** User L is like user K, except that it prints times and doesn't sleep
** each time, it just preempts itself.
*/
void user_l( void ) {
int i;
int pid;
uint32_t time;
char buf[16];
time = get_system_info( SYSINFO_TIME );
i = itos16( buf, time, 1 );
write( FD_CONSOLE, "User L running, initial time ", 0 );
write( FD_CONSOLE, buf, i );
write( FD_CONSOLE, "\n", 1 );
write( FD_SIO, "L", 1 );
for( i = 0; i < 3 ; ++i ) {
pid = spawn( user_x );
if( pid < 0 ) {
write( FD_CONSOLE, "User L spawn() #", 0 );
pid = itos10( buf, i );
write( FD_CONSOLE, buf, pid );
write( FD_CONSOLE, " failed\n", 0 );
} else {
sleep( 0 );
write( FD_SIO, "L", 1 );
}
}
time = get_system_info( SYSINFO_TIME );
i = itos16( buf, time, 1 );
write( FD_CONSOLE, "User L exiting at time ", 0 );
write( FD_CONSOLE, buf, i );
write( FD_CONSOLE, "\n", 1 );
exit();
}
/*
** User M is like A except that it runs at low priority.
*/
void user_m( void ) {
int i, j;
int prio;
char buf[16];
prio = get_process_info( INFO_PRIO, 0 );
i = itos10( buf, prio );
write( FD_CONSOLE, "User M running @ ", 0 );
write( FD_CONSOLE, buf, i );
write( FD_CONSOLE, "\n", 1 );
write( FD_SIO, "M", 1 );
for( i = 0; i < 30; ++i ) {
for( j = 0; j < DELAY_STD; ++j )
continue;
write( FD_SIO, "M", 1 );
}
write( FD_CONSOLE, "User M exiting\n", 0 );
exit();
}
/*
** User N is like user M, except that it runs at high priority.
*/
void user_n( void ) {
int i, j;
int prio;
char buf[16];
prio = get_process_info( INFO_PRIO, 0 );
i = itos10( buf, prio );
write( FD_CONSOLE, "User N running @ ", 0 );
write( FD_CONSOLE, buf, i );
write( FD_CONSOLE, "\n", 1 );
write( FD_SIO, "N", 1 );
for( i = 0; i < 30; ++i ) {
for( j = 0; j < DELAY_STD; ++j )
continue;
write( FD_SIO, "N", 1 );
}
write( FD_CONSOLE, "User N exiting\n", 0 );
exit();
}
/*
** User P runs forever, sleeping for 15 seconds at a time.
*/
void user_p( void ) {
write( FD_CONSOLE, "User P running\n", 0 );
for(;;){
sleep( SECONDS_TO_MS(15) );
write( FD_SIO, "P", 1 );
}
write( FD_CONSOLE, "User P exiting!?!?!\n", 0 );
exit();
}
/*
** User Q does a bogus system call
*/
void user_q( void ) {
write( FD_CONSOLE, "User Q running\n", 0 );
write( FD_SIO, "Q", 1 );
bogus();
write( FD_SIO, "q", 1 );
write( FD_CONSOLE, "User Q returned from bogus syscall!?!?!\n", 0 );
exit();
}
/*
** User R loops 3 times reading/writing, then exits.
*/
void user_r( void ) {
int ch;
char buf[16];
write( FD_CONSOLE, "User R running\n", 0 );
sleep( SECONDS_TO_MS(10) );
for( int i = 0; i < 3; ++i ) {
do {
write( FD_SIO, "R", 1 );
ch = read( FD_SIO, buf, 1 );
if( ch < 1 ) { /* wait a bit */
write( FD_SIO, "r", 1 );
sleep( SECONDS_TO_MS(1) );
}
} while( ch < 1 );
write( FD_SIO, buf, 1 );
}
write( FD_CONSOLE, "User R exiting\n", 0 );
exit();
}
// no user S, T, U, or V
/*
** User X prints X characters 20 times. It is spawned multiple
** times, and prints its PID and PPID when started and exiting.
*/
void user_x( void ) {
int i, j;
int pid, ppid;
char buf1[16], buf2[16];
pid = get_process_info( INFO_PID, 0 );
ppid = get_process_info( INFO_PPID, 0 );
i = itos10( buf1, pid );
j = itos10( buf2, ppid );
write( FD_CONSOLE, "User X running, PID ", 0 );
write( FD_CONSOLE, buf1, i );
write( FD_CONSOLE, " PPID ", 0 );
write( FD_CONSOLE, buf2, j );
write( FD_CONSOLE, "\n", 1 );
for( int k = 0; k < 20 ; ++k ) {
write( FD_SIO, "X", 1 );
for( int k2 = 0; k2 < DELAY_STD; ++k2 )
continue;
}
write( FD_CONSOLE, "User X exiting, PID ", 0 );
write( FD_CONSOLE, buf1, i );
write( FD_CONSOLE, " PPID ", 0 );
write( FD_CONSOLE, buf2, j );
write( FD_CONSOLE, "\n", 1 );
exit();
}
/*
** User Y prints Y characters 10 times.
*/
void user_y( void ) {
int i, j;
write( FD_CONSOLE, "User Y running\n", 0 );
for( i = 0; i < 10 ; ++i ) {
write( FD_SIO, "Y", 1 );
for( j = 0; j < DELAY_ALT; ++j )
continue;
sleep( SECONDS_TO_MS(1) );
}
write( FD_CONSOLE, "User Y exiting\n", 0 );
exit();
}
/*
** User Z prints Z characters 10 times.
*/
void user_z( void ) {
int n;
int pid, ppid;
char buf1[16], buf2[16];
pid = get_process_info( INFO_PID, 0 );
ppid = get_process_info( INFO_PPID, 0 );
n = itos10( buf1, pid );
pid = n;
n = itos10( buf2, ppid );
ppid = n;
write( FD_CONSOLE, "User Z running, PID ", 0 );
write( FD_CONSOLE, buf1, pid );
write( FD_CONSOLE, " PPID ", 0 );
write( FD_CONSOLE, buf2, ppid );
write( FD_CONSOLE, "\n", 1 );
for( int i = 0; i < 10 ; ++i ) {
write( FD_SIO, "Z", 1 );
for( int j = 0; j < DELAY_STD; ++j )
continue;
}
write( FD_CONSOLE, "User Z exiting, PID ", 0 );
write( FD_CONSOLE, buf1, pid );
write( FD_CONSOLE, " PPID ", 0 );
write( FD_CONSOLE, buf2, ppid );
write( FD_CONSOLE, "\n", 1 );
exit();
}
/*
** SYSTEM PROCESSES
*/
/*
** init - the initial user process
**
** starts the other top-level user processes
*/
void init( void ) {
// int16_t pid;
write( FD_CONSOLE, "Init started\n", 0 );
// write( FD_SIO, "$", 1 );
#ifdef SPAWN_A
pid = spawnp( user_a, PRIO_USER_STD );
if( pid < 0 ) {
write( FD_CONSOLE, "init, spawnp() user A failed\n", 0 );
exit();
}
#endif
#ifdef SPAWN_B
pid = spawnp( user_b, PRIO_USER_STD );
if( pid < 0 ) {
write( FD_CONSOLE, "init, spawnp() user B failed\n", 0 );
exit();
}
#endif
#ifdef SPAWN_C
pid = spawnp( user_c, PRIO_USER_STD );
if( pid < 0 ) {
write( FD_CONSOLE, "init, spawnp() user C failed\n", 0 );
exit();
}
#endif
#ifdef SPAWN_D
pid = spawnp( user_d, PRIO_USER_STD );
if( pid < 0 ) {
write( FD_CONSOLE, "init, spawnp() user D failed\n", 0 );
exit();
}
#endif
#ifdef SPAWN_E
pid = spawnp( user_e, PRIO_USER_STD );
if( pid < 0 ) {
write( FD_CONSOLE, "init, spawnp() user E failed\n", 0 );
exit();
}
#endif
#ifdef SPAWN_F
pid = spawnp( user_f, PRIO_USER_STD );
if( pid < 0 ) {
write( FD_CONSOLE, "init, spawnp() user F failed\n", 0 );
exit();
}
#endif
#ifdef SPAWN_G
pid = spawnp( user_g, PRIO_USER_STD );
if( pid < 0 ) {
write( FD_CONSOLE, "init, spawnp() user G failed\n", 0 );
exit();
}
#endif
#ifdef SPAWN_H
pid = spawnp( user_h, PRIO_USER_STD );
if( pid < 0 ) {
write( FD_CONSOLE, "init, spawnp() user H failed\n", 0 );
exit();
}
#endif
#ifdef SPAWN_J
pid = spawnp( user_j, PRIO_USER_STD );
if( pid < 0 ) {
write( FD_CONSOLE, "init, spawnp() user J failed\n", 0 );
exit();
}
#endif
#ifdef SPAWN_K
pid = spawnp( user_k, PRIO_USER_STD );
if( pid < 0 ) {
write( FD_CONSOLE, "init, spawnp() user K failed\n", 0 );
exit();
}
#endif
#ifdef SPAWN_L
pid = spawnp( user_l, PRIO_USER_STD );
if( pid < 0 ) {
write( FD_CONSOLE, "init, spawnp() user L failed\n", 0 );
exit();
}
#endif
#ifdef SPAWN_M
pid = spawnp( user_m, PRIO_USER_LOW );
if( pid < 0 ) {
write( FD_CONSOLE, "init, spawnp() user M failed\n", 0 );
exit();
}
#endif
#ifdef SPAWN_N
pid = spawnp( user_n, PRIO_USER_HIGH );
if( pid < 0 ) {
write( FD_CONSOLE, "init, spawnp() user N failed\n", 0 );
exit();
}
#endif
#ifdef SPAWN_P
pid = spawnp( user_p, PRIO_USER_STD );
if( pid < 0 ) {
write( FD_CONSOLE, "init, spawnp() user P failed\n", 0 );
exit();
}
#endif
#ifdef SPAWN_Q
pid = spawnp( user_q, PRIO_USER_STD );
if( pid < 0 ) {
write( FD_CONSOLE, "init, spawnp() user Q failed\n", 0 );
exit();
}
#endif
#ifdef SPAWN_R
pid = spawnp( user_r, PRIO_USER_STD );
if( pid < 0 ) {
write( FD_CONSOLE, "init, spawnp() user R failed\n", 0 );
exit();
}
#endif
#ifdef SPAWN_S
pid = spawnp( user_s, PRIO_USER_STD );
if( pid < 0 ) {
write( FD_CONSOLE, "init, spawnp() user S failed\n", 0 );
exit();
}
#endif
#ifdef SPAWN_T
pid = spawnp( user_t, PRIO_USER_STD );
if( pid < 0 ) {
write( FD_CONSOLE, "init, spawnp() user T failed\n", 0 );
exit();
}
#endif
#ifdef SPAWN_U
pid = spawnp( user_u, PRIO_USER_STD );
if( pid < 0 ) {
write( FD_CONSOLE, "init, spawnp() user U failed\n", 0 );
exit();
}
#endif
#ifdef SPAWN_V
pid = spawnp( user_v, PRIO_USER_STD );
if( pid < 0 ) {
write( FD_CONSOLE, "init, spawnp() user V failed\n", 0 );
exit();
}
#endif
// write( FD_SIO, "!", 1 );
exit();
}
/*
** idle - what to run when there's nothing else to run
*/
void idle( void ) {
/*
** Go into an infinite loop at low priority, printing dot characters.
*/
// write( FD_SIO, ".", 1 );
for(;;) {
for( int i = 0; i < DELAY_LONG; ++i )
continue;
// write( FD_SIO, ".", 1 );
}
/*
** SHOULD NEVER REACH HERE
*/
write( FD_CONSOLE, "+++ IDLE EXITING!?!?!\n", 0 );
exit();
}
/*
** hello - Process to output "Hello world!" to prove we can succesfully create
** new processes.
*/
void hello( void ) {
write( FD_SIO, "\nHello world!\n", 0 );
exit();
}
/*
** Simple hash to figure out what command is entered in shell
** No longer used
*/
int hashCommand(char* commandBuffer){
int hash = 0;
char* temp = commandBuffer;
while (*temp != '\0') {
hash += (int)*temp;
temp++;
//write( FD_CONSOLE, (char*)hash, 0);
}
return hash;
}
/*
** Helper function to get the length of a string or other null terminated
** buffer
**
** Retuns an int representing the length of the string.
*/
int len(char* buffer){
char* temp = buffer;
int len = 0;
while (*temp != '\0') {
len++;
temp++;
}
return len;
}
/*
** Helper function to compare two strings or other null terminated buffer
**
** Returns 0 if the buffers are equal
** Returns <0 if the first buffer is greater (maybe, will not return 0)
** Returns >0 if the second buffer is greater (maybe, will not return 0)
*/
int compare(char* buffer1, char* buffer2) {
int sizeDiff = len(buffer1) != len(buffer2);
if(sizeDiff != 0) {
return sizeDiff;
}
else {
char* temp1 = buffer1;
char* temp2 = buffer2;
while (*temp1 != '\0') {
if(*temp1 != *temp2) {
return (int) *temp1 - (int) *temp2;
}
temp1++;
temp2++;
}
return 0;
}
}
/*
** This is the main process that runs to allow user interaction
**
** The shell reads in a command with up to 2 space separated parameters.
** If the command is matched, then a new process is created if necessary.
** The shell loops until the OS shuts down.
*/
void shell( void ) {
char readBuffer[5];
char resultBuffer[2048];
char commandBuffer[20];
char paramBuffer[20];
char paramBuffer2[1024];
int resBufIndex = -1;
int gotCommand = 0; // 0 for no, 1 for yes
int comBufIndex = 0;
int pBufIndex = 0;
int pBufIndex2 = 0;
int usedSpace = 1;
while( 1 ) {
write( FD_SIO, "$ ", 0 );
while( !gotCommand ) {
int size = read( FD_SIO, readBuffer, 5 );
if( size != 0 ){
for( int i = 0; i < size; ++i ) {
// Only allow 1 space
if( readBuffer[i] == ' ' ) {
if( usedSpace ) continue;
else usedSpace = 1;
} else usedSpace = 0;
resBufIndex++;
write( FD_CONSOLE, &readBuffer[i], 1 );
write( FD_SIO, &readBuffer[i], 1 );
if(readBuffer[i] == '\n' ||
readBuffer[i] == '\r') {
gotCommand = 1;
break;
}
resultBuffer[resBufIndex] =
readBuffer[i];
}
}
}
// Skip the rest of the code here if the user entered nothing
if(resBufIndex == 0) {
gotCommand = 0;
resBufIndex = -1;
usedSpace = 1;
continue;
}
// Split up the command by spaces, up to 3 words
int doneCommand = 0;
int doneParam1 = 0;
int doneParam2 = 0;
int inTextBlock = 0;
for( int i = 0; i < resBufIndex; ++i ) {
if( resultBuffer[i] == '\'') {
if(inTextBlock) inTextBlock = 0;
else inTextBlock = 1;
continue;
}
if( resultBuffer[i] == ' ' && !inTextBlock) {
if( !doneCommand ) doneCommand = 1;
else if( !doneParam1 ) doneParam1 = 1;
else if( !doneParam2 ) doneParam2 = 1;
else write( FD_CONSOLE,
"Ignoring extra spaces!", 0 );
continue;
}
if (!doneCommand) {
commandBuffer[comBufIndex] = resultBuffer[i];
comBufIndex++;
}
else if( !doneParam1 ) {
paramBuffer[pBufIndex] = resultBuffer[i];
pBufIndex++;
}
else if( !doneParam2 ) {
paramBuffer2[pBufIndex2] = resultBuffer[i];
pBufIndex2++;
}
else {
write( FD_CONSOLE,
"Ignoring extra params!", 0 );
}
}
// Null terminate buffer.
commandBuffer[comBufIndex] = '\0';
paramBuffer[pBufIndex] = '\0';
paramBuffer2[pBufIndex2] = '\0';
// Print out the buffers on the console.
write( FD_CONSOLE, "CommandBuffer\n", 0);
write( FD_CONSOLE, commandBuffer, 0 );
write( FD_CONSOLE, "\nParamBuffer1\n", 0);
(pBufIndex > 0 ? write( FD_CONSOLE, paramBuffer, 0 ) :
write(FD_CONSOLE, "empty", 0));
write( FD_CONSOLE, "\nParamBuffer2\n", 0);
(pBufIndex2 > 0 ? write( FD_CONSOLE, paramBuffer2, 0 ) :
write(FD_CONSOLE, "empty", 0));
write(FD_CONSOLE, "\n", 0);
int16_t pid;
if(compare(commandBuffer, "ls") == 0) {
char* res = (char *) list_files();
write(FD_SIO, res, 0);
} else if( compare(commandBuffer, "hello") == 0) {
pid = spawnp(helloCommand, PRIO_USER_HIGH);
if( pid < 0) {
write( FD_CONSOLE,
"init, spawn() hello failed\n", 0);
exit();
}
} else if(compare(commandBuffer, "cd") == 0) {
write(FD_SIO, "Changing directory to: ", 0);
write(FD_SIO, paramBuffer, 0);
write(FD_SIO, "\n", 0);
int res = create_file(paramBuffer);
if(res == 0) write(FD_SIO, "Changed directory!\n", 0);
else write( FD_SIO, "Failed to change directory\n", 0);
} else if(compare(commandBuffer, "mkdir") == 0) {
} else if(compare(commandBuffer, "mkfile") == 0) {
write(FD_SIO, "Creating file: ", 0);
write(FD_SIO, paramBuffer, 0);
write(FD_SIO, "\n", 0);
int res = create_file(paramBuffer);
if(res == 0) write(FD_SIO, "Created file!\n", 0);
else write( FD_SIO, "Failed to create file\n", 0);
} else if(compare(commandBuffer, "rm") == 0) {
write(FD_SIO, "Deleting file: ", 0);
write(FD_SIO, paramBuffer, 0);
write(FD_SIO, "\n", 0);
int res = delete_file(paramBuffer);
if(res == 0) write(FD_SIO, "Deleted file!\n", 0);