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