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 | ||
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 | |
f9267e15 | 11 | as published by the Free Software Foundation; either version 2, or |
d60d9f65 SS |
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, | |
f9267e15 | 22 | 59 Temple Place, Suite 330, Boston, MA 02111 USA. */ |
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 | ||
f9267e15 | 47 | #include <fcntl.h> |
c862e87b | 48 | #include <pwd.h> |
d60d9f65 | 49 | |
f9267e15 EZ |
50 | #include <stdio.h> |
51 | ||
52 | #include "rlshell.h" | |
53 | #include "xmalloc.h" | |
54 | ||
c862e87b JM |
55 | #if !defined (HAVE_GETPW_DECLS) |
56 | extern struct passwd *getpwuid (); | |
57 | #endif /* !HAVE_GETPW_DECLS */ | |
d60d9f65 | 58 | |
f9267e15 EZ |
59 | #ifndef NULL |
60 | # define NULL 0 | |
61 | #endif | |
d60d9f65 | 62 | |
c862e87b JM |
63 | /* All of these functions are resolved from bash if we are linking readline |
64 | as part of bash. */ | |
d60d9f65 SS |
65 | |
66 | /* Does shell-like quoting using single quotes. */ | |
67 | char * | |
68 | single_quote (string) | |
69 | char *string; | |
70 | { | |
71 | register int c; | |
72 | char *result, *r, *s; | |
73 | ||
f9267e15 | 74 | result = (char *)xmalloc (3 + (4 * strlen (string))); |
d60d9f65 SS |
75 | r = result; |
76 | *r++ = '\''; | |
77 | ||
78 | for (s = string; s && (c = *s); s++) | |
79 | { | |
80 | *r++ = c; | |
81 | ||
82 | if (c == '\'') | |
83 | { | |
84 | *r++ = '\\'; /* insert escaped single quote */ | |
85 | *r++ = '\''; | |
86 | *r++ = '\''; /* start new quoted string */ | |
87 | } | |
88 | } | |
89 | ||
90 | *r++ = '\''; | |
91 | *r = '\0'; | |
92 | ||
93 | return (result); | |
94 | } | |
95 | ||
96 | /* Set the environment variables LINES and COLUMNS to lines and cols, | |
97 | respectively. */ | |
98 | void | |
99 | set_lines_and_columns (lines, cols) | |
100 | int lines, cols; | |
101 | { | |
102 | char *b; | |
103 | ||
104 | #if defined (HAVE_PUTENV) | |
105 | b = xmalloc (24); | |
106 | sprintf (b, "LINES=%d", lines); | |
107 | putenv (b); | |
108 | b = xmalloc (24); | |
109 | sprintf (b, "COLUMNS=%d", cols); | |
110 | putenv (b); | |
111 | #else /* !HAVE_PUTENV */ | |
112 | # if defined (HAVE_SETENV) | |
113 | b = xmalloc (8); | |
114 | sprintf (b, "%d", lines); | |
115 | setenv ("LINES", b, 1); | |
116 | b = xmalloc (8); | |
117 | sprintf (b, "%d", cols); | |
118 | setenv ("COLUMNS", b, 1); | |
119 | # endif /* HAVE_SETENV */ | |
120 | #endif /* !HAVE_PUTENV */ | |
121 | } | |
122 | ||
123 | char * | |
124 | get_env_value (varname) | |
125 | char *varname; | |
126 | { | |
127 | return ((char *)getenv (varname)); | |
128 | } | |
129 | ||
d60d9f65 | 130 | char * |
c862e87b | 131 | get_home_dir () |
d60d9f65 | 132 | { |
c862e87b JM |
133 | char *home_dir; |
134 | struct passwd *entry; | |
135 | ||
136 | home_dir = (char *)NULL; | |
137 | entry = getpwuid (getuid ()); | |
138 | if (entry) | |
139 | home_dir = entry->pw_dir; | |
140 | return (home_dir); | |
141 | } | |
f9267e15 EZ |
142 | |
143 | #if !defined (O_NDELAY) | |
144 | # if defined (FNDELAY) | |
145 | # define O_NDELAY FNDELAY | |
146 | # endif | |
147 | #endif | |
148 | ||
149 | int | |
150 | unset_nodelay_mode (fd) | |
151 | int fd; | |
152 | { | |
153 | int flags, bflags; | |
154 | ||
155 | if ((flags = fcntl (fd, F_GETFL, 0)) < 0) | |
156 | return -1; | |
157 | ||
158 | bflags = 0; | |
159 | ||
160 | #ifdef O_NONBLOCK | |
161 | bflags |= O_NONBLOCK; | |
162 | #endif | |
163 | ||
164 | #ifdef O_NDELAY | |
165 | bflags |= O_NDELAY; | |
166 | #endif | |
167 | ||
168 | if (flags & bflags) | |
169 | { | |
170 | flags &= ~bflags; | |
171 | return (fcntl (fd, F_SETFL, flags)); | |
172 | } | |
173 | ||
174 | return 0; | |
175 | } |