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