]>
diplodocus.org Git - nmh/blob - uip/ali.c
6a9078bd29466f255544fdd76f11fb1c09405d57
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.
10 #include <h/addrsbr.h>
11 #include <h/aliasbr.h>
16 #define ALI_SWITCHES \
17 X("alias aliasfile", 0, ALIASW) \
18 X("noalias", 0, NALIASW) \
19 X("list", 0, LISTSW) \
20 X("nolist", 0, NLISTSW) \
21 X("user", 0, USERSW) \
22 X("nouser", 0, NUSERSW) \
23 X("version", 0, VERSIONSW) \
24 X("help", 0, HELPSW) \
26 #define X(sw, minchars, id) id,
27 DEFINE_SWITCH_ENUM(ALI
);
30 #define X(sw, minchars, id) { sw, minchars, id },
31 DEFINE_SWITCH_ARRAY(ALI
, switches
);
36 extern struct aka
*akahead
;
41 static void print_aka (char *, bool, int);
42 static void print_usr (char *, bool);
46 main (int argc
, char **argv
)
49 bool inverted
, list
, noalias
;
50 char *cp
, **ap
, **argp
, buf
[BUFSIZ
];
51 /* Really only need to allocate for argc-1, but must allocate at least 1,
52 so go ahead and allocate for argc char pointers. */
53 char **vec
= mh_xmalloc (argc
* sizeof (char *)), **arguments
;
56 if (nmh_init(argv
[0], true, true)) { return 1; }
59 arguments
= getarguments (invo_name
, argc
, argv
, 1);
62 inverted
= list
= noalias
= false;
63 while ((cp
= *argp
++)) {
65 switch (smatch (++cp
, switches
)) {
67 ambigsw (cp
, switches
);
70 die("-%s unknown", cp
);
73 snprintf (buf
, sizeof(buf
), "%s [switches] aliases ...",
75 print_help (buf
, switches
, 1);
78 print_version (invo_name
);
82 if (!(cp
= *argp
++) || *cp
== '-')
83 die("missing argument to %s", argp
[-2]);
84 if ((i
= alias (cp
)) != AK_OK
)
85 die("aliasing error in %s - %s", cp
, akerror (i
));
110 /* Should never happen, but try to protect against code changes
111 that could allow it. */
112 die("too many arguments");
117 /* allow Aliasfile: profile entry */
118 if ((cp
= context_find ("Aliasfile"))) {
121 for (ap
= brkstring(dp
= mh_xstrdup(cp
), " ", "\n"); ap
&& *ap
; ap
++)
122 if ((i
= alias (*ap
)) != AK_OK
)
123 die("aliasing error in %s - %s", *ap
, akerror (i
));
130 * If -user is specified
134 die("usage: %s -user addresses ... (you forgot the addresses)",
137 for (i
= 0; i
< vecp
; i
++)
138 print_usr (vec
[i
], list
);
141 /* print specified aliases */
142 for (i
= 0; i
< vecp
; i
++)
143 print_aka (akvalue (vec
[i
]), list
, 0);
146 for (ak
= akahead
; ak
; ak
= ak
->ak_next
) {
149 printf ("%s: ", ak
->ak_name
);
150 pos
+= strlen (ak
->ak_name
) + 1;
152 print_aka(res
, list
, pos
);
164 print_aka (char *p
, bool list
, int margin
)
178 printf ("\n%*s", margin
, "");
181 fputs(",\n ", stdout
);
204 print_usr (char *s
, bool list
)
208 struct mailname
*mp
, *np
;
210 if ((pp
= getname (s
)) == NULL
)
211 die("no address in \"%s\"", s
);
212 if ((mp
= getm (pp
, NULL
, 0, NULL
, 0)) == NULL
)
213 die("bad address \"%s\"", s
);
218 for (ak
= akahead
; ak
; ak
= ak
->ak_next
) {
220 while ((cp
= getname (pp
))) {
221 if ((np
= getm (cp
, NULL
, 0, NULL
, 0)) == NULL
)
223 if (!strcasecmp (FENDNULL(mp
->m_host
),
224 FENDNULL(np
->m_host
)) &&
225 !strcasecmp (FENDNULL(mp
->m_mbox
),
226 FENDNULL(np
->m_mbox
))) {
227 vp
= vp
? add (ak
->ak_name
, add (",", vp
))
228 : getcpy (ak
->ak_name
);
239 print_aka (vp
? vp
: s
, list
, 0);