1 /*- 2 * %sccs.include.proprietary.c% 3 */ 4 5 #ifndef lint 6 static char sccsid[] = "@(#)refer7.c 4.5 (Berkeley) 04/18/91"; 7 #endif /* not lint */ 8 9 #include "refer..c" 10 11 int newr[250]; 12 13 chkdup(tag) 14 char *tag; 15 { 16 int i; 17 18 for(i = 1; i <= refnum; i++) { 19 if (strcmp(reftable[i], tag)==0) 20 return(i); 21 } 22 reftable[refnum+1] = rtp; 23 if (refnum >= NRFTBL) 24 err("too many references (%d) for table", refnum); 25 strcpy(rtp, tag); 26 while (*rtp++); 27 if (rtp > reftext + NRFTXT) 28 err("reference pointers too long (%d)", rtp-reftext); 29 return(0); 30 } 31 32 dumpold() 33 { 34 FILE *fi; 35 int c, g1 = 0, nr = 1; 36 37 if (!endpush) 38 return; 39 if (fo != NULL) { 40 fclose(fo); 41 fo = NULL; 42 } 43 if (sort) { 44 char comm[100]; 45 sprintf(comm, "sort -f %s -o %s", tfile, tfile); 46 system(comm); 47 } 48 fi = fopen(tfile, "r"); 49 if (fi == NULL) 50 return; 51 flout(); 52 fprintf(ftemp, ".]<\n"); 53 while ((c = getc(fi)) > 0) { 54 if (c == '\n') { 55 nr++; 56 g1 = 0; 57 } 58 if (c == sep) 59 c = '\n'; 60 if (c == FLAG) { 61 /* make old-new ref number table */ 62 char tb[20]; 63 char *s = tb; 64 while ((c = getc(fi)) != FLAG) 65 *s++ = c; 66 *s = 0; 67 if (g1++ == 0) 68 newr[atoi(tb)] = nr; 69 #if EBUG 70 fprintf(stderr, 71 "nr %d assigned to atoi(tb) %d\n",nr,atoi(tb)); 72 # endif 73 fprintf(ftemp,"%d", nr); 74 continue; 75 } 76 putc(c, ftemp); 77 } 78 fclose(fi); 79 #ifndef TF 80 unlink(tfile); 81 #endif 82 fprintf(ftemp, ".]>\n"); 83 } 84 85 recopy (fnam) 86 char *fnam; 87 { 88 int c; 89 int *wref = NULL; 90 int wcnt = 0; 91 int wsize = 50; 92 int finalrn; 93 char sig[MXSIG]; 94 extern int *realloc(); 95 96 wref = (int *)calloc((unsigned)wsize, (unsigned)sizeof(int)); 97 fclose(ftemp); 98 ftemp = fopen(fnam, "r"); 99 if (ftemp == NULL) { 100 fprintf(stderr, "Can't reopen %s\n", fnam); 101 exit(1); 102 } 103 while ((c = getc(ftemp)) != EOF) { 104 if (c == FLAG) { 105 char tb[10]; 106 char *s = tb; 107 while ((c = getc(ftemp)) != FLAG) 108 *s++ = c; 109 *s = 0; 110 /* 111 * If sort was done, permute the reference number 112 * to obtain the final reference number, finalrn. 113 */ 114 if (sort) 115 finalrn = newr[atoi(tb)]; 116 else 117 finalrn = atoi(tb); 118 if ((++wcnt > wsize) && 119 ((wref=realloc(wref,(wsize+=50)*sizeof(int)))==NULL)){ 120 fprintf(stderr, "Ref condense out of memory."); 121 exit(1); 122 } 123 wref[wcnt-1] = finalrn; 124 if ((c = getc(ftemp)) == AFLAG) 125 continue; 126 wref[wcnt] = 0; 127 condense(wref,wcnt,sig); 128 wcnt = 0; 129 printf("%s", sig); 130 } 131 putchar(c); 132 } 133 fclose(ftemp); 134 unlink(fnam); 135 } 136 137 /* 138 * sort and condense referance signals when they are placed in 139 * the text. Viz, the signal 1,2,3,4 is condensed to 1-4 and signals 140 * of the form 5,2,9 are converted to 2,5,9 141 */ 142 condense(wref, wcnt, sig) 143 int *wref; 144 int wcnt; 145 char *sig; 146 { 147 register int i = 0; 148 char wt[4]; 149 extern int wswap(); 150 151 qsort(wref, wcnt, sizeof(int), wswap); 152 sig[0] = 0; 153 while (i < wcnt) { 154 sprintf(wt,"%d",wref[i]); 155 strcat(sig,wt); 156 if ((i+2 < wcnt) && (wref[i] == (wref[i+2] - 2))) { 157 while (wref[i] == (wref[i+1] - 1)) 158 i++; 159 strcat(sig, "-"); 160 } else if (++i < wcnt) 161 strcat(sig,",\\|"); 162 } 163 } 164 165 wswap(iw1, iw2) 166 register int *iw1,*iw2; 167 { 168 return(*iw1 - *iw2); 169 } 170