Skip to content

Commit 64a410a

Browse files
committed
cups-browsed, implicitclass, driverless, libcupsfilters: General white space, indentation, and compiler warning clean-up after addition of printer auto-selection.
1 parent 9e7f88b commit 64a410a

File tree

7 files changed

+5824
-5487
lines changed

7 files changed

+5824
-5487
lines changed

backend/implicitclass.c

+125-134
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,7 @@
3737
the current job */
3838
#define CUPS_BROWSED_DEST_PRINTER "cups-browsed-dest-printer"
3939

40-
static int job_canceled = 0;
41-
/* Set to 1 on SIGTERM */
40+
static int job_canceled = 0; /* Set to 1 on SIGTERM */
4241

4342
/*
4443
* Local functions... */
@@ -64,13 +63,10 @@ next_delay(int current, /* I - Current delay value or 0 */
6463
int *previous) /* IO - Previous delay value */
6564
{
6665
int next; /* Next delay value */
67-
if (current > 0)
68-
{
66+
if (current > 0) {
6967
next = (current + *previous) % 12;
7068
*previous = next < current ? 0 : current;
71-
}
72-
else
73-
{
69+
} else {
7470
next = 1;
7571
*previous = 0;
7672
}
@@ -82,22 +78,20 @@ next_delay(int current, /* I - Current delay value or 0 */
8278
*/
8379

8480
void /* O - 0 on success, 1 on error */
85-
set_option_in_str(char *buf, /* I - Buffer with option list string */
86-
int buflen, /* I - Length of buffer */
87-
const char *option, /* I - Option to change/add */
88-
const char *value) /* I - New value for option, NULL
89-
removes option */
81+
set_option_in_str(char *buf, /* I - Buffer with option list string */
82+
int buflen, /* I - Length of buffer */
83+
const char *option, /* I - Option to change/add */
84+
const char *value) /* I - New value for option, NULL
85+
removes option */
9086
{
9187
char *p1, *p2;
9288

9389
if (!buf || buflen == 0 || !option)
9490
return;
9591
/* Remove any occurrence of option in the string */
9692
p1 = buf;
97-
while (*p1 != '\0' && (p2 = strcasestr(p1, option)) != NULL)
98-
{
99-
if (p2 > buf && *(p2 - 1) != ' ' && *(p2 - 1) != '\t')
100-
{
93+
while (*p1 != '\0' && (p2 = strcasestr(p1, option)) != NULL) {
94+
if (p2 > buf && *(p2 - 1) != ' ' && *(p2 - 1) != '\t') {
10195
p1 = p2 + 1;
10296
continue;
10397
}
@@ -141,10 +135,10 @@ main(int argc, /* I - Number of command-line args */
141135
char dest_host[1024]; /* Destination host */
142136
ipp_t *request, *response;
143137
ipp_attribute_t *attr;
144-
int bytes; /* Bytes copied */
138+
int bytes; /* Bytes copied */
145139
char uri[HTTP_MAX_URI];
146140
char *argv_nt[7];
147-
int outbuflen,filter_pid,filefd,exit_status;
141+
int outbuflen,filefd,exit_status;
148142
static const char *pattrs[] =
149143
{
150144
"printer-defaults"
@@ -175,10 +169,8 @@ main(int argc, /* I - Number of command-line args */
175169
* Check command-line...
176170
*/
177171

178-
if (argc >= 6)
179-
{
180-
if ((device_uri = getenv("DEVICE_URI")) == NULL)
181-
{
172+
if (argc >= 6) {
173+
if ((device_uri = getenv("DEVICE_URI")) == NULL) {
182174
if (!argv || !argv[0] || !strchr(argv[0], ':'))
183175
return (-1);
184176

@@ -235,7 +227,8 @@ main(int argc, /* I - Number of command-line args */
235227
if (ptr1 != NULL)
236228
break;
237229
}
238-
fprintf(stderr, "DEBUG: Read " CUPS_BROWSED_DEST_PRINTER " option: %s\n", ptr1);
230+
fprintf(stderr, "DEBUG: Read " CUPS_BROWSED_DEST_PRINTER " option: %s\n",
231+
ptr1);
239232
if (ptr1 == NULL)
240233
goto failed;
241234
/* Destination host is between double quotes, as double quotes are
@@ -289,14 +282,9 @@ main(int argc, /* I - Number of command-line args */
289282
char buffer[8192];
290283

291284
fprintf(stderr, "DEBUG: Received destination host name from cups-browsed: printer-uri %s\n",
292-
ptr1);
285+
ptr1);
293286

294-
/* Instead of feeding the job into the IPP backend, we re-print it into
295-
the server's CUPS queue. This way the job gets spooled on the server
296-
and we are not blocked until the job is printed. So a subsequent job
297-
will be immediately processed and sent out to another server */
298-
299-
/* Parse the command line option and prepare them for the new print
287+
/* Parse the command line options and prepare them for the new print
300288
job */
301289
cupsSetUser(argv[2]);
302290
title = argv[3];
@@ -317,114 +305,118 @@ main(int argc, /* I - Number of command-line args */
317305
else
318306
fd = 0; /* stdin */
319307

320-
/* Finding the document format in which the pdftoippprinter will convert the pdf
321-
file*/
322-
if ((ptr3 = strchr(ptr1, ' ')) != NULL) {
323-
*ptr3='\0';
324-
ptr3++;
325-
}
308+
/* Finding the document format in which the pdftoippprinter will
309+
convert the pdf file */
310+
if ((ptr3 = strchr(ptr1, ' ')) != NULL) {
311+
*ptr3='\0';
312+
ptr3++;
313+
}
326314

327-
/* Finding the resolution requested for the job*/
328-
if ((ptr4 = strchr(ptr3, ' ')) != NULL) {
329-
*ptr4='\0';
330-
ptr4++;
331-
}
315+
/* Finding the resolution requested for the job*/
316+
if ((ptr4 = strchr(ptr3, ' ')) != NULL) {
317+
*ptr4='\0';
318+
ptr4++;
319+
}
332320

333-
strncpy(printer_uri,ptr1,sizeof(printer_uri));
334-
strncpy(document_format,ptr3,sizeof(document_format));
335-
strncpy(resolution,ptr4,sizeof(resolution));
336-
337-
fprintf(stderr,"DEBUG: Recieved job for the printer with the destination uri - %s, Final-document format for the printer - %s and requested resolution - %s\n",
338-
printer_uri,document_format,resolution);
339-
340-
/* We need to send modified arguments to the IPP backend */
341-
if (argc == 6){
342-
/** Copy stdin to a temp file...*/
343-
if ((fd = cupsTempFd(tempfile, sizeof(tempfile))) < 0){
344-
fprintf(stderr,"Debug: Can't Read PDF file.\n");
345-
return CUPS_BACKEND_FAILED;
346-
}
347-
fprintf(stderr, "Debug: implicitclass - copying to temp print file \"%s\"\n",
348-
tempfile);
349-
while ((bytes = fread(buffer, 1, sizeof(buffer), stdin)) > 0)
350-
bytes = write(fd, buffer, bytes);
351-
close(fd);
352-
filename = tempfile;
353-
}
354-
else{
355-
/** Use the filename on the command-line...*/
356-
filename = argv[6];
357-
tempfile[0] = '\0';
321+
strncpy(printer_uri, ptr1, sizeof(printer_uri));
322+
strncpy(document_format, ptr3, sizeof(document_format));
323+
strncpy(resolution, ptr4, sizeof(resolution));
324+
325+
fprintf(stderr,"DEBUG: Received job for the printer with the destination uri - %s, Final-document format for the printer - %s and requested resolution - %s\n",
326+
printer_uri, document_format, resolution);
327+
328+
/* We need to send modified arguments to the IPP backend */
329+
if (argc == 6) {
330+
/* Copy stdin to a temp file...*/
331+
if ((fd = cupsTempFd(tempfile, sizeof(tempfile))) < 0){
332+
fprintf(stderr,"Debug: Can't Read PDF file.\n");
333+
return CUPS_BACKEND_FAILED;
358334
}
335+
fprintf(stderr, "Debug: implicitclass - copying to temp print file \"%s\"\n",
336+
tempfile);
337+
while ((bytes = fread(buffer, 1, sizeof(buffer), stdin)) > 0)
338+
bytes = write(fd, buffer, bytes);
339+
close(fd);
340+
filename = tempfile;
341+
} else{
342+
/** Use the filename on the command-line...*/
343+
filename = argv[6];
344+
tempfile[0] = '\0';
345+
}
359346

360-
/*Copying the argument to a new char** which will be sent to the filter and
361-
the ipp backend*/
362-
for (i = 0; i < 5; i++)
363-
argv_nt[i] = argv[i];
364-
365-
/* Few new options will be added to the argv[5]*/
366-
outbuflen = strlen(argv[5]) + 256;
367-
argv_nt[5] = calloc(outbuflen, sizeof(char));
368-
strcpy(argv_nt[5], (const char*)argv[5]);
369-
370-
/* Filter pdftoippprinter.c will read the input from this file*/
371-
argv_nt[6] = filename;
372-
373-
set_option_in_str(argv_nt[5], outbuflen, "output-format", document_format);
374-
set_option_in_str(argv_nt[5], outbuflen, "Resolution",resolution);
375-
setenv("DEVICE_URI",printer_uri, 1);
376-
377-
filefd = cupsTempFd(tempfile_filter, sizeof(tempfile_filter));
378-
/* The output of the last filter in pdftoippprinter will be written to
379-
this file. We could have sent the output directly to the backend, but having
380-
this temperory file will help us find whether the filter worked correctly and
381-
what was the document-format of the filtered output.*/
382-
close(1);
383-
dup(filefd);
384-
/* Calling pdftoippprinter.c filter*/
385-
apply_filters(7,argv_nt);
386-
387-
close(filefd);
388-
389-
/*We will send the filtered output of the pdftoippprinter.c to the IPP Backend*/
390-
argv_nt[6] = tempfile_filter;
391-
fprintf(stderr, "DEBUG: The filtered output of pdftoippprinter is written to file %s\n",
392-
tempfile_filter);
393-
394-
/*Setting the final content type to the best pdl supported by the printer.*/
395-
if(!strcmp(document_format,"pdf"))
396-
setenv("FINAL_CONTENT_TYPE", "application/pdf", 1);
397-
else if(!strcmp(document_format,"raster"))
398-
setenv("FINAL_CONTENT_TYPE", "image/pwg-raster", 1);
399-
else if(!strcmp(document_format,"apple-raster"))
400-
setenv("FINAL_CONTENT_TYPE", "image/urf", 1);
401-
else if(!strcmp(document_format,"pclm"))
402-
setenv("FINAL_CONTENT_TYPE", "application/PCLm", 1);
403-
else if(!strcmp(document_format,"pclxl"))
404-
setenv("FINAL_CONTENT_TYPE", "application/vnd.hp-pclxl", 1);
405-
else if(!strcmp(document_format,"pcl"))
406-
setenv("FINAL_CONTENT_TYPE", "application/pcl", 1);
407-
408-
ippDelete(response);
409-
/* The implicitclass backend will send the job directly to the ipp backend*/
410-
pid_t pid = fork();
411-
if ( pid == 0 ) {
412-
fprintf(stderr, "DEBUG: Started IPP Backend with pid: %d\n",getpid());
413-
execv("/usr/lib/cups/backend/ipp",argv_nt);
414-
}else{
415-
int status;
416-
waitpid(pid, &status, 0);
417-
if(WIFEXITED(status)){
418-
exit_status = WEXITSTATUS(status);
419-
fprintf(stderr, "DEBUG: The IPP Backend exited with the status %d\n",
420-
exit_status);
421-
}
422-
return exit_status;
423-
}
424-
}
347+
/* Copying the argument to a new char** which will be sent to the filter
348+
and the ipp backend*/
349+
for (i = 0; i < 5; i++)
350+
argv_nt[i] = argv[i];
351+
352+
/* Few new options will be added to the argv[5]*/
353+
outbuflen = strlen(argv[5]) + 256;
354+
argv_nt[5] = calloc(outbuflen, sizeof(char));
355+
strcpy(argv_nt[5], (const char*)argv[5]);
356+
357+
/* Filter pdftoippprinter.c will read the input from this file*/
358+
argv_nt[6] = filename;
359+
set_option_in_str(argv_nt[5], outbuflen, "output-format",
360+
document_format);
361+
set_option_in_str(argv_nt[5], outbuflen, "Resolution",resolution);
362+
setenv("DEVICE_URI",printer_uri, 1);
363+
364+
filefd = cupsTempFd(tempfile_filter, sizeof(tempfile_filter));
365+
366+
/* The output of the last filter in pdftoippprinter will be
367+
written to this file. We could have sent the output directly
368+
to the backend, but having this temperory file will help us
369+
find whether the filter worked correctly and what was the
370+
document-format of the filtered output.*/
371+
close(1);
372+
/*dup(filefd);*/
373+
374+
/* Calling pdftoippprinter.c filter*/
375+
apply_filters(7,argv_nt);
376+
377+
close(filefd);
378+
379+
/* We will send the filtered output of the pdftoippprinter.c to
380+
the IPP Backend*/
381+
argv_nt[6] = tempfile_filter;
382+
fprintf(stderr, "DEBUG: The filtered output of pdftoippprinter is written to file %s\n",
383+
tempfile_filter);
384+
385+
/* Setting the final content type to the best pdl supported by
386+
the printer.*/
387+
if(!strcmp(document_format,"pdf"))
388+
setenv("FINAL_CONTENT_TYPE", "application/pdf", 1);
389+
else if(!strcmp(document_format,"raster"))
390+
setenv("FINAL_CONTENT_TYPE", "image/pwg-raster", 1);
391+
else if(!strcmp(document_format,"apple-raster"))
392+
setenv("FINAL_CONTENT_TYPE", "image/urf", 1);
393+
else if(!strcmp(document_format,"pclm"))
394+
setenv("FINAL_CONTENT_TYPE", "application/PCLm", 1);
395+
else if(!strcmp(document_format,"pclxl"))
396+
setenv("FINAL_CONTENT_TYPE", "application/vnd.hp-pclxl", 1);
397+
else if(!strcmp(document_format,"pcl"))
398+
setenv("FINAL_CONTENT_TYPE", "application/pcl", 1);
399+
400+
ippDelete(response);
401+
402+
/* The implicitclass backend will send the job directly to the
403+
ipp backend*/
404+
pid_t pid = fork();
405+
if ( pid == 0 ) {
406+
fprintf(stderr, "DEBUG: Started IPP Backend with pid: %d\n",getpid());
407+
execv("/usr/lib/cups/backend/ipp",argv_nt);
408+
} else {
409+
int status;
410+
waitpid(pid, &status, 0);
411+
if (WIFEXITED(status)) {
412+
exit_status = WEXITSTATUS(status);
413+
fprintf(stderr, "DEBUG: The IPP Backend exited with the status %d\n",
414+
exit_status);
425415
}
426-
else if (argc != 1)
427-
{
416+
return exit_status;
417+
}
418+
}
419+
} else if (argc != 1) {
428420
fprintf(stderr,
429421
"Usage: %s job-id user title copies options [file]",
430422
argv[0]);
@@ -453,4 +445,3 @@ sigterm_handler(int sig) /* I - Signal number (unused) */
453445
else
454446
job_canceled = 1;
455447
}
456-

0 commit comments

Comments
 (0)