PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
read.c File Reference
#include "postgres.h"
#include <ctype.h>
#include "nodes/pg_list.h"
#include "nodes/readfuncs.h"
#include "nodes/value.h"
Include dependency graph for read.c:

Go to the source code of this file.

Macros

#define RIGHT_PAREN   (1000000 + 1)
 
#define LEFT_PAREN   (1000000 + 2)
 
#define LEFT_BRACE   (1000000 + 3)
 
#define OTHER_TOKEN   (1000000 + 4)
 

Functions

void * stringToNode (char *str)
 
char * pg_strtok (int *length)
 
char * debackslash (char *token, int length)
 
static NodeTag nodeTokenType (char *token, int length)
 
void * nodeRead (char *token, int tok_len)
 

Variables

static char * pg_strtok_ptr = NULL
 

Macro Definition Documentation

#define LEFT_BRACE   (1000000 + 3)

Definition at line 186 of file read.c.

Referenced by nodeRead(), and nodeTokenType().

#define LEFT_PAREN   (1000000 + 2)

Definition at line 185 of file read.c.

Referenced by nodeRead(), and nodeTokenType().

#define OTHER_TOKEN   (1000000 + 4)

Definition at line 187 of file read.c.

Referenced by nodeRead(), and nodeTokenType().

#define RIGHT_PAREN   (1000000 + 1)

Definition at line 184 of file read.c.

Referenced by nodeRead(), and nodeTokenType().

Function Documentation

char* debackslash ( char *  token,
int  length 
)

Definition at line 168 of file read.c.

References palloc(), and result.

Referenced by nodeRead().

169 {
170  char *result = palloc(length + 1);
171  char *ptr = result;
172 
173  while (length > 0)
174  {
175  if (*token == '\\' && length > 1)
176  token++, length--;
177  *ptr++ = *token++;
178  length--;
179  }
180  *ptr = '\0';
181  return result;
182 }
int length(const List *list)
Definition: list.c:1271
return result
Definition: formatting.c:1618
void * palloc(Size size)
Definition: mcxt.c:849
void* nodeRead ( char *  token,
int  tok_len 
)

Definition at line 278 of file read.c.

References debackslash(), elog, ERROR, lappend(), lappend_int(), lappend_oid(), LEFT_BRACE, LEFT_PAREN, makeBitString(), makeFloat(), makeInteger(), makeString(), NIL, nodeRead(), nodeTokenType(), NULL, OTHER_TOKEN, palloc(), parseNodeString(), pg_strtok(), result, RIGHT_PAREN, T_BitString, T_Float, T_Integer, T_String, and val.

Referenced by nodeRead(), and stringToNode().

279 {
280  Node *result;
281  NodeTag type;
282 
283  if (token == NULL) /* need to read a token? */
284  {
285  token = pg_strtok(&tok_len);
286 
287  if (token == NULL) /* end of input */
288  return NULL;
289  }
290 
291  type = nodeTokenType(token, tok_len);
292 
293  switch ((int) type)
294  {
295  case LEFT_BRACE:
296  result = parseNodeString();
297  token = pg_strtok(&tok_len);
298  if (token == NULL || token[0] != '}')
299  elog(ERROR, "did not find '}' at end of input node");
300  break;
301  case LEFT_PAREN:
302  {
303  List *l = NIL;
304 
305  /*----------
306  * Could be an integer list: (i int int ...)
307  * or an OID list: (o int int ...)
308  * or a list of nodes/values: (node node ...)
309  *----------
310  */
311  token = pg_strtok(&tok_len);
312  if (token == NULL)
313  elog(ERROR, "unterminated List structure");
314  if (tok_len == 1 && token[0] == 'i')
315  {
316  /* List of integers */
317  for (;;)
318  {
319  int val;
320  char *endptr;
321 
322  token = pg_strtok(&tok_len);
323  if (token == NULL)
324  elog(ERROR, "unterminated List structure");
325  if (token[0] == ')')
326  break;
327  val = (int) strtol(token, &endptr, 10);
328  if (endptr != token + tok_len)
329  elog(ERROR, "unrecognized integer: \"%.*s\"",
330  tok_len, token);
331  l = lappend_int(l, val);
332  }
333  }
334  else if (tok_len == 1 && token[0] == 'o')
335  {
336  /* List of OIDs */
337  for (;;)
338  {
339  Oid val;
340  char *endptr;
341 
342  token = pg_strtok(&tok_len);
343  if (token == NULL)
344  elog(ERROR, "unterminated List structure");
345  if (token[0] == ')')
346  break;
347  val = (Oid) strtoul(token, &endptr, 10);
348  if (endptr != token + tok_len)
349  elog(ERROR, "unrecognized OID: \"%.*s\"",
350  tok_len, token);
351  l = lappend_oid(l, val);
352  }
353  }
354  else
355  {
356  /* List of other node types */
357  for (;;)
358  {
359  /* We have already scanned next token... */
360  if (token[0] == ')')
361  break;
362  l = lappend(l, nodeRead(token, tok_len));
363  token = pg_strtok(&tok_len);
364  if (token == NULL)
365  elog(ERROR, "unterminated List structure");
366  }
367  }
368  result = (Node *) l;
369  break;
370  }
371  case RIGHT_PAREN:
372  elog(ERROR, "unexpected right parenthesis");
373  result = NULL; /* keep compiler happy */
374  break;
375  case OTHER_TOKEN:
376  if (tok_len == 0)
377  {
378  /* must be "<>" --- represents a null pointer */
379  result = NULL;
380  }
381  else
382  {
383  elog(ERROR, "unrecognized token: \"%.*s\"", tok_len, token);
384  result = NULL; /* keep compiler happy */
385  }
386  break;
387  case T_Integer:
388 
389  /*
390  * we know that the token terminates on a char atol will stop at
391  */
392  result = (Node *) makeInteger(atol(token));
393  break;
394  case T_Float:
395  {
396  char *fval = (char *) palloc(tok_len + 1);
397 
398  memcpy(fval, token, tok_len);
399  fval[tok_len] = '\0';
400  result = (Node *) makeFloat(fval);
401  }
402  break;
403  case T_String:
404  /* need to remove leading and trailing quotes, and backslashes */
405  result = (Node *) makeString(debackslash(token + 1, tok_len - 2));
406  break;
407  case T_BitString:
408  {
409  char *val = palloc(tok_len);
410 
411  /* skip leading 'b' */
412  memcpy(val, token + 1, tok_len - 1);
413  val[tok_len - 1] = '\0';
414  result = (Node *) makeBitString(val);
415  break;
416  }
417  default:
418  elog(ERROR, "unrecognized node type: %d", (int) type);
419  result = NULL; /* keep compiler happy */
420  break;
421  }
422 
423  return (void *) result;
424 }
Value * makeString(char *str)
Definition: value.c:53
#define NIL
Definition: pg_list.h:69
char * pg_strtok(int *length)
Definition: read.c:107
Definition: nodes.h:506
return result
Definition: formatting.c:1618
void * nodeRead(char *token, int tok_len)
Definition: read.c:278
unsigned int Oid
Definition: postgres_ext.h:31
NodeTag
Definition: nodes.h:26
List * lappend_oid(List *list, Oid datum)
Definition: list.c:164
static NodeTag nodeTokenType(char *token, int length)
Definition: read.c:200
Value * makeInteger(long i)
Definition: value.c:23
#define ERROR
Definition: elog.h:43
#define RIGHT_PAREN
Definition: read.c:184
Value * makeBitString(char *str)
Definition: value.c:68
char * debackslash(char *token, int length)
Definition: read.c:168
List * lappend_int(List *list, int datum)
Definition: list.c:146
List * lappend(List *list, void *datum)
Definition: list.c:128
Value * makeFloat(char *numericStr)
Definition: value.c:38
#define LEFT_BRACE
Definition: read.c:186
#define NULL
Definition: c.h:229
Node * parseNodeString(void)
Definition: readfuncs.c:2395
Definition: nodes.h:280
void * palloc(Size size)
Definition: mcxt.c:849
#define elog
Definition: elog.h:219
#define OTHER_TOKEN
Definition: read.c:187
Definition: pg_list.h:45
long val
Definition: informix.c:689
#define LEFT_PAREN
Definition: read.c:185
static NodeTag nodeTokenType ( char *  token,
int  length 
)
static

Definition at line 200 of file read.c.

References LEFT_BRACE, LEFT_PAREN, length(), OTHER_TOKEN, RIGHT_PAREN, T_BitString, T_Float, T_Integer, T_String, and val.

Referenced by nodeRead().

201 {
202  NodeTag retval;
203  char *numptr;
204  int numlen;
205 
206  /*
207  * Check if the token is a number
208  */
209  numptr = token;
210  numlen = length;
211  if (*numptr == '+' || *numptr == '-')
212  numptr++, numlen--;
213  if ((numlen > 0 && isdigit((unsigned char) *numptr)) ||
214  (numlen > 1 && *numptr == '.' && isdigit((unsigned char) numptr[1])))
215  {
216  /*
217  * Yes. Figure out whether it is integral or float; this requires
218  * both a syntax check and a range check. strtol() can do both for us.
219  * We know the token will end at a character that strtol will stop at,
220  * so we do not need to modify the string.
221  */
222  long val;
223  char *endptr;
224 
225  errno = 0;
226  val = strtol(token, &endptr, 10);
227  (void) val; /* avoid compiler warning if unused */
228  if (endptr != token + length || errno == ERANGE
229 #ifdef HAVE_LONG_INT_64
230  /* if long > 32 bits, check for overflow of int4 */
231  || val != (long) ((int32) val)
232 #endif
233  )
234  return T_Float;
235  return T_Integer;
236  }
237 
238  /*
239  * these three cases do not need length checks, since pg_strtok() will
240  * always treat them as single-byte tokens
241  */
242  else if (*token == '(')
243  retval = LEFT_PAREN;
244  else if (*token == ')')
245  retval = RIGHT_PAREN;
246  else if (*token == '{')
247  retval = LEFT_BRACE;
248  else if (*token == '"' && length > 1 && token[length - 1] == '"')
249  retval = T_String;
250  else if (*token == 'b')
251  retval = T_BitString;
252  else
253  retval = OTHER_TOKEN;
254  return retval;
255 }
int length(const List *list)
Definition: list.c:1271
NodeTag
Definition: nodes.h:26
signed int int32
Definition: c.h:256
#define RIGHT_PAREN
Definition: read.c:184
#define LEFT_BRACE
Definition: read.c:186
Definition: nodes.h:280
#define OTHER_TOKEN
Definition: read.c:187
long val
Definition: informix.c:689
#define LEFT_PAREN
Definition: read.c:185
char* pg_strtok ( int *  length)

Definition at line 107 of file read.c.

References NULL, and pg_strtok_ptr.

Referenced by _readBitmapset(), _readBoolExpr(), _readConst(), _readCustomScan(), _readExtensibleNode(), nodeRead(), parseNodeString(), readAttrNumberCols(), readBoolCols(), readDatum(), readIntCols(), and readOidCols().

108 {
109  char *local_str; /* working pointer to string */
110  char *ret_str; /* start of token to return */
111 
112  local_str = pg_strtok_ptr;
113 
114  while (*local_str == ' ' || *local_str == '\n' || *local_str == '\t')
115  local_str++;
116 
117  if (*local_str == '\0')
118  {
119  *length = 0;
120  pg_strtok_ptr = local_str;
121  return NULL; /* no more tokens */
122  }
123 
124  /*
125  * Now pointing at start of next token.
126  */
127  ret_str = local_str;
128 
129  if (*local_str == '(' || *local_str == ')' ||
130  *local_str == '{' || *local_str == '}')
131  {
132  /* special 1-character token */
133  local_str++;
134  }
135  else
136  {
137  /* Normal token, possibly containing backslashes */
138  while (*local_str != '\0' &&
139  *local_str != ' ' && *local_str != '\n' &&
140  *local_str != '\t' &&
141  *local_str != '(' && *local_str != ')' &&
142  *local_str != '{' && *local_str != '}')
143  {
144  if (*local_str == '\\' && local_str[1] != '\0')
145  local_str += 2;
146  else
147  local_str++;
148  }
149  }
150 
151  *length = local_str - ret_str;
152 
153  /* Recognize special case for "empty" token */
154  if (*length == 2 && ret_str[0] == '<' && ret_str[1] == '>')
155  *length = 0;
156 
157  pg_strtok_ptr = local_str;
158 
159  return ret_str;
160 }
int length(const List *list)
Definition: list.c:1271
static char * pg_strtok_ptr
Definition: read.c:30
#define NULL
Definition: c.h:229
void* stringToNode ( char *  str)

Definition at line 38 of file read.c.

References nodeRead(), NULL, and pg_strtok_ptr.

Referenced by AddRelationNewConstraints(), AlterPolicy(), ATExecAttachPartition(), build_column_default(), ExecParallelGetQueryDesc(), ExecRelCheck(), fetch_function_defaults(), func_get_detail(), generate_partition_qual(), generateClonedIndexStmt(), get_relation_constraints(), get_typdefault(), load_domaintype_info(), make_ruledef(), make_viewdef(), MergeAttributes(), MergeWithExistingConstraint(), pg_get_constraintdef_worker(), pg_get_expr_worker(), pg_get_function_arg_default(), pg_get_indexdef_worker(), pg_get_partkeydef_worker(), pg_get_triggerdef_worker(), print_function_arguments(), ProcedureCreate(), RelationBuildPartitionDesc(), RelationBuildPartitionKey(), RelationBuildRowSecurity(), RelationBuildRuleLock(), RelationGetIndexExpressions(), RelationGetIndexPredicate(), RemoveRoleFromObjectPolicy(), transformTableLikeClause(), TriggerEnabled(), TypeCreate(), validateCheckConstraint(), and validateDomainConstraint().

39 {
40  char *save_strtok;
41  void *retval;
42 
43  /*
44  * We save and restore the pre-existing state of pg_strtok. This makes the
45  * world safe for re-entrant invocation of stringToNode, without incurring
46  * a lot of notational overhead by having to pass the next-character
47  * pointer around through all the readfuncs.c code.
48  */
49  save_strtok = pg_strtok_ptr;
50 
51  pg_strtok_ptr = str; /* point pg_strtok at the string to read */
52 
53  retval = nodeRead(NULL, 0); /* do the reading */
54 
55  pg_strtok_ptr = save_strtok;
56 
57  return retval;
58 }
void * nodeRead(char *token, int tok_len)
Definition: read.c:278
static char * pg_strtok_ptr
Definition: read.c:30
#define NULL
Definition: c.h:229

Variable Documentation

char* pg_strtok_ptr = NULL
static

Definition at line 30 of file read.c.

Referenced by pg_strtok(), and stringToNode().