PxPlus User Forum

Twitter Twitter Twitter

Author Topic: Knowing a Call from the Command Prompt VS from Program Code  (Read 1826 times)

Ken Sproul

  • Gold Member
  • ****
  • Posts: 61
    • View Profile
Knowing a Call from the Command Prompt VS from Program Code
« on: September 06, 2022, 11:03:13 PM »
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

  • Silver Member
  • ***
  • Posts: 42
    • View Profile
Re: Knowing a Call from the Command Prompt VS from Program Code
« Reply #1 on: September 07, 2022, 01:45:52 AM »
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.

Code: [Select]
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:
Code: [Select]
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

  • Gold Member
  • ****
  • Posts: 57
    • View Profile
    • EDIAS
Re: Knowing a Call from the Command Prompt VS from Program Code
« Reply #2 on: September 07, 2022, 03:09:09 AM »
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

  • Diamond Member
  • *****
  • Posts: 3830
  • Mike King
    • View Profile
    • BBSysco Consulting
Re: Knowing a Call from the Command Prompt VS from Program Code
« Reply #3 on: September 07, 2022, 11:29:50 AM »
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

  • Gold Member
  • ****
  • Posts: 61
    • View Profile
Re: Knowing a Call from the Command Prompt VS from Program Code
« Reply #4 on: September 08, 2022, 04:02:42 AM »
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

  • Diamond Member
  • *****
  • Posts: 3830
  • Mike King
    • View Profile
    • BBSysco Consulting
Re: Knowing a Call from the Command Prompt VS from Program Code
« Reply #5 on: September 08, 2022, 09:14:52 AM »
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