xref: /original-bsd/old/vfilters/railmag/railmag.c (revision f1324ba5)
1 /*
2  * Copyright (c) 1983 Regents of the University of California.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms are permitted
6  * provided that the above copyright notice and this paragraph are
7  * duplicated in all such forms and that any documentation,
8  * advertising materials, and other materials related to such
9  * distribution and use acknowledge that the software was developed
10  * by the University of California, Berkeley.  The name of the
11  * University may not be used to endorse or promote products derived
12  * from this software without specific prior written permission.
13  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
16  */
17 
18 #ifndef lint
19 char copyright[] =
20 "@(#) Copyright (c) 1983 Regents of the University of California.\n\
21  All rights reserved.\n";
22 #endif /* not lint */
23 
24 #ifndef lint
25 static char sccsid[] = "@(#)railmag.c	5.3 (Berkeley) 06/30/88";
26 #endif /* not lint */
27 
28 /*
29  * tell vcat which fonts are loaded on the "typesetter"
30  */
31 
32 #define MAGIC_NUMBER 0436
33 #define RAILMAG_FILE "/usr/lib/vfont/railmag"
34 
35 char	*concat();
36 int	rmfd;
37 char	*rm[4];
38 char	tbuf[256];
39 
40 main(argc, argv)
41 	int argc;
42 	char *argv[];
43 {
44 	register int fnum;
45 	char cbuf[4][50];
46 
47 	readrm();
48 	if (argc <= 1) {
49 		printrm();
50 		exit(0);
51 	}
52 	while (--argc) {
53 		argv++;
54 		fnum = argv[0][0] - '0';
55 		if (fnum < 1 || fnum > 4)
56 			error("Invalid font number");
57 		checkfont(argv[1]);
58 		if (argv[1][0] == '/')
59 			rm[fnum-1] = argv[1];
60 		else
61 			rm[fnum-1] = concat(cbuf[fnum-1], "/usr/lib/vfont/", argv[1]);
62 		argv++;	argc--;
63 	}
64 	writerm();
65 }
66 
67 error(str)
68 	char *str;
69 {
70 	write(2, "Railmag: ", 9);
71 	write(2, str, strlen(str));
72 	write(2, "\n", 1);
73 	exit();
74 }
75 
76 checkfont(file)
77 	char *file;
78 {
79 	register int fd;
80 	char cbuf[80];
81 	char cbuf2[80];
82 	short word;
83 
84 	if ((fd = open(concat(cbuf, file, ".10"), 0)) < 0)
85 		if ((fd = open(concat(cbuf2, "/usr/lib/vfont/", cbuf), 0)) < 0)
86 			error("cant open font");
87 	if (read(fd, &word, 2) != 2)
88 		error("cant read font");
89 	if (word != MAGIC_NUMBER)
90 		error("font has no magic number");
91 	close(fd);
92 }
93 
94 readrm()
95 {
96 	register int i;
97 	register char *cp;
98 	char c;
99 
100 	if ((rmfd = open(RAILMAG_FILE, 0)) < 0)
101 		error("No railmag file");
102 	cp = tbuf;
103 	for (i = 0; i < 4; i++) {
104 		rm[i] = cp;
105 		while (read(rmfd, &c, 1) == 1 && c != '\n')
106 			*cp++ = c;
107 		*cp++ = '\0';
108 	}
109 }
110 
111 printrm()
112 {
113 	register int i;
114 
115 	for (i = 0; i < 4; i++)
116 		printf("%s on %d\n", rm[i], i+1);
117 }
118 
119 writerm()
120 {
121 	register int i;
122 	register char *cp;
123 
124 	unlink(RAILMAG_FILE);
125 	if ((rmfd = creat(RAILMAG_FILE, 0644)) < 0)
126 		error("cant recreate railmag file");
127 	for (i = 0; i < 4; i++) {
128 		cp = rm[i];
129 		while (*cp != '\0')
130 			write(rmfd, cp++, 1);
131 		write(rmfd, "\n", 1);
132 	}
133 }
134 
135 char *
136 concat(outbuf, in1, in2)
137 	register char *outbuf, *in1, *in2;
138 {
139 	char *save;
140 
141 	save = outbuf;
142 	while (*in1)
143 		*outbuf++ = *in1++;
144 	while (*in2)
145 		*outbuf++ = *in2++;
146 	return(save);
147 }
148