saveHistory.c (4215B)
1 #! /usr/bin/env sheepy 2 #include "libsheepyObject.h" 3 // ARG 1: exit code 4 // ARG 2: last command 5 // export PROMPT_COMMAND='rs=$? ; lastCommand=`history 1 | cut -c 26-` ; ~/bin/saveHistory $rs "$lastCommand"' 6 7 #define H "~/myhistory.txt" 8 #define HE "~/myhistoryExit.txt" 9 #define HP "~/myhistoryPath.txt" 10 #define HB "~/myhistory.bin" 11 // command is equal to 12 char *filter[] = { 13 "h", 14 "fg", 15 "bg", 16 "p", 17 "gu", 18 "gg", 19 "gh", 20 "gs", 21 "g lg", 22 "l", 23 "ls", 24 "la", 25 "history", 26 "E", 27 "env", 28 "df", 29 "dfh", 30 "du", 31 "duu", 32 "exit", 33 "logout", 34 "top", 35 "htop", 36 "reset", 37 null 38 }; 39 // command starts with 40 char *start[] = { 41 "fg ", 42 "whal ", 43 "l ", 44 "ls ", 45 "la ", 46 "df ", 47 "dfh ", 48 "du ", 49 "duu ", 50 "history|", 51 null 52 }; 53 54 55 int main(int ARGC, char** ARGV) { 56 cleanCharP(newCmd) = trimS(ARGV[2]); 57 forEachS(filter, s) { 58 if (eqG(newCmd, s)) ret 0; 59 } 60 forEachS(start, s) { 61 if (startsWithG(ARGV[2], s)) ret 0; 62 } 63 initLibsheepy(ARGV[0]); 64 65 cleanCharP(hbp) = expandHomeG(HB); 66 67 // check if last command is duplicate 68 // check only if history file exists 69 // don't save duplicate commands 70 71 // export PROMPT_COMMAND='rs=$? ; pwd >> ~/myhistoryPath.txt ; d=`date +"%F %T"` ; printf "%s %s\n" "$rs" "$d" >> ~/myhistoryExit.txt ; history 1 | cut -c 26- >> ~/myhistory.txt' 72 if (isPath(hbp)) { 73 // ignore duplicate command 74 75 u8 *buf = null; 76 size_t hlen = readFile(hbp, (void**)&buf); 77 78 // get last command 79 cleanAllocateSmallJson(a); 80 u32 len = *(u32*)(buf + hlen - 4); 81 cleanAllocateSmallBytes(b); 82 o(b,set, buf + hlen - len + 4, len-8); 83 deserialG(a, b); 84 // a = [path,exit code, date, command] 85 86 // this is a duplicate, stop 87 cleanCharP(l) = trimS(getG(a, rtChar, 3)); 88 if (eqG(newCmd, l)) ret 0; 89 } 90 91 char path[8192] = init0Var; 92 char *res = bLGetCwd(path, sizeof(path)); 93 char date[128] = init0Var; 94 pErrorNULL(bGetCurrentDateYMD(date)); 95 cleanAllocateSmallJson(a); 96 // save history as binary 97 // a = [path,exit code, date, command] 98 pushG(a, path); 99 pushG(a, ARGV[1]); 100 pushG(a, date); 101 pushG(a, newCmd); 102 smallBytest *b = serialG(a); 103 u8 buf[65536]; 104 u32 len = lenG(b) + 4*2; 105 memcpy(buf, &len, 4); 106 memcpy(buf+4, getValO(b), lenG(b)); 107 memcpy(buf+4+lenG(b), &len, 4); 108 pError0(appendFile(hbp, buf, len)); 109 ret 0; 110 } 111 112 #if 0 113 int main(int ARGC, char** ARGV) { 114 cleanCharP(newCmd) = trimS(ARGV[2]); 115 forEachS(filter, s) { 116 if (eqG(newCmd, s)) ret 0; 117 } 118 forEachS(start, s) { 119 if (startsWithG(ARGV[2], s)) ret 0; 120 } 121 initLibsheepy(ARGV[0]); 122 // export PROMPT_COMMAND='rs=$? ; pwd >> ~/myhistoryPath.txt ; d=`date +"%F %T"` ; printf "%s %s\n" "$rs" "$d" >> ~/myhistoryExit.txt ; history 1 | cut -c 26- >> ~/myhistory.txt' 123 cleanCharP(hp) = expandHomeG(H); 124 if (isPath(hp)) { 125 // ignore duplicate command 126 cleanCharP(h) = readFileS(hp); 127 size_t len = lenG(h); 128 char *last = h+len; 129 u8 status = 0; 130 range(i, len) { 131 if (*last == '\n') { 132 if (status == 1) break; 133 if (status == 0) { 134 // end of last command 135 *last = 0; 136 inc status; 137 } 138 } 139 dec last; 140 } 141 // this is a duplicate, stop 142 cleanCharP(l) = trimS(last); 143 if (eqG(newCmd, l)) ret 0; 144 } 145 cleanCharP(hep) = expandHomeG(HE); 146 cleanCharP(hpp) = expandHomeG(HP); 147 char path[8192] = init0Var; 148 pError0(appendFileS(hpp, bLGetCwd(path, sizeof(path)))); 149 pError0(appendFileS(hpp, "\n")); 150 char date[128] = init0Var; 151 pErrorNULL(bGetCurrentDateYMD(date)); 152 cleanCharP(edate) = formatS("%s %s\n", ARGV[1], date); 153 pError0(appendFileS(hep, edate)); 154 pError0(appendFileS(hp, newCmd)); 155 pError0(appendFileS(hp, "\n")); 156 // doesnt work - system("history 1 | cut -c 26- >> "H); 157 cleanAllocateSmallJson(a); 158 // save history as binary 159 // a = [path,exit code, date, command] 160 pushG(a, path); 161 pushG(a, ARGV[1]); 162 pushG(a, date); 163 pushG(a, newCmd); 164 smallBytest *b = serialG(a); 165 u8 buf[65536]; 166 u32 len = lenG(b) + 4*2; 167 memcpy(buf, &len, 4); 168 memcpy(buf+4, getValO(b), lenG(b)); 169 memcpy(buf+4+lenG(b), &len, 4); 170 cleanCharP(hbp) = expandHomeG(HB); 171 pError0(appendFile(hbp, buf, len)); 172 ret 0; 173 } 174 #endif 175 // vim: set expandtab ts=2 sw=2: