Skip to content

Commit

Permalink
added matchregex function
Browse files Browse the repository at this point in the history
  • Loading branch information
mbhangui committed Sep 5, 2023
1 parent 165d7d3 commit 79d7e8f
Show file tree
Hide file tree
Showing 5 changed files with 154 additions and 14 deletions.
28 changes: 14 additions & 14 deletions Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -85,16 +85,16 @@ include_HEADERS = base/alloc.h base/allwrite.h base/arc4random.h \
base/gen_alloc.h base/get_scram_secrets.h base/getEnvConfig.h \
base/getln.h base/global.h base/hasbltnofl.h base/hashmethods.h base/hmac.h \
base/in_crypt.h base/iopause.h base/isnum.h base/leapsecs.h \
base/lock.h base/makeargs.h base/makesalt.h base/md5.h base/mess822.h \
base/mkpasswd.h base/mktempfile.h base/ndelay.h base/noreturn.h \
base/now.h base/no_of_days.h base/open.h base/openreadclose.h \
base/pathexec.h base/prot.h base/pw_comp.h base/qgetpwgr.h \
base/qtime.h base/readclose.h base/replacestr.h base/rewritehost.h \
base/ripemd.h base/r_mkdir.h base/scan.h base/sconfig.h base/seek.h \
base/select.h base/setuserid.h base/sgetopt.h base/sha1.h base/sig.h \
base/sodium_random.h base/stralloc.h base/strerr.h base/str.h \
base/strmsg.h base/subfd.h base/subgetopt.h base/substdio.h \
base/taia.h base/tai.h base/tai2.h base/timeoutread.h \
base/lock.h base/makeargs.h base/makesalt.h base/matchregex.h base/md5.h \
base/mess822.h base/mkpasswd.h base/mktempfile.h base/ndelay.h \
base/noreturn.h base/now.h base/no_of_days.h base/open.h \
base/openreadclose.h base/pathexec.h base/prot.h base/pw_comp.h \
base/qgetpwgr.h base/qtime.h base/readclose.h base/replacestr.h \
base/rewritehost.h base/ripemd.h base/r_mkdir.h base/scan.h \
base/sconfig.h base/seek.h base/select.h base/setuserid.h base/sgetopt.h \
base/sha1.h base/sig.h base/sodium_random.h base/stralloc.h \
base/strerr.h base/str.h base/strmsg.h base/subfd.h base/subgetopt.h \
base/substdio.h base/taia.h base/tai.h base/tai2.h base/timeoutread.h \
base/timeoutwrite.h base/timestamp.h base/token822.h base/typesx.h \
base/uint16.h base/uint32.h base/uint64.h base/wait.h base/qprintf.h \
array/array.h textcode/entities.h textcode/textcode.h base/likely.h \
Expand Down Expand Up @@ -126,10 +126,10 @@ COMMONSOURCES = base/alloc.c base/alloc_re.c base/allwrite.c base/arc4random.c \
base/hmac_sha224.c base/hmac_sha256.c base/hmac_sha384.c base/hmac_sha512.c \
base/in_crypt.c base/iopause.c base/isnum.c base/leapsecs_add.c \
base/leapsecs_init.c base/leapsecs_read.c base/leapsecs_sub.c base/lock_ex.c \
base/lock_exnb.c base/lock_un.c base/makeargs.c base/makesalt.c base/md5.c \
base/md5_crypt.c base/mess822_addr.c base/mess822_fold.c base/mess822_line.c \
base/mess822_quote.c base/mess822_when.c base/mess822_date.c base/mess822.h \
base/mess822_ok.c base/mess822_token.c base/mktempfile.c \
base/lock_exnb.c base/lock_un.c base/makeargs.c base/makesalt.c base/matchregex.c \
base/md5.c base/md5_crypt.c base/mess822_addr.c base/mess822_fold.c \
base/mess822_line.c base/mess822_quote.c base/mess822_when.c base/mess822_date.c \
base/mess822.h base/mess822_ok.c base/mess822_token.c base/mktempfile.c \
base/ndelay.c base/ndelay_off.c base/no_of_days.c base/now.c \
base/open_append.c base/open_excl.c base/open_exclr.c base/open_read.c \
base/open_readwrite.c base/open_trunc.c base/open_write.c \
Expand Down
100 changes: 100 additions & 0 deletions base/matchregex.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
/*
* $Log: matchregex.c,v $
* Revision 1.6 2023-09-05 11:02:50+05:30 Cprogrammer
* removed qregex.h for inclusion of matchregex in libqmail
*
* Revision 1.5 2021-08-21 18:50:43+05:30 Cprogrammer
* handle regexec error
*
* Revision 1.4 2020-09-16 19:02:25+05:30 Cprogrammer
* FreeBSD fix
*
* Revision 1.3 2019-03-07 00:52:18+05:30 Cprogrammer
* added regcomp error cases for documentation
*
* Revision 1.2 2015-07-23 15:01:42+05:30 Cprogrammer
* fixed comparision of constant '-1' with 0
*
* Revision 1.1 2009-05-01 10:34:29+05:30 Cprogrammer
* Initial revision
*
*/
#include <regex.h>
#include <unistd.h>
#include "stralloc.h"
#include "error.h"
#include "matchregex.h"

#define REGEXEC(X,Y) regexec(&X, Y, (size_t) 0, (regmatch_t *) 0, (int) 0)

int
matchregex(char *text, char *regex, char **errStr)
{
regex_t qreg;
char errbuf[512];
int retval = 0;
static stralloc err_str = { 0 };

if (errStr)
*errStr = 0;
/*- build the regex */
if ((retval = regcomp(&qreg, regex, REG_EXTENDED|REG_ICASE)) != 0) {
regerror(retval, &qreg, errbuf, sizeof(errbuf));
regfree(&qreg);
if (!stralloc_copys(&err_str, text) ||
!stralloc_cats(&err_str, ": ") ||
!stralloc_cats(&err_str, regex) ||
!stralloc_cats(&err_str, ": ") ||
!stralloc_cats(&err_str, errbuf) ||
!stralloc_0(&err_str))
return (AM_MEMORY_ERR);
if (errStr)
*errStr = err_str.s;
switch (retval)
{
case REG_BADBR:
case REG_BADPAT:
case REG_BADRPT:
case REG_EBRACE:
case REG_EBRACK:
case REG_ECOLLATE:
case REG_ECTYPE:
#ifdef REG_EEND
case REG_EEND:
#endif
case REG_EESCAPE:
case REG_EPAREN:
case REG_ERANGE:
#ifdef REG_ESIZE
case REG_ESIZE:
#endif
case REG_ESPACE:
case REG_ESUBREG:
default:
return (AM_REGEX_ERR);
}
}
/*- execute the regex */
if ((retval = regexec(&qreg, text, (size_t) NULL, (regmatch_t *) NULL, 0)) == -1) {
if (!stralloc_copys(&err_str, text) ||
!stralloc_cats(&err_str, ": ") ||
!stralloc_cats(&err_str, regex) ||
!stralloc_cats(&err_str, ": ") ||
!stralloc_cats(&err_str, errbuf) ||
!stralloc_0(&err_str))
return (AM_MEMORY_ERR);
if (errStr)
*errStr = err_str.s;
return -1;
}
regfree(&qreg);
return (retval == REG_NOMATCH ? 0 : 1);
}

void
getversion_matchregex_c()
{
static char *x = "$Id: matchregex.c,v 1.6 2023-09-05 11:02:50+05:30 Cprogrammer Exp mbhangui $";

x++;
}
37 changes: 37 additions & 0 deletions base/matchregex.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* $Log: matchregex.h,v $
* Revision 1.3 2023-09-05 11:03:15+05:30 Cprogrammer
* added error definitions for matchregex function
*
* Revision 1.2 2009-05-01 10:40:37+05:30 Cprogrammer
* added errorstr argument to matchregex()
*
* Revision 1.1 2009-04-30 15:49:47+05:30 Cprogrammer
* Initial revision
*
*/
/*
* simple header file for the matchregex prototype
*/
#ifndef _MATCHREGEX_H_
#define _MATCHREGEX_H_

#ifndef AM_MEMORY_ERR
#define AM_MEMORY_ERR -1
#endif
#ifndef AM_FILE_ERR
#define AM_FILE_ERR -2
#endif
#ifndef AM_LSEEK_ERR
#define AM_LSEEK_ERR -3
#endif
#ifndef AM_REGEX_ERR
#define AM_REGEX_ERR -4
#endif
#ifndef AM_CONFIG_ERR
#define AM_CONFIG_ERR -5
#endif
#define MAX_AM_ERR -5

int matchregex(char *, char *, char **);
#endif
2 changes: 2 additions & 0 deletions doc/ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ Release @version@-@release@ Start 24/04/2023 End XX/XX/XXXX
non-zero
- 26/08/2023
25. tls/tls.c: refactored tls_get_method(), tls_set_method()
- 05/09/2023
26. matchregex.[c,h]: added matchregex function

* Sun Apr 23 2023 16:55:15 +0000 Manvendra Bhangui <[email protected]> 1.1.3-1.1%{?dist}
Release 1.1.3-1.1 Start 08/02/2023 End 23/04/2023
Expand Down
1 change: 1 addition & 0 deletions libqmail.spec.in
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ done
%attr(644,root,root) %{_prefix}/include/qmail/lock.h
%attr(644,root,root) %{_prefix}/include/qmail/makeargs.h
%attr(644,root,root) %{_prefix}/include/qmail/makesalt.h
%attr(644,root,root) %{_prefix}/include/qmail/matchregex.h
%attr(644,root,root) %{_prefix}/include/qmail/md5.h
%attr(644,root,root) %{_prefix}/include/qmail/mess822.h
%attr(644,root,root) %{_prefix}/include/qmail/mkpasswd.h
Expand Down

0 comments on commit 79d7e8f

Please sign in to comment.