diff options
author | xultist <xultist@proton.me> | 2023-03-02 16:55:23 +1100 |
---|---|---|
committer | xultist <xultist@proton.me> | 2023-03-02 16:55:23 +1100 |
commit | 6802fb6cf1eebfccb6eeda39c26f6a043b485489 (patch) | |
tree | becdc35d1ffdba97cd51ca972429c48a12162eda |
tinker fixes
-rwxr-xr-x | Apps/CowSay.HC | 44 | ||||
-rwxr-xr-x | Commands.HC | 55 | ||||
-rwxr-xr-x | Run.HC | 69 | ||||
-rwxr-xr-x | libs/ExultistLib.HH | 116 |
4 files changed, 284 insertions, 0 deletions
diff --git a/Apps/CowSay.HC b/Apps/CowSay.HC new file mode 100755 index 0000000..d97fbd7 --- /dev/null +++ b/Apps/CowSay.HC @@ -0,0 +1,44 @@ +U8 *border(I64 length){ + U8 *heap=StrNew(""),*tmp; + I64 i; + for (i = 0; i < length + 2; i++){ + tmp=MStrPrint("%s%c",heap,'-'); + Free(heap); + heap=tmp; + } + return heap; +} +U0 CowSay(U8 *string = "This is CowSay running on TempleOS!"){ + I64 inputLength = StrLen(string); + " %s \n", border(inputLength); + "< %s >\n", string; + " %s \n", border(inputLength); + " \\ ^__^\n"; + " \\ (oo)\\_______\n"; + " (__)\\ )\\/\\\n"; + " ||----w |\n"; + " || ||\n"; + +} + +U0 GodSay(U8 *string = NULL){ + if (!string){ + I64 number = 6; + I64 i; + for (i=0; i<number; i++) { + string = MStrPrint("%s%s ", string, god.words[GodBits(17)%god.num_words]); + } + } + I64 inputLength = StrLen(string); + + " __ %s\n", border(inputLength); + " __----__ < %s >\n", string; + "{ p p } %s\n", border(inputLength); + "{____||__}__\n"; + " / || \\/ / \n"; + " / / - |_/ \n"; + " \\_\\ | \n"; + " |= = =| \n"; + " '-----' \n"; + " |_) |_)\n"; +}
\ No newline at end of file diff --git a/Commands.HC b/Commands.HC new file mode 100755 index 0000000..5a868f1 --- /dev/null +++ b/Commands.HC @@ -0,0 +1,55 @@ +#include "./libs/ExultistLib.HH" +#define MY_HASH_TABLE_SIZE 256 + +// -- HashTables +class CMyClass:CHash { + U0 (*f)(U8 **, I64); +}; + +CHashTable *my_hash_table=HashTableNew(MY_HASH_TABLE_SIZE); + +U0 Register(CMyClass *h,U8 *str, I64 type,U0 (*f)()){ + h->str=StrNew(str); + h->type=type; + h->f=f; + HashAdd(h,my_hash_table); + +} +// I know that Cd(); checks if a file exists but I don't like the output sadly. +U0 CMDCd(U8 **params, I64 cnt){ + if (cnt>1){ + U8 *path = RevDirSub(params[0]); + // FileFind doesn't work if the directory is just *:/ so it must be a full path, so we just skip it and go through. + if (!StrCmp(path, "/") || !StrCmp(path, "/Home")){ + Cd(path); + } else{ + if (FileFind(path)){ // Directories are still counted as files + if (IsDir(path)){ // Makes sure they are actually directories though. + Cd(path); + } else{ + "$FG,8$ '%s' is a file, not a directory.\n$FD$", path; + } + } else{ + "$FG,8$'%s' is not a directory or file\n$FD$", path; + } + } + Free(path); + } +} + +U0 Pwd(){ + "%s\n", DirCur; +} + +U0 Ls(){ + Dir; +} + +CMyClass *my_item=CAlloc(sizeof(CMyClass)); +Register(my_item, "cd",HTT_FUN,&CMDCd); + +CMyClass *my_item2=CAlloc(sizeof(CMyClass)); +Register(my_item2, "pwd",HTT_FUN,&Pwd); + +CMyClass *my_item3=CAlloc(sizeof(CMyClass)); +Register(my_item3, "ls",HTT_FUN,&Ls); @@ -0,0 +1,69 @@ +#include "./libs/ExultistLib.HH"; +#include "./Commands.HC" +#define TILDE_BM 0x70F99F0E0000 +#define TILDE_CHARCODE 0x7E +#define SIGMA "\xE4" + + + +U0 PatchFont() { + text.font[TILDE_CHARCODE] = TILDE_BM; +} + +U0 KOnce(){ + U8 *file = "/Home/.kadoshrc"; + if (FileFind(file)){ + ExeFile2(file); + } + else { + CDirEntry de; + FileWrite(file,NULL,0,de.datetime,0); + } +} +U8 *Prompt(){ + static U8 buf[200]; + U8 *prompt = DirSub(DirCur); + StrPrint(buf, "$FG,8$%s $FG,2$"SIGMA" $FD$", prompt); + Free(prompt); + return buf; +} +U0 Kadosh(){ + U16 count = 0; + U8 *input; + U8 **str_arr, **params; + I64 word_cnt; + + PatchFont; + while (1){ + if (count == 0) KOnce; + + "%s", Prompt; + input = GetStr(""); + count++; + + if (!StrCmp(input, "quit")) break; + else{ + str_arr=StrSplit(input,&word_cnt); + if (word_cnt != 0){ + CMyClass *tmph; + tmph=HashFind(str_arr[0],my_hash_table,HTT_FUN); + //params = SpliceAfter(str_arr, word_cnt, 1); + params = &str_arr[1]; + if (tmph) { + tmph->f(params, word_cnt); + } + else{ + "$FG,4$ERR:$FG,0$ command '%s' not recognized.\n$FD$", str_arr[0]; + } + //Free(params); + + } + + + } + Free(str_arr); + Free(input); + } + +} +Kadosh;
\ No newline at end of file diff --git a/libs/ExultistLib.HH b/libs/ExultistLib.HH new file mode 100755 index 0000000..dec112a --- /dev/null +++ b/libs/ExultistLib.HH @@ -0,0 +1,116 @@ +/* +Formally xStrings, now ExultistsLib to reflect the amount this library covers. +License: GPLv3 +Credits to: + -> TheTinkerer: For literally loads. + -> Sree Kotay: for his suggestion on Reddit to improve wordCount. + -> Nrootconauto: for teaching me how arrays work prior to this. + +This is basically a library for this shell that I feel like could be applied to other projects. +Attributions will be made for certain function. +*/ + + +I64 wordCount(U8 *string){ + // Returning word count of a string. + U8 *stringEnd = string + StrLen(string); + I64 numOfWords= 0; + while (string < stringEnd && *string == ' ') string++; + while (string < stringEnd) { + while (string < stringEnd && *string != ' ') string++; + while (string < stringEnd && *string == ' ') string++; + numOfWords++; + } + return numOfWords; +} + +/* +TheTinker StrSplit +License: Public Domain + +This method only MAllocs one chuck of memory +which holds both the array of pointers to the +individual words that got split and also all +the words themselves at the same time. + +Since there is only 1 MAlloc, you only have +to do one Free (just free what StrSplit returns). +*/ +U8 **StrSplit(U8* String, I64 *_word_cnt){ + + I64 len,arr_end,word_index; + U8 *str,**str_arr,*stringEnd; + + arr_end=*_word_cnt=wordCount(String); + len=StrLen(String); + + // Points to end of the array of pointers + arr_end*=sizeof(U8*); + + str_arr = MAlloc(arr_end + len + 1); + + // The strings are stored here, just after the array + str = str_arr(U8*)+arr_end; + stringEnd=str+len; + + // Copy in the original string + MemCpy(str,String,len+1); + + word_index=0; + + while (str < stringEnd && *str == ' ') str++; + while (str < stringEnd) { + str_arr[word_index++]=str; // Store word start pointer + while (str < stringEnd && *str != ' ') str++; + *str++=0; // Split the string when we find the end + while (str < stringEnd && *str == ' ') str++; + } + + return str_arr; + +} + +Bool StartsWith(U8 *str, U8 *check){ + return !StrNCmp(str,check,StrLen(check)); +} + +U8 **SpliceAfter(U8 **str_arr, I64 cnt, I64 index){ + // Splices an array into a new array after certain index. + I64 i; + I64 newCnt = cnt - index; + U8 **new_arr = MAlloc(newCnt * sizeof(U8*)); + for (i = 0; i < newCnt; i++) { + new_arr[i] = str_arr[i + index]; + } + return new_arr; +} + +U8 *DirSub(U8 *string){ + U8 *ret = StrNew(""); + U8 *SplitStr = CAlloc(StrLen(string)+1); + + // &string[2] is a string that ignores the drive letter: part + if(StartsWith(&string[2], "/Home")) { + StrScan(&string[2],"/Home%s",&SplitStr); + ret = MStrPrint("~%s", SplitStr); + } + else if(StartsWith(&string[2], "/")){ + StrScan(string,"T:/%s",&SplitStr); + ret = MStrPrint("/%s", SplitStr); + } else{ret = string;} + Free(SplitStr); + return ret; +} + +U8 *RevDirSub(U8 *string){ + U8 *ret = StrNew(""); + U8 *SplitStr = CAlloc(StrLen(string)+1); + if(StartsWith(string, "~")) { + StrScan(string,"~%s",&SplitStr); + ret = MStrPrint("/Home%s", SplitStr); + } + else{ret = StrNew(string);} + + Free(SplitStr); + return ret; +} |