int state_load(memfile_t *file) { stateheader_t header; block_t *block; u32 size = 0; int i; readvar(header.ident,4); readvar(header.version,2); readvar(header.flags,2); readvar(header.usize,4); readvar(header.csize,4); readvar(header.crc32,4); log_printf("state_load: state header loaded. version %04X\n",header.version); while(memfile_eof(file) == 0 && size < header.usize) { if((block = block_load(file)) == 0) break; size += 8 + block->size; log_printf("state_load: loaded block '%4s' (%08X) (%d bytes)\n",&block->type,block->type,block->size); for(i=0;blockinfo[i].type;i++) { if(blockinfo[i].type == block->type) { blockinfo[i].func(STATE_LOAD,block->data); break; } } if(blockinfo[i].type == 0) { log_printf("state_load: no handler for block type '%4s' (%d bytes)\n",&block->type,block->size); } block_destroy(block); } return(0); }
int main(int argc, char *argv[]) { char var[VARLEN]; int maxlen = VARLEN; int first_n = 0; struct gp* groups = NULL; /* get parameter */ if ((first_n = get_para(argc, argv)) < 0) { printf ("%s: input group flag n.\n", argv[0]); return -1; } while (readvar(var, maxlen) > 0) { add_to_groups(groups, var, first_n); } print_groups(groups); return 0; }
/* * getoffsets - read the magic offsets from the specified file */ static void getoffsets( off_t *tick_off, off_t *tickadj_off, off_t *dosync_off, off_t *noprintf_off ) { #ifndef NOKMEM # ifndef HAVE_KVM_OPEN const char **kname; # endif #endif #ifndef NOKMEM # ifdef NLIST_NAME_UNION # define NL_B {{ # define NL_E }} # else # define NL_B { # define NL_E } # endif #endif #define K_FILLER_NAME "DavidLetterman" #ifdef NLIST_EXTRA_INDIRECTION int i; #endif #ifndef NOKMEM static struct nlist nl[] = { NL_B #ifdef K_TICKADJ_NAME #define N_TICKADJ 0 K_TICKADJ_NAME #else K_FILLER_NAME #endif NL_E, NL_B #ifdef K_TICK_NAME #define N_TICK 1 K_TICK_NAME #else K_FILLER_NAME #endif NL_E, NL_B #ifdef K_DOSYNCTODR_NAME #define N_DOSYNC 2 K_DOSYNCTODR_NAME #else K_FILLER_NAME #endif NL_E, NL_B #ifdef K_NOPRINTF_NAME #define N_NOPRINTF 3 K_NOPRINTF_NAME #else K_FILLER_NAME #endif NL_E, NL_B "" NL_E, }; #ifndef HAVE_KVM_OPEN static const char *kernels[] = { #ifdef HAVE_GETBOOTFILE NULL, /* *** SEE BELOW! *** */ #endif "/kernel/unix", "/kernel", "/vmunix", "/unix", "/mach", "/hp-ux", "/386bsd", "/netbsd", "/stand/vmunix", "/bsd", NULL }; #endif /* not HAVE_KVM_OPEN */ #ifdef HAVE_KVM_OPEN /* * Solaris > 2.5 doesn't have a kernel file. Use the kvm_* interface * to read the kernel name list. -- stolcke 3/4/96 */ kvm_t *kvm_handle = kvm_open(NULL, NULL, NULL, O_RDONLY, progname); if (kvm_handle == NULL) { (void) fprintf(stderr, "%s: kvm_open failed\n", progname); exit(1); } if (kvm_nlist(kvm_handle, nl) == -1) { (void) fprintf(stderr, "%s: kvm_nlist failed\n", progname); exit(1); } kvm_close(kvm_handle); #else /* not HAVE_KVM_OPEN */ #ifdef HAVE_GETBOOTFILE /* *** SEE HERE! *** */ if (kernels[0] == NULL) { char * cp = (char *)getbootfile(); if (cp) { kernels[0] = cp; } else { kernels[0] = "/Placeholder"; } } #endif /* HAVE_GETBOOTFILE */ for (kname = kernels; *kname != NULL; kname++) { struct stat stbuf; if (stat(*kname, &stbuf) == -1) { continue; } if (nlist(*kname, nl) >= 0) { break; } else { (void) fprintf(stderr, "%s: nlist didn't find needed symbols from <%s>: %s\n", progname, *kname, strerror(errno)); } } if (*kname == NULL) { (void) fprintf(stderr, "%s: Couldn't find the kernel\n", progname); exit(1); } #endif /* HAVE_KVM_OPEN */ if (dokmem) { file = kmem; fd = openfile(file, O_RDONLY); #ifdef NLIST_EXTRA_INDIRECTION /* * Go one more round of indirection. */ for (i = 0; i < (sizeof(nl) / sizeof(struct nlist)); i++) { if ((nl[i].n_value) && (nl[i].n_sclass == 0x6b)) { readvar(fd, nl[i].n_value, &nl[i].n_value); } } #endif /* NLIST_EXTRA_INDIRECTION */ } #endif /* not NOKMEM */ *tickadj_off = 0; *tick_off = 0; *dosync_off = 0; *noprintf_off = 0; #if defined(N_TICKADJ) *tickadj_off = nl[N_TICKADJ].n_value; #endif #if defined(N_TICK) *tick_off = nl[N_TICK].n_value; #endif #if defined(N_DOSYNC) *dosync_off = nl[N_DOSYNC].n_value; #endif #if defined(N_NOPRINTF) *noprintf_off = nl[N_NOPRINTF].n_value; #endif return; }
/* * main - parse arguments and handle options */ int main( int argc, char *argv[] ) { int c; int errflg = 0; off_t tickadj_offset; off_t tick_offset; off_t dosync_offset; off_t noprintf_offset; int tickadj, ktickadj; /* HMS: Why isn't this u_long? */ int tick, ktick; /* HMS: Why isn't this u_long? */ int dosynctodr; int noprintf; int hz; int hz_int, hz_hundredths; int recommend_tickadj; long tmp; progname = argv[0]; while ((c = ntp_getopt(argc, argv, "a:Adkpqst:")) != EOF) { switch (c) { case 'a': writetickadj = atoi(ntp_optarg); if (writetickadj <= 0) { (void) fprintf(stderr, "%s: unlikely value for tickadj: %s\n", progname, ntp_optarg); errflg++; } #if defined SCO5_CLOCK if (writetickadj % HZ) { writetickadj = (writetickadj / HZ) * HZ; (void) fprintf(stderr, "tickadj truncated to: %d\n", writetickadj); } #endif /* SCO5_CLOCK */ break; case 'A': writeopttickadj = 1; break; case 'd': ++debug; break; case 'k': dokmem = 1; break; case 'p': setnoprintf = 1; break; case 'q': quiet = 1; break; case 's': unsetdosync = 1; break; case 't': writetick = atoi(ntp_optarg); if (writetick <= 0) { (void) fprintf(stderr, "%s: unlikely value for tick: %s\n", progname, ntp_optarg); errflg++; } break; default: errflg++; break; } } if (errflg || ntp_optind != argc) { (void) fprintf(stderr, "usage: %s [-Adkpqs] [-a newadj] [-t newtick]\n", progname); exit(2); } getoffsets(&tick_offset, &tickadj_offset, &dosync_offset, &noprintf_offset); if (debug) { (void) printf("tick offset = %lu\n", (unsigned long)tick_offset); (void) printf("tickadj offset = %lu\n", (unsigned long)tickadj_offset); (void) printf("dosynctodr offset = %lu\n", (unsigned long)dosync_offset); (void) printf("noprintf offset = %lu\n", (unsigned long)noprintf_offset); } if (writetick && (tick_offset == 0)) { (void) fprintf(stderr, "No tick kernel variable\n"); errflg++; } if (writeopttickadj && (tickadj_offset == 0)) { (void) fprintf(stderr, "No tickadj kernel variable\n"); errflg++; } if (unsetdosync && (dosync_offset == 0)) { (void) fprintf(stderr, "No dosynctodr kernel variable\n"); errflg++; } if (setnoprintf && (noprintf_offset == 0)) { (void) fprintf(stderr, "No noprintf kernel variable\n"); errflg++; } if (tick_offset != 0) { readvar(fd, tick_offset, &tick); #if defined(TICK_NANO) && defined(K_TICK_NAME) if (!quiet) (void) printf("KERNEL %s = %d nsec\n", K_TICK_NAME, tick); #endif /* TICK_NANO && K_TICK_NAME */ #ifdef TICK_NANO tick /= 1000; #endif } else { tick = 0; } if (tickadj_offset != 0) { readvar(fd, tickadj_offset, &tickadj); #ifdef SCO5_CLOCK /* scale from nsec/sec to usec/tick */ tickadj /= (1000L * HZ); #endif /*SCO5_CLOCK */ #if defined(TICKADJ_NANO) && defined(K_TICKADJ_NAME) if (!quiet) (void) printf("KERNEL %s = %d nsec\n", K_TICKADJ_NAME, tickadj); #endif /* TICKADJ_NANO && K_TICKADJ_NAME */ #ifdef TICKADJ_NANO tickadj += 999; tickadj /= 1000; #endif } else { tickadj = 0; } if (dosync_offset != 0) { readvar(fd, dosync_offset, &dosynctodr); } if (noprintf_offset != 0) { readvar(fd, noprintf_offset, &noprintf); } (void) close(fd); if (unsetdosync && dosync_offset == 0) { (void) fprintf(stderr, "%s: can't find %s in namelist\n", progname, #ifdef K_DOSYNCTODR_NAME K_DOSYNCTODR_NAME #else /* not K_DOSYNCTODR_NAME */ "dosynctodr" #endif /* not K_DOSYNCTODR_NAME */ ); exit(1); } hz = HZ; #if defined(HAVE_SYSCONF) && defined(_SC_CLK_TCK) hz = (int) sysconf (_SC_CLK_TCK); #endif /* not HAVE_SYSCONF && _SC_CLK_TCK */ #ifdef OVERRIDE_HZ hz = DEFAULT_HZ; #endif ktick = tick; #ifdef PRESET_TICK tick = PRESET_TICK; #endif /* PRESET_TICK */ #ifdef TICKADJ_NANO tickadj /= 1000; if (tickadj == 0) tickadj = 1; #endif ktickadj = tickadj; #ifdef PRESET_TICKADJ tickadj = (PRESET_TICKADJ) ? PRESET_TICKADJ : 1; #endif /* PRESET_TICKADJ */ if (!quiet) { if (tick_offset != 0) { (void) printf("KERNEL tick = %d usec (from %s kernel variable)\n", ktick, #ifdef K_TICK_NAME K_TICK_NAME #else "<this can't happen>" #endif ); } #ifdef PRESET_TICK (void) printf("PRESET tick = %d usec\n", tick); #endif /* PRESET_TICK */ if (tickadj_offset != 0) { (void) printf("KERNEL tickadj = %d usec (from %s kernel variable)\n", ktickadj, #ifdef K_TICKADJ_NAME K_TICKADJ_NAME #else "<this can't happen>" #endif ); } #ifdef PRESET_TICKADJ (void) printf("PRESET tickadj = %d usec\n", tickadj); #endif /* PRESET_TICKADJ */ if (dosync_offset != 0) { (void) printf("dosynctodr is %s\n", dosynctodr ? "on" : "off"); } if (noprintf_offset != 0) { (void) printf("kernel level printf's: %s\n", noprintf ? "off" : "on"); } } if (tick <= 0) { (void) fprintf(stderr, "%s: the value of tick is silly!\n", progname); exit(1); } hz_int = (int)(1000000L / (long)tick); hz_hundredths = (int)((100000000L / (long)tick) - ((long)hz_int * 100L)); if (!quiet) { (void) printf("KERNEL hz = %d\n", hz); (void) printf("calculated hz = %d.%02d Hz\n", hz_int, hz_hundredths); } #if defined SCO5_CLOCK recommend_tickadj = 100; #else /* SCO5_CLOCK */ tmp = (long) tick * 500L; recommend_tickadj = (int)(tmp / 1000000L); if (tmp % 1000000L > 0) { recommend_tickadj++; } #ifdef MIN_REC_TICKADJ if (recommend_tickadj < MIN_REC_TICKADJ) { recommend_tickadj = MIN_REC_TICKADJ; } #endif /* MIN_REC_TICKADJ */ #endif /* SCO5_CLOCK */ if ((!quiet) && (tickadj_offset != 0)) { (void) printf("recommended value of tickadj = %d us\n", recommend_tickadj); } if ( writetickadj == 0 && !writeopttickadj && !unsetdosync && writetick == 0 && !setnoprintf) { exit(errflg ? 1 : 0); } if (writetickadj == 0 && writeopttickadj) { writetickadj = recommend_tickadj; } fd = openfile(file, O_WRONLY); if (setnoprintf && (noprintf_offset != 0)) { if (!quiet) { (void) fprintf(stderr, "setting noprintf: "); (void) fflush(stderr); } writevar(fd, noprintf_offset, 1); if (!quiet) { (void) fprintf(stderr, "done!\n"); } } if ((writetick > 0) && (tick_offset != 0)) { if (!quiet) { (void) fprintf(stderr, "writing tick, value %d: ", writetick); (void) fflush(stderr); } writevar(fd, tick_offset, writetick); if (!quiet) { (void) fprintf(stderr, "done!\n"); } } if ((writetickadj > 0) && (tickadj_offset != 0)) { if (!quiet) { (void) fprintf(stderr, "writing tickadj, value %d: ", writetickadj); (void) fflush(stderr); } #ifdef SCO5_CLOCK /* scale from usec/tick to nsec/sec */ writetickadj *= (1000L * HZ); #endif /* SCO5_CLOCK */ writevar(fd, tickadj_offset, writetickadj); if (!quiet) { (void) fprintf(stderr, "done!\n"); } } if (unsetdosync && (dosync_offset != 0)) { if (!quiet) { (void) fprintf(stderr, "zeroing dosynctodr: "); (void) fflush(stderr); } writevar(fd, dosync_offset, 0); if (!quiet) { (void) fprintf(stderr, "done!\n"); } } (void) close(fd); return(errflg ? 1 : 0); }
int main(int argc, char **argv) { int c; int hflag = 0, cflag = 0, wflag = 0, nflag = 0; int count = 0, waittime = 0; int headercount; int num_devices_specified; int havelast = 0; maxshowdevs = 3; while ((c = getopt(argc, argv, "c:CdIKM:n:oTw:?")) != -1) { switch(c) { case 'c': cflag++; count = atoi(optarg); if (count < 1) errx(1, "count %d is < 1", count); break; case 'C': Cflag++; break; case 'd': dflag++; break; case 'I': Iflag++; break; case 'K': Kflag++; break; case 'n': nflag++; maxshowdevs = atoi(optarg); if (maxshowdevs < 0) errx(1, "number of devices %d is < 0", maxshowdevs); break; case 'o': oflag++; break; case 'T': Tflag++; break; case 'w': wflag++; waittime = atoi(optarg); if (waittime < 1) errx(1, "wait time is < 1"); break; default: usage(); exit(1); break; } } argc -= optind; argv += optind; /* * Get the Mach private port. */ host_priv_port = mach_host_self(); /* * Get the I/O Kit communication handle. */ IOMasterPort(bootstrap_port, &masterPort); /* * Make sure Tflag and/or Cflag are set if dflag == 0. If dflag is * greater than 0, they may be 0 or non-zero. */ if (dflag == 0) { Cflag = 1; Tflag = 1; } /* * Figure out how many devices we should display if not given * an explicit value. */ if (nflag == 0) { if (oflag > 0) { if ((dflag > 0) && (Cflag == 0) && (Tflag == 0)) maxshowdevs = 5; else if ((dflag > 0) && (Tflag > 0) && (Cflag == 0)) maxshowdevs = 5; else maxshowdevs = 4; } else { if ((dflag > 0) && (Cflag == 0)) maxshowdevs = 4; else maxshowdevs = 3; } } /* * If the user specified any devices on the command line, record * them for monitoring. */ for (num_devices_specified = 0; *argv; ++argv) { if (isdigit(**argv)) break; if (record_one_device(*argv)) errx(1, "can't record '%s' for monitoring"); num_devices_specified++; } if (nflag == 0 && maxshowdevs < num_devices_specified) maxshowdevs = num_devices_specified; /* if no devices were specified, pick them ourselves */ if ((num_devices_specified == 0) && record_all_devices()) err(1, "can't find any devices to display"); /* * Look for the traditional wait time and count arguments. */ if (*argv) { waittime = atoi(*argv); /* Let the user know he goofed, but keep going anyway */ if (wflag != 0) warnx("discarding previous wait interval, using" " %d instead", waittime); wflag++; if (*++argv) { count = atoi(*argv); if (cflag != 0) warnx("discarding previous count, using %d" " instead", count); cflag++; } else count = -1; } /* * If the user specified a count, but not an interval, we default * to an interval of 1 second. */ if ((wflag == 0) && (cflag > 0)) waittime = 1; /* * If the user specified a wait time, but not a count, we want to * go on ad infinitum. This can be redundant if the user uses the * traditional method of specifying the wait, since in that case we * already set count = -1 above. Oh well. */ if ((wflag > 0) && (cflag == 0)) count = -1; cur.tk_nout = 0; cur.tk_nin = 0; /* * Set the busy time to the system boot time, so the stats are * calculated since system boot. */ if (readvar("kern.boottime", &cur_time, sizeof(cur_time)) != 0) exit(1); /* * If the user stops the program (control-Z) and then resumes it, * print out the header again. */ (void)signal(SIGCONT, phdr); for (headercount = 1;;) { long tmp; long double etime; if (Tflag > 0) { if ((readvar("kern.tty_nin", &cur.tk_nin, sizeof(cur.tk_nin)) != 0) || (readvar("kern.tty_nout", &cur.tk_nout, sizeof(cur.tk_nout))!= 0)) { Tflag = 0; warnx("disabling TTY statistics"); } } if (phdr_flag) { phdr_flag = 0; do_phdr(); } if (!--headercount) { do_phdr(); headercount = 20; } last_time = cur_time; gettimeofday(&cur_time, NULL); if (Tflag > 0) { tmp = cur.tk_nin; cur.tk_nin -= last.tk_nin; last.tk_nin = tmp; tmp = cur.tk_nout; cur.tk_nout -= last.tk_nout; last.tk_nout = tmp; } etime = compute_etime(cur_time, last_time); if (etime == 0.0) etime = 1.0; if (Tflag > 0) printf("%4.0Lf%5.0Lf", cur.tk_nin / etime, cur.tk_nout / etime); devstats(hflag, etime, havelast); if (Cflag > 0) cpustats(); printf("\n"); fflush(stdout); if (count >= 0 && --count <= 0) break; sleep(waittime); havelast = 1; } exit(0); }
int main(int argc, char **argv) { int c, i; int tflag = 0, hflag = 0, cflag = 0, wflag = 0, nflag = 0; int count = 0, waittime = 0; char *memf = NULL, *nlistf = NULL; struct devstat_match *matches; struct itimerval alarmspec; int num_matches = 0; char errbuf[_POSIX2_LINE_MAX]; kvm_t *kd = NULL; long generation; int num_devices_specified; int num_selected, num_selections; long select_generation; char **specified_devices; devstat_select_mode select_mode; float f; int havelast = 0; matches = NULL; maxshowdevs = 3; while ((c = getopt(argc, argv, "c:CdhIKM:n:N:ot:Tw:xz?")) != -1) { switch(c) { case 'c': cflag++; count = atoi(optarg); if (count < 1) errx(1, "count %d is < 1", count); break; case 'C': Cflag++; break; case 'd': dflag++; break; case 'h': hflag++; break; case 'I': Iflag++; break; case 'K': Kflag++; break; case 'M': memf = optarg; break; case 'n': nflag++; maxshowdevs = atoi(optarg); if (maxshowdevs < 0) errx(1, "number of devices %d is < 0", maxshowdevs); break; case 'N': nlistf = optarg; break; case 'o': oflag++; break; case 't': tflag++; if (devstat_buildmatch(optarg, &matches, &num_matches) != 0) errx(1, "%s", devstat_errbuf); break; case 'T': Tflag++; break; case 'w': wflag++; f = atof(optarg); waittime = f * 1000; if (waittime < 1) errx(1, "wait time is < 1ms"); break; case 'x': xflag++; break; case 'z': zflag++; break; default: usage(); exit(1); break; } } argc -= optind; argv += optind; if (nlistf != NULL || memf != NULL) { kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, errbuf); if (kd == NULL) errx(1, "kvm_openfiles: %s", errbuf); if (kvm_nlist(kd, namelist) == -1) errx(1, "kvm_nlist: %s", kvm_geterr(kd)); } /* * Make sure that the userland devstat version matches the kernel * devstat version. If not, exit and print a message informing * the user of his mistake. */ if (devstat_checkversion(kd) < 0) errx(1, "%s", devstat_errbuf); /* * Make sure Tflag and/or Cflag are set if dflag == 0. If dflag is * greater than 0, they may be 0 or non-zero. */ if (dflag == 0 && xflag == 0) { Cflag = 1; Tflag = 1; } /* find out how many devices we have */ if ((num_devices = devstat_getnumdevs(kd)) < 0) err(1, "can't get number of devices"); /* * Figure out how many devices we should display. */ if (nflag == 0) { if (xflag > 0) maxshowdevs = num_devices; else if (oflag > 0) { if ((dflag > 0) && (Cflag == 0) && (Tflag == 0)) maxshowdevs = 5; else if ((dflag > 0) && (Tflag > 0) && (Cflag == 0)) maxshowdevs = 5; else maxshowdevs = 4; } else { if ((dflag > 0) && (Cflag == 0)) maxshowdevs = 4; else maxshowdevs = 3; } } cur.dinfo = (struct devinfo *)calloc(1, sizeof(struct devinfo)); if (cur.dinfo == NULL) err(1, "calloc failed"); last.dinfo = (struct devinfo *)calloc(1, sizeof(struct devinfo)); if (last.dinfo == NULL) err(1, "calloc failed"); /* * Grab all the devices. We don't look to see if the list has * changed here, since it almost certainly has. We only look for * errors. */ if (devstat_getdevs(kd, &cur) == -1) errx(1, "%s", devstat_errbuf); num_devices = cur.dinfo->numdevs; generation = cur.dinfo->generation; /* * If the user specified any devices on the command line, see if * they are in the list of devices we have now. */ specified_devices = (char **)malloc(sizeof(char *)); if (specified_devices == NULL) err(1, "malloc failed"); for (num_devices_specified = 0; *argv; ++argv) { if (isdigit(**argv)) break; num_devices_specified++; specified_devices = (char **)realloc(specified_devices, sizeof(char *) * num_devices_specified); if (specified_devices == NULL) err(1, "realloc failed"); specified_devices[num_devices_specified - 1] = *argv; } if (nflag == 0 && maxshowdevs < num_devices_specified) maxshowdevs = num_devices_specified; dev_select = NULL; if ((num_devices_specified == 0) && (num_matches == 0)) select_mode = DS_SELECT_ADD; else select_mode = DS_SELECT_ONLY; /* * At this point, selectdevs will almost surely indicate that the * device list has changed, so we don't look for return values of 0 * or 1. If we get back -1, though, there is an error. */ if (devstat_selectdevs(&dev_select, &num_selected, &num_selections, &select_generation, generation, cur.dinfo->devices, num_devices, matches, num_matches, specified_devices, num_devices_specified, select_mode, maxshowdevs, hflag) == -1) errx(1, "%s", devstat_errbuf); /* * Look for the traditional wait time and count arguments. */ if (*argv) { f = atof(*argv); waittime = f * 1000; /* Let the user know he goofed, but keep going anyway */ if (wflag != 0) warnx("discarding previous wait interval, using" " %g instead", waittime / 1000.0); wflag++; if (*++argv) { count = atoi(*argv); if (cflag != 0) warnx("discarding previous count, using %d" " instead", count); cflag++; } else count = -1; } /* * If the user specified a count, but not an interval, we default * to an interval of 1 second. */ if ((wflag == 0) && (cflag > 0)) waittime = 1 * 1000; /* * If the user specified a wait time, but not a count, we want to * go on ad infinitum. This can be redundant if the user uses the * traditional method of specifying the wait, since in that case we * already set count = -1 above. Oh well. */ if ((wflag > 0) && (cflag == 0)) count = -1; bzero(cur.cp_time, sizeof(cur.cp_time)); cur.tk_nout = 0; cur.tk_nin = 0; /* * Set the snap time to the system boot time (ie: zero), so the * stats are calculated since system boot. */ cur.snap_time = 0; /* * If the user stops the program (control-Z) and then resumes it, * print out the header again. */ (void)signal(SIGCONT, needhdr); /* * If our standard output is a tty, then install a SIGWINCH handler * and set wresized so that our first iteration through the main * iostat loop will peek at the terminal's current rows to find out * how many lines can fit in a screenful of output. */ if (isatty(fileno(stdout)) != 0) { wresized = 1; (void)signal(SIGWINCH, needresize); } else { wresized = 0; wrows = IOSTAT_DEFAULT_ROWS; } /* * Register a SIGINT handler so that we can print out final statistics * when we get that signal */ (void)signal(SIGINT, needreturn); /* * Register a SIGALRM handler to implement sleeps if the user uses the * -c or -w options */ (void)signal(SIGALRM, alarm_clock); alarmspec.it_interval.tv_sec = waittime / 1000; alarmspec.it_interval.tv_usec = 1000 * (waittime % 1000); alarmspec.it_value.tv_sec = waittime / 1000; alarmspec.it_value.tv_usec = 1000 * (waittime % 1000); setitimer(ITIMER_REAL, &alarmspec, NULL); for (headercount = 1;;) { struct devinfo *tmp_dinfo; long tmp; long double etime; sigset_t sigmask, oldsigmask; if (Tflag > 0) { if ((readvar(kd, "kern.tty_nin", X_TTY_NIN, &cur.tk_nin, sizeof(cur.tk_nin)) != 0) || (readvar(kd, "kern.tty_nout", X_TTY_NOUT, &cur.tk_nout, sizeof(cur.tk_nout))!= 0)) { Tflag = 0; warnx("disabling TTY statistics"); } } if (Cflag > 0) { if (kd == NULL) { if (readvar(kd, "kern.cp_time", 0, &cur.cp_time, sizeof(cur.cp_time)) != 0) Cflag = 0; } else { if (kvm_getcptime(kd, cur.cp_time) < 0) { warnx("kvm_getcptime: %s", kvm_geterr(kd)); Cflag = 0; } } if (Cflag == 0) warnx("disabling CPU time statistics"); } if (!--headercount) { phdr(); if (wresized != 0) doresize(); headercount = wrows; } tmp_dinfo = last.dinfo; last.dinfo = cur.dinfo; cur.dinfo = tmp_dinfo; last.snap_time = cur.snap_time; /* * Here what we want to do is refresh our device stats. * devstat_getdevs() returns 1 when the device list has changed. * If the device list has changed, we want to go through * the selection process again, in case a device that we * were previously displaying has gone away. */ switch (devstat_getdevs(kd, &cur)) { case -1: errx(1, "%s", devstat_errbuf); break; case 1: { int retval; num_devices = cur.dinfo->numdevs; generation = cur.dinfo->generation; retval = devstat_selectdevs(&dev_select, &num_selected, &num_selections, &select_generation, generation, cur.dinfo->devices, num_devices, matches, num_matches, specified_devices, num_devices_specified, select_mode, maxshowdevs, hflag); switch(retval) { case -1: errx(1, "%s", devstat_errbuf); break; case 1: phdr(); if (wresized != 0) doresize(); headercount = wrows; break; default: break; } break; } default: break; } /* * We only want to re-select devices if we're in 'top' * mode. This is the only mode where the devices selected * could actually change. */ if (hflag > 0) { int retval; retval = devstat_selectdevs(&dev_select, &num_selected, &num_selections, &select_generation, generation, cur.dinfo->devices, num_devices, matches, num_matches, specified_devices, num_devices_specified, select_mode, maxshowdevs, hflag); switch(retval) { case -1: errx(1,"%s", devstat_errbuf); break; case 1: phdr(); if (wresized != 0) doresize(); headercount = wrows; break; default: break; } } if (Tflag > 0) { tmp = cur.tk_nin; cur.tk_nin -= last.tk_nin; last.tk_nin = tmp; tmp = cur.tk_nout; cur.tk_nout -= last.tk_nout; last.tk_nout = tmp; } etime = cur.snap_time - last.snap_time; if (etime == 0.0) etime = 1.0; for (i = 0; i < CPUSTATES; i++) { tmp = cur.cp_time[i]; cur.cp_time[i] -= last.cp_time[i]; last.cp_time[i] = tmp; } if (xflag == 0 && Tflag > 0) printf("%4.0Lf %5.0Lf", cur.tk_nin / etime, cur.tk_nout / etime); devstats(hflag, etime, havelast); if (xflag == 0) { if (Cflag > 0) cpustats(); printf("\n"); } fflush(stdout); if ((count >= 0 && --count <= 0) || return_requested) break; /* * Use sigsuspend to safely sleep until either signal is * received */ alarm_rang = 0; sigemptyset(&sigmask); sigaddset(&sigmask, SIGINT); sigaddset(&sigmask, SIGALRM); sigprocmask(SIG_BLOCK, &sigmask, &oldsigmask); while (! (alarm_rang || return_requested) ) { sigsuspend(&oldsigmask); } sigprocmask(SIG_UNBLOCK, &sigmask, NULL); havelast = 1; } exit(0); }
// Parse MIDI file int parsemidi(char * filename) { FILE * f = fopen(filename,"rb"); if (!f) return -1; int len; int id = loadchunkheader(f,len); printf("%08x %d\n",id,len); if (id != 'MThd') { printf("Bad header id\n"); fclose(f); return -1; } if (len < 6) { printf("Bad header block length\n"); fclose(f); return -1; } int format = readword(f); printf("format %d\n", format); if (format != 1 && format != 0) { printf("Unsupported format\n"); fclose(f); return -1; } int tracks = readword(f); printf("tracks %d\n", tracks); int ppqn = readword(f); printf("ppqn %d\n",ppqn); // pulses (clocks) per quater note if (ppqn < 0) { printf("negative ppqn formats not supported\n"); fclose(f); return -1; } if (len > 6) { while (len > 6) { fgetc(f); len--; } } int uspertick = (500000 / ppqn); while (!feof(f) && tracks) { id = loadchunkheader(f,len); if (id != 'MTrk') { printf("Unknown chunk\n"); fclose(f); return -1; } printf("\nNew track, length %d\n",len); int trackend = 0; int command = 0; int time = 0; while (!trackend) { int dtime = readvar(f); time += dtime; printf("%3.3f ",((float)time * (float)uspertick)/1000000.0f); int data1 = fgetc(f); if (data1 == 0xff) { data1 = fgetc(f); // sub-command int len = readvar(f); switch (data1) { case 1: case 2: case 3: case 4: case 5: case 6: case 7: case 8: case 9: switch (data1) { case 1: printf("Text:\""); break; case 2: printf("Copyright:\""); break; case 3: printf("Track name:\""); break; case 4: printf("Instrument:\""); break; case 5: printf("Lyric:\""); break; case 6: printf("Marker:\""); break; case 7: printf("Cue point:\""); break; case 8: printf("Patch name:\""); break; case 9: printf("Port name:\""); break; } while (len) { printf("%c",fgetc(f)); len--; } printf("\"\n"); break; case 0x2f: { trackend = 1; printf("Track end\n"); } break; case 0x58: // time signature { int nn = fgetc(f); int dd = fgetc(f); int cc = fgetc(f); int bb = fgetc(f); printf("Time sig: %d:%d, metronome:%d, quarter:%d\n",nn,dd,cc,bb); } break; case 0x59: // key signature { int sf = fgetc(f); int mi = fgetc(f); printf("Key sig: %d %s, %s\n",abs(sf),sf == 0?"c":(sf < 0 ? "flat":"sharp"), mi?"minor":"major"); } break; case 0x51: // tempo { int t = 0; t = fgetc(f) << 16; t |= fgetc(f) << 8; t |= fgetc(f); printf("Tempo: quarter is %dus (%3.3fs) long - BPM = %3.3f\n",t,t/1000000.0f, 60000000.0f/t); uspertick = t / ppqn; } break; case 0x21: // obsolete: midi port { int pp = fgetc(f); printf("[obsolete] midi port: %d\n",pp); } break; case 0x20: // obsolete: midi channel { int cc = fgetc(f); printf("[obsolete] midi channel: %d\n",cc); } break; case 0x54: // SMPTE offset { int hr = fgetc(f); int mn = fgetc(f); int se = fgetc(f); int fr = fgetc(f); int ff = fgetc(f); printf("SMPTE Offset: %dh %dm %ds %dfr %dff\n",hr,mn,se,fr,ff); } break; case 0x7f: // Proprietary event { printf("Proprietary event "); while (len) { int d = fgetc(f); printf("%02X ",d); len--; } printf("\n"); } break; default: printf("meta command %02x %d\n", data1, len); while (len) { fgetc(f); len--; } } } else { if (data1 & 0x80) // new command? { command = data1; data1 = fgetc(f); } int chan = command & 0xf; int chan_masked = ((1 << chan) & channelmask) != 0; switch (command & 0xf0) { case 0x80: // note off { int data2 = fgetc(f); if (!chan_masked) { printf("(masked)\n"); } else { int chan = command & 0xf; int chan_masked = ((1 << chan) & channelmask) != 0; printf("Note off: channel %d (%d), Oct %d Note %s Velocity %d\n",chan, chan_masked, (data1/12)-1,note[data1%12], data2); song[currentnote].volume = 0; song[currentnote].note = data1; //volume = data2; song[currentnote].tick = (int)((((float)time * (float)uspertick)/1000000.0f)*TICK_CALC_HZ_VALUE); song[currentnote].inputchannel = chan; currentnote++; inputchannel[chan].noteoffcount++; } } break; case 0x90: // note on { int data2 = fgetc(f); if (!chan_masked) { printf("(masked)\n"); } else { int chan = command & 0xf; int chan_masked = ((1 << chan) & channelmask) != 0; data1 += note_offset; if (data1 < 0) { data1 = 0; data2 = 0; } if (data1 > 255) { data1 = 0; data2 = 0; } if (data1 == 0 && data2 == 0) { printf("(out of range)\n"); } else { printf("Note on: channel %d (%d), Oct %d Note %s Velocity %d%s\n",chan, chan_masked, (data1/12)-1,note[data1%12], data2, data1 >= 12*8?" (out of range)":""); song[currentnote].note = data1; song[currentnote].volume = data2; song[currentnote].tick = (int)((((float)time * (float)uspertick)/1000000.0f)*TICK_CALC_HZ_VALUE); song[currentnote].inputchannel = chan; currentnote++; if (data2 >= min_vol) inputchannel[chan].notecount++; else inputchannel[chan].noteoffcount++; if (inputchannel[chan].min_note > data1) inputchannel[chan].min_note = data1; if (inputchannel[chan].max_note < data1) inputchannel[chan].max_note = data1; } } } break; case 0xa0: // Note aftertouch { int data2 = fgetc(f); printf("Aftertouch: channel %d, Oct %d, Note %s Aftertouch %d\n",command & 0xf, (data1/12)-1,note[data1%12], data2); } break; case 0xb0: // Controller { int data2 = fgetc(f); printf("Controller: channel %d, Controller (%d) %s Value %d\n",command & 0xf, data1, controller[data1], data2); if (data1 == 64) hold = data2; } break; case 0xc0: // program change { printf("Program change: channel %d, program %d\n",command & 0xf, data1); } break; case 0xd0: // Channel aftertouch { printf("Channel aftertouch: channel %d, Aftertouch %d\n",command & 0xf, data1); } break; case 0xe0: // Pitch bend { int data2 = fgetc(f); printf("Pitchbend: channel %d, Pitch %d\n",command & 0xf, data1 + (data2 << 7)); } break; case 0xf0: // general / immediate { switch (command) { case 0xf0: // SysEx { printf("SysEx "); while (data1 != 0xf7) { printf("%02X ", data1); data1 = fgetc(f); } printf("\n"); // universal sysexes of note: // f0 (05) 7e 7f 09 01 f7 = "general midi enable" // f0 (05) 7e 7f 09 00 f7 = "general midi disable" // f0 (07) 7f 7f 04 01 ll mm f7 = "master volume", ll mm = 14bit value // spec doesn't say that the length byte should be there, // but it appears to be (the ones in brackets) } break; case 0xf1: // MTC quater frame { int dd = fgetc(f); printf("MTC quater frame %d\n",dd); } break; case 0xf2: // Song position pointer { int data1 = fgetc(f); int data2 = fgetc(f); printf("Song position pointer %d\n", data1 + (data2 << 7)); } break; case 0xf3: // Song select { int song = fgetc(f); printf("Song select %d\n", song); } break; case 0xf6: // Tuning request printf("Tuning request\n"); break; case 0xf8: // MIDI clock printf("MIDI clock\n"); break; case 0xf9: // MIDI Tick printf("MIDI Tick\n"); break; case 0xfa: // MIDI start printf("MIDI start\n"); break; case 0xfc: printf("MIDI stop\n"); break; case 0xfb: printf("MIDI continue\n"); break; case 0xfe: printf("Active sense\n"); break; case 0xff: printf("Reset\n"); break; default: { printf("Unknown: command 0x%02x, data 0x%02x\n", command, data1); } break; } } break; default: { printf("Unknown: command 0x%02x, data 0x%02x\n", command, data1); } break; } } } tracks--; } fclose(f); return 0; }
int execute(TREPTR argt, int execflg, int *pf1, int *pf2) { /* `stakbot' is preserved by this routine */ register TREPTR t; STKPTR sav = savstak(); sigchk(); if ((t = argt) && execbrk == 0) { register int treeflgs; int oldexit, type; register char **com; treeflgs = t->tretyp; type = treeflgs & COMMSK; oldexit = exitval; exitval = 0; switch (type) { case TCOM: { STRING a1; int argn, internal; ARGPTR schain = gchain; IOPTR io = t->treio; gchain = 0; argn = getarg((void *)t);/*FIXME*/ com = scan(argn); a1 = com[1]; gchain = schain; if ((internal = syslook(com[0], commands)) || argn == 0) setlist(((COMPTR) t)->comset, 0); if (argn && (flags & noexec) == 0) { /* print command if execpr */ if (flags & execpr) { argn = 0; prs(execpmsg); while (com[argn] != ENDARGS) { prs(com[argn++]); blank(); } newline(); } switch (internal) { case SYSDOT: if (a1) { register int f; if ((f = pathopen(getpath(a1), a1)) < 0) failed(a1, notfound); else execexp(0, f); } break; case SYSTIMES: { struct tms t; times(&t); prt(t.tms_cutime); blank(); prt(t.tms_cstime); newline(); } break; case SYSEXIT: exitsh(a1 ? stoi(a1) : oldexit); case SYSNULL: io = 0; break; case SYSCONT: execbrk = -loopcnt; break; case SYSBREAK: if ((execbrk = loopcnt) && a1) breakcnt = stoi(a1); break; case SYSTRAP: if (a1) { BOOL clear; if ((clear = digit(*a1)) == 0) ++com; while (*++com) { int i; if ((i = stoi(*com)) >= MAXTRAP || i < MINTRAP) failed(*com, badtrap); else if (clear) clrsig(i); else { replace(&trapcom[i], a1); if (*a1) getsig(i); else ignsig(i); } } } else { /* print out current traps */ int i; for (i = 0; i < MAXTRAP; i++) { if (trapcom[i]) { prn(i); prs(colon); prs(trapcom[i]); newline(); } } } break; case SYSEXEC: com++; initio(io); ioset = 0; io = 0; if (a1 == 0) break; case SYSLOGIN: flags |= forked; oldsigs(); execa((const char **)com); done(); case SYSCD: if (flags & rshflg) failed(com[0], restricted); else if ((a1 == 0 && (a1 = (char *)homenod.namval) == 0) || chdir(a1) < 0) /* FIXME */ failed(a1, baddir); break; case SYSSHFT: if (dolc < 1) error(badshift); else { dolv++; dolc--; } assnum(&dolladr, dolc); break; case SYSWAIT: await(-1); break; case SYSREAD: exitval = readvar(&com[1]); break; /* case SYSTST: exitval=testcmd(com); break; */ case SYSSET: if (a1) { int argc; argc = options(argn, (const char **)com); if (argc > 1) setargs((const char **)com + argn - argc); } else if (((COMPTR) t)->comset == 0) /* Scan name chain and print */ namscan(printnam); break; case SYSRDONLY: exitval = N_RDONLY; case SYSXPORT: if (exitval == 0) exitval = N_EXPORT;; if (a1) { while (*++com) attrib(lookup(*com), exitval); } else { namscan(printflg); } exitval = 0; break; case SYSEVAL: if (a1) execexp(a1, (UFD)&com[2]); /* FIXME */ break; case SYSUMASK: if (a1) { int c, i; i = 0; while ((c = *a1++) >= '0' && c <= '7') i = (i << 3) + c - '0'; umask(i); } else { int i, j; umask(i = umask(0)); prc('0'); for (j = 6; j >= 0; j -= 3) prc(((i >> j) & 07) + '0'); newline(); } break; default: internal = builtin(argn, com); } if (internal) { if (io) error(illegal); chktrap(); break; } } else if (t->treio == 0) break; } case TFORK: if (execflg && (treeflgs & (FAMP | FPOU)) == 0) parent = 0; else { while ((parent = fork()) == -1) { sigchk(); alarm(10); pause(); } } if (parent) { /* This is the parent branch of fork; */ /* it may or may not wait for the child. */ if (treeflgs & FPRS && flags & ttyflg) { prn(parent); newline(); } if (treeflgs & FPCL) closepipe(pf1); if ((treeflgs & (FAMP | FPOU)) == 0) await(parent); else if ((treeflgs & FAMP) == 0) post(parent); else assnum(&pcsadr, parent); chktrap(); break; } else { /* this is the forked branch (child) of execute */ flags |= forked; iotemp = 0; postclr(); settmp(); /* Turn off INTR and QUIT if `FINT' */ /* Reset ramaining signals to parent */ /* except for those `lost' by trap */ oldsigs(); if (treeflgs & FINT) { signal(INTR, SIG_IGN); signal(QUIT, SIG_IGN); } /* pipe in or out */ if (treeflgs & FPIN) { sh_rename(pf1[INPIPE], 0); close(pf1[OTPIPE]); } if (treeflgs & FPOU) { sh_rename(pf2[OTPIPE], 1); close(pf2[INPIPE]); } /* default std input for & */ if (treeflgs & FINT && ioset == 0) sh_rename(chkopen(devnull), 0); /* io redirection */ initio(t->treio); if (type != TCOM) execute(((FORKPTR) t)->forktre, 1, NULL, NULL); else if (com[0] != ENDARGS) { setlist(((COMPTR) t)->comset, N_EXPORT); execa((const char **)com); } done(); } case TPAR: sh_rename(dup(2), output); execute(((PARPTR) t)->partre, execflg, NULL, NULL); done(); case TFIL: { int pv[2]; chkpipe(pv); if (execute(((LSTPTR) t)->lstlef, 0, pf1, pv) == 0) execute(((LSTPTR) t)->lstrit, execflg, pv, pf2); else closepipe(pv); break; } case TLST: execute(((LSTPTR) t)->lstlef, 0, NULL, NULL); execute(((LSTPTR) t)->lstrit, execflg, NULL, NULL); break; case TAND: if (execute(((LSTPTR) t)->lstlef, 0, NULL, NULL) == 0) execute(((LSTPTR) t)->lstrit, execflg, NULL, NULL); break; case TORF: if (execute(((LSTPTR) t)->lstlef, 0, NULL, NULL) != 0) execute(((LSTPTR) t)->lstrit, execflg, NULL, NULL); break; case TFOR: { NAMPTR n = lookup(((FORPTR) t)->fornam); char **args; DOLPTR argsav = 0; if (((FORPTR) t)->forlst == 0) { args = (char **)dolv + 1; argsav = useargs(); } else { ARGPTR schain = gchain; gchain = 0; trim((args = scan(getarg(((FORPTR) t)->forlst)))[0]); gchain = schain; } loopcnt++; while (*args != ENDARGS && execbrk == 0) { assign(n, *args++); execute(((FORPTR) t)->fortre, 0, NULL, NULL); if (execbrk < 0) { execbrk = 0; } } if (breakcnt) breakcnt--; execbrk = breakcnt; loopcnt--; argfor = freeargs(argsav); break; } case TWH: case TUN: { int i = 0; loopcnt++; while (execbrk == 0 && (execute(((WHPTR) t)->whtre, 0, NULL, NULL) == 0) == (type == TWH)) { i = execute(((WHPTR) t)->dotre, 0, NULL, NULL); if (execbrk < 0) execbrk = 0; } if (breakcnt) breakcnt--; execbrk = breakcnt; loopcnt--; exitval = i; break; } case TIF: if (execute(((IFPTR) t)->iftre, 0, NULL, NULL) == 0) execute(((IFPTR) t)->thtre, execflg, NULL, NULL); else execute(((IFPTR) t)->eltre, execflg, NULL, NULL); break; case TSW: { register char *r = mactrim(((SWPTR) t)->swarg); t = (TREPTR) ((SWPTR) t)->swlst; while (t) { ARGPTR rex = ((REGPTR) t)->regptr; while (rex) { register char *s; if (gmatch(r, s = macro(rex->argval)) || (trim(s), eq(r, s))) { execute(((REGPTR)t)->regcom, 0, NULL, NULL); t = 0; break; } else rex = ((ARGPTR)rex)->argnxt; } if (t) t = (TREPTR) ((REGPTR) t)->regnxt; } } break; } exitset(); }