]> git.lizzy.rs Git - plan9front.git/blob - sys/src/cmd/scat/header.c
ndb/dnsquery, ndb/csquery: handle long lines
[plan9front.git] / sys / src / cmd / scat / header.c
1 #include        <u.h>
2 #include        <libc.h>
3 #include        <bio.h>
4 #include        "sky.h"
5
6 #define JUKEFS "tcp!jukefs"     /* dial string for jukebox; was il!jukefs */
7
8 struct
9 {
10         char    name[9];
11         char    offset;
12 } Hproto[] =
13 {
14         "ppo1",         Pppo1,
15         "ppo2",         Pppo2,
16         "ppo3",         Pppo3,
17         "ppo4",         Pppo4,
18         "ppo5",         Pppo5,
19         "ppo6",         Pppo6,
20
21         "amdx1",        Pamdx1,
22         "amdx2",        Pamdx2,
23         "amdx3",        Pamdx3,
24         "amdx4",        Pamdx4,
25         "amdx5",        Pamdx5,
26         "amdx6",        Pamdx6,
27         "amdx7",        Pamdx7,
28         "amdx8",        Pamdx8,
29         "amdx9",        Pamdx9,
30         "amdx10",       Pamdx10,
31         "amdx11",       Pamdx11,
32         "amdx12",       Pamdx12,
33         "amdx13",       Pamdx13,
34         "amdx14",       Pamdx14,
35         "amdx15",       Pamdx15,
36         "amdx16",       Pamdx16,
37         "amdx17",       Pamdx17,
38         "amdx18",       Pamdx18,
39         "amdx19",       Pamdx19,
40         "amdx20",       Pamdx20,
41
42         "amdy1",        Pamdy1,
43         "amdy2",        Pamdy2,
44         "amdy3",        Pamdy3,
45         "amdy4",        Pamdy4,
46         "amdy5",        Pamdy5,
47         "amdy6",        Pamdy6,
48         "amdy7",        Pamdy7,
49         "amdy8",        Pamdy8,
50         "amdy9",        Pamdy9,
51         "amdy10",       Pamdy10,
52         "amdy11",       Pamdy11,
53         "amdy12",       Pamdy12,
54         "amdy13",       Pamdy13,
55         "amdy14",       Pamdy14,
56         "amdy15",       Pamdy15,
57         "amdy16",       Pamdy16,
58         "amdy17",       Pamdy17,
59         "amdy18",       Pamdy18,
60         "amdy19",       Pamdy19,
61         "amdy20",       Pamdy20,
62
63         "pltscale",     Ppltscale,
64         "xpixelsz",     Pxpixelsz,
65         "ypixelsz",     Pypixelsz,
66
67         "pltrah",       Ppltrah,
68         "pltram",       Ppltram,
69         "pltras",       Ppltras,
70         "pltdecd",      Ppltdecd,
71         "pltdecm",      Ppltdecm,
72         "pltdecs",      Ppltdecs,
73
74 };
75
76 Header*
77 getheader(char *rgn)
78 {
79         char rec[81], name[81], value[81];
80         char *p;
81         Biobuf *bin;
82         Header hd, *h;
83         int i, j, decsn, dss;
84
85         dss = 0;
86         sprint(rec, "/lib/sky/dssheaders/%s.hhh", rgn);
87         bin = Bopen(rec, OREAD);
88         if(bin == 0) {
89                 dss = 102;
90                 sprint(rec, "/n/juke/dss/dss.102/headers/%s.hhh", rgn);
91                 bin = Bopen(rec, OREAD);
92         }
93         if(bin == 0) {
94                 dss = 61;
95                 sprint(rec, "/n/juke/dss/dss.061/headers/%s.hhh", rgn);
96                 bin = Bopen(rec, OREAD);
97         }
98         if(bin == 0) {
99                 fprint(2, "cannot open %s\n", rgn);
100                 exits("file");
101         }
102         if(debug)
103                 Bprint(&bout, "reading %s\n", rec);
104         if(dss)
105                 Bprint(&bout, "warning: reading %s from jukebox\n", rec);
106
107         memset(&hd, 0, sizeof(hd));
108         j = 0;
109         decsn = 0;
110         for(;;) {
111                 if(dss) {
112                         if(Bread(bin, rec, 80) != 80)
113                                 break;
114                         rec[80] = 0;
115                 } else {
116                         p = Brdline(bin, '\n');
117                         if(p == 0)
118                                 break;
119                         p[Blinelen(bin)-1] = 0;
120                         strcpy(rec, p);
121                 }
122
123                 p = strchr(rec, '/');
124                 if(p)
125                         *p = 0;
126                 p = strchr(rec, '=');
127                 if(p == 0)
128                         continue;
129                 *p++ = 0;
130                 if(getword(name, rec) == 0)
131                         continue;
132                 if(getword(value, p) == 0)
133                         continue;
134                 if(strcmp(name, "pltdecsn") == 0) {
135                         if(strchr(value, '-'))
136                                 decsn = 1;
137                         continue;
138                 }
139                 for(i=0; i<nelem(Hproto); i++) {
140                         j++;
141                         if(j >= nelem(Hproto))
142                                 j = 0;
143                         if(strcmp(name, Hproto[j].name) == 0) {
144                                 hd.param[Hproto[j].offset] = atof(value);
145                                 break;
146                         }
147                 }
148         }
149         Bterm(bin);
150
151         hd.param[Ppltra] = RAD(hd.param[Ppltrah]*15 +
152                 hd.param[Ppltram]/4 + hd.param[Ppltras]/240);
153         hd.param[Ppltdec] = RAD(hd.param[Ppltdecd] +
154                 hd.param[Ppltdecm]/60 + hd.param[Ppltdecs]/3600);
155         if(decsn)
156                 hd.param[Ppltdec] = -hd.param[Ppltdec];
157         hd.amdflag = 0;
158         for(i=Pamdx1; i<=Pamdx20; i++)
159                 if(hd.param[i] != 0) {
160                         hd.amdflag = 1;
161                         break;
162                 }
163         h = malloc(sizeof(*h));
164         *h = hd;
165         return h;
166 }
167
168 void
169 getplates(void)
170 {
171         char rec[81], *q;
172         Plate *p;
173         Biobuf *bin;
174         int c, i, dss;
175
176         dss = 0;
177         sprint(rec, "/lib/sky/dssheaders/lo_comp.lis");
178         bin = Bopen(rec, OREAD);
179         if(bin == 0) {
180                 dss = 102;
181                 sprint(rec, "%s/headers/lo_comp.lis", dssmount(dss));
182                 bin = Bopen(rec, OREAD);
183         }
184         if(bin == 0) {
185                 dss = 61;
186                 sprint(rec, "%s/headers/lo_comp.lis", dssmount(dss));
187                 bin = Bopen(rec, OREAD);
188         }
189         if(bin == 0) {
190                 fprint(2, "can't open lo_comp.lis; try 9fs juke\n");
191                 exits("open");
192         }
193         if(debug)
194                 Bprint(&bout, "reading %s\n", rec);
195         if(dss)
196                 Bprint(&bout, "warning: reading %s from jukebox\n", rec);
197         for(nplate=0;;) {
198                 if(dss) {
199                         if(Bread(bin, rec, 80) != 80)
200                                 break;
201                         rec[80] = 0;
202                 } else {
203                         q = Brdline(bin, '\n');
204                         if(q == 0)
205                                 break;
206                         q[Blinelen(bin)-1] = 0;
207                         strcpy(rec, q);
208                 }
209                 if(rec[0] == '#')
210                         continue;
211                 if(nplate < nelem(plate)) {
212                         p = &plate[nplate];
213                         memmove(p->rgn, rec+0, 5);
214                         if(p->rgn[4] == ' ')
215                                 p->rgn[4] = 0;
216                         for(i=0; c=p->rgn[i]; i++)
217                                 if(c >= 'A' && c <= 'Z')
218                                         p->rgn[i] += 'a'-'A';
219                         p->ra = RAD(atof(rec+12)*15 +
220                                 atof(rec+15)/4 +
221                                 atof(rec+18)/240);
222                         p->dec = RAD(atof(rec+26) +
223                                 atof(rec+29)/60 +
224                                 atof(rec+32)/3600);
225                         if(rec[25] == '-')
226                                 p->dec = -p->dec;
227                         p->disk = atoi(rec+53);
228                         if(p->disk == 0)
229                                 continue;
230                 }
231                 nplate++;
232         }
233         Bterm(bin);
234
235         if(nplate >= nelem(plate))
236                 fprint(2, "nplate too small %d %d\n", nelem(plate), nplate);
237         if(debug)
238                 Bprint(&bout, "%d plates\n", nplate);
239 }
240
241 char*
242 dssmount(int dskno)
243 {
244         char dssname[100];
245         int s1, s2, count;
246         static int sdiskno = -1;
247
248         if(sdiskno == dskno)
249                 goto out;
250         count = 0;
251
252 loop:
253         unmount(nil, "/n/njuke");
254         unmount(nil, "/n/dss");
255         sprint(dssname, "/n/njuke/juke/dss/dss.%.3d", dskno);
256
257         /*
258          * start nfs jukebox server
259          */
260         s1 = open("/srv/" JUKEFS, ORDWR);
261         if(s1 < 0) {
262                 if(fork() == 0) {
263                         execl("/bin/srv", "srv", "-q", JUKEFS, nil);
264                         exits(0);
265                 }
266                 waitpid();
267                 s1 = open("/srv/" JUKEFS, ORDWR);
268                 if(s1 < 0) {
269                         Bprint(&bout, "can't open /srv/%s: %r\n", JUKEFS);
270                         goto out;
271                 }
272         }
273
274         /*
275          * mount nfs jukebox server
276          */
277         if(mount(s1, -1, "/n/njuke", 0, "") < 0) {
278                 close(s1);
279                 Bprint(&bout, "\"mount /srv/%s /n/juke\" failed: %r\n", JUKEFS);
280                 goto out;
281         }
282
283         /*
284          * run 9660 server
285          */
286         s2 = open("/srv/9660", ORDWR);
287         if(s2 < 0) {
288                 if(fork() == 0) {
289                         execl("/bin/9660srv", "9660srv", nil);
290                         exits(0);
291                 }
292                 waitpid();
293                 s2 = open("/srv/9660", ORDWR);
294                 if(s2 < 0) {
295                         Bprint(&bout, "can't open /srv/9660: %r\n");
296                         goto out;
297                 }
298         }
299
300         /*
301          * mount 9660 server
302          */
303         if(mount(s2, -1, "/n/dss", 0, dssname) < 0) {
304                 close(s2);
305                 if(count == 0) {
306                         // do it again so /n/njuke is in 9660's namespace
307                         remove("/srv/9660");
308                         remove("/srv/" JUKEFS);
309                         count = 1;
310                         goto loop;
311                 }
312                 Bprint(&bout, "\"mount /srv/9660 /n/dss %s\" failed %r\n", dssname);
313                 goto out;
314         }
315
316 //      print("mount %s\n", dssname);
317         sdiskno = dskno;
318
319 out:
320         return "/n/dss";
321 }