]>
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.
10 #include <h/aliasbr.h>
15 #define ALI_SWITCHES \
16 X("alias aliasfile", 0, ALIASW) \
17 X("noalias", 0, NALIASW) \
18 X("list", 0, LISTSW) \
19 X("nolist", 0, NLISTSW) \
20 X("user", 0, USERSW) \
21 X("nouser", 0, NUSERSW) \
22 X("version", 0, VERSIONSW) \
23 X("help", 0, HELPSW) \
25 #define X(sw, minchars, id) id,
26 DEFINE_SWITCH_ENUM(ALI
);
29 #define X(sw, minchars, id) { sw, minchars, id },
30 DEFINE_SWITCH_ARRAY(ALI
, switches
);
35 extern struct aka
*akahead
;
40 static void print_aka (char *, bool, int);
41 static void print_usr (char *, bool);
45 main (int argc
, char **argv
)
48 bool inverted
, list
, noalias
;
49 char *cp
, **ap
, **argp
, buf
[BUFSIZ
];
50 /* Really only need to allocate for argc-1, but must allocate at least 1,
51 so go ahead and allocate for argc char pointers. */
52 char **vec
= mh_xmalloc (argc
* sizeof (char *)), **arguments
;
55 if (nmh_init(argv
[0], true, true)) { return 1; }
58 arguments
= getarguments (invo_name
, argc
, argv
, 1);
61 inverted
= list
= noalias
= false;
62 while ((cp
= *argp
++)) {
64 switch (smatch (++cp
, switches
)) {
66 ambigsw (cp
, switches
);
69 die("-%s unknown", cp
);
72 snprintf (buf
, sizeof(buf
), "%s [switches] aliases ...",
74 print_help (buf
, switches
, 1);
77 print_version (invo_name
);
81 if (!(cp
= *argp
++) || *cp
== '-')
82 die("missing argument to %s", argp
[-2]);
83 if ((i
= alias (cp
)) != AK_OK
)
84 die("aliasing error in %s - %s", cp
, akerror (i
));
109 /* Should never happen, but try to protect against code changes
110 that could allow it. */
111 die("too many arguments");
116 /* allow Aliasfile: profile entry */
117 if ((cp
= context_find ("Aliasfile"))) {
120 for (ap
= brkstring(dp
= mh_xstrdup(cp
), " ", "\n"); ap
&& *ap
; ap
++)
121 if ((i
= alias (*ap
)) != AK_OK
)
122 die("aliasing error in %s - %s", *ap
, akerror (i
));
129 * If -user is specified
133 die("usage: %s -user addresses ... (you forgot the addresses)",
136 for (i
= 0; i
< vecp
; i
++)
137 print_usr (vec
[i
], list
);
140 /* print specified aliases */
141 for (i
= 0; i
< vecp
; i
++)
142 print_aka (akvalue (vec
[i
]), list
, 0);
145 for (ak
= akahead
; ak
; ak
= ak
->ak_next
) {
148 printf ("%s: ", ak
->ak_name
);
149 pos
+= strlen (ak
->ak_name
) + 1;
151 print_aka(res
, list
, pos
);
163 print_aka (char *p
, bool list
, int margin
)
177 printf ("\n%*s", margin
, "");
180 fputs(",\n ", stdout
);
203 print_usr (char *s
, bool list
)
207 struct mailname
*mp
, *np
;
209 if ((pp
= getname (s
)) == NULL
)
210 die("no address in \"%s\"", s
);
211 if ((mp
= getm (pp
, NULL
, 0, NULL
, 0)) == NULL
)
212 die("bad address \"%s\"", s
);
217 for (ak
= akahead
; ak
; ak
= ak
->ak_next
) {
219 while ((cp
= getname (pp
))) {
220 if ((np
= getm (cp
, NULL
, 0, NULL
, 0)) == NULL
)
222 if (!strcasecmp (FENDNULL(mp
->m_host
),
223 FENDNULL(np
->m_host
)) &&
224 !strcasecmp (FENDNULL(mp
->m_mbox
),
225 FENDNULL(np
->m_mbox
))) {
226 vp
= vp
? add (ak
->ak_name
, add (",", vp
))
227 : getcpy (ak
->ak_name
);
238 print_aka (vp
? vp
: s
, list
, 0);