| 1 |
/*------------------------------------------------------------------------- |
| 2 |
* |
| 3 |
* ts_locale.c |
| 4 |
* locale compatibility layer for tsearch |
| 5 |
* |
| 6 |
* Portions Copyright (c) 1996-2025, PostgreSQL Global Development Group |
| 7 |
* |
| 8 |
* |
| 9 |
* IDENTIFICATION |
| 10 |
* src/backend/tsearch/ts_locale.c |
| 11 |
* |
| 12 |
*------------------------------------------------------------------------- |
| 13 |
*/ |
| 14 |
#include "postgres.h" |
| 15 |
|
| 16 |
#include "common/string.h" |
| 17 |
#include "storage/fd.h" |
| 18 |
#include "tsearch/ts_locale.h" |
| 19 |
|
| 20 |
static void tsearch_readline_callback(void *arg); |
| 21 |
|
| 22 |
|
| 23 |
/* |
| 24 |
* The reason these functions use a 3-wchar_t output buffer, not 2 as you |
| 25 |
* might expect, is that on Windows "wchar_t" is 16 bits and what we'll be |
| 26 |
* getting from char2wchar() is UTF16 not UTF32. A single input character |
| 27 |
* may therefore produce a surrogate pair rather than just one wchar_t; |
| 28 |
* we also need room for a trailing null. When we do get a surrogate pair, |
| 29 |
* we pass just the first code to iswdigit() etc, so that these functions will |
| 30 |
* always return false for characters outside the Basic Multilingual Plane. |
| 31 |
*/ |
| 32 |
#define WC_BUF_LEN 3 |
| 33 |
|
| 34 |
#define GENERATE_T_ISCLASS_DEF(character_class) \ |
| 35 |
/* mblen shall be that of the first character */ \ |
| 36 |
int \ |
| 37 |
t_is##character_class##_with_len(const char *ptr, int mblen) \ |
| 38 |
{ \ |
| 39 |
int clen = pg_mblen_with_len(ptr, mblen); \ |
| 40 |
wchar_t character[WC_BUF_LEN]; \ |
| 41 |
pg_locale_t mylocale = 0; /* TODO */ \ |
| 42 |
if (clen == 1 || database_ctype_is_c) \ |
| 43 |
return is##character_class(TOUCHAR(ptr)); \ |
| 44 |
char2wchar(character, WC_BUF_LEN, ptr, clen, mylocale); \ |
| 45 |
return isw##character_class((wint_t) character[0]); \ |
| 46 |
} \ |
| 47 |
\ |
| 48 |
/* ptr shall point to a NUL-terminated string */ \ |
| 49 |
int \ |
| 50 |
t_is##character_class##_cstr(const char *ptr) \ |
| 51 |
{ \ |
| 52 |
return t_is##character_class##_with_len(ptr, pg_mblen_cstr(ptr)); \ |
| 53 |
} \ |
| 54 |
/* ptr shall point to a string with pre-validated encoding */ \ |
| 55 |
int \ |
| 56 |
t_is##character_class##_unbounded(const char *ptr) \ |
| 57 |
{ \ |
| 58 |
return t_is##character_class##_with_len(ptr, pg_mblen_unbounded(ptr)); \ |
| 59 |
} \ |
| 60 |
/* historical name for _unbounded */ \ |
| 61 |
int \ |
| 62 |
t_is##character_class(const char *ptr) \ |
| 63 |
{ \ |
| 64 |
return t_is##character_class##_unbounded(ptr); \ |
| 65 |
} |
| 66 |
|
| 67 |
GENERATE_T_ISCLASS_DEF(alnum) |
| 68 |
GENERATE_T_ISCLASS_DEF(alpha) |
| 69 |
|
| 70 |
/* |
| 71 |
* Set up to read a file using tsearch_readline(). This facility is |
| 72 |
* better than just reading the file directly because it provides error |
| 73 |
* context pointing to the specific line where a problem is detected. |
| 74 |
* |
| 75 |
* Expected usage is: |
| 76 |
* |
| 77 |
* tsearch_readline_state trst; |
| 78 |
* |
| 79 |
* if (!tsearch_readline_begin(&trst, filename)) |
| 80 |
* ereport(ERROR, |
| 81 |
* (errcode(ERRCODE_CONFIG_FILE_ERROR), |
| 82 |
* errmsg("could not open stop-word file \"%s\": %m", |
| 83 |
* filename))); |
| 84 |
* while ((line = tsearch_readline(&trst)) != NULL) |
| 85 |
* process line; |
| 86 |
* tsearch_readline_end(&trst); |
| 87 |
* |
| 88 |
* Note that the caller supplies the ereport() for file open failure; |
| 89 |
* this is so that a custom message can be provided. The filename string |
| 90 |
* passed to tsearch_readline_begin() must remain valid through |
| 91 |
* tsearch_readline_end(). |
| 92 |
*/ |
| 93 |
bool |
| 94 |
tsearch_readline_begin(tsearch_readline_state *stp, |
| 95 |
const char *filename) |
| 96 |
{ |
| 97 |
if ((stp->fp = AllocateFile(filename, "r")) == NULL) |
| 98 |
return false; |
| 99 |
stp->filename = filename; |
| 100 |
stp->lineno = 0; |
| 101 |
initStringInfo(&stp->buf); |
| 102 |
stp->curline = NULL; |
| 103 |
/* Setup error traceback support for ereport() */ |
| 104 |
stp->cb.callback = tsearch_readline_callback; |
| 105 |
stp->cb.arg = stp; |
| 106 |
stp->cb.previous = error_context_stack; |
| 107 |
error_context_stack = &stp->cb; |
| 108 |
return true; |
| 109 |
} |
| 110 |
|
| 111 |
/* |
| 112 |
* Read the next line from a tsearch data file (expected to be in UTF-8), and |
| 113 |
* convert it to database encoding if needed. The returned string is palloc'd. |
| 114 |
* NULL return means EOF. |
| 115 |
*/ |
| 116 |
char * |
| 117 |
tsearch_readline(tsearch_readline_state *stp) |
| 118 |
{ |
| 119 |
char *recoded; |
| 120 |
|
| 121 |
/* Advance line number to use in error reports */ |
| 122 |
stp->lineno++; |
| 123 |
|
| 124 |
/* Clear curline, it's no longer relevant */ |
| 125 |
if (stp->curline) |
| 126 |
{ |
| 127 |
if (stp->curline != stp->buf.data) |
| 128 |
pfree(stp->curline); |
| 129 |
stp->curline = NULL; |
| 130 |
} |
| 131 |
|
| 132 |
/* Collect next line, if there is one */ |
| 133 |
if (!pg_get_line_buf(stp->fp, &stp->buf)) |
| 134 |
return NULL; |
| 135 |
|
| 136 |
/* Validate the input as UTF-8, then convert to DB encoding if needed */ |
| 137 |
recoded = pg_any_to_server(stp->buf.data, stp->buf.len, PG_UTF8); |
| 138 |
|
| 139 |
/* Save the correctly-encoded string for possible error reports */ |
| 140 |
stp->curline = recoded; /* might be equal to buf.data */ |
| 141 |
|
| 142 |
/* |
| 143 |
* We always return a freshly pstrdup'd string. This is clearly necessary |
| 144 |
* if pg_any_to_server() returned buf.data, and we need a second copy even |
| 145 |
* if encoding conversion did occur. The caller is entitled to pfree the |
| 146 |
* returned string at any time, which would leave curline pointing to |
| 147 |
* recycled storage, causing problems if an error occurs after that point. |
| 148 |
* (It's preferable to return the result of pstrdup instead of the output |
| 149 |
* of pg_any_to_server, because the conversion result tends to be |
| 150 |
* over-allocated. Since callers might save the result string directly |
| 151 |
* into a long-lived dictionary structure, we don't want it to be a larger |
| 152 |
* palloc chunk than necessary. We'll reclaim the conversion result on |
| 153 |
* the next call.) |
| 154 |
*/ |
| 155 |
return pstrdup(recoded); |
| 156 |
} |
| 157 |
|
| 158 |
/* |
| 159 |
* Close down after reading a file with tsearch_readline() |
| 160 |
*/ |
| 161 |
void |
| 162 |
tsearch_readline_end(tsearch_readline_state *stp) |
| 163 |
{ |
| 164 |
/* Suppress use of curline in any error reported below */ |
| 165 |
if (stp->curline) |
| 166 |
{ |
| 167 |
if (stp->curline != stp->buf.data) |
| 168 |
pfree(stp->curline); |
| 169 |
stp->curline = NULL; |
| 170 |
} |
| 171 |
|
| 172 |
/* Release other resources */ |
| 173 |
pfree(stp->buf.data); |
| 174 |
FreeFile(stp->fp); |
| 175 |
|
| 176 |
/* Pop the error context stack */ |
| 177 |
error_context_stack = stp->cb.previous; |
| 178 |
} |
| 179 |
|
| 180 |
/* |
| 181 |
* Error context callback for errors occurring while reading a tsearch |
| 182 |
* configuration file. |
| 183 |
*/ |
| 184 |
static void |
| 185 |
tsearch_readline_callback(void *arg) |
| 186 |
{ |
| 187 |
tsearch_readline_state *stp = (tsearch_readline_state *) arg; |
| 188 |
|
| 189 |
/* |
| 190 |
* We can't include the text of the config line for errors that occur |
| 191 |
* during tsearch_readline() itself. The major cause of such errors is |
| 192 |
* encoding violations, and we daren't try to print error messages |
| 193 |
* containing badly-encoded data. |
| 194 |
*/ |
| 195 |
if (stp->curline) |
| 196 |
errcontext("line %d of configuration file \"%s\": \"%s\"", |
| 197 |
stp->lineno, |
| 198 |
stp->filename, |
| 199 |
stp->curline); |
| 200 |
else |
| 201 |
errcontext("line %d of configuration file \"%s\"", |
| 202 |
stp->lineno, |
| 203 |
stp->filename); |
| 204 |
} |