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