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