PostgreSQL Source Code  git master
percentrepl.c File Reference
#include "postgres.h"
#include "common/percentrepl.h"
#include "lib/stringinfo.h"
Include dependency graph for percentrepl.c:

Go to the source code of this file.

Functions

char * replace_percent_placeholders (const char *instr, const char *param_name, const char *letters,...)
 

Function Documentation

◆ replace_percent_placeholders()

char* replace_percent_placeholders ( const char *  instr,
const char *  param_name,
const char *  letters,
  ... 
)

Definition at line 59 of file percentrepl.c.

60 {
61  StringInfoData result;
62 
63  initStringInfo(&result);
64 
65  for (const char *sp = instr; *sp; sp++)
66  {
67  if (*sp == '%')
68  {
69  if (sp[1] == '%')
70  {
71  /* Convert %% to a single % */
72  sp++;
73  appendStringInfoChar(&result, *sp);
74  }
75  else if (sp[1] == '\0')
76  {
77  /* Incomplete escape sequence, expected a character afterward */
78 #ifdef FRONTEND
79  pg_log_error("invalid value for parameter \"%s\": \"%s\"", param_name, instr);
80  pg_log_error_detail("String ends unexpectedly after escape character \"%%\".");
81  exit(1);
82 #else
83  ereport(ERROR,
84  errcode(ERRCODE_INVALID_PARAMETER_VALUE),
85  errmsg("invalid value for parameter \"%s\": \"%s\"", param_name, instr),
86  errdetail("String ends unexpectedly after escape character \"%%\"."));
87 #endif
88  }
89  else
90  {
91  /* Look up placeholder character */
92  bool found = false;
93  va_list ap;
94 
95  sp++;
96 
97  va_start(ap, letters);
98  for (const char *lp = letters; *lp; lp++)
99  {
100  char *val = va_arg(ap, char *);
101 
102  if (*sp == *lp)
103  {
104  if (val)
105  {
106  appendStringInfoString(&result, val);
107  found = true;
108  }
109  /* If val is NULL, we will report an error. */
110  break;
111  }
112  }
113  va_end(ap);
114  if (!found)
115  {
116  /* Unknown placeholder */
117 #ifdef FRONTEND
118  pg_log_error("invalid value for parameter \"%s\": \"%s\"", param_name, instr);
119  pg_log_error_detail("String contains unexpected placeholder \"%%%c\".", *sp);
120  exit(1);
121 #else
122  ereport(ERROR,
123  errcode(ERRCODE_INVALID_PARAMETER_VALUE),
124  errmsg("invalid value for parameter \"%s\": \"%s\"", param_name, instr),
125  errdetail("String contains unexpected placeholder \"%%%c\".", *sp));
126 #endif
127  }
128  }
129  }
130  else
131  {
132  appendStringInfoChar(&result, *sp);
133  }
134  }
135 
136  return result.data;
137 }
int errdetail(const char *fmt,...)
Definition: elog.c:1205
int errcode(int sqlerrcode)
Definition: elog.c:859
int errmsg(const char *fmt,...)
Definition: elog.c:1072
#define ERROR
Definition: elog.h:39
#define ereport(elevel,...)
Definition: elog.h:149
long val
Definition: informix.c:670
va_end(args)
exit(1)
va_start(args, fmt)
#define pg_log_error(...)
Definition: logging.h:106
#define pg_log_error_detail(...)
Definition: logging.h:109
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:182
void appendStringInfoChar(StringInfo str, char ch)
Definition: stringinfo.c:194
void initStringInfo(StringInfo str)
Definition: stringinfo.c:59

References appendStringInfoChar(), appendStringInfoString(), StringInfoData::data, ereport, errcode(), errdetail(), errmsg(), ERROR, exit(), initStringInfo(), pg_log_error, pg_log_error_detail, va_end(), va_start(), and val.

Referenced by BuildRestoreCommand(), ExecuteRecoveryCommand(), run_ssl_passphrase_command(), shell_archive_file(), and shell_construct_command().