shell.c revision 58314
158314Sache/* $FreeBSD: head/contrib/libreadline/shell.c 58314 2000-03-19 22:00:57Z ache $ */ 226497Sache/* shell.c -- readline utility functions that are normally provided by 326497Sache bash when readline is linked as part of the shell. */ 426497Sache 526497Sache/* Copyright (C) 1997 Free Software Foundation, Inc. 626497Sache 726497Sache This file is part of the GNU Readline Library, a library for 826497Sache reading lines of text with interactive input and history editing. 926497Sache 1026497Sache The GNU Readline Library is free software; you can redistribute it 1126497Sache and/or modify it under the terms of the GNU General Public License 1258314Sache as published by the Free Software Foundation; either version 2, or 1326497Sache (at your option) any later version. 1426497Sache 1526497Sache The GNU Readline Library is distributed in the hope that it will be 1626497Sache useful, but WITHOUT ANY WARRANTY; without even the implied warranty 1726497Sache of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 1826497Sache GNU General Public License for more details. 1926497Sache 2026497Sache The GNU General Public License is often shipped with GNU software, and 2126497Sache is generally kept in a file called COPYING or LICENSE. If you do not 2226497Sache have a copy of the license, write to the Free Software Foundation, 2358314Sache 59 Temple Place, Suite 330, Boston, MA 02111 USA. */ 2426497Sache#define READLINE_LIBRARY 2526497Sache 2626497Sache#if defined (HAVE_CONFIG_H) 2726497Sache# include <config.h> 2826497Sache#endif 2926497Sache 3047563Sache#include <sys/types.h> 3147563Sache 3226497Sache#if defined (HAVE_UNISTD_H) 3326497Sache# include <unistd.h> 3426497Sache#endif /* HAVE_UNISTD_H */ 3526497Sache 3626497Sache#if defined (HAVE_STDLIB_H) 3726497Sache# include <stdlib.h> 3826497Sache#else 3926497Sache# include "ansi_stdlib.h" 4026497Sache#endif /* HAVE_STDLIB_H */ 4126497Sache 4235489Sache#if defined (HAVE_STRING_H) 4335489Sache# include <string.h> 4435489Sache#else 4535489Sache# include <strings.h> 4635489Sache#endif /* !HAVE_STRING_H */ 4735489Sache 4858314Sache#include <fcntl.h> 4947563Sache#include <pwd.h> 5026497Sache 5158314Sache#include <stdio.h> 5258314Sache 5358314Sache#include "rlshell.h" 5458314Sache#include "xmalloc.h" 5558314Sache 5647563Sache#if !defined (HAVE_GETPW_DECLS) 5747563Sacheextern struct passwd *getpwuid (); 5847563Sache#endif /* !HAVE_GETPW_DECLS */ 5926497Sache 6058314Sache#ifndef NULL 6158314Sache# define NULL 0 6258314Sache#endif 6326497Sache 6447563Sache/* All of these functions are resolved from bash if we are linking readline 6547563Sache as part of bash. */ 6626497Sache 6726497Sache/* Does shell-like quoting using single quotes. */ 6826497Sachechar * 6926497Sachesingle_quote (string) 7026497Sache char *string; 7126497Sache{ 7226497Sache register int c; 7326497Sache char *result, *r, *s; 7426497Sache 7558314Sache result = (char *)xmalloc (3 + (4 * strlen (string))); 7626497Sache r = result; 7726497Sache *r++ = '\''; 7826497Sache 7926497Sache for (s = string; s && (c = *s); s++) 8026497Sache { 8126497Sache *r++ = c; 8226497Sache 8326497Sache if (c == '\'') 8426497Sache { 8526497Sache *r++ = '\\'; /* insert escaped single quote */ 8626497Sache *r++ = '\''; 8726497Sache *r++ = '\''; /* start new quoted string */ 8826497Sache } 8926497Sache } 9026497Sache 9126497Sache *r++ = '\''; 9226497Sache *r = '\0'; 9326497Sache 9426497Sache return (result); 9526497Sache} 9626497Sache 9726497Sache/* Set the environment variables LINES and COLUMNS to lines and cols, 9826497Sache respectively. */ 9926497Sachevoid 10026497Sacheset_lines_and_columns (lines, cols) 10126497Sache int lines, cols; 10226497Sache{ 10326497Sache char *b; 10426497Sache 10526497Sache#if defined (HAVE_PUTENV) 10626497Sache b = xmalloc (24); 10726497Sache sprintf (b, "LINES=%d", lines); 10826497Sache putenv (b); 10926497Sache b = xmalloc (24); 11026497Sache sprintf (b, "COLUMNS=%d", cols); 11126497Sache putenv (b); 11226497Sache#else /* !HAVE_PUTENV */ 11326497Sache# if defined (HAVE_SETENV) 11426497Sache b = xmalloc (8); 11526497Sache sprintf (b, "%d", lines); 11626497Sache setenv ("LINES", b, 1); 11726497Sache b = xmalloc (8); 11826497Sache sprintf (b, "%d", cols); 11926497Sache setenv ("COLUMNS", b, 1); 12026497Sache# endif /* HAVE_SETENV */ 12126497Sache#endif /* !HAVE_PUTENV */ 12226497Sache} 12326497Sache 12426497Sachechar * 12526497Sacheget_env_value (varname) 12626497Sache char *varname; 12726497Sache{ 12826497Sache return ((char *)getenv (varname)); 12926497Sache} 13026497Sache 13126497Sachechar * 13247563Sacheget_home_dir () 13326497Sache{ 13447563Sache char *home_dir; 13547563Sache struct passwd *entry; 13647563Sache 13747563Sache home_dir = (char *)NULL; 13847563Sache entry = getpwuid (getuid ()); 13947563Sache if (entry) 14047563Sache home_dir = entry->pw_dir; 14147563Sache return (home_dir); 14247563Sache} 14358314Sache 14458314Sache#if !defined (O_NDELAY) 14558314Sache# if defined (FNDELAY) 14658314Sache# define O_NDELAY FNDELAY 14758314Sache# endif 14858314Sache#endif 14958314Sache 15058314Sacheint 15158314Sacheunset_nodelay_mode (fd) 15258314Sache int fd; 15358314Sache{ 15458314Sache int flags, bflags; 15558314Sache 15658314Sache if ((flags = fcntl (fd, F_GETFL, 0)) < 0) 15758314Sache return -1; 15858314Sache 15958314Sache bflags = 0; 16058314Sache 16158314Sache#ifdef O_NONBLOCK 16258314Sache bflags |= O_NONBLOCK; 16358314Sache#endif 16458314Sache 16558314Sache#ifdef O_NDELAY 16658314Sache bflags |= O_NDELAY; 16758314Sache#endif 16858314Sache 16958314Sache if (flags & bflags) 17058314Sache { 17158314Sache flags &= ~bflags; 17258314Sache return (fcntl (fd, F_SETFL, flags)); 17358314Sache } 17458314Sache 17558314Sache return 0; 17658314Sache} 177