2 /* shell.c -- readline utility functions that are normally provided by
3 bash when readline is linked as part of the shell. */
5 /* Copyright (C) 1997 Free Software Foundation, Inc.
7 This file is part of the GNU Readline Library, a library for
8 reading lines of text with interactive input and history editing.
10 The GNU Readline Library is free software; you can redistribute it
11 and/or modify it under the terms of the GNU General Public License
12 as published by the Free Software Foundation; either version 2, or
13 (at your option) any later version.
15 The GNU Readline Library is distributed in the hope that it will be
16 useful, but WITHOUT ANY WARRANTY; without even the implied warranty
17 of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 The GNU General Public License is often shipped with GNU software, and
21 is generally kept in a file called COPYING or LICENSE. If you do not
22 have a copy of the license, write to the Free Software Foundation,
23 59 Temple Place, Suite 330, Boston, MA 02111 USA. */
24 #define READLINE_LIBRARY
26 #if defined (HAVE_CONFIG_H)
30 #include <sys/types.h>
32 #if defined (HAVE_UNISTD_H)
34 #endif /* HAVE_UNISTD_H */
36 #if defined (HAVE_STDLIB_H)
39 # include "ansi_stdlib.h"
40 #endif /* HAVE_STDLIB_H */
42 #if defined (HAVE_STRING_H)
46 #endif /* !HAVE_STRING_H */
48 #if defined (HAVE_LIMITS_H)
52 #if defined (HAVE_FCNTL_H)
55 #if defined (HAVE_PWD_H)
65 #if defined (HAVE_GETPWUID) && !defined (HAVE_GETPW_DECLS)
66 extern struct passwd *getpwuid PARAMS((uid_t));
67 #endif /* HAVE_GETPWUID && !HAVE_GETPW_DECLS */
77 /* Nonzero if the integer type T is signed. */
78 #define TYPE_SIGNED(t) (! ((t) 0 < (t) -1))
80 /* Bound on length of the string representing an integer value of type T.
81 Subtract one for the sign bit if T is signed;
82 302 / 1000 is log10 (2) rounded up;
83 add one for integer division truncation;
84 add one more for a minus sign if t is signed. */
85 #define INT_STRLEN_BOUND(t) \
86 ((sizeof (t) * CHAR_BIT - TYPE_SIGNED (t)) * 302 / 1000 \
87 + 1 + TYPE_SIGNED (t))
89 /* All of these functions are resolved from bash if we are linking readline
92 /* Does shell-like quoting using single quotes. */
94 sh_single_quote (string)
100 result = (char *)xmalloc (3 + (4 * strlen (string)));
104 for (s = string; s && (c = *s); s++)
110 *r++ = '\\'; /* insert escaped single quote */
112 *r++ = '\''; /* start new quoted string */
122 /* Set the environment variables LINES and COLUMNS to lines and cols,
125 sh_set_lines_and_columns (lines, cols)
130 #if defined (HAVE_SETENV)
131 b = (char *)xmalloc (INT_STRLEN_BOUND (int) + 1);
132 sprintf (b, "%d", lines);
133 setenv ("LINES", b, 1);
136 b = (char *)xmalloc (INT_STRLEN_BOUND (int) + 1);
137 sprintf (b, "%d", cols);
138 setenv ("COLUMNS", b, 1);
140 #else /* !HAVE_SETENV */
141 # if defined (HAVE_PUTENV)
142 b = (char *)xmalloc (INT_STRLEN_BOUND (int) + sizeof ("LINES=") + 1);
143 sprintf (b, "LINES=%d", lines);
146 b = (char *)xmalloc (INT_STRLEN_BOUND (int) + sizeof ("COLUMNS=") + 1);
147 sprintf (b, "COLUMNS=%d", cols);
149 # endif /* HAVE_PUTENV */
150 #endif /* !HAVE_SETENV */
154 sh_get_env_value (varname)
157 return ((char *)getenv (varname));
164 struct passwd *entry;
166 home_dir = (char *)NULL;
167 #if defined (HAVE_GETPWUID)
168 entry = getpwuid (getuid ());
170 home_dir = entry->pw_dir;
175 #if !defined (O_NDELAY)
176 # if defined (FNDELAY)
177 # define O_NDELAY FNDELAY
182 sh_unset_nodelay_mode (fd)
185 #if defined (HAVE_FCNTL)
188 if ((flags = fcntl (fd, F_GETFL, 0)) < 0)
194 bflags |= O_NONBLOCK;
204 return (fcntl (fd, F_SETFL, flags));