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 | ||
cc88a640 | 4 | /* Copyright (C) 1997-2009 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 EZ |
61 | #include "rlshell.h" |
62 | #include "xmalloc.h" | |
63 | ||
5bdf8622 | 64 | #if defined (HAVE_GETPWUID) && !defined (HAVE_GETPW_DECLS) |
9255ee31 | 65 | extern struct passwd *getpwuid PARAMS((uid_t)); |
5bdf8622 | 66 | #endif /* HAVE_GETPWUID && !HAVE_GETPW_DECLS */ |
d60d9f65 | 67 | |
1b17e766 EZ |
68 | #ifndef NULL |
69 | # define NULL 0 | |
70 | #endif | |
d60d9f65 | 71 | |
9255ee31 EZ |
72 | #ifndef CHAR_BIT |
73 | # define CHAR_BIT 8 | |
74 | #endif | |
75 | ||
76 | /* Nonzero if the integer type T is signed. */ | |
77 | #define TYPE_SIGNED(t) (! ((t) 0 < (t) -1)) | |
78 | ||
79 | /* Bound on length of the string representing an integer value of type T. | |
80 | Subtract one for the sign bit if T is signed; | |
81 | 302 / 1000 is log10 (2) rounded up; | |
82 | add one for integer division truncation; | |
83 | add one more for a minus sign if t is signed. */ | |
84 | #define INT_STRLEN_BOUND(t) \ | |
85 | ((sizeof (t) * CHAR_BIT - TYPE_SIGNED (t)) * 302 / 1000 \ | |
86 | + 1 + TYPE_SIGNED (t)) | |
87 | ||
c862e87b JM |
88 | /* All of these functions are resolved from bash if we are linking readline |
89 | as part of bash. */ | |
d60d9f65 SS |
90 | |
91 | /* Does shell-like quoting using single quotes. */ | |
92 | char * | |
9255ee31 | 93 | sh_single_quote (string) |
d60d9f65 SS |
94 | char *string; |
95 | { | |
96 | register int c; | |
97 | char *result, *r, *s; | |
98 | ||
1b17e766 | 99 | result = (char *)xmalloc (3 + (4 * strlen (string))); |
d60d9f65 SS |
100 | r = result; |
101 | *r++ = '\''; | |
102 | ||
103 | for (s = string; s && (c = *s); s++) | |
104 | { | |
105 | *r++ = c; | |
106 | ||
107 | if (c == '\'') | |
108 | { | |
109 | *r++ = '\\'; /* insert escaped single quote */ | |
110 | *r++ = '\''; | |
111 | *r++ = '\''; /* start new quoted string */ | |
112 | } | |
113 | } | |
114 | ||
115 | *r++ = '\''; | |
116 | *r = '\0'; | |
117 | ||
118 | return (result); | |
119 | } | |
120 | ||
121 | /* Set the environment variables LINES and COLUMNS to lines and cols, | |
122 | respectively. */ | |
123 | void | |
9255ee31 | 124 | sh_set_lines_and_columns (lines, cols) |
d60d9f65 SS |
125 | int lines, cols; |
126 | { | |
5836a818 | 127 | char *b; |
4a11f206 | 128 | |
5836a818 PP |
129 | #if defined (HAVE_SETENV) |
130 | b = (char *)xmalloc (INT_STRLEN_BOUND (int) + 1); | |
131 | sprintf (b, "%d", lines); | |
132 | setenv ("LINES", b, 1); | |
133 | xfree (b); | |
134 | ||
135 | b = (char *)xmalloc (INT_STRLEN_BOUND (int) + 1); | |
136 | sprintf (b, "%d", cols); | |
137 | setenv ("COLUMNS", b, 1); | |
138 | xfree (b); | |
5bdf8622 DJ |
139 | #else /* !HAVE_SETENV */ |
140 | # if defined (HAVE_PUTENV) | |
5836a818 PP |
141 | b = (char *)xmalloc (INT_STRLEN_BOUND (int) + sizeof ("LINES=") + 1); |
142 | sprintf (b, "LINES=%d", lines); | |
143 | putenv (b); | |
5bdf8622 | 144 | |
5836a818 PP |
145 | b = (char *)xmalloc (INT_STRLEN_BOUND (int) + sizeof ("COLUMNS=") + 1); |
146 | sprintf (b, "COLUMNS=%d", cols); | |
147 | putenv (b); | |
5bdf8622 DJ |
148 | # endif /* HAVE_PUTENV */ |
149 | #endif /* !HAVE_SETENV */ | |
d60d9f65 SS |
150 | } |
151 | ||
152 | char * | |
9255ee31 EZ |
153 | sh_get_env_value (varname) |
154 | const char *varname; | |
d60d9f65 SS |
155 | { |
156 | return ((char *)getenv (varname)); | |
157 | } | |
158 | ||
d60d9f65 | 159 | char * |
9255ee31 | 160 | sh_get_home_dir () |
d60d9f65 | 161 | { |
5836a818 | 162 | char *home_dir; |
c862e87b JM |
163 | struct passwd *entry; |
164 | ||
165 | home_dir = (char *)NULL; | |
5bdf8622 | 166 | #if defined (HAVE_GETPWUID) |
c862e87b JM |
167 | entry = getpwuid (getuid ()); |
168 | if (entry) | |
5836a818 | 169 | home_dir = entry->pw_dir; |
430b7832 | 170 | #endif |
c862e87b JM |
171 | return (home_dir); |
172 | } | |
1b17e766 EZ |
173 | |
174 | #if !defined (O_NDELAY) | |
175 | # if defined (FNDELAY) | |
176 | # define O_NDELAY FNDELAY | |
177 | # endif | |
178 | #endif | |
179 | ||
180 | int | |
9255ee31 | 181 | sh_unset_nodelay_mode (fd) |
1b17e766 EZ |
182 | int fd; |
183 | { | |
5bdf8622 | 184 | #if defined (HAVE_FCNTL) |
1b17e766 EZ |
185 | int flags, bflags; |
186 | ||
187 | if ((flags = fcntl (fd, F_GETFL, 0)) < 0) | |
188 | return -1; | |
189 | ||
190 | bflags = 0; | |
191 | ||
192 | #ifdef O_NONBLOCK | |
193 | bflags |= O_NONBLOCK; | |
194 | #endif | |
195 | ||
196 | #ifdef O_NDELAY | |
197 | bflags |= O_NDELAY; | |
198 | #endif | |
199 | ||
200 | if (flags & bflags) | |
201 | { | |
202 | flags &= ~bflags; | |
203 | return (fcntl (fd, F_SETFL, flags)); | |
204 | } | |
430b7832 | 205 | #endif |
1b17e766 EZ |
206 | |
207 | return 0; | |
208 | } |