Remove tests/inc/test-eom-align
[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 #include <unistd.h>
12 #include <locale.h>
13 #include <sysexits.h>
14
15 static struct swit switches[] = {
16 #define VERSIONSW 0
17         { "Version", 0 },
18 #define HELPSW  1
19         { "help", 0 },
20         { NULL, 0 }
21 };
22
23 char *version=VERSION;
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         setlocale(LC_ALL, "");
36         invo_name = mhbasename(argv[0]);
37
38         /* read user profile/context */
39         context_read();
40
41         arguments = getarguments(invo_name, argc, argv, 1);
42         argp = arguments;
43
44         /*
45         ** Parse arguments
46         */
47         while ((cp = *argp++)) {
48                 if (*cp == '-') {
49                         switch (smatch(++cp, switches)) {
50                         case AMBIGSW:
51                                 ambigsw(cp, switches);
52                                 exit(EX_USAGE);
53                         case UNKWNSW:
54                                 adios(EX_USAGE, NULL, "-%s unknown", cp);
55
56                         case HELPSW:
57                                 snprintf(buf, sizeof(buf), "%s [+folder] [msgs] [switches]", invo_name);
58                                 print_help(buf, switches, 1);
59                                 exit(argc == 2 ? EX_OK : EX_USAGE);
60                         case VERSIONSW:
61                                 print_version(invo_name);
62                                 exit(argc == 2 ? EX_OK : EX_USAGE);
63                         }
64                 }
65                 if (*cp == '+' || *cp == '@') {
66                         if (folder) {
67                                 adios(EX_USAGE, NULL, "only one folder at a time!");
68                         } else {
69                                 folder = mh_xstrdup(expandfol(cp));
70                         }
71                 } else {
72                         app_msgarg(&msgs, cp);
73                 }
74         }
75
76         if (!folder)
77                 folder = getcurfol();
78         maildir = toabsdir(folder);
79
80         /* If no messages are given, print folder pathname */
81         if (!msgs.size) {
82                 printf("%s\n", maildir);
83                 exit(EX_OK);
84         }
85
86         if (chdir(maildir) == NOTOK)
87                 adios(EX_OSERR, maildir, "unable to change directory to");
88
89         /* read folder and create message structure */
90         if (!(mp = folder_read(folder)))
91                 adios(EX_IOERR, NULL, "unable to read folder %s", folder);
92
93         /*
94         ** We need to make sure there is message status space
95         ** for all the message numbers from 1 to one beyond last since
96         ** mhpath can select empty slots.  If we are adding
97         ** space at the end, we go ahead and add 10 slots.
98         */
99         if (mp->hghmsg >= mp->hghoff) {
100                 if (!(mp = folder_realloc(mp, 1, mp->hghmsg + 10))) {
101                         adios(EX_OSERR, NULL, "unable to allocate folder storage");
102                 }
103         } else if (mp->lowoff > 1) {
104                 if (!(mp = folder_realloc(mp, 1, mp->hghoff))) {
105                         adios(EX_OSERR, NULL, "unable to allocate folder storage");
106                 }
107         }
108         /*
109         ** TODO: As folder_realloc() checks itself if the realloc
110         ** really is necessary, why don't we then:
111         **    if (!(mp = folder_realloc (mp, 1, mp->hghmsg+1)))
112         **        adios (NULL, "unable to allocate folder storage");
113         ** ? This at least appears most clear to me. -- meillo
114         */
115
116
117         mp->msgflags |= ALLOW_BEYOND;  /* allow the beyond sequence */
118
119         /* parse all the message ranges/sequences and set SELECTED */
120         for (i = 0; i < msgs.size; i++) {
121                 if (!m_convert(mp, msgs.msgs[i])) {
122                         exit(EX_SOFTWARE);
123                 }
124         }
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         return EX_OK;
137 }