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