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 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455
|
/********************************************************************
* $Author: jgoerzen $
* $Revision: 1.3 $
* $Date: 2001/01/17 21:16:35 $
* $Source: /home/jgoerzen/tmp/gopher-umn/gopher/head/object/BLblock.c,v $
* $State: Exp $
*
* Paul Lindner, University of Minnesota CIS.
*
* Copyright 1991, 1992, 1993 by the Regents of the University of Minnesota
* see the file "Copyright" in the distribution for conditions of use.
*********************************************************************
* MODULE: BLblock.c
* Implementation of Block handling routines.
*********************************************************************
* Revision History:
* $Log: BLblock.c,v $
* Revision 1.3 2001/01/17 21:16:35 jgoerzen
* More psinrtf -> snprintf changes
*
* Revision 1.2 2000/12/20 01:19:20 jgoerzen
* Added patches from David Allen <s2mdalle@titan.vcu.edu>
*
* Revision 1.1.1.1 2000/08/19 00:28:56 jgoerzen
* Import from UMN Gopher 2.3.1 after GPLization
*
* Revision 3.14 1995/09/25 22:07:15 lindner
* Ansification
*
* Revision 3.13 1995/04/15 06:50:46 lindner
* check error return on BLblock
*
* Revision 3.12 1995/02/17 18:29:36 lindner
* Abstract display support
*
* Revision 3.11 1994/08/08 20:26:35 lindner
* fix bug for Abstract= lines.
*
* Revision 3.10 1994/06/29 05:45:24 lindner
* Mods for tickets
*
* Revision 3.9 1994/02/20 21:49:09 lindner
* deal with malformed views lines better
*
* Revision 3.8 1993/11/02 06:13:36 lindner
* Fix for HTML mods
*
* Revision 3.7 1993/07/29 19:59:23 lindner
* Removed extraneous variables
*
* Revision 3.6 1993/07/27 05:30:17 lindner
* Mondo Debug overhaul from Mitra
*
* Revision 3.5 1993/06/15 06:10:34 lindner
* Moved string.h include
*
* Revision 3.4 1993/04/27 20:54:32 lindner
* really fixed BLtoNet()
*
* Revision 3.3 1993/04/23 20:14:48 lindner
* Fix for BltoNet(), using wrong datatype
*
* Revision 3.2 1993/03/26 19:49:34 lindner
* More comments, memory leak fix, etc
*
* Revision 3.1.1.1 1993/02/11 18:03:06 lindner
* Gopher+1.2beta release
*
* Revision 2.1 1993/02/09 22:45:54 lindner
* Fixes for retrieving from the net. New BLAsearch
*
* Revision 1.1 1993/01/31 00:22:51 lindner
* Initial revision
*
*
*********************************************************************/
#include "String.h"
#include "BLblock.h"
#include "Malloc.h"
#include <stdio.h>
/*
* Make a new Block Object
*/
Blockobj *
BLnew()
{
Blockobj *temp;
temp = (Blockobj *) malloc(sizeof(Blockobj));
temp->btype = BLOCK_UNKNOWN;
temp->Blockname = STRnew();
temp->datatype = BDATA_NONE;
return(temp);
}
/*
* Initialize the block structure
*/
void
BLinit(Blockobj *bl)
{
STRinit(bl->Blockname);
switch (BLgetDatatype(bl)) {
case BDATA_FILE:
STRinit(bl->data.filename);
break;
case BDATA_GREF:
GSinit(bl->data.gs);
break;
case BDATA_TEXT:
STAinit(bl->data.text);
break;
case BDATA_NONE:
break;
}
}
/*
* Calls the right destory fcn depending on the object in the union.
*
* wouldn't c++ be nice right here? :-)
*/
static void
BLdatadestroy(Blockobj *bl)
{
switch (BLgetDatatype(bl)) {
case BDATA_FILE:
STRdestroy(bl->data.filename);
break;
case BDATA_TEXT:
STAdestroy(bl->data.text);
break;
case BDATA_GREF:
GSdestroy(bl->data.gs);
break;
case BDATA_NONE:
break;
}
}
/*
* Free memory of BlockObj
*/
void
BLdestroy(Blockobj *bl)
{
STRdestroy(bl->Blockname);
BLdatadestroy(bl);
free(bl);
}
void
BLcpy(Blockobj *dest, Blockobj *orig)
{
BLsetName(dest,BLgetName(orig));
dest->btype = orig->btype;
switch (BLgetDatatype(orig)) {
case BDATA_FILE:
BLsetFile(dest, STRget(orig->data.filename));
break;
case BDATA_GREF:
BLsetGref(dest, orig->data.gs);
break;
case BDATA_TEXT:
BLsetText(dest, orig->data.text);
break;
case BDATA_NONE:
break;
}
}
/*
* BLgetNumLines() returns the number of lines for the Text record
* It returns -1 if this isn't the type of record.
*/
int
BLgetNumLines(Blockobj *bl)
{
switch (BLgetDatatype(bl)) {
case BDATA_TEXT:
return(STAgetTop(bl->data.text));
default:
return(-1);
}
;
}
/*
* BLgetLine() returns a character pointer to the line for the Text data type
*/
char*
BLgetLine(Blockobj *bl, int lineno)
{
switch (BLgetDatatype(bl)) {
case BDATA_TEXT:
return(STRget(STAgetEntry(bl->data.text, lineno)));
case BDATA_NONE:
case BDATA_FILE:
case BDATA_GREF:
break;
}
return(NULL);
}
/*
* BLsetFile() sets up a block to contain a file reference.
* Really handy for the server.
*/
void
BLsetFile(Blockobj *bl, char *filename)
{
/** Reset data field and put in new values **/
BLdatadestroy(bl);
bl->datatype = BDATA_FILE;
bl->data.filename = STRnew();
STRset(bl->data.filename, filename);
}
/*
* BLsetText sets everything up for storing text, if the optional parameter
* sta is set, it copies it in.
*/
void
BLsetText(Blockobj *bl, StrArray *sta)
{
/** Reset data field and put in new values **/
if (BLgetDatatype(bl) != BDATA_TEXT) {
BLdatadestroy(bl);
bl->datatype = BDATA_TEXT;
bl->data.text = STAnew(10);
}
if (sta != NULL) {
STAcpy(bl->data.text, sta);
}
}
/*
* Keep data in memory, text is a line to be added to the text
*/
void
BLaddText(Blockobj *bl, char *text)
{
String *tempstr;
BLsetText(bl, NULL);
tempstr = STRnew();
STRset(tempstr, text);
STApush(bl->data.text, tempstr);
STRdestroy(tempstr);
}
/*
* Make the block a gopher-reference
*/
void
BLsetGref(Blockobj *bl, GopherObj *gs)
{
/** Reset data field and put in new values **/
BLdatadestroy(bl);
bl->datatype = BDATA_GREF;
bl->data.gs = GSnew();
GScpy(bl->data.gs, gs);
GSdestroy(gs);
}
/*
* BLtoNet() transmits the block as per gopher+ protocol
* if the data field is a file, it opens it and sends it out
*/
void
BLtoNet(Blockobj *bl, int fd, boolean showheader)
{
FILE *infile;
int i;
char outputline[512];
/** Switch on data type **/
if (showheader) {
snprintf(outputline, sizeof(outputline), "+%s:", BLgetName(bl));
writestring(fd, outputline);
}
switch (BLgetDatatype(bl)) {
case BDATA_GREF:
writestring(fd, " ");
GStoNet(bl->data.gs,fd, GSFORM_G0, NULL);
break;
case BDATA_TEXT:
writestring(fd, "\r\n");
for (i=0; i<STAgetTop(bl->data.text); i++) {
writestring(fd, " ");
writestring(fd, STRget(STAgetEntry(bl->data.text, i)));
writestring(fd, "\r\n");
}
break;
case BDATA_FILE:
writestring(fd, "\r\n");
if ((infile = fopen(STRget(bl->data.filename), "r"))==NULL)
break;
while (fgets(outputline, sizeof(outputline), infile)!=NULL) {
ZapCRLF(outputline);
writestring(fd, " ");
writestring(fd, outputline);
writestring(fd, "\r\n");
}
fclose(infile);
break;
case BDATA_NONE:
break;
}
}
/*
* BLfromNet() assumes that the initial '+' in the data stream has been read,
* along with the blockname, up to the ':', but not anything after..
*
* It then executes most of the state diagram, it returns when it has
* encountered EOF, or encounters the next '+', or '.'
*
* Returns the following:
* 0 for EOF encountered, block retrieved successfully
* 1 for successful retrieve, and another block coming
* neg value for error conditions from read routines and botched block vals
*/
int
BLfromNet(Blockobj *bl, int fd, char *blockname)
{
char inputline[512];
int err;
/*** State: _GotBlockName_ ***/
BLsetName(bl, blockname);
/** Find out if there's a gopher reference **/
if ((err = readrecvbuf(fd, inputline, 1)) <=0) {
return(err);
}
if (*inputline == ' ') {
/** GREF coming up, State: _GotBnameSpace_ **/
GopherObj *gs;
gs = GSnew();
GSfromNet(gs, fd);
BLsetGref(bl, gs);
/** read up to the next \r\n+ **/
while (1) {
if ((err= readrecvbuf(fd, inputline, 1)) <= 0)
return(err);
if (*inputline == '+')
return(1);
else {
err = readline(fd, inputline, sizeof(inputline));
if (err <= 0)
return(err); /*** Error ***/
}
}
}
/** Okay, let's just stick the text in verbatim **/
/** get rid of remaining newline **/
readline(fd, inputline, sizeof(inputline));
/** State: _FirstChar_ **/
while (1) {
/** Check for plus **/
if ((err= readrecvbuf(fd, inputline, 1)) <=0)
return(err);
if (*inputline == '+')
return(1);
/*** Return to state _NewBlock_ ***/
if (*inputline == '.') {
readline(fd, inputline, sizeof(inputline));
return(0);
}
if (*inputline == ' ') {
/** add a line otherwise State: _Addline_ **/
readline(fd, inputline, sizeof(inputline));
ZapCRLF(inputline);
BLaddText(bl, inputline);
} else {
/** malformed block line received
proceed as if it was OK, but don't add the line **/
readline(fd, inputline, sizeof(inputline));
}
}
}
int
BLAsearch(BlockArray *bla, char *bname)
{
int i;
if (bla == NULL)
return(-1);
for (i=0; i<BLAgetTop(bla); i++) {
if (strcmp(BLgetName(BLAgetEntry(bla,i)), bname)==0)
return(i);
}
return(-1);
}
|