Applied clang-format

Nothing changed at all, just formatting of c-file
exp
Alexey N. Vinogradov 6 years ago
parent fcd9b33fe5
commit d80dd2d1df

@ -3,9 +3,9 @@
#include <cups/cups.h> #include <cups/cups.h>
#include <cups/ppd.h> #include <cups/ppd.h>
#include <cups/raster.h> #include <cups/raster.h>
#include <stdlib.h>
#include <fcntl.h> #include <fcntl.h>
#include <signal.h> #include <signal.h>
#include <stdlib.h>
// uncomment next line in order to have verbose dump in DEBUGFILE // uncomment next line in order to have verbose dump in DEBUGFILE
// after print // after print
@ -14,342 +14,320 @@
#define DEBUGFILE "/tmp/debugraster.txt" #define DEBUGFILE "/tmp/debugraster.txt"
struct settings_ struct settings_ {
{ int cashDrawer1;
int cashDrawer1; int cashDrawer2;
int cashDrawer2; int blankSpace;
int blankSpace; int feedDist;
int feedDist;
}; };
struct settings_ settings; struct settings_ settings;
struct command struct command {
{ int length;
int length; char *command;
char* command;
}; };
// define printer initialize command // define printer initialize command
static const struct command printerInitializeCommand = static const struct command printerInitializeCommand = {2,
{2,(char[2]){0x1b,0x40}}; (char[2]){0x1b, 0x40}};
// define cashDrawerEjector command // define cashDrawerEjector command
static const struct command cashDrawerEject [2] = static const struct command cashDrawerEject[2] = {
{{5,(char[5]){0x1b,0x70,0,0x40,0x50}}, {5, (char[5]){0x1b, 0x70, 0, 0x40, 0x50}},
{5,(char[5]){0x1b,0x70,1,0x40,0x50}}}; {5, (char[5]){0x1b, 0x70, 1, 0x40, 0x50}}};
// define raster mode start command // define raster mode start command
static const struct command rasterModeStartCommand = static const struct command rasterModeStartCommand = {
{4,(char[4]){0x1d,0x76,0x30,0}}; 4, (char[4]){0x1d, 0x76, 0x30, 0}};
#ifdef DEBUGP #ifdef DEBUGP
FILE* lfd = 0; FILE *lfd = 0;
#endif #endif
// putchar with debug wrapper // putchar with debug wrapper
inline void mputchar(char c) inline void mputchar(char c) {
{ unsigned char m = c;
unsigned char m = c;
#ifdef DEBUGP #ifdef DEBUGP
if (lfd) if (lfd)
fprintf (lfd,"%02x ",m); fprintf(lfd, "%02x ", m);
#endif #endif
putchar(m); putchar(m);
} }
// procedure to output an array // procedure to output an array
inline void outputarray(const char * array, int length) inline void outputarray(const char *array, int length) {
{ int i = 0;
int i = 0; for (; i < length; ++i)
for (;i<length;++i) mputchar(array[i]);
mputchar(array[i]);
} }
// output a command // output a command
inline void outputCommand(struct command output) inline void outputCommand(struct command output) {
{ outputarray(output.command, output.length);
outputarray(output.command,output.length);
} }
inline int lo(int val) inline int lo(int val) { return val & 0xFF; }
{
return val & 0xFF;
}
inline int hi (int val) inline int hi(int val) { return lo(val >> 8); }
{
return lo (val>>8);
}
// enter raster mode and set up x and y dimensions // enter raster mode and set up x and y dimensions
inline void rasterheader(int xsize, int ysize) inline void rasterheader(int xsize, int ysize) {
{ outputCommand(rasterModeStartCommand);
outputCommand(rasterModeStartCommand); mputchar(lo(xsize));
mputchar(lo(xsize)); mputchar(hi(xsize));
mputchar(hi(xsize)); mputchar(lo(ysize));
mputchar(lo(ysize)); mputchar(hi(ysize));
mputchar(hi(ysize));
} }
// print all unprinted (i.e. flush the buffer) // print all unprinted (i.e. flush the buffer)
// then feed given number of lines // then feed given number of lines
inline void skiplines(int size) inline void skiplines(int size) {
{ mputchar(0x1b);
mputchar(0x1b); mputchar(0x4a);
mputchar(0x4a); mputchar(size);
mputchar(size);
} }
// get an option // get an option
inline int getOptionChoiceIndex(const char * choiceName, ppd_file_t * ppd) inline int getOptionChoiceIndex(const char *choiceName, ppd_file_t *ppd) {
{ ppd_choice_t *choice;
ppd_choice_t * choice; ppd_option_t *option;
ppd_option_t * option;
choice = ppdFindMarkedChoice(ppd, choiceName); choice = ppdFindMarkedChoice(ppd, choiceName);
if (choice == NULL) if (choice == NULL) {
{ if ((option = ppdFindOption(ppd, choiceName)) == NULL)
if ((option = ppdFindOption(ppd, choiceName)) == NULL) return -1; return -1;
if ((choice = ppdFindChoice(option,option->defchoice)) == NULL) return -1; if ((choice = ppdFindChoice(option, option->defchoice)) == NULL)
} return -1;
}
return atoi(choice->choice); return atoi(choice->choice);
} }
void initializeSettings(char *commandLineOptionSettings) {
ppd_file_t *ppd = NULL;
cups_option_t *options = NULL;
int numOptions = 0;
void initializeSettings(char * commandLineOptionSettings) ppd = ppdOpenFile(getenv("PPD"));
{
ppd_file_t * ppd = NULL;
cups_option_t * options = NULL;
int numOptions = 0;
ppd = ppdOpenFile(getenv("PPD")); ppdMarkDefaults(ppd);
ppdMarkDefaults(ppd); numOptions = cupsParseOptions(commandLineOptionSettings, 0, &options);
if ((numOptions != 0) && (options != NULL)) {
cupsMarkOptions(ppd, numOptions, options);
cupsFreeOptions(numOptions, options);
}
numOptions = cupsParseOptions(commandLineOptionSettings, 0, &options); memset(&settings, 0x00, sizeof(struct settings_));
if ((numOptions != 0) && (options != NULL))
{
cupsMarkOptions(ppd, numOptions, options);
cupsFreeOptions(numOptions, options);
}
memset(&settings, 0x00, sizeof(struct settings_)); settings.cashDrawer1 = getOptionChoiceIndex("CashDrawer1Setting", ppd);
settings.cashDrawer2 = getOptionChoiceIndex("CashDrawer2Setting", ppd);
settings.blankSpace = getOptionChoiceIndex("BlankSpace", ppd);
settings.feedDist = getOptionChoiceIndex("FeedDist", ppd);
settings.cashDrawer1 = getOptionChoiceIndex("CashDrawer1Setting", ppd); ppdClose(ppd);
settings.cashDrawer2 = getOptionChoiceIndex("CashDrawer2Setting", ppd);
settings.blankSpace = getOptionChoiceIndex("BlankSpace" , ppd);
settings.feedDist = getOptionChoiceIndex("FeedDist" , ppd);
ppdClose(ppd);
} }
// sent on the beginning of print job // sent on the beginning of print job
void jobSetup() void jobSetup() {
{ if (settings.cashDrawer1 == 1)
if ( settings.cashDrawer1==1 ) outputCommand(cashDrawerEject[0]);
outputCommand(cashDrawerEject[0]); if (settings.cashDrawer2 == 1)
if ( settings.cashDrawer2==1 ) outputCommand(cashDrawerEject[1]);
outputCommand(cashDrawerEject[1]); outputCommand(printerInitializeCommand);
outputCommand(printerInitializeCommand);
} }
// sent at the very end of print job // sent at the very end of print job
void ShutDown() void ShutDown() {
{ if (settings.cashDrawer1 == 2)
if ( settings.cashDrawer1==2 ) outputCommand(cashDrawerEject[0]);
outputCommand(cashDrawerEject[0]); if (settings.cashDrawer2 == 2)
if ( settings.cashDrawer2==2 ) outputCommand(cashDrawerEject[1]);
outputCommand(cashDrawerEject[1]); outputCommand(printerInitializeCommand);
outputCommand(printerInitializeCommand);
} }
// sent at the end of every page // sent at the end of every page
#ifndef __sighandler_t #ifndef __sighandler_t
typedef void (*__sighandler_t) (int); typedef void (*__sighandler_t)(int);
#endif #endif
__sighandler_t old_signal; __sighandler_t old_signal;
void EndPage() void EndPage() {
{ int i;
int i; for (i = 0; i < settings.feedDist; ++i)
for (i=0; i<settings.feedDist; ++i) skiplines(0x18);
skiplines(0x18); signal(15, old_signal);
signal(15,old_signal);
} }
// sent on job canceling // sent on job canceling
void cancelJob(int foo) void cancelJob(int foo) {
{ int i = 0;
int i=0; for (; i < 0x258; ++i)
for(;i<0x258;++i) mputchar(0);
mputchar(0); EndPage();
EndPage(); ShutDown();
ShutDown();
} }
// invoked before starting to print a page // invoked before starting to print a page
void pageSetup() void pageSetup() { old_signal = signal(15, cancelJob); }
{
old_signal = signal(15,cancelJob);
}
////////////////////////////////////////////// //////////////////////////////////////////////
////////////////////////////////////////////// //////////////////////////////////////////////
int main(int argc, char *argv[]) int main(int argc, char *argv[]) {
{ int fd = 0; // File descriptor providing CUPS raster data
int fd = 0; // File descriptor providing CUPS raster data cups_raster_t *ras = NULL; // Raster stream for printing
cups_raster_t * ras = NULL; // Raster stream for printing cups_page_header2_t header; // CUPS Page header
cups_page_header2_t header; // CUPS Page header int page = 0; // Current page
int page = 0; // Current page int y = 0; // Vertical position in page 0 <= y <= header.cupsHeight
int y = 0; // Vertical position in page 0 <= y <= header.cupsHeight
unsigned char *rasterData = NULL; // Pointer to raster data from CUPS
unsigned char * rasterData = NULL; // Pointer to raster data from CUPS unsigned char *originalRasterDataPtr =
unsigned char * originalRasterDataPtr = NULL; // Copy of original pointer for freeing buffer NULL; // Copy of original pointer for freeing buffer
if (argc < 6 || argc > 7) if (argc < 6 || argc > 7) {
{ fputs("ERROR: rastertozj job-id user title copies options [file]\n",
fputs("ERROR: rastertozj job-id user title copies options [file]\n", stderr); stderr);
return EXIT_FAILURE; return EXIT_FAILURE;
} }
if (argc == 7) if (argc == 7) {
{ if ((fd = open(argv[6], O_RDONLY)) == -1) {
if ((fd = open(argv[6], O_RDONLY)) == -1) perror("ERROR: Unable to open raster file - ");
{ sleep(1);
perror("ERROR: Unable to open raster file - "); return EXIT_FAILURE;
sleep(1); }
return EXIT_FAILURE; } else
} fd = 0;
} else
fd = 0;
#ifdef DEBUGP #ifdef DEBUGP
lfd = fopen (DEBUGFILE, "w"); lfd = fopen(DEBUGFILE, "w");
#endif #endif
initializeSettings(argv[5]); initializeSettings(argv[5]);
jobSetup(); jobSetup();
ras = cupsRasterOpen(fd, CUPS_RASTER_READ); ras = cupsRasterOpen(fd, CUPS_RASTER_READ);
page = 0; page = 0;
while (cupsRasterReadHeader2(ras, &header)) while (cupsRasterReadHeader2(ras, &header)) {
{ if ((header.cupsHeight == 0) || (header.cupsBytesPerLine == 0))
if ((header.cupsHeight == 0) || (header.cupsBytesPerLine == 0)) break;
break;
#ifdef DEBUGP #ifdef DEBUGP
if (lfd) fprintf(lfd,"\nheader.cupsHeight=%d, header.cupsBytesPerLine=%d\n",header.cupsHeight,header.cupsBytesPerLine); if (lfd)
fprintf(lfd, "\nheader.cupsHeight=%d, header.cupsBytesPerLine=%d\n",
header.cupsHeight, header.cupsBytesPerLine);
#endif #endif
if (rasterData == NULL) if (rasterData == NULL) {
{ rasterData = malloc(header.cupsBytesPerLine * 24);
rasterData = malloc(header.cupsBytesPerLine*24); if (rasterData == NULL) {
if (rasterData == NULL) if (originalRasterDataPtr != NULL)
{ free(originalRasterDataPtr);
if (originalRasterDataPtr != NULL) free(originalRasterDataPtr); cupsRasterClose(ras);
cupsRasterClose(ras); if (fd != 0)
if (fd != 0) close(fd);
close(fd); return EXIT_FAILURE;
return EXIT_FAILURE; }
originalRasterDataPtr = rasterData; // used to later free the memory
} }
originalRasterDataPtr = rasterData; // used to later free the memory
} fprintf(stderr, "PAGE: %d %d\n", ++page, header.NumCopies);
pageSetup();
fprintf(stderr, "PAGE: %d %d\n", ++page, header.NumCopies);
pageSetup(); int foo = (header.cupsWidth > 0x180) ? 0x180 : header.cupsWidth;
foo = (foo + 7) & 0xFFFFFFF8;
int foo = ( header.cupsWidth > 0x180 ) ? 0x180 : header.cupsWidth; int width_size = foo >> 3;
foo = (foo+7) & 0xFFFFFFF8;
int width_size = foo >> 3;
#ifdef DEBUGP #ifdef DEBUGP
if (lfd) fprintf(lfd,"\nheader.cupsWidth=%d, foo=%d, width_size=%d\n",header.cupsWidth,foo,width_size); if (lfd)
fprintf(lfd, "\nheader.cupsWidth=%d, foo=%d, width_size=%d\n",
header.cupsWidth, foo, width_size);
#endif #endif
y = 0; y = 0;
int zeroy = 0; int zeroy = 0;
while ( y < header.cupsHeight ) while (y < header.cupsHeight) {
{ if ((y & 127) != 0)
if ((y & 127) != 0) fprintf(stderr, "INFO: Printing page %d, %d%% complete...\n", page,
fprintf(stderr, "INFO: Printing page %d, %d%% complete...\n", page, (100 * y / header.cupsHeight)); (100 * y / header.cupsHeight));
int rest = header.cupsHeight-y; int rest = header.cupsHeight - y;
if (rest>24) if (rest > 24)
rest=24; rest = 24;
#ifdef DEBUGP #ifdef DEBUGP
if (lfd) fprintf(lfd,"\nProcessing block of %d, starting from %d lines\n",rest,y); if (lfd)
fprintf(lfd, "\nProcessing block of %d, starting from %d lines\n", rest,
y);
#endif #endif
y+=rest; y += rest;
if (y) if (y) {
{ unsigned char *buf = rasterData;
unsigned char * buf = rasterData; int j;
int j; for (j = 0; j < rest; ++j) {
for ( j=0; j<rest; ++j) if (!cupsRasterReadPixels(ras, buf, header.cupsBytesPerLine))
{ break;
if (!cupsRasterReadPixels(ras,buf,header.cupsBytesPerLine)) buf += width_size;
break; }
buf+=width_size;
}
#ifdef DEBUGP #ifdef DEBUGP
if (lfd) fprintf(lfd,"\nReaded %d lines\n",j); if (lfd)
fprintf(lfd, "\nReaded %d lines\n", j);
#endif #endif
if (j<rest) if (j < rest)
continue; continue;
} }
int rest_bytes = rest * width_size; int rest_bytes = rest * width_size;
int j; int j;
for (j=0; j<rest_bytes;++j) for (j = 0; j < rest_bytes; ++j)
if (rasterData[j]!=0) if (rasterData[j] != 0)
break; break;
#ifdef DEBUGP #ifdef DEBUGP
if (lfd) fprintf(lfd,"\nChecked %d bytes of %d for zero\n",j,rest_bytes); if (lfd)
fprintf(lfd, "\nChecked %d bytes of %d for zero\n", j, rest_bytes);
#endif #endif
if (j>=rest_bytes) if (j >= rest_bytes) {
{ ++zeroy;
++zeroy; continue;
continue; }
}
for (; zeroy > 0; --zeroy)
for (;zeroy>0;--zeroy) skiplines(24);
skiplines(24);
rasterheader(width_size, rest);
rasterheader(width_size,rest); outputarray((char *)rasterData, width_size * 24);
outputarray((char*)rasterData,width_size*24); skiplines(0);
skiplines(0); }
}
if (!settings.blankSpace)
if (!settings.blankSpace) for (; zeroy > 0; --zeroy)
for(;zeroy>0;--zeroy) skiplines(24);
skiplines(24);
EndPage();
EndPage(); }
}
ShutDown();
ShutDown();
if (originalRasterDataPtr != NULL)
if (originalRasterDataPtr != NULL) free(originalRasterDataPtr); free(originalRasterDataPtr);
cupsRasterClose(ras); cupsRasterClose(ras);
if (fd != 0) if (fd != 0)
close(fd); close(fd);
if (page == 0) if (page == 0)
fputs("ERROR: No pages found!\n", stderr); fputs("ERROR: No pages found!\n", stderr);
else else
fputs("INFO: Ready to print.\n", stderr); fputs("INFO: Ready to print.\n", stderr);
#ifdef DEBUGP #ifdef DEBUGP
if (lfd) if (lfd)
fclose(lfd); fclose(lfd);
#endif #endif
return (page == 0)?EXIT_FAILURE:EXIT_SUCCESS; return (page == 0) ? EXIT_FAILURE : EXIT_SUCCESS;
} }
// end of rastertozj.c // end of rastertozj.c

Loading…
Cancel
Save