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