]>
diplodocus.org Git - nmh/blob - uip/ali.c
3662ed71afb254c2feab3513f5580d59e18a6ed5
2 * ali.c -- list nmh mail aliases
4 * This code is Copyright (c) 2002, by the authors of nmh. See the
5 * COPYRIGHT file in the root directory of the nmh distribution for
6 * complete copyright information.
10 #include <h/addrsbr.h>
11 #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 *, int, int);
41 static void print_usr (char *, int);
45 main (int argc
, char **argv
)
47 int i
, vecp
= 0, inverted
= 0, list
= 0;
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], 1)) { return 1; }
58 arguments
= getarguments (invo_name
, argc
, argv
, 1);
61 while ((cp
= *argp
++)) {
63 switch (smatch (++cp
, switches
)) {
65 ambigsw (cp
, switches
);
68 adios (NULL
, "-%s unknown", cp
);
71 snprintf (buf
, sizeof(buf
), "%s [switches] aliases ...",
73 print_help (buf
, switches
, 1);
76 print_version (invo_name
);
80 if (!(cp
= *argp
++) || *cp
== '-')
81 adios (NULL
, "missing argument to %s", argp
[-2]);
82 if ((i
= alias (cp
)) != AK_OK
)
83 adios (NULL
, "aliasing error in %s - %s", cp
, akerror (i
));
108 /* Should never happen, but try to protect against code changes
109 that could allow it. */
110 adios (NULL
, "too many arguments");
115 /* allow Aliasfile: profile entry */
116 if ((cp
= context_find ("Aliasfile"))) {
119 for (ap
= brkstring(dp
= mh_xstrdup(cp
), " ", "\n"); ap
&& *ap
; ap
++)
120 if ((i
= alias (*ap
)) != AK_OK
)
121 adios (NULL
, "aliasing error in %s - %s", *ap
, akerror (i
));
128 * If -user is specified
132 adios (NULL
, "usage: %s -user addresses ... (you forgot the addresses)",
135 for (i
= 0; i
< vecp
; i
++)
136 print_usr (vec
[i
], list
);
139 /* print specified aliases */
140 for (i
= 0; i
< vecp
; i
++)
141 print_aka (akvalue (vec
[i
]), list
, 0);
144 for (ak
= akahead
; ak
; ak
= ak
->ak_next
) {
147 printf ("%s: ", ak
->ak_name
);
148 pos
+= strlen (ak
->ak_name
) + 1;
150 print_aka(res
, list
, pos
);
162 print_aka (char *p
, int list
, int margin
)
176 printf ("\n%*s", margin
, "");
202 print_usr (char *s
, int list
)
206 struct mailname
*mp
, *np
;
208 if ((pp
= getname (s
)) == NULL
)
209 adios (NULL
, "no address in \"%s\"", s
);
210 if ((mp
= getm (pp
, NULL
, 0, NULL
, 0)) == NULL
)
211 adios (NULL
, "bad address \"%s\"", s
);
216 for (ak
= akahead
; ak
; ak
= ak
->ak_next
) {
218 while ((cp
= getname (pp
))) {
219 if ((np
= getm (cp
, NULL
, 0, NULL
, 0)) == NULL
)
221 if (!strcasecmp (mp
->m_host
? mp
->m_host
: "",
222 np
->m_host
? np
->m_host
: "") &&
223 !strcasecmp (mp
->m_mbox
? mp
->m_mbox
: "",
224 np
->m_mbox
? np
->m_mbox
: "")) {
225 vp
= vp
? add (ak
->ak_name
, add (",", vp
))
226 : getcpy (ak
->ak_name
);
237 print_aka (vp
? vp
: s
, list
, 0);