DataMuseum.dk

Presents historical artifacts from the history of:

DKUUG/EUUG Conference tapes

This is an automatic "excavation" of a thematic subset of
artifacts from Datamuseum.dk's BitArchive.

See our Wiki for more about DKUUG/EUUG Conference tapes

Excavated with: AutoArchaeologist - Free & Open Source Software.


top - metrics - download
Index: T p

⟦0e026861f⟧ TextFile

    Length: 16855 (0x41d7)
    Types: TextFile
    Names: »p2flatten.c«

Derivation

└─⟦2d1937cfd⟧ Bits:30007241 EUUGD22: P.P 5.0
    └─⟦dc59850a2⟧ »EurOpenD22/pp5.0/pp-5.tar.Z« 
        └─⟦e5a54fb17⟧ 
            └─⟦this⟧ »pp-5.0/Format/p2flatten/p2flatten.c« 

TextFile

/* p2flatten: flatten a P2 directory structure into a message again */

# ifndef lint
static char Rcsid[] = "@(#)$Header: /cs/research/pp/hubris/pp-beta/Format/p2flatten/RCS/p2flatten.c,v 5.0 90/09/20 16:01:09 pp Exp Locker: pp $";
# endif

/*
 * $Header: /cs/research/pp/hubris/pp-beta/Format/p2flatten/RCS/p2flatten.c,v 5.0 90/09/20 16:01:09 pp Exp Locker: pp $
 *
 * $Log:	p2flatten.c,v $
 * Revision 5.0  90/09/20  16:01:09  pp
 * rcsforce : 5.0 public release
 * 
 */



#include "util.h"
#include <sys/stat.h>
#include <isode/psap.h>
#include <varargs.h>
#include "retcode.h"
#include <isode/cmd_srch.h>
#include "tb_bpt88.h"
#include "P2-types.h"

extern CMD_TABLE bptbl_body_parts88[/* x400 88 body_parts */];
extern char *quedfldir;
extern char *mquedir, *cont_p2, *cont_p22;
extern void	err_abrt();
static char curdir[FILNSIZE];   /* directory stack */
static PE read_bp();

char    curfile[FILNSIZE];
int     curdepth = 0;
int     more = TRUE;
int     flatresult = OK;

void    advise ();
void    adios ();
#define ps_advise(ps, f) \
	advise (NULLCP, "%s: %s", (f), ps_error ((ps) -> ps_errno))

flatten(old,new, x40084, perr)
char    *old;
char    *new;
int	x40084;
char	**perr;
{
	PE      pe = NULLPE;
	struct	type_P2_InformationObject	*infoobj = NULL;
	char	buf[BUFSIZ];

	curdepth = depth(old) + 1;
	msg_rinit(old);
	flatresult = OK;

	if (getname(curfile) != OK) {
		(void) sprintf (buf,
				"Source directory '%s' is empty",
				old);
		*perr = strdup(buf);
		PP_LOG(LLOG_EXCEPTIONS,
		       ("No files in %s", old));
		flatresult = NOTOK;
	}
	setname(old);
	more = TRUE;

	fillin_infoobj(&infoobj, x40084, perr);
	
	msg_rend();

	if (flatresult == OK) {
		encode_P2_InformationObject (&pe, 1, 0, NULLCP, infoobj);
		pe_fragment (pe, 128);
	}

	if (flatresult == OK) {
		setname (new);
		write_bp((x40084 == TRUE) ? cont_p2 : cont_p22, pe, perr);
		if (flatresult == NOTOK) {
			PP_LOG(LLOG_EXCEPTIONS,
				("Chans/p2flatten : write_bp failed"));
			*perr = strdup("Failed to write out p2 body part");
		}
	} else
		PP_LOG(LLOG_EXCEPTIONS,
			("Chans/p2flatten : encode_P2_UAPDU failed"));
	free_P2_InformationObject(infoobj);
	if (pe != NULLPE) pe_free (pe);

	return flatresult;
}

extern  errno;

/* pe_done: utility routine to do the right thing for pe errors */
int             pe_done (pe, msg)
PE              pe;
char            *msg;
{
	if (pe->pe_errno)
	{
		advise (LLOG_EXCEPTIONS,NULLCP,"%s: %s", msg, pe_error(pe->pe_errno));
		pe_free (pe);
		return 1;
	}
	else
	{
		pe_free (pe);
		return 0;
	}
}

/* \f

 */

int	fillin_infoobj(pinfoobj, x40084, perr)
struct type_P2_InformationObject	**pinfoobj;
int	x40084;
char	**perr;
{
	char	buf[BUFSIZ];
	int  type = bp_type(curfile);
	*pinfoobj = (struct type_P2_InformationObject *) calloc(1,
						  sizeof(struct type_P2_InformationObject));
	/* what to do about sr ? */
	if (type == ((x40084 == TRUE) ? BPT_HDR_P2 : BPT_HDR_P22)) {
		(*pinfoobj)->offset = type_P2_InformationObject_ipm;
		fillin_ipm(&((*pinfoobj)->un.ipm), x40084, perr);
	} else if (type == BPT_HDR_IPN) {
		(*pinfoobj)->offset = type_P2_InformationObject_ipn;
		fillin_ipn (&((*pinfoobj)->un.ipn), x40084, perr);
	} else {
		PP_LOG(LLOG_EXCEPTIONS,
		       ("Expected p2 header got '%s'", curfile));
		(void) sprintf(buf,
			       "Expected p2 header got '%s'",
			       curfile);
		*perr = strdup(buf);
		flatresult = NOTOK;
	}
}

int	fillin_ipn (pipn, x40084, perr)
struct type_P2_IPN	**pipn;
int 	x40084;
char	**perr;
{
	PE	pe;
	char	buf[BUFSIZ];

	pe = read_bp(curfile);
	
	PY_pepy[0] = 0;
	if (decode_P2_IPN (pe, 1, NULLIP, NULLVP, pipn) == NOTOK) {
		PP_LOG(LLOG_EXCEPTIONS,
		       ("decode_P2_IPN failure [%s]",
			PY_pepy));
		(void) sprintf (buf,
				"Failed to decode P2 IPN [%s]",
				PY_pepy);
		*perr = strdup(buf);
		flatresult = NOTOK;
		return;
	}
	if ((*pipn)->choice->offset == choice_P2_0_non__receipt__fields
	    && getname(curfile) == OK)
		fillin_ipm(&((*pipn) -> choice -> un.non__receipt__fields -> returned__ipm),
			   x40084, perr);
	else
		more = FALSE;
}

int	fillin_ipm(pipm, x40084, perr)
struct type_P2_IPM	**pipm;
int	x40084;
char	**perr;
{
	*pipm = (struct type_P2_IPM *)
		calloc(1, sizeof(struct type_P2_IPM));
	
	fillin_heading(&((*pipm)->heading), x40084, perr);
	if (flatresult == OK)
		fillin_body(&((*pipm)->body), x40084, perr);
}

int	fillin_heading(pheading, x40084, perr)
struct type_P2_Heading	**pheading;
int	x40084;
char	**perr;
{
	char	buf[BUFSIZ];
	extern char *hdr_p2_bp, *hdr_p22_bp;

	int type = bp_type(curfile);
	if (type == (x40084 == TRUE) ? BPT_HDR_P2 : BPT_HDR_P22) {
		*pheading = read_bp(curfile);
		if (getname(curfile) != OK)
			more = FALSE;
	} else {
		PP_LOG(LLOG_EXCEPTIONS,
		       ("Expected '%s' got '%s'",
			(x40084 == TRUE) ? hdr_p2_bp : hdr_p22_bp,
			curfile));
		(void) sprintf(buf,
			       "Expected '%s' got '%s'",
			       (x40084 == TRUE) ? hdr_p2_bp : hdr_p22_bp,
			       curfile);
		*perr = strdup(buf);
		flatresult = NOTOK;
	}
}

int	fillin_body(pbody, x40084, perr)
struct type_P2_Body 	**pbody;
int			x40084;
char			**perr;
{
	struct type_P2_Body	*end = NULL, *list = NULL, *temp;
	char	buf[BUFSIZ];

	while (more == TRUE
	       && curfile != NULL
	       && depth(curfile) >= curdepth
	       && flatresult == OK) {
		
		temp = (struct type_P2_Body *) 
			calloc(1, sizeof(struct type_P2_Body));
		
		if (depth(curfile) > curdepth)
			fillin_forwarded(&(temp->BodyPart), x40084, perr);
		else {
			temp->BodyPart = (struct type_P2_BodyPart *)
				calloc(1, sizeof(struct type_P2_BodyPart));

			switch (bp_type(curfile)) {
			    case BPT_HDR_P2:
			    case BPT_HDR_P22:
			    case BPT_HDR_IPN:
			    case BPT_P2_DLIV_TXT:
				/* should not get these as dealt with by forwarded */
				PP_LOG(LLOG_EXCEPTIONS,
				       ("Unexpected p2 header or delivery text file '%s' ", curfile));
				(void) sprintf (buf,
						"Unexpected p2 header or delivery text file '%s'",
						curfile);
				*perr = strdup(buf);
				flatresult = NOTOK;
				break;
				
			    case BPT_HDR_822:
				PP_LOG(LLOG_EXCEPTIONS,
				       ("822 header in p2 message '%s'", curfile));
				(void) sprintf (buf,
						"822 header in p2 message '%s'",
						curfile);
				*perr = strdup(buf);
				flatresult = NOTOK;
				break;

			    case BPT_IA5:
				temp->BodyPart->offset = type_P2_BodyPart_ia5__text;
				fillin_ia5(&(temp->BodyPart->un.ia5__text));
				break;

			    case BPT_TLX:
				temp->BodyPart->offset = type_P2_BodyPart_tlx;
				temp->BodyPart->un.tlx = read_bp(curfile);
				break;

			    case BPT_VOICE:
				temp->BodyPart->offset = type_P2_BodyPart_voice;
				temp->BodyPart->un.voice = read_bp(curfile);
				break;

			    case BPT_G3FAX:
				temp->BodyPart->offset = type_P2_BodyPart_g3__facsimile;
				temp->BodyPart->un.g3__facsimile = read_bp(curfile);
				break;

			    case BPT_TIF0:
				temp->BodyPart->offset = type_P2_BodyPart_g4__class1;
				temp->BodyPart->un.g4__class1 = read_bp(curfile);
				break;

			    case BPT_TTX:
				temp->BodyPart->offset = type_P2_BodyPart_teletex;
				temp->BodyPart->un.teletex = read_bp(curfile);
				break;

			    case BPT_VIDEOTEX:
				temp->BodyPart->offset = type_P2_BodyPart_videotex;
				temp->BodyPart->un.videotex = read_bp(curfile);
				break;

			    case BPT_NATIONAL:
				temp->BodyPart->offset = type_P2_BodyPart_nationally__defined;
				temp->BodyPart->un.nationally__defined = read_bp(curfile);
				break;

			    case BPT_ENCRYPTED:
				temp->BodyPart->offset = type_P2_BodyPart_encrypted;
				temp->BodyPart->un.encrypted = read_bp(curfile);
				break;

			    case BPT_SFD:
				temp->BodyPart->offset = type_P2_BodyPart_sfd;
				temp->BodyPart->un.sfd = read_bp(curfile);
				break;

			    case BPT_TIF1:
				temp->BodyPart->offset = type_P2_BodyPart_mixed__mode;
				temp->BodyPart->un.mixed__mode = read_bp(curfile);
				break;
			    
			    case BPT_ODIF:
				temp->BodyPart->offset = type_P2_BodyPart_odif;
				fillin_odif(&(temp->BodyPart->un.odif));
				break;

			    case BPT_ISO6937TEXT:
				temp->BodyPart->offset = type_P2_BodyPart_iso6937Text;
				temp->BodyPart->un.iso6937Text = read_bp(curfile);
				break;
			    
			    case BPT_BILATERAL:
				if (x40084 == TRUE) {
					PP_LOG(LLOG_EXCEPTIONS,
					       ("Unknown p2 84 body part '%s'", 
						curfile));
					(void) sprintf (buf,
							"Unknown p2 84 body part '%s'", 
							curfile);
					*perr = strdup (buf);
					flatresult = NOTOK;
				} else {
					temp->BodyPart->offset = type_P2_BodyPart_bilaterally__defined;
					temp->BodyPart->un.bilaterally__defined = read_bp(curfile);
				}
				break;

			    case BPT_EXTERNAL:
				if (x40084 == TRUE) {
					PP_LOG(LLOG_EXCEPTIONS,
					       ("Unknown p2 84 body part '%s'", 
						curfile));
					(void) sprintf (buf,
							"Unknown p2 84 body part '%s'", 
							curfile);
					*perr = strdup (buf);
					flatresult = NOTOK;
				} else {
					temp->BodyPart->offset = type_P2_BodyPart_externally__defined;
					temp->BodyPart->un.externally__defined = read_bp(curfile);
				}
				break;
			    
					
			    default:
				PP_LOG(LLOG_EXCEPTIONS,
				       ("Unknown p2 body part '%s'", curfile));
				(void) sprintf (buf,
						"Unknown p2 body part '%s'", 
						curfile);
				*perr = strdup (buf);
				flatresult = NOTOK;
				break;
			}
		}
		
		if (flatresult == OK) {
			if (list == NULL)
				list = end = temp;
			else {
				end -> next = temp;
				end = temp;
			}
			if (getname(curfile) != OK)
				more = FALSE;
		}
	}
	*pbody = list;
}
	
/* ps_done: like pe_done */
int             ps_done (ps, msg)
PS              ps;
char           *msg;
{
	if (ps->ps_errno)
	{
		ps_advise (ps, msg);
		ps_free (ps);
		return 1;
	}
	else
	{
		ps_free (ps);
		return 0;
	}
}

getname(str)
char    *str;
{
	/* msg_rfile returns full pathname need to split of curdir */
	if (msg_rfile(str) != RP_OK) {
		str = NULL;
		return NOTOK;
	}
	return OK;
}

/* initialise name for above */
setname (name)
char    *name;
{
	(void) strcpy (curdir, name);
/*      printf ("setname -> %s\n", curdir);*/
}
/*
 * write_bp: write a body part contained in pe out to the file named
 * name. name is converted into the correct place in the directory
 * tree.
 */

write_bp(name, pe, perr)
char    *name;
PE      pe;
char	**perr;
{
	PS      psout;
	FILE    *fp;
	static char fullname[FILNSIZE];
	char	buf[BUFSIZ];
	sprintf(fullname,"%s/%s", curdir, name);
	PP_LOG(LLOG_TRACE,
		("Chans/p2flatten : Writing %s", fullname));

	if((fp = fopen(fullname, "w")) == NULL)
	{
		PP_SLOG(LLOG_EXCEPTIONS, fullname,
			("Can't open file"));
		(void) sprintf (buf,
				"Unable to open output file '%s'",
				fullname);
		*perr = strdup(buf);
		flatresult = NOTOK;
		return NOTOK;
	}

	if ((psout = ps_alloc(std_open)) == NULLPS)
	{
		ps_advise (psout, "ps_alloc");
		(void) fclose (fp);
		flatresult = NOTOK;
		return NOTOK;
	}
	if (std_setup (psout, fp) == NOTOK)
	{
		advise (NULLCP, "std_setup loses", fullname);
		(void) fclose (fp);
		flatresult = NOTOK;
		return NOTOK;
	}

	if(pe2ps(psout, pe) == NOTOK)
	{
		ps_advise(psout, "pe2ps loses");
		flatresult = NOTOK;
		return NOTOK;
	}
	(void) fclose (fp);
	ps_free(psout);
/*      printf (" done\n");*/
	return OK;
}

/*
 * read_bp: read in a body part and stuff in pe.
 */

static PE read_bp(name)
char    *name;
{
	PS      psout;
	PE      pe;
	FILE    *fp;

	PP_LOG(LLOG_TRACE,
		("Chans/p2flatten : Reading %s", name));

	if((fp = fopen(name, "r")) == NULL)
	{
		PP_SLOG(LLOG_EXCEPTIONS, name,
			("Can't open file"));
		flatresult = NOTOK;
		return NULLPE;
	}

	if ((psout = ps_alloc(std_open)) == NULLPS)
	{
		ps_advise (psout, "ps_alloc");
		(void) fclose (fp);
		flatresult = NOTOK;             
		return NULLPE;
	}
	if (std_setup (psout, fp) == NOTOK)
	{
		advise (NULLCP, "std_setup loses", name);
		(void) fclose (fp);
		flatresult = NOTOK;
		return NULLPE;
	}

	if((pe = ps2pe(psout)) == NULLPE)
	{
		ps_advise(psout, "ps2pe loses");
		flatresult = NOTOK;             
		return NULLPE;
	}
	(void) fclose (fp);
	ps_free(psout);
/*      printf (" done\n");*/
	return pe;
}

bp_type (str)
char    *str;
{
	char *s, *ix;
	int retval;
	if ((s = rindex(str,'/')) == NULLCP)
		s = str;
	else
		s++;
	/* search for those files that aren't num.str */
	if (strcmp(s,rcmd_srch(BPT_HDR_P2,bptbl_body_parts88)) == 0)
		return BPT_HDR_P2;
	if (strcmp(s,rcmd_srch(BPT_HDR_P22,bptbl_body_parts88)) == 0)
		return BPT_HDR_P22;
	if (strcmp(s,rcmd_srch(BPT_HDR_822,bptbl_body_parts88)) == 0)
		return BPT_HDR_822;
	if (strcmp(s,rcmd_srch(BPT_HDR_IPN,bptbl_body_parts88)) == 0)
		return BPT_HDR_IPN;
	if (strcmp(s,rcmd_srch(BPT_P2_DLIV_TXT,bptbl_body_parts88)) == 0)
		return BPT_P2_DLIV_TXT;

	/* not hdr so n.xxx where n is number and xxx bpt name */

	if ((ix = index(s,'.')) == NULL) {
/*              printf("cannot find '.' char in %s",s);*/
		retval = -1;
	} else {
		ix++;
		retval = (cmd_srch(ix, bptbl_body_parts88));    /* returns # defined number of bp type */
	}
	if (retval == -1) {
		/* bomb out */
		PP_LOG(LLOG_EXCEPTIONS,
			("Chans/p2flatten : Unknown body type '%s'",str));
		flatresult = NOTOK;
	}
	return retval;
}

depth (file)
char    *file;
{
	char    *p;
	int     count = 0;

	for (p = file; *p; p++)
		if (*p == '/') {
			count ++;
			while(*p == '/') p++;
		}
	return count;
}

char    *readoctet (file, len)
char    *file;
int     *len;
{
	char    *p;
	int     fd;
	struct stat st;

	PP_LOG(LLOG_TRACE,
		("Chans/p2flatten reading octet %s",file));

	if ((fd = open(file ,0)) == NOTOK)
	{
		PP_SLOG(LLOG_EXCEPTIONS, file,
			("Can't open file"));
		flatresult = NOTOK;
		return NULLCP;
	}
	if (fstat (fd, &st) == NOTOK)
	{
		PP_SLOG(LLOG_EXCEPTIONS, file,
			("Can't stat file"));
		flatresult = NOTOK;
		return NULLCP;
	}
	*len = st.st_size;
	p = smalloc (*len + 1);
	if (read (fd, p, *len) != *len) {
		advise ("read", "Read failed");
		flatresult = NOTOK;
	}
	close (fd);
/*      printf ("done\n"); */
	return p;
}

#define BUF_INC	128

char    *readCRLFstring (file, plen)
char    *file;
int	*plen;
{
	char    *p;
	FILE 	*fd;
	unsigned int	c,
		len,
		count;

	PP_LOG(LLOG_TRACE,
		("Chans/p2flatten reading CRLF string %s",file));

	if ((fd = fopen(file ,"r")) == (FILE *) 0)
	{
		PP_SLOG(LLOG_EXCEPTIONS, file,
			("Can't open file"));
		flatresult = NOTOK;
		return NULLCP;
	}
	len = BUF_INC;
	p = smalloc (len);
	count = 0;
	while ((c = getc(fd)) != EOF) {
		if (c == (int) ('\n')) {
			if (count >= len) {
				len += BUF_INC;
				p = realloc(p, len);
			}
			p[count++] = '\r';
		}
		if (count >= len) {
			len += BUF_INC;
			p = realloc(p, len);
		}
		p[count++] = (char) c;
	}
	if (count >= len) {
		len += 1;
		p = realloc(p, len);
	}
	*plen = count;	
	fclose (fd);
/*      printf ("done\n"); */
	return p;
}

fillin_ia5(pia5text)
struct type_P2_IA5TextBodyPart	**pia5text;
{
	int	len = 0;
	char	*str;

	*pia5text = (struct type_P2_IA5TextBodyPart *)
		calloc(1, sizeof(struct type_P2_IA5TextBodyPart));
	(*pia5text)->parameters = (struct type_P2_IA5Parameters *)
		calloc(1, sizeof(struct type_P2_IA5Parameters));
	(*pia5text)->parameters->parm = int_P2_Repertoire_ia5;
	str = readCRLFstring(curfile, &len);
	(*pia5text)->data = str2qb(str, len, 1);

	free(str);
}
	
fillin_odif(podif)
struct type_P2_ODIFBodyPart	**podif;
{
	int	len = 0;
	char	*str;
	
	str = readoctet(curfile, &len);
	if (str != NULL) {
		*podif = str2qb(str, len, 1);
		free(str);
	}
}
	
fillin_forwarded(pbp, x40084)
struct type_P2_BodyPart	**pbp;
int	x40084;
{
	int	oldepth = curdepth;

	*pbp = (struct type_P2_BodyPart *)
		calloc (1, sizeof(struct type_P2_BodyPart));
	(*pbp)->offset = type_P2_BodyPart_message;
	(*pbp)->un.message = (struct type_P2_MessageBodyPart *)
		calloc(1, sizeof(struct type_P2_MessageBodyPart));
	
	curdepth = depth(curfile);

	PY_pepy[0] = 0;
	if (bp_type(curfile) == BPT_P2_DLIV_TXT) {
		PE pe;	
		pe = read_bp(curfile);
		if (decode_P2_MessageParameters(pe, 1, NULLIP, 
						NULLVP, 
						&((*pbp)->un.message->parameters)) == NOTOK) {
			PP_LOG(LLOG_EXCEPTIONS,
			       ("decode_P2_MessageParameters failure [%s]",
				PY_pepy));
			flatresult = NOTOK;
			return;
		}
	} else 
		(*pbp)->un.message->parameters = (struct type_P2_MessageParameters *) calloc (1, sizeof(struct type_P2_MessageParameters));

	fillin_ipm(&((*pbp)->un.message->data), x40084);

	curdepth = oldepth;
}
		
			
/* \f

   ERRORS */

#ifndef lint
void    adios (va_alist)
va_dcl
{
	va_list ap;

	va_start (ap);

	_ll_log (pp_log_norm, LLOG_FATAL, ap);

	va_end (ap);

	_exit (1);
}
#else
/* VARARGS2 */

void    adios (what, fmt)
char   *what,
       *fmt;
{
	adios (what, fmt);
}
#endif


#ifndef lint
void    advise (va_alist)
va_dcl
{
	int     code;
	va_list ap;

	va_start (ap);

	code = va_arg (ap, int);

	_ll_log (pp_log_norm, code, ap);

	va_end (ap);
}
#else
/* VARARGS3 */

void    advise (code, what, fmt)
char   *what,
       *fmt;
int     code;
{
	advise (code, what, fmt);
}
#endif