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