1: # include "stdio.h" 2: # include "ctype.h" 3: char *months[] {"Jan", "Feb", "Mar", "Apr", "May", 4: "Jun", "Jul", "Aug", "Sep","Oct", "Nov", "Dec",0}; 5: main() 6: { 7: int state 1000, i, book, volume, corp, report; 8: int na; 9: char *v[20], **vv, **rr; 10: char ubuff[1000], *up; 11: char line[100]; 12: char *p, *s, *r, *q; 13: while (gets(line)) 14: { 15: if (line[1]>'9' || line[1]<'0') continue; 16: switch(line[0]) 17: { 18: case 'T': 19: if (state > 'T') 20: { 21: book=0; 22: report=0; 23: printf("\n%%T "); 24: } 25: printf("%s\n", line+18); 26: state='T'; 27: na = getargs(line+18, v); 28: for(i=0;i<na;i++) 29: if (strcmp(v[i], "(Book)")==0) 30: book=1; 31: continue; 32: case 'A': 33: state = 'A'; 34: na=getargs(line+18, vv=v); 35: if (na<=0) continue; 36: while (na>0) 37: { 38: printf("%%A "); 39: corp=0; 40: for(p=vv[1]; *p; p++) 41: if (islower(*p)) 42: corp=1; 43: if (corp==0) 44: { 45: for(p=vv[1]; *p; p++) 46: printf("%c. ", *p); 47: if (na>2 &&strcmp(vv[2], "+")) 48: { 49: printf("%s", vv[0]); 50: if (strcmp(vv[2], "Jr.")==0) 51: printf(","); 52: printf(" %s\n",vv[2]); 53: vv++;na--; 54: } 55: else 56: printf("%s\n", vv[0]); 57: } 58: else 59: printf("%s %s\n",vv[0],vv[1]); 60: vv+=2; 61: na-=2; 62: if (strcmp(vv[0], "+")==0) 63: { 64: vv++;na--; 65: } 66: } 67: continue; 68: case 'U': 69: if (state!='U') 70: ubuff[0]=0; 71: else 72: strcat(ubuff, " "); 73: state = 'U'; 74: strcat(ubuff, line+18); 75: if (line[2]=='.') 76: { /* end of item */ 77: p=ubuff; /*start*/ 78: volume=0; 79: for(s=ubuff; *s; s++) 80: if (s[-1]==' ' && prefix("Vol", s)) 81: { 82: for(q=s-1; q>ubuff; q--) 83: { 84: if (*q==' ' || *q==',') *q=0; 85: else break; 86: } 87: volume=1; 88: break; 89: } 90: if (*s==0) 91: for(s=ubuff; *s && (*s!=',' || sprefix("Inc", s+1)); s++) 92: ; 93: else 94: s++; 95: if (*s==',')*s++=0; 96: if (book) 97: printf("%%I %s\n",ubuff); 98: else if (volume) 99: printf("%%J %s\n", ubuff); 100: else if (substr(ubuff, "Report")!=0) 101: { 102: report=1; 103: printf("%%R %s\n", ubuff); 104: } 105: else 106: printf("%%J %s\n", ubuff); 107: if (volume) 108: { 109: s += 3; /* Vol */ 110: if (*s=='.') s++; 111: while (*s==' ')s++; 112: printf("%%V "); 113: while (*s && *s != ' ' && *s!=',' && *s!=';' && *s!= ':') 114: putchar(*s++); 115: putchar('\n'); 116: if (*s==':') 117: { 118: printf("%%N "); 119: while (*s==' ')s++; 120: while (isdigit(*s)) 121: putchar(*s++); 122: putchar('\n'); 123: } 124: *s++=0; 125: while (*s==' ')*s++=0; 126: if (s[0]=='N' && s[1]=='o' && (s[2]==' '||s[2]=='.')) 127: { 128: s+=2; 129: while (*s==' '||*s=='.')s++; 130: printf("%%N "); 131: while (isdigit(*s)||*s=='-') 132: putchar(*s++); 133: putchar('\n'); 134: } 135: if (*s==',') *s++=0; 136: } 137: for(rr=months; *rr; rr++) 138: { 139: q= substr(s, *rr); 140: if (q) 141: { 142: for(r=q; *r; r++); 143: r--; 144: if (*r=='.')*r=0; 145: printf("%%D %s\n",q); 146: *(q-1)=0; 147: break; 148: } 149: } 150: if (*rr==0) 151: { 152: for(q=s; *q; q++) 153: { 154: if (q[0]=='1' && q[1]=='9' && (q[4]==0 || (q[4]=='.' && q[5]==0))) 155: { 156: if (q[4]=='.') q[4]=0; 157: printf("%%D %s\n",q); 158: rr=months; 159: q[-1]=0; 160: if (q==s) q[0]=0; 161: break; 162: } 163: } 164: } 165: if (*rr==0) /* no date */ 166: printf("%%D 19xx\n"); 167: /* if book bite off next field for city, if report for issuer */ 168: if (book) 169: { 170: for(q=s; *q && *q != ','; q++) 171: ; 172: if (*q==',') 173: { 174: r=q; 175: r++; 176: while (*r==' ')r++; 177: if (isupper(r[0]) && isupper(r[1])) 178: { 179: r+=2; 180: *r++=0; 181: while (*r==' ')r++; 182: } 183: else 184: *q=0; 185: printf("%%C %s\n", s); 186: s=r; 187: } 188: } 189: for(q=s; *q; q++) 190: { 191: if (q[0]==' ' && q[1]=='p' && (q[2]=='p'||q[2]==0)) 192: { 193: for(r=q; r>s; r--) 194: { 195: if (*r==' ' || *r==',') 196: *r=0; 197: } 198: *q=0; 199: q+=2; 200: if (q[0]=='p')q++; 201: while (*q==' '||*q=='.')q++; 202: r=q; 203: while (isdigit(*q)||*q=='.'||*q=='-'||isalpha(*q))q++; 204: *q++=0; 205: while (*q==' ')q++; 206: printf("%%P %s\n",r); 207: break; 208: } 209: } 210: s=ispp(s); 211: while (*s==' ')s++; 212: while (*q==' ')q++; 213: if (*s||*q) 214: printf("%%O %s %s\n", *s?s:"", *q?q:""); 215: } 216: continue; 217: } 218: } 219: } 220: getargs(s, arps) 221: char *s, *arps[]; 222: { 223: int i; 224: i = 0; 225: while (1) 226: { 227: arps[i++]=s; 228: while (*s != 0 && *s!=' '&& *s != '\t')s++; 229: if (*s==0) break; 230: *s++ =0; 231: while (*s==' ' || *s=='\t')s++; 232: if (*s==0)break; 233: } 234: return(i); 235: } 236: prefix(small, big) 237: char *small, *big; 238: { 239: int c; 240: while ((c= *small++) == *big++) 241: if (c==0) return(1); 242: return(c==0); 243: } 244: substr(big, small) 245: char *small, *big; 246: { 247: while (*big) 248: if (prefix(small, big)) 249: return(big); 250: else 251: big++; 252: return(0); 253: } 254: sprefix(small, big) 255: char *small, *big; 256: { 257: while (*big==' ') big++; 258: return(prefix(small,big)); 259: } 260: ispp(s) 261: char *s; 262: { 263: static char buff[50]; 264: char *p, *t; 265: p=s; 266: while (*p==' ') p++; 267: if (!isdigit(*p)) return(s); 268: t=p; 269: while (isdigit(*p))p++; 270: if (p[0]!='p' || p[1]!='p') return(s); 271: *p=0; 272: sprintf(buff, "%spp.", t); 273: return(buff); 274: }