PageRenderTime 45ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/rlwrap-0.37/src/rlwrap.h

#
C Header | 541 lines | 395 code | 113 blank | 33 comment | 20 complexity | f6671d930b1cf26c5a51b25f9638cac2 MD5 | raw file
Possible License(s): GPL-2.0
  1. /* rlwrap.h: includes, definitions, declarations */
  2. /* This program is free software; you can redistribute it and/or modify
  3. it under the terms of the GNU General Public License as published by
  4. the Free Software Foundation; either version 2 of the License , or
  5. (at your option) any later version.
  6. This program is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. GNU General Public License for more details.
  10. You should have received a copy of the GNU General Public License
  11. along with this program; see the file COPYING. If not, write to
  12. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  13. You may contact the author by:
  14. e-mail: hlub@knoware.nl
  15. */
  16. #include "../config.h"
  17. #include <sys/types.h>
  18. #if HAVE_SYS_WAIT_H
  19. # include <sys/wait.h>
  20. #endif
  21. #ifndef WEXITSTATUS
  22. # define WEXITSTATUS(stat_val) ((unsigned)(stat_val) >> 8)
  23. #endif
  24. #include <sys/stat.h>
  25. #include <sys/ioctl.h>
  26. #include <sys/resource.h>
  27. #include <fcntl.h>
  28. #include <unistd.h>
  29. #include <signal.h>
  30. #include <stdio.h>
  31. #include <ctype.h>
  32. #include <inttypes.h> /* stdint.h is not on AIX, inttypes.h is in ISO C 1999 */
  33. #include <errno.h>
  34. #include <stdarg.h>
  35. /* #define __USE_XOPEN
  36. #define __USE_GNU */
  37. #include <stdlib.h>
  38. #include <sched.h>
  39. #if TIME_WITH_SYS_TIME
  40. # include <sys/time.h>
  41. # include <time.h>
  42. #else
  43. # if HAVE_SYS_TIME_H
  44. # include <sys/time.h>
  45. # else
  46. # include <time.h>
  47. # endif
  48. #endif
  49. #ifdef HAVE_GETOPT_H
  50. # include <getopt.h>
  51. #endif
  52. #ifdef HAVE_CURSES_H
  53. # include <curses.h>
  54. # ifdef HAVE_TERM_H
  55. # include <term.h>
  56. # else
  57. # ifdef HAVE_NCURSES_TERM_H /* cygwin? AIX? */
  58. # include <ncurses/term.h>
  59. # endif
  60. # endif
  61. #else
  62. # ifdef HAVE_TERMCAP_H
  63. # include <termcap.h>
  64. # endif
  65. #endif
  66. #include <termios.h>
  67. #ifdef HAVE_REGEX_H
  68. # include <regex.h>
  69. #endif
  70. #if STDC_HEADERS
  71. # include <string.h>
  72. #else
  73. # ifndef HAVE_STRRCHR
  74. # define strrchr rindex
  75. # endif
  76. char *strchr(), *strrchr();
  77. # ifndef HAVE_MEMMOVE
  78. # define memmove(d, s, n) bcopy ((s), (d), (n))
  79. # endif
  80. #endif
  81. #ifdef HAVE_PTY_H
  82. # include <pty.h>
  83. #endif
  84. #ifdef HAVE_LIBUTIL_H
  85. # include <libutil.h>
  86. #endif
  87. #ifdef HAVE_UTIL_H
  88. # include <util.h>
  89. #endif
  90. #define BUFFSIZE 512
  91. #ifndef MAXPATHLEN
  92. #define MAXPATHLEN 512
  93. #endif
  94. #ifdef HAVE_SNPRINTF /* don't rely on the compiler understanding variadic macros */
  95. # define snprintf0(buf,bufsize,format) snprintf(buf,bufsize,format)
  96. # define snprintf1(buf,bufsize,format,arg1) snprintf(buf,bufsize,format,arg1)
  97. # define snprintf2(buf,bufsize,format,arg1,arg2) snprintf(buf,bufsize,format,arg1,arg2)
  98. #else
  99. # define snprintf0(buf,bufsize,format) sprintf(buf,format)
  100. # define snprintf1(buf,bufsize,format,arg1) sprintf(buf,format,arg1)
  101. # define snprintf2(buf,bufsize,format,arg1,arg2) sprintf(buf,format,arg1,arg2)
  102. # define vsnprintf(buf,bufsize,format,ap) vsprintf(buf,format,ap)
  103. #endif
  104. #ifndef HAVE_STRNLEN
  105. # define strnlen(s,l) strlen(s)
  106. #endif
  107. #include <readline/readline.h>
  108. #include <readline/history.h>
  109. #ifndef HAVE_RL_VARIABLE_VALUE
  110. # define rl_variable_value(s) "off"
  111. #endif
  112. #ifndef HAVE_RL_READLINE_VERSION
  113. # define rl_readline_version 0xbaddef
  114. #endif
  115. #if defined(SPY_ON_READLINE)
  116. extern int _rl_eof_char; /* Spying on readline's private life .... */
  117. extern int _rl_horizontal_scroll_mode;
  118. # if !defined(HOMEGROWN_REDISPLAY)
  119. # define MAYBE_MULTILINE 1
  120. # endif
  121. #else
  122. # define _rl_eof_char 0;
  123. #endif
  124. #ifdef MAYBE_MULTILINE
  125. # define redisplay_multiple_lines (!_rl_horizontal_scroll_mode)
  126. #else
  127. # define redisplay_multiple_lines (strncmp(rl_variable_value("horizontal-scroll-mode"),"off",3) == 0)
  128. #endif
  129. /* in main.c: */
  130. extern int master_pty_fd;
  131. extern int slave_pty_fd;
  132. extern FILE *debug_fp;
  133. extern char *program_name, *command_name;
  134. extern int always_readline;
  135. extern int complete_filenames;
  136. extern pid_t command_pid;
  137. extern char *command_line;
  138. extern int i_am_child;
  139. extern int nowarn;
  140. extern int debug;
  141. extern char *password_prompt_search_string;
  142. extern int one_shot_rlwrap;
  143. extern char *substitute_prompt;
  144. extern char *history_format;
  145. extern char *forget_regexp;
  146. extern char *prompt_regexp;
  147. extern int renice;
  148. extern int ignore_queued_input;
  149. extern int history_duplicate_avoidance_policy;
  150. extern int pass_on_sigINT_as_sigTERM;
  151. /* now follow the possible values for history_duplicate_avoidance_policy: */
  152. #define KEEP_ALL_DOUBLES 0
  153. #define ELIMINATE_SUCCESIVE_DOUBLES 1
  154. #define ELIMINATE_ALL_DOUBLES 2
  155. extern int one_shot_rlwrap;
  156. extern int ansi_colour_aware;
  157. extern int colour_the_prompt;
  158. extern int received_WINCH;
  159. extern int prompt_is_still_uncooked;
  160. extern int impatient_prompt;
  161. extern int we_just_got_a_signal_or_EOF;
  162. extern int remember_for_completion;
  163. extern int commands_children_not_wrapped;
  164. extern int accepted_lines;
  165. void cleanup_rlwrap_and_exit(int status);
  166. void put_in_output_queue(char *stuff);
  167. int output_queue_is_nonempty(void);
  168. void flush_output_queue(void);
  169. /* in readline.c: */
  170. extern struct rl_state
  171. { /* struct to save readline state while we're processing output from slave command*/
  172. char *input_buffer; /* current input buffer */
  173. char *raw_prompt; /* current prompt */
  174. char *cooked_prompt; /* ditto redefined by user, or with colour added */
  175. int point; /* cursor position within input buffer */
  176. int already_saved; /* flag set when saved, cleared when restored */
  177. } saved_rl_state;
  178. void save_rl_state(void);
  179. void restore_rl_state(void);
  180. void init_readline(char *);
  181. void my_redisplay(void);
  182. void initialise_colour_codes(char *colour);
  183. void reprint_prompt(int coloured);
  184. char *colourise (const char *prompt);
  185. void move_cursor_to_start_of_prompt(int erase);
  186. #define ERASE 1
  187. #define DONT_ERASE 0
  188. int prompt_is_single_line(void);
  189. char *process_new_output(const char* buffer, struct rl_state* state);
  190. int cook_prompt_if_necessary (void);
  191. extern int within_line_edit, transparent;
  192. extern char *multiline_separator;
  193. extern char *pre_given;
  194. extern int leave_prompt_alone;
  195. /* in signals.c */
  196. extern int command_is_dead;
  197. extern int commands_exit_status;
  198. extern int filter_is_dead;
  199. extern int filters_exit_status;
  200. extern int sigterm_received;
  201. extern int deferred_adapt_commands_window_size;
  202. extern int signal_handlers_were_installed;
  203. extern int received_sigALRM;
  204. #ifndef RETSIGTYPE
  205. #define RETSIGTYPE void /* systems where RETSIGTYPE = int have died out, apparently */
  206. #endif
  207. typedef RETSIGTYPE (*sighandler_type)(int);
  208. void mysignal(int sig, sighandler_type handler);
  209. void install_signal_handlers(void);
  210. void block_signals(int *sigs);
  211. void unblock_signals(int *sigs);
  212. void block_all_passed_on_signals(void);
  213. void block_all_signals(void);
  214. void unblock_all_signals(void);
  215. void ignore_sigchld(void);
  216. void suicide_by(int sig, int status);
  217. int adapt_tty_winsize(int from_fd, int to_fd);
  218. void myalarm(int msec);
  219. void handle_sigALRM(int signo);
  220. char *signal_name(int signal);
  221. /* in utils.c */
  222. void yield(void);
  223. void zero_select_timeout(void);
  224. int my_pselect(int n, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, const struct timespec *ptimeout_ts, const sigset_t *sigmask);
  225. struct termios *my_tcgetattr(int fd, char *which);
  226. int read_patiently(int fd, void *buffer, int count, char *whence);
  227. int write_patiently(int fd, const void *buffer, int count, const char *whither);
  228. void read_patiently2(int fd, void *buffer, int count, int uninterruptible_msec, const char *whence);
  229. void write_patiently2(int fd, const void *buffer, int count, int uninterruptible_msec, const char *whither);
  230. void mysetenv(const char *name, const char *value);
  231. void set_ulimit(int resource, long value);
  232. void usage(int status);
  233. void mywarn(const char *message, ...);
  234. void myerror(const char *message, ...);
  235. void *mymalloc(size_t size);
  236. void myfree(void *ptr);
  237. void mysetsid(void);
  238. void close_open_files_without_writing_buffers(void);
  239. size_t filesize(const char *filename);
  240. void open_logfile(const char *filename);
  241. void write_logfile(const char *str);
  242. void close_logfile(void);
  243. void timestamp(char *buf, int size);
  244. int killed_by(int status);
  245. void change_working_directory(void);
  246. void log_terminal_settings(struct termios *terminal_settings);
  247. void log_fd_info(int fd);
  248. void last_minute_checks(void);
  249. void mymicrosleep(int msec);
  250. void do_nothing(int unused);
  251. extern char slaves_working_directory[];
  252. /* in string_utils.c */
  253. char *mybasename(char *filename);
  254. char *mydirname(char *filename);
  255. void mystrlcpy(char *dst, const char *src, size_t size);
  256. void mystrlcat(char *dst, const char *src, size_t size);
  257. char *mystrstr(const char *haystack, const char *needle);
  258. char *mysavestring(const char *string);
  259. char *add3strings(const char *str1, const char *str2, const char *str3);
  260. #define add2strings(a,b) add3strings(a,b,"")
  261. char *mystrtok(const char *s, const char *delim);
  262. char **split_with(const char *string, const char *delim);
  263. char **split_on_single_char(const char *string, char c);
  264. int scan_metacharacters(const char* string, const char *metacharacters);
  265. char **list4 (char *el0, char *el1, char *el2, char *el3);
  266. void free_splitlist (char **list);
  267. char *append_and_free_old(char *str1, const char *str2);
  268. char *mangle_char_for_debug_log(char c, int quote_me);
  269. char *mangle_string_for_debug_log(const char *string, int maxlen);
  270. char *mangle_buffer_for_debug_log(const char *buffer, int length);
  271. char *search_and_replace(char *patt, char *repl, const char *string,
  272. int cursorpos, int *line, int *col);
  273. char *first_of(char **strings);
  274. char *as_string(int i);
  275. char *append_and_expand_history_format(char *line);
  276. void remove_padding_and_terminate(char *buf, int length);
  277. void unbackspace(char* buf);
  278. void test_unbackspace (const char *input, const char *expected_result);
  279. char *mark_invisible(const char *buf);
  280. char *copy_and_unbackspace(const char *original);
  281. int colourless_strlen(const char *str, char **pcopy_without_ignore_markers, int termwidth);
  282. int colourless_strlen_unmarked (const char *str, int termwidth);
  283. char *get_last_screenline(char *long_line, int termwidth);
  284. char *lowercase(const char *str);
  285. char *colour_name_to_ansi_code(const char *colour_name);
  286. int match_regexp(const char *string, const char *regexp, int case_insensitive);
  287. /* in pty.c: */
  288. pid_t my_pty_fork(int *, const struct termios *, const struct winsize *);
  289. int slave_is_in_raw_mode(void);
  290. struct termios *get_pterm_slave(void);
  291. void mirror_slaves_echo_mode(void);
  292. void completely_mirror_slaves_terminal_settings(void);
  293. void completely_mirror_slaves_output_settings(void);
  294. void write_EOF_to_master_pty(void);
  295. void write_EOL_to_master_pty(char *);
  296. int dont_wrap_command_waits(void);
  297. int skip_rlwrap(void);
  298. /* in ptytty.c: */
  299. int ptytty_get_pty(int *fd_tty, const char **ttydev);
  300. int ptytty_get_tty(const char *ttydev);
  301. int ptytty_control_tty(int fd_tty, const char *ttydev);
  302. int ptytty_openpty(int *amaster, int *aslave, const char **name);
  303. /* in completion.rb: */
  304. void init_completer(void);
  305. void feed_file_into_completion_list(const char *completions_file);
  306. void feed_line_into_completion_list(const char *line);
  307. void add_word_to_completions(const char *word);
  308. void remove_word_from_completions(const char *word);
  309. char *my_completion_function(char *prefix, int state);
  310. extern int completion_is_case_sensitive;
  311. /* in term.c: */
  312. extern int redisplay; /* TRUE when user input should be readable (instead of *******) */
  313. void init_terminal(void);
  314. void set_echo(int);
  315. void prepare_terminal(void);
  316. void cr(void);
  317. void backspace(int);
  318. void clear_line(void);
  319. void clear_the_screen(void);
  320. void curs_up(void);
  321. void curs_down(void);
  322. void test_terminal(void);
  323. int my_putchar(TPUTS_PUTC_ARGTYPE c);
  324. void my_putstr(const char *string);
  325. int cursor_hpos(int col);
  326. extern struct termios saved_terminal_settings;
  327. extern int terminal_settings_saved;
  328. extern struct winsize winsize;
  329. extern char *term_name;
  330. extern char *term_backspace, term_eof, term_stop, *term_cursor_hpos,
  331. *term_cursor_up, *term_cursor_down;
  332. extern int newline_came_last;
  333. /* in filter.c */
  334. #define TAG_INPUT 0
  335. #define TAG_OUTPUT 1
  336. #define TAG_HISTORY 2
  337. #define TAG_COMPLETION 3
  338. #define TAG_PROMPT 4
  339. #define TAG_IGNORE 251
  340. #define TAG_ADD_TO_COMPLETION_LIST 252
  341. #define TAG_REMOVE_FROM_COMPLETION_LIST 253
  342. #define TAG_OUTPUT_OUT_OF_BAND 254
  343. #define TAG_ERROR 255
  344. #define out_of_band(tag) (tag & 128)
  345. extern pid_t filter_pid;
  346. extern int filter_is_dead;
  347. void spawn_filter(const char *filter_command);
  348. void kill_filter(void);
  349. char *pass_through_filter(int tag, const char *buffer);
  350. char *filters_last_words(void);
  351. void filter_test(void);
  352. /* some handy macros */
  353. #ifndef TRUE
  354. # define TRUE 1
  355. #endif
  356. #ifndef FALSE
  357. # define FALSE 0
  358. #endif
  359. #ifndef min
  360. # define min(a,b) ((a) < (b) ? (a) : (b))
  361. #endif
  362. #ifndef max
  363. # define max(a,b) ((a) < (b) ? (b) : (a))
  364. #endif
  365. #include "malloc_debug.h" /* malloc_debug.{c,h} not ready for prime time */
  366. #define DEBUG_FILENAME "/tmp/rlwrap.debug"
  367. #define KA_BOOM {char *p = (char *) 1; *p = 'c';} /* dump core right here */
  368. #define KA_SCRUNCH {volatile int x=1, y=0; x = x/y;} /* force a SIGFPE */
  369. #define KA_SCREECH kill(getpid(),SIGTRAP); /* enter the debugger - use it to set (conditional) breakpoints from within C code: if (condition) KA_SCREECH; */
  370. /* DPRINTF0 and its ilk doesn't produce any output except when DEBUG is #defined (via --enable-debug configure option) */
  371. #ifdef DEBUG
  372. # define DEBUG_TERMIO 1
  373. # define DEBUG_SIGNALS 2
  374. # define DEBUG_READLINE 4
  375. # define DEBUG_MEMORY_MANAGEMENT 8 /* used with malloc_debug.c */
  376. # define DEBUG_FILTERING 16
  377. # define DEBUG_AD_HOC 64 /* only used during rlwrap development */
  378. # define DEBUG_WITH_TIMESTAMPS 128 /* add timestamps to every line in debug log */
  379. # define FORCE_HOMEGROWN_REDISPLAY 256 /* force use of my_homegrown_redisplay() */
  380. # define DEBUG_LONG_STRINGS 512 /* log all strings completely, however long they are */
  381. # define DEBUG_RACES 1024 /* introduce random delays */
  382. # define DEBUG_RANDOM_FAIL 2048 /* fail tests randomly */
  383. # define DEBUG_TEST_MAIN 4096 /* run test_main and exit */
  384. # define DEBUG_MAX DEBUG_TEST_MAIN
  385. # define MANGLE_LENGTH ((debug_saved & DEBUG_LONG_STRINGS) ? 0 : 20) /* debug_saved is defined within DPRINTF macro */
  386. # define DEBUG_DEFAULT (DEBUG_TERMIO | DEBUG_SIGNALS | DEBUG_READLINE)
  387. # define DEBUG_ALL (2*DEBUG_MAX-1)
  388. # ifndef __GNUC__
  389. # define __FUNCTION__ ""
  390. # endif
  391. # define WHERE_AND_WHEN \
  392. int debug_saved = debug; char file_line[100], when[100]; \
  393. if(debug & DEBUG_WITH_TIMESTAMPS) timestamp(when, sizeof(when)); else *when='\0'; \
  394. debug = 0; /* don't debug while evaluating the DPRINTF arguments */ \
  395. snprintf2(file_line, sizeof(file_line),"%.15s:%d:",__FILE__,__LINE__); \
  396. fprintf(debug_fp, "%-20s %s %-25.25s ", file_line, when, __FUNCTION__);\
  397. # define NL_AND_FLUSH fputc('\n', debug_fp) ; fflush(debug_fp); debug = debug_saved;
  398. # define DPRINTF0(mask, format) \
  399. if ((debug & mask) && debug_fp) {WHERE_AND_WHEN; fprintf(debug_fp, format); NL_AND_FLUSH; }
  400. # define DPRINTF1(mask, format,arg) \
  401. if ((debug & mask) && debug_fp) {WHERE_AND_WHEN; fprintf(debug_fp, format, arg); NL_AND_FLUSH; }
  402. # define DPRINTF2(mask, format,arg1, arg2) \
  403. if ((debug & mask) && debug_fp) {WHERE_AND_WHEN; fprintf(debug_fp, format, arg1, arg2); NL_AND_FLUSH; }
  404. # define DPRINTF3(mask, format,arg1, arg2, arg3) \
  405. if ((debug & mask) && debug_fp) {WHERE_AND_WHEN; fprintf(debug_fp, format, arg1, arg2, arg3); NL_AND_FLUSH; }
  406. # define DPRINTF4(mask, format,arg1, arg2, arg3, arg4) \
  407. if ((debug & mask) && debug_fp) {WHERE_AND_WHEN; fprintf(debug_fp, format, arg1, arg2, arg3,arg4); NL_AND_FLUSH; }
  408. # define ERRMSG(b) (b && (errno != 0) ? add3strings("(", strerror(errno), ")") : "" )
  409. # define SHOWCURSOR(c) if (debug & DEBUG_READLINE) {my_putchar(c); mymicrosleep(800); backspace(1);} /* (may work incorrectly at last column!)*/
  410. # define DEBUG_RANDOM_SLEEP if (debug & DEBUG_RACES) {int sleeptime=rand()&31; DPRINTF1(DEBUG_RACES,"sleeping for %d msecs", sleeptime); mymicrosleep(sleeptime);}
  411. #else
  412. # define HEAVEN_FORBID(exp) (exp)
  413. # define OH_PLEASE(exp) (exp)
  414. # define MANGLE_LENGTH 0
  415. # define DPRINTF0(mask, format)
  416. # define DPRINTF1(mask, format,arg)
  417. # define DPRINTF2(mask, format,arg1, arg2)
  418. # define DPRINTF3(mask, format,arg1, arg2, arg3)
  419. # define DPRINTF4(mask, format,arg1, arg2, arg3, arg4)
  420. # define ERRMSG(b)
  421. # define SHOWCURSOR
  422. # define NDEBUG /* disable assertions */
  423. # define DEBUG_RANDOM_SLEEP
  424. #endif
  425. #include <assert.h>