Validating DFS (or ADFS) filenames

Discuss all aspects of programming here. From 8-bit through to modern architectures.
Post Reply
User avatar
EdwardianDuck
Posts: 47
Joined: Thu Aug 10, 2017 8:07 pm
Location: Northamptonshire
Contact:

Validating DFS (or ADFS) filenames

Post by EdwardianDuck » Tue Jan 02, 2018 1:40 pm

I'm writing a program in assembler which prompts the user to enter a file name as part of a "Save As" operation. Unfortunately as things stand if the user enters an invalid file/path name, I get a "Bad name", "Bad program" sequence. Looking at the documentation for OSFIND, this doesn't seem to return any useful error information in this situation. As far as I can see "Bad name" can't be trapped.

It's easy enough to write a routine to validate a DFS filename before calling OSFIND (I'll probably simplify it in the short term by making the validation overly restrictive), but I thought I'd ask in case I'm missing something obvious.

On the other hand, am I over-thinking this? Should I perhaps assume anyone who ends up using the program will just expect to know that an invalid name will cause problems?

Any input would be greatly appreciated.

Jeremy
Master 128 + RetroClinic DataCentre + Internal Pi Zero Coprocessor, MiST

User avatar
jgharston
Posts: 3126
Joined: Thu Sep 24, 2009 11:22 am
Location: Whitby/Sheffield
Contact:

Re: Validating DFS (or ADFS) filenames

Post by jgharston » Tue Jan 02, 2018 2:14 pm

The only way you can validate a filename is to ask the filing system and catch the 204,Bad filename error generated if it is a bad filename. A badly constructed path could also give other errors, such as 205,Bad drive, 206,Bad directory, 253,Bad string, etc.

It's not possible for a program to read a filing system author's mind and work out what has been programmed into the filing system, psychic abilities aren't something that is programmable. For programmatically generated leafnames 0-9, A-Z are guaranteed to be valid characters, but restricting a user to those characters prevents the user from accessing anything that uses other characters - I'm looking at you EDWORD.
EdwardianDuck wrote:As far as I can see "Bad name" can't be trapped.
ON ERROR, exactly the same as any other (non-fatal) error.

Code: Select all

$ bbcbasic
PDP11 BBC BASIC IV Version 0.25
(C) Copyright J.G.Harston 1989,2005-2015
>_

User avatar
EdwardianDuck
Posts: 47
Joined: Thu Aug 10, 2017 8:07 pm
Location: Northamptonshire
Contact:

Re: Validating DFS (or ADFS) filenames

Post by EdwardianDuck » Tue Jan 02, 2018 3:00 pm

ON ERROR, exactly the same as any other (non-fatal) error.
Only in BASIC I suspect.

I guess I'll just do enough to deal with my own typing errors and resolve the larger problem by keeping the final program to myself.

Thank you
Jeremy
Master 128 + RetroClinic DataCentre + Internal Pi Zero Coprocessor, MiST

Coeus
Posts: 884
Joined: Mon Jul 25, 2016 11:05 am
Contact:

Re: Validating DFS (or ADFS) filenames

Post by Coeus » Tue Jan 02, 2018 3:32 pm

EdwardianDuck wrote:
ON ERROR, exactly the same as any other (non-fatal) error.
Only in BASIC I suspect.
Indeed you did say that your are writing in assembler. If that's the case, and you are not writing something that is expected to interwork with BASIC, then probably what you need to do is to "become the current language" by pointing the BRK vector at your own handler. If the user hits Break to leave your program BASIC will be re-selected. if you have some other way to quit then you need to finish by doing *BASIC. Working that way also means you have the whole of the language space in ZP as well as the language worksspace from 400-700.

One issue with attempting to do independent validation is that sooner or later someone will want to to use your program with a filing system that doesn't follow the rules you have programmed and will get frustrated.

crj
Posts: 834
Joined: Thu May 02, 2013 4:58 pm
Contact:

Re: Validating DFS (or ADFS) filenames

Post by crj » Tue Jan 02, 2018 6:25 pm

jgharston wrote:For programmatically generated leafnames 0-9, A-Z are guaranteed to be valid characters
Even then, if I wanted to be sure, I'd avoid filenames beginning with a digit, or of the form ABC or ABC1. The world is full of strange filesystems.

User avatar
EdwardianDuck
Posts: 47
Joined: Thu Aug 10, 2017 8:07 pm
Location: Northamptonshire
Contact:

Re: Validating DFS (or ADFS) filenames

Post by EdwardianDuck » Tue Jan 02, 2018 9:04 pm

Coeus wrote:If that's the case, and you are not writing something that is expected to interwork with BASIC, then probably what you need to do is to "become the current language" by pointing the BRK vector at your own handler. If the user hits Break to leave your program BASIC will be re-selected.
That seems to work. At least in the one minimal experiment I've had time to write I can catch the error and regain control at the correct point in the program.

Thank you.
Jeremy
Master 128 + RetroClinic DataCentre + Internal Pi Zero Coprocessor, MiST

User avatar
jgharston
Posts: 3126
Joined: Thu Sep 24, 2009 11:22 am
Location: Whitby/Sheffield
Contact:

Re: Validating DFS (or ADFS) filenames

Post by jgharston » Thu Jan 04, 2018 10:53 pm

EdwardianDuck wrote:
ON ERROR, exactly the same as any other (non-fatal) error.
Only in BASIC I suspect.
Well, the non-BASIC version of ON ERROR, ie claim BRKV.

Code: Select all

$ bbcbasic
PDP11 BBC BASIC IV Version 0.25
(C) Copyright J.G.Harston 1989,2005-2015
>_

User avatar
jgharston
Posts: 3126
Joined: Thu Sep 24, 2009 11:22 am
Location: Whitby/Sheffield
Contact:

Re: Validating DFS (or ADFS) filenames

Post by jgharston » Thu Jan 04, 2018 10:59 pm

A lot of my transient utilities that need to tidy up after an error (eg, closing open files), do something like this:

.startup
LDA BRKV+0:STA oldBRK+0
LDA BRKV+1:STA oldBRK+1
LDA myBRK AND 255:STA BRKV+0
LDA myBRK DIV 256:STA BRKV+1
...etc
program finishes by dropping into:
.restoreBRK
LDA oldBRKV+0:STA BRKV+0
LDA oldBRKV+1:STA BRKV+1
RTS
\ my error handler that tidies up before aborting:
.myBRK
JSR restoreBRK
do whatever tidying up needed, eg close files I've opened
JMP (BRKV) ; pass error on to original handler

Code: Select all

$ bbcbasic
PDP11 BBC BASIC IV Version 0.25
(C) Copyright J.G.Harston 1989,2005-2015
>_

Coeus
Posts: 884
Joined: Mon Jul 25, 2016 11:05 am
Contact:

Re: Validating DFS (or ADFS) filenames

Post by Coeus » Fri Jan 05, 2018 11:48 am

jgharston wrote:A lot of my transient utilities that need to tidy up after an error (eg, closing open files), do something like this:

.startup
LDA BRKV+0:STA oldBRK+0
LDA BRKV+1:STA oldBRK+1
LDA myBRK AND 255:STA BRKV+0
LDA myBRK DIV 256:STA BRKV+1
...etc
program finishes by dropping into:
.restoreBRK
LDA oldBRKV+0:STA BRKV+0
LDA oldBRKV+1:STA BRKV+1
RTS
\ my error handler that tidies up before aborting:
.myBRK
JSR restoreBRK
do whatever tidying up needed, eg close files I've opened
JMP (BRKV) ; pass error on to original handler
That's clever. By restoring BASIC's (or other language's) BRK handler before doing the tidying up this means if the tidying up throws another error that goes to the original language's handler and doesn't cause an infinite loop.

User avatar
jgharston
Posts: 3126
Joined: Thu Sep 24, 2009 11:22 am
Location: Whitby/Sheffield
Contact:

Re: Validating DFS (or ADFS) filenames

Post by jgharston » Sat Jan 06, 2018 12:05 pm

Coeus wrote:That's clever. By restoring BASIC's (or other language's) BRK handler before doing the tidying up this means if the tidying up throws another error that goes to the original language's handler and doesn't cause an infinite loop.
It's similar to the construction:

ON ERROR REPORT:PROCcloseall:PRINT:END
in%=OPENIN(blah)
rma%=FNm_alloc(size)
...
DEFPROCcloseall
in%=in%:IF in% THEN tmp%=in%:in%=0:CLOSE#tmp%
rma%=rma%:IF rma% THEN tmp%=rma%:rma%=0:PROCm_free(tmp%)
...
ENDPROC

See this wiki entry.

Code: Select all

$ bbcbasic
PDP11 BBC BASIC IV Version 0.25
(C) Copyright J.G.Harston 1989,2005-2015
>_

Post Reply