gno/usr.bin/getvers/getvers.asm
1998-04-29 00:42:47 +00:00

703 lines
20 KiB
NASM

*
* getvers version 2.0 for Apple IIGS
*
* Original getvers written by Ian Schmidt (Copyright 1993 Two Meg Software)
* Version 2.0 was created from a disassembly of getvers 1.2.
* by Dave Tribby for GNO/ME 2.0.6 * April 1998
* See README.getvers for additional implementation notes.
*
* $Id
*
; Macro library
MCOPY getvers.mac
; Predefined labels:
GSOS gequ $E100A8
;
; Locations used on direct page
;
CmdLn gequ $00 00-03 Command line address
CmdIndx gequ $04 04-05 index into Command line
RezHndl gequ $06 06-09 rVersion resource handle
RezAddr gequ $0A 0A-0D rVersion resource address
rezFieldAddr gequ $0E 0E-11 rVersion field address
AdrPStr gequ $12 12-15 pString addr for WritePString
RezRefNum gequ $16 16-17 ref num for resource file
CntryPStrAdr gequ $18 18-1B addr of country pString
; (Not used) UserID gequ $1C 1C-1D ID number of program
* -----------------------------------------------------------------
;
; Beginning of program
;
getvers start GETVERS
longa on
longi on
; Handle the GS/OS startup protocol for shell text programs
phk Use program bank register
plb to set data bank register.
sty CmdLn Save address of
stx CmdLn+2 command line.
; (Not used) sta UserID Save the user ID number.
; Setup for using stdout
jsr GetStdOutNum
; Start the Resource manager
pha
_ResourceStartUp
; Were parameters provided?
ldy #8 Start beyond shell identifier.
jsr GetNxtPrm 1st parameter is program name.
ChkPrm lda [CmdLn],y Get next command line
and #$FF character.
bne GetParam If not end-of-string, continue.
; No filename parameter: print the usage message and exit from the program
DoUsage PH4 #UsageMsg
jsr WritePString
PH4 #PStrNewLn
jsr WritePString
lda #1 Set return
sta rval status to 1.
;
; Shutdown and exit the program
;
AllDone _ResourceShutDown Close the resource manager.
jsr CloseStdout Close stdout.
lda rval Set status and
rtl exit from program.
;
; Process program's filename parameters
;
GetParam jsr GetNxtPrm Get filename
stx BufLen Set dest length.
sty CmdIndx Save cmd line pointer.
; Was it a valid option?
lda EndOptFlg If end-of-options flag is set,
bne Fname process parameter as pathname.
SHORT M Use short mode to access chars.
lda Buffer Get parameter.
cmp #'-' If 1st character isn't '-',
bne FrstName it must be a filename.
ldx #1
GetPCh lda Buffer,x Get option character.
cmp #'b' If it's 'b',
bne ChkPc
sta bflag set bflag.
bra BumpIt
ChkPc cmp #'c' If it's 'c',
bne ChkPq
sta cflag set cflag.
bra BumpIt
ChkPq cmp #'q' If it's 'q',
bne BadParam
sta qflag set qflag.
BumpIt inx Increment index.
cpx BufLen If not at end of buffer,
bcc GetPCh get the next parameter character.
LONG M Otherwise, restore long mode
ldy CmdIndx Restore cmd line pointer
bra ChkPrm and check if params are provided.
; First filename encountered
Frstname LONG M Restore long mode.
inc EndOptFlg Set end-of-options flag.
; Check for "quiet" mode
Fname lda qflag If -q option was specified,
bne PRF go process the rez file.
; Write the pathname
PH2 BufLen
PH4 #Buffer
jsr WriteBuf
; Write a colon and a space
PH4 #PstrColSp
jsr WritePString
; Try to open the file and print rVersion information
PRF jsr ProcessRezFile
ldy CmdIndx Restore cmd line pointer.
lda [CmdLn],y Get next command line
and #$FF character.
beq AllDone If end-of-string, all done.
lda bflag If "brief" option
ora qflag and "quiet" options aren't set,
bne GetParam
PH4 #PStrNewLn print newline separator.
jsr WritePString
bra GetParam Get next filename.
; A parameter was offered, but it's not "b", "c" or "q".
BadParam sta BadOpChr Save character in error message.
LONG M Restore long mode.
PH4 #BadOption Print "Illegal option"
jsr WritePString
PH4 #PStrNewLn
jsr WritePString
brl DoUsage Print usage and exit program.
;
; Subroutine to open and process the rez fork of the file named in Buffer
;
ProcessRezFile pha Word for result.
PH2 #1 Access mode = read.
PH4 #0 Ptr to map in mem (0: load from disk).
PH4 #BufLen Filename (GS/OS string format).
_OpenResourceFile Open the resource fork.
bcc OpenedOK If no error, go print the information.
ply Discard return value.
PrintErr ldy qflag If -q option was specified,
bne ErRtn don't print error message.
sta ErrGS_error Save error value.
; Check for two specific error cases
cmp #$0046 If error = $0046,
bne chkerr2
PH4 #E0046Msg print "File not found"
bra MsgRtn and exit the subroutine.
chkerr2 cmp #$0063 If error = $0063,
bne Generr
PH4 #E0063Msg print "No resource fork"
MsgRtn jsr WritePString
PH4 #PStrNewLn
jsr WritePString
; Return with error status
ErRtn lda #1 Set error status
sta rval
rts and return from ProcessRezFile.
; Print a generic error message
Generr jsl >GSOS Shell:ErrorGS
dc i2'$0145'
dc a4'ErrorGSbuf'
bra ErRtn
;
; Resource file was opened without error
;
OpenedOK PL2 RezRefNum Save rtn value (file ref num).
; Get file ID of resource file containing rVersion resource ID # 1
pha Space for result.
PH2 #$8029 Resource type: rVersion
PH4 #1 ID: 1
_HomeResourceFile
pla If return value
cmp RezRefNum != loaded rez file number,
beq LoadVRez
ldy qflag and -q option wasn't specified,
bne ErRtn
PH4 #NoRezMsg print error message,
jsr WritePString
PH4 #PStrNewLn
jsr WritePString
bra ErRtn Set err stat & rtn from ProcessRezFile.
;
; The resource exists in the target file. Load it.
;
LoadVRez pha Reserve space for
pha return value.
PH2 #$8029 Resource type: rVersion
PH4 #1 ID: 1
_LoadResource
bcc GotRez If error getting resource,
plx Remove the
plx returned value,
brl PrintErr Print error msg and exit.
;
; The rVersion resource was successfully loaded. Print its information.
;
GotRez PL4 RezHndl Rtn value = resource handle.
ldy #4 Make sure resource
lda [RezHndl],y doesn't move by
ora #$8000 setting AttrLocked bit
sta [RezHndl],y in handle.
lda [RezHndl] De-reference
sta RezAddr handle
ldy #2 and
lda [RezHndl],y save
sta RezAddr+2 address.
; Convert version field to pString using _VersionString
pea 0 Flags (= 0).
lda [RezAddr],y Version (long).
pha
lda [RezAddr]
pha
PH4 #VersPStr Address of result.
_VersionString
ldy #4
lda [RezAddr],y Get country code.
cmp #55 If >= 55,
bcc shift
lda #30 use 30 (an unknown country).
shift asl a Multiply country code
asl a by 4
tax and use as index.
lda CntryAddrTbl,x Get address of pString
sta CntryPStrAdr of country name and
lda CntryAddrTbl+2,x store in CntryPStrAdr.
sta CntryPStrAdr+2
clc
lda RezAddr Get rVersion address,
adc #6 add 6
sta rezFieldAddr and store pointer
lda RezAddr+2 to name of
adc #0 product.
sta rezFieldAddr+2
; Print product name
PH4 rezFieldAddr
jsr Wrt8bitPStr
; Print a single space
PH4 #PstrSpace
jsr WritePString
; Print version number
PH4 #VersPStr
jsr WritePString
; Print newline
PH4 #PStrNewLn
jsr WritePString
; If "brief" option is set, skip moreInfo and Country.
lda bflag
bne ChkCflag
; Calculate address of moreInfo field
lda [rezFieldAddr] Get product name's
and #$FF length byte.
inc a Add one for length byte itself.
clc
adc rezFieldAddr Add to addr of product name.
sta rezFieldAddr Store addr of moreInfo field.
bcc PrintMI
inc rezFieldAddr+2
; Print moreInfo field
PrintMI PH4 rezFieldAddr
jsr Wrt8bitPStr
; Print newline
PH4 #PStrNewLn
jsr WritePString
; Print "Country: "
PH4 #CountryPStrK
jsr WritePString
; Print country name
PH4 CntryPStrAdr
jsr WritePString
; Print newline
PH4 #PStrNewLn
jsr WritePString
; If "comment" option is set, load and print Comment resource.
ChkCflag lda cflag
beq CloseRez
; Get file ID of resource file containing rComment resource ID # 1
pha Space for result.
PH2 #$802A Resource type: rVersion
PH4 #1 ID: 1
_HomeResourceFile
pla If return value
cmp RezRefNum != loaded rez file number,
bne CloseRez skip it.
pha Reserve space for
pha return value.
PH2 #$802A Resource type: rVersion
PH4 #1 ID: 1
_LoadResource
bcs CloseRez If error, skip it.
;
; The rComment resource was successfully loaded. Print its information.
;
PL4 RezHndl Rtn value = resource handle.
ldy #4 Make sure resource
lda [RezHndl],y doesn't move by
ora #$8000 setting AttrLocked bit
sta [RezHndl],y in handle.
lda [RezHndl] De-reference
sta GSWriteAdr handle
ldy #2 and
lda [RezHndl],y save
sta GSWriteAdr+2 address.
pha Reserve space for
pha result.
PH4 RezHndl Get size of
_GetHandleSize rComment resource.
PL4 GSWriteLen Save length.
jsr Wrt8bitStr Convert and write comment string.
; Print newline
PH4 #PStrNewLn
jsr WritePString
; Done with the resource file; close it
CloseRez PH2 RezRefNum fileID
_CloseResourceFile
Rtn rts Return from ProcessRezFile.
;
; Subroutine to copy next word of input line into Buffer
; Upon entry: Y-reg = index into CmdLn (beginning of parameter)
; Upon exit: Y-reg = index into CmdLn (beyond end of parameter)
; A-reg = char beyond end: [CmdLn],y (0 or ' ')
; X-reg = length of parameter
;
GetNxtPrm ldx #0 Destination index and len = 0.
SHORT M Use short mode to access chars.
; Skip leading blanks.
SkipSp lda [CmdLn],y Get next character.
beq EndParam If null character, at end of string.
cmp #' ' If not blank,
bne SaveCh found start of parameter.
iny If blank, skip it
bra SkipSp and get the next character.
SaveCh sta Buffer,x Save char in Buffer.
iny Increment source and
inx destination pointers.
lda [CmdLn],y Get next character.
beq EndParam If null character, found end.
cmp #' ' If not a blank,
bne SaveCh keep getting characters.
; Found the end of parameter (or end of string)
EndParam LONG M Restore long mode.
rts Return from GetNxtPrm.
;
; Subroutine to ensure that stdout is open
;
GetStdOutNum jsl >GSOS GetStdRefNum
dc i2'$2037'
dc a4'GetSRefBlk'
lda GSRrefNum Use current refnum, unless
bcc SvRefNum stdout isn't already open.
jsl >GSOS GSOpen "Console"
dc i2'$2010'
dc a4'OpenBlock'
lda OpenRefNum and use that refnum.
inc OpenFlag Set "open" flag.
SvRefNum sta StdOutRefNum Save stdout refnum.
rts Return from GetStdOutNum.
;
; Subroutine to close stdout
;
CloseStdout lda OpenFlag If this program opened .CONSOLE
beq GoBack for use as stdout,
lda OpenRefNum
sta GSClose_ref
jsl >GSOS Close the file.
dc i2'$2014'
dc a4'GSClosePB'
GoBack rts Return from CloseStdout.
;
; Subroutine to write the Pascal string whose address is on the stack
;
WritePString plx Hold the return address.
PL4 AdrPStr Get pString address.
phx Restore return address.
clc Text starts at one
lda AdrPStr byte beyond the
adc #1 beginning of the
sta GSWriteAdr pString. Store
lda AdrPStr+2 addr in GSWrite
adc #0 Param Block.
sta GSWriteAdr+2
lda [AdrPStr] Get pString length
and #$FF (byte).
sta GSWriteLen Store in Param Block
stz GSWriteLen+2 (long).
jsl >GSOS GSWrite
dc i2'$2013'
dc a4'GSWritePB'
rts Return from WritePString.
;
; Subroutine to write the Pascal string whose address is on the stack
; and whose contents may contain 8-bit characters.
;
Wrt8bitPStr plx Hold the return address.
PL4 AdrPStr Get pString address.
phx Restore return address.
clc Text starts at one
lda AdrPStr byte beyond the
adc #1 beginning of the
sta GSWriteAdr pString. Store
lda AdrPStr+2 addr in GSWrite
adc #0 Param Block.
sta GSWriteAdr+2
lda [AdrPStr] Store pString length
and #$FF in GSWrite Param Block.
sta GSWriteLen
;
; Alternate entry point to write 8-bit characters, with address
; and length already stored at GSWriteAdr and GSEriteLen.
;
Wrt8bitStr anop
; Convert 8-bit characters to printable
pha Reserve space for
pha return values.
PH2 #$4000 Flags: allow longer subs.
PH4 GSWriteAdr Pointer to source text.
PH2 GSWriteLen Source text length.
PH4 #BufSize Pointer to destination.
_StringToText
PL2 GSWriteLen Save length.
pla Ignore resultFlags.
PH4 #Buffer Set GSWrite address
PL4 GSWriteAdr to the result buffer.
jsl >GSOS GSWrite
dc i2'$2013'
dc a4'GSWritePB'
rts Return from Wrt8bitPStr/Wrt8bitStr.
;
; Subroutine to write text; Addr and Len passed on stack
;
WriteBuf plx Hold return address.
PL4 GSWriteAdr Move parameters into
PL2 GSWriteLen GS Write
stz GSWriteLen+2 Param Block.
phx Restore return address.
jsl >GSOS GSWrite
dc i2'$2013'
dc a4'GSWritePB'
rts Return from WriteBuf.
; -------------------------------------------------------------------
; Miscellaneous program constants and storage
; -------------------------------------------------------------------
; Usage message
UsageMsg dw 'usage: getvers [-b] [-c] [-q] file ...'
; Bad option error message (pString)
BadOption dc i1'BadOpChr-BadOption'
dc c'illegal option -- '
BadOpChr dc c' '
; File open error pStrings
E0046Msg dw 'File not found'
E0063Msg dw 'No resource fork'
NoRezMsg dw 'No version resource'
; Indicates whether this program opened stdout
OpenFlag dc i2'0'
; Status value returned to shell
rval dc i2'0'
; Parameter flags
EndOptFlg dc i2'0' Has end-of-options been reached?
bflag dc i2'0' Has -b option been specified?
cflag dc i2'0' Has -c option been specified?
qflag dc i2'0' Has -q option been specified?
; Miscellaneous pString constants:
PstrColSp dw ': ' Colon and space
PstrSpace dw ' ' Space
PStrNewLn dc h'010D' Newline
; pString Version (filled in by call to SetVersion)
VersPStr dw ' '
; Constants used for printing country information
CountryPStrK dw 'Country: ' Label
; Table of addresses for country pStrings
CntryAddrTbl dc a4'US,France,Britain,Germany,Italy,Netherlands,BelgiumLux'
dc a4'Sweden,Spain,Denmark,Portugal,FrCanada,Norway,Israel'
dc a4'Japan,Australia,Arabia,Finland,FrSwiss,GrSwiss,Greece'
dc a4'Iceland,Malta,Cyprus,Turkey,Yugoslavia'
dc a4'Unknown,Unknown,Unknown,Unknown,Unknown,Unknown,Unknown'
dc a4'India,Pakistan'
dc a4'Unknown,Unknown,Unknown,Unknown,Unknown,Unknown'
dc a4'Lithuania,Poland,Hungary,Estonia,Latvia,Lapland'
dc a4'FaeroeIsl,Iran,Russia,Ireland,Korea,China,Taiwan'
dc a4'Thailand'
; Message printed when country is not valid
Unknown dw 'unknown to this version of getvers'
; pStrings for the various countries
US dw 'United States'
France dw 'France'
Britain dw 'Britain'
Germany dw 'Germany'
Italy dw 'Italy'
Netherlands dw 'Netherlands'
BelgiumLux dw 'Belgium/Luxembourg'
Sweden dw 'Sweden'
Spain dw 'Spain'
Denmark dw 'Denmark'
Portugal dw 'Portugal'
FrCanada dw 'French Canadian'
Norway dw 'Norway'
Israel dw 'Israel'
Japan dw 'Japan'
Australia dw 'Australia'
Arabia dw 'Arabia'
Finland dw 'Finland'
FrSwiss dw 'French Swiss'
GrSwiss dw 'German Swiss'
Greece dw 'Greece'
Iceland dw 'Iceland'
Malta dw 'Malta'
Cyprus dw 'Cyprus'
Turkey dw 'Turkey'
Yugoslavia dw 'Bosnia/Herzegovena/Yugoslavia/Croatia'
India dw 'India'
Pakistan dw 'Pakistan'
Lithuania dw 'Lithuania'
Poland dw 'Poland'
Hungary dw 'Hungary'
Estonia dw 'Estonia'
Latvia dw 'Latvia'
Lapland dw 'Lapland'
FaeroeIsl dw 'Faeroe Islands'
Iran dw 'Iran'
Russia dw 'Russia'
Ireland dw 'Ireland'
Korea dw 'Korea'
China dw 'China'
Taiwan dw 'Taiwan'
Thailand dw 'Thailand'
; GS/OS input/result string: two length words followed by 256 bytes
BufSize dc i2'260' Total size (when used as result buf)
BufLen dc i2'0' Num chars used in buffer
Buffer ds 256 Storage area (256 bytes)
; Parameter Block used for ErrorGS
ErrorGSbuf dc i2'1' Number of parameters
ErrGS_error dc i2'0' Error code
; Parameter block for GSOpen of .CONSOLE
OpenBlock dc i2'4' pCount
OpenRefNum dc i2'0' refNum
OpenPath dc i4'ConName' pathname
OpenAccess dc i2'2' requestAccess (write)
OpenRNum dc i2'0' resourceNumber (open data fork)
; ".CONSOLE" as a GS/OS input string
ConName dc i2'8'
dc c'.CONSOLE'
; GetStdRefNum parameter block
GetSRefBlk dc i2'2' pCount
GSRpfixNum dc i2'11' prefixNum
GSRrefNum dc i2'0' refNum (returned)
; Parameter block for GSClose
GSClosePB dc i2'1' pCount
GSClose_ref dc i2'0' refNum
; Parameter block used for GSWrite in WriteBuf and WritePString
GSWritePB dc i2'4' pCount
StdOutRefNum dc i2'0' device number
GSWriteAdr dc a4'0' buffer
GSWriteLen dc i4'0' request count
dc i4'0' starting block
end
; -----------------------------------------------------------------
; Segment for direct-page and stack
stack data STACK
kind $12
; Fill the direct-page/stack pages with question marks,
; so they can be examined for use during execution.
dc 128c'?'
dc 128c'?'
dc 128c'?'
dc 128c'?'
end