PostgreSQL Source Code  git master
regerror.c File Reference
#include "regex/regguts.h"
#include "regex/regerrs.h"
Include dependency graph for regerror.c:

Go to the source code of this file.

Data Structures

struct  rerr
 

Functions

size_t pg_regerror (int errcode, const regex_t *preg, char *errbuf, size_t errbuf_size)
 

Variables

static const char unk [] = "*** unknown regex error code 0x%x ***"
 
static const struct rerr rerrs []
 

Function Documentation

◆ pg_regerror()

size_t pg_regerror ( int  errcode,
const regex_t preg,
char *  errbuf,
size_t  errbuf_size 
)

Definition at line 60 of file regerror.c.

References rerr::code, rerr::explain, rerr::name, REG_ATOI, REG_ITOA, rerrs, and unk.

Referenced by check_ident_usermap(), NIAddAffix(), parse_ident_line(), RE_compile(), RE_compile_and_cache(), RE_wchar_execute(), regexp_fixed_prefix(), and replace_text_regexp().

64 {
65  const struct rerr *r;
66  const char *msg;
67  char convbuf[sizeof(unk) + 50]; /* 50 = plenty for int */
68  size_t len;
69  int icode;
70 
71  switch (errcode)
72  {
73  case REG_ATOI: /* convert name to number */
74  for (r = rerrs; r->code >= 0; r++)
75  if (strcmp(r->name, errbuf) == 0)
76  break;
77  sprintf(convbuf, "%d", r->code); /* -1 for unknown */
78  msg = convbuf;
79  break;
80  case REG_ITOA: /* convert number to name */
81  icode = atoi(errbuf); /* not our problem if this fails */
82  for (r = rerrs; r->code >= 0; r++)
83  if (r->code == icode)
84  break;
85  if (r->code >= 0)
86  msg = r->name;
87  else
88  { /* unknown; tell him the number */
89  sprintf(convbuf, "REG_%u", (unsigned) icode);
90  msg = convbuf;
91  }
92  break;
93  default: /* a real, normal error code */
94  for (r = rerrs; r->code >= 0; r++)
95  if (r->code == errcode)
96  break;
97  if (r->code >= 0)
98  msg = r->explain;
99  else
100  { /* unknown; say so */
101  sprintf(convbuf, unk, errcode);
102  msg = convbuf;
103  }
104  break;
105  }
106 
107  len = strlen(msg) + 1; /* space needed, including NUL */
108  if (errbuf_size > 0)
109  {
110  if (errbuf_size > len)
111  strcpy(errbuf, msg);
112  else
113  { /* truncate to fit */
114  memcpy(errbuf, msg, errbuf_size - 1);
115  errbuf[errbuf_size - 1] = '\0';
116  }
117  }
118 
119  return len;
120 }
int code
Definition: regerror.c:42
static const struct rerr rerrs[]
static const char unk[]
Definition: regerror.c:37
int errcode(int sqlerrcode)
Definition: elog.c:575
const char * name
Definition: regerror.c:43
#define REG_ATOI
Definition: regex.h:159
#define REG_ITOA
Definition: regex.h:160
const char * explain
Definition: regerror.c:44
Definition: regerror.c:40

Variable Documentation

◆ rerrs

const struct rerr rerrs[]
static

Referenced by pg_regerror().

◆ unk

const char unk[] = "*** unknown regex error code 0x%x ***"
static

Definition at line 37 of file regerror.c.

Referenced by pg_regerror().