summaryrefslogtreecommitdiff
path: root/biblio/bibtex/utils/refer-tools/tib-to-bibtex.shar
blob: c03f44f144ea6668c2bb6a5f4264428872f2b7cb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
#! /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