]>
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 *, bool, int);
40 static void print_usr (char *, bool);
44 main (int argc
, char **argv
)
47 bool inverted
, list
, noalias
;
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 inverted
= list
= noalias
= false;
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
, bool list
, int margin
)
176 printf ("\n%*s", margin
, "");
202 print_usr (char *s
, bool 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 (FENDNULL(mp
->m_host
),
222 FENDNULL(np
->m_host
)) &&
223 !strcasecmp (FENDNULL(mp
->m_mbox
),
224 FENDNULL(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);