* histfile.c (read_history_range): Remove '\r' character from
[deliverable/binutils-gdb.git] / readline / shell.c
CommitLineData
d60d9f65
SS
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
1b17e766 11 as published by the Free Software Foundation; either version 2, or
d60d9f65
SS
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,
1b17e766 22 59 Temple Place, Suite 330, Boston, MA 02111 USA. */
d60d9f65
SS
23#define READLINE_LIBRARY
24
25#if defined (HAVE_CONFIG_H)
26# include <config.h>
27#endif
28
c862e87b 29#include <sys/types.h>
d1857004 30#include <stdio.h>
c862e87b 31
d60d9f65 32#if defined (HAVE_UNISTD_H)
d60d9f65
SS
33# include <unistd.h>
34#endif /* HAVE_UNISTD_H */
35
36#if defined (HAVE_STDLIB_H)
37# include <stdlib.h>
38#else
39# include "ansi_stdlib.h"
40#endif /* HAVE_STDLIB_H */
41
42#if defined (HAVE_STRING_H)
43# include <string.h>
44#else
45# include <strings.h>
46#endif /* !HAVE_STRING_H */
47
9255ee31
EZ
48#if defined (HAVE_LIMITS_H)
49# include <limits.h>
50#endif
51
1b17e766 52#include <fcntl.h>
430b7832 53#ifdef HAVE_PWD_H
c862e87b 54#include <pwd.h>
430b7832 55#endif
d60d9f65 56
1b17e766
EZ
57#include <stdio.h>
58
9255ee31 59#include "rlstdc.h"
1b17e766
EZ
60#include "rlshell.h"
61#include "xmalloc.h"
62
c862e87b 63#if !defined (HAVE_GETPW_DECLS)
9255ee31 64extern struct passwd *getpwuid PARAMS((uid_t));
c862e87b 65#endif /* !HAVE_GETPW_DECLS */
d60d9f65 66
1b17e766
EZ
67#ifndef NULL
68# define NULL 0
69#endif
d60d9f65 70
9255ee31
EZ
71#ifndef CHAR_BIT
72# define CHAR_BIT 8
73#endif
74
75/* Nonzero if the integer type T is signed. */
76#define TYPE_SIGNED(t) (! ((t) 0 < (t) -1))
77
78/* Bound on length of the string representing an integer value of type T.
79 Subtract one for the sign bit if T is signed;
80 302 / 1000 is log10 (2) rounded up;
81 add one for integer division truncation;
82 add one more for a minus sign if t is signed. */
83#define INT_STRLEN_BOUND(t) \
84 ((sizeof (t) * CHAR_BIT - TYPE_SIGNED (t)) * 302 / 1000 \
85 + 1 + TYPE_SIGNED (t))
86
c862e87b
JM
87/* All of these functions are resolved from bash if we are linking readline
88 as part of bash. */
d60d9f65
SS
89
90/* Does shell-like quoting using single quotes. */
91char *
9255ee31 92sh_single_quote (string)
d60d9f65
SS
93 char *string;
94{
95 register int c;
96 char *result, *r, *s;
97
1b17e766 98 result = (char *)xmalloc (3 + (4 * strlen (string)));
d60d9f65
SS
99 r = result;
100 *r++ = '\'';
101
102 for (s = string; s && (c = *s); s++)
103 {
104 *r++ = c;
105
106 if (c == '\'')
107 {
108 *r++ = '\\'; /* insert escaped single quote */
109 *r++ = '\'';
110 *r++ = '\''; /* start new quoted string */
111 }
112 }
113
114 *r++ = '\'';
115 *r = '\0';
116
117 return (result);
118}
119
120/* Set the environment variables LINES and COLUMNS to lines and cols,
121 respectively. */
122void
9255ee31 123sh_set_lines_and_columns (lines, cols)
d60d9f65
SS
124 int lines, cols;
125{
126 char *b;
127
128#if defined (HAVE_PUTENV)
9255ee31 129 b = (char *)xmalloc (INT_STRLEN_BOUND (int) + sizeof ("LINES=") + 1);
d60d9f65
SS
130 sprintf (b, "LINES=%d", lines);
131 putenv (b);
9255ee31 132 b = (char *)xmalloc (INT_STRLEN_BOUND (int) + sizeof ("COLUMNS=") + 1);
d60d9f65
SS
133 sprintf (b, "COLUMNS=%d", cols);
134 putenv (b);
135#else /* !HAVE_PUTENV */
136# if defined (HAVE_SETENV)
9255ee31 137 b = (char *)xmalloc (INT_STRLEN_BOUND (int) + 1);
d60d9f65
SS
138 sprintf (b, "%d", lines);
139 setenv ("LINES", b, 1);
9255ee31 140 b = (char *)xmalloc (INT_STRLEN_BOUND (int) + 1);
d60d9f65
SS
141 sprintf (b, "%d", cols);
142 setenv ("COLUMNS", b, 1);
143# endif /* HAVE_SETENV */
144#endif /* !HAVE_PUTENV */
145}
146
147char *
9255ee31
EZ
148sh_get_env_value (varname)
149 const char *varname;
d60d9f65
SS
150{
151 return ((char *)getenv (varname));
152}
153
d60d9f65 154char *
9255ee31 155sh_get_home_dir ()
d60d9f65 156{
c862e87b
JM
157 char *home_dir;
158 struct passwd *entry;
159
160 home_dir = (char *)NULL;
430b7832 161#ifdef HAVE_GETPWUID
c862e87b
JM
162 entry = getpwuid (getuid ());
163 if (entry)
164 home_dir = entry->pw_dir;
430b7832 165#endif
c862e87b
JM
166 return (home_dir);
167}
1b17e766
EZ
168
169#if !defined (O_NDELAY)
170# if defined (FNDELAY)
171# define O_NDELAY FNDELAY
172# endif
173#endif
174
175int
9255ee31 176sh_unset_nodelay_mode (fd)
1b17e766
EZ
177 int fd;
178{
430b7832 179#ifdef HAVE_FNCTL
1b17e766
EZ
180 int flags, bflags;
181
182 if ((flags = fcntl (fd, F_GETFL, 0)) < 0)
183 return -1;
184
185 bflags = 0;
186
187#ifdef O_NONBLOCK
188 bflags |= O_NONBLOCK;
189#endif
190
191#ifdef O_NDELAY
192 bflags |= O_NDELAY;
193#endif
194
195 if (flags & bflags)
196 {
197 flags &= ~bflags;
198 return (fcntl (fd, F_SETFL, flags));
199 }
430b7832 200#endif
1b17e766
EZ
201
202 return 0;
203}
This page took 0.289795 seconds and 4 git commands to generate.