PostgreSQL Source Code git master
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
readfuncs.h File Reference
#include "nodes/nodes.h"
Include dependency graph for readfuncs.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

const char * pg_strtok (int *length)
 
char * debackslash (const char *token, int length)
 
void * nodeRead (const char *token, int tok_len)
 
NodeparseNodeString (void)
 

Function Documentation

◆ debackslash()

char * debackslash ( const char *  token,
int  length 
)

Definition at line 214 of file read.c.

215{
216 char *result = palloc(length + 1);
217 char *ptr = result;
218
219 while (length > 0)
220 {
221 if (*token == '\\' && length > 1)
222 token++, length--;
223 *ptr++ = *token++;
224 length--;
225 }
226 *ptr = '\0';
227 return result;
228}
#define token
Definition: indent_globs.h:126
void * palloc(Size size)
Definition: mcxt.c:1317

References palloc(), and token.

Referenced by nodeRead(), and nullable_string().

◆ nodeRead()

void * nodeRead ( const char *  token,
int  tok_len 
)

Definition at line 320 of file read.c.

321{
322 Node *result;
324
325 if (token == NULL) /* need to read a token? */
326 {
327 token = pg_strtok(&tok_len);
328
329 if (token == NULL) /* end of input */
330 return NULL;
331 }
332
333 type = nodeTokenType(token, tok_len);
334
335 switch ((int) type)
336 {
337 case LEFT_BRACE:
338 result = parseNodeString();
339 token = pg_strtok(&tok_len);
340 if (token == NULL || token[0] != '}')
341 elog(ERROR, "did not find '}' at end of input node");
342 break;
343 case LEFT_PAREN:
344 {
345 List *l = NIL;
346
347 /*----------
348 * Could be an integer list: (i int int ...)
349 * or an OID list: (o int int ...)
350 * or an XID list: (x int int ...)
351 * or a bitmapset: (b int int ...)
352 * or a list of nodes/values: (node node ...)
353 *----------
354 */
355 token = pg_strtok(&tok_len);
356 if (token == NULL)
357 elog(ERROR, "unterminated List structure");
358 if (tok_len == 1 && token[0] == 'i')
359 {
360 /* List of integers */
361 for (;;)
362 {
363 int val;
364 char *endptr;
365
366 token = pg_strtok(&tok_len);
367 if (token == NULL)
368 elog(ERROR, "unterminated List structure");
369 if (token[0] == ')')
370 break;
371 val = (int) strtol(token, &endptr, 10);
372 if (endptr != token + tok_len)
373 elog(ERROR, "unrecognized integer: \"%.*s\"",
374 tok_len, token);
375 l = lappend_int(l, val);
376 }
377 result = (Node *) l;
378 }
379 else if (tok_len == 1 && token[0] == 'o')
380 {
381 /* List of OIDs */
382 for (;;)
383 {
384 Oid val;
385 char *endptr;
386
387 token = pg_strtok(&tok_len);
388 if (token == NULL)
389 elog(ERROR, "unterminated List structure");
390 if (token[0] == ')')
391 break;
392 val = (Oid) strtoul(token, &endptr, 10);
393 if (endptr != token + tok_len)
394 elog(ERROR, "unrecognized OID: \"%.*s\"",
395 tok_len, token);
396 l = lappend_oid(l, val);
397 }
398 result = (Node *) l;
399 }
400 else if (tok_len == 1 && token[0] == 'x')
401 {
402 /* List of TransactionIds */
403 for (;;)
404 {
406 char *endptr;
407
408 token = pg_strtok(&tok_len);
409 if (token == NULL)
410 elog(ERROR, "unterminated List structure");
411 if (token[0] == ')')
412 break;
413 val = (TransactionId) strtoul(token, &endptr, 10);
414 if (endptr != token + tok_len)
415 elog(ERROR, "unrecognized Xid: \"%.*s\"",
416 tok_len, token);
417 l = lappend_xid(l, val);
418 }
419 result = (Node *) l;
420 }
421 else if (tok_len == 1 && token[0] == 'b')
422 {
423 /* Bitmapset -- see also _readBitmapset() */
424 Bitmapset *bms = NULL;
425
426 for (;;)
427 {
428 int val;
429 char *endptr;
430
431 token = pg_strtok(&tok_len);
432 if (token == NULL)
433 elog(ERROR, "unterminated Bitmapset structure");
434 if (tok_len == 1 && token[0] == ')')
435 break;
436 val = (int) strtol(token, &endptr, 10);
437 if (endptr != token + tok_len)
438 elog(ERROR, "unrecognized integer: \"%.*s\"",
439 tok_len, token);
440 bms = bms_add_member(bms, val);
441 }
442 result = (Node *) bms;
443 }
444 else
445 {
446 /* List of other node types */
447 for (;;)
448 {
449 /* We have already scanned next token... */
450 if (token[0] == ')')
451 break;
452 l = lappend(l, nodeRead(token, tok_len));
453 token = pg_strtok(&tok_len);
454 if (token == NULL)
455 elog(ERROR, "unterminated List structure");
456 }
457 result = (Node *) l;
458 }
459 break;
460 }
461 case RIGHT_PAREN:
462 elog(ERROR, "unexpected right parenthesis");
463 result = NULL; /* keep compiler happy */
464 break;
465 case OTHER_TOKEN:
466 if (tok_len == 0)
467 {
468 /* must be "<>" --- represents a null pointer */
469 result = NULL;
470 }
471 else
472 {
473 elog(ERROR, "unrecognized token: \"%.*s\"", tok_len, token);
474 result = NULL; /* keep compiler happy */
475 }
476 break;
477 case T_Integer:
478
479 /*
480 * we know that the token terminates on a char atoi will stop at
481 */
482 result = (Node *) makeInteger(atoi(token));
483 break;
484 case T_Float:
485 {
486 char *fval = (char *) palloc(tok_len + 1);
487
488 memcpy(fval, token, tok_len);
489 fval[tok_len] = '\0';
490 result = (Node *) makeFloat(fval);
491 }
492 break;
493 case T_Boolean:
494 result = (Node *) makeBoolean(token[0] == 't');
495 break;
496 case T_String:
497 /* need to remove leading and trailing quotes, and backslashes */
498 result = (Node *) makeString(debackslash(token + 1, tok_len - 2));
499 break;
500 case T_BitString:
501 /* need to remove backslashes, but there are no quotes */
502 result = (Node *) makeBitString(debackslash(token, tok_len));
503 break;
504 default:
505 elog(ERROR, "unrecognized node type: %d", (int) type);
506 result = NULL; /* keep compiler happy */
507 break;
508 }
509
510 return result;
511}
Bitmapset * bms_add_member(Bitmapset *a, int x)
Definition: bitmapset.c:815
uint32 TransactionId
Definition: c.h:609
#define ERROR
Definition: elog.h:39
#define elog(elevel,...)
Definition: elog.h:225
long val
Definition: informix.c:689
List * lappend(List *list, void *datum)
Definition: list.c:339
List * lappend_xid(List *list, TransactionId datum)
Definition: list.c:393
List * lappend_int(List *list, int datum)
Definition: list.c:357
List * lappend_oid(List *list, Oid datum)
Definition: list.c:375
NodeTag
Definition: nodes.h:27
#define NIL
Definition: pg_list.h:68
unsigned int Oid
Definition: postgres_ext.h:32
#define LEFT_BRACE
Definition: read.c:232
char * debackslash(const char *token, int length)
Definition: read.c:214
#define OTHER_TOKEN
Definition: read.c:233
static NodeTag nodeTokenType(const char *token, int length)
Definition: read.c:246
void * nodeRead(const char *token, int tok_len)
Definition: read.c:320
#define LEFT_PAREN
Definition: read.c:231
#define RIGHT_PAREN
Definition: read.c:230
const char * pg_strtok(int *length)
Definition: read.c:153
Node * parseNodeString(void)
Definition: readfuncs.c:565
Definition: pg_list.h:54
Definition: nodes.h:129
Integer * makeInteger(int i)
Definition: value.c:23
String * makeString(char *str)
Definition: value.c:63
BitString * makeBitString(char *str)
Definition: value.c:77
Float * makeFloat(char *numericStr)
Definition: value.c:37
Boolean * makeBoolean(bool val)
Definition: value.c:49
const char * type

References bms_add_member(), debackslash(), elog, ERROR, lappend(), lappend_int(), lappend_oid(), lappend_xid(), LEFT_BRACE, LEFT_PAREN, makeBitString(), makeBoolean(), makeFloat(), makeInteger(), makeString(), NIL, nodeRead(), nodeTokenType(), OTHER_TOKEN, palloc(), parseNodeString(), pg_strtok(), RIGHT_PAREN, token, type, and val.

Referenced by _readA_Const(), _readA_Expr(), nodeRead(), and stringToNodeInternal().

◆ parseNodeString()

Node * parseNodeString ( void  )

Definition at line 565 of file readfuncs.c.

566{
568
569 /* Guard against stack overflow due to overly complex expressions */
571
572 token = pg_strtok(&length);
573
574#define MATCH(tokname, namelen) \
575 (length == namelen && memcmp(token, tokname, namelen) == 0)
576
577#include "readfuncs.switch.c"
578
579 elog(ERROR, "badly formatted node string \"%.32s\"...", token);
580 return NULL; /* keep compiler quiet */
581}
#define READ_TEMP_LOCALS()
Definition: readfuncs.c:50
void check_stack_depth(void)
Definition: stack_depth.c:95

References check_stack_depth(), elog, ERROR, pg_strtok(), READ_TEMP_LOCALS, and token.

Referenced by nodeRead().

◆ pg_strtok()

const char * pg_strtok ( int *  length)

Definition at line 153 of file read.c.

154{
155 const char *local_str; /* working pointer to string */
156 const char *ret_str; /* start of token to return */
157
158 local_str = pg_strtok_ptr;
159
160 while (*local_str == ' ' || *local_str == '\n' || *local_str == '\t')
161 local_str++;
162
163 if (*local_str == '\0')
164 {
165 *length = 0;
166 pg_strtok_ptr = local_str;
167 return NULL; /* no more tokens */
168 }
169
170 /*
171 * Now pointing at start of next token.
172 */
173 ret_str = local_str;
174
175 if (*local_str == '(' || *local_str == ')' ||
176 *local_str == '{' || *local_str == '}')
177 {
178 /* special 1-character token */
179 local_str++;
180 }
181 else
182 {
183 /* Normal token, possibly containing backslashes */
184 while (*local_str != '\0' &&
185 *local_str != ' ' && *local_str != '\n' &&
186 *local_str != '\t' &&
187 *local_str != '(' && *local_str != ')' &&
188 *local_str != '{' && *local_str != '}')
189 {
190 if (*local_str == '\\' && local_str[1] != '\0')
191 local_str += 2;
192 else
193 local_str++;
194 }
195 }
196
197 *length = local_str - ret_str;
198
199 /* Recognize special case for "empty" token */
200 if (*length == 2 && ret_str[0] == '<' && ret_str[1] == '>')
201 *length = 0;
202
203 pg_strtok_ptr = local_str;
204
205 return ret_str;
206}
static const char * pg_strtok_ptr
Definition: read.c:32

References pg_strtok_ptr.

Referenced by _readA_Const(), _readA_Expr(), _readBitmapset(), _readBoolExpr(), _readConst(), _readExtensibleNode(), nodeRead(), parseNodeString(), and readDatum().