⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 faxsr.c

📁 传真示例
💻 C
📖 第 1 页 / 共 2 页
字号:
   if (localidp==NULL) {
      sprintf(buff,"FAXSR CH: %02d",channel);
      localidp=&buff[0];
   }
   
   if (fx_setparm(faxhandle,FC_LOCALID,localidp)!=0) {
      disperror(faxhandle,"Setting local id");
   }
   
   if (fx_getparm(faxhandle,FC_LOCALID,buff)!=0) {
      disperror(faxhandle,"Reading local id");
   } else {
      printf("Local ID set to \"%s\"\n",buff);
   }
   
   /* wait for rings */
   printf("Waiting for rings...\n");
   
   if (dx_wtring(voxhandle,1,DX_OFFHOOK, -1) == -1) {
      disperror(voxhandle,"Wait Ring failed");
      dx_sethook(voxhandle,DX_ONHOOK, EV_SYNC);  
      fx_close(faxhandle);
      dx_close(voxhandle);
      return(-1);
   }
   
   /*
    * Receive fax loop.  If we are storing the fax in Raw format,
    * each page requires a separate call to fx_rcvfax().  Terminate
    * the loop on EOP or error.
    */
   
   while (1) {
      /* receive the fax */
      printf("\nReceiving \"%s\"...\n",rxfilep);
      if ((rc=fx_rcvfax(faxhandle,rxfilep,flags))==-1) {
	 disperror(faxhandle,"Receive failed");
	 printf("Phase E Status: %ld\n",ATFX_ESTAT(faxhandle));
      }
      
      dispstatus(faxhandle,"     -- Receive fax complete --");
      
      /* break from while loop if fx_rcvfax returned an error */
      if (rc!=0)
	 break;  /* break on error */
      
      if (flags & DF_RAW) {
	 /* Raw receive, check if we need to continue */
	 phdcmd=ATFX_PHDCMD(faxhandle);
	 phdrpy=ATFX_PHDCMD(faxhandle);
	 
	 /*
	  * If we received EOP from transmitter, check to make sure
	  * we didn't respond with an RTN or PIN.  If we did, the
	  * transmitter may retransmit the page, so we need to issue
	  * another fx_rcvfax() command.
	  */
	 
	 if (phdcmd==DFS_EOP && phdrpy!=DFS_RTN && phdrpy!=DFS_PIN) {
	    break;  /* completed, break from while loop */
	 }
	 
	 /* change file extension to "02.raw","03.raw","04.raw", etc. */
         strcpy(buff, rxfile);
	 if ((strp=strrchr(buff,'.'))!=NULL) {              
	    sprintf(strp,"%02d.raw",++rxcount);
            rxfilep = buff;
         }
      }
      else {
	 /* TIFF/F receive, we are done */
	 break; /* completed, break from while loop */
      }
   }   /* end of while */
   
   
   /* place channel on hook */
   if (dx_sethook(voxhandle,DX_ONHOOK,EV_SYNC) == -1) {
      disperror(voxhandle,"ONHOOK failed");
   }
   
   fx_close(faxhandle);
   dx_close(voxhandle);
   return rc;              /* return error code from rcvfax call */
}

/****************************************************************
 *        NAME : chkargs(argc,argv)
 * DESCRIPTION : check command line arguments, display help if error
 *       INPUT : argc = argument count.
 *             : *argv[] = array of pointers to command line arguments.
 *      OUTPUT : none.
 *     RETURNS : none.
 *    CAUTIONS : issues exit() if error or invalid parameter
 ****************************************************************/
void chkargs(argc, argv)
int argc;
char * argv[];
{
   int     c;
   int error=0;
   char    *strp;
   
   while ((c=getopt(argc,argv,"c:d:hl:r:s:u:?")) !=-1) {
      switch (c) {
      case 'h':
      case '?':
	 usage();
	 break;
      case 'c':
	 channel=atoi(optarg);
         if (channel < 1) {
           printf("Channel number must be 1 or greater\n");
           exit(1);
         }
	 break;
      case 'd':
	 dialstringp=optarg;     /* save pointer to dial string */
	 break;
      case 'l':
	 localidp=optarg;            /* save pointer to local ID */
	 break;
	 
      case 'r':
	 rxfilep=optarg;
	 if (strrchr(optarg,'.')==NULL) {
	    printf("Error: File must have \"tif\" or \"raw\" extension: %s\n",rxfilep);
	    error++;
	 }
	 break;
	 
      case 's':
	 if ((strp = strtok(optarg, " \n\r")) == NULL) {
	    printf("Error: no files were specified with -s option %s\n",optarg);
	    error++;
	    break;
	 }
	 
	 do {
	    
	    txfiles[txcount++]=strp;    /* save pointer to file name */
	    
	 } while (((strp=strtok(NULL," \r\n\t"))!=NULL) && (txcount<MAXIOTT));
	 
	 if (strp!=NULL) {                   /* too many files were specified */
	    printf("Error: A maximum of %d files can be sent\n",MAXIOTT);
	    error++;
	 }
	 break;
	 
      case 'u':
	 userheaderp=optarg;             /* save pointer to User Header */
	 
      } /* end of switch */
   }   /* end of while */
   
   /* Check to make sure we have valid connamd line arguments */
   if (dialstringp==NULL && txcount==0 && rxfilep==NULL) {
      printf("Error: Send or Receive options must be specified\n");
      error++;                                        /* send or rcv not specified */
   }
   
   if ((dialstringp==NULL && txcount!=0) || (dialstringp!=NULL && txcount==0)) {
      printf("Error: Both dial string and send file list must be specified\n");
      error++;
   }
   
   if (rxfilep!=NULL && (dialstringp!=NULL || txcount!=0)) {
      printf("Error: Can't send and receive at the same time\n");
      error++;
   }
   
   if (rxfilep!=NULL && (dialstringp!=NULL || txcount!=0)) {
      printf("Error: Can't send and receive at the same time\n");
      error++;
   }
   
   if (error) {    /* display command line help if an error occurred */
      usage();
   }
}

/***************************************************************************
 *        NAME: int phd_hndlr()
 * DESCRIPTION: display Phase D information
 *      INPUTS: dev handle of device on whech Phase D occurred
 *     OUTPUTS: nothing
 *     RETURNS: nothing
 *    CAUTIONS: Called from within the Standard Runtime library.
 *              Perform minimal processing within the handler to
 *              return control to the fax library.
 *************************************************************************/
long phd_hndlr(tmp)
void * tmp;
{
   int devhandle = sr_getevtdev(0);
   /* call our status display routine */
   dispstatus(devhandle,"          -- Phase D --");    
   return(0);
}  


/***************************************************************************
 *        NAME: int phb_hndlr()
 * DESCRIPTION: display Phase B information
 *      INPUTS: none
 *     OUTPUTS: nothing
 *     RETURNS: nothing
 *    CAUTIONS: Called from within the Standard Runtime library.
 *              Perform minimal processing within the handler to
 *              return control to the fax library.
 *************************************************************************/
long phb_hndlr(tmp)
void * tmp;
{
   static char remoteid[21] = {' '};
   int devhandle = sr_getevtdev(0);
   
   printf("\n          -- Phase B --\n");
   /* Get phase B status and display connect message */
   if (ATFX_BSTAT(devhandle) & DFS_REMOTEID) {
      /* Remote machine's ID available */
      
      if (fx_getparm(devhandle, FC_REMOTEID, (void*)remoteid) == -1) {
	 disperror(devhandle,"Reading remote ID\n");
      }
      printf("Connected to \"%s\" at %d baud\n", 
         remoteid, ATFX_SPEED(devhandle));
   } else {
      printf("Connected to remote (no id string) at %d baud", 
         ATFX_SPEED(devhandle));
   }
   return(0);

}
/***************************************************************************
 *        NAME: call()
 * DESCRIPTION: go offhook, dial and check for connection
 *      INPUTS: dev handle of open device, phonenoumber or null
 *     OUTPUTS: nothing
 *     RETURNS: call analysis, -1 check dl_errno
 *    CAUTIONS: 
 *************************************************************************/
int call(devhandle,phno)
int devhandle;
char *phno;
{
   
   /* go off hook */
   if (dx_sethook(devhandle,DX_OFFHOOK, EV_SYNC) == -1) {
      disperror(devhandle, "OFFHOOK failed");
      return(-1);
   }
 
   /* if directly connected, return connected */
   if (phno==NULL || *phno=='\0')
      return CR_CNCT;
   
   /* dial only if phone number specified returning call analysis */
   return(dx_dial(devhandle,phno,NULL,DX_CALLP|EV_SYNC));
   
}

/***************************************************************************
 *        NAME: dispstatus()
 * DESCRIPTION: display page count,Phase D info, etc.
 *      INPUTS: dev handle, optional description string 
 *     OUTPUTS: nothing
 *     RETURNS: none
 *    CAUTIONS: none
 *************************************************************************/
void dispstatus(devhandle,strp)
int devhandle;
char *strp;
{
   long badscanlines;
   char    buff[22];
   
   /* print optional header if specified */
   if (strp!=NULL)     
      printf("\n%s\n",strp);
   
   /* get remote ID and print it */
   if (fx_getparm(devhandle,FC_REMOTEID,buff)!=0) {
      disperror(devhandle,"Reading remote ID");
   }
   else
      printf("Remote ID: \"%s\"\n",buff);
   
   /* print information */
   printf("Page: %ld, Scan lines: %ld\n",
	  ATFX_PGXFER(devhandle),ATFX_SCANLINES(devhandle));
   printf("Phase D cmd: %s, reply: %s\n",
	  Dcmd(devhandle),Drpy(devhandle));
   printf("Width: %ld, Resln: %ld, Byte Count: %ld\n",
	  ATFX_WIDTH(devhandle),ATFX_RESLN(devhandle),ATDX_TRCOUNT(devhandle));
   printf("RTN Pages: %ld",ATFX_RTNPAGES(devhandle));
   
   /* only print bad scanline count if supported (not supported on fax/120) */
   if ((badscanlines=ATFX_BADSCANLINES(devhandle))!=AT_FAILURE)
      printf(", Bad Scan lines: %ld\n",badscanlines); 
   else
      printf("\n");
}

/***************************************************************************
 *        NAME: Dcmd()
 * DESCRIPTION: convert ATFX_PHDCMD to string or number if unknown
 *      INPUTS: dev handle
 *     OUTPUTS: nothing
 *     RETURNS: pointer to string
 *    CAUTIONS: none
 *************************************************************************/
char    *Dcmd(devhandle)
int devhandle;
{
   
   switch (ATFX_PHDCMD(devhandle)) {
   case DFS_EOP:
      return("EOP");
   case DFS_MPS:
      return("MPS");
   case DFS_EOM:
      return("EOM");
   case DFS_POLL:
      return("POLL");
   case DFS_PRI_EOP:
      return("PRI-EOP");
   case DFS_PRI_MPS:
      return("PRI-MPS");
   case DFS_PRI_EOM:
      return("PRI-EOM");
   case -1:
   default:
      return("none");
   }
}

/***************************************************************************
 *        NAME: Drpy()
 * DESCRIPTION: convert ATFX_PHDRPY to string or number if unknown
 *      INPUTS: dev handle
 *     OUTPUTS: nothing
 *     RETURNS: pointer to string
 *    CAUTIONS: none
 *************************************************************************/
char    *Drpy(devhandle)
int devhandle;
{
   
   switch (ATFX_PHDRPY(devhandle)) {
   case DFS_MCF:
      return("MCF");
   case DFS_RTN:
      return("RTN");
   case DFS_RTP:
      return("RTP");
   case DFS_PIP:
      return("PIP");
   case DFS_PIN:
      return("PIN");
   case -1:
   default:
      return("none");
   }
}

/***************************************************************************
 *        NAME: disperror()
 * DESCRIPTION: display dl_errno error string
 *      INPUTS: dev handle, optional description string 
 *     OUTPUTS: nothing
 *     RETURNS: none
 *    CAUTIONS: none
 *************************************************************************/
void disperror(devhandle,strp)
int devhandle;
char *strp;
{
   int error;
   
   printf("ERROR: %s\n",strp);
   
   if ((error = ATDV_LASTERR(devhandle)) == EDX_SYSTEM) {
      printf("%s. Errno: %d\n",ATDV_ERRMSGP(devhandle), errno);
   } else {
      printf("%s. Error code: 0x%x\n",ATDV_ERRMSGP(devhandle), error);
   }
}

/****************************************************************
 *        NAME : getopt(argc,argv,options)
 * DESCRIPTION : parses command line option using options string
 *       INPUT : argc = argument count.
 *             : *argv[] = array of pointers to command line arguments.
 *             : options char array of acceptable options
 *      OUTPUT : none.
 *     RETURNS : none.
 *    CAUTIONS : returns -1 if option is not found
 ****************************************************************/
int getopt(argc, argv, opt)
int argc;
char *argv[];
char *opt;
{
   static int indx = 1;
   char  *ptr;
   int intchar;
   
   if(argc <= indx)            /* have we gone thru all arguments */
      return(-1);
   
   ptr = argv[indx];           /* set pointer to current argument */
   
   if(*ptr != '-')         /* does it start with the switch char? */
      return(-1);
   
   ptr++;                      /* yes. get char and check options string */
   intchar = (int)*ptr;
   
   if( strchr(opt, intchar) == NULL)
      return(-1);
   
   if (*(ptr+1)!='\0')     /* setup optarg to point to optional data */
      optarg=&argv[indx][2];
   else
      if (argv[indx+1] && *argv[indx+1] != '-' && argc > indx+1) {
	 optarg = argv[indx+1];
	 indx++;
      }
      else
	 optarg = NULL;
   
   indx++;
   return(*ptr);
   
}

/****************************************************************
 *        NAME : usage()
 * DESCRIPTION : display command line arguments
 *       INPUT : nothing
 *      OUTPUT : none.
 *     RETURNS : never returns
 *    CAUTIONS : issues exit() 
 ****************************************************************/
void usage()
{
   printf("Usage:\n");
   printf("To send faxes:\n");
   printf("faxsr -d\"number\" -s\"sndfile1.ext [f2.ext f3.ext f4.ext]\" [-clu]\n");
   printf("To receive faxes:\n");
   printf("faxsr -rrcvfile.ext [-cl]\n\n");
   printf("-c\tChannel number, 1=first channel in system (e.g. dxxxB1C1)\n");
   printf("\t\tdefault = %d\n",channel);
   printf("-d\tDial string, enclosed in double quotes\n");
   printf("-l\tSpecify Local ID string, default = FAXSR CH: nn\n");
   printf("-?\tDisplay this message\n");
   printf("-r\tReceive file name.  File extension determines how fax is stored\n");
   printf("\t\".tif\" for TIFF/F format\n\t\".raw\" for Raw format.\n");
   printf("\tIf Raw format and multipage fax is received subsequent pages are\n");
   printf("\tstored in rcvfile02.raw, rcvfile03.raw, etc.\n");
   printf("-s\tSend file list.  Up to %d files enclosed in quotes\n",MAXIOTT);
   printf("\tFile extension determines send data type.  \".raw\" for Raw files,\n");
   printf("\t\".tif\" for TIFF/F files, any other extension is assumed to be ASCII file\n");
   printf("-u\tSpecify User Header field. Default is NULL string\n");
   exit(1);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -