Commit | Line | Data |
---|---|---|
d60d9f65 SS |
1 | /* shell.c -- readline utility functions that are normally provided by |
2 | bash when readline is linked as part of the shell. */ | |
3 | ||
cb41b9e7 | 4 | /* Copyright (C) 1997-2009,2017 Free Software Foundation, Inc. |
d60d9f65 | 5 | |
cc88a640 JK |
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. | |
d60d9f65 | 8 | |
cc88a640 JK |
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 | |
d60d9f65 SS |
12 | (at your option) any later version. |
13 | ||
cc88a640 JK |
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 | |
d60d9f65 SS |
17 | GNU General Public License for more details. |
18 | ||
cc88a640 JK |
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/>. | |
21 | */ | |
22 | ||
d60d9f65 SS |
23 | #define READLINE_LIBRARY |
24 | ||
25 | #if defined (HAVE_CONFIG_H) | |
26 | # include <config.h> | |
27 | #endif | |
28 | ||
c862e87b JM |
29 | #include <sys/types.h> |
30 | ||
d60d9f65 | 31 | #if defined (HAVE_UNISTD_H) |
d60d9f65 SS |
32 | # include <unistd.h> |
33 | #endif /* HAVE_UNISTD_H */ | |
34 | ||
35 | #if defined (HAVE_STDLIB_H) | |
36 | # include <stdlib.h> | |
37 | #else | |
38 | # include "ansi_stdlib.h" | |
39 | #endif /* HAVE_STDLIB_H */ | |
40 | ||
41 | #if defined (HAVE_STRING_H) | |
42 | # include <string.h> | |
43 | #else | |
44 | # include <strings.h> | |
45 | #endif /* !HAVE_STRING_H */ | |
46 | ||
9255ee31 EZ |
47 | #if defined (HAVE_LIMITS_H) |
48 | # include <limits.h> | |
49 | #endif | |
50 | ||
5bdf8622 | 51 | #if defined (HAVE_FCNTL_H) |
1b17e766 | 52 | #include <fcntl.h> |
5bdf8622 DJ |
53 | #endif |
54 | #if defined (HAVE_PWD_H) | |
c862e87b | 55 | #include <pwd.h> |
430b7832 | 56 | #endif |
d60d9f65 | 57 | |
1b17e766 EZ |
58 | #include <stdio.h> |
59 | ||
9255ee31 | 60 | #include "rlstdc.h" |
1b17e766 | 61 | #include "rlshell.h" |
775e241e TT |
62 | #include "rldefs.h" |
63 | ||
1b17e766 EZ |
64 | #include "xmalloc.h" |
65 | ||
5bdf8622 | 66 | #if defined (HAVE_GETPWUID) && !defined (HAVE_GETPW_DECLS) |
9255ee31 | 67 | extern struct passwd *getpwuid PARAMS((uid_t)); |
5bdf8622 | 68 | #endif /* HAVE_GETPWUID && !HAVE_GETPW_DECLS */ |
d60d9f65 | 69 | |
1b17e766 EZ |
70 | #ifndef NULL |
71 | # define NULL 0 | |
72 | #endif | |
d60d9f65 | 73 | |
9255ee31 EZ |
74 | #ifndef CHAR_BIT |
75 | # define CHAR_BIT 8 | |
76 | #endif | |
77 | ||
78 | /* Nonzero if the integer type T is signed. */ | |
79 | #define TYPE_SIGNED(t) (! ((t) 0 < (t) -1)) | |
80 | ||
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)) | |
89 | ||
c862e87b JM |
90 | /* All of these functions are resolved from bash if we are linking readline |
91 | as part of bash. */ | |
d60d9f65 SS |
92 | |
93 | /* Does shell-like quoting using single quotes. */ | |
94 | char * | |
cb41b9e7 | 95 | sh_single_quote (char *string) |
d60d9f65 SS |
96 | { |
97 | register int c; | |
98 | char *result, *r, *s; | |
99 | ||
1b17e766 | 100 | result = (char *)xmalloc (3 + (4 * strlen (string))); |
d60d9f65 SS |
101 | r = result; |
102 | *r++ = '\''; | |
103 | ||
104 | for (s = string; s && (c = *s); s++) | |
105 | { | |
106 | *r++ = c; | |
107 | ||
108 | if (c == '\'') | |
109 | { | |
110 | *r++ = '\\'; /* insert escaped single quote */ | |
111 | *r++ = '\''; | |
112 | *r++ = '\''; /* start new quoted string */ | |
113 | } | |
114 | } | |
115 | ||
116 | *r++ = '\''; | |
117 | *r = '\0'; | |
118 | ||
119 | return (result); | |
120 | } | |
121 | ||
122 | /* Set the environment variables LINES and COLUMNS to lines and cols, | |
123 | respectively. */ | |
775e241e TT |
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 */ | |
127 | ||
d60d9f65 | 128 | void |
cb41b9e7 | 129 | sh_set_lines_and_columns (int lines, int cols) |
d60d9f65 | 130 | { |
5836a818 | 131 | #if defined (HAVE_SETENV) |
775e241e TT |
132 | sprintf (setenv_buf, "%d", lines); |
133 | setenv ("LINES", setenv_buf, 1); | |
134 | ||
135 | sprintf (setenv_buf, "%d", cols); | |
136 | setenv ("COLUMNS", setenv_buf, 1); | |
5bdf8622 DJ |
137 | #else /* !HAVE_SETENV */ |
138 | # if defined (HAVE_PUTENV) | |
775e241e TT |
139 | sprintf (putenv_buf1, "LINES=%d", lines); |
140 | putenv (putenv_buf1); | |
5bdf8622 | 141 | |
775e241e TT |
142 | sprintf (putenv_buf2, "COLUMNS=%d", cols); |
143 | putenv (putenv_buf2); | |
5bdf8622 DJ |
144 | # endif /* HAVE_PUTENV */ |
145 | #endif /* !HAVE_SETENV */ | |
d60d9f65 SS |
146 | } |
147 | ||
148 | char * | |
cb41b9e7 | 149 | sh_get_env_value (const char *varname) |
d60d9f65 SS |
150 | { |
151 | return ((char *)getenv (varname)); | |
152 | } | |
153 | ||
d60d9f65 | 154 | char * |
cb41b9e7 | 155 | sh_get_home_dir (void) |
d60d9f65 | 156 | { |
775e241e | 157 | static char *home_dir = (char *)NULL; |
c862e87b JM |
158 | struct passwd *entry; |
159 | ||
775e241e TT |
160 | if (home_dir) |
161 | return (home_dir); | |
162 | ||
c862e87b | 163 | home_dir = (char *)NULL; |
5bdf8622 | 164 | #if defined (HAVE_GETPWUID) |
775e241e TT |
165 | # if defined (__TANDEM) |
166 | entry = getpwnam (getlogin ()); | |
167 | # else | |
c862e87b | 168 | entry = getpwuid (getuid ()); |
775e241e | 169 | # endif |
c862e87b | 170 | if (entry) |
775e241e TT |
171 | home_dir = savestring (entry->pw_dir); |
172 | #endif | |
173 | ||
174 | #if defined (HAVE_GETPWENT) | |
175 | endpwent (); /* some systems need this */ | |
430b7832 | 176 | #endif |
775e241e | 177 | |
c862e87b JM |
178 | return (home_dir); |
179 | } | |
1b17e766 EZ |
180 | |
181 | #if !defined (O_NDELAY) | |
182 | # if defined (FNDELAY) | |
183 | # define O_NDELAY FNDELAY | |
184 | # endif | |
185 | #endif | |
186 | ||
187 | int | |
cb41b9e7 | 188 | sh_unset_nodelay_mode (int fd) |
1b17e766 | 189 | { |
5bdf8622 | 190 | #if defined (HAVE_FCNTL) |
1b17e766 EZ |
191 | int flags, bflags; |
192 | ||
193 | if ((flags = fcntl (fd, F_GETFL, 0)) < 0) | |
194 | return -1; | |
195 | ||
196 | bflags = 0; | |
197 | ||
198 | #ifdef O_NONBLOCK | |
199 | bflags |= O_NONBLOCK; | |
200 | #endif | |
201 | ||
202 | #ifdef O_NDELAY | |
203 | bflags |= O_NDELAY; | |
204 | #endif | |
205 | ||
206 | if (flags & bflags) | |
207 | { | |
208 | flags &= ~bflags; | |
209 | return (fcntl (fd, F_SETFL, flags)); | |
210 | } | |
430b7832 | 211 | #endif |
1b17e766 EZ |
212 | |
213 | return 0; | |
214 | } |