Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #ifndef _term_h_
- #define _term_h_
- #include "../../type.h"
- #include "../../def.h"
- #include "../../envvar/envvar.h"
- #include "../../vmem/vmem.h"
- #include "../../kvideo/kvideo.h"
- #include "../../dev/dev.h"
- #include "../../dev/kb/kb.h"
- #include "../../tickcounter/tickcounter.h"
- uint32 term_main(void);
- uint32 term_process(char *string);
- #endif
- #include "term.h"
- uint32 term_main(void)
- {
- //setup screen and show welcome message
- kvideo_setcolor(KVIDEO_NOBLINK(KVIDEO_FGBG(KVIDEO_LTGREEN, KVIDEO_BLACK)));
- kvideo_clear();
- kvideo_print("Sidon X86 Network Attack System. [Terminal v0.0]\n");
- envvar_set("TERMPROMPT", "SIDON#"); //terminal string
- envvar_seti("TERMPROMPTTD", 4); //key timeout divier
- //initalize keyboard
- uint32 kbdev = dev_find_device("keyboard");
- if (kbdev != UINT32_MAX)
- dev_ioctl(kbdev, DEV_IOCTL_INITDEV, 0);
- uint8 lsc = 0; //holder for last keyboard scan code
- //enter terminal loop
- uint8 running = 1;
- char *inputbuf = (char*)vmem_alloc_block();
- uint32 keytimeout = 0xFFFFFFFF;
- _term_prompt:;
- uint32 inputidx = 0, timeoutdiv = (uint32)envvar_geti("TERMPROMPTTD");
- uint8 kbBuf = 0, lastBuf = 0;
- memset(inputbuf, 0, VMEM_BLOCK_SZ);
- kvideo_print(envvar_get("TERMPROMPT")); kvideo_print(" ");
- uint16 inpbuf_x = _kvideo_print_x;
- uint16 inpbuf_y = _kvideo_print_y;
- inputidx = 0; if (timeoutdiv == 0) timeoutdiv = 1;
- for (lsc = 0; lsc != 0x81; dev_read(kbdev, (void*)&lsc, 1))
- {
- kbBuf = lsc;
- if (lastBuf != kbBuf)
- {
- _term_keytimeout_ok:
- inputbuf[inputidx] = kb_scan2chr(kbBuf);
- if (inputbuf[inputidx] == '\b') { inputidx--; continue; }
- if ((inputidx == 0) && (inputbuf[inputidx] == '\n'))
- continue; // we just ran a command, lsc is newline so skip
- if (inputbuf[inputidx] == 0) { lastBuf = kbBuf; continue; }
- kvideo_movecursor(inpbuf_x, inpbuf_y);
- kvideo_print(inputbuf);
- if (inputbuf[inputidx] == '\n') //execute commands
- {
- inputbuf[inputidx] = '\0'; //replace newline with NULL
- if (term_process(inputbuf)) goto _term_exit; //exit if 1
- goto _term_prompt;
- }
- lastBuf = kbBuf;
- keytimeout = tickcounter_gettickcount() +
- (tickcounter_tickspersec()/timeoutdiv);
- inputidx++;
- }
- else if (tickcounter_gettickcount() > keytimeout)
- {
- goto _term_keytimeout_ok;
- }
- }
- _term_exit:
- vmem_free_block(inputbuf);
- //return no error
- return 0;
- }
- KFUNC uint32 term_atoui(char *s)
- {
- uint32 i = 0;
- while(*s >= '0' && *s <= '9')
- {
- i = (i<<3) + (i<<1) + (*s - '0');
- s++;
- }
- return i;
- }
- uint32 term_process(char *string)
- {
- kvideo_print("RUNNING: ");
- kvideo_print(string);
- kvideo_print("\n");
- //do stuff
- if (strncmp(string, "cls", 3) == 0) //cls
- {
- kvideo_clear();
- }
- else if (strncmp(string, "envs", 4) == 0) //envs (string variables)
- {
- char *key = string + 5;
- char *space = strrchr(string, ' ');
- if (key > space) //space before key, only one argument
- {
- kvideo_print(key);
- kvideo_print(" = ");
- kvideo_print(envvar_get(key));
- kvideo_print("\n");
- }
- else
- {
- *space = '\0';
- char *value = space + 1;
- kvideo_print(key);
- kvideo_print(" = ");
- kvideo_print(value);
- kvideo_print("\n");
- envvar_set(key, value);
- *space = ' ';
- }
- }
- else if (strncmp(string, "envi", 4) == 0) //envs (number variables)
- {
- char *key = string + 5;
- char *space = strrchr(string, ' ');
- if (key > space) //space before key, only one argument
- {
- kvideo_print(key);
- kvideo_print(" = ");
- kvideo_printdec(envvar_geti(key));
- kvideo_print("\n");
- }
- else
- {
- *space = '\0';
- char *strvalue = space + 1;
- int value = (int)term_atoui(strvalue);
- kvideo_print(key);
- kvideo_print(" = ");
- kvideo_printdec(value);
- kvideo_print("\n");
- envvar_seti(key, value);
- *space = ' ';
- }
- }
- else if (strncmp(string, "exit", 4) == 0) //exit
- {
- return 1;
- }
- else if (strncmp(string, "launch", 6) == 0) //launch app (hardcoded list)
- {
- if (strncmp(string + 7, "xedit", 5) == 0)
- {
- kvideo_pushcursor();
- memcpy((uint8*)0x00090000, (uint8*)VGA_TEXTCOLOR, KVIDEO_W * KVIDEO_H * 2);
- xedit_main();
- kvideo_popcursor();
- memcpy((uint8*)VGA_TEXTCOLOR, (uint8*)0x00090000, KVIDEO_W * KVIDEO_H * 2);
- uint32 kbdev = dev_find_device("keyboard");
- if (kbdev != UINT32_MAX) //re-init keyboard
- dev_ioctl(kbdev, DEV_IOCTL_INITDEV, 0);
- }
- }
- else if (strncmp(string, "help", 4) == 0) //help
- {
- kvideo_print("COMMANDS:\n");
- kvideo_print("cls - Clear the screen\n");
- kvideo_print("envs KEY [VALUE] - Get [set] environment string\n");
- kvideo_print("envi KEY [VALUE] - Get [set] environment integer\n");
- kvideo_print("exit - Exit terminal (return to kernel)\n");
- kvideo_print("launch [APP] - Launch app (apps: xedit, )\n");
- kvideo_print("help - Show this information\n");
- }
- kvideo_print("DONE.\n");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement