summaryrefslogtreecommitdiff
path: root/src/bin/psql/stringutils.c
blob: c495fd496601376c46430f4b6444d6c3b3c558a8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#include <config.h>
#include <c.h>
#include "stringutils.h"

//#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
//#include <stdio.h>

#include <postgres.h>
#ifndef HAVE_STRDUP
#include <strdup.h>
#endif
#include <libpq-fe.h>



static void
unescape_quotes(char *source, char quote, char escape);


/*
 * Replacement for strtok() (a.k.a. poor man's flex)
 *
 * The calling convention is similar to that of strtok.
 * s -          string to parse, if NULL continue parsing the last string
 * delim -      set of characters that delimit tokens (usually whitespace)
 * quote -      set of characters that quote stuff, they're not part of the token
 * escape -     character than can quote quotes
 * was_quoted - if not NULL, stores the quoting character if any was encountered
 * token_pos -  if not NULL, receives a count to the start of the token in the
 *              parsed string
 *
 * Note that the string s is _not_ overwritten in this implementation.
 */
char * strtokx(const char *s,
	       const char *delim,
	       const char *quote,
	       char escape,
	       char * was_quoted,
	       unsigned int * token_pos)
{
    static char * storage = NULL; /* store the local copy of the users string here */
    static char * string  = NULL; /* pointer into storage where to continue on next call */
    /* variously abused variables: */
    unsigned int offset;
    char * start;
    char *cp = NULL;

    if (s) {
	free(storage);
	storage = strdup(s);
	string = storage;
    }

    if (!storage)
	return NULL;

    /* skip leading "whitespace" */
    offset = strspn(string, delim);

    /* end of string reached */
    if (string[offset] == '\0') {
	/* technically we don't need to free here, but we're nice */
	free(storage);
	storage = NULL;
	string = NULL;
	return NULL;
    }

    /* test if quoting character */
    if (quote)
	cp = strchr(quote, string[offset]);

    if (cp) {
	/* okay, we have a quoting character, now scan for the closer */
	char *p;
	start = &string[offset+1];

	if (token_pos)
	    *token_pos = start - storage;

	for(p = start;
	    *p && (*p != *cp || *(p-1) == escape) ;
#ifdef MULTIBYTE
	    p += PQmblen(p)
#else
	    p++
#endif
	    );

	/* not yet end of string? */
	if (*p != '\0') {
	    *p = '\0';
	    string = p + 1;
	    if (was_quoted)
		*was_quoted = *cp;
	    unescape_quotes (start, *cp, escape);
	    return start;
	}
	else {
	    if (was_quoted)
		*was_quoted = *cp;
	    string = p;

	    unescape_quotes (start, *cp, escape);
	    return start;
	}
    }

    /* otherwise no quoting character. scan till next delimiter */
    start = &string[offset];

    if (token_pos)
	*token_pos = start - storage;

    offset = strcspn(start, delim);
    if (was_quoted)
	*was_quoted = 0;

    if (start[offset] != '\0') {
	start[offset] = '\0';
	string = &start[offset]+1;

	return start;
    }
    else {
	string = &start[offset];
	return start;
    }
}




/*
 * unescape_quotes
 *
 * Resolves escaped quotes. Used by strtokx above.
 */
static void
unescape_quotes(char *source, char quote, char escape)
{
    char *p;
    char *destination, *tmp;

#ifdef USE_ASSERT_CHECKING
    assert(source);
#endif

    destination = (char *) calloc(1, strlen(source)+1);
    if (!destination) {
	perror("calloc");
	exit(EXIT_FAILURE);
    }

    tmp = destination;

    for (p = source; *p; p++)
    {
	char c;

	if (*p == escape && *(p+1) && quote == *(p+1)) {
	    c = *(p+1);
	    p++;
	}
	else
	    c = *p;

	*tmp = c;
	tmp ++;
    }

    /* Terminating null character */
    *tmp = '\0';

    strcpy(source, destination);
}