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