WebAssembly / WASI

WebAssembly System Interface
Other
4.72k stars 240 forks source link

Converting a little Bash script to WAT as an exercise #546

Open philiprhoades opened 11 months ago

philiprhoades commented 11 months ago

People,

I want to convert the little Bash script hereunder to WAT as an actuall useful exercise for me to getting started with WASM - the script simply counts the number of emails in a Maildir tree that are less than x minutes old and prints the numbers out for each of the dirs.

I presume I will need WASI for this exercise - pointers to any similar exercises would be helpful . .

Thanks,

Phil.

#!/bin/sh

# $1  host
# $2  minutes
# $3  x - extra flag to do all dirs

function proc_dir()
{
    if [ "$3" == "count" ]; then        # Show counts of mails of all dirs within minutes
        count=`find $1 -name "1*," -mmin -$2 | wc -l`

        if [ "$count" != "0" ]; then
            echo -n "$1: " | sed 's/^.*Maildir\/\.//'
            echo $count
        else    
            continue
        fi
    else                                # Shows Subjects of main dirs
        emails=`find $1/new $1/cur -name "1*," -mmin -$2 | sort -r`

        if [ "$emails" != "" ]; then
            for email in $emails
            do
                base=`echo $dir | sed 's/^.*\/\.//'`
                echo -n "${base}: "
                grep "^Subject: " $email | head -1 | sed 's/^Subject: //' | decode_utf8.pl
            done
        fi
    fi
}

if [ $1 == "localhost" ]; then
    maildir=$HOME/Maildir
else
    sshfs $1:/home/phr/Maildir /home/phr/md
    maildir=$HOME/md
fi

# echo $maildir

maindirs="
0_caa
0_caa_list
0_cryo
0_cryo_ssa-sc
0_naf
1_qps
1_qps_3-5_saje_crt"

if (( $# > 2 )); then                   # Do counts
#   dirs=`cd $maildir ; find . -exec basename {} \; -regextype grep -regex "^\.[0-9a-z][^.]+$ | sort" 
    dirs=`cd $maildir ; ls -1a | grep -E "^\.[0-9a-z][^.]+$"`

    for dir in $dirs
    do
        proc_dir $maildir/$dir $2 count
    done
else                                    # Get maindirs Subjects
    proc_dir $maildir $2

    for dir in $maindirs
    do
        proc_dir $maildir/.$dir $2
    done
fi

sleep 5

if [ $1 != "localhost" ]; then
    fusermount -u /home/phr/md
fi
sbc100 commented 11 months ago

bash scripts are not really the kind of thing that can be easily compiled to WebAssembly. As of today you are probably better choosing a project that is written in some language that does have good WebAssembly support such as C/C++ or rust.

If you are hoping to run your program in the browser you probably want to look at using emscripten to build your project... if you are planning on running your project on the server you probably want to use wasi-sdk. If you are targeting the web, remember that you don't have easy access the actual filesystem of either the server or the client, so Maildir processing might not be a good match.b

philiprhoades commented 11 months ago

bash scripts are not really the kind of thing that can be easily compiled to WebAssembly

Yep, worked that out!

some language that does have good WebAssembly support such as C

Yep, using C, converting to WASM then to WAT to see what it looks like . .

If you are hoping to run your program in the browser you probably want to look at using emscripten to build your project...

Yes, doing that as the first stage . .

if you are planning on running your project on the server you probably want to use wasi-sdk.

If I make some progress with the first stage, I will then look at that angle . .

so Maildir processing might not be a good match.

Yes, that could certainly be an issue . .

Thanks!

sbc100 commented 11 months ago

I don't think this issue is really WASI related so maybe we can close this?