1*0a6a1f1dSLionel Sambuc /* $NetBSD: histedit.h,v 1.53 2014/06/18 18:12:28 christos Exp $ */ 23e1db26aSLionel Sambuc 33e1db26aSLionel Sambuc /*- 43e1db26aSLionel Sambuc * Copyright (c) 1992, 1993 53e1db26aSLionel Sambuc * The Regents of the University of California. All rights reserved. 63e1db26aSLionel Sambuc * 73e1db26aSLionel Sambuc * This code is derived from software contributed to Berkeley by 83e1db26aSLionel Sambuc * Christos Zoulas of Cornell University. 93e1db26aSLionel Sambuc * 103e1db26aSLionel Sambuc * Redistribution and use in source and binary forms, with or without 113e1db26aSLionel Sambuc * modification, are permitted provided that the following conditions 123e1db26aSLionel Sambuc * are met: 133e1db26aSLionel Sambuc * 1. Redistributions of source code must retain the above copyright 143e1db26aSLionel Sambuc * notice, this list of conditions and the following disclaimer. 153e1db26aSLionel Sambuc * 2. Redistributions in binary form must reproduce the above copyright 163e1db26aSLionel Sambuc * notice, this list of conditions and the following disclaimer in the 173e1db26aSLionel Sambuc * documentation and/or other materials provided with the distribution. 183e1db26aSLionel Sambuc * 3. Neither the name of the University nor the names of its contributors 193e1db26aSLionel Sambuc * may be used to endorse or promote products derived from this software 203e1db26aSLionel Sambuc * without specific prior written permission. 213e1db26aSLionel Sambuc * 223e1db26aSLionel Sambuc * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 233e1db26aSLionel Sambuc * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 243e1db26aSLionel Sambuc * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 253e1db26aSLionel Sambuc * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 263e1db26aSLionel Sambuc * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 273e1db26aSLionel Sambuc * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 283e1db26aSLionel Sambuc * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 293e1db26aSLionel Sambuc * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 303e1db26aSLionel Sambuc * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 313e1db26aSLionel Sambuc * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 323e1db26aSLionel Sambuc * SUCH DAMAGE. 333e1db26aSLionel Sambuc * 343e1db26aSLionel Sambuc * @(#)histedit.h 8.2 (Berkeley) 1/3/94 353e1db26aSLionel Sambuc */ 363e1db26aSLionel Sambuc 373e1db26aSLionel Sambuc /* 383e1db26aSLionel Sambuc * histedit.h: Line editor and history interface. 393e1db26aSLionel Sambuc */ 403e1db26aSLionel Sambuc #ifndef _HISTEDIT_H_ 413e1db26aSLionel Sambuc #define _HISTEDIT_H_ 423e1db26aSLionel Sambuc 433e1db26aSLionel Sambuc #define LIBEDIT_MAJOR 2 443e1db26aSLionel Sambuc #define LIBEDIT_MINOR 11 453e1db26aSLionel Sambuc 463e1db26aSLionel Sambuc #include <sys/types.h> 473e1db26aSLionel Sambuc #include <stdio.h> 483e1db26aSLionel Sambuc 493e1db26aSLionel Sambuc #ifdef __cplusplus 503e1db26aSLionel Sambuc extern "C" { 513e1db26aSLionel Sambuc #endif 523e1db26aSLionel Sambuc 533e1db26aSLionel Sambuc /* 543e1db26aSLionel Sambuc * ==== Editing ==== 553e1db26aSLionel Sambuc */ 563e1db26aSLionel Sambuc 573e1db26aSLionel Sambuc typedef struct editline EditLine; 583e1db26aSLionel Sambuc 593e1db26aSLionel Sambuc /* 603e1db26aSLionel Sambuc * For user-defined function interface 613e1db26aSLionel Sambuc */ 623e1db26aSLionel Sambuc typedef struct lineinfo { 633e1db26aSLionel Sambuc const char *buffer; 643e1db26aSLionel Sambuc const char *cursor; 653e1db26aSLionel Sambuc const char *lastchar; 663e1db26aSLionel Sambuc } LineInfo; 673e1db26aSLionel Sambuc 683e1db26aSLionel Sambuc /* 693e1db26aSLionel Sambuc * EditLine editor function return codes. 703e1db26aSLionel Sambuc * For user-defined function interface 713e1db26aSLionel Sambuc */ 723e1db26aSLionel Sambuc #define CC_NORM 0 733e1db26aSLionel Sambuc #define CC_NEWLINE 1 743e1db26aSLionel Sambuc #define CC_EOF 2 753e1db26aSLionel Sambuc #define CC_ARGHACK 3 763e1db26aSLionel Sambuc #define CC_REFRESH 4 773e1db26aSLionel Sambuc #define CC_CURSOR 5 783e1db26aSLionel Sambuc #define CC_ERROR 6 793e1db26aSLionel Sambuc #define CC_FATAL 7 803e1db26aSLionel Sambuc #define CC_REDISPLAY 8 813e1db26aSLionel Sambuc #define CC_REFRESH_BEEP 9 823e1db26aSLionel Sambuc 833e1db26aSLionel Sambuc /* 843e1db26aSLionel Sambuc * Initialization, cleanup, and resetting 853e1db26aSLionel Sambuc */ 863e1db26aSLionel Sambuc EditLine *el_init(const char *, FILE *, FILE *, FILE *); 8784d9c625SLionel Sambuc EditLine *el_init_fd(const char *, FILE *, FILE *, FILE *, 8884d9c625SLionel Sambuc int, int, int); 893e1db26aSLionel Sambuc void el_end(EditLine *); 903e1db26aSLionel Sambuc void el_reset(EditLine *); 913e1db26aSLionel Sambuc 923e1db26aSLionel Sambuc /* 933e1db26aSLionel Sambuc * Get a line, a character or push a string back in the input queue 943e1db26aSLionel Sambuc */ 953e1db26aSLionel Sambuc const char *el_gets(EditLine *, int *); 963e1db26aSLionel Sambuc int el_getc(EditLine *, char *); 973e1db26aSLionel Sambuc void el_push(EditLine *, const char *); 983e1db26aSLionel Sambuc 993e1db26aSLionel Sambuc /* 1003e1db26aSLionel Sambuc * Beep! 1013e1db26aSLionel Sambuc */ 1023e1db26aSLionel Sambuc void el_beep(EditLine *); 1033e1db26aSLionel Sambuc 1043e1db26aSLionel Sambuc /* 1053e1db26aSLionel Sambuc * High level function internals control 1063e1db26aSLionel Sambuc * Parses argc, argv array and executes builtin editline commands 1073e1db26aSLionel Sambuc */ 1083e1db26aSLionel Sambuc int el_parse(EditLine *, int, const char **); 1093e1db26aSLionel Sambuc 1103e1db26aSLionel Sambuc /* 1113e1db26aSLionel Sambuc * Low level editline access functions 1123e1db26aSLionel Sambuc */ 1133e1db26aSLionel Sambuc int el_set(EditLine *, int, ...); 1143e1db26aSLionel Sambuc int el_get(EditLine *, int, ...); 1153e1db26aSLionel Sambuc unsigned char _el_fn_complete(EditLine *, int); 1163e1db26aSLionel Sambuc 1173e1db26aSLionel Sambuc /* 1183e1db26aSLionel Sambuc * el_set/el_get parameters 1193e1db26aSLionel Sambuc * 1203e1db26aSLionel Sambuc * When using el_wset/el_wget (as opposed to el_set/el_get): 1213e1db26aSLionel Sambuc * Char is wchar_t, otherwise it is char. 1223e1db26aSLionel Sambuc * prompt_func is el_wpfunc_t, otherwise it is el_pfunc_t . 1233e1db26aSLionel Sambuc 1243e1db26aSLionel Sambuc * Prompt function prototypes are: 1253e1db26aSLionel Sambuc * typedef char *(*el_pfunct_t) (EditLine *); 1263e1db26aSLionel Sambuc * typedef wchar_t *(*el_wpfunct_t) (EditLine *); 1273e1db26aSLionel Sambuc * 1283e1db26aSLionel Sambuc * For operations that support set or set/get, the argument types listed are for 1293e1db26aSLionel Sambuc * the "set" operation. For "get", each listed type must be a pointer. 1303e1db26aSLionel Sambuc * E.g. EL_EDITMODE takes an int when set, but an int* when get. 1313e1db26aSLionel Sambuc * 1323e1db26aSLionel Sambuc * Operations that only support "get" have the correct argument types listed. 1333e1db26aSLionel Sambuc */ 1343e1db26aSLionel Sambuc #define EL_PROMPT 0 /* , prompt_func); set/get */ 1353e1db26aSLionel Sambuc #define EL_TERMINAL 1 /* , const char *); set/get */ 1363e1db26aSLionel Sambuc #define EL_EDITOR 2 /* , const Char *); set/get */ 1373e1db26aSLionel Sambuc #define EL_SIGNAL 3 /* , int); set/get */ 1383e1db26aSLionel Sambuc #define EL_BIND 4 /* , const Char *, ..., NULL); set */ 1393e1db26aSLionel Sambuc #define EL_TELLTC 5 /* , const Char *, ..., NULL); set */ 1403e1db26aSLionel Sambuc #define EL_SETTC 6 /* , const Char *, ..., NULL); set */ 1413e1db26aSLionel Sambuc #define EL_ECHOTC 7 /* , const Char *, ..., NULL); set */ 1423e1db26aSLionel Sambuc #define EL_SETTY 8 /* , const Char *, ..., NULL); set */ 1433e1db26aSLionel Sambuc #define EL_ADDFN 9 /* , const Char *, const Char, set */ 1443e1db26aSLionel Sambuc /* el_func_t); */ 1453e1db26aSLionel Sambuc #define EL_HIST 10 /* , hist_fun_t, const void *); set */ 1463e1db26aSLionel Sambuc #define EL_EDITMODE 11 /* , int); set/get */ 1473e1db26aSLionel Sambuc #define EL_RPROMPT 12 /* , prompt_func); set/get */ 1483e1db26aSLionel Sambuc #define EL_GETCFN 13 /* , el_rfunc_t); set/get */ 1493e1db26aSLionel Sambuc #define EL_CLIENTDATA 14 /* , void *); set/get */ 1503e1db26aSLionel Sambuc #define EL_UNBUFFERED 15 /* , int); set/get */ 1513e1db26aSLionel Sambuc #define EL_PREP_TERM 16 /* , int); set */ 1523e1db26aSLionel Sambuc #define EL_GETTC 17 /* , const Char *, ..., NULL); get */ 1533e1db26aSLionel Sambuc #define EL_GETFP 18 /* , int, FILE **); get */ 1543e1db26aSLionel Sambuc #define EL_SETFP 19 /* , int, FILE *); set */ 1553e1db26aSLionel Sambuc #define EL_REFRESH 20 /* , void); set */ 1563e1db26aSLionel Sambuc #define EL_PROMPT_ESC 21 /* , prompt_func, Char); set/get */ 1573e1db26aSLionel Sambuc #define EL_RPROMPT_ESC 22 /* , prompt_func, Char); set/get */ 1583e1db26aSLionel Sambuc #define EL_RESIZE 23 /* , el_zfunc_t, void *); set */ 159*0a6a1f1dSLionel Sambuc #define EL_ALIAS_TEXT 24 /* , el_afunc_t, void *); set */ 1603e1db26aSLionel Sambuc 1613e1db26aSLionel Sambuc #define EL_BUILTIN_GETCFN (NULL) 1623e1db26aSLionel Sambuc 1633e1db26aSLionel Sambuc /* 1643e1db26aSLionel Sambuc * Source named file or $PWD/.editrc or $HOME/.editrc 1653e1db26aSLionel Sambuc */ 1663e1db26aSLionel Sambuc int el_source(EditLine *, const char *); 1673e1db26aSLionel Sambuc 1683e1db26aSLionel Sambuc /* 1693e1db26aSLionel Sambuc * Must be called when the terminal changes size; If EL_SIGNAL 1703e1db26aSLionel Sambuc * is set this is done automatically otherwise it is the responsibility 1713e1db26aSLionel Sambuc * of the application 1723e1db26aSLionel Sambuc */ 1733e1db26aSLionel Sambuc void el_resize(EditLine *); 1743e1db26aSLionel Sambuc 1753e1db26aSLionel Sambuc /* 1763e1db26aSLionel Sambuc * User-defined function interface. 1773e1db26aSLionel Sambuc */ 1783e1db26aSLionel Sambuc const LineInfo *el_line(EditLine *); 1793e1db26aSLionel Sambuc int el_insertstr(EditLine *, const char *); 1803e1db26aSLionel Sambuc void el_deletestr(EditLine *, int); 1813e1db26aSLionel Sambuc 1823e1db26aSLionel Sambuc 1833e1db26aSLionel Sambuc /* 1843e1db26aSLionel Sambuc * ==== History ==== 1853e1db26aSLionel Sambuc */ 1863e1db26aSLionel Sambuc 1873e1db26aSLionel Sambuc typedef struct history History; 1883e1db26aSLionel Sambuc 1893e1db26aSLionel Sambuc typedef struct HistEvent { 1903e1db26aSLionel Sambuc int num; 1913e1db26aSLionel Sambuc const char *str; 1923e1db26aSLionel Sambuc } HistEvent; 1933e1db26aSLionel Sambuc 1943e1db26aSLionel Sambuc /* 1953e1db26aSLionel Sambuc * History access functions. 1963e1db26aSLionel Sambuc */ 1973e1db26aSLionel Sambuc History * history_init(void); 1983e1db26aSLionel Sambuc void history_end(History *); 1993e1db26aSLionel Sambuc 2003e1db26aSLionel Sambuc int history(History *, HistEvent *, int, ...); 2013e1db26aSLionel Sambuc 2023e1db26aSLionel Sambuc #define H_FUNC 0 /* , UTSL */ 2033e1db26aSLionel Sambuc #define H_SETSIZE 1 /* , const int); */ 2043e1db26aSLionel Sambuc #define H_GETSIZE 2 /* , void); */ 2053e1db26aSLionel Sambuc #define H_FIRST 3 /* , void); */ 2063e1db26aSLionel Sambuc #define H_LAST 4 /* , void); */ 2073e1db26aSLionel Sambuc #define H_PREV 5 /* , void); */ 2083e1db26aSLionel Sambuc #define H_NEXT 6 /* , void); */ 2093e1db26aSLionel Sambuc #define H_CURR 8 /* , const int); */ 2103e1db26aSLionel Sambuc #define H_SET 7 /* , int); */ 2113e1db26aSLionel Sambuc #define H_ADD 9 /* , const wchar_t *); */ 2123e1db26aSLionel Sambuc #define H_ENTER 10 /* , const wchar_t *); */ 2133e1db26aSLionel Sambuc #define H_APPEND 11 /* , const wchar_t *); */ 2143e1db26aSLionel Sambuc #define H_END 12 /* , void); */ 2153e1db26aSLionel Sambuc #define H_NEXT_STR 13 /* , const wchar_t *); */ 2163e1db26aSLionel Sambuc #define H_PREV_STR 14 /* , const wchar_t *); */ 2173e1db26aSLionel Sambuc #define H_NEXT_EVENT 15 /* , const int); */ 2183e1db26aSLionel Sambuc #define H_PREV_EVENT 16 /* , const int); */ 2193e1db26aSLionel Sambuc #define H_LOAD 17 /* , const char *); */ 2203e1db26aSLionel Sambuc #define H_SAVE 18 /* , const char *); */ 2213e1db26aSLionel Sambuc #define H_CLEAR 19 /* , void); */ 2223e1db26aSLionel Sambuc #define H_SETUNIQUE 20 /* , int); */ 2233e1db26aSLionel Sambuc #define H_GETUNIQUE 21 /* , void); */ 2243e1db26aSLionel Sambuc #define H_DEL 22 /* , int); */ 2253e1db26aSLionel Sambuc #define H_NEXT_EVDATA 23 /* , const int, histdata_t *); */ 2263e1db26aSLionel Sambuc #define H_DELDATA 24 /* , int, histdata_t *);*/ 2273e1db26aSLionel Sambuc #define H_REPLACE 25 /* , const char *, histdata_t); */ 228*0a6a1f1dSLionel Sambuc #define H_SAVE_FP 26 /* , FILE *); */ 2293e1db26aSLionel Sambuc 2303e1db26aSLionel Sambuc 2313e1db26aSLionel Sambuc 2323e1db26aSLionel Sambuc /* 2333e1db26aSLionel Sambuc * ==== Tokenization ==== 2343e1db26aSLionel Sambuc */ 2353e1db26aSLionel Sambuc 2363e1db26aSLionel Sambuc typedef struct tokenizer Tokenizer; 2373e1db26aSLionel Sambuc 2383e1db26aSLionel Sambuc /* 2393e1db26aSLionel Sambuc * String tokenization functions, using simplified sh(1) quoting rules 2403e1db26aSLionel Sambuc */ 2413e1db26aSLionel Sambuc Tokenizer *tok_init(const char *); 2423e1db26aSLionel Sambuc void tok_end(Tokenizer *); 2433e1db26aSLionel Sambuc void tok_reset(Tokenizer *); 2443e1db26aSLionel Sambuc int tok_line(Tokenizer *, const LineInfo *, 2453e1db26aSLionel Sambuc int *, const char ***, int *, int *); 2463e1db26aSLionel Sambuc int tok_str(Tokenizer *, const char *, 2473e1db26aSLionel Sambuc int *, const char ***); 2483e1db26aSLionel Sambuc 2493e1db26aSLionel Sambuc /* 2503e1db26aSLionel Sambuc * Begin Wide Character Support 2513e1db26aSLionel Sambuc */ 2523e1db26aSLionel Sambuc #ifdef __linux__ 2533e1db26aSLionel Sambuc /* Apparently we need _GNU_SOURCE defined to get access to wcsdup on Linux */ 2543e1db26aSLionel Sambuc #ifndef _GNU_SOURCE 2553e1db26aSLionel Sambuc #define _GNU_SOURCE 2563e1db26aSLionel Sambuc #endif 2573e1db26aSLionel Sambuc #endif 2583e1db26aSLionel Sambuc 2593e1db26aSLionel Sambuc #include <wchar.h> 2603e1db26aSLionel Sambuc #include <wctype.h> 2613e1db26aSLionel Sambuc 2623e1db26aSLionel Sambuc /* 2633e1db26aSLionel Sambuc * Wide character versions 2643e1db26aSLionel Sambuc */ 2653e1db26aSLionel Sambuc 2663e1db26aSLionel Sambuc /* 2673e1db26aSLionel Sambuc * ==== Editing ==== 2683e1db26aSLionel Sambuc */ 2693e1db26aSLionel Sambuc typedef struct lineinfow { 2703e1db26aSLionel Sambuc const wchar_t *buffer; 2713e1db26aSLionel Sambuc const wchar_t *cursor; 2723e1db26aSLionel Sambuc const wchar_t *lastchar; 2733e1db26aSLionel Sambuc } LineInfoW; 2743e1db26aSLionel Sambuc 2753e1db26aSLionel Sambuc const wchar_t *el_wgets(EditLine *, int *); 2763e1db26aSLionel Sambuc int el_wgetc(EditLine *, wchar_t *); 2773e1db26aSLionel Sambuc void el_wpush(EditLine *, const wchar_t *); 2783e1db26aSLionel Sambuc 2793e1db26aSLionel Sambuc int el_wparse(EditLine *, int, const wchar_t **); 2803e1db26aSLionel Sambuc 2813e1db26aSLionel Sambuc int el_wset(EditLine *, int, ...); 2823e1db26aSLionel Sambuc int el_wget(EditLine *, int, ...); 2833e1db26aSLionel Sambuc 28484d9c625SLionel Sambuc int el_cursor(EditLine *, int); 2853e1db26aSLionel Sambuc const LineInfoW *el_wline(EditLine *); 2863e1db26aSLionel Sambuc int el_winsertstr(EditLine *, const wchar_t *); 2873e1db26aSLionel Sambuc #define el_wdeletestr el_deletestr 2883e1db26aSLionel Sambuc 2893e1db26aSLionel Sambuc /* 2903e1db26aSLionel Sambuc * ==== History ==== 2913e1db26aSLionel Sambuc */ 2923e1db26aSLionel Sambuc typedef struct histeventW { 2933e1db26aSLionel Sambuc int num; 2943e1db26aSLionel Sambuc const wchar_t *str; 2953e1db26aSLionel Sambuc } HistEventW; 2963e1db26aSLionel Sambuc 2973e1db26aSLionel Sambuc typedef struct historyW HistoryW; 2983e1db26aSLionel Sambuc 2993e1db26aSLionel Sambuc HistoryW * history_winit(void); 3003e1db26aSLionel Sambuc void history_wend(HistoryW *); 3013e1db26aSLionel Sambuc 3023e1db26aSLionel Sambuc int history_w(HistoryW *, HistEventW *, int, ...); 3033e1db26aSLionel Sambuc 3043e1db26aSLionel Sambuc /* 3053e1db26aSLionel Sambuc * ==== Tokenization ==== 3063e1db26aSLionel Sambuc */ 3073e1db26aSLionel Sambuc typedef struct tokenizerW TokenizerW; 3083e1db26aSLionel Sambuc 3093e1db26aSLionel Sambuc /* Wide character tokenizer support */ 3103e1db26aSLionel Sambuc TokenizerW *tok_winit(const wchar_t *); 3113e1db26aSLionel Sambuc void tok_wend(TokenizerW *); 3123e1db26aSLionel Sambuc void tok_wreset(TokenizerW *); 3133e1db26aSLionel Sambuc int tok_wline(TokenizerW *, const LineInfoW *, 3143e1db26aSLionel Sambuc int *, const wchar_t ***, int *, int *); 3153e1db26aSLionel Sambuc int tok_wstr(TokenizerW *, const wchar_t *, 3163e1db26aSLionel Sambuc int *, const wchar_t ***); 3173e1db26aSLionel Sambuc 3183e1db26aSLionel Sambuc #ifdef __cplusplus 3193e1db26aSLionel Sambuc } 3203e1db26aSLionel Sambuc #endif 3213e1db26aSLionel Sambuc 3223e1db26aSLionel Sambuc #endif /* _HISTEDIT_H_ */ 323