annotate ueda/libunet/unetrd.c @ 18:52000ae7a6cf

ueda/libunet/unetrd.c: close the stdio stream on EOF
author Space Falcon <falcon@ivan.Harhan.ORG>
date Sun, 02 Aug 2015 01:23:09 +0000
parents faeb83c43f1c
children dda8e455c863
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
9
faeb83c43f1c libunet started
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
1 /*
faeb83c43f1c libunet started
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
2 * This module provides library functions for reading and parsing
faeb83c43f1c libunet started
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
3 * netlist files in our unet format.
faeb83c43f1c libunet started
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
4 */
faeb83c43f1c libunet started
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
5
faeb83c43f1c libunet started
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
6 #include <stdio.h>
faeb83c43f1c libunet started
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
7 #include <stdlib.h>
faeb83c43f1c libunet started
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
8 #include <ctype.h>
faeb83c43f1c libunet started
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
9 #include <string.h>
faeb83c43f1c libunet started
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
10 #include <strings.h>
faeb83c43f1c libunet started
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
11 #include "unetrd.h"
faeb83c43f1c libunet started
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
12
faeb83c43f1c libunet started
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
13 open_unet_input_file(filename, state)
faeb83c43f1c libunet started
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
14 char *filename;
faeb83c43f1c libunet started
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
15 struct unetrd_state *state;
faeb83c43f1c libunet started
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
16 {
faeb83c43f1c libunet started
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
17 state->filename = filename;
faeb83c43f1c libunet started
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
18 state->stream = fopen(filename, "r");
faeb83c43f1c libunet started
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
19 if (!state->stream) {
faeb83c43f1c libunet started
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
20 perror(filename);
faeb83c43f1c libunet started
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
21 exit(1);
faeb83c43f1c libunet started
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
22 }
faeb83c43f1c libunet started
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
23 state->lineno = 0;
faeb83c43f1c libunet started
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
24 }
faeb83c43f1c libunet started
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
25
faeb83c43f1c libunet started
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
26 static void
faeb83c43f1c libunet started
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
27 handle_name_only(state, out, rest)
faeb83c43f1c libunet started
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
28 struct unetrd_state *state;
faeb83c43f1c libunet started
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
29 struct unetrd_out *out;
faeb83c43f1c libunet started
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
30 char *rest;
faeb83c43f1c libunet started
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
31 {
faeb83c43f1c libunet started
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
32 char *cp;
faeb83c43f1c libunet started
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
33
faeb83c43f1c libunet started
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
34 for (cp = rest; isspace(cp); cp++)
faeb83c43f1c libunet started
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
35 ;
faeb83c43f1c libunet started
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
36 if (*cp == '\0' || *cp == '#') {
faeb83c43f1c libunet started
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
37 fprintf(stderr, "%s line %d: a name is expected after %s\n",
faeb83c43f1c libunet started
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
38 state->filename, state->lineno, out->keyword);
faeb83c43f1c libunet started
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
39 exit(1);
faeb83c43f1c libunet started
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
40 }
faeb83c43f1c libunet started
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
41 out->objname = cp;
faeb83c43f1c libunet started
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
42 while (*cp && !isspace(*cp))
faeb83c43f1c libunet started
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
43 cp++;
faeb83c43f1c libunet started
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
44 if (*cp)
faeb83c43f1c libunet started
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
45 *cp++ = '\0';
faeb83c43f1c libunet started
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
46 while (isspace(*cp))
faeb83c43f1c libunet started
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
47 cp++;
faeb83c43f1c libunet started
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
48 if (*cp && *cp != '#') {
faeb83c43f1c libunet started
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
49 fprintf(stderr,
faeb83c43f1c libunet started
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
50 "%s line %d: unexpected extra fields on %s line\n",
faeb83c43f1c libunet started
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
51 state->filename, state->lineno, out->keyword);
faeb83c43f1c libunet started
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
52 exit(1);
faeb83c43f1c libunet started
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
53 }
faeb83c43f1c libunet started
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
54 }
faeb83c43f1c libunet started
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
55
faeb83c43f1c libunet started
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
56 static void
faeb83c43f1c libunet started
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
57 handle_component_opening(state, out, rest)
faeb83c43f1c libunet started
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
58 struct unetrd_state *state;
faeb83c43f1c libunet started
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
59 struct unetrd_out *out;
faeb83c43f1c libunet started
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
60 char *rest;
faeb83c43f1c libunet started
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
61 {
faeb83c43f1c libunet started
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
62 char *cp;
faeb83c43f1c libunet started
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
63
faeb83c43f1c libunet started
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
64 for (cp = rest; isspace(cp); cp++)
faeb83c43f1c libunet started
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
65 ;
faeb83c43f1c libunet started
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
66 if (*cp == '\0' || *cp == '#') {
faeb83c43f1c libunet started
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
67 fprintf(stderr, "%s line %d: a name is expected after %s\n",
faeb83c43f1c libunet started
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
68 state->filename, state->lineno, out->keyword);
faeb83c43f1c libunet started
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
69 exit(1);
faeb83c43f1c libunet started
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
70 }
faeb83c43f1c libunet started
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
71 out->objname = cp;
faeb83c43f1c libunet started
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
72 while (*cp && !isspace(*cp))
faeb83c43f1c libunet started
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
73 cp++;
faeb83c43f1c libunet started
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
74 if (*cp)
faeb83c43f1c libunet started
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
75 *cp++ = '\0';
faeb83c43f1c libunet started
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
76 while (isspace(*cp))
faeb83c43f1c libunet started
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
77 cp++;
faeb83c43f1c libunet started
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
78 if (*cp != '{') {
faeb83c43f1c libunet started
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
79 fprintf(stderr,
faeb83c43f1c libunet started
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
80 "%s line %d: expected '{' at the end of %s line\n",
faeb83c43f1c libunet started
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
81 state->filename, state->lineno, out->keyword);
faeb83c43f1c libunet started
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
82 exit(1);
faeb83c43f1c libunet started
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
83 }
faeb83c43f1c libunet started
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
84 }
faeb83c43f1c libunet started
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
85
faeb83c43f1c libunet started
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
86 static void
faeb83c43f1c libunet started
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
87 handle_pin_line(state, out, rest)
faeb83c43f1c libunet started
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
88 struct unetrd_state *state;
faeb83c43f1c libunet started
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
89 struct unetrd_out *out;
faeb83c43f1c libunet started
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
90 char *rest;
faeb83c43f1c libunet started
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
91 {
faeb83c43f1c libunet started
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
92 char *cp = rest, *fields[3];
faeb83c43f1c libunet started
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
93 int i;
faeb83c43f1c libunet started
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
94
faeb83c43f1c libunet started
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
95 for (i = 0; i < 3; i++) {
faeb83c43f1c libunet started
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
96 while (isspace(*cp))
faeb83c43f1c libunet started
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
97 cp++;
faeb83c43f1c libunet started
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
98 if (*cp == '\0' || *cp == '#') {
faeb83c43f1c libunet started
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
99 error: fprintf(stderr,
faeb83c43f1c libunet started
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
100 "%s line %d: invalid syntax on %s line\n",
faeb83c43f1c libunet started
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
101 state->filename, state->lineno, out->keyword);
faeb83c43f1c libunet started
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
102 exit(1);
faeb83c43f1c libunet started
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
103 }
faeb83c43f1c libunet started
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
104 fields[i] = cp;
faeb83c43f1c libunet started
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
105 while (*cp && !isspace(*cp))
faeb83c43f1c libunet started
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
106 cp++;
faeb83c43f1c libunet started
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
107 if (*cp)
faeb83c43f1c libunet started
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
108 *cp++ = '\0';
faeb83c43f1c libunet started
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
109 }
faeb83c43f1c libunet started
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
110 out->objname = fields[0];
faeb83c43f1c libunet started
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
111 if (strcmp(fields[1], "="))
faeb83c43f1c libunet started
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
112 goto error;
faeb83c43f1c libunet started
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
113 if (!strcmp(fields[2], "NET")) {
faeb83c43f1c libunet started
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
114 while (isspace(*cp))
faeb83c43f1c libunet started
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
115 cp++;
faeb83c43f1c libunet started
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
116 if (*cp == '\0' || *cp == '#')
faeb83c43f1c libunet started
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
117 goto error;
faeb83c43f1c libunet started
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
118 out->connect_to_net = cp;
faeb83c43f1c libunet started
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
119 while (*cp && !isspace(*cp))
faeb83c43f1c libunet started
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
120 cp++;
faeb83c43f1c libunet started
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
121 if (*cp)
faeb83c43f1c libunet started
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
122 *cp++ = '\0';
faeb83c43f1c libunet started
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
123 } else if (!strcmp(fields[2], "NC")) {
faeb83c43f1c libunet started
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
124 out->connect_to_net = 0;
faeb83c43f1c libunet started
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
125 while (isspace(*cp))
faeb83c43f1c libunet started
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
126 cp++;
faeb83c43f1c libunet started
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
127 if (*cp++ != '(')
faeb83c43f1c libunet started
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
128 goto error;
faeb83c43f1c libunet started
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
129 out->nc_comment = cp;
faeb83c43f1c libunet started
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
130 while (*cp && *cp != ')')
faeb83c43f1c libunet started
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
131 cp++;
faeb83c43f1c libunet started
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
132 if (*cp != ')')
faeb83c43f1c libunet started
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
133 goto error;
faeb83c43f1c libunet started
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
134 *cp++ = '\0';
faeb83c43f1c libunet started
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
135 } else
faeb83c43f1c libunet started
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
136 goto error;
faeb83c43f1c libunet started
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
137 while (isspace(*cp))
faeb83c43f1c libunet started
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
138 cp++;
faeb83c43f1c libunet started
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
139 if (*cp && *cp != '#') {
faeb83c43f1c libunet started
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
140 fprintf(stderr,
faeb83c43f1c libunet started
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
141 "%s line %d: unexpected extra fields on %s line\n",
faeb83c43f1c libunet started
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
142 state->filename, state->lineno, out->keyword);
faeb83c43f1c libunet started
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
143 exit(1);
faeb83c43f1c libunet started
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
144 }
faeb83c43f1c libunet started
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
145 }
faeb83c43f1c libunet started
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
146
faeb83c43f1c libunet started
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
147 static struct objmap {
faeb83c43f1c libunet started
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
148 char *keyword;
faeb83c43f1c libunet started
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
149 int typecode;
faeb83c43f1c libunet started
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
150 void (*handler)();
faeb83c43f1c libunet started
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
151 } objmap[] = {
faeb83c43f1c libunet started
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
152 {"}", UNETOBJ_CLOSINGBRACE, 0},
faeb83c43f1c libunet started
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
153 {"NET", UNETOBJ_NET, handle_name_only},
faeb83c43f1c libunet started
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
154 {"COMPONENT", UNETOBJ_COMPONENT, handle_component_opening},
faeb83c43f1c libunet started
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
155 {"STARPOINT", UNETOBJ_STARPOINT, handle_component_opening},
faeb83c43f1c libunet started
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
156 {"PRIMITIVE", UNETOBJ_PRIMITIVE, handle_name_only},
faeb83c43f1c libunet started
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
157 {"ALTNAME", UNETOBJ_ALTNAME, handle_name_only},
faeb83c43f1c libunet started
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
158 {"PIN", UNETOBJ_PIN, handle_pin_line},
faeb83c43f1c libunet started
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
159 {"PINMAP", UNETOBJ_PINMAP, handle_pin_line},
faeb83c43f1c libunet started
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
160 {0, 0, 0}
faeb83c43f1c libunet started
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
161 };
faeb83c43f1c libunet started
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
162
faeb83c43f1c libunet started
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
163 read_unet_line(state, out)
faeb83c43f1c libunet started
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
164 struct unetrd_state *state;
faeb83c43f1c libunet started
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
165 struct unetrd_out *out;
faeb83c43f1c libunet started
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
166 {
faeb83c43f1c libunet started
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
167 static char linebuf[256];
faeb83c43f1c libunet started
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
168 char *cp;
faeb83c43f1c libunet started
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
169 struct objmap *tp;
faeb83c43f1c libunet started
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
170
faeb83c43f1c libunet started
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
171 /* read lines until we get a non-empty, non-comment line or EOF */
faeb83c43f1c libunet started
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
172 for (;;) {
18
52000ae7a6cf ueda/libunet/unetrd.c: close the stdio stream on EOF
Space Falcon <falcon@ivan.Harhan.ORG>
parents: 9
diff changeset
173 if (!fgets(linebuf, sizeof linebuf, state->stream)) {
52000ae7a6cf ueda/libunet/unetrd.c: close the stdio stream on EOF
Space Falcon <falcon@ivan.Harhan.ORG>
parents: 9
diff changeset
174 fclose(state->stream);
9
faeb83c43f1c libunet started
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
175 return(0);
18
52000ae7a6cf ueda/libunet/unetrd.c: close the stdio stream on EOF
Space Falcon <falcon@ivan.Harhan.ORG>
parents: 9
diff changeset
176 }
9
faeb83c43f1c libunet started
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
177 state->lineno++;
faeb83c43f1c libunet started
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
178 cp = index(linebuf, '\n');
faeb83c43f1c libunet started
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
179 if (!cp) {
faeb83c43f1c libunet started
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
180 fprintf(stderr,
faeb83c43f1c libunet started
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
181 "error: %s line %d is too long or unterminated\n",
faeb83c43f1c libunet started
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
182 state->filename, state->lineno);
faeb83c43f1c libunet started
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
183 exit(1);
faeb83c43f1c libunet started
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
184 }
faeb83c43f1c libunet started
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
185 *cp = '\0';
faeb83c43f1c libunet started
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
186 for (cp = linebuf; isspace(*cp); cp++)
faeb83c43f1c libunet started
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
187 ;
faeb83c43f1c libunet started
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
188 if (*cp && *cp != '#')
faeb83c43f1c libunet started
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
189 break;
faeb83c43f1c libunet started
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
190 }
faeb83c43f1c libunet started
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
191 out->keyword = cp;
faeb83c43f1c libunet started
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
192 while (*cp && !isspace(*cp))
faeb83c43f1c libunet started
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
193 cp++;
faeb83c43f1c libunet started
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
194 if (*cp)
faeb83c43f1c libunet started
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
195 *cp++ = '\0';
faeb83c43f1c libunet started
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
196 for (tp = objmap; tp->keyword; tp++)
faeb83c43f1c libunet started
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
197 if (!strcmp(tp->keyword, out->keyword))
faeb83c43f1c libunet started
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
198 break;
faeb83c43f1c libunet started
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
199 if (!tp->keyword) {
faeb83c43f1c libunet started
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
200 fprintf(stderr, "%s line %d: object type \"%s\" unknown\n",
faeb83c43f1c libunet started
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
201 state->filename, state->lineno, out->keyword);
faeb83c43f1c libunet started
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
202 exit(1);
faeb83c43f1c libunet started
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
203 }
faeb83c43f1c libunet started
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
204 out->typecode = tp->typecode;
faeb83c43f1c libunet started
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
205 if (tp->handler)
faeb83c43f1c libunet started
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
206 tp->handler(state, out, cp);
faeb83c43f1c libunet started
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
207 return(1);
faeb83c43f1c libunet started
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
208 }