addHistoryFromFile.c (1744B)
1 #! /usr/bin/env sheepy 2 #include "libsheepyObject.h" 3 4 #define HB "~/myhistory.bin" 5 6 // command is equal to 7 char *filter[] = { 8 "h", 9 "fg", 10 "bg", 11 "p", 12 "gu", 13 "gg", 14 "gh", 15 "gs", 16 "g lg", 17 "l", 18 "ls", 19 "la", 20 "history", 21 "E", 22 "env", 23 "df", 24 "dfh", 25 "du", 26 "duu", 27 "exit", 28 "logout", 29 "top", 30 "htop", 31 "reset", 32 null 33 }; 34 // command starts with 35 char *start[] = { 36 "fg ", 37 "whal ", 38 "l ", 39 "ls ", 40 "la ", 41 "df ", 42 "dfh ", 43 "du ", 44 "duu ", 45 "history|", 46 null 47 }; 48 49 50 int main(int ARGC, char** ARGV) { 51 52 // bashHist.txt 53 // 1 24-10-24 20:56:35 ssw 54 55 if (!ARGV[1] or not isPath(ARGV[1])) { 56 logE("Input not found.\n" 57 "Usage: addHistoryFromFile bashHistoryFile.txt"); 58 ret 1; 59 } 60 61 createAllocateSmallArray(h); 62 readFileG(h, ARGV[1]); 63 64 cleanCharP(hbp) = expandHomeG(HB); 65 66 iter(h, L) { 67 char *l = ssGet(L); 68 char *d = l + 7; 69 char *cmd = l + 25; 70 *(cmd-1) = 0; 71 cleanCharP(date) = catS("20", d); 72 /* logVarG(date); */ 73 /* logVarG(cmd); */ 74 75 // filter commands 76 bool skip = no; 77 forEachS(filter, s) { 78 if (eqG(cmd, s)) { skip = yes; break;} 79 } 80 forEachS(start, s) { 81 if (startsWithG(cmd, s)) { skip = yes; break;} 82 } 83 if (skip) continue; 84 85 // add commands 86 87 cleanAllocateSmallJson(a); 88 // save history as binary 89 // a = [path,exit code, date, command] 90 pushG(a, ""/*path*/); 91 pushG(a, "0" /*exit code*/); 92 pushG(a, date); 93 pushG(a, cmd); 94 smallBytest *b = serialG(a); 95 u8 buf[65536]; 96 u32 len = lenG(b) + 4*2; 97 memcpy(buf, &len, 4); 98 memcpy(buf+4, getValO(b), lenG(b)); 99 memcpy(buf+4+lenG(b), &len, 4); 100 pError0(appendFile(hbp, buf, len)); 101 } 102 103 ret 0; 104 } 105 // vim: set expandtab ts=2 sw=2: