1

我有调用 COBOL 的 C 程序。我想在调用 COBOL 程序之前设置开关。这通常使用 COBSW 环境变量来完成。

当我在 C 程序中设置 COBSW 时,它被忽略了。当我在调用程序之前设置 COBSW 时,它被检测到了。

如何在 C 中设置 COBSW 并让 COBOL 识别它?

这是我尝试过的一些事情:

  1. 我删除了 cobinit() 并且程序仍然运行。这向我表明,当 C 程序启动时,cobinit 在某种程度上是自动的,它只会拾取 sh 中设置的 COBSW。

  2. 我将 cobrescanenv() 添加到程序中。

  3. 将 cobinit() 移到 cobputenv() 之前。

这是运行测试的 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
4

1 回答 1

1

COBOL 运行时开关在进程初始化期间从环境变量 COBSW 或命令行读取一次。

所以不幸的是,设置 COBSW 将不起作用,因为运行时已经读取了 COBSW。

一切都不会丢失,因为您可以使用 COBOL 中的X"91" 函数 11 API来设置开关。

于 2015-09-02T07:43:07.130 回答