home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / mail / pp / pp-6.0 / Format / p2flatten / p2flatten.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-12-18  |  22.1 KB  |  991 lines

  1. /* p2flatten: flatten a P2 directory structure into a message again */
  2.  
  3. # ifndef lint
  4. static char Rcsid[] = "@(#)$Header: /xtel/pp/pp-beta/Format/p2flatten/RCS/p2flatten.c,v 6.0 1991/12/18 20:20:12 jpo Rel $";
  5. # endif
  6.  
  7. /*
  8.  * $Header: /xtel/pp/pp-beta/Format/p2flatten/RCS/p2flatten.c,v 6.0 1991/12/18 20:20:12 jpo Rel $
  9.  *
  10.  * $Log: p2flatten.c,v $
  11.  * Revision 6.0  1991/12/18  20:20:12  jpo
  12.  * Release 6.0
  13.  *
  14.  */
  15.  
  16.  
  17.  
  18. #include "util.h"
  19. #include <sys/stat.h>
  20. #include <isode/psap.h>
  21. #include <varargs.h>
  22. #include "retcode.h"
  23. #include <isode/cmd_srch.h>
  24. #include "tb_bpt88.h"
  25. #include "IOB-types.h"
  26.  
  27. extern CMD_TABLE bptbl_body_parts88[/* x400 88 body_parts */];
  28. extern char *quedfldir;
  29. extern char *cont_p2, *cont_p22;
  30. extern void    err_abrt();
  31. static char curdir[MAXPATHLENGTH];   /* directory stack */
  32. static caddr_t read_bpfile();
  33.  
  34. char    curfile[MAXPATHLENGTH];
  35. int     curdepth = 0;
  36. int     more = TRUE;
  37. int     flatresult = OK, err_fatal = FALSE;
  38.  
  39. void    advise ();
  40. void    adios ();
  41. #define ps_advise(ps, f) \
  42.     advise (NULLCP, "%s: %s", (f), ps_error ((ps) -> ps_errno))
  43.  
  44. flatten(old,new, x40084, perr)
  45. char    *old;
  46. char    *new;
  47. int    x40084;
  48. char    **perr;
  49. {
  50.     PE      pe = NULLPE;
  51.     struct    type_IOB_InformationObject    *infoobj = NULL;
  52.     char    buf[BUFSIZ];
  53.  
  54.     curdepth = depth(old) + 1;
  55.     (void) sprintf(curdir, old);
  56.     msg_rinit(old);
  57.     flatresult = OK;
  58.  
  59.     if (getname(curfile) != OK) {
  60.         (void) sprintf (buf,
  61.                 "Source directory '%s' is empty",
  62.                 old);
  63.         *perr = strdup(buf);
  64.         PP_LOG(LLOG_EXCEPTIONS,
  65.                ("No files in %s", old));
  66.         flatresult = NOTOK;
  67.     }
  68.     setname(old);
  69.     more = TRUE;
  70.  
  71.     fillin_infoobj(&infoobj, x40084, perr);
  72.     
  73.     msg_rend();
  74.  
  75.     PY_pepy[0] = 0;
  76.  
  77.     if (flatresult == OK) {
  78.         if (encode_IOB_InformationObject (&pe, 1, 0, NULLCP, infoobj) != OK) {
  79.             PP_LOG(LLOG_EXCEPTIONS,
  80.                    ("p2flatten failed to encode InformationObject [%s]",
  81.                 PY_pepy));
  82.             (void) sprintf (buf,
  83.                     "Failed to encode message as p2 (%s)",
  84.                     (x40084 == TRUE) ? "84" : "88");
  85.             *perr = strdup(buf);
  86.             err_fatal = TRUE;
  87.             flatresult = NOTOK;
  88.         } else
  89.             pe_fragment (pe, 128);
  90.     }
  91.  
  92.     if (flatresult == OK) {
  93.         setname (new);
  94.         write_bp((x40084 == TRUE) ? cont_p2 : cont_p22, pe, perr);
  95.         if (flatresult == NOTOK) {
  96.             PP_LOG(LLOG_EXCEPTIONS,
  97.                 ("Chans/p2flatten : write_bp failed"));
  98.             *perr = strdup("Failed to write out p2 body part");
  99.         }
  100.     } else
  101.         PP_LOG(LLOG_EXCEPTIONS,
  102.             ("Chans/p2flatten : encode_IOB_InformationObject failed"));
  103.     free_IOB_InformationObject(infoobj);
  104.     if (pe != NULLPE) pe_free (pe);
  105.  
  106.     return flatresult;
  107. }
  108.  
  109. extern  errno;
  110.  
  111. /* pe_done: utility routine to do the right thing for pe errors */
  112. int             pe_done (pe, msg)
  113. PE              pe;
  114. char            *msg;
  115. {
  116.     if (pe->pe_errno)
  117.     {
  118.         advise (LLOG_EXCEPTIONS,NULLCP,"%s: %s", msg, pe_error(pe->pe_errno));
  119.         pe_free (pe);
  120.         return 1;
  121.     }
  122.     else
  123.     {
  124.         pe_free (pe);
  125.         return 0;
  126.     }
  127. }
  128.  
  129. /*   */
  130.  
  131. int    fillin_infoobj(pinfoobj, x40084, perr)
  132. struct type_IOB_InformationObject    **pinfoobj;
  133. int    x40084;
  134. char    **perr;
  135. {
  136.     char    buf[BUFSIZ];
  137.     int  type = bp_type(curfile);
  138.     *pinfoobj = (struct type_IOB_InformationObject *) calloc(1,
  139.                           sizeof(struct type_IOB_InformationObject));
  140.     /* what to do about sr ? */
  141.     if (type == ((x40084 == TRUE) ? BPT_HDR_P2 : BPT_HDR_P22)) {
  142.         (*pinfoobj)->offset = type_IOB_InformationObject_ipm;
  143.         fillin_ipm(&((*pinfoobj)->un.ipm), x40084, perr);
  144.     } else if (type == BPT_HDR_IPN) {
  145.         (*pinfoobj)->offset = type_IOB_InformationObject_ipn;
  146.         fillin_ipn (&((*pinfoobj)->un.ipn), x40084, perr);
  147.     } else {
  148.         PP_LOG(LLOG_EXCEPTIONS,
  149.                ("Expected %s header got '%s'",
  150.             (x40084 == TRUE) ? "p2" : "p22",
  151.             curfile));
  152.         (void) sprintf(buf,
  153.                    "Expected %s header got '%s'",
  154.                    (x40084 == TRUE) ? "p2" : "p22",
  155.                    curfile);
  156.         *perr = strdup(buf);
  157.         flatresult = NOTOK;
  158.     }
  159. }
  160.  
  161. int    fillin_ipn (pipn, x40084, perr)
  162. struct type_IOB_IPN    **pipn;
  163. int     x40084;
  164. char    **perr;
  165. {
  166.     if ((*pipn = (struct type_IOB_IPN *) 
  167.          read_bpfile(curfile,
  168.              &_ZIOB_mod,
  169.              _ZIPNIOB)) == NULL)
  170.         return;
  171.  
  172.     if ((*pipn)->choice->offset == choice_IOB_0_non__receipt__fields
  173.         && getname(curfile) == OK)
  174.         fillin_ipm(&((*pipn) -> choice -> un.non__receipt__fields -> returned__ipm),
  175.                x40084, perr);
  176.     else
  177.         more = FALSE;
  178. }
  179.  
  180. int    fillin_ipm(pipm, x40084, perr)
  181. struct type_IOB_IPM    **pipm;
  182. int    x40084;
  183. char    **perr;
  184. {
  185.     *pipm = (struct type_IOB_IPM *)
  186.         calloc(1, sizeof(struct type_IOB_IPM));
  187.     
  188.     fillin_heading(&((*pipm)->heading), x40084, perr);
  189.     if (flatresult == OK)
  190.         fillin_body(&((*pipm)->body), x40084, perr);
  191. }
  192.  
  193. int    fillin_heading(pheading, x40084, perr)
  194. struct type_IOB_Heading    **pheading;
  195. int    x40084;
  196. char    **perr;
  197. {
  198.     char    buf[BUFSIZ];
  199.     extern char *hdr_p2_bp, *hdr_p22_bp;
  200.  
  201.     int type = bp_type(curfile);
  202.     if (type == (x40084 == TRUE) ? BPT_HDR_P2 : BPT_HDR_P22) {
  203.         *pheading = (struct type_IOB_Heading *)
  204.             read_bpfile(curfile,
  205.                     &_ZIOB_mod,
  206.                     _ZHeadingIOB);
  207.         if (getname(curfile) != OK)
  208.             more = FALSE;
  209.     } else {
  210.         PP_LOG(LLOG_EXCEPTIONS,
  211.                ("Expected '%s' got '%s'",
  212.             (x40084 == TRUE) ? hdr_p2_bp : hdr_p22_bp,
  213.             curfile));
  214.         (void) sprintf(buf,
  215.                    "Expected '%s' got '%s'",
  216.                    (x40084 == TRUE) ? hdr_p2_bp : hdr_p22_bp,
  217.                    curfile);
  218.         *perr = strdup(buf);
  219.         flatresult = NOTOK;
  220.     }
  221. }
  222.  
  223. int    fillin_body(pbody, x40084, perr)
  224. struct type_IOB_Body     **pbody;
  225. int            x40084;
  226. char            **perr;
  227. {
  228.     struct type_IOB_Body    *end = NULL, *list = NULL, *temp;
  229.     char    buf[BUFSIZ];
  230.  
  231.     while (more == TRUE
  232.            && curfile != NULL
  233.            && depth(curfile) >= curdepth
  234.            && lexnequ(curfile, curdir, strlen(curdir)) == 0
  235.            && flatresult == OK) {
  236.         
  237.         temp = (struct type_IOB_Body *) 
  238.             calloc(1, sizeof(struct type_IOB_Body));
  239.         
  240.         if (depth(curfile) > curdepth)
  241.             fillin_forwarded(&(temp->BodyPart), x40084, perr);
  242.         else {
  243.             temp->BodyPart = (struct type_IOB_BodyPart *)
  244.                 calloc(1, sizeof(struct type_IOB_BodyPart));
  245.  
  246.             switch (bp_type(curfile)) {
  247.                 case BPT_HDR_P2:
  248.                 case BPT_HDR_P22:
  249.                 case BPT_HDR_IPN:
  250.                 case BPT_P2_DLIV_TXT:
  251.                 /* should not get these as dealt with by forwarded */
  252.                 PP_LOG(LLOG_EXCEPTIONS,
  253.                        ("Unexpected p2 header or delivery text file '%s' ", curfile));
  254.                 (void) sprintf (buf,
  255.                         "Unexpected p2 header or delivery text file '%s'",
  256.                         curfile);
  257.                 *perr = strdup(buf);
  258.                 flatresult = NOTOK;
  259.                 break;
  260.                 
  261.                 case BPT_HDR_822:
  262.                 PP_LOG(LLOG_EXCEPTIONS,
  263.                        ("822 header in p2 message '%s'", curfile));
  264.                 (void) sprintf (buf,
  265.                         "822 header in p2 message '%s'",
  266.                         curfile);
  267.                 *perr = strdup(buf);
  268.                 flatresult = NOTOK;
  269.                 break;
  270.  
  271.                 case BPT_IA5:
  272.                 temp->BodyPart->offset = type_IOB_BodyPart_ia5__text;
  273.                 fillin_ia5(&(temp->BodyPart->un.ia5__text));
  274.                 break;
  275.  
  276.                 case BPT_TLX:
  277.                 temp->BodyPart->offset = type_IOB_BodyPart_tlx;
  278.                 temp->BodyPart->un.tlx = 
  279.                     (struct type_IOB_TLXBodyPart *) 
  280.                         read_bpfile(curfile,
  281.                                 &_ZIOB_mod,
  282.                                 _ZTLXBodyPartIOB);
  283.                 if (flatresult != OK) {
  284.                     PP_LOG(LLOG_EXCEPTIONS,
  285.                            ("Unable to read in telex bodypart '%s'", 
  286.                             curfile));
  287.                     (void) sprintf (buf,
  288.                             "problem decoding telex bodypart");
  289.                     err_fatal = TRUE;
  290.                     *perr = strdup(buf);
  291.                 }
  292.                 break;
  293.  
  294.                 case BPT_VOICE:
  295.                 temp->BodyPart->offset = type_IOB_BodyPart_voice;
  296.                 temp->BodyPart->un.voice = 
  297.                     (struct type_IOB_VoiceBodyPart *)
  298.                         read_bpfile(curfile,
  299.                                 &_ZIOB_mod,
  300.                                 _ZVoiceBodyPartIOB);
  301.                 if (flatresult != OK) {
  302.                     PP_LOG(LLOG_EXCEPTIONS,
  303.                            ("Unable to read in voice bodypart '%s'", 
  304.                         curfile));
  305.                     (void) sprintf (buf,
  306.                             "problem decoding voice bodypart");
  307.                     err_fatal = TRUE;
  308.                     *perr = strdup(buf);
  309.                 }
  310.                 break;
  311.  
  312.                 case BPT_G3FAX:
  313.                 temp->BodyPart->offset = type_IOB_BodyPart_g3__facsimile;
  314.                 temp->BodyPart->un.g3__facsimile = 
  315.                     (struct type_IOB_G3FacsimileBodyPart *)
  316.                         read_bpfile(curfile,
  317.                                 &_ZIOB_mod,
  318.                                 _ZG3FacsimileBodyPartIOB);
  319.                 if (flatresult != OK) {
  320.                     PP_LOG(LLOG_EXCEPTIONS,
  321.                            ("Unable to read in g3fax bodypart '%s'", 
  322.                         curfile));
  323.                     (void) sprintf (buf,
  324.                             "problem decoding g3fax bodypart");
  325.                     err_fatal = TRUE;
  326.                     *perr = strdup(buf);
  327.                 }
  328.                 break;
  329.  
  330.                 case BPT_TIF0:
  331.                 temp->BodyPart->offset = type_IOB_BodyPart_g4__class1;
  332.                 temp->BodyPart->un.g4__class1 = 
  333.                     (struct type_IOB_G4Class1BodyPart *)
  334.                         read_bpfile(curfile,
  335.                                 &_ZIOB_mod,
  336.                                 _ZG4Class1BodyPartIOB);
  337.                 if (flatresult != OK) {
  338.                     PP_LOG(LLOG_EXCEPTIONS,
  339.                            ("Unable to read in g4 bodypart '%s'", 
  340.                         curfile));
  341.                     (void) sprintf (buf,
  342.                             "problem decoding g4 bodypart");
  343.                     err_fatal = TRUE;
  344.                     *perr = strdup(buf);
  345.                 }
  346.                 break;
  347.  
  348.                 case BPT_TTX:
  349.                 temp->BodyPart->offset = type_IOB_BodyPart_teletex;
  350.                 temp->BodyPart->un.teletex = 
  351.                     (struct type_IOB_TeletexBodyPart *)
  352.                         read_bpfile(curfile,
  353.                                 &_ZIOB_mod,
  354.                                 _ZTeletexBodyPartIOB);
  355.                 if (flatresult != OK) {
  356.                     PP_LOG(LLOG_EXCEPTIONS,
  357.                            ("Unable to read in teletex bodypart '%s'", 
  358.                         curfile));
  359.                     (void) sprintf (buf, 
  360.                             "problem decoding teletex bodypart");
  361.                     err_fatal = TRUE;
  362.                     *perr = strdup(buf);
  363.                 }
  364.                 break;
  365.  
  366.                 case BPT_VIDEOTEX:
  367.                 temp->BodyPart->offset = type_IOB_BodyPart_videotex;
  368.                 temp->BodyPart->un.videotex =
  369.                     (struct type_IOB_VideotexBodyPart *)
  370.                         read_bpfile(curfile,
  371.                                 &_ZIOB_mod,
  372.                                 _ZVideotexBodyPartIOB);
  373.                 if (flatresult != OK) {
  374.                     PP_LOG(LLOG_EXCEPTIONS,
  375.                            ("Unable to read in videotex bodypart '%s'", 
  376.                         curfile));
  377.                     (void) sprintf (buf,
  378.                             "problem decoding videotex bodypart");
  379.                     err_fatal = TRUE;
  380.                     *perr = strdup(buf);
  381.                 }
  382.                 break;
  383.  
  384.                 case BPT_NATIONAL:
  385.                 temp->BodyPart->offset = type_IOB_BodyPart_nationally__defined;
  386.                 temp->BodyPart->un.nationally__defined = 
  387.                     (struct type_IOB_NationallyDefinedBodyPart *)
  388.                         read_bpfile(curfile,
  389.                                 &_ZIOB_mod,
  390.                                 _ZNationallyDefinedBodyPartIOB);
  391.                 if (flatresult != OK) {
  392.                     PP_LOG(LLOG_EXCEPTIONS,
  393.                            ("Unable to read in nationally defined bodypart '%s'", 
  394.                         curfile));
  395.                     (void) sprintf (buf,
  396.                             "problem decoding nationally defined bodypart");
  397.                     err_fatal = TRUE;
  398.                     *perr = strdup(buf);
  399.                 }
  400.                 break;
  401.  
  402.                 case BPT_ENCRYPTED:
  403.                 temp->BodyPart->offset = type_IOB_BodyPart_encrypted;
  404.                 temp->BodyPart->un.encrypted = 
  405.                     (struct type_IOB_EncryptedBodyPart *)
  406.                         read_bpfile(curfile,
  407.                                 &_ZIOB_mod,
  408.                                 _ZEncryptedBodyPartIOB);
  409.  
  410.                 if (flatresult != OK) {
  411.                     PP_LOG(LLOG_EXCEPTIONS,
  412.                            ("Unable to read in encrypted bodypart '%s'",
  413.                         curfile));
  414.                     (void) sprintf (buf,
  415.                             "problem decoding encrypted bodypart");
  416.                     err_fatal = TRUE;
  417.                     *perr = strdup(buf);
  418.                 }
  419.                 break;
  420.  
  421.                 case BPT_SFD:
  422.                 temp->BodyPart->offset = type_IOB_BodyPart_sfd;
  423.                 temp->BodyPart->un.sfd = 
  424.                     (struct type_IOB_SFDBodyPart *)
  425.                         read_bpfile(curfile,
  426.                                 &_ZIOB_mod,
  427.                                 _ZSFDBodyPartIOB);
  428.                 if (flatresult != OK) {
  429.                     PP_LOG(LLOG_EXCEPTIONS,
  430.                            ("Unable to read in SFD bodypart '%s'", 
  431.                         curfile));
  432.                     (void) sprintf (buf,
  433.                             "problem decoding SFD bodypart");
  434.                     err_fatal = TRUE;
  435.                     *perr = strdup(buf);
  436.                 }
  437.                 break;
  438.  
  439.                 case BPT_TIF1:
  440.                 temp->BodyPart->offset = type_IOB_BodyPart_mixed__mode;
  441.                 temp->BodyPart->un.mixed__mode = 
  442.                     (struct type_IOB_MixedModeBodyPart *)
  443.                         read_bpfile(curfile,
  444.                                 &_ZIOB_mod,
  445.                                 _ZMixedModeBodyPartIOB);
  446.                 if (flatresult != OK) {
  447.                     PP_LOG(LLOG_EXCEPTIONS,
  448.                            ("Unable to read in mixed mode bodypart '%s'", 
  449.                         curfile));
  450.                     (void) sprintf (buf,
  451.                             "problem decoding mixed mode bodypart");
  452.                     err_fatal = TRUE;
  453.                     *perr = strdup(buf);
  454.                 }
  455.                 break;
  456.                 
  457.                 case BPT_ODIF:
  458.                 temp->BodyPart->offset = type_IOB_BodyPart_odif;
  459.                 fillin_odif(&(temp->BodyPart->un.odif));
  460.                 break;
  461.  
  462.                 case BPT_ISO6937TEXT:
  463.                 temp->BodyPart->offset = type_IOB_BodyPart_iso6937Text;
  464.                 temp->BodyPart->un.iso6937Text = 
  465.                     (struct type_IOB_ISO6937TextBodyPart *)
  466.                         read_bpfile(curfile,
  467.                                 &_ZIOB_mod,
  468.                                 _ZISO6937TextBodyPartIOB);
  469.                 if (flatresult != OK) {
  470.                     PP_LOG(LLOG_EXCEPTIONS,
  471.                            ("Unable to read in ISO 6937 bodypart '%s'", 
  472.                         curfile));
  473.                     (void) sprintf (buf,
  474.                             "problem decoding ISO 6937 bodypart");
  475.                     err_fatal = TRUE;
  476.                     *perr = strdup(buf);
  477.                 }
  478.                 break;
  479.                 
  480.                 case BPT_BILATERAL:
  481.                 if (x40084 == TRUE) {
  482.                     PP_LOG(LLOG_EXCEPTIONS,
  483.                            ("Unknown p2 84 body part '%s'", 
  484.                         curfile));
  485.                     (void) sprintf (buf,
  486.                             "Unknown p2 84 body part '%s'", 
  487.                             curfile);
  488.                     *perr = strdup (buf);
  489.                     err_fatal = TRUE;
  490.                     flatresult = NOTOK;
  491.                 } else {
  492.                     temp->BodyPart->offset = type_IOB_BodyPart_bilaterally__defined;
  493.                     temp->BodyPart->un.bilaterally__defined = 
  494.                         (struct type_IOB_BilaterallyDefinedBodyPart *)
  495.                             read_bpfile(curfile,
  496.                                     &_ZIOB_mod,
  497.                                     _ZBilaterallyDefinedBodyPartIOB);
  498.                     if (flatresult != OK) {
  499.                         PP_LOG(LLOG_EXCEPTIONS,
  500.                                ("Unable to read in bilaterally defined bodypart '%s'", 
  501.                             curfile));
  502.                         (void) sprintf (buf,
  503.                                 "problem decoding bilaterally defined bodypart");
  504.                         err_fatal = TRUE;
  505.                         *perr = strdup(buf);
  506.                     }
  507.                 }
  508.                 break;
  509.  
  510.                 case BPT_EXTERNAL:
  511.                 if (x40084 == TRUE) {
  512.                     PP_LOG(LLOG_EXCEPTIONS,
  513.                            ("Unknown p2 84 body part '%s'", 
  514.                         curfile));
  515.                     (void) sprintf (buf,
  516.                             "Unknown p2 84 body part '%s'", 
  517.                             curfile);
  518.                     *perr = strdup (buf);
  519.                     err_fatal = TRUE;
  520.                     flatresult = NOTOK;
  521.                 } else {
  522.                     temp->BodyPart->offset = type_IOB_BodyPart_externally__defined;
  523.                     temp->BodyPart->un.externally__defined = 
  524.                         (struct type_IOB_ExternallyDefinedBodyPart *)
  525.                             read_bpfile(curfile,
  526.                                     &_ZIOB_mod,
  527.                                     _ZExternallyDefinedBodyPartIOB);
  528.                     if (flatresult != OK) {
  529.                         PP_LOG(LLOG_EXCEPTIONS,
  530.                                ("Unable to read in externally defined bodypart '%s'", 
  531.                             curfile));
  532.                         (void) sprintf (buf,
  533.                                 "problem decoding externally defined bodypart");
  534.                         err_fatal = TRUE;
  535.                         *perr = strdup(buf);
  536.                     }
  537.  
  538.                 }
  539.                 break;
  540.                 
  541.                     
  542.                 default:
  543.                 PP_LOG(LLOG_EXCEPTIONS,
  544.                        ("Unknown p2 body part '%s'", curfile));
  545.                 (void) sprintf (buf,
  546.                         "Unknown p2 body part '%s'", 
  547.                         curfile);
  548.                 *perr = strdup (buf);
  549.                 flatresult = NOTOK;
  550.                 err_fatal = TRUE;
  551.                 break;
  552.             }
  553.             if (flatresult == OK
  554.                 && getname(curfile) != OK)
  555.                 more = FALSE;
  556.             
  557.         }
  558.         
  559.         if (flatresult == OK) {
  560.             if (list == NULL)
  561.                 list = end = temp;
  562.             else {
  563.                 end -> next = temp;
  564.                 end = temp;
  565.             }
  566.         }
  567.     }
  568.     *pbody = list;
  569. }
  570.     
  571. /* ps_done: like pe_done */
  572. int             ps_done (ps, msg)
  573. PS              ps;
  574. char           *msg;
  575. {
  576.     if (ps->ps_errno)
  577.     {
  578.         ps_advise (ps, msg);
  579.         ps_free (ps);
  580.         return 1;
  581.     }
  582.     else
  583.     {
  584.         ps_free (ps);
  585.         return 0;
  586.     }
  587. }
  588.  
  589. getname(str)
  590. char    *str;
  591. {
  592.     /* msg_rfile returns full pathname need to split of curdir */
  593.     if (msg_rfile(str) != RP_OK) {
  594.         str = NULL;
  595.         return NOTOK;
  596.     }
  597.     return OK;
  598. }
  599.  
  600. /* initialise name for above */
  601. setname (name)
  602. char    *name;
  603. {
  604.     (void) strcpy (curdir, name);
  605. /*      printf ("setname -> %s\n", curdir);*/
  606. }
  607. /*
  608.  * write_bp: write a body part contained in pe out to the file named
  609.  * name. name is converted into the correct place in the directory
  610.  * tree.
  611.  */
  612.  
  613. write_bp(name, pe, perr)
  614. char    *name;
  615. PE      pe;
  616. char    **perr;
  617. {
  618.     PS      psout;
  619.     FILE    *fp;
  620.     static char fullname[MAXPATHLENGTH];
  621.     char    buf[BUFSIZ];
  622.     sprintf(fullname,"%s/%s", curdir, name);
  623.     PP_LOG(LLOG_TRACE,
  624.         ("Chans/p2flatten : Writing %s", fullname));
  625.  
  626.     if((fp = fopen(fullname, "w")) == NULL)
  627.     {
  628.         PP_SLOG(LLOG_EXCEPTIONS, fullname,
  629.             ("Can't open file"));
  630.         (void) sprintf (buf,
  631.                 "Unable to open output file '%s'",
  632.                 fullname);
  633.         *perr = strdup(buf);
  634.         flatresult = NOTOK;
  635.         return NOTOK;
  636.     }
  637.  
  638.     if ((psout = ps_alloc(std_open)) == NULLPS)
  639.     {
  640.         ps_advise (psout, "ps_alloc");
  641.         (void) fclose (fp);
  642.         flatresult = NOTOK;
  643.         return NOTOK;
  644.     }
  645.     if (std_setup (psout, fp) == NOTOK)
  646.     {
  647.         advise (NULLCP, "std_setup loses", fullname);
  648.         (void) fclose (fp);
  649.         flatresult = NOTOK;
  650.         return NOTOK;
  651.     }
  652.  
  653.     if(pe2ps(psout, pe) == NOTOK)
  654.     {
  655.         ps_advise(psout, "pe2ps loses");
  656.         flatresult = NOTOK;
  657.         return NOTOK;
  658.     }
  659.     (void) fclose (fp);
  660.     ps_free(psout);
  661. /*      printf (" done\n");*/
  662.     return OK;
  663. }
  664.  
  665. /*
  666.  * read_bpfile: read in a body part and stuff in pe and decode.
  667.  */
  668.  
  669. static caddr_t read_bpfile(name, pmod, idx)
  670. char    *name;
  671. modtyp    *pmod;
  672. int    idx;
  673. {
  674.     PS      psout;
  675.     PE      pe;
  676.     FILE    *fp;
  677.     caddr_t    ret;
  678.  
  679.     PP_LOG(LLOG_TRACE,
  680.         ("Chans/p2flatten : Reading %s", name));
  681.  
  682.     if((fp = fopen(name, "r")) == NULL)
  683.     {
  684.         PP_SLOG(LLOG_EXCEPTIONS, name,
  685.             ("Can't open file"));
  686.         flatresult = NOTOK;
  687.         return NULL;
  688.     }
  689.  
  690.     if ((psout = ps_alloc(std_open)) == NULLPS)
  691.     {
  692.         ps_advise (psout, "ps_alloc");
  693.         (void) fclose (fp);
  694.         flatresult = NOTOK;             
  695.         return NULL;
  696.     }
  697.     if (std_setup (psout, fp) == NOTOK)
  698.     {
  699.         advise (NULLCP, "std_setup loses", name);
  700.         (void) fclose (fp);
  701.         flatresult = NOTOK;
  702.         return NULL;
  703.     }
  704.  
  705.     if((pe = ps2pe(psout)) == NULLPE)
  706.     {
  707.         ps_advise(psout, "ps2pe loses");
  708.         flatresult = NOTOK;             
  709.         return NULL;
  710.     }
  711.  
  712.     if (dec_f(idx, pmod, pe, 0, NULL, NULLCP, &ret) == NOTOK) {
  713.         PP_LOG (LLOG_EXCEPTIONS,
  714.             ("Can't decode - %s",
  715.              PY_pepy));
  716.         flatresult = NOTOK;
  717.         return NULL;
  718.     }
  719.     
  720.     pe_free(pe);
  721.     (void) fclose (fp);
  722.     ps_free(psout);
  723.     return ret;
  724. }
  725.  
  726. bp_type (str)
  727. char    *str;
  728. {
  729.     char *s, *ix;
  730.     int retval;
  731.     if ((s = rindex(str,'/')) == NULLCP)
  732.         s = str;
  733.     else
  734.         s++;
  735.     /* search for those files that aren't num.str */
  736.     if (strcmp(s,rcmd_srch(BPT_HDR_P2,bptbl_body_parts88)) == 0)
  737.         return BPT_HDR_P2;
  738.     if (strcmp(s,rcmd_srch(BPT_HDR_P22,bptbl_body_parts88)) == 0)
  739.         return BPT_HDR_P22;
  740.     if (strcmp(s,rcmd_srch(BPT_HDR_822,bptbl_body_parts88)) == 0)
  741.         return BPT_HDR_822;
  742.     if (strcmp(s,rcmd_srch(BPT_HDR_IPN,bptbl_body_parts88)) == 0)
  743.         return BPT_HDR_IPN;
  744.     if (strcmp(s,rcmd_srch(BPT_P2_DLIV_TXT,bptbl_body_parts88)) == 0)
  745.         return BPT_P2_DLIV_TXT;
  746.  
  747.     /* not hdr so n.xxx where n is number and xxx bpt name */
  748.  
  749.     if ((ix = index(s,'.')) == NULL) {
  750. /*              printf("cannot find '.' char in %s",s);*/
  751.         retval = -1;
  752.     } else {
  753.         ix++;
  754.         retval = (cmd_srch(ix, bptbl_body_parts88));    /* returns # defined number of bp type */
  755.     }
  756.     if (retval == -1) {
  757.         /* bomb out */
  758.         PP_LOG(LLOG_EXCEPTIONS,
  759.             ("Chans/p2flatten : Unknown body type '%s'",str));
  760.         flatresult = NOTOK;
  761.     }
  762.     return retval;
  763. }
  764.  
  765. depth (file)
  766. char    *file;
  767. {
  768.     char    *p;
  769.     int     count = 0;
  770.  
  771.     for (p = file; *p; p++)
  772.         if (*p == '/') {
  773.             count ++;
  774.             while(*p == '/') p++;
  775.         }
  776.     return count;
  777. }
  778.  
  779. char    *readoctet (file, len)
  780. char    *file;
  781. int     *len;
  782. {
  783.     char    *p;
  784.     int     fd;
  785.     struct stat st;
  786.  
  787.     PP_LOG(LLOG_TRACE,
  788.         ("Chans/p2flatten reading octet %s",file));
  789.  
  790.     if ((fd = open(file ,0)) == NOTOK)
  791.     {
  792.         PP_SLOG(LLOG_EXCEPTIONS, file,
  793.             ("Can't open file"));
  794.         flatresult = NOTOK;
  795.         return NULLCP;
  796.     }
  797.     if (fstat (fd, &st) == NOTOK)
  798.     {
  799.         PP_SLOG(LLOG_EXCEPTIONS, file,
  800.             ("Can't stat file"));
  801.         flatresult = NOTOK;
  802.         return NULLCP;
  803.     }
  804.     *len = st.st_size;
  805.     p = smalloc (*len + 1);
  806.     if (read (fd, p, *len) != *len) {
  807.         advise ("read", "Read failed");
  808.         flatresult = NOTOK;
  809.     }
  810.     close (fd);
  811. /*      printf ("done\n"); */
  812.     return p;
  813. }
  814.  
  815. #define BUF_INC    128
  816.  
  817. char    *readCRLFstring (file, plen)
  818. char    *file;
  819. int    *plen;
  820. {
  821.     char    *p;
  822.     FILE     *fd;
  823.     unsigned int    c,
  824.         len,
  825.         count;
  826.  
  827.     PP_LOG(LLOG_TRACE,
  828.         ("Chans/p2flatten reading CRLF string %s",file));
  829.  
  830.     if ((fd = fopen(file ,"r")) == (FILE *) 0)
  831.     {
  832.         PP_SLOG(LLOG_EXCEPTIONS, file,
  833.             ("Can't open file"));
  834.         flatresult = NOTOK;
  835.         return NULLCP;
  836.     }
  837.     len = BUF_INC;
  838.     p = smalloc (len);
  839.     count = 0;
  840.     while ((c = getc(fd)) != EOF) {
  841.         if (c == (int) ('\n')) {
  842.             if (count >= len) {
  843.                 len += BUF_INC;
  844.                 p = realloc(p, len);
  845.             }
  846.             p[count++] = '\r';
  847.         }
  848.         if (count >= len) {
  849.             len += BUF_INC;
  850.             p = realloc(p, len);
  851.         }
  852.         p[count++] = (char) c;
  853.     }
  854.     if (count >= len) {
  855.         len += 1;
  856.         p = realloc(p, len);
  857.     }
  858.     *plen = count;    
  859.     fclose (fd);
  860. /*      printf ("done\n"); */
  861.     return p;
  862. }
  863.  
  864. fillin_ia5(pia5text)
  865. struct type_IOB_IA5TextBodyPart    **pia5text;
  866. {
  867.     int    len = 0;
  868.     char    *str;
  869.  
  870.     *pia5text = (struct type_IOB_IA5TextBodyPart *)
  871.         calloc(1, sizeof(struct type_IOB_IA5TextBodyPart));
  872.     (*pia5text)->parameters = (struct type_IOB_IA5TextParameters *)
  873.         calloc(1, sizeof(struct type_IOB_IA5TextParameters));
  874.     (*pia5text)->parameters->parm = int_IOB_Repertoire_ia5;
  875.     str = readCRLFstring(curfile, &len);
  876.     (*pia5text)->data = str2qb(str, len, 1);
  877.  
  878.     free(str);
  879. }
  880.     
  881. fillin_odif(podif)
  882. struct type_IOB_ODIFBodyPart    **podif;
  883. {
  884.     int    len = 0;
  885.     char    *str;
  886.     
  887.     str = readoctet(curfile, &len);
  888.     if (str != NULL) {
  889.         *podif = str2qb(str, len, 1);
  890.         free(str);
  891.     }
  892. }
  893.     
  894. fillin_forwarded(pbp, x40084, perr)
  895. struct type_IOB_BodyPart    **pbp;
  896. int    x40084;
  897. char    **perr;
  898. {
  899.     int    oldepth = curdepth;
  900.     char    olddir[MAXPATHLENGTH];
  901.     char    *ix = rindex(curfile, '/');
  902.     (void) sprintf(olddir, curdir);
  903.  
  904.     *pbp = (struct type_IOB_BodyPart *)
  905.         calloc (1, sizeof(struct type_IOB_BodyPart));
  906.     (*pbp)->offset = type_IOB_BodyPart_message;
  907.     (*pbp)->un.message = (struct type_IOB_MessageBodyPart *)
  908.         calloc(1, sizeof(struct type_IOB_MessageBodyPart));
  909.     
  910.     curdepth = depth(curfile);
  911.     if (ix != NULLCP)
  912.         *ix = '\0';
  913.     (void) sprintf(curdir, curfile);
  914.     if (ix != NULLCP)
  915.         *ix = '/';
  916.     
  917.     PY_pepy[0] = 0;
  918.     if (bp_type(curfile) == BPT_P2_DLIV_TXT) {
  919.         if (((*pbp)->un.message->parameters =
  920.              (struct type_IOB_MessageParameters *)
  921.              read_bpfile(curfile,
  922.                  &_ZIOB_mod,
  923.                  _ZMessageParametersIOB)) == NULL) 
  924.             return;
  925.         if (getname(curfile) != OK)
  926.             more = FALSE;
  927.     } else 
  928.         (*pbp)->un.message->parameters = (struct type_IOB_MessageParameters *) calloc (1, sizeof(struct type_IOB_MessageParameters));
  929.  
  930.     fillin_ipm(&((*pbp)->un.message->data), x40084, perr);
  931.  
  932.     curdepth = oldepth;
  933.     (void) sprintf(curdir, olddir);
  934. }
  935.         
  936.             
  937. /*     ERRORS */
  938.  
  939. #ifndef lint
  940. void    adios (va_alist)
  941. va_dcl
  942. {
  943.     va_list ap;
  944.  
  945.     va_start (ap);
  946.  
  947.     _ll_log (pp_log_norm, LLOG_FATAL, ap);
  948.  
  949.     va_end (ap);
  950.  
  951.     _exit (1);
  952. }
  953. #else
  954. /* VARARGS2 */
  955.  
  956. void    adios (what, fmt)
  957. char   *what,
  958.        *fmt;
  959. {
  960.     adios (what, fmt);
  961. }
  962. #endif
  963.  
  964.  
  965. #ifndef lint
  966. void    advise (va_alist)
  967. va_dcl
  968. {
  969.     int     code;
  970.     va_list ap;
  971.  
  972.     va_start (ap);
  973.  
  974.     code = va_arg (ap, int);
  975.  
  976.     _ll_log (pp_log_norm, code, ap);
  977.  
  978.     va_end (ap);
  979. }
  980. #else
  981. /* VARARGS3 */
  982.  
  983. void    advise (code, what, fmt)
  984. char   *what,
  985.        *fmt;
  986. int     code;
  987. {
  988.     advise (code, what, fmt);
  989. }
  990. #endif
  991.