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