Annotation of hatari/tools/hatari-prg-args.sh, revision 1.1.1.3

1.1       root        1: #!/bin/sh
                      2: #
1.1.1.3 ! root        3: # script to set Atari program args after TOS starts it.
1.1       root        4: #
1.1.1.3 ! root        5: # this is achieved by chaining breakpoints from Hatari startup
        !             6: # to program startup, and finally writing the arguments to
        !             7: # the program basepage.
1.1       root        8: 
1.1.1.3 ! root        9: set -e
1.1       root       10: 
1.1.1.3 ! root       11: # name or path to hatari Mercurial version
        !            12: hatari=hatari
1.1       root       13: 
                     14: usage ()
                     15: {
                     16:        name=${0##*/}
1.1.1.3 ! root       17:        echo "usage: $name [-q] [Hatari args] -- <Atari program> <program args>"
1.1       root       18:        echo
                     19:        echo "Script for running Hatari so that it autostarts given Atari program"
                     20:        echo "and inserts given arguments to program (to its basepage, with builtin"
                     21:        echo "Hatari debugger facilities)."
                     22:        echo
1.1.1.3 ! root       23:        echo "Options:"
        !            24:        echo "    -q  Quit Hatari after Atari program exits"
        !            25:        echo
1.1       root       26:        echo "If arguments have same (host) path prefix as given Atari program,"
                     27:        echo "those prefixes are replaced with 'C:\', Unix path separators ('/')"
                     28:        echo "are replaced with Atari ones ('\\') and whole filename upper-cased."
                     29:        echo
                     30:        echo "Other arguments are passed unmodified."
                     31:        echo
1.1.1.3 ! root       32:        echo "Examples:"
        !            33:        echo
        !            34:        echo "* program in host folder and input files in same folder:"
1.1       root       35:        echo "  $name -m -- atari/sidplay.ttp atari/sids/test.sid"
                     36:        echo
1.1.1.3 ! root       37:        echo "* program on a disk image:"
        !            38:        echo "  $name -m -- 'A:\\SIDPLAY.TTP' 'SIDS\\TEST.SID'"
        !            39:        echo
        !            40:        echo "Note: argument quoting is needed with '\\' chars."
        !            41:        echo
1.1       root       42:        echo "ERROR: $1!"
                     43:        exit 1
                     44: }
                     45: 
                     46: # --------- argument parsing --------
                     47: 
                     48: # generic argument checking
                     49: if [ $# -lt 3 ]; then
                     50:        usage "not enough arguments"
                     51: fi
                     52: echo " $* " | grep -q ' -- '
                     53: if [ $? -ne 0 ]; then
                     54:        usage "Separator missing for Hatari -- Atari options"
                     55: fi
                     56: 
1.1.1.3 ! root       57: # quiet option comes first
        !            58: quit=0
        !            59: if [ $1 = "-q" ]; then
        !            60:        quit=1
        !            61:        shift
        !            62: fi
        !            63: 
1.1       root       64: # collect/remove hatari arguments
1.1.1.3 ! root       65: hargs=""
1.1       root       66: for arg in $*; do
                     67:        shift
                     68:        if [ $arg = '--' ]; then
                     69:                break
                     70:        fi
1.1.1.3 ! root       71:        hargs="$hargs $arg"
1.1       root       72: done
                     73: 
                     74: # check atari program
                     75: prg=$1
                     76: shift
                     77: if [ \! -f $prg ]; then
1.1.1.3 ! root       78:        if [ "${prg#[A-Z]:}" != "$prg" ]; then
        !            79:                # program path inside disk image
        !            80:                hargs="--auto $prg $hargs"
        !            81:                prg=""
        !            82:        else
        !            83:                # non-existing *non-Atari* path given
        !            84:                usage "given Atari program '$prg' doesn't exist"
        !            85:        fi
1.1       root       86: fi
                     87: 
                     88: # builtin shell echo can be broken, so that it interprets backlashes by default,
                     89: # i.e. concatenating '\' path separator with dirname 't', produces TAB, not '\t'...
                     90: # -> use separate echo binary
                     91: echo=$(which echo)
                     92: 
1.1.1.3 ! root       93: # temporary dir for debugger scripts
        !            94: dir=$(mktemp -d)
1.1       root       95: 
                     96: # collect/convert atari program arguments
                     97: args=$dir/args
                     98: rm -f $args
                     99: touch $args
                    100: prefix=""
                    101: drive="C:\\"
                    102: path="${prg%/*}/"
                    103: for arg in $*; do
                    104:        if [ "${arg#$path}" != "$arg" ]; then
                    105:                # file path needing conversion
                    106:                if [ \! -f $arg ]; then
                    107:                        usage "given file name '$arg' doesn't exits"
                    108:                fi
                    109:                # prefix with separator & drive letter & remove host path,
                    110:                # upper-case, convert path separators
                    111:                $echo -n "${prefix}${drive}${arg#$path}" | tr a-z A-Z | tr '/' '\\' >> $args
                    112:        else
                    113:                $echo -n "${prefix}$arg" >> $args
                    114:        fi
                    115:        prefix=" "
                    116:        shift
                    117: done
                    118: 
1.1.1.2   root      119: # calculate command line length and append zero just in case
1.1       root      120: cmdlen=$(wc -c $args | awk '{print $1}')
1.1.1.3 ! root      121: printf "\0" >> $args
1.1       root      122: if [ $cmdlen -gt 126 ]; then
                    123:        usage "command line is too long, $cmdlen chars (basepage limit is 126)"
                    124: fi
                    125: 
                    126: # --------- debugger scripts --------
                    127: 
1.1.1.3 ! root      128: # until TOS has started, it's not possible to set breakpoint
        !           129: # to program startup, there's no valid basepage and therefore
        !           130: # no valid TEXT variable.  So set breakpoint on Pexec(0,...)
        !           131: cat > $dir/pexec.ini << EOF
        !           132: b GemdosOpcode = 0x4B && OsCallParam = 0x0 :once :quiet :trace :file $dir/start.ini
1.1       root      133: EOF
                    134: 
                    135: # real work can be done when program starts,
                    136: # i.e. PC is at TEXT section beginning
                    137: cat > $dir/start.ini << EOF
                    138: b pc = text :once :trace :quiet :file $dir/basepage.ini
                    139: EOF
                    140: 
1.1.1.3 ! root      141: # add command line args args to program basepage
1.1       root      142: cat > $dir/basepage.ini << EOF
                    143: setopt dec
                    144: w "basepage+0x80" $cmdlen
                    145: l $args "basepage+0x81"
1.1.1.3 ! root      146: # info basepage
1.1       root      147: EOF
                    148: 
1.1.1.3 ! root      149: # quit Hatari after program exits
        !           150: if [ $quit -eq 1 ]; then
        !           151:        # catch Pterm0() & Pterm()
        !           152:        cat >> $dir/basepage.ini << EOF
        !           153: b GemdosOpcode = 0x00 :quiet :trace :file $dir/quit.ini
        !           154: b GemdosOpcode = 0x4C :quiet :trace :file $dir/quit.ini
        !           155: EOF
        !           156:        echo "quit" > $dir/quit.ini
        !           157: fi
        !           158: 
1.1       root      159: # --------- ready, run ----------
                    160: 
                    161: # show args & debugger scripts
                    162: head $dir/*
                    163: 
                    164: # run Hatari
1.1.1.3 ! root      165: echo $hatari --parse $dir/pexec.ini $hargs $prg
        !           166: $hatari --parse $dir/pexec.ini $hargs $prg
1.1       root      167: 
                    168: # cleanup
                    169: rm -r $dir

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.