00001 /* shell.c -- readline utility functions that are normally provided by 00002 bash when readline is linked as part of the shell. */ 00003 00004 /* Copyright (C) 1997 Free Software Foundation, Inc. 00005 00006 This file is part of the GNU Readline Library, a library for 00007 reading lines of text with interactive input and history editing. 00008 00009 The GNU Readline Library is free software; you can redistribute it 00010 and/or modify it under the terms of the GNU General Public License 00011 as published by the Free Software Foundation; either version 2, or 00012 (at your option) any later version. 00013 00014 The GNU Readline Library is distributed in the hope that it will be 00015 useful, but WITHOUT ANY WARRANTY; without even the implied warranty 00016 of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00017 GNU General Public License for more details. 00018 00019 The GNU General Public License is often shipped with GNU software, and 00020 is generally kept in a file called COPYING or LICENSE. If you do not 00021 have a copy of the license, write to the Free Software Foundation, 00022 59 Temple Place, Suite 330, Boston, MA 02111 USA. */ 00023 #define READLINE_LIBRARY 00024 00025 #include "config_readline.h" 00026 00027 #include <sys/types.h> 00028 00029 #if defined (HAVE_UNISTD_H) 00030 # include <unistd.h> 00031 #endif /* HAVE_UNISTD_H */ 00032 00033 #if defined (HAVE_STDLIB_H) 00034 # include <stdlib.h> 00035 #else 00036 # include "ansi_stdlib.h" 00037 #endif /* HAVE_STDLIB_H */ 00038 00039 #if defined (HAVE_STRING_H) 00040 # include <string.h> 00041 #else 00042 # include <strings.h> 00043 #endif /* !HAVE_STRING_H */ 00044 00045 #if defined (HAVE_LIMITS_H) 00046 # include <limits.h> 00047 #endif 00048 00049 #include <fcntl.h> 00050 #include <pwd.h> 00051 00052 #include <stdio.h> 00053 00054 #include "rlstdc.h" 00055 #include "rlshell.h" 00056 #include "xmalloc.h" 00057 00058 #if !defined (HAVE_GETPW_DECLS) 00059 extern struct passwd *getpwuid PARAMS((uid_t)); 00060 #endif /* !HAVE_GETPW_DECLS */ 00061 00062 #ifndef NULL 00063 # define NULL 0 00064 #endif 00065 00066 #ifndef CHAR_BIT 00067 # define CHAR_BIT 8 00068 #endif 00069 00070 /* Nonzero if the integer type T is signed. */ 00071 #define TYPE_SIGNED(t) (! ((t) 0 < (t) -1)) 00072 00073 /* Bound on length of the string representing an integer value of type T. 00074 Subtract one for the sign bit if T is signed; 00075 302 / 1000 is log10 (2) rounded up; 00076 add one for integer division truncation; 00077 add one more for a minus sign if t is signed. */ 00078 #define INT_STRLEN_BOUND(t) \ 00079 ((sizeof (t) * CHAR_BIT - TYPE_SIGNED (t)) * 302 / 1000 \ 00080 + 1 + TYPE_SIGNED (t)) 00081 00082 /* All of these functions are resolved from bash if we are linking readline 00083 as part of bash. */ 00084 00085 /* Does shell-like quoting using single quotes. */ 00086 char * 00087 sh_single_quote (string) 00088 char *string; 00089 { 00090 register int c; 00091 char *result, *r, *s; 00092 00093 result = (char *)xmalloc (3 + (4 * strlen (string))); 00094 r = result; 00095 *r++ = '\''; 00096 00097 for (s = string; s && (c = *s); s++) 00098 { 00099 *r++ = c; 00100 00101 if (c == '\'') 00102 { 00103 *r++ = '\\'; /* insert escaped single quote */ 00104 *r++ = '\''; 00105 *r++ = '\''; /* start new quoted string */ 00106 } 00107 } 00108 00109 *r++ = '\''; 00110 *r = '\0'; 00111 00112 return (result); 00113 } 00114 00115 /* Set the environment variables LINES and COLUMNS to lines and cols, 00116 respectively. */ 00117 void 00118 sh_set_lines_and_columns (lines, cols) 00119 int lines, cols; 00120 { 00121 char *b; 00122 00123 #if defined (HAVE_PUTENV) 00124 b = (char *)xmalloc (INT_STRLEN_BOUND (int) + sizeof ("LINES=") + 1); 00125 sprintf (b, "LINES=%d", lines); 00126 putenv (b); 00127 00128 b = (char *)xmalloc (INT_STRLEN_BOUND (int) + sizeof ("COLUMNS=") + 1); 00129 sprintf (b, "COLUMNS=%d", cols); 00130 putenv (b); 00131 #else /* !HAVE_PUTENV */ 00132 # if defined (HAVE_SETENV) 00133 b = (char *)xmalloc (INT_STRLEN_BOUND (int) + 1); 00134 sprintf (b, "%d", lines); 00135 setenv ("LINES", b, 1); 00136 free (b); 00137 00138 b = (char *)xmalloc (INT_STRLEN_BOUND (int) + 1); 00139 sprintf (b, "%d", cols); 00140 setenv ("COLUMNS", b, 1); 00141 free (b); 00142 # endif /* HAVE_SETENV */ 00143 #endif /* !HAVE_PUTENV */ 00144 } 00145 00146 char * 00147 sh_get_env_value (varname) 00148 const char *varname; 00149 { 00150 return ((char *)getenv (varname)); 00151 } 00152 00153 char * 00154 sh_get_home_dir () 00155 { 00156 char *home_dir; 00157 struct passwd *entry; 00158 00159 home_dir = (char *)NULL; 00160 entry = getpwuid (getuid ()); 00161 if (entry) 00162 home_dir = entry->pw_dir; 00163 return (home_dir); 00164 } 00165 00166 #if !defined (O_NDELAY) 00167 # if defined (FNDELAY) 00168 # define O_NDELAY FNDELAY 00169 # endif 00170 #endif 00171 00172 int 00173 sh_unset_nodelay_mode (fd) 00174 int fd; 00175 { 00176 int flags, bflags; 00177 00178 if ((flags = fcntl (fd, F_GETFL, 0)) < 0) 00179 return -1; 00180 00181 bflags = 0; 00182 00183 #ifdef O_NONBLOCK 00184 bflags |= O_NONBLOCK; 00185 #endif 00186 00187 #ifdef O_NDELAY 00188 bflags |= O_NDELAY; 00189 #endif 00190 00191 if (flags & bflags) 00192 { 00193 flags &= ~bflags; 00194 return (fcntl (fd, F_SETFL, flags)); 00195 } 00196 00197 return 0; 00198 }
1.4.7

