1 #include <sys/cdefs.h> 2 __FBSDID("$FreeBSD$"); 3 4 #include <stdio.h> 5 #include <string.h> 6 #include <signal.h> 7 #include <sys/wait.h> 8 #include <err.h> 9 #include <ctype.h> 10 #include <stdlib.h> 11 #include <unistd.h> 12 #include <dirent.h> 13 #include <limits.h> 14 #include <locale.h> 15 16 #include "../histedit.h" 17 18 19 static int continuation; 20 volatile sig_atomic_t gotsig; 21 static const char hfile[] = ".whistory"; 22 23 static wchar_t * 24 prompt(EditLine *el) 25 { 26 static wchar_t a[] = L"\1\033[7m\1Edit$\1\033[0m\1 "; 27 static wchar_t b[] = L"Edit> "; 28 29 return continuation ? b : a; 30 } 31 32 33 static void 34 sig(int i) 35 { 36 gotsig = i; 37 } 38 39 const char * 40 my_wcstombs(const wchar_t *wstr) 41 { 42 static struct { 43 char *str; 44 int len; 45 } buf; 46 47 int needed = wcstombs(0, wstr, 0) + 1; 48 if (needed > buf.len) { 49 buf.str = malloc(needed); 50 buf.len = needed; 51 } 52 wcstombs(buf.str, wstr, needed); 53 buf.str[needed - 1] = 0; 54 55 return buf.str; 56 } 57 58 59 static unsigned char 60 complete(EditLine *el, int ch) 61 { 62 DIR *dd = opendir("."); 63 struct dirent *dp; 64 const wchar_t *ptr; 65 char *buf, *bptr; 66 const LineInfoW *lf = el_wline(el); 67 int len, mblen, i; 68 unsigned char res = 0; 69 wchar_t dir[1024]; 70 71 /* Find the last word */ 72 for (ptr = lf->cursor -1; !iswspace(*ptr) && ptr > lf->buffer; --ptr) 73 continue; 74 len = lf->cursor - ++ptr; 75 76 /* Convert last word to multibyte encoding, so we can compare to it */ 77 wctomb(NULL, 0); /* Reset shift state */ 78 mblen = MB_LEN_MAX * len + 1; 79 buf = bptr = malloc(mblen); 80 if (buf == NULL) 81 err(1, "malloc"); 82 for (i = 0; i < len; ++i) { 83 /* Note: really should test for -1 return from wctomb */ 84 bptr += wctomb(bptr, ptr[i]); 85 } 86 *bptr = 0; /* Terminate multibyte string */ 87 mblen = bptr - buf; 88 89 /* Scan directory for matching name */ 90 for (dp = readdir(dd); dp != NULL; dp = readdir(dd)) { 91 if (mblen > strlen(dp->d_name)) 92 continue; 93 if (strncmp(dp->d_name, buf, mblen) == 0) { 94 mbstowcs(dir, &dp->d_name[mblen], 95 sizeof(dir) / sizeof(*dir)); 96 if (el_winsertstr(el, dir) == -1) 97 res = CC_ERROR; 98 else 99 res = CC_REFRESH; 100 break; 101 } 102 } 103 104 closedir(dd); 105 free(buf); 106 return res; 107 } 108 109 110 int 111 main(int argc, char *argv[]) 112 { 113 EditLine *el = NULL; 114 int numc, ncontinuation; 115 const wchar_t *line; 116 TokenizerW *tok; 117 HistoryW *hist; 118 HistEventW ev; 119 #ifdef DEBUG 120 int i; 121 #endif 122 123 setlocale(LC_ALL, ""); 124 125 (void)signal(SIGINT, sig); 126 (void)signal(SIGQUIT, sig); 127 (void)signal(SIGHUP, sig); 128 (void)signal(SIGTERM, sig); 129 130 hist = history_winit(); /* Init built-in history */ 131 history_w(hist, &ev, H_SETSIZE, 100); /* Remember 100 events */ 132 history_w(hist, &ev, H_LOAD, hfile); 133 134 tok = tok_winit(NULL); /* Init the tokenizer */ 135 136 el = el_init(argv[0], stdin, stdout, stderr); 137 138 el_wset(el, EL_EDITOR, L"vi"); /* Default editor is vi */ 139 el_wset(el, EL_SIGNAL, 1); /* Handle signals gracefully */ 140 el_wset(el, EL_PROMPT_ESC, prompt, '\1'); /* Set the prompt function */ 141 142 el_wset(el, EL_HIST, history_w, hist); /* FIXME - history_w? */ 143 144 /* Add a user-defined function */ 145 el_wset(el, EL_ADDFN, L"ed-complete", L"Complete argument", complete); 146 147 /* Bind <tab> to it */ 148 el_wset(el, EL_BIND, L"^I", L"ed-complete", NULL); 149 150 /* 151 * Bind j, k in vi command mode to previous and next line, instead 152 * of previous and next history. 153 */ 154 el_wset(el, EL_BIND, L"-a", L"k", L"ed-prev-line", NULL); 155 el_wset(el, EL_BIND, L"-a", L"j", L"ed-next-line", NULL); 156 157 /* Source the user's defaults file. */ 158 el_source(el, NULL); 159 160 while((line = el_wgets(el, &numc)) != NULL && numc != 0) { 161 int ac, cc, co, rc; 162 const wchar_t **av; 163 164 const LineInfoW *li; 165 li = el_wline(el); 166 167 #ifdef DEBUG 168 (void)fwprintf(stderr, L"==> got %d %ls", numc, line); 169 (void)fwprintf(stderr, L" > li `%.*ls_%.*ls'\n", 170 (li->cursor - li->buffer), li->buffer, 171 (li->lastchar - 1 - li->cursor), 172 (li->cursor >= li->lastchar) ? L"" : li->cursor); 173 #endif 174 175 if (gotsig) { 176 (void)fprintf(stderr, "Got signal %d.\n", (int)gotsig); 177 gotsig = 0; 178 el_reset(el); 179 } 180 181 if(!continuation && numc == 1) 182 continue; /* Only got a linefeed */ 183 184 ac = cc = co = 0; 185 ncontinuation = tok_wline(tok, li, &ac, &av, &cc, &co); 186 if (ncontinuation < 0) { 187 (void) fprintf(stderr, "Internal error\n"); 188 continuation = 0; 189 continue; 190 } 191 192 #ifdef DEBUG 193 (void)fprintf(stderr, " > nc %d ac %d cc %d co %d\n", 194 ncontinuation, ac, cc, co); 195 #endif 196 history_w(hist, &ev, continuation ? H_APPEND : H_ENTER, line); 197 198 continuation = ncontinuation; 199 ncontinuation = 0; 200 if(continuation) 201 continue; 202 203 #ifdef DEBUG 204 for (i = 0; i < ac; ++i) { 205 (void)fwprintf(stderr, L" > arg# %2d ", i); 206 if (i != cc) 207 (void)fwprintf(stderr, L"`%ls'\n", av[i]); 208 else 209 (void)fwprintf(stderr, L"`%.*ls_%ls'\n", 210 co, av[i], av[i] + co); 211 } 212 #endif 213 214 if (wcscmp (av[0], L"history") == 0) { 215 switch(ac) { 216 case 1: 217 for(rc = history_w(hist, &ev, H_LAST); 218 rc != -1; 219 rc = history_w(hist, &ev, H_PREV)) 220 (void)fwprintf(stdout, L"%4d %ls", 221 ev.num, ev.str); 222 break; 223 case 2: 224 if (wcscmp(av[1], L"clear") == 0) 225 history_w(hist, &ev, H_CLEAR); 226 else 227 goto badhist; 228 break; 229 case 3: 230 if (wcscmp(av[1], L"load") == 0) 231 history_w(hist, &ev, H_LOAD, 232 my_wcstombs(av[2])); 233 else if (wcscmp(av[1], L"save") == 0) 234 history_w(hist, &ev, H_SAVE, 235 my_wcstombs(av[2])); 236 else 237 goto badhist; 238 break; 239 badhist: 240 default: 241 (void)fprintf(stderr, 242 "Bad history arguments\n"); 243 break; 244 } 245 } else if (el_wparse(el, ac, av) == -1) { 246 switch (fork()) { 247 case 0: { 248 Tokenizer *ntok = tok_init(NULL); 249 int nargc; 250 const char **nav; 251 tok_str(ntok, my_wcstombs(line), &nargc, &nav); 252 execvp(nav[0],(char **)nav); 253 perror(nav[0]); 254 _exit(1); 255 /* NOTREACHED */ 256 break; 257 } 258 case -1: 259 perror("fork"); 260 break; 261 default: 262 if (wait(&rc) == -1) 263 perror("wait"); 264 (void)fprintf(stderr, "Exit %x\n", rc); 265 break; 266 } 267 } 268 269 tok_wreset(tok); 270 } 271 272 el_end(el); 273 tok_wend(tok); 274 history_w(hist, &ev, H_SAVE, hfile); 275 history_wend(hist); 276 277 fprintf(stdout, "\n"); 278 return 0; 279 } 280 281 282