1 /* $OpenBSD: autoexec.c,v 1.14 2007/02/08 21:40:03 kjell Exp $ */
2 /* this file is in the public domain */
3 /* Author: Vincent Labrecque <vincent@openbsd.org> April 2002 */
11 SLIST_ENTRY(autoexec) next; /* link in the linked list */
12 const char *pattern; /* Pattern to match to filenames */
16 static SLIST_HEAD(, autoexec) autos;
22 * Return a NULL terminated array of function pointers to be called
23 * when we open a file that matches <fname>. The list must be free(ed)
27 find_autoexec(const char *fname)
39 SLIST_FOREACH(ae, &autos, next) {
40 if (fnmatch(ae->pattern, fname, 0) == 0) {
42 npfl = realloc(pfl, (have + AUTO_GROW + 1) *
45 panic("out of memory");
59 add_autoexec(const char *pattern, const char *func)
68 fp = name_function(func);
71 ae = malloc(sizeof(*ae));
75 ae->pattern = strdup(pattern);
76 if (ae->pattern == NULL) {
80 SLIST_INSERT_HEAD(&autos, ae, next);
86 * Register an auto-execute hook; that is, specify a filename pattern
87 * (conforming to the shell's filename globbing rules) and an associated
88 * function to execute when a file matching the specified pattern
89 * is read into a buffer.
93 auto_execute(int f, int n)
95 char patbuf[128], funcbuf[128], *patp, *funcp;
98 if ((patp = eread("Filename pattern: ", patbuf, sizeof(patbuf),
99 EFNEW | EFCR)) == NULL)
101 else if (patp[0] == '\0')
103 if ((funcp = eread("Execute: ", funcbuf, sizeof(funcbuf),
104 EFNEW | EFCR | EFFUNC)) == NULL)
106 else if (funcp[0] == '\0')
108 if ((s = add_autoexec(patp, funcp)) != TRUE)