Shield template's |program{} and |attach_program{} so that the command-line
[claws.git] / src / quote_fmt_parse.y
index 515d910aeefdb9a5d671eb28e1ede6d27c5a9709..3bb8cd4d1949c8fec483c5e41c89baf72c3cfeab 100644 (file)
 #include <glib/gi18n.h>
 
 #include <ctype.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <sys/types.h>
+#include <sys/wait.h>
 
 #include "procmsg.h"
 #include "procmime.h"
@@ -37,7 +41,7 @@
 #include "quote_fmt.h"
 #include "quote_fmt_lex.h"
 #include "account.h"
-#include "claws_io.h"
+#include "file-utils.h"
 
 /* decl */
 /*
@@ -488,11 +492,11 @@ static void quote_fmt_show_msg(MsgInfo *msginfo, const gchar *body,
        if (fp == NULL)
                g_warning("Can't get text part");
        else {
-               account_signatures_matchlist_create();
+               account_sigsep_matchlist_create();
                while (fgets(buf, sizeof(buf), fp) != NULL) {
                        strcrchomp(buf);
 
-                       if (!signature && account_signatures_matchlist_nchar_found(buf, "%s\n"))
+                       if (!signature && account_sigsep_matchlist_nchar_found(buf, "%s\n"))
                                break;
                
                        if (quoted && quote_str)
@@ -500,7 +504,7 @@ static void quote_fmt_show_msg(MsgInfo *msginfo, const gchar *body,
                        
                        INSERT(buf);
                }
-               account_signatures_matchlist_delete();
+               account_sigsep_matchlist_delete();
                fclose(fp);
        }
 }
@@ -508,7 +512,7 @@ static void quote_fmt_show_msg(MsgInfo *msginfo, const gchar *body,
 static void quote_fmt_insert_file(const gchar *filename)
 {
        FILE *file;
-       char buffer[256];
+       char buffer[PATH_MAX];
        
        if ((file = g_fopen(filename, "rb")) != NULL) {
                while (fgets(buffer, sizeof(buffer), file)) {
@@ -521,15 +525,65 @@ static void quote_fmt_insert_file(const gchar *filename)
 
 static void quote_fmt_insert_program_output(const gchar *progname)
 {
-       FILE *file;
-       char buffer[256];
-
-       if ((file = popen(progname, "r")) != NULL) {
-               while (fgets(buffer, sizeof(buffer), file)) {
-                       INSERT(buffer);
-               }
-               pclose(file);
+    int pipefd[2];
+    pid_t pid;
+
+    GError *error;
+       gint argc;
+       gchar **argv;
+       gboolean ret;
+
+    /* turn the command-line string into an array */
+       argv = NULL;
+       argc = 0;
+       error = NULL;
+       ret = g_shell_parse_argv (progname, &argc, &argv, &error);
+       if (!ret) {
+               g_error("could not parse command line from '%s'", progname);
+        return;
+    }
+
+       if (pipe(pipefd) == -1) {
+               g_error("can't pipe - error %s", g_strerror(errno));
+               return;
        }
+
+       if (0 == (pid = fork())) {
+               /*
+                * redirect output to write end of pipe
+                */
+        close(pipefd[0]);
+        dup2(pipefd[1], STDOUT_FILENO);
+               if (-1 == execvp(argv[0], argv))
+                       perror("execvp");
+    } else {
+           char buffer[BUFFSIZE];
+        int r;
+
+               waitpid(pid, NULL, 0);
+
+           g_strfreev (argv);
+
+               /*
+                * make it non blocking
+                */
+        if (-1 == fcntl(pipefd[0], F_SETFL, fcntl(pipefd[0], F_GETFL) | O_NONBLOCK))
+                       g_warning("set to non blocking failed");
+
+               /*
+                * get the output
+                */
+               do {
+                       r = read(pipefd[0], buffer, sizeof buffer - 1);
+                       if (r > 0) {
+                               buffer[r] = 0;
+                           INSERT(buffer);
+                       }
+               } while (r > 0);
+
+               close(pipefd[0]);
+        close(pipefd[1]);
+    }
 }
 
 static void quote_fmt_insert_user_input(const gchar *varname)
@@ -565,18 +619,67 @@ static void quote_fmt_attach_file(const gchar *filename)
 
 static void quote_fmt_attach_file_program_output(const gchar *progname)
 {
-       FILE *file;
-       char buffer[PATH_MAX];
-
-       if ((file = popen(progname, "r")) != NULL) {
-               /* get first line only */
-               if (fgets(buffer, sizeof(buffer), file)) {
-                       /* trim trailing CR/LF */
-                       strretchomp(buffer);
-                       attachments = g_list_append(attachments, g_strdup(buffer));
-               }
-               pclose(file);
+    int pipefd[2];
+    pid_t pid;
+
+    GError *error;
+       gint argc;
+       gchar **argv;
+       gboolean ret;
+
+    /* turn the command-line string into an array */
+       argv = NULL;
+       argc = 0;
+       error = NULL;
+       ret = g_shell_parse_argv (progname, &argc, &argv, &error);
+       if (!ret) {
+               g_error("could not parse command line from '%s'", progname);
+        return;
+    }
+
+       if (pipe(pipefd) == -1) {
+               g_error("can't pipe - error %s", g_strerror(errno));
+               return;
        }
+
+       if (0 == (pid = fork())) {
+               /*
+                * redirect output to write end of pipe
+                */
+        close(pipefd[0]);
+        dup2(pipefd[1], STDOUT_FILENO);
+               if (-1 == execvp(argv[0], argv))
+                       perror("execvp");
+    } else {
+           char buffer[BUFFSIZE];
+        int r;
+
+               waitpid(pid, NULL, 0);
+
+           g_strfreev (argv);
+
+               /*
+                * make it non blocking
+                */
+        if (-1 == fcntl(pipefd[0], F_SETFL, fcntl(pipefd[0], F_GETFL) | O_NONBLOCK))
+                       g_warning("set to non blocking failed");
+
+               /*
+                * get the output
+                */
+               do {
+                       r = read(pipefd[0], buffer, sizeof buffer - 1);
+                       if (r > 0) {
+                               buffer[r] = 0;
+                           /* trim trailing CR/LF */
+                           strretchomp(buffer);
+                           attachments = g_list_append(attachments, g_strdup(buffer));
+                       }
+               } while (r > 0);
+
+               close(pipefd[0]);
+        close(pipefd[1]);
+    }
 }
 
 static gchar *quote_fmt_complete_address(const gchar *addr)