Skip to content

Commit

Permalink
Merge pull request #17 from skx/10-delete
Browse files Browse the repository at this point in the history
Allow deleting files
  • Loading branch information
skx authored Apr 14, 2024
2 parents 3f57452 + ff040e9 commit 4749431
Show file tree
Hide file tree
Showing 7 changed files with 92 additions and 7 deletions.
4 changes: 4 additions & 0 deletions cpm/cpm.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,10 @@ func New(filename string, logger *slog.Logger) *CPM {
Desc: "F_SNEXT",
Handler: SysCallFindNext,
}
sys[19] = CPMHandler{
Desc: "F_DELETE",
Handler: SysCallDeleteFile,
}
sys[22] = CPMHandler{
Desc: "F_MAKE",
Handler: SysCallMakeFile,
Expand Down
29 changes: 29 additions & 0 deletions cpm/cpm_syscalls.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,35 @@ func SysCallFindNext(cpm *CPM) error {
return nil
}

// SyscallDeleteFile deletes the filename specified by the FCB in DE.
func SysCallDeleteFile(cpm *CPM) error {

// The pointer to the FCB
ptr := cpm.CPU.States.DE.U16()
// Get the bytes which make up the FCB entry.
xxx := cpm.Memory.GetRange(ptr, 36)

// Create a structure with the contents
fcbPtr := fcb.FromBytes(xxx)

// Get the name
name := fcbPtr.GetName()
ext := fcbPtr.GetType()

fileName := name
if ext != "" && ext != " " {
fileName += "."
fileName += ext
}

// Delete the named file
err := os.Remove(fileName)
if os.IsNotExist(err) {
return nil
}
return err
}

// SysCallMakeFile creates the file named in the FCB given in DE
func SysCallMakeFile(cpm *CPM) error {
// The pointer to the FCB
Expand Down
4 changes: 2 additions & 2 deletions fcb/fcb.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func (f *FCB) GetName() string {
t += string(c)
}
}
return t
return strings.TrimSpace(t)
}

// GetType returns the type/extension component of an FCB entry.
Expand All @@ -43,7 +43,7 @@ func (f *FCB) GetType() string {
t += string(c)
}
}
return t
return strings.TrimSpace(t)
}

// AsBytes returns the entry of the FCB in a format suitable
Expand Down
8 changes: 4 additions & 4 deletions fcb/fcb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ func TestFCBFromString(t *testing.T) {
if f.Drive != 1 {
t.Fatalf("drive wrong")
}
if f.GetName() != "FOO " {
if f.GetName() != "FOO" {
t.Fatalf("name wrong, got '%v'", f.GetName())
}
if f.GetType() != " " {
if f.GetType() != "" {
t.Fatalf("unexpected suffix '%v'", f.GetType())
}

Expand All @@ -25,7 +25,7 @@ func TestFCBFromString(t *testing.T) {
if f.GetName() != "THIS-IS-" {
t.Fatalf("name wrong, got '%v'", f.GetName())
}
if f.GetType() != " " {
if f.GetType() != "" {
t.Fatalf("unexpected suffix '%v'", f.GetType())
}

Expand Down Expand Up @@ -54,7 +54,7 @@ func TestFCBFromString(t *testing.T) {
if f.Drive != 2 {
t.Fatalf("drive wrong")
}
if f.GetName() != "TEST " {
if f.GetName() != "TEST" {
t.Fatalf("name wrong, got '%v'", f.GetName())
}
if f.GetType() != "C??" {
Expand Down
2 changes: 1 addition & 1 deletion samples/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#
# The files we wish to generate.
#
all: cli-args.com create.com drive.com find.com ret.com user-num.com
all: cli-args.com create.com delete.com drive.com find.com ret.com user-num.com


#
Expand Down
Binary file added samples/delete.com
Binary file not shown.
52 changes: 52 additions & 0 deletions samples/delete.z80
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
;; delete.asm - Delete the named file
;;

FCB1: EQU 0x5C

BDOS_ENTRY_POINT: EQU 5

BDOS_OUTPUT_STRING: EQU 9
BDOS_DELETE_FILE: EQU 19

;;
;; CP/M programs start at 0x100.
;;
ORG 100H

;; The FCB will be populated with the pattern/first argument,
;; if the first character of that region is a space-character
;; then we've got nothing to search for.
ld a, (FCB1 + 1)
cp 0x20 ; 0x20 = 32 == SPACE
jp nz, got_argument ; Not a space, so we can proceed

;;
;; No argument, so show the error and exit
;;
ld de, usage_message
ld c, BDOS_OUTPUT_STRING
call BDOS_ENTRY_POINT
jr exit_fn

got_argument:
LD DE, FCB1
LD C, BDOS_DELETE_FILE
CALL BDOS_ENTRY_POINT

exit_fn:
;; exit
LD C,0x00
CALL BDOS_ENTRY_POINT


;;;
;;; The message displayed if no command-line argument was present.
;;;
usage_message:
db "Usage: DELETE FILENAME.EXT"

;; note fall-through here :)
newline:
db 0xa, 0xd, "$"

END

0 comments on commit 4749431

Please sign in to comment.