Created
February 23, 2020 11:23
-
-
Save lilydjwg/c4c3f27214c9bd6805da5181ed170b4b to your computer and use it in GitHub Desktop.
A simple C program to decode & encode zsh history file
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//===================================================================== | |
// 让 zsh 的历史记录可读 | |
//===================================================================== | |
#include<stdio.h> | |
#include<stdlib.h> | |
#include<string.h> | |
//--------------------------------------------------------------------- | |
void readhist(); | |
void writehist(); | |
void usage(); | |
static int ismeta(int ch); | |
//--------------------------------------------------------------------- | |
int main(int argc, char** argv){ | |
if(argc == 1) | |
readhist(); | |
else if(argc == 2){ | |
if(!strcmp(argv[1], "read")) | |
readhist(); | |
else if(!strcmp(argv[1], "write")) | |
writehist(); | |
else | |
usage(); | |
}else | |
usage(); | |
return 0; | |
} | |
void readhist(){ | |
int change = 0; | |
int ch, d; | |
while((ch = getchar()) != EOF){ | |
if(ch != 0x83){ | |
if(change){ | |
d = ch ^ 32; | |
}else | |
d = ch; | |
putchar(d); | |
change = 0; | |
}else | |
change = 1; | |
} | |
} | |
void writehist(){ | |
int ch, d; | |
while((ch = getchar()) != EOF){ | |
if(ismeta(ch)){ | |
d = 0x83; | |
putchar(d); | |
d = ch ^ 32; | |
putchar(d); | |
}else{ | |
putchar(ch); | |
} | |
} | |
} | |
void usage(){ | |
fprintf(stderr, "Usage: zhist [read|write]\n" | |
"\tzhist reads from stdin and outputs to stdout, " | |
"with the content changes\nto be read by human being " | |
"or by the zsh program.\n"); | |
exit(1); | |
} | |
static int ismeta(int ch){ | |
return (ch > 0x83 && ch < 0x9e) | |
|| ch == 0xa0 || ch == 0x83 || ch == 0; | |
} | |
//===================================================================== |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment