From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from groucho.cs.psu.edu ([130.203.2.10]) by archone.tamu.edu with SMTP id <45317>; Sun, 5 Apr 1992 03:56:11 -0500 Received: by groucho.cs.psu.edu id <2581>; Sun, 5 Apr 1992 04:55:58 -0400 From: Scott Schwartz To: rc@archone.tamu.edu Subject: file completion Message-Id: <92Apr5.045558edt.2581@groucho.cs.psu.edu> Date: Sun, 5 Apr 1992 03:55:49 -0500 In honor of daylight savings time, here's a quick kludge to do csh style file completion in rc. Feeback solicited. *** 1.1 1992/04/05 08:49:55 --- comp.c 1992/04/05 08:47:41 *************** *** 0 **** --- 1,52 ---- + #include "comp.h" + #include + + static int fdunread (int fd, char* buf, SIZE_T len); + + /* read_with_completion -- if a line doesn't end with newline, + try and expand the last item to match things in the filesystem. + this function does it incorrectly, by looking for a whitespace + to delimit the final 'item'. we really should parse the expression + and do completion on the last item computed in that way, and taking + error recovery into account. Sanjeev is writing his dissertation on + an incremental parser --- ask him how to do it. */ + + extern int read_with_completion (int fd, char* buf, SIZE_T len) { + int r, n, i; + List* list; + char* meta; + char* p; + + while (1) { + r = rc_read (fd, buf, len); + if (r <= 0 || buf[r-1] == '\n') + return r; + else { + buf[r] = '*'; /* XXX - caller has to make sure buf is big enough */ + buf[r+1] = 0; + p = strrchr(buf, ' '); + if (p == 0) p = buf; else ++p; + n = r - (p-buf); + meta = ealloc(n+2); + clear(meta, n+2); + meta[n] = 1; + list = glob(word(p, meta)); /* XXX - what to return on failure? */ + fprint (1, " # (%L)\n", list, " "); + efree(meta); + fdunread (fd, buf, r); + } + } + /* NOTREACHED */ + } + + static int fdunread (int fd, char* buf, SIZE_T len) { + int r; + while (len--) { + r = ioctl (fd, TIOCSTI, buf++); + if (r < 0) { + uerror("ioctl/TIOCSTI"); + return r; + } + } + return 0; + } *** 1.1 1992/04/04 20:14:03 --- input.c 1992/04/04 20:16:51 *************** *** 111,117 **** --- 111,121 ---- } else #endif { + #ifdef COMPLETION + long /*ssize_t*/ r = read_with_completion(istack->fd, inbuf + 2, BUFSIZE); + #else long /*ssize_t*/ r = rc_read(istack->fd, inbuf + 2, BUFSIZE); + #endif if (r < 0) { if (errno == EINTR) continue; /* Suppose it was interrupted by a signal */