]>
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/context_find.h"
10 #include "sbr/brkstring.h"
11 #include "sbr/ambigsw.h"
12 #include "sbr/print_version.h"
13 #include "sbr/print_help.h"
14 #include "sbr/error.h"
15 #include "h/addrsbr.h"
16 #include "h/aliasbr.h"
21 #define ALI_SWITCHES \
22 X("alias aliasfile", 0, ALIASW) \
23 X("noalias", 0, NALIASW) \
24 X("list", 0, LISTSW) \
25 X("nolist", 0, NLISTSW) \
26 X("user", 0, USERSW) \
27 X("nouser", 0, NUSERSW) \
28 X("version", 0, VERSIONSW) \
29 X("help", 0, HELPSW) \
31 #define X(sw, minchars, id) id,
32 DEFINE_SWITCH_ENUM(ALI
);
35 #define X(sw, minchars, id) { sw, minchars, id },
36 DEFINE_SWITCH_ARRAY(ALI
, switches
);
41 extern struct aka
*akahead
;
46 static void print_aka (char *, bool, int);
47 static void print_usr (char *, bool);
51 main (int argc
, char **argv
)
54 bool inverted
, list
, noalias
;
55 char *cp
, **ap
, **argp
, buf
[BUFSIZ
];
56 /* Really only need to allocate for argc-1, but must allocate at least 1,
57 so go ahead and allocate for argc char pointers. */
58 char **vec
= mh_xmalloc (argc
* sizeof (char *)), **arguments
;
61 if (nmh_init(argv
[0], true, true)) { return 1; }
64 arguments
= getarguments (invo_name
, argc
, argv
, 1);
67 inverted
= list
= noalias
= false;
68 while ((cp
= *argp
++)) {
70 switch (smatch (++cp
, switches
)) {
72 ambigsw (cp
, switches
);
75 die("-%s unknown", cp
);
78 snprintf (buf
, sizeof(buf
), "%s [switches] aliases ...",
80 print_help (buf
, switches
, 1);
83 print_version (invo_name
);
87 if (!(cp
= *argp
++) || *cp
== '-')
88 die("missing argument to %s", argp
[-2]);
89 if ((i
= alias (cp
)) != AK_OK
)
90 die("aliasing error in %s - %s", cp
, akerror (i
));
115 /* Should never happen, but try to protect against code changes
116 that could allow it. */
117 die("too many arguments");
122 /* allow Aliasfile: profile entry */
123 if ((cp
= context_find ("Aliasfile"))) {
126 for (ap
= brkstring(dp
= mh_xstrdup(cp
), " ", "\n"); ap
&& *ap
; ap
++)
127 if ((i
= alias (*ap
)) != AK_OK
)
128 die("aliasing error in %s - %s", *ap
, akerror (i
));
135 * If -user is specified
139 die("usage: %s -user addresses ... (you forgot the addresses)",
142 for (i
= 0; i
< vecp
; i
++)
143 print_usr (vec
[i
], list
);
146 /* print specified aliases */
147 for (i
= 0; i
< vecp
; i
++)
148 print_aka (akvalue (vec
[i
]), list
, 0);
151 for (ak
= akahead
; ak
; ak
= ak
->ak_next
) {
154 printf ("%s: ", ak
->ak_name
);
155 pos
+= strlen (ak
->ak_name
) + 1;
157 print_aka(res
, list
, pos
);
169 print_aka (char *p
, bool list
, int margin
)
183 printf ("\n%*s", margin
, "");
186 fputs(",\n ", stdout
);
209 print_usr (char *s
, bool list
)
213 struct mailname
*mp
, *np
;
215 if ((pp
= getname (s
)) == NULL
)
216 die("no address in \"%s\"", s
);
217 if ((mp
= getm (pp
, NULL
, 0, NULL
, 0)) == NULL
)
218 die("bad address \"%s\"", s
);
223 for (ak
= akahead
; ak
; ak
= ak
->ak_next
) {
225 while ((cp
= getname (pp
))) {
226 if ((np
= getm (cp
, NULL
, 0, NULL
, 0)) == NULL
)
228 if (!strcasecmp (FENDNULL(mp
->m_host
),
229 FENDNULL(np
->m_host
)) &&
230 !strcasecmp (FENDNULL(mp
->m_mbox
),
231 FENDNULL(np
->m_mbox
))) {
232 vp
= vp
? add (ak
->ak_name
, add (",", vp
))
233 : getcpy (ak
->ak_name
);
244 print_aka (vp
? vp
: s
, list
, 0);