Merge commit 'b1e7e97d3b60469b243b3b2e22c7d8cbd11c7c90'
[unleashed.git] / usr / src / cmd / mailx / optim.c
blobef18da0e6f2781ae7dc4bebd35dffe5882bb4b9e
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License"). You may not use this file except in compliance
7 * with the License.
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or http://www.opensolaris.org/os/licensing.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
20 * CDDL HEADER END
23 * Copyright 1998 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
28 /* All Rights Reserved */
31 #pragma ident "%Z%%M% %I% %E% SMI"
34 * mailx -- a modified version of a University of California at Berkeley
35 * mail program
37 * Network name modification routines.
40 #include "rcv.h"
41 #include "configdefs.h"
42 #include <locale.h>
44 static char *arpafix(char name[], char from[]);
45 static char *lasthost(char *addr);
46 static char *makeremote(char name[], char from[]);
47 static int mstash(char name[], int attnet);
48 static int mtype(int mid);
49 static int netlook(char machine[], int attnet);
50 static int nettype(int mid);
51 static int ntype(register int nc);
52 static void stradd(register char *str, int n, register int c);
53 static char *tackon(char *sys, char *rest);
54 static struct xtrahash *xlocate(char name[]);
55 #ifdef OPTIM
56 static char best(int src, int dest);
57 static char *mlook(int mid);
58 static int netkind(register int nt);
59 static void optiboth(char net[]);
60 static void optim(char net[], char name[]);
61 static void optim1(char netstr[], char name[]);
62 static int optimex(char net[], char name[]);
63 static int optimimp(char net[], char name[]);
64 static void prefer(char name[]);
65 static char *rpair(char str[], int mach);
66 #endif
69 * Map a name into the correct network "view" of the
70 * name. This is done by prepending the name with the
71 * network address of the sender, then optimizing away
72 * nonsense.
75 char *
76 netmap(char name[], char from[])
78 char nbuf[BUFSIZ], ret[BUFSIZ];
79 register char *cp, *oname;
81 if (debug) fprintf(stderr, "netmap(name '%s', from '%s')\n", name, from);
82 if (strlen(from) == 0)
83 return(name); /* "from" is empty - can't do anything */
85 if (strcmp(from, name) == 0)
86 return(name); /* "from" and "name" are the same, do nothing */
89 * If the name contains an "@" or a "%", remove it and the host
90 * following it if that host is "known".
92 if (any('@', name) || any('%', name))
93 return(arpafix(name, from));
96 * If the sender contains a "@" or a "%", make "name" into an
97 * address on that host, on the presumption that it should
98 * really have read "name@from" when we received the message
99 * rather than just "name".
101 if (any('@', from) || any('%', from))
102 return(unuucp(makeremote(name, from)));
103 if (value("onehop") && (cp = strchr(name, '!')) && cp > name) {
105 * "onehop" is set, meaning all machines are one UUCP
106 * hop away (fat chance, in this day and age), and "name"
107 * is a UUCP path rather than just a name. Leave it alone.
109 nstrcpy(nbuf, sizeof (nbuf), name);
110 } else {
111 from = tackon(host, from);
112 *strrchr(from, '!') = 0;
113 name = tackon(lasthost(from), name);
114 while (((cp = lasthost(from)) != 0) && ishost(cp, name)) {
115 oname = name;
116 name = strchr(name, '!') + 1;
117 if (cp == from) {
118 from[strlen(from)] = '!';
119 if (value("mustbang") && !strchr(name, '!'))
120 name = oname;
121 return(unuucp(name));
123 *--cp = 0;
125 from[strlen(from)] = '!';
126 from = strchr(from, '!') + 1;
127 snprintf(nbuf, sizeof (nbuf), "%s!%s", from, name);
129 if (debug) fprintf(stderr, "before optim, nbuf '%s'\n", name);
130 #ifdef OPTIM
131 if ((cp = value("conv"))==NOSTR || strcmp(cp, "optimize") != 0)
132 nstrcpy(ret, sizeof (ret), nbuf);
133 else
134 optim(nbuf, ret);
135 #else
136 nstrcpy(ret, sizeof (ret), nbuf);
137 #endif /* OPTIM */
138 if (debug) fprintf(stderr, "after optim, nbuf '%s', ret '%s'\n", nbuf, ret);
139 cp = ret;
140 if (debug) fprintf(stderr, "wind up with '%s'\n", name);
141 if (!icequal(name, cp))
142 return(unuucp((char *) savestr(cp)));
143 return(unuucp(name));
147 * Stick a host on the beginning of a uucp
148 * address if it isn't there already.
150 static char *
151 tackon(char *sys, char *rest)
153 while (*rest == '!')
154 rest++;
155 if (!ishost(sys, rest)) {
156 char *r = (char *)salloc(strlen(sys) + strlen(rest) + 2);
157 sprintf(r, "%s!%s", sys, rest);
158 rest = r;
160 return rest;
164 * Check equality of the first host in a uucp address.
166 int
167 ishost(char *sys, char *rest)
169 while (*sys && *sys == *rest)
170 sys++, rest++;
171 return(*sys == 0 && *rest == '!');
175 * Return last host in a uucp address.
177 static char *
178 lasthost(char *addr)
180 char *r = strrchr(addr, '!');
181 return r ? ++r : addr;
185 * Optionally translate an old format uucp name into a new one, e.g.
186 * "mach1!mach2!user" becomes "user@mach2.UUCP". This optional because
187 * some information is necessarily lost (e.g. the route it got here
188 * via) and if we don't have the host in our routing tables, we lose.
189 * XXX THIS IS NO LONGER VALID WITH THE NEW UUCP PROJECT PLANS TO
190 * REGISTER UUCP HOSTS IN THE STANDARD INTERNET NAMESPACE, E.G.
191 * ihnp4 BECOMES "ihnp4.att.com".
193 char *
194 unuucp(char *name)
196 register char *np, *hp, *cp;
197 char result[100];
198 char tname[300];
200 if (UnUUCP==0 &&
201 ((cp = value("conv"))==NOSTR || strcmp(cp, "internet")))
202 return name;
203 if (debug) fprintf(stderr, "unuucp(%s)\n", name);
204 nstrcpy(tname, sizeof (tname), name);
205 np = strrchr(tname, '!');
206 if (np == NOSTR)
207 return name;
208 *np++ = 0;
209 hp = strrchr(tname, '!');
210 if (hp == NOSTR)
211 hp = tname;
212 else
213 *hp++ = 0;
214 cp = strchr(np, '@');
215 if (cp == NOSTR)
216 cp = strchr(np, '%');
217 if (cp)
218 *cp = 0;
219 if (debug) fprintf(stderr, "host %s, name %s\n", hp, np);
220 snprintf(result, sizeof (result), "%s@%s.UUCP", np, hp);
221 if (debug) fprintf(stderr, "unuucp returns %s\n", result);
222 return savestr(result);
226 * Turn a network machine name into a unique character
228 static int
229 netlook(char machine[], int attnet)
231 register struct netmach *np;
232 register char *cp, *cp2;
233 char nbuf[BUFSIZ];
236 * Make into lower case.
238 for (cp = machine, cp2 = nbuf;
239 *cp && cp2 < &nbuf[BUFSIZ-1];
240 *cp2++ = tolower(*cp++))
241 /*nothing*/;
242 *cp2 = 0;
245 * If a single letter machine, look through those first.
248 if (strlen(nbuf) == 1)
249 for (np = netmach; np->nt_mid != 0; np++)
250 if (np->nt_mid == nbuf[0])
251 return(nbuf[0]);
254 * Look for usual name
257 for (np = netmach; np->nt_mid != 0; np++)
258 if (strcmp(np->nt_machine, nbuf) == 0)
259 return(np->nt_mid);
262 * Look in side hash table.
265 return(mstash(nbuf, attnet));
268 #ifdef OPTIM
270 * Turn a network unique character identifier into a network name.
273 static char *
274 netname(int mid)
276 register struct netmach *np;
278 if (mid & 0200)
279 return(mlook(mid));
280 for (np = netmach; np->nt_mid != 0; np++)
281 if (np->nt_mid == mid)
282 return(np->nt_machine);
283 return(NOSTR);
285 #endif
288 * Deal with arpa net addresses. The way this is done is strange.
289 * name contains an "@" or "%". Look up the machine after it in
290 * the hash table. If it isn't found, return name unmolested.
291 * If ???, return name unmolested.
292 * Otherwise, delete the "@" or "%" and the machine after it from
293 * name, and return the new string.
295 static char *
296 arpafix(char name[], char from[])
298 register char *cp;
299 register int arpamach;
300 char newname[BUFSIZ];
302 if (debug) {
303 fprintf(stderr, "arpafix(%s, %s)\n", name, from);
305 cp = strrchr(name, '@');
306 if (cp == NOSTR)
307 cp = strrchr(name, '%');
308 if (cp == NOSTR) {
309 fprintf(stderr,
310 gettext("Something's amiss -- no @ or %% in arpafix\n"));
311 return(name);
313 cp++;
314 arpamach = netlook(cp, '@');
315 if (debug)
316 fprintf(stderr,
317 "cp '%s', arpamach %o, nettypes arpamach %o LOCAL %o\n",
318 cp, arpamach, nettype(arpamach), nettype(LOCAL));
319 if (arpamach == 0) {
320 if (debug)
321 fprintf(stderr, "machine %s unknown, uses: %s\n",
322 cp, name);
323 return(name);
325 if (((nettype(arpamach) & nettype(LOCAL)) & ~AN) == 0) {
326 if (debug)
327 fprintf(stderr, "machine %s known but remote, uses: %s\n",
328 cp, name);
329 return(name);
331 nstrcpy(newname, sizeof (newname), name);
332 cp = strrchr(newname, '@');
333 if (cp == NOSTR)
334 cp = strrchr(newname, '%');
335 *cp = 0;
336 if (debug) fprintf(stderr, "local address, return '%s'\n", newname);
337 return(savestr(newname));
341 * We have name with no @'s in it, and from with @'s.
342 * Assume that name is meaningful only on the site in from,
343 * and return "name@site_in_from".
345 static char *
346 makeremote(char name[], char from[])
348 register char *cp;
349 char rbuf[BUFSIZ];
351 if (!value("makeremote"))
352 return(name);
353 if (debug) fprintf(stderr, "makeremote(%s, %s) returns ", name, from);
354 cp = strrchr(from, '@');
355 if (cp == NOSTR)
356 cp = strrchr(from, '%');
357 snprintf(rbuf, sizeof (rbuf), "%s%s", name, cp);
358 if (debug) fprintf(stderr, "%s\n", rbuf);
359 return(savestr(rbuf));
363 * Take a network machine descriptor and find the types of connected
364 * nets and return it.
366 static int
367 nettype(int mid)
369 register struct netmach *np;
371 if (mid & 0200)
372 return(mtype(mid));
373 for (np = netmach; np->nt_mid != 0; np++)
374 if (np->nt_mid == mid)
375 return(np->nt_type);
376 return(0);
380 * Hashing routines to salt away machines seen scanning
381 * networks paths that we don't know about.
384 #define XHSIZE 97 /* Size of extra hash table */
385 #define NXMID (XHSIZE*3/4) /* Max extra machines */
387 struct xtrahash {
388 char *xh_name; /* Name of machine */
389 short xh_mid; /* Machine ID */
390 short xh_attnet; /* Attached networks */
391 } xtrahash[XHSIZE];
393 static struct xtrahash *xtab[XHSIZE]; /* F: mid-->machine name */
395 static short midfree; /* Next free machine id */
398 * Initialize the extra host hash table.
399 * Called by sreset.
401 void
402 minit(void)
404 register struct xtrahash *xp, **tp;
406 midfree = 0;
407 tp = &xtab[0];
408 for (xp = &xtrahash[0]; xp < &xtrahash[XHSIZE]; xp++) {
409 xp->xh_name = NOSTR;
410 xp->xh_mid = 0;
411 xp->xh_attnet = 0;
412 *tp++ = NULL;
417 * Stash a net name in the extra host hash table.
418 * If a new entry is put in the hash table, deduce what
419 * net the machine is attached to from the net character.
421 * If the machine is already known, add the given attached
422 * net to those already known.
424 static int
425 mstash(char name[], int attnet)
427 register struct xtrahash *xp;
428 int x;
430 xp = xlocate(name);
431 if (xp == NULL) {
432 printf(gettext("Ran out of machine id spots\n"));
433 return(0);
435 if (xp->xh_name == NOSTR) {
436 if (midfree >= XHSIZE) {
437 printf(gettext("Out of machine ids\n"));
438 return(0);
440 xtab[midfree] = xp;
441 xp->xh_name = savestr(name);
442 xp->xh_mid = 0200 + midfree++;
444 x = ntype(attnet);
445 if (x == 0)
446 xp->xh_attnet |= AN;
447 else
448 xp->xh_attnet |= x;
449 return(xp->xh_mid);
453 * Search for the given name in the hash table
454 * and return the pointer to it if found, or to the first
455 * empty slot if not found.
457 * If no free slots can be found, return 0.
460 static struct xtrahash *
461 xlocate(char name[])
463 register int h, q, i;
464 register char *cp;
465 register struct xtrahash *xp;
467 for (h = 0, cp = name; *cp; h = (h << 2) + *cp++)
469 if (h < 0 && (h = -h) < 0)
470 h = 0;
471 h = h % XHSIZE;
472 cp = name;
473 for (i = 0, q = 0; q < XHSIZE; i++, q = i * i) {
474 xp = &xtrahash[(h + q) % XHSIZE];
475 if (xp->xh_name == NOSTR)
476 return(xp);
477 if (strcmp(cp, xp->xh_name) == 0)
478 return(xp);
479 if (h - q < 0)
480 h += XHSIZE;
481 xp = &xtrahash[(h - q) % XHSIZE];
482 if (xp->xh_name == NOSTR)
483 return(xp);
484 if (strcmp(cp, xp->xh_name) == 0)
485 return(xp);
487 return(NULL);
490 #ifdef OPTIM
492 * Return the name from the extra host hash table corresponding
493 * to the passed machine id.
496 static char *
497 mlook(int mid)
499 register int m;
501 if ((mid & 0200) == 0)
502 return(NOSTR);
503 m = mid & 0177;
504 if (m >= midfree) {
505 printf(gettext("Use made of undefined machine id\n"));
506 return(NOSTR);
508 return(xtab[m]->xh_name);
510 #endif
513 * Return the bit mask of net's that the given extra host machine
514 * id has so far.
516 static int
517 mtype(int mid)
519 register int m;
521 if ((mid & 0200) == 0)
522 return(0);
523 m = mid & 0177;
524 if (m >= midfree) {
525 printf(gettext("Use made of undefined machine id\n"));
526 return(0);
528 return(xtab[m]->xh_attnet);
531 #ifdef OPTIM
533 * Take a network name and optimize it. This gloriously messy
534 * operation takes place as follows: the name with machine names
535 * in it is tokenized by mapping each machine name into a single
536 * character machine id (netlook). The separator characters (network
537 * metacharacters) are left intact. The last component of the network
538 * name is stripped off and assumed to be the destination user name --
539 * it does not participate in the optimization. As an example, the
540 * name "res!vax!res!uvax!bill" becomes, tokenized,
541 * "r!x!r!v!" and "bill" A low level routine, optim1, fixes up the
542 * network part (eg, "r!x!r!v!"), then we convert back to network
543 * machine names and tack the user name on the end.
545 * The result of this is copied into the parameter "name"
548 static void
549 optim(char net[], char name[])
551 char netcomp[BUFSIZ], netstr[STSIZ], xfstr[STSIZ];
552 register char *cp, *cp2;
553 register int c;
555 if (debug) fprintf(stderr, "optim(%s, %s) called\n", net, name);
556 *netstr = '\0';
557 cp = net;
558 for (;;) {
560 * Rip off next path component into netcomp
562 cp2 = netcomp;
563 while (*cp && !any(*cp, metanet))
564 *cp2++ = *cp++;
565 *cp2 = 0;
567 * If we hit null byte, then we just scanned
568 * the destination user name. Go off and optimize
569 * if its so.
571 if (*cp == 0)
572 break;
573 if ((c = netlook(netcomp, *cp)) == 0) {
574 printf(gettext("No host named \"%s\"\n"), netcomp);
575 err:
576 nstrcpy(name, BUFSIZ, net);
577 return;
579 stradd(name, BUFSIZ, c);
580 stradd(name, BUFSIZ, *cp++);
582 * If multiple network separators given,
583 * throw away the extras.
585 while (any(*cp, metanet))
586 cp++;
588 if (strlen(netcomp) == 0) {
589 printf(gettext("net name syntax\n"));
590 goto err;
592 if (debug) fprintf(stderr, "optim1(%s,%s) called\n", netstr, xfstr);
593 optim1(netstr, xfstr);
594 if (debug) fprintf(stderr, "optim1(%s,%s) returns\n", netstr, xfstr);
597 * Convert back to machine names.
600 cp = xfstr;
601 *name = '\0';
602 while (*cp) {
603 if ((cp2 = netname(*cp++)) == NOSTR) {
604 printf(gettext("Made up bad net name\n"));
605 printf(gettext("Machine code %c (0%o)\n"), cp[-1],
606 cp[-1]);
607 printf(gettext("Sorry.\n"));
608 goto err;
610 nstrcat(name, BUFSIZ, cp2);
611 stradd(name, BUFSIZ, *cp++);
613 nstrcat(name, BUFSIZ, netcomp);
614 if (debug) fprintf(stderr, "optim returns %s in name\n", name);
618 * Take a string of network machine id's and separators and
619 * optimize them. We process these by pulling off maximal
620 * leading strings of the same type, passing these to the appropriate
621 * optimizer and concatenating the results.
624 static void
625 optim1(char netstr[], char name[])
627 char path[STSIZ], rpath[STSIZ];
628 register char *cp, *cp2;
629 register int tp, nc;
631 cp = netstr;
632 prefer(cp);
633 *name = '\0';
635 * If the address ultimately points back to us,
636 * just return a null network path.
638 if ((int)strlen(cp) > 1 && cp[strlen(cp) - 2] == LOCAL)
639 return;
640 while (*cp != 0) {
641 *path = '\0';
643 tp = ntype(cp[1]);
644 nc = cp[1];
645 while (*cp && tp == ntype(cp[1])) {
646 stradd(path, sizeof (path), *cp++);
647 cp++;
649 switch (netkind(tp)) {
650 default:
651 nstrcpy(rpath, sizeof (rpath), path);
652 break;
654 case IMPLICIT:
655 optimimp(path, rpath);
656 break;
658 case EXPLICIT:
659 optimex(path, rpath);
660 break;
662 for (cp2 = rpath; *cp2 != 0; cp2++) {
663 stradd(name, BUFSIZ, *cp2);
664 stradd(name, BUFSIZ, nc);
667 optiboth(name);
668 prefer(name);
670 #endif /* OPTIM */
673 * Return the network of the separator --
674 * AN for arpa net
675 * BN for Bell labs net (e.g. UUCP, NOT Berknet)
676 * SN for Schmidt net (Berknet)
677 * 0 if we don't know.
679 static int
680 ntype(register int nc)
682 register struct ntypetab *np;
684 for (np = ntypetab; np->nt_char != 0; np++)
685 if (np->nt_char == nc)
686 return(np->nt_bcode);
687 return(0);
690 #ifdef OPTIM
692 * Return the kind of routing used for the particular net
693 * EXPLICIT means explicitly routed
694 * IMPLICIT means implicitly routed
695 * 0 means don't know
698 static int
699 netkind(register int nt)
701 register struct nkindtab *np;
703 for (np = nkindtab; np->nk_type != 0; np++)
704 if (np->nk_type == nt)
705 return(np->nk_kind);
706 return(0);
710 * Do name optimization for an explicitly routed network (eg uucp).
713 static int
714 optimex(char net[], char name[])
716 register char *cp, *rp;
717 register int m;
719 nstrcpy(name, STSIZ, net);
720 cp = name;
721 if (strlen(cp) == 0)
722 return(-1);
723 if (cp[strlen(cp)-1] == LOCAL) {
724 name[0] = 0;
725 return(0);
727 for (cp = name; *cp; cp++) {
728 m = *cp;
729 rp = strrchr(cp+1, m);
730 if (rp != NOSTR)
731 strcpy(cp, rp);
733 return(0);
737 * Do name optimization for implicitly routed network (eg, arpanet).
740 static int
741 optimimp(char net[], char name[])
743 register char *cp;
744 register char m;
746 cp = net;
747 if (strlen(cp) == 0)
748 return(-1);
749 m = cp[strlen(cp) - 1];
750 if (m == LOCAL) {
751 *name = '\0';
752 return(0);
754 name[0] = m;
755 name[1] = 0;
756 return(0);
760 * Perform global optimization on the given network path.
761 * The trick here is to look ahead to see if there are any loops
762 * in the path and remove them. The interpretation of loops is
763 * more strict here than in optimex since both the machine and net
764 * type must match.
767 static void
768 optiboth(char net[])
770 register char *cp, *cp2;
772 cp = net;
773 if (strlen(cp) == 0)
774 return;
775 if (((int)strlen(cp) % 2) != 0) {
776 printf(gettext("Strange arg to optiboth\n"));
777 return;
779 while (*cp) {
780 cp2 = rpair(cp+2, *cp);
781 if (cp2 != NOSTR)
782 strcpy(cp, cp2);
783 cp += 2;
788 * Find the rightmost instance of the given (machine, type) pair.
791 static char *
792 rpair(char str[], int mach)
794 register char *cp, *last;
796 cp = str;
797 last = NOSTR;
798 while (*cp) {
799 if (*cp == mach)
800 last = cp;
801 cp += 2;
803 return(last);
807 * Change the network separators in the given network path
808 * to the preferred network transmission means.
811 static void
812 prefer(char name[])
814 register char *cp, n;
815 register int state;
817 state = LOCAL;
818 for (cp = name; *cp; cp += 2) {
819 n = best(state, *cp);
820 if (n)
821 cp[1] = n;
822 state = *cp;
827 * Return the best network separator for the given machine pair.
830 static char
831 best(int src, int dest)
833 register int dtype, stype;
834 register struct netorder *np;
836 stype = nettype(src);
837 dtype = nettype(dest);
838 fflush(stdout);
839 if (stype == 0 || dtype == 0) {
840 printf(gettext("ERROR: unknown internal machine id\n"));
841 return(0);
843 if ((stype & dtype) == 0)
844 return(0);
845 np = &netorder[0];
846 while ((np->no_stat & stype & dtype) == 0)
847 np++;
848 return(np->no_char);
850 #endif /* OPTIM */
852 #ifdef notdef
854 * Code to twist around arpa net names.
857 #define WORD 257 /* Token for a string */
859 static char netbuf[256];
860 static char *yylval;
863 * Reverse all of the arpa net addresses in the given name to
864 * be of the form "host @ user" instead of "user @ host"
865 * This function is its own inverse.
868 char *
869 revarpa(char str[])
872 if (yyinit(str) < 0)
873 return(NOSTR);
874 if (name())
875 return(NOSTR);
876 if (strcmp(str, netbuf) == 0)
877 return(str);
878 return(savestr(netbuf));
882 * Parse (by recursive descent) network names, using the following grammar:
883 * name:
884 * term {':' term}
885 * term {'^' term}
886 * term {'!' term}
887 * term '@' name
888 * term '%' name
890 * term:
891 * string of characters.
894 static int
895 name(void)
897 register int t;
898 register char *cp;
900 for (;;) {
901 t = yylex();
902 if (t != WORD)
903 return(-1);
904 cp = yylval;
905 t = yylex();
906 switch (t) {
907 case 0:
908 nstrcat(netbuf, sizeof (netbuf), cp);
909 return(0);
911 case '@':
912 case '%':
913 if (name())
914 return(-1);
915 stradd(netbuf, sizeof (netbuf), '@');
916 nstrcat(netbuf, sizeof (netbuf), cp);
917 return(0);
918 case WORD:
919 return(-1);
921 default:
922 nstrcat(netbuf, sizeof (netbuf), cp);
923 stradd(netbuf, sizeof (netbuf), t);
929 * Scanner for network names.
932 static char *charp; /* Current input pointer */
933 static int nexttok; /* Salted away next token */
936 * Initialize the network name scanner.
939 int
940 yyinit(char str[])
942 static char lexbuf[BUFSIZ];
944 netbuf[0] = 0;
945 if (strlen(str) >= sizeof lexbuf - 1)
946 return(-1);
947 nexttok = 0;
948 nstrcpy(lexbuf, sizeof (lexbuf), str);
949 charp = lexbuf;
950 return(0);
954 * Scan and return a single token.
955 * yylval is set to point to a scanned string.
958 int
959 yylex(void)
961 register char *cp, *dotp;
962 register int s;
964 if (nexttok) {
965 s = nexttok;
966 nexttok = 0;
967 return(s);
969 cp = charp;
970 while (*cp && isspace(*cp))
971 cp++;
972 if (*cp == 0)
973 return(0);
974 if (any(*cp, metanet)) {
975 charp = cp+1;
976 return(*cp);
978 dotp = cp;
979 while (*cp && !any(*cp, metanet) && !any(*cp, " \t"))
980 cp++;
981 if (any(*cp, metanet))
982 nexttok = *cp;
983 if (*cp == 0)
984 charp = cp;
985 else
986 charp = cp+1;
987 *cp = 0;
988 yylval = dotp;
989 return(WORD);
991 #endif
994 * Add a single character onto a string. Here dstsize is the size of the
995 * destnation buffer.
998 static void
999 stradd(register char *dst, int dstsize, register int c)
1001 while (*dst != '\0') {
1002 dst++;
1003 dstsize--;
1005 if (--dstsize > 0)
1006 *dst++ = (char)c;
1007 *dst = '\0';