/[dtapublic]/projs/trunk/shared_source/c_tcl_base_7_5_w_mods/regerror.c
ViewVC logotype

Diff of /projs/trunk/shared_source/c_tcl_base_7_5_w_mods/regerror.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 67 by dashley, Mon Oct 31 00:57:34 2016 UTC revision 71 by dashley, Sat Nov 5 11:07:06 2016 UTC
# Line 1  Line 1 
1  /* $Header$ */  /* $Header$ */
2  /*  /*
3   * regerror - error-code expansion   * regerror - error-code expansion
4   *   *
5   * Copyright (c) 1998, 1999 Henry Spencer.  All rights reserved.   * Copyright (c) 1998, 1999 Henry Spencer.  All rights reserved.
6   *   *
7   * Development of this software was funded, in part, by Cray Research Inc.,   * Development of this software was funded, in part, by Cray Research Inc.,
8   * UUNET Communications Services Inc., Sun Microsystems Inc., and Scriptics   * UUNET Communications Services Inc., Sun Microsystems Inc., and Scriptics
9   * Corporation, none of whom are responsible for the results.  The author   * Corporation, none of whom are responsible for the results.  The author
10   * thanks all of them.   * thanks all of them.
11   *   *
12   * Redistribution and use in source and binary forms -- with or without   * Redistribution and use in source and binary forms -- with or without
13   * modification -- are permitted for any purpose, provided that   * modification -- are permitted for any purpose, provided that
14   * redistributions in source form retain this entire copyright notice and   * redistributions in source form retain this entire copyright notice and
15   * indicate the origin and nature of any modifications.   * indicate the origin and nature of any modifications.
16   *   *
17   * I'd appreciate being given credit for this package in the documentation   * I'd appreciate being given credit for this package in the documentation
18   * of software which uses it, but that is not a requirement.   * of software which uses it, but that is not a requirement.
19   *   *
20   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
21   * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY   * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
22   * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL   * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
23   * HENRY SPENCER BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,   * HENRY SPENCER BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24   * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,   * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25   * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;   * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
26   * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,   * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27   * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR   * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
28   * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF   * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
29   * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.   * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30   *   *
31   */   */
32    
33  #include "regguts.h"  #include "regguts.h"
34    
35  /* unknown-error explanation */  /* unknown-error explanation */
36  static char unk[] = "*** unknown regex error code 0x%x ***";  static char unk[] = "*** unknown regex error code 0x%x ***";
37    
38  /* struct to map among codes, code names, and explanations */  /* struct to map among codes, code names, and explanations */
39  static struct rerr {  static struct rerr {
40          int code;          int code;
41          char *name;          char *name;
42          char *explain;          char *explain;
43  } rerrs[] = {  } rerrs[] = {
44          /* the actual table is built from regex.h */          /* the actual table is built from regex.h */
45  #       include "regerrs.h"  #       include "regerrs.h"
46          { -1,   "",     "oops" },       /* explanation special-cased in code */          { -1,   "",     "oops" },       /* explanation special-cased in code */
47  };  };
48    
49  /*  /*
50   - regerror - the interface to error numbers   - regerror - the interface to error numbers
51   */   */
52  /* ARGSUSED */  /* ARGSUSED */
53  size_t                          /* actual space needed (including NUL) */  size_t                          /* actual space needed (including NUL) */
54  regerror(errcode, preg, errbuf, errbuf_size)  regerror(errcode, preg, errbuf, errbuf_size)
55  int errcode;                    /* error code, or REG_ATOI or REG_ITOA */  int errcode;                    /* error code, or REG_ATOI or REG_ITOA */
56  CONST regex_t *preg;            /* associated regex_t (unused at present) */  CONST regex_t *preg;            /* associated regex_t (unused at present) */
57  char *errbuf;                   /* result buffer (unless errbuf_size==0) */  char *errbuf;                   /* result buffer (unless errbuf_size==0) */
58  size_t errbuf_size;             /* available space in errbuf, can be 0 */  size_t errbuf_size;             /* available space in errbuf, can be 0 */
59  {  {
60          struct rerr *r;          struct rerr *r;
61          char *msg;          char *msg;
62          char convbuf[sizeof(unk)+50];   /* 50 = plenty for int */          char convbuf[sizeof(unk)+50];   /* 50 = plenty for int */
63          size_t len;          size_t len;
64          int icode;          int icode;
65    
66          switch (errcode) {          switch (errcode) {
67          case REG_ATOI:          /* convert name to number */          case REG_ATOI:          /* convert name to number */
68                  for (r = rerrs; r->code >= 0; r++)                  for (r = rerrs; r->code >= 0; r++)
69                          if (strcmp(r->name, errbuf) == 0)                          if (strcmp(r->name, errbuf) == 0)
70                                  break;                                  break;
71                  sprintf(convbuf, "%d", r->code);        /* -1 for unknown */                  sprintf(convbuf, "%d", r->code);        /* -1 for unknown */
72                  msg = convbuf;                  msg = convbuf;
73                  break;                  break;
74          case REG_ITOA:          /* convert number to name */          case REG_ITOA:          /* convert number to name */
75                  icode = atoi(errbuf);   /* not our problem if this fails */                  icode = atoi(errbuf);   /* not our problem if this fails */
76                  for (r = rerrs; r->code >= 0; r++)                  for (r = rerrs; r->code >= 0; r++)
77                          if (r->code == icode)                          if (r->code == icode)
78                                  break;                                  break;
79                  if (r->code >= 0)                  if (r->code >= 0)
80                          msg = r->name;                          msg = r->name;
81                  else {                  /* unknown; tell him the number */                  else {                  /* unknown; tell him the number */
82                          sprintf(convbuf, "REG_%u", (unsigned)icode);                          sprintf(convbuf, "REG_%u", (unsigned)icode);
83                          msg = convbuf;                          msg = convbuf;
84                  }                  }
85                  break;                  break;
86          default:                /* a real, normal error code */          default:                /* a real, normal error code */
87                  for (r = rerrs; r->code >= 0; r++)                  for (r = rerrs; r->code >= 0; r++)
88                          if (r->code == errcode)                          if (r->code == errcode)
89                                  break;                                  break;
90                  if (r->code >= 0)                  if (r->code >= 0)
91                          msg = r->explain;                          msg = r->explain;
92                  else {                  /* unknown; say so */                  else {                  /* unknown; say so */
93                          sprintf(convbuf, unk, errcode);                          sprintf(convbuf, unk, errcode);
94                          msg = convbuf;                          msg = convbuf;
95                  }                  }
96                  break;                  break;
97          }          }
98    
99          len = strlen(msg) + 1;          /* space needed, including NUL */          len = strlen(msg) + 1;          /* space needed, including NUL */
100          if (errbuf_size > 0) {          if (errbuf_size > 0) {
101                  if (errbuf_size > len)                  if (errbuf_size > len)
102                          strcpy(errbuf, msg);                          strcpy(errbuf, msg);
103                  else {                  /* truncate to fit */                  else {                  /* truncate to fit */
104                          strncpy(errbuf, msg, errbuf_size-1);                          strncpy(errbuf, msg, errbuf_size-1);
105                          errbuf[errbuf_size-1] = '\0';                          errbuf[errbuf_size-1] = '\0';
106                  }                  }
107          }          }
108    
109          return len;          return len;
110  }  }
111    
112  /* End of regerror.c */  /* End of regerror.c */

Legend:
Removed from v.67  
changed lines
  Added in v.71

dashley@gmail.com
ViewVC Help
Powered by ViewVC 1.1.25