-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathterminal.c
4098 lines (3383 loc) · 100 KB
/
terminal.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
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
// terminal.c
// Created by Fred Nora.
//#include <ctype.h>
// #todo:
// We need to change the name of this document??
// #test:
// Testing ioctl()
#include <termios.h>
#include <fcntl.h>
#include <sys/ioctls.h>
#include <sys/ioctl.h>
#include <stdlib.h>
//#include <stdio.h>
//#include <unistd.h>
// #ps
// This thing is including a lot of libc headers too.
#include "include/terminal.h"
// Client-side library.
#include <gws.h>
#define IP(a, b, c, d) \
(a << 24 | b << 16 | c << 8 | d)
// The main structure.
// see: terminal.h
struct terminal_d Terminal;
FILE *__terminal_input_fp;
// Windows
struct gws_window_info_d *wi; // Window info for the main window.
// Private
static int main_window=0;
static int terminal_window=0;
// color
static unsigned int bg_color = COLOR_BLACK;
static unsigned int fg_color = COLOR_WHITE;
static unsigned int prompt_color = COLOR_GREEN;
// cursor
static int cursor_x=0;
static int cursor_y=0;
// Embedded shell
// We are using the embedded shell.
static int isUsingEmbeddedShell=TRUE;
// #todo: #maybe:
// Fazer estrutura para gerenciar a sequencia.
static int __sequence_status=0;
// ---------------------------------------
// CSI - Control Sequence Introducer.
// see: term0.h
char CSI_BUFFER[CSI_BUFFER_SIZE];
int __csi_buffer_tail=0;
int __csi_buffer_head=0;
// ---------------------------------------
// see: term0.h
//static CSIEscape csiescseq;
//static STREscape strescseq;
// ---------------------------------------
unsigned long __tmp_x=0;
unsigned long __tmp_y=0;
// ---------------------------------------
// see: term0.h
struct terminal_line LINES[32];
// Conterá ponteiros para estruturas de linha.
unsigned long lineList[LINE_COUNT_MAX];
// Conterá ponteiros para estruturas de linha.
unsigned long screenbufferList[8];
// ---------------------------------------
// see: term0.h
// Marcador do cursor.
unsigned long screen_buffer_pos=0; //(offset)
unsigned long screen_buffer_x=0; //current col
unsigned long screen_buffer_y=0; //current row
static unsigned long screen_buffer_saved_x=0;
static unsigned long screen_buffer_saved_y=0;
// ---------------------------------------
//
// System Metrics
//
int smScreenWidth=0; //1
int smScreenHeight=0; //2
unsigned long smCursorWidth=0; //3
unsigned long smCursorHeight=0; //4
unsigned long smMousePointerWidth=0; //5
unsigned long smMousePointerHeight=0; //6
unsigned long smCharWidth=0; //7
unsigned long smCharHeight=0; //8
//...
//
// Window limits
//
// Full screen support
unsigned long wlFullScreenLeft=0;
unsigned long wlFullScreenTop=0;
unsigned long wlFullScreenWidth=0;
unsigned long wlFullScreenHeight=0;
// Limite de tamanho da janela.
unsigned long wlMinWindowWidth=0;
unsigned long wlMinWindowHeight=0;
unsigned long wlMaxWindowWidth=0;
unsigned long wlMaxWindowHeight=0;
//
// Linhas
//
// Quantidade de linhas e colunas na área de cliente.
int wlMinColumns=0;
int wlMinRows=0;
int __wlMaxColumns=0;
int __wlMaxRows=0;
//
// ## Window size ##
//
unsigned long wsWindowWidth=0;
unsigned long wsWindowHeight=0;
//...
//
// ## Window position ##
//
unsigned long wpWindowLeft=0;
unsigned long wpWindowTop=0;
//..
//#importante:
//Linhas visíveis.
//número da linha
//isso será atualizado na hora do scroll.
int textTopRow=0; //Top nem sempre será '0'.
int textBottomRow=0;
int textSavedRow=0;
int textSavedCol=0;
int textWheelDelta=0; //delta para rolagem do texto.
int textMinWheelDelta=0; //mínimo que se pode rolar o texto
int textMaxWheelDelta=0; //máximo que se pode rolar o texto
//...
//
// Bg window
//
unsigned long __bgleft=0;
unsigned long __bgtop=0;
unsigned long __bgwidth=0;
unsigned long __bgheight=0;
unsigned long __barleft=0;
unsigned long __bartop=0;
unsigned long __barwidth=0;
unsigned long __barheight=0;
// Program name
static const char *program_name = "TERMINAL";
// Client window title
static const char *cw_string = "Client";
// see: font00.h
struct font_info_d FontInfo;
//
// == Private functions: Prototypes ==============
//
static void __initialize_basics(void);
static void __initializeTerminalComponents(void);
static void terminalInitWindowPosition(void);
static void terminalInitWindowSizes(void);
static void terminalInitWindowLimits(void);
static void terminalInitSystemMetrics(void);
static int
terminalProcedure (
int fd,
int window,
int msg,
unsigned long long1,
unsigned long long2 );
//
// Event loop
//
// System messages.
static void __get_system_event(int fd, int wid);
static void __get_ws_event(int fd, int event_wid);
static int __input_STDIN(int fd);
static int __input_from_connector(int fd);
static int embedded_shell_run(int fd);
static int terminal_run(int fd);
static void compareStrings(int fd);
static void doPrompt(int fd);
static void __on_return_key_pressed(int fd);
static void __try_execute(int fd);
static void doHelp(int fd);
static void doAbout(int fd);
static void __libc_test(int fd);
static void clear_terminal_client_window(int fd);
static void __send_to_child (void);
static void __test_winfo(int fd, int wid);
static void __test_ioctl(void);
static void __test_post_async_hello(void);
static void __winmax(int fd);
static void __winmin(int fd);
//#test
static void update_clients(int fd);
static void terminal_poweroff_machine(int fd);
//====================================================
// Shutdown machine via display server.
static void terminal_poweroff_machine(int fd)
{
// Parameter:
if (fd<0){
return;
}
cr();
lf();
tputstring(fd, "Poweroff machine via ds\n");
gws_destroy_window(fd,terminal_window);
gws_destroy_window(fd,main_window);
gws_shutdown(fd);
}
//#test
static void update_clients(int fd)
{
// Terminal window
int wid = Terminal.client_window_id;
// Local
struct gws_window_info_d lWi;
if (fd<0){
return;
}
// Get info about the main window.
// IN: fd, wid, window info structure.
gws_get_window_info(
fd,
main_window, // The app window.
(struct gws_window_info_d *) &lWi );
unsigned long l = 0;
unsigned long t = 0;
unsigned long w = lWi.cr_width;
unsigned long h = lWi.cr_height;
if (wid < 0)
return;
gws_change_window_position(
fd,
wid, // Terminal window
l,
t );
gws_resize_window(
fd,
wid, // Terminal window
w,
h );
// #todo:
// We need a list o clients. maybe clients[i]
gws_set_focus(fd,wid);
gws_redraw_window(fd, wid, TRUE);
// ------------------------------------------------
// Update some font info based on our new viewport.
// Update information in Terminal structure
// Font info again
// Based on our corrent viewport
// In chars.
// Terminal
if (Terminal.initialized != TRUE)
return;
Terminal.width = w;
Terminal.height = h;
// Font
// #todo: We need the font information in the window structure.
if (FontInfo.initialized != TRUE)
return;
if ( FontInfo.width > 0 &&
FontInfo.width < Terminal.width )
{
Terminal.width_in_chars =
(unsigned long)((Terminal.width/FontInfo.width) & 0xFFFF);
}
if ( FontInfo.height > 0 &&
FontInfo.height < Terminal.height )
{
Terminal.height_in_chars =
(unsigned long)((Terminal.height/FontInfo.height) & 0xFFFF);
}
}
static void __test_post_async_hello(void)
{
// Send async hello. 44888.
unsigned long message_buffer[32];
// The tid of init.bin is '0', i guess. :)
int InitProcessControlTID = 0;
// Response support.
//int __src_tid = -1;
//int __dst_tid = -1;
// The hello message
message_buffer[0] = 0; //window
message_buffer[1] = (unsigned long) 44888; // message code
message_buffer[2] = (unsigned long) 1234; // Signature
message_buffer[3] = (unsigned long) 5678; // Signature
message_buffer[4] = 0; // Receiver
message_buffer[5] = 0; // Sender
// ---------------------------------
// Post
// Add the message into the queue. In tail.
// IN: tid, message buffer address
rtl_post_system_message(
(int) InitProcessControlTID,
(unsigned long) message_buffer );
}
// Redraw and refresh the client window.
// Setup the cursor position.
// #todo: Maybe we need to get the window info again.
static void clear_terminal_client_window(int fd)
{
int wid = Terminal.client_window_id;
if (fd<0){
return;
}
// Redraw and refresh the window.
//gws_redraw_window( fd, wid, TRUE ); //Slower?
// Clear the window
// Repaint it using the default background color.
gws_clear_window(fd,wid); // Faster?
// Update cursor.
cursor_x = Terminal.left;
cursor_y = Terminal.top;
}
// Maximize application window.
// #bugbug: Covering the taskbar.
// #todo: Isso pode virar uma função na biblioteca.
// mas podemos deixar o window server fazer isso.
static void __winmax(int fd)
{
// #bugbug
// Esse tipo de torina nao eh atribuiçao do terminal.
// Talvez seja atribuiçao do display server.
// Talvez uma biblioteca client side tambem possa tratar disso.
// Talvez um wm client-side tambem possa tratar isso.
int wid = (int) Terminal.main_window_id;
int client_wid = (int) Terminal.client_window_id;
unsigned long w=rtl_get_system_metrics(1);
unsigned long h=rtl_get_system_metrics(2);
// #bugbug
// The server needs to respect the working area.
h = (h -40);
if(fd<0){
return;
}
// Change position, resize and redraw the window.
gws_change_window_position(fd,wid,0,0);
gws_resize_window(fd, wid, w, h );
gws_redraw_window(fd, wid, TRUE );
//---------------
// get the info for the main window.
// change the position of the terminal window.
// its because the client are also changed.
// Get window info:
// IN: fd, wid, window info structure.
gws_get_window_info(
fd,
wid,
(struct gws_window_info_d *) wi );
if (wi->used != TRUE){ return; }
if (wi->magic!=1234) { return; }
// Show info:
// Frame: l,t,w,h
//printf("Frame info: l=%d t=%d w=%d h=%d\n",
// wi->left, wi->top, wi->width, wi->height );
// Client rectangle: l,t,w,h
//printf("Client rectangle info: l=%d t=%d w=%d h=%d\n",
// wi->cr_left, wi->cr_top, wi->cr_width, wi->cr_height );
// The terminal window. (client area)
// Change position, resize and redraw the window.
gws_change_window_position(fd,client_wid,wi->cr_left,wi->cr_top);
gws_resize_window(fd, client_wid, wi->cr_width, wi->cr_height );
gws_redraw_window(fd, client_wid, TRUE );
}
// Minimize application window.
// #bugbug: Covering the taskbar.
// #todo: Isso pode virar uma função na biblioteca.
// mas podemos deixar o window server fazer isso.
static void __winmin(int fd)
{
// #bugbug
// Esse tipo de torina nao eh atribuiçao do terminal.
// Talvez seja atribuiçao do display server.
// Talvez uma biblioteca client side tambem possa tratar disso.
// Talvez um wm client-side tambem possa tratar isso.
int wid = (int) Terminal.main_window_id;
int client_wid = (int) Terminal.client_window_id;
// #bugbug
// Estamos chamando o kernel pra pegar informações sobre tela.
// Devemos considerar as dimensões da área de trabalho e
// não as dimensões da tela.
// #todo: Devemos fazer requests ao servidor para pegar essas informações.
// #todo: Criar requests para pegar os valores da área de trabalho.
unsigned long w=rtl_get_system_metrics(1);
unsigned long h=rtl_get_system_metrics(2);
// h=h-40;
// resize
//unsigned long w_width=100;
//unsigned long w_height=100;
//if(w>200){w_width=200;}
//if(h>100){w_height=100;}
unsigned long w_width = (w>>1);
unsigned long w_height = (h>>1);
if (fd<0){
return;
}
// Change position, resize and redraw the window.
gws_change_window_position(fd,wid,0,0);
gws_resize_window( fd, wid, w_width, w_height );
gws_redraw_window( fd, wid, TRUE );
//---------------
// get the info for the main window.
// change the position of the terminal window.
// its because the client are also changed.
// Get window info:
// IN: fd, wid, window info structure.
gws_get_window_info(
fd,
wid,
(struct gws_window_info_d *) wi );
if (wi->used != TRUE){ return; }
if (wi->magic!=1234) { return; }
// Show info:
// Frame: l,t,w,h
//printf("Frame info: l=%d t=%d w=%d h=%d\n",
// wi->left, wi->top, wi->width, wi->height );
// Client rectangle: l,t,w,h
//printf("Client rectangle info: l=%d t=%d w=%d h=%d\n",
// wi->cr_left, wi->cr_top, wi->cr_width, wi->cr_height );
// The terminal window. (client area)
// Change position, resize and redraw the window.
gws_change_window_position(fd,client_wid,wi->cr_left,wi->cr_top);
gws_resize_window(fd, client_wid, wi->cr_width, wi->cr_height );
gws_redraw_window(fd, client_wid, TRUE );
}
// local
// command "window"
// Testando serviços variados.
void __test_gws(int fd)
{
int Window = Terminal.main_window_id;
//int Window = Terminal.client_window_id;
if(fd<0){
return;
}
gws_change_window_position(fd,Window,0,0);
gws_resize_window(
fd, Window, 400, 400);
//gws_refresh_window(fd,Window); //#bugbug
//text
//gws_draw_text(fd,Window,0,0,COLOR_RED,"This is a string");
//redraw and refresh.
gws_redraw_window(
fd, Window, TRUE );
//redraw and not refresh.
//gws_redraw_window(
//fd, Window, FALSE );
//text
//gws_draw_text(fd,Window,0,0,COLOR_RED,"This is a string");
}
// Testing the 'foreground console' configuration.
// It's working.
static void __test_ioctl(void)
{
//printf ("~ioctl: Tests...\n");
// #test
// TIOCCONS - redirecting console output
// Changing the output console tty.
// A implementaçao que trata do fd=1 eh console_ioctl
// e nao tty_ioctl.
// https://man7.org/linux/man-pages/man2/TIOCCONS.2const.html
printf("\n");
printf("Changing the output console\n");
// IN: fd, request, arg.
int ioctl_return;
ioctl_return = (int) ioctl( STDOUT_FILENO, TIOCCONS, 0 );
printf("ioctl_return: {%d}\n",ioctl_return);
//printf("Done\n");
// Setup cursor position.
//ioctl(1, 1001, 10); // Cursor x
//ioctl(1, 1002, 10); // Cursor y
//ioctl(1, 1003, 2); //switch to the virtual console 2.
// Setup cursor position.
//ioctl( STDOUT_FILENO, 1001, 0 ); // Cursor x
//ioctl( STDOUT_FILENO, 1002, 0 ); // Cursor y
//printf("| Test: Cursor position at 0:0\n");
/*
// Indentation
ioctl(1, 1010, 8);
printf ("| Starting at column 8\n");
*/
//-----------
// 512 = right
// Get max col
int maxcol = ioctl( STDOUT_FILENO, 512, 0 );
//-----------
// Goto first line, position 0.
ioctl( STDOUT_FILENO, 1008, 0 );
printf("a"); fflush(stdout);
// Goto first line. last position
// Not the last column. If we hit the last,
// the console goes to the next line.
ioctl( STDOUT_FILENO, 1008, maxcol -2 ); //Set
printf("A"); fflush(stdout);
//-----------
// Goto last line, position 0.
ioctl( STDOUT_FILENO, 1009, 0 );
printf("z"); fflush(stdout);
// Goto last line. last position
// Not the last column. If we hit the last,
// the console goes to the next line.
ioctl( STDOUT_FILENO, 1009, maxcol -2 );
printf("Z"); fflush(stdout);
// Scroll forever.
//while(1){
// printf("%d\n",rtl_jiffies());
// ioctl(1,999,0); //scroll
//};
// Flush?
// It's not about flushing the ring3 buffer into the file.
//ioctl ( STDIN_FILENO, TCIFLUSH, 0 ); // input
//ioctl ( STDOUT_FILENO, TCIFLUSH, 0 ); // console
//ioctl ( STDERR_FILENO, TCIFLUSH, 0 ); // regular file
//ioctl ( 4, TCIFLUSH, 0 ); // invalid?
// Invalid limits
//ioctl ( -1, -1, 0 );
//ioctl ( 33, -1, 0 );
// Changing the color.
// #deprecated.
// The application will not change the colors anymore.
//ioctl(1, 1000,COLOR_CYAN);
//printf ("done\n");
}
// Comand 'w-main'.
static void __test_winfo(int fd, int wid)
{
struct gws_window_info_d *Info;
if(fd<0) { return; }
if(wid<0){ return; }
Info = (void*) malloc( sizeof(struct gws_window_info_d) );
if ((void*) Info == NULL){
return;
}
memset ( Info, 0, sizeof(struct gws_window_info_d) );
// Get window info:
// IN: fd, wid, window info structure.
gws_get_window_info(
fd,
wid,
(struct gws_window_info_d *) Info );
if (Info->used != TRUE){ return; }
if (Info->magic!=1234) { return; }
// Show info:
// Frame: l,t,w,h
printf("Frame info: l=%d t=%d w=%d h=%d\n",
Info->left, Info->top, Info->width, Info->height );
// Client rectangle: l,t,w,h
printf("Client rectangle info: l=%d t=%d w=%d h=%d\n",
Info->cr_left, Info->cr_top, Info->cr_width, Info->cr_height );
}
/*
static void __test_rand(void);
static void __test_rand(void)
{
int i, n;
time_t t;
n = 5;
//printf ("M=%d\n",rtl_get_system_metrics(118) ); //jiffies
//Intializes random number generator
//srand((unsigned) time(&t));
//Print 5 random numbers from 0 to 49
for( i = 0 ; i < n ; i++ ) {
printf("%d\n", rand() % 50);
}
return(0);
}
*/
static inline void do_int3(void)
{
asm ("int $3");
}
static inline void do_cli(void)
{
asm ("cli");
}
// Try to execute the command line in the prompt[].
static void __try_execute(int fd)
{
// Limits:
// + The prompt[] limit is BUFSIZ = 1024;
// + The limit for the write() operation is 512 for now.
size_t WriteLimit = 512;
if (fd<0){
return;
}
// Empty buffer
if (*prompt == 0){
goto fail;
}
// Clone.
// #important:
// For now the system will crash if the
// command is not found.
// #bugbug
// We are using the whole 'command line' as an argument.
// We need to work on that routine of passing
// the arguments to the child process.
// See: rtl.c
// Stop using the embedded shell.
// rebubina o arquivo de input.
//rewind(__terminal_input_fp);
// ==================================
//
// Send commandline via stdin.
//
// Write it into stdin.
// It's working
// See: crt0.c
//rewind(stdin);
//prompt[511]=0;
//write(fileno(stdin), prompt, 512);
//fail
//fprintf(stdin,"One Two Three ...");
//fflush(stdin);
/*
// it's working
char *shared_buffer = (char *) 0x30E00000; //extra heap 3.
sprintf(shared_buffer,"One Two Three ...");
shared_buffer[511] = 0;
*/
// ==================================
//
// Get filename
//
// #bugbug
// The command line accepts only one word
// and the command line has too many words.
//#todo
//Create a method.
//int rtl_get_first_word_in_a_string(char *buffer_pointer, char *string);
register int ii=0;
char filename_buffer[12]; //8+3+1
char *p;
// ---------------
// Grab the filename in the first word of the cmdline.
memset(filename_buffer,0,12);
p = prompt;
while (1)
{
// Se tem tamanho o suficiente ou sobra.
if (ii >= 12){
filename_buffer[ii] = 0; //finalize
break;
}
// Se o tamanho esta no limite.
// 0, space or tab.
// Nao pode haver espace no nome do programa.
// Depois do nome vem os parametros.
if ( *p == 0 ||
*p == ' ' ||
*p == '\t' )
{
// Finalize the buffer that contain the image name.
filename_buffer[ii] = 0;
break;
}
// Printable.
// Put the char into the buffer.
// What are these chars? It includes symbols? Or just letters?
if ( *p >= 0x20 && *p <= 0x7F )
{
filename_buffer[ii] = (char) *p;
}
p++; // next char in the command line.
ii++; // next byte into the filename buffer.
};
//
// Parse the filename inside its local buffer.
//
register int i=0;
// Is it a valid extension?
// Pois podemos executar sem extensão.
int isValidExt = FALSE;
int dotWasFound = FALSE;
// Look up for the first occorence of '.'.
// 12345678.123 = (8+1+3) = 12
for (i=0; i<=12; i++)
{
// The command name can't have these chars.
// It means that we reached the end of the command name.
// Maybe we have parameters after the name, maybe not.
if ( filename_buffer[i] == 0 ||
filename_buffer[i] == ' ' ||
filename_buffer[i] == '\t' )
{
break;
}
if ( filename_buffer[i] == '.' ){
dotWasFound = TRUE;
break;
}
};
// ----------------
// '.' was NOT found,
// but the filename is bigger than 8 bytes.
if (dotWasFound != TRUE)
{
if (i > 8){
printf("terminal: Long command name\n");
goto fail;
}
}
// ----------------
// '.' was found.
// Se temos um ponto e
// o que segue o ponto não é 'bin' ou 'BIN',
// entao a estencao e' invalida.
if (dotWasFound == TRUE)
{
if ( filename_buffer[i] != '.' )
goto fail;
// Ainda nao temos uma extensao valida.
// Encontramos um ponto,
// mas ainda não sabemos se a extensão é valida
// ou não.
// isValidExt = TRUE;
// Valida a extensao se os proximos chars forem "bin".
if ( filename_buffer[i+1] == 'b' &&
filename_buffer[i+2] == 'i' &&
filename_buffer[i+3] == 'n' )
{
isValidExt = TRUE;
}
// Valida a extensao se os proximos chars forem "BIN".
if ( filename_buffer[i+1] == 'B' &&
filename_buffer[i+2] == 'I' &&
filename_buffer[i+3] == 'N' )
{
isValidExt = TRUE;
}
// ...
}
// No extension
// The dot was found, but the extension is invalid.
// Invalid extension.
if (dotWasFound == TRUE)
{
if (isValidExt == FALSE){
printf("terminal: Invalid extension in command name\n");
goto fail;
}
}
//----------------------------------
//
// Clone and execute.
//
//#todo
// Tem que limpar o buffer do arquivo em ring0,
// antes de escrever no arquivo.
// cmdline:
// Only if the name is a valid name.
rewind(stdin);
//off_t v=-1;
//v=lseek( fileno(stdin), 0, SEEK_SET );
//if (v!=0){
// printf("testing lseek: %d\n",v);
// asm("int $3");
//}
// Finalize the command line.
// Nao pode ser maior que o buffer.
if (WriteLimit > PROMPT_MAX_DEFAULT){
WriteLimit = PROMPT_MAX_DEFAULT;
}
int __LastChar = (int) (WriteLimit-1);
prompt[__LastChar]=0;
// #debug
// OK!
//printf("promt: {%s}\n",prompt);
//asm ("int $3");
// #bugbug:
// A cmdline ja estava dentro do arquivo
// antes de escrevermos. Isso porque pegamos mensagens de
// teclado de dentro do sdtin.
// Tambem significa que rewind() não funcionou.
// #test
// Nao pode ser maior que o limite atual para operaçoes de escrita.
if (WriteLimit > 512){
WriteLimit = 512;
}
write(fileno(stdin), prompt, WriteLimit);
//rtl_clone_and_execute(filename_buffer);
//rtl_clone_and_execute(prompt);
//rtl_clone_and_execute("shutdown.bin");
// while(1){}
// #todo #test
// This is a method for the whole routine above.
// rtl_execute_cmdline(prompt);
// clone and execute via ws.
// four arguments and a string pointer.
int res = -1;
res =
(int) gws_clone_and_execute2(
fd,
0,0,0,0,
filename_buffer );
if (res<0){
//#debug #todo: do not use printf.
//printf("gws_clone_and_execute2: fail\n");
}
// #bugbug
// breakpoint
// something is wrong when we return here.
//printf("terminal: breakpoint\n");
//while(1){}
// #bugbug:
// Se não estamos usando então
// o terminal vai sair do loop de input e fechar o programa.
//isUsingEmbeddedShell = FALSE;
//return;
//printf("Command not found\n");
done:
return;
fail:
return;