From f36b68a22e66f2878d3b5479f74dc84255f0039f Mon Sep 17 00:00:00 2001 From: klirichek Date: Sat, 9 Aug 2014 18:25:33 +0700 Subject: [PATCH] formatting fixed, and also restoring signal handler added --- rastertozj.c | 236 ++++++++++++++++++++++++++------------------------- 1 file changed, 119 insertions(+), 117 deletions(-) diff --git a/rastertozj.c b/rastertozj.c index 6f09788..9ce10df 100644 --- a/rastertozj.c +++ b/rastertozj.c @@ -11,8 +11,6 @@ #include #include -#define MAX(a,b) ( ((a) > (b)) ? (a) : (b) ) - // uncomment next line in order to have verbose dump in DEBUGFILE // after print @@ -20,21 +18,19 @@ #define DEBUGFILE "/tmp/debugraster.txt" -#define FALSE 0 -#define TRUE (!FALSE) - struct settings_ { - int cashDrawer1; - int cashDrawer2; + int cashDrawer1; + int cashDrawer2; int blankSpace; int feedDist; }; +struct settings_ settings; struct command { - int length; - char* command; + int length; + char* command; }; // define printer initialize command @@ -50,14 +46,11 @@ static const struct command cashDrawerEject [2] = static const struct command rasterModeStartCommand = {4,(char[4]){0x1d,0x76,0x30,0}}; -// define raster flush command -static const struct command rasterModeFlushCommand = -{3,(char[3]){0x1b,0x4a,0x15}}; - #ifdef DEBUGP FILE* lfd = 0; #endif +// putchar with debug wrapper inline void mputchar(char c) { unsigned char m = c; @@ -68,89 +61,95 @@ inline void mputchar(char c) putchar(m); } - +// procedure to output an array inline void outputarray(const char * array, int length) { -int i = 0; -for (;i>8); + return lo (val>>8); } +// enter raster mode and set up x and y dimensions inline void rasterheader(int xsize, int ysize) { -outputCommand(rasterModeStartCommand); -mputchar(lo(xsize)); -mputchar(hi(xsize)); -mputchar(lo(ysize)); -mputchar(hi(ysize)); + outputCommand(rasterModeStartCommand); + mputchar(lo(xsize)); + mputchar(hi(xsize)); + mputchar(lo(ysize)); + mputchar(hi(ysize)); } +// print all unprinted (i.e. flush the buffer) +// then feed given number of lines inline void skiplines(int size) -{ mputchar(0x1b); -mputchar(0x4a); -mputchar(size); +{ + mputchar(0x1b); + mputchar(0x4a); + mputchar(size); } +// get an option inline int getOptionChoiceIndex(const char * choiceName, ppd_file_t * ppd) { -ppd_choice_t * choice; -ppd_option_t * option; + ppd_choice_t * choice; + ppd_option_t * option; -choice = ppdFindMarkedChoice(ppd, choiceName); + choice = ppdFindMarkedChoice(ppd, choiceName); -if (choice == NULL) -{ - if ((option = ppdFindOption(ppd, choiceName)) == NULL) return -1; - if ((choice = ppdFindChoice(option,option->defchoice)) == NULL) return -1; -} + if (choice == NULL) + { + if ((option = ppdFindOption(ppd, choiceName)) == NULL) return -1; + if ((choice = ppdFindChoice(option,option->defchoice)) == NULL) return -1; + } -return atoi(choice->choice); + return atoi(choice->choice); } -struct settings_ settings; inline void initializeSettings(char * commandLineOptionSettings) { -ppd_file_t * ppd = NULL; -cups_option_t * options = NULL; -int numOptions = 0; + ppd_file_t * ppd = NULL; + cups_option_t * options = NULL; + int numOptions = 0; -ppd = ppdOpenFile(getenv("PPD")); + 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); + if ((numOptions != 0) && (options != NULL)) + { + cupsMarkOptions(ppd, numOptions, options); + cupsFreeOptions(numOptions, options); + } -memset(&settings, 0x00, sizeof(struct settings_)); + 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); + settings.cashDrawer2 = getOptionChoiceIndex("CashDrawer2Setting", ppd); + settings.blankSpace = getOptionChoiceIndex("BlankSpace" , ppd); + settings.feedDist = getOptionChoiceIndex("FeedDist" , ppd); - ppdClose(ppd); + ppdClose(ppd); } +// sent on the beginning of print job void jobSetup() { if ( settings.cashDrawer1==1 ) @@ -160,6 +159,7 @@ void jobSetup() outputCommand(printerInitializeCommand); } +// sent at the very end of print job void ShutDown() { if ( settings.cashDrawer1==2 ) @@ -169,15 +169,17 @@ void ShutDown() outputCommand(printerInitializeCommand); } -int num_of_feed; - +// sent at the end of every page +__sighandler_t old_signal; void EndPage() { int i; for (i=0; i 7) - { - fputs("ERROR: rastertozj58 job-id user title copies options [file]\n", stderr); - return EXIT_FAILURE; - } - - if (argc == 7) - { - if ((fd = open(argv[6], O_RDONLY)) == -1) - { - perror("ERROR: Unable to open raster file - "); - sleep(1); - - return EXIT_FAILURE; - } - } - else - fd = 0; + int fd = 0; // File descriptor providing CUPS raster data + cups_raster_t * ras = NULL; // Raster stream for printing + cups_page_header2_t header; /* CUPS Page header */ + int page = 0; /* Current page */ + int y = 0; /* Vertical position in page 0 <= y <= header.cupsHeight */ + + unsigned char * rasterData = NULL; /* Pointer to raster data */ + unsigned char * originalRasterDataPtr = NULL; /* Copy of original pointer for freeing buffer */ + + if (argc < 6 || argc > 7) + { + fputs("ERROR: rastertozj58 job-id user title copies options [file]\n", stderr); + return EXIT_FAILURE; + } + + if (argc == 7) + { + if ((fd = open(argv[6], O_RDONLY)) == -1) + { + perror("ERROR: Unable to open raster file - "); + sleep(1); + return EXIT_FAILURE; + } + } else + fd = 0; + #ifdef DEBUGP lfd = fopen ("/tmp/raster.txt","w"); #endif - initializeSettings(argv[5]); - jobSetup(); - ras = cupsRasterOpen(fd, CUPS_RASTER_READ); - page = 0; + initializeSettings(argv[5]); + jobSetup(); + ras = cupsRasterOpen(fd, CUPS_RASTER_READ); + page = 0; - while (cupsRasterReadHeader2(ras, &header)) - { - if ((header.cupsHeight == 0) || (header.cupsBytesPerLine == 0)) - break; + while (cupsRasterReadHeader2(ras, &header)) + { + if ((header.cupsHeight == 0) || (header.cupsBytesPerLine == 0)) + break; #ifdef DEBUGP if (lfd) fprintf(lfd,"\nheader.cupsHeight=%d, header.cupsBytesPerLine=%d\n",header.cupsHeight,header.cupsBytesPerLine); #endif - if (rasterData == NULL) - { - rasterData = malloc(header.cupsBytesPerLine*24); - if (rasterData == NULL) - { + if (rasterData == NULL) + { + rasterData = malloc(header.cupsBytesPerLine*24); + if (rasterData == NULL) + { if (originalRasterDataPtr != NULL) free(originalRasterDataPtr); cupsRasterClose(ras); if (fd != 0) close(fd); - return EXIT_FAILURE; - - } - originalRasterDataPtr = rasterData; // used to later free the memory - } + return EXIT_FAILURE; + } + 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 width_size = foo >> 3; - #ifdef DEBUGP if (lfd) fprintf(lfd,"\nheader.cupsWidth=%d, foo=%d, width_size=%d\n",header.cupsWidth,foo,width_size); #endif @@ -311,6 +310,7 @@ int main(int argc, char *argv[]) #ifdef DEBUGP if (lfd) fprintf(lfd,"\nChecked %d bytes of %d for zero\n",j,rest_bytes); #endif + if (j>=rest_bytes) { ++zeroy; @@ -329,25 +329,27 @@ int main(int argc, char *argv[]) for(;zeroy>0;--zeroy) skiplines(24); - EndPage(); - } + EndPage(); + } ShutDown(); if (originalRasterDataPtr != NULL) free(originalRasterDataPtr); cupsRasterClose(ras); if (fd != 0) - close(fd); + close(fd); + + if (page == 0) + fputs("ERROR: No pages found!\n", stderr); + else + fputs("INFO: Ready to print.\n", stderr); - if (page == 0) - fputs("ERROR: No pages found!\n", stderr); - else - fputs("INFO: Ready to print.\n", stderr); #ifdef DEBUGP if (lfd) fclose(lfd); #endif - return (page == 0)?EXIT_FAILURE:EXIT_SUCCESS; + + return (page == 0)?EXIT_FAILURE:EXIT_SUCCESS; } // end of rastertostar.c