1: # include <stdio.h>
2: # include <ctype.h>
3:
4: # define reg register
5: # define bool char
6:
7: # define TRUE 1
8: # define FALSE 0
9:
10: static char *sccsid ="@(#)addr.c 1.8 (Berkeley) 4/10/81";
11:
12: struct fd {
13: char *f_name;
14: char *f_desc;
15: char *f_value;
16: };
17:
18: typedef struct fd FDES;
19:
20: char *Fmtfile,
21: *Addrfile,
22: Fmt[BUFSIZ],
23: *malloc();
24:
25: FDES Fmtab[BUFSIZ],
26: *name();
27:
28: main(ac, av)
29: int ac;
30: char **av;
31: {
32: setbuf(stdout, 0);
33: if (ac != 3) {
34: printf("usage: %s fmt-file addr-file\n", av[0]);
35: exit(1);
36: }
37: Fmtfile = av[1];
38: Addrfile = av[2];
39: parsefmt();
40: doaddrs();
41: }
42:
43: /*
44: * parse the fmt file
45: */
46: parsefmt()
47: {
48: reg FILE *inf;
49: reg char *sp, *fmt;
50: reg FDES *fp;
51: reg bool inquest, not;
52: char buf[80];
53:
54: if ((inf = fopen(Fmtfile, "r")) == NULL) {
55: perror(Fmtfile);
56: exit(1);
57: }
58: for (fp = Fmtab; fp < &Fmtab[BUFSIZ]; fp++)
59: fp->f_name = NULL;
60:
61: inquest = FALSE;
62: for (fmt = Fmt; (*fmt = getc(inf)) != EOF; fmt++)
63: if (*fmt == '<' || *fmt == '?') {
64: register char c;
65:
66: if (inquest && *fmt == '?') {
67: inquest = FALSE;
68: if ((c = getc(inf)) == '\n') {
69: *fmt++ = '\n';
70: *fmt = '?';
71: }
72: else
73: ungetc(c, inf);
74: continue;
75: }
76: if (*fmt == '?')
77: if ((c = getc(inf)) == '!')
78: *fmt = '!';
79: else
80: ungetc(c, inf);
81: sp = buf;
82: while ((*sp = getc(inf)) != '>' && *sp != ':')
83: sp++;
84: c = *sp;
85: *sp++ = '\0';
86: fp = name(buf);
87: fp->f_name = malloc(sp - buf);
88: strcpy(fp->f_name, buf);
89: if (c == ':' && *fmt == '<') {
90: for (sp = buf; (*sp = getc(inf)) != '>'; sp++)
91: continue;
92: *sp++ = 0;
93: fp->f_desc = malloc(sp - buf);
94: strcpy(fp->f_desc, buf);
95: }
96: else if (*fmt == '?' || *fmt == '!')
97: inquest = TRUE;
98: else
99: fp->f_desc = "";
100: *++fmt = fp - Fmtab;
101: }
102: fclose(inf);
103: *fmt = '\0';
104: # ifdef DEBUG
105: printf("---\n");
106: inquest = FALSE;
107: for (fmt = Fmt; *fmt; fmt++) {
108: putchar(*fmt);
109: if (*fmt == '?' || *fmt == '!') {
110: if (!inquest)
111: printf("%d", *++fmt);
112: inquest = !inquest;
113: }
114: else if (*fmt == '<')
115: printf("%d", *++fmt);
116: }
117: printf("---\n");
118: inquest = FALSE;
119: for (fmt = Fmt; *fmt; fmt++)
120: if (*fmt == '<') {
121: fp = &Fmtab[*++fmt];
122: printf("<%s", fp->f_name);
123: if (strlen(fp->f_desc))
124: printf(":%s", fp->f_desc);
125: putchar('>');
126: }
127: else if (*fmt == '?' || *fmt == '!')
128: if (!inquest) {
129: fp = &Fmtab[*++fmt];
130: printf("%c%s:", *fmt, fp->f_name);
131: inquest = TRUE;
132: }
133: else
134: inquest = FALSE;
135: else
136: putchar(*fmt);
137: printf("---\n");
138: # endif
139: }
140:
141: doaddrs()
142: {
143: reg FILE *inf;
144: reg char *sp;
145: reg FDES *fp;
146: reg int len;
147: reg bool justprinted;
148: char buf[BUFSIZ];
149:
150: if ((inf = fopen(Addrfile, "r")) == NULL) {
151: perror(Addrfile);
152: exit(1);
153: }
154:
155: for (fp = Fmtab; fp->f_name != NULL; fp++)
156: fp->f_value = NULL;
157:
158: while (fgets(buf, BUFSIZ, inf) != NULL) {
159: justprinted = FALSE;
160: buf[strlen(buf)-1] = '\0';
161: if (strcmp(buf, "$") == 0) {
162: printaddr();
163: for (fp = Fmtab; fp->f_name != NULL; fp++)
164: if (fp->f_value != NULL) {
165: cfree(fp->f_value);
166: fp->f_value = NULL;
167: }
168: justprinted = TRUE;
169: continue;
170: }
171: for (sp = buf; !isspace(*sp) && *sp != '\0'; sp++)
172: continue;
173: len = sp - buf;
174: for (fp = Fmtab; fp->f_name != NULL; fp++)
175: if (strncmp(fp->f_name, buf, len) == 0) {
176: while (isspace(*sp))
177: sp++;
178: if ((len = strlen(sp)) == 0)
179: fp->f_value = NULL;
180: else {
181: fp->f_value = malloc(len + 1);
182: strcpy(fp->f_value, sp);
183: }
184: break;
185: }
186: }
187: if (!justprinted)
188: printaddr();
189: }
190:
191: printaddr()
192: {
193: reg char *sp;
194: reg FDES *fp;
195: reg bool printout, inquest;
196: char buf[80];
197:
198: printout = TRUE;
199: inquest = FALSE;
200: for (sp = Fmt; *sp; sp++)
201: if (*sp == '<' || *sp == '?' || *sp == '!') {
202: register char c;
203:
204: if (*sp == '?' && inquest) {
205: inquest = FALSE;
206: printout = TRUE;
207: continue;
208: }
209: c = *sp++;
210: fp = &Fmtab[*sp];
211: if (c == '<' && printout) {
212: sprintf(buf, "%%%ss", fp->f_desc);
213: printf(buf, fp->f_value);
214: }
215: else if (c == '?' || c == '!') {
216: inquest = TRUE;
217: printout = (fp->f_value != NULL);
218: if (c == '!')
219: printout = !printout;
220: }
221: }
222: else if (printout == TRUE)
223: putchar(*sp);
224: }
225:
226: FDES *
227: name(str)
228: reg char *str; {
229:
230: reg FDES *fp;
231:
232: for (fp = Fmtab; fp->f_name != NULL; fp++)
233: if (strcmp(str, fp->f_name) == 0)
234: return fp;
235: return fp;
236: }
Defined functions
main
defined in line
28;
never used
name
defined in line
226; used 2 times
Defined variables
Fmt
defined in line
22; used 4 times
Fmtab
defined in line
25; used 10 times
Defined struct's
fd
defined in line
12; used 2 times
Defined typedef's
FDES
defined in line
18; used 6 times
Defined macros
FALSE
defined in line
8; used 8 times
TRUE
defined in line
7; used 7 times
bool
defined in line
5; used 3 times
reg
defined in line
4; used 14 times