home *** CD-ROM | disk | FTP | other *** search
/ PC Online 1999 April / PCO0499.ISO / filesbbs / os2 / apach134.arj / APACH134.ZIP / src / regex / regerror.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-02-04  |  3.1 KB  |  125 lines

  1. #include <sys/types.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <ctype.h>
  5. #include <limits.h>
  6. #include <stdlib.h>
  7.  
  8. #include "hsregex.h"
  9. #include "utils.h"
  10. #include "regerror.ih"
  11.  
  12. /*
  13.  = #define    REG_NOMATCH     1
  14.  = #define    REG_BADPAT     2
  15.  = #define    REG_ECOLLATE     3
  16.  = #define    REG_ECTYPE     4
  17.  = #define    REG_EESCAPE     5
  18.  = #define    REG_ESUBREG     6
  19.  = #define    REG_EBRACK     7
  20.  = #define    REG_EPAREN     8
  21.  = #define    REG_EBRACE     9
  22.  = #define    REG_BADBR    10
  23.  = #define    REG_ERANGE    11
  24.  = #define    REG_ESPACE    12
  25.  = #define    REG_BADRPT    13
  26.  = #define    REG_EMPTY    14
  27.  = #define    REG_ASSERT    15
  28.  = #define    REG_INVARG    16
  29.  = #define    REG_ATOI    255    // convert name to number (!)
  30.  = #define    REG_ITOA    0400    // convert number to name (!)
  31.  */
  32. static struct rerr {
  33.     int code;
  34.     char *name;
  35.     char *explain;
  36. } rerrs[] = {
  37.     { REG_NOMATCH,    "REG_NOMATCH",    "regexec() failed to match" },
  38.     { REG_BADPAT,    "REG_BADPAT",    "invalid regular expression" },
  39.     { REG_ECOLLATE,    "REG_ECOLLATE",    "invalid collating element" },
  40.     { REG_ECTYPE,    "REG_ECTYPE",    "invalid character class" },
  41.     { REG_EESCAPE,    "REG_EESCAPE",    "trailing backslash (\\)" },
  42.     { REG_ESUBREG,    "REG_ESUBREG",    "invalid backreference number" },
  43.     { REG_EBRACK,    "REG_EBRACK",    "brackets ([ ]) not balanced" },
  44.     { REG_EPAREN,    "REG_EPAREN",    "parentheses not balanced" },
  45.     { REG_EBRACE,    "REG_EBRACE",    "braces not balanced" },
  46.     { REG_BADBR,    "REG_BADBR",    "invalid repetition count(s)" },
  47.     { REG_ERANGE,    "REG_ERANGE",    "invalid character range" },
  48.     { REG_ESPACE,    "REG_ESPACE",    "out of memory" },
  49.     { REG_BADRPT,    "REG_BADRPT",    "repetition-operator operand invalid" },
  50.     { REG_EMPTY,    "REG_EMPTY",    "empty (sub)expression" },
  51.     { REG_ASSERT,    "REG_ASSERT",    "\"can't happen\" -- you found a bug" },
  52.     { REG_INVARG,    "REG_INVARG",    "invalid argument to regex routine" },
  53.     { 0,        "",        "*** unknown regexp error code ***" }
  54. };
  55.  
  56. /*
  57.  - regerror - the interface to error numbers
  58.  = API_EXPORT(size_t) regerror(int, const regex_t *, char *, size_t);
  59.  */
  60. /* ARGSUSED */
  61. API_EXPORT(size_t)
  62. regerror(errcode, preg, errbuf, errbuf_size)
  63. int errcode;
  64. const regex_t *preg;
  65. char *errbuf;
  66. size_t errbuf_size;
  67. {
  68.     register struct rerr *r;
  69.     register size_t len;
  70.     register int target = errcode &~ REG_ITOA;
  71.     register char *s;
  72.     char convbuf[50];
  73.  
  74.     if (errcode == REG_ATOI)
  75.         s = regatoi(preg, convbuf);
  76.     else {
  77.         for (r = rerrs; r->code != 0; r++)
  78.             if (r->code == target)
  79.                 break;
  80.     
  81.         if (errcode®_ITOA) {
  82.             if (r->code != 0)
  83.                 (void) strcpy(convbuf, r->name);
  84.             else
  85.                 sprintf(convbuf, "REG_0x%x", target);
  86.             assert(strlen(convbuf) < sizeof(convbuf));
  87.             s = convbuf;
  88.         } else
  89.             s = r->explain;
  90.     }
  91.  
  92.     len = strlen(s) + 1;
  93.     if (errbuf_size > 0) {
  94.         if (errbuf_size > len)
  95.             (void) strcpy(errbuf, s);
  96.         else {
  97.             (void) strncpy(errbuf, s, errbuf_size-1);
  98.             errbuf[errbuf_size-1] = '\0';
  99.         }
  100.     }
  101.  
  102.     return(len);
  103. }
  104.  
  105. /*
  106.  - regatoi - internal routine to implement REG_ATOI
  107.  == static char *regatoi(const regex_t *preg, char *localbuf);
  108.  */
  109. static char *
  110. regatoi(preg, localbuf)
  111. const regex_t *preg;
  112. char *localbuf;
  113. {
  114.     register struct rerr *r;
  115.  
  116.     for (r = rerrs; r->code != 0; r++)
  117.         if (strcmp(r->name, preg->re_endp) == 0)
  118.             break;
  119.     if (r->code == 0)
  120.         return("0");
  121.  
  122.     sprintf(localbuf, "%d", r->code);
  123.     return(localbuf);
  124. }
  125.