drm/linux: Add mod_timer_pinned()
[dragonfly.git] / contrib / amd / fsinfo / fsi_util.c
blobf3fe793aae949f49ebf02cd33500d9841c5fe161
1 /*
2 * Copyright (c) 1997-1999 Erez Zadok
3 * Copyright (c) 1989 Jan-Simon Pendry
4 * Copyright (c) 1989 Imperial College of Science, Technology & Medicine
5 * Copyright (c) 1989 The Regents of the University of California.
6 * All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgment:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
36 * %W% (Berkeley) %G%
38 * $Id: fsi_util.c,v 1.2 1999/01/10 21:54:28 ezk Exp $
42 #ifdef HAVE_CONFIG_H
43 # include <config.h>
44 #endif /* HAVE_CONFIG_H */
45 #include <am_defs.h>
46 #include <fsi_data.h>
47 #include <fsinfo.h>
49 /* static variables */
50 static int show_range = 10;
51 static int col = 0;
52 static int total_shown = 0;
53 static int total_mmm = 8;
56 static int
57 col_output(int len)
59 int wrapped = 0;
61 col += len;
62 if (col > 77) {
63 fputc('\n', stdout);
64 col = len;
65 wrapped = 1;
67 return wrapped;
71 static void
72 show_total(void)
74 if (total_mmm != -show_range + 1) {
75 char n[8];
76 int len;
78 if (total_mmm < 0)
79 fputc('*', stdout);
80 sprintf(n, "%d", total_shown);
81 len = strlen(n);
82 if (col_output(len))
83 fputc(' ', stdout);
84 fputs(n, stdout);
85 fflush(stdout);
86 total_mmm = -show_range;
91 void
92 col_cleanup(int eoj)
94 if (verbose < 0)
95 return;
96 if (eoj) {
97 show_total();
98 fputs(")]", stdout);
100 if (col) {
101 fputc('\n', stdout);
102 col = 0;
108 * Lots of ways of reporting errors...
110 void
111 error(char *fmt, ...)
113 va_list ap;
115 va_start(ap, fmt);
116 col_cleanup(0);
117 fprintf(stderr, "%s: Error, ", progname);
118 fprintf(stderr, fmt, ap);
119 fputc('\n', stderr);
120 errors++;
121 va_end(ap);
125 void
126 lerror(ioloc *l, char *fmt, ...)
128 va_list ap;
130 va_start(ap, fmt);
131 col_cleanup(0);
132 fprintf(stderr, "%s:%d: ", l->i_file, l->i_line);
133 fprintf(stderr, fmt, ap);
134 fputc('\n', stderr);
135 errors++;
136 va_end(ap);
140 void
141 lwarning(ioloc *l, char *fmt, ...)
143 va_list ap;
145 va_start(ap, fmt);
146 col_cleanup(0);
147 fprintf(stderr, "%s:%d: ", l->i_file, l->i_line);
148 fprintf(stderr, fmt, ap);
149 fputc('\n', stderr);
150 va_end(ap);
154 void
155 fatal(char *fmt, ...)
157 va_list ap;
159 va_start(ap, fmt);
160 col_cleanup(1);
161 fprintf(stderr, "%s: Fatal, ", progname);
162 fprintf(stderr, fmt, ap);
163 fputc('\n', stderr);
164 va_end(ap);
165 exit(1);
170 * Debug log
172 void
173 log(char *fmt, ...)
175 va_list ap;
177 if (verbose > 0) {
178 va_start(ap, fmt);
179 fputc('#', stdout);
180 fprintf(stdout, "%s: ", progname);
181 fprintf(stdout, fmt, ap);
182 putc('\n', stdout);
183 va_end(ap);
188 void
189 info_hdr(FILE *ef, char *info)
191 fprintf(ef, "# *** NOTE: This file contains %s info\n", info);
195 void
196 gen_hdr(FILE *ef, char *hn)
198 fprintf(ef, "# *** NOTE: Only for use on %s\n", hn);
202 static void
203 make_banner(FILE *fp)
205 time_t t = time((time_t *) 0);
206 char *cp = ctime(&t);
208 fprintf(fp,
210 # *** This file was automatically generated -- DO NOT EDIT HERE ***\n\
211 # \"%s\" run by %s@%s on %s\
212 #\n\
214 progname, username, hostname, cp);
218 void
219 show_new(char *msg)
221 if (verbose < 0)
222 return;
224 total_shown++;
225 if (total_mmm > show_range) {
226 show_total();
227 } else if (total_mmm == 0) {
228 fputc('*', stdout);
229 fflush(stdout);
230 col += 1;
232 total_mmm++;
236 void
237 show_area_being_processed(char *area, int n)
239 static char *last_area = 0;
241 if (verbose < 0)
242 return;
243 if (last_area) {
244 if (total_shown)
245 show_total();
246 fputs(")", stdout);
247 col += 1;
250 if (!last_area || !STREQ(area, last_area)) {
251 if (last_area) {
252 col_cleanup(0);
253 total_shown = 0;
254 total_mmm = show_range + 1;
256 (void) col_output(strlen(area) + 2);
257 fprintf(stdout, "[%s", area);
258 last_area = area;
261 fputs(" (", stdout);
262 col += 2;
263 show_range = n;
264 total_mmm = n + 1;
266 fflush(stdout);
271 * Open a file with the given prefix and name
273 FILE *
274 pref_open(char *pref, char *hn, void (*hdr) (FILE *, char *), char *arg)
276 char p[MAXPATHLEN];
277 FILE *ef;
279 sprintf(p, "%s%s", pref, hn);
280 log("Writing %s info for %s to %s", pref, hn, p);
281 ef = fopen(p, "w");
282 if (ef) {
283 (*hdr) (ef, arg);
284 make_banner(ef);
285 } else {
286 error("can't open %s for writing", p);
289 return ef;
294 pref_close(FILE *fp)
296 return fclose(fp) == 0;
301 * Determine where Amd would automount the host/volname pair
303 void
304 compute_automount_point(char *buf, host *hp, char *vn)
306 sprintf(buf, "%s/%s%s", autodir, hp->h_lochost, vn);
310 char *
311 xcalloc(int i, int s)
313 char *p = (char *) calloc(i, (unsigned) s);
315 if (!p)
316 fatal("Out of memory");
317 return p;
322 * Data constructors..
324 automount *
325 new_automount(char *name)
327 automount *ap = CALLOC(struct automount);
329 ap->a_ioloc = current_location();
330 ap->a_name = name;
331 ap->a_volname = 0;
332 ap->a_mount = 0;
333 ap->a_opts = 0;
334 show_new("automount");
335 return ap;
339 auto_tree *
340 new_auto_tree(char *def, qelem *ap)
342 auto_tree *tp = CALLOC(struct auto_tree);
344 tp->t_ioloc = current_location();
345 tp->t_defaults = def;
346 tp->t_mount = ap;
347 show_new("auto_tree");
348 return tp;
352 host *
353 new_host(void)
355 host *hp = CALLOC(struct host);
357 hp->h_ioloc = current_location();
358 hp->h_mask = 0;
359 show_new("host");
360 return hp;
364 void
365 set_host(host *hp, int k, char *v)
367 int m = 1 << k;
369 if (hp->h_mask & m) {
370 yyerror("host field \"%s\" already set", host_strings[k]);
371 return;
373 hp->h_mask |= m;
375 switch (k) {
377 case HF_HOST:{
378 char *p = strdup(v);
379 dict_ent *de = dict_locate(dict_of_hosts, v);
381 if (de)
382 yyerror("duplicate host %s!", v);
383 else
384 dict_add(dict_of_hosts, v, (char *) hp);
385 hp->h_hostname = v;
386 domain_strip(p, hostname);
387 if (strchr(p, '.') != 0)
388 XFREE(p);
389 else
390 hp->h_lochost = p;
392 break;
394 case HF_CONFIG:{
395 qelem *q;
396 qelem *vq = (qelem *) v;
398 hp->h_mask &= ~m;
399 if (hp->h_config)
400 q = hp->h_config;
401 else
402 q = hp->h_config = new_que();
403 ins_que(vq, q->q_back);
405 break;
407 case HF_ETHER:{
408 qelem *q;
409 qelem *vq = (qelem *) v;
411 hp->h_mask &= ~m;
412 if (hp->h_ether)
413 q = hp->h_ether;
414 else
415 q = hp->h_ether = new_que();
416 ins_que(vq, q->q_back);
418 break;
420 case HF_ARCH:
421 hp->h_arch = v;
422 break;
424 case HF_OS:
425 hp->h_os = v;
426 break;
428 case HF_CLUSTER:
429 hp->h_cluster = v;
430 break;
432 default:
433 abort();
434 break;
439 ether_if *
440 new_ether_if(void)
442 ether_if *ep = CALLOC(struct ether_if);
444 ep->e_mask = 0;
445 ep->e_ioloc = current_location();
446 show_new("ether_if");
447 return ep;
451 void
452 set_ether_if(ether_if *ep, int k, char *v)
454 int m = 1 << k;
456 if (ep->e_mask & m) {
457 yyerror("netif field \"%s\" already set", ether_if_strings[k]);
458 return;
460 ep->e_mask |= m;
462 switch (k) {
464 case EF_INADDR:{
465 ep->e_inaddr.s_addr = inet_addr(v);
466 if (ep->e_inaddr.s_addr == (u_long) - 1)
467 yyerror("malformed IP dotted quad: %s", v);
468 XFREE(v);
470 break;
472 case EF_NETMASK:{
473 u_long nm = 0;
475 if ((sscanf(v, "0x%lx", &nm) == 1 || sscanf(v, "%lx", &nm) == 1) && nm != 0)
476 ep->e_netmask = htonl(nm);
477 else
478 yyerror("malformed netmask: %s", v);
479 XFREE(v);
481 break;
483 case EF_HWADDR:
484 ep->e_hwaddr = v;
485 break;
487 default:
488 abort();
489 break;
494 void
495 set_disk_fs(disk_fs *dp, int k, char *v)
497 int m = 1 << k;
499 if (dp->d_mask & m) {
500 yyerror("fs field \"%s\" already set", disk_fs_strings[k]);
501 return;
503 dp->d_mask |= m;
505 switch (k) {
507 case DF_FSTYPE:
508 dp->d_fstype = v;
509 break;
511 case DF_OPTS:
512 dp->d_opts = v;
513 break;
515 case DF_DUMPSET:
516 dp->d_dumpset = v;
517 break;
519 case DF_LOG:
520 dp->d_log = v;
521 break;
523 case DF_PASSNO:
524 dp->d_passno = atoi(v);
525 XFREE(v);
526 break;
528 case DF_FREQ:
529 dp->d_freq = atoi(v);
530 XFREE(v);
531 break;
533 case DF_MOUNT:
534 dp->d_mount = &((fsi_mount *) v)->m_q;
535 break;
537 default:
538 abort();
539 break;
544 disk_fs *
545 new_disk_fs(void)
547 disk_fs *dp = CALLOC(struct disk_fs);
549 dp->d_ioloc = current_location();
550 show_new("disk_fs");
551 return dp;
555 void
556 set_mount(fsi_mount *mp, int k, char *v)
558 int m = 1 << k;
560 if (mp->m_mask & m) {
561 yyerror("mount tree field \"%s\" already set", mount_strings[k]);
562 return;
564 mp->m_mask |= m;
566 switch (k) {
568 case DM_VOLNAME:
569 dict_add(dict_of_volnames, v, (char *) mp);
570 mp->m_volname = v;
571 break;
573 case DM_EXPORTFS:
574 mp->m_exportfs = v;
575 break;
577 case DM_SEL:
578 mp->m_sel = v;
579 break;
581 default:
582 abort();
583 break;
588 fsi_mount *
589 new_mount(void)
591 fsi_mount *fp = CALLOC(struct fsi_mount);
593 fp->m_ioloc = current_location();
594 show_new("mount");
595 return fp;
599 void
600 set_fsmount(fsmount *fp, int k, char *v)
602 int m = 1 << k;
604 if (fp->f_mask & m) {
605 yyerror("mount field \"%s\" already set", fsmount_strings[k]);
606 return;
608 fp->f_mask |= m;
610 switch (k) {
612 case FM_LOCALNAME:
613 fp->f_localname = v;
614 break;
616 case FM_VOLNAME:
617 fp->f_volname = v;
618 break;
620 case FM_FSTYPE:
621 fp->f_fstype = v;
622 break;
624 case FM_OPTS:
625 fp->f_opts = v;
626 break;
628 case FM_FROM:
629 fp->f_from = v;
630 break;
632 case FM_DIRECT:
633 break;
635 default:
636 abort();
637 break;
642 fsmount *
643 new_fsmount(void)
645 fsmount *fp = CALLOC(struct fsmount);
647 fp->f_ioloc = current_location();
648 show_new("fsmount");
649 return fp;
653 void
654 init_que(qelem *q)
656 q->q_forw = q->q_back = q;
660 qelem *
661 new_que(void)
663 qelem *q = CALLOC(qelem);
665 init_que(q);
666 return q;
670 void
671 ins_que(qelem *elem, qelem *pred)
673 qelem *p;
675 p = pred->q_forw;
676 elem->q_back = pred;
677 elem->q_forw = p;
678 pred->q_forw = elem;
679 p->q_back = elem;
683 void
684 rem_que(qelem *elem)
686 qelem *p, *p2;
688 p = elem->q_forw;
689 p2 = elem->q_back;
691 p2->q_forw = p;
692 p->q_back = p2;