我有调用COBOL的C程序。我想在调用COBOL程序之前设置开关。这通常使用COBSW环境变量完成。
当我在C程序中设置COBSW时,它被忽略了。 当我在调用程序之前设置COBSW时,检测到它很好。
如何在C中设置COBSW并让COBOL识别它?
以下是我尝试过的事情:
我删除了cobinit(),程序仍在运行。这向我表明,当C程序启动时,cobinit会以某种方式自动运行,并且它只会拾取在sh中设置的COBSW。
我在程序中添加了cobrescanenv()。
在cobputenv()之前移动了cobinit()。
以下是运行测试的sh脚本:
export C_INCLUDE_PATH=$COBDIR/include
PATH=$COBDIR/bin:$PATH
gcc -o callcobol.o -c -g -Wall -Wno-unused-variable -fPIC -Ibuild -Isrc callcobol.c
/opt/microfocus/VisualCOBOL/bin/cob -o callcobol -g callcobol.o -L/usr/local/lib -ldl -lrt -lpthread
cob -z cobsw.cbl -o TESTSW.so
unset COBSW
echo "Test ONE, COBSW not set:"
echo "We want the output to be:"
echo "SW0=ON sw1=ON sw2=ON sw3=ON sw4=ON sw5=ON sw6=OFF sw7=ON "
echo "Getting:"
./callcobol
# Output:
# value of COBSW is: +0+1+2+3+4+5-6+7
echo ""
echo ""
export COBSW=+1+2 # This should be ignore because we are setting COBSW in the c program.
echo "Test TWO, COBSW set to ${COBSW}, but should be overlaid by putenv() in C:"
echo "We want the output to be:"
echo "SW0=ON sw1=ON sw2=ON sw3=ON sw4=ON sw5=ON sw6=OFF sw7=ON "
echo "Getting:"
./callcobol
以下是callcobol.c
:
#include <stdlib.h>
#include <stdio.h>
#include "cobcall.h"
#include "cobmain.h"
#include "cobenv.h"
PFR cobgetfuncaddr(int type, const cobchar_t *name);
int cobinit (void);
int main(int argc, char *argv[])
{
cobchar_t *prog = (cobchar_t *) "TESTSW";
//char *cobswval;
const char *cobswname = "COBSW";
cobputenv( (cobchar_t *) "COBSW=+0+1+2+3+4+5-6+7"); // out dummy computed SWITCH values for the program
//cobswval = getenv( cobswname);
//fprintf(stdout,"value of COBSW is: %s\n", cobswval);
cobinit(); /* Initialize COBOL environment */
cobrescanenv();
PFR cobprog;
if ((cobprog = cobgetfuncaddr(0, prog)) == NULL)
{
fprintf(stderr,"ERROR: could not find cobol module %s\n", (char *) prog);
}
else
{
/* Loaded */
(*cobprog)(); /* Call it! */
}
cobtidy(); /* Close down COBOL environment */
return(0); // return a return code of zero
}
以下是cobsw.cbl
:
$SET CONFIRM
$SET IBMCOMP
$SET CHARSET"EBCDIC"
IDENTIFICATION DIVISION.
PROGRAM-ID. TESTSW.
ENVIRONMENT DIVISION.
CONFIGURATION SECTION.
SPECIAL-NAMES.
SWITCH-0 IS SW0 ON IS SW_0_ON
SWITCH-1 IS SW1 ON IS SW_1_ON
UPSI-2 ON IS SW_2_ON
UPSI-3 ON IS SW_3_ON
UPSI-4 ON IS SW_4_ON
UPSI-5 ON IS SW_5_ON
UPSI-6 ON IS SW_6_ON
UPSI-7 ON IS SW_7_ON.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
DATA DIVISION.
FILE SECTION.
WORKING-STORAGE SECTION.
01 rec-cnt pic 9(11) value zero.
01 sw-out.
02 filler pic x(4) value "SW0=".
02 SW0-status pic x(3).
02 filler pic x(5) value " sw1=".
02 SW1-status pic x(3).
02 filler pic x(5) value " sw2=".
02 SW2-status pic x(3).
02 filler pic x(5) value " sw3=".
02 SW3-status pic x(3).
02 filler pic x(5) value " sw4=".
02 SW4-status pic x(3).
02 filler pic x(5) value " sw5=".
02 SW5-status pic x(3).
02 filler pic x(5) value " sw6=".
02 SW6-status pic x(3).
02 filler pic x(5) value " sw7=".
02 SW7-status pic x(3).
01 acc-data pic x(80) value spaces.
LINKAGE SECTION.
PROCEDURE DIVISION.
MAIN-PARAGRAPH.
move "OFF" to SW0-status SW1-status SW2-status
SW3-status SW4-status SW5-status
SW6-status SW7-status.
IF SW_0_ON move "ON " to SW0-status.
IF SW_1_ON move "ON " to SW1-status.
IF SW_2_ON move "ON " to SW2-status.
IF SW_3_ON move "ON " to SW3-status.
IF SW_4_ON move "ON " to SW4-status.
IF SW_5_ON move "ON " to SW5-status.
IF SW_6_ON move "ON " to SW6-status.
IF SW_7_ON move "ON " to SW7-status.
display sw-out.
EXIT PROGRAM.
END PROGRAM TESTSW.
这是脚本输出:
Test ONE, COBSW not set:
We want the output to be:
SW0=ON sw1=ON sw2=ON sw3=ON sw4=ON sw5=ON sw6=OFF sw7=ON
Getting:
SW0=OFF sw1=OFF sw2=OFF sw3=OFF sw4=OFF sw5=OFF sw6=OFF sw7=OFF
Test TWO, COBSW set to +1+2, but should be overlaid by putenv() in C:
We want the output to be:
SW0=ON sw1=ON sw2=ON sw3=ON sw4=ON sw5=ON sw6=OFF sw7=ON
Getting:
SW0=OFF sw1=ON sw2=ON sw3=OFF sw4=OFF sw5=OFF sw6=OFF sw7=OFF
答案 0 :(得分:1)
在进程初始化期间,COBOL运行时开关从环境变量COBSW或命令行读取一次。
很遗憾,设置COBSW不起作用,因为运行时已经存在 阅读COBSW。
所有内容都不会丢失,因为您可以使用COBOL中的X"91" function 11 API来设置开关。