]> git.ipfire.org Git - thirdparty/bash.git/blame - lib/readline/shell.c
Imported from ../bash-2.04.tar.gz.
[thirdparty/bash.git] / lib / readline / shell.c
CommitLineData
d166f048
JA
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
bb70624e 11 as published by the Free Software Foundation; either version 2, or
d166f048
JA
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,
bb70624e 22 59 Temple Place, Suite 330, Boston, MA 02111 USA. */
d166f048
JA
23#define READLINE_LIBRARY
24
25#if defined (HAVE_CONFIG_H)
26# include <config.h>
27#endif
28
b72432fd
JA
29#include <sys/types.h>
30
d166f048
JA
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
cce855bc
JA
41#if defined (HAVE_STRING_H)
42# include <string.h>
43#else
44# include <strings.h>
45#endif /* !HAVE_STRING_H */
46
bb70624e 47#include <fcntl.h>
b72432fd 48#include <pwd.h>
d166f048 49
bb70624e
JA
50#include <stdio.h>
51
52#include "rlshell.h"
53#include "xmalloc.h"
54
b72432fd
JA
55#if !defined (HAVE_GETPW_DECLS)
56extern struct passwd *getpwuid ();
57#endif /* !HAVE_GETPW_DECLS */
d166f048 58
bb70624e
JA
59#ifndef NULL
60# define NULL 0
61#endif
d166f048 62
b72432fd
JA
63/* All of these functions are resolved from bash if we are linking readline
64 as part of bash. */
d166f048
JA
65
66/* Does shell-like quoting using single quotes. */
67char *
68single_quote (string)
69 char *string;
70{
71 register int c;
72 char *result, *r, *s;
73
bb70624e 74 result = (char *)xmalloc (3 + (4 * strlen (string)));
d166f048
JA
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. */
98void
99set_lines_and_columns (lines, cols)
100 int lines, cols;
101{
102 char *b;
103
104#if defined (HAVE_PUTENV)
105 b = xmalloc (24);
106 sprintf (b, "LINES=%d", lines);
107 putenv (b);
108 b = xmalloc (24);
109 sprintf (b, "COLUMNS=%d", cols);
110 putenv (b);
111#else /* !HAVE_PUTENV */
112# if defined (HAVE_SETENV)
113 b = xmalloc (8);
114 sprintf (b, "%d", lines);
115 setenv ("LINES", b, 1);
116 b = xmalloc (8);
117 sprintf (b, "%d", cols);
118 setenv ("COLUMNS", b, 1);
119# endif /* HAVE_SETENV */
120#endif /* !HAVE_PUTENV */
121}
122
123char *
124get_env_value (varname)
125 char *varname;
126{
127 return ((char *)getenv (varname));
128}
129
d166f048 130char *
b72432fd 131get_home_dir ()
d166f048 132{
b72432fd
JA
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}
bb70624e
JA
142
143#if !defined (O_NDELAY)
144# if defined (FNDELAY)
145# define O_NDELAY FNDELAY
146# endif
147#endif
148
149int
150unset_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}