
/************************************************************************
 * Copyright (C) 1986-1999 Micro Focus International Lid. All rights reserved.
 *  
 * This demonstration program is provided for use by users of 
 * Micro Focus products (the "Software") and may be used, 
 * modified and distributed as part of your application provided 
 * that you properly acknowledge the copyright of Micro Focus in this
 * material.
 *     
 * All conditions, warranties and undertakings, express or implied, 
 * statutory or otherwise, on the part of Micro Focus are excluded, 
 * including without limitation those of merchantability or fitness  
 * for purpose. This shall not, however, purport to exclude or 
 * restrict the liability of Micro Focus to any extent not 
 * permitted by law.
 *
 * IN NO EVENT WILL MICRO FOCUS BE LIABLE FOR ANY INDIRECT, 
 * INCIDENTAL, SPECIAL, CONSEQUENTIAL OR OTHER DAMAGES ARISING OUT 
 * OF THE USE OF THE DEMONSTRATION PROGRAM DISTRIBUTED WITH THE 
 * SOFTWARE, WHETHER OR NOT INFORMED OF THE POSSIBILITIES OF 
 * DAMAGES IN ADVANCE.  THESE LIMITATIONS APPLY TO ALL CAUSES OF 
 * ACTION, INCLUDING BREACH OF CONTRACT, BREACH OF WARRANTY, STRICT
 * LIABILITY, AND ANY ACTIONS IN TORTS.
 ************************************************************************
 *
 * Start of source module cmain1.c
 *
 ************************************************************************/

#include <stdio.h>
#include "cobmain.h"
#include "cobcall.h"

#include "cmain1.h"

int main()
{
    struct param1 param;
    struct param2 paramb;
    char text[80];
    void * ptr;


    /**
    *** First off, lets just let everyone know we're starting
    *** in the correct place.
    **/
    printf("In C main1\n");

    /**
    *** Now we need to initialise the Cobol system before we
    *** start making any calls to Cobol.
    **/
    cobinit();


    /**
    *** ************************************************************
    *** The first call to a function is to be made using the
    *** cobcall facility. In this we need to pass a char**
    *** which points to our list of parameters. In this case
    *** we're only passing one parameter, which is the address
    *** of a structure.
    *** ************************************************************
    **/

    /**
    *** ...make sure the structure is empty.
    **/
    param.a     = 0;
    param.b     = 0;
    param.c     = 0;
    param.d     = '\0';

    /**
    *** Setup a pointer to the address of our structure.
    *** We can pass the address of the pointer to cobcall
    *** as a (char**).
    **/
    ptr = (void*)&param;

    /**
    *** ...call that function with 1 parameter
    **/
    cobcall((cobchar_t*)"cobsub1", 1, (cobchar_t**)&ptr);

    /**
    *** ...and now cancel it, as we don't need it anymore...
    **/
    cobcancel((cobchar_t*)"cobsub1");

    /**
    *** Now we just check to make sure that the COBOL function
    *** has changed our parameters as expected.
    **/
    if((param.a == 10.5) && (param.b == 10) &&
					(param.c == 10) && (param.d == 'a'))
	printf("PASS\n");
    else
	printf("FAIL\n");


    /**
    *** ************************************************************
    *** ...now we'll try again without using cobcall. Instead we'll
    *** attempt a straight C call and see if the cob -d.... option
    *** has worked correctly.
    *** ************************************************************
    **/

    /**
    *** ...make sure the structure is empty.
    **/
    param.a     = 0;
    param.b     = 0;
    param.c     = 0;
    param.d     = '\0';

    /**
    *** ...call that function with 1 parameter
    **/
    cobsub2(&param);

    /**
    *** Now we just check to make sure that the COBOL function
    *** has changed our parameters as expected.
    **/
    if((param.a == 10.5) && (param.b == 10) &&
					(param.c == 10) && (param.d == 'a'))
	printf("PASS\n");
    else
	printf("FAIL\n");

    /**
    *** ************************************************************
    *** This last call to a COBOL function is similar to the
    *** previous call, except paramb.p_pid is a pid_t type. This
    *** also shows the advantages of h2cpy.
    *** ************************************************************
    **/

    /**
    *** We'll setup some useless info for COBOL to check this time.
    **/
    strcpy(text, "Here is some text");
    paramb.p_pid = getpid();
    paramb.p_status = 'a';
    paramb.p_string = text;
    paramb.p_price = 10.34;

    /**
    *** ...and then call the function.
    **/
    cobsub3(&paramb);

    /**
    *** ...and just check if all went well.
    **/
    if(paramb.p_status == 'b')
	printf("PASS\n");
    else
	printf("FAIL\n");

    /**
    *** Now we just need to deinitialise the Cobol system.
    **/
    cobtidy();

    printf("Bye\n");
    return 0;
}

