shell.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. /* shell.c -- readline utility functions that are normally provided by
  2. bash when readline is linked as part of the shell. */
  3. /* Copyright (C) 1997-2009 Free Software Foundation, Inc.
  4. This file is part of the GNU Readline Library (Readline), a library
  5. for reading lines of text with interactive input and history editing.
  6. Readline is free software: you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation, either version 3 of the License, or
  9. (at your option) any later version.
  10. Readline is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with Readline. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #define READLINE_LIBRARY
  18. #if defined (HAVE_CONFIG_H)
  19. # include <config.h>
  20. #endif
  21. #include <sys/types.h>
  22. #if defined (HAVE_UNISTD_H)
  23. # include <unistd.h>
  24. #endif /* HAVE_UNISTD_H */
  25. #if defined (HAVE_STDLIB_H)
  26. # include <stdlib.h>
  27. #else
  28. # include "ansi_stdlib.h"
  29. #endif /* HAVE_STDLIB_H */
  30. #if defined (HAVE_STRING_H)
  31. # include <string.h>
  32. #else
  33. # include <strings.h>
  34. #endif /* !HAVE_STRING_H */
  35. #if defined (HAVE_LIMITS_H)
  36. # include <limits.h>
  37. #endif
  38. #if defined (HAVE_FCNTL_H)
  39. #include <fcntl.h>
  40. #endif
  41. #if defined (HAVE_PWD_H)
  42. #include <pwd.h>
  43. #endif
  44. #include <stdio.h>
  45. #include "rlstdc.h"
  46. #include "rlshell.h"
  47. #include "xmalloc.h"
  48. #if defined (HAVE_GETPWUID) && !defined (HAVE_GETPW_DECLS)
  49. extern struct passwd *getpwuid PARAMS((uid_t));
  50. #endif /* HAVE_GETPWUID && !HAVE_GETPW_DECLS */
  51. #ifndef NULL
  52. # define NULL 0
  53. #endif
  54. #ifndef CHAR_BIT
  55. # define CHAR_BIT 8
  56. #endif
  57. /* Nonzero if the integer type T is signed. */
  58. #define TYPE_SIGNED(t) (! ((t) 0 < (t) -1))
  59. /* Bound on length of the string representing an integer value of type T.
  60. Subtract one for the sign bit if T is signed;
  61. 302 / 1000 is log10 (2) rounded up;
  62. add one for integer division truncation;
  63. add one more for a minus sign if t is signed. */
  64. #define INT_STRLEN_BOUND(t) \
  65. ((sizeof (t) * CHAR_BIT - TYPE_SIGNED (t)) * 302 / 1000 \
  66. + 1 + TYPE_SIGNED (t))
  67. /* All of these functions are resolved from bash if we are linking readline
  68. as part of bash. */
  69. /* Does shell-like quoting using single quotes. */
  70. char *
  71. sh_single_quote (string)
  72. char *string;
  73. {
  74. register int c;
  75. char *result, *r, *s;
  76. result = (char *)xmalloc (3 + (4 * strlen (string)));
  77. r = result;
  78. *r++ = '\'';
  79. for (s = string; s && (c = *s); s++)
  80. {
  81. *r++ = c;
  82. if (c == '\'')
  83. {
  84. *r++ = '\\'; /* insert escaped single quote */
  85. *r++ = '\'';
  86. *r++ = '\''; /* start new quoted string */
  87. }
  88. }
  89. *r++ = '\'';
  90. *r = '\0';
  91. return (result);
  92. }
  93. /* Set the environment variables LINES and COLUMNS to lines and cols,
  94. respectively. */
  95. void
  96. sh_set_lines_and_columns (lines, cols)
  97. int lines, cols;
  98. {
  99. char *b;
  100. #if defined (HAVE_SETENV)
  101. b = (char *)xmalloc (INT_STRLEN_BOUND (int) + 1);
  102. sprintf (b, "%d", lines);
  103. setenv ("LINES", b, 1);
  104. xfree (b);
  105. b = (char *)xmalloc (INT_STRLEN_BOUND (int) + 1);
  106. sprintf (b, "%d", cols);
  107. setenv ("COLUMNS", b, 1);
  108. xfree (b);
  109. #else /* !HAVE_SETENV */
  110. # if defined (HAVE_PUTENV)
  111. b = (char *)xmalloc (INT_STRLEN_BOUND (int) + sizeof ("LINES=") + 1);
  112. sprintf (b, "LINES=%d", lines);
  113. putenv (b);
  114. b = (char *)xmalloc (INT_STRLEN_BOUND (int) + sizeof ("COLUMNS=") + 1);
  115. sprintf (b, "COLUMNS=%d", cols);
  116. putenv (b);
  117. # endif /* HAVE_PUTENV */
  118. #endif /* !HAVE_SETENV */
  119. }
  120. char *
  121. sh_get_env_value (varname)
  122. const char *varname;
  123. {
  124. return ((char *)getenv (varname));
  125. }
  126. char *
  127. sh_get_home_dir ()
  128. {
  129. char *home_dir;
  130. struct passwd *entry;
  131. home_dir = (char *)NULL;
  132. #if defined (HAVE_GETPWUID)
  133. entry = getpwuid (getuid ());
  134. if (entry)
  135. home_dir = entry->pw_dir;
  136. #endif
  137. return (home_dir);
  138. }
  139. #if !defined (O_NDELAY)
  140. # if defined (FNDELAY)
  141. # define O_NDELAY FNDELAY
  142. # endif
  143. #endif
  144. int
  145. sh_unset_nodelay_mode (fd)
  146. int fd;
  147. {
  148. #if defined (HAVE_FCNTL)
  149. int flags, bflags;
  150. if ((flags = fcntl (fd, F_GETFL, 0)) < 0)
  151. return -1;
  152. bflags = 0;
  153. #ifdef O_NONBLOCK
  154. bflags |= O_NONBLOCK;
  155. #endif
  156. #ifdef O_NDELAY
  157. bflags |= O_NDELAY;
  158. #endif
  159. if (flags & bflags)
  160. {
  161. flags &= ~bflags;
  162. return (fcntl (fd, F_SETFL, flags));
  163. }
  164. #endif
  165. return 0;
  166. }