c - Console input in 32-bit Protected mode -
i working on os. i've started building since a-day before yesterday. os command-based.
this kernel.c(the main file):
#include "include/screen.h" #include "include/kb.h" #include "include/string.h" #include "data/userdata.c" kmain() { clearscreen(); print("halcyon os 1.05 beta "); while (1) { print("\nhalcyon@halcyon ~\n$ "); string ch = readstr(); if(streql(ch,"cmd")!=0) { print("\nyou in cmd\n"); } else if(streql(ch,"clear")!=0) { clearscreen(); } else if(streql(ch,"help")!=0) { print("halcyon help."); } else if(streql(ch,"")!=0) { continue; } else { print("\nno command found:");print(ch); break; } }// end while loop! }
and here kb.h (i've made keyboard support.):
#ifndef kb_h #define kb_h #include "screen.h" #include "system.h" #include "types.h" string readstr() { char buff; string buffstr; uint8 = 0; uint8 reading = 1; while(reading) { if(inportb(0x64) & 0x1) { switch(inportb(0x60)) { /*case 1: printch('(char)27); escape button buffstr[i] = (char)27; i++; break;*/ case 2: printch('1'); buffstr[i] = '1'; i++; break; case 3: printch('2'); buffstr[i] = '2'; i++; break; case 4: printch('3'); buffstr[i] = '3'; i++; break; case 5: printch('4'); buffstr[i] = '4'; i++; break; case 6: printch('5'); buffstr[i] = '5'; i++; break; case 7: printch('6'); buffstr[i] = '6'; i++; break; case 8: printch('7'); buffstr[i] = '7'; i++; break; case 9: printch('8'); buffstr[i] = '8'; i++; break; case 10: printch('9'); buffstr[i] = '9'; i++; break; case 11: printch('0'); buffstr[i] = '0'; i++; break; case 12: printch('-'); buffstr[i] = '-'; i++; break; case 13: printch('='); buffstr[i] = '='; i++; break; case 14: printch('\b'); i--; buffstr[i] = 0; break; /* case 15: printch('\t'); tab button buffstr[i] = '\t'; i++; break;*/ case 16: printch('q'); buffstr[i] = 'q'; i++; break; case 17: printch('w'); buffstr[i] = 'w'; i++; break; case 18: printch('e'); buffstr[i] = 'e'; i++; break; case 19: printch('r'); buffstr[i] = 'r'; i++; break; case 20: printch('t'); buffstr[i] = 't'; i++; break; case 21: printch('y'); buffstr[i] = 'y'; i++; break; case 22: printch('u'); buffstr[i] = 'u'; i++; break; case 23: printch('i'); buffstr[i] = 'i'; i++; break; case 24: printch('o'); buffstr[i] = 'o'; i++; break; case 25: printch('p'); buffstr[i] = 'p'; i++; break; case 26: printch('['); buffstr[i] = '['; i++; break; case 27: printch(']'); buffstr[i] = ']'; i++; break; case 28: // printch('\n'); // buffstr[i] = '\n'; i++; reading = 0; break; /* case 29: printch('q'); left control buffstr[i] = 'q'; i++; break;*/ case 30: printch('a'); buffstr[i] = 'a'; i++; break; case 31: printch('s'); buffstr[i] = 's'; i++; break; case 32: printch('d'); buffstr[i] = 'd'; i++; break; case 33: printch('f'); buffstr[i] = 'f'; i++; break; case 34: printch('g'); buffstr[i] = 'g'; i++; break; case 35: printch('h'); buffstr[i] = 'h'; i++; break; case 36: printch('j'); buffstr[i] = 'j'; i++; break; case 37: printch('k'); buffstr[i] = 'k'; i++; break; case 38: printch('l'); buffstr[i] = 'l'; i++; break; case 39: printch(';'); buffstr[i] = ';'; i++; break; case 40: printch((char)44); // single quote (') buffstr[i] = (char)44; i++; break; case 41: printch((char)44); // tick (`) buffstr[i] = (char)44; i++; break; /* case 42: left shift printch('q'); buffstr[i] = 'q'; i++; break; case 43: \ (< somekeyboards) printch((char)92); buffstr[i] = 'q'; i++; break;*/ case 44: printch('z'); buffstr[i] = 'z'; i++; break; case 45: printch('x'); buffstr[i] = 'x'; i++; break; case 46: printch('c'); buffstr[i] = 'c'; i++; break; case 47: printch('v'); buffstr[i] = 'v'; i++; break; case 48: printch('b'); buffstr[i] = 'b'; i++; break; case 49: printch('n'); buffstr[i] = 'n'; i++; break; case 50: printch('m'); buffstr[i] = 'm'; i++; break; case 51: printch(','); buffstr[i] = ','; i++; break; case 52: printch('.'); buffstr[i] = '.'; i++; break; case 53: printch('/'); buffstr[i] = '/'; i++; break; case 54: printch('.'); buffstr[i] = '.'; i++; break; case 55: printch('/'); buffstr[i] = '/'; i++; break; /*case 56: printch(' '); right shift buffstr[i] = ' '; i++; break;*/ case 57: printch(' '); buffstr[i] = ' '; i++; break; } } } buffstr[i] = 0; return buffstr; } #endif
and @ last string.h(this has function compare 2 strings.):
#ifndef string_h #define string_h #include "types.h" uint16 strlength(string ch) { uint16 = 1; while(ch[i++]); return --i; } uint8 streql(string ch1,string ch2) { uint8 result = 1; uint8 size = strlength(ch1); if(size != strlength(ch2)) result =0; else { uint8 = 0; for(i;i<=size;i++) { if(ch1[i] != ch2[i]) result = 0; } } return result; } #endif
but problem if enter correct command 'cmd', says 'no command found: cm' , works! goes correct , says: 'you're in cmd!'
also, recognizing 'cm' not 'cmd'.
and if write in kernel.c: "print("hello");" print: 'hell' not 'hello'! misses last character.
i don't know what's wrong program. use gcc compile it, platform linux ubuntu. work , not. if compile kernel windows command never works.
please help! appreciated.
your string length function wrong:
uint16 strlength(string ch) { uint16 = 1; while(ch[i++]); return --i; }
assuming string "cmd", start , check if second character, m
, null, third, fourth null. i
starts off 1 , increases 3, , gets decremented 2.
also fail on 0 length strings.
the consequence is, not printing enough in print function.
unit16 strlength(string ch) { unit16 l = 0; while (ch[l]) { ++l; } return l; }
to find out why comparison fails i'd need know type string
is.
if it's char *
, undefined behaviour, writing random memory.
if it's char [n]
out won't work either, because array decays pointer on return. , pointer points local variable automatic storage, no longer valid after function return.
ok, it's pointer. pointers point memory, store actual data. have working need
- memory store data to
- set pointer point memory.
normally 1 allocate memory via malloc
, in context of operating system development you'd first have implement yourself.
you cold use buffer automatic storage (on stack) in caller:
char buffer[20]; size_t num_read = read_into(buffer, 20); // pass pointer memory data plus maximal characters buffer can hold. size_t read_into (char * buffer, size_t max) { // read max characters // buffer, don't forget count 0 @ end. }
judging rest of code, seem new c programming. while writing operating system kernel fun task, you'll have more fun doing when understand basics first.
since you're writing random memory locations uninitialised char *
don't know store data. "normal", free memory. work expected. write memory mapped device information or own code, making impossible predict might happen.
Comments
Post a Comment