Skip to content

Instantly share code, notes, and snippets.

@OndraZizka
Last active October 18, 2019 01:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save OndraZizka/33ffc838f5bef163a2d322881aec5807 to your computer and use it in GitHub Desktop.
Save OndraZizka/33ffc838f5bef163a2d322881aec5807 to your computer and use it in GitHub Desktop.
LogTransformer (fixes the scrabled log)
set -e
echo "Must run as root.";
apt-get install build-essential autotools
cd /tmp
wget https://github.com/kernc/logkeys/archive/master.zip
unzip master.zip
cd logkeys-master/
./autogen.sh
cd build
../configure
make
sudo make install
sudo locale-gen
sudo logkeys -s
## Init scripts
echo -e '#!/bin/sh\nsudo logkeys -s' > /etc/init.d/runlk.sh
chmod 755 /etc/init.d/runlk.sh
ln -s /etc/init.d/runlk.sh /etc/rc3.d/S99runlk
## SystemD
cat << EOF > /etc/systemd/system/kl.service
[Unit]
Description=Run LK
[Service]
Type=oneshot
ExecStart=/etc/init.d/runlk.sh
RemainAfterExit=yes
[Install]
WantedBy=multi-user.target
# sysinit.target
EOF
## Cron
set -e
sudo apt-get install -y build-essential automake;
cd /tmp
mkdir -p yuf
cd yuf
wget https://github.com/kernc/logkeys/archive/master.zip
unzip master.zip
rm master.zip
cd logkeys-master/
./autogen.sh
cd build
../configure
make
sudo make install
cd /tmp
rm -rf /tmp/yuf
sudo apt-get install -y locales;
sudo locale-gen;
sudo logkeys -s;
#echo 'logkeys ­-s || true;' >> ~/.bash_profile
sudo echo 'logkeys -s || true;' > /etc/init.d/kl.sh
sudo chmod 755 /etc/init.d/kl.sh
## Run at start as root
sudo ln -s /etc/init.d/kl.sh /etc/rc2.d/S99kl.sh
#sudo logkeys -k;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
/**
* In case you're judging this code: It's quick and non-optimal. It's a one-time use tool.
*/
public class TransformLog {
static Map<Integer, Character> charMap = buildCharsMap();
static Set<Integer> twoBytes = buildTwoBytesSet(charMap);
static Map<String, Integer> escapes = buildEscapesMap();
public static final void main(String[] args) throws IOException
{
String inFilePath = args[0];
if (inFilePath == null)
throw new IllegalArgumentException("Transforms chars. Usage: ... <inFilePath>");
String outFilePath = "out-" + inFilePath;
System.out.println("Will write to: " + outFilePath);
FileInputStream is = new FileInputStream(inFilePath);
FileOutputStream os = new FileOutputStream(outFilePath);
InputStreamReader reader = new InputStreamReader(is);
OutputStreamWriter writer = new OutputStreamWriter(os, StandardCharsets.UTF_8);
int ch;
// Main loop
while (-1 != (ch = is.read()))
{
if (ch == 0x0A) { // 0A = entr, skip the time.
os.write(0x0A);
copyBytes(26, is, os);
continue;
}
// Escapes
if (ch == 0x3C) { // <, may start <LShift>
copyEscape(is, writer);
continue;
}
// Two-byte chars
if (twoBytes.contains(ch)) {
//System.err.println("Two-bytes prefix detected: " + (String.format("0x%X", ch)));
int ch2 = is.read();
ch2 = (ch << 8) | ch2;
Character chTrans = charMap.get(ch2);
//System.err.println("Two-bytes char: " + (String.format("0x%X -> %c", ch2, chTrans)));
if (chTrans != null) {
writer.write(chTrans);
writer.flush();
continue;
}
}
// Same char
Character chTrans = charMap.get(ch);
if (chTrans == null) {
os.write(ch);
continue;
}
// Write translated char.
writer.write(chTrans);
writer.flush();
}
}
/**
* Copies the whole escape, e.g. <LShift> or <BckSp>.
*/
private static String copyEscape(FileInputStream is, OutputStreamWriter writer) throws IOException
{
String prefix = "";
int ch;
nextChar:
while (-1 != (ch = is.read())) {
if (ch == '>') {
writer.write("<?");
writer.write(prefix);
writer.write('>');
writer.flush();
System.err.println("Unknown escape: <"+prefix+">");
return null;
}
prefix = prefix + Character.toString(ch);
//System.err.println("Trying prefix: " + prefix);
for (String escape : escapes.keySet()) {
//System.err.println(" Escape: " + escape);
if (escape.equals(prefix)) {
//System.err.println(" MATCH!");
writer.write("<");
writer.write(prefix);
writer.write('>');
writer.flush();
is.read(); // The ending '>';
return prefix;
}
if (escape.startsWith(prefix))
continue nextChar;
}
// We tried all and none matches.
System.err.println("Unknown prefix: " + prefix);
return null;
}
return null;
}
/**
* Some matched chars consist of 2 bytes. This gives a set of those.
*/
private static Set<Integer> buildTwoBytesSet(Map<Integer, Character> charMap)
{
HashSet<Integer> twoBytesChars = new HashSet<>();
for (Integer integer : buildCharsMap().keySet()) {
int firstByte = (integer >> 8) & 0xFF;
if (firstByte != 0)
twoBytesChars.add(firstByte);
}
return twoBytesChars;
}
private static void skipBytes(int byteCount, FileInputStream is) throws IOException
{
is.skip(byteCount);
/*for (int j = 0; j < byteCount; j++) {
is.read();
}*/
}
private static void copyBytes(int byteCount, FileInputStream is, FileOutputStream os) throws IOException
{
for (int j = 0; j < byteCount; j++) {
os.write(is.read());
}
}
static Map<String, Integer> buildEscapesMap()
{
Map<String, Integer> escapesMap = new HashMap();
List<String> escapes = Arrays.asList(
("Esc LShft RShft LCtrl RCtrl BckSp Del Home End Up Right Left Down Tab LAlt RAlt"
+ " E-6 E-7 E-8 E-9 E-10 E-71 E-72 E-73 #+1 #+2 #+3 #+4 #+5 #+6 #+7 #+8 #+9 #+57"
+ " F1 F2 F3 F4 F5 F6 F7 F8 F9 F10 F11 F12").split(" "));
for (String e : escapes) {
if (e.isBlank())
continue;
escapesMap.put(e, 0);
}
return escapesMap;
}
static Map<Integer, Character> buildCharsMap() {
Map<Integer, Character> charsMap = new HashMap();
// ---- UPPER CASE first, to override with lovercase below. ----
// <LShift>*
// 1234567890% je stejne
// Q W E R T Z U I O P / (
// 7F 09 51 57 45 52 54 5A 55 49 4F 70
charsMap.put(0x7F, 'Q'); // -nektere stejne jako mala pismena?
charsMap.put(0x09, 'W');
charsMap.put(0x51, 'E');
charsMap.put(0x57, 'R');
charsMap.put(0x45, 'T');
charsMap.put(0x52, 'Z');
charsMap.put(0x54, 'U');
charsMap.put(0x5A, 'I');
charsMap.put(0x55, 'O');
charsMap.put(0x49, 'P');
charsMap.put(0x4F, '/');
charsMap.put(0x70, '(');
// A S D F G H J K L " ! '
// 2F 28 C881 DC82 A S D F G H J 22
charsMap.put(0x2F, 'A');
charsMap.put(0x28, 'S');
charsMap.putIfAbsent(0xC881, 'D');
charsMap.putIfAbsent(0xDC82, 'F');
charsMap.put(0+'A', 'G');
charsMap.put(0+'S', 'H');
charsMap.put(0+'D', 'J');
charsMap.put(0+'F', 'K');
charsMap.put(0+'G', 'L');
charsMap.put(0+'H', '"');
charsMap.put(0+'J', '!');
charsMap.put(0x22, '\'');
// | Y X C V B N M ? : _
// C486 21 3B DC80 27 59 58 43 56 42 6E
charsMap.put(0xC486, '|');
charsMap.put(0x21, 'Y');
charsMap.put(0x3B, 'X');
charsMap.putIfAbsent(0xDC80, 'C');
charsMap.put(0x27, 'V');
charsMap.put(0x59, 'B');
charsMap.put(0x58, 'N');
charsMap.put(0x43, 'M');
charsMap.put(0x56, '?');
charsMap.put(0x42, ':');
charsMap.put(0x6E, '_');
// ---- LOWER CASE ----
// ; + ě š č ř ž ý á í é = | ´
// 2B 02 03 04 05 06 C3BD C3A1 C3AD C3A9 3D D081 |0A to je entr
// Asi posunuté?
// ; + ě š č ř ž ý á í é = | ´
// 2B 02 03 04 05 06 C3BD C3A1 C3AD C3A9 3D D081 nic // 0A je entr
charsMap.put(0x2B, '+');
charsMap.put(0x02, 'ě');
charsMap.put(0x03, 'š');
charsMap.put(0x04, 'č');
charsMap.put(0x05, 'ř');
charsMap.put(0x06, 'ž');
charsMap.put(0xC3BD, 'ý');
charsMap.put(0xC3A1, 'á');
charsMap.put(0xC3AD, 'í');
charsMap.put(0xC3A9, 'é');
charsMap.put(0x3D, '=');
charsMap.put(0xD081, '´');
// q w e r t z u i o p ú )
// 7F 09 71 77 65 72 74 7A 75 69 6F 70
charsMap.put(0x7F, 'q');
charsMap.put(0x09, 'w');
charsMap.put(0x71, 'e');
charsMap.put(0x77, 'r');
charsMap.put(0x65, 't');
charsMap.put(0x72, 'z');
charsMap.put(0x74, 'u');
charsMap.put(0x7A, 'i');
charsMap.put(0x75, 'o');
charsMap.put(0x69, 'p');
charsMap.put(0x6F, 'ú');
charsMap.put(0x70, ')');
// a s d f g h j k l ů § ¨
// C3BA 29 C881 DC82 61 73 64 66 67 68 6A 6C
charsMap.put(0xC3BA, 'a');
charsMap.put(0x29, 's');
//charsMap.put(0xC8, 'd');
charsMap.put(0xC881, 'd'); // Urcite
charsMap.put(0xDC82, 'f');
charsMap.put(0x61, 'g');
charsMap.put(0x73, 'h');
charsMap.put(0x64, 'j');
charsMap.put(0x66, 'k');
charsMap.put(0x67, 'l');
charsMap.put(0x68, 'ů');
charsMap.put(0x6A, '§');
charsMap.put(0x6C, '¨');
// \ y x c v b n m , . -
// C486 39 3B DC80 D084 79 78 63 76 62 6E
charsMap.put(0xC486, '\'');
charsMap.put(0x39, 'y');
charsMap.put(0x3B, 'x');
charsMap.put(0xDC80, 'c');
charsMap.put(0xD084, 'v');
charsMap.put(0x79, 'b');
charsMap.put(0x78, 'n');
charsMap.put(0x63, 'm');
charsMap.put(0x76, ',');
charsMap.put(0x62, '.');
charsMap.put(0x6E, '-');
return charsMap;
}
}
// ` 1 2 3 4 5 6 7 8 9 0 - =
// ; + ě š č ř ž ý á í é = | ´
// 2B 02 03 04 05 06 C3BD C3A1 C3AD C3A9 3D D081 |0A to je entr
// q w e r t z u i o p ú )
// 7F 09 71 77 65 72 74 7A 75 69 6F 70
// a s d f g h j k l ů § ¨
// C3BA 29 C881 DC82 61 73 64 66 67 68 6A 6C
// \ y x c v b n m , . -
// C486 39 3B DC80 D084 79 78 63 76 62 6E
// 1234567890% je stejne
// Q W E R T Z U I O P / (
// 7F 09 51 57 45 52 54 5A 55 49 4F 70
// A S D F G H J K L : " |
// 2F 28 C881 DC82 A S D F G H J 22
// | Y X C V B N M ? : _
// C486 21 3B DC80 27 59 58 43 56 42 6E
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment