summaryrefslogtreecommitdiff
path: root/biblio/tib/src/tiblist.c
blob: 0072f5d24751e8f9d9d2a95ae9416fa35ed81379 (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
/*
        Tiblist - list references for tib system

                                                        */
#include <stdio.h>
#include <ctype.h>
#include "tib.h"
#define MAXLIST 2000  /* maximum number of references that can be listed */
#define getch(c,fd) (c = getc(fd))

FILE *tfd;                      /* temporary file position */
char tmp_file[120];              /* temporary file (see tib.h) */
FILE *rfd;                      /* reference file position */
char reffile[120];              /* temporary file (see tib.h) */
long int refspos[MAXLIST];      /* references temporary file, seek positions */
long int rend = 1;              /* last used position in reference file */
int numrefs = -1;               /* number of references */
char *citestr[MAXLIST];         /* citation strings */
extern int sort;                /* see if things are to be sorted */
int unlmark[MAXLIST];           /* underline flags */
extern char bibfname[], citetemplate[];
extern int biblineno;
extern int underline, silent, stndout;
extern char dirsp2[], optch2[], suffix[];

main(argc, argv)
   int argc;
   char **argv;
{  int  i, rcomp1();
   char *outname;
   char *outfname();
   int undrline();
   FILE *ofd;
   int c;
   int strcmp();
   char *pdefst, *getenv(), *ptemp, argchk[3];

   /* header */
   for (i = 1; i < argc; i++) {
      strcpy(argchk,OPTCH);
      strcat(argchk,"z");
      if (strcmp(argv[i],argchk) == 0)
          silent = true;
      strcpy(argchk,OPTCH);
      strcat(argchk,"|");
      if (strcmp(argv[i],argchk) == 0)
         silent = true;
   }
   if (silent == false)
      fprintf (stderr, "Tiblist -- version %s, released %s.\n",
             VERSION, RDATE);

   /* get file names from environment */

   ptemp = getenv("TMPDIR");
   if (ptemp != NULL) {
      strcpy(reffile,ptemp);
      strcat(reffile,dirsp2);
      strcat(reffile,"tibrXXXXXX");
      strcpy(tmp_file,ptemp);
      strcat(tmp_file,dirsp2);
      strcat(tmp_file,"tibpXXXXXX");
   }
   else {
      strcat(reffile,TMPREFFILE);
      strcat(tmp_file,TMPTEXTFILE);
   }

   mktemp(reffile);
   rfd = fopen(reffile,"w+");
   if (rfd == NULL)
      error("can't open temporary reference file");
   putc('x', rfd);      /* put garbage in first position */
   mktemp(tmp_file);
   tfd = fopen(tmp_file,"w");
   if (tfd == NULL)
      error("can't open temporary output file");

   strcpy(suffix,".ref");
   pdefst = getenv("DEFSTYLE");
   if (pdefst == NULL)
      doargs(argc, argv, DEFSTYLE);
   else
      doargs(argc, argv, pdefst);

   if (silent == 0)
      fprintf(stderr, "Processing reference list ...\n");
   if (sort)
      qsort(refspos, numrefs+1, sizeof(long), rcomp1);
   makecites(citestr);
   if (underline)
      undrline();
   if (citetemplate[0] != '0')
      disambiguate();


   /*
   reopen temporaries
   */

   fclose(tfd);
   tfd = fopen(tmp_file,"r");
   if (tfd == NULL)
      error("can't open temporary output file for reading");

   if (stndout == true && bibfname != "<stdin>") {
      while (getch(c,tfd) != EOF)
         putc(c, stdout);
      for (i = 0; i <= numrefs; i++)
         dumpref(i, stdout);
      fprintf(stdout, "%cbye\n", '\\');
      }
   else {
      outname = outfname (bibfname);
      ofd = fopen (outname, "w");
      if (ofd == NULL) {
          fprintf(stderr,"can't open %s\n", outname);
          clnup();
          exit(1);
          }
      else {
          if (silent == 0)
             fprintf(stderr, "Writing output file ...\n");
          while (getch(c,tfd) != EOF)
             putc(c, ofd);
          for (i = 0; i <= numrefs; i++)
             dumpref(i, ofd);
          fprintf(ofd, "%cbye\n", '\\');
          fclose (ofd);
          fprintf (stderr,"Output of tiblist in %s.\n", outname);
      }
    }
   clnup();
   exit(0);
}

   /*   clean up   */
   clnup()
{
   unlink(tmp_file);
   unlink(reffile);
   return;
}

/* rdtext - process a file */
   rdtext(ifile)
   FILE *ifile;
{  char c, *p, rec[REFSIZE];
   int i, toolong;
   int sortdefs();

   sortdefs();
   biblineno = 1;
   for (;;) {
      while (getch(c, ifile) == '\n')
         biblineno++;   /* skip leading newlines */
      if (c == EOF)
         return;

      toolong = false;		/* flag to indicate reference is too long */
      p = rec;			/* read a reference */
      for(*p++ = c; getch(c,ifile) != EOF; *p++ = c){
	if (p-rec >= REFSIZE-1) {
	  toolong = true;	/* reference entry is too long */
	  break;
	}
	if (c != '\n') continue; /* get next character */
	if (getch(c, ifile) == '\n' || c == EOF) { /* end of ref */
	  biblineno++;
	  c = *p++ = '\n';	/* set c = \n to indicate normal exit */
	  break;		
	}
	if (c == '.' || c == '%')
	  *p++ = '\n';
	else
	  *p++ = ' ';
	if (p-rec >= REFSIZE-1) {
	  toolong = true;	/* reference entry is too long */
	  break;
	}
      }
      if (c == EOF) error("ill formed reference file");
      *p = 0;
      
      if (toolong){		/* ref. entry too long */
	biblineno++;		/* biblineno was not inc due to break exit */
	*(p-1) = '\n';		/* set end of field to \n for consistency */
	for (; c != EOF; getch(c,ifile)){
	  if (c != '\n') continue; /* get next character */
	  if (getch(c, ifile) == '\n') break;
	  if (c == EOF){
	    c = '\n';		/* normal exit, file ends with \n */
	    break;
	    }
	}
	if (c == EOF) error("ill formed reference file");
	if (silent == false){
	  fprintf(stderr,
	  "Truncated following input reference to REFSIZE-1 (%d) characters:\n"
		  ,REFSIZE-1);
	  fprintf(stderr,"-------------\n%s\n-------------\n",rec);
	}
      }

      expnd2(rec);

      if (numrefs++ > MAXLIST)
         error("too many references");
      refspos[numrefs] = rend;
#ifdef READWRITE
      fixrfd( WRITE );          /* fix access mode of rfd, if nec. */
#else
      fseek(rfd, rend, 0);
#endif
      i = strlen(rec) + 1;
      fwrite(rec, 1, i, rfd);
      rend = rend + i;
      }
}