debug: made it a counter rather than boolean

This commit is contained in:
Zellyn Hunter 2021-08-01 14:19:26 -04:00
parent b264fe8e96
commit 0b85248dde
13 changed files with 49 additions and 56 deletions

View File

@ -97,9 +97,10 @@ My rough TODO list (apart from anything marked (✗) in the disk
operations matrix is listed below. Anything that an actual user needs operations matrix is listed below. Anything that an actual user needs
will be likely to get priority. will be likely to get priority.
- [ ] Make `put` accept load address for appropriate filetypes. - [x] Make `put` accept load address for appropriate filetypes.
- [ ] Fix `golint` errors - [x] Fix `golint` errors
- [ ] Implement `GetFile` for prodos - [ ] Implement `GetFile` for prodos
- [ ] Implement `PutFile` for prodos
- [ ] Implement `Delete` for Super-Mon - [ ] Implement `Delete` for Super-Mon
- [ ] Implement `Delete` for DOS 3.3 - [ ] Implement `Delete` for DOS 3.3
- [ ] Implement `Delete` for ProDOS - [ ] Implement `Delete` for ProDOS

View File

@ -35,7 +35,7 @@ func (l *LsCmd) Run(globals *types.Globals) error {
if err != nil { if err != nil {
return fmt.Errorf("%w: %s", err, l.Image.Name()) return fmt.Errorf("%w: %s", err, l.Image.Name())
} }
if globals.Debug { if globals.Debug > 0 {
fmt.Fprintf(os.Stderr, "Opened disk with order %q, system %q\n", order, op.Name()) fmt.Fprintf(os.Stderr, "Opened disk with order %q, system %q\n", order, op.Name())
} }

View File

@ -16,6 +16,7 @@ type PutCmd struct {
System string `kong:"default='auto',enum='auto,dos3',help='DOS system used for image.'"` System string `kong:"default='auto',enum='auto,dos3',help='DOS system used for image.'"`
FiletypeName string `kong:"default='B',help='Type of file (“diskii filetypes” to list).'"` FiletypeName string `kong:"default='B',help='Type of file (“diskii filetypes” to list).'"`
Overwrite bool `kong:"short='f',help='Overwrite existing file?'"` Overwrite bool `kong:"short='f',help='Overwrite existing file?'"`
Address uint16 `kong:"type='anybaseuint16',default='0x6000',help='For filetypes where it is appropriate, address to load the code at.'"`
DiskImage string `kong:"arg,required,type='existingfile',help='Disk image to modify.'"` DiskImage string `kong:"arg,required,type='existingfile',help='Disk image to modify.'"`
TargetFilename string `kong:"arg,required,help='Filename to use on disk.'"` TargetFilename string `kong:"arg,required,help='Filename to use on disk.'"`
@ -56,7 +57,8 @@ func (p *PutCmd) Run(globals *types.Globals) error {
Length: len(contents), Length: len(contents),
Type: filetype, Type: filetype,
}, },
Data: contents, Data: contents,
StartAddress: p.Address,
} }
_, err = op.PutFile(fileInfo, p.Overwrite) _, err = op.PutFile(fileInfo, p.Overwrite)
if err != nil { if err != nil {

View File

@ -13,7 +13,7 @@ import (
) )
// OpenFilename attempts to open a disk or device image, using the provided ordering and system type. // OpenFilename attempts to open a disk or device image, using the provided ordering and system type.
func OpenFilename(filename string, order types.DiskOrder, system string, operatorFactories []types.OperatorFactory, debug bool) (types.Operator, types.DiskOrder, error) { func OpenFilename(filename string, order types.DiskOrder, system string, operatorFactories []types.OperatorFactory, debug int) (types.Operator, types.DiskOrder, error) {
if filename == "-" { if filename == "-" {
return OpenFile(os.Stdin, order, system, operatorFactories, debug) return OpenFile(os.Stdin, order, system, operatorFactories, debug)
} }
@ -26,7 +26,7 @@ func OpenFilename(filename string, order types.DiskOrder, system string, operato
// OpenFile attempts to open a disk or device image, using the provided ordering and system type. // OpenFile attempts to open a disk or device image, using the provided ordering and system type.
// OpenFile will close the file. // OpenFile will close the file.
func OpenFile(file *os.File, order types.DiskOrder, system string, operatorFactories []types.OperatorFactory, debug bool) (types.Operator, types.DiskOrder, error) { func OpenFile(file *os.File, order types.DiskOrder, system string, operatorFactories []types.OperatorFactory, debug int) (types.Operator, types.DiskOrder, error) {
bb, err := io.ReadAll(file) bb, err := io.ReadAll(file)
if err != nil { if err != nil {
return nil, "", err return nil, "", err
@ -38,7 +38,7 @@ func OpenFile(file *os.File, order types.DiskOrder, system string, operatorFacto
} }
// OpenImage attempts to open a disk or device image, using the provided ordering and system type. // OpenImage attempts to open a disk or device image, using the provided ordering and system type.
func OpenImage(filebytes []byte, filename string, order types.DiskOrder, system string, operatorFactories []types.OperatorFactory, debug bool) (types.Operator, types.DiskOrder, error) { func OpenImage(filebytes []byte, filename string, order types.DiskOrder, system string, operatorFactories []types.OperatorFactory, debug int) (types.Operator, types.DiskOrder, error) {
ext := strings.ToLower(path.Ext(filename)) ext := strings.ToLower(path.Ext(filename))
size := len(filebytes) size := len(filebytes)
if size == FloppyDiskBytes { if size == FloppyDiskBytes {
@ -54,7 +54,7 @@ func OpenImage(filebytes []byte, filename string, order types.DiskOrder, system
return nil, "", fmt.Errorf("can only open disk-sized images and .hdv files") return nil, "", fmt.Errorf("can only open disk-sized images and .hdv files")
} }
func openHDV(rawbytes []byte, order types.DiskOrder, system string, operatorFactories []types.OperatorFactory, debug bool) (types.Operator, types.DiskOrder, error) { func openHDV(rawbytes []byte, order types.DiskOrder, system string, operatorFactories []types.OperatorFactory, debug int) (types.Operator, types.DiskOrder, error) {
size := len(rawbytes) size := len(rawbytes)
if size%512 > 0 { if size%512 > 0 {
return nil, "", fmt.Errorf("can only open .hdv files that are a multiple of 512 bytes: %d %% 512 == %d", size, size%512) return nil, "", fmt.Errorf("can only open .hdv files that are a multiple of 512 bytes: %d %% 512 == %d", size, size%512)
@ -80,7 +80,7 @@ func openHDV(rawbytes []byte, order types.DiskOrder, system string, operatorFact
return nil, "", fmt.Errorf("unable to find prodos module to open .hdv file") // Should not happen. return nil, "", fmt.Errorf("unable to find prodos module to open .hdv file") // Should not happen.
} }
func openDoOrPo(rawbytes []byte, order types.DiskOrder, system string, ext string, operatorFactories []types.OperatorFactory, debug bool) (types.Operator, types.DiskOrder, error) { func openDoOrPo(rawbytes []byte, order types.DiskOrder, system string, ext string, operatorFactories []types.OperatorFactory, debug int) (types.Operator, types.DiskOrder, error) {
var factories []types.OperatorFactory var factories []types.OperatorFactory
for _, factory := range operatorFactories { for _, factory := range operatorFactories {
if system == "auto" || system == factory.Name() { if system == "auto" || system == factory.Name() {
@ -121,7 +121,7 @@ func openDoOrPo(rawbytes []byte, order types.DiskOrder, system string, ext strin
} }
if len(orders) == 1 && system != "auto" { if len(orders) == 1 && system != "auto" {
if debug { if debug > 1 {
fmt.Fprintf(os.Stderr, "Attempting to open with order=%s, system=%s.\n", order, factory.Name()) fmt.Fprintf(os.Stderr, "Attempting to open with order=%s, system=%s.\n", order, factory.Name())
} }
op, err := factory.Operator(diskbytes, debug) op, err := factory.Operator(diskbytes, debug)
@ -131,7 +131,7 @@ func openDoOrPo(rawbytes []byte, order types.DiskOrder, system string, ext strin
return op, order, nil return op, order, nil
} }
if debug { if debug > 1 {
fmt.Fprintf(os.Stderr, "Testing whether order=%s, system=%s seems to match.\n", order, factory.Name()) fmt.Fprintf(os.Stderr, "Testing whether order=%s, system=%s seems to match.\n", order, factory.Name())
} }
if factory.SeemsToMatch(diskbytes, debug) { if factory.SeemsToMatch(diskbytes, debug) {
@ -139,7 +139,7 @@ func openDoOrPo(rawbytes []byte, order types.DiskOrder, system string, ext strin
if err == nil { if err == nil {
return op, order, nil return op, order, nil
} }
if debug { if debug > 1 {
fmt.Fprintf(os.Stderr, "Got error opening with order=%s, system=%s: %v\n", order, factory.Name(), err) fmt.Fprintf(os.Stderr, "Got error opening with order=%s, system=%s: %v\n", order, factory.Name(), err)
} }
} }

View File

@ -490,7 +490,7 @@ func (tsl *TrackSectorList) FromSector(data []byte) error {
// readCatalogSectors reads the raw CatalogSector structs from a DOS // readCatalogSectors reads the raw CatalogSector structs from a DOS
// 3.3 disk. // 3.3 disk.
func readCatalogSectors(diskbytes []byte, debug bool) ([]CatalogSector, error) { func readCatalogSectors(diskbytes []byte, debug int) ([]CatalogSector, error) {
v := &VTOC{} v := &VTOC{}
err := disk.UnmarshalLogicalSector(diskbytes, v, VTOCTrack, VTOCSector) err := disk.UnmarshalLogicalSector(diskbytes, v, VTOCTrack, VTOCSector)
if err != nil { if err != nil {
@ -528,7 +528,7 @@ func readCatalogSectors(diskbytes []byte, debug bool) ([]CatalogSector, error) {
} }
// ReadCatalog reads the catalog of a DOS 3.3 disk. // ReadCatalog reads the catalog of a DOS 3.3 disk.
func ReadCatalog(diskbytes []byte, debug bool) (files, deleted []FileDesc, err error) { func ReadCatalog(diskbytes []byte, debug int) (files, deleted []FileDesc, err error) {
css, err := readCatalogSectors(diskbytes, debug) css, err := readCatalogSectors(diskbytes, debug)
if err != nil { if err != nil {
return nil, nil, err return nil, nil, err
@ -553,7 +553,7 @@ func ReadCatalog(diskbytes []byte, debug bool) (files, deleted []FileDesc, err e
// high-level operations on files and directories. // high-level operations on files and directories.
type operator struct { type operator struct {
data []byte data []byte
debug bool debug int
} }
var _ types.Operator = operator{} var _ types.Operator = operator{}
@ -692,14 +692,14 @@ func (of OperatorFactory) Name() string {
// SeemsToMatch returns true if the []byte disk image seems to match the // SeemsToMatch returns true if the []byte disk image seems to match the
// system of this operator. // system of this operator.
func (of OperatorFactory) SeemsToMatch(diskbytes []byte, debug bool) bool { func (of OperatorFactory) SeemsToMatch(diskbytes []byte, debug int) bool {
// For now, just return true if we can run Catalog successfully. // For now, just return true if we can run Catalog successfully.
_, _, err := ReadCatalog(diskbytes, debug) _, _, err := ReadCatalog(diskbytes, debug)
return err == nil return err == nil
} }
// Operator returns an Operator for the []byte disk image. // Operator returns an Operator for the []byte disk image.
func (of OperatorFactory) Operator(diskbytes []byte, debug bool) (types.Operator, error) { func (of OperatorFactory) Operator(diskbytes []byte, debug int) (types.Operator, error) {
return operator{data: diskbytes, debug: debug}, nil return operator{data: diskbytes, debug: debug}, nil
} }

View File

@ -97,7 +97,7 @@ func TestReadCatalog(t *testing.T) {
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
fds, deleted, err := ReadCatalog(diskbytes, false) fds, deleted, err := ReadCatalog(diskbytes, 0)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }

View File

@ -19,7 +19,7 @@ import (
) )
var cli struct { var cli struct {
Debug bool `kong:"short='v',help='Enable debug mode.'"` Debug int `kong:"short='v',type='counter',help='Enable debug mode.'"`
Ls cmd.LsCmd `cmd:"" aliases:"list,cat,catalog" help:"List files/directories on a disk."` Ls cmd.LsCmd `cmd:"" aliases:"list,cat,catalog" help:"List files/directories on a disk."`
Reorder cmd.ReorderCmd `cmd:"" help:"Convert between DOS-order and ProDOS-order disk images."` Reorder cmd.ReorderCmd `cmd:"" help:"Convert between DOS-order and ProDOS-order disk images."`

1
next
View File

@ -4,3 +4,4 @@ set -euo pipefail
set -x set -x
# go run . nakedos mkhello supermon-audit-new.dsk DF02 # go run . nakedos mkhello supermon-audit-new.dsk DF02
# go run . put -f supermon-audit-new.dsk DF02:FWORLD audit.o # go run . put -f supermon-audit-new.dsk DF02:FWORLD audit.o
go run . dump ./data/disks/ProDOS_2_4_2.po VIEW.README

View File

@ -666,7 +666,7 @@ func (v Volume) subdirDescriptors() []FileDescriptor {
// readVolume reads the entire volume and subdirectories from a device // readVolume reads the entire volume and subdirectories from a device
// into memory. // into memory.
func readVolume(devicebytes []byte, keyBlock uint16, debug bool) (Volume, error) { func readVolume(devicebytes []byte, keyBlock uint16, debug int) (Volume, error) {
v := Volume{ v := Volume{
keyBlock: &VolumeDirectoryKeyBlock{}, keyBlock: &VolumeDirectoryKeyBlock{},
subdirsByBlock: make(map[uint16]*Subdirectory), subdirsByBlock: make(map[uint16]*Subdirectory),
@ -699,7 +699,7 @@ func readVolume(devicebytes []byte, keyBlock uint16, debug bool) (Volume, error)
} }
v.blocks = append(v.blocks, &vdb) v.blocks = append(v.blocks, &vdb)
v.firstSubdirBlocks[block] = keyBlock v.firstSubdirBlocks[block] = keyBlock
if debug { if debug > 1 {
fmt.Fprintf(os.Stderr, " firstSubdirBlocks[%d] → %d\n", block, keyBlock) fmt.Fprintf(os.Stderr, " firstSubdirBlocks[%d] → %d\n", block, keyBlock)
} }
// if debug { // if debug {
@ -708,7 +708,7 @@ func readVolume(devicebytes []byte, keyBlock uint16, debug bool) (Volume, error)
} }
sdds := v.subdirDescriptors() sdds := v.subdirDescriptors()
if debug { if debug > 1 {
fmt.Fprintf(os.Stderr, "got %d top-level subdir descriptors\n", len(sdds)) fmt.Fprintf(os.Stderr, "got %d top-level subdir descriptors\n", len(sdds))
} }
@ -719,24 +719,24 @@ func readVolume(devicebytes []byte, keyBlock uint16, debug bool) (Volume, error)
return v, err return v, err
} }
v.subdirsByBlock[sdd.KeyPointer] = &sub v.subdirsByBlock[sdd.KeyPointer] = &sub
if debug { if debug > 1 {
fmt.Fprintf(os.Stderr, " subdirsByBlock[%d] → %q\n", sdd.KeyPointer, sub.keyBlock.Header.Name()) fmt.Fprintf(os.Stderr, " subdirsByBlock[%d] → %q\n", sdd.KeyPointer, sub.keyBlock.Header.Name())
} }
sdds = append(sdds, sub.subdirDescriptors()...) sdds = append(sdds, sub.subdirDescriptors()...)
for _, block := range sub.blocks { for _, block := range sub.blocks {
v.firstSubdirBlocks[block.block] = sdd.KeyPointer v.firstSubdirBlocks[block.block] = sdd.KeyPointer
if debug { if debug > 1 {
fmt.Fprintf(os.Stderr, " firstSubdirBlocks[%d] → %d\n", block.block, sdd.KeyPointer) fmt.Fprintf(os.Stderr, " firstSubdirBlocks[%d] → %d\n", block.block, sdd.KeyPointer)
} }
} }
} }
if debug { if debug > 1 {
fmt.Fprintf(os.Stderr, "got %d total subdir descriptors\n", len(sdds)) fmt.Fprintf(os.Stderr, "got %d total subdir descriptors\n", len(sdds))
} }
for _, sd := range v.subdirsByBlock { for _, sd := range v.subdirsByBlock {
name := sd.keyBlock.Header.Name() name := sd.keyBlock.Header.Name()
if debug { if debug > 1 {
fmt.Fprintf(os.Stderr, "processing subdir %q\n", name) fmt.Fprintf(os.Stderr, "processing subdir %q\n", name)
} }
parentName, err := parentDirName(sd.keyBlock.Header.ParentPointer, keyBlock, v.subdirsByBlock, v.firstSubdirBlocks) parentName, err := parentDirName(sd.keyBlock.Header.ParentPointer, keyBlock, v.subdirsByBlock, v.firstSubdirBlocks)
@ -749,8 +749,11 @@ func readVolume(devicebytes []byte, keyBlock uint16, debug bool) (Volume, error)
v.subdirsByName[name] = sd v.subdirsByName[name] = sd
} }
if debug { if debug > 1 {
fmt.Fprintf(os.Stderr, "HERE2\n") fmt.Fprintf(os.Stderr, "subdirsByName:\n")
for k := range v.subdirsByName {
fmt.Fprintf(os.Stderr, " %s\n", k)
}
} }
return v, nil return v, nil
} }
@ -842,7 +845,7 @@ func copyBytes(dst, src []byte) int {
// high-level operations on files and directories. // high-level operations on files and directories.
type operator struct { type operator struct {
data []byte data []byte
debug bool debug int
} }
var _ types.Operator = operator{} var _ types.Operator = operator{}
@ -865,7 +868,7 @@ func (o operator) HasSubdirs() bool {
// Catalog returns a catalog of disk entries. subdir should be empty // Catalog returns a catalog of disk entries. subdir should be empty
// for operating systems that do not support subdirectories. // for operating systems that do not support subdirectories.
func (o operator) Catalog(subdir string) ([]types.Descriptor, error) { func (o operator) Catalog(subdir string) ([]types.Descriptor, error) {
if o.debug { if o.debug > 1 {
fmt.Fprintf(os.Stderr, "Catalog of %q\n", subdir) fmt.Fprintf(os.Stderr, "Catalog of %q\n", subdir)
} }
vol, err := readVolume(o.data, 2, o.debug) vol, err := readVolume(o.data, 2, o.debug)
@ -936,14 +939,14 @@ func (of OperatorFactory) Name() string {
// SeemsToMatch returns true if the []byte disk image seems to match the // SeemsToMatch returns true if the []byte disk image seems to match the
// system of this operator. // system of this operator.
func (of OperatorFactory) SeemsToMatch(devicebytes []byte, debug bool) bool { func (of OperatorFactory) SeemsToMatch(devicebytes []byte, debug int) bool {
// For now, just return true if we can run Catalog successfully. // For now, just return true if we can run Catalog successfully.
_, err := readVolume(devicebytes, 2, debug) _, err := readVolume(devicebytes, 2, debug)
return err == nil return err == nil
} }
// Operator returns an Operator for the []byte disk image. // Operator returns an Operator for the []byte disk image.
func (of OperatorFactory) Operator(devicebytes []byte, debug bool) (types.Operator, error) { func (of OperatorFactory) Operator(devicebytes []byte, debug int) (types.Operator, error) {
return operator{data: devicebytes, debug: debug}, nil return operator{data: devicebytes, debug: debug}, nil
} }

View File

@ -646,7 +646,7 @@ type Operator struct {
data []byte data []byte
SM SectorMap SM SectorMap
ST SymbolTable ST SymbolTable
debug bool debug int
} }
var _ types.Operator = Operator{} var _ types.Operator = Operator{}
@ -806,7 +806,7 @@ func (of OperatorFactory) Name() string {
// SeemsToMatch returns true if the []byte disk image seems to match the // SeemsToMatch returns true if the []byte disk image seems to match the
// system of this operator. // system of this operator.
func (of OperatorFactory) SeemsToMatch(diskbytes []byte, debug bool) bool { func (of OperatorFactory) SeemsToMatch(diskbytes []byte, debug int) bool {
// For now, just return true if we can run Catalog successfully. // For now, just return true if we can run Catalog successfully.
sm, err := LoadSectorMap(diskbytes) sm, err := LoadSectorMap(diskbytes)
if err != nil { if err != nil {
@ -819,7 +819,7 @@ func (of OperatorFactory) SeemsToMatch(diskbytes []byte, debug bool) bool {
} }
// Operator returns an Operator for the []byte disk image. // Operator returns an Operator for the []byte disk image.
func (of OperatorFactory) Operator(diskbytes []byte, debug bool) (types.Operator, error) { func (of OperatorFactory) Operator(diskbytes []byte, debug int) (types.Operator, error) {
sm, err := LoadSectorMap(diskbytes) sm, err := LoadSectorMap(diskbytes)
if err != nil { if err != nil {
return nil, err return nil, err

View File

@ -4,7 +4,6 @@ package supermon
import ( import (
"os" "os"
"path/filepath"
"reflect" "reflect"
"strings" "strings"
"testing" "testing"
@ -46,20 +45,6 @@ func loadSectorMap(filename string) (SectorMap, []byte, error) {
return sm, diskbytes, nil return sm, diskbytes, nil
} }
// getOperator gets a types.Operator for the given NakedOS disk, assumed to be
// in "do" order.
func getOperator(filename string) (types.Operator, error) {
f, err := os.Open(filepath.Clean(filename))
if err != nil {
return nil, err
}
op, _, err := disk.OpenFile(f, "do", "nakedos", []types.OperatorFactory{OperatorFactory{}}, false)
if err != nil {
return nil, err
}
return op, nil
}
// TestReadSectorMap tests the reading of the sector map of a test // TestReadSectorMap tests the reading of the sector map of a test
// disk. // disk.
func TestReadSectorMap(t *testing.T) { func TestReadSectorMap(t *testing.T) {
@ -147,7 +132,7 @@ func TestReadSymbolTable(t *testing.T) {
// TestGetFile tests the retrieval of a file's contents, using the // TestGetFile tests the retrieval of a file's contents, using the
// Operator interface. // Operator interface.
func TestGetFile(t *testing.T) { func TestGetFile(t *testing.T) {
op, err := getOperator(testDisk) op, _, err := disk.OpenFilename(testDisk, types.DiskOrderAuto, "nakedos", []types.OperatorFactory{OperatorFactory{}}, 0)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -228,7 +213,7 @@ func TestReadWriteSymbolTable(t *testing.T) {
// TestPutFile tests the creation of a file, using the Operator // TestPutFile tests the creation of a file, using the Operator
// interface. // interface.
func TestPutFile(t *testing.T) { func TestPutFile(t *testing.T) {
op, err := getOperator(testDisk) op, _, err := disk.OpenFilename(testDisk, types.DiskOrderAuto, "nakedos", []types.OperatorFactory{OperatorFactory{}}, 0)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }

View File

@ -42,9 +42,9 @@ type OperatorFactory interface {
DiskOrder() DiskOrder DiskOrder() DiskOrder
// SeemsToMatch returns true if the []byte disk image seems to match the // SeemsToMatch returns true if the []byte disk image seems to match the
// system of this operator. // system of this operator.
SeemsToMatch(diskbytes []byte, debug bool) bool SeemsToMatch(diskbytes []byte, debug int) bool
// Operator returns an Operator for the []byte disk image. // Operator returns an Operator for the []byte disk image.
Operator(diskbytes []byte, debug bool) (Operator, error) Operator(diskbytes []byte, debug int) (Operator, error)
} }
// Operator is the interface that can operate on disks. // Operator is the interface that can operate on disks.

View File

@ -4,7 +4,8 @@ package types
// Globals holds flags and configuration that are shared globally. // Globals holds flags and configuration that are shared globally.
type Globals struct { type Globals struct {
Debug bool // Debug level (0 = no debugging, 1 = normal user debugging, 2+ is mostly for me)
Debug int
// DiskOperatorFactories holds the current list of registered OperatorFactory instances.
DiskOperatorFactories []OperatorFactory DiskOperatorFactories []OperatorFactory
} }