#include <config.h>
#if defined (HAVE_UNISTD_H)
# ifdef _MINIX
# include <sys/types.h>
# endif
# include <unistd.h>
#endif
#include <stdio.h>
#include "syntax.h"
#include <xmalloc.h>
char *
sh_single_quote (string)
char *string;
{
register int c;
char *result, *r, *s;
result = (char *)xmalloc (3 + (4 * strlen (string)));
r = result;
*r++ = '\'';
for (s = string; s && (c = *s); s++)
{
*r++ = c;
if (c == '\'')
{
*r++ = '\\';
*r++ = '\'';
*r++ = '\'';
}
}
*r++ = '\'';
*r = '\0';
return (result);
}
char *
sh_double_quote (string)
char *string;
{
register unsigned char c;
char *result, *r, *s;
result = (char *)xmalloc (3 + (2 * strlen (string)));
r = result;
*r++ = '"';
for (s = string; s && (c = *s); s++)
{
if (sh_syntaxtab[c] & CBSDQUOTE)
*r++ = '\\';
*r++ = c;
}
*r++ = '"';
*r = '\0';
return (result);
}
char *
sh_un_double_quote (string)
char *string;
{
register int c, pass_next;
char *result, *r, *s;
r = result = (char *)xmalloc (strlen (string) + 1);
for (pass_next = 0, s = string; s && (c = *s); s++)
{
if (pass_next)
{
*r++ = c;
pass_next = 0;
continue;
}
if (c == '\\' && (sh_syntaxtab[(unsigned char) s[1]] & CBSDQUOTE))
{
pass_next = 1;
continue;
}
*r++ = c;
}
*r = '\0';
return result;
}
char *
sh_backslash_quote (string)
char *string;
{
int c;
char *result, *r, *s;
result = (char *)xmalloc (2 * strlen (string) + 1);
for (r = result, s = string; s && (c = *s); s++)
{
switch (c)
{
case ' ': case '\t': case '\n':
case '\'': case '"': case '\\':
case '|': case '&': case ';':
case '(': case ')': case '<': case '>':
case '!': case '{': case '}':
case '*': case '[': case '?': case ']':
case '^':
case '$': case '`':
case ',':
*r++ = '\\';
*r++ = c;
break;
#if 0
case '~':
if (s == string || s[-1] == '=' || s[-1] == ':')
*r++ = '\\';
*r++ = c;
break;
#endif
case '#':
if (s == string)
*r++ = '\\';
default:
*r++ = c;
break;
}
}
*r = '\0';
return (result);
}
#if defined (PROMPT_STRING_DECODE)
char *
sh_backslash_quote_for_double_quotes (string)
char *string;
{
unsigned char c;
char *result, *r, *s;
result = (char *)xmalloc (2 * strlen (string) + 1);
for (r = result, s = string; s && (c = *s); s++)
{
if (sh_syntaxtab[c] & CBSDQUOTE)
*r++ = '\\';
*r++ = c;
}
*r = '\0';
return (result);
}
#endif
int
sh_contains_shell_metas (string)
char *string;
{
char *s;
for (s = string; s && *s; s++)
{
switch (*s)
{
case ' ': case '\t': case '\n':
case '\'': case '"': case '\\':
case '|': case '&': case ';':
case '(': case ')': case '<': case '>':
case '!': case '{': case '}':
case '*': case '[': case '?': case ']':
case '^':
case '$': case '`':
return (1);
case '~':
if (s == string || s[-1] == '=' || s[-1] == ':')
return (1);
break;
case '#':
if (s == string)
return (1);
default:
break;
}
}
return (0);
}