]>
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", -7, NALIASW) \
19 X("list", 0, LISTSW) \
20 X("nolist", 0, NLISTSW) \
21 X("normalize", 0, NORMSW) \
22 X("nonormalize", 0, NNORMSW) \
23 X("user", 0, USERSW) \
24 X("nouser", 0, NUSERSW) \
25 X("version", 0, VERSIONSW) \
26 X("help", 0, HELPSW) \
28 #define X(sw, minchars, id) id,
29 DEFINE_SWITCH_ENUM(ALI
);
32 #define X(sw, minchars, id) { sw, minchars, id },
33 DEFINE_SWITCH_ARRAY(ALI
, switches
);
38 extern struct aka
*akahead
;
43 static void print_aka (char *, int, int);
44 static void print_usr (char *, int, int);
48 main (int argc
, char **argv
)
50 int i
, vecp
= 0, inverted
= 0, list
= 0;
51 int noalias
= 0, normalize
= AD_NHST
;
52 char *cp
, **ap
, **argp
, buf
[BUFSIZ
];
53 /* Really only need to allocate for argc-1, but must allocate at least 1,
54 so go ahead and allocate for argc char pointers. */
55 char **vec
= mh_xmalloc (argc
* sizeof (char *)), **arguments
;
59 setlocale(LC_ALL
, "");
61 invo_name
= r1bindex (argv
[0], '/');
63 /* read user profile/context */
67 arguments
= getarguments (invo_name
, argc
, argv
, 1);
70 while ((cp
= *argp
++)) {
72 switch (smatch (++cp
, switches
)) {
74 ambigsw (cp
, switches
);
77 adios (NULL
, "-%s unknown", cp
);
80 snprintf (buf
, sizeof(buf
), "%s [switches] aliases ...",
82 print_help (buf
, switches
, 1);
85 print_version (invo_name
);
89 if (!(cp
= *argp
++) || *cp
== '-')
90 adios (NULL
, "missing argument to %s", argp
[-2]);
91 if ((i
= alias (cp
)) != AK_OK
)
92 adios (NULL
, "aliasing error in %s - %s", cp
, akerror (i
));
124 /* Should never happen, but try to protect against code changes
125 that could allow it. */
126 adios (NULL
, "too many arguments");
131 /* allow Aliasfile: profile entry */
132 if ((cp
= context_find ("Aliasfile"))) {
135 for (ap
= brkstring(dp
= getcpy(cp
), " ", "\n"); ap
&& *ap
; ap
++)
136 if ((i
= alias (*ap
)) != AK_OK
)
137 adios (NULL
, "aliasing error in %s - %s", *ap
, akerror (i
));
145 * If -user is specified
149 adios (NULL
, "usage: %s -user addresses ... (you forgot the addresses)",
152 for (i
= 0; i
< vecp
; i
++)
153 print_usr (vec
[i
], list
, normalize
);
156 /* print specified aliases */
157 for (i
= 0; i
< vecp
; i
++)
158 print_aka (akvalue (vec
[i
]), list
, 0);
161 for (ak
= akahead
; ak
; ak
= ak
->ak_next
) {
162 printf ("%s: ", ak
->ak_name
);
163 pos
+= strlen (ak
->ak_name
) + 1;
164 print_aka (akresult (ak
), list
, pos
);
175 print_aka (char *p
, int list
, int margin
)
180 printf ("<empty>\n");
189 printf ("\n%*s", margin
, "");
215 print_usr (char *s
, int list
, int norm
)
217 register char *cp
, *pp
, *vp
;
218 register struct aka
*ak
;
219 register struct mailname
*mp
, *np
;
221 if ((pp
= getname (s
)) == NULL
)
222 adios (NULL
, "no address in \"%s\"", s
);
223 if ((mp
= getm (pp
, NULL
, 0, norm
, NULL
)) == NULL
)
224 adios (NULL
, "bad address \"%s\"", s
);
229 for (ak
= akahead
; ak
; ak
= ak
->ak_next
) {
231 while ((cp
= getname (pp
))) {
232 if ((np
= getm (cp
, NULL
, 0, norm
, NULL
)) == NULL
)
234 if (!mh_strcasecmp (mp
->m_host
, np
->m_host
)
235 && !mh_strcasecmp (mp
->m_mbox
, np
->m_mbox
)) {
236 vp
= vp
? add (ak
->ak_name
, add (",", vp
))
237 : getcpy (ak
->ak_name
);
248 print_aka (vp
? vp
: s
, list
, 0);