Templates: add a way to attach a file, 'attach_output' or 'A', file whose absolute...
authorwwp <wwp@free.fr>
Thu, 1 Sep 2016 22:40:27 +0000 (00:40 +0200)
committerwwp <wwp@free.fr>
Thu, 1 Sep 2016 22:40:27 +0000 (00:40 +0200)
is the output of a command-line (it s a mix of 'attach' and 'program'). The first line of the
command-line output is taken into account, trailing newline being stripped down.
Like 'attach', if the filename is not found or not accessible, CM will raise an error. Like
'program', if the command-line fails, no error raises but the standard output of CM shows it.

src/quote_fmt.c
src/quote_fmt_lex.l
src/quote_fmt_parse.y

index 22fb57695bfb3350560bcfbe1b5caab76de57c01..90592907b33c3e0ad0d5cd7538172f386a56ef6e 100644 (file)
@@ -93,6 +93,7 @@ static gchar *quote_desc_strings[] = {
        "|program{<span style=\"oblique\">sub_expr</span>}\n(|p{<span style=\"oblique\">sub_expr</span>})\n",   N_("insert program output:\n<span style=\"oblique\">sub_expr</span> is evaluated as a command-line to get\nthe output from"), /* insert program output */
        "|input{<span style=\"oblique\">sub_expr</span>}\n(|i{<span style=\"oblique\">sub_expr</span>})\n",             N_("insert user input:\n<span style=\"oblique\">sub_expr</span> is a variable to be replaced by\nuser-entered text"), /* insert user input */
        "|attach{<span style=\"oblique\">sub_expr</span>}\n(|a{<span style=\"oblique\">sub_expr</span>})",              N_("attach file:\n<span style=\"oblique\">sub_expr</span> is evaluated as the path of the file to attach"), /* attach file */
+       "|attach_output{<span style=\"oblique\">sub_expr</span>}\n(|A{<span style=\"oblique\">sub_expr</span>})",       N_("attach file:\n<span style=\"oblique\">sub_expr</span> is evaluated as a command-line to get\nthe filename from"), /* attach file whose name's got from program output */
        "",                                     NULL,
        N_("<span weight=\"bold\">definition of terms:</span>"),        NULL,
        "<span style=\"oblique\">expr</span>\n",                        N_("text that can contain any of the symbols or\ncommands above"),
index 46c295f5022e75652ca526b13959fbb88b300384..7af7f61aefe2db0a8172d7574b897fae3b444a77 100644 (file)
@@ -152,6 +152,7 @@ int escaped_string = 0;
 ("|p"|"|program") /* insert program output */ return INSERT_PROGRAMOUTPUT;
 ("|i"|"|input") /* insert user input */ return INSERT_USERINPUT;
 ("|a"|"|attach") /* attach file */ return ATTACH_FILE;
+("|A"|"|attach_program") /*  file whose name's got from program output */ return ATTACH_PROGRAMOUTPUT;
 <S_DATE>"{" return OPARENT;
 <S_DATE>"}" { BEGIN S_NORMAL; return CPARENT; }
 <S_NORMAL>"{" return OPARENT;
index af7f421462c7409410c97b0bd563757c1e9be644..dc330c1104f80f51fe72d549942912d6d233d0cc 100644 (file)
@@ -560,6 +560,22 @@ static void quote_fmt_attach_file(const gchar *filename)
        attachments = g_list_append(attachments, g_strdup(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);
+       }
+}
+
 static gchar *quote_fmt_complete_address(const gchar *addr)
 {
        gint count;
@@ -641,7 +657,7 @@ static gchar *quote_fmt_complete_address(const gchar *addr)
 %token QUERY_NOT_TO_FOUND_IN_ADDRESSBOOK
 /* other tokens */
 %token INSERT_FILE INSERT_PROGRAMOUTPUT INSERT_USERINPUT
-%token ATTACH_FILE
+%token ATTACH_FILE ATTACH_PROGRAMOUTPUT
 %token OPARENT CPARENT
 %token CHARACTER
 %token SHOW_DATE_EXPR
@@ -1312,4 +1328,17 @@ attach:
                if (!dry_run) {
                        quote_fmt_attach_file(sub_expr.buffer);
                }
+       }
+       | ATTACH_PROGRAMOUTPUT
+       {
+               current = &sub_expr;
+               clear_buffer();
+       }
+       OPARENT sub_expr CPARENT
+       {
+               current = &main_expr;
+               if (!dry_run) {
+                       quote_fmt_attach_file_program_output(sub_expr.buffer);
+               }
        };
+;