]>
diplodocus.org Git - nmh/blob - uip/ali.c
b4fe3d85abd580db794a90b3a833923506d50371
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/brkstring.h"
10 #include "sbr/ambigsw.h"
11 #include "sbr/print_version.h"
12 #include "sbr/print_help.h"
13 #include "sbr/error.h"
14 #include "h/addrsbr.h"
15 #include "h/aliasbr.h"
20 #define ALI_SWITCHES \
21 X("alias aliasfile", 0, ALIASW) \
22 X("noalias", 0, NALIASW) \
23 X("list", 0, LISTSW) \
24 X("nolist", 0, NLISTSW) \
25 X("user", 0, USERSW) \
26 X("nouser", 0, NUSERSW) \
27 X("version", 0, VERSIONSW) \
28 X("help", 0, HELPSW) \
30 #define X(sw, minchars, id) id,
31 DEFINE_SWITCH_ENUM(ALI
);
34 #define X(sw, minchars, id) { sw, minchars, id },
35 DEFINE_SWITCH_ARRAY(ALI
, switches
);
40 extern struct aka
*akahead
;
45 static void print_aka (char *, bool, int);
46 static void print_usr (char *, bool);
50 main (int argc
, char **argv
)
53 bool inverted
, list
, noalias
;
54 char *cp
, **ap
, **argp
, buf
[BUFSIZ
];
55 /* Really only need to allocate for argc-1, but must allocate at least 1,
56 so go ahead and allocate for argc char pointers. */
57 char **vec
= mh_xmalloc (argc
* sizeof (char *)), **arguments
;
60 if (nmh_init(argv
[0], true, true)) { return 1; }
63 arguments
= getarguments (invo_name
, argc
, argv
, 1);
66 inverted
= list
= noalias
= false;
67 while ((cp
= *argp
++)) {
69 switch (smatch (++cp
, switches
)) {
71 ambigsw (cp
, switches
);
74 die("-%s unknown", cp
);
77 snprintf (buf
, sizeof(buf
), "%s [switches] aliases ...",
79 print_help (buf
, switches
, 1);
82 print_version (invo_name
);
86 if (!(cp
= *argp
++) || *cp
== '-')
87 die("missing argument to %s", argp
[-2]);
88 if ((i
= alias (cp
)) != AK_OK
)
89 die("aliasing error in %s - %s", cp
, akerror (i
));
114 /* Should never happen, but try to protect against code changes
115 that could allow it. */
116 die("too many arguments");
121 /* allow Aliasfile: profile entry */
122 if ((cp
= context_find ("Aliasfile"))) {
125 for (ap
= brkstring(dp
= mh_xstrdup(cp
), " ", "\n"); ap
&& *ap
; ap
++)
126 if ((i
= alias (*ap
)) != AK_OK
)
127 die("aliasing error in %s - %s", *ap
, akerror (i
));
134 * If -user is specified
138 die("usage: %s -user addresses ... (you forgot the addresses)",
141 for (i
= 0; i
< vecp
; i
++)
142 print_usr (vec
[i
], list
);
145 /* print specified aliases */
146 for (i
= 0; i
< vecp
; i
++)
147 print_aka (akvalue (vec
[i
]), list
, 0);
150 for (ak
= akahead
; ak
; ak
= ak
->ak_next
) {
153 printf ("%s: ", ak
->ak_name
);
154 pos
+= strlen (ak
->ak_name
) + 1;
156 print_aka(res
, list
, pos
);
168 print_aka (char *p
, bool list
, int margin
)
182 printf ("\n%*s", margin
, "");
185 fputs(",\n ", stdout
);
208 print_usr (char *s
, bool list
)
212 struct mailname
*mp
, *np
;
214 if ((pp
= getname (s
)) == NULL
)
215 die("no address in \"%s\"", s
);
216 if ((mp
= getm (pp
, NULL
, 0, NULL
, 0)) == NULL
)
217 die("bad address \"%s\"", s
);
222 for (ak
= akahead
; ak
; ak
= ak
->ak_next
) {
224 while ((cp
= getname (pp
))) {
225 if ((np
= getm (cp
, NULL
, 0, NULL
, 0)) == NULL
)
227 if (!strcasecmp (FENDNULL(mp
->m_host
),
228 FENDNULL(np
->m_host
)) &&
229 !strcasecmp (FENDNULL(mp
->m_mbox
),
230 FENDNULL(np
->m_mbox
))) {
231 vp
= vp
? add (ak
->ak_name
, add (",", vp
))
232 : getcpy (ak
->ak_name
);
243 print_aka (vp
? vp
: s
, list
, 0);