A2osX/BIN/SH.S.txt
2019-03-08 16:36:21 +00:00

729 lines
13 KiB
Plaintext
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

NEW
PREFIX
AUTO 4,1
.LIST OFF
.OP 65C02
.OR $2000
.TF BIN/SH
*--------------------------------------
History.MAX .EQ 256
CORE.STACK.MAX .EQ 64
PUSHD.STACK.MAX .EQ 8
*--------------------------------------
C.NOHUP .EQ 0
C.CD .EQ 2
C.DATE .EQ 4
C.ECHO .EQ 6
C.EXIT .EQ 8
C.GETKEY .EQ 10
C.MD .EQ 12
C.PAUSE .EQ 14
C.PWD .EQ 16
C.RD .EQ 18
C.READ .EQ 20
C.REN .EQ 22
C.SET .EQ 24
C.SLEEP .EQ 26
C.SHIFT .EQ 28
C.TIME .EQ 30
C.PUSHD .EQ 32
C.POPD .EQ 34
C.FUNC .EQ 36
C.RETURN .EQ 38
C.SWITCH .EQ 40
C.CASE .EQ 42
C.DEFAULT .EQ 44
C.BREAK .EQ 46
C.END .EQ 48
*--------------------------------------
C.FOR .EQ 50
C.FOR.LIST .EQ 0
C.FOR.FILE .EQ 2
C.FOR.EXEC .EQ 4
C.WHILE .EQ 52
C.IF .EQ 54
C.NEXT .EQ 56
C.LOOP .EQ 58
C.ELSE .EQ 60
C.FI .EQ 62
*--------------------------------------
E.CSYN .EQ $C0
E.ESYN .EQ $C1
E.STACKERROR .EQ $C2
E.STKOVERFLOW .EQ $C3
E.NOWHILE .EQ $C4
E.NOFOR .EQ $C5
E.NOIF .EQ $C6
E.BADEXP .EQ $C7
E.BADFTYPE .EQ $C8
*--------------------------------------
.INB INC/MACROS.I
.INB INC/A2OSX.I
.INB INC/MLI.I
.INB INC/MLI.E.I
*--------------------------------------
.DUMMY
.OR ZPBIN
ZS.START
ZPCLBuf .BS 2
ZPInputBuf .BS 2
ZPInputBufPtr .BS 2
ZPInputCmdPtr .BS 2
ZPArgVBuf .BS 2
ZPArgVBufPrev .BS 2
ZPArgVBufPtr .BS 2
ZPVarNamePtr .BS 2
ZPPtr1 .BS 2
ZPPtr2 .BS 2
TmpWord .BS 2
TmpByte .BS 1
ZS.END .ED
*--------------------------------------
* File Header (16 Bytes)
*--------------------------------------
CS.START cld
jmp (.1,x)
.DA #$61 6502,Level 1 (65c02)
.DA #1 BIN Layout Version 1
.DA #S.PS.F.EVENT S.PS.F
.DA #0
.DA CS.END-CS.START Code Size (without Constants)
.DA DS.END-DS.START Data SegmentSize
.DA #64 Stack Size
.DA #ZS.END-ZS.START Zero Page Size
.DA 0
*--------------------------------------
* Relocation Table
*--------------------------------------
.1 .DA CS.INIT
.DA CS.RUN
.DA CS.DOEVENT
.DA CS.QUIT
*--------------------------------------
L.MSG.GREETINGS .DA MSG.GREETINGS
L.MSG.PROMPT .DA MSG.PROMPT
L.MSG.TRACE .DA MSG.TRACE
L.MSG.ECHOCRLF .DA MSG.ECHOCRLF
L.MSG.BATCHERR .DA MSG.BATCHERR
L.MSG.ERROR .DA MSG.ERROR
L.MSG.PRINTENV .DA MSG.PRINTENV
L.MSG.PID .DA MSG.PID
L.ENV.ROOT .DA ENV.ROOT
L.ENV.PATH .DA ENV.PATH
L.ENV.PWD .DA ENV.PWD
L.ENV.PS1 .DA ENV.PS1
L.ENV.HOME .DA ENV.HOME
L.FMT.DATE .DA FMT.DATE
L.FMT.TIME .DA FMT.TIME
L.FMT.GETKEY .DA FMT.GETKEY
J.ESC .DA CL.BS left arrow
.DA HIS.GetNext
.DA HIS.GetPrev
.DA CL.NAK right arow
L.CMD .DA CMD
J.CMD .DA CMD.NOHUP
.DA CMD.CD
.DA CMD.DATE
.DA CMD.ECHO
.DA CMD.EXIT
.DA CMD.GETKEY
.DA CMD.MD
.DA CMD.PAUSE
.DA CMD.PWD
.DA CMD.RD
.DA CMD.READ
.DA CMD.REN
.DA CMD.SET
.DA CMD.SLEEP
.DA CMD.SHIFT
.DA CMD.TIME
.DA CMD.PUSHD
.DA CMD.POPD
.DA CMD.FUNC
.DA CMD.RETURN
.DA CMD.SWITCH
.DA CMD.CASE
.DA CMD.DEFAULT
.DA CMD.BREAK
.DA CMD.END
.DA CMD.FOR
.DA CMD.WHILE
.DA CMD.IF
.DA CMD.NEXT
.DA CMD.LOOP
.DA CMD.ELSE
.DA CMD.FI
J.NEXT .DA CMD.NEXT.LIST
.DA CMD.NEXT.FILE
.DA CMD.NEXT.EXEC
L.CORE.IO .DA CORE.IO
J.CORE.IO .DA CORE.IO.AMP
.DA CORE.IO.IN
.DA CORE.IO.OUTA
.DA CORE.IO.OUT
.DA CORE.IO.1OUTA
.DA CORE.IO.1OUT
.DA CORE.IO.2OUTA
.DA CORE.IO.2OUT
L.EXP.BEGIN .DA EXP.BEGIN
L.EXP.OP.UNARY .DA EXP.OP.UNARY
J.EXP.OP.UNARY .DA EXP.OP.UNARY.D
.DA EXP.OP.UNARY.E
.DA EXP.OP.UNARY.F
.DA EXP.OP.UNARY.N
.DA EXP.OP.UNARY.Z
L.EXP.OP.BINARY .DA EXP.OP.BINARY
L.EXP.OP.MATH .DA EXP.OP.MATH
J.EXP.OP.MATH .DA M32.Add
.DA M32.Sub
.DA M32.Mul
.DA M32.Div
.DA M32.Mod
L.HOME.PROFILE .DA HOME.PROFILE
L.M32.Printf .DA M32.Printf
L.ERR.Codes .DA ERR.Codes
L.ERR.Messages .DA ERR.Messages
L.NOD.Template .DA NOD.Template
.DA 0
*--------------------------------------
CS.INIT clc
CS.INIT.RTS rts
*--------------------------------------
CS.RUN jsr CL.Init
jsr CORE.Init
ldy #S.PS.ARGC
lda (pPS),y
beq .1 no arg, continue starting interactive
>PUSHWI 0 Aux type
>PUSHBI S.FI.T.TXT
>PUSHBI O.RDONLY
lda #1
>SYSCALL ArgV
>SYSCALL LoadTxtFile
bcs CS.INIT.RTS
txa
>STA.G hInputBuf
>SYSCALL GetMemPtr
>STYA ZPInputBuf
>STYA ZPInputBufPtr
lda #0
jsr CMD.SHIFTA Remove $0=/bin/sh
lda #$ff
>STA.G bBatchMode
bra CS.RUN.LOOP
*--------------------------------------
.1 jsr CMD.CD.HOME
jsr HIS.Init
bcs CS.INIT.RTS
>PUSHWI K.VER
>PUSHBI 2
>LDYA L.MSG.GREETINGS
>SYSCALL printf
bcs CS.INIT.RTS
>PUSHEA.G StatBuf
>LDYA L.HOME.PROFILE
>SYSCALL STAT
bcs CS.RUN.LOOP
>PUSHBI S.PS.F.HOLD
>LDYA L.HOME.PROFILE
>SYSCALL execl
*--------------------------------------
CS.RUN.LOOP >SLEEP
.1 jsr CheckSleep
beq .3
ldy #S.PS.hStdIn
lda (pPS),y
>SYSCALL feof
bcs .9
tay
beq CS.RUN.LOOP
>SYSCALL GetChar
bcs .9 I/O err
cmp #3 Ctrl-C
bne CS.RUN.LOOP
>LDA.G bBatchMode batch mode ?
beq CS.RUN.LOOP
>LDA.G bSET.C
beq .8
bra CS.RUN.LOOP
.3 >LDA.G bPause
bpl .6
.4 >SYSCALL GetChar
bcs .9 I/O err
.5 cmp #3 Ctrl-C
bne .60
>LDA.G bBatchMode batch mode ?
beq .61
>LDA.G bSET.C
beq .8
bra .61
.60 cmp #C.CR
bne .4
.61 >STZ.G bPause
bra .62
.6 >LDA.G bReadMode read mode ?
bne .7
.62 >LDA.G bBatchMode batch mode ?
beq CS.RUN.INTERACTIVE
jmp CS.RUN.BATCH
.7 jmp CS.RUN.READ
.8 lda #3 User Interrupt
sec
.9 rts
*--------------------------------------
CS.RUN.INTERACTIVE
>LDA.G hInputBuf some commmands left in buffer
beq .10
lda (ZPInputBufPtr)
bne .6
>LDA.G hInputBuf
>SYSCALL freemem
>STZ.G hInputBuf
.10 jsr IO.Reset
jsr CL.Reset
jsr CL.PrintPrompt
bcs .9
.1 >SYSCALL GetChar
bcs .9 I/O error
.2 cmp #C.EOF ....or Ctrl-D
beq .9 CS
jsr CL.CHARIN
>LDA.G CL.bReady Something to execute ?
bpl .1
>PUSHBI 0
>LDYA L.MSG.ECHOCRLF
>SYSCALL printf
bcs .9
lda (ZPCLBuf)
beq .8 Empty line
jsr HIS.Add
>LDYA ZPCLBuf
>SYSCALL strdup
bcs .8
>STYA ZPInputBuf
>STYA ZPInputBufPtr
txa
>STA.G hInputBuf
.6 jsr CORE.Run
.8 jmp CS.RUN.LOOP.END
.9 rts
*--------------------------------------
CS.RUN.READ jsr CL.RESET
.1 >SYSCALL GetChar
bcs CS.RUN.READ.9
jsr CL.CHARIN
>LDA.G CL.bReady
bpl .1
lda (ZPCLBuf)
bne .2
>LDYA ZPVarNamePtr
>SYSCALL UnSetEnv
bra .7
.2 >PUSHW ZPCLBuf
>LDYA ZPVarNamePtr
>SYSCALL SetEnv
.7 php
pha
>STZ.G bReadMode
>STA.G bREAD.S
pla
plp
.8 jmp CS.RUN.LOOP.END
CS.RUN.READ.9 rts
*--------------------------------------
CS.RUN.BATCH
* ldy #S.PS.hStdIn
* lda (pPS),y
* >SYSCALL feof
* bcs .2 unsupported
* tay
* beq .2
* >SYSCALL GetChar
* bcs CS.RUN.READ.9
* cmp #3 test Ctrl-c
* bne .1
* sec
* rts
*.1 cmp #19 test Ctrl-s
* bne .2
* lda #$80
* >STA.G bPause
* clc
* jmp CS.RUN.LOOP.END
.2 >LDA.G bSET.X
beq .5
ldy #$ff
.3 iny
lda (ZPInputBufPtr),y
sta (ZPArgVBuf),y
beq .4
eor #C.CR
bne .3
sta (ZPArgVBuf),y
.4 >PUSHW ZPArgVBuf
>PUSHBI 2
>LDYA L.MSG.TRACE
>SYSCALL printf
bcs CS.RUN.LOOP.RTS
.5 jsr CORE.Run
bcc CS.RUN.LOOP.END
tay 0 = EOF
bne CS.RUN.LOOP.END
pha
lda #$ff
>STA.G bExit
pla
CS.RUN.LOOP.END ldy #S.PS.RC
sta (pPs),y
bcc CS.RUN.LOOP.8
tay
beq CS.RUN.LOOP.8
>LDA.G bBatchMode batch mode ?
beq .1
jsr IO.PrintBatchErrMsg
bcs CS.RUN.LOOP.RTS
.1 ldy #S.PS.RC
lda (pPs),y
jsr IO.PrintErrMsg
bcs CS.RUN.LOOP.RTS
CS.RUN.LOOP.8 >LDA.G bExit
beq CS.RUN.LOOP.80
ldy #S.PS.RC
lda (pPs),y
sec
CS.RUN.LOOP.RTS rts
CS.RUN.LOOP.80 jmp CS.RUN.LOOP
*--------------------------------------
CS.DOEVENT lda (pEvent)
bpl .9
jsr CheckSleep
beq .9
* ldy #Sleep already set by CheckSleep
ldx #4
clc
.1 lda (pData),y
sbc #0
sta (pData),y
iny
dex
bne .1
.9 sec
rts
*--------------------------------------
CS.QUIT >LDA.G hInputBuf
beq .1
>SYSCALL FreeMem
.1 jsr HIS.Quit
jsr CL.Quit
jsr CORE.Quit
clc
rts
*--------------------------------------
CheckSleep ldy #Sleep+3
lda (pData),y
dey
ora (pData),y
dey
ora (pData),y
dey
ora (pData),y
rts
*--------------------------------------
RemoveStrFromArgV
>STYA ZPPtr1
>STYA ZPPtr2
ldy #$ff
.1 iny Compute ArgX len
lda (ZPPtr1),y
bne .1
.2 tya
sec
adc ZPPtr1
sta ZPPtr1
bcc .3
inc ZPPtr1+1
.3 ldy #$ff
.4 iny
lda (ZPPtr1),y
sta (ZPPtr2),y
bne .4
tya
beq .8
sec
adc ZPPtr2
sta ZPPtr2
bcc .2
inc ZPPtr2+1
bra .2
.8 rts
*--------------------------------------
ToUpperCase cmp #'a'
bcc .8
cmp #'z'+1
bcs .8
eor #$20
.8 clc exit CC to allow Jmp to
rts
*--------------------------------------
.INB USR/SRC/BIN/SH.S.CL
.INB USR/SRC/BIN/SH.S.HIS
.INB USR/SRC/BIN/SH.S.CORE
.INB USR/SRC/BIN/SH.S.IO
.INB USR/SRC/BIN/SH.S.CMD
.INB USR/SRC/BIN/SH.S.EXP
*--------------------------------------
CS.END
*--------------------------------------
MSG.GREETINGS .AZ "\r\nA2osX-SH %d.%d\r\n\r\n"
MSG.PROMPT .AZ "\e[7h$ "
MSG.TRACE .AZ ">%s\r\n"
MSG.BATCHERR .AZ "^\r\nLine #%D:"
MSG.ERROR .AS "[$%h]:%S."
MSG.ECHOCRLF .AZ "\r\n"
MSG.PRINTENV .AZ "%s = %s\r\n"
MSG.PID .AZ "PID=%d\r\n"
FMT.DATE .AZ "%A (%w), %B %d %Y"
FMT.TIME .AZ "%H:%M:%S (%I:%M:%S%p)"
FMT.GETKEY .AZ "%d"
*--------------------------------------
ENV.ROOT .AZ "ROOT"
ENV.PATH .AZ "PATH"
ENV.PWD .AZ "PWD"
ENV.PS1 .AZ "PS1"
ENV.HOME .AZ "HOME"
HOME.PROFILE .AZ "${HOME}PROFILE"
*--------------------------------------
CMD .AZ "NOHUP"
.AZ "CD"
.AZ "DATE"
.AZ "ECHO"
.AZ "EXIT"
.AZ "GETKEY"
.AZ "MD"
.AZ "PAUSE"
.AZ "PWD"
.AZ "RD"
.AZ "READ"
.AZ "REN"
.AZ "SET"
.AZ "SLEEP"
.AZ "SHIFT"
.AZ "TIME"
.AZ "PUSHD"
.AZ "POPD"
.AZ "FUNC"
.AZ "RETURN"
.AZ "SWITCH"
.AZ "CASE"
.AZ "DEFAULT"
.AZ "BREAK"
.AZ "END"
.AZ "FOR"
.AZ "WHILE"
.AZ "IF"
.AZ "NEXT"
.AZ "LOOP"
.AZ "ELSE"
.AZ "FI"
.HS 00
*--------------------------------------
* https://www.tldp.org/LDP/abs/html/io-redirection.html
*--------------------------------------
CORE.IO .AZ "&"
.AZ "<"
.AZ ">>"
.AZ ">"
.AZ "1>>"
.AZ "1>"
.AZ "2>>"
.AZ "2>"
.HS 00
*--------------------------------------
EXP.BEGIN .AZ "!["
.AZ "["
.HS 00
EXP.OP.UNARY .AZ "-D"
.AZ "-E"
.AZ "-F"
.AZ "-N"
.AZ "-Z"
.HS 00
EXP.OP.BINARY .AZ "="
.AZ "!="
.AZ "-EQ"
.AZ "-NE"
.AZ "-LT"
.AZ "-LE"
.AZ "-GT"
.AZ "-GE"
.HS 00
EXP.OP.BINARY.BITS
.DA #%000
.DA #%000
.DA #%010
.DA #%101
.DA #%100
.DA #%110
.DA #%001
.DA #%011
EXP.OP.MATH .AZ "+"
.AZ "-"
.AZ "*"
.AZ "div"
.AZ "mod"
.HS 00
*--------------------------------------
.INB USR/SRC/X.M32.S
.INB USR/SRC/X.ERRORS.S
*--------------------------------------
EscChars .AS 'DBAC'
EscChars.Cnt .EQ *-EscChars
NOD.Template .AZ "/DEV/PIPE%h"
M32.Printf .AZ "%L"
*--------------------------------------
.DUMMY
.OR 0
DS.START
CORE.STACK .BS CORE.STACK.MAX+1 pData (0 = Stack Ptr)
PUSHD.STACK .BS PUSHD.STACK.MAX+1
hInputBuf .BS 1
bReadMode .BS 1
bBatchMode .BS 1
bEscMode .BS 1
bPause .BS 1
bExit .BS 1
bSET.C .BS 1
bSET.X .BS 1
bREAD.S .BS 1
Sleep .BS 4
StatBuf .BS S.STAT
TimeBuf .BS S.TIME
CL.hCLBuf .BS 1
CL.Ptr .BS 1
CL.Len .BS 1
CL.bReady .BS 1
CORE.IntCmd .BS 1
CORE.hArgVBuf .BS 1
CORE.ArgIndex .BS 1
CORE.Test .BS 1
CORE.Operator .BS 1
CORE.PSFlags .BS 1
HIS.hBuf .BS 1
HIS.Count .BS 1
HIS.Index .BS 1
IO.hIn .BS 1
IO.hOut .BS 1
IO.hErr .BS 1
IO.hPipe .BS 1
IO.NodBuf .BS 12 /DEV/PIPExx\0
*--------------------------------------
M32.BUF .BS 12 -1234567890\0
M32.ACC .BS 4
M32.ARG .BS 4
M32.TMP .BS 4
DS.END .ED
*--------------------------------------
MAN
SAVE USR/SRC/BIN/SH.S
ASM