#! /bin/sh
# This is a shell archive.  Remove anything before this line, then unpack
# it by saving it into a file and typing "sh file".  To overwrite existing
# files, type "sh file -c".  You can also feed this as standard input via
# unshar, or by typing "sh <file", e.g..  If this archive is complete, you
# will see the following message at the end:
#		"End of shell archive."
# Contents:  ttb.c ttb.1
# Wrapped by sk@sun4 on Tue Jul  3 16:41:38 1990
PATH=/bin:/usr/bin:/usr/ucb ; export PATH
if test -f ttb.c -a "${1}" != "-c" ; then 
  echo shar: Will not over-write existing file \"ttb.c\"
else
echo shar: Extracting \"ttb.c\" \(6982 characters\)
sed "s/^X//" >ttb.c <<'END_OF_ttb.c'
X#define about "tibtobib 0.3, 1989 by uf"
X
X#include <stdio.h>
X#include <ctype.h>
X
X#define maxlines 50
X#define linelen 256
X#define ntypes 8
X#define ntags 18
X
X#define article 0
X#define conference 1
X#define inbook 2
X#define mastersthesis 3
X#define phdthesis 4
X#define techreport 5
X#define book 6
X#define misc 7
X
X#define HELP 0
X#define NOREF 10
X#define NOBIB 11
X#define BIGITEM 12
X#define NOCONT1 13
X#define NOCONT2 14
X#define BIGLINE 15
X#define BADTAG 16
X#define MEMFULL 17
X
Xchar RCSHeader[]="$Header: /home/tex/local/ttb.c,v 1.3 90/02/15 14:55:30 sk Rel $";
X
Xstatic char *inputtype[]={"ARTICLE","CONFERENCE","INBOOK","MASTERSTHESIS",
X			     "PHDTHESIS","TECHREPORT","BOOK","MISC"},
X  *recordtype[]={"AUTHOR","BOOKTITLE","ADDRESS","YEAR","EDITOR",
X		   "PUBLISHER","JOURNAL","KEY","NUMBER","NOTE","PAGES",
X		   "TYPE","SERIES","TITLE","VOLUME","COMMENT","ANNOTE",
X		   "ABSTRACT"},
X  allowedtag[]={'A','B','C','D','E','I','J','K','N','O','P','R','S','T','V',
X		  '%','x','X'},
X  organization[]="ORGANIZATION";
X
X
Xchar *item[maxlines],buffer[maxlines*linelen],
X  reffile[linelen],bibfile[linelen];
Xint curline=0,type,ilines;
X
XFILE *ref,*bib,*fopen();
X
Xmain(argc,argv)
Xint argc;
Xchar **argv;
X{ int notready;
X
X  if (argc != 2) {
X    fprintf(stderr,"%s, usage: ttb <filename>\n",about);
X    exit(HELP);
X  }
X
X  strcpy(reffile,argv[1]);
X  if ((ref=fopen(reffile,"r")) == NULL) {
X    sprintf(reffile,"%s.ref",argv[1]);
X    if ((ref=fopen(reffile,"r")) == NULL) {
X      fprintf(stderr,"ttb: can open neither %s nor %s\n",argv[1],reffile);
X      exit(NOREF);
X    }
X  }
X
X  if (!strcmp(argv[1]+strlen(argv[1])-4,".ref"))
X      argv[1][strlen(argv[1])-4]='\0';
X  sprintf(bibfile,"%s.bib",argv[1]);
X  if ((bib=fopen(bibfile,"w")) == NULL) {
X    fprintf(stderr,"ttb: can't open %s\n",bibfile);
X    exit(NOBIB);
X  }
X
X  /* main loop */
X
X  do {
X    notready=getitem();
X    type=classifyitem();
X    /* printitem(); */
X    putitem();
X  }
X  while(notready);
X
X  /* exit */
X
X  fclose(ref);
X  fclose(bib);
X}
X
X
Xgetitem()
X{ char firstchar,secondchar;
X  int i,j,k,ok,ilm1,space,used,ignored=0;
X
X  ilines = 0;
X  space = maxlines*linelen-1;
X  *item = buffer;
X
X  while(!feof(ref)) {
X
X    if (space<=0) {
X      fprintf(stderr,"ttb: %s, %d: buffer space exhausted\n",reffile,curline);
X      exit(MEMFULL);
X    }
X
X    fgets(item[ilines++],space,ref);
X    ilm1 = ilines-1;
X    for (k=strlen(item[ilm1])-1; k>-1 && isspace(item[ilm1][k]); item[ilm1][k--]=0)
X      ;
X    /* printf("%s\n",item[ilm1]); */
X    curline++;
X
X    if (ilines>maxlines) {
X      fprintf(stderr,"ttb: %s, line %d: too many lines for this item, line ignored\n",reffile,curline);
X      ilines--;
X      continue;
X    }
X
X    if (strlen(item[ilm1]) == 0) {  /* blank line */
X      ilines--;
X      ignored=0;
X      break;
X    }
X
X    if ((firstchar=item[ilm1][0]) != '%') {  /* continuation line */
X      if (ilines == 1) {
X	fprintf(stderr,"ttb: %s, line %d: leading continuation line ignored\n",reffile,curline);
X	ilines--;
X	continue;
X      }
X      if (ignored) {
X	fprintf(stderr,"ttb: %s, line %d: continuation line ignored\n",reffile,curline);
X	ilines--;
X	continue;
X      }
X
X      ilines--;
X      *(item[ilines]-1) = '\n';
X      used = strlen(item[ilines])+1;
X      item[ilines] += used;
X      continue;
X    }
X
X    secondchar = item[ilm1][1];
X    ok = 0;
X    for (i=0; i<ntags; i++)
X      if (secondchar == allowedtag[i]) {
X	ok = -1;
X	break;
X      }
X
X    if (!ok) {
X      fprintf(stderr,"ttb: %s, line %d: field tag %c not allowed, line ignored\n",reffile,curline,secondchar);
X      ignored = -1;
X      ilines--;
X      continue;
X    }
X
X    ignored = 0;
X    space -= (used = strlen(item[ilm1])+1);
X    item[ilines] = item[ilm1]+used;
X  }
X
X  return(!feof(ref));
X}
X
X
Xint classifyitem()
X{ int p,type;
X
X  if (test('J') && test('V'))
X      type = article;
X  else
X    if (test('B'))
X      type = inbook;
X    else
X      if (p=test('R'))
X	if (index(item[p-1],"thesis"))
X	  if (index(item[p-1],"master"))
X	    type = mastersthesis;
X	  else
X	    type = phdthesis;
X	else
X	  type = techreport;
X      else
X	if (test('I'))
X	  type = book;
X	else
X	  type = conference;
X  
X  return(type);
X}
X
X
Xindex(s,t)
Xchar s[],t[];
X{ int i,j,k;
X
X  for (i=0; s[i]; i++) {
X    for (j=i,k=0; t[k] && tolower(s[j])==tolower(t[k]); j++,k++)
X      ;
X    if (t[k] == 0)
X      return(i+1);
X  }
X
X  return(0);
X}
X
X
Xint test(c)
Xchar c;
X{ int i;
X
X  for (i=0; i<ilines; i++)
X    if (item[i][1] == c)
X      return(i+1);
X
X  return(0);
X}
X
X
Xputitem()
X{ char author[linelen],year[linelen],page[linelen],record[linelen],
X    editor[linelen],secondchar;
X  int i,j,k,p,first;
X
X  if (p=test('A')) {
X    if ((i = index(item[p-1],",")) > 4) {
X      strcpy(author,item[p-1]+3);
X      author[i-4]=0;
X    }
X    else {
X      for (k=strlen(item[p-1]); k>2 && item[p-1][k] != ' ' && item[p-1][k] != '.'; k--)
X	;
X      strcpy(author,item[p-1]+k+1);
X    }
X  }
X  else
X    strcpy(author,"Mr.X");
X
X  if (p=test('D'))
X    strcpy(year,item[p-1]+strlen(item[p-1])-2);
X  else
X    strcpy(year,"??");
X
X  if (p=test('P')) {
X    for (i=3; i<strlen(item[p-1]) && (isalpha(item[p-1][i]) && i==3 || isdigit(item[p-1][i])); page[i-3]=item[p-1][i], i++)
X      ;
X    page[i-3]=0;
X  }
X  else
X    strcpy(page,"0");
X
X  if (p=test('K'))     /* user provides keyword */
X    fprintf(bib,"@%s{%s%c\n",inputtype[type],item[p-1]+3,ilines>0 ? ',' : ' ');
X  else
X    fprintf(bib,"@%s{%s%s:%s%c\n",inputtype[type],author,year,page,ilines>0 ? ',' : ' ');
X
X  /* special cases: several authors, editors */
X
X  first = !0;
X  strcpy(author,"");
X  if (test('A')) {
X    for (i=0; i<ilines; i++) {
X      if (item[i][1] == 'A') {
X	if (first)
X	  first = 0;
X	else
X	  strcat(author," and ");
X	strcat(author,item[i]+3);
X      }
X    }
X    fprintf(bib,"  %s = {%s}%c\n",recordtype[0],author,item[ilines-1][1] != 'A' ? ',' : ' ');
X  }
X  
X  first = !0;
X  strcpy(editor,"");
X  if (test('E')) {
X    for (i=0; i<ilines; i++) {
X      if (item[i][1] == 'E') {
X	if (first)
X	  first = 0;
X	else
X	  strcat(editor," and ");
X	strcat(editor,item[i]+3);
X      }
X    }
X    fprintf(bib,"  %s = {%s}%c\n",recordtype[4],editor,item[ilines-1][1] != 'E' ? ',' : ' ');
X  }
X
X  /* all other fields */
X  
X  for (i=0; i<ilines; i++) {
X    secondchar = item[i][1];
X    if (secondchar == 'A' || secondchar == 'E')
X      continue;
X    
X    /* special case: %J in a conference */
X    
X    if (secondchar == 'J' && type == conference)
X      strcpy(record,organization);
X    
X    /* all other cases */
X    
X    else
X      for (j=0; j<ntags; j++)
X	if (secondchar == allowedtag[j]) {
X	  strcpy(record,recordtype[j]);
X	  break;
X	}
X    
X    fprintf(bib,"  %s = {%s}%c\n",record,item[i]+3,i<ilines-1 ? ',' : ' ');
X  }
X  
X  fprintf(bib,"}\n\n");
X}
X
Xoption(argc,argv,c)
Xchar **argv,c;
Xint argc;
X{ int i,j;
X
X  for (i=1; i<argc; i++)
X    if (argv[i][0] == '-')
X      for (j=1; j<strlen(argv[i]); j++)
X	if (argv[i][j] == c)
X	  return(i);
X  return(0);
X}
X
Xprintitem()
X{ int i;
X
X  for (i=0; i<ilines; i++)
X    printf("%s\n",item[i]);
X  printf("\nitem consisting of %d lines, type = %d\n\n",ilines,type);
X}
X
END_OF_ttb.c
if test 6982 -ne `wc -c <ttb.c`; then
    echo shar: \"ttb.c\" unpacked with wrong size!
fi
# end of overwriting check
fi
if test -f ttb.1 -a "${1}" != "-c" ; then 
  echo shar: Will not over-write existing file \"ttb.1\"
else
echo shar: Extracting \"ttb.1\" \(727 characters\)
sed "s/^X//" >ttb.1 <<'END_OF_ttb.1'
X.TH TTB 1 "15-Feb-1990"
X.SH NAME
Xttb - tib to bibtex bibliography converter
X
X.SH SYNOPSIS
X.B ttb
X.IB file [.ref]
X
X.SH DESCRIPTION
X.B ttb
Xtakes as input a bibliography in tib format (see
X.BR tib(5) )
Xand produces an equivalent bibliography for use with 
X.BR bibtex(1)
Xin
X.IB file .bib.
X.PP
XIf a
X.B %K
Xfield is present in the
X.B .ref
Xfile, it is used as
X.I key
Xfor a LaTeX \\cite command.
XOtherwise the
X.I key
Xis made up from the author, year and page of the reference like this:
X.I author year
X.B :
X.I page
X.SH FILES
X.TP 16
X~tex/local
Xsource directory for ttb
X.SH VERSION
Xttb.c,v 1.3
X.PP
XTo find out what version you are using, type
X.IP
X.B ident `which ttb`
X.SH "SEE ALSO"
X.B tib(1,5,7), bibtex(1)
X
X.SH AUTHOR
XUlrich Fastenrath
END_OF_ttb.1
if test 727 -ne `wc -c <ttb.1`; then
    echo shar: \"ttb.1\" unpacked with wrong size!
fi
# end of overwriting check
fi
echo shar: End of shell archive.
exit 0

