Imported from ../bash-2.01.tar.gz.
[platform/upstream/bash.git] / lib / readline / shell.c
1 /* shell.c -- readline utility functions that are normally provided by
2               bash when readline is linked as part of the shell. */
3
4 /* Copyright (C) 1997 Free Software Foundation, Inc.
5
6    This file is part of the GNU Readline Library, a library for
7    reading lines of text with interactive input and history editing.
8
9    The GNU Readline Library is free software; you can redistribute it
10    and/or modify it under the terms of the GNU General Public License
11    as published by the Free Software Foundation; either version 1, or
12    (at your option) any later version.
13
14    The GNU Readline Library is distributed in the hope that it will be
15    useful, but WITHOUT ANY WARRANTY; without even the implied warranty
16    of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18
19    The GNU General Public License is often shipped with GNU software, and
20    is generally kept in a file called COPYING or LICENSE.  If you do not
21    have a copy of the license, write to the Free Software Foundation,
22    675 Mass Ave, Cambridge, MA 02139, USA. */
23 #define READLINE_LIBRARY
24
25 #if defined (HAVE_CONFIG_H)
26 #  include <config.h>
27 #endif
28
29 #if defined (HAVE_UNISTD_H)
30 #  include <unistd.h>
31 #endif /* HAVE_UNISTD_H */
32
33 #if defined (HAVE_STDLIB_H)
34 #  include <stdlib.h>
35 #else
36 #  include "ansi_stdlib.h"
37 #endif /* HAVE_STDLIB_H */
38
39 extern char *xmalloc (), *xrealloc ();
40
41 #if !defined (SHELL)
42
43 #ifdef savestring
44 #undef savestring
45 #endif
46
47 /* Backwards compatibility, now that savestring has been removed from
48    all `public' readline header files. */
49 char *
50 savestring (s)
51      char *s;
52 {
53   return ((char *)strcpy (xmalloc (1 + (int)strlen (s)), (s)));
54 }
55
56 /* Does shell-like quoting using single quotes. */
57 char *
58 single_quote (string)
59      char *string;
60 {
61   register int c;
62   char *result, *r, *s;
63
64   result = (char *)xmalloc (3 + (3 * strlen (string)));
65   r = result;
66   *r++ = '\'';
67
68   for (s = string; s && (c = *s); s++)
69     {
70       *r++ = c;
71
72       if (c == '\'')
73         {
74           *r++ = '\\';  /* insert escaped single quote */
75           *r++ = '\'';
76           *r++ = '\'';  /* start new quoted string */
77         }
78     }
79
80   *r++ = '\'';
81   *r = '\0';
82
83   return (result);
84 }
85
86 /* Set the environment variables LINES and COLUMNS to lines and cols,
87    respectively. */
88 void
89 set_lines_and_columns (lines, cols)
90      int lines, cols;
91 {
92   char *b;
93
94 #if defined (HAVE_PUTENV)
95   b = xmalloc (24);
96   sprintf (b, "LINES=%d", lines);
97   putenv (b);
98   b = xmalloc (24);
99   sprintf (b, "COLUMNS=%d", cols);
100   putenv (b);
101 #else /* !HAVE_PUTENV */
102 #  if defined (HAVE_SETENV)
103   b = xmalloc (8);
104   sprintf (b, "%d", lines);
105   setenv ("LINES", b, 1);
106   b = xmalloc (8);
107   sprintf (b, "%d", cols);
108   setenv ("COLUMNS", b, 1);
109 #  endif /* HAVE_SETENV */
110 #endif /* !HAVE_PUTENV */
111 }
112
113 char *
114 get_env_value (varname)
115      char *varname;
116 {
117   return ((char *)getenv (varname));
118 }
119
120 #else /* SHELL */
121 extern char *get_string_value ();
122
123 char *
124 get_env_value (varname)
125      char *varname;
126 {
127   return get_string_value (varname);
128 }       
129 #endif /* SHELL */