GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: lib/libc/regex/regerror.c Lines: 0 23 0.0 %
Date: 2017-11-13 Branches: 0 18 0.0 %

Line Branch Exec Source
1
/*	$OpenBSD: regerror.c,v 1.14 2015/11/01 03:45:29 guenther Exp $ */
2
/*-
3
 * Copyright (c) 1992, 1993, 1994 Henry Spencer.
4
 * Copyright (c) 1992, 1993, 1994
5
 *	The Regents of the University of California.  All rights reserved.
6
 *
7
 * This code is derived from software contributed to Berkeley by
8
 * Henry Spencer.
9
 *
10
 * Redistribution and use in source and binary forms, with or without
11
 * modification, are permitted provided that the following conditions
12
 * are met:
13
 * 1. Redistributions of source code must retain the above copyright
14
 *    notice, this list of conditions and the following disclaimer.
15
 * 2. Redistributions in binary form must reproduce the above copyright
16
 *    notice, this list of conditions and the following disclaimer in the
17
 *    documentation and/or other materials provided with the distribution.
18
 * 3. Neither the name of the University nor the names of its contributors
19
 *    may be used to endorse or promote products derived from this software
20
 *    without specific prior written permission.
21
 *
22
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32
 * SUCH DAMAGE.
33
 *
34
 *	@(#)regerror.c	8.4 (Berkeley) 3/20/94
35
 */
36
37
#include <sys/types.h>
38
#include <stdio.h>
39
#include <string.h>
40
#include <ctype.h>
41
#include <limits.h>
42
#include <stdlib.h>
43
#include <regex.h>
44
45
#include "utils.h"
46
47
static char *regatoi(const regex_t *, char *, int);
48
49
static struct rerr {
50
	int code;
51
	char *name;
52
	char *explain;
53
} rerrs[] = {
54
	{ REG_NOMATCH,	"REG_NOMATCH",	"regexec() failed to match" },
55
	{ REG_BADPAT,	"REG_BADPAT",	"invalid regular expression" },
56
	{ REG_ECOLLATE,	"REG_ECOLLATE",	"invalid collating element" },
57
	{ REG_ECTYPE,	"REG_ECTYPE",	"invalid character class" },
58
	{ REG_EESCAPE,	"REG_EESCAPE",	"trailing backslash (\\)" },
59
	{ REG_ESUBREG,	"REG_ESUBREG",	"invalid backreference number" },
60
	{ REG_EBRACK,	"REG_EBRACK",	"brackets ([ ]) not balanced" },
61
	{ REG_EPAREN,	"REG_EPAREN",	"parentheses not balanced" },
62
	{ REG_EBRACE,	"REG_EBRACE",	"braces not balanced" },
63
	{ REG_BADBR,	"REG_BADBR",	"invalid repetition count(s)" },
64
	{ REG_ERANGE,	"REG_ERANGE",	"invalid character range" },
65
	{ REG_ESPACE,	"REG_ESPACE",	"out of memory" },
66
	{ REG_BADRPT,	"REG_BADRPT",	"repetition-operator operand invalid" },
67
	{ REG_EMPTY,	"REG_EMPTY",	"empty (sub)expression" },
68
	{ REG_ASSERT,	"REG_ASSERT",	"\"can't happen\" -- you found a bug" },
69
	{ REG_INVARG,	"REG_INVARG",	"invalid argument to regex routine" },
70
	{ 0,		"",		"*** unknown regexp error code ***" }
71
};
72
73
/*
74
 - regerror - the interface to error numbers
75
 = extern size_t regerror(int, const regex_t *, char *, size_t);
76
 */
77
size_t
78
regerror(int errcode, const regex_t *preg, char *errbuf, size_t errbuf_size)
79
{
80
	struct rerr *r;
81
	size_t len;
82
	int target = errcode &~ REG_ITOA;
83
	char *s;
84
	char convbuf[50];
85
86
	if (errcode == REG_ATOI)
87
		s = regatoi(preg, convbuf, sizeof convbuf);
88
	else {
89
		for (r = rerrs; r->code != 0; r++)
90
			if (r->code == target)
91
				break;
92
93
		if (errcode&REG_ITOA) {
94
			if (r->code != 0) {
95
				assert(strlen(r->name) < sizeof(convbuf));
96
				(void) strlcpy(convbuf, r->name, sizeof convbuf);
97
			} else
98
				(void)snprintf(convbuf, sizeof convbuf,
99
				    "REG_0x%x", target);
100
			s = convbuf;
101
		} else
102
			s = r->explain;
103
	}
104
105
	len = strlen(s) + 1;
106
	if (errbuf_size > 0) {
107
		strlcpy(errbuf, s, errbuf_size);
108
	}
109
110
	return(len);
111
}
112
113
/*
114
 - regatoi - internal routine to implement REG_ATOI
115
 */
116
static char *
117
regatoi(const regex_t *preg, char *localbuf, int localbufsize)
118
{
119
	struct rerr *r;
120
121
	for (r = rerrs; r->code != 0; r++)
122
		if (strcmp(r->name, preg->re_endp) == 0)
123
			break;
124
	if (r->code == 0)
125
		return("0");
126
127
	(void)snprintf(localbuf, localbufsize, "%d", r->code);
128
	return(localbuf);
129
}