]>
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>
14 #define ALI_SWITCHES \
15 X("alias aliasfile", 0, ALIASW) \
16 X("noalias", 0, NALIASW) \
17 X("list", 0, LISTSW) \
18 X("nolist", 0, NLISTSW) \
19 X("user", 0, USERSW) \
20 X("nouser", 0, NUSERSW) \
21 X("version", 0, VERSIONSW) \
22 X("help", 0, HELPSW) \
24 #define X(sw, minchars, id) id,
25 DEFINE_SWITCH_ENUM(ALI
);
28 #define X(sw, minchars, id) { sw, minchars, id },
29 DEFINE_SWITCH_ARRAY(ALI
, switches
);
34 extern struct aka
*akahead
;
39 static void print_aka (char *, int, int);
40 static void print_usr (char *, int);
44 main (int argc
, char **argv
)
46 int i
, vecp
= 0, inverted
= 0, list
= 0;
48 char *cp
, **ap
, **argp
, buf
[BUFSIZ
];
49 /* Really only need to allocate for argc-1, but must allocate at least 1,
50 so go ahead and allocate for argc char pointers. */
51 char **vec
= mh_xmalloc (argc
* sizeof (char *)), **arguments
;
54 if (nmh_init(argv
[0], 1)) { return 1; }
57 arguments
= getarguments (invo_name
, argc
, argv
, 1);
60 while ((cp
= *argp
++)) {
62 switch (smatch (++cp
, switches
)) {
64 ambigsw (cp
, switches
);
67 adios (NULL
, "-%s unknown", cp
);
70 snprintf (buf
, sizeof(buf
), "%s [switches] aliases ...",
72 print_help (buf
, switches
, 1);
75 print_version (invo_name
);
79 if (!(cp
= *argp
++) || *cp
== '-')
80 adios (NULL
, "missing argument to %s", argp
[-2]);
81 if ((i
= alias (cp
)) != AK_OK
)
82 adios (NULL
, "aliasing error in %s - %s", cp
, akerror (i
));
107 /* Should never happen, but try to protect against code changes
108 that could allow it. */
109 adios (NULL
, "too many arguments");
114 /* allow Aliasfile: profile entry */
115 if ((cp
= context_find ("Aliasfile"))) {
118 for (ap
= brkstring(dp
= mh_xstrdup(cp
), " ", "\n"); ap
&& *ap
; ap
++)
119 if ((i
= alias (*ap
)) != AK_OK
)
120 adios (NULL
, "aliasing error in %s - %s", *ap
, akerror (i
));
127 * If -user is specified
131 adios (NULL
, "usage: %s -user addresses ... (you forgot the addresses)",
134 for (i
= 0; i
< vecp
; i
++)
135 print_usr (vec
[i
], list
);
138 /* print specified aliases */
139 for (i
= 0; i
< vecp
; i
++)
140 print_aka (akvalue (vec
[i
]), list
, 0);
143 for (ak
= akahead
; ak
; ak
= ak
->ak_next
) {
146 printf ("%s: ", ak
->ak_name
);
147 pos
+= strlen (ak
->ak_name
) + 1;
149 print_aka(res
, list
, pos
);
161 print_aka (char *p
, int list
, int margin
)
175 printf ("\n%*s", margin
, "");
201 print_usr (char *s
, int list
)
205 struct mailname
*mp
, *np
;
207 if ((pp
= getname (s
)) == NULL
)
208 adios (NULL
, "no address in \"%s\"", s
);
209 if ((mp
= getm (pp
, NULL
, 0, NULL
, 0)) == NULL
)
210 adios (NULL
, "bad address \"%s\"", s
);
215 for (ak
= akahead
; ak
; ak
= ak
->ak_next
) {
217 while ((cp
= getname (pp
))) {
218 if ((np
= getm (cp
, NULL
, 0, NULL
, 0)) == NULL
)
220 if (!strcasecmp (FENDNULL(mp
->m_host
),
221 FENDNULL(np
->m_host
)) &&
222 !strcasecmp (FENDNULL(mp
->m_mbox
),
223 FENDNULL(np
->m_mbox
))) {
224 vp
= vp
? add (ak
->ak_name
, add (",", vp
))
225 : getcpy (ak
->ak_name
);
236 print_aka (vp
? vp
: s
, list
, 0);