xref: /dflybsd-src/contrib/gdb-7/readline/shell.c (revision 16003dcfd2baa152f5dd24794ec9f36e139eaeb8)
1*6b445a62SJohn Marino /* shell.c -- readline utility functions that are normally provided by
2*6b445a62SJohn Marino 	      bash when readline is linked as part of the shell. */
3*6b445a62SJohn Marino 
4*6b445a62SJohn Marino /* Copyright (C) 1997-2009 Free Software Foundation, Inc.
5*6b445a62SJohn Marino 
6*6b445a62SJohn Marino    This file is part of the GNU Readline Library (Readline), a library
7*6b445a62SJohn Marino    for reading lines of text with interactive input and history editing.
8*6b445a62SJohn Marino 
9*6b445a62SJohn Marino    Readline is free software: you can redistribute it and/or modify
10*6b445a62SJohn Marino    it under the terms of the GNU General Public License as published by
11*6b445a62SJohn Marino    the Free Software Foundation, either version 3 of the License, or
12*6b445a62SJohn Marino    (at your option) any later version.
13*6b445a62SJohn Marino 
14*6b445a62SJohn Marino    Readline is distributed in the hope that it will be useful,
15*6b445a62SJohn Marino    but WITHOUT ANY WARRANTY; without even the implied warranty of
16*6b445a62SJohn Marino    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17*6b445a62SJohn Marino    GNU General Public License for more details.
18*6b445a62SJohn Marino 
19*6b445a62SJohn Marino    You should have received a copy of the GNU General Public License
20*6b445a62SJohn Marino    along with Readline.  If not, see <http://www.gnu.org/licenses/>.
21*6b445a62SJohn Marino */
22*6b445a62SJohn Marino 
23*6b445a62SJohn Marino #define READLINE_LIBRARY
24*6b445a62SJohn Marino 
25*6b445a62SJohn Marino #if defined (HAVE_CONFIG_H)
26*6b445a62SJohn Marino #  include <config.h>
27*6b445a62SJohn Marino #endif
28*6b445a62SJohn Marino 
29*6b445a62SJohn Marino #include <sys/types.h>
30*6b445a62SJohn Marino 
31*6b445a62SJohn Marino #if defined (HAVE_UNISTD_H)
32*6b445a62SJohn Marino #  include <unistd.h>
33*6b445a62SJohn Marino #endif /* HAVE_UNISTD_H */
34*6b445a62SJohn Marino 
35*6b445a62SJohn Marino #if defined (HAVE_STDLIB_H)
36*6b445a62SJohn Marino #  include <stdlib.h>
37*6b445a62SJohn Marino #else
38*6b445a62SJohn Marino #  include "ansi_stdlib.h"
39*6b445a62SJohn Marino #endif /* HAVE_STDLIB_H */
40*6b445a62SJohn Marino 
41*6b445a62SJohn Marino #if defined (HAVE_STRING_H)
42*6b445a62SJohn Marino #  include <string.h>
43*6b445a62SJohn Marino #else
44*6b445a62SJohn Marino #  include <strings.h>
45*6b445a62SJohn Marino #endif /* !HAVE_STRING_H */
46*6b445a62SJohn Marino 
47*6b445a62SJohn Marino #if defined (HAVE_LIMITS_H)
48*6b445a62SJohn Marino #  include <limits.h>
49*6b445a62SJohn Marino #endif
50*6b445a62SJohn Marino 
51*6b445a62SJohn Marino #if defined (HAVE_FCNTL_H)
52*6b445a62SJohn Marino #include <fcntl.h>
53*6b445a62SJohn Marino #endif
54*6b445a62SJohn Marino #if defined (HAVE_PWD_H)
55*6b445a62SJohn Marino #include <pwd.h>
56*6b445a62SJohn Marino #endif
57*6b445a62SJohn Marino 
58*6b445a62SJohn Marino #include <stdio.h>
59*6b445a62SJohn Marino 
60*6b445a62SJohn Marino #include "rlstdc.h"
61*6b445a62SJohn Marino #include "rlshell.h"
62*6b445a62SJohn Marino #include "xmalloc.h"
63*6b445a62SJohn Marino 
64*6b445a62SJohn Marino #if defined (HAVE_GETPWUID) && !defined (HAVE_GETPW_DECLS)
65*6b445a62SJohn Marino extern struct passwd *getpwuid PARAMS((uid_t));
66*6b445a62SJohn Marino #endif /* HAVE_GETPWUID && !HAVE_GETPW_DECLS */
67*6b445a62SJohn Marino 
68*6b445a62SJohn Marino #ifndef NULL
69*6b445a62SJohn Marino #  define NULL 0
70*6b445a62SJohn Marino #endif
71*6b445a62SJohn Marino 
72*6b445a62SJohn Marino #ifndef CHAR_BIT
73*6b445a62SJohn Marino #  define CHAR_BIT 8
74*6b445a62SJohn Marino #endif
75*6b445a62SJohn Marino 
76*6b445a62SJohn Marino /* Nonzero if the integer type T is signed.  */
77*6b445a62SJohn Marino #define TYPE_SIGNED(t) (! ((t) 0 < (t) -1))
78*6b445a62SJohn Marino 
79*6b445a62SJohn Marino /* Bound on length of the string representing an integer value of type T.
80*6b445a62SJohn Marino    Subtract one for the sign bit if T is signed;
81*6b445a62SJohn Marino    302 / 1000 is log10 (2) rounded up;
82*6b445a62SJohn Marino    add one for integer division truncation;
83*6b445a62SJohn Marino    add one more for a minus sign if t is signed.  */
84*6b445a62SJohn Marino #define INT_STRLEN_BOUND(t) \
85*6b445a62SJohn Marino   ((sizeof (t) * CHAR_BIT - TYPE_SIGNED (t)) * 302 / 1000 \
86*6b445a62SJohn Marino    + 1 + TYPE_SIGNED (t))
87*6b445a62SJohn Marino 
88*6b445a62SJohn Marino /* All of these functions are resolved from bash if we are linking readline
89*6b445a62SJohn Marino    as part of bash. */
90*6b445a62SJohn Marino 
91*6b445a62SJohn Marino /* Does shell-like quoting using single quotes. */
92*6b445a62SJohn Marino char *
sh_single_quote(string)93*6b445a62SJohn Marino sh_single_quote (string)
94*6b445a62SJohn Marino      char *string;
95*6b445a62SJohn Marino {
96*6b445a62SJohn Marino   register int c;
97*6b445a62SJohn Marino   char *result, *r, *s;
98*6b445a62SJohn Marino 
99*6b445a62SJohn Marino   result = (char *)xmalloc (3 + (4 * strlen (string)));
100*6b445a62SJohn Marino   r = result;
101*6b445a62SJohn Marino   *r++ = '\'';
102*6b445a62SJohn Marino 
103*6b445a62SJohn Marino   for (s = string; s && (c = *s); s++)
104*6b445a62SJohn Marino     {
105*6b445a62SJohn Marino       *r++ = c;
106*6b445a62SJohn Marino 
107*6b445a62SJohn Marino       if (c == '\'')
108*6b445a62SJohn Marino 	{
109*6b445a62SJohn Marino 	  *r++ = '\\';	/* insert escaped single quote */
110*6b445a62SJohn Marino 	  *r++ = '\'';
111*6b445a62SJohn Marino 	  *r++ = '\'';	/* start new quoted string */
112*6b445a62SJohn Marino 	}
113*6b445a62SJohn Marino     }
114*6b445a62SJohn Marino 
115*6b445a62SJohn Marino   *r++ = '\'';
116*6b445a62SJohn Marino   *r = '\0';
117*6b445a62SJohn Marino 
118*6b445a62SJohn Marino   return (result);
119*6b445a62SJohn Marino }
120*6b445a62SJohn Marino 
121*6b445a62SJohn Marino /* Set the environment variables LINES and COLUMNS to lines and cols,
122*6b445a62SJohn Marino    respectively. */
123*6b445a62SJohn Marino void
sh_set_lines_and_columns(lines,cols)124*6b445a62SJohn Marino sh_set_lines_and_columns (lines, cols)
125*6b445a62SJohn Marino      int lines, cols;
126*6b445a62SJohn Marino {
127*6b445a62SJohn Marino   char *b;
128*6b445a62SJohn Marino 
129*6b445a62SJohn Marino #if defined (HAVE_SETENV)
130*6b445a62SJohn Marino   b = (char *)xmalloc (INT_STRLEN_BOUND (int) + 1);
131*6b445a62SJohn Marino   sprintf (b, "%d", lines);
132*6b445a62SJohn Marino   setenv ("LINES", b, 1);
133*6b445a62SJohn Marino   xfree (b);
134*6b445a62SJohn Marino 
135*6b445a62SJohn Marino   b = (char *)xmalloc (INT_STRLEN_BOUND (int) + 1);
136*6b445a62SJohn Marino   sprintf (b, "%d", cols);
137*6b445a62SJohn Marino   setenv ("COLUMNS", b, 1);
138*6b445a62SJohn Marino   xfree (b);
139*6b445a62SJohn Marino #else /* !HAVE_SETENV */
140*6b445a62SJohn Marino #  if defined (HAVE_PUTENV)
141*6b445a62SJohn Marino   b = (char *)xmalloc (INT_STRLEN_BOUND (int) + sizeof ("LINES=") + 1);
142*6b445a62SJohn Marino   sprintf (b, "LINES=%d", lines);
143*6b445a62SJohn Marino   putenv (b);
144*6b445a62SJohn Marino 
145*6b445a62SJohn Marino   b = (char *)xmalloc (INT_STRLEN_BOUND (int) + sizeof ("COLUMNS=") + 1);
146*6b445a62SJohn Marino   sprintf (b, "COLUMNS=%d", cols);
147*6b445a62SJohn Marino   putenv (b);
148*6b445a62SJohn Marino #  endif /* HAVE_PUTENV */
149*6b445a62SJohn Marino #endif /* !HAVE_SETENV */
150*6b445a62SJohn Marino }
151*6b445a62SJohn Marino 
152*6b445a62SJohn Marino char *
sh_get_env_value(varname)153*6b445a62SJohn Marino sh_get_env_value (varname)
154*6b445a62SJohn Marino      const char *varname;
155*6b445a62SJohn Marino {
156*6b445a62SJohn Marino   return ((char *)getenv (varname));
157*6b445a62SJohn Marino }
158*6b445a62SJohn Marino 
159*6b445a62SJohn Marino char *
sh_get_home_dir()160*6b445a62SJohn Marino sh_get_home_dir ()
161*6b445a62SJohn Marino {
162*6b445a62SJohn Marino   char *home_dir;
163*6b445a62SJohn Marino   struct passwd *entry;
164*6b445a62SJohn Marino 
165*6b445a62SJohn Marino   home_dir = (char *)NULL;
166*6b445a62SJohn Marino #if defined (HAVE_GETPWUID)
167*6b445a62SJohn Marino   entry = getpwuid (getuid ());
168*6b445a62SJohn Marino   if (entry)
169*6b445a62SJohn Marino     home_dir = entry->pw_dir;
170*6b445a62SJohn Marino #endif
171*6b445a62SJohn Marino   return (home_dir);
172*6b445a62SJohn Marino }
173*6b445a62SJohn Marino 
174*6b445a62SJohn Marino #if !defined (O_NDELAY)
175*6b445a62SJohn Marino #  if defined (FNDELAY)
176*6b445a62SJohn Marino #    define O_NDELAY FNDELAY
177*6b445a62SJohn Marino #  endif
178*6b445a62SJohn Marino #endif
179*6b445a62SJohn Marino 
180*6b445a62SJohn Marino int
sh_unset_nodelay_mode(fd)181*6b445a62SJohn Marino sh_unset_nodelay_mode (fd)
182*6b445a62SJohn Marino      int fd;
183*6b445a62SJohn Marino {
184*6b445a62SJohn Marino #if defined (HAVE_FCNTL)
185*6b445a62SJohn Marino   int flags, bflags;
186*6b445a62SJohn Marino 
187*6b445a62SJohn Marino   if ((flags = fcntl (fd, F_GETFL, 0)) < 0)
188*6b445a62SJohn Marino     return -1;
189*6b445a62SJohn Marino 
190*6b445a62SJohn Marino   bflags = 0;
191*6b445a62SJohn Marino 
192*6b445a62SJohn Marino #ifdef O_NONBLOCK
193*6b445a62SJohn Marino   bflags |= O_NONBLOCK;
194*6b445a62SJohn Marino #endif
195*6b445a62SJohn Marino 
196*6b445a62SJohn Marino #ifdef O_NDELAY
197*6b445a62SJohn Marino   bflags |= O_NDELAY;
198*6b445a62SJohn Marino #endif
199*6b445a62SJohn Marino 
200*6b445a62SJohn Marino   if (flags & bflags)
201*6b445a62SJohn Marino     {
202*6b445a62SJohn Marino       flags &= ~bflags;
203*6b445a62SJohn Marino       return (fcntl (fd, F_SETFL, flags));
204*6b445a62SJohn Marino     }
205*6b445a62SJohn Marino #endif
206*6b445a62SJohn Marino 
207*6b445a62SJohn Marino   return 0;
208*6b445a62SJohn Marino }
209