Thanks to visit codestin.com
Credit goes to doxygen.postgresql.org

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

Go to the source code of this file.

Data Structures

struct  PromptInterruptContext
 

Typedefs

typedef struct StringInfoDataStringInfo
 
typedef struct PromptInterruptContext PromptInterruptContext
 

Functions

bool pg_str_endswith (const char *str, const char *end)
 
int strtoint (const char *pg_restrict str, char **pg_restrict endptr, int base)
 
char * pg_clean_ascii (const char *str, int alloc_flags)
 
int pg_strip_crlf (char *str)
 
bool pg_is_ascii (const char *str)
 
char * pg_get_line (FILE *stream, PromptInterruptContext *prompt_ctx)
 
bool pg_get_line_buf (FILE *stream, StringInfo buf)
 
bool pg_get_line_append (FILE *stream, StringInfo buf, PromptInterruptContext *prompt_ctx)
 
char * simple_prompt (const char *prompt, bool echo)
 
char * simple_prompt_extended (const char *prompt, bool echo, PromptInterruptContext *prompt_ctx)
 

Typedef Documentation

◆ PromptInterruptContext

◆ StringInfo

typedef struct StringInfoData* StringInfo

Definition at line 15 of file string.h.

Function Documentation

◆ pg_clean_ascii()

char * pg_clean_ascii ( const char *  str,
int  alloc_flags 
)

Definition at line 85 of file string.c.

86{
87 size_t dstlen;
88 char *dst;
89 const char *p;
90 size_t i = 0;
91
92 /* Worst case, each byte can become four bytes, plus a null terminator. */
93 dstlen = strlen(str) * 4 + 1;
94
95#ifdef FRONTEND
96 dst = malloc(dstlen);
97#else
98 dst = palloc_extended(dstlen, alloc_flags);
99#endif
100
101 if (!dst)
102 return NULL;
103
104 for (p = str; *p != '\0'; p++)
105 {
106
107 /* Only allow clean ASCII chars in the string */
108 if (*p < 32 || *p > 126)
109 {
110 Assert(i < (dstlen - 3));
111 snprintf(&dst[i], dstlen - i, "\\x%02x", (unsigned char) *p);
112 i += 4;
113 }
114 else
115 {
116 Assert(i < dstlen);
117 dst[i] = *p;
118 i++;
119 }
120 }
121
122 Assert(i < dstlen);
123 dst[i] = '\0';
124 return dst;
125}
Assert(PointerIsAligned(start, uint64))
const char * str
#define malloc(a)
Definition: header.h:50
int i
Definition: isn.c:77
void * palloc_extended(Size size, int flags)
Definition: mcxt.c:1417
#define snprintf
Definition: port.h:239

References Assert(), i, malloc, palloc_extended(), snprintf, and str.

Referenced by check_application_name(), check_cluster_name(), prepare_cert_name(), and ProcessStartupPacket().

◆ pg_get_line()

char * pg_get_line ( FILE *  stream,
PromptInterruptContext prompt_ctx 
)

Definition at line 59 of file pg_get_line.c.

60{
62
64
65 if (!pg_get_line_append(stream, &buf, prompt_ctx))
66 {
67 /* ensure that free() doesn't mess up errno */
68 int save_errno = errno;
69
70 pfree(buf.data);
71 errno = save_errno;
72 return NULL;
73 }
74
75 return buf.data;
76}
void pfree(void *pointer)
Definition: mcxt.c:1594
bool pg_get_line_append(FILE *stream, StringInfo buf, PromptInterruptContext *prompt_ctx)
Definition: pg_get_line.c:124
static char * buf
Definition: pg_test_fsync.c:72
void initStringInfo(StringInfo str)
Definition: stringinfo.c:97

References buf, initStringInfo(), pfree(), and pg_get_line_append().

Referenced by get_su_pwd(), pipe_read_line(), and simple_prompt_extended().

◆ pg_get_line_append()

bool pg_get_line_append ( FILE *  stream,
StringInfo  buf,
PromptInterruptContext prompt_ctx 
)

Definition at line 124 of file pg_get_line.c.

126{
127 int orig_len = buf->len;
128
129 if (prompt_ctx && sigsetjmp(*((sigjmp_buf *) prompt_ctx->jmpbuf), 1) != 0)
130 {
131 /* Got here with longjmp */
132 prompt_ctx->canceled = true;
133 /* Discard any data we collected before detecting error */
134 buf->len = orig_len;
135 buf->data[orig_len] = '\0';
136 return false;
137 }
138
139 /* Loop until newline or EOF/error */
140 for (;;)
141 {
142 char *res;
143
144 /* Enable longjmp while waiting for input */
145 if (prompt_ctx)
146 *(prompt_ctx->enabled) = true;
147
148 /* Read some data, appending it to whatever we already have */
149 res = fgets(buf->data + buf->len, buf->maxlen - buf->len, stream);
150
151 /* Disable longjmp again, then break if fgets failed */
152 if (prompt_ctx)
153 *(prompt_ctx->enabled) = false;
154
155 if (res == NULL)
156 break;
157
158 /* Got data, so update buf->len */
159 buf->len += strlen(buf->data + buf->len);
160
161 /* Done if we have collected a newline */
162 if (buf->len > orig_len && buf->data[buf->len - 1] == '\n')
163 return true;
164
165 /* Make some more room in the buffer, and loop to read more data */
167 }
168
169 /* Check for I/O errors and EOF */
170 if (ferror(stream) || buf->len == orig_len)
171 {
172 /* Discard any data we collected before detecting error */
173 buf->len = orig_len;
174 buf->data[orig_len] = '\0';
175 return false;
176 }
177
178 /* No newline at EOF, but we did collect some data */
179 return true;
180}
void enlargeStringInfo(StringInfo str, int needed)
Definition: stringinfo.c:337
volatile sig_atomic_t * enabled
Definition: string.h:22

References buf, PromptInterruptContext::canceled, PromptInterruptContext::enabled, enlargeStringInfo(), and PromptInterruptContext::jmpbuf.

Referenced by pg_get_line(), pg_get_line_buf(), and tokenize_auth_file().

◆ pg_get_line_buf()

bool pg_get_line_buf ( FILE *  stream,
StringInfo  buf 
)

Definition at line 95 of file pg_get_line.c.

96{
97 /* We just need to drop any data from the previous call */
99 return pg_get_line_append(stream, buf, NULL);
100}
void resetStringInfo(StringInfo str)
Definition: stringinfo.c:126

References buf, pg_get_line_append(), and resetStringInfo().

Referenced by ecpg_filter_source(), ecpg_filter_stderr(), filter_read_item(), read_quoted_string(), readfile(), SortTocFromFile(), and tsearch_readline().

◆ pg_is_ascii()

bool pg_is_ascii ( const char *  str)

Definition at line 132 of file string.c.

133{
134 while (*str)
135 {
136 if (IS_HIGHBIT_SET(*str))
137 return false;
138 str++;
139 }
140 return true;
141}
#define IS_HIGHBIT_SET(ch)
Definition: c.h:1155

References IS_HIGHBIT_SET, and str.

Referenced by check_locale(), check_locale_name(), parse_key_value_arrays(), pg_import_system_collations(), and pg_saslprep().

◆ pg_str_endswith()

bool pg_str_endswith ( const char *  str,
const char *  end 
)

Definition at line 31 of file string.c.

32{
33 size_t slen = strlen(str);
34 size_t elen = strlen(end);
35
36 /* can't be a postfix if longer */
37 if (elen > slen)
38 return false;
39
40 /* compare the end of the strings */
41 str += slen - elen;
42 return strcmp(str, end) == 0;
43}

References str.

Referenced by decide_file_action(), and StartupReplicationSlots().

◆ pg_strip_crlf()

int pg_strip_crlf ( char *  str)

Definition at line 154 of file string.c.

155{
156 int len = strlen(str);
157
158 while (len > 0 && (str[len - 1] == '\n' ||
159 str[len - 1] == '\r'))
160 str[--len] = '\0';
161
162 return len;
163}
const void size_t len

References len, and str.

Referenced by adjust_data_dir(), check_exec(), CheckDataVersion(), get_control_data(), get_prompt(), get_sock_dir(), get_su_pwd(), getRestoreCommand(), passwordFromFile(), run_ssl_passphrase_command(), simple_prompt_extended(), and tokenize_auth_file().

◆ simple_prompt()

char * simple_prompt ( const char *  prompt,
bool  echo 
)

Definition at line 38 of file sprompt.c.

39{
40 return simple_prompt_extended(prompt, echo, NULL);
41}
char * simple_prompt_extended(const char *prompt, bool echo, PromptInterruptContext *prompt_ctx)
Definition: sprompt.c:53

References simple_prompt_extended().

Referenced by ConnectDatabase(), connectDatabase(), ConnectDatabaseAhx(), doConnect(), get_su_pwd(), GetConnection(), main(), sql_conn(), vacuumlo(), and yesno_prompt().

◆ simple_prompt_extended()

char * simple_prompt_extended ( const char *  prompt,
bool  echo,
PromptInterruptContext prompt_ctx 
)

Definition at line 53 of file sprompt.c.

55{
56 char *result;
57 FILE *termin,
58 *termout;
59#if defined(HAVE_TERMIOS_H)
60 struct termios t_orig,
61 t;
62#elif defined(WIN32)
63 HANDLE t = NULL;
64 DWORD t_orig = 0;
65#endif
66
67#ifdef WIN32
68
69 /*
70 * A Windows console has an "input code page" and an "output code page";
71 * these usually match each other, but they rarely match the "Windows ANSI
72 * code page" defined at system boot and expected of "char *" arguments to
73 * Windows API functions. The Microsoft CRT write() implementation
74 * automatically converts text between these code pages when writing to a
75 * console. To identify such file descriptors, it calls GetConsoleMode()
76 * on the underlying HANDLE, which in turn requires GENERIC_READ access on
77 * the HANDLE. Opening termout in mode "w+" allows that detection to
78 * succeed. Otherwise, write() would not recognize the descriptor as a
79 * console, and non-ASCII characters would display incorrectly.
80 *
81 * XXX fgets() still receives text in the console's input code page. This
82 * makes non-ASCII credentials unportable.
83 *
84 * Unintuitively, we also open termin in mode "w+", even though we only
85 * read it; that's needed for SetConsoleMode() to succeed.
86 */
87 termin = fopen("CONIN$", "w+");
88 termout = fopen("CONOUT$", "w+");
89#else
90
91 /*
92 * Do not try to collapse these into one "w+" mode file. Doesn't work on
93 * some platforms (eg, HPUX 10.20).
94 */
95 termin = fopen("/dev/tty", "r");
96 termout = fopen("/dev/tty", "w");
97#endif
98 if (!termin || !termout
99#ifdef WIN32
100
101 /*
102 * Direct console I/O does not work from the MSYS 1.0.10 console. Writes
103 * reach nowhere user-visible; reads block indefinitely. XXX This affects
104 * most Windows terminal environments, including rxvt, mintty, Cygwin
105 * xterm, Cygwin sshd, and PowerShell ISE. Switch to a more-generic test.
106 */
107 || (getenv("OSTYPE") && strcmp(getenv("OSTYPE"), "msys") == 0)
108#endif
109 )
110 {
111 if (termin)
112 fclose(termin);
113 if (termout)
114 fclose(termout);
115 termin = stdin;
116 termout = stderr;
117 }
118
119 if (!echo)
120 {
121#if defined(HAVE_TERMIOS_H)
122 /* disable echo via tcgetattr/tcsetattr */
123 tcgetattr(fileno(termin), &t);
124 t_orig = t;
125 t.c_lflag &= ~ECHO;
126 tcsetattr(fileno(termin), TCSAFLUSH, &t);
127#elif defined(WIN32)
128 /* need the file's HANDLE to turn echo off */
129 t = (HANDLE) _get_osfhandle(_fileno(termin));
130
131 /* save the old configuration first */
132 GetConsoleMode(t, &t_orig);
133
134 /* set to the new mode */
135 SetConsoleMode(t, ENABLE_LINE_INPUT | ENABLE_PROCESSED_INPUT);
136#endif
137 }
138
139 if (prompt)
140 {
141 fputs(_(prompt), termout);
142 fflush(termout);
143 }
144
145 result = pg_get_line(termin, prompt_ctx);
146
147 /* If we failed to read anything, just return an empty string */
148 if (result == NULL)
149 result = pg_strdup("");
150
151 /* strip trailing newline, including \r in case we're on Windows */
152 (void) pg_strip_crlf(result);
153
154 if (!echo)
155 {
156 /* restore previous echo behavior, then echo \n */
157#if defined(HAVE_TERMIOS_H)
158 tcsetattr(fileno(termin), TCSAFLUSH, &t_orig);
159 fputs("\n", termout);
160 fflush(termout);
161#elif defined(WIN32)
162 SetConsoleMode(t, t_orig);
163 fputs("\n", termout);
164 fflush(termout);
165#endif
166 }
167 else if (prompt_ctx && prompt_ctx->canceled)
168 {
169 /* also echo \n if prompt was canceled */
170 fputs("\n", termout);
171 fflush(termout);
172 }
173
174 if (termin != stdin)
175 {
176 fclose(termin);
177 fclose(termout);
178 }
179
180 return result;
181}
#define _(x)
Definition: elog.c:91
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
char * pg_get_line(FILE *stream, PromptInterruptContext *prompt_ctx)
Definition: pg_get_line.c:59
int pg_strip_crlf(char *str)
Definition: string.c:154

References _, PromptInterruptContext::canceled, pg_get_line(), pg_strdup(), and pg_strip_crlf().

Referenced by exec_command_password(), exec_command_prompt(), prompt_for_password(), and simple_prompt().

◆ strtoint()

int strtoint ( const char *pg_restrict  str,
char **pg_restrict  endptr,
int  base 
)

Definition at line 50 of file string.c.

51{
52 long val;
53
54 val = strtol(str, endptr, base);
55 if (val != (int) val)
56 errno = ERANGE;
57 return (int) val;
58}
long val
Definition: informix.c:689

References str, and val.

Referenced by buildDefItem(), DecodeDateTime(), DecodeInterval(), DecodeNumber(), DecodeNumberField(), DecodeTime(), DecodeTimeCommon(), DecodeTimeOnly(), DecodeTimezone(), exec_command_watch(), get_path_all(), jsonb_get_element(), nodeTokenType(), option_parse_int(), process_integer_literal(), push_path(), px_crypt_shacrypt(), and setPathArray().