Commit 63bea6e1 63bea6e1af33d7d6d04f84d52b4d894e65350079 by Sergey Poznyakoff

A framework for mhl program.

1 parent b9a12f86
Showing 1 changed file with 226 additions and 0 deletions
1 /* GNU Mailutils -- a suite of utilities for electronic mail
2 Copyright (C) 2003 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 2, 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, write to the Free Software
16 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
17
18 /* MH mhl command */
19
20 #include <mh.h>
21 #include <sys/stat.h>
22 #include <unistd.h>
23
24 const char *argp_program_version = "mhl (" PACKAGE_STRING ")";
25 static char doc[] = N_("GNU MH mhl\v"
26 "Options marked with `*' are not yet implemented.\n"
27 "Use -help to obtain the list of traditional MH options.");
28 static char args_doc[] = N_("[files]");
29
30 /* GNU options */
31 static struct argp_option options[] = {
32 {"folder", ARG_FOLDER, N_("FOLDER"), 0,
33 N_("Specify folder to operate upon")},
34 { "bell", ARG_BELL, N_("BOOL"), OPTION_ARG_OPTIONAL,
35 N_("* Ring the bell at the end of each output page") },
36 {"nobell", ARG_NOBELL, NULL, OPTION_HIDDEN, "" },
37 { "clear", ARG_CLEAR, N_("BOOL"), OPTION_ARG_OPTIONAL,
38 N_("* Clear the screen after each page of output")},
39 {"noclear", ARG_NOCLEAR, NULL, OPTION_HIDDEN, "" },
40 {"form", ARG_FORM, N_("FILE"), 0,
41 N_("* Read format from given file")},
42 {"width", ARG_WIDTH, N_("NUMBER"), 0,
43 N_("* Set output width")},
44 {"length", ARG_LENGTH, N_("NUMBER"), 0,
45 N_("* Set output screen length")},
46 {"moreproc", ARG_MOREPROC, N_("PROG"), 0,
47 N_("Use given PROG instead of the default") },
48 {"nomoreproc", ARG_NOMOREPROC, NULL, 0,
49 N_("Disable use of moreproc program") },
50 { NULL }
51 };
52
53 /* Traditional MH options */
54 struct mh_option mh_option[] = {
55 { "bell", 1, MH_OPT_BOOL },
56 { "clear", 1, MH_OPT_BOOL },
57 { "form", 1, MH_OPT_ARG, "formatfile"},
58 { "width", 1, MH_OPT_ARG, "number"},
59 { "length", 1, MH_OPT_ARG, "number"},
60 { "moreproc", 1, MH_OPT_ARG, "program"},
61 { "nomoreproc", 3, },
62 { NULL }
63 };
64
65 static int interactive; /* Using interactive output */
66 static int bell = 1; /* Ring the bell after each page of output */
67 static int clear = 0; /* Clear the screen after each page of output */
68 static int length = 40; /* Length of output page */
69 static int width = 80; /* Width of output page */
70 static char *formfile = "mhl.format";
71 static char *moreproc;
72 static int nomoreproc;
73
74 static int
75 opt_handler (int key, char *arg, void *unused)
76 {
77 switch (key)
78 {
79 case '+':
80 case ARG_FOLDER:
81 current_folder = arg;
82 break;
83
84 case ARG_BELL:
85 bell = is_true (arg);
86 break;
87
88 case ARG_NOBELL:
89 bell = 0;
90 break;
91
92 case ARG_CLEAR:
93 clear = is_true (arg);
94 break;
95
96 case ARG_NOCLEAR:
97 clear = 0;
98 break;
99
100 case ARG_FORM:
101 formfile = arg;
102 break;
103
104 case ARG_WIDTH:
105 width = strtoul (arg, NULL, 0);
106 if (!width)
107 {
108 mh_error (_("Invalid width"));
109 exit (1);
110 }
111 break;
112
113 case ARG_LENGTH:
114 length = strtoul (arg, NULL, 0);
115 if (!length)
116 {
117 mh_error (_("Invalid length"));
118 exit (1);
119 }
120 break;
121
122 case ARG_MOREPROC:
123 moreproc = arg;
124 break;
125
126 case ARG_NOMOREPROC:
127 nomoreproc = 1;
128 break;
129
130 default:
131 return 1;
132 }
133 return 0;
134 }
135
136 static stream_t
137 open_output ()
138 {
139 int rc;
140 stream_t output;
141
142 if (interactive && !nomoreproc)
143 moreproc = mh_global_profile_get ("moreproc", getenv ("PAGER"));
144 else
145 moreproc = NULL;
146
147 if (moreproc)
148 rc = prog_stream_create (&output, moreproc, MU_STREAM_WRITE);
149 else
150 rc = stdio_stream_create (&output, stdout, MU_STREAM_WRITE);
151
152 if (rc)
153 {
154 mh_error (_("cannot create output stream: %s"), mu_strerror (rc));
155 exit (1);
156 }
157
158 if ((rc = stream_open (output)))
159 {
160 mh_error (_("cannot open output stream: %s"), mu_strerror (rc));
161 exit (1);
162 }
163 return output;
164 }
165
166 static void
167 list_message (char *name, stream_t output)
168 {
169 stream_t input;
170 int rc;
171 char buf[512];
172 size_t n;
173
174 if (!name)
175 rc = stdio_stream_create (&input, stdin, 0);
176 else
177 rc = file_stream_create (&input, name, MU_STREAM_READ);
178 if (rc)
179 {
180 mh_error (_("cannot create input stream: %s"), mu_strerror (rc));
181 return;
182 }
183
184 if ((rc = stream_open (input)))
185 {
186 mh_error (_("cannot open input stream: %s"), mu_strerror (rc));
187 stream_destroy (&input, stream_get_owner (input));
188 return;
189 }
190
191 while (stream_sequential_readline (input, buf, sizeof buf, &n) == 0
192 && n > 0)
193 stream_sequential_write (output, buf, n);
194
195 stream_close (input);
196 stream_destroy (&input, stream_get_owner (input));
197 }
198
199 int
200 main (int argc, char **argv)
201 {
202 int index;
203 stream_t output;
204
205 interactive = isatty (1) && isatty (0);
206
207 mu_init_nls ();
208 mh_argp_parse (argc, argv, options, mh_option, args_doc, doc,
209 opt_handler, NULL, &index);
210
211 argc -= index;
212 argv += index;
213
214 if (argc == 0)
215 nomoreproc = 1;
216
217 output = open_output ();
218
219 if (argc == 0)
220 list_message (NULL, output);
221 else
222 while (argc--)
223 list_message (*argv++, output);
224
225 return 0;
226 }