home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: OtherApp / OtherApp.zip / PSFAX2.ZIP / psfax2 / psfax2.c < prev    next >
C/C++ Source or Header  |  1992-10-16  |  22KB  |  804 lines

  1. /* psfax2.c */
  2. /*---------------------------------------------------------------------------*/
  3. /* This file is part of PSFax/2.                                             */
  4. /*                                                                           */
  5. /* Written By: Gary L. Hennigan                                              */
  6. /*                                                                           */
  7. /* This code can modified as much as you like provided you follow the        */
  8. /* guidelines as described in the file install.doc which you should have     */
  9. /* received with this file. If you did not please notify the author for a    */
  10. /* copy of this file via Internet email. The address is:             */
  11. /*            ghenniga@NMSU.Edu                     */
  12. /*---------------------------------------------------------------------------*/
  13.  
  14. #include <stdio.h>
  15. #include <stdlib.h>
  16. #include <string.h>
  17. #include <ctype.h>
  18.  
  19. #include "psfax2.h"
  20.  
  21. /**Global variable(s). **/
  22. char cGlobResp[MAXRESPLEN];
  23.  
  24. int main(int argc, char **argv)
  25. {
  26. /**Local functions. **/
  27.    void vMemCheck( void * );
  28.    void vCleanExit( char *, char *, int, int );
  29.    int iPageCount( char * );
  30.    ULONG ulOpenPort(char *, HFILE *);
  31.    ULONG ulClosePort( HFILE );
  32.    int iPortGetSet( HFILE, DCBINFO * );
  33.    int iPortInit( HFILE, DCBINFO * );
  34.    int iModemInit( HFILE, DCBINFO * );
  35.    int iModemDial( HFILE, char *, DCBINFO *, int );
  36.    int iFaxInit( HFILE , DCBINFO *, int, int, int, int );
  37.    int iFaxSendPage( HFILE, DCBINFO *, FILE *, int );
  38.    int iFaxInterrupt( HFILE, DCBINFO * );
  39.  
  40. /**Local variables. **/
  41.    FILE *fp;
  42.    int i1, iTemp, iPageCnt, iPageStat;
  43.    char *cTemp, *cRecipPhone, *cSendPhone, *cRecipName, *cSendName;
  44.    char *cPSFileName[MAXPSFILES], *cTempDir, *cFaxDir, *cFaxCnfFile;
  45.    char cBuff[CCHMAXPATH], cBuff2[2*CCHMAXPATH], cCoverName[CCHMAXPATH];
  46.    char cPortID[6];
  47.  
  48.    HFILE hTTY;
  49.    DCBINFO dcbCom;
  50.  
  51. /**Initialized local variables. **/
  52.    BOOL bRecipPhone=FALSE, bCover=FALSE, bSendPhone=FALSE;
  53.    BOOL bRecipName=FALSE, bSendName=FALSE, bCQuote=FALSE;
  54.    int iNumPSFiles=0, iWait=60, iRedial=5;
  55.    
  56. /*-------------------------------End Declarations---------------------------*/
  57.  
  58. /**If no arguments were given print out help information. **/   
  59.    if( argc <= 1 )
  60.    {
  61.       printf("usage: psfax2 -p <rnum> -c -s <sname> -S <snum> -r <rname> ");
  62.       printf("[psfile]\n");
  63.       printf("where:\n(required)\n");
  64.       printf("\trnum - fax phone number of recipient\n");
  65.       printf("(optional cover sheet)\n");
  66.       printf("\t-c - use the predefined cover sheet\n");
  67.       printf("\tsnam  - the sender's name\n");
  68.       printf("\tsnum  - the sender's return fax phone number\n");
  69.       printf("\trname - the recipient's name\n");
  70.       printf("\t[psfile1] - the name of the postscript file ");
  71.       printf("to send\n\n");
  72.       printf("example:\n psfax2 -p1-505-555-1234 -c -s \"jay ");
  73.       printf("doe\" -S1-202-555-4321 -r \"jan doe\" psout.ps\n");
  74.       exit(0);
  75.    }
  76.    
  77. /**Determine the location of the configuration file. **/
  78.    cTemp = getenv("FAXPATH");
  79.    if(!cTemp)
  80.    {
  81.       printf("You must set the environment variable FAXPATH to point to");
  82.       printf(" the directory\nwhich contains the file psfax2.cnf\n");
  83.       exit(-1);
  84.    }
  85.    cFaxDir = (char *)malloc((strlen(cTemp)+2)*sizeof(char));
  86.    vMemCheck(cFaxDir);
  87.    strcpy(cFaxDir,cTemp);
  88.    if(cFaxDir[strlen(cTemp)] != '\\')
  89.    {
  90.       cFaxDir[strlen(cTemp)] = '\\';
  91.       cFaxDir[strlen(cTemp)+1] = '\000';
  92.    }
  93.    cFaxCnfFile = (char *)malloc((strlen(cFaxDir)+12)*sizeof(char));
  94.    vMemCheck(cFaxCnfFile);
  95.    strcpy(cFaxCnfFile,cFaxDir);
  96.    strcat(cFaxCnfFile,"psfax2.cnf");
  97.  
  98. /**If it exists, open the configuration file. **/
  99.    fp=fopen(cFaxCnfFile,"r");
  100.    if(!fp)
  101.    {
  102.       printf("The configuration file \"%s\" could not be found!\n",
  103.          cFaxCnfFile);
  104.       exit(-1);
  105.    }
  106.    fscanf(fp, "%s", cPortID);
  107.    strcpy(cCoverName,cFaxDir);
  108.    fscanf(fp, "%s", &cCoverName[strlen(cCoverName)]);
  109.    fscanf(fp, "%d", &iWait);
  110.    fscanf(fp, "%d", &iRedial);
  111.    fclose(fp);
  112.    free(cFaxCnfFile);
  113.    free(cFaxDir);
  114.  
  115. /**Parse the command line. **/
  116.    for( i1=1; i1 < argc; i1++ )
  117.    {
  118.       cTemp = strchr(argv[i1],'-');
  119.       if( cTemp )
  120.       {
  121.      switch(cTemp[1])
  122.      {
  123.  
  124. /**        Recipient's phone number. **/
  125.         case 'p':
  126.            if(cTemp[2])
  127.            {
  128.           cRecipPhone = (char *)malloc((strlen(&cTemp[2])+1)*
  129.                            sizeof(char));
  130.           vMemCheck(cRecipPhone);
  131.           strcpy(cRecipPhone,&cTemp[2]);
  132.            }
  133.            else
  134.            {
  135.           cRecipPhone = (char *)malloc((strlen(argv[i1+1])+1)*
  136.                            sizeof(char));
  137.           vMemCheck(cRecipPhone);
  138.           strcpy(cRecipPhone,argv[i1+1]);
  139.           i1++;
  140.            }
  141.            bRecipPhone=TRUE;
  142.            break;
  143.  
  144. /**        Cover page flag. **/
  145.         case 'c':
  146.            bCover = TRUE;
  147.            break;
  148.  
  149. /**        Sender's phone number. **/
  150.         case 'S':
  151.            if(cTemp[2])
  152.            {
  153.           cSendPhone = (char *)malloc((strlen(&cTemp[2])+1)*
  154.                           sizeof(char));
  155.           vMemCheck(cSendPhone);
  156.           strcpy(cSendPhone,&cTemp[2]);
  157.            }
  158.            else
  159.            {
  160.           cSendPhone = (char *)malloc((strlen(argv[i1+1])+1)*
  161.                           sizeof(char));
  162.           vMemCheck(cSendPhone);
  163.           strcpy(cSendPhone,argv[i1+1]);
  164.           i1++;
  165.            }
  166.            bSendPhone=TRUE;
  167.            break;
  168.  
  169. /**        Recipient's name. **/
  170.         case 'r':
  171.            if(cTemp[2])
  172.            {
  173.      
  174. /**          Multiple word and/or quoted name. **/
  175.           if(cTemp[2] == '\"'|| cTemp[2] == '\'')
  176.           {
  177.              iTemp = strlen(&cTemp[3]);
  178.              if(cTemp[iTemp+2] == '\"' || cTemp[iTemp+2] == '\'')
  179.              {
  180.             cTemp[iTemp+2] = 0x00;
  181.             iTemp--;
  182.             bCQuote = TRUE;
  183.              }
  184.              cRecipName = (char *)malloc((iTemp+1)*
  185.                          sizeof(char));
  186.              vMemCheck(cRecipName);
  187.              strcpy(cRecipName,&cTemp[3]);
  188.              while(bCQuote == FALSE)
  189.              {
  190.             i1++;
  191.             iTemp = strlen(argv[i1]);
  192.             if(argv[i1][iTemp-1] == '\"' ||
  193.                argv[i1][iTemp-1] == '\'')
  194.             {
  195.                argv[i1][iTemp-1] = 0x00;
  196.                bCQuote = TRUE;
  197.                iTemp--;
  198.             }
  199.             cRecipName = (char *)realloc(cRecipName,
  200.                              (strlen(cRecipName)+
  201.                               iTemp+2)*
  202.                              sizeof(char));
  203.             vMemCheck(cRecipName);
  204.             strcat(cRecipName," ");
  205.             strcat(cRecipName,argv[i1]);
  206.              }
  207.           }
  208. /**          Single word name, no quotes. **/
  209.           else
  210.           {
  211.              cRecipName = (char *)malloc((strlen(&cTemp[2])+1)*
  212.                          sizeof(char));
  213.              vMemCheck(cRecipName);
  214.              strcpy(cRecipName,&cTemp[2]);
  215.           }
  216.            }
  217.  
  218. /**           Space between paramater and argument. **/
  219.            else
  220.            {
  221.  
  222. /**          Multiple word name. **/
  223.           if(argv[i1+1][0] == '\"' || argv[i1+1][0] == '\'')
  224.           {
  225.              cTemp = &argv[i1+1][1];
  226.              iTemp = strlen(cTemp);
  227.              if(cTemp[iTemp-1] == '\"' || cTemp[iTemp-1] == '\'')
  228.              {
  229.             cTemp[iTemp-1] = 0x00;
  230.             iTemp--;
  231.             bCQuote = TRUE;
  232.              }
  233.              cRecipName = (char *)malloc((iTemp+1)*
  234.                          sizeof(char));
  235.              vMemCheck(cRecipName);
  236.              strcpy(cRecipName,cTemp);
  237.              while(bCQuote == FALSE)
  238.              {
  239.             i1++;
  240.             iTemp = strlen(argv[i1+1]);
  241.             if(argv[i1+1][iTemp-1] == '\"' ||
  242.                argv[i1+1][iTemp-1] == '\'')
  243.             {
  244.                argv[i1+1][iTemp-1] = 0x00;
  245.                bCQuote = TRUE;
  246.                iTemp--;
  247.             }
  248.             cRecipName = (char *)realloc(cRecipName,
  249.                              (strlen(cRecipName)+
  250.                               iTemp+2)*
  251.                              sizeof(char));
  252.             vMemCheck(cRecipName);
  253.             strcat(cRecipName," ");
  254.             strcat(cRecipName,argv[i1+1]);
  255.              }
  256.              i1++;
  257.           }
  258.  
  259. /**          Single word name. **/
  260.           else
  261.           {
  262.              i1++;
  263.              cRecipName = (char *)malloc((strlen(argv[i1])+1)*
  264.                          sizeof(char));
  265.              vMemCheck(cRecipName);
  266.              strcpy(cRecipName,argv[i1]);
  267.           }
  268.            }
  269.            bCQuote=FALSE;
  270.            bRecipName=TRUE;
  271.            break; /* Recipient's Name */
  272.            
  273. /**        Sender's name. **/
  274.         case 's':
  275.            if(cTemp[2])
  276.            {
  277.      
  278. /**          Multiple word and/or quoted name. **/
  279.           if(cTemp[2] == '\"'|| cTemp[2] == '\'')
  280.           {
  281.              iTemp = strlen(&cTemp[3]);
  282.              if(cTemp[iTemp+2] == '\"' || cTemp[iTemp+2] == '\'')
  283.              {
  284.             cTemp[iTemp+2] = 0x00;
  285.             iTemp--;
  286.             bCQuote = TRUE;
  287.              }
  288.              cSendName = (char *)malloc((iTemp+1)*
  289.                          sizeof(char));
  290.              vMemCheck(cSendName);
  291.              strcpy(cSendName,&cTemp[3]);
  292.              while(bCQuote == FALSE)
  293.              {
  294.             i1++;
  295.             iTemp = strlen(argv[i1]);
  296.             if(argv[i1][iTemp-1] == '\"' ||
  297.                argv[i1][iTemp-1] == '\'')
  298.             {
  299.                argv[i1][iTemp-1] = 0x00;
  300.                bCQuote = TRUE;
  301.                iTemp--;
  302.             }
  303.             cSendName = (char *)realloc(cSendName,
  304.                              (strlen(cSendName)+
  305.                               iTemp+2)*
  306.                              sizeof(char));
  307.             vMemCheck(cSendName);
  308.             strcat(cSendName," ");
  309.             strcat(cSendName,argv[i1]);
  310.              }
  311.           }
  312. /**          Single word name, no quotes. **/
  313.           else
  314.           {
  315.              cSendName = (char *)malloc((strlen(&cTemp[2])+1)*
  316.                          sizeof(char));
  317.              vMemCheck(cSendName);
  318.              strcpy(cSendName,&cTemp[2]);
  319.           }
  320.            }
  321.  
  322. /**           Space between paramater and argument. **/
  323.            else
  324.            {
  325.  
  326. /**          Multiple word name. **/
  327.           if(argv[i1+1][0] == '\"' || argv[i1+1][0] == '\'')
  328.           {
  329.              cTemp = &argv[i1+1][1];
  330.              iTemp = strlen(cTemp);
  331.              if(cTemp[iTemp-1] == '\"' || cTemp[iTemp-1] == '\'')
  332.              {
  333.             cTemp[iTemp-1] = 0x00;
  334.             iTemp--;
  335.             bCQuote = TRUE;
  336.              }
  337.              cSendName = (char *)malloc((iTemp+1)*
  338.                          sizeof(char));
  339.              vMemCheck(cSendName);
  340.              strcpy(cSendName,cTemp);
  341.              while(bCQuote == FALSE)
  342.              {
  343.             i1++;
  344.             iTemp = strlen(argv[i1+1]);
  345.             if(argv[i1+1][iTemp-1] == '\"' ||
  346.                argv[i1+1][iTemp-1] == '\'')
  347.             {
  348.                argv[i1+1][iTemp-1] = 0x00;
  349.                bCQuote = TRUE;
  350.                iTemp--;
  351.             }
  352.             cSendName = (char *)realloc(cSendName,
  353.                              (strlen(cSendName)+
  354.                               iTemp+2)*
  355.                              sizeof(char));
  356.             vMemCheck(cSendName);
  357.             strcat(cSendName," ");
  358.             strcat(cSendName,argv[i1+1]);
  359.              }
  360.              i1++;
  361.           }
  362.  
  363. /**          Single word name. **/
  364.           else
  365.           {
  366.              i1++;
  367.              cSendName = (char *)malloc((strlen(argv[i1])+1)*
  368.                          sizeof(char));
  369.              vMemCheck(cSendName);
  370.              strcpy(cSendName,argv[i1]);
  371.           }
  372.            }
  373.            bCQuote=FALSE;
  374.            bSendName=TRUE;
  375.            break; /* Sender Name */
  376.            
  377.         default:
  378.            printf("Unknown option %s!\n", argv[i1]);
  379.            break;
  380.  
  381.      } /* end switch cTemp[1] */
  382.       } /* endif cTemp */
  383.  
  384. /**   Anything without a "-" is considered a postscript file name. **/
  385.       else
  386.       {
  387.      if(iNumPSFiles < MAXPSFILES)
  388.      {
  389.         cPSFileName[iNumPSFiles] = (char *)malloc((strlen(argv[i1])+1)*
  390.                               sizeof(char));
  391.         strcpy(cPSFileName[iNumPSFiles],argv[i1]);
  392.      }
  393.      else
  394.      {
  395.         printf("Too many files! Maximum is %d\n", MAXPSFILES);
  396.         exit(0);
  397.      }
  398.      iNumPSFiles++;
  399.       }
  400.       
  401.    } /* end i1 for loop, command line parser */
  402.  
  403. /**Check that at least one file was specified. **/
  404.    if( iNumPSFiles <= 0 )
  405.    {
  406.       puts("No files specified!");
  407.       exit(-1);
  408.    }
  409.  
  410. /**Check if the specified files exist. **/
  411.    i1 = 0;
  412.    do
  413.    {
  414.       fp = fopen(cPSFileName[i1++],"r");
  415.       if( !fp )
  416.       {
  417.      printf("The file specified, \"%s\", does not exist!\n",
  418.         cPSFileName[i1-1]);
  419.      exit(-1);
  420.       }
  421.       fclose(fp);
  422.    } while(i1 < iNumPSFiles);
  423.  
  424. /**Locate the TMP directory. **/
  425.    cTemp = getenv("TMP");
  426.    if(cTemp)
  427.    {
  428.       cTempDir = (char *)malloc((strlen(cTemp)+2)*sizeof(char));
  429.       vMemCheck(cTempDir);
  430.       strcpy(cTempDir,cTemp);
  431.       if(cTempDir[strlen(cTempDir)-1] != '\\')
  432.          strcat(cTempDir,"\\");
  433.  
  434. /**   Translate to lower case. For some reason the DosDelete function **/
  435. /**   doesn't like uppercase?                           **/
  436.       for(i1=0; i1 < strlen(cTempDir); i1++)
  437.       {
  438.      if(!islower(cTempDir[i1]))
  439.         cTempDir[i1] = tolower(cTempDir[i1]);
  440.       }
  441.    }
  442.    else
  443.    {
  444.       cTempDir = (char *)malloc(3*sizeof(char));
  445.       vMemCheck(cTempDir);
  446.       strcpy(cTempDir,"\\");
  447.    }
  448.  
  449. /**Clean out any old fax files. **/
  450.    puts("Cleaning out old files...");
  451.    strcpy(cBuff,cTempDir);
  452.    strcat(cBuff,"g3cfax.1");
  453.    DosDelete(cBuff);
  454.    strcpy(cBuff,cTempDir);
  455.    strcat(cBuff,"g3fax.1");
  456.    i1 = 2;
  457.    while(DosDelete(cBuff) == 0)
  458.    {
  459.       sprintf(cBuff,"%sg3fax.%d",cTempDir,i1);
  460.       i1++;
  461.    }
  462.  
  463. /**Convert the PostScript documents to G3 fax files. **/
  464.    puts("Converting documents...");
  465.    strcpy(cBuff,cTempDir);
  466.    strcat(cBuff,"g3fax.%%d");
  467.    sprintf(cBuff2, "gs.exe -dNOPAUSE -sDEVICE=dfaxhigh -q -sOutputFile=%s ",
  468.        cBuff);
  469.    for(i1=0; i1 < iNumPSFiles; i1++ )
  470.    {
  471.       strcat(cBuff2,cPSFileName[i1]);
  472.       fp = popen(cBuff2, "w");
  473.       fprintf(fp, "quit\n");
  474.       pclose(fp);
  475.    }
  476.  
  477. /**Get a page count. **/
  478.    iPageCnt = iPageCount(cTempDir);
  479.  
  480. /**If it was specified, generate a coverpage. **/
  481.    if( bCover == TRUE )
  482.    {
  483.       fp = fopen("mcover.ps","w");
  484.       fprintf(fp, "/recipnum (%s) def\n", cRecipPhone);
  485.       if(bRecipName == TRUE)
  486.          fprintf(fp, "/recipient (%s) def\n", cRecipName);
  487.       if(bSendName == TRUE)
  488.          fprintf(fp, "/sender (%s) def\n", cSendName);
  489.       if(bSendPhone == TRUE)
  490.          fprintf(fp, "/returnfaxnum (%s) def\n", cSendPhone);
  491.  
  492.       fprintf(fp, "/pages (%d) def\n", iPageCnt+1);
  493.       
  494.       fprintf(fp,"(");
  495.       for(i1=0; i1 < strlen(cCoverName); i1++)
  496.       {
  497.      if(cCoverName[i1] == '\\')
  498.      {
  499.         fprintf(fp, "\\");
  500.      }
  501.      fprintf(fp, "%c", cCoverName[i1]);
  502.       }
  503.       fprintf(fp, ")");
  504.       fprintf(fp, " run\n");
  505.       fclose(fp);
  506.  
  507. /**   Create the Group 3 coverpage. **/
  508.       strcpy(cBuff,cTempDir);
  509.       strcat(cBuff,"g3cfax.%%d");
  510.       sprintf(cBuff2,
  511.     "gs.exe -dNOPAUSE -sDEVICE=dfaxhigh -q -sOutputFile=%s mcover.ps",
  512.      cBuff);
  513.       fp = popen(cBuff2,"w");
  514.       fprintf(fp, "quit\n");
  515.       pclose(fp);
  516.       DosDelete("mcover.ps");
  517.    }
  518.    printf("\n"); /* To make room on the screen. */
  519.  
  520. /**Open the port. **/
  521.    puts("Initializing port and modem...");
  522.    while(ulOpenPort(cPortID, &hTTY) != 0)
  523.    {
  524.       printf("Port %s is invalid or being used by another process\n", 
  525.          cPortID);
  526.       printf("Enter 'e' to exit or 'r' to retry: ");
  527.       scanf("%s",cBuff);
  528.       if(cBuff[0] == 'e' || cBuff[0] == 'E')
  529.          vCleanExit(cTempDir,cBuff,iPageCnt,-1);
  530.    }
  531.  
  532. /**Get the current port characteristics. **/
  533.    if(iPortGetSet(hTTY, &dcbCom) != 0)
  534.    {
  535.       puts("Could not get port settings!");
  536.       vCleanExit(cTempDir,cBuff,iPageCnt,-1);
  537.    }
  538.       
  539. /**Initialize the port. **/
  540.    if(iPortInit(hTTY, &dcbCom) != 0)
  541.    {
  542.       puts("Setting port characteristics failed!");
  543.       vCleanExit(cTempDir,cBuff,iPageCnt,-1);
  544.    }
  545.  
  546. /**Initialize the modem. **/
  547.    if(iModemInit(hTTY, &dcbCom) != 0)
  548.    {
  549.       puts("Modem initialization timed out!");
  550.       vCleanExit(cTempDir,cBuff,iPageCnt,-1);
  551.    }
  552.  
  553. /**Dial the modem. **/
  554.    printf("Dialing %s...\n", cRecipPhone);
  555.    do
  556.    {
  557.       iTemp = iModemDial(hTTY, cRecipPhone, &dcbCom, iWait);
  558.       switch(iTemp)
  559.       {
  560.          case DIAL_BUSY:
  561.         puts("Phone busy!");
  562.         break;
  563.      case DIAL_NOANSWER:
  564.         printf("No answer from remote! ");
  565.         printf("Enter 'r' to retry, 'e' to exit: ");
  566.         scanf("%s",cBuff);
  567.         if(cBuff[0] == 'e' || cBuff[0] == 'E')
  568.            vCleanExit(cTempDir,cBuff,iPageCnt,-1);
  569.         break;
  570.      case DIAL_NOCARRIER:
  571.         printf("Could not detect carrier!");
  572.         printf(" Enter 'r' to retry, 'e' to exit: ");
  573.         scanf("%s",cBuff);
  574.         if(cBuff[0] == 'e' || cBuff[0] == 'E')
  575.            vCleanExit(cTempDir,cBuff,iPageCnt,-1);
  576.         break;
  577.      case DIAL_NODIALTONE:
  578.         printf("No dial tone! Enter 'r' to retry, 'e' to exit: ");
  579.         scanf("%s",cBuff);
  580.         if(cBuff[0] == 'e' || cBuff[0] == 'E')
  581.            vCleanExit(cTempDir,cBuff,iPageCnt,-1);
  582.         break;
  583.      case DIAL_UNKNOWN:
  584.         printf("Unexpected or unknown response! ");
  585.         printf("Enter 'r' to retry, 'e' to exit: ");
  586.         scanf("%s",cBuff);
  587.         if(cBuff[0] == 'e' || cBuff[0] == 'E')
  588.            vCleanExit(cTempDir,cBuff,iPageCnt,-1);
  589.         break;
  590.       }
  591.       if( iTemp != 0 )
  592.       {
  593.      printf("Waiting %d seconds to redial...",iRedial);
  594.      sleep(iRedial);
  595.      printf("\n");
  596.       }
  597.    } 
  598.    while(iTemp != 0);
  599.  
  600. /**Initiate fax connection. */
  601.    if(iFaxInit(hTTY, &dcbCom, DF_1DHUFFMAN, VR_FINE, WD_1728,
  602.            LN_UNLIMITED) != 0)
  603.    {
  604.       puts("Could not init fax data transmission for the page!");
  605.       vCleanExit(cTempDir, cBuff, iPageCnt, -1);
  606.    }
  607.  
  608. /**Begin sending the document(s). **/
  609.    iTemp = PEND_ANOTHER;
  610.    if(bCover == TRUE)
  611.    {
  612.       puts("Sending cover page...");
  613.       if(iPageCnt <= 0)
  614.          iTemp = PEND_ENDTRAN;
  615.  
  616.       strcpy(cBuff,cTempDir);
  617.       strcat(cBuff,"g3cfax.1");
  618.       fp = fopen(cBuff,"r");
  619.       if(!fp)
  620.       {
  621.      puts("G3 cover page could not be found...");
  622.      vCleanExit(cTempDir, cBuff, iPageCnt, -1);
  623.       }
  624.       do
  625.       {
  626.      iPageStat = iFaxSendPage(hTTY, &dcbCom, fp, iTemp);
  627.      switch( iPageStat )
  628.      {
  629.             case PPR_PPR:
  630.            printf("There were partial page errors during the cover page");
  631.            printf(" transmission! Continuing...\n");
  632.            iPageStat = 0;
  633.            break;
  634.         case PPR_MCF:
  635.            iPageStat = 0;
  636.            break;
  637.         case PPR_RTN:
  638.            printf("Remote requested retransmission! Retransmitting...\n");
  639.            rewind(fp);
  640.            iPageStat = 1;
  641.            break;
  642.         case PPR_RTP:
  643.            printf("Page good but remote wants a retransmission? ");
  644.            printf("Retransmitting...\n");
  645.            rewind(fp);
  646.            iPageStat = 1;
  647.            break;
  648.         case PPR_PIN:
  649.            printf("Page bad and remote requests an interrupt!\n");
  650.            iPageStat = -1;
  651.            break;
  652.         case PPR_PIP:
  653.            printf("Page good but remote requests an interrupt!\n");
  654.            iFaxInterrupt(hTTY, &dcbCom);
  655.            vCleanExit(cTempDir, cBuff, iPageCnt, -1);
  656.            break;
  657.         default:
  658.            puts("Cancelling transmission!");
  659.            iFaxInterrupt(hTTY, &dcbCom);
  660.            vCleanExit(cTempDir, cBuff, iPageCnt, -1);
  661.            break;
  662.          }
  663.       } while(iPageStat == 1);
  664.  
  665.       fclose(fp);
  666.    }
  667.  
  668. /**Send the pages of the document. **/
  669.    for(i1=0; i1 < iPageCnt; i1++)
  670.    {
  671.  
  672. /**   Send the page. **/
  673.       printf("Sending page %d...\n",i1+1);
  674.       if(i1 == iPageCnt-1)
  675.          iTemp = PEND_ENDTRAN;
  676.  
  677.       sprintf(cBuff, "%sg3fax.%d", cTempDir, i1+1);
  678.       fp = fopen(cBuff,"r");
  679.       if(!fp)
  680.       {
  681.      printf("G3 page %d could not be opened!\n", i1);
  682.      vCleanExit(cTempDir, cBuff, iPageCnt, -1);
  683.       }
  684.       do
  685.       {
  686.      iPageStat = iFaxSendPage(hTTY, &dcbCom, fp, iTemp);
  687.      switch( iPageStat )
  688.      {
  689.             case PPR_PPR:
  690.            printf("There were partial page errors during the page");
  691.            printf(" transmission! Continuing...\n");
  692.            iPageStat = 0;
  693.            break;
  694.         case PPR_MCF:
  695.            iPageStat = 0;
  696.            break;
  697.         case PPR_RTN:
  698.            printf("Remote requested retransmission! Retransmitting...\n");
  699.            rewind(fp);
  700.            iPageStat = 1;
  701.            break;
  702.         case PPR_RTP:
  703.            printf("Page good but remote wants a retransmission? ");
  704.            printf("Retransmitting...\n");
  705.            rewind(fp);
  706.            iPageStat = 1;
  707.            break;
  708.         case PPR_PIN:
  709.            printf("Page bad and remote requests an interrupt!\n");
  710.            iPageStat = -1;
  711.            break;
  712.         case PPR_PIP:
  713.            printf("Page good but remote requests an interrupt!\n");
  714.            iPageStat = -1;
  715.            break;
  716.         default:
  717.            puts("Cancelling transmission!");
  718.            iPageStat = -1;
  719.            break;
  720.          }
  721.       } while(iPageStat == 1);
  722.  
  723.       fclose(fp);
  724.  
  725. /**   If an interrupt was requested, Grant It. **/
  726.       if(iPageStat == -1)
  727.       {
  728.      iFaxInterrupt(hTTY, &dcbCom);
  729.      vCleanExit(cTempDir, cBuff, iPageCnt, -1);
  730.       }
  731.    }
  732.  
  733. /**Close the port, clean up and exit. **/
  734.    ulClosePort(hTTY);
  735.    vCleanExit(cTempDir, cBuff, iPageCnt, 0);
  736.  
  737.    return 0;
  738. }
  739. /****************************************************************************/
  740. /************************Memory check function*******************************/
  741. /****************************************************************************/
  742. void vMemCheck( void *ptr )
  743. {
  744.    if( !ptr )
  745.    {
  746.       puts("Not enough memory!");
  747.       exit(-1);
  748.    }
  749. }
  750. /****************************************************************************/
  751. /*************************Count the number of********************************/
  752. /*****************************pages to fax***********************************/
  753. /****************************************************************************/
  754. int iPageCount(CHAR *cTempDir)
  755. {
  756.    FILE *fp;
  757.    char cBuff[CCHMAXPATH];
  758.    int iCnt=0;
  759. /*-----------------------------End Declarations----------------------------*/
  760.  
  761.    sprintf(cBuff, "%sg3fax.1", cTempDir);
  762.    fp = fopen(cBuff,"r");
  763.    while( fp )
  764.    {
  765.       iCnt++;
  766.       fclose(fp);
  767.       sprintf(cBuff,"%sg3fax.%d", cTempDir, iCnt+1);
  768.       fp = fopen(cBuff,"r");
  769.    }
  770.    
  771.    return iCnt;
  772. }
  773.  
  774. /****************************************************************************/
  775. /**************************Clean up any files********************************/
  776. /****************************and exit PSFax2*********************************/
  777. /****************************************************************************/
  778. void vCleanExit(char *cTempDir, char *cBuff, int iPageCnt, int iError)
  779. {
  780.    int i1;
  781. /*-----------------------------End Declarations----------------------------*/
  782.  
  783. /**Clean out the fax files. **/
  784.    if(iError != 0)
  785.       printf("Cleaning up and exiting on error condition %d\n", iError);
  786.    else
  787.       puts("Cleaning up and exiting...");
  788.  
  789.    strcpy(cBuff,cTempDir);
  790.    strcat(cBuff,"g3cfax.1");
  791.    DosDelete(cBuff);
  792.    strcpy(cBuff,cTempDir);
  793.    strcat(cBuff,"g3fax.1");
  794.    for(i1=1; i1 <= iPageCnt; i1++)
  795.    {
  796.       sprintf(cBuff,"%sg3fax.%d",cTempDir,i1);
  797.       DosDelete(cBuff);
  798.    }
  799.  
  800.    exit(iError);
  801. }
  802.  
  803.  
  804.