strcpy(reffile,argv[1]);
if ((ref=fopen(reffile,"r")) == NULL) {
sprintf(reffile,"%s.ref",argv[1]);
if ((ref=fopen(reffile,"r")) == NULL) {
fprintf(stderr,"ttb: can open neither %s nor %s\n",argv[1],reffile);
exit(NOREF);
}
}
if (!strcmp(argv[1]+strlen(argv[1])-4,".ref"))
argv[1][strlen(argv[1])-4]='\0';
sprintf(bibfile,"%s.bib",argv[1]);
if ((bib=fopen(bibfile,"w")) == NULL) {
fprintf(stderr,"ttb: can't open %s\n",bibfile);
exit(NOBIB);
}
/* main loop */
do {
notready=getitem();
type=classifyitem();
/* printitem(); */
putitem();
}
while(notready);
/* exit */
fclose(ref);
fclose(bib);
}
getitem()
{ char firstchar,secondchar;
int i,j,k,ok,ilm1,space,used,ignored=0;
ilines = 0;
space = maxlines*linelen-1;
*item = buffer;
while(!feof(ref)) {
if (space<=0) {
fprintf(stderr,"ttb: %s, %d: buffer space exhausted\n",reffile,curline);
exit(MEMFULL);
}
if (ilines>maxlines) {
fprintf(stderr,"ttb: %s, line %d: too many lines for this item, line ignored\n",reffile,curline);
ilines--;
continue;
}
if (strlen(item[ilm1]) == 0) { /* blank line */
ilines--;
ignored=0;
break;
}
if ((firstchar=item[ilm1][0]) != '%') { /* continuation line */
if (ilines == 1) {
fprintf(stderr,"ttb: %s, line %d: leading continuation line ignored\n",reffile,curline);
ilines--;
continue;
}
if (ignored) {
fprintf(stderr,"ttb: %s, line %d: continuation line ignored\n",reffile,curline);
ilines--;
continue;
}
secondchar = item[ilm1][1];
ok = 0;
for (i=0; i<ntags; i++)
if (secondchar == allowedtag[i]) {
ok = -1;
break;
}
if (!ok) {
fprintf(stderr,"ttb: %s, line %d: field tag %c not allowed, line ignored\n",reffile,curline,secondchar);
ignored = -1;
ilines--;
continue;
}
if (test('J') && test('V'))
type = article;
else
if (test('B'))
type = inbook;
else
if (p=test('R'))
if (index(item[p-1],"thesis"))
if (index(item[p-1],"master"))
type = mastersthesis;
else
type = phdthesis;
else
type = techreport;
else
if (test('I'))
type = book;
else
type = conference;
return(type);
}
index(s,t)
char s[],t[];
{ int i,j,k;
for (i=0; s[i]; i++) {
for (j=i,k=0; t[k] && tolower(s[j])==tolower(t[k]); j++,k++)
;
if (t[k] == 0)
return(i+1);
}
return(0);
}
int test(c)
char c;
{ int i;
for (i=0; i<ilines; i++)
if (item[i][1] == c)
return(i+1);
return(0);
}
putitem()
{ char author[linelen],year[linelen],page[linelen],record[linelen],
editor[linelen],secondchar;
int i,j,k,p,first;