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 |
---|
11 | as published by the Free Software Foundation; either version 1, or |
---|
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, |
---|
22 | 675 Mass Ave, Cambridge, MA 02139, USA. */ |
---|
23 | #define READLINE_LIBRARY |
---|
24 | |
---|
25 | #if defined (HAVE_CONFIG_H) |
---|
26 | # include <config.h> |
---|
27 | #endif |
---|
28 | |
---|
29 | #include <sys/types.h> |
---|
30 | |
---|
31 | #if defined (HAVE_UNISTD_H) |
---|
32 | # include <unistd.h> |
---|
33 | #endif /* HAVE_UNISTD_H */ |
---|
34 | |
---|
35 | #if defined (HAVE_STDLIB_H) |
---|
36 | # include <stdlib.h> |
---|
37 | #else |
---|
38 | # include "ansi_stdlib.h" |
---|
39 | #endif /* HAVE_STDLIB_H */ |
---|
40 | |
---|
41 | #if defined (HAVE_STRING_H) |
---|
42 | # include <string.h> |
---|
43 | #else |
---|
44 | # include <strings.h> |
---|
45 | #endif /* !HAVE_STRING_H */ |
---|
46 | |
---|
47 | #include <pwd.h> |
---|
48 | |
---|
49 | #if !defined (HAVE_GETPW_DECLS) |
---|
50 | extern struct passwd *getpwuid (); |
---|
51 | #endif /* !HAVE_GETPW_DECLS */ |
---|
52 | |
---|
53 | extern char *xmalloc (); |
---|
54 | |
---|
55 | /* All of these functions are resolved from bash if we are linking readline |
---|
56 | as part of bash. */ |
---|
57 | |
---|
58 | /* Does shell-like quoting using single quotes. */ |
---|
59 | char * |
---|
60 | single_quote (string) |
---|
61 | char *string; |
---|
62 | { |
---|
63 | register int c; |
---|
64 | char *result, *r, *s; |
---|
65 | |
---|
66 | result = (char *)xmalloc (3 + (3 * strlen (string))); |
---|
67 | r = result; |
---|
68 | *r++ = '\''; |
---|
69 | |
---|
70 | for (s = string; s && (c = *s); s++) |
---|
71 | { |
---|
72 | *r++ = c; |
---|
73 | |
---|
74 | if (c == '\'') |
---|
75 | { |
---|
76 | *r++ = '\\'; /* insert escaped single quote */ |
---|
77 | *r++ = '\''; |
---|
78 | *r++ = '\''; /* start new quoted string */ |
---|
79 | } |
---|
80 | } |
---|
81 | |
---|
82 | *r++ = '\''; |
---|
83 | *r = '\0'; |
---|
84 | |
---|
85 | return (result); |
---|
86 | } |
---|
87 | |
---|
88 | /* Set the environment variables LINES and COLUMNS to lines and cols, |
---|
89 | respectively. */ |
---|
90 | void |
---|
91 | set_lines_and_columns (lines, cols) |
---|
92 | int lines, cols; |
---|
93 | { |
---|
94 | char *b; |
---|
95 | |
---|
96 | #if defined (HAVE_PUTENV) |
---|
97 | b = xmalloc (24); |
---|
98 | sprintf (b, "LINES=%d", lines); |
---|
99 | putenv (b); |
---|
100 | b = xmalloc (24); |
---|
101 | sprintf (b, "COLUMNS=%d", cols); |
---|
102 | putenv (b); |
---|
103 | #else /* !HAVE_PUTENV */ |
---|
104 | # if defined (HAVE_SETENV) |
---|
105 | b = xmalloc (8); |
---|
106 | sprintf (b, "%d", lines); |
---|
107 | setenv ("LINES", b, 1); |
---|
108 | b = xmalloc (8); |
---|
109 | sprintf (b, "%d", cols); |
---|
110 | setenv ("COLUMNS", b, 1); |
---|
111 | # endif /* HAVE_SETENV */ |
---|
112 | #endif /* !HAVE_PUTENV */ |
---|
113 | } |
---|
114 | |
---|
115 | char * |
---|
116 | get_env_value (varname) |
---|
117 | char *varname; |
---|
118 | { |
---|
119 | return ((char *)getenv (varname)); |
---|
120 | } |
---|
121 | |
---|
122 | char * |
---|
123 | get_home_dir () |
---|
124 | { |
---|
125 | char *home_dir; |
---|
126 | struct passwd *entry; |
---|
127 | |
---|
128 | home_dir = (char *)NULL; |
---|
129 | entry = getpwuid (getuid ()); |
---|
130 | if (entry) |
---|
131 | home_dir = entry->pw_dir; |
---|
132 | return (home_dir); |
---|
133 | } |
---|