]>
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/smatch.h"
10 #include "sbr/getcpy.h"
11 #include "sbr/context_find.h"
12 #include "sbr/brkstring.h"
13 #include "sbr/ambigsw.h"
14 #include "sbr/print_version.h"
15 #include "sbr/print_help.h"
16 #include "sbr/error.h"
17 #include "h/addrsbr.h"
18 #include "h/aliasbr.h"
23 #define ALI_SWITCHES \
24 X("alias aliasfile", 0, ALIASW) \
25 X("noalias", 0, NALIASW) \
26 X("list", 0, LISTSW) \
27 X("nolist", 0, NLISTSW) \
28 X("user", 0, USERSW) \
29 X("nouser", 0, NUSERSW) \
30 X("version", 0, VERSIONSW) \
31 X("help", 0, HELPSW) \
33 #define X(sw, minchars, id) id,
34 DEFINE_SWITCH_ENUM(ALI
);
37 #define X(sw, minchars, id) { sw, minchars, id },
38 DEFINE_SWITCH_ARRAY(ALI
, switches
);
43 extern struct aka
*akahead
;
48 static void print_aka (char *, bool, int);
49 static void print_usr (char *, bool);
53 main (int argc
, char **argv
)
56 bool inverted
, list
, noalias
;
57 char *cp
, **ap
, **argp
, buf
[BUFSIZ
];
58 /* Really only need to allocate for argc-1, but must allocate at least 1,
59 so go ahead and allocate for argc char pointers. */
60 char **vec
= mh_xmalloc (argc
* sizeof (char *)), **arguments
;
63 if (nmh_init(argv
[0], true, true)) { return 1; }
66 arguments
= getarguments (invo_name
, argc
, argv
, 1);
69 inverted
= list
= noalias
= false;
70 while ((cp
= *argp
++)) {
72 switch (smatch (++cp
, switches
)) {
74 ambigsw (cp
, switches
);
77 die("-%s unknown", cp
);
80 snprintf (buf
, sizeof(buf
), "%s [switches] aliases ...",
82 print_help (buf
, switches
, 1);
85 print_version (invo_name
);
89 if (!(cp
= *argp
++) || *cp
== '-')
90 die("missing argument to %s", argp
[-2]);
91 if ((i
= alias (cp
)) != AK_OK
)
92 die("aliasing error in %s - %s", cp
, akerror (i
));
117 /* Should never happen, but try to protect against code changes
118 that could allow it. */
119 die("too many arguments");
124 /* allow Aliasfile: profile entry */
125 if ((cp
= context_find ("Aliasfile"))) {
128 for (ap
= brkstring(dp
= mh_xstrdup(cp
), " ", "\n"); ap
&& *ap
; ap
++)
129 if ((i
= alias (*ap
)) != AK_OK
)
130 die("aliasing error in %s - %s", *ap
, akerror (i
));
137 * If -user is specified
141 die("usage: %s -user addresses ... (you forgot the addresses)",
144 for (i
= 0; i
< vecp
; i
++)
145 print_usr (vec
[i
], list
);
148 /* print specified aliases */
149 for (i
= 0; i
< vecp
; i
++)
150 print_aka (akvalue (vec
[i
]), list
, 0);
153 for (ak
= akahead
; ak
; ak
= ak
->ak_next
) {
156 printf ("%s: ", ak
->ak_name
);
157 pos
+= strlen (ak
->ak_name
) + 1;
159 print_aka(res
, list
, pos
);
171 print_aka (char *p
, bool list
, int margin
)
185 printf ("\n%*s", margin
, "");
188 fputs(",\n ", stdout
);
211 print_usr (char *s
, bool list
)
215 struct mailname
*mp
, *np
;
217 if ((pp
= getname (s
)) == NULL
)
218 die("no address in \"%s\"", s
);
219 if ((mp
= getm (pp
, NULL
, 0, NULL
, 0)) == NULL
)
220 die("bad address \"%s\"", s
);
225 for (ak
= akahead
; ak
; ak
= ak
->ak_next
) {
227 while ((cp
= getname (pp
))) {
228 if ((np
= getm (cp
, NULL
, 0, NULL
, 0)) == NULL
)
230 if (!strcasecmp (FENDNULL(mp
->m_host
),
231 FENDNULL(np
->m_host
)) &&
232 !strcasecmp (FENDNULL(mp
->m_mbox
),
233 FENDNULL(np
->m_mbox
))) {
234 vp
= vp
? add (ak
->ak_name
, add (",", vp
))
235 : getcpy (ak
->ak_name
);
246 print_aka (vp
? vp
: s
, list
, 0);