("" < 3.4) always evaluates to true, which unconditionally
[dragonfly.git] / contrib / libreadline / shell.c
blobeb1b75fa79708e2fbc15ed276e287e3073cae975
1 /* $FreeBSD: src/contrib/libreadline/shell.c,v 1.4.2.2 2000/07/06 23:04:24 ache Exp $ */
2 /* $DragonFly: src/contrib/libreadline/Attic/shell.c,v 1.2 2003/06/17 04:24:03 dillon Exp $ */
3 /* shell.c -- readline utility functions that are normally provided by
4 bash when readline is linked as part of the shell. */
6 /* Copyright (C) 1997 Free Software Foundation, Inc.
8 This file is part of the GNU Readline Library, a library for
9 reading lines of text with interactive input and history editing.
11 The GNU Readline Library is free software; you can redistribute it
12 and/or modify it under the terms of the GNU General Public License
13 as published by the Free Software Foundation; either version 2, or
14 (at your option) any later version.
16 The GNU Readline Library is distributed in the hope that it will be
17 useful, but WITHOUT ANY WARRANTY; without even the implied warranty
18 of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
21 The GNU General Public License is often shipped with GNU software, and
22 is generally kept in a file called COPYING or LICENSE. If you do not
23 have a copy of the license, write to the Free Software Foundation,
24 59 Temple Place, Suite 330, Boston, MA 02111 USA. */
25 #define READLINE_LIBRARY
27 #if defined (HAVE_CONFIG_H)
28 # include <config.h>
29 #endif
31 #include <sys/types.h>
33 #if defined (HAVE_UNISTD_H)
34 # include <unistd.h>
35 #endif /* HAVE_UNISTD_H */
37 #if defined (HAVE_STDLIB_H)
38 # include <stdlib.h>
39 #else
40 # include "ansi_stdlib.h"
41 #endif /* HAVE_STDLIB_H */
43 #if defined (HAVE_STRING_H)
44 # include <string.h>
45 #else
46 # include <strings.h>
47 #endif /* !HAVE_STRING_H */
49 #include <fcntl.h>
50 #include <pwd.h>
52 #include <stdio.h>
54 #include "rlshell.h"
55 #include "xmalloc.h"
57 #if !defined (HAVE_GETPW_DECLS)
58 extern struct passwd *getpwuid ();
59 #endif /* !HAVE_GETPW_DECLS */
61 #ifndef NULL
62 # define NULL 0
63 #endif
65 /* All of these functions are resolved from bash if we are linking readline
66 as part of bash. */
68 /* Does shell-like quoting using single quotes. */
69 char *
70 single_quote (string)
71 char *string;
73 register int c;
74 char *result, *r, *s;
76 result = (char *)xmalloc (3 + (4 * strlen (string)));
77 r = result;
78 *r++ = '\'';
80 for (s = string; s && (c = *s); s++)
82 *r++ = c;
84 if (c == '\'')
86 *r++ = '\\'; /* insert escaped single quote */
87 *r++ = '\'';
88 *r++ = '\''; /* start new quoted string */
92 *r++ = '\'';
93 *r = '\0';
95 return (result);
98 /* Set the environment variables LINES and COLUMNS to lines and cols,
99 respectively. */
100 void
101 set_lines_and_columns (lines, cols)
102 int lines, cols;
104 char *b;
106 #if defined (HAVE_PUTENV)
107 b = xmalloc (24);
108 sprintf (b, "LINES=%d", lines);
109 putenv (b);
110 b = xmalloc (24);
111 sprintf (b, "COLUMNS=%d", cols);
112 putenv (b);
113 #else /* !HAVE_PUTENV */
114 # if defined (HAVE_SETENV)
115 b = xmalloc (8);
116 sprintf (b, "%d", lines);
117 setenv ("LINES", b, 1);
118 b = xmalloc (8);
119 sprintf (b, "%d", cols);
120 setenv ("COLUMNS", b, 1);
121 # endif /* HAVE_SETENV */
122 #endif /* !HAVE_PUTENV */
125 char *
126 get_env_value (varname)
127 char *varname;
129 return ((char *)getenv (varname));
132 char *
133 get_home_dir ()
135 char *home_dir;
136 struct passwd *entry;
138 home_dir = (char *)NULL;
139 entry = getpwuid (getuid ());
140 if (entry)
141 home_dir = entry->pw_dir;
142 return (home_dir);
145 #if !defined (O_NDELAY)
146 # if defined (FNDELAY)
147 # define O_NDELAY FNDELAY
148 # endif
149 #endif
152 unset_nodelay_mode (fd)
153 int fd;
155 int flags, bflags;
157 if ((flags = fcntl (fd, F_GETFL, 0)) < 0)
158 return -1;
160 bflags = 0;
162 #ifdef O_NONBLOCK
163 bflags |= O_NONBLOCK;
164 #endif
166 #ifdef O_NDELAY
167 bflags |= O_NDELAY;
168 #endif
170 if (flags & bflags)
172 flags &= ~bflags;
173 return (fcntl (fd, F_SETFL, flags));
176 return 0;