]>
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/print_version.h"
10 #include "sbr/print_help.h"
11 #include "sbr/error.h"
12 #include "h/addrsbr.h"
13 #include "h/aliasbr.h"
18 #define ALI_SWITCHES \
19 X("alias aliasfile", 0, ALIASW) \
20 X("noalias", 0, NALIASW) \
21 X("list", 0, LISTSW) \
22 X("nolist", 0, NLISTSW) \
23 X("user", 0, USERSW) \
24 X("nouser", 0, NUSERSW) \
25 X("version", 0, VERSIONSW) \
26 X("help", 0, HELPSW) \
28 #define X(sw, minchars, id) id,
29 DEFINE_SWITCH_ENUM(ALI
);
32 #define X(sw, minchars, id) { sw, minchars, id },
33 DEFINE_SWITCH_ARRAY(ALI
, switches
);
38 extern struct aka
*akahead
;
43 static void print_aka (char *, bool, int);
44 static void print_usr (char *, bool);
48 main (int argc
, char **argv
)
51 bool inverted
, list
, noalias
;
52 char *cp
, **ap
, **argp
, buf
[BUFSIZ
];
53 /* Really only need to allocate for argc-1, but must allocate at least 1,
54 so go ahead and allocate for argc char pointers. */
55 char **vec
= mh_xmalloc (argc
* sizeof (char *)), **arguments
;
58 if (nmh_init(argv
[0], true, true)) { return 1; }
61 arguments
= getarguments (invo_name
, argc
, argv
, 1);
64 inverted
= list
= noalias
= false;
65 while ((cp
= *argp
++)) {
67 switch (smatch (++cp
, switches
)) {
69 ambigsw (cp
, switches
);
72 die("-%s unknown", cp
);
75 snprintf (buf
, sizeof(buf
), "%s [switches] aliases ...",
77 print_help (buf
, switches
, 1);
80 print_version (invo_name
);
84 if (!(cp
= *argp
++) || *cp
== '-')
85 die("missing argument to %s", argp
[-2]);
86 if ((i
= alias (cp
)) != AK_OK
)
87 die("aliasing error in %s - %s", cp
, akerror (i
));
112 /* Should never happen, but try to protect against code changes
113 that could allow it. */
114 die("too many arguments");
119 /* allow Aliasfile: profile entry */
120 if ((cp
= context_find ("Aliasfile"))) {
123 for (ap
= brkstring(dp
= mh_xstrdup(cp
), " ", "\n"); ap
&& *ap
; ap
++)
124 if ((i
= alias (*ap
)) != AK_OK
)
125 die("aliasing error in %s - %s", *ap
, akerror (i
));
132 * If -user is specified
136 die("usage: %s -user addresses ... (you forgot the addresses)",
139 for (i
= 0; i
< vecp
; i
++)
140 print_usr (vec
[i
], list
);
143 /* print specified aliases */
144 for (i
= 0; i
< vecp
; i
++)
145 print_aka (akvalue (vec
[i
]), list
, 0);
148 for (ak
= akahead
; ak
; ak
= ak
->ak_next
) {
151 printf ("%s: ", ak
->ak_name
);
152 pos
+= strlen (ak
->ak_name
) + 1;
154 print_aka(res
, list
, pos
);
166 print_aka (char *p
, bool list
, int margin
)
180 printf ("\n%*s", margin
, "");
183 fputs(",\n ", stdout
);
206 print_usr (char *s
, bool list
)
210 struct mailname
*mp
, *np
;
212 if ((pp
= getname (s
)) == NULL
)
213 die("no address in \"%s\"", s
);
214 if ((mp
= getm (pp
, NULL
, 0, NULL
, 0)) == NULL
)
215 die("bad address \"%s\"", s
);
220 for (ak
= akahead
; ak
; ak
= ak
->ak_next
) {
222 while ((cp
= getname (pp
))) {
223 if ((np
= getm (cp
, NULL
, 0, NULL
, 0)) == NULL
)
225 if (!strcasecmp (FENDNULL(mp
->m_host
),
226 FENDNULL(np
->m_host
)) &&
227 !strcasecmp (FENDNULL(mp
->m_mbox
),
228 FENDNULL(np
->m_mbox
))) {
229 vp
= vp
? add (ak
->ak_name
, add (",", vp
))
230 : getcpy (ak
->ak_name
);
241 print_aka (vp
? vp
: s
, list
, 0);