1996-07-09 08:22:35 +02:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
*
|
1999-02-14 00:22:53 +01:00
|
|
|
* scansup.c
|
1997-09-07 07:04:48 +02:00
|
|
|
* support routines for the lex/flex scanner, used by both the normal
|
1996-07-09 08:22:35 +02:00
|
|
|
* backend as well as the bootstrap backend
|
|
|
|
*
|
2000-01-26 06:58:53 +01:00
|
|
|
* Portions Copyright (c) 1996-2000, PostgreSQL, Inc
|
|
|
|
* Portions Copyright (c) 1994, Regents of the University of California
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
|
|
|
*
|
|
|
|
* IDENTIFICATION
|
2000-04-12 19:17:23 +02:00
|
|
|
* $Header: /cvsroot/pgsql/src/backend/parser/scansup.c,v 1.19 2000/04/12 17:15:27 momjian Exp $
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
1996-08-27 09:42:29 +02:00
|
|
|
|
1996-07-09 08:22:35 +02:00
|
|
|
#include <ctype.h>
|
1997-11-26 02:14:33 +01:00
|
|
|
|
1996-07-09 08:22:35 +02:00
|
|
|
#include "postgres.h"
|
|
|
|
#include "miscadmin.h"
|
|
|
|
#include "parser/scansup.h"
|
|
|
|
|
|
|
|
/* ----------------
|
1997-09-07 07:04:48 +02:00
|
|
|
* scanstr
|
|
|
|
*
|
1996-07-09 08:22:35 +02:00
|
|
|
* if the string passed in has escaped codes, map the escape codes to actual
|
|
|
|
* chars
|
|
|
|
*
|
1999-09-12 00:26:47 +02:00
|
|
|
* the string returned is palloc'd and should eventually be pfree'd by the
|
|
|
|
* caller!
|
1996-07-09 08:22:35 +02:00
|
|
|
* ----------------
|
|
|
|
*/
|
|
|
|
|
1998-02-26 05:46:47 +01:00
|
|
|
char *
|
1996-07-09 08:22:35 +02:00
|
|
|
scanstr(char *s)
|
|
|
|
{
|
1999-09-12 00:26:47 +02:00
|
|
|
char *newStr;
|
1997-09-08 04:41:22 +02:00
|
|
|
int len,
|
|
|
|
i,
|
|
|
|
j;
|
1996-07-09 08:22:35 +02:00
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
if (s == NULL || s[0] == '\0')
|
1999-09-12 00:26:47 +02:00
|
|
|
return pstrdup("");
|
1996-07-09 08:22:35 +02:00
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
len = strlen(s);
|
|
|
|
|
2000-04-12 19:17:23 +02:00
|
|
|
newStr = palloc(len + 1); /* string cannot get longer */
|
1999-09-12 00:26:47 +02:00
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
for (i = 0, j = 0; i < len; i++)
|
|
|
|
{
|
|
|
|
if (s[i] == '\'')
|
|
|
|
{
|
1999-05-25 18:15:34 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Note: if scanner is working right, unescaped quotes can
|
|
|
|
* only appear in pairs, so there should be another character.
|
1999-02-08 00:59:59 +01:00
|
|
|
*/
|
|
|
|
i++;
|
|
|
|
newStr[j] = s[i];
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
1999-02-08 00:59:59 +01:00
|
|
|
else if (s[i] == '\\')
|
1997-09-07 07:04:48 +02:00
|
|
|
{
|
1999-02-08 00:59:59 +01:00
|
|
|
i++;
|
|
|
|
switch (s[i])
|
1997-09-07 07:04:48 +02:00
|
|
|
{
|
1999-02-08 00:59:59 +01:00
|
|
|
case 'b':
|
|
|
|
newStr[j] = '\b';
|
|
|
|
break;
|
|
|
|
case 'f':
|
|
|
|
newStr[j] = '\f';
|
|
|
|
break;
|
|
|
|
case 'n':
|
|
|
|
newStr[j] = '\n';
|
|
|
|
break;
|
|
|
|
case 'r':
|
|
|
|
newStr[j] = '\r';
|
|
|
|
break;
|
|
|
|
case 't':
|
|
|
|
newStr[j] = '\t';
|
|
|
|
break;
|
|
|
|
case '0':
|
|
|
|
case '1':
|
|
|
|
case '2':
|
|
|
|
case '3':
|
|
|
|
case '4':
|
|
|
|
case '5':
|
|
|
|
case '6':
|
|
|
|
case '7':
|
|
|
|
{
|
|
|
|
int k;
|
|
|
|
long octVal = 0;
|
1996-07-09 08:22:35 +02:00
|
|
|
|
1999-02-08 00:59:59 +01:00
|
|
|
for (k = 0;
|
1997-09-07 07:04:48 +02:00
|
|
|
s[i + k] >= '0' && s[i + k] <= '7' && k < 3;
|
1999-02-08 00:59:59 +01:00
|
|
|
k++)
|
|
|
|
octVal = (octVal << 3) + (s[i + k] - '0');
|
|
|
|
i += k - 1;
|
|
|
|
newStr[j] = ((char) octVal);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
newStr[j] = s[i];
|
|
|
|
break;
|
|
|
|
} /* switch */
|
|
|
|
} /* s[i] == '\\' */
|
|
|
|
else
|
|
|
|
newStr[j] = s[i];
|
1997-09-07 07:04:48 +02:00
|
|
|
j++;
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
1997-09-07 07:04:48 +02:00
|
|
|
newStr[j] = '\0';
|
|
|
|
return newStr;
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|