📄 lpdclient.c
字号:
/* lpdClient.c V1.01 */
#include "VxWorks.h"
#include "sockLib.h"
#include "inetLib.h"
#include "stdioLib.h"
#include "strLib.h"
#include "hostLib.h"
#include "ioLib.h"
#include "dirent.h"
#include "logLib.h"
#include "taskLib.h"
#include "iosLib.h"
#include "stdio.h" /* fopen, getchar */
#include "fioLib.h" /* printf */ /* fioLib - formatted I/O library */
#include "string.h"
#include "sysLib.h"
#include "usrLib.h" /* cd */
#include "taskLib.h" /* taskDelay */
#include "ifLib.h"
#include "stdlib.h"
#include "sys\stat.h" /* struct stat */
#include "errnoLib.h"
#include "ctype.h"
#include "pubDir.h"
#include "usb/usbPlatform.h"
#include "usb/tools/cmdParser.h"
#include "lpdClient.h"
#define PRN_NAME "prnTool"
#define PRN_DESCR "Network printer daemon client Application Program"
#define PRN_VERSION "01f"
#define PRN_COPYRIGHT "Copyright (c) 2007, Zhao Housework.\n"
#define PRN_DEBUG 1
#undef PRN_DEBUG
/* Prompt definition */
#define PROMPT_PRN "printer>"
#define LPD_PORT 515
#define LPD_LHOST "mpc5200"
#define LPD_RUSER "mpc5200"
#define LPD_OK 0x00
#define MAX_BUF 1024
PRN_INFO s_prnInfo ={
{'/','c','/','q','u','e','u','e','@','p','r','n','s','v','r','.','l','p','d','\0'},
{'p','r','n','s','v','r','\0'},
{'q','u','e','u','e','\0'}
};
PRN_PROFILE s_prnProfile = {
{'m','p','c','5','2','0','0','\0'},
{'m','p','c','5','2','0','0','\0'},
{'5','\0'},
{'/','c','/','q','u','e','u','e','@','p','r','n','s','v','r','.','l','p','d','\0'},
{'m','p','c','5','2','0','0','\0'},
{'/','c','/','q','u','e','u','e','@','p','r','n','s','v','r','.','l','p','d','\0'},
{'m','p','c','5','2','0','0','\0'},
{'N','e','t','w','o','r','k', '_','P','r','i','n','t','_','D','a','e','m','o','n','\0'},
{'1','3','1','\0'},
{'1'},
{'f'}
};
int sFd = -1;
int job = 0;
LOCAL UINT16 cmdPrnInit(pVOID Param, char **ppCmd, FILE *fin, FILE *fout)
{
int sockAddrSize;
struct sockaddr_in localAddr, remoteAddr;
Prompt("The print file: ", PROMPT_STRING, &s_prnInfo.prnfile[0]);
Prompt("The print host: ", PROMPT_STRING, &s_prnInfo.prnhost[0]);
Prompt("The print queue: ", PROMPT_STRING, &s_prnInfo.prnqueue[0]);
/* resolve host ip */
if(sFd >= 0)
{
close(sFd);
}
sockAddrSize = sizeof( struct sockaddr_in );
bzero((char *)&remoteAddr, sockAddrSize);
remoteAddr.sin_family = AF_INET;
remoteAddr.sin_len = (u_char) sockAddrSize;
remoteAddr.sin_port = htons(LPD_PORT);
if(((remoteAddr.sin_addr.s_addr = inet_addr(s_prnInfo.prnhost)) == ERROR) &&
((remoteAddr.sin_addr.s_addr = hostGetByName(s_prnInfo.prnhost)) ==ERROR))
{
fprintf(fout, "unknown host %s\n", s_prnInfo.prnhost);
return RET_CONTINUE;
}
/* open socket */
if((sFd = socket(AF_INET, SOCK_STREAM, 0)) == ERROR)
{
fprintf(fout, "failed in socket opening\n");
return RET_CONTINUE;
}
/* bind port 900 - needs to be priveledged for some lpds */
bzero((char *)&localAddr, sockAddrSize);
localAddr.sin_family = AF_INET;
localAddr.sin_addr.s_addr = htonl(INADDR_ANY);
localAddr.sin_port = htons(900);
localAddr.sin_len = (u_char) sockAddrSize;
if( bind(sFd, (struct sockaddr *)&localAddr, sockAddrSize) == ERROR)
{
fprintf(fout, "cannot bind port TCP %u\n", LPD_PORT);
close(sFd);
return RET_CONTINUE;
}
/* connect to server */
if(connect(sFd, (struct sockaddr*)&remoteAddr, sockAddrSize) == ERROR)
{
fprintf(fout, "cannot connect\n");
close(sFd);
return RET_CONTINUE;
}
return RET_CONTINUE;
}
LOCAL UINT16 cmdPrnProfile(pVOID Param, char **ppCmd, FILE *fin, FILE *fout)
{
Prompt("Class for banner page: ", PROMPT_STRING, &s_prnProfile.Cclass[0]);
Prompt("Host name: ", PROMPT_STRING, &s_prnProfile.Hhost[0]);
Prompt("Indent Printing: ", PROMPT_STRING, &s_prnProfile.Iident[0]);
Prompt("Job name for banner page: ", PROMPT_STRING, &s_prnProfile.Jjobname[0]);
Prompt("Print banner page: ", PROMPT_STRING, &s_prnProfile.Luser[0]);
Prompt("Name of source file: ", PROMPT_STRING, &s_prnProfile.Nname[0]);
Prompt("User identification: ", PROMPT_STRING, &s_prnProfile.Pname[0]);
Prompt("Title for print: ", PROMPT_STRING, &s_prnProfile.Ttitle[0]);
Prompt("Width of output: ", PROMPT_STRING, &s_prnProfile.Wwidth[0]);
Prompt("print font 1-troff R font;2-troff I font;3-troff B font;4-troff S font: ", PROMPT_CHAR, &s_prnProfile.font);
Prompt("plot mode c-CIF;d-DVI;f-plained text;g-Berkeley;l-without filter;n-ditroff;o-Postscript;p-prformat;r-FORTRAN carriage control;t-troff;v-raster: ", PROMPT_CHAR, &s_prnProfile.plotmode);
return RET_CONTINUE;
}
LOCAL UINT16 cmdPrnBegin(pVOID Param, char **ppCmd, FILE *fin, FILE *fout)
{
char buf[MAX_BUF]; /* string buffer */
int sz_buf; /* occupied size of buffer */
int rc;
sz_buf = sprintf(buf,"\1%s\12", s_prnInfo.prnqueue);
#ifdef PRN_DEBUG
fprintf(fout,"%s\n", buf);
#endif
rc = send(sFd, buf, sz_buf, 0);
if(rc == ERROR)
{
fprintf(fout,"Command send error %d!\n", rc);
return RET_CONTINUE;
}
fprintf(fout,"Print waiting job %s command sended!\n", s_prnInfo.prnqueue);
return RET_CONTINUE;
}
LOCAL UINT16 cmdPrnQueueShort(pVOID Param, char **ppCmd, FILE *fin, FILE *fout)
{
char buf[MAX_BUF]; /* string buffer */
char resp[MAX_BUF];
int sz_buf; /* occupied size of buffer */
int rc;
sz_buf = sprintf(buf,"\3%s %s\12", s_prnInfo.prnqueue, LPD_RUSER);
#ifdef PRN_DEBUG
fprintf(fout,"%s\n", buf);
#endif
rc = send(sFd, buf, sz_buf, 0);
if(rc == ERROR)
{
fprintf(fout,"Command send error %d!\n", rc);
return RET_CONTINUE;
}
rc = recv(sFd, &resp, MAX_BUF, 0); /* wait for char */
if(rc == ERROR)
{
fprintf(fout,"Receive queue state short information failed\n");
return RET_CONTINUE;
}
fprintf(fout,"Queue state short information: %s\n", resp);
return RET_CONTINUE;
}
LOCAL UINT16 cmdPrnLongQueue(pVOID Param, char **ppCmd, FILE *fin, FILE *fout)
{
char buf[MAX_BUF]; /* string buffer */
char resp[MAX_BUF];
int sz_buf; /* occupied size of buffer */
int rc;
sz_buf = sprintf(buf,"\4%s %s\12", s_prnInfo.prnqueue, LPD_RUSER);
#ifdef PRN_DEBUG
fprintf(fout,"%s\n", buf);
#endif
rc = send(sFd, buf, sz_buf, 0);
if(rc == ERROR)
{
fprintf(fout,"Command send error %d!\n", rc);
return RET_CONTINUE;
}
rc = recv(sFd, &resp, MAX_BUF, 0); /* wait for char */
if(rc == ERROR)
{
fprintf(fout,"Receive queue state long information failed\n");
return RET_CONTINUE;
}
fprintf(fout,"Queue state long information: %s\n", resp);
return RET_CONTINUE;
}
LOCAL UINT16 cmdPrnRemoveJob(pVOID Param, char **ppCmd, FILE *fin, FILE *fout)
{
char buf[MAX_BUF]; /* string buffer */
int sz_buf; /* occupied size of buffer */
int rc;
sz_buf = sprintf(buf,"\5%s root %s\12", s_prnInfo.prnqueue, LPD_RUSER);
#ifdef PRN_DEBUG
fprintf(fout,"%s\n", buf);
#endif
rc = send(sFd, buf, sz_buf, 0);
if(rc == ERROR)
{
fprintf(fout,"Command send error %d!\n", rc);
return RET_CONTINUE;
}
fprintf(fout,"removing jobs command sended\n");
return RET_CONTINUE;
}
LOCAL UINT16 cmdPrnClose(pVOID Param, char **ppCmd, FILE *fin, FILE *fout)
{
close(sFd);
fprintf(fout,"connection with printer server disconnected!\n");
return RET_CONTINUE;
}
LOCAL UINT16 cmdPrnSend(pVOID Param, char **ppCmd, FILE *fin, FILE *fout)
{
char buf[MAX_BUF]; /* string buffer */
char ctrlBuf[MAX_BUF]; /* string buffer */
char tempBuf[48];
char resp;
int sz_buf; /* occupied size of buffer */
int rc;
struct stat file_attrib; /* structure to store file attributes */
FILE* fd; /* file descriptor for local print file */
int cnt_file_bytes; /* length of control file */
fd =fopen(s_prnInfo.prnfile,"r");
if( fd == ERROR )
{
fprintf(fout,"ERROR open file errno = %d\n" , errno);
return RET_CONTINUE;
}
rc = stat(s_prnInfo.prnfile, &file_attrib);
if(rc == ERROR)
{
fprintf(fout,"get file status error\n");
fclose(fd);
return RET_CONTINUE;
}
fprintf(fout,"sending printer file and control information ....\n");
sz_buf = sprintf(buf,"\2%s\12", s_prnInfo.prnqueue);
#ifdef PRN_DEBUG
fprintf(fout,"%s\n", buf);
#endif
rc = send(sFd, buf, sz_buf, 0);
if(rc == ERROR)
{
fprintf(fout,"Command send error %d!\n", rc);
fclose(fd);
return RET_CONTINUE;
}
rc = recv(sFd, &resp, 1, 0); /* wait for char */
if(rc == ERROR)
{
fprintf(fout,"server not responsed or error\n");
fclose(fd);
return RET_CONTINUE;
}
if(resp != LPD_OK)
{
fprintf("LPD error %d!\n", resp);
fclose(fd);
return RET_CONTINUE;
}
/* send datafile header */
fprintf(fout, "Sending datafile header...\n");
sz_buf = sprintf(buf, "\3%d dfA%03d%s\12", file_attrib.st_size, job, LPD_LHOST);
#ifdef PRN_DEBUG
fprintf(fout,"%s\n", buf);
#endif
rc = send(sFd, buf, sz_buf, 0);
if(rc == ERROR)
{
fprintf(fout,"Command send error %d!\n", rc);
fclose(fd);
return RET_CONTINUE;
}
rc = recv(sFd, &resp, 1, 0); /* wait for char */
if(rc == ERROR)
{
fprintf(fout, "Receive data header echo error\n");
fclose(fd);
return RET_CONTINUE;
}
if(resp != LPD_OK)
{
fprintf(fout, "LPD,Receive data header echo error %d!\n", resp);
fclose(fd);
return RET_CONTINUE;
}
/* send datafile */
fprintf(fout, "Sending datafile...\n");
sz_buf = fread(buf, 1, MAX_BUF, fd);
while(sz_buf != 0)
{
rc = send( sFd, buf, sz_buf, 0);
if(rc == ERROR)
break;
sz_buf = fread(buf, 1, MAX_BUF, fd);
}
fclose(fd);
if(rc == ERROR)
{
fprintf(fout, "Send data file error %d!\n",rc);
return RET_CONTINUE;
}
rc = send(sFd, "\0", 1, 0);
if(rc == ERROR)
{
fprintf(fout, "Send data file last char error %d!\n", rc);
return RET_CONTINUE;
}
rc = recv(sFd, &resp, 1, 0); /* wait for char */
if(rc == ERROR)
{
fprintf(fout, "Receive data file echo error\n");
return RET_CONTINUE;
}
if(resp != LPD_OK)
{
fprintf(fout, "LPD,Receive data file echo error %d!\n", resp);
return RET_CONTINUE;
}
/* build control file according printer profile */
bzero(ctrlBuf, MAX_BUF);
strcpy(ctrlBuf, "C");
strcat(ctrlBuf, s_prnProfile.Cclass);
strcat(ctrlBuf, "\n");
strcat(ctrlBuf, "H");
strcat(ctrlBuf, s_prnProfile.Hhost);
strcat(ctrlBuf, "\n");
strcat(ctrlBuf, "I");
strcat(ctrlBuf, s_prnProfile.Iident);
strcat(ctrlBuf, "\n");
strcat(ctrlBuf, "J");
strcat(ctrlBuf, s_prnProfile.Jjobname);
strcat(ctrlBuf, "\n");
strcat(ctrlBuf, "L");
strcat(ctrlBuf, s_prnProfile.Luser);
strcat(ctrlBuf, "\n");
strcat(ctrlBuf, "N");
strcat(ctrlBuf, s_prnProfile.Nname);
strcat(ctrlBuf, "\n");
strcat(ctrlBuf, "P");
strcat(ctrlBuf, s_prnProfile.Pname);
strcat(ctrlBuf, "\n");
strcat(ctrlBuf, "T");
strcat(ctrlBuf, s_prnProfile.Ttitle);
strcat(ctrlBuf, "\n");
strcat(ctrlBuf, "W");
strcat(ctrlBuf, s_prnProfile.Wwidth);
strcat(ctrlBuf, "\n");
bzero(tempBuf,48);
sz_buf = sprintf(tempBuf, "%cdfA%03d%s\n", s_prnProfile.font, job, LPD_LHOST);
strcat(ctrlBuf, tempBuf);
bzero(tempBuf,48);
sz_buf = sprintf(tempBuf, "%cdfA%03d%s\n", s_prnProfile.plotmode, job, LPD_LHOST);
strcat(ctrlBuf, tempBuf);
#ifdef PRN_DEBUG
fprintf(fout,"%s\n", ctrlBuf);
#endif
/* size control file */
cnt_file_bytes = sprintf(buf, ctrlBuf);
/* send control file header */
fprintf(fout, "Sending control file header...\n");
sz_buf = sprintf(buf, "\2%d cfA%03d%s\12", cnt_file_bytes, job, LPD_LHOST);
#ifdef PRN_DEBUG
fprintf(fout,"%s\n", buf);
#endif
rc = send (sFd, buf, sz_buf, 0);
if(rc == ERROR)
{
fprintf(fout, "Send control file header error!\n");
return RET_CONTINUE;
}
rc = recv(sFd, &resp, 1, 0); /* waits for char */
if(rc == ERROR)
{
fprintf(fout, "receive control file header echo error!\n");
return RET_CONTINUE;
}
if(resp != LPD_OK)
{
fprintf(fout, "LPD receive control file header echo error!\n");
return RET_CONTINUE;
}
/* Send Control file */
printf("Sending control file ...\n");
rc = send(sFd, ctrlBuf, cnt_file_bytes, 0);
if(rc == ERROR)
{
fprintf(fout, "Send control file header error!\n");
return RET_CONTINUE;
}
rc = send(sFd, "\0", 1, 0);
if(rc == ERROR)
{
fprintf(fout, "Send control file Last char error!\n");
return RET_CONTINUE;
}
rc = recv( sFd, &resp, 1, 0); /* wait for char */
if(rc == ERROR)
{
fprintf(fout, "receive control file echo error!\n");
return RET_CONTINUE;
}
if(resp != LPD_OK)
{
fprintf(fout, "LPD receive control file echo error!\n");
return RET_CONTINUE;
}
fprintf(fout, "Print job successfully submitted!\n");
return RET_CONTINUE;
}
LOCAL CMD_DESCR cmdTable [] =
{
{"prnInit", 4, "prnInit", "print operation initialization", cmdPrnInit},
{"prnProfile", 4, "prnProfile", "printer profile configuration", cmdPrnProfile},
{"prnSend", 4, "prnSend", "sending printer job ", cmdPrnSend},
{"prnBegin", 4, "prnBegin", "print waiting queue jobs", cmdPrnBegin},
{"prnQueueShort", 4, "prnQueueShort", "request short print queue state", cmdPrnQueueShort},
{"prnLongQueue", 4, "prnLongQueue", "request long print queue state", cmdPrnLongQueue},
{"prnRemoveJob", 4, "prnRemoveJob", "printer remove jobs", cmdPrnRemoveJob},
{"prnClose", 4, "prnClose", "printer control Close", cmdPrnClose},
{"Help", 4, "Help/?", "Displays list of supported commands.", CmdParserHelpFunc},
{"?", 1, NULL, NULL, CmdParserHelpFunc},
{"Exit", 4, "Exit/Quit/Bye", "Exits program.", CmdParserExitFunc},
{"Quit", 1, NULL, NULL, CmdParserExitFunc},
{"Bye", 3, NULL, NULL, CmdParserExitFunc},
{NULL, 0, NULL, NULL, NULL}
};
UINT16 prnTool (void)
{
UINT16 s;
setvbuf (stdin, NULL, _IONBF, 0);
setvbuf (stdout, NULL, _IONBF, 0);
fprintf (stdout, PRN_NAME ": " PRN_DESCR ", version " PRN_VERSION "\n"
PRN_COPYRIGHT "\n\n");
while ((s = PromptAndExecCmd (NULL, PROMPT_PRN, stdin, stdout, cmdTable))
== RET_CONTINUE)
;
if (s == RET_OK)
fprintf (stdout, PRN_NAME " terminating normally.\n");
else if (s == RET_ERROR)
fprintf (stdout, PRN_NAME " terminating with error.\n");
else
fprintf (stdout, PRN_NAME " terminating with code %d.\n", s);
return s;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -