1*2718b568SThomas Cort /* $NetBSD: edit.c,v 1.25 2010/06/05 03:02:37 sjg Exp $ */
2*2718b568SThomas Cort
3*2718b568SThomas Cort /*
4*2718b568SThomas Cort * Command line editing - common code
5*2718b568SThomas Cort *
6*2718b568SThomas Cort */
7*2718b568SThomas Cort #include <sys/cdefs.h>
8*2718b568SThomas Cort
9*2718b568SThomas Cort #ifndef lint
10*2718b568SThomas Cort __RCSID("$NetBSD: edit.c,v 1.25 2010/06/05 03:02:37 sjg Exp $");
11*2718b568SThomas Cort #endif
12*2718b568SThomas Cort
13*2718b568SThomas Cort
14*2718b568SThomas Cort #include "config.h"
15*2718b568SThomas Cort #ifdef EDIT
16*2718b568SThomas Cort
17*2718b568SThomas Cort #include "sh.h"
18*2718b568SThomas Cort #include "tty.h"
19*2718b568SThomas Cort #define EXTERN
20*2718b568SThomas Cort #include "edit.h"
21*2718b568SThomas Cort #undef EXTERN
22*2718b568SThomas Cort #ifdef OS_SCO /* SCO Unix 3.2v4.1 */
23*2718b568SThomas Cort # include <sys/stream.h> /* needed for <sys/ptem.h> */
24*2718b568SThomas Cort # include <sys/ptem.h> /* needed for struct winsize */
25*2718b568SThomas Cort #endif /* OS_SCO */
26*2718b568SThomas Cort #include <sys/ioctl.h>
27*2718b568SThomas Cort #include <ctype.h>
28*2718b568SThomas Cort #include "ksh_stat.h"
29*2718b568SThomas Cort
30*2718b568SThomas Cort
31*2718b568SThomas Cort #if defined(TIOCGWINSZ)
32*2718b568SThomas Cort static RETSIGTYPE x_sigwinch ARGS((int sig));
33*2718b568SThomas Cort static int got_sigwinch;
34*2718b568SThomas Cort static void check_sigwinch ARGS((void));
35*2718b568SThomas Cort #endif /* TIOCGWINSZ */
36*2718b568SThomas Cort
37*2718b568SThomas Cort static int x_file_glob ARGS((int flags, const char *str, int slen,
38*2718b568SThomas Cort char ***wordsp));
39*2718b568SThomas Cort static int x_command_glob ARGS((int flags, const char *str, int slen,
40*2718b568SThomas Cort char ***wordsp));
41*2718b568SThomas Cort static int x_locate_word ARGS((const char *buf, int buflen, int pos,
42*2718b568SThomas Cort int *startp, int *is_command));
43*2718b568SThomas Cort
44*2718b568SThomas Cort static char vdisable_c;
45*2718b568SThomas Cort
46*2718b568SThomas Cort
47*2718b568SThomas Cort /* Called from main */
48*2718b568SThomas Cort void
x_init()49*2718b568SThomas Cort x_init()
50*2718b568SThomas Cort {
51*2718b568SThomas Cort /* set to -2 to force initial binding */
52*2718b568SThomas Cort edchars.erase = edchars.kill = edchars.intr = edchars.quit
53*2718b568SThomas Cort = edchars.eof = -2;
54*2718b568SThomas Cort /* default value for deficient systems */
55*2718b568SThomas Cort edchars.werase = 027; /* ^W */
56*2718b568SThomas Cort
57*2718b568SThomas Cort #ifdef TIOCGWINSZ
58*2718b568SThomas Cort # ifdef SIGWINCH
59*2718b568SThomas Cort if (setsig(&sigtraps[SIGWINCH], x_sigwinch, SS_RESTORE_ORIG|SS_SHTRAP))
60*2718b568SThomas Cort sigtraps[SIGWINCH].flags |= TF_SHELL_USES;
61*2718b568SThomas Cort # endif /* SIGWINCH */
62*2718b568SThomas Cort got_sigwinch = 1; /* force initial check */
63*2718b568SThomas Cort check_sigwinch();
64*2718b568SThomas Cort #endif /* TIOCGWINSZ */
65*2718b568SThomas Cort
66*2718b568SThomas Cort #ifdef EMACS
67*2718b568SThomas Cort x_init_emacs();
68*2718b568SThomas Cort #endif /* EMACS */
69*2718b568SThomas Cort
70*2718b568SThomas Cort /* Bizarreness to figure out how to disable
71*2718b568SThomas Cort * a struct termios.c_cc[] char
72*2718b568SThomas Cort */
73*2718b568SThomas Cort #ifdef _POSIX_VDISABLE
74*2718b568SThomas Cort if (_POSIX_VDISABLE >= 0)
75*2718b568SThomas Cort vdisable_c = (char) _POSIX_VDISABLE;
76*2718b568SThomas Cort else
77*2718b568SThomas Cort /* `feature not available' */
78*2718b568SThomas Cort vdisable_c = (char) 0377;
79*2718b568SThomas Cort #else
80*2718b568SThomas Cort # if defined(HAVE_PATHCONF) && defined(_PC_VDISABLE)
81*2718b568SThomas Cort vdisable_c = fpathconf(tty_fd, _PC_VDISABLE);
82*2718b568SThomas Cort # else
83*2718b568SThomas Cort vdisable_c = (char) 0377; /* default to old BSD value */
84*2718b568SThomas Cort # endif
85*2718b568SThomas Cort #endif /* _POSIX_VDISABLE */
86*2718b568SThomas Cort }
87*2718b568SThomas Cort
88*2718b568SThomas Cort #if defined(TIOCGWINSZ)
89*2718b568SThomas Cort static RETSIGTYPE
x_sigwinch(sig)90*2718b568SThomas Cort x_sigwinch(sig)
91*2718b568SThomas Cort int sig;
92*2718b568SThomas Cort {
93*2718b568SThomas Cort got_sigwinch = 1;
94*2718b568SThomas Cort return RETSIGVAL;
95*2718b568SThomas Cort }
96*2718b568SThomas Cort
97*2718b568SThomas Cort static void
check_sigwinch(void)98*2718b568SThomas Cort check_sigwinch ARGS((void))
99*2718b568SThomas Cort {
100*2718b568SThomas Cort if (got_sigwinch) {
101*2718b568SThomas Cort struct winsize ws;
102*2718b568SThomas Cort
103*2718b568SThomas Cort got_sigwinch = 0;
104*2718b568SThomas Cort if (procpid == kshpid && ioctl(tty_fd, TIOCGWINSZ, &ws) >= 0) {
105*2718b568SThomas Cort struct tbl *vp;
106*2718b568SThomas Cort
107*2718b568SThomas Cort /* Do NOT export COLUMNS/LINES. Many applications
108*2718b568SThomas Cort * check COLUMNS/LINES before checking ws.ws_col/row,
109*2718b568SThomas Cort * so if the app is started with C/L in the environ
110*2718b568SThomas Cort * and the window is then resized, the app won't
111*2718b568SThomas Cort * see the change cause the environ doesn't change.
112*2718b568SThomas Cort */
113*2718b568SThomas Cort if (ws.ws_col) {
114*2718b568SThomas Cort x_cols = ws.ws_col < MIN_COLS ? MIN_COLS
115*2718b568SThomas Cort : ws.ws_col;
116*2718b568SThomas Cort
117*2718b568SThomas Cort if ((vp = typeset("COLUMNS", 0, 0, 0, 0)))
118*2718b568SThomas Cort setint(vp, (long) ws.ws_col);
119*2718b568SThomas Cort }
120*2718b568SThomas Cort if (ws.ws_row
121*2718b568SThomas Cort && (vp = typeset("LINES", 0, 0, 0, 0)))
122*2718b568SThomas Cort setint(vp, (long) ws.ws_row);
123*2718b568SThomas Cort }
124*2718b568SThomas Cort }
125*2718b568SThomas Cort }
126*2718b568SThomas Cort #endif /* TIOCGWINSZ */
127*2718b568SThomas Cort
128*2718b568SThomas Cort /*
129*2718b568SThomas Cort * read an edited command line
130*2718b568SThomas Cort */
131*2718b568SThomas Cort int
x_read(buf,len)132*2718b568SThomas Cort x_read(buf, len)
133*2718b568SThomas Cort char *buf;
134*2718b568SThomas Cort size_t len;
135*2718b568SThomas Cort {
136*2718b568SThomas Cort int i;
137*2718b568SThomas Cort
138*2718b568SThomas Cort x_mode(TRUE);
139*2718b568SThomas Cort #ifdef EMACS
140*2718b568SThomas Cort if (Flag(FEMACS) || Flag(FGMACS))
141*2718b568SThomas Cort i = x_emacs(buf, len);
142*2718b568SThomas Cort else
143*2718b568SThomas Cort #endif
144*2718b568SThomas Cort #ifdef VI
145*2718b568SThomas Cort if (Flag(FVI))
146*2718b568SThomas Cort i = x_vi(buf, len);
147*2718b568SThomas Cort else
148*2718b568SThomas Cort #endif
149*2718b568SThomas Cort i = -1; /* internal error */
150*2718b568SThomas Cort x_mode(FALSE);
151*2718b568SThomas Cort #if defined(TIOCGWINSZ)
152*2718b568SThomas Cort if (got_sigwinch)
153*2718b568SThomas Cort check_sigwinch();
154*2718b568SThomas Cort #endif /* TIOCGWINSZ */
155*2718b568SThomas Cort
156*2718b568SThomas Cort return i;
157*2718b568SThomas Cort }
158*2718b568SThomas Cort
159*2718b568SThomas Cort /* tty I/O */
160*2718b568SThomas Cort
161*2718b568SThomas Cort int
x_getc()162*2718b568SThomas Cort x_getc()
163*2718b568SThomas Cort {
164*2718b568SThomas Cort #ifdef OS2
165*2718b568SThomas Cort unsigned char c = _read_kbd(0, 1, 0);
166*2718b568SThomas Cort return c == 0 ? 0xE0 : c;
167*2718b568SThomas Cort #else /* OS2 */
168*2718b568SThomas Cort char c;
169*2718b568SThomas Cort int n;
170*2718b568SThomas Cort
171*2718b568SThomas Cort while ((n = blocking_read(0, &c, 1)) < 0 && errno == EINTR)
172*2718b568SThomas Cort if (trap) {
173*2718b568SThomas Cort x_mode(FALSE);
174*2718b568SThomas Cort runtraps(0);
175*2718b568SThomas Cort x_mode(TRUE);
176*2718b568SThomas Cort }
177*2718b568SThomas Cort if (n != 1)
178*2718b568SThomas Cort return -1;
179*2718b568SThomas Cort return (int) (unsigned char) c;
180*2718b568SThomas Cort #endif /* OS2 */
181*2718b568SThomas Cort }
182*2718b568SThomas Cort
183*2718b568SThomas Cort void
x_flush()184*2718b568SThomas Cort x_flush()
185*2718b568SThomas Cort {
186*2718b568SThomas Cort shf_flush(shl_out);
187*2718b568SThomas Cort }
188*2718b568SThomas Cort
189*2718b568SThomas Cort void
x_putc(c)190*2718b568SThomas Cort x_putc(c)
191*2718b568SThomas Cort int c;
192*2718b568SThomas Cort {
193*2718b568SThomas Cort shf_putc(c, shl_out);
194*2718b568SThomas Cort }
195*2718b568SThomas Cort
196*2718b568SThomas Cort void
x_puts(s)197*2718b568SThomas Cort x_puts(s)
198*2718b568SThomas Cort const char *s;
199*2718b568SThomas Cort {
200*2718b568SThomas Cort while (*s != 0)
201*2718b568SThomas Cort shf_putc(*s++, shl_out);
202*2718b568SThomas Cort }
203*2718b568SThomas Cort
204*2718b568SThomas Cort bool_t
x_mode(onoff)205*2718b568SThomas Cort x_mode(onoff)
206*2718b568SThomas Cort bool_t onoff;
207*2718b568SThomas Cort {
208*2718b568SThomas Cort static bool_t x_cur_mode;
209*2718b568SThomas Cort bool_t prev;
210*2718b568SThomas Cort
211*2718b568SThomas Cort if (x_cur_mode == onoff)
212*2718b568SThomas Cort return x_cur_mode;
213*2718b568SThomas Cort prev = x_cur_mode;
214*2718b568SThomas Cort x_cur_mode = onoff;
215*2718b568SThomas Cort
216*2718b568SThomas Cort if (onoff) {
217*2718b568SThomas Cort TTY_state cb;
218*2718b568SThomas Cort X_chars oldchars;
219*2718b568SThomas Cort
220*2718b568SThomas Cort oldchars = edchars;
221*2718b568SThomas Cort cb = tty_state;
222*2718b568SThomas Cort
223*2718b568SThomas Cort #if defined(HAVE_TERMIOS_H) || defined(HAVE_TERMIO_H)
224*2718b568SThomas Cort edchars.erase = cb.c_cc[VERASE];
225*2718b568SThomas Cort edchars.kill = cb.c_cc[VKILL];
226*2718b568SThomas Cort edchars.intr = cb.c_cc[VINTR];
227*2718b568SThomas Cort edchars.quit = cb.c_cc[VQUIT];
228*2718b568SThomas Cort edchars.eof = cb.c_cc[VEOF];
229*2718b568SThomas Cort # ifdef VWERASE
230*2718b568SThomas Cort edchars.werase = cb.c_cc[VWERASE];
231*2718b568SThomas Cort # endif
232*2718b568SThomas Cort # ifdef _CRAY2 /* brain-damaged terminal handler */
233*2718b568SThomas Cort cb.c_lflag &= ~(ICANON|ECHO);
234*2718b568SThomas Cort /* rely on print routine to map '\n' to CR,LF */
235*2718b568SThomas Cort # else
236*2718b568SThomas Cort cb.c_iflag &= ~(INLCR|ICRNL);
237*2718b568SThomas Cort # ifdef _BSD_SYSV /* need to force CBREAK instead of RAW (need CRMOD on output) */
238*2718b568SThomas Cort cb.c_lflag &= ~(ICANON|ECHO);
239*2718b568SThomas Cort # else
240*2718b568SThomas Cort # ifdef SWTCH /* need CBREAK to handle swtch char */
241*2718b568SThomas Cort cb.c_lflag &= ~(ICANON|ECHO);
242*2718b568SThomas Cort cb.c_lflag |= ISIG;
243*2718b568SThomas Cort cb.c_cc[VINTR] = vdisable_c;
244*2718b568SThomas Cort cb.c_cc[VQUIT] = vdisable_c;
245*2718b568SThomas Cort # else
246*2718b568SThomas Cort cb.c_lflag &= ~(ISIG|ICANON|ECHO);
247*2718b568SThomas Cort # endif
248*2718b568SThomas Cort # endif
249*2718b568SThomas Cort # ifdef VLNEXT
250*2718b568SThomas Cort /* osf/1 processes lnext when ~icanon */
251*2718b568SThomas Cort cb.c_cc[VLNEXT] = vdisable_c;
252*2718b568SThomas Cort # endif /* VLNEXT */
253*2718b568SThomas Cort # ifdef VDISCARD
254*2718b568SThomas Cort /* sunos 4.1.x & osf/1 processes discard(flush) when ~icanon */
255*2718b568SThomas Cort cb.c_cc[VDISCARD] = vdisable_c;
256*2718b568SThomas Cort # endif /* VDISCARD */
257*2718b568SThomas Cort cb.c_cc[VTIME] = 0;
258*2718b568SThomas Cort cb.c_cc[VMIN] = 1;
259*2718b568SThomas Cort # endif /* _CRAY2 */
260*2718b568SThomas Cort #else
261*2718b568SThomas Cort /* Assume BSD tty stuff. */
262*2718b568SThomas Cort edchars.erase = cb.sgttyb.sg_erase;
263*2718b568SThomas Cort edchars.kill = cb.sgttyb.sg_kill;
264*2718b568SThomas Cort cb.sgttyb.sg_flags &= ~ECHO;
265*2718b568SThomas Cort cb.sgttyb.sg_flags |= CBREAK;
266*2718b568SThomas Cort # ifdef TIOCGATC
267*2718b568SThomas Cort edchars.intr = cb.lchars.tc_intrc;
268*2718b568SThomas Cort edchars.quit = cb.lchars.tc_quitc;
269*2718b568SThomas Cort edchars.eof = cb.lchars.tc_eofc;
270*2718b568SThomas Cort edchars.werase = cb.lchars.tc_werasc;
271*2718b568SThomas Cort cb.lchars.tc_suspc = -1;
272*2718b568SThomas Cort cb.lchars.tc_dsuspc = -1;
273*2718b568SThomas Cort cb.lchars.tc_lnextc = -1;
274*2718b568SThomas Cort cb.lchars.tc_statc = -1;
275*2718b568SThomas Cort cb.lchars.tc_intrc = -1;
276*2718b568SThomas Cort cb.lchars.tc_quitc = -1;
277*2718b568SThomas Cort cb.lchars.tc_rprntc = -1;
278*2718b568SThomas Cort # else
279*2718b568SThomas Cort edchars.intr = cb.tchars.t_intrc;
280*2718b568SThomas Cort edchars.quit = cb.tchars.t_quitc;
281*2718b568SThomas Cort edchars.eof = cb.tchars.t_eofc;
282*2718b568SThomas Cort cb.tchars.t_intrc = -1;
283*2718b568SThomas Cort cb.tchars.t_quitc = -1;
284*2718b568SThomas Cort # ifdef TIOCGLTC
285*2718b568SThomas Cort edchars.werase = cb.ltchars.t_werasc;
286*2718b568SThomas Cort cb.ltchars.t_suspc = -1;
287*2718b568SThomas Cort cb.ltchars.t_dsuspc = -1;
288*2718b568SThomas Cort cb.ltchars.t_lnextc = -1;
289*2718b568SThomas Cort cb.ltchars.t_rprntc = -1;
290*2718b568SThomas Cort # endif
291*2718b568SThomas Cort # endif /* TIOCGATC */
292*2718b568SThomas Cort #endif /* HAVE_TERMIOS_H || HAVE_TERMIO_H */
293*2718b568SThomas Cort
294*2718b568SThomas Cort set_tty(tty_fd, &cb, TF_WAIT);
295*2718b568SThomas Cort
296*2718b568SThomas Cort #ifdef __CYGWIN__
297*2718b568SThomas Cort if (edchars.eof == '\0')
298*2718b568SThomas Cort edchars.eof = '\4';
299*2718b568SThomas Cort #endif /* __CYGWIN__ */
300*2718b568SThomas Cort
301*2718b568SThomas Cort /* Convert unset values to internal `unset' value */
302*2718b568SThomas Cort if (edchars.erase == vdisable_c)
303*2718b568SThomas Cort edchars.erase = -1;
304*2718b568SThomas Cort if (edchars.kill == vdisable_c)
305*2718b568SThomas Cort edchars.kill = -1;
306*2718b568SThomas Cort if (edchars.intr == vdisable_c)
307*2718b568SThomas Cort edchars.intr = -1;
308*2718b568SThomas Cort if (edchars.quit == vdisable_c)
309*2718b568SThomas Cort edchars.quit = -1;
310*2718b568SThomas Cort if (edchars.eof == vdisable_c)
311*2718b568SThomas Cort edchars.eof = -1;
312*2718b568SThomas Cort if (edchars.werase == vdisable_c)
313*2718b568SThomas Cort edchars.werase = -1;
314*2718b568SThomas Cort if (memcmp(&edchars, &oldchars, sizeof(edchars)) != 0) {
315*2718b568SThomas Cort #ifdef EMACS
316*2718b568SThomas Cort x_emacs_keys(&edchars);
317*2718b568SThomas Cort #endif
318*2718b568SThomas Cort }
319*2718b568SThomas Cort } else {
320*2718b568SThomas Cort /* TF_WAIT doesn't seem to be necessary when leaving xmode */
321*2718b568SThomas Cort set_tty(tty_fd, &tty_state, TF_NONE);
322*2718b568SThomas Cort }
323*2718b568SThomas Cort
324*2718b568SThomas Cort return prev;
325*2718b568SThomas Cort }
326*2718b568SThomas Cort
327*2718b568SThomas Cort /* NAME:
328*2718b568SThomas Cort * promptlen - calculate the length of PS1 etc.
329*2718b568SThomas Cort *
330*2718b568SThomas Cort * DESCRIPTION:
331*2718b568SThomas Cort * This function is based on a fix from guy@demon.co.uk
332*2718b568SThomas Cort * It fixes a bug in that if PS1 contains '!', the length
333*2718b568SThomas Cort * given by strlen() is probably wrong.
334*2718b568SThomas Cort *
335*2718b568SThomas Cort * RETURN VALUE:
336*2718b568SThomas Cort * length
337*2718b568SThomas Cort */
338*2718b568SThomas Cort int
promptlen(cp,spp)339*2718b568SThomas Cort promptlen(cp, spp)
340*2718b568SThomas Cort const char *cp;
341*2718b568SThomas Cort const char **spp;
342*2718b568SThomas Cort {
343*2718b568SThomas Cort int count = 0;
344*2718b568SThomas Cort const char *sp = cp;
345*2718b568SThomas Cort char delimiter = 0;
346*2718b568SThomas Cort int indelimit = 0;
347*2718b568SThomas Cort
348*2718b568SThomas Cort /* Undocumented AT&T ksh feature:
349*2718b568SThomas Cort * If the second char in the prompt string is \r then the first char
350*2718b568SThomas Cort * is taken to be a non-printing delimiter and any chars between two
351*2718b568SThomas Cort * instances of the delimiter are not considered to be part of the
352*2718b568SThomas Cort * prompt length
353*2718b568SThomas Cort */
354*2718b568SThomas Cort if (*cp && cp[1] == '\r') {
355*2718b568SThomas Cort delimiter = *cp;
356*2718b568SThomas Cort cp += 2;
357*2718b568SThomas Cort }
358*2718b568SThomas Cort
359*2718b568SThomas Cort for (; *cp; cp++) {
360*2718b568SThomas Cort if (indelimit && *cp != delimiter)
361*2718b568SThomas Cort ;
362*2718b568SThomas Cort else if (*cp == '\n' || *cp == '\r') {
363*2718b568SThomas Cort count = 0;
364*2718b568SThomas Cort sp = cp + 1;
365*2718b568SThomas Cort } else if (*cp == '\t') {
366*2718b568SThomas Cort count = (count | 7) + 1;
367*2718b568SThomas Cort } else if (*cp == '\b') {
368*2718b568SThomas Cort if (count > 0)
369*2718b568SThomas Cort count--;
370*2718b568SThomas Cort } else if (*cp == delimiter)
371*2718b568SThomas Cort indelimit = !indelimit;
372*2718b568SThomas Cort else
373*2718b568SThomas Cort count++;
374*2718b568SThomas Cort }
375*2718b568SThomas Cort if (spp)
376*2718b568SThomas Cort *spp = sp;
377*2718b568SThomas Cort return count;
378*2718b568SThomas Cort }
379*2718b568SThomas Cort
380*2718b568SThomas Cort void
set_editmode(ed)381*2718b568SThomas Cort set_editmode(ed)
382*2718b568SThomas Cort const char *ed;
383*2718b568SThomas Cort {
384*2718b568SThomas Cort static const enum sh_flag edit_flags[] = {
385*2718b568SThomas Cort #ifdef EMACS
386*2718b568SThomas Cort FEMACS, FGMACS,
387*2718b568SThomas Cort #endif
388*2718b568SThomas Cort #ifdef VI
389*2718b568SThomas Cort FVI,
390*2718b568SThomas Cort #endif
391*2718b568SThomas Cort };
392*2718b568SThomas Cort char *rcp;
393*2718b568SThomas Cort size_t i;
394*2718b568SThomas Cort
395*2718b568SThomas Cort if ((rcp = ksh_strrchr_dirsep(ed)))
396*2718b568SThomas Cort ed = ++rcp;
397*2718b568SThomas Cort for (i = 0; i < NELEM(edit_flags); i++)
398*2718b568SThomas Cort if (strstr(ed, goptions[(int) edit_flags[i]].name)) {
399*2718b568SThomas Cort change_flag(edit_flags[i], OF_SPECIAL, 1);
400*2718b568SThomas Cort return;
401*2718b568SThomas Cort }
402*2718b568SThomas Cort }
403*2718b568SThomas Cort
404*2718b568SThomas Cort /* ------------------------------------------------------------------------- */
405*2718b568SThomas Cort /* Misc common code for vi/emacs */
406*2718b568SThomas Cort
407*2718b568SThomas Cort /* Handle the commenting/uncommenting of a line.
408*2718b568SThomas Cort * Returns:
409*2718b568SThomas Cort * 1 if a carriage return is indicated (comment added)
410*2718b568SThomas Cort * 0 if no return (comment removed)
411*2718b568SThomas Cort * -1 if there is an error (not enough room for comment chars)
412*2718b568SThomas Cort * If successful, *lenp contains the new length. Note: cursor should be
413*2718b568SThomas Cort * moved to the start of the line after (un)commenting.
414*2718b568SThomas Cort */
415*2718b568SThomas Cort int
x_do_comment(buf,bsize,lenp)416*2718b568SThomas Cort x_do_comment(buf, bsize, lenp)
417*2718b568SThomas Cort char *buf;
418*2718b568SThomas Cort int bsize;
419*2718b568SThomas Cort int *lenp;
420*2718b568SThomas Cort {
421*2718b568SThomas Cort int i, j;
422*2718b568SThomas Cort int len = *lenp;
423*2718b568SThomas Cort
424*2718b568SThomas Cort if (len == 0)
425*2718b568SThomas Cort return 1; /* somewhat arbitrary - it's what at&t ksh does */
426*2718b568SThomas Cort
427*2718b568SThomas Cort /* Already commented? */
428*2718b568SThomas Cort if (buf[0] == '#') {
429*2718b568SThomas Cort int saw_nl = 0;
430*2718b568SThomas Cort
431*2718b568SThomas Cort for (j = 0, i = 1; i < len; i++) {
432*2718b568SThomas Cort if (!saw_nl || buf[i] != '#')
433*2718b568SThomas Cort buf[j++] = buf[i];
434*2718b568SThomas Cort saw_nl = buf[i] == '\n';
435*2718b568SThomas Cort }
436*2718b568SThomas Cort *lenp = j;
437*2718b568SThomas Cort return 0;
438*2718b568SThomas Cort } else {
439*2718b568SThomas Cort int n = 1;
440*2718b568SThomas Cort
441*2718b568SThomas Cort /* See if there's room for the #'s - 1 per \n */
442*2718b568SThomas Cort for (i = 0; i < len; i++)
443*2718b568SThomas Cort if (buf[i] == '\n')
444*2718b568SThomas Cort n++;
445*2718b568SThomas Cort if (len + n >= bsize)
446*2718b568SThomas Cort return -1;
447*2718b568SThomas Cort /* Now add them... */
448*2718b568SThomas Cort for (i = len, j = len + n; --i >= 0; ) {
449*2718b568SThomas Cort if (buf[i] == '\n')
450*2718b568SThomas Cort buf[--j] = '#';
451*2718b568SThomas Cort buf[--j] = buf[i];
452*2718b568SThomas Cort }
453*2718b568SThomas Cort buf[0] = '#';
454*2718b568SThomas Cort *lenp += n;
455*2718b568SThomas Cort return 1;
456*2718b568SThomas Cort }
457*2718b568SThomas Cort }
458*2718b568SThomas Cort
459*2718b568SThomas Cort /* ------------------------------------------------------------------------- */
460*2718b568SThomas Cort /* Common file/command completion code for vi/emacs */
461*2718b568SThomas Cort
462*2718b568SThomas Cort
463*2718b568SThomas Cort static char *add_glob ARGS((const char *, int));
464*2718b568SThomas Cort static void glob_table ARGS((const char *, XPtrV *, struct table *));
465*2718b568SThomas Cort static void glob_path ARGS((int, const char *, XPtrV *, const char *));
466*2718b568SThomas Cort
467*2718b568SThomas Cort #if 0 /* not used... */
468*2718b568SThomas Cort int x_complete_word ARGS((const char *, int, int, int *, char **));
469*2718b568SThomas Cort int
470*2718b568SThomas Cort x_complete_word(str, slen, is_command, nwordsp, ret)
471*2718b568SThomas Cort const char *str;
472*2718b568SThomas Cort int slen;
473*2718b568SThomas Cort int is_command;
474*2718b568SThomas Cort int *nwordsp;
475*2718b568SThomas Cort char **ret;
476*2718b568SThomas Cort {
477*2718b568SThomas Cort int nwords;
478*2718b568SThomas Cort int prefix_len;
479*2718b568SThomas Cort char **words;
480*2718b568SThomas Cort
481*2718b568SThomas Cort nwords = (is_command ? x_command_glob : x_file_glob)(XCF_FULLPATH,
482*2718b568SThomas Cort str, slen, &words);
483*2718b568SThomas Cort *nwordsp = nwords;
484*2718b568SThomas Cort if (nwords == 0) {
485*2718b568SThomas Cort *ret = (char *) 0;
486*2718b568SThomas Cort return -1;
487*2718b568SThomas Cort }
488*2718b568SThomas Cort
489*2718b568SThomas Cort prefix_len = x_longest_prefix(nwords, words);
490*2718b568SThomas Cort *ret = str_nsave(words[0], prefix_len, ATEMP);
491*2718b568SThomas Cort x_free_words(nwords, words);
492*2718b568SThomas Cort return prefix_len;
493*2718b568SThomas Cort }
494*2718b568SThomas Cort #endif /* 0 */
495*2718b568SThomas Cort
496*2718b568SThomas Cort void
x_print_expansions(nwords,words,is_command)497*2718b568SThomas Cort x_print_expansions(nwords, words, is_command)
498*2718b568SThomas Cort int nwords;
499*2718b568SThomas Cort char *const *words;
500*2718b568SThomas Cort int is_command;
501*2718b568SThomas Cort {
502*2718b568SThomas Cort int use_copy = 0;
503*2718b568SThomas Cort int prefix_len;
504*2718b568SThomas Cort XPtrV l;
505*2718b568SThomas Cort
506*2718b568SThomas Cort l.beg = NULL;
507*2718b568SThomas Cort
508*2718b568SThomas Cort /* Check if all matches are in the same directory (in this
509*2718b568SThomas Cort * case, we want to omit the directory name)
510*2718b568SThomas Cort */
511*2718b568SThomas Cort if (!is_command
512*2718b568SThomas Cort && (prefix_len = x_longest_prefix(nwords, words)) > 0)
513*2718b568SThomas Cort {
514*2718b568SThomas Cort int i;
515*2718b568SThomas Cort
516*2718b568SThomas Cort /* Special case for 1 match (prefix is whole word) */
517*2718b568SThomas Cort if (nwords == 1)
518*2718b568SThomas Cort prefix_len = x_basename(words[0], (char *) 0);
519*2718b568SThomas Cort /* Any (non-trailing) slashes in non-common word suffixes? */
520*2718b568SThomas Cort for (i = 0; i < nwords; i++)
521*2718b568SThomas Cort if (x_basename(words[i] + prefix_len, (char *) 0)
522*2718b568SThomas Cort > prefix_len)
523*2718b568SThomas Cort break;
524*2718b568SThomas Cort /* All in same directory? */
525*2718b568SThomas Cort if (i == nwords) {
526*2718b568SThomas Cort while (prefix_len > 0
527*2718b568SThomas Cort && !ISDIRSEP(words[0][prefix_len - 1]))
528*2718b568SThomas Cort prefix_len--;
529*2718b568SThomas Cort use_copy = 1;
530*2718b568SThomas Cort XPinit(l, nwords + 1);
531*2718b568SThomas Cort for (i = 0; i < nwords; i++)
532*2718b568SThomas Cort XPput(l, words[i] + prefix_len);
533*2718b568SThomas Cort XPput(l, (char *) 0);
534*2718b568SThomas Cort }
535*2718b568SThomas Cort }
536*2718b568SThomas Cort
537*2718b568SThomas Cort /*
538*2718b568SThomas Cort * Enumerate expansions
539*2718b568SThomas Cort */
540*2718b568SThomas Cort x_putc('\r');
541*2718b568SThomas Cort x_putc('\n');
542*2718b568SThomas Cort pr_list(use_copy ? (char **) XPptrv(l) : words);
543*2718b568SThomas Cort
544*2718b568SThomas Cort if (use_copy)
545*2718b568SThomas Cort XPfree(l); /* not x_free_words() */
546*2718b568SThomas Cort }
547*2718b568SThomas Cort
548*2718b568SThomas Cort /*
549*2718b568SThomas Cort * Do file globbing:
550*2718b568SThomas Cort * - appends * to (copy of) str if no globbing chars found
551*2718b568SThomas Cort * - does expansion, checks for no match, etc.
552*2718b568SThomas Cort * - sets *wordsp to array of matching strings
553*2718b568SThomas Cort * - returns number of matching strings
554*2718b568SThomas Cort */
555*2718b568SThomas Cort static int
x_file_glob(flags,str,slen,wordsp)556*2718b568SThomas Cort x_file_glob(flags, str, slen, wordsp)
557*2718b568SThomas Cort int flags;
558*2718b568SThomas Cort const char *str;
559*2718b568SThomas Cort int slen;
560*2718b568SThomas Cort char ***wordsp;
561*2718b568SThomas Cort {
562*2718b568SThomas Cort char *toglob;
563*2718b568SThomas Cort char **words;
564*2718b568SThomas Cort int nwords, i, idx, escaping;
565*2718b568SThomas Cort XPtrV w;
566*2718b568SThomas Cort struct source *s, *sold;
567*2718b568SThomas Cort
568*2718b568SThomas Cort if (slen < 0)
569*2718b568SThomas Cort return 0;
570*2718b568SThomas Cort
571*2718b568SThomas Cort toglob = add_glob(str, slen);
572*2718b568SThomas Cort
573*2718b568SThomas Cort /* remove all escaping backward slashes */
574*2718b568SThomas Cort escaping = 0;
575*2718b568SThomas Cort for(i = 0, idx = 0; toglob[i]; i++) {
576*2718b568SThomas Cort if (toglob[i] == '\\' && !escaping) {
577*2718b568SThomas Cort escaping = 1;
578*2718b568SThomas Cort continue;
579*2718b568SThomas Cort }
580*2718b568SThomas Cort
581*2718b568SThomas Cort toglob[idx] = toglob[i];
582*2718b568SThomas Cort idx++;
583*2718b568SThomas Cort if (escaping) escaping = 0;
584*2718b568SThomas Cort }
585*2718b568SThomas Cort toglob[idx] = '\0';
586*2718b568SThomas Cort
587*2718b568SThomas Cort /*
588*2718b568SThomas Cort * Convert "foo*" (toglob) to an array of strings (words)
589*2718b568SThomas Cort */
590*2718b568SThomas Cort sold = source;
591*2718b568SThomas Cort s = pushs(SWSTR, ATEMP);
592*2718b568SThomas Cort s->start = s->str = toglob;
593*2718b568SThomas Cort source = s;
594*2718b568SThomas Cort if (yylex(ONEWORD) != LWORD) {
595*2718b568SThomas Cort source = sold;
596*2718b568SThomas Cort internal_errorf(0, "fileglob: substitute error");
597*2718b568SThomas Cort return 0;
598*2718b568SThomas Cort }
599*2718b568SThomas Cort source = sold;
600*2718b568SThomas Cort XPinit(w, 32);
601*2718b568SThomas Cort expand(yylval.cp, &w, DOGLOB|DOTILDE|DOMARKDIRS);
602*2718b568SThomas Cort XPput(w, NULL);
603*2718b568SThomas Cort words = (char **) XPclose(w);
604*2718b568SThomas Cort
605*2718b568SThomas Cort for (nwords = 0; words[nwords]; nwords++)
606*2718b568SThomas Cort ;
607*2718b568SThomas Cort if (nwords == 1) {
608*2718b568SThomas Cort struct stat statb;
609*2718b568SThomas Cort
610*2718b568SThomas Cort /* Check if globbing failed (returned glob pattern),
611*2718b568SThomas Cort * but be careful (E.g. toglob == "ab*" when the file
612*2718b568SThomas Cort * "ab*" exists is not an error).
613*2718b568SThomas Cort * Also, check for empty result - happens if we tried
614*2718b568SThomas Cort * to glob something which evaluated to an empty
615*2718b568SThomas Cort * string (e.g., "$FOO" when there is no FOO, etc).
616*2718b568SThomas Cort */
617*2718b568SThomas Cort if ((strcmp(words[0], toglob) == 0
618*2718b568SThomas Cort && stat(words[0], &statb) < 0)
619*2718b568SThomas Cort || words[0][0] == '\0')
620*2718b568SThomas Cort {
621*2718b568SThomas Cort x_free_words(nwords, words);
622*2718b568SThomas Cort words = NULL;
623*2718b568SThomas Cort nwords = 0;
624*2718b568SThomas Cort }
625*2718b568SThomas Cort }
626*2718b568SThomas Cort afree(toglob, ATEMP);
627*2718b568SThomas Cort
628*2718b568SThomas Cort if (nwords) {
629*2718b568SThomas Cort *wordsp = words;
630*2718b568SThomas Cort } else if (words) {
631*2718b568SThomas Cort x_free_words(nwords, words);
632*2718b568SThomas Cort *wordsp = NULL;
633*2718b568SThomas Cort }
634*2718b568SThomas Cort return nwords;
635*2718b568SThomas Cort }
636*2718b568SThomas Cort
637*2718b568SThomas Cort /* Data structure used in x_command_glob() */
638*2718b568SThomas Cort struct path_order_info {
639*2718b568SThomas Cort char *word;
640*2718b568SThomas Cort int base;
641*2718b568SThomas Cort int path_order;
642*2718b568SThomas Cort };
643*2718b568SThomas Cort
644*2718b568SThomas Cort static int path_order_cmp(const void *aa, const void *bb);
645*2718b568SThomas Cort
646*2718b568SThomas Cort /* Compare routine used in x_command_glob() */
647*2718b568SThomas Cort static int
path_order_cmp(aa,bb)648*2718b568SThomas Cort path_order_cmp(aa, bb)
649*2718b568SThomas Cort const void *aa;
650*2718b568SThomas Cort const void *bb;
651*2718b568SThomas Cort {
652*2718b568SThomas Cort const struct path_order_info *a = (const struct path_order_info *) aa;
653*2718b568SThomas Cort const struct path_order_info *b = (const struct path_order_info *) bb;
654*2718b568SThomas Cort int t;
655*2718b568SThomas Cort
656*2718b568SThomas Cort t = FILECMP(a->word + a->base, b->word + b->base);
657*2718b568SThomas Cort return t ? t : a->path_order - b->path_order;
658*2718b568SThomas Cort }
659*2718b568SThomas Cort
660*2718b568SThomas Cort static int
x_command_glob(flags,str,slen,wordsp)661*2718b568SThomas Cort x_command_glob(flags, str, slen, wordsp)
662*2718b568SThomas Cort int flags;
663*2718b568SThomas Cort const char *str;
664*2718b568SThomas Cort int slen;
665*2718b568SThomas Cort char ***wordsp;
666*2718b568SThomas Cort {
667*2718b568SThomas Cort char *toglob;
668*2718b568SThomas Cort char *pat;
669*2718b568SThomas Cort char *fpath;
670*2718b568SThomas Cort int nwords;
671*2718b568SThomas Cort XPtrV w;
672*2718b568SThomas Cort struct block *l;
673*2718b568SThomas Cort
674*2718b568SThomas Cort if (slen < 0)
675*2718b568SThomas Cort return 0;
676*2718b568SThomas Cort
677*2718b568SThomas Cort toglob = add_glob(str, slen);
678*2718b568SThomas Cort
679*2718b568SThomas Cort /* Convert "foo*" (toglob) to a pattern for future use */
680*2718b568SThomas Cort pat = evalstr(toglob, DOPAT|DOTILDE);
681*2718b568SThomas Cort afree(toglob, ATEMP);
682*2718b568SThomas Cort
683*2718b568SThomas Cort XPinit(w, 32);
684*2718b568SThomas Cort
685*2718b568SThomas Cort glob_table(pat, &w, &keywords);
686*2718b568SThomas Cort glob_table(pat, &w, &aliases);
687*2718b568SThomas Cort glob_table(pat, &w, &builtins);
688*2718b568SThomas Cort for (l = e->loc; l; l = l->next)
689*2718b568SThomas Cort glob_table(pat, &w, &l->funs);
690*2718b568SThomas Cort
691*2718b568SThomas Cort glob_path(flags, pat, &w, path);
692*2718b568SThomas Cort if ((fpath = str_val(global("FPATH"))) != null)
693*2718b568SThomas Cort glob_path(flags, pat, &w, fpath);
694*2718b568SThomas Cort
695*2718b568SThomas Cort nwords = XPsize(w);
696*2718b568SThomas Cort
697*2718b568SThomas Cort if (!nwords) {
698*2718b568SThomas Cort *wordsp = (char **) 0;
699*2718b568SThomas Cort XPfree(w);
700*2718b568SThomas Cort return 0;
701*2718b568SThomas Cort }
702*2718b568SThomas Cort
703*2718b568SThomas Cort /* Sort entries */
704*2718b568SThomas Cort if (flags & XCF_FULLPATH) {
705*2718b568SThomas Cort /* Sort by basename, then path order */
706*2718b568SThomas Cort struct path_order_info *info;
707*2718b568SThomas Cort struct path_order_info *last_info = 0;
708*2718b568SThomas Cort char **words = (char **) XPptrv(w);
709*2718b568SThomas Cort int path_order = 0;
710*2718b568SThomas Cort int i;
711*2718b568SThomas Cort
712*2718b568SThomas Cort info = (struct path_order_info *)
713*2718b568SThomas Cort alloc(sizeof(struct path_order_info) * nwords, ATEMP);
714*2718b568SThomas Cort for (i = 0; i < nwords; i++) {
715*2718b568SThomas Cort info[i].word = words[i];
716*2718b568SThomas Cort info[i].base = x_basename(words[i], (char *) 0);
717*2718b568SThomas Cort if (!last_info || info[i].base != last_info->base
718*2718b568SThomas Cort || FILENCMP(words[i],
719*2718b568SThomas Cort last_info->word, info[i].base) != 0)
720*2718b568SThomas Cort {
721*2718b568SThomas Cort last_info = &info[i];
722*2718b568SThomas Cort path_order++;
723*2718b568SThomas Cort }
724*2718b568SThomas Cort info[i].path_order = path_order;
725*2718b568SThomas Cort }
726*2718b568SThomas Cort qsort(info, nwords, sizeof(struct path_order_info),
727*2718b568SThomas Cort path_order_cmp);
728*2718b568SThomas Cort for (i = 0; i < nwords; i++)
729*2718b568SThomas Cort words[i] = info[i].word;
730*2718b568SThomas Cort afree((void *) info, ATEMP);
731*2718b568SThomas Cort } else {
732*2718b568SThomas Cort /* Sort and remove duplicate entries */
733*2718b568SThomas Cort char **words = (char **) XPptrv(w);
734*2718b568SThomas Cort int i, j;
735*2718b568SThomas Cort
736*2718b568SThomas Cort qsortp(XPptrv(w), (size_t) nwords, xstrcmp);
737*2718b568SThomas Cort
738*2718b568SThomas Cort for (i = j = 0; i < nwords - 1; i++) {
739*2718b568SThomas Cort if (strcmp(words[i], words[i + 1]))
740*2718b568SThomas Cort words[j++] = words[i];
741*2718b568SThomas Cort else
742*2718b568SThomas Cort afree(words[i], ATEMP);
743*2718b568SThomas Cort }
744*2718b568SThomas Cort words[j++] = words[i];
745*2718b568SThomas Cort nwords = j;
746*2718b568SThomas Cort w.cur = (void **) &words[j];
747*2718b568SThomas Cort }
748*2718b568SThomas Cort
749*2718b568SThomas Cort XPput(w, NULL);
750*2718b568SThomas Cort *wordsp = (char **) XPclose(w);
751*2718b568SThomas Cort
752*2718b568SThomas Cort return nwords;
753*2718b568SThomas Cort }
754*2718b568SThomas Cort
755*2718b568SThomas Cort #define IS_WORDC(c) !( ctype(c, C_LEX1) || (c) == '\'' || (c) == '"' \
756*2718b568SThomas Cort || (c) == '`' || (c) == '=' || (c) == ':' )
757*2718b568SThomas Cort
758*2718b568SThomas Cort static int
x_locate_word(buf,buflen,pos,startp,is_commandp)759*2718b568SThomas Cort x_locate_word(buf, buflen, pos, startp, is_commandp)
760*2718b568SThomas Cort const char *buf;
761*2718b568SThomas Cort int buflen;
762*2718b568SThomas Cort int pos;
763*2718b568SThomas Cort int *startp;
764*2718b568SThomas Cort int *is_commandp;
765*2718b568SThomas Cort {
766*2718b568SThomas Cort int p;
767*2718b568SThomas Cort int start, end;
768*2718b568SThomas Cort
769*2718b568SThomas Cort /* Bad call? Probably should report error */
770*2718b568SThomas Cort if (pos < 0 || pos > buflen) {
771*2718b568SThomas Cort *startp = pos;
772*2718b568SThomas Cort *is_commandp = 0;
773*2718b568SThomas Cort return 0;
774*2718b568SThomas Cort }
775*2718b568SThomas Cort /* The case where pos == buflen happens to take care of itself... */
776*2718b568SThomas Cort
777*2718b568SThomas Cort start = pos;
778*2718b568SThomas Cort /* Keep going backwards to start of word (has effect of allowing
779*2718b568SThomas Cort * one blank after the end of a word)
780*2718b568SThomas Cort */
781*2718b568SThomas Cort for (; (start > 0 && IS_WORDC(buf[start - 1]))
782*2718b568SThomas Cort || (start > 1 && buf[start-2] == '\\'); start--)
783*2718b568SThomas Cort ;
784*2718b568SThomas Cort /* Go forwards to end of word */
785*2718b568SThomas Cort for (end = start; end < buflen && IS_WORDC(buf[end]); end++) {
786*2718b568SThomas Cort if (buf[end] == '\\' && (end+1) < buflen)
787*2718b568SThomas Cort end++;
788*2718b568SThomas Cort }
789*2718b568SThomas Cort
790*2718b568SThomas Cort if (is_commandp) {
791*2718b568SThomas Cort int iscmd;
792*2718b568SThomas Cort
793*2718b568SThomas Cort /* Figure out if this is a command */
794*2718b568SThomas Cort for (p = start - 1; p >= 0 && isspace((unsigned char)buf[p]); p--)
795*2718b568SThomas Cort ;
796*2718b568SThomas Cort iscmd = p < 0 || strchr(";|&()`", buf[p]);
797*2718b568SThomas Cort if (iscmd) {
798*2718b568SThomas Cort /* If command has a /, path, etc. is not searched;
799*2718b568SThomas Cort * only current directory is searched, which is just
800*2718b568SThomas Cort * like file globbing.
801*2718b568SThomas Cort */
802*2718b568SThomas Cort for (p = start; p < end; p++)
803*2718b568SThomas Cort if (ISDIRSEP(buf[p]))
804*2718b568SThomas Cort break;
805*2718b568SThomas Cort iscmd = p == end;
806*2718b568SThomas Cort }
807*2718b568SThomas Cort *is_commandp = iscmd;
808*2718b568SThomas Cort }
809*2718b568SThomas Cort
810*2718b568SThomas Cort *startp = start;
811*2718b568SThomas Cort
812*2718b568SThomas Cort return end - start;
813*2718b568SThomas Cort }
814*2718b568SThomas Cort
815*2718b568SThomas Cort int
x_cf_glob(flags,buf,buflen,pos,startp,endp,wordsp,is_commandp)816*2718b568SThomas Cort x_cf_glob(flags, buf, buflen, pos, startp, endp, wordsp, is_commandp)
817*2718b568SThomas Cort int flags;
818*2718b568SThomas Cort const char *buf;
819*2718b568SThomas Cort int buflen;
820*2718b568SThomas Cort int pos;
821*2718b568SThomas Cort int *startp;
822*2718b568SThomas Cort int *endp;
823*2718b568SThomas Cort char ***wordsp;
824*2718b568SThomas Cort int *is_commandp;
825*2718b568SThomas Cort {
826*2718b568SThomas Cort int len;
827*2718b568SThomas Cort int nwords;
828*2718b568SThomas Cort char **words;
829*2718b568SThomas Cort int is_command;
830*2718b568SThomas Cort
831*2718b568SThomas Cort len = x_locate_word(buf, buflen, pos, startp, &is_command);
832*2718b568SThomas Cort if (!(flags & XCF_COMMAND))
833*2718b568SThomas Cort is_command = 0;
834*2718b568SThomas Cort /* Don't do command globing on zero length strings - it takes too
835*2718b568SThomas Cort * long and isn't very useful. File globs are more likely to be
836*2718b568SThomas Cort * useful, so allow these.
837*2718b568SThomas Cort */
838*2718b568SThomas Cort if (len == 0 && is_command)
839*2718b568SThomas Cort return 0;
840*2718b568SThomas Cort
841*2718b568SThomas Cort nwords = (is_command ? x_command_glob : x_file_glob)(flags,
842*2718b568SThomas Cort buf + *startp, len, &words);
843*2718b568SThomas Cort if (nwords == 0) {
844*2718b568SThomas Cort *wordsp = (char **) 0;
845*2718b568SThomas Cort return 0;
846*2718b568SThomas Cort }
847*2718b568SThomas Cort
848*2718b568SThomas Cort if (is_commandp)
849*2718b568SThomas Cort *is_commandp = is_command;
850*2718b568SThomas Cort *wordsp = words;
851*2718b568SThomas Cort *endp = *startp + len;
852*2718b568SThomas Cort
853*2718b568SThomas Cort return nwords;
854*2718b568SThomas Cort }
855*2718b568SThomas Cort
856*2718b568SThomas Cort /* Given a string, copy it and possibly add a '*' to the end. The
857*2718b568SThomas Cort * new string is returned.
858*2718b568SThomas Cort */
859*2718b568SThomas Cort static char *
add_glob(str,slen)860*2718b568SThomas Cort add_glob(str, slen)
861*2718b568SThomas Cort const char *str;
862*2718b568SThomas Cort int slen;
863*2718b568SThomas Cort {
864*2718b568SThomas Cort char *toglob;
865*2718b568SThomas Cort char *s;
866*2718b568SThomas Cort bool_t saw_slash = FALSE;
867*2718b568SThomas Cort
868*2718b568SThomas Cort if (slen < 0)
869*2718b568SThomas Cort return (char *) 0;
870*2718b568SThomas Cort
871*2718b568SThomas Cort toglob = str_nsave(str, slen + 1, ATEMP); /* + 1 for "*" */
872*2718b568SThomas Cort toglob[slen] = '\0';
873*2718b568SThomas Cort
874*2718b568SThomas Cort /*
875*2718b568SThomas Cort * If the pathname contains a wildcard (an unquoted '*',
876*2718b568SThomas Cort * '?', or '['), or a ~username
877*2718b568SThomas Cort * with no trailing slash, then it is globbed based on that
878*2718b568SThomas Cort * value (i.e., without the appended '*').
879*2718b568SThomas Cort */
880*2718b568SThomas Cort for (s = toglob; *s; s++) {
881*2718b568SThomas Cort if (*s == '\\' && s[1])
882*2718b568SThomas Cort s++;
883*2718b568SThomas Cort else if (*s == '*' || *s == '[' || *s == '?'
884*2718b568SThomas Cort || (s[1] == '(' /*)*/ && strchr("*+?@!", *s)))
885*2718b568SThomas Cort break;
886*2718b568SThomas Cort else if (ISDIRSEP(*s))
887*2718b568SThomas Cort saw_slash = TRUE;
888*2718b568SThomas Cort }
889*2718b568SThomas Cort if (!*s && (*toglob != '~' || saw_slash)) {
890*2718b568SThomas Cort toglob[slen] = '*';
891*2718b568SThomas Cort toglob[slen + 1] = '\0';
892*2718b568SThomas Cort }
893*2718b568SThomas Cort
894*2718b568SThomas Cort return toglob;
895*2718b568SThomas Cort }
896*2718b568SThomas Cort
897*2718b568SThomas Cort /*
898*2718b568SThomas Cort * Find longest common prefix
899*2718b568SThomas Cort */
900*2718b568SThomas Cort int
x_longest_prefix(nwords,words)901*2718b568SThomas Cort x_longest_prefix(nwords, words)
902*2718b568SThomas Cort int nwords;
903*2718b568SThomas Cort char *const *words;
904*2718b568SThomas Cort {
905*2718b568SThomas Cort int i, j;
906*2718b568SThomas Cort int prefix_len;
907*2718b568SThomas Cort char *p;
908*2718b568SThomas Cort
909*2718b568SThomas Cort if (nwords <= 0)
910*2718b568SThomas Cort return 0;
911*2718b568SThomas Cort
912*2718b568SThomas Cort prefix_len = strlen(words[0]);
913*2718b568SThomas Cort for (i = 1; i < nwords; i++)
914*2718b568SThomas Cort for (j = 0, p = words[i]; j < prefix_len; j++)
915*2718b568SThomas Cort if (FILECHCONV((unsigned char)p[j])
916*2718b568SThomas Cort != FILECHCONV((unsigned char)words[0][j])) {
917*2718b568SThomas Cort prefix_len = j;
918*2718b568SThomas Cort break;
919*2718b568SThomas Cort }
920*2718b568SThomas Cort return prefix_len;
921*2718b568SThomas Cort }
922*2718b568SThomas Cort
923*2718b568SThomas Cort void
x_free_words(nwords,words)924*2718b568SThomas Cort x_free_words(nwords, words)
925*2718b568SThomas Cort int nwords;
926*2718b568SThomas Cort char **words;
927*2718b568SThomas Cort {
928*2718b568SThomas Cort int i;
929*2718b568SThomas Cort
930*2718b568SThomas Cort for (i = 0; i < nwords; i++)
931*2718b568SThomas Cort if (words[i])
932*2718b568SThomas Cort afree(words[i], ATEMP);
933*2718b568SThomas Cort afree(words, ATEMP);
934*2718b568SThomas Cort }
935*2718b568SThomas Cort
936*2718b568SThomas Cort /* Return the offset of the basename of string s (which ends at se - need not
937*2718b568SThomas Cort * be null terminated). Trailing slashes are ignored. If s is just a slash,
938*2718b568SThomas Cort * then the offset is 0 (actually, length - 1).
939*2718b568SThomas Cort * s Return
940*2718b568SThomas Cort * /etc 1
941*2718b568SThomas Cort * /etc/ 1
942*2718b568SThomas Cort * /etc// 1
943*2718b568SThomas Cort * /etc/fo 5
944*2718b568SThomas Cort * foo 0
945*2718b568SThomas Cort * /// 2
946*2718b568SThomas Cort * 0
947*2718b568SThomas Cort */
948*2718b568SThomas Cort int
x_basename(s,se)949*2718b568SThomas Cort x_basename(s, se)
950*2718b568SThomas Cort const char *s;
951*2718b568SThomas Cort const char *se;
952*2718b568SThomas Cort {
953*2718b568SThomas Cort const char *p;
954*2718b568SThomas Cort
955*2718b568SThomas Cort if (se == (char *) 0)
956*2718b568SThomas Cort se = s + strlen(s);
957*2718b568SThomas Cort if (s == se)
958*2718b568SThomas Cort return 0;
959*2718b568SThomas Cort
960*2718b568SThomas Cort /* Skip trailing slashes */
961*2718b568SThomas Cort for (p = se - 1; p > s && ISDIRSEP(*p); p--)
962*2718b568SThomas Cort ;
963*2718b568SThomas Cort for (; p > s && !ISDIRSEP(*p); p--)
964*2718b568SThomas Cort ;
965*2718b568SThomas Cort if (ISDIRSEP(*p) && p + 1 < se)
966*2718b568SThomas Cort p++;
967*2718b568SThomas Cort
968*2718b568SThomas Cort return p - s;
969*2718b568SThomas Cort }
970*2718b568SThomas Cort
971*2718b568SThomas Cort /*
972*2718b568SThomas Cort * Apply pattern matching to a table: all table entries that match a pattern
973*2718b568SThomas Cort * are added to wp.
974*2718b568SThomas Cort */
975*2718b568SThomas Cort static void
glob_table(pat,wp,tp)976*2718b568SThomas Cort glob_table(pat, wp, tp)
977*2718b568SThomas Cort const char *pat;
978*2718b568SThomas Cort XPtrV *wp;
979*2718b568SThomas Cort struct table *tp;
980*2718b568SThomas Cort {
981*2718b568SThomas Cort struct tstate ts;
982*2718b568SThomas Cort struct tbl *te;
983*2718b568SThomas Cort
984*2718b568SThomas Cort for (twalk(&ts, tp); (te = tnext(&ts)); ) {
985*2718b568SThomas Cort if (gmatch(te->name, pat, FALSE))
986*2718b568SThomas Cort XPput(*wp, str_save(te->name, ATEMP));
987*2718b568SThomas Cort }
988*2718b568SThomas Cort }
989*2718b568SThomas Cort
990*2718b568SThomas Cort static void
glob_path(flags,pat,wp,xpath)991*2718b568SThomas Cort glob_path(flags, pat, wp, xpath)
992*2718b568SThomas Cort int flags;
993*2718b568SThomas Cort const char *pat;
994*2718b568SThomas Cort XPtrV *wp;
995*2718b568SThomas Cort const char *xpath;
996*2718b568SThomas Cort {
997*2718b568SThomas Cort const char *sp, *p;
998*2718b568SThomas Cort char *xp;
999*2718b568SThomas Cort int staterr;
1000*2718b568SThomas Cort int pathlen;
1001*2718b568SThomas Cort int patlen;
1002*2718b568SThomas Cort int oldsize, newsize, i, j;
1003*2718b568SThomas Cort char **words;
1004*2718b568SThomas Cort XString xs;
1005*2718b568SThomas Cort
1006*2718b568SThomas Cort patlen = strlen(pat) + 1;
1007*2718b568SThomas Cort sp = xpath;
1008*2718b568SThomas Cort Xinit(xs, xp, patlen + 128, ATEMP);
1009*2718b568SThomas Cort while (sp) {
1010*2718b568SThomas Cort xp = Xstring(xs, xp);
1011*2718b568SThomas Cort if (!(p = strchr(sp, PATHSEP)))
1012*2718b568SThomas Cort p = sp + strlen(sp);
1013*2718b568SThomas Cort pathlen = p - sp;
1014*2718b568SThomas Cort if (pathlen) {
1015*2718b568SThomas Cort /* Copy sp into xp, stuffing any MAGIC characters
1016*2718b568SThomas Cort * on the way
1017*2718b568SThomas Cort */
1018*2718b568SThomas Cort const char *s = sp;
1019*2718b568SThomas Cort
1020*2718b568SThomas Cort XcheckN(xs, xp, pathlen * 2);
1021*2718b568SThomas Cort while (s < p) {
1022*2718b568SThomas Cort if (ISMAGIC(*s))
1023*2718b568SThomas Cort *xp++ = MAGIC;
1024*2718b568SThomas Cort *xp++ = *s++;
1025*2718b568SThomas Cort }
1026*2718b568SThomas Cort *xp++ = DIRSEP;
1027*2718b568SThomas Cort pathlen++;
1028*2718b568SThomas Cort }
1029*2718b568SThomas Cort sp = p;
1030*2718b568SThomas Cort XcheckN(xs, xp, patlen);
1031*2718b568SThomas Cort memcpy(xp, pat, patlen);
1032*2718b568SThomas Cort
1033*2718b568SThomas Cort oldsize = XPsize(*wp);
1034*2718b568SThomas Cort glob_str(Xstring(xs, xp), wp, 1); /* mark dirs */
1035*2718b568SThomas Cort newsize = XPsize(*wp);
1036*2718b568SThomas Cort
1037*2718b568SThomas Cort /* Check that each match is executable... */
1038*2718b568SThomas Cort words = (char **) XPptrv(*wp);
1039*2718b568SThomas Cort for (i = j = oldsize; i < newsize; i++) {
1040*2718b568SThomas Cort staterr = 0;
1041*2718b568SThomas Cort if ((search_access(words[i], X_OK, &staterr) >= 0)
1042*2718b568SThomas Cort || (staterr == EISDIR)) {
1043*2718b568SThomas Cort words[j] = words[i];
1044*2718b568SThomas Cort if (!(flags & XCF_FULLPATH))
1045*2718b568SThomas Cort memmove(words[j], words[j] + pathlen,
1046*2718b568SThomas Cort strlen(words[j] + pathlen) + 1);
1047*2718b568SThomas Cort j++;
1048*2718b568SThomas Cort } else
1049*2718b568SThomas Cort afree(words[i], ATEMP);
1050*2718b568SThomas Cort }
1051*2718b568SThomas Cort wp->cur = (void **) &words[j];
1052*2718b568SThomas Cort
1053*2718b568SThomas Cort if (!*sp++)
1054*2718b568SThomas Cort break;
1055*2718b568SThomas Cort }
1056*2718b568SThomas Cort Xfree(xs, xp);
1057*2718b568SThomas Cort }
1058*2718b568SThomas Cort
1059*2718b568SThomas Cort /*
1060*2718b568SThomas Cort * if argument string contains any special characters, they will
1061*2718b568SThomas Cort * be escaped and the result will be put into edit buffer by
1062*2718b568SThomas Cort * keybinding-specific function
1063*2718b568SThomas Cort */
1064*2718b568SThomas Cort int
x_escape(s,len,putbuf_func)1065*2718b568SThomas Cort x_escape(s, len, putbuf_func)
1066*2718b568SThomas Cort const char *s;
1067*2718b568SThomas Cort size_t len;
1068*2718b568SThomas Cort int (*putbuf_func) ARGS((const char *, size_t));
1069*2718b568SThomas Cort {
1070*2718b568SThomas Cort size_t add, wlen;
1071*2718b568SThomas Cort const char *ifs = str_val(local("IFS", 0));
1072*2718b568SThomas Cort int rval=0;
1073*2718b568SThomas Cort
1074*2718b568SThomas Cort for (add = 0, wlen = len; wlen - add > 0; add++) {
1075*2718b568SThomas Cort if (strchr("\\$(){}[]?*&;#|<>\"'`", s[add]) || strchr(ifs, s[add])) {
1076*2718b568SThomas Cort if (putbuf_func(s, add) != 0) {
1077*2718b568SThomas Cort rval = -1;
1078*2718b568SThomas Cort break;
1079*2718b568SThomas Cort }
1080*2718b568SThomas Cort
1081*2718b568SThomas Cort putbuf_func("\\", 1);
1082*2718b568SThomas Cort putbuf_func(&s[add], 1);
1083*2718b568SThomas Cort
1084*2718b568SThomas Cort add++;
1085*2718b568SThomas Cort wlen -= add;
1086*2718b568SThomas Cort s += add;
1087*2718b568SThomas Cort add = -1; /* after the increment it will go to 0 */
1088*2718b568SThomas Cort }
1089*2718b568SThomas Cort }
1090*2718b568SThomas Cort if (wlen > 0 && rval == 0)
1091*2718b568SThomas Cort rval = putbuf_func(s, wlen);
1092*2718b568SThomas Cort
1093*2718b568SThomas Cort return (rval);
1094*2718b568SThomas Cort }
1095*2718b568SThomas Cort #endif /* EDIT */
1096