]>
diplodocus.org Git - nmh/blob - uip/ali.c
1 /* ali.c -- list nmh mail aliases
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.
9 #include "sbr/getcpy.h"
10 #include "sbr/context_find.h"
11 #include "sbr/brkstring.h"
12 #include "sbr/ambigsw.h"
13 #include "sbr/print_version.h"
14 #include "sbr/print_help.h"
15 #include "sbr/error.h"
16 #include "h/addrsbr.h"
17 #include "h/aliasbr.h"
22 #define ALI_SWITCHES \
23 X("alias aliasfile", 0, ALIASW) \
24 X("noalias", 0, NALIASW) \
25 X("list", 0, LISTSW) \
26 X("nolist", 0, NLISTSW) \
27 X("user", 0, USERSW) \
28 X("nouser", 0, NUSERSW) \
29 X("version", 0, VERSIONSW) \
30 X("help", 0, HELPSW) \
32 #define X(sw, minchars, id) id,
33 DEFINE_SWITCH_ENUM(ALI
);
36 #define X(sw, minchars, id) { sw, minchars, id },
37 DEFINE_SWITCH_ARRAY(ALI
, switches
);
42 extern struct aka
*akahead
;
47 static void print_aka (char *, bool, int);
48 static void print_usr (char *, bool);
52 main (int argc
, char **argv
)
55 bool inverted
, list
, noalias
;
56 char *cp
, **ap
, **argp
, buf
[BUFSIZ
];
57 /* Really only need to allocate for argc-1, but must allocate at least 1,
58 so go ahead and allocate for argc char pointers. */
59 char **vec
= mh_xmalloc (argc
* sizeof (char *)), **arguments
;
62 if (nmh_init(argv
[0], true, true)) { return 1; }
65 arguments
= getarguments (invo_name
, argc
, argv
, 1);
68 inverted
= list
= noalias
= false;
69 while ((cp
= *argp
++)) {
71 switch (smatch (++cp
, switches
)) {
73 ambigsw (cp
, switches
);
76 die("-%s unknown", cp
);
79 snprintf (buf
, sizeof(buf
), "%s [switches] aliases ...",
81 print_help (buf
, switches
, 1);
84 print_version (invo_name
);
88 if (!(cp
= *argp
++) || *cp
== '-')
89 die("missing argument to %s", argp
[-2]);
90 if ((i
= alias (cp
)) != AK_OK
)
91 die("aliasing error in %s - %s", cp
, akerror (i
));
116 /* Should never happen, but try to protect against code changes
117 that could allow it. */
118 die("too many arguments");
123 /* allow Aliasfile: profile entry */
124 if ((cp
= context_find ("Aliasfile"))) {
127 for (ap
= brkstring(dp
= mh_xstrdup(cp
), " ", "\n"); ap
&& *ap
; ap
++)
128 if ((i
= alias (*ap
)) != AK_OK
)
129 die("aliasing error in %s - %s", *ap
, akerror (i
));
136 * If -user is specified
140 die("usage: %s -user addresses ... (you forgot the addresses)",
143 for (i
= 0; i
< vecp
; i
++)
144 print_usr (vec
[i
], list
);
147 /* print specified aliases */
148 for (i
= 0; i
< vecp
; i
++)
149 print_aka (akvalue (vec
[i
]), list
, 0);
152 for (ak
= akahead
; ak
; ak
= ak
->ak_next
) {
155 printf ("%s: ", ak
->ak_name
);
156 pos
+= strlen (ak
->ak_name
) + 1;
158 print_aka(res
, list
, pos
);
170 print_aka (char *p
, bool list
, int margin
)
184 printf ("\n%*s", margin
, "");
187 fputs(",\n ", stdout
);
210 print_usr (char *s
, bool list
)
214 struct mailname
*mp
, *np
;
216 if ((pp
= getname (s
)) == NULL
)
217 die("no address in \"%s\"", s
);
218 if ((mp
= getm (pp
, NULL
, 0, NULL
, 0)) == NULL
)
219 die("bad address \"%s\"", s
);
224 for (ak
= akahead
; ak
; ak
= ak
->ak_next
) {
226 while ((cp
= getname (pp
))) {
227 if ((np
= getm (cp
, NULL
, 0, NULL
, 0)) == NULL
)
229 if (!strcasecmp (FENDNULL(mp
->m_host
),
230 FENDNULL(np
->m_host
)) &&
231 !strcasecmp (FENDNULL(mp
->m_mbox
),
232 FENDNULL(np
->m_mbox
))) {
233 vp
= vp
? add (ak
->ak_name
, add (",", vp
))
234 : getcpy (ak
->ak_name
);
245 print_aka (vp
? vp
: s
, list
, 0);