EDIT: This script pauses the framework to prevent typing on the Kindle 3 keyboard from interacting with the framework (search bar). To exit the shell and resume the framework, type the "exit" command. If you run this script on a Kindle 4 or Touch, you will not be able to type an "exit" command, so you will need to resume the framework from SSH, or restart your kindle (hold power switch for 20 seconds). Do not panic just because the framework is paused. Just restart.
tinysh
UPDATE: This post was changed so the script code is now in a CODE box instead of a PHP box, which was hiding the "triple backslash" in the "eicho" function. It lost its pretty colors!
This is a tiny limited onscreen shell written in /bin/sh with minimal external linux command dependencies. This first version is only for the K3, but I plan to add support for onscreen keyboards for K4 and Touch. I have most of the code nearing completion for that. For now, this is just for the K3, and with a little work it should work on the DXG and earlier. It is severely limited by eips text support not allowing many characters needed in linux shell commands.
Although eips is rather slow on the K3, it is extremely fast on the K4 and Touch, even when scrolling text (so fast that you need to insert delays to not just have a blur of smeared text scrolling faster than you can see it). On the K3, you only get a couple of lines per second or so, but it is better than nothing until we get a faster and better eips replacement. It can be made several time faster by NOT scrolling the text, but instead adding "(Press any key to continue . . .)" and waiting for a key, then redrawing a new screen full of text.
Code:
#!/bin/sh
#===================================================
# tinysh - tiny limited onscreen shell (v1.0) for K3
#---------------------------------------------------
# Copyright (C) 2012 geekmaster
# MIT License: Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#===========================
# initvar - init global vars
#---------------------------
initvar() {
SYM=126 ENT=28 SHFT=42 SPC=57 DEL=14
DN=/dev/null
KN="1234567890____qwertyuiop____asdfghjkl_____zxcvbnm_."
KS="1234567890____QWERTYUIOP____ASDFGHJKL_____ZXCVBNM_,"
KP="1234567890____~@#$%^&*()____<>[]{}?/\_____;:'|-+=_?"
KM=$KN
B=" ";B="$B$B$B$B$B";B="$B$B"
CMD= # cmd line
}
#=============================================
# shcmd - emit shell command, preceeded by pwd
#---------------------------------------------
shcmd() {
echo "echo \"\$(pwd)> $CMD\";$CMD"
CMD=
}
#===================================================
# getkb - get translated ascii keys from K3 keyboard
#---------------------------------------------------
getkb() {
shcmd
while :;do
A="_"
set $(waitforkey); K=$1 D=$2
[[ $D -eq 0 ]]&& continue
[[ $K -eq $ENT ]]&& shcmd && continue
[[ $K -eq $SHFT ]]&& KM=$KS && continue
[[ $K -eq $SYM ]]&& KM=$KP && continue
[[ $K -eq $DEL ]]&& if [[ ${#CMD} -gt 1 ]];
then CMD=$(echo "$CMD"|cut -b-$((${#CMD}-1)))
else CMD=; fi
[[ $K -eq $SPC ]]&& A=" "
[[ $K -le 52 ]]&& A="$(echo \"$KM\"|cut -b$K-$K)"
[[ "$A" == "_" ]]&& A=""
CMD="$CMD$A"
eips "$(echo "~~~> $CMD$B"|cut -b-50)"
KM=$KN
done
}
#====================
# eicho - eink echo
# usage: eicho "text"
#--------------------
eicho() {
eips -z 2 38
eips 0 39 "$(echo "$1"|tr ";{}\\\`!#$%^" _|cut -b-50)"
}
initvar # init global vars
killall -stop cvm launchpad 2>$DN
eips -c;eips "~~~~~~~~~~~~ Geekmaster Tiny Shell 1.0 ~~~~~~~~~~~"
getkb|sh 2>&1|while read l;do eicho "$l";done
killall -cont cvm launchpad 2>$DN
This is really limited by eips, which has a lot of characters it refuses to display (especially characters useful for linux commands). You can do simple commands like "pwd", "cd /etc", and "cat shadow", but good luck blind-typing the characters that do not echo to the display.
To improve this, we need to eliminate the dependency on eips for text output. I plan to do graphics blitting (block image copy) from an image of characters using "dd", for graphics text output. Or using Hershey vector fonts, if I finish that first. I plan to start with Hershey Simplex characters -- other language support can be added later.
This only works on the K3, but should be easy to adapt to DX and earlier. I even abstracted the keycodes out to initvar so they are easier to change.
Be sure your kindle is ON for the keyboard to work. If the keyboard is not echoing keys to the eink display, slide your power switch to wake your kindle and unlock the keyboard, then try again.
The "Sym" key is used as a symbol shift key, and all symbols on a standard keyboard can be created on the K3 keyboard except the underscore and double-quote keys (see the KP string assignment in initvar below), but those can be supported too with a little additional code. This script is really just a proof-of-concept example, for educational purposes to show how you can do interesting stuff with shell scripts.
To avoid confusion if you plan to modify this, "|while .. read .. do" loop contents run in a separate process space that uses a COPY of the shell variables. Code outside the loop cannot see changes to values inside the loop. You can only pass STDOUT to code outside the loop. That goes for functions called from inside the loop too. You can pass multiple values with STDOUT then extract them with "set" (like I did here with the "waitforkey" call).
EDIT: This could be used in a RUNME.sh script to provide a simple onscreen linux command shell for debricking. You can type (and use) characters that you cannot see (eips limitation), which is what I called "blind-typing" above.
Enjoy!
EDIT: This script pauses the framework to prevent typing on the Kindle 3 keyboard from interacting with the framework (search bar). To exit the shell and resume the framework, type the "exit" command. If you run this script on a Kindle 4 or Touch, you will not be able to type an "exit" command, so you will need to resume the framework from SSH, or restart your kindle (hold power switch for 20 seconds). Do not panic just because the framework is paused. Just restart.