#include <windows.h>
#include <winsock.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <ctype.h>
SOCKET call_socket(const char *hostname, unsigned short portnum);
void printLogoFile(FILE *printer, int logoNum);
void printLogoNetwork(SOCKET s, int logoNum);
void printReceiptFile(FILE *printer, char buffer[4096], int bufferLength);
void printReceiptNetwork(SOCKET s, char buffer[4096], int bufferLength);
void cutPaperFile(FILE *printer);
void cutPaperNetwork(SOCKET s);
void printMarketingFile(FILE *printer, char *message);
void printMarketingNetwork(SOCKET s, char *message, int messageLen);
void enqueue(char c, char* q);
int getClientName(char *clientName);
int getHostName(char *hostname);
int main() {
int sections;
int i, i2;
int marketingSetup[64];
int portnum;
int bufferLength = 0;
int numReceipts = 1;
int tempStringLen;
int logoSetup[64];
char c;
char buffer[4096];
char dataString[1024];
char emptyString[64] = "\0";
char marketingString[64];
char marketingPath[64][64];
char tempString[1024];
char sectionString[64];
char sectionSetup[64];
char hostname[64];
char portType[2];
char q[5] = "0000";
char filter[4];
FILE *marketingFile;
FILE *printer;
WSADATA wsaData;
SOCKET s;
numReceipts = GetPrivateProfileInt("Setup", "numberCopies", 1, "tmPrint.ini");
GetPrivateProfileString("Setup", "portType", emptyString, portType, 2, "tmPrint.ini");
GetPrivateProfileString("Setup", "hostname", emptyString, hostname, 64, "tmPrint.ini");
portnum = GetPrivateProfileInt("Setup", "hostport", 9100, "tmPrint.ini");
sections = GetPrivateProfileInt("PrintSections", "number", -1, "tmPrint.ini");
GetPrivateProfileString("Setup", "filter", emptyString, filter, 4, "tmPrint.ini");
/*printf("numberCopies: %d\n", numReceipts);
printf("portType: %s\n", portType);
printf("hostname: %s\n", hostname);
printf("hostport: %d\n", portnum);
printf("sections: %d\n", sections);
printf("filter: %s\n", filter);*/
for(i=0; i<sections; i++) {
sprintf(sectionString, "PrintSection%d", i+1);
if(GetPrivateProfileInt(sectionString, "logo", -1, "tmPrint.ini") > 0) {
sectionSetup[i] = 'l';
logoSetup[i] = GetPrivateProfileInt(sectionString, "logo", -1, "tmPrint.ini");
}
else if(GetPrivateProfileInt(sectionString, "marketing", -1, "tmPrint.ini") > 0) {
sectionSetup[i] = 'm';
marketingSetup[i] = GetPrivateProfileInt(sectionString, "marketing", -1, "tmPrint.ini");
}
else if(GetPrivateProfileInt(sectionString, "text", -1, "tmPrint.ini") > 0)
sectionSetup[i] = 'r';
else if(GetPrivateProfileInt(sectionString, "cut", -1, "tmPrint.ini") > 0)
sectionSetup[i] = 'c';
}
if(toupper(portType[0]) == 'P') {
if ((printer = fopen("LPT1", "w")) == NULL) {
printf("Error opening Printer");
exit(1);
}
}
else if(toupper(portType[0]) == 'S') {
if((printer = fopen("COM1", "w")) == NULL) {
printf("Error opening printer");
exit(1);
}
}
else if(toupper(portType[0]) == 'N') {
if (WSAStartup(MAKEWORD(1,1), &wsaData ) != 0 ) {
printf("Could not find a usable WinSock DLL.\n");
exit(1);
}
s = call_socket(hostname, portnum);
if(s == INVALID_SOCKET) {
printf("connect() failed");
exit(1);
}
}
else if(toupper(portType[0]) == 'T') {
portnum = getHostName(hostname);
portType[0] = 'N';
if (WSAStartup(MAKEWORD(1,1), &wsaData ) != 0 ) {
printf("Could not find a usable WinSock DLL.\n");
exit(1);
}
s = call_socket(hostname, portnum);
if(s == INVALID_SOCKET) {
printf("connect() failed");
exit(1);
}
}
/* buffer the input */
while(!feof(stdin)) {
if(toupper(filter[0]) == 'Y') {
scanf("%c", &c);
enqueue(c, q);
if(q[3]==0x4A && q[2]=='\e' && q[4]==0xC0)
buffer[bufferLength++] = 0x18;
else if(q[3]==0x4A && q[2]=='\e' && q[4]==0xD0)
buffer[bufferLength++] = 0x18;
else
buffer[bufferLength++] = c;
}
else {
scanf("%c", &c);
buffer[bufferLength++] = c;
}
}
for(i2=0; i2<numReceipts; i2++) {
/* 1C 70 n m - print NV image
n = image number
m = image mode
0 normal
1 double-width
2 double height
4 quadruple
1B 64 n - print and feed n lines
1D 56 m - select cut mode and cut paper
m = cut mode
0 full cut
1 partial cut
0D - CR
00 - null
*/
for(i=0; i<sections; i++) {
switch(sectionSetup[i]) {
case 'l':
if(toupper(portType[0]) == 'S' || toupper(portType[0]) == 'P')
printLogoFile(printer, logoSetup[i]);
else if(toupper(portType[0]) == 'N')
printLogoNetwork(s, logoSetup[i]);
break;
case 'm':
sprintf(marketingString, "Marketing%d", marketingSetup[i]);
GetPrivateProfileString(marketingString, "path", emptyString, marketingPath[i], 64, "tmPrint.ini");
marketingFile = fopen(marketingPath[i], "r");
tempStringLen = 0;
while(!feof(marketingFile)) {
fscanf(marketingFile, "%c", &c);
tempString[tempStringLen++] = c;
}
tempString[tempStringLen++] = '\0';
if(toupper(portType[0]) == 'S' || toupper(portType[0]) == 'P')
printMarketingFile(printer, tempString);
else if(toupper(portType[0]) == 'N')
printMarketingNetwork(s, tempString, tempStringLen);
break;
case 'r':
if(toupper(portType[0]) == 'S' || toupper(portType[0]) == 'P')
printReceiptFile(printer, buffer, bufferLength);
else if(toupper(portType[0]) == 'N')
printReceiptNetwork(s, buffer, bufferLength);
break;
case 'c':
if(toupper(portType[0]) == 'S' || toupper(portType[0]) == 'P')
cutPaperFile(printer);
else if(toupper(portType[0]) == 'N')
cutPaperNetwork(s);
break;
}
}
}
if(toupper(portType[0]) == 'N') {
closesocket(s);
WSACleanup();
}
return 0;
}
void printLogoFile(FILE *printer, int logoNum) {
fprintf(printer,"%c%c%c%c%c%c%c%c",0x1C,0x70,logoNum,0x00,0x0D,0x00,0x00,0x00);
}
void printLogoNetwork(SOCKET s, int logoNum) {
char dataString[1024];
sprintf(dataString,"%c%c%c%c%c%c%c%c",0x1C,0x70,logoNum,0x00,0x0D,0x00,0x00,0x00);
if (send(s, dataString, 8, 0) != 8) {
printf("send() sent a different number of bytes than expected");
exit(1);
}
}
void printReceiptFile(FILE *printer, char buffer[4096], int bufferLength) {
int i;
for(i = 0; i<bufferLength; i++) {
fprintf(printer, "%c", buffer[i]);
}
}
void printReceiptNetwork(SOCKET s, char buffer[4096], int bufferLength) {
if (send(s, buffer, bufferLength, 0) != bufferLength) {
printf("send() sent a different number of bytes than expected");
exit(1);
}
}
void cutPaperFile(FILE *printer) {
fprintf(printer,"%c%c%c%c%c%c",0x1B,0x64,0x05,0x1D,0x56,0x00);
}
void cutPaperNetwork(SOCKET s) {
char dataString[1024];
sprintf(dataString,"%c%c%c%c%c%c",0x1B,0x64,0x05,0x1D,0x56,0x00);
if (send(s, dataString, 6, 0) != 6) {
printf("send() sent a different number of bytes than expected");
exit(1);
}
}
void printMarketingFile(FILE *printer, char *message) {
fprintf(printer, "%s", message);
}
void printMarketingNetwork(SOCKET s, char *message, int messageLen) {
if (send(s, message, messageLen, 0) != messageLen) {
printf("send() sent a different number of bytes than expected");
exit(1);
}
}
SOCKET call_socket(const char *hostname, unsigned short portnum) {
struct sockaddr_in sa;
struct hostent *hp;
SOCKET s;
hp = gethostbyname(hostname);
if (hp == NULL) /* we don't know who this host is */
return INVALID_SOCKET;
memset(&sa,0,sizeof(sa));
memcpy((char *)&sa.sin_addr, hp->h_addr, hp->h_length); /* set address */
sa.sin_family = hp->h_addrtype;
sa.sin_port = htons((u_short)portnum);
s = socket(hp->h_addrtype, SOCK_STREAM, 0);
if (s == INVALID_SOCKET)
return INVALID_SOCKET; /* try to connect to the specified socket */
if (connect(s, (struct sockaddr *)&sa, sizeof sa) == SOCKET_ERROR) {
closesocket(s);
return INVALID_SOCKET;
}
return s;
}
void enqueue(char c, char* q) {
q[0] = q[1];
q[1] = q[2];
q[2] = q[3];
q[3] = q[4];
q[4] = c;
}
int getHostName(char *hostname) {
int portNum;
char clientName[16] = "\0";
char emptyString[64] = "\0";
getClientName(clientName);
GetPrivateProfileString(clientName, "hostname", emptyString, hostname, 63, "tmPrint.ini");
portNum = GetPrivateProfileInt(clientName, "hostport", 9100, "tmPrint.ini");
return portNum;
}
int getClientName(char *clientName) {
char *userName;
char userFilePath[64];
char emptyString[64] = "\0";
FILE *userFile;
GetPrivateProfileString("Setup", "userFilePath", emptyString, userFilePath, 63, "tmPrint.ini");
userName = getenv("REDMON_USER");
strcat(userFilePath, userName);
userFile = fopen(userFilePath, "r");
if(userFile == NULL) {
printf("Unable to open the %s file\n", userFilePath);
exit(1);
}
fscanf(userFile, "%s", clientName);
return 0;
}