]> diplodocus.org Git - nmh/blobdiff - sbr/ext_hook.c
context_replace.c: Move interface to own file.
[nmh] / sbr / ext_hook.c
index e94083b337f73508fb343c9633ea832d67114e4d..106114f829b02ada5e341fb5a13aee91d2ea2a4b 100644 (file)
@@ -1,6 +1,4 @@
-/*
- *
- *     Run a program that hooks into some other system.  The first argument is
+/*     Run a program that hooks into some other system.  The first argument is
  *     name of the hook to use, the second is the full path name of a mail message.
  *     The third argument is also the full path name of a mail message, or a NULL
  *     pointer if it isn't needed.  Look in the context for an error message if
  *     name of the hook to use, the second is the full path name of a mail message.
  *     The third argument is also the full path name of a mail message, or a NULL
  *     pointer if it isn't needed.  Look in the context for an error message if
@@ -8,7 +6,11 @@
  *     Only produce the error message once.
  */
 
  *     Only produce the error message once.
  */
 
-#include <h/mh.h>
+#include "h/mh.h"
+#include "context_find.h"
+#include "pidstatus.h"
+#include "arglist.h"
+#include "error.h"
 
 int
 ext_hook(char *hook_name, char *message_file_name_1, char *message_file_name_2)
 
 int
 ext_hook(char *hook_name, char *message_file_name_1, char *message_file_name_2)
@@ -16,26 +18,29 @@ ext_hook(char *hook_name, char *message_file_name_1, char *message_file_name_2)
     char       *hook;                  /* hook program from context */
     pid_t      pid;                    /* ID of child process */
     int                status;                 /* exit or other child process status */
     char       *hook;                  /* hook program from context */
     pid_t      pid;                    /* ID of child process */
     int                status;                 /* exit or other child process status */
-    char       *vec[4];                /* argument vector for child process */
+    char       **vec;                  /* argument vector for child process */
+    int                vecp;                   /* Vector index */
+    char       *program;               /* Name of program to execute */
 
 
-    static  int        did_message = 0;        /* set if we've already output a message */
+    static bool        did_message;            /* set if we've already output a message */
 
 
-    if ((hook = context_find(hook_name)) == (char *)0)
-       return (OK);
+    if ((hook = context_find(hook_name)) == NULL)
+       return OK;
 
 
-    switch (pid = vfork()) {
+    switch (pid = fork()) {
     case -1:
        status = NOTOK;
     case -1:
        status = NOTOK;
-       advise(NULL, "external database may be out-of-date.");
+       inform("external database may be out-of-date.");
        break;
 
     case 0:
        break;
 
     case 0:
-       vec[0] = r1bindex(hook, '/');
-       vec[1] = message_file_name_1;
-       vec[2] = message_file_name_2;
-       vec[3] = (char *)0;
-       execvp(hook, vec);
-       _exit(-1);
+        vec = argsplit(hook, &program, &vecp);
+       vec[vecp++] = message_file_name_1;
+       vec[vecp++] = message_file_name_2;
+       vec[vecp++] = NULL;
+       execvp(program, vec);
+       advise(program, "Unable to execute");
+       _exit(1);
        /* NOTREACHED */
 
     default:
        /* NOTREACHED */
 
     default:
@@ -43,19 +48,20 @@ ext_hook(char *hook_name, char *message_file_name_1, char *message_file_name_2)
        break;
     }
 
        break;
     }
 
-    if (status != OK) {
-       if (did_message == 0) {
-           if ((hook = context_find("msg-hook")) != (char *)0)
-               advise(NULL, hook);
-           else
-               advise(NULL, "external hook (%s) did not work properly.", hook);
-       
-           did_message = 1;
-       }
-
-       return (NOTOK);
+    if (status == OK)
+       return OK;
+
+    if (!did_message) {
+        char *msghook;
+        if ((msghook = context_find("msg-hook")) != NULL)
+            inform("%s", msghook);
+        else {
+            char errbuf[BUFSIZ];
+            snprintf(errbuf, sizeof(errbuf), "external hook \"%s\"", hook);
+            pidstatus(status, stderr, errbuf);
+        }
+        did_message = true;
     }
 
     }
 
-    else
-       return (OK);
+    return NOTOK;
 }
 }