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