]>
diplodocus.org Git - nmh/blob - uip/ali.c
3 * ali.c -- list nmh mail aliases
5 * This code is Copyright (c) 2002, by the authors of nmh. See the
6 * COPYRIGHT file in the root directory of the nmh distribution for
7 * complete copyright information.
11 #include <h/addrsbr.h>
12 #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 *, int, int);
42 static void print_usr (char *, int);
46 main (int argc
, char **argv
)
48 int i
, vecp
= 0, inverted
= 0, list
= 0;
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], 1)) { return 1; }
59 arguments
= getarguments (invo_name
, argc
, argv
, 1);
62 while ((cp
= *argp
++)) {
64 switch (smatch (++cp
, switches
)) {
66 ambigsw (cp
, switches
);
69 adios (NULL
, "-%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 adios (NULL
, "missing argument to %s", argp
[-2]);
83 if ((i
= alias (cp
)) != AK_OK
)
84 adios (NULL
, "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 adios (NULL
, "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 adios (NULL
, "aliasing error in %s - %s", *ap
, akerror (i
));
129 * If -user is specified
133 adios (NULL
, "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
) {
146 printf ("%s: ", ak
->ak_name
);
147 pos
+= strlen (ak
->ak_name
) + 1;
148 print_aka (akresult (ak
), list
, pos
);
159 print_aka (char *p
, int list
, int margin
)
164 printf ("<empty>\n");
173 printf ("\n%*s", margin
, "");
199 print_usr (char *s
, int list
)
203 struct mailname
*mp
, *np
;
205 if ((pp
= getname (s
)) == NULL
)
206 adios (NULL
, "no address in \"%s\"", s
);
207 if ((mp
= getm (pp
, NULL
, 0, NULL
, 0)) == NULL
)
208 adios (NULL
, "bad address \"%s\"", s
);
213 for (ak
= akahead
; ak
; ak
= ak
->ak_next
) {
215 while ((cp
= getname (pp
))) {
216 if ((np
= getm (cp
, NULL
, 0, NULL
, 0)) == NULL
)
218 if (!strcasecmp (mp
->m_host
? mp
->m_host
: "",
219 np
->m_host
? np
->m_host
: "") &&
220 !strcasecmp (mp
->m_mbox
? mp
->m_mbox
: "",
221 np
->m_mbox
? np
->m_mbox
: "")) {
222 vp
= vp
? add (ak
->ak_name
, add (",", vp
))
223 : getcpy (ak
->ak_name
);
234 print_aka (vp
? vp
: s
, list
, 0);