/* $Header: head.c,v 4.3.1.2 85/05/10 13:47:25 lwall Exp $ * * $Log: head.c,v $ * Revision 4.3.1.2 85/05/10 13:47:25 lwall * Added debugging stuff. * * Revision 4.3.1.1 85/05/10 11:32:30 lwall * Branch for patches. * * Revision 4.3 85/05/01 11:38:21 lwall * Baseline for release with 4.3bsd. * */ #include "EXTERN.h" #include "common.h" #include "artio.h" #include "bits.h" #include "server.h" #include "util.h" #include "INTERN.h" #include "head.h" bool first_one; /* is this the 1st occurance of this header line? */ static char htypeix[26] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; void head_init() { register int i; for (i=HEAD_FIRST+1; i LONGKEY+2) { /* is it the end of the header? */ htype[PAST_HEADER].ht_minpos = (*art_buf == '\n') ? ftell(artfp) : artpos; /* remember where body starts */ in_header = PAST_HEADER; } else { /* it is a new header line */ in_header = set_line_type(art_buf,s); first_one = (htype[in_header].ht_minpos < 0); if (first_one) htype[in_header].ht_minpos = artpos; #ifdef DEBUGGING if (debug & 4) dumpheader(art_buf); #endif if (htype[in_header].ht_flags & HT_HIDE) return newhide; } } return FALSE; /* don't hide this line */ } #ifdef ASYNC_PARSE int parse_maybe(artnum) ART_NUM artnum; { char tmpbuf[LBUFLEN]; if (parsed_art == artnum) return 0; /* no maybe about it now */ if (artopen(artnum) == Nullfp) { return -1; } start_header(artnum); while (in_header) { artpos = ftell(artfp); if (fgets(tmpbuf,LBUFLEN,artfp) == Nullch) break; parseline(tmpbuf,FALSE,FALSE); } in_header = PAST_HEADER; return 0; } #endif /* get the subject line for an article */ char * fetchsubj(artnum,current_subject,copy) ART_NUM artnum; /* article to get subject from */ bool current_subject; /* is it in a parsed header? */ bool copy; /* do you want it savestr()ed? */ { char *s = Nullch, *t; #ifdef SERVER static int xhdr = 1; /* Can we use xhdr command? */ int eoo; /* End of server output */ char ser_line[256]; #endif SERVER #ifdef CACHESUBJ if (!subj_list) { register ART_NUM i; #ifndef lint subj_list = (char**)safemalloc((MEM_SIZE)((OFFSET(lastart)+2)*sizeof(char *))); #endif lint for (i=0; i<=OFFSET(lastart); i++) subj_list[i] = Nullch; } if (!artnum || artnum > lastart) s = nullstr; else s = subj_list[OFFSET(artnum)]; #endif if (s == Nullch) { if (current_subject) { s = fetchlines(artnum,SUBJ_LINE); #ifdef CACHESUBJ subj_list[OFFSET(artnum)] = s; #endif } else { s = safemalloc((MEM_SIZE)256); *s = '\0'; #ifdef SERVER if (xhdr) { sprintf(ser_line, "XHDR subject %ld", artnum); put_server(ser_line); if (get_server(ser_line, sizeof (ser_line)) >= 0) { if (ser_line[0] == CHAR_FATAL) { xhdr = 0; } else { while (get_server(ser_line, sizeof (ser_line)) >= 0) { if (ser_line[0] == '.') break; else { t = index(ser_line, ' '); if (t++) { strcpy(s, t); if (t = index(s, '\r')) *t = '\0'; } } } } } else { fprintf(stderr, "rrn: Unexpected close of server socket.\n"); finalize(1); } } if (!xhdr) { sprintf(ser_line, "HEAD %ld", artnum); put_server(ser_line); eoo = 0; if (get_server(ser_line, 256) >= 0 && ser_line[0] == CHAR_OK) { do { if (get_server(s, 256) < 0 || (*s == '.')) { strcpy(s, "Title: \n"); eoo = 1; } } while (strnNE(s,"Title:",6) && strnNE(s,"Subject:",8)); if (!eoo) while (get_server(ser_line, sizeof (ser_line)) >= 0 && ser_line[0] != '.'); t = index(s,':')+1; while (*t == ' ') t++; strcpy(s, t); } } #else not SERVER if (artopen(artnum) != Nullfp) { do { if (fgets(s,256,artfp) == Nullch) strcpy(s, "Title: \n"); } while (strnNE(s,"Title:",6) && strnNE(s,"Subject:",8)); s[strlen(s)-1] = '\0'; t = index(s,':')+1; while (*t == ' ') t++; strcpy(s, t); } #endif s = saferealloc(s, (MEM_SIZE)strlen(s)+1); #ifdef CACHESUBJ subj_list[OFFSET(artnum)] = s; #endif } } #ifdef CACHESUBJ if (copy) { t = savestr(s); return t; } else return s; #else if (copy) return s; else { safecpy(cmd_buf,s,CBUFLEN); /* hope this is okay--we're */ free(s); return cmd_buf; /* really scraping for space here */ } #endif } /* get header lines from an article */ char * fetchlines(artnum,which_line) ART_NUM artnum; /* article to get line from */ int which_line; /* type of line desired */ { char *newbuf, *t, tmp_buf[LBUFLEN]; register ART_POS curpos; int size; register ART_POS firstpos; register ART_POS lastpos; #ifdef ASYNC_PARSE if (parse_maybe(artnum)) artnum = 0; #endif firstpos = htype[which_line].ht_minpos; lastpos = htype[which_line].ht_maxpos; if (!artnum || firstpos < 0 || artopen(artnum) == Nullfp) { newbuf = safemalloc((unsigned int)1); *newbuf = '\0'; return newbuf; } #ifndef lint size = lastpos - firstpos + 1; #else size = Null(int); #endif lint #ifdef DEBUGGING if (debug && (size < 1 || size > 1000)) { printf("Firstpos = %ld, lastpos = %ld\n",(long)firstpos,(long)lastpos); gets(tmp_buf); } #endif newbuf = safemalloc((unsigned int)size); *newbuf = '\0'; fseek(artfp,firstpos,0); for (curpos = firstpos; curpos < lastpos; curpos = ftell(artfp)) { if (fgets(tmp_buf,LBUFLEN,artfp) == Nullch) break; if (*tmp_buf == ' ' || *tmp_buf == '\t') t = tmp_buf; else t = index(tmp_buf,':')+1; if (t == Nullch) break; else { while (*t == ' ' || *t == '\t') t++; safecat(newbuf,t,size); } } return newbuf; }