OSDN Git Service

Finished merging in src/backend from Dr. George's source tree
[pg-rex/syncrep.git] / src / backend / parser / scansup.c
1 /*-------------------------------------------------------------------------
2  *
3  * scansup.c--
4  *    support routines for the lex/flex scanner, used by both the normal
5  * backend as well as the bootstrap backend
6  *
7  * Copyright (c) 1994, Regents of the University of California
8  *
9  *
10  * IDENTIFICATION
11  *    $Header: /cvsroot/pgsql/src/backend/parser/scansup.c,v 1.2 1996/07/23 02:23:35 scrappy Exp $
12  *
13  *-------------------------------------------------------------------------
14  */
15 #include <ctype.h>
16 #include <string.h>
17 #include "c.h"
18 #include "postgres.h"
19 #include "miscadmin.h"
20 #include "utils/elog.h"
21 #include "parser/scansup.h"
22
23 /*
24  *      Scanner error handler.
25  */
26 static void
27 serror(char *str)
28 {
29     elog(WARN, "*** scanner error: %s\n", str);
30 }
31
32 /* ----------------
33  *      scanstr
34  * 
35  * if the string passed in has escaped codes, map the escape codes to actual
36  * chars
37  *
38  * also, remove leading and ending quotes '"' if any 
39  *
40  * the string passed in must be non-null
41  *
42  * the string returned is a pointer to static storage and should NOT
43  * be freed by the CALLER.
44  * ----------------
45  */
46
47 char* 
48 scanstr(char *s)
49 {
50     static char newStr[MAX_PARSE_BUFFER]; 
51     int len, i, start, j;
52     char delimiter;
53     
54     if (s == NULL || s[0] == '\0')
55         return s;
56
57     len = strlen(s);
58     start = 0;
59
60     /* remove leading and trailing quotes, if any */
61     /* the normal backend lexer only accepts single quotes, but the
62        bootstrap lexer accepts double quotes */
63     delimiter = 0;
64     if (s[0] == '"' || s[0] == '\''){
65         delimiter = s[0];
66         start = 1;
67     }
68     if (delimiter != 0)  {
69         if (s[len-1] == delimiter) 
70             len = len - 1;
71         else
72             serror("mismatched quote delimiters");
73     }
74
75     for (i = start, j = 0; i < len ; i++) {
76         if (s[i] == '\'') {
77             i = i + 1;
78             if (s[i] == '\'')
79                 newStr[j] = '\'';
80         }
81         else {
82             if (s[i] == '\\') {
83                 i = i + 1;
84                 switch (s[i]) {
85                 case '\\':
86                     newStr[j] = '\\';
87                     break;
88                 case 'b':
89                     newStr[j] = '\b';
90                     break;
91                 case 'f':
92                     newStr[j] = '\f';
93                     break;
94                 case 'n':
95                     newStr[j] = '\n';
96                     break;
97                 case 'r':
98                     newStr[j] = '\r';
99                     break;
100                 case 't':
101                     newStr[j] = '\t';
102                     break;
103                 case '"':
104                     newStr[j] = '"';
105                     break;
106                 case '\'':
107                     newStr[j] = '\'';
108                     break;
109                 case '0': 
110                 case '1': 
111                 case '2': 
112                 case '3': 
113                 case '4': 
114                 case '5':
115                 case '6': 
116                 case '7':
117                     {
118                         char octal[4];
119                         int k;
120                         long octVal;
121
122                         for (k=0; 
123                              s[i+k] >= '0' && s[i+k] <= '7' && k < 3;
124                              k++)
125                             octal[k] = s[i+k];
126                         i += k-1;
127                         octal[3] = '\0';
128                     
129                         octVal = strtol(octal,0,8);
130 /*                      elog (NOTICE, "octal = %s octVal = %d, %od", octal, octVal, octVal);*/
131                         if (octVal <= 0377) {
132                             newStr[j] = ((char)octVal);
133                             break;
134                         }
135                     }
136                 default:
137 #ifdef ESCAPE_PATCH
138                     newStr[j] = s[i];
139 #else
140                     elog (WARN, "Bad escape sequence, s[i] = %d", s[i]);
141 #endif
142                 } /* switch */
143             } /* s[i] == '\\' */
144             else
145                 newStr[j] = s[i];
146         }
147         j++;
148     }
149     newStr[j] = '\0';
150     return newStr;
151 }
152