Guest User

ksh with wchar_t

a guest
May 30th, 2017
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Diff 51.98 KB | None | 0 0
  1. ksh-wchar_t (with a bug: you'll see nothing until you press Ctrl+L)
  2.  
  3. Index: emacs.c
  4. ===================================================================
  5. RCS file: /cvs/src/bin/ksh/emacs.c,v
  6. retrieving revision 1.67
  7. diff -u -p -U100 -r1.67 emacs.c
  8. --- emacs.c 12 May 2017 14:37:52 -0000  1.67
  9. +++ emacs.c 30 May 2017 10:56:27 -0000
  10. @@ -1,1265 +1,1408 @@
  11.  /* $OpenBSD: emacs.c,v 1.67 2017/05/12 14:37:52 schwarze Exp $ */
  12.  
  13.  /*
  14.   *  Emacs-like command line editing and history
  15.   *
  16.   *  created by Ron Natalie at BRL
  17.   *  modified by Doug Kingston, Doug Gwyn, and Lou Salkind
  18.   *  adapted to PD ksh by Eric Gisin
  19.   *
  20.   * partial rewrite by Marco Peereboom <marco@openbsd.org>
  21.   * under the same license
  22.   */
  23.  
  24.  #include "config.h"
  25.  #ifdef EMACS
  26.  
  27.  #include <sys/queue.h>
  28.  #include <sys/stat.h>
  29. +#include <sys/limits.h>
  30.  
  31.  #include <ctype.h>
  32.  #include <locale.h>
  33.  #include <stdio.h>
  34.  #include <stdlib.h>
  35.  #include <string.h>
  36. +#include <wchar.h>
  37. +#include <wctype.h>
  38.  
  39.  #include "sh.h"
  40.  #include "edit.h"
  41.  
  42.  static Area    aedit;
  43.  #define    AEDIT   &aedit      /* area for kill ring and macro defns */
  44.  
  45.  #define    CTRL(x)     ((x) == '?' ? 0x7F : (x) & 0x1F)    /* ASCII */
  46.  #define    UNCTRL(x)   ((x) == 0x7F ? '?' : (x) | 0x40)    /* ASCII */
  47.  
  48.  /* values returned by keyboard functions */
  49.  #define    KSTD    0
  50.  #define    KEOL    1       /* ^M, ^J */
  51.  #define    KINTR   2       /* ^G, ^C */
  52.  
  53.  struct x_ftab {
  54.     int     (*xf_func)(int c);
  55.     const char  *xf_name;
  56.     short       xf_flags;
  57.  };
  58.  
  59.  #define XF_ARG     1   /* command takes number prefix */
  60.  #define    XF_NOBIND   2   /* not allowed to bind to function */
  61.  #define    XF_PREFIX   4   /* function sets prefix */
  62.  
  63.  /* Separator for completion */
  64.  #define    is_cfs(c)   (c == ' ' || c == '\t' || c == '"' || c == '\'')
  65.  
  66.  /* Separator for motion */
  67. -#define    is_mfs(c)   (!(isalnum((unsigned char)c) || \
  68. -           c == '_' || c == '$' || c & 0x80))
  69. +#define    is_mfs(c)   (!(iswalnum(c) || \
  70. +           c == L'_' || c == L'$'))
  71.  
  72.  /* Arguments for do_complete()
  73.   * 0 = enumerate  M-= complete as much as possible and then list
  74.   * 1 = complete   M-Esc
  75.   * 2 = list       M-?
  76.   */
  77.  typedef enum {
  78.     CT_LIST,    /* list the possible completions */
  79.     CT_COMPLETE,    /* complete to longest prefix */
  80.     CT_COMPLIST /* complete and then list (if non-exact) */
  81.  } Comp_type;
  82.  
  83.  /* keybindings */
  84.  struct kb_entry {
  85.     TAILQ_ENTRY(kb_entry)   entry;
  86.     unsigned char       *seq;
  87.     int         len;
  88.     struct x_ftab       *ftab;
  89.     void            *args;
  90.  };
  91.  TAILQ_HEAD(kb_list, kb_entry);
  92.  struct kb_list         kblist = TAILQ_HEAD_INITIALIZER(kblist);
  93.  
  94.  /* { from 4.9 edit.h */
  95.  /*
  96.   * The following are used for my horizontal scrolling stuff
  97.   */
  98. -static char    *xbuf;      /* beg input buffer */
  99. -static char    *xend;      /* end input buffer */
  100. -static char    *xcp;       /* current position */
  101. -static char    *xep;       /* current end */
  102. -static char    *xbp;       /* start of visible portion of input buffer */
  103. -static char    *xlp;       /* last byte visible on screen */
  104. +static wchar_t *xbuf;      /* beg input buffer */
  105. +static wchar_t *xend;      /* end input buffer */
  106. +static wchar_t *xcp;       /* current position */
  107. +static wchar_t *xep;       /* current end */
  108. +static wchar_t *xbp;       /* start of visible portion of input buffer */
  109. +static wchar_t *xlp;       /* last byte visible on screen */
  110.  static int x_adj_ok;
  111.  /*
  112.   * we use x_adj_done so that functions can tell
  113.   * whether x_adjust() has been called while they are active.
  114.   */
  115.  static int x_adj_done;
  116.  
  117.  static int xx_cols;
  118.  static int x_col;
  119.  static int x_displen;
  120.  static int x_arg;      /* general purpose arg */
  121.  static int x_arg_defaulted;/* x_arg not explicitly set; defaulted to 1 */
  122.  
  123.  static int xlp_valid;
  124.  /* end from 4.9 edit.h } */
  125.  static int x_tty;      /* are we on a tty? */
  126.  static int x_bind_quiet;   /* be quiet when binding keys */
  127.  static int (*x_last_command)(int);
  128.  
  129.  static char   **x_histp;   /* history position */
  130.  static int x_nextcmd;  /* for newline-and-next */
  131. -static char    *xmp;       /* mark pointer */
  132. +static wchar_t *xmp;       /* mark pointer */
  133.  #define    KILLSIZE    20
  134.  static char    *killstack[KILLSIZE];
  135.  static int killsp, killtp;
  136.  static int x_literal_set;
  137.  static int x_arg_set;
  138.  static char    *macro_args;
  139.  static int prompt_skip;
  140.  static int prompt_redraw;
  141.  
  142.  static int x_ins(char *);
  143. +static int x_wins(wchar_t);
  144. +static int x_after_ins(void);
  145.  static void    x_delete(int, int);
  146.  static int x_bword(void);
  147.  static int x_fword(void);
  148. -static void    x_goto(char *);
  149. -static void    x_bs(int);
  150. -static int x_size_str(char *);
  151. -static int x_size(int);
  152. -static void    x_zots(char *);
  153. -static void    x_zotc(int);
  154. +static void    x_goto(wchar_t *);
  155. +static void    x_bs(wchar_t);
  156. +static int x_size_str(wchar_t *);
  157. +static int x_size(wchar_t);
  158. +static void    x_zots(wchar_t *);
  159. +static void    x_zotc(wchar_t);
  160.  static void    x_load_hist(char **);
  161.  static int x_search(char *, int, int);
  162.  static int x_match(char *, char *);
  163. +static int x_wmatch(wchar_t *, char *);
  164.  static void    x_redraw(int);
  165.  static void    x_push(int);
  166.  static void    x_adjust(void);
  167.  static void    x_e_ungetc(int);
  168.  static int x_e_getc(void);
  169. -static void    x_e_putc(int);
  170. +static void    x_e_putc(wchar_t);
  171.  static void    x_e_puts(const char *);
  172.  static int x_comment(int);
  173.  static int x_fold_case(int);
  174. -static char    *x_lastcp(void);
  175. +static wchar_t *x_lastcp(void);
  176.  static void    do_complete(int, Comp_type);
  177. -static int isu8cont(unsigned char);
  178.  
  179.  /* proto's for keybindings */
  180.  static int x_abort(int);
  181.  static int x_beg_hist(int);
  182.  static int x_comp_comm(int);
  183.  static int x_comp_file(int);
  184.  static int x_complete(int);
  185.  static int x_del_back(int);
  186.  static int x_del_bword(int);
  187.  static int x_del_char(int);
  188.  static int x_del_fword(int);
  189.  static int x_del_line(int);
  190.  static int x_draw_line(int);
  191.  static int x_end_hist(int);
  192.  static int x_end_of_text(int);
  193.  static int x_enumerate(int);
  194.  static int x_eot_del(int);
  195.  static int x_error(int);
  196.  static int x_goto_hist(int);
  197.  static int x_ins_string(int);
  198.  static int x_insert(int);
  199. +static int x_winsert(wchar_t);
  200.  static int x_kill(int);
  201.  static int x_kill_region(int);
  202.  static int x_list_comm(int);
  203.  static int x_list_file(int);
  204.  static int x_literal(int);
  205.  static int x_meta_yank(int);
  206.  static int x_mv_back(int);
  207.  static int x_mv_begin(int);
  208.  static int x_mv_bword(int);
  209.  static int x_mv_end(int);
  210.  static int x_mv_forw(int);
  211.  static int x_mv_fword(int);
  212.  static int x_newline(int);
  213.  static int x_next_com(int);
  214.  static int x_nl_next_com(int);
  215.  static int x_noop(int);
  216.  static int x_prev_com(int);
  217.  static int x_prev_histword(int);
  218.  static int x_search_char_forw(int);
  219.  static int x_search_char_back(int);
  220.  static int x_search_hist(int);
  221.  static int x_set_mark(int);
  222.  static int x_stuff(int);
  223.  static int x_stuffreset(int);
  224.  static int x_transpose(int);
  225.  static int x_version(int);
  226.  static int x_xchg_point_mark(int);
  227.  static int x_yank(int);
  228.  static int x_comp_list(int);
  229.  static int x_expand(int);
  230.  static int x_fold_capitalize(int);
  231.  static int x_fold_lower(int);
  232.  static int x_fold_upper(int);
  233.  static int x_set_arg(int);
  234.  static int x_comment(int);
  235.  #ifdef DEBUG
  236.  static int x_debug_info(int);
  237.  #endif
  238.  
  239.  static const struct x_ftab x_ftab[] = {
  240.     { x_abort,      "abort",            0 },
  241.     { x_beg_hist,       "beginning-of-history",     0 },
  242.     { x_comp_comm,      "complete-command",     0 },
  243.     { x_comp_file,      "complete-file",        0 },
  244.     { x_complete,       "complete",         0 },
  245.     { x_del_back,       "delete-char-backward",     XF_ARG },
  246.     { x_del_bword,      "delete-word-backward",     XF_ARG },
  247.     { x_del_char,       "delete-char-forward",      XF_ARG },
  248.     { x_del_fword,      "delete-word-forward",      XF_ARG },
  249.     { x_del_line,       "kill-line",            0 },
  250.     { x_draw_line,      "redraw",           0 },
  251.     { x_end_hist,       "end-of-history",       0 },
  252.     { x_end_of_text,    "eot",              0 },
  253.     { x_enumerate,      "list",             0 },
  254.     { x_eot_del,        "eot-or-delete",        XF_ARG },
  255.     { x_error,      "error",            0 },
  256.     { x_goto_hist,      "goto-history",         XF_ARG },
  257.     { x_ins_string,     "macro-string",         XF_NOBIND },
  258.     { x_insert,     "auto-insert",          XF_ARG },
  259.     { x_kill,       "kill-to-eol",          XF_ARG },
  260.     { x_kill_region,    "kill-region",          0 },
  261.     { x_list_comm,      "list-command",         0 },
  262.     { x_list_file,      "list-file",            0 },
  263.     { x_literal,        "quote",            0 },
  264.     { x_meta_yank,      "yank-pop",         0 },
  265.     { x_mv_back,        "backward-char",        XF_ARG },
  266.     { x_mv_begin,       "beginning-of-line",        0 },
  267.     { x_mv_bword,       "backward-word",        XF_ARG },
  268.     { x_mv_end,     "end-of-line",          0 },
  269.     { x_mv_forw,        "forward-char",         XF_ARG },
  270.     { x_mv_fword,       "forward-word",         XF_ARG },
  271.     { x_newline,        "newline",          0 },
  272.     { x_next_com,       "down-history",         XF_ARG },
  273.     { x_nl_next_com,    "newline-and-next",     0 },
  274.     { x_noop,       "no-op",            0 },
  275.     { x_prev_com,       "up-history",           XF_ARG },
  276.     { x_prev_histword,  "prev-hist-word",       XF_ARG },
  277.     { x_search_char_forw,   "search-character-forward", XF_ARG },
  278.     { x_search_char_back,   "search-character-backward",    XF_ARG },
  279.     { x_search_hist,    "search-history",       0 },
  280.     { x_set_mark,       "set-mark-command",     0 },
  281.     { x_stuff,      "stuff",            0 },
  282.     { x_stuffreset,     "stuff-reset",          0 },
  283.     { x_transpose,      "transpose-chars",      0 },
  284.     { x_version,        "version",          0 },
  285.     { x_xchg_point_mark,    "exchange-point-and-mark",  0 },
  286.     { x_yank,       "yank",             0 },
  287.     { x_comp_list,      "complete-list",        0 },
  288.     { x_expand,     "expand-file",          0 },
  289.     { x_fold_capitalize,    "capitalize-word",      XF_ARG },
  290.     { x_fold_lower,     "downcase-word",        XF_ARG },
  291.     { x_fold_upper,     "upcase-word",          XF_ARG },
  292.     { x_set_arg,        "set-arg",          XF_NOBIND },
  293.     { x_comment,        "comment",          0 },
  294.     { 0, 0, 0 },
  295.  #ifdef DEBUG
  296.     { x_debug_info,     "debug-info",           0 },
  297.  #else
  298.     { 0, 0, 0 },
  299.  #endif
  300.     { 0, 0, 0 },
  301.  };
  302.  
  303. -int
  304. -isu8cont(unsigned char c)
  305. +/* utf-8:
  306. + * returns remaining bytes to read
  307. + * or -1 if an error occurred */
  308. +static int
  309. +u8bytes(unsigned char c)
  310.  {
  311. -   return (c & (0x80 | 0x40)) == 0x80;
  312. +   int i = 0;
  313. +
  314. +   while (i < 5 && (c & (1<<(7-i))))
  315. +       i++;
  316. +
  317. +   if (i == 1 || i == 5)
  318. +       i = -1;/* illegal utf-8 char */
  319. +   else if (i)
  320. +       i--;
  321. +   return i;
  322. +}
  323. +
  324. +/* utf-8: writes at most 6 bytes */
  325. +static int
  326. +x_e_getmb(char *mbs) {
  327. +   int i, c;
  328. +   char *mbp = mbs;
  329. +
  330. +   if ((c = x_e_getc()) < 0)
  331. +       return -1;
  332. +   *mbp++ = c;
  333. +   *mbp = '\0';
  334. +
  335. +   if ((i = u8bytes(c)) == -1)
  336. +       return -1;
  337. +   while (i--) {
  338. +       if ((c = x_e_getc()) < 0)
  339. +           return -1;
  340. +       if ((c & (0x80 | 0x40)) != 0x80)
  341. +           return -1;
  342. +       *mbp++ = c;
  343. +       *mbp = '\0';
  344. +   }
  345. +   return mbp - mbs;
  346.  }
  347.  
  348.  int
  349.  x_emacs(char *buf, size_t len)
  350.  {
  351.     struct kb_entry     *k, *kmatch = NULL;
  352.     char            line[LINE + 1];
  353. -   int         at = 0, submatch, ret, c;
  354. -   const char      *p;
  355. -
  356. -   xbp = xbuf = buf; xend = buf + len;
  357. -   xlp = xcp = xep = buf;
  358. +   wchar_t         wline[LINE + 1];
  359. +   int         at = 0, wat = 0, submatch, ret, c, i;
  360. +   const char      *mbp;
  361. +   wchar_t         wc, *p;
  362. +
  363. +   if ((p = reallocarray(xbuf, len, sizeof(wchar_t))) == NULL) {
  364. +       free(xbuf);
  365. +       xbuf = NULL;
  366. +       return 0;
  367. +   }
  368. +   xbuf = p;
  369. +   if ((i = mbstowcs(xbuf, buf, len)) == (size_t)-1)
  370. +       return 0;
  371. +   xbp = xbuf; xend = xbuf + len;
  372. +   xlp = xcp = xep = xbuf;
  373.     *xcp = 0;
  374.     xlp_valid = true;
  375.     xmp = NULL;
  376.     x_histp = histptr + 1;
  377.  
  378.     xx_cols = x_cols;
  379. -   x_col = promptlen(prompt, &p);
  380. -   prompt_skip = p - prompt;
  381. +   x_col = promptlen(prompt, &mbp); /* should return width, not length */
  382. +   prompt_skip = mbp - prompt;
  383.     x_adj_ok = 1;
  384.     prompt_redraw = 1;
  385.     if (x_col > xx_cols)
  386.         x_col = x_col - (x_col / xx_cols) * xx_cols;
  387.     x_displen = xx_cols - 2 - x_col;
  388.     x_adj_done = 0;
  389.  
  390.     pprompt(prompt, 0);
  391.     if (x_displen < 1) {
  392.         x_col = 0;
  393.         x_displen = xx_cols - 2;
  394. -       x_e_putc('\n');
  395. +       x_e_putc(L'\n');
  396.         prompt_redraw = 0;
  397.     }
  398.  
  399.     if (x_nextcmd >= 0) {
  400.         int off = source->line - x_nextcmd;
  401.         if (histptr - history >= off)
  402.             x_load_hist(histptr - off);
  403.         x_nextcmd = -1;
  404.     }
  405.  
  406. -   line[0] = '\0';
  407. +   line[0] = '\0', wline[0] = L'\0';
  408.     x_literal_set = 0;
  409.     x_arg = -1;
  410.     x_last_command = NULL;
  411.     while (1) {
  412.         x_flush();
  413. -       if ((c = x_e_getc()) < 0)
  414. +       if ((c = x_e_getmb(line + at)) < 0)
  415.             return 0;
  416. -
  417. -       line[at++] = c;
  418. -       line[at] = '\0';
  419. +       at += c;
  420. +       if ((i = mbtowc(&wc, line, c + 1)) == -1) {
  421. +           (void)mbtowc(NULL, NULL, MB_CUR_MAX);
  422. +           return 0;
  423. +       }
  424. +       wline[wat++] = wc;
  425. +       wline[wat] = L'\0';
  426.  
  427.         if (x_arg == -1) {
  428.             x_arg = 1;
  429.             x_arg_defaulted = 1;
  430.         }
  431.  
  432.         if (x_literal_set) {
  433.             /* literal, so insert it */
  434.             x_literal_set = 0;
  435.             submatch = 0;
  436.         } else {
  437.             submatch = 0;
  438.             kmatch = NULL;
  439.             TAILQ_FOREACH(k, &kblist, entry) {
  440.                 if (at > k->len)
  441.                     continue;
  442.  
  443.                 if (memcmp(k->seq, line, at) == 0) {
  444.                     /* sub match */
  445.                     submatch++;
  446.                     if (k->len == at)
  447.                         kmatch = k;
  448.                 }
  449.  
  450.                 /* see if we can abort search early */
  451.                 if (submatch > 1)
  452.                     break;
  453.             }
  454.         }
  455.  
  456.         if (submatch == 1 && kmatch) {
  457.             if (kmatch->ftab->xf_func == x_ins_string &&
  458.                 kmatch->args && !macro_args) {
  459.                 /* treat macro string as input */
  460.                 macro_args = kmatch->args;
  461.                 ret = KSTD;
  462.             } else
  463.                 ret = kmatch->ftab->xf_func(c);
  464.         } else {
  465.             if (submatch)
  466.                 continue;
  467. -           if (at == 1)
  468. -               ret = x_insert(c);
  469. +
  470. +           if (wat == 1)
  471. +               ret = x_winsert(wc);
  472.             else
  473.                 ret = x_error(c); /* not matched meta sequence */
  474.         }
  475.  
  476.         switch (ret) {
  477.         case KSTD:
  478.             if (kmatch)
  479.                 x_last_command = kmatch->ftab->xf_func;
  480.             else
  481.                 x_last_command = NULL;
  482.             break;
  483.         case KEOL:
  484. -           ret = xep - xbuf;
  485. +           ret = xep - xbuf; *xep = L'\0';
  486. +           if ((ret = wcstombs(buf, xbuf, len)) == (size_t)-1)
  487. +               return 0;
  488.             return (ret);
  489.             break;
  490.         case KINTR:
  491.             trapsig(SIGINT);
  492.             x_mode(false);
  493.             unwind(LSHELL);
  494.             x_arg = -1;
  495.             break;
  496.         default:
  497.             bi_errorf("invalid return code"); /* can't happen */
  498.         }
  499.  
  500.         /* reset meta sequence */
  501. -       at = 0;
  502. -       line[0] = '\0';
  503. +       at = 0, wat = 0;
  504. +       line[0] = '\0', wline[0] = L'\0';
  505.         if (x_arg_set)
  506.             x_arg_set = 0; /* reset args next time around */
  507.         else
  508.             x_arg = -1;
  509.     }
  510.  }
  511.  
  512.  static int
  513.  x_insert(int c)
  514.  {
  515. -   char    str[2];
  516. -
  517.     /*
  518.      *  Should allow tab and control chars.
  519.      */
  520.     if (c == 0) {
  521.         x_e_putc(BEL);
  522.         return KSTD;
  523.     }
  524. -   str[0] = c;
  525. -   str[1] = '\0';
  526.     while (x_arg--)
  527. -       x_ins(str);
  528. +       x_wins(c);
  529. +   return KSTD;
  530. +}
  531. +
  532. +static int
  533. +x_winsert(wchar_t wc)
  534. +{
  535. +   /*
  536. +    *  Should allow tab and control chars.
  537. +    */
  538. +   if (wc == L'\0') {
  539. +       x_e_putc(BEL);
  540. +       return KSTD;
  541. +   }
  542. +   while (x_arg--)
  543. +       x_wins(wc);
  544.     return KSTD;
  545.  }
  546.  
  547.  static int
  548.  x_ins_string(int c)
  549.  {
  550.     return x_insert(c);
  551.  }
  552.  
  553.  static int
  554.  x_do_ins(const char *cp, size_t len)
  555.  {
  556. -   if (xep+len >= xend) {
  557. +   int offset;
  558. +   char *mb = strndup(cp, len+1);
  559. +
  560. +   if (mb == NULL || (offset = mbstowcs(NULL, mb, len)) == (size_t)-1 ||
  561. +           xep+offset >= xend) {
  562. +       x_e_putc(BEL);
  563. +       return -1;
  564. +   }
  565. +
  566. +   wmemmove(xcp+offset, xcp, xep - xcp + 1);
  567. +   mbstowcs(xcp, mb, len);
  568. +   free(mb);
  569. +   xcp += offset;
  570. +   xep += offset;
  571. +   return 0;
  572. +}
  573. +
  574. +static int
  575. +x_do_wins(const wchar_t wc)
  576. +{
  577. +   if (xep+1 >= xend) {
  578.         x_e_putc(BEL);
  579.         return -1;
  580.     }
  581.  
  582. -   memmove(xcp+len, xcp, xep - xcp + 1);
  583. -   memmove(xcp, cp, len);
  584. -   xcp += len;
  585. -   xep += len;
  586. +   wmemmove(xcp+1, xcp, xep - xcp + 1);
  587. +   *xcp++ = wc;
  588. +   xep++;
  589.     return 0;
  590.  }
  591.  
  592.  static int
  593.  x_ins(char *s)
  594.  {
  595. -   char    *cp = xcp;
  596. -   int adj = x_adj_done;
  597. -
  598.     if (x_do_ins(s, strlen(s)) < 0)
  599.         return -1;
  600. +   return (x_after_ins());
  601. +}
  602. +
  603. +static int
  604. +x_wins(wchar_t wc)
  605. +{
  606. +   if (x_do_wins(wc) < 0)
  607. +       return -1;
  608. +   return (x_after_ins());
  609. +}
  610. +
  611. +static int
  612. +x_after_ins()
  613. +{
  614. +   wchar_t *cp = xcp;
  615. +   int adj = x_adj_done;
  616. +
  617.     /*
  618.      * x_zots() may result in a call to x_adjust()
  619.      * we want xcp to reflect the new position.
  620.      */
  621.     xlp_valid = false;
  622.     x_lastcp();
  623.     x_adj_ok = (xcp >= xlp);
  624.     x_zots(cp);
  625.     if (adj == x_adj_done) {    /* has x_adjust() been called? */
  626.         /* no */
  627.         for (cp = xlp; cp > xcp; )
  628.             x_bs(*--cp);
  629.     }
  630.  
  631.     x_adj_ok = 1;
  632.     return 0;
  633.  }
  634.  
  635.  static int
  636.  x_del_back(int c)
  637.  {
  638.     int col = xcp - xbuf;
  639.  
  640.     if (col == 0) {
  641.         x_e_putc(BEL);
  642.         return KSTD;
  643.     }
  644.     if (x_arg > col)
  645.         x_arg = col;
  646. -   while (x_arg < col && isu8cont(xcp[-x_arg]))
  647. -       x_arg++;
  648.     x_goto(xcp - x_arg);
  649.     x_delete(x_arg, false);
  650.     return KSTD;
  651.  }
  652.  
  653.  static int
  654.  x_del_char(int c)
  655.  {
  656.     int nleft = xep - xcp;
  657.  
  658.     if (!nleft) {
  659.         x_e_putc(BEL);
  660.         return KSTD;
  661.     }
  662.     if (x_arg > nleft)
  663.         x_arg = nleft;
  664. -   while (x_arg < nleft && isu8cont(xcp[x_arg]))
  665. -       x_arg++;
  666.     x_delete(x_arg, false);
  667.     return KSTD;
  668.  }
  669.  
  670.  /* Delete nc bytes to the right of the cursor (including cursor position) */
  671.  static void
  672.  x_delete(int nc, int push)
  673.  {
  674.     int i,j;
  675. -   char    *cp;
  676. +   wchar_t *cp;
  677.  
  678.     if (nc == 0)
  679.         return;
  680.     if (xmp != NULL && xmp > xcp) {
  681.         if (xcp + nc > xmp)
  682.             xmp = xcp;
  683.         else
  684.             xmp -= nc;
  685.     }
  686.  
  687.     /*
  688.      * This lets us yank a word we have deleted.
  689.      */
  690.     if (push)
  691.         x_push(nc);
  692.  
  693.     xep -= nc;
  694.     cp = xcp;
  695.     j = 0;
  696.     i = nc;
  697.     while (i--) {
  698. -       j += x_size((unsigned char)*cp++);
  699. +       j += x_size(*cp++);
  700.     }
  701. -   memmove(xcp, xcp+nc, xep - xcp + 1);    /* Copies the null */
  702. +   wmemmove(xcp, xcp+nc, xep - xcp + 1);   /* Copies the null */
  703.     x_adj_ok = 0;           /* don't redraw */
  704.     xlp_valid = false;
  705.     x_zots(xcp);
  706.     /*
  707.      * if we are already filling the line,
  708.      * there is no need to ' ','\b'.
  709.      * But if we must, make sure we do the minimum.
  710.      */
  711.     if ((i = xx_cols - 2 - x_col) > 0) {
  712.         j = (j < i) ? j : i;
  713.         i = j;
  714.         while (i--)
  715. -           x_e_putc(' ');
  716. +           x_e_putc(L' ');
  717.         i = j;
  718.         while (i--)
  719. -           x_e_putc('\b');
  720. +           x_e_putc(L'\b');
  721.     }
  722.     /*x_goto(xcp);*/
  723.     x_adj_ok = 1;
  724.     xlp_valid = false;
  725.     for (cp = x_lastcp(); cp > xcp; )
  726.         x_bs(*--cp);
  727.  
  728.     return;
  729.  }
  730.  
  731.  static int
  732.  x_del_bword(int c)
  733.  {
  734.     x_delete(x_bword(), true);
  735.     return KSTD;
  736.  }
  737.  
  738.  static int
  739.  x_mv_bword(int c)
  740.  {
  741.     (void)x_bword();
  742.     return KSTD;
  743.  }
  744.  
  745.  static int
  746.  x_mv_fword(int c)
  747.  {
  748.     x_goto(xcp + x_fword());
  749.     return KSTD;
  750.  }
  751.  
  752.  static int
  753.  x_del_fword(int c)
  754.  {
  755.     x_delete(x_fword(), true);
  756.     return KSTD;
  757.  }
  758.  
  759.  static int
  760.  x_bword(void)
  761.  {
  762.     int nc = 0;
  763. -   char    *cp = xcp;
  764. +   wchar_t *cp = xcp;
  765.  
  766.     if (cp == xbuf) {
  767.         x_e_putc(BEL);
  768.         return 0;
  769.     }
  770.     while (x_arg--) {
  771.         while (cp != xbuf && is_mfs(cp[-1])) {
  772.             cp--;
  773.             nc++;
  774.         }
  775.         while (cp != xbuf && !is_mfs(cp[-1])) {
  776.             cp--;
  777.             nc++;
  778.         }
  779.     }
  780.     x_goto(cp);
  781.     return nc;
  782.  }
  783.  
  784.  static int
  785.  x_fword(void)
  786.  {
  787.     int nc = 0;
  788. -   char    *cp = xcp;
  789. +   wchar_t *cp = xcp;
  790.  
  791.     if (cp == xep) {
  792.         x_e_putc(BEL);
  793.         return 0;
  794.     }
  795.     while (x_arg--) {
  796.         while (cp != xep && is_mfs(*cp)) {
  797.             cp++;
  798.             nc++;
  799.         }
  800.         while (cp != xep && !is_mfs(*cp)) {
  801.             cp++;
  802.             nc++;
  803.         }
  804.     }
  805.     return nc;
  806.  }
  807.  
  808.  static void
  809. -x_goto(char *cp)
  810. +x_goto(wchar_t *cp)
  811.  {
  812.     if (cp < xbp || cp >= (xbp + x_displen)) {
  813.         /* we are heading off screen */
  814.         xcp = cp;
  815.         x_adjust();
  816.     } else if (cp < xcp) {      /* move back */
  817.         while (cp < xcp)
  818. -           x_bs((unsigned char)*--xcp);
  819. +           x_bs(*--xcp);
  820.     } else if (cp > xcp) {      /* move forward */
  821.         while (cp > xcp)
  822. -           x_zotc((unsigned char)*xcp++);
  823. +           x_zotc(*xcp++);
  824.     }
  825.  }
  826.  
  827.  static void
  828. -x_bs(int c)
  829. +x_bs(wchar_t c)
  830.  {
  831.     int i;
  832.  
  833.     i = x_size(c);
  834.     while (i--)
  835. -       x_e_putc('\b');
  836. +       x_e_putc(L'\b');
  837.  }
  838.  
  839.  static int
  840. -x_size_str(char *cp)
  841. +x_size_str(wchar_t *cp)
  842.  {
  843.     int size = 0;
  844.     while (*cp)
  845.         size += x_size(*cp++);
  846.     return size;
  847.  }
  848.  
  849.  static int
  850. -x_size(int c)
  851. +x_size(wchar_t c)
  852.  {
  853. -   if (c=='\t')
  854. +   int w;
  855. +
  856. +   if (c==L'\t')
  857.         return 4;   /* Kludge, tabs are always four spaces. */
  858. -   if (iscntrl(c))     /* control char */
  859. +   if (iswcntrl(c))        /* control char */
  860.         return 2;
  861. -   if (isu8cont(c))
  862. -       return 0;
  863. -   return 1;
  864. +   if ((w = wcwidth(c)) == -1)
  865. +       w = 0;
  866. +   return w;
  867.  }
  868.  
  869.  static void
  870. -x_zots(char *str)
  871. +x_zots(wchar_t *str)
  872.  {
  873.     int adj = x_adj_done;
  874.  
  875. -   if (str > xbuf && isu8cont(*str)) {
  876. -       while (str > xbuf && isu8cont(*str))
  877. -           str--;
  878. -       x_e_putc('\b');
  879. -   }
  880.     x_lastcp();
  881.     while (*str && str < xlp && adj == x_adj_done)
  882.         x_zotc(*str++);
  883.  }
  884.  
  885.  static void
  886. -x_zotc(int c)
  887. +x_zotc(wchar_t c)
  888.  {
  889. -   if (c == '\t') {
  890. +   if (c == L'\t') {
  891.         /*  Kludge, tabs are always four spaces.  */
  892.         x_e_puts("    ");
  893. -   } else if (iscntrl(c)) {
  894. -       x_e_putc('^');
  895. +   } else if (iswcntrl(c)) {
  896. +       x_e_putc(L'^');
  897.         x_e_putc(UNCTRL(c));
  898.     } else
  899.         x_e_putc(c);
  900.  }
  901.  
  902.  static int
  903.  x_mv_back(int c)
  904.  {
  905.     int col = xcp - xbuf;
  906.  
  907.     if (col == 0) {
  908.         x_e_putc(BEL);
  909.         return KSTD;
  910.     }
  911.     if (x_arg > col)
  912.         x_arg = col;
  913. -   while (x_arg < col && isu8cont(xcp[-x_arg]))
  914. -       x_arg++;
  915.     x_goto(xcp - x_arg);
  916.     return KSTD;
  917.  }
  918.  
  919.  static int
  920.  x_mv_forw(int c)
  921.  {
  922.     int nleft = xep - xcp;
  923.  
  924.     if (!nleft) {
  925.         x_e_putc(BEL);
  926.         return KSTD;
  927.     }
  928.     if (x_arg > nleft)
  929.         x_arg = nleft;
  930. -   while (x_arg < nleft && isu8cont(xcp[x_arg]))
  931. -       x_arg++;
  932.     x_goto(xcp + x_arg);
  933.     return KSTD;
  934.  }
  935.  
  936.  static int
  937.  x_search_char_forw(int c)
  938.  {
  939. -   char *cp = xcp;
  940. +   wchar_t wc, *cp = xcp;
  941. +   char mb[MB_LEN_MAX + 1];
  942.  
  943. -   *xep = '\0';
  944. -   c = x_e_getc();
  945. +   *xep = L'\0';
  946. +   if ((c = x_e_getmb(mb)) == -1) {
  947. +       x_e_putc(BEL);
  948. +       return KSTD;
  949. +   }
  950. +   c = mbtowc(&wc, mb, c+1);
  951.     while (x_arg--) {
  952.         if (c < 0 ||
  953. -           ((cp = (cp == xep) ? NULL : strchr(cp + 1, c)) == NULL &&
  954. -           (cp = strchr(xbuf, c)) == NULL)) {
  955. +           ((cp = (cp == xep) ? NULL : wcschr(cp + 1, wc)) == NULL &&
  956. +           (cp = wcschr(xbuf, wc)) == NULL)) {
  957.             x_e_putc(BEL);
  958.             return KSTD;
  959.         }
  960.     }
  961.     x_goto(cp);
  962.     return KSTD;
  963.  }
  964.  
  965.  static int
  966.  x_search_char_back(int c)
  967.  {
  968. -   char *cp = xcp, *p;
  969. +   wchar_t *cp = xcp, *p, wc;
  970. +   char mb[MB_LEN_MAX + 1];
  971.  
  972. -   c = x_e_getc();
  973. +   if ((c = x_e_getmb(mb)) == -1) {
  974. +       x_e_putc(BEL);
  975. +       return KSTD;
  976. +   }
  977. +   c = mbtowc(&wc, mb, c+1);
  978.     for (; x_arg--; cp = p)
  979.         for (p = cp; ; ) {
  980.             if (p-- == xbuf)
  981.                 p = xep;
  982.             if (c < 0 || p == cp) {
  983.                 x_e_putc(BEL);
  984.                 return KSTD;
  985.             }
  986. -           if (*p == c)
  987. +           if (*p == wc)
  988.                 break;
  989.         }
  990.     x_goto(cp);
  991.     return KSTD;
  992.  }
  993.  
  994.  static int
  995.  x_newline(int c)
  996.  {
  997. -   x_e_putc('\r');
  998. -   x_e_putc('\n');
  999. +   x_e_putc(L'\r');
  1000. +   x_e_putc(L'\n');
  1001.     x_flush();
  1002. -   *xep++ = '\n';
  1003. +   *xep++ = L'\n';
  1004.     return KEOL;
  1005.  }
  1006.  
  1007.  static int
  1008.  x_end_of_text(int c)
  1009.  {
  1010.     x_zotc(edchars.eof);
  1011.     x_putc('\r');
  1012.     x_putc('\n');
  1013.     x_flush();
  1014.     return KEOL;
  1015.  }
  1016.  
  1017.  static int x_beg_hist(int c) { x_load_hist(history); return KSTD;}
  1018.  
  1019.  static int x_end_hist(int c) { x_load_hist(histptr); return KSTD;}
  1020.  
  1021.  static int x_prev_com(int c) { x_load_hist(x_histp - x_arg); return KSTD;}
  1022.  
  1023.  static int x_next_com(int c) { x_load_hist(x_histp + x_arg); return KSTD;}
  1024.  
  1025.  /* Goto a particular history number obtained from argument.
  1026.   * If no argument is given history 1 is probably not what you
  1027.   * want so we'll simply go to the oldest one.
  1028.   */
  1029.  static int
  1030.  x_goto_hist(int c)
  1031.  {
  1032.     if (x_arg_defaulted)
  1033.         x_load_hist(history);
  1034.     else
  1035.         x_load_hist(histptr + x_arg - source->line);
  1036.     return KSTD;
  1037.  }
  1038.  
  1039.  static void
  1040.  x_load_hist(char **hp)
  1041.  {
  1042. -   int oldsize;
  1043. +   int oldsize, i;
  1044.  
  1045.     if (hp < history || hp > histptr) {
  1046.         x_e_putc(BEL);
  1047.         return;
  1048.     }
  1049.     x_histp = hp;
  1050.     oldsize = x_size_str(xbuf);
  1051. -   strlcpy(xbuf, *hp, xend - xbuf);
  1052. +   if ((i = mbstowcs(xbuf, *hp, xend - xbuf) == (size_t)-1)) {
  1053. +       x_e_putc(BEL);
  1054. +       return;
  1055. +   }
  1056.     xbp = xbuf;
  1057. -   xep = xcp = xbuf + strlen(xbuf);
  1058. +   xep = xcp = xbuf + wcslen(xbuf);
  1059.     xlp_valid = false;
  1060.     if (xep <= x_lastcp())
  1061.         x_redraw(oldsize);
  1062.     x_goto(xep);
  1063.  }
  1064.  
  1065.  static int
  1066.  x_nl_next_com(int c)
  1067.  {
  1068.     x_nextcmd = source->line - (histptr - x_histp) + 1;
  1069.     return (x_newline(c));
  1070.  }
  1071.  
  1072.  static int
  1073.  x_eot_del(int c)
  1074.  {
  1075.     if (xep == xbuf && x_arg_defaulted)
  1076.         return (x_end_of_text(c));
  1077.     else
  1078.         return (x_del_char(c));
  1079.  }
  1080.  
  1081.  static void *
  1082.  kb_find_hist_func(char c)
  1083.  {
  1084.     struct kb_entry     *k;
  1085.     char            line[LINE + 1];
  1086.  
  1087.     line[0] = c;
  1088.     line[1] = '\0';
  1089.     TAILQ_FOREACH(k, &kblist, entry)
  1090.         if (!strcmp(k->seq, line))
  1091.             return (k->ftab->xf_func);
  1092.  
  1093.     return (x_insert);
  1094.  }
  1095.  
  1096.  /* reverse incremental history search */
  1097.  static int
  1098.  x_search_hist(int c)
  1099.  {
  1100.     int offset = -1;    /* offset of match in xbuf, else -1 */
  1101.     char pat [256+1];   /* pattern buffer */
  1102.     char *p = pat;
  1103.     int (*f)(int);
  1104.  
  1105.     *p = '\0';
  1106.     while (1) {
  1107.         if (offset < 0) {
  1108.             x_e_puts("\nI-search: ");
  1109.             x_e_puts(pat);
  1110.         }
  1111.         x_flush();
  1112. -       if ((c = x_e_getc()) < 0)
  1113. +       if ((c = x_e_getc()) < 0) /*XXX*/
  1114.             return KSTD;
  1115.         f = kb_find_hist_func(c);
  1116.         if (c == CTRL('[')) {
  1117.             x_e_ungetc(c);
  1118.             break;
  1119.         } else if (f == x_search_hist)
  1120.             offset = x_search(pat, 0, offset);
  1121.         else if (f == x_del_back) {
  1122.             if (p == pat) {
  1123.                 offset = -1;
  1124.                 break;
  1125.             }
  1126.             if (p > pat)
  1127.                 *--p = '\0';
  1128.             if (p == pat)
  1129.                 offset = -1;
  1130.             else
  1131.                 offset = x_search(pat, 1, offset);
  1132.             continue;
  1133.         } else if (f == x_insert) {
  1134.             /* add char to pattern */
  1135.             /* overflow check... */
  1136.             if (p >= &pat[sizeof(pat) - 1]) {
  1137.                 x_e_putc(BEL);
  1138.                 continue;
  1139.             }
  1140.             *p++ = c, *p = '\0';
  1141.             if (offset >= 0) {
  1142.                 /* already have partial match */
  1143. -               offset = x_match(xbuf, pat);
  1144. +               offset = x_wmatch(xbuf, pat);
  1145.                 if (offset >= 0) {
  1146.                     x_goto(xbuf + offset + (p - pat) -
  1147.                         (*pat == '^'));
  1148.                     continue;
  1149.                 }
  1150.             }
  1151.             offset = x_search(pat, 0, offset);
  1152.         } else { /* other command */
  1153.             x_e_ungetc(c);
  1154.             break;
  1155.         }
  1156.     }
  1157.     if (offset < 0)
  1158.         x_redraw(-1);
  1159.     return KSTD;
  1160.  }
  1161.  
  1162.  /* search backward from current line */
  1163.  static int
  1164.  x_search(char *pat, int sameline, int offset)
  1165.  {
  1166.     char **hp;
  1167.     int i;
  1168.  
  1169.     for (hp = x_histp - (sameline ? 0 : 1) ; hp >= history; --hp) {
  1170.         i = x_match(*hp, pat);
  1171.         if (i >= 0) {
  1172.             if (offset < 0)
  1173. -               x_e_putc('\n');
  1174. +               x_e_putc(L'\n');
  1175.             x_load_hist(hp);
  1176.             x_goto(xbuf + i + strlen(pat) - (*pat == '^'));
  1177.             return i;
  1178.         }
  1179.     }
  1180.     x_e_putc(BEL);
  1181.     x_histp = histptr;
  1182.     return -1;
  1183.  }
  1184.  
  1185.  /* return position of first match of pattern in string, else -1 */
  1186.  static int
  1187.  x_match(char *str, char *pat)
  1188.  {
  1189.     if (*pat == '^') {
  1190.         return (strncmp(str, pat+1, strlen(pat+1)) == 0) ? 0 : -1;
  1191.     } else {
  1192.         char *q = strstr(str, pat);
  1193.         return (q == NULL) ? -1 : q - str;
  1194.     }
  1195.  }
  1196.  
  1197.  static int
  1198. +x_wmatch(wchar_t *str, char *pat)
  1199. +{
  1200. +   int i;
  1201. +   wchar_t *wpat;
  1202. +
  1203. +   if ((i = mbstowcs(NULL, pat, 0)) == (size_t)-1)
  1204. +       return -1;
  1205. +   if ((wpat = calloc(i+1, sizeof(wchar_t))) == NULL)
  1206. +       return -1;
  1207. +   mbstowcs(wpat, pat, i+1);
  1208. +   wpat[i] = L'\0';
  1209. +
  1210. +   if (*pat == '^') {
  1211. +       i = (wcsncmp(str, wpat+1, wcslen(wpat+1)) == 0) ? 0 : -1;
  1212. +       free(wpat);
  1213. +       return i;
  1214. +   } else {
  1215. +       wchar_t *q = wcsstr(str, wpat);
  1216. +       free(wpat);
  1217. +       return (q == NULL) ? -1 : q - str;
  1218. +   }
  1219. +}
  1220. +
  1221. +static int
  1222.  x_del_line(int c)
  1223.  {
  1224.     int i, j;
  1225.  
  1226.     *xep = 0;
  1227.     i = xep - xbuf;
  1228.     j = x_size_str(xbuf);
  1229.     xcp = xbuf;
  1230.     x_push(i);
  1231.     xlp = xbp = xep = xbuf;
  1232.     xlp_valid = true;
  1233.     *xcp = 0;
  1234.     xmp = NULL;
  1235.     x_redraw(j);
  1236.     return KSTD;
  1237.  }
  1238.  
  1239.  static int
  1240.  x_mv_end(int c)
  1241.  {
  1242.     x_goto(xep);
  1243.     return KSTD;
  1244.  }
  1245.  
  1246.  static int
  1247.  x_mv_begin(int c)
  1248.  {
  1249.     x_goto(xbuf);
  1250.     return KSTD;
  1251.  }
  1252.  
  1253.  static int
  1254.  x_draw_line(int c)
  1255.  {
  1256.     x_redraw(-1);
  1257.     return KSTD;
  1258.  
  1259.  }
  1260.  
  1261.  /* Redraw (part of) the line.  If limit is < 0, the everything is redrawn
  1262.   * on a NEW line, otherwise limit is the screen column up to which needs
  1263.   * redrawing.
  1264.   */
  1265.  static void
  1266.  x_redraw(int limit)
  1267.  {
  1268. -   int i, j, truncate = 0;
  1269. -   char    *cp;
  1270. +   int j, truncate = 0;
  1271. +   wchar_t *cp;
  1272. +   wchar_t i;
  1273.  
  1274.     x_adj_ok = 0;
  1275.     if (limit == -1)
  1276. -       x_e_putc('\n');
  1277. +       x_e_putc(L'\n');
  1278.     else
  1279. -       x_e_putc('\r');
  1280. +       x_e_putc(L'\r');
  1281.     x_flush();
  1282.     if (xbp == xbuf) {
  1283.         x_col = promptlen(prompt, NULL);
  1284.         if (x_col > xx_cols)
  1285.             truncate = (x_col / xx_cols) * xx_cols;
  1286.         if (prompt_redraw)
  1287.             pprompt(prompt + prompt_skip, truncate);
  1288.     }
  1289.     if (x_col > xx_cols)
  1290.         x_col = x_col - (x_col / xx_cols) * xx_cols;
  1291.     x_displen = xx_cols - 2 - x_col;
  1292.     if (x_displen < 1) {
  1293.         x_col = 0;
  1294.         x_displen = xx_cols - 2;
  1295.     }
  1296.     xlp_valid = false;
  1297.     cp = x_lastcp();
  1298.     x_zots(xbp);
  1299.     if (xbp != xbuf || xep > xlp)
  1300.         limit = xx_cols;
  1301.     if (limit >= 0) {
  1302.         if (xep > xlp)
  1303.             i = 0;          /* we fill the line */
  1304.         else
  1305.             i = limit - (xlp - xbp);
  1306.  
  1307.         for (j = 0; j < i && x_col < (xx_cols - 2); j++)
  1308. -           x_e_putc(' ');
  1309. -       i = ' ';
  1310. +           x_e_putc(L' ');
  1311. +       i = L' ';
  1312.         if (xep > xlp) {        /* more off screen */
  1313.             if (xbp > xbuf)
  1314. -               i = '*';
  1315. +               i = L'*';
  1316.             else
  1317. -               i = '>';
  1318. +               i = L'>';
  1319.         } else if (xbp > xbuf)
  1320. -           i = '<';
  1321. +           i = L'<';
  1322.         x_e_putc(i);
  1323.         j++;
  1324.         while (j--)
  1325. -           x_e_putc('\b');
  1326. +           x_e_putc(L'\b');
  1327.     }
  1328.     for (cp = xlp; cp > xcp; )
  1329.         x_bs(*--cp);
  1330.     x_adj_ok = 1;
  1331.  #ifdef DEBUG
  1332.     x_flush();
  1333.  #endif
  1334.     return;
  1335.  }
  1336.  
  1337.  static int
  1338.  x_transpose(int c)
  1339.  {
  1340.     char    tmp;
  1341.  
  1342.     /* What transpose is meant to do seems to be up for debate. This
  1343.      * is a general summary of the options; the text is abcd with the
  1344.      * upper case character or underscore indicating the cursor position:
  1345.      *     Who          Before  After  Before   After
  1346.      *     at&t ksh in emacs mode:  abCd    abdC   abcd_    (bell)
  1347.      *     at&t ksh in gmacs mode:  abCd    baCd   abcd_    abdc_
  1348.      *     gnu emacs:       abCd    acbD   abcd_    abdc_
  1349.      * Pdksh currently goes with GNU behavior since I believe this is the
  1350.      * most common version of emacs, unless in gmacs mode, in which case
  1351.      * it does the at&t ksh gmacs mode.
  1352.      * This should really be broken up into 3 functions so users can bind
  1353.      * to the one they want.
  1354.      */
  1355.     if (xcp == xbuf) {
  1356.         x_e_putc(BEL);
  1357.         return KSTD;
  1358.     } else if (xcp == xep || Flag(FGMACS)) {
  1359.         if (xcp - xbuf == 1) {
  1360.             x_e_putc(BEL);
  1361.             return KSTD;
  1362.         }
  1363.         /* Gosling/Unipress emacs style: Swap two characters before the
  1364.          * cursor, do not change cursor position
  1365.          */
  1366.         x_bs(xcp[-1]);
  1367.         x_bs(xcp[-2]);
  1368.         x_zotc(xcp[-1]);
  1369.         x_zotc(xcp[-2]);
  1370.         tmp = xcp[-1];
  1371.         xcp[-1] = xcp[-2];
  1372.         xcp[-2] = tmp;
  1373.     } else {
  1374.         /* GNU emacs style: Swap the characters before and under the
  1375.          * cursor, move cursor position along one.
  1376.          */
  1377.         x_bs(xcp[-1]);
  1378.         x_zotc(xcp[0]);
  1379.         x_zotc(xcp[-1]);
  1380.         tmp = xcp[-1];
  1381.         xcp[-1] = xcp[0];
  1382.         xcp[0] = tmp;
  1383.         x_bs(xcp[0]);
  1384.         x_goto(xcp + 1);
  1385.     }
  1386.     return KSTD;
  1387.  }
  1388.  
  1389.  static int
  1390.  x_literal(int c)
  1391.  {
  1392.     x_literal_set = 1;
  1393.     return KSTD;
  1394.  }
  1395.  
  1396.  static int
  1397.  x_kill(int c)
  1398.  {
  1399.     int col = xcp - xbuf;
  1400.     int lastcol = xep - xbuf;
  1401.     int ndel;
  1402.  
  1403.     if (x_arg_defaulted)
  1404.         x_arg = lastcol;
  1405.     else if (x_arg > lastcol)
  1406.         x_arg = lastcol;
  1407. -   while (x_arg < lastcol && isu8cont(xbuf[x_arg]))
  1408. -       x_arg++;
  1409.     ndel = x_arg - col;
  1410.     if (ndel < 0) {
  1411.         x_goto(xbuf + x_arg);
  1412.         ndel = -ndel;
  1413.     }
  1414.     x_delete(ndel, true);
  1415.     return KSTD;
  1416.  }
  1417.  
  1418.  static void
  1419.  x_push(int nchars)
  1420.  {
  1421. -   char    *cp = str_nsave(xcp, nchars, AEDIT);
  1422. +   char    *cp, *mb, *mp;
  1423. +   wchar_t *wp = xcp;
  1424. +   int i;
  1425. +
  1426. +   if ((mb = calloc(nchars + 1, MB_CUR_MAX)) == NULL)
  1427. +       return;
  1428. +   mp = mb;
  1429. +   while (nchars--) {
  1430. +       if ((i = wctomb(mp, *wp++)) == -1) {
  1431. +           *mp = '?';
  1432. +           i = 1;
  1433. +       }
  1434. +       mp += i;
  1435. +   }
  1436. +   cp = str_nsave(mb, mp - mb, AEDIT);
  1437. +   free(mb);
  1438.     afree(killstack[killsp], AEDIT);
  1439.     killstack[killsp] = cp;
  1440.     killsp = (killsp + 1) % KILLSIZE;
  1441.  }
  1442.  
  1443.  static int
  1444.  x_yank(int c)
  1445.  {
  1446.     if (killsp == 0)
  1447.         killtp = KILLSIZE;
  1448.     else
  1449.         killtp = killsp;
  1450.     killtp --;
  1451.     if (killstack[killtp] == 0) {
  1452.         x_e_puts("\nnothing to yank");
  1453.         x_redraw(-1);
  1454.         return KSTD;
  1455.     }
  1456.     xmp = xcp;
  1457.     x_ins(killstack[killtp]);
  1458.     return KSTD;
  1459.  }
  1460.  
  1461.  static int
  1462.  x_meta_yank(int c)
  1463.  {
  1464.     int len;
  1465.     if ((x_last_command != x_yank && x_last_command != x_meta_yank) ||
  1466.         killstack[killtp] == 0) {
  1467.         killtp = killsp;
  1468.         x_e_puts("\nyank something first");
  1469.         x_redraw(-1);
  1470.         return KSTD;
  1471.     }
  1472.     len = strlen(killstack[killtp]);
  1473.     x_goto(xcp - len);
  1474.     x_delete(len, false);
  1475.     do {
  1476.         if (killtp == 0)
  1477.             killtp = KILLSIZE - 1;
  1478.         else
  1479.             killtp--;
  1480.     } while (killstack[killtp] == 0);
  1481.     x_ins(killstack[killtp]);
  1482.     return KSTD;
  1483.  }
  1484.  
  1485.  static int
  1486.  x_abort(int c)
  1487.  {
  1488.     /* x_zotc(c); */
  1489.     xlp = xep = xcp = xbp = xbuf;
  1490.     xlp_valid = true;
  1491.     *xcp = 0;
  1492.     return KINTR;
  1493.  }
  1494.  
  1495.  static int
  1496.  x_error(int c)
  1497.  {
  1498.     x_e_putc(BEL);
  1499.     return KSTD;
  1500.  }
  1501.  
  1502.  static int
  1503.  x_stuffreset(int c)
  1504.  {
  1505.  #ifdef TIOCSTI
  1506.     (void)x_stuff(c);
  1507.     return KINTR;
  1508.  #else
  1509.     x_zotc(c);
  1510.     xlp = xcp = xep = xbp = xbuf;
  1511.     xlp_valid = true;
  1512.     *xcp = 0;
  1513.     x_redraw(-1);
  1514.     return KSTD;
  1515.  #endif
  1516.  }
  1517.  
  1518.  static int
  1519.  x_stuff(int c)
  1520.  {
  1521.  #ifdef TIOCSTI
  1522.     char    ch = c;
  1523.     bool    savmode = x_mode(false);
  1524.  
  1525.     (void)ioctl(TTY, TIOCSTI, &ch);
  1526.     (void)x_mode(savmode);
  1527.     x_redraw(-1);
  1528.  #endif
  1529.     return KSTD;
  1530.  }
  1531.  
  1532.  static char *
  1533.  kb_encode(const char *s)
  1534.  {
  1535.     static char     l[LINE + 1];
  1536.     int         at = 0;
  1537.  
  1538. @@ -1390,790 +1533,813 @@ kb_print(struct kb_entry *k)
  1539.     else if (k->args) {
  1540.         shprintf("%s = ", kb_decode(k->seq));
  1541.         shprintf("'%s'\n", kb_decode(k->args));
  1542.     }
  1543.  }
  1544.  
  1545.  int
  1546.  x_bind(const char *a1, const char *a2,
  1547.     int macro,      /* bind -m */
  1548.     int list)       /* bind -l */
  1549.  {
  1550.     int         i;
  1551.     struct kb_entry     *k, *kb;
  1552.     char            in[LINE + 1];
  1553.  
  1554.     if (x_tty == 0) {
  1555.         bi_errorf("cannot bind, not a tty");
  1556.         return (1);
  1557.     }
  1558.  
  1559.     if (list) {
  1560.         /* show all function names */
  1561.         for (i = 0; i < NELEM(x_ftab); i++) {
  1562.             if (x_ftab[i].xf_name == NULL)
  1563.                 continue;
  1564.             if (x_ftab[i].xf_name &&
  1565.                 !(x_ftab[i].xf_flags & XF_NOBIND))
  1566.                 shprintf("%s\n", x_ftab[i].xf_name);
  1567.         }
  1568.         return (0);
  1569.     }
  1570.  
  1571.     if (a1 == NULL) {
  1572.         /* show all bindings */
  1573.         TAILQ_FOREACH(k, &kblist, entry)
  1574.             kb_print(k);
  1575.         return (0);
  1576.     }
  1577.  
  1578.     snprintf(in, sizeof in, "%s", kb_encode(a1));
  1579.     if (a2 == NULL) {
  1580.         /* print binding */
  1581.         TAILQ_FOREACH(k, &kblist, entry)
  1582.             if (!strcmp(k->seq, in)) {
  1583.                 kb_print(k);
  1584.                 return (0);
  1585.             }
  1586.         shprintf("%s = %s\n", kb_decode(a1), "auto-insert");
  1587.         return (0);
  1588.     }
  1589.  
  1590.     if (strlen(a2) == 0) {
  1591.         /* clear binding */
  1592.         TAILQ_FOREACH_SAFE(k, &kblist, entry, kb)
  1593.             if (!strcmp(k->seq, in)) {
  1594.                 kb_del(k);
  1595.                 break;
  1596.             }
  1597.         return (0);
  1598.     }
  1599.  
  1600.     /* set binding */
  1601.     if (macro) {
  1602.         /* delete old mapping */
  1603.         TAILQ_FOREACH_SAFE(k, &kblist, entry, kb)
  1604.             if (!strcmp(k->seq, in)) {
  1605.                 kb_del(k);
  1606.                 break;
  1607.             }
  1608.         kb_add_string(x_ins_string, kb_encode(a2), in);
  1609.         return (0);
  1610.     }
  1611.  
  1612.     /* set non macro binding */
  1613.     for (i = 0; i < NELEM(x_ftab); i++) {
  1614.         if (x_ftab[i].xf_name == NULL)
  1615.             continue;
  1616.         if (!strcmp(x_ftab[i].xf_name, a2)) {
  1617.             /* delete old mapping */
  1618.             TAILQ_FOREACH_SAFE(k, &kblist, entry, kb)
  1619.                 if (!strcmp(k->seq, in)) {
  1620.                     kb_del(k);
  1621.                     break;
  1622.                 }
  1623.             kb_add_string(x_ftab[i].xf_func, NULL, in);
  1624.             return (0);
  1625.         }
  1626.     }
  1627.     bi_errorf("%s: no such function", a2);
  1628.     return (1);
  1629.  }
  1630.  
  1631.  void
  1632.  x_init_emacs(void)
  1633.  {
  1634.     char *locale;
  1635.  
  1636.     x_tty = 1;
  1637.     ainit(AEDIT);
  1638.     x_nextcmd = -1;
  1639. +   xbuf = NULL;
  1640.  
  1641. +   setlocale(LC_CTYPE, "");
  1642.     /* Determine if we can translate meta key or use 8-bit AscII
  1643.      * XXX - It would be nice if there was a locale attribute to
  1644.      * determine if the locale is 7-bit or not.
  1645.      */
  1646.     locale = setlocale(LC_CTYPE, NULL);
  1647.     if (locale == NULL || !strcmp(locale, "C") || !strcmp(locale, "POSIX"))
  1648.         Flag(FEMACSUSEMETA) = 1;
  1649.  
  1650.     /* new keybinding stuff */
  1651.     TAILQ_INIT(&kblist);
  1652.  
  1653.     /* man page order */
  1654.     kb_add(x_abort,         NULL, CTRL('G'), 0);
  1655.     kb_add(x_mv_back,       NULL, CTRL('B'), 0);
  1656.     kb_add(x_mv_back,       NULL, CTRL('X'), CTRL('D'), 0);
  1657.     kb_add(x_mv_bword,      NULL, CTRL('['), 'b', 0);
  1658.     kb_add(x_beg_hist,      NULL, CTRL('['), '<', 0);
  1659.     kb_add(x_mv_begin,      NULL, CTRL('A'), 0);
  1660.     kb_add(x_fold_capitalize,   NULL, CTRL('['), 'C', 0);
  1661.     kb_add(x_fold_capitalize,   NULL, CTRL('['), 'c', 0);
  1662.     kb_add(x_comment,       NULL, CTRL('['), '#', 0);
  1663.     kb_add(x_complete,      NULL, CTRL('['), CTRL('['), 0);
  1664.     kb_add(x_comp_comm,     NULL, CTRL('X'), CTRL('['), 0);
  1665.     kb_add(x_comp_file,     NULL, CTRL('['), CTRL('X'), 0);
  1666.     kb_add(x_comp_list,     NULL, CTRL('I'), 0);
  1667.     kb_add(x_comp_list,     NULL, CTRL('['), '=', 0);
  1668.     kb_add(x_del_back,      NULL, CTRL('?'), 0);
  1669.     kb_add(x_del_back,      NULL, CTRL('H'), 0);
  1670.     kb_add(x_del_char,      NULL, CTRL('['), '[', '3', '~', 0); /* delete */
  1671.     kb_add(x_del_bword,     NULL, CTRL('['), CTRL('?'), 0);
  1672.     kb_add(x_del_bword,     NULL, CTRL('['), CTRL('H'), 0);
  1673.     kb_add(x_del_bword,     NULL, CTRL('['), 'h', 0);
  1674.     kb_add(x_del_fword,     NULL, CTRL('['), 'd', 0);
  1675.     kb_add(x_next_com,      NULL, CTRL('N'), 0);
  1676.     kb_add(x_next_com,      NULL, CTRL('X'), 'B', 0);
  1677.     kb_add(x_fold_lower,        NULL, CTRL('['), 'L', 0);
  1678.     kb_add(x_fold_lower,        NULL, CTRL('['), 'l', 0);
  1679.     kb_add(x_end_hist,      NULL, CTRL('['), '>', 0);
  1680.     kb_add(x_mv_end,        NULL, CTRL('E'), 0);
  1681.     /* how to handle: eot: ^_, underneath copied from original keybindings */
  1682.     kb_add(x_end_of_text,       NULL, CTRL('_'), 0);
  1683.     kb_add(x_eot_del,       NULL, CTRL('D'), 0);
  1684.     /* error */
  1685.     kb_add(x_xchg_point_mark,   NULL, CTRL('X'), CTRL('X'), 0);
  1686.     kb_add(x_expand,        NULL, CTRL('['), '*', 0);
  1687.     kb_add(x_mv_forw,       NULL, CTRL('F'), 0);
  1688.     kb_add(x_mv_forw,       NULL, CTRL('X'), 'C', 0);
  1689.     kb_add(x_mv_fword,      NULL, CTRL('['), 'f', 0);
  1690.     kb_add(x_goto_hist,     NULL, CTRL('['), 'g', 0);
  1691.     /* kill-line */
  1692.     kb_add(x_del_bword,     NULL, CTRL('W'), 0); /* not what man says */
  1693.     kb_add(x_kill,          NULL, CTRL('K'), 0);
  1694.     kb_add(x_enumerate,     NULL, CTRL('['), '?', 0);
  1695.     kb_add(x_list_comm,     NULL, CTRL('X'), '?', 0);
  1696.     kb_add(x_list_file,     NULL, CTRL('X'), CTRL('Y'), 0);
  1697.     kb_add(x_newline,       NULL, CTRL('J'), 0);
  1698.     kb_add(x_newline,       NULL, CTRL('M'), 0);
  1699.     kb_add(x_nl_next_com,       NULL, CTRL('O'), 0);
  1700.     /* no-op */
  1701.     kb_add(x_prev_histword,     NULL, CTRL('['), '.', 0);
  1702.     kb_add(x_prev_histword,     NULL, CTRL('['), '_', 0);
  1703.     /* how to handle: quote: ^^ */
  1704.     kb_add(x_draw_line,     NULL, CTRL('L'), 0);
  1705.     kb_add(x_search_char_back,  NULL, CTRL('['), CTRL(']'), 0);
  1706.     kb_add(x_search_char_forw,  NULL, CTRL(']'), 0);
  1707.     kb_add(x_search_hist,       NULL, CTRL('R'), 0);
  1708.     kb_add(x_set_mark,      NULL, CTRL('['), ' ', 0);
  1709.  #if defined(TIOCSTI)
  1710.     kb_add(x_stuff,         NULL, CTRL('T'), 0);
  1711.     /* stuff-reset */
  1712.  #else
  1713.     kb_add(x_transpose,     NULL, CTRL('T'), 0);
  1714.  #endif
  1715.     kb_add(x_prev_com,      NULL, CTRL('P'), 0);
  1716.     kb_add(x_prev_com,      NULL, CTRL('X'), 'A', 0);
  1717.     kb_add(x_fold_upper,        NULL, CTRL('['), 'U', 0);
  1718.     kb_add(x_fold_upper,        NULL, CTRL('['), 'u', 0);
  1719.     kb_add(x_literal,       NULL, CTRL('V'), 0);
  1720.     kb_add(x_literal,       NULL, CTRL('^'), 0);
  1721.     kb_add(x_yank,          NULL, CTRL('Y'), 0);
  1722.     kb_add(x_meta_yank,     NULL, CTRL('['), 'y', 0);
  1723.     /* man page ends here */
  1724.  
  1725.     /* arrow keys */
  1726.     kb_add(x_prev_com,      NULL, CTRL('['), '[', 'A', 0); /* up */
  1727.     kb_add(x_next_com,      NULL, CTRL('['), '[', 'B', 0); /* down */
  1728.     kb_add(x_mv_forw,       NULL, CTRL('['), '[', 'C', 0); /* right */
  1729.     kb_add(x_mv_back,       NULL, CTRL('['), '[', 'D', 0); /* left */
  1730.     kb_add(x_prev_com,      NULL, CTRL('['), 'O', 'A', 0); /* up */
  1731.     kb_add(x_next_com,      NULL, CTRL('['), 'O', 'B', 0); /* down */
  1732.     kb_add(x_mv_forw,       NULL, CTRL('['), 'O', 'C', 0); /* right */
  1733.     kb_add(x_mv_back,       NULL, CTRL('['), 'O', 'D', 0); /* left */
  1734.  
  1735.     /* more navigation keys */
  1736.     kb_add(x_mv_begin,      NULL, CTRL('['), '[', 'H', 0); /* home */
  1737.     kb_add(x_mv_end,        NULL, CTRL('['), '[', 'F', 0); /* end */
  1738.     kb_add(x_mv_begin,      NULL, CTRL('['), 'O', 'H', 0); /* home */
  1739.     kb_add(x_mv_end,        NULL, CTRL('['), 'O', 'F', 0); /* end */
  1740.     kb_add(x_mv_begin,      NULL, CTRL('['), '[', '1', '~', 0); /* home */
  1741.     kb_add(x_mv_end,        NULL, CTRL('['), '[', '4', '~', 0); /* end */
  1742.  
  1743.     /* can't be bound */
  1744.     kb_add(x_set_arg,       NULL, CTRL('['), '0', 0);
  1745.     kb_add(x_set_arg,       NULL, CTRL('['), '1', 0);
  1746.     kb_add(x_set_arg,       NULL, CTRL('['), '2', 0);
  1747.     kb_add(x_set_arg,       NULL, CTRL('['), '3', 0);
  1748.     kb_add(x_set_arg,       NULL, CTRL('['), '4', 0);
  1749.     kb_add(x_set_arg,       NULL, CTRL('['), '5', 0);
  1750.     kb_add(x_set_arg,       NULL, CTRL('['), '6', 0);
  1751.     kb_add(x_set_arg,       NULL, CTRL('['), '7', 0);
  1752.     kb_add(x_set_arg,       NULL, CTRL('['), '8', 0);
  1753.     kb_add(x_set_arg,       NULL, CTRL('['), '9', 0);
  1754.  
  1755.     /* ctrl arrow keys */
  1756.     kb_add(x_mv_end,        NULL, CTRL('['), '[', '1', ';', '5', 'A', 0); /* ctrl up */
  1757.     kb_add(x_mv_begin,      NULL, CTRL('['), '[', '1', ';', '5', 'B', 0); /* ctrl down */
  1758.     kb_add(x_mv_fword,      NULL, CTRL('['), '[', '1', ';', '5', 'C', 0); /* ctrl right */
  1759.     kb_add(x_mv_bword,      NULL, CTRL('['), '[', '1', ';', '5', 'D', 0); /* ctrl left */
  1760.  }
  1761.  
  1762.  void
  1763.  x_emacs_keys(X_chars *ec)
  1764.  {
  1765.     x_bind_quiet = 1;
  1766.     if (ec->erase >= 0) {
  1767.         kb_add(x_del_back, NULL, ec->erase, 0);
  1768.         kb_add(x_del_bword, NULL, CTRL('['), ec->erase, 0);
  1769.     }
  1770.     if (ec->kill >= 0)
  1771.         kb_add(x_del_line, NULL, ec->kill, 0);
  1772.     if (ec->werase >= 0)
  1773.         kb_add(x_del_bword, NULL, ec->werase, 0);
  1774.     if (ec->intr >= 0)
  1775.         kb_add(x_abort, NULL, ec->intr, 0);
  1776.     if (ec->quit >= 0)
  1777.         kb_add(x_noop, NULL, ec->quit, 0);
  1778.     x_bind_quiet = 0;
  1779.  }
  1780.  
  1781.  static int
  1782.  x_set_mark(int c)
  1783.  {
  1784.     xmp = xcp;
  1785.     return KSTD;
  1786.  }
  1787.  
  1788.  static int
  1789.  x_kill_region(int c)
  1790.  {
  1791.     int rsize;
  1792. -   char    *xr;
  1793. +   wchar_t *xr;
  1794.  
  1795.     if (xmp == NULL) {
  1796.         x_e_putc(BEL);
  1797.         return KSTD;
  1798.     }
  1799.     if (xmp > xcp) {
  1800.         rsize = xmp - xcp;
  1801.         xr = xcp;
  1802.     } else {
  1803.         rsize = xcp - xmp;
  1804.         xr = xmp;
  1805.     }
  1806.     x_goto(xr);
  1807.     x_delete(rsize, true);
  1808.     xmp = xr;
  1809.     return KSTD;
  1810.  }
  1811.  
  1812.  static int
  1813.  x_xchg_point_mark(int c)
  1814.  {
  1815. -   char    *tmp;
  1816. +   wchar_t *tmp;
  1817.  
  1818.     if (xmp == NULL) {
  1819.         x_e_putc(BEL);
  1820.         return KSTD;
  1821.     }
  1822.     tmp = xmp;
  1823.     xmp = xcp;
  1824.     x_goto( tmp );
  1825.     return KSTD;
  1826.  }
  1827.  
  1828.  static int
  1829.  x_version(int c)
  1830.  {
  1831. -   char *o_xbuf = xbuf, *o_xend = xend;
  1832. -   char *o_xbp = xbp, *o_xep = xep, *o_xcp = xcp;
  1833. +   wchar_t *o_xbuf = xbuf, *o_xend = xend;
  1834. +   wchar_t *o_xbp = xbp, *o_xep = xep, *o_xcp = xcp;
  1835.     int lim = x_lastcp() - xbp;
  1836. +   wchar_t wv[30]; /* just enough, eh? */
  1837.  
  1838. -   xbuf = xbp = xcp = (char *) ksh_version + 4;
  1839. -   xend = xep = (char *) ksh_version + 4 + strlen(ksh_version + 4);
  1840. +   mbstowcs(wv, ksh_version, sizeof(wv));
  1841. +   xbuf = xbp = xcp = wv + 4;
  1842. +   xend = xep = wv + 4 + wcslen(wv + 4);
  1843.     x_redraw(lim);
  1844.     x_flush();
  1845.  
  1846.     c = x_e_getc();
  1847.     xbuf = o_xbuf;
  1848.     xend = o_xend;
  1849.     xbp = o_xbp;
  1850.     xep = o_xep;
  1851.     xcp = o_xcp;
  1852. -   x_redraw(strlen(ksh_version));
  1853. +   x_redraw(wcslen(wv)); /*width = len because of ASCII*/
  1854.  
  1855.     if (c < 0)
  1856.         return KSTD;
  1857.     /* This is what at&t ksh seems to do...  Very bizarre */
  1858.     if (c != ' ')
  1859.         x_e_ungetc(c);
  1860.  
  1861.     return KSTD;
  1862.  }
  1863.  
  1864.  static int
  1865.  x_noop(int c)
  1866.  {
  1867.     return KSTD;
  1868.  }
  1869.  
  1870.  /*
  1871.   * File/command name completion routines
  1872.   */
  1873.  
  1874.  static int
  1875.  x_comp_comm(int c)
  1876.  {
  1877.     do_complete(XCF_COMMAND, CT_COMPLETE);
  1878.     return KSTD;
  1879.  }
  1880.  static int
  1881.  x_list_comm(int c)
  1882.  {
  1883.     do_complete(XCF_COMMAND, CT_LIST);
  1884.     return KSTD;
  1885.  }
  1886.  static int
  1887.  x_complete(int c)
  1888.  {
  1889.     do_complete(XCF_COMMAND_FILE, CT_COMPLETE);
  1890.     return KSTD;
  1891.  }
  1892.  static int
  1893.  x_enumerate(int c)
  1894.  {
  1895.     do_complete(XCF_COMMAND_FILE, CT_LIST);
  1896.     return KSTD;
  1897.  }
  1898.  static int
  1899.  x_comp_file(int c)
  1900.  {
  1901.     do_complete(XCF_FILE, CT_COMPLETE);
  1902.     return KSTD;
  1903.  }
  1904.  static int
  1905.  x_list_file(int c)
  1906.  {
  1907.     do_complete(XCF_FILE, CT_LIST);
  1908.     return KSTD;
  1909.  }
  1910.  static int
  1911.  x_comp_list(int c)
  1912.  {
  1913.     do_complete(XCF_COMMAND_FILE, CT_COMPLIST);
  1914.     return KSTD;
  1915.  }
  1916.  static int
  1917.  x_expand(int c)
  1918.  {
  1919.     char **words;
  1920.     int nwords = 0;
  1921.     int start, end;
  1922.     int is_command;
  1923.     int i;
  1924.  
  1925. +   /* XXX: broken because xbuf is wchar_t* */
  1926.     nwords = x_cf_glob(XCF_FILE, xbuf, xep - xbuf, xcp - xbuf,
  1927.         &start, &end, &words, &is_command);
  1928.  
  1929.     if (nwords == 0) {
  1930.         x_e_putc(BEL);
  1931.         return KSTD;
  1932.     }
  1933.  
  1934.     x_goto(xbuf + start);
  1935.     x_delete(end - start, false);
  1936.     for (i = 0; i < nwords;) {
  1937.         if (x_escape(words[i], strlen(words[i]), x_do_ins) < 0 ||
  1938.             (++i < nwords && x_ins(" ") < 0)) {
  1939.             x_e_putc(BEL);
  1940.             return KSTD;
  1941.         }
  1942.     }
  1943.     x_adjust();
  1944.  
  1945.     return KSTD;
  1946.  }
  1947.  
  1948.  /* type == 0 for list, 1 for complete and 2 for complete-list */
  1949.  static void
  1950.  do_complete(int flags, /* XCF_{COMMAND,FILE,COMMAND_FILE} */
  1951.      Comp_type type)
  1952.  {
  1953.     char **words;
  1954.     int nwords;
  1955.     int start, end, nlen, olen;
  1956.     int is_command;
  1957.     int completed = 0;
  1958.  
  1959. +   /* XXX: broken because xbuf is wchar_t* */
  1960.     nwords = x_cf_glob(flags, xbuf, xep - xbuf, xcp - xbuf,
  1961.         &start, &end, &words, &is_command);
  1962.     /* no match */
  1963.     if (nwords == 0) {
  1964.         x_e_putc(BEL);
  1965.         return;
  1966.     }
  1967.  
  1968.     if (type == CT_LIST) {
  1969.         x_print_expansions(nwords, words, is_command);
  1970.         x_redraw(0);
  1971.         x_free_words(nwords, words);
  1972.         return;
  1973.     }
  1974.  
  1975.     olen = end - start;
  1976.     nlen = x_longest_prefix(nwords, words);
  1977.     /* complete */
  1978.     if (nwords == 1 || nlen > olen) {
  1979.         x_goto(xbuf + start);
  1980.         x_delete(olen, false);
  1981.         x_escape(words[0], nlen, x_do_ins);
  1982.         x_adjust();
  1983.         completed = 1;
  1984.     }
  1985.     /* add space if single non-dir match */
  1986.     if (nwords == 1 && words[0][nlen - 1] != '/') {
  1987.         x_ins(" ");
  1988.         completed = 1;
  1989.     }
  1990.  
  1991.     if (type == CT_COMPLIST && !completed) {
  1992.         x_print_expansions(nwords, words, is_command);
  1993.         completed = 1;
  1994.     }
  1995.  
  1996.     if (completed)
  1997.         x_redraw(0);
  1998.  
  1999.     x_free_words(nwords, words);
  2000.  }
  2001.  
  2002.  /* NAME:
  2003.   *      x_adjust - redraw the line adjusting starting point etc.
  2004.   *
  2005.   * DESCRIPTION:
  2006.   *      This function is called when we have exceeded the bounds
  2007.   *      of the edit window.  It increments x_adj_done so that
  2008.   *      functions like x_ins and x_delete know that we have been
  2009.   *      called and can skip the x_bs() stuff which has already
  2010.   *      been done by x_redraw.
  2011.   *
  2012.   * RETURN VALUE:
  2013.   *      None
  2014.   */
  2015.  
  2016.  static void
  2017.  x_adjust(void)
  2018.  {
  2019.     x_adj_done++;           /* flag the fact that we were called. */
  2020.     /*
  2021.      * we had a problem if the prompt length > xx_cols / 2
  2022.      */
  2023.     if ((xbp = xcp - (x_displen / 2)) < xbuf)
  2024.         xbp = xbuf;
  2025.     xlp_valid = false;
  2026.     x_redraw(xx_cols);
  2027.     x_flush();
  2028.  }
  2029.  
  2030.  static int unget_char = -1;
  2031.  
  2032.  static void
  2033.  x_e_ungetc(int c)
  2034.  {
  2035.     unget_char = c;
  2036.  }
  2037.  
  2038.  static int
  2039.  x_e_getc(void)
  2040.  {
  2041.     int c;
  2042.  
  2043.     if (unget_char >= 0) {
  2044.         c = unget_char;
  2045.         unget_char = -1;
  2046.     } else if (macro_args) {
  2047.         c = *macro_args++;
  2048.         if (!c) {
  2049.             macro_args = NULL;
  2050.             c = x_getc();
  2051.         }
  2052.     } else
  2053.         c = x_getc();
  2054.  
  2055.     return c;
  2056.  }
  2057.  
  2058.  static void
  2059. -x_e_putc(int c)
  2060. +x_e_putc(wchar_t c)
  2061.  {
  2062. -   if (c == '\r' || c == '\n')
  2063. +   int w;
  2064. +   char mb[MB_LEN_MAX + 1];
  2065. +
  2066. +   if (c == L'\r' || c == L'\n')
  2067.         x_col = 0;
  2068.     if (x_col < xx_cols) {
  2069. -       x_putc(c);
  2070.         switch (c) {
  2071.         case BEL:
  2072. +           x_putc(BEL);
  2073.             break;
  2074. -       case '\r':
  2075. -       case '\n':
  2076. -           break;
  2077. -       case '\b':
  2078. +       case L'\b':
  2079. +           x_putc('\b');
  2080.             x_col--;
  2081.             break;
  2082.         default:
  2083. -           if (!isu8cont(c))
  2084. -               x_col++;
  2085. +           if ((w = wctomb(mb, c)) == -1) {
  2086. +               x_putc(BEL);
  2087. +               break;
  2088. +           }
  2089. +           mb[w] = '\0';
  2090. +           x_puts(mb);
  2091. +           if ((w = wcwidth(c)) != -1)
  2092. +               x_col += w;
  2093.             break;
  2094.         }
  2095.     }
  2096.     if (x_adj_ok && (x_col < 0 || x_col >= (xx_cols - 2)))
  2097.         x_adjust();
  2098.  }
  2099.  
  2100.  #ifdef DEBUG
  2101.  static int
  2102.  x_debug_info(int c)
  2103.  {
  2104.     x_flush();
  2105.     shellf("\nksh debug:\n");
  2106.     shellf("\tx_col == %d,\t\tx_cols == %d,\tx_displen == %d\n",
  2107.         x_col, xx_cols, x_displen);
  2108.     shellf("\txcp == 0x%lx,\txep == 0x%lx\n", (long) xcp, (long) xep);
  2109.     shellf("\txbp == 0x%lx,\txbuf == 0x%lx\n", (long) xbp, (long) xbuf);
  2110.     shellf("\txlp == 0x%lx\n", (long) xlp);
  2111.     shellf("\txlp == 0x%lx\n", (long) x_lastcp());
  2112.     shellf("\n");
  2113.     x_redraw(-1);
  2114.     return 0;
  2115.  }
  2116.  #endif
  2117.  
  2118.  static void
  2119.  x_e_puts(const char *s)
  2120.  {
  2121.     int adj = x_adj_done;
  2122. +   size_t  i, n = strlen(s);
  2123. +   wchar_t *pwcs, *p;
  2124.  
  2125. -   while (*s && adj == x_adj_done)
  2126. -       x_e_putc(*s++);
  2127. +   if ((i = mbstowcs(NULL, s, n)) == (size_t)-1) {
  2128. +       return; /*XXX*/
  2129. +   }
  2130. +   if ((pwcs = (wchar_t *)calloc(i+1, sizeof(wchar_t))) == NULL)
  2131. +       return; /*XXX*/
  2132. +   (void)mbstowcs(pwcs, s, n);
  2133. +   p = pwcs;
  2134. +   while (*p && adj == x_adj_done)
  2135. +       x_e_putc(*p++);
  2136. +   free(pwcs);
  2137.  }
  2138.  
  2139.  /* NAME:
  2140.   *      x_set_arg - set an arg value for next function
  2141.   *
  2142.   * DESCRIPTION:
  2143.   *      This is a simple implementation of M-[0-9].
  2144.   *
  2145.   * RETURN VALUE:
  2146.   *      KSTD
  2147.   */
  2148.  
  2149.  static int
  2150.  x_set_arg(int c)
  2151.  {
  2152.     int n = 0;
  2153.     int first = 1;
  2154.  
  2155.     for (; c >= 0 && isdigit(c); c = x_e_getc(), first = 0)
  2156.         n = n * 10 + (c - '0');
  2157.     if (c < 0 || first) {
  2158.         x_e_putc(BEL);
  2159.         x_arg = 1;
  2160.         x_arg_defaulted = 1;
  2161.     } else {
  2162.         x_e_ungetc(c);
  2163.         x_arg = n;
  2164.         x_arg_defaulted = 0;
  2165.         x_arg_set = 1;
  2166.     }
  2167.     return KSTD;
  2168.  }
  2169.  
  2170.  
  2171.  /* Comment or uncomment the current line. */
  2172.  static int
  2173.  x_comment(int c)
  2174.  {
  2175.     int oldsize = x_size_str(xbuf);
  2176.     int len = xep - xbuf;
  2177. -   int ret = x_do_comment(xbuf, xend - xbuf, &len);
  2178. +   int ret = x_do_comment(xbuf, xend - xbuf, &len); /* XXX wchar_t */
  2179.  
  2180.     if (ret < 0)
  2181.         x_e_putc(BEL);
  2182.     else {
  2183.         xep = xbuf + len;
  2184.         *xep = '\0';
  2185.         xcp = xbp = xbuf;
  2186.         x_redraw(oldsize);
  2187.         if (ret > 0)
  2188.             return x_newline('\n');
  2189.     }
  2190.     return KSTD;
  2191.  }
  2192.  
  2193.  
  2194.  /* NAME:
  2195.   *      x_prev_histword - recover word from prev command
  2196.   *
  2197.   * DESCRIPTION:
  2198.   *      This function recovers the last word from the previous
  2199.   *      command and inserts it into the current edit line.  If a
  2200.   *      numeric arg is supplied then the n'th word from the
  2201.   *      start of the previous command is used.
  2202.   *
  2203.   *      Bound to M-.
  2204.   *
  2205.   * RETURN VALUE:
  2206.   *      KSTD
  2207.   */
  2208.  
  2209.  static int
  2210.  x_prev_histword(int c)
  2211.  {
  2212.     char *rcp;
  2213.     char *cp;
  2214.  
  2215.     cp = *histptr;
  2216.     if (!cp)
  2217.         x_e_putc(BEL);
  2218.     else if (x_arg_defaulted) {
  2219.         rcp = &cp[strlen(cp) - 1];
  2220.         /*
  2221.          * ignore white-space after the last word
  2222.          */
  2223.         while (rcp > cp && is_cfs(*rcp))
  2224.             rcp--;
  2225.         while (rcp > cp && !is_cfs(*rcp))
  2226.             rcp--;
  2227.         if (is_cfs(*rcp))
  2228.             rcp++;
  2229.         x_ins(rcp);
  2230.     } else {
  2231.         int c;
  2232.  
  2233.         rcp = cp;
  2234.         /*
  2235.          * ignore white-space at start of line
  2236.          */
  2237.         while (*rcp && is_cfs(*rcp))
  2238.             rcp++;
  2239.         while (x_arg-- > 1) {
  2240.             while (*rcp && !is_cfs(*rcp))
  2241.                 rcp++;
  2242.             while (*rcp && is_cfs(*rcp))
  2243.                 rcp++;
  2244.         }
  2245.         cp = rcp;
  2246.         while (*rcp && !is_cfs(*rcp))
  2247.             rcp++;
  2248.         c = *rcp;
  2249.         *rcp = '\0';
  2250.         x_ins(cp);
  2251.         *rcp = c;
  2252.     }
  2253.     return KSTD;
  2254.  }
  2255.  
  2256.  /* Uppercase N(1) words */
  2257.  static int
  2258.  x_fold_upper(int c)
  2259.  {
  2260.     return x_fold_case('U');
  2261.  }
  2262.  
  2263.  /* Lowercase N(1) words */
  2264.  static int
  2265.  x_fold_lower(int c)
  2266.  {
  2267.     return x_fold_case('L');
  2268.  }
  2269.  
  2270.  /* Lowercase N(1) words */
  2271.  static int
  2272.  x_fold_capitalize(int c)
  2273.  {
  2274.     return x_fold_case('C');
  2275.  }
  2276.  
  2277.  /* NAME:
  2278.   *      x_fold_case - convert word to UPPER/lower/Capital case
  2279.   *
  2280.   * DESCRIPTION:
  2281.   *      This function is used to implement M-U,M-u,M-L,M-l,M-C and M-c
  2282.   *      to UPPER case, lower case or Capitalize words.
  2283.   *
  2284.   * RETURN VALUE:
  2285.   *      None
  2286.   */
  2287.  
  2288.  static int
  2289.  x_fold_case(int c)
  2290.  {
  2291. -   char *cp = xcp;
  2292. +   wchar_t *cp = xcp;
  2293.  
  2294.     if (cp == xep) {
  2295.         x_e_putc(BEL);
  2296.         return KSTD;
  2297.     }
  2298.     while (x_arg--) {
  2299.         /*
  2300.          * first skip over any white-space
  2301.          */
  2302.         while (cp != xep && is_mfs(*cp))
  2303.             cp++;
  2304.         /*
  2305.          * do the first char on its own since it may be
  2306.          * a different action than for the rest.
  2307.          */
  2308.         if (cp != xep) {
  2309.             if (c == 'L') {     /* lowercase */
  2310. -               if (isupper((unsigned char)*cp))
  2311. -                   *cp = tolower((unsigned char)*cp);
  2312. +               if (iswupper(*cp))
  2313. +                   *cp = towlower(*cp);
  2314.             } else {        /* uppercase, capitalize */
  2315. -               if (islower((unsigned char)*cp))
  2316. -                   *cp = toupper((unsigned char)*cp);
  2317. +               if (iswlower(*cp))
  2318. +                   *cp = towupper(*cp);
  2319.             }
  2320.             cp++;
  2321.         }
  2322.         /*
  2323.          * now for the rest of the word
  2324.          */
  2325.         while (cp != xep && !is_mfs(*cp)) {
  2326.             if (c == 'U') {     /* uppercase */
  2327. -               if (islower((unsigned char)*cp))
  2328. -                   *cp = toupper((unsigned char)*cp);
  2329. +               if (iswlower(*cp))
  2330. +                   *cp = towupper(*cp);
  2331.             } else {        /* lowercase, capitalize */
  2332. -               if (isupper((unsigned char)*cp))
  2333. -                   *cp = tolower((unsigned char)*cp);
  2334. +               if (iswupper(*cp))
  2335. +                   *cp = towlower(*cp);
  2336.             }
  2337.             cp++;
  2338.         }
  2339.     }
  2340.     x_goto(cp);
  2341.     return KSTD;
  2342.  }
  2343.  
  2344.  /* NAME:
  2345.   *      x_lastcp - last visible byte
  2346.   *
  2347.   * SYNOPSIS:
  2348.   *      x_lastcp()
  2349.   *
  2350.   * DESCRIPTION:
  2351.   *      This function returns a pointer to that byte in the
  2352.   *      edit buffer that will be the last displayed on the
  2353.   *      screen.  The sequence:
  2354.   *
  2355.   *      for (cp = x_lastcp(); cp > xcp; cp)
  2356.   *        x_bs(*--cp);
  2357.   *
  2358.   *      Will position the cursor correctly on the screen.
  2359.   *
  2360.   * RETURN VALUE:
  2361.   *      cp or NULL
  2362.   */
  2363.  
  2364. -static char *
  2365. +static wchar_t *
  2366.  x_lastcp(void)
  2367.  {
  2368. -   char *rcp;
  2369. +   wchar_t *rcp;
  2370.     int i;
  2371.  
  2372.     if (!xlp_valid) {
  2373.         for (i = 0, rcp = xbp; rcp < xep && i < x_displen; rcp++)
  2374. -           i += x_size((unsigned char)*rcp);
  2375. +           i += x_size(*rcp);
  2376.         xlp = rcp;
  2377.     }
  2378.     xlp_valid = true;
  2379.     return (xlp);
  2380.  }
  2381.  
  2382.  #endif /* EDIT */
Add Comment
Please, Sign In to add comment