Merge from vendor branch OPENSSL:
[dragonfly.git] / contrib / libreadline / shell.c
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. */
5
6 /* Copyright (C) 1997 Free Software Foundation, Inc.
7
8    This file is part of the GNU Readline Library, a library for
9    reading lines of text with interactive input and history editing.
10
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.
15
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.
20
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
26
27 #if defined (HAVE_CONFIG_H)
28 #  include <config.h>
29 #endif
30
31 #include <sys/types.h>
32
33 #if defined (HAVE_UNISTD_H)
34 #  include <unistd.h>
35 #endif /* HAVE_UNISTD_H */
36
37 #if defined (HAVE_STDLIB_H)
38 #  include <stdlib.h>
39 #else
40 #  include "ansi_stdlib.h"
41 #endif /* HAVE_STDLIB_H */
42
43 #if defined (HAVE_STRING_H)
44 #  include <string.h>
45 #else
46 #  include <strings.h>
47 #endif /* !HAVE_STRING_H */
48
49 #include <fcntl.h>
50 #include <pwd.h>
51
52 #include <stdio.h>
53
54 #include "rlshell.h"
55 #include "xmalloc.h"
56
57 #if !defined (HAVE_GETPW_DECLS)
58 extern struct passwd *getpwuid ();
59 #endif /* !HAVE_GETPW_DECLS */
60
61 #ifndef NULL
62 #  define NULL 0
63 #endif
64
65 /* All of these functions are resolved from bash if we are linking readline
66    as part of bash. */
67
68 /* Does shell-like quoting using single quotes. */
69 char *
70 single_quote (string)
71      char *string;
72 {
73   register int c;
74   char *result, *r, *s;
75
76   result = (char *)xmalloc (3 + (4 * strlen (string)));
77   r = result;
78   *r++ = '\'';
79
80   for (s = string; s && (c = *s); s++)
81     {
82       *r++ = c;
83
84       if (c == '\'')
85         {
86           *r++ = '\\';  /* insert escaped single quote */
87           *r++ = '\'';
88           *r++ = '\'';  /* start new quoted string */
89         }
90     }
91
92   *r++ = '\'';
93   *r = '\0';
94
95   return (result);
96 }
97
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;
103 {
104   char *b;
105
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 */
123 }
124
125 char *
126 get_env_value (varname)
127      char *varname;
128 {
129   return ((char *)getenv (varname));
130 }
131
132 char *
133 get_home_dir ()
134 {
135   char *home_dir;
136   struct passwd *entry;
137
138   home_dir = (char *)NULL;
139   entry = getpwuid (getuid ());
140   if (entry)
141     home_dir = entry->pw_dir;
142   return (home_dir);
143 }
144
145 #if !defined (O_NDELAY)
146 #  if defined (FNDELAY)
147 #    define O_NDELAY FNDELAY
148 #  endif
149 #endif
150
151 int
152 unset_nodelay_mode (fd)
153      int fd;
154 {
155   int flags, bflags;
156
157   if ((flags = fcntl (fd, F_GETFL, 0)) < 0)
158     return -1;
159
160   bflags = 0;
161
162 #ifdef O_NONBLOCK
163   bflags |= O_NONBLOCK;
164 #endif
165
166 #ifdef O_NDELAY
167   bflags |= O_NDELAY;
168 #endif
169
170   if (flags & bflags)
171     {
172       flags &= ~bflags;
173       return (fcntl (fd, F_SETFL, flags));
174     }
175
176   return 0;
177 }