OSDN Git Service

Simplify scanstr(), fix broken octal-escape code.
[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.11 1999/02/07 23:59:59 tgl Exp $
12  *
13  *-------------------------------------------------------------------------
14  */
15
16 #include "config.h"
17
18 #include <ctype.h>
19 #include <string.h>
20
21 #include "postgres.h"
22 #include "miscadmin.h"
23 #include "parser/scansup.h"
24 #include "utils/elog.h"
25
26 /* ----------------
27  *              scanstr
28  *
29  * if the string passed in has escaped codes, map the escape codes to actual
30  * chars
31  *
32  * the string returned is a pointer to static storage and should NOT
33  * be freed by the caller.
34  * ----------------
35  */
36
37 char *
38 scanstr(char *s)
39 {
40         static char newStr[MAX_PARSE_BUFFER];
41         int                     len,
42                                 i,
43                                 j;
44
45         if (s == NULL || s[0] == '\0')
46                 return s;
47
48         len = strlen(s);
49
50         for (i = 0, j = 0; i < len; i++)
51         {
52                 if (s[i] == '\'')
53                 {
54                         /* Note: if scanner is working right, unescaped quotes can only
55                          * appear in pairs, so there should be another character.
56                          */
57                         i++;
58                         newStr[j] = s[i];
59                 }
60                 else if (s[i] == '\\')
61                 {
62                         i++;
63                         switch (s[i])
64                         {
65                                 case 'b':
66                                         newStr[j] = '\b';
67                                         break;
68                                 case 'f':
69                                         newStr[j] = '\f';
70                                         break;
71                                 case 'n':
72                                         newStr[j] = '\n';
73                                         break;
74                                 case 'r':
75                                         newStr[j] = '\r';
76                                         break;
77                                 case 't':
78                                         newStr[j] = '\t';
79                                         break;
80                                 case '0':
81                                 case '1':
82                                 case '2':
83                                 case '3':
84                                 case '4':
85                                 case '5':
86                                 case '6':
87                                 case '7':
88                                         {
89                                                 int                     k;
90                                                 long            octVal = 0;
91
92                                                 for (k = 0;
93                                                          s[i + k] >= '0' && s[i + k] <= '7' && k < 3;
94                                                          k++)
95                                                         octVal = (octVal << 3) + (s[i + k] - '0');
96                                                 i += k - 1;
97                                                 newStr[j] = ((char) octVal);
98                                         }
99                                         break;
100                                 default:
101                                         newStr[j] = s[i];
102                                         break;
103                         }                                       /* switch */
104                 }                                               /* s[i] == '\\' */
105                 else
106                         newStr[j] = s[i];
107                 j++;
108         }
109         newStr[j] = '\0';
110         return newStr;
111 }