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