xref: /original-bsd/old/htable/htable.c (revision 3aaceb89)
1 #ifndef lint
2 static char sccsid[] = "@(#)htable.c	4.1 (Berkeley) 10/20/82";
3 #endif
4 
5 /*
6  * htable - convert NIC host table into a UNIX format.
7  * NIC format is described in RFC 810, 1 March 1982.
8  */
9 #include <stdio.h>
10 #include <ctype.h>
11 #include "htable.h"
12 
13 FILE *hf;
14 FILE *gf;
15 
16 main(argc, argv)
17 	int argc;
18 	char *argv[];
19 {
20 	if (argc > 2) {
21 		fprintf(stderr, "usage: %s [ input-file ]\n",
22 			argv[0]);
23 		exit(1);
24 	}
25 	infile = "(stdin)";
26 	if (argc == 2) {
27 		infile = argv[1];
28 		if (freopen(infile, "r", stdin) == NULL) {
29 			perror(infile);
30 			exit(1);
31 		}
32 	}
33 	hf = fopen("hosts", "w");
34 	if (hf == NULL) {
35 		perror("hosts");
36 		exit(1);
37 	}
38 	copylocal();
39 #ifdef GATEWAYS
40 	hf = fopen("gateways", "w");
41 	if (hf == NULL) {
42 		perror("gateways");
43 		exit(1);
44 	}
45 #endif
46 	exit(yyparse());
47 }
48 
49 struct name *
50 newname(str)
51 	char *str;
52 {
53 	char *p;
54 	struct name *nm;
55 
56 	p = malloc(strlen(str) + 1);
57 	strcpy(p, str);
58 	nm = alloc_name();
59 	nm->name_val = p;
60 	nm->name_link = NONAME;
61 	return (nm);
62 }
63 
64 char *
65 lower(str)
66 	char *str;
67 {
68 	register char *cp = str;
69 
70 	while (*cp) {
71 		if (isupper(*cp))
72 			*cp = tolower(*cp);
73 		cp++;
74 	}
75 	return (str);
76 }
77 
78 do_entry(keyword, addrlist, namelist, cputype, opsys, protos)
79 	int keyword;
80 	struct addr *addrlist;
81 	struct name *namelist, *cputype, *opsys, *protos;
82 {
83 	register struct addr *al, *al2;
84 	register struct name *nl, *nl2;
85 	register flag;
86 
87 	switch (keyword) {
88 
89 	case KW_NET:
90 		break;
91 
92 	case KW_GATEWAY:
93 		break;
94 
95 	case KW_HOST:
96 		for (al = addrlist; al; al = al2) {
97 			if (net(al->addr_val) != LOCALNET) {
98 				fprintf(hf, "%d.%d.%d.%d\t",
99 					net(al->addr_val), host(al->addr_val),
100 					lhost(al->addr_val), imp(al->addr_val));
101 				for (nl = namelist; nl; nl = nl->name_link)
102 					fprintf(hf, " %s", lower(nl->name_val));
103 				putc('\n', hf);
104 			}
105 			al2 = al->addr_link;
106 			free_addr(al);
107 		}
108 		break;
109 
110 	default:
111 		fprintf(stderr, "Unknown keyword: %d.\n", keyword);
112 	}
113 	for (nl = namelist; nl; nl = nl2) {
114 		nl2 = nl->name_link;
115 		free_name(nl);
116 	}
117 	for (nl = protos; nl; nl = nl2) {
118 		nl2 = nl->name_link;
119 		free_name(nl);
120 	}
121 }
122 
123 copylocal()
124 {
125 	register FILE *lhf;
126 	register cc;
127 	char buf[BUFSIZ];
128 
129 	lhf = fopen("localhosts", "r");
130 	if (lhf == NULL) {
131 		perror("localhosts");
132 		fprintf(stderr, "(continuing)\n");
133 		return;
134 	}
135 	while (cc = fread(buf, 1, sizeof(buf), lhf))
136 		fwrite(buf, 1, cc, hf);
137 	fclose(lhf);
138 }
139