Remove tm.h and xm.h handling, as it wasn't used. Use nm.h only when needed.
[dragonfly.git] / contrib / readline-5.0 / shell.c
bloba07e2b96fc1707590e8a83d27a862c7b80f8db31
1 /* shell.c -- readline utility functions that are normally provided by
2 bash when readline is linked as part of the shell. */
4 /* Copyright (C) 1997 Free Software Foundation, Inc.
6 This file is part of the GNU Readline Library, a library for
7 reading lines of text with interactive input and history editing.
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
11 as published by the Free Software Foundation; either version 2, or
12 (at your option) any later version.
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.
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,
22 59 Temple Place, Suite 330, Boston, MA 02111 USA. */
23 #define READLINE_LIBRARY
25 #if defined (HAVE_CONFIG_H)
26 # include <config.h>
27 #endif
29 #include <sys/types.h>
31 #if defined (HAVE_UNISTD_H)
32 # include <unistd.h>
33 #endif /* HAVE_UNISTD_H */
35 #if defined (HAVE_STDLIB_H)
36 # include <stdlib.h>
37 #else
38 # include "ansi_stdlib.h"
39 #endif /* HAVE_STDLIB_H */
41 #if defined (HAVE_STRING_H)
42 # include <string.h>
43 #else
44 # include <strings.h>
45 #endif /* !HAVE_STRING_H */
47 #if defined (HAVE_LIMITS_H)
48 # include <limits.h>
49 #endif
51 #include <fcntl.h>
52 #include <pwd.h>
54 #include <stdio.h>
56 #include "rlstdc.h"
57 #include "rlshell.h"
58 #include "xmalloc.h"
60 #if !defined (HAVE_GETPW_DECLS)
61 extern struct passwd *getpwuid PARAMS((uid_t));
62 #endif /* !HAVE_GETPW_DECLS */
64 #ifndef NULL
65 # define NULL 0
66 #endif
68 #ifndef CHAR_BIT
69 # define CHAR_BIT 8
70 #endif
72 /* Nonzero if the integer type T is signed. */
73 #define TYPE_SIGNED(t) (! ((t) 0 < (t) -1))
75 /* Bound on length of the string representing an integer value of type T.
76 Subtract one for the sign bit if T is signed;
77 302 / 1000 is log10 (2) rounded up;
78 add one for integer division truncation;
79 add one more for a minus sign if t is signed. */
80 #define INT_STRLEN_BOUND(t) \
81 ((sizeof (t) * CHAR_BIT - TYPE_SIGNED (t)) * 302 / 1000 \
82 + 1 + TYPE_SIGNED (t))
84 /* All of these functions are resolved from bash if we are linking readline
85 as part of bash. */
87 /* Does shell-like quoting using single quotes. */
88 char *
89 sh_single_quote (string)
90 char *string;
92 register int c;
93 char *result, *r, *s;
95 result = (char *)xmalloc (3 + (4 * strlen (string)));
96 r = result;
97 *r++ = '\'';
99 for (s = string; s && (c = *s); s++)
101 *r++ = c;
103 if (c == '\'')
105 *r++ = '\\'; /* insert escaped single quote */
106 *r++ = '\'';
107 *r++ = '\''; /* start new quoted string */
111 *r++ = '\'';
112 *r = '\0';
114 return (result);
117 /* Set the environment variables LINES and COLUMNS to lines and cols,
118 respectively. */
119 void
120 sh_set_lines_and_columns (lines, cols)
121 int lines, cols;
123 char *b;
125 #if defined (HAVE_PUTENV)
126 b = (char *)xmalloc (INT_STRLEN_BOUND (int) + sizeof ("LINES=") + 1);
127 sprintf (b, "LINES=%d", lines);
128 putenv (b);
130 b = (char *)xmalloc (INT_STRLEN_BOUND (int) + sizeof ("COLUMNS=") + 1);
131 sprintf (b, "COLUMNS=%d", cols);
132 putenv (b);
133 #else /* !HAVE_PUTENV */
134 # if defined (HAVE_SETENV)
135 b = (char *)xmalloc (INT_STRLEN_BOUND (int) + 1);
136 sprintf (b, "%d", lines);
137 setenv ("LINES", b, 1);
138 free (b);
140 b = (char *)xmalloc (INT_STRLEN_BOUND (int) + 1);
141 sprintf (b, "%d", cols);
142 setenv ("COLUMNS", b, 1);
143 free (b);
144 # endif /* HAVE_SETENV */
145 #endif /* !HAVE_PUTENV */
148 char *
149 sh_get_env_value (varname)
150 const char *varname;
152 return ((char *)getenv (varname));
155 char *
156 sh_get_home_dir ()
158 char *home_dir;
159 struct passwd *entry;
161 home_dir = (char *)NULL;
162 entry = getpwuid (getuid ());
163 if (entry)
164 home_dir = entry->pw_dir;
165 return (home_dir);
168 #if !defined (O_NDELAY)
169 # if defined (FNDELAY)
170 # define O_NDELAY FNDELAY
171 # endif
172 #endif
175 sh_unset_nodelay_mode (fd)
176 int fd;
178 int flags, bflags;
180 if ((flags = fcntl (fd, F_GETFL, 0)) < 0)
181 return -1;
183 bflags = 0;
185 #ifdef O_NONBLOCK
186 bflags |= O_NONBLOCK;
187 #endif
189 #ifdef O_NDELAY
190 bflags |= O_NDELAY;
191 #endif
193 if (flags & bflags)
195 flags &= ~bflags;
196 return (fcntl (fd, F_SETFL, flags));
199 return 0;