PostgreSQL Source Code git master
psqlscan_int.h File Reference
Include dependency graph for psqlscan_int.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  StackElem
 
struct  PsqlScanStateData
 

Macros

#define YY_TYPEDEF_YY_BUFFER_STATE
 
#define YY_TYPEDEF_YY_SCANNER_T
 

Typedefs

typedef struct yy_buffer_state * YY_BUFFER_STATE
 
typedef void * yyscan_t
 
typedef struct StackElem StackElem
 
typedef struct PsqlScanStateData PsqlScanStateData
 

Functions

void psqlscan_push_new_buffer (PsqlScanState state, const char *newstr, const char *varname)
 
void psqlscan_pop_buffer_stack (PsqlScanState state)
 
void psqlscan_select_top_buffer (PsqlScanState state)
 
bool psqlscan_var_is_current_source (PsqlScanState state, const char *varname)
 
YY_BUFFER_STATE psqlscan_prepare_buffer (PsqlScanState state, const char *txt, int len, char **txtcopy)
 
void psqlscan_emit (PsqlScanState state, const char *txt, int len)
 
char * psqlscan_extract_substring (PsqlScanState state, const char *txt, int len)
 
void psqlscan_escape_variable (PsqlScanState state, const char *txt, int len, PsqlScanQuoteType quote)
 
void psqlscan_test_variable (PsqlScanState state, const char *txt, int len)
 

Macro Definition Documentation

◆ YY_TYPEDEF_YY_BUFFER_STATE

#define YY_TYPEDEF_YY_BUFFER_STATE

Definition at line 55 of file psqlscan_int.h.

◆ YY_TYPEDEF_YY_SCANNER_T

#define YY_TYPEDEF_YY_SCANNER_T

Definition at line 59 of file psqlscan_int.h.

Typedef Documentation

◆ PsqlScanStateData

◆ StackElem

typedef struct StackElem StackElem

◆ YY_BUFFER_STATE

typedef struct yy_buffer_state* YY_BUFFER_STATE

Definition at line 56 of file psqlscan_int.h.

◆ yyscan_t

typedef void* yyscan_t

Definition at line 60 of file psqlscan_int.h.

Function Documentation

◆ psqlscan_emit()

void psqlscan_emit ( PsqlScanState  state,
const char *  txt,
int  len 
)

Definition at line 1463 of file psqlscan.l.

1464{
1465 PQExpBuffer output_buf = state->output_buf;
1466
1467 if (state->safe_encoding)
1468 appendBinaryPQExpBuffer(output_buf, txt, len);
1469 else
1470 {
1471 /* Gotta do it the hard way */
1472 const char *reference = state->refline;
1473 int i;
1474
1475 reference += (txt - state->curline);
1476
1477 for (i = 0; i < len; i++)
1478 {
1479 char ch = txt[i];
1480
1481 if (ch == (char) 0xFF)
1482 ch = reference[i];
1483 appendPQExpBufferChar(output_buf, ch);
1484 }
1485 }
1486}
int i
Definition: isn.c:72
const void size_t len
void appendBinaryPQExpBuffer(PQExpBuffer str, const char *data, size_t datalen)
Definition: pqexpbuffer.c:397
void appendPQExpBufferChar(PQExpBuffer str, char ch)
Definition: pqexpbuffer.c:378
Definition: regguts.h:323

References appendBinaryPQExpBuffer(), appendPQExpBufferChar(), i, and len.

Referenced by psqlscan_escape_variable(), and psqlscan_test_variable().

◆ psqlscan_escape_variable()

void psqlscan_escape_variable ( PsqlScanState  state,
const char *  txt,
int  len,
PsqlScanQuoteType  quote 
)

Definition at line 1531 of file psqlscan.l.

1533{
1534 char *varname;
1535 char *value;
1536
1537 /* Variable lookup. */
1538 varname = psqlscan_extract_substring(state, txt + 2, len - 3);
1539 if (state->callbacks->get_variable)
1540 value = state->callbacks->get_variable(varname, quote,
1541 state->cb_passthrough);
1542 else
1543 value = NULL;
1544 free(varname);
1545
1546 if (value)
1547 {
1548 /* Emit the suitably-escaped value */
1549 appendPQExpBufferStr(state->output_buf, value);
1550 free(value);
1551 }
1552 else
1553 {
1554 /* Emit original token as-is */
1555 psqlscan_emit(state, txt, len);
1556 }
1557}
#define free(a)
Definition: header.h:65
static struct @162 value
void appendPQExpBufferStr(PQExpBuffer str, const char *data)
Definition: pqexpbuffer.c:367
void psqlscan_emit(PsqlScanState state, const char *txt, int len)
Definition: psqlscan.l:1463
char * psqlscan_extract_substring(PsqlScanState state, const char *txt, int len)
Definition: psqlscan.l:1495

References appendPQExpBufferStr(), free, len, psqlscan_emit(), psqlscan_extract_substring(), and value.

◆ psqlscan_extract_substring()

char * psqlscan_extract_substring ( PsqlScanState  state,
const char *  txt,
int  len 
)

Definition at line 1495 of file psqlscan.l.

1496{
1497 char *result = (char *) pg_malloc(len + 1);
1498
1499 if (state->safe_encoding)
1500 memcpy(result, txt, len);
1501 else
1502 {
1503 /* Gotta do it the hard way */
1504 const char *reference = state->refline;
1505 int i;
1506
1507 reference += (txt - state->curline);
1508
1509 for (i = 0; i < len; i++)
1510 {
1511 char ch = txt[i];
1512
1513 if (ch == (char) 0xFF)
1514 ch = reference[i];
1515 result[i] = ch;
1516 }
1517 }
1518 result[len] = '\0';
1519 return result;
1520}
void * pg_malloc(size_t size)
Definition: fe_memutils.c:47

References i, len, and pg_malloc().

Referenced by psqlscan_escape_variable(), and psqlscan_test_variable().

◆ psqlscan_pop_buffer_stack()

void psqlscan_pop_buffer_stack ( PsqlScanState  state)

Definition at line 1359 of file psqlscan.l.

1360{
1361 StackElem *stackelem = state->buffer_stack;
1362
1363 state->buffer_stack = stackelem->next;
1364 yy_delete_buffer(stackelem->buf, state->scanner);
1365 free(stackelem->bufstring);
1366 if (stackelem->origstring)
1367 free(stackelem->origstring);
1368 if (stackelem->varname)
1369 free(stackelem->varname);
1370 free(stackelem);
1371}
char * origstring
Definition: psqlscan_int.h:73
char * varname
Definition: psqlscan_int.h:74
YY_BUFFER_STATE buf
Definition: psqlscan_int.h:71
char * bufstring
Definition: psqlscan_int.h:72
struct StackElem * next
Definition: psqlscan_int.h:75

References StackElem::buf, StackElem::bufstring, free, StackElem::next, StackElem::origstring, and StackElem::varname.

Referenced by psql_scan_finish().

◆ psqlscan_prepare_buffer()

YY_BUFFER_STATE psqlscan_prepare_buffer ( PsqlScanState  state,
const char *  txt,
int  len,
char **  txtcopy 
)

Definition at line 1422 of file psqlscan.l.

1424{
1425 char *newtxt;
1426
1427 /* Flex wants two \0 characters after the actual data */
1428 newtxt = pg_malloc(len + 2);
1429 *txtcopy = newtxt;
1430 newtxt[len] = newtxt[len + 1] = YY_END_OF_BUFFER_CHAR;
1431
1432 if (state->safe_encoding)
1433 memcpy(newtxt, txt, len);
1434 else
1435 {
1436 /* Gotta do it the hard way */
1437 int i = 0;
1438
1439 while (i < len)
1440 {
1441 int thislen = PQmblen(txt + i, state->encoding);
1442
1443 /* first byte should always be okay... */
1444 newtxt[i] = txt[i];
1445 i++;
1446 while (--thislen > 0 && i < len)
1447 newtxt[i++] = (char) 0xFF;
1448 }
1449 }
1450
1451 return yy_scan_buffer(newtxt, len + 2, state->scanner);
1452}
int PQmblen(const char *s, int encoding)
Definition: fe-misc.c:1233

References i, len, pg_malloc(), and PQmblen().

Referenced by psql_scan_setup(), and psqlscan_push_new_buffer().

◆ psqlscan_push_new_buffer()

void psqlscan_push_new_buffer ( PsqlScanState  state,
const char *  newstr,
const char *  varname 
)

Definition at line 1320 of file psqlscan.l.

1322{
1323 StackElem *stackelem;
1324
1325 stackelem = (StackElem *) pg_malloc(sizeof(StackElem));
1326
1327 /*
1328 * In current usage, the passed varname points at the current flex input
1329 * buffer; we must copy it before calling psqlscan_prepare_buffer()
1330 * because that will change the buffer state.
1331 */
1332 stackelem->varname = varname ? pg_strdup(varname) : NULL;
1333
1334 stackelem->buf = psqlscan_prepare_buffer(state, newstr, strlen(newstr),
1335 &stackelem->bufstring);
1336 state->curline = stackelem->bufstring;
1337 if (state->safe_encoding)
1338 {
1339 stackelem->origstring = NULL;
1340 state->refline = stackelem->bufstring;
1341 }
1342 else
1343 {
1344 stackelem->origstring = pg_strdup(newstr);
1345 state->refline = stackelem->origstring;
1346 }
1347 stackelem->next = state->buffer_stack;
1348 state->buffer_stack = stackelem;
1349}
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
YY_BUFFER_STATE psqlscan_prepare_buffer(PsqlScanState state, const char *txt, int len, char **txtcopy)
Definition: psqlscan.l:1422

References StackElem::buf, StackElem::bufstring, StackElem::next, StackElem::origstring, pg_malloc(), pg_strdup(), psqlscan_prepare_buffer(), and StackElem::varname.

◆ psqlscan_select_top_buffer()

void psqlscan_select_top_buffer ( PsqlScanState  state)

Definition at line 1377 of file psqlscan.l.

1378{
1379 StackElem *stackelem = state->buffer_stack;
1380
1381 if (stackelem != NULL)
1382 {
1383 yy_switch_to_buffer(stackelem->buf, state->scanner);
1384 state->curline = stackelem->bufstring;
1385 state->refline = stackelem->origstring ? stackelem->origstring : stackelem->bufstring;
1386 }
1387 else
1388 {
1389 yy_switch_to_buffer(state->scanbufhandle, state->scanner);
1390 state->curline = state->scanbuf;
1391 state->refline = state->scanline;
1392 }
1393}

References StackElem::buf, StackElem::bufstring, and StackElem::origstring.

◆ psqlscan_test_variable()

void psqlscan_test_variable ( PsqlScanState  state,
const char *  txt,
int  len 
)

Definition at line 1560 of file psqlscan.l.

1561{
1562 char *varname;
1563 char *value;
1564
1565 varname = psqlscan_extract_substring(state, txt + 3, len - 4);
1566 if (state->callbacks->get_variable)
1567 value = state->callbacks->get_variable(varname, PQUOTE_PLAIN,
1568 state->cb_passthrough);
1569 else
1570 value = NULL;
1571 free(varname);
1572
1573 if (value != NULL)
1574 {
1575 psqlscan_emit(state, "TRUE", 4);
1576 free(value);
1577 }
1578 else
1579 {
1580 psqlscan_emit(state, "FALSE", 5);
1581 }
1582}
@ PQUOTE_PLAIN
Definition: psqlscan.h:54

References free, len, PQUOTE_PLAIN, psqlscan_emit(), psqlscan_extract_substring(), and value.

◆ psqlscan_var_is_current_source()

bool psqlscan_var_is_current_source ( PsqlScanState  state,
const char *  varname 
)

Definition at line 1400 of file psqlscan.l.

1401{
1402 StackElem *stackelem;
1403
1404 for (stackelem = state->buffer_stack;
1405 stackelem != NULL;
1406 stackelem = stackelem->next)
1407 {
1408 if (stackelem->varname && strcmp(stackelem->varname, varname) == 0)
1409 return true;
1410 }
1411 return false;
1412}

References StackElem::next, and StackElem::varname.