]> git.ipfire.org Git - thirdparty/bash.git/blame - lib/readline/shell.c
Imported from ../bash-2.05.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
28ef6c31 52#include "rlstdc.h"
bb70624e
JA
53#include "rlshell.h"
54#include "xmalloc.h"
55
b72432fd 56#if !defined (HAVE_GETPW_DECLS)
28ef6c31 57extern struct passwd *getpwuid __P((uid_t));
b72432fd 58#endif /* !HAVE_GETPW_DECLS */
d166f048 59
bb70624e
JA
60#ifndef NULL
61# define NULL 0
62#endif
d166f048 63
b72432fd
JA
64/* All of these functions are resolved from bash if we are linking readline
65 as part of bash. */
d166f048
JA
66
67/* Does shell-like quoting using single quotes. */
68char *
28ef6c31 69sh_single_quote (string)
d166f048
JA
70 char *string;
71{
72 register int c;
73 char *result, *r, *s;
74
bb70624e 75 result = (char *)xmalloc (3 + (4 * strlen (string)));
d166f048
JA
76 r = result;
77 *r++ = '\'';
78
79 for (s = string; s && (c = *s); s++)
80 {
81 *r++ = c;
82
83 if (c == '\'')
84 {
85 *r++ = '\\'; /* insert escaped single quote */
86 *r++ = '\'';
87 *r++ = '\''; /* start new quoted string */
88 }
89 }
90
91 *r++ = '\'';
92 *r = '\0';
93
94 return (result);
95}
96
97/* Set the environment variables LINES and COLUMNS to lines and cols,
98 respectively. */
99void
28ef6c31 100sh_set_lines_and_columns (lines, cols)
d166f048
JA
101 int lines, cols;
102{
103 char *b;
104
105#if defined (HAVE_PUTENV)
106 b = xmalloc (24);
107 sprintf (b, "LINES=%d", lines);
108 putenv (b);
109 b = xmalloc (24);
110 sprintf (b, "COLUMNS=%d", cols);
111 putenv (b);
112#else /* !HAVE_PUTENV */
113# if defined (HAVE_SETENV)
114 b = xmalloc (8);
115 sprintf (b, "%d", lines);
116 setenv ("LINES", b, 1);
117 b = xmalloc (8);
118 sprintf (b, "%d", cols);
119 setenv ("COLUMNS", b, 1);
120# endif /* HAVE_SETENV */
121#endif /* !HAVE_PUTENV */
122}
123
124char *
28ef6c31
JA
125sh_get_env_value (varname)
126 const char *varname;
d166f048
JA
127{
128 return ((char *)getenv (varname));
129}
130
d166f048 131char *
28ef6c31 132sh_get_home_dir ()
d166f048 133{
b72432fd
JA
134 char *home_dir;
135 struct passwd *entry;
136
137 home_dir = (char *)NULL;
138 entry = getpwuid (getuid ());
139 if (entry)
140 home_dir = entry->pw_dir;
141 return (home_dir);
142}
bb70624e
JA
143
144#if !defined (O_NDELAY)
145# if defined (FNDELAY)
146# define O_NDELAY FNDELAY
147# endif
148#endif
149
150int
28ef6c31 151sh_unset_nodelay_mode (fd)
bb70624e
JA
152 int fd;
153{
154 int flags, bflags;
155
156 if ((flags = fcntl (fd, F_GETFL, 0)) < 0)
157 return -1;
158
159 bflags = 0;
160
161#ifdef O_NONBLOCK
162 bflags |= O_NONBLOCK;
163#endif
164
165#ifdef O_NDELAY
166 bflags |= O_NDELAY;
167#endif
168
169 if (flags & bflags)
170 {
171 flags &= ~bflags;
172 return (fcntl (fd, F_SETFL, flags));
173 }
174
175 return 0;
176}