]> rtime.felk.cvut.cz Git - l4.git/blob - l4/pkg/readline/lib/contrib/libreadline/shell.c
Inital import
[l4.git] / l4 / pkg / readline / lib / contrib / libreadline / shell.c
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
11    as published by the Free Software Foundation; either version 2, or
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,
22    59 Temple Place, Suite 330, Boston, MA 02111 USA. */
23 #define READLINE_LIBRARY
24
25 #if defined (HAVE_CONFIG_H)
26 #  include <config.h>
27 #endif
28
29 #include <sys/types.h>
30
31 #if defined (HAVE_UNISTD_H)
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
47 #include <fcntl.h>
48 #include <pwd.h>
49
50 #include <stdio.h>
51
52 #include "rlshell.h"
53 #include "xmalloc.h"
54
55 #if !defined (HAVE_GETPW_DECLS)
56 extern struct passwd *getpwuid ();
57 #endif /* !HAVE_GETPW_DECLS */
58
59 #ifndef NULL
60 #  define NULL 0
61 #endif
62
63 /* All of these functions are resolved from bash if we are linking readline
64    as part of bash. */
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
74   result = (char *)xmalloc (3 + (4 * strlen (string)));
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   if (asprintf (&b, "LINES=%d", lines) == -1)
106           memory_error_and_abort("asprintf");
107   putenv (b);
108   if (asprintf (&b, "COLUMNS=%d", cols) == -1)
109           memory_error_and_abort("asprintf");
110   putenv (b);
111 #else /* !HAVE_PUTENV */
112 #  if defined (HAVE_SETENV)
113   if (asprintf(&b, "%d", lines) == -1)
114           memory_error_and_abort("asprintf");
115   setenv ("LINES", b, 1);
116   if (asprintf (&b, "%d", cols) == -1)
117           memory_error_and_abort("asprintf");
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
130 char *
131 get_home_dir ()
132 {
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 }
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 }