New file. Source for mhpath command.
Showing
1 changed file
with
128 additions
and
0 deletions
mh/mhpath.c
0 → 100644
1 | /* GNU mailutils - a suite of utilities for electronic mail | ||
2 | Copyright (C) 2002 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 mhpath command */ | ||
19 | |||
20 | #include <mh.h> | ||
21 | |||
22 | const char *argp_program_version = "mhpath (" PACKAGE_STRING ")"; | ||
23 | static char doc[] = "GNU MH mhpath"; | ||
24 | static char args_doc[] = "[+folder] [msgs]"; | ||
25 | |||
26 | /* GNU options */ | ||
27 | static struct argp_option options[] = { | ||
28 | {"folder", 'f', "FOLDER", 0, "Specify folder to operate upon"}, | ||
29 | { "\nUse -help switch to obtain the list of traditional MH options. ", 0, 0, OPTION_DOC, "" }, | ||
30 | { 0 } | ||
31 | }; | ||
32 | |||
33 | /* Traditional MH options */ | ||
34 | struct mh_option mh_option[] = { | ||
35 | { 0 } | ||
36 | }; | ||
37 | |||
38 | static int | ||
39 | opt_handler (int key, char *arg, void *unused) | ||
40 | { | ||
41 | switch (key) | ||
42 | { | ||
43 | case '+': | ||
44 | case 'f': | ||
45 | current_folder = arg; | ||
46 | break; | ||
47 | |||
48 | default: | ||
49 | return 1; | ||
50 | } | ||
51 | return 0; | ||
52 | } | ||
53 | |||
54 | void | ||
55 | mhpath (mailbox_t mbox, message_t msg, size_t num, void *data) | ||
56 | { | ||
57 | size_t uid; | ||
58 | |||
59 | mh_message_number (msg, &uid); | ||
60 | printf ("%s/%lu\n", (char*) data, (unsigned long) uid); | ||
61 | } | ||
62 | |||
63 | int | ||
64 | main (int argc, char **argv) | ||
65 | { | ||
66 | int index = 0; | ||
67 | mailbox_t mbox = NULL; | ||
68 | url_t url = NULL; | ||
69 | char *mhdir; | ||
70 | size_t total; | ||
71 | mh_msgset_t msgset; | ||
72 | int status; | ||
73 | |||
74 | mh_argp_parse (argc, argv, options, mh_option, args_doc, doc, | ||
75 | opt_handler, NULL, &index); | ||
76 | |||
77 | /* If the only argument is `+', your MH Path is output; this | ||
78 | can be useful is shell scripts. */ | ||
79 | if (current_folder[0] == 0) | ||
80 | { | ||
81 | printf ("%s\n", mu_path_folder_dir); | ||
82 | exit (0); | ||
83 | } | ||
84 | |||
85 | mbox = mh_open_folder (current_folder, 0); | ||
86 | |||
87 | mailbox_messages_count (mbox, &total); | ||
88 | mailbox_get_url (mbox, &url); | ||
89 | mhdir = url_to_string (url); | ||
90 | if (strncmp (mhdir, "mh:", 3) == 0) | ||
91 | mhdir += 3; | ||
92 | |||
93 | /* If no `msgs' are specified, mhpath outputs the folder pathname | ||
94 | instead. */ | ||
95 | if (index == argc) | ||
96 | { | ||
97 | printf ("%s\n", mhdir); | ||
98 | exit (0); | ||
99 | } | ||
100 | |||
101 | /* the name "new" has been added to mhpath's list of | ||
102 | reserved message names (the others are "first", "last", | ||
103 | "prev", "next", "cur", and "all"). The new message is | ||
104 | equivalent to the message after the last message in a | ||
105 | folder (and equivalent to 1 in a folder without messages). | ||
106 | The "new" message may not be used as part of a message | ||
107 | range. */ | ||
108 | |||
109 | if (argc - index == 1 && strcmp (argv[index], "new") == 0) | ||
110 | { | ||
111 | message_t msg = NULL; | ||
112 | size_t num; | ||
113 | |||
114 | mailbox_get_message (mbox, total, &msg); | ||
115 | mh_message_number (msg, &num); | ||
116 | printf ("%s/%lu\n", mhdir, num + 1); | ||
117 | exit (0); | ||
118 | } | ||
119 | |||
120 | /* Mhpath expands and sorts the message list `msgs' and | ||
121 | writes the full pathnames of the messages to the standard | ||
122 | output separated by newlines. */ | ||
123 | mh_msgset_parse (mbox, &msgset, argc - index, argv + index); | ||
124 | status = mh_iterate (mbox, &msgset, mhpath, mhdir); | ||
125 | mailbox_close (mbox); | ||
126 | mailbox_destroy (&mbox); | ||
127 | return status; | ||
128 | } |
-
Please register or sign in to post a comment