From de0d48dfdb02e98f2ee4e9ee7961c358ab7f0c69 Mon Sep 17 00:00:00 2001 From: Peter De Wachter Date: Sat, 18 Apr 2020 10:58:27 +0200 Subject: [PATCH] Be tolerant of zero-page jobs https://github.com/OpenPrinting/cups-filters/issues/117 --- CMakeLists.txt | 4 +++- src/job.cc | 14 ++++++++++---- src/job.h | 5 +++++ src/main.cc | 14 ++++++-------- test/test_job.cc | 35 +++++++++++++++++++++++++++++++++++ 5 files changed, 59 insertions(+), 13 deletions(-) create mode 100644 test/test_job.cc diff --git a/CMakeLists.txt b/CMakeLists.txt index 5f20c32..bba0828 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -116,15 +116,17 @@ add_executable(brdecode EXCLUDE_FROM_ALL src/brdecode.cc) add_executable(test_lest test/test_lest.cc) add_executable(test_line test/test_line.cc src/line.cc) add_executable(test_block test/test_block.cc) +add_executable(test_job test/test_job.cc src/job.cc src/line.cc) enable_testing() add_test(test_lest test_lest) add_test(test_line test_line) add_test(test_block test_block) +add_test(test_job test_job) # Autotools-style "make check" command add_custom_target(check COMMAND ${CMAKE_CTEST_COMMAND}) -add_dependencies(check test_lest test_line test_block) +add_dependencies(check test_lest test_line test_block test_job) # Installation diff --git a/src/job.cc b/src/job.cc index 6f088ae..dd3629c 100644 --- a/src/job.cc +++ b/src/job.cc @@ -26,17 +26,18 @@ job::job(FILE *out, const std::string &job_name) : out_(out), job_name_(job_name), - page_params_() { + page_params_(), + pages_(0) { // Delete dubious characters from job name std::replace_if(job_name_.begin(), job_name_.end(), [](char c) { return c < 32 || c >= 127 || c == '"' || c == '\\'; }, ' '); - - begin_job(); } job::~job() { - end_job(); + if (pages_ != 0) { + end_job(); + } } void job::begin_job() { @@ -86,6 +87,11 @@ void job::encode_page(const page_params &page_params, int lines, int linesize, nextline_fn nextline) { + if (pages_ == 0) { + begin_job(); + } + ++pages_; + if (!(page_params_ == page_params)) { page_params_ = page_params; write_page_header(); diff --git a/src/job.h b/src/job.h index 2ae7ea5..bd7a83b 100644 --- a/src/job.h +++ b/src/job.h @@ -55,6 +55,10 @@ class job { int linesize, nextline_fn nextline); + int pages() const { + return pages_; + } + private: void begin_job(); void end_job(); @@ -63,6 +67,7 @@ class job { FILE *out_; std::string job_name_; page_params page_params_; + int pages_; }; #endif // JOB_H diff --git a/src/main.cc b/src/main.cc index a942d00..d6232b4 100644 --- a/src/main.cc +++ b/src/main.cc @@ -170,7 +170,6 @@ int main(int argc, char *argv[]) { return 1; } - int pages = 0; { job job(stdout, ascii_job_name(job_id, job_user, job_name)); cups_page_header2_t header; @@ -179,11 +178,11 @@ int main(int argc, char *argv[]) { || header.cupsBitsPerColor != 1 || header.cupsNumColors != 1 || header.cupsBytesPerLine > 10000) { - fprintf(stderr, "ERROR: " PACKAGE ": Page %d: Bogus raster data.\n", pages + 1); + fprintf(stderr, "ERROR: " PACKAGE ": Page %d: Bogus raster data.\n", job.pages() + 1); dump_page_header(header); return 1; } - if (pages == 0) { + if (job.pages() == 0) { fprintf(stderr, "DEBUG: " PACKAGE ": Page header of first page\n"); dump_page_header(header); } @@ -191,13 +190,12 @@ int main(int argc, char *argv[]) { header.cupsHeight, header.cupsBytesPerLine, next_line); - fprintf(stderr, "PAGE: %d %d\n", ++pages, header.NumCopies); + fprintf(stderr, "PAGE: %d %d\n", job.pages(), header.NumCopies); } - } - if (pages == 0) { - fprintf(stderr, "ERROR: " PACKAGE ": No pages were found.\n"); - return 1; + if (job.pages() == 0) { + fprintf(stderr, "ERROR: " PACKAGE ": No pages were found.\n"); + } } fflush(stdout); diff --git a/test/test_job.cc b/test/test_job.cc new file mode 100644 index 0000000..5353564 --- /dev/null +++ b/test/test_job.cc @@ -0,0 +1,35 @@ +// This file is part of the brlaser printer driver. +// +// Copyright 2020 Peter De Wachter +// +// brlaser is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 2 of the License, or +// (at your option) any later version. +// +// brlaser is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with brlaser. If not, see . + +#include "lest.hpp" +#include "tempfile.h" +#include "../src/job.h" + +const lest::test specification[] = { + "An empty job produces no output", + [] { + tempfile f; + { + job j(f.file(), "name"); + } + EXPECT(f.data().empty()); + }, +}; + +int main() { + return lest::run(specification); +}