]>
diplodocus.org Git - nmh/blob - uip/ali.c
6e0f39c18c003ad71ca44092d33f40d6b7d2fab9
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_help.h"
10 #include "sbr/error.h"
11 #include "h/addrsbr.h"
12 #include "h/aliasbr.h"
17 #define ALI_SWITCHES \
18 X("alias aliasfile", 0, ALIASW) \
19 X("noalias", 0, NALIASW) \
20 X("list", 0, LISTSW) \
21 X("nolist", 0, NLISTSW) \
22 X("user", 0, USERSW) \
23 X("nouser", 0, NUSERSW) \
24 X("version", 0, VERSIONSW) \
25 X("help", 0, HELPSW) \
27 #define X(sw, minchars, id) id,
28 DEFINE_SWITCH_ENUM(ALI
);
31 #define X(sw, minchars, id) { sw, minchars, id },
32 DEFINE_SWITCH_ARRAY(ALI
, switches
);
37 extern struct aka
*akahead
;
42 static void print_aka (char *, bool, int);
43 static void print_usr (char *, bool);
47 main (int argc
, char **argv
)
50 bool inverted
, list
, noalias
;
51 char *cp
, **ap
, **argp
, buf
[BUFSIZ
];
52 /* Really only need to allocate for argc-1, but must allocate at least 1,
53 so go ahead and allocate for argc char pointers. */
54 char **vec
= mh_xmalloc (argc
* sizeof (char *)), **arguments
;
57 if (nmh_init(argv
[0], true, true)) { return 1; }
60 arguments
= getarguments (invo_name
, argc
, argv
, 1);
63 inverted
= list
= noalias
= false;
64 while ((cp
= *argp
++)) {
66 switch (smatch (++cp
, switches
)) {
68 ambigsw (cp
, switches
);
71 die("-%s unknown", cp
);
74 snprintf (buf
, sizeof(buf
), "%s [switches] aliases ...",
76 print_help (buf
, switches
, 1);
79 print_version (invo_name
);
83 if (!(cp
= *argp
++) || *cp
== '-')
84 die("missing argument to %s", argp
[-2]);
85 if ((i
= alias (cp
)) != AK_OK
)
86 die("aliasing error in %s - %s", cp
, akerror (i
));
111 /* Should never happen, but try to protect against code changes
112 that could allow it. */
113 die("too many arguments");
118 /* allow Aliasfile: profile entry */
119 if ((cp
= context_find ("Aliasfile"))) {
122 for (ap
= brkstring(dp
= mh_xstrdup(cp
), " ", "\n"); ap
&& *ap
; ap
++)
123 if ((i
= alias (*ap
)) != AK_OK
)
124 die("aliasing error in %s - %s", *ap
, akerror (i
));
131 * If -user is specified
135 die("usage: %s -user addresses ... (you forgot the addresses)",
138 for (i
= 0; i
< vecp
; i
++)
139 print_usr (vec
[i
], list
);
142 /* print specified aliases */
143 for (i
= 0; i
< vecp
; i
++)
144 print_aka (akvalue (vec
[i
]), list
, 0);
147 for (ak
= akahead
; ak
; ak
= ak
->ak_next
) {
150 printf ("%s: ", ak
->ak_name
);
151 pos
+= strlen (ak
->ak_name
) + 1;
153 print_aka(res
, list
, pos
);
165 print_aka (char *p
, bool list
, int margin
)
179 printf ("\n%*s", margin
, "");
182 fputs(",\n ", stdout
);
205 print_usr (char *s
, bool list
)
209 struct mailname
*mp
, *np
;
211 if ((pp
= getname (s
)) == NULL
)
212 die("no address in \"%s\"", s
);
213 if ((mp
= getm (pp
, NULL
, 0, NULL
, 0)) == NULL
)
214 die("bad address \"%s\"", s
);
219 for (ak
= akahead
; ak
; ak
= ak
->ak_next
) {
221 while ((cp
= getname (pp
))) {
222 if ((np
= getm (cp
, NULL
, 0, NULL
, 0)) == NULL
)
224 if (!strcasecmp (FENDNULL(mp
->m_host
),
225 FENDNULL(np
->m_host
)) &&
226 !strcasecmp (FENDNULL(mp
->m_mbox
),
227 FENDNULL(np
->m_mbox
))) {
228 vp
= vp
? add (ak
->ak_name
, add (",", vp
))
229 : getcpy (ak
->ak_name
);
240 print_aka (vp
? vp
: s
, list
, 0);