This is an example of how to extend another language with functions found within the C library using the Systems/C Direct Call facility. Regular expressions are used in a variety of ways for very flexible text matching facilities. For more information on the details of the functions used below as well as regular expressions, see the Systems/C documentation.
If you'd like to learn more about Systems/C, visit the Systems/C description here.
#include <sys/types.h> #include <regex.h> #pragma prolkey(DCCREBG,"DCALL=ALLOCATE,NOSTDIO=1") void * DCCREBG(void ** envp) { { /* Save the created environment */ __register(13) long * sa; *envp = *(long **)(sa+1); } return *envp; } #pragma prolkey(DCCRECO,"DCALL=SUPPLIED,FINDENV=REGFND") int DCCRECO(void ** envp, regex_t *preg, const char *pattern, int cflags) { return regcomp(preg,pattern,cflags); } #pragma prolkey(DCCREXE,"DCALL=SUPPLIED,FINDENV=REGFND") int DCCREXE(void ** env,const regex_t *preg, const char *string, size_t nmatch, regmatch_t pmatch[], int eflags) { return regexec(preg, string, nmatch, pmatch, eflags); } #pragma prolkey(DCCRERR,"DCALL=SUPPLIED,FINDENV=REGFND") size_t DCCRERR(void ** env,int errcode, const regex_t *preg, char *errbuf, size_t errbuf_size) { return regerror(errcode, preg,errbuf,errbuf_size); } #pragma prolkey(DCCREFR,"DCALL=SUPPLIED,FINDENV=REGFND") void DCCREFR(void ** env, regex_t *preg) { regfree(preg); return; } #pragma prolkey(DCCREND,"DCALL=DESTROY,FINDENV=REGFND") void DCCREND(void ** env) { } __asm { REGFND CSECT USING REGFND,15 L 2,0(,1) Get the address of the Environment L 0,0(,2) The Environment L 15,=v(@crt9a) Get the address of CRT9A BR 15 LTORG }
callre csect stm 14,12,12(13) lr 12,15 using callre,12 st 13,savearea+4 la 13,savearea using savearea,13 call DCCREBG,(envp) * create the C Environment call DCCRECO,(envp,preg,pattern,0) * compile the RE ltr 6,15 bz ok1 o 6,=a(1000) * Indicate regcomp failed b returnrc ok1 call DCCREXE,(envp,preg,match,0,0,0) * compare match ltr 6,15 bz ok2 o 6,=a(2000) * Indicate regexec of match faile ok2 call DCCREXE,(envp,preg,nomatch,0,0,0) * compare nomatch ltr 6,15 bz ok3 o 6,=a(3000) * Indicate regexec of nomatch, should fail ok3 call DCCREND,(envp) * Destroy the C Environment returnrc lr 15,6 l 13,4(,13) l 14,12(,13) lm 0,12,20(13) br 14 ltorg savearea ds 18f envp ds 1f preg ds 1f pattern dc C'[0-9]+',x'00' only accept numbers match dc C'0123999',x'00' this will match nomatch dc C'ABCDEF',X'00' this will not match end
//LINK EXEC PGM=IEWL,REGION=2M,PARM=('LIST', // 'MAP,XREF', // 'ALIASES=NO,UPCASE=NO,MSGLEVEL=4,EDIT=YES') //SYSPRINT DD SYSOUT=* //SYSUT1 DD UNIT=SYSDA,SPACE=(CYL,(1,1)) //SYSUT2 DD UNIT=SYSDA,SPACE=(CYL,(1,1)) //SYSLMOD DD DISP=SHR,DSN=&SYSUID..LOAD(CALLRE) //SYSLIN DD DISP=SHR,DSN=&SYSUID..OBJ(CALLRE) // EXEC PGM=CALLRE //STEPLIB DD DISP=SHR,DSN=&SYSUID..LOAD
If you'd like to learn more about Systems/C, visit the Systems/C description here.
Contact us $Date: 2002/11/14 21:18:53 $ |
Copyright © 2002 Dignus, LLC All rights reserved. |