File: ali.c

package info (click to toggle)
nmh 1.6-2
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 6,204 kB
  • ctags: 3,851
  • sloc: ansic: 48,922; sh: 16,422; makefile: 559; perl: 509; lex: 402; awk: 74
file content (239 lines) | stat: -rw-r--r-- 4,976 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239

/*
 * ali.c -- list nmh mail aliases
 *
 * This code is Copyright (c) 2002, by the authors of nmh.  See the
 * COPYRIGHT file in the root directory of the nmh distribution for
 * complete copyright information.
 */

#include <h/mh.h>
#include <h/addrsbr.h>
#include <h/aliasbr.h>
#include <h/mts.h>
#include <h/utils.h>

#define ALI_SWITCHES \
    X("alias aliasfile", 0, ALIASW) \
    X("noalias", -7, NALIASW) \
    X("list", 0, LISTSW) \
    X("nolist", 0, NLISTSW) \
    X("user", 0, USERSW) \
    X("nouser", 0, NUSERSW) \
    X("version", 0, VERSIONSW) \
    X("help", 0, HELPSW) \

#define X(sw, minchars, id) id,
DEFINE_SWITCH_ENUM(ALI);
#undef X

#define X(sw, minchars, id) { sw, minchars, id },
DEFINE_SWITCH_ARRAY(ALI, switches);
#undef X

static int pos = 1;

extern struct aka *akahead;

/*
 * prototypes
 */
static void print_aka (char *, int, int);
static void print_usr (char *, int);


int
main (int argc, char **argv)
{
    int i, vecp = 0, inverted = 0, list = 0;
    int noalias = 0;
    char *cp, **ap, **argp, buf[BUFSIZ];
    /* Really only need to allocate for argc-1, but must allocate at least 1,
       so go ahead and allocate for argc char pointers. */
    char **vec = mh_xmalloc (argc * sizeof (char *)), **arguments;
    struct aka *ak;

    if (nmh_init(argv[0], 1)) { return 1; }

    mts_init (invo_name);
    arguments = getarguments (invo_name, argc, argv, 1);
    argp = arguments;

    while ((cp = *argp++)) {
	if (*cp == '-') {
	    switch (smatch (++cp, switches)) {
		case AMBIGSW: 
		    ambigsw (cp, switches);
		    done (1);
		case UNKWNSW: 
		    adios (NULL, "-%s unknown", cp);

		case HELPSW: 
		    snprintf (buf, sizeof(buf), "%s [switches] aliases ...",
			invo_name);
		    print_help (buf, switches, 1);
		    done (0);
		case VERSIONSW:
		    print_version (invo_name);
		    done (0);

		case ALIASW: 
		    if (!(cp = *argp++) || *cp == '-')
			adios (NULL, "missing argument to %s", argp[-2]);
		    if ((i = alias (cp)) != AK_OK)
			adios (NULL, "aliasing error in %s - %s", cp, akerror (i));
		    continue;
		case NALIASW: 
		    noalias++;
		    continue;

		case LISTSW: 
		    list++;
		    continue;
		case NLISTSW: 
		    list = 0;
		    continue;

		case USERSW: 
		    inverted++;
		    continue;
		case NUSERSW: 
		    inverted = 0;
		    continue;
	    }
	}

	if (vecp < argc) {
	    vec[vecp++] = cp;
	} else {
	    /* Should never happen, but try to protect against code changes
	       that could allow it. */
	    adios (NULL, "too many arguments");
	}
    }

    if (!noalias) {
	/* allow Aliasfile: profile entry */
	if ((cp = context_find ("Aliasfile"))) {
	    char *dp = NULL;

	    for (ap = brkstring(dp = getcpy(cp), " ", "\n"); ap && *ap; ap++)
		if ((i = alias (*ap)) != AK_OK)
		    adios (NULL, "aliasing error in %s - %s", *ap, akerror (i));
	    if (dp)
		free(dp);
	}
	alias (AliasFile);
    }

    /*
     * If -user is specified
     */
    if (inverted) {
	if (vecp == 0)
	    adios (NULL, "usage: %s -user addresses ...  (you forgot the addresses)",
		   invo_name);

	for (i = 0; i < vecp; i++)
	    print_usr (vec[i], list);
    } else {
	if (vecp) {
	    /* print specified aliases */
	    for (i = 0; i < vecp; i++)
		print_aka (akvalue (vec[i]), list, 0);
	} else {
	    /* print them all */
	    for (ak = akahead; ak; ak = ak->ak_next) {
		printf ("%s: ", ak->ak_name);
		pos += strlen (ak->ak_name) + 1;
		print_aka (akresult (ak), list, pos);
	    }
	}
    }

    free (vec);
    done (0);
    return 1;
}

static void
print_aka (char *p, int list, int margin)
{
    char c;

    if (p == NULL) {
	printf ("<empty>\n");
	return;
    }

    while ((c = *p++)) {
	switch (c) {
	    case ',': 
		if (*p) {
		    if (list)
			printf ("\n%*s", margin, "");
		    else {
			if (pos >= 68) {
			    printf (",\n ");
			    pos = 2;
			} else {
			    printf (", ");
			    pos += 2;
			}
		    }
		}

	    case 0: 
		break;

	    default: 
		pos++;
		putchar (c);
	}
    }

    putchar ('\n');
    pos = 1;
}

static void
print_usr (char *s, int list)
{
    register char *cp, *pp, *vp;
    register struct aka *ak;
    register struct mailname *mp, *np;

    if ((pp = getname (s)) == NULL)
	adios (NULL, "no address in \"%s\"", s);
    if ((mp = getm (pp, NULL, 0, NULL, 0)) == NULL)
	adios (NULL, "bad address \"%s\"", s);
    while (getname (""))
	continue;

    vp = NULL;
    for (ak = akahead; ak; ak = ak->ak_next) {
	pp = akresult (ak);
	while ((cp = getname (pp))) {
	    if ((np = getm (cp, NULL, 0, NULL, 0)) == NULL)
		continue;
	    if (!strcasecmp (mp->m_host ? mp->m_host : "",
			     np->m_host ? np->m_host : "")  &&
		!strcasecmp (mp->m_mbox ? mp->m_mbox : "",
			     np->m_mbox ? np->m_mbox : "")) {
		vp = vp ? add (ak->ak_name, add (",", vp))
		    : getcpy (ak->ak_name);
		mnfree (np);
		while (getname (""))
		    continue;
		break;
	    }
	    mnfree (np);
	}
    }
    mnfree (mp);

    print_aka (vp ? vp : s, list, 0);

    if (vp)
	free (vp);
}