Issue while passing RC from MACRO to caller REXX program - macros

I am executing a MACRO to find string (using regular expression), and want to handle the return code of that regular expression execution in my caller REXX in mainframe.
I tried using PARM and, VGET and VPUT, but no positive result for me.
/* REXX */
ADDRESS ISPEXEC "VPUT (MRC) SHARED"
ADDRESS ISPEXEC "VIEW DATASET('XXXX.XXXX.XXXX') MACRO(REGEX)
SAY "RC IN CALLER:" RC
SAY "MRC:" MRC
Macro defined:
/* REXX */
ADDRESS ISREDIT
"MACRO"
ADDRESS ISPEXEC "VGET (MRC) SHARED"
"F RC'[0-9]{16}'"
MRC = RC
SAY "INSIDE MACRO:" MRC
ADDRESS ISREDIT 'END'
Also tried reversing VGET and VPUT in macro and caller REXX and also changes the place of the statement. Still no result.
Output:
INSIDE MACRO: 0 (when string found)
RC IN CALLER: 0
MRC:
INSIDE MACRO: 4 (when string not found)
RC IN CALLER: 0
MRC:
Could any one please suggest.

Related

Ada - Commando- line reader and processer

A program that loads and processes command-line arguments should be created.
Here comes a few examples on how it should look when you run it (bold text is the text that the user will type):
Terminal prompt % **./my_program**
No arguments given.
Terminal prompt % **./my_program 123**
Wrong amounts of arguments given.
Terminal prompt % **./my_program 10 XYZ 999 Greetings!**
Wrong amounts of arguments given.
Terminal prompt % **./my_program 3 HELLO**
Message: HELLOHELLOHELLO
The program "./my program" is ending.
Terminal prompt % **./my_program 0 Bye**
Message:
The program "./my program" is ending.
This is my code so far:
with Ada.Text_IO; use Ada.text_IO;
with Ada.Integer_Text_IO; use Ada.Integer_Text_IO;
with Ada.Command_Line; use Ada.Command_Line;
procedure my_program is
type String is array (Positive) of Character;
N : Integer;
Text : String;
begin
N := Argument_Count;
if N = 0 then
Put_Line("No arguments given.");
elsif N /= 2 then
Put_Line("Wrong number of arguments given.");
elsif N = 2 then
Put("Message: ");
for I in 1 .. N loop
Put(Text);
New_Line;
end loop;
Put("The program """);
Put(""" is ending. ");
end if;
end my_program;
My program handles the first 3 three cases but when I go ahead with the 4th and 5th (last) case I get an error code at the row Put(Text) where it says
Missing argument for parameter "Item" in call to "Put"
I don't know if I declared my string right because I don't want a string of a specific length. Can anyone come up with something that could help me solve case 4 and 5? It would be nice and highly appreciated
This seems to be a homework or exam question, so I would usually not provide a full answer. But Chris already gave that (with some defects), so here is my suggestion. Compared to Chris's solution, I try to avoid using unnecessary variables, and I favour case statements over if-then-else cascades, and I try to reduce the scope of exception handlers. I prefer to put use clauses in the subprogram so that the context-clause section contains only with clauses. I use the string-multiplying "*" operator from Ada.Strings.Fixed, but that is perhaps an unnecessary refinement.
with Ada.Command_Line;
with Ada.Strings.Fixed;
with Ada.Text_IO;
procedure My_Program
is
use Ada.Strings.Fixed;
use Ada.Text_IO;
begin
case Ada.Command_Line.Argument_Count is
when 0 =>
Put_Line ("No arguments given.");
when 2 =>
begin
Put_Line (
Natural'Value (Ada.Command_Line.Argument(1))
* Ada.Command_Line.Argument(2));
exception
when Constraint_Error =>
Put_Line ("Invalid input for argument 1.");
end;
when others =>
Put_Line ("Wrong amount of arguments given.");
end case;
Put_Line (
"The program """
& Ada.Command_Line.Command_Name
& """ is ending.");
end My_Program;
Note that my version:
Rejects negative first arguments (like "-3").
Outputs the repeated strings on a single line, as was required by the examples given.
Includes the name of the program in the final message, as was also required.
Given the clarification in comments as to the purpose of the program, to print a message n times where n is the first argument, and the message is the second argument, you need to parse the first argument as an integer. This can be done with Integer'Value.
Now, that raises the prospect of the user not running the program with an integer. So we have to handle the possible Constraint_Error exception.
with Ada.Text_IO; use Ada.text_IO;
with Ada.Integer_Text_IO; use Ada.Integer_Text_IO;
with Ada.Command_Line; use Ada.Command_Line;
procedure my_program is
argc : Integer;
N : Integer;
begin
argc := Argument_Count;
if argc = 0 then
Put_Line("No arguments given.");
elsif argc /= 2 then
Put_Line("Wrong number of arguments given.");
else
n := Integer'Value(Argument(1));
Put("Message: ");
for I in 1 .. N loop
Put_Line(Argument(2));
end loop;
Put("The program """);
Put(""" is ending. ");
end if;
exception
when Constraint_Error =>
Put_Line("Invalid input for argument 1.");
end my_program;
As an aside, when we've checked in our conditional if argc is zero, and that it doesn't equal two, we don't have to use elsif. The only other possibility is that it is 2.
You say
My program handles the first 3 three cases but when I go ahead with the 4th and 5th (last) case I get an error code at the row Put(Text) where it says "Missing argument for parameter "Item" in call to "Put". "
which doesn't make sense, because your program as shown doesn't compile. I guess what you mean is "when I try to add the code to handle cases 4 and 5, it doesn't compile".
The reason why it doesn’t compile is hidden in the actual error messages:
leun.adb:24:10: no candidate interpretations match the actuals:
leun.adb:24:10: missing argument for parameter "Item" in call to "put" declared at a-tiinio.ads:97, instance at a-inteio.ads:18
...
leun.adb:24:14: expected type "Standard.Integer"
leun.adb:24:14: found type "String" defined at line 7
leun.adb:24:14: ==> in call to "Put" at a-tiinio.ads:80, instance at a-inteio.
You have at line 7
type String is array (Positive) of Character;
which is both misleading and not what you meant.
It’s ’not what you meant’ because array (Positive) means an array of fixed length from 1 to Positive’Last, which will not fit into your computer’s memory. What you meant is array (Positive range <>).
Even with this correction, it's 'misleading' because although it would be textually the same as the declaration of the standard String in ARM 3.6.3(4), in Ada two different type declarations declare two different types. So, when you write Put(Text); the Put that you meant to call (the second in ARM A.10.7(16)) doesn’t match because it’s expecting a parameter of type Standard.String but Text is of type my_program.String.
Cure for this problem: don’t declare your own String type.

Passing a varying number of macro arguments as a string in System Verilog

I have an existing code that uses some macro definitions in order to display messages from my test cases. I want to change the implementation of these macros, however, as these macros are extensively used in already existing testcases, I am looking to reimplement their functionality witout having to modify how the macros are used.
Currently the macros are difined as such:
`define My_Info $write("INFO:"); $display
`define My_Error $write("ERROR:"); $display
In the testcases, example calls of these macros include:
`My_Info("This is an info message with arguments %0d, %0d, and %0d", var1, var2, var3);
`My_Info("My_ID", $psprintf("ID : %s", var4));
`My_Error("Failed to open file: %s ", fname);
Currently $display, displays the messages in the brakets.
What I want to do is to define the macros in a way that these messages in the brakets of the macro calls could be passed as a string argument to a function (for example the function my_msg(msg) where msg is a string and my_msg is a function that formats and returns this string to the log file.
My issue is, because in the testcases the macro calls have varying number of arguments as seen in the example above, I am not sure how to define the macros in a universal way.
Currenlty my solution is to define the macros like:
`define My_Info(string=" ", var1= , var2= , var3= , var4= ) my_msg($sformat(s,var1, var2, var3, var4)
But this relies on a finite number of arguments (in this case 5).
Is there a more elegant way of doing it?
You can workaround the lack of varying numbers of macro arguments by requiring an extra set of ()'s.
module top;
`define my_error(msg) begin $error({"My ID:",$sformatf msg}); end
int a,b;
initial begin
`my_error( ("hello") )
`my_error( ("A = %0d B = %0d", a,b) )
end
endmodule

Autohotkey / AHK - Param %1% not accessible within a function

I'm having a problem with accessing the %1% ( Startup Param that has been passed to the Script by the console ) in Autohotkey.
When I use the following code (outside of a function):
Msgbox %1%
I get the output of the Param that has been passed to the Script. But as soon as I use the following Code:
HelloWorld() {
Msgbox %1%
}
HelloWorld()
The output is empty.
I also tried to assign %1% to a global variable, or to pass it to the Function as a parameter but it didn't work for me neither.
Thank you
I believe the command line parameter variables are considered global variables, so in order to use them in a non-expression context inside a function you have to declare them as global:
HelloWorld() {
global 1
Msgbox %1%
}
HelloWorld()
It gets even more confusing once you want to use them in expressions (such as using % in the text argument for MsgBox), since they will be treated as numbers so you have to indirectly access them through variables:
HelloWorld() {
;global 1
; Neither of these two expressions access the variable named "1"
;Msgbox % 1
;Msgbox % %1%
; You have to do this instead:
p := 1
MsgBox % %p% ; p is translated to 1 and then "1" is used as a variable name
}
HelloWorld()
Note that doing this doesn't require global 1.
If you're using the newest version of AHK, you instead probably want to use the newly introduced built-in variable A_Args, which is an array that holds the command line parameters. Being built-in, it doesn't have to be declared global, and it ultimately makes the code clearer:
HelloWorld() {
MsgBox % "Number of command line args received: " A_Args.Length() "`n"
. "First argument: " A_Args[1]
}
HelloWorld()
Just declare your cli variables as Global - outside the function - to make them globally available to any and all internal functions. For me, this is how I do it with my version of AHK (Version 1.1.25.01):
Global 1, 2, 3
HelloWorld() {
MsgBox Hello`t1:`t%1%`n`t2:`t%2%`n`t3:`t%3%
}
HelloWorld()
Note, these are different command lines:
"Scripts\myScript.ahk" one two three
"Scripts\myScript.ahk" "one two" three
"Scripts\myScript.ahk" "one" "two three"
"Scripts\myScript.ahk" "one two three"
The first is three separate parameters, the second and third, only two and the last is only one param (2 and 3 exist, but are empty).
Hth,

SAS IML use of Mattrib with Macro (symget) in a loop

In an IML proc I have several martices and several vectors with the names of columns:
proc IML;
mydata1 = {1 2 3, 2 3 4};
mydata2 = {1 2, 2 3};
names1 = {'red' 'green' 'blue'};
names2 = {'black' 'white'};
To assign column names to columns in matrices one can copypaste the mattrib statement enough times:
/* mattrib mydata1 colname=names1;*/
/* mattrib mydata2 colname=names2;*/
However, in my case the number of matrices is defined at execution, thus a do loop is needed. The following code
varNumb=2;
do idx=1 to varNumb;
call symputx ('mydataX', cat('mydata',idx));
call symputx ('namesX', cat('names',idx));
mattrib (symget('mydataX')) colname=(symget('namesX'));
end;
print (mydata1[,'red']) (mydata2[,'white']);
quit;
however produces the "Expecting a name" error on the first symget.
Similar question Loop over names in SAS-IML? offers the macro workaround with symget, what produces an error here.
What is the correct way of using mattrib with symget? Is there other way of making a variable from a string than macro?
Any help would be appreciated.
Thanks,
Alex
EDIT1
The problem is in the symget function. The &-sign resolves the name of the matrix contained in the macro variable, the symget only returns the name of the macro.
proc IML;
mydata1 = {1 2 3};
call symputx ('mydataX', 'mydata1');
mydataNew = (symget('mydataX'));
print (&mydataX);
print (symget("mydataX"));
print mydataNew;
quit;
results in
mydata1 :
1 2 3
mydata1
mydataNew :
mydata1
Any ideas?
EDIT2
Function value solves the symget problem in EDIT1
mydataNew = value(symget('mydataX'));
print (&mydataX);
print (value(symget("mydataX")));
print mydataNew;
The mattrib issue but persists.
SOLVED
Thanks Rick, you have opened my eyes to CALL EXECUTE() statement.
When you use CALL SYMPUTX, you should not use quotes for the second argument. Your statement
call symputx ('mydataX', 'mydata1');
assigns the string 'mydata1' to the macro variable.
In general, trying to use macro variables in SAS/IML loops often results in complicated code. See the article Macros and loops in the SAS/IML language for an indication of the issues caused by trying to combine a macro preprocessor with an interactive language. Because the MATTRIB statement expects a literal value for the matrix name, I recomend that you use CALL EXECUTE rather than macro substitution to execute the MATTRIB statement.
You are also having problems because a macro variable is always a scalar string, whereas the column name is a vector of strings. Use the ROWCAT function to concatenate the vector of names into a single string.
The following statements accomplish your objective without using macro variables:
/* Use CALL EXECUTE to set matrix attributes dynamically.
Requires that matrixName and varNames be defined at main scope */
start SetMattrib;
cmd = "mattrib " + matrixName + " colname={" + varNames + "};";
*print cmd; /* for debugging */
call execute(cmd);
finish;
varNumb=2;
do idx=1 to varNumb;
matrixName = cat('mydata',idx);
varNames = rowcat( value(cat('names',idx)) + " " );
run SetMattrib;
end;

RC special variable in REXX?

how to assign a value to RC special variable in REXX?
/* REXX */
"LISTDS ?" /* Command that sets RC to 12 */
SAY 'RC IS' RC /* RC is 12 */
RC = X /* RC set to X */
SAY 'RC IS' RC /* RC is X */
The above works, there is nothing special about the RC variable except it will be over written by the return code from the last command.
So you can set it to whatever you want at least on a mainframe running Zos.
Maybe you need to provide more detail in your question like what type of Rexx it is (Classic or OO) and what environment you are using.
If you want to set the return value of your method you need to use the "return" commend and to get the return code with the "result", for example:
/* REXX - program A */
SAY "THIS IS PROG. A WITH RC = 4"
RETURN 4
/* REXX - PROGRAM B */
SAY "CALLING PROGRAM A..."
CALL PROG_A
RC = RESULT
SAY "RC = "RC " RETURN FROM PROGRAM A..."
As Deuian said before, RC is set by last command executed and more detail should be provided to get a precise answer (environment, goal/task, batch/interactive etc.).
A silly working way to set RC on Zos REXX is to make a buffer: RC is set to the buffer count (so if you need RC = 100 you should create 100 buffers...), see the example (I do not endorse the usage of this method, it's just a conjecture)
/* rexx */
'MAKEBUF'
say RC
'MAKEBUF'
say RC
'DROPBUF'
say RC
/* exec output */
1
2
0
***
Beware that the previous code leaves a buffer active! (another DROPBUF needed)
The SAY instructions will send screen prompts or include text in the output. If you want to set the RC to something that can be interpreted by subsequent steps in a job, try:
/* REXX */
setrc = X /* set a variable for RC to X */
exit(setrc)