#!/bin/bash read_input () { read -e -p "$cwd> " cmd args; } get_parent_dir () { local dir="$1" dir="${dir%/*}" if [ "x$dir" = "x" ]; then dir="/" fi echo "$dir" } is_dir () { local path="$1" if [ "x$path" = "x/" ]; then return 0 fi local parent="$(get_parent_dir "$path")" local dir="${path##*/}" wmiir read "$parent" 2>&1 | awk 'BEGIN{r=1} /^d/ && $NF == "'"$dir"'"{r=0; exit} END{exit(r)}' return $? } is_absolute () { [ "x$1" != "x" -a "x${1##/*}" == "x" ]; return $?; } absolute_path () { local path="$1" if ! is_absolute "$path"; then if [ "x$cwd" = "x/" ]; then path="/$path" else path="$cwd/$path" fi fi if [ "x$path" = "x/" ]; then echo "/" else echo "${path%/}" fi } search_dir () { local dir="${1%/}" local pattern="$2" wmiir read "${dir:-/}" | while read line; do name="$(echo "$line" | awk '{print $NF}')" if [ "x${name##$pattern}" = "x" ]; then echo "$dir/$name" fi if [ "x${line##d*}" = "x" ]; then search_dir "$dir/$name" "$pattern" fi done } echo "wmiish starting" if [ "$#" -gt 0 ]; then cwd="${1%/}" if [ "x$cwd" = "x" ]; then cwd="/" elif ! is_absolute "$1" || ! is_dir "$1"; then echo "$cwd: invalid directory" cwd="/" fi else cwd="/" fi while read_input; do case "$cmd" in ca*) path=$(absolute_path "$args") if is_dir "$path"; then echo "$path is a directory; try ls $args" else wmiir read "$path" [ "$?" == 0 ] && echo fi ;; cd) if [ "x$args" = "x/" ]; then new_cwd=/ elif is_absolute "$args"; then new_cwd="${args%/}" elif [ "x$args" = "x.." ]; then new_cwd="$(get_parent_dir "$cwd")" else new_cwd="${cwd%/}/${args%/}" fi if is_dir "$new_cwd"; then cwd="$new_cwd" else echo "$new_cwd: not a directory" fi ;; e*|q*) exit ;; f*) if [ "x$args" = "x" ]; then echo "usage: find " else search_dir "$cwd" "$args" fi ;; h*|\?) cat < CD ( | .. ) Exit Find Help Ls [ ] Pwd Quit Restart Set EOF ;; l*) path=$(absolute_path "$args") if is_dir "$path"; then wmiir read $(absolute_path "$args") else echo "$path is not a directory; try cat $args" fi ;; p*) echo "current directory is $cwd" ;; r*) exec $0 "$cwd" ;; s*) read var value < <(echo -n "$args") if [ "x$var" == "x" -o "x$value" == "x" ]; then echo "usage: set " else path=$(absolute_path "$var") echo -n "$value" | wmiir write "$path" fi ;; *) if [ "x$cmd" != "x" ]; then echo "$cmd: unknown command" >&2 fi ;; esac done