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