In section “Example: Program for copying files” an Assembler program KOP for copying SAM and ISAM files was created. The following example is intended to show how to achieve similar results with a C program. In addition to the SDF standard statements, the program KOPC has the following statement:
COPY-FILE |
FROM-FILE = <filename 1..54> ,TO-FILE = <filename 1..54 without-gen-vers>(...) <filename 1..54 without-gen-vers>(...) ACCESS-METHOD = *SAME *ISAM(...) KEY-LENGTH = *STD / <integer 1..50> ,RECORD-SIZE = *SAME / *VARIABLE / <integer 1..2048> ,PASSWORD = *NONE / <c-string 1..4> / *SECRET-PROMPT |
Defining the program in the user syntax file
The KOPC program is defined in the syntax file SDF.USER.SYNTAX. This is done by passing the same statements to SDF-A as are given on "Example: Program for copying files", except for statements 4 and 5 (Example: Program for copying files), which are entered as follows:
//add-prog kopc --------------------------------------------------------- (
4)
//add-stmt name=copy-file,prog=kopc,intern-name=copyfi,stmt-vers=1 ------ (
5)
Generating the program
Excerpts from the KOPC program are reproduced on the following pages.
#include <stdio.h> #include <string.h> #include <stdlib.h> /*******************************/ /* SDF includes */ /*******************************/ #include "sdfc.h" #include "sdfcext.h" /*******************************/ /* Constants & types */ /*******************************/ #define OUTPUTL 200 typedef char STR8[8+1]; /* string of 8 chars, null terminated */ typedef char FILENAME[54+1]; typedef char STR4[4+1]; main () { /*******************************/ /* data */ /*******************************/ /* SDF interface data */ char *output; SDF_STATUS_SHORT *status; char *usfname; STR8 name; int sdf_err; int sdf_op_l,sdf_op_t; char val_char[16]; /* program data */ FILENAME from_file,to_file; int access; int keyl; int recs_var; int recs; int passwd_given; STR4 passwd; int nocorr; /* SDF interface data allocation */ if ( (output=malloc(OUTPUTL)) == NULL ) exit(2); if ( (status=malloc(SDF_STATUS_SIZE_SHORT)) == NULL )exit(2); /*******************************/ /* check syntax file */ /*******************************/ sdf_err = sdfsta((char *)status); if (sdf_err) exit(3); usfname = strchr((status)->sdf_user_sf_name,'.') + 1; if (strncmp( usfname, "SDF.KOP.SYNTAX",14)) exit(3); /*******************************/ /* initialization */ /*******************************/ sdf_err = sdfinit (output,OUTPUTL, "KOP "); if (sdf_err) exit(4); /*******************************/ /* read first statement */ /*******************************/ sdf_err = sdfrd (output); switch (sdf_err) { case SDF_END: case SDF_EOF: return(0); case SDF_OK: break; default: exit(5); } /*******************************/ /* analyse loop */ /*******************************/ do { /* * * * * * * * * * * * * * * * */ /* transfer area analysis */ /* * * * * * * * * * * * * * * * */ /* statement name */ sdf_err = sdfstmt (output,name); if (sdf_err) exit(6); if (strncmp (name,"COPYFI ",8)) exit(7); /* operand from file */ sdf_err = sdftyp (output,1,&sdf_op_t,&sdf_op_l); if (sdf_err) exit(8); sdf_err = sdfval (output,1,from_file,sdf_op_l); if (sdf_err) exit(9); from_file[sdf_op_l] = '\0'; /* operand password */ passwd_given = 0; sdf_err = sdftyp (output,6,&sdf_op_t,&sdf_op_l); if (sdf_err) exit(10); if (sdf_op_t == SDF_CSTR) { passwd_given = 1; sdf_err = sdfval (output,6,passwd,sdf_op_l); if (sdf_err) exit(11); passwd[sdf_op_l] = '\0'; } /* operand to file */ sdf_err = sdftyp (output,2,&sdf_op_t,&sdf_op_l); if (sdf_err) exit(13); sdf_err = sdfval (output,2,to_file,sdf_op_l); if (sdf_err) exit(14); to_file[sdf_op_l] = '\0'; /* sub operand access-method */ sdf_err = sdftyp (output,3,&sdf_op_t,&sdf_op_l); if (sdf_err) exit(15); sdf_err = sdfval (output,3,val_char,sdf_op_l); if (sdf_err) exit(16); if (sdf_op_l == 3) access = 1; /* assume SAM is 1 */ else if ( val_char[0] == 'I' ) access = 2; /* assume ISAM is 2 */ else access = 0; /* assume SAME is 0 */ /* sub sub operand key length */ if (access == 2) { /* only for ACCESS-METHOD=ISAM */ sdf_err = sdftyp (output,4,&sdf_op_t,&sdf_op_l); if (sdf_err) exit(17); switch (sdf_op_t) { case (SDF_NOTY): return(17); /* operand not occupied */ case (SDF_INTG): sdf_err = sdfval (output,4,(char *)&keyl,sdf_op_l); if (sdf_err) exit(18); break; default: keyl = 8; break; } } /* sub operand record-size */ recs = 0; recs_var = 0; sdf_err = sdftyp (output,5,&sdf_op_t,&sdf_op_l); if (sdf_err) exit(21); if (sdf_op_t == SDF_INTG) { sdf_err = sdfval (output,5,(char *)&recs,sdf_op_l); if (sdf_err) exit(22); } else { sdf_err = sdfval (output,5,val_char,sdf_op_l); val_char[sdf_op_l] = '\0'; if (sdf_err) exit(23); if (!strcmp (val_char,"SAME")) recs = 0; /* assume 0 is same */ else if (!strcmp (val_char,"VARIABLE")) recs_var = 1; else exit(24); } /* * * * * * * * * * * * * * * * */ /* actual processing */ /* * * * * * * * * * * * * * * * */ /* semantic validation of input parameter and correction if necessary */ /* example : open of input file call DMS IF error sdf_err = sdfcbit (output,1); sdf_err = sdfcor (output,"Input file can not be opened"); if (!sdf_err) continue; restart analysis */ /* example : assume password 'AAAA' wrong : prompt for correction */ nocorr = 0; if (!strcmp (passwd,"AAAA")) { sdf_err = sdfcbit (output,6); sdf_err = sdfcor (output,"Invalid password"); if (!sdf_err) continue; /* restart analysis */ else nocorr = 1; } if (!nocorr) { /* copy processing .... */ printf ("from file %s\n",from_file); printf ("to file %s\n",to_file); printf ("access %d \n",access); if (access == 2) { printf ("key l %d\n",keyl); } if (!recs_var) printf ("recs %d \n",recs); else printf ("rec var\n"); if (passwd_given) printf ("password %s\n",passwd); } /* * * * * * * * * * * * * * * * */ /* read next statement */ /* * * * * * * * * * * * * * * * */ sdf_err = sdfrd (output); switch (sdf_err) { case SDF_END: case SDF_EOF: return(0); case SDF_OK: break; default: exit(3); } } while (1); /* endless loop */ }
Testing the program
The source program KOPC shown above has been compiled and linked and can now be tested.
|
A task is started under the user ID SDFUSR.
The user syntax file SDF.KOP.SYNTAX, in which statements for the program KOPC are defined, is activated.
The program KOPC is started. The command used here, START-EXECUTABLE-PROGRAM, is available in BLSSERV V2.3 and higher (the START-PROGRAM command with RUN-MODE=*ADVANCED is to be used if necessary).
The program KOPC expects the entry of a statement. This can be one of the SDF standard statements or the COPY-FILE statement. The KOPC program can be tested in the same way as the KOP program (cf. "Example: Program for copying files", from step 8 (Example: Program for copying files) onwards).