]> granicus.if.org Git - postgresql/blob - src/backend/parser/scansup.c
Be careful to include postgres.h *before* any system headers, to ensure
[postgresql] / 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  * Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group
8  * Portions Copyright (c) 1994, Regents of the University of California
9  *
10  *
11  * IDENTIFICATION
12  *        $Header: /cvsroot/pgsql/src/backend/parser/scansup.c,v 1.23 2002/09/05 00:43:07 tgl Exp $
13  *
14  *-------------------------------------------------------------------------
15  */
16 #include "postgres.h"
17
18 #include <ctype.h>
19
20 #include "miscadmin.h"
21 #include "parser/scansup.h"
22
23 /* ----------------
24  *              scanstr
25  *
26  * if the string passed in has escaped codes, map the escape codes to actual
27  * chars
28  *
29  * the string returned is palloc'd and should eventually be pfree'd by the
30  * caller!
31  * ----------------
32  */
33
34 char *
35 scanstr(char *s)
36 {
37         char       *newStr;
38         int                     len,
39                                 i,
40                                 j;
41
42         if (s == NULL || s[0] == '\0')
43                 return pstrdup("");
44
45         len = strlen(s);
46
47         newStr = palloc(len + 1);       /* string cannot get longer */
48
49         for (i = 0, j = 0; i < len; i++)
50         {
51                 if (s[i] == '\'')
52                 {
53                         /*
54                          * Note: if scanner is working right, unescaped quotes can
55                          * only 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 }