1 /* GNU Mailutils -- a suite of utilities for electronic mail
2    Copyright (C) 2003-2021 Free Software Foundation, Inc.
3 
4    GNU Mailutils is free software; you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation; either version 3, or (at your option)
7    any later version.
8 
9    GNU Mailutils is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13 
14    You should have received a copy of the GNU General Public License
15    along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
16 
17 /* MH mhl command */
18 
19 #include <mh.h>
20 #include <sys/stat.h>
21 #include <unistd.h>
22 
23 static char prog_doc[] = N_("Produce formatted listings of MH messages");
24 static char args_doc[] = N_("[FILE [FILE...]]");
25 
26 static int bell_option;
27 static int clear_option;
28 
29 static int interactive;  /* Using interactive output */
30 static int mhl_fmt_flags; /* MHL format flags. Controlled by -bell
31                              and -clear */
32 static int length = 40;  /* Length of output page */
33 static int width = 80;   /* Width of output page */
34 static char *formfile = MHLIBDIR "/mhl.format";
35 static const char *moreproc;
36 static int nomoreproc;
37 
38 static mu_list_t format;
39 
40 static struct mu_option options[] = {
41   { "bell",      0,       NULL, MU_OPTION_DEFAULT,
42     N_("ring the bell at the end of each output page"),
43     mu_c_bool, &bell_option },
44   { "clear",     0,       NULL, MU_OPTION_DEFAULT,
45     N_("clear the screen after each page of output"),
46     mu_c_bool, &clear_option },
47   { "form",      0,       N_("FILE"), MU_OPTION_DEFAULT,
48     N_("read format from given file"),
49     mu_c_string, &formfile, mh_opt_find_file },
50   { "width",     0,       N_("NUMBER"), MU_OPTION_DEFAULT,
51     N_("set output width"),
52     mu_c_int, &width },
53   { "length",    0,       N_("NUMBER"), MU_OPTION_DEFAULT,
54     N_("set output screen length"),
55     mu_c_int, &length },
56   { "moreproc",  0,       N_("PROG"), MU_OPTION_DEFAULT,
57     N_("use given PROG instead of the default"),
58     mu_c_string, &moreproc },
59   { "nomoreproc", 0, NULL, MU_OPTION_DEFAULT,
60     N_("disable use of moreproc program"),
61     mu_c_bool, &nomoreproc },
62   MU_OPTION_END
63 };
64 
65 static mu_stream_t
open_output()66 open_output ()
67 {
68   int rc;
69   mu_stream_t output;
70 
71   if (interactive && !nomoreproc)
72     {
73       if (!moreproc || !moreproc[0])
74 	moreproc = mh_global_profile_get ("moreproc", getenv ("PAGER"));
75     }
76   else
77     moreproc = NULL;
78 
79   if (moreproc)
80     rc = mu_command_stream_create (&output, moreproc, MU_STREAM_WRITE);
81   else
82     {
83       rc = 0;
84       output = mu_strout;
85       mu_stream_ref (output);
86     }
87 
88   if (rc)
89     {
90       mu_error (_("cannot create output stream: %s"), mu_strerror (rc));
91       exit (1);
92     }
93 
94   return output;
95 }
96 
97 static void
list_message(char * name,mu_stream_t output)98 list_message (char *name, mu_stream_t output)
99 {
100   int rc;
101   mu_stream_t input;
102   mu_message_t msg;
103 
104   if (!name)
105     {
106       rc = 0;
107       input = mu_strin;
108       mu_stream_ref (input);
109     }
110   else
111     rc = mu_file_stream_create (&input, name, MU_STREAM_READ);
112   if (rc)
113     {
114       mu_error (_("cannot create input stream: %s"), mu_strerror (rc));
115       return;
116     }
117 
118   msg = mh_stream_to_message (input);
119   if (!msg)
120     {
121       mu_error (_("input stream %s is not a message (%s)"),
122 		name, mu_strerror (rc));
123       mu_stream_close (input);
124       mu_stream_destroy (&input);
125     }
126   else
127     {
128       mhl_format_run (format, width, length, mhl_fmt_flags, msg, output);
129       mu_message_unref (msg);
130     }
131 }
132 
133 int
main(int argc,char ** argv)134 main (int argc, char **argv)
135 {
136   mu_stream_t output;
137 
138   interactive = isatty (1) && isatty (0);
139 
140   mh_getopt (&argc, &argv, options, MH_GETOPT_DEFAULT_FOLDER,
141 	     args_doc, prog_doc, NULL);
142 
143   if (bell_option == -1)
144     /* use default */;
145   else if (bell_option)
146     mhl_fmt_flags |= MHL_BELL;
147   else
148     mhl_fmt_flags &= ~MHL_BELL;
149 
150   if (clear_option == -1)
151     /* use default */;
152   else if (clear_option)
153     mhl_fmt_flags |= MHL_CLEARSCREEN;
154   else
155     mhl_fmt_flags &= ~MHL_CLEARSCREEN;
156 
157   format = mhl_format_compile (formfile);
158   if (!format)
159     exit (1);
160 
161   if (argc == 0)
162     nomoreproc = 1;
163 
164   if (!interactive)
165     mhl_fmt_flags &= ~MHL_BELL;
166 
167   output = open_output ();
168 
169   if (argc == 0)
170     list_message (NULL, output);
171   else
172     while (argc--)
173       list_message (*argv++, output);
174 
175   mu_stream_close (output);
176   return 0;
177 }
178