Commit | Line | Data |
---|---|---|
2ae9f53f | 1 | #ifndef lint |
f486c0e5 | 2 | static char sccsid[] = "@(#)optim.c 2.10 (Berkeley) %G%"; |
2ae9f53f | 3 | #endif |
7537529b KS |
4 | |
5 | /* | |
6 | * Mail -- a program for sending and receiving mail. | |
7 | * | |
8 | * Network name modification routines. | |
9 | */ | |
10 | ||
11 | #include "rcv.h" | |
6049dc7a | 12 | #include "configdefs.h" |
7537529b KS |
13 | #include <ctype.h> |
14 | ||
7537529b KS |
15 | /* |
16 | * Map a name into the correct network "view" of the | |
17 | * name. This is done by prepending the name with the | |
18 | * network address of the sender, then optimizing away | |
19 | * nonsense. | |
20 | */ | |
21 | ||
7537529b KS |
22 | char * |
23 | netmap(name, from) | |
24 | char name[], from[]; | |
25 | { | |
26 | char nbuf[BUFSIZ], ret[BUFSIZ]; | |
27 | register char *cp; | |
28 | ||
29 | if (strlen(from) == 0) | |
30 | return(name); | |
31 | if (any('@', name) || any('%', name)) | |
7567d2bb | 32 | return(savestr(arpafix(name, from))); |
7537529b KS |
33 | cp = revarpa(from); |
34 | if (cp == NOSTR) | |
35 | return(name); | |
36 | strcpy(nbuf, cp); | |
37 | cp = &nbuf[strlen(nbuf) - 1]; | |
38 | while (!any(*cp, metanet) && cp > nbuf) | |
39 | cp--; | |
40 | if (cp == nbuf) | |
41 | return(name); | |
42 | *++cp = 0; | |
43 | strcat(nbuf, revarpa(name)); | |
44 | optim(nbuf, ret); | |
45 | cp = revarpa(ret); | |
46 | if (!icequal(name, cp)) | |
7567d2bb | 47 | return(savestr(cp)); |
7537529b KS |
48 | return(name); |
49 | } | |
50 | ||
51 | /* | |
52 | * Rename the given network path to use | |
53 | * the kinds of names that we would right here. | |
54 | */ | |
55 | ||
56 | char * | |
57 | rename(str) | |
58 | char str[]; | |
59 | { | |
60 | register char *cp, *cp2; | |
61 | char buf[BUFSIZ], path[BUFSIZ]; | |
62 | register int c, host; | |
63 | ||
64 | strcpy(path, ""); | |
65 | for (;;) { | |
66 | if ((c = *cp++) == 0) | |
67 | break; | |
68 | cp2 = buf; | |
69 | while (!any(c, metanet) && c != 0) { | |
70 | *cp2++ = c; | |
71 | c = *cp++; | |
72 | } | |
73 | *cp2 = 0; | |
74 | if (c == 0) { | |
75 | strcat(path, buf); | |
76 | break; | |
77 | } | |
78 | host = netlook(buf, ntype(c)); | |
79 | strcat(path, netname(host)); | |
80 | stradd(path, c); | |
81 | } | |
82 | if (strcmp(str, path) != 0) | |
83 | return(savestr(path)); | |
84 | return(str); | |
85 | } | |
6049dc7a | 86 | |
7537529b KS |
87 | /* |
88 | * Turn a network machine name into a unique character | |
7537529b | 89 | */ |
7537529b KS |
90 | netlook(machine, attnet) |
91 | char machine[]; | |
92 | { | |
93 | register struct netmach *np; | |
94 | register char *cp, *cp2; | |
f486c0e5 | 95 | char nbuf[100]; |
7537529b KS |
96 | |
97 | /* | |
98 | * Make into lower case. | |
99 | */ | |
100 | ||
101 | for (cp = machine, cp2 = nbuf; *cp; *cp2++ = little(*cp++)) | |
f486c0e5 RC |
102 | if (cp2 >= &nbuf[sizeof(nbuf)-1]) |
103 | break; | |
7537529b KS |
104 | *cp2 = 0; |
105 | ||
106 | /* | |
107 | * If a single letter machine, look through those first. | |
108 | */ | |
109 | ||
110 | if (strlen(nbuf) == 1) | |
111 | for (np = netmach; np->nt_mid != 0; np++) | |
112 | if (np->nt_mid == nbuf[0]) | |
113 | return(nbuf[0]); | |
114 | ||
115 | /* | |
116 | * Look for usual name | |
117 | */ | |
118 | ||
119 | for (np = netmach; np->nt_mid != 0; np++) | |
120 | if (strcmp(np->nt_machine, nbuf) == 0) | |
121 | return(np->nt_mid); | |
122 | ||
123 | /* | |
124 | * Look in side hash table. | |
125 | */ | |
126 | ||
127 | return(mstash(nbuf, attnet)); | |
128 | } | |
129 | ||
130 | /* | |
131 | * Make a little character. | |
132 | */ | |
133 | ||
134 | little(c) | |
135 | register int c; | |
136 | { | |
137 | ||
138 | if (c >= 'A' && c <= 'Z') | |
139 | c += 'a' - 'A'; | |
140 | return(c); | |
141 | } | |
142 | ||
143 | /* | |
144 | * Turn a network unique character identifier into a network name. | |
145 | */ | |
146 | ||
147 | char * | |
148 | netname(mid) | |
149 | { | |
150 | register struct netmach *np; | |
151 | char *mlook(); | |
152 | ||
153 | if (mid & 0200) | |
154 | return(mlook(mid)); | |
155 | for (np = netmach; np->nt_mid != 0; np++) | |
156 | if (np->nt_mid == mid) | |
157 | return(np->nt_machine); | |
158 | return(NOSTR); | |
159 | } | |
160 | ||
161 | /* | |
162 | * Deal with arpa net addresses. The way this is done is strange. | |
163 | * In particular, if the destination arpa net host is not Berkeley, | |
164 | * then the address is correct as stands. Otherwise, we strip off | |
165 | * the trailing @Berkeley, then cook up a phony person for it to | |
166 | * be from and optimize the result. | |
167 | */ | |
168 | char * | |
169 | arpafix(name, from) | |
170 | char name[]; | |
171 | char from[]; | |
172 | { | |
173 | register char *cp; | |
174 | register int arpamach; | |
175 | char newname[BUFSIZ]; | |
176 | char fake[5]; | |
177 | char fakepath[20]; | |
178 | ||
179 | if (debug) { | |
180 | fprintf(stderr, "arpafix(%s, %s)\n", name, from); | |
181 | } | |
182 | cp = rindex(name, '@'); | |
183 | if (cp == NOSTR) | |
184 | cp = rindex(name, '%'); | |
185 | if (cp == NOSTR) { | |
186 | fprintf(stderr, "Somethings amiss -- no @ or % in arpafix\n"); | |
187 | return(name); | |
188 | } | |
189 | cp++; | |
190 | arpamach = netlook(cp, '@'); | |
191 | if (arpamach == 0) { | |
192 | if (debug) | |
193 | fprintf(stderr, "machine %s unknown, uses: %s\n", cp, name); | |
194 | return(name); | |
195 | } | |
196 | if (((nettype(arpamach) & nettype(LOCAL)) & ~AN) == 0) { | |
197 | if (debug) | |
198 | fprintf(stderr, "machine %s known but remote, uses: %s\n", | |
199 | cp, name); | |
200 | return(name); | |
201 | } | |
202 | strcpy(newname, name); | |
203 | cp = rindex(newname, '@'); | |
204 | if (cp == NOSTR) | |
205 | cp = rindex(newname, '%'); | |
206 | *cp = 0; | |
207 | fake[0] = arpamach; | |
208 | fake[1] = ':'; | |
209 | fake[2] = LOCAL; | |
210 | fake[3] = ':'; | |
211 | fake[4] = 0; | |
212 | prefer(fake); | |
213 | strcpy(fakepath, netname(fake[0])); | |
214 | stradd(fakepath, fake[1]); | |
215 | strcat(fakepath, "daemon"); | |
216 | if (debug) | |
217 | fprintf(stderr, "machine local, call netmap(%s, %s)\n", | |
218 | newname, fakepath); | |
219 | return(netmap(newname, fakepath)); | |
220 | } | |
221 | ||
222 | /* | |
223 | * Take a network machine descriptor and find the types of connected | |
224 | * nets and return it. | |
225 | */ | |
226 | ||
227 | nettype(mid) | |
228 | { | |
229 | register struct netmach *np; | |
230 | ||
231 | if (mid & 0200) | |
232 | return(mtype(mid)); | |
233 | for (np = netmach; np->nt_mid != 0; np++) | |
234 | if (np->nt_mid == mid) | |
235 | return(np->nt_type); | |
236 | return(0); | |
237 | } | |
238 | ||
239 | /* | |
240 | * Hashing routines to salt away machines seen scanning | |
241 | * networks paths that we don't know about. | |
242 | */ | |
243 | ||
244 | #define XHSIZE 19 /* Size of extra hash table */ | |
245 | #define NXMID (XHSIZE*3/4) /* Max extra machines */ | |
246 | ||
247 | struct xtrahash { | |
248 | char *xh_name; /* Name of machine */ | |
249 | short xh_mid; /* Machine ID */ | |
250 | short xh_attnet; /* Attached networks */ | |
251 | } xtrahash[XHSIZE]; | |
252 | ||
253 | struct xtrahash *xtab[XHSIZE]; /* F: mid-->machine name */ | |
254 | ||
255 | short midfree; /* Next free machine id */ | |
256 | ||
257 | /* | |
258 | * Initialize the extra host hash table. | |
259 | * Called by sreset. | |
260 | */ | |
261 | ||
262 | minit() | |
263 | { | |
264 | register struct xtrahash *xp, **tp; | |
265 | register int i; | |
266 | ||
267 | midfree = 0; | |
268 | tp = &xtab[0]; | |
269 | for (xp = &xtrahash[0]; xp < &xtrahash[XHSIZE]; xp++) { | |
270 | xp->xh_name = NOSTR; | |
271 | xp->xh_mid = 0; | |
272 | xp->xh_attnet = 0; | |
273 | *tp++ = (struct xtrahash *) 0; | |
274 | } | |
275 | } | |
276 | ||
277 | /* | |
278 | * Stash a net name in the extra host hash table. | |
279 | * If a new entry is put in the hash table, deduce what | |
280 | * net the machine is attached to from the net character. | |
281 | * | |
282 | * If the machine is already known, add the given attached | |
283 | * net to those already known. | |
284 | */ | |
285 | ||
286 | mstash(name, attnet) | |
287 | char name[]; | |
288 | { | |
289 | register struct xtrahash *xp; | |
290 | struct xtrahash *xlocate(); | |
6049dc7a | 291 | int x; |
7537529b KS |
292 | |
293 | xp = xlocate(name); | |
294 | if (xp == (struct xtrahash *) 0) { | |
295 | printf("Ran out of machine id spots\n"); | |
296 | return(0); | |
297 | } | |
298 | if (xp->xh_name == NOSTR) { | |
299 | if (midfree >= XHSIZE) { | |
300 | printf("Out of machine ids\n"); | |
301 | return(0); | |
302 | } | |
303 | xtab[midfree] = xp; | |
304 | xp->xh_name = savestr(name); | |
305 | xp->xh_mid = 0200 + midfree++; | |
306 | } | |
6049dc7a KS |
307 | x = ntype(attnet); |
308 | if (x == 0) | |
7537529b | 309 | xp->xh_attnet |= SN; |
6049dc7a KS |
310 | else |
311 | xp->xh_attnet |= x; | |
7537529b KS |
312 | return(xp->xh_mid); |
313 | } | |
314 | ||
315 | /* | |
316 | * Search for the given name in the hash table | |
317 | * and return the pointer to it if found, or to the first | |
318 | * empty slot if not found. | |
319 | * | |
320 | * If no free slots can be found, return 0. | |
321 | */ | |
322 | ||
323 | struct xtrahash * | |
324 | xlocate(name) | |
325 | char name[]; | |
326 | { | |
327 | register int h, q, i; | |
328 | register char *cp; | |
329 | register struct xtrahash *xp; | |
330 | ||
331 | for (h = 0, cp = name; *cp; h = (h << 2) + *cp++) | |
332 | ; | |
333 | if (h < 0 && (h = -h) < 0) | |
334 | h = 0; | |
335 | h = h % XHSIZE; | |
336 | cp = name; | |
337 | for (i = 0, q = 0; q < XHSIZE; i++, q = i * i) { | |
338 | xp = &xtrahash[(h + q) % XHSIZE]; | |
339 | if (xp->xh_name == NOSTR) | |
340 | return(xp); | |
341 | if (strcmp(cp, xp->xh_name) == 0) | |
342 | return(xp); | |
343 | if (h - q < 0) | |
344 | q += XHSIZE; | |
345 | xp = &xtrahash[(h - q) % XHSIZE]; | |
346 | if (xp->xh_name == NOSTR) | |
347 | return(xp); | |
348 | if (strcmp(cp, xp->xh_name) == 0) | |
349 | return(xp); | |
350 | } | |
351 | return((struct xtrahash *) 0); | |
352 | } | |
353 | ||
354 | /* | |
355 | * Return the name from the extra host hash table corresponding | |
356 | * to the passed machine id. | |
357 | */ | |
358 | ||
359 | char * | |
360 | mlook(mid) | |
361 | { | |
362 | register int m; | |
363 | ||
364 | if ((mid & 0200) == 0) | |
365 | return(NOSTR); | |
366 | m = mid & 0177; | |
367 | if (m >= midfree) { | |
368 | printf("Use made of undefined machine id\n"); | |
369 | return(NOSTR); | |
370 | } | |
371 | return(xtab[m]->xh_name); | |
372 | } | |
373 | ||
374 | /* | |
375 | * Return the bit mask of net's that the given extra host machine | |
376 | * id has so far. | |
377 | */ | |
378 | ||
379 | mtype(mid) | |
380 | { | |
381 | register int m; | |
382 | ||
383 | if ((mid & 0200) == 0) | |
384 | return(0); | |
385 | m = mid & 0177; | |
386 | if (m >= midfree) { | |
387 | printf("Use made of undefined machine id\n"); | |
388 | return(0); | |
389 | } | |
390 | return(xtab[m]->xh_attnet); | |
391 | } | |
392 | ||
393 | /* | |
394 | * Take a network name and optimize it. This gloriously messy | |
bedc7b35 | 395 | * operation takes place as follows: the name with machine names |
7537529b KS |
396 | * in it is tokenized by mapping each machine name into a single |
397 | * character machine id (netlook). The separator characters (network | |
398 | * metacharacters) are left intact. The last component of the network | |
399 | * name is stripped off and assumed to be the destination user name -- | |
400 | * it does not participate in the optimization. As an example, the | |
401 | * name "research!vax135!research!ucbvax!bill" becomes, tokenized, | |
402 | * "r!x!r!v!" and "bill" A low level routine, optim1, fixes up the | |
403 | * network part (eg, "r!x!r!v!"), then we convert back to network | |
404 | * machine names and tack the user name on the end. | |
405 | * | |
406 | * The result of this is copied into the parameter "name" | |
407 | */ | |
408 | ||
409 | optim(net, name) | |
410 | char net[], name[]; | |
411 | { | |
412 | char netcomp[BUFSIZ], netstr[40], xfstr[40]; | |
413 | register char *cp, *cp2; | |
414 | register int c; | |
415 | ||
416 | strcpy(netstr, ""); | |
417 | cp = net; | |
418 | for (;;) { | |
419 | /* | |
420 | * Rip off next path component into netcomp | |
421 | */ | |
422 | cp2 = netcomp; | |
423 | while (*cp && !any(*cp, metanet)) | |
424 | *cp2++ = *cp++; | |
425 | *cp2 = 0; | |
426 | /* | |
427 | * If we hit null byte, then we just scanned | |
428 | * the destination user name. Go off and optimize | |
429 | * if its so. | |
430 | */ | |
431 | if (*cp == 0) | |
432 | break; | |
433 | if ((c = netlook(netcomp, *cp)) == 0) { | |
434 | printf("No host named \"%s\"\n", netcomp); | |
435 | err: | |
436 | strcpy(name, net); | |
437 | return; | |
438 | } | |
439 | stradd(netstr, c); | |
440 | stradd(netstr, *cp++); | |
441 | /* | |
442 | * If multiple network separators given, | |
443 | * throw away the extras. | |
444 | */ | |
445 | while (any(*cp, metanet)) | |
446 | cp++; | |
447 | } | |
448 | if (strlen(netcomp) == 0) { | |
449 | printf("net name syntax\n"); | |
450 | goto err; | |
451 | } | |
452 | optim1(netstr, xfstr); | |
453 | ||
454 | /* | |
455 | * Convert back to machine names. | |
456 | */ | |
457 | ||
458 | cp = xfstr; | |
459 | strcpy(name, ""); | |
460 | while (*cp) { | |
461 | if ((cp2 = netname(*cp++)) == NOSTR) { | |
462 | printf("Made up bad net name\n"); | |
34e33c1a KS |
463 | printf("Machine code %c (0%o)\n", cp[-1], cp[-1]); |
464 | printf("Sorry -- dumping now. Alert K. Shoens\n"); | |
465 | core(0); | |
7537529b KS |
466 | goto err; |
467 | } | |
468 | strcat(name, cp2); | |
469 | stradd(name, *cp++); | |
470 | } | |
471 | strcat(name, netcomp); | |
472 | } | |
473 | ||
474 | /* | |
475 | * Take a string of network machine id's and separators and | |
476 | * optimize them. We process these by pulling off maximal | |
477 | * leading strings of the same type, passing these to the appropriate | |
478 | * optimizer and concatenating the results. | |
479 | */ | |
480 | ||
7537529b KS |
481 | optim1(netstr, name) |
482 | char netstr[], name[]; | |
483 | { | |
484 | char path[40], rpath[40]; | |
485 | register char *cp, *cp2; | |
486 | register int tp, nc; | |
487 | ||
488 | cp = netstr; | |
489 | prefer(cp); | |
9d24b3cb | 490 | strcpy(name, ""); |
bedc7b35 KS |
491 | /* |
492 | * If the address ultimately points back to us, | |
493 | * just return a null network path. | |
494 | */ | |
495 | if (strlen(cp) > 1 && cp[strlen(cp) - 2] == LOCAL) | |
496 | return; | |
7537529b KS |
497 | while (*cp != 0) { |
498 | strcpy(path, ""); | |
499 | tp = ntype(cp[1]); | |
500 | nc = cp[1]; | |
501 | while (*cp && tp == ntype(cp[1])) { | |
502 | stradd(path, *cp++); | |
503 | cp++; | |
504 | } | |
505 | switch (netkind(tp)) { | |
506 | default: | |
507 | strcpy(rpath, path); | |
508 | break; | |
509 | ||
510 | case IMPLICIT: | |
511 | optimimp(path, rpath); | |
512 | break; | |
513 | ||
514 | case EXPLICIT: | |
515 | optimex(path, rpath); | |
516 | break; | |
517 | } | |
518 | for (cp2 = rpath; *cp2 != 0; cp2++) { | |
519 | stradd(name, *cp2); | |
520 | stradd(name, nc); | |
521 | } | |
522 | } | |
523 | optiboth(name); | |
524 | prefer(name); | |
525 | } | |
526 | ||
527 | /* | |
528 | * Return the network of the separator -- | |
529 | * AN for arpa net | |
530 | * BN for Bell labs net | |
531 | * SN for Schmidt (berkeley net) | |
532 | * 0 if we don't know. | |
533 | */ | |
534 | ||
535 | ntype(nc) | |
536 | register int nc; | |
537 | { | |
7737ae6a | 538 | register struct ntypetab *np; |
7537529b | 539 | |
7737ae6a | 540 | for (np = ntypetab; np->nt_char != 0; np++) |
6049dc7a | 541 | if (np->nt_char == nc) |
7737ae6a | 542 | return(np->nt_bcode); |
6049dc7a | 543 | return(0); |
7537529b KS |
544 | } |
545 | ||
546 | /* | |
547 | * Return the kind of routing used for the particular net | |
548 | * EXPLICIT means explicitly routed | |
549 | * IMPLICIT means implicitly routed | |
550 | * 0 means don't know | |
551 | */ | |
552 | ||
553 | netkind(nt) | |
554 | register int nt; | |
555 | { | |
7737ae6a | 556 | register struct nkindtab *np; |
7537529b | 557 | |
7737ae6a | 558 | for (np = nkindtab; np->nk_type != 0; np++) |
6049dc7a KS |
559 | if (np->nk_type == nt) |
560 | return(np->nk_kind); | |
561 | return(0); | |
7537529b KS |
562 | } |
563 | ||
564 | /* | |
565 | * Do name optimization for an explicitly routed network (eg BTL network). | |
566 | */ | |
567 | ||
568 | optimex(net, name) | |
569 | char net[], name[]; | |
570 | { | |
571 | register char *cp, *rp; | |
572 | register int m; | |
573 | char *rindex(); | |
574 | ||
575 | strcpy(name, net); | |
576 | cp = name; | |
577 | if (strlen(cp) == 0) | |
578 | return(-1); | |
579 | if (cp[strlen(cp)-1] == LOCAL) { | |
580 | name[0] = 0; | |
581 | return(0); | |
582 | } | |
583 | for (cp = name; *cp; cp++) { | |
584 | m = *cp; | |
585 | rp = rindex(cp+1, m); | |
586 | if (rp != NOSTR) | |
587 | strcpy(cp, rp); | |
588 | } | |
589 | return(0); | |
590 | } | |
591 | ||
592 | /* | |
593 | * Do name optimization for implicitly routed network (eg, arpanet, | |
594 | * Berkeley network) | |
595 | */ | |
596 | ||
597 | optimimp(net, name) | |
598 | char net[], name[]; | |
599 | { | |
600 | register char *cp; | |
601 | register int m; | |
602 | ||
603 | cp = net; | |
604 | if (strlen(cp) == 0) | |
605 | return(-1); | |
606 | m = cp[strlen(cp) - 1]; | |
607 | if (m == LOCAL) { | |
608 | strcpy(name, ""); | |
609 | return(0); | |
610 | } | |
611 | name[0] = m; | |
612 | name[1] = 0; | |
613 | return(0); | |
614 | } | |
615 | ||
616 | /* | |
7537529b KS |
617 | * Perform global optimization on the given network path. |
618 | * The trick here is to look ahead to see if there are any loops | |
619 | * in the path and remove them. The interpretation of loops is | |
620 | * more strict here than in optimex since both the machine and net | |
621 | * type must match. | |
622 | */ | |
623 | ||
624 | optiboth(net) | |
625 | char net[]; | |
626 | { | |
627 | register char *cp, *cp2; | |
628 | char *rpair(); | |
629 | ||
630 | cp = net; | |
631 | if (strlen(cp) == 0) | |
632 | return; | |
633 | if ((strlen(cp) % 2) != 0) { | |
634 | printf("Strange arg to optiboth\n"); | |
635 | return; | |
636 | } | |
637 | while (*cp) { | |
638 | cp2 = rpair(cp+2, *cp); | |
639 | if (cp2 != NOSTR) | |
640 | strcpy(cp, cp2); | |
641 | cp += 2; | |
642 | } | |
643 | } | |
644 | ||
645 | /* | |
646 | * Find the rightmost instance of the given (machine, type) pair. | |
647 | */ | |
648 | ||
649 | char * | |
650 | rpair(str, mach) | |
651 | char str[]; | |
652 | { | |
653 | register char *cp, *last; | |
654 | ||
655 | last = NOSTR; | |
656 | while (*cp) { | |
657 | if (*cp == mach) | |
658 | last = cp; | |
659 | cp += 2; | |
660 | } | |
661 | return(last); | |
662 | } | |
663 | ||
664 | /* | |
665 | * Change the network separators in the given network path | |
666 | * to the preferred network transmission means. | |
667 | */ | |
668 | ||
669 | prefer(name) | |
670 | char name[]; | |
671 | { | |
672 | register char *cp; | |
673 | register int state, n; | |
674 | ||
675 | state = LOCAL; | |
676 | for (cp = name; *cp; cp += 2) { | |
677 | n = best(state, *cp); | |
678 | if (n) | |
679 | cp[1] = n; | |
680 | state = *cp; | |
681 | } | |
682 | } | |
683 | ||
684 | /* | |
685 | * Return the best network separator for the given machine pair. | |
686 | */ | |
687 | ||
7537529b KS |
688 | best(src, dest) |
689 | { | |
690 | register int dtype, stype; | |
691 | register struct netorder *np; | |
692 | ||
693 | stype = nettype(src); | |
694 | dtype = nettype(dest); | |
bedc7b35 | 695 | fflush(stdout); |
7537529b KS |
696 | if (stype == 0 || dtype == 0) { |
697 | printf("ERROR: unknown internal machine id\n"); | |
698 | return(0); | |
699 | } | |
bedc7b35 | 700 | if ((stype & dtype) == 0) |
7537529b | 701 | return(0); |
7537529b KS |
702 | np = &netorder[0]; |
703 | while ((np->no_stat & stype & dtype) == 0) | |
704 | np++; | |
705 | return(np->no_char); | |
706 | } | |
707 | ||
03906560 | 708 | #ifdef GETHOST |
168407ab KM |
709 | /* |
710 | * Initialize the network name of the current host. | |
711 | */ | |
712 | inithost() | |
713 | { | |
714 | register struct netmach *np; | |
715 | static char host[64]; | |
716 | ||
717 | gethostname(host, sizeof host); | |
718 | for (np = netmach; np->nt_machine != 0; np++) | |
719 | if (strcmp(np->nt_machine, EMPTY) == 0) | |
720 | break; | |
721 | if (np->nt_machine == 0) { | |
722 | printf("Cannot find empty slot for dynamic host entry\n"); | |
723 | exit(1); | |
724 | } | |
725 | np->nt_machine = host; | |
726 | } | |
03906560 | 727 | #endif GETHOST |
168407ab | 728 | |
7537529b KS |
729 | /* |
730 | * Code to twist around arpa net names. | |
731 | */ | |
732 | ||
733 | #define WORD 257 /* Token for a string */ | |
734 | ||
735 | static char netbuf[256]; | |
736 | static char *yylval; | |
737 | ||
738 | /* | |
739 | * Reverse all of the arpa net addresses in the given name to | |
740 | * be of the form "host @ user" instead of "user @ host" | |
741 | * This function is its own inverse. | |
742 | */ | |
743 | ||
744 | char * | |
745 | revarpa(str) | |
746 | char str[]; | |
747 | { | |
748 | ||
749 | if (yyinit(str) < 0) | |
750 | return(NOSTR); | |
751 | if (name()) | |
752 | return(NOSTR); | |
753 | if (strcmp(str, netbuf) == 0) | |
754 | return(str); | |
755 | return(savestr(netbuf)); | |
756 | } | |
757 | ||
758 | /* | |
759 | * Parse (by recursive descent) network names, using the following grammar: | |
760 | * name: | |
761 | * term {':' term} | |
762 | * term {'^' term} | |
763 | * term {'!' term} | |
764 | * term '@' name | |
765 | * term '%' name | |
766 | * | |
767 | * term: | |
768 | * string of characters. | |
769 | */ | |
770 | ||
771 | name() | |
772 | { | |
773 | register int t; | |
774 | register char *cp; | |
775 | ||
776 | for (;;) { | |
777 | t = yylex(); | |
778 | if (t != WORD) | |
779 | return(-1); | |
780 | cp = yylval; | |
781 | t = yylex(); | |
782 | switch (t) { | |
783 | case 0: | |
784 | strcat(netbuf, cp); | |
785 | return(0); | |
786 | ||
787 | case '@': | |
788 | case '%': | |
789 | if (name()) | |
790 | return(-1); | |
791 | stradd(netbuf, '@'); | |
792 | strcat(netbuf, cp); | |
793 | return(0); | |
794 | ||
795 | case WORD: | |
796 | return(-1); | |
797 | ||
798 | default: | |
799 | strcat(netbuf, cp); | |
800 | stradd(netbuf, t); | |
801 | } | |
802 | } | |
803 | } | |
804 | ||
805 | /* | |
806 | * Scanner for network names. | |
807 | */ | |
808 | ||
809 | static char *charp; /* Current input pointer */ | |
810 | static int nexttok; /* Salted away next token */ | |
811 | ||
812 | /* | |
813 | * Initialize the network name scanner. | |
814 | */ | |
815 | ||
816 | yyinit(str) | |
817 | char str[]; | |
818 | { | |
819 | static char lexbuf[BUFSIZ]; | |
820 | ||
821 | netbuf[0] = 0; | |
822 | if (strlen(str) >= sizeof lexbuf - 1) | |
823 | return(-1); | |
824 | nexttok = 0; | |
825 | strcpy(lexbuf, str); | |
826 | charp = lexbuf; | |
827 | return(0); | |
828 | } | |
829 | ||
830 | /* | |
831 | * Scan and return a single token. | |
832 | * yylval is set to point to a scanned string. | |
833 | */ | |
834 | ||
835 | yylex() | |
836 | { | |
837 | register char *cp, *dot; | |
838 | register int s; | |
839 | ||
840 | if (nexttok) { | |
841 | s = nexttok; | |
842 | nexttok = 0; | |
843 | return(s); | |
844 | } | |
845 | cp = charp; | |
846 | while (*cp && isspace(*cp)) | |
847 | cp++; | |
848 | if (*cp == 0) | |
849 | return(0); | |
6049dc7a | 850 | if (any(*cp, metanet)) { |
7537529b KS |
851 | charp = cp+1; |
852 | return(*cp); | |
853 | } | |
854 | dot = cp; | |
6049dc7a | 855 | while (*cp && !any(*cp, metanet) && !any(*cp, " \t")) |
7537529b | 856 | cp++; |
6049dc7a | 857 | if (any(*cp, metanet)) |
7537529b KS |
858 | nexttok = *cp; |
859 | if (*cp == 0) | |
860 | charp = cp; | |
861 | else | |
862 | charp = cp+1; | |
863 | *cp = 0; | |
864 | yylval = dot; | |
865 | return(WORD); | |
866 | } | |
867 | ||
868 | /* | |
869 | * Add a single character onto a string. | |
870 | */ | |
871 | ||
872 | stradd(str, c) | |
873 | register char *str; | |
874 | register int c; | |
875 | { | |
876 | ||
877 | str += strlen(str); | |
878 | *str++ = c; | |
879 | *str = 0; | |
880 | } |