Knowing a Call from the Command Prompt VS from Program Code

Started by Ken Sproul, September 06, 2022, 11:03:13 PM

Previous topic - Next topic

Ken Sproul

Is there a way to know whether a program was called by entering the call from the command prompt vs. being called from a program line (such as running it or stepping into it)?


Given this program:
10 ! CALLED_PROG
20 ENTER ARG1$
30 IF SOMETHING THEN PRINT "CALLED FROM COMMAND LINE" ELSE PRINT "CALLED FROM PROGRAM"
40 EXIT


Call made from the command prompt:
->CALL "CALLED_PROG","TEST"


Call made from a program:
1010 CALL "CALLED_PROG","TEST"


What would SOMETHING be at line 30 in CALLED_PROG?


Keep in mind that in both cases, the name of the program being called could be referenced in a variable instead of quoted text.


I've played with the stk() function but that doesn't help if the call from the command line is done while stepping through a program because stk(-1) references the line/program stepped to.


I considered parsing the code pointed to by stk(-1) using lst(pgm(num(mid(stk(-1),1,5)),tcb(12)-1)), but even if the code at that line does in fact call the program, it still may have been called from the command prompt.


The one difference I am sure of is that when calling from a command prompt, there is no "next" statement to execute.  Is there a way to know this?

Ken Sproul
DPI Information Service, Inc.
Pivotal Systems LLC

Jeff Wilder

One way I can think of is to create a *cmd program which calls your "CALLED_PROG" instead of using the CALL directive from the prompt.

NEW
0010 ENTER arg1$,ERR=*NEXT; GOTO ArgsOK
0020 PRINT "You must supply an argument."
0030 END
0040 ArgsOK:
0050 CALL "CALLED_PROG",arg1$,1
0060 END
SAVE "*cmd/cp"


Change the CALLED_PROG to accept an extra argument:
0010 ! CALLED_PROG
0020 ENTER Arg1$,CmdPrompt,ERR=*NEXT
0030 IF CmdPrompt THEN PRINT "CALLED FROM COMMAND LINE" ELSE PRINT "CALLED FROM PROGRAM"
0040 EXIT


Call made from the command prompt:
->cp TEST

Call made from a program:
1010 CALL "CALLED_PROG","TEST"

EVa

Doesn't this work:

0025 LET SOMETHING=(NUM(MID(STK(-1),1,5))=0)

If I'm not mistaken, the line number of the caller when in console mode is '0'.  If you're in a program, it's the actual linenumber of that program.

Mike King

If the user enters the CALL from an escaped/paused running program then STK(-1) will have the statement at which the program is paused.

->load "calltest"
->list
0010 ESCAPE
0020 END
0030 label:
0040 ESCAPE ! In call
0050 EXIT
->run
0010 ESCAPE
1>call "calltest;label"
0040 ESCAPE ! In call
2>print stk(-1)
00010C:\pvxsrc\calltest
2>


Notice that STK(-1) indicates line 10 as that is where the ESCAOE is that paused execution.

Only if the call is issued from command line and the program is stopped/ended will the statement number be zero.

Mike King
President - BBSysco Consulting
eMail: mike.king@bbsysco.com

Ken Sproul

Thank you to all who replied.

I have the same need to know for *cmd programs which also can be entered at a command prompt or called using code in a program.

What I would like to know is how the program was initiated, either entered at a command prompt or called/performed in a program, regardless of the procedures and arguments that may be involved.

Wish: Maybe a new tcb() flag could be established to indicate this. :)
Ken Sproul
DPI Information Service, Inc.
Pivotal Systems LLC

Mike King

Ken,

Why does your code need to know if the call was done from command mode or a running program?

Generally systems attempt to avoid end-users from getting to command mode using something like the 'XT' system parameter.
Mike King
President - BBSysco Consulting
eMail: mike.king@bbsysco.com