Ejemplo n.º 1
0
/* reverse input lines, a line at a time*/
int main() {
    char line[MAXLINE];     /* curreent input line */
    while ( rline(line, MAXLINE) > 0 )
        if (reverse(line) > 0)
            printf("%s", line);
    return 0;
}
Ejemplo n.º 2
0
Archivo: main.c Proyecto: 99years/plan9
double
readdt(void)
{

	fprint(2, "ΔT (sec) (%.3f)\n", deltat);
	rline(0);
	return atof(skip(0));
}
Ejemplo n.º 3
0
Archivo: main.c Proyecto: 99years/plan9
void
readlat(int f)
{

	rline(f);
	nlat = atof(skip(0)) * radian;
	awlong = atof(skip(1)) * radian;
	elev = atof(skip(2)) * 3.28084;
}
Ejemplo n.º 4
0
void polyline(int n,int *tp,int c)
{
	int i;

	if (c>=0) setcolor(c);
	for(i=0;i<n-1;i++)
		rline(tp[i*2],tp[i*2+1],tp[i*2+2],tp[i*2+3],-1);

}
Ejemplo n.º 5
0
Archivo: main.c Proyecto: 99years/plan9
double
readate(void)
{
	int i;
	Tim t;

	fprint(2, "year mo da hr min\n");
	rline(0);
	for(i=0; i<5; i++)
		t.ifa[i] = atof(skip(i));
	return convdate(&t);
}
Ejemplo n.º 6
0
int main(int argc,char *argv[]){
  vqgen v;
  static_codebook c;
  codebook b;
  quant_meta q;

  long *quantlist=NULL;
  int entries=-1,dim=-1,aux=-1;
  FILE *out=NULL;
  FILE *in=NULL;
  char *line,*name;
  long i,j,k;

  b.c=&c;

  if(argv[1]==NULL){
    fprintf(stderr,"Need a trained data set on the command line.\n");
    exit(1);
  }

  {
    char *ptr;
    char *filename=strdup(argv[1]);

    in=fopen(filename,"r");
    if(!in){
      fprintf(stderr,"Could not open input file %s\n",filename);
      exit(1);
    }
    
    ptr=strrchr(filename,'-');
    if(ptr){
      *ptr='\0';
      name=strdup(filename);
      sprintf(ptr,".vqh");
    }else{
      name=strdup(filename);
      strcat(filename,".vqh");
    }

    out=fopen(filename,"w");
    if(out==NULL){
      fprintf(stderr,"Unable to open %s for writing\n",filename);
      exit(1);
    }
  }

  /* suck in the trained book */

  /* read book type, but it doesn't matter */
  line=rline(in,out);
  
  line=rline(in,out);
  if(sscanf(line,"%d %d %d",&entries,&dim,&aux)!=3){
    fprintf(stderr,"Syntax error reading book file\n");
    exit(1);
  }
  
  /* just use it to allocate mem */
  vqgen_init(&v,dim,0,entries,0.f,NULL,NULL,0);
  
  /* quant */
  line=rline(in,out);
  if(sscanf(line,"%ld %ld %d %d",&q.min,&q.delta,
	    &q.quant,&q.sequencep)!=4){
    fprintf(stderr,"Syntax error reading book file\n");
    exit(1);
  }
  
  /* quantized entries */
  /* save quant data; we don't want to requantize later as our method
     is currently imperfect wrt repeated application */
  i=0;
  quantlist=_ogg_malloc(sizeof(long)*v.elements*v.entries);
  for(j=0;j<entries;j++){
    float a;
    for(k=0;k<dim;k++){
      line=rline(in,out);
      sscanf(line,"%f",&a);
      v.entrylist[i]=a;
      quantlist[i++]=rint(a);
    }
  }    
  
  /* ignore bias */
  for(j=0;j<entries;j++)line=rline(in,out);
  free(v.bias);
  v.bias=NULL;
  
  /* training points */
  {
    float *b=alloca(sizeof(float)*(dim+aux));
    i=0;
    v.entries=0; /* hack to avoid reseeding */
    while(1){
      for(k=0;k<dim+aux;k++){
	line=rline(in,out);
	if(!line)break;
	sscanf(line,"%f",b+k);
      }
      if(feof(in))break;
      vqgen_addpoint(&v,b,NULL);
    }
    v.entries=entries;
  }
  
  fclose(in);
  vqgen_unquantize(&v,&q);

  /* build the book */
  vqsp_book(&v,&b,quantlist);
  c.q_min=q.min;
  c.q_delta=q.delta;
  c.q_quant=q.quant;
  c.q_sequencep=q.sequencep;

  /* save the book in C header form */
  write_codebook(out,name,b.c);

  fclose(out);
  exit(0);
}
Ejemplo n.º 7
0
void
satels(void)
{
	double ifa[10], t, t1, t2, tinc;
	char **satp;
	int flag, f, i, n;

	satp = satlst;

loop:
	if(*satp == 0)
		return;
	f = open(*satp, 0);
	if(f < 0) {
		fprint(2, "cannot open %s\n", *satp);
		satp += 2;
		goto loop;
	}
	satp++;
	rline(f);
	tinc = atof(skip(6));
	rline(f);
	rline(f);
	for(i=0; i<9; i++)
		ifa[i] = atof(skip(i));
	n = ifa[0];
	i = ifa[1];
	t = dmo[i-1] + ifa[2] - 1.;
	if(n%4 == 0 && i > 2)
		t += 1.;
	for(i=1970; i<n; i++) {
		t += 365.;
		if(i%4 == 0)
			t += 1.;
	}
	t = (t * 24. + ifa[3]) * 60. + ifa[4];
	satl.time = t * 60.;
	satl.tilt = ifa[5] * radian;
	satl.pnni = ifa[6] * radian;
	satl.psi = ifa[7];
	satl.ppi = ifa[8] * radian;
	rline(f);
	for(i=0; i<5; i++)
		ifa[i] = atof(skip(i));
	satl.d1pp = ifa[0] * radian;
	satl.peri = ifa[1];
	satl.d1per = ifa[2];
	satl.e0 = ifa[3];
	satl.deo = 0;
	satl.rdp = ifa[4];

	satl.st = sin(satl.tilt);
	satl.ct = cos(satl.tilt);
	satl.rot = pipi / (1440. + satl.psi);
	satl.semi = satl.rdp * (1. + satl.e0);

	n = PER*288.; /* 5 min steps */
	t = day;
	for(i=0; i<n; i++) {
		if(sunel((t-day)/deld) > 0.)
			goto out;
		satel(t);
		if(el > 0) {
			t1 = t;
			flag = 0;
			do {
				if(el > 30.)
					flag++;
				t -= tinc/(24.*3600.);
				satel(t);
			} while(el > 0.);
			t2 = (t - day)/deld;
			t = t1;
			do {
				t += tinc/(24.*3600.);
				satel(t);
				if(el > 30.)
					flag++;
			} while(el > 0.);
			if(flag)
				if((*satp)[0] == '-')
					event("%s pass at ", (*satp)+1, "",
						t2, SIGNIF+PTIME+DARK); else
					event("%s pass at ", *satp, "",
						t2, PTIME+DARK);
		}
	out:
		t += 5./(24.*60.);
	}
	close(f);
	satp++;
	goto loop;
}
Ejemplo n.º 8
0
/*
 * fcomp
 */
static int fcomp(sed_commands_t *commands, PRFileDesc *fin)
{
    char *p, *op, *tp;
    sed_reptr_t *pt, *pt1;
    int i, ii;
    sed_label_t *lpt;
    char fnamebuf[PATH_MAX];
    PRStatus status;
    sed_comp_args compargs;

    op = commands->lastre;
    if (!commands->linebuf) {
        commands->linebuf = (char *)pool_calloc(commands->pool, LBSIZE + 1, 1);
    }

    if (rline(commands, fin, commands->linebuf,
              (commands->linebuf + LBSIZE + 1)) < 0)
        return 0;
    if (*commands->linebuf == '#') {
        if (commands->linebuf[1] == 'n')
            commands->nflag = 1;
    }
    else {
        commands->cp = commands->linebuf;
        goto comploop;
    }

    for (;;) {
        if (rline(commands, fin, commands->linebuf,
                  (commands->linebuf + LBSIZE + 1)) < 0)
            break;

        commands->cp = commands->linebuf;

comploop:
        while (*commands->cp == ' ' || *commands->cp == '\t')
            commands->cp++;
        if (*commands->cp == '\0' || *commands->cp == '#')
            continue;
        if (*commands->cp == ';') {
            commands->cp++;
            goto comploop;
        }

        p = address(commands, commands->rep->ad1, &status);
        if (status != PR_SUCCESS) {
            command_errf(commands, XP_GetAdminStr(DBT_CGMES), commands->linebuf);
            return -1;
        }

        if (p == commands->rep->ad1) {
            if (op)
                commands->rep->ad1 = op;
            else {
                command_errf(commands, XP_GetAdminStr(DBT_NRMES));
                return -1;
            }
        } else if (p == 0) {
            p = commands->rep->ad1;
            commands->rep->ad1 = 0;
        } else {
            op = commands->rep->ad1;
            if (*commands->cp == ',' || *commands->cp == ';') {
                commands->cp++;
                commands->rep->ad2 = p;
                p = address(commands, commands->rep->ad2, &status);
                if ((status != PR_SUCCESS) || (p == 0)) {
                    command_errf(commands, XP_GetAdminStr(DBT_CGMES), commands->linebuf);
                    return -1;
                }
                if (p == commands->rep->ad2)
                    commands->rep->ad2 = op;
                else
                    op = commands->rep->ad2;
            } else
                commands->rep->ad2 = 0;
        }

        if(p > &commands->respace[RESIZE-1]) {
            command_errf(commands, XP_GetAdminStr(DBT_TMMES));
            return -1;
        }

        while (*commands->cp == ' ' || *commands->cp == '\t')
            commands->cp++;

swit:
        switch(*commands->cp++) {
        default:
            command_errf(commands, XP_GetAdminStr(DBT_UCMES), commands->linebuf);
            return -1;

        case '!':
            commands->rep->negfl = 1;
            goto swit;

        case '{':
            commands->rep->command = BCOM;
            commands->rep->negfl = !(commands->rep->negfl);
            commands->cmpend[commands->depth++] = &commands->rep->lb1;
            commands->rep = alloc_reptr(commands);
            commands->rep->ad1 = p;
            if (*commands->cp == '\0')
                continue;
            goto comploop;

        case '}':
            if (commands->rep->ad1) {
                command_errf(commands, XP_GetAdminStr(DBT_AD0MES), commands->linebuf);
                return -1;
            }

            if (--commands->depth < 0) {
                command_errf(commands, XP_GetAdminStr(DBT_TMCMES));
                return -1;
            }
            *commands->cmpend[commands->depth] = commands->rep;

            commands->rep->ad1 = p;
            continue;

        case '=':
            commands->rep->command = EQCOM;
            if (commands->rep->ad2) {
                command_errf(commands, XP_GetAdminStr(DBT_AD1MES), commands->linebuf);
                return -1;
            }
            break;

        case ':':
            if (commands->rep->ad1) {
                command_errf(commands, XP_GetAdminStr(DBT_AD0MES), commands->linebuf);
                return -1;
            }

            while (*commands->cp++ == ' ');
            commands->cp--;

            tp = commands->lab->asc;
            while ((*tp++ = *commands->cp++)) {
                if (tp >= &(commands->lab->asc[8])) {
                    command_errf(commands, XP_GetAdminStr(DBT_LTLMES), commands->linebuf);
                    return -1;
                }
            }
            *--tp = '\0';

            if ((lpt = search(commands)) != NULL) {
                if (lpt->address) {
                    command_errf(commands, XP_GetAdminStr(DBT_DLMES), commands->linebuf);
                    return -1;
                }
                dechain(lpt, commands->rep);
            } else {
                commands->lab->chain = 0;
                lpt = commands->lab;
                if (++commands->lab >= commands->labend) {
                    command_errf(commands, XP_GetAdminStr(DBT_TMLMES), commands->linebuf);
                    return -1;
                }
            }
            lpt->address = commands->rep;
            commands->rep->ad1 = p;

            continue;

        case 'a':
            commands->rep->command = ACOM;
            if (commands->rep->ad2) {
                command_errf(commands, XP_GetAdminStr(DBT_AD1MES), commands->linebuf);
                return -1;
            }
            if (*commands->cp == '\\')
                commands->cp++;
            if (*commands->cp++ != '\n') {
                command_errf(commands, XP_GetAdminStr(DBT_CGMES), commands->linebuf);
                return -1;
            }
            commands->rep->re1 = p;
            p = text(commands, commands->rep->re1, commands->reend);
            if (p == NULL)
                return -1;
            break;

        case 'c':
            commands->rep->command = CCOM;
            if (*commands->cp == '\\') commands->cp++;
            if (*commands->cp++ != ('\n')) {
                command_errf(commands, XP_GetAdminStr(DBT_CGMES), commands->linebuf);
                return -1;
            }
            commands->rep->re1 = p;
            p = text(commands, commands->rep->re1, commands->reend);
            if (p == NULL)
                return -1;
            break;

        case 'i':
            commands->rep->command = ICOM;
            if (commands->rep->ad2) {
                command_errf(commands, XP_GetAdminStr(DBT_AD1MES), commands->linebuf);
                return -1;
            }
            if (*commands->cp == '\\') commands->cp++;
            if (*commands->cp++ != ('\n')) {
                command_errf(commands, XP_GetAdminStr(DBT_CGMES), commands->linebuf);
                return -1;
            }
            commands->rep->re1 = p;
            p = text(commands, commands->rep->re1, commands->reend);
            if (p == NULL)
                return -1;
            break;

        case 'g':
            commands->rep->command = GCOM;
            break;

        case 'G':
            commands->rep->command = CGCOM;
            break;

        case 'h':
            commands->rep->command = HCOM;
            break;

        case 'H':
            commands->rep->command = CHCOM;
            break;

        case 't':
            commands->rep->command = TCOM;
            goto jtcommon;

        case 'b':
            commands->rep->command = BCOM;
jtcommon:
            while (*commands->cp++ == ' ');
            commands->cp--;

            if (*commands->cp == '\0') {
                if ((pt = commands->labtab->chain) != NULL) {
                    while ((pt1 = pt->lb1) != NULL)
                        pt = pt1;
                    pt->lb1 = commands->rep;
                } else
                    commands->labtab->chain = commands->rep;
                break;
            }
            tp = commands->lab->asc;
            while ((*tp++ = *commands->cp++))
                if (tp >= &(commands->lab->asc[8])) {
                    command_errf(commands, XP_GetAdminStr(DBT_LTLMES), commands->linebuf);
                    return -1;
                }
            commands->cp--;
            *--tp = '\0';

            if ((lpt = search(commands)) != NULL) {
                if (lpt->address) {
                    commands->rep->lb1 = lpt->address;
                } else {
                    pt = lpt->chain;
                    while ((pt1 = pt->lb1) != NULL)
                        pt = pt1;
                    pt->lb1 = commands->rep;
                }
            } else {
                commands->lab->chain = commands->rep;
                commands->lab->address = 0;
                if (++commands->lab >= commands->labend) {
                    command_errf(commands, XP_GetAdminStr(DBT_TMLMES), commands->linebuf);
                    return -1;
                }
            }
            break;

        case 'n':
            commands->rep->command = NCOM;
            break;

        case 'N':
            commands->rep->command = CNCOM;
            break;

        case 'p':
            commands->rep->command = PCOM;
            break;

        case 'P':
            commands->rep->command = CPCOM;
            break;

        case 'r':
            commands->rep->command = RCOM;
            if (commands->rep->ad2) {
                command_errf(commands, XP_GetAdminStr(DBT_AD1MES), commands->linebuf);
                return -1;
            }
            if (*commands->cp++ != ' ') {
                command_errf(commands, XP_GetAdminStr(DBT_CGMES), commands->linebuf);
                return -1;
            }
            commands->rep->re1 = p;
            p = text(commands, commands->rep->re1, commands->reend);
            if (p == NULL)
                return -1;
            break;

        case 'd':
            commands->rep->command = DCOM;
            break;

        case 'D':
            commands->rep->command = CDCOM;
            commands->rep->lb1 = commands->ptrspace;
            break;

        case 'q':
            commands->rep->command = QCOM;
            if (commands->rep->ad2) {
                command_errf(commands, XP_GetAdminStr(DBT_AD1MES), commands->linebuf);
                return -1;
            }
            break;

        case 'l':
            commands->rep->command = LCOM;
            break;

        case 's':
            commands->rep->command = SCOM;
            commands->sseof = *commands->cp++;
            commands->rep->re1 = p;
            p = comple(commands, &compargs, (char *) 0, commands->rep->re1,
                       commands->reend, commands->sseof);
            if (p == NULL)
                return -1;
            if (p == commands->rep->re1) {
                if (op)
                    commands->rep->re1 = op;
                else {
                    command_errf(commands, XP_GetAdminStr(DBT_NRMES));
                    return -1;
                }
            } else 
                op = commands->rep->re1;
            commands->rep->rhs = p;

            p = compsub(commands, &compargs, commands->rep->rhs);
            if ((p) == NULL)
                return -1;

            if (*commands->cp == 'g') {
                commands->cp++;
                commands->rep->gfl = 999;
            } else if (commands->gflag)
                commands->rep->gfl = 999;

            if (*commands->cp >= '1' && *commands->cp <= '9') {
                i = *commands->cp - '0';
                commands->cp++;
                while (1) {
                    ii = *commands->cp;
                    if (ii < '0' || ii > '9')
                        break;
                    i = i*10 + ii - '0';
                    if (i > 512) {
                        command_errf(commands, XP_GetAdminStr(DBT_TOOBIG), commands->linebuf);
                        return -1;
                    }
                    commands->cp++;
                }
                commands->rep->gfl = i;
            }

            if (*commands->cp == 'p') {
                commands->cp++;
                commands->rep->pfl = 1;
            }

            if (*commands->cp == 'P') {
                commands->cp++;
                commands->rep->pfl = 2;
            }

            if (*commands->cp == 'w') {
                commands->cp++;
                if (*commands->cp++ !=  ' ') {
                    command_errf(commands, XP_GetAdminStr(DBT_CGMES), commands->linebuf);
                    return -1;
                }
                if (text(commands, fnamebuf, &fnamebuf[PATH_MAX]) == NULL) {
                    command_errf(commands, XP_GetAdminStr(DBT_FNTL), commands->linebuf);
                    return -1;
                }
                for (i = commands->nfiles - 1; i >= 0; i--)
                    if (strcmp(fnamebuf,commands->fname[i]) == 0) {
                        commands->rep->findex = i;
                        goto done;
                    }
                if (commands->nfiles >= NWFILES) {
                    command_errf(commands, XP_GetAdminStr(DBT_TMWFMES));
                    return -1;
                }
                commands->fname[commands->nfiles] = (char *)
                            pool_strdup(commands->pool, (const char *)fnamebuf);
                if (commands->fname[commands->nfiles] == NULL) {
                    command_errf(commands, XP_GetAdminStr(DBT_OOMMES));
                    return -1;
                }
                commands->rep->findex = commands->nfiles++;
            }
            break;

        case 'w':
            commands->rep->command = WCOM;
            if (*commands->cp++ != ' ') {
                command_errf(commands, XP_GetAdminStr(DBT_SMMES), commands->linebuf);
                return -1;
            }
            if (text(commands, fnamebuf, &fnamebuf[PATH_MAX]) == NULL) {
                command_errf(commands, XP_GetAdminStr(DBT_FNTL), commands->linebuf);
                return -1;
            }
            for (i = commands->nfiles - 1; i >= 0; i--)
                if (strcmp(fnamebuf, commands->fname[i]) == 0) {
                    commands->rep->findex = i;
                    goto done;
                }
            if (commands->nfiles >= NWFILES) {
                command_errf(commands, XP_GetAdminStr(DBT_TMWFMES));
                return -1;
            }
            if ((commands->fname[commands->nfiles] =
                        (char *)pool_strdup(commands->pool, fnamebuf)) == NULL) {
                command_errf(commands, XP_GetAdminStr(DBT_OOMMES));
                return -1;
            }
            commands->rep->findex = commands->nfiles++;
            break;

        case 'x':
            commands->rep->command = XCOM;
            break;

        case 'y':
            commands->rep->command = YCOM;
            commands->sseof = *commands->cp++;
            commands->rep->re1 = p;
            p = ycomp(commands, commands->rep->re1);
            if (p == NULL)
                return -1;
            break;
        }
done:
        commands->rep = alloc_reptr(commands);

        commands->rep->ad1 = p;

        if (*commands->cp++ != '\0') {
            if (commands->cp[-1] == ';')
                goto comploop;
            command_errf(commands, XP_GetAdminStr(DBT_CGMES), commands->linebuf);
            return -1;
        }
    }
    commands->rep->command = 0;
    commands->lastre = op;

    return 0;
}
Ejemplo n.º 9
0
void
stars(void)
{
	double lomoon, himoon, sd;
	int wrap, f, i;
	char *saop;
	static char saoa[100];

	sd = 1000*radsec;
	lomoon = omoon.point[0].ra - sd;
	if(lomoon < 0)
		lomoon += pipi;
	himoon = omoon.point[NPTS+1].ra + sd;
	if(himoon > pipi)
		himoon -= pipi;
	lomoon *= 12/pi;
	himoon *= 12/pi;
	wrap = 0;
	if(lomoon > himoon)
		wrap++;

	f = open(startab, OREAD);
	if(f < 0) {
		fprint(2, "%s?\n", startab);
		return;
	}
	epoch = 1950.0;
	epoch = (epoch-1900.0) * 365.24220 + 0.313;
	saop = saoa;

/*
 *	read mean places of stars at epoch of star table
 */

loop:
	if(rline(f)) {
		close(f);
		return;
	}
	rah = atof(line+17);
	ram = atof(line+20);
	ras = atof(line+23);

	alpha = rah + ram/60 + ras/3600;
	if(wrap == 0) {
		if(alpha < lomoon || alpha > himoon)
			goto loop;
	} else
		if(alpha < lomoon && alpha > himoon)
			goto loop;

	sao = atof(line+0);
	sprint(saop, "%ld", sao);
	da = atof(line+30);
	dday = atof(line+37);
	dmin = atof(line+41);
	dsec = atof(line+44);
	dd = atof(line+50);
	px = atof(line+57);
	mag = atof(line+61);

/*
 *	convert rt ascension and declination to internal format
 */

	delta = fabs(dday) + dmin/60 + dsec/3600;
	if(dday < 0)
		delta = -delta;

	star();
/*
 *	if(fabs(beta) > 6.55*radian)
 *		goto loop;
 */
	sd = .0896833e0*cos(beta)*sin(lambda-1.3820+.00092422117*eday)
		 + 0.99597*sin(beta);
	if(fabs(sd) > .0183)
		goto loop;

	for(i=0; i<=NPTS+1; i++)
		setobj(&ostar.point[i]);

	occult(&omoon, &ostar, 0);
	if(occ.t1 >= 0 || occ.t5 >= 0) {
		i = PTIME;
		if(mag > 2)
			i |= DARK;
		if(mag < 5)
			i |= SIGNIF;
		if(occ.t1 >= 0 && occ.e1 >= 0)
			event("Occultation of SAO %s begins at ",
				saop, "", occ.t1, i);
		if(occ.t5 >= 0 && occ.e5 >= 0)
			event("Occultation of SAO %s ends at ",
				saop, "", occ.t5, i);
		while(*saop++)
			;
	}
	goto loop;
}
Ejemplo n.º 10
0
int main(int argc,char *argv[]){
  vqgen v;

  int entries=-1,dim=-1;
  int start=0,num=-1;
  float desired=.05f,mindist=0.f;
  int iter=1000;
  int biasp=1;
  int centroid=0;

  FILE *out=NULL;
  char *line;
  long i,j,k;
  int init=0;
  q.quant=-1;

  argv++;
  if(!*argv){
    usage();
    exit(0);
  }

  /* get the book name, a preexisting book to continue training */
  {
    FILE *in=NULL;
    char *filename=alloca(strlen(*argv)+30),*ptr;

    strcpy(filename,*argv);
    in=fopen(filename,"r");
    ptr=strrchr(filename,'-');
    if(ptr){
      int num;
      ptr++;
      num=atoi(ptr);
      sprintf(ptr,"%d.vqi",num+1);
    }else
      strcat(filename,"-0.vqi");
    
    out=fopen(filename,"w");
    if(out==NULL){
      fprintf(stderr,"Unable to open %s for writing\n",filename);
      exit(1);
    }
    
    if(in){
      /* we wish to suck in a preexisting book and continue to train it */
      float a;
      
      line=rline(in,out,1);
      if(strcmp(line,vqext_booktype)){
        fprintf(stderr,"wrong book type; %s!=%s\n",line,vqext_booktype);
        exit(1);
      } 
      
      line=rline(in,out,1);
      if(sscanf(line,"%d %d %d",&entries,&dim,&vqext_aux)!=3){
        fprintf(stderr,"Syntax error reading book file\n");
        exit(1);
      }
      
      vqgen_init(&v,dim,vqext_aux,entries,mindist,
                 vqext_metric,vqext_weight,centroid);
      init=1;
      
      /* quant setup */
      line=rline(in,out,1);
      if(sscanf(line,"%ld %ld %d %d",&q.min,&q.delta,
                &q.quant,&q.sequencep)!=4){
        fprintf(stderr,"Syntax error reading book file\n");
        exit(1);
      }
      
      /* quantized entries */
      i=0;
      for(j=0;j<entries;j++){
        for(k=0;k<dim;k++){
          line=rline(in,out,0);
          sscanf(line,"%f",&a);
          v.entrylist[i++]=a;
        }
      }      
      vqgen_unquantize(&v,&q);

      /* bias */
      i=0;
      for(j=0;j<entries;j++){
        line=rline(in,out,0);
        sscanf(line,"%f",&a);
        v.bias[i++]=a;
      }
      
      v.seeded=1;
      {
        float *b=alloca((dim+vqext_aux)*sizeof(float));
        i=0;
        while(1){
          for(k=0;k<dim+vqext_aux;k++){
            line=rline(in,out,0);
            if(!line)break;
            sscanf(line,"%f",b+k);
          }
          if(feof(in))break;
          vqgen_addpoint(&v,b,b+dim);
        }
      }
      
      fclose(in);
    }
  }
  
  /* get the rest... */
  argv=argv++;
  while(*argv){
    if(argv[0][0]=='-'){
      /* it's an option */
      if(!argv[1]){
        fprintf(stderr,"Option %s missing argument.\n",argv[0]);
        exit(1);
      }
      switch(argv[0][1]){
      case 'p':
        if(sscanf(argv[1],"%d,%d,%d",&entries,&dim,&q.quant)!=3)
          goto syner;
        break;
      case 's':
        if(sscanf(argv[1],"%d,%d",&start,&num)!=2){
          num= -1;
          if(sscanf(argv[1],"%d",&start)!=1)
            goto syner;
        }
        break;
      case 'e':
        if(sscanf(argv[1],"%f",&desired)!=1)
          goto syner;
        break;
      case 'd':
        if(sscanf(argv[1],"%f",&mindist)!=1)
          goto syner;
        if(init)v.mindist=mindist;
        break;
      case 'i':
        if(sscanf(argv[1],"%d",&iter)!=1)
          goto syner;
        break;
      case 'b':
        biasp=0;
        break;
      case 'c':
        centroid=1;
        break;
      default:
        fprintf(stderr,"Unknown option %s\n",argv[0]);
        exit(1);
      }
      argv+=2;
    }else{
      /* it's an input file */
      char *file=strdup(*argv++);
      FILE *in;
      int cols=-1;

      if(!init){
        if(dim==-1 || entries==-1 || q.quant==-1){
          fprintf(stderr,"-p required when training a new set\n");
          exit(1);
        }
        vqgen_init(&v,dim,vqext_aux,entries,mindist,
                   vqext_metric,vqext_weight,centroid);
        init=1;
      }

      in=fopen(file,"r");
      if(in==NULL){
        fprintf(stderr,"Could not open input file %s\n",file);
        exit(1);
      }
      fprintf(out,"# training file entry: %s\n",file);

      while((line=rline(in,out,0))){
        if(cols==-1){
          char *temp=line;
          while(*temp==' ')temp++;
          for(cols=0;*temp;cols++){
            while(*temp>32)temp++;
            while(*temp==' ')temp++;
          }

          fprintf(stderr,"%d colums per line in file %s\n",cols,file);

        }
        {
          int i;
          float b[cols];
          if(start+num*dim>cols){
            fprintf(stderr,"ran out of columns reading %s\n",file);
            exit(1);
          }
          while(*line==' ')line++;
          for(i=0;i<cols;i++){

            /* static length buffer bug workaround */
            char *temp=line;
            char old;
            while(*temp>32)temp++;

            old=temp[0];
            temp[0]='\0';
            b[i]=atof(line);
            temp[0]=old;
            
            while(*line>32)line++;
            while(*line==' ')line++;
          }
          if(num<=0)num=(cols-start)/dim;
          for(i=0;i<num;i++)
            vqext_addpoint_adj(&v,b,start+i*dim,dim,cols,num);

        }
      }
      fclose(in);
    }
  }

  if(!init){
    fprintf(stderr,"No input files!\n");
    exit(1);
  }

  vqext_preprocess(&v);

  /* train the book */
  signal(SIGTERM,setexit);
  signal(SIGINT,setexit);

  for(i=0;i<iter && !exiting;i++){
    float result;
    if(i!=0){
      vqgen_unquantize(&v,&q);
      vqgen_cellmetric(&v);
    }
    result=vqgen_iterate(&v,biasp);
    vqext_quantize(&v,&q);
    if(result<desired)break;
  }

  /* save the book */

  fprintf(out,"# OggVorbis VQ codebook trainer, intermediate file\n");
  fprintf(out,"%s\n",vqext_booktype);
  fprintf(out,"%d %d %d\n",entries,dim,vqext_aux);
  fprintf(out,"%ld %ld %d %d\n",
          q.min,q.delta,q.quant,q.sequencep);

  /* quantized entries */
  fprintf(out,"# quantized entries---\n");
  i=0;
  for(j=0;j<entries;j++)
    for(k=0;k<dim;k++)
      fprintf(out,"%d\n",(int)(rint(v.entrylist[i++])));
  
  fprintf(out,"# biases---\n");
  i=0;
  for(j=0;j<entries;j++)
    fprintf(out,"%f\n",v.bias[i++]);

  /* we may have done the density limiting mesh trick; refetch the
     training points from the temp file */

  rewind(v.asciipoints);
  fprintf(out,"# points---\n");
  {
    /* sloppy, no error handling */
    long bytes;
    char buff[4096];
    while((bytes=fread(buff,1,4096,v.asciipoints)))
      while(bytes)bytes-=fwrite(buff,1,bytes,out);
  }

  fclose(out);
  fclose(v.asciipoints);

  vqgen_unquantize(&v,&q);
  vqgen_cellmetric(&v);
  exit(0);

  syner:
    fprintf(stderr,"Syntax error in argument '%s'\n",*argv);
    exit(1);
}