From: jhmeyers@miu.edu (John H Meyers)
Newsgroups: comp.sys.hp48
Subject: CallerID programs (getting their own variable name)
Date: 14 Nov 1998 00:43:40 GMT
Organization: MIU Computer Services,  Fairfield, IA 52557.  Not Approved.
Lines: 163
Message-ID: <72ijns$g4n$1@news.iastate.edu>
References: <19981110181309.03049.00001768@ng149.aol.com>,<72f9ut$udo$2@news.iastate.edu>
Reply-To: jhmeyers@miu.edu
NNTP-Posting-Host: vax1.mum.edu
Xref: republic.btigate.com comp.sys.hp48:18533

Can a program stored in a user variable determine its own name?

Two methods are presented here:

o For SysRPL/ML programs, a sample program below shows
  simple coding you can include in your program which will
  obtain the variable name, although not identifying the directory.

o For UserRPL or SysRPL programs, we also have a separate
  function which you can call; it will return to you the
  complete path name of the variable containing the program
  from which you made the call, e.g. { HOME MYDIR MYPROG }
  Follow with DUP SIZE GET for just the simple variable name.
  This program is fastest when the variable being located is
  not towards the end of a directory of vastly many variables.

For either of these methods to function properly,
your program must reside in a user variable;
otherwise the results may be meaningless.

Programs copied to temporary memory will return
null output values; programs in uncovered port memory (0-1)
will return a port object name or a meaningless path.

What can you do with this information?
Perhaps you can use it for recursive programs
(which need to call themselves), or include the name in messages,
or decide that this isn't all that useful, after all.

"Hunger is a thing of persons outside windows;
 the entering takes away."  - Emily Dickinson

---------------------------------------------

An example for programmers who use SysRPL/ML:

* A program which puts its own name on the stack
( This is a *model* of how you could code your own program )

ASSEMBLE
LBLabc
RPL
::
  CK0NOLASTWD ( Any normal program beginning )

  ( ... any additional programming ... )

  ( Push this program's address on stack )
  CODE
	AD0EX
	D0=A
	LC(5)	LBLxyz
	A=A-C	A
	GOVLNG	=PUSHA
LBLxyz	EQU	(*)-LBLabc
ENDCODE
  ( Omit next line to just keep the pointer )
  RAM-WORDNAME ( Get variable name from pointer )

  ( ... any additional programming ... )

;


An ASC version of just the above skeleton program,
which does nothing other than return its name:

%%HP: T(1); @ 26 bytes, #71D9h
"D9D2051A81CCD20B100013213034A2000EA8D68A303E280B21309D17"

How to use the ASC format is explained in the FAQ:
<http://www.engr.uvic.ca/~aschoorl/faq/>
and on Goodies Disk #1 at www.hpcalc.org


An ASC version of a callable function which
returns a complete path when called by a user program:

%%HP: T(1); @ 143.5 bytes, #C4DDh
"D9D20D488151A81A5D80CCD2012000D91361841421348FB97608DC753029D80A
5D802A1708813062380D9D202A170EE170D9D2088130CCD20310008FC15238DC
7530C121621D36D9D20DB12639916D9D20B9F06E4116B21303E2803223080D80
C848132230AF250B482680D80E4116E4116B213067380B21305E1708523080D8
047A20B2130E4116B2130BA170B2130DD4C"


Source code for the above program:

* Find path of variable from which this was called
( This is the Jazz version; see comments for RPLCOMP )

( ASSEMBLE )
( NIBASC \HPHP48-Q\ )
( RPL )
::
  0LASTOWDOB! ( CK0NoBlame )
  CK0NOLASTWD
  CONTEXT@ ( Original directory )
  ( Get return address within calling program )
  CODE
	C=B	A
	CD0EX
	D0=D0-	5
	A=DAT0	A
	D0=C
	GOSBVL	=SAVPTR
	GOVLNG	=PUSH#ALOOP
ENDCODE
  SYSCONTEXT ( Start in HOME directory )
  CONTEXT@
* ----- Stack -----
* Original context
* Return address
* rrp
* ramwords
* address or type
* -----------------
  BEGIN ( Next directory level )
    DUP
    LASTRAM-WORD ( First object in rrp )
    ::
      BEGIN ( Objects in rrp )
	WHILE
	:: ( Remove for RPLCOMP )
	  DUP
	  ( Get address of object )
	  CODE
	      GOSBVL	=PopASavptr
	      GOVLNG	=PUSH#ALOOP
ENDCODE
	  4PICK ( Calling program address )
	  #<case ( Object contains calling program )
	  ::
	    DUPTYPERRP?
	    case
	    ::
	      SWAPDROP  ( Continue search in lower directory )
	      2RDROP
	    ;
	    RAM-WORDNAME ( Return full path for variable, exit )
	    SWAP
	    CONTEXT!
	    PATHDIR
	    SWAP
	    >TCOMP
	    UNROTDROP
	    CONTEXT!
	    2RDROP
	    2RDROP
	  ;
	  PREVRAM-WORD ( Next object in rrp )
	; ( Remove for RPLCOMP )
      REPEAT
      2DROP ( Variable not found )
      CONTEXT!
      { }
      2RDROP
    ;
  AGAIN
;

-----------------------------------------------------------
With best wishes from:   John H Meyers   <jhmeyers@mum.edu>