Implementation of scan command.
Showing
1 changed file
with
251 additions
and
0 deletions
mh/scan.c
0 → 100644
1 | /* GNU mailutils - a suite of utilities for electronic mail | ||
2 | Copyright (C) 1999, 2000, 2001 Free Software Foundation, Inc. | ||
3 | |||
4 | This program 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 | This program 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 this program; if not, write to the Free Software | ||
16 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ | ||
17 | |||
18 | /* MH scan command */ | ||
19 | |||
20 | #include <mh.h> | ||
21 | #ifdef HAVE_TERMCAP_H | ||
22 | # include <termcap.h> | ||
23 | #endif | ||
24 | |||
25 | const char *argp_program_version = "scan (" PACKAGE ") " VERSION; | ||
26 | const char *argp_program_bug_address = "<bug-mailutils@gnu.org>"; | ||
27 | static char doc[] = "GNU MH scan"; | ||
28 | static char args_doc[] = ""; | ||
29 | |||
30 | /* GNU options */ | ||
31 | static struct argp_option options[] = { | ||
32 | {"folder", 'f', "FOLDER", 0, "Specify folder to scan"}, | ||
33 | {"clear", 'c', "BOOL", OPTION_ARG_OPTIONAL, "Clear screen after displaying the list"}, | ||
34 | {"form", 'F', "FILE", 0, "Read format from given file"}, | ||
35 | {"format", 't', "FORMAT", 0, "Use this format string"}, | ||
36 | {"header", 'H', "BOOL", OPTION_ARG_OPTIONAL, "Display header"}, | ||
37 | {"width", 'w', "NUMBER", 0, "Set output width"}, | ||
38 | {"reverse", 'r', "BOOL", OPTION_ARG_OPTIONAL, "List messages in reverse order"}, | ||
39 | {"file", 'i', "FILE", 0, "[Not yet implemented]"}, | ||
40 | |||
41 | {"license", 'l', 0, 0, "Display software license", -1}, | ||
42 | |||
43 | { "\nUse -help switch to obtain the list of traditional MH options. ", 0, 0, OPTION_DOC, "" }, | ||
44 | |||
45 | { 0 } | ||
46 | }; | ||
47 | |||
48 | /* Traditional MH options */ | ||
49 | struct mh_option mh_option[] = { | ||
50 | {"clear", 1, 'c', MH_OPT_BOOL}, | ||
51 | {"form", 4, 'F', MH_OPT_ARG, "formatfile"}, | ||
52 | {"format", 5, 't', MH_OPT_ARG, "string"}, | ||
53 | {"header", 3, 'H', MH_OPT_BOOL}, | ||
54 | {"width", 1, 'w', MH_OPT_ARG, "number"}, | ||
55 | {"reverse", 1, 'r', MH_OPT_BOOL}, | ||
56 | {"file", 4, 'i', MH_OPT_ARG, "file"}, | ||
57 | { 0 } | ||
58 | }; | ||
59 | |||
60 | static char *scan_folder = "inbox"; | ||
61 | static int clear; | ||
62 | static char *formfile; | ||
63 | static char *format_str = | ||
64 | "%4(msg)%<(cur)+%| %>%<{replied}-%?{encrypted}E%| %>" | ||
65 | "%02(mon{date})/%02(mday{date})" | ||
66 | "%<{date} %|*%>" | ||
67 | "%<(mymbox{from})%<{to}To:%14(friendly{to})%>%>" | ||
68 | "%<(zero)%17(friendly{from})%>" | ||
69 | " %{subject}%<{body}<<%{body}>>%>"; | ||
70 | |||
71 | static int width = 80; | ||
72 | static int reverse; | ||
73 | |||
74 | static mh_format_t format; | ||
75 | |||
76 | void list_message (mailbox_t mbox, size_t msgno, char *bp, size_t width); | ||
77 | int scan (mailbox_t mbox); | ||
78 | |||
79 | static int | ||
80 | opt_handler (int key, char *arg, void *unused) | ||
81 | { | ||
82 | switch (key) | ||
83 | { | ||
84 | case 'f': | ||
85 | scan_folder = arg; | ||
86 | break; | ||
87 | |||
88 | case 'c': | ||
89 | clear = arg[0] == 'y'; | ||
90 | break; | ||
91 | |||
92 | case 'F': | ||
93 | formfile = arg; | ||
94 | break; | ||
95 | |||
96 | case 't': | ||
97 | format_str = arg; | ||
98 | break; | ||
99 | |||
100 | case 'H': | ||
101 | clear = arg[0] == 'y'; | ||
102 | break; | ||
103 | |||
104 | case 'w': | ||
105 | width = strtoul (arg, NULL, 0); | ||
106 | if (!width) | ||
107 | { | ||
108 | mh_error ("Invalid width"); | ||
109 | exit (1); | ||
110 | } | ||
111 | break; | ||
112 | |||
113 | case 'r': | ||
114 | reverse = arg[0] == 'y'; | ||
115 | break; | ||
116 | |||
117 | case 'i': | ||
118 | mh_error ("'i' is not yet implemented."); | ||
119 | break; | ||
120 | |||
121 | case 'l': | ||
122 | mh_license (argp_program_version); | ||
123 | break; | ||
124 | |||
125 | default: | ||
126 | return 1; | ||
127 | } | ||
128 | return 0; | ||
129 | } | ||
130 | |||
131 | void | ||
132 | set_folder () | ||
133 | { | ||
134 | scan_folder = mu_tilde_expansion (scan_folder, "/", NULL); | ||
135 | if (strchr (scan_folder, '/') == NULL) | ||
136 | { | ||
137 | char *p = mu_get_homedir (); | ||
138 | asprintf (&scan_folder, "mh:%s/Mail/%s", p, scan_folder); | ||
139 | if (!scan_folder) | ||
140 | { | ||
141 | mh_error ("low memory"); | ||
142 | exit (1); | ||
143 | } | ||
144 | } | ||
145 | else if (strchr (scan_folder, ':') == NULL) | ||
146 | { | ||
147 | char *p; | ||
148 | p = xmalloc (strlen (scan_folder) + 4); | ||
149 | strcat (strcpy (p, "mh:"), scan_folder); | ||
150 | scan_folder = p; | ||
151 | } | ||
152 | } | ||
153 | |||
154 | int | ||
155 | main (int argc, char **argv) | ||
156 | { | ||
157 | mailbox_t mbox = NULL; | ||
158 | |||
159 | mh_init (); | ||
160 | mh_argp_parse (argc, argv, options, mh_option, args_doc, doc, | ||
161 | opt_handler, NULL); | ||
162 | |||
163 | set_folder (); | ||
164 | if (mh_format_parse (format_str, &format)) | ||
165 | { | ||
166 | mh_error ("Bad format string"); | ||
167 | exit (1); | ||
168 | } | ||
169 | |||
170 | if (mailbox_create_default (&mbox, scan_folder)) | ||
171 | { | ||
172 | mh_error ("Can't create mailbox %s: %s", scan_folder, strerror (errno)); | ||
173 | exit (1); | ||
174 | } | ||
175 | |||
176 | if (mailbox_open (mbox, MU_STREAM_READ)) | ||
177 | { | ||
178 | mh_error ("Can't open mailbox %s: %s", scan_folder, strerror (errno)); | ||
179 | exit (1); | ||
180 | } | ||
181 | |||
182 | return scan (mbox); | ||
183 | } | ||
184 | |||
185 | #ifdef HAVE_TERMCAP_H | ||
186 | /* A subroutine for tputs */ | ||
187 | int | ||
188 | putstdout(char c) | ||
189 | { | ||
190 | return putc(c, stdout); | ||
191 | } | ||
192 | #endif | ||
193 | |||
194 | void | ||
195 | clear_screen () | ||
196 | { | ||
197 | #ifdef HAVE_TERMCAP_H | ||
198 | if (clear) | ||
199 | { | ||
200 | char termcap_buf[1024]; | ||
201 | char *buffer; | ||
202 | char *termname; | ||
203 | |||
204 | if ((termname = getenv("TERM")) == NULL) | ||
205 | /* No terminal; Try ansi */ | ||
206 | termname = "ansi"; | ||
207 | |||
208 | if (tgetent(termcap_buf, termname) != 1) | ||
209 | fprintf (stdout, "\f"); | ||
210 | else | ||
211 | { | ||
212 | char *clr = tgetstr ("cl", &buffer); | ||
213 | if (clr) | ||
214 | tputs(clr, 1, (int (*)())putstdout); | ||
215 | else | ||
216 | fprintf (stdout, "\f"); | ||
217 | } | ||
218 | } | ||
219 | #endif | ||
220 | } | ||
221 | |||
222 | int | ||
223 | scan (mailbox_t mbox) | ||
224 | { | ||
225 | size_t i, total; | ||
226 | char *buffer; | ||
227 | |||
228 | buffer = xmalloc (width); | ||
229 | |||
230 | mailbox_messages_count (mbox, &total); | ||
231 | |||
232 | if (reverse) | ||
233 | for (i = total; i >= 1; i--) | ||
234 | list_message (mbox, i, buffer, width); | ||
235 | else | ||
236 | for (i = 1; i <= total; i++) | ||
237 | list_message (mbox, i, buffer, width); | ||
238 | clear_screen (); | ||
239 | return 0; | ||
240 | } | ||
241 | |||
242 | void | ||
243 | list_message (mailbox_t mbox, size_t msgno, char *buffer, size_t width) | ||
244 | { | ||
245 | message_t msg; | ||
246 | |||
247 | buffer[0] = 0; | ||
248 | mailbox_get_message (mbox, msgno, &msg); | ||
249 | mh_format (&format, msg, msgno, buffer, width); | ||
250 | printf ("%s\n", buffer); | ||
251 | } |
-
Please register or sign in to post a comment