1/* $FreeBSD: head/contrib/libreadline/shell.c 136758 2004-10-21 20:02:02Z peter $ */ 2
| 1/* $FreeBSD: head/contrib/libreadline/shell.c 157188 2006-03-27 23:11:32Z ache $ */
|
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#if defined (HAVE_LIMITS_H) 50# include <limits.h> 51#endif 52
| 2/* shell.c -- readline utility functions that are normally provided by 3 bash when readline is linked as part of the shell. */ 4 5/* Copyright (C) 1997 Free Software Foundation, Inc. 6 7 This file is part of the GNU Readline Library, a library for 8 reading lines of text with interactive input and history editing. 9 10 The GNU Readline Library is free software; you can redistribute it 11 and/or modify it under the terms of the GNU General Public License 12 as published by the Free Software Foundation; either version 2, or 13 (at your option) any later version. 14 15 The GNU Readline Library is distributed in the hope that it will be 16 useful, but WITHOUT ANY WARRANTY; without even the implied warranty 17 of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 GNU General Public License for more details. 19 20 The GNU General Public License is often shipped with GNU software, and 21 is generally kept in a file called COPYING or LICENSE. If you do not 22 have a copy of the license, write to the Free Software Foundation, 23 59 Temple Place, Suite 330, Boston, MA 02111 USA. */ 24#define READLINE_LIBRARY 25 26#if defined (HAVE_CONFIG_H) 27# include <config.h> 28#endif 29 30#include <sys/types.h> 31 32#if defined (HAVE_UNISTD_H) 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 48#if defined (HAVE_LIMITS_H) 49# include <limits.h> 50#endif 51
|
| 52#if defined (HAVE_FCNTL_H)
|
53#include <fcntl.h>
| 53#include <fcntl.h>
|
| 54#endif 55#if defined (HAVE_PWD_H)
|
54#include <pwd.h>
| 56#include <pwd.h>
|
| 57#endif
|
55 56#include <stdio.h> 57 58#include "rlstdc.h" 59#include "rlshell.h" 60#include "xmalloc.h" 61
| 58 59#include <stdio.h> 60 61#include "rlstdc.h" 62#include "rlshell.h" 63#include "xmalloc.h" 64
|
62#if !defined (HAVE_GETPW_DECLS)
| 65#if defined (HAVE_GETPWUID) && !defined (HAVE_GETPW_DECLS)
|
63extern struct passwd *getpwuid PARAMS((uid_t));
| 66extern struct passwd *getpwuid PARAMS((uid_t));
|
64#endif /* !HAVE_GETPW_DECLS */
| 67#endif /* HAVE_GETPWUID && !HAVE_GETPW_DECLS */
|
65 66#ifndef NULL 67# define NULL 0 68#endif 69 70#ifndef CHAR_BIT 71# define CHAR_BIT 8 72#endif 73 74/* Nonzero if the integer type T is signed. */ 75#define TYPE_SIGNED(t) (! ((t) 0 < (t) -1)) 76 77/* Bound on length of the string representing an integer value of type T. 78 Subtract one for the sign bit if T is signed; 79 302 / 1000 is log10 (2) rounded up; 80 add one for integer division truncation; 81 add one more for a minus sign if t is signed. */ 82#define INT_STRLEN_BOUND(t) \ 83 ((sizeof (t) * CHAR_BIT - TYPE_SIGNED (t)) * 302 / 1000 \ 84 + 1 + TYPE_SIGNED (t)) 85 86/* All of these functions are resolved from bash if we are linking readline 87 as part of bash. */ 88 89/* Does shell-like quoting using single quotes. */ 90char * 91sh_single_quote (string) 92 char *string; 93{ 94 register int c; 95 char *result, *r, *s; 96 97 result = (char *)xmalloc (3 + (4 * strlen (string))); 98 r = result; 99 *r++ = '\''; 100 101 for (s = string; s && (c = *s); s++) 102 { 103 *r++ = c; 104 105 if (c == '\'') 106 { 107 *r++ = '\\'; /* insert escaped single quote */ 108 *r++ = '\''; 109 *r++ = '\''; /* start new quoted string */ 110 } 111 } 112 113 *r++ = '\''; 114 *r = '\0'; 115 116 return (result); 117} 118 119/* Set the environment variables LINES and COLUMNS to lines and cols, 120 respectively. */ 121void 122sh_set_lines_and_columns (lines, cols) 123 int lines, cols; 124{ 125 char *b; 126
| 68 69#ifndef NULL 70# define NULL 0 71#endif 72 73#ifndef CHAR_BIT 74# define CHAR_BIT 8 75#endif 76 77/* Nonzero if the integer type T is signed. */ 78#define TYPE_SIGNED(t) (! ((t) 0 < (t) -1)) 79 80/* Bound on length of the string representing an integer value of type T. 81 Subtract one for the sign bit if T is signed; 82 302 / 1000 is log10 (2) rounded up; 83 add one for integer division truncation; 84 add one more for a minus sign if t is signed. */ 85#define INT_STRLEN_BOUND(t) \ 86 ((sizeof (t) * CHAR_BIT - TYPE_SIGNED (t)) * 302 / 1000 \ 87 + 1 + TYPE_SIGNED (t)) 88 89/* All of these functions are resolved from bash if we are linking readline 90 as part of bash. */ 91 92/* Does shell-like quoting using single quotes. */ 93char * 94sh_single_quote (string) 95 char *string; 96{ 97 register int c; 98 char *result, *r, *s; 99 100 result = (char *)xmalloc (3 + (4 * strlen (string))); 101 r = result; 102 *r++ = '\''; 103 104 for (s = string; s && (c = *s); s++) 105 { 106 *r++ = c; 107 108 if (c == '\'') 109 { 110 *r++ = '\\'; /* insert escaped single quote */ 111 *r++ = '\''; 112 *r++ = '\''; /* start new quoted string */ 113 } 114 } 115 116 *r++ = '\''; 117 *r = '\0'; 118 119 return (result); 120} 121 122/* Set the environment variables LINES and COLUMNS to lines and cols, 123 respectively. */ 124void 125sh_set_lines_and_columns (lines, cols) 126 int lines, cols; 127{ 128 char *b; 129
|
127#if defined (HAVE_PUTENV) 128 b = (char *)xmalloc (INT_STRLEN_BOUND (int) + sizeof ("LINES=") + 1); 129 sprintf (b, "LINES=%d", lines); 130 putenv (b); 131 132 b = (char *)xmalloc (INT_STRLEN_BOUND (int) + sizeof ("COLUMNS=") + 1); 133 sprintf (b, "COLUMNS=%d", cols); 134 putenv (b); 135#else /* !HAVE_PUTENV */ 136# if defined (HAVE_SETENV)
| 130#if defined (HAVE_SETENV)
|
137 b = (char *)xmalloc (INT_STRLEN_BOUND (int) + 1); 138 sprintf (b, "%d", lines); 139 setenv ("LINES", b, 1); 140 free (b); 141 142 b = (char *)xmalloc (INT_STRLEN_BOUND (int) + 1); 143 sprintf (b, "%d", cols); 144 setenv ("COLUMNS", b, 1); 145 free (b);
| 131 b = (char *)xmalloc (INT_STRLEN_BOUND (int) + 1); 132 sprintf (b, "%d", lines); 133 setenv ("LINES", b, 1); 134 free (b); 135 136 b = (char *)xmalloc (INT_STRLEN_BOUND (int) + 1); 137 sprintf (b, "%d", cols); 138 setenv ("COLUMNS", b, 1); 139 free (b);
|
146# endif /* HAVE_SETENV */ 147#endif /* !HAVE_PUTENV */
| 140#else /* !HAVE_SETENV */ 141# if defined (HAVE_PUTENV) 142 b = (char *)xmalloc (INT_STRLEN_BOUND (int) + sizeof ("LINES=") + 1); 143 sprintf (b, "LINES=%d", lines); 144 putenv (b); 145 146 b = (char *)xmalloc (INT_STRLEN_BOUND (int) + sizeof ("COLUMNS=") + 1); 147 sprintf (b, "COLUMNS=%d", cols); 148 putenv (b); 149# endif /* HAVE_PUTENV */ 150#endif /* !HAVE_SETENV */
|
148} 149 150char * 151sh_get_env_value (varname) 152 const char *varname; 153{ 154 return ((char *)getenv (varname)); 155} 156 157char * 158sh_get_home_dir () 159{ 160 char *home_dir; 161 struct passwd *entry; 162 163 home_dir = (char *)NULL;
| 151} 152 153char * 154sh_get_env_value (varname) 155 const char *varname; 156{ 157 return ((char *)getenv (varname)); 158} 159 160char * 161sh_get_home_dir () 162{ 163 char *home_dir; 164 struct passwd *entry; 165 166 home_dir = (char *)NULL;
|
| 167#if defined (HAVE_GETPWUID)
|
164 entry = getpwuid (getuid ()); 165 if (entry) 166 home_dir = entry->pw_dir;
| 168 entry = getpwuid (getuid ()); 169 if (entry) 170 home_dir = entry->pw_dir;
|
| 171#endif
|
167 return (home_dir); 168} 169 170#if !defined (O_NDELAY) 171# if defined (FNDELAY) 172# define O_NDELAY FNDELAY 173# endif 174#endif 175 176int 177sh_unset_nodelay_mode (fd) 178 int fd; 179{
| 172 return (home_dir); 173} 174 175#if !defined (O_NDELAY) 176# if defined (FNDELAY) 177# define O_NDELAY FNDELAY 178# endif 179#endif 180 181int 182sh_unset_nodelay_mode (fd) 183 int fd; 184{
|
| 185#if defined (HAVE_FCNTL)
|
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 }
| 186 int flags, bflags; 187 188 if ((flags = fcntl (fd, F_GETFL, 0)) < 0) 189 return -1; 190 191 bflags = 0; 192 193#ifdef O_NONBLOCK 194 bflags |= O_NONBLOCK; 195#endif 196 197#ifdef O_NDELAY 198 bflags |= O_NDELAY; 199#endif 200 201 if (flags & bflags) 202 { 203 flags &= ~bflags; 204 return (fcntl (fd, F_SETFL, flags)); 205 }
|
| 206#endif
|
200 201 return 0; 202}
| 207 208 return 0; 209}
|