]>
diplodocus.org Git - nmh/blob - uip/ali.c
8c5cd360d36641789c80a29dcc2c740231d396a1
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/ambigsw.h"
10 #include "sbr/print_version.h"
11 #include "sbr/print_help.h"
12 #include "sbr/error.h"
13 #include "h/addrsbr.h"
14 #include "h/aliasbr.h"
19 #define ALI_SWITCHES \
20 X("alias aliasfile", 0, ALIASW) \
21 X("noalias", 0, NALIASW) \
22 X("list", 0, LISTSW) \
23 X("nolist", 0, NLISTSW) \
24 X("user", 0, USERSW) \
25 X("nouser", 0, NUSERSW) \
26 X("version", 0, VERSIONSW) \
27 X("help", 0, HELPSW) \
29 #define X(sw, minchars, id) id,
30 DEFINE_SWITCH_ENUM(ALI
);
33 #define X(sw, minchars, id) { sw, minchars, id },
34 DEFINE_SWITCH_ARRAY(ALI
, switches
);
39 extern struct aka
*akahead
;
44 static void print_aka (char *, bool, int);
45 static void print_usr (char *, bool);
49 main (int argc
, char **argv
)
52 bool inverted
, list
, noalias
;
53 char *cp
, **ap
, **argp
, buf
[BUFSIZ
];
54 /* Really only need to allocate for argc-1, but must allocate at least 1,
55 so go ahead and allocate for argc char pointers. */
56 char **vec
= mh_xmalloc (argc
* sizeof (char *)), **arguments
;
59 if (nmh_init(argv
[0], true, true)) { return 1; }
62 arguments
= getarguments (invo_name
, argc
, argv
, 1);
65 inverted
= list
= noalias
= false;
66 while ((cp
= *argp
++)) {
68 switch (smatch (++cp
, switches
)) {
70 ambigsw (cp
, switches
);
73 die("-%s unknown", cp
);
76 snprintf (buf
, sizeof(buf
), "%s [switches] aliases ...",
78 print_help (buf
, switches
, 1);
81 print_version (invo_name
);
85 if (!(cp
= *argp
++) || *cp
== '-')
86 die("missing argument to %s", argp
[-2]);
87 if ((i
= alias (cp
)) != AK_OK
)
88 die("aliasing error in %s - %s", cp
, akerror (i
));
113 /* Should never happen, but try to protect against code changes
114 that could allow it. */
115 die("too many arguments");
120 /* allow Aliasfile: profile entry */
121 if ((cp
= context_find ("Aliasfile"))) {
124 for (ap
= brkstring(dp
= mh_xstrdup(cp
), " ", "\n"); ap
&& *ap
; ap
++)
125 if ((i
= alias (*ap
)) != AK_OK
)
126 die("aliasing error in %s - %s", *ap
, akerror (i
));
133 * If -user is specified
137 die("usage: %s -user addresses ... (you forgot the addresses)",
140 for (i
= 0; i
< vecp
; i
++)
141 print_usr (vec
[i
], list
);
144 /* print specified aliases */
145 for (i
= 0; i
< vecp
; i
++)
146 print_aka (akvalue (vec
[i
]), list
, 0);
149 for (ak
= akahead
; ak
; ak
= ak
->ak_next
) {
152 printf ("%s: ", ak
->ak_name
);
153 pos
+= strlen (ak
->ak_name
) + 1;
155 print_aka(res
, list
, pos
);
167 print_aka (char *p
, bool list
, int margin
)
181 printf ("\n%*s", margin
, "");
184 fputs(",\n ", stdout
);
207 print_usr (char *s
, bool list
)
211 struct mailname
*mp
, *np
;
213 if ((pp
= getname (s
)) == NULL
)
214 die("no address in \"%s\"", s
);
215 if ((mp
= getm (pp
, NULL
, 0, NULL
, 0)) == NULL
)
216 die("bad address \"%s\"", s
);
221 for (ak
= akahead
; ak
; ak
= ak
->ak_next
) {
223 while ((cp
= getname (pp
))) {
224 if ((np
= getm (cp
, NULL
, 0, NULL
, 0)) == NULL
)
226 if (!strcasecmp (FENDNULL(mp
->m_host
),
227 FENDNULL(np
->m_host
)) &&
228 !strcasecmp (FENDNULL(mp
->m_mbox
),
229 FENDNULL(np
->m_mbox
))) {
230 vp
= vp
? add (ak
->ak_name
, add (",", vp
))
231 : getcpy (ak
->ak_name
);
242 print_aka (vp
? vp
: s
, list
, 0);