]>
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/getarguments.h"
10 #include "sbr/smatch.h"
11 #include "sbr/getcpy.h"
12 #include "sbr/context_find.h"
13 #include "sbr/brkstring.h"
14 #include "sbr/ambigsw.h"
15 #include "sbr/print_version.h"
16 #include "sbr/print_help.h"
17 #include "sbr/error.h"
18 #include "h/addrsbr.h"
19 #include "h/aliasbr.h"
24 #define ALI_SWITCHES \
25 X("alias aliasfile", 0, ALIASW) \
26 X("noalias", 0, NALIASW) \
27 X("list", 0, LISTSW) \
28 X("nolist", 0, NLISTSW) \
29 X("user", 0, USERSW) \
30 X("nouser", 0, NUSERSW) \
31 X("version", 0, VERSIONSW) \
32 X("help", 0, HELPSW) \
34 #define X(sw, minchars, id) id,
35 DEFINE_SWITCH_ENUM(ALI
);
38 #define X(sw, minchars, id) { sw, minchars, id },
39 DEFINE_SWITCH_ARRAY(ALI
, switches
);
44 extern struct aka
*akahead
;
49 static void print_aka (char *, bool, int);
50 static void print_usr (char *, bool);
54 main (int argc
, char **argv
)
57 bool inverted
, list
, noalias
;
58 char *cp
, **ap
, **argp
, buf
[BUFSIZ
];
59 /* Really only need to allocate for argc-1, but must allocate at least 1,
60 so go ahead and allocate for argc char pointers. */
61 char **vec
= mh_xmalloc (argc
* sizeof (char *)), **arguments
;
64 if (nmh_init(argv
[0], true, true)) { return 1; }
67 arguments
= getarguments (invo_name
, argc
, argv
, 1);
70 inverted
= list
= noalias
= false;
71 while ((cp
= *argp
++)) {
73 switch (smatch (++cp
, switches
)) {
75 ambigsw (cp
, switches
);
78 die("-%s unknown", cp
);
81 snprintf (buf
, sizeof(buf
), "%s [switches] aliases ...",
83 print_help (buf
, switches
, 1);
86 print_version (invo_name
);
90 if (!(cp
= *argp
++) || *cp
== '-')
91 die("missing argument to %s", argp
[-2]);
92 if ((i
= alias (cp
)) != AK_OK
)
93 die("aliasing error in %s - %s", cp
, akerror (i
));
118 /* Should never happen, but try to protect against code changes
119 that could allow it. */
120 die("too many arguments");
125 /* allow Aliasfile: profile entry */
126 if ((cp
= context_find ("Aliasfile"))) {
129 for (ap
= brkstring(dp
= mh_xstrdup(cp
), " ", "\n"); ap
&& *ap
; ap
++)
130 if ((i
= alias (*ap
)) != AK_OK
)
131 die("aliasing error in %s - %s", *ap
, akerror (i
));
138 * If -user is specified
142 die("usage: %s -user addresses ... (you forgot the addresses)",
145 for (i
= 0; i
< vecp
; i
++)
146 print_usr (vec
[i
], list
);
149 /* print specified aliases */
150 for (i
= 0; i
< vecp
; i
++)
151 print_aka (akvalue (vec
[i
]), list
, 0);
154 for (ak
= akahead
; ak
; ak
= ak
->ak_next
) {
157 printf ("%s: ", ak
->ak_name
);
158 pos
+= strlen (ak
->ak_name
) + 1;
160 print_aka(res
, list
, pos
);
172 print_aka (char *p
, bool list
, int margin
)
186 printf ("\n%*s", margin
, "");
189 fputs(",\n ", stdout
);
212 print_usr (char *s
, bool list
)
216 struct mailname
*mp
, *np
;
218 if ((pp
= getname (s
)) == NULL
)
219 die("no address in \"%s\"", s
);
220 if ((mp
= getm (pp
, NULL
, 0, NULL
, 0)) == NULL
)
221 die("bad address \"%s\"", s
);
226 for (ak
= akahead
; ak
; ak
= ak
->ak_next
) {
228 while ((cp
= getname (pp
))) {
229 if ((np
= getm (cp
, NULL
, 0, NULL
, 0)) == NULL
)
231 if (!strcasecmp (FENDNULL(mp
->m_host
),
232 FENDNULL(np
->m_host
)) &&
233 !strcasecmp (FENDNULL(mp
->m_mbox
),
234 FENDNULL(np
->m_mbox
))) {
235 vp
= vp
? add (ak
->ak_name
, add (",", vp
))
236 : getcpy (ak
->ak_name
);
247 print_aka (vp
? vp
: s
, list
, 0);