]> diplodocus.org Git - nmh/blob - uip/mhpath.c
46b24361119756f32ccad6d0ebb311bcc692bed2
[nmh] / uip / mhpath.c
1 /* mhpath.c -- print full pathnames of nmh messages and folders
2 *
3 * This code is Copyright (c) 2002, by the authors of nmh. See the
4 * COPYRIGHT file in the root directory of the nmh distribution for
5 * complete copyright information.
6 */
7
8 #include "h/mh.h"
9 #include "sbr/getarguments.h"
10 #include "sbr/seq_setprev.h"
11 #include "sbr/seq_save.h"
12 #include "sbr/smatch.h"
13 #include "sbr/m_convert.h"
14 #include "sbr/getfolder.h"
15 #include "sbr/folder_read.h"
16 #include "sbr/folder_realloc.h"
17 #include "sbr/folder_free.h"
18 #include "sbr/context_save.h"
19 #include "sbr/context_find.h"
20 #include "sbr/ambigsw.h"
21 #include "sbr/path.h"
22 #include "sbr/print_version.h"
23 #include "sbr/print_help.h"
24 #include "sbr/error.h"
25 #include "h/done.h"
26 #include "h/utils.h"
27 #include "sbr/m_maildir.h"
28
29 #define MHPATH_SWITCHES \
30 X("version", 0, VERSIONSW) \
31 X("help", 0, HELPSW) \
32
33 #define X(sw, minchars, id) id,
34 DEFINE_SWITCH_ENUM(MHPATH);
35 #undef X
36
37 #define X(sw, minchars, id) { sw, minchars, id },
38 DEFINE_SWITCH_ARRAY(MHPATH, switches);
39 #undef X
40
41 int
42 main(int argc, char **argv)
43 {
44 int i;
45 char *cp, *maildir, *folder = NULL;
46 char **argp;
47 char **arguments, buf[BUFSIZ];
48 struct msgs_array msgs = { 0, 0, NULL };
49 struct msgs *mp;
50
51 if (nmh_init(argv[0], true, false)) { return 1; }
52
53 arguments = getarguments (invo_name, argc, argv, 1);
54 argp = arguments;
55
56 /*
57 * Parse arguments
58 */
59 while ((cp = *argp++)) {
60 if (*cp == '-') {
61 switch (smatch (++cp, switches)) {
62 case AMBIGSW:
63 ambigsw (cp, switches);
64 done (1);
65 case UNKWNSW:
66 die("-%s unknown", cp);
67
68 case HELPSW:
69 snprintf (buf, sizeof(buf), "%s [+folder] [msgs] [switches]",
70 invo_name);
71 print_help (buf, switches, 1);
72 done (0);
73 case VERSIONSW:
74 print_version(invo_name);
75 done (0);
76 }
77 }
78 if (*cp == '+' || *cp == '@') {
79 if (folder)
80 die("only one folder at a time!");
81 folder = pluspath (cp);
82 } else
83 app_msgarg(&msgs, cp);
84 }
85
86 if (!context_find ("path"))
87 free (path ("./", TFOLDER));
88
89 if (!folder)
90 folder = getfolder (1);
91 maildir = m_maildir (folder);
92
93 /* If no messages are given, print folder pathname */
94 if (!msgs.size) {
95 puts(maildir);
96 done (0);
97 }
98
99 if (chdir (maildir) == NOTOK)
100 adios (maildir, "unable to change directory to");
101
102 /* read folder and create message structure */
103 if (!(mp = folder_read (folder, 1)))
104 die("unable to read folder %s", folder);
105
106 /*
107 * We need to make sure there is message status space
108 * for all the message numbers from 1 to "new" since
109 * mhpath can select empty slots. If we are adding
110 * space at the end, we go ahead and add 10 slots.
111 */
112 if (mp->hghmsg >= mp->hghoff) {
113 if (!(mp = folder_realloc (mp, 1, mp->hghmsg + 10)))
114 die("unable to allocate folder storage");
115 } else if (mp->lowoff > 1) {
116 if (!(mp = folder_realloc (mp, 1, mp->hghoff)))
117 die("unable to allocate folder storage");
118 }
119
120 mp->msgflags |= ALLOW_NEW; /* allow the "new" sequence */
121
122 /* parse all the message ranges/sequences and set SELECTED */
123 for (i = 0; i < msgs.size; i++)
124 if (!m_convert (mp, msgs.msgs[i]))
125 done (1);
126
127 seq_setprev (mp); /* set the previous-sequence */
128
129 /* print the path of all selected messages */
130 for (i = mp->lowsel; i <= mp->hghsel; i++)
131 if (is_selected (mp, i))
132 printf ("%s/%s\n", mp->foldpath, m_name (i));
133
134 seq_save (mp); /* synchronize message sequences */
135 context_save (); /* save the context file */
136 folder_free (mp); /* free folder/message structure */
137 done (0);
138 return 1;
139 }