553f3c1cc902b2162872d8f393468cee6b376fff
[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 #  ifdef _MINIX
31 #    include <sys/types.h>
32 #  endif
33 #  include <unistd.h>
34 #endif /* HAVE_UNISTD_H */
35
36 #if defined (HAVE_STDLIB_H)
37 #  include <stdlib.h>
38 #else
39 #  include "ansi_stdlib.h"
40 #endif /* HAVE_STDLIB_H */
41
42 #if defined (HAVE_STRING_H)
43 #  include <string.h>
44 #else
45 #  include <strings.h>
46 #endif /* !HAVE_STRING_H */
47
48 extern char *xmalloc (), *xrealloc ();
49
50 #if !defined (SHELL)
51
52 #ifdef savestring
53 #undef savestring
54 #endif
55
56 /* Backwards compatibility, now that savestring has been removed from
57    all `public' readline header files. */
58 char *
59 savestring (s)
60      char *s;
61 {
62   return ((char *)strcpy (xmalloc (1 + (int)strlen (s)), (s)));
63 }
64
65 /* Does shell-like quoting using single quotes. */
66 char *
67 single_quote (string)
68      char *string;
69 {
70   register int c;
71   char *result, *r, *s;
72
73   result = (char *)xmalloc (3 + (3 * strlen (string)));
74   r = result;
75   *r++ = '\'';
76
77   for (s = string; s && (c = *s); s++)
78     {
79       *r++ = c;
80
81       if (c == '\'')
82         {
83           *r++ = '\\';  /* insert escaped single quote */
84           *r++ = '\'';
85           *r++ = '\'';  /* start new quoted string */
86         }
87     }
88
89   *r++ = '\'';
90   *r = '\0';
91
92   return (result);
93 }
94
95 /* Set the environment variables LINES and COLUMNS to lines and cols,
96    respectively. */
97 void
98 set_lines_and_columns (lines, cols)
99      int lines, cols;
100 {
101   char *b;
102
103 #if defined (HAVE_PUTENV)
104   b = xmalloc (24);
105   sprintf (b, "LINES=%d", lines);
106   putenv (b);
107   b = xmalloc (24);
108   sprintf (b, "COLUMNS=%d", cols);
109   putenv (b);
110 #else /* !HAVE_PUTENV */
111 #  if defined (HAVE_SETENV)
112   b = xmalloc (8);
113   sprintf (b, "%d", lines);
114   setenv ("LINES", b, 1);
115   b = xmalloc (8);
116   sprintf (b, "%d", cols);
117   setenv ("COLUMNS", b, 1);
118 #  endif /* HAVE_SETENV */
119 #endif /* !HAVE_PUTENV */
120 }
121
122 char *
123 get_env_value (varname)
124      char *varname;
125 {
126   return ((char *)getenv (varname));
127 }
128
129 #else /* SHELL */
130 extern char *get_string_value ();
131
132 char *
133 get_env_value (varname)
134      char *varname;
135 {
136   return get_string_value (varname);
137 }       
138 #endif /* SHELL */