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