]> diplodocus.org Git - nmh/blob - sbr/ext_hook.c
Alter mh-chart(7)'s NAME to be lowercase.
[nmh] / sbr / ext_hook.c
1 /*
2 *
3 * Run a program that hooks into some other system. The first argument is
4 * name of the hook to use, the second is the full path name of a mail message.
5 * The third argument is also the full path name of a mail message, or a NULL
6 * pointer if it isn't needed. Look in the context for an error message if
7 * something goes wrong; there is a built-in message in case one isn't specified.
8 * Only produce the error message once.
9 */
10
11 #include <h/mh.h>
12
13 int
14 ext_hook(char *hook_name, char *message_file_name_1, char *message_file_name_2)
15 {
16 char *hook; /* hook program from context */
17 pid_t pid; /* ID of child process */
18 int status; /* exit or other child process status */
19 char **vec; /* argument vector for child process */
20 int vecp; /* Vector index */
21 char *program; /* Name of program to execute */
22
23 static int did_message = 0; /* set if we've already output a message */
24
25 if ((hook = context_find(hook_name)) == NULL)
26 return (OK);
27
28 switch (pid = fork()) {
29 case -1:
30 status = NOTOK;
31 advise(NULL, "external database may be out-of-date.");
32 break;
33
34 case 0:
35 vec = argsplit(hook, &program, &vecp);
36 vec[vecp++] = message_file_name_1;
37 vec[vecp++] = message_file_name_2;
38 vec[vecp++] = NULL;
39 execvp(program, vec);
40 advise(program, "Unable to execute");
41 _exit(-1);
42 /* NOTREACHED */
43
44 default:
45 status = pidwait(pid, -1);
46 break;
47 }
48
49 if (status != OK) {
50 if (did_message == 0) {
51 char *msghook;
52 if ((msghook = context_find("msg-hook")) != NULL)
53 advise(NULL, msghook);
54 else {
55 char errbuf[BUFSIZ];
56 snprintf(errbuf, sizeof(errbuf), "external hook \"%s\"", hook);
57 pidstatus(status, stderr, errbuf);
58 }
59 did_message = 1;
60 }
61
62 return (NOTOK);
63 }
64
65 else
66 return (OK);
67 }