-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkernel.cpp
1513 lines (1330 loc) · 27.7 KB
/
kernel.cpp
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
__asm("jmp KernelStart");
typedef unsigned char boolean;
typedef unsigned char u8;
typedef unsigned short u16;
typedef unsigned int u32;
typedef signed char i8;
typedef signed short i16;
typedef signed int i32;
typedef unsigned long size_t;
typedef signed int errno_t;
#define OSMODE ((boolean *)0xbf1c)
#define OSMODE_STD 0
#define OSMODE_BM 1
#define VIDEO_BUF_PTR ((u8 *)0x000b8000)
#define VIDEO_BUF_PTR_END ((u8 *)0x000c7fff)
#define VIDEO_BUF_LINEMAX ((size_t)80)
#define VIDEO_BUF_MAX ((size_t)260)
#define VIDEO_COL_DEF 0x07
#define VIDEO_POS_CHR(X, Y) (*(VIDEO_BUF_PTR + (Y) * VIDEO_BUF_LINEMAX * 2 + (X) * 2))
#define VIDEO_POS_COL(X, Y) (*(VIDEO_BUF_PTR + (Y) * VIDEO_BUF_LINEMAX * 2 + (X) * 2 + 1))
#define VIDEO_SET(CHR, COL, X, Y) \
{ \
VIDEO_POS_CHR(X, Y) = CHR; \
VIDEO_POS_COL(X, Y) = COL; \
}
#define CURSOR_PORT (0x3D4)
#define KSCAN_OFFSET (0x80)
#define KSCAN_SPACE (0x29)
#define KSCAN_RETURN (0x1C)
#define KSCAN_SHIFT (0x2A)
#define KSCAN_CTRL (0x1D)
#define KSCAN_ALT (0x38)
#define KSCAN_BACK (0x0E)
#define TERMINAL_STDIN_SZ ((size_t)0xFF)
#define TERMINAL_STDOUT_SZ ((size_t)0x7FF)
#define TERMINAL_OUT_SZ (TERMINAL_STDIN_SZ + TERMINAL_STDOUT_SZ)
#define TERMINAL_MAX_LINES ((size_t)25)
#define BUFSIZE ((size_t)0xFF)
#define KSHARE_COUNT_MAX ((size_t)0x10)
#define KSHARE_ALLOC_MAX ((size_t)0x1FF)
#define KSHARE_NAMEMAX ((size_t)0x20)
#define PROGRAM_MAX ((size_t)0xFF) // Maximum loadable number of programs
#define PROGRAM_NAMEMAX ((size_t)32)
#define PROGRAM_ARGMAX ((size_t)4)
#define SUCCESS 0
#define FAIL 1
#define ERR_NULL_POINTER 2
#define ERR_TERMINAL_BUFFER_CRASHED 3
#define ERR_TERMINAL_INPUT_IS_EMPTY 4
#define FALSE 0
#define TRUE 1
#define NULL (0UL)
#define IS_OK(VAR, EXIT) \
if ((VAR) != SUCCESS) \
{ \
goto EXIT; \
}
#define IS_NULL(VAR, EXIT) \
if ((VAR) == NULL) \
{ \
goto EXIT; \
}
#define IDT_TYPE_INTR (0x0E)
#define IDT_TYPE_TRAP (0x0F)
#define GDT_CS (0x8)
#define PIC1_PORT (0x20)
//
// Structures
//
struct _IdtEntry
{
unsigned short BaseLo;
unsigned short SegmSel;
unsigned char AlwaysZero;
unsigned char Flags;
unsigned short BaseHi;
} __attribute__((packed));
typedef struct _IdtEntry IdtEntry;
struct _IdtPtr
{
unsigned short Limit;
unsigned int Base;
} __attribute__((packed));
typedef struct _IdtPtr IdtPtr;
IdtEntry g_idt[256];
IdtPtr g_idt_ptr;
typedef void (*IntrHandler)();
typedef struct _Cursor
{
size_t X;
size_t Y;
boolean IsInput;
} Cursor;
typedef struct _KerShare
{
struct {
char Name[KSHARE_NAMEMAX];
u8 *Ptr;
u8 Size;
} Table[KSHARE_COUNT_MAX];
u8 Memory[KSHARE_ALLOC_MAX];
u8 *Next;
u8 *End;
u16 Count;
} KerShare;
typedef struct _MsgProg
{
u16 Count;
char **Args;
} MsgProg;
typedef struct _SingleProg
{
char Name[PROGRAM_NAMEMAX];
int (*Main)(MsgProg *);
} SingleProg;
typedef struct _ProgramBox
{
u16 Count;
SingleProg Program[PROGRAM_MAX];
} ProgramBox;
static const char alphabet[] = {
' ', '!', '"', '#', '$', '%', '&', '\'',
'(', ')', '*', '+', ',', '-', '.', '/',
'0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', ':', ';', '<', '=', '>', '?',
'@', 'A', 'B', 'C', 'D', 'E', 'F', 'G',
'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O',
'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W',
'X', 'Y', 'Z', '[', '\\', ']', '^', '_',
'`', 'a', 'b', 'c', 'd', 'e', 'f', 'g',
'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
'p', 'q', 'r', 's', 't', 'u', 'v', 'w',
'x', 'y', 'z', '{', '|', '}', '~'
};
//
// Prototypes
//
i32 idiv(i32 a, i32 b);
u32 udiv(u32 a, u32 b);
u32 mod(u32 a, u32 b);
char *itoa(size_t num, char *p_str, size_t base);
void TerminalClear(void);
void TerminalApplyBackspace(void);
errno_t TerminalGetChar(const char c);
errno_t TerminalPutChar(const char c);
void TerminalDelLastChar();
errno_t TerminalPrint(const char *p_str);
void TerminalKeyHandle(Cursor *p_cur, const char c);
Cursor *TerminalCursor(Cursor *p_cur);
void TerminalCursorSetPos(size_t x, size_t y);
void TerminalInput(char *p_data, size_t *p_dataSz);
void TerminalEnter();
void TerminalOpen();
boolean TerminalReturn();
void TerminalClose();
boolean TerminalIsOpen();
void TerminalFlush();
errno_t StrCatA(char *p_dst, size_t dstSz, const char *p_src);
errno_t StrCpyA(char *p_dst, size_t dstSz, const char *p_src);
i8 StrCmpA(char *p_str, char *p_cmp);
i8 StrnCmpA(char *p_str, char *p_cmp);
size_t StrLenA(const char *p_str);
const char *StrChrA(const char *p_str, const char c);
char *StrChrA(char *p_str, const char c);
const char *StrStrA(const char *p_str, const char *p_sub);
char *StrTokA(char *p_str, const char delim);
char *StrTokA(char *p_str, const char *p_sub);
char *IntToStrA(size_t val);
extern inline boolean IsUpper(const char c);
extern inline boolean IsLower(const char c);
char ToUpper(const char c);
char ToLower(const char c);
void ZeroMemory(void *p_buf, size_t bufSz);
void CopyMemory(void *p_buf, void *p_data, size_t dataSz);
char GetKeyChar(u8 code);
char *Stdin(char p_newBuf[TERMINAL_STDIN_SZ]);
char *Stdout(char p_newBuf[TERMINAL_STDOUT_SZ]);
void DefaultIntrHandler();
void IntrRegHandler(i32 num, u16 segmSel, u16 flags, IntrHandler hndlr);
void IntrStart();
void IntrEnable();
void IntrDisable();
extern inline char inb(u16 port);
extern inline void outb(u16 port, char data);
extern inline void outw(unsigned short port, unsigned int data);
void KeyboardHandler();
void KeyboardKey();
void KeyHandler(u8 code);
extern inline boolean GetOsMode();
void InitTerminal();
void InitIntr();
void InitKeyboard();
void InitShare();
void InitProgBox();
KerShare *KernelShare(KerShare *p_ks);
u8 *KernelNewShare(const char *p_name, size_t blockSz);
u8 *KernelGetShare(const char *p_name, size_t *p_size);
errno_t ProgStart(const char *p_progName, MsgProg *p_arg, int *p_result);
boolean ProgExists(const char *p_progName, u16 *p_progId);
errno_t ProgAdd(const char *p_name, int (*main)(MsgProg *));
ProgramBox *ProgBox(ProgramBox *p_progBox);
void BoyerMooreBuildShift(const char *p_sub, size_t p_shift[sizeof(alphabet)]);
size_t BoyerMoore(const char *p_str, const char *p_sub);
//
// Program staff
//
void PrintFmt(const char *p_format, ...);
//
// Programs
//
static int StringOs_Help(MsgProg *p_msg);
static int StringOs_Info(MsgProg *p_msg);
static int StringOs_Upcase(MsgProg *p_msg);
static int StringOs_Downcase(MsgProg *p_msg);
static int StringOs_Titlize(MsgProg *p_msg);
static int StringOs_Template(MsgProg *p_msg);
static int StringOs_Search(MsgProg *p_msg);
static int StringOs_Shutdown(MsgProg *p_msg);
//
// Definitions
//
extern "C" int KernelStart()
{
InitIntr();
InitKeyboard();
InitTerminal();
IntrStart();
IntrEnable();
InitShare();
InitProgBox();
TerminalPrint("Welcome to StringOS!\n");
TerminalFlush();
while (TRUE)
{
TerminalEnter();
}
return SUCCESS;
}
i32 idiv(i32 a, i32 b)
{
i32 res;
a = (a > 0) ? a : -a;
b = (b > 0) ? b : -b;
for (res = 0; a > 0; res++)
{
a -= b;
}
return res;
}
u32 udiv(u32 a, u32 b)
{
u32 res;
for (res = 0; a > 0; res++)
{
a -= b;
}
return res;
}
u32 mod(u32 a, u32 b)
{
for (;;)
{
if (a <= b)
{
return a;
}
a -= b;
}
}
char *itoa(size_t num, char *p_str, size_t base)
{
size_t i = 0;
bool isNegative = false;
if (num == 0)
{
p_str[i++] = '0';
p_str[i] = '\0';
return p_str;
}
if (num < 0 && base == 10)
{
isNegative = true;
num = -num;
}
while (num != 0)
{
size_t rem = num % base;
p_str[i++] = (rem > 9) ? (rem - 10) + 'a' : rem + '0';
num = num / base;
}
if (isNegative)
p_str[i++] = '-';
p_str[i] = '\0';
size_t start = 0;
size_t end = i - 1;
char buf;
while (start < end)
{
buf = *(p_str + start);
*(p_str + start) = *(p_str + end);
*(p_str + end) = buf;
start++;
end--;
}
return p_str;
}
void TerminalClear(void)
{
u8 *p_video_buf = VIDEO_BUF_PTR;
while (p_video_buf < VIDEO_BUF_PTR_END)
{
*p_video_buf = 0;
p_video_buf++;
}
}
void TerminalApplyBackspace(void)
{
char *p_stdin = Stdin(NULL);
char *p_stdout = Stdout(NULL);
size_t stdin_sz = StrLenA(p_stdin);
size_t stdout_sz = StrLenA(p_stdout);
for (size_t i = 0; i < stdin_sz; i++)
{
if (p_stdin[i] == '\b')
{
if (i > 0)
{
p_stdin[i] = 0;
p_stdin[i - 1] = 0;
}
else
{
p_stdin[i] = 0;
}
}
}
for (size_t i = 0; i < stdout_sz; i++)
{
if (p_stdout[i] == '\b')
{
if (i > 0)
{
p_stdout[i] = 0;
p_stdout[i - 1] = 0;
}
else
{
p_stdout[i] = 0;
}
}
}
}
errno_t TerminalGetChar(const char c)
{
char *p_stdin = Stdin(NULL);
size_t stdout_sz = StrLenA(p_stdin);
if (stdout_sz >= 40 && c != '\b' && c != '\n')
{
return FAIL;
}
char p_buf[2] = { c, '\0' };
return StrCatA(p_stdin, TERMINAL_STDIN_SZ, p_buf);
}
errno_t TerminalPutChar(const char c)
{
char *p_stdout = Stdout(NULL);
char p_buf[2] = { c, '\0' };
return StrCatA(p_stdout, TERMINAL_STDOUT_SZ, p_buf);
}
void TerminalDelLastChar()
{
char *p_stdin = Stdin(NULL);
size_t stdin_sz = StrLenA(p_stdin);
if (stdin_sz > 0)
{
p_stdin[stdin_sz] = 0;
}
}
errno_t TerminalPrint(const char *p_str)
{
char *p_stdout = Stdout(NULL);
return StrCatA(p_stdout, TERMINAL_STDOUT_SZ, p_str);
}
void TerminalKeyHandle(Cursor *p_cur, const char c)
{
static boolean is_input = FALSE;
if (p_cur != NULL)
{
is_input = p_cur->IsInput;
}
if (is_input && c != '\0')
{
if (c == '`')
{
TerminalClear();
ZeroMemory(Stdout(NULL), StrLenA(Stdout(NULL)));
TerminalPrint("(> ");
TerminalFlush();
}
else
{
TerminalGetChar(c);
}
}
}
Cursor *TerminalCursor(Cursor *p_cur)
{
static Cursor *p = NULL;
if (p_cur != NULL)
{
p = p_cur;
}
return p;
}
void TerminalCursorSetPos(size_t x, size_t y)
{
size_t new_pos = (y * VIDEO_BUF_LINEMAX) + x;
outb(CURSOR_PORT, 0x0F);
outb(CURSOR_PORT + 1, (char)(new_pos & 0xFF));
outb(CURSOR_PORT, 0x0E);
outb(CURSOR_PORT + 1, (char)((new_pos >> 8) & 0xFF));
}
void TerminalInput(char *p_data, size_t *p_dataSz)
{
char *p_stdin = Stdin(NULL);
size_t stdin_sz;
TerminalOpen();
while (TerminalReturn() == FALSE)
{
TerminalFlush();
}
TerminalClose();
stdin_sz = StrLenA(p_stdin);
for (size_t i = 0; i < stdin_sz; i++)
{
if (p_stdin[i] == '\n')
{
p_data[i] = '\0';
*p_dataSz = i + 1;
break;
}
p_data[i] = p_stdin[i];
}
ZeroMemory(p_stdin, stdin_sz);
}
void TerminalEnter()
{
char p_data[BUFSIZE];
static char pp_args[PROGRAM_ARGMAX][PROGRAM_NAMEMAX] = {0};
char *p_token;
size_t data_sz;
size_t i;
MsgProg msg = {0};
int result;
errno_t err;
TerminalPrint("(> ");
TerminalFlush();
TerminalInput(p_data, &data_sz);
if (p_data[0] == 0)
{
return;
}
p_token = StrTokA(p_data, ' ');
StrCpyA(pp_args[0], PROGRAM_NAMEMAX, p_token);
for (i = 1; i < PROGRAM_MAX; i++)
{
p_token = StrTokA(NULL, ' ');
if (p_token == NULL)
{
break;
}
StrCpyA(pp_args[i], PROGRAM_NAMEMAX, p_token);
}
msg.Count = i;
for (i = 0; i < msg.Count; i++)
{
msg.Args[i] = pp_args[i];
}
ProgStart(pp_args[0], &msg, &result);
}
void TerminalOpen()
{
Cursor *p_cur = TerminalCursor(NULL);
p_cur->IsInput = TRUE;
TerminalKeyHandle(p_cur, '\0');
}
boolean TerminalReturn()
{
char *p_buf = Stdin(NULL);
size_t len = StrLenA(p_buf);
if (p_buf[len - 1] == '\n')
{
return TRUE;
}
return FALSE;
}
void TerminalClose()
{
Cursor *p_cur = TerminalCursor(NULL);
p_cur->IsInput = FALSE;
StrCatA(Stdout(NULL), TERMINAL_STDOUT_SZ, Stdin(NULL));
TerminalKeyHandle(p_cur, '\0');
}
boolean TerminalIsOpen()
{
return TerminalCursor(NULL)->IsInput;
}
void TerminalFlush()
{
Cursor *p_cur = TerminalCursor(NULL);
char out_buf[TERMINAL_OUT_SZ];
size_t out_len;
size_t x;
size_t y;
TerminalApplyBackspace();
ZeroMemory(out_buf, TERMINAL_OUT_SZ);
StrCatA(out_buf, TERMINAL_OUT_SZ, Stdout(NULL));
if (p_cur->IsInput)
{
StrCatA(out_buf, TERMINAL_OUT_SZ, Stdin(NULL));
}
out_len = StrLenA(out_buf);
x = 0;
y = 0;
for (size_t i = 0; i < TERMINAL_OUT_SZ; i++)
{
if (out_buf[i] == '\n')
{
x = 0;
y += 1;
if (y == TERMINAL_MAX_LINES)
{
ZeroMemory(Stdout(NULL), StrLenA(Stdout(NULL)));
TerminalPrint("(> ");
TerminalClear();
y = 0;
}
}
else
{
if (i == out_len)
{
if (p_cur->IsInput)
{
p_cur->X = x;
p_cur->Y = y;
TerminalCursorSetPos(p_cur->X, p_cur->Y);
}
}
VIDEO_SET(out_buf[i], VIDEO_COL_DEF, x, y);
if (x == VIDEO_BUF_LINEMAX)
{
x = 0;
y += 1;
}
else
{
x += 1;
}
}
}
}
errno_t StrCatA(char *p_dst, size_t dstSz, const char *p_src)
{
errno_t res = SUCCESS;
size_t src_len = StrLenA(p_src);
char *p_from = (char *)p_src;
for (; dstSz > src_len || ((res = FAIL) && 0); dstSz--)
{
if (*p_dst == 0)
{
break;
}
p_dst++;
}
if (res == SUCCESS)
{
while (*p_from)
{
*p_dst = *p_from;
p_dst++;
p_from++;
}
}
return res;
}
errno_t StrCpyA(char *p_dst, size_t dstSz, const char *p_src)
{
for (size_t i = 0; i < dstSz; i++)
{
if (i == dstSz)
{
return FAIL;
}
p_dst[i] = p_src[i];
}
return SUCCESS;
}
i8 StrCmpA(char *p_str, char *p_cmp)
{
i8 res = 0;
while ((*p_str != '\0' && *p_cmp != '\0') && *p_str == *p_cmp)
{
p_str++;
p_cmp++;
}
res = (*p_str == *p_cmp) ? 0 : (*p_str > *p_cmp) ? 1
: -1;
return res;
}
i8 StrnCmpA(char *p_str, char *p_cmp, size_t n)
{
i8 res = 0;
for (size_t i = 0; i < n; i++)
{
if (p_str[i] != p_cmp[i])
{
res = (p_str[i] == p_cmp[i]) ? 0 : (p_str[i] > p_cmp[i]) ? 1
: -1;
break;
}
if (p_str[i] == 0 || p_cmp[i] == 0)
{
res = (p_str[i] == p_cmp[i]) ? 0 : (p_str[i] > p_cmp[i]) ? 1
: -1;
break;
}
}
return res;
}
size_t StrLenA(const char *p_str)
{
size_t len = 0;
for (; p_str[len]; len++)
{
continue;
}
return len;
}
const char *StrChrA(const char *p_str, const char c)
{
size_t len = StrLenA(p_str);
for (size_t i = 0; i < len; i++)
{
if (p_str[i] == c)
{
return p_str + i;
}
}
return NULL;
}
char *StrChrA(char *p_str, const char c)
{
size_t len = StrLenA(p_str);
for (size_t i = 0; i < len; i++)
{
if (p_str[i] == c)
{
return p_str + i;
}
}
return NULL;
}
const char *StrStrA(const char *p_str, const char *p_sub)
{
size_t off;
if (GetOsMode() == OSMODE_BM)
{
off = BoyerMoore(p_str, p_sub);
if (off != -1)
{
return p_str + off;
}
}
else
{
if (StrLenA(p_str) < StrLenA(p_sub))
{
return NULL;
}
size_t len = StrLenA(p_str) - StrLenA(p_sub);
for (off = 0; off <= len; off++)
{
if (StrnCmpA((char *)(p_str + off), (char *)p_sub, StrLenA(p_sub)) == 0)
{
return p_str + off;
}
}
}
return NULL;
}
char *StrTokA(char *p_str, const char delim)
{
static char p_save[BUFSIZE];
static char *p_ret;
static size_t len;
static size_t i;
char c;
if (p_str)
{
len = StrLenA(p_str);
if (len >= BUFSIZE || len == 0)
{
return NULL;
}
StrCpyA(p_save, BUFSIZE, p_str);
i = 0;
}
if (i)
{
i++;
}
if (i >= len)
{
return NULL;
}
p_ret = p_save + i;
for (; p_save[i] && p_save[i] != delim; i++)
{
continue;
}
p_save[i] = '\0';
return p_ret;
}
char *StrTokA(char *p_str, const char *p_delim)
{
static char *buf;
char *ret;
char *b;
const char *d;
if (p_str != NULL) buf = p_str;
if (buf[0] == '\0') return NULL;
ret = buf;
for (b = buf; *b != '\0'; b++)
{
for (d = p_delim; *d != '\0'; d++)
{
if (*b == *d)
{
*b = '\0';
buf = b + 1;
if (b == ret)
{
ret++;
continue;
}
return ret;
}
}
}
return ret;
}
char *IntToStrA(size_t val)
{
static char buf[BUFSIZE];
return itoa(val, buf, 10);
}
inline boolean IsUpper(const char c)
{
return (c >= 'A') && (c <= 'Z');
}
inline boolean IsLower(const char c)
{
return (c >= 'a') && (c <= 'z');
}
char ToUpper(const char c)
{
if (IsLower(c))
{
return c - ('a' - 'A');
}
else
{
return c;
}
}
char ToLower(const char c)
{
if (IsUpper(c))
{
return c + ('a' - 'A');
}
else
{
return c;
}
}
void ZeroMemory(void *p_buf, size_t bufSz)
{
u8 *p = (u8 *)p_buf;
for (; bufSz; bufSz--)
{
*p = 0;
p++;
}
}
void CopyMemory(void *p_buf, void *p_data, size_t dataSz)
{
i8 *p_dst = (i8 *)p_buf;
i8 *p_src = (i8 *)p_data;
for (; dataSz; dataSz--)
{
*p_dst = *p_src;
p_dst++;
p_src++;
}
}
char GetKeyChar(u8 code)
{
static char key_map[128] = {
0, '\x13', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', // 0x0B
'-', '+', '\b', '?', 'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', // 0x18
'p', 0, 0, '\n', 0, 'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', // 0x26
':', '\'', '`', 0, 0, 'z', 'x', 'c', 'v', 'b', 'n', 'm', 0, 0, '/', // 0x35
0, '*', 0, ' '
};
return key_map[code];
}
char *Stdin(char p_newBuf[TERMINAL_STDIN_SZ])
{
static char *p_buf = NULL;
if (p_newBuf != NULL)
{
p_buf = p_newBuf;
}
return p_buf;
}
char *Stdout(char p_newBuf[TERMINAL_STDOUT_SZ])
{
static char *p_buf = NULL;
if (p_newBuf != NULL)
{
p_buf = p_newBuf;
}
return p_buf;
}
void DefaultIntrHandler()
{
asm("pusha");
asm("popa; leave; iret");
}
void IntrRegHandler(i32 num, u16 segmSel, u16 flags, IntrHandler hndlr)
{
u32 hndlr_addr = (u32)hndlr;
g_idt[num].BaseLo = (u16)(hndlr_addr & 0xFFFF);
g_idt[num].SegmSel = segmSel;
g_idt[num].AlwaysZero = 0;
g_idt[num].Flags = flags;
g_idt[num].BaseHi = (u16)(hndlr_addr >> 16);
}
void IntrStart()
{
int idt_count = sizeof(g_idt) / sizeof(g_idt[0]);
g_idt_ptr.Base = (unsigned int)(&g_idt[0]);
g_idt_ptr.Limit = (sizeof(struct _IdtEntry) * idt_count) - 1;
asm("lidt %0" : : "m"(g_idt_ptr));
}
void IntrEnable()
{
asm("sti");
}
void IntrDisable()
{
asm("cli");
}
inline char inb(u16 port)
{
unsigned char data;
asm volatile ("inb %w1, %b0" : "=a" (data) : "Nd" (port));
return data;
}