#include "shell.h" static void output(BOB64_SHELL *shell,const char *text) { shell->Write(shell->Context,text); } static char lower_ascii(char value) { if(value>='A'&&value<='Z')return (char)(value-'A'+'a'); return value; } static int equals(const char *left,const char *right) { while(*left&&*right&&lower_ascii(*left)==*right){left++;right++;} return !*left&&!*right; } static void print_hex64(BOB64_SHELL *shell,u64 value) { static const char digits[]="0123456789abcdef"; char text[17]; for(u32 i=0;i<16;i++)text[i]=digits[(value>>(60-i*4))&15]; text[16]=0;output(shell,text); } static void print_decimal(BOB64_SHELL *shell,usize value) { char text[21];usize length=0; do {text[length++]=(char)('0'+value%10);value/=10;} while(value&&lengthPageAllocator; if(allocator) { for(usize i=0;iCount;i++)free_pages+=allocator->Extents[i].Pages; for(usize i=0;iAllocatedCount;i++) allocated_pages+=allocator->Allocated[i].Pages; } output(shell,"free pages=0x");print_hex64(shell,free_pages);output(shell,"\r\n"); output(shell,"allocated pages=0x");print_hex64(shell,allocated_pages);output(shell,"\r\n"); output(shell,"heap mapped=0x"); print_hex64(shell,(u64)bob64_heap_mapped_bytes(shell->Heap));output(shell," bytes\r\n"); } static void print_file_data(BOB64_SHELL *shell,const char *data,usize length) { static const char digits[]="0123456789abcdef"; char character[2]={0,0},escaped[5]={'\\','x','0','0',0}; for(usize i=0;i=32&&value<=126)||value=='\r'||value=='\n'||value=='\t') { character[0]=(char)value;output(shell,character); } else { escaped[2]=digits[value>>4];escaped[3]=digits[value&15]; output(shell,escaped); } } } static void run_file_command(BOB64_SHELL *shell,char *arguments) { char *argv[BOB64_SHELL_ARGUMENT_LIMIT]; usize argc=0; char *cursor=arguments; int started=0; if(!shell->Run) {output(shell,"app execution unavailable\r\n");return;} while(*cursor==' ')cursor++; if(!*cursor) {output(shell,"usage: run NAME [ARG ...]\r\n");return;} while(*cursor) { if(argc==BOB64_SHELL_ARGUMENT_LIMIT) { output(shell,"too many arguments\r\n");return; } argv[argc++]=cursor; while(*cursor&&*cursor!=' ')cursor++; if(*cursor)*cursor++=0; while(*cursor==' ')cursor++; } s64 status=shell->Run(shell->Context,argv[0],argc, (const char *const *)argv,&started); if(!started) {output(shell,"unable to start application\r\n");return;} output(shell,"application exit status=0x"); print_hex64(shell,(u64)status);output(shell,"\r\n"); } static void compile_file_command(BOB64_SHELL *shell,char *arguments) { char source[BOB64_FS_NAME_CAPACITY],destination[BOB64_FS_NAME_CAPACITY]; usize source_length=0,destination_length=0,error_offset=0; char *cursor=arguments; int result; while(*cursor==' ')cursor++; while(cursor[source_length]&&cursor[source_length]!=' '&& source_lengthCompile) {output(shell,"C compiler unavailable\r\n");return;} result=shell->Compile(shell->Context,source,destination,&error_offset); if(result==-2) { output(shell,"C compile error near character ");print_decimal(shell,error_offset); output(shell,". Supports int main(void), helper calls, void/char returns, pointers, and bob64_app_write.\r\n"); } else if(result)output(shell,"C compile failed; check source and free filesystem space\r\n"); else {output(shell,"compiled to ");output(shell,destination);output(shell,"\r\n");} } static int list_file(void *context,const char *name,usize length) { BOB64_SHELL *shell=(BOB64_SHELL *)context; output(shell,name);output(shell," 0x");print_hex64(shell,(u64)length); output(shell," bytes\r\n"); return 0; } static usize text_length(const char *text) { usize length=0; while(text[length])length++; return length; } static void execute_file_command(BOB64_SHELL *shell,u8 operation,char *arguments) { BOB64_FILESYSTEM *filesystem=shell->Filesystem; char *name=arguments,*separator=arguments,*contents_argument; const char *contents; usize length; if(!filesystem) { output(shell,"filesystem unavailable\r\n");return; } if(operation==0) { if(!bob64_fs_list(filesystem,list_file,shell))output(shell,"no files\r\n"); return; } while(*name==' ')name++; separator=name; while(*separator&&*separator!=' ')separator++; contents_argument=separator; if(*separator) { *separator++=0; while(*separator==' ')separator++; } if(!*name||((operation==1||operation==3)&&*contents_argument)) { output(shell,"usage: cat NAME | write NAME TEXT | rm NAME\r\n");return; } if(operation==1) { if(bob64_fs_read(filesystem,name,&contents,&length)) { output(shell,"file not found\r\n");return; } print_file_data(shell,contents,length);output(shell,"\r\n");return; } if(operation==2) { if(bob64_fs_write(filesystem,name,separator,text_length(separator))) output(shell,"write failed (name/data invalid or heap full)\r\n"); else output(shell,"saved\r\n"); return; } if(bob64_fs_delete(filesystem,name))output(shell,"file not found\r\n"); else output(shell,"deleted\r\n"); } static void execute(BOB64_SHELL *shell) { char *line=shell->Line; if(!shell->Length)return; line[shell->Length]=0; if(equals(line,"help")) { output(shell,"help show commands\r\nclear clear screen\r\nmem physical memory and heap\r\n"); output(shell,"heap heap usage\r\nls list files\r\ncat NAME read a file\r\n"); output(shell,"write NAME TEXT save text\r\nrm NAME delete a file\r\n"); output(shell,"run NAME [ARG ...] launch a B64E application (try bob.b64e)\r\n"); output(shell,"run desktop.b64e [FILE] edit a RAM text file in the GUI\r\n"); output(shell,"cc SOURCE [OUTPUT] compile supported C to B64E (try bob.c)\r\n"); output(shell,"save checkpoint files in RAM\r\nrestore restore RAM checkpoint\r\n"); output(shell,"echo print text\r\nversion show kernel version\r\n"); } else if(equals(line,"clear")) { if(shell->Clear)shell->Clear(shell->Context); output(shell,"bob64! kernel shell\r\n"); } else if(equals(line,"mem"))print_memory(shell); else if(equals(line,"heap")) { output(shell,"heap mapped=0x"); print_hex64(shell,(u64)bob64_heap_mapped_bytes(shell->Heap)); output(shell," bytes\r\n"); } else if(equals(line,"version"))output(shell,"bob64 kernel 0.1\r\n"); else if(equals(line,"save")) { usize size,written; if(bob64_fs_snapshot_size(shell->Filesystem,&size)) { output(shell,"snapshot size failed\r\n");return; } void *snapshot=bob64_heap_alloc(shell->Heap,size); if(!snapshot||bob64_fs_snapshot_write(shell->Filesystem,snapshot,size,&written)) { if(snapshot)bob64_heap_free(shell->Heap,snapshot); output(shell,"snapshot save failed (heap full)\r\n");return; } if(shell->Snapshot)bob64_heap_free(shell->Heap,shell->Snapshot); shell->Snapshot=snapshot;shell->SnapshotSize=written; output(shell,"saved B64S v1 checkpoint in RAM; lost on reboot\r\n"); } else if(equals(line,"restore")) { if(!shell->Snapshot||bob64_fs_snapshot_restore(shell->Filesystem, shell->Snapshot,shell->SnapshotSize)) { output(shell,"restore failed; files unchanged\r\n");return; } output(shell,"restored B64S v1 checkpoint\r\n"); } else if(equals(line,"ls"))execute_file_command(shell,0,line+2); else if(shell->Length>4&&lower_ascii(line[0])=='c'&& lower_ascii(line[1])=='a'&&lower_ascii(line[2])=='t'&&line[3]==' ') execute_file_command(shell,1,line+4); else if(shell->Length>6&&lower_ascii(line[0])=='w'&& lower_ascii(line[1])=='r'&&lower_ascii(line[2])=='i'&& lower_ascii(line[3])=='t'&&lower_ascii(line[4])=='e'&&line[5]==' ') execute_file_command(shell,2,line+6); else if(shell->Length>3&&lower_ascii(line[0])=='r'&& lower_ascii(line[1])=='m'&&line[2]==' ') execute_file_command(shell,3,line+3); else if(shell->Length>4&&lower_ascii(line[0])=='r'&& lower_ascii(line[1])=='u'&&lower_ascii(line[2])=='n'&&line[3]==' ') run_file_command(shell,line+4); else if(shell->Length>3&&lower_ascii(line[0])=='c'&& lower_ascii(line[1])=='c'&&line[2]==' ') compile_file_command(shell,line+3); else if(shell->Length>=5&&lower_ascii(line[0])=='e'&& lower_ascii(line[1])=='c'&&lower_ascii(line[2])=='h'&& lower_ascii(line[3])=='o'&&line[4]==' ') { output(shell,line+5);output(shell,"\r\n"); } else { output(shell,"unknown command: ");output(shell,line);output(shell,"\r\n"); } } static void prompt(BOB64_SHELL *shell) { output(shell,"bob64> "); } int bob64_shell_init(BOB64_SHELL *shell,BOB64_PAGE_ALLOCATOR *allocator, BOB64_HEAP *heap,BOB64_FILESYSTEM *filesystem, BOB64_SHELL_WRITE write, BOB64_SHELL_CLEAR clear,void *context) { if(!shell||!write)return -1; shell->Length=0;shell->IgnoreLineFeed=0;shell->Line[0]=0; shell->PageAllocator=allocator;shell->Heap=heap;shell->Filesystem=filesystem; shell->Snapshot=0;shell->SnapshotSize=0; shell->Write=write;shell->Clear=clear;shell->Context=context; shell->Run=0; shell->Compile=0; output(shell,"bob64! kernel shell; type help\r\n");prompt(shell); return 0; } void bob64_shell_set_runner(BOB64_SHELL *shell,BOB64_SHELL_RUN run) { if(shell)shell->Run=run; } void bob64_shell_set_compiler(BOB64_SHELL *shell,BOB64_SHELL_COMPILE compile) { if(shell)shell->Compile=compile; } void bob64_shell_input(BOB64_SHELL *shell,int character) { char echo[2]; if(!shell||!shell->Write||character<0)return; if(character=='\n'&&shell->IgnoreLineFeed) { shell->IgnoreLineFeed=0;return; } shell->IgnoreLineFeed=(u8)(character=='\r'); if(character=='\r'||character=='\n') { output(shell,"\r\n");execute(shell);shell->Length=0;shell->Line[0]=0; prompt(shell);return; } if(character=='\b'||character==0x7f) { if(shell->Length) { shell->Length--;shell->Line[shell->Length]=0; output(shell,"\b \b"); } return; } if(character<32||character>126||shell->Length>=BOB64_SHELL_LINE_CAPACITY-1)return; shell->Line[shell->Length++]=(char)character; shell->Line[shell->Length]=0; echo[0]=(char)character;echo[1]=0;output(shell,echo); }