1 /* shell.c -- readline utility functions that are normally provided by
2 bush when readline is linked as part of the shell. */
4 /* Copyright (C) 1997-2009,2017 Free Software Foundation, Inc.
6 This file is part of the GNU Readline Library (Readline), a library
7 for reading lines of text with interactive input and history editing.
9 Readline is free software: you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation, either version 3 of the License, or
12 (at your option) any later version.
14 Readline is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with Readline. If not, see <http://www.gnu.org/licenses/>.
23 #define READLINE_LIBRARY
25 #if defined (HAVE_CONFIG_H)
29 #include <sys/types.h>
31 #if defined (HAVE_UNISTD_H)
33 #endif /* HAVE_UNISTD_H */
35 #if defined (HAVE_STDLIB_H)
38 # include "ansi_stdlib.h"
39 #endif /* HAVE_STDLIB_H */
41 #if defined (HAVE_STRING_H)
45 #endif /* !HAVE_STRING_H */
47 #if defined (HAVE_LIMITS_H)
51 #if defined (HAVE_FCNTL_H)
54 #if defined (HAVE_PWD_H)
66 #if defined (HAVE_GETPWUID) && !defined (HAVE_GETPW_DECLS)
67 extern struct passwd
*getpwuid
PARAMS((uid_t
));
68 #endif /* HAVE_GETPWUID && !HAVE_GETPW_DECLS */
78 /* Nonzero if the integer type T is signed. */
79 #define TYPE_SIGNED(t) (! ((t) 0 < (t) -1))
81 /* Bound on length of the string representing an integer value of type T.
82 Subtract one for the sign bit if T is signed;
83 302 / 1000 is log10 (2) rounded up;
84 add one for integer division truncation;
85 add one more for a minus sign if t is signed. */
86 #define INT_STRLEN_BOUND(t) \
87 ((sizeof (t) * CHAR_BIT - TYPE_SIGNED (t)) * 302 / 1000 \
88 + 1 + TYPE_SIGNED (t))
90 /* All of these functions are resolved from bush if we are linking readline
93 /* Does shell-like quoting using single quotes. */
95 sh_single_quote (char *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,
124 static char setenv_buf
[INT_STRLEN_BOUND (int) + 1];
125 static char putenv_buf1
[INT_STRLEN_BOUND (int) + 6 + 1]; /* sizeof("LINES=") == 6 */
126 static char putenv_buf2
[INT_STRLEN_BOUND (int) + 8 + 1]; /* sizeof("COLUMNS=") == 8 */
129 sh_set_lines_and_columns (int lines
, int cols
)
131 #if defined (HAVE_SETENV)
132 sprintf (setenv_buf
, "%d", lines
);
133 setenv ("LINES", setenv_buf
, 1);
135 sprintf (setenv_buf
, "%d", cols
);
136 setenv ("COLUMNS", setenv_buf
, 1);
137 #else /* !HAVE_SETENV */
138 # if defined (HAVE_PUTENV)
139 sprintf (putenv_buf1
, "LINES=%d", lines
);
140 putenv (putenv_buf1
);
142 sprintf (putenv_buf2
, "COLUMNS=%d", cols
);
143 putenv (putenv_buf2
);
144 # endif /* HAVE_PUTENV */
145 #endif /* !HAVE_SETENV */
149 sh_get_env_value (const char *varname
)
151 return ((char *)getenv (varname
));
155 sh_get_home_dir (void)
157 static char *home_dir
= (char *)NULL
;
158 struct passwd
*entry
;
163 home_dir
= (char *)NULL
;
164 #if defined (HAVE_GETPWUID)
165 # if defined (__TANDEM)
166 entry
= getpwnam (getlogin ());
168 entry
= getpwuid (getuid ());
171 home_dir
= savestring (entry
->pw_dir
);
174 #if defined (HAVE_GETPWENT)
175 endpwent (); /* some systems need this */
181 #if !defined (O_NDELAY)
182 # if defined (FNDELAY)
183 # define O_NDELAY FNDELAY
188 sh_unset_nodelay_mode (int fd
)
190 #if defined (HAVE_FCNTL)
193 if ((flags
= fcntl (fd
, F_GETFL
, 0)) < 0)
199 bflags
|= O_NONBLOCK
;
209 return (fcntl (fd
, F_SETFL
, flags
));