A2osX/SBIN/SHELL.S.CMD.txt
2017-09-27 17:40:31 +02:00

682 lines
12 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.

PR#3
PREFIX /A2OSX.BUILD
NEW
INC 1
AUTO 6
.LIST OFF
*--------------------------------------
CMD.Init >LDYAI CmdLine.MAX+1
>SYSCALL GetMem.YA
bcs .9
>STYA ZPCMDBuf
txa
>STA.G hCmdBuf
.9 rts
*--------------------------------------
CMD.Quit >LDA.G hCmdBuf
beq .9
>SYSCALL FreeMem.A
.9 rts
*--------------------------------------
* ZPCMDBuf : Remove extra SPACE & comment
*--------------------------------------
Cmd.Parse >LDYA ZPCMDBuf
>STYA ZPPtr1
ldy #0
lda (ZPCMDBuf) empty line...quit
beq .9
.1 jsr Cmd.Parse.NextC
beq .8 no more char, exit
cmp #' ' skip leading spaces
beq .1
cmp #'#' Comment ?
beq .8 ignore remaining chars
.3 sta (ZPCMDBuf),y add char to buffer
iny
jsr Cmd.Parse.NextC
beq .8
cmp #' '
bne .3
sta (ZPCMDBuf),y add One SPACE to buffer
iny
.5 jsr Cmd.Parse.NextC
beq .8
cmp #' '
beq .5 skip additional spaces
bne .3 no more space, add next word...
.8 lda #0
sta (ZPCMDBuf),y
.9 tya
>STA.G CmdBuflen
rts
Cmd.Parse.NextC lda (ZPPtr1)
beq .8
inc ZPPtr1
bne .8
inc ZPPtr1+1 never Z
.8 rts
*--------------------------------------
* ZPCMDBuf-> Command line (formatted & not empty)
*--------------------------------------
Cmd.Exec lda #0
>STA.G CMD.bStartProc
>STA.G CMD.hFullpath
>LDYA ZPCMDBuf
>SYSCALL ExpandStr.YA
>STYA ZPPtr1 Ptr to CMD ARGS
txa
>STA.G CMD.hCmdLine
>LDYA L.INTCMDS
>STYA ZPPtr2
ldx #0
.3 lda (ZPPtr2)
beq Cmd.Exec.EXT Array Ending 0, must be an external Cmd....
ldy #$ff
.4 iny
lda (ZPPtr2),y
bne .41 End of string in array
lda (ZPPtr1),y Get CMD char...
beq .8 both end with 0, found!
cmp #' '
beq .8 we reached space between CMD & ARGS....
.41 lda (ZPPtr1),y Get CMD char...
beq .61 end of CMD.....too short...
cmp #' '
beq .61 we reached space between CMD & ARGS....
cmp #'a' To Uppercase
bcc .5
cmp #'z'+1
bcs .5
eor #$20
.5 cmp (ZPPtr2),y
beq .4
.61 iny
lda (ZPPtr2),y
bne .61
.7 inx
inx
tya
sec
adc ZPPtr2
sta ZPPtr2
bcc .3
inc ZPPtr2+1
bra .3
.8 jsr Cmd.Exec.Split X unmodified
jmp (J.INTCMDS,x) Found an internal Cmd...
*--------------------------------------
Cmd.Exec.Done php
pha
>LDA.G CMD.hCmdLine
>SYSCALL FreeMem.A
>LDA.G CMD.hFullpath
beq .1
>SYSCALL FreeMem.A
.1 pla
plp
rts
*--------------------------------------
Cmd.Exec.EXT jsr Cmd.Exec.Split
lda (ZPPtr1)
cmp #'/' Command line is already full path, no search
bne .1
>PUSHEA.G CMD.Stat
>PUSHW ZPPtr1
>SYSCALL STAT
bcs Cmd.Exec.Done
>LDYA ZPPtr1
bra .4
.1 >LDYA L.ENV.PATH push ENVNAME=PATH for search
>SYSCALL GetEnv.YA get value for ENV=PATH
bcs .2 No PATH, try in CD
jsr Cmd.Exec.EXT.Search
bcc .3
.2 ldy #S.PS.hPREFIX not found, try in CD
lda (pPs),y
>SYSCALL GetMemPtr.A
jsr Cmd.Exec.EXT.Search
bcs .99
.3 >LDYA ZPCMDBuf
.4 >SYSCALL NewStr.YA
bcs .99
txa
>STA.G CMD.hFullpath
ldy #CMD.Stat+S.STAT.P.TYPE
lda (pData),y
cmp #$04 TXT File ?
beq Cmd.Exec.EXT.TXT
cmp #$06 BIN File ?
beq Cmd.Exec.EXT.BIN
cmp #$ff SYS File ?
bne .9
bra *
.9 lda #SYSMGR.ERRSYN
sec
.99 jmp Cmd.Exec.Done
*--------------------------------------
* TXT : Launch "/PATH/SHELL /PATH/CMD ARGS"
*--------------------------------------
Cmd.Exec.EXT.TXT
lda #0 Get arg[0] = /PATH/SHELL
>SYSCALL GetArg.A
>PUSHYA
>PUSHW ZPCMDBuf
>SYSCALL StrCpy
jsr Cmd.Exec.EXT.SEP Add a space....
>LDA.G CMD.hFullpath
>SYSCALL GetMemPtr.A
>PUSHYA
>PUSHW ZPCMDBuf
>SYSCALL StrCat
>LDYA ZPCMDBuf
>SYSCALL PrintF.YA
lda #'!'
>SYSCALL PutChar.A
lda #13
>SYSCALL PutChar.A
lda #10
>SYSCALL PutChar.A
*--------------------------------------
* BIN : Launch "/PATH/CMD ARGS"
*--------------------------------------
Cmd.Exec.EXT.BIN
lda (ZPPTR2) Some ARGS ?
beq .1
jsr Cmd.Exec.EXT.SEP Add a space....
>PUSHW ZPPTR2
>PUSHW ZPCMDBuf
>SYSCALL StrCat
>LDYA ZPCMDBuf
>SYSCALL PrintF.YA
lda #'!'
>SYSCALL PutChar.A
lda #13
>SYSCALL PutChar.A
lda #10
>SYSCALL PutChar.A
.1 >LDA.G CMD.bStartProc
asl
>LDYA ZPCMDBuf
bcs .2 startproc
>SYSCALL ExecProcessNewEnvYA
jmp Cmd.Exec.Done
.2 >SYSCALL CreateProcessYA
jmp Cmd.Exec.Done
*--------------------------------------
Cmd.Exec.EXT.Search
>PUSHYA push search list
>PUSHW ZPPtr1 push CMD
>PUSHW ZPCMDBuf push dest full path
>PUSHEA.G CMD.Stat
>SYSCALL FileSearch
rts
*--------------------------------------
Cmd.Exec.EXT.SEP
ldy #$ff
.1 iny
lda (ZPCMDBuf),y
bne .1
lda #' '
sta (ZPCMDBuf),y
iny
lda #0
sta (ZPCMDBuf),y
rts
*--------------------------------------
* Internal Commands
*--------------------------------------
Cmd.Exec.CD lda (ZPPTR2)
bne Cmd.Exec.CD1
Cmd.Exec.PWD ldy #S.PS.hPREFIX
lda (pPs),y
>SYSCALL GetMemPtr.A
>SYSCALL PrintF.YA
jmp Cmd.Exec.ECHO.CR
Cmd.Exec.CD1 lda (ZPPTR2) Get ARG
cmp #'/' Full Path?
bne .20
sta (ZPCMDBuf)
ldy #1 strlen = 1
lda #0
sta (ZPCMDBuf),y
bra .1
.20 ldy #S.PS.hPREFIX no, init target prefix with actual prefix
lda (pPs),y
>SYSCALL GetMemPtr.A
>PUSHYA
>PUSHW ZPCMDBuf
>SYSCALL StrCpy
ldy #0
.10 iny
lda (ZPCMDBuf),y
bne .10 get strlen ...
.1 ldx #0 dot counter for /./ & /../ detection
.12 lda (ZPPTR2)
bne .21 last char...
dey
lda (ZPCMDBuf),y
iny
cmp #'/'
beq .8
lda #'/'
sta (ZPCMDBuf),y
iny
bra .13
.21 inc ZPPTR2
bne .11
inc ZPPTR2+1
.11 sta (ZPCMDBuf),y
iny
cmp #'/'
beq .13
cmp #'.' a dot ?
bne .1 no, reset counter...
inx
bra .12
.13 txa counting dot ?
beq .12
cpx #2 /../ ?
beq .14
dex /./ ?
bne Cmd.Exec.ERRSYN
dey
dey remove "./" at the end of path....
bra .1
.14 dey remove "../" at the end of path : "/dir1/../"
dey
dey
beq Cmd.Exec.ERRSYN path was "/../".....error....
.16 dey path is something like : /dir1/, skip ending '/'
lda (ZPCMDBuf),y path is something like : /dir1
cmp #'/'
bne .16
bra .1 Start over...
.8 lda #0
sta (ZPCMDBuf),y
cpy #1
beq .80 we have '/' go change prefix
>LDYA ZPCMDBuf
>SYSCALL PrintF.YA
lda #'!'
>SYSCALL PutChar.A
>PUSHEA.G CMD.Stat
>PUSHW ZPCMDBuf
>SYSCALL STAT
bcs .9
ldy #CMD.Stat+S.STAT.P.TYPE
lda (pData),y
cmp #$0F Directory ?
bne Cmd.Exec.ERRSYN
.80 >LDYA ZPCMDBuf
>SYSCALL NewStr.YA
bcs .9
phx
ldy #S.PS.hPREFIX
lda (pPs),y
>SYSCALL FreeMem.A
pla
ldy #S.PS.hPREFIX
sta (pPs),y
jsr SetPWD
clc
.9 jmp Cmd.Exec.Done
*--------------------------------------
Cmd.Exec.ERRSYN lda #SYSMGR.ERRSYN
sec
jmp Cmd.Exec.Done
*--------------------------------------
Cmd.Exec.STARTPROC
lda (ZPPTR2)
beq Cmd.Exec.ERRSYN
lda #$ff
>STA.G CMD.bStartProc
>LDYA ZPPTR2
>STYA ZPPTR1
jmp Cmd.Exec.EXT
*--------------------------------------
Cmd.Exec.SET lda (ZPPTR2)
bne Cmd.Exec.SETVAR
* No arg, print all ENV
ldy #S.PS.hENV
lda (pPs),y
>SYSCALL GetMemPtr.A
>STYA ZPPTR1
.1 lda (ZPPTR1)
beq .8 Ending 0 ?
ldy #$ff
.2 iny
lda (ZPPTR1),y
bne .2
tya
sec
adc ZPPTR1
sta ZPPTR2
lda #0
adc ZPPTR1+1
sta ZPPTR2+1
>PUSHW ZPPTR2 Push value
>PUSHW ZPPTR1 Push name
>LDYA L.MSG.PRINTENV
>SYSCALL PrintF.YA
bcs .9
ldy #$ff
.3 iny
lda (ZPPTR2),y
bne .3
tya
sec
adc ZPPTR2
sta ZPPTR1
lda #0
adc ZPPTR2+1
sta ZPPTR1+1
bra .1
.8 clc
.9 jmp Cmd.Exec.Done
Cmd.Exec.SETVAR ldy #0
lda (ZPPTR2),y
iny
cmp #'=' string is '=value' ?
beq .99 syntax error
.1 lda (ZPPTR2),y
iny
beq .3 no =, GetEnv
cmp #'=' ARGS contains =, PutEnv
bne .1
lda (ZPPTR2),y
beq .2 "VAR=" go UnsetEnv
>LDYA ZPPTR2 String is VAR=VALUE...
>SYSCALL PutEnv.YA
jmp Cmd.Exec.Done
.2 dey String is "VAR=", Remove endig '='
sta (ZPPTR2),y
>LDYA ZPPTR2 String is "VAR"...
>SYSCALL UnsetEnv.YA
jmp Cmd.Exec.Done
* Print requested VAR
.3 >LDYA ZPPTR2
>SYSCALL GetEnv.YA
bcs .8
>PUSHYA push value
>PUSHW ZPPTR2 push name
>LDYA L.MSG.PRINTENV
>SYSCALL PrintF.YA
.8 clc
.9 jmp Cmd.Exec.Done
.99 jmp Cmd.Exec.ERRSYN
*--------------------------------------
Cmd.Exec.DATE sec
.HS 90 bcc
Cmd.Exec.TIME clc
php
>LEA.G CMD.Time
>SYSCALL Time.YA
>PUSHWI ZPCMDBuf
plp
bcc .1
>PUSHW L.FMT.DATE
bra .2
.1 >PUSHW L.FMT.TIME
.2 >PUSHEA.G CMD.Time
>SYSCALL StrFTime
>LDYAI ZPCMDBuf
>SYSCALL PrintF.YA
bcs Cmd.Exec.ECHO.END
bra Cmd.Exec.ECHO.CR
*--------------------------------------
Cmd.Exec.ECHO lda (ZPPtr2)
beq Cmd.Exec.ECHO.CR
>LDYA ZPPtr2
>SYSCALL PrintF.YA
Cmd.Exec.ECHO.CR
lda #13
>SYSCALL PutChar.A
bcs Cmd.Exec.ECHO.END
lda #10
>SYSCALL PutChar.A
Cmd.Exec.ECHO.END
jmp Cmd.Exec.Done
*--------------------------------------
Cmd.Exec.READ lda (ZPPtr2)
beq .9
lda #0
>STA.G bSecureRead
lda pData
clc
adc #CMD.VarName
sta ZPPTR1
lda pData+1
adc #0
sta ZPPTR1+1
ldy #0
.1 lda (ZPPtr2),y
beq .8
iny
cmp #' '
beq .1
cmp #'-'
bne .7
lda (ZPPtr2),y
beq .9
iny
cmp #'S'
bne .2
lda #$80
>STA.G bSecureRead
bra .1
.2 cmp #'P'
bne .9
lda (ZPPtr2),y
beq .9
iny
cmp #' '
bne .9
lda (ZPPtr2),y
beq .9
iny
cmp #'"'
bne .9
.3 lda (ZPPtr2),y
beq .9
iny
cmp #'"'
beq .1
phy
>SYSCALL PutChar.A
ply
bcs .99
bra .3
.9 jmp Cmd.Exec.ERRSYN
.70 lda (ZPPtr2),y
cmp #' '
beq .1
.7 lda (ZPPTR1)
cmp #VarLen.MAX
beq .9
inc
sta (ZPPTR1)
tay
lda (ZPPtr2),y
sta (ZPPTR1),y
bne .70
.8 lda (ZPPTR1) No var name ? SYNERR
beq .9
clc
.99 jmp Cmd.Exec.Done
*--------------------------------------
Cmd.Exec.SLEEP lda (ZPPtr2)
beq .9
>PUSHEA.G Sleep
>PUSHW ZPPtr2
>SYSCALL AToI.YA
bcs .9
jmp Cmd.Exec.Done
.9 jmp Cmd.Exec.ERRSYN
*--------------------------------------
Cmd.Exec.PAUSE lda #$FF
>STA.G bPause
clc
jmp Cmd.Exec.Done
*--------------------------------------
Cmd.Exec.EXIT lda #$FF
>STA.G bExit
clc
jmp Cmd.Exec.Done
*--------------------------------------
Cmd.Exec.Split ldy #$ff CMD Len
.1 iny
lda (ZPPtr1),y Get CMD char...
beq .2
cmp #' ' we reached blank between CMD ARGS
bne .1
lda #0
sta (ZPPtr1),y Cut CMD with the ending 0
iny
.2 tya Y=CMD len+0, if ARGS="" Y=
clc
adc ZPPtr1
sta ZPPtr2
lda ZPPtr1+1
adc #0
sta ZPPtr2+1 ZPPtr2->ARGS or CMD ending 0 if empty
rts
*--------------------------------------
MAN
SAVE /A2OSX.SRC/SBIN/SHELL.S.CMD
LOAD /A2OSX.SRC/SBIN/SHELL.S
ASM