[__GO32__]: Don't undef HANDLE_SIGNALS if __DJGPP__ is defined.
[external/binutils.git] / 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 #include <sys/types.h>
30 #include <stdio.h>
31
32 #if defined (HAVE_UNISTD_H)
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 #include <pwd.h>
49
50 #if !defined (HAVE_GETPW_DECLS)
51 extern struct passwd *getpwuid ();
52 #endif /* !HAVE_GETPW_DECLS */
53
54 extern char *xmalloc ();
55
56 /* All of these functions are resolved from bash if we are linking readline
57    as part of bash. */
58
59 /* Does shell-like quoting using single quotes. */
60 char *
61 single_quote (string)
62      char *string;
63 {
64   register int c;
65   char *result, *r, *s;
66
67   result = (char *)xmalloc (3 + (3 * strlen (string)));
68   r = result;
69   *r++ = '\'';
70
71   for (s = string; s && (c = *s); s++)
72     {
73       *r++ = c;
74
75       if (c == '\'')
76         {
77           *r++ = '\\';  /* insert escaped single quote */
78           *r++ = '\'';
79           *r++ = '\'';  /* start new quoted string */
80         }
81     }
82
83   *r++ = '\'';
84   *r = '\0';
85
86   return (result);
87 }
88
89 /* Set the environment variables LINES and COLUMNS to lines and cols,
90    respectively. */
91 void
92 set_lines_and_columns (lines, cols)
93      int lines, cols;
94 {
95   char *b;
96
97 #if defined (HAVE_PUTENV)
98   b = xmalloc (24);
99   sprintf (b, "LINES=%d", lines);
100   putenv (b);
101   b = xmalloc (24);
102   sprintf (b, "COLUMNS=%d", cols);
103   putenv (b);
104 #else /* !HAVE_PUTENV */
105 #  if defined (HAVE_SETENV)
106   b = xmalloc (8);
107   sprintf (b, "%d", lines);
108   setenv ("LINES", b, 1);
109   b = xmalloc (8);
110   sprintf (b, "%d", cols);
111   setenv ("COLUMNS", b, 1);
112 #  endif /* HAVE_SETENV */
113 #endif /* !HAVE_PUTENV */
114 }
115
116 char *
117 get_env_value (varname)
118      char *varname;
119 {
120   return ((char *)getenv (varname));
121 }
122
123 char *
124 get_home_dir ()
125 {
126   char *home_dir;
127   struct passwd *entry;
128
129   home_dir = (char *)NULL;
130   entry = getpwuid (getuid ());
131   if (entry)
132     home_dir = entry->pw_dir;
133   return (home_dir);
134 }