summaryrefslogtreecommitdiff
path: root/support/dktools/wprclean.c
blob: b2bb5a2f5c75d64f8914be1e9222cb070f23e7d2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
/*
	WARNING: This file was generated by dkct.
	Changes you make here will be lost if dkct is run again!
	You should modify the original source and run dkct on it.
	Original source: wprclean.ctr
*/

/*
Copyright (C) 2013-2017, Dirk Krause

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above copyright notice,
  this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above opyright notice,
  this list of conditions and the following disclaimer in the documentation
  and/or other materials provided with the distribution.
* Neither the name of the author nor the names of contributors may be used
  to endorse or promote products derived from this software without specific
  prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS''
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED.
IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

/**	@file wprclean.c The wprclean module.
*/


#line 8 "wprclean.ctr"

#include "dk3all.h"
#include "dk3print.h"
#include "dk3prcfg.h"
#if 0
#include "dkt-version.h"
#endif
#include "dk4vers.h"





#line 20 "wprclean.ctr"



/**	Job structure.
*/
typedef struct {
  dk3_app_t		*app;	/**< Application structure. */
  dkChar const * const	*msg;	/**< Localized texts. */
  dkChar const * const	*nlmsg;	/**< Not localized texts. */
  dk3_option_set_t	*opt;	/**< Options. */
  dk3_print_conf_t	*pc;	/**< Print configuration. */
  int			 cmd;	/**< Command to execute. */
  int			 comp;	/**< Only jobs from this machine. */
  int			 exval;	/**< Exit status code. */
} WPC_JOB;



#if DK3_ON_WINDOWS

/**	Information required to delete a print job.
*/
typedef struct {
  DWORD			position;	/**< Position in queue. */
  DWORD			jobid;		/**< Job ID. */
} wprclean_printjob_t;

#endif



#ifndef WPRCLEAN_QUEUE_JOB_LIST_SIZE
/**	Buffer size for job list.
*/
#define	WPRCLEAN_QUEUE_JOB_LIST_SIZE	4096
#endif



#ifndef	WPRCLEAN_NUM_JOBS
/**	Number of jobs to retrieve at once.
*/
#define	WPRCLEAN_NUM_JOBS		64
#endif



/**	Texts used by the program, not localized.
*/
static dkChar const * const	wprclean_nlmsg[] = {
/* 0 */
dkT("dktools"),

/* 1 */
dkT("wprclean.str"),

/* 2 */
dkT("wprclean.txt"),

NULL


#line 84 "wprclean.ctr"
};



/**	Texts used by the program, replaced by localized versions.
*/
static dkChar const * const	wprclean_msg[] = {
/* 0 */
dkT("Print job "),

/* 1 */
dkT(" deleted."),

/* 2 */
dkT("Failed to delete print job "),

/* 3 */
dkT(", reason: "),

/* 4 */
dkT("."),

/* 5 */
dkT("Failed to open printer \""),

/* 6 */
dkT("\"!"),

/* 7 */
dkT("Printer \""),

/* 8 */
dkT("\" not found!"),

NULL


#line 114 "wprclean.ctr"
};



/**	Options for the wprclean program.
*/
static dk3_option_t const	wprclean_options[] = {
  { dkT('h'), dkT("help"), 0 },
  { dkT('v'), dkT("version"), 0 },
  { dkT('L'), dkT("license-terms"), 0 },
  { dkT('c'), dkT("computer"), 0}
};


/**	Number of options in wprclean_options.
*/
static size_t const	wprclean_sz_options =
sizeof(wprclean_options)/sizeof(dk3_option_t);



/**	Default help text shown if help file not found.
*/
static dkChar const * const	wprclean_text_help[] = {
dkT(""),
dkT("NAME"),
dkT(""),
dkT("  wprclean - Windows print cleanup"),
dkT(""),
dkT("SYNOPSIS"),
dkT(""),
dkT("  wprclean [-c] [<printers>]"),
dkT("  wprclean [-h] [-v] [-L]"),
dkT(""),
dkT("DESCRIPTION"),
dkT(""),
dkT("The wprclean program deletes all print jobs created by the current user."),
dkT("It is intended for use in computer labs or PC classrooms in schools and"),
dkT("universities."),
dkT(""),
dkT("OPTIONS"),
dkT(""),
dkT("-c"),
dkT("--computer"),
dkT("\tRemove only print jobs created on the current computer."),
dkT(""),
dkT("-h"),
dkT("--help"),
dkT("\tShow this help text."),
dkT(""),
dkT("-v"),
dkT("--version"),
dkT("\tShow version information."),
dkT(""),
dkT("-L"),
dkT("--license-terms"),
dkT("\tShow the license terms."),
dkT(""),
dkT("EXIT STATUS"),
dkT(""),
dkT("0 on success, all other exit status codes indicate an error."),
dkT(""),
dkT("FILES"),
dkT(""),
dkT("The dk3print.conf file can be used to conigure short alias names for"),
dkT("Windows print queues."),
dkT(""),
dkT("AUTHOR"),
dkT(""),
dkT("Dirk Krause"),
dkT(""),
dkT("COPYRIGHT AND LICENSE"),
dkT(""),
dkT("Run"),
dkT("  wprclean --license"),
dkT("to see the license conditions."),
dkT(""),
dkT("SEE ALSO"),
dkT(""),
dkT("http://dktools.sourceforge.net"),
dkT(""),
NULL


#line 197 "wprclean.ctr"
};



/**	License conditions.
*/
static dkChar const * const	wprclean_text_license[] = {
dkT("Copyright (c) 2013-2016, Dirk Krause"),
dkT("All rights reserved."),
dkT(""),
dkT("Redistribution and use in source and binary forms, with or without"),
dkT("modification, are permitted provided that the following conditions are met:"),
dkT(""),
dkT("* Redistributions of source code must retain the above copyright notice,"),
dkT("  this list of conditions and the following disclaimer."),
dkT("* Redistributions in binary form must reproduce the above copyright"),
dkT("  notice, this list of conditions and the following disclaimer in the"),
dkT("  documentation and/or other materials provided with the distribution."),
dkT("* Neither the name of the copyright holder(s) nor the names of"),
dkT("  contributors may be used to endorse or promote products derived from "),
dkT("  this software without specific prior written permission."),
dkT(""),
dkT("THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS"),
dkT("\"AS IS\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT"),
dkT("LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR"),
dkT("A PARTICULAR PURPOSE ARE DISCLAIMED."),
dkT(""),
dkT("IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY"),
dkT("DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL"),
dkT("DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS"),
dkT("OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)"),
dkT("HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,"),
dkT("STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING"),
dkT("IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE"),
dkT("POSSIBILITY OF SUCH DAMAGE."),
dkT(""),
NULL


#line 235 "wprclean.ctr"
};



/**	Version information.
*/
static dkChar const wprclean_version[] = { DKT_VERSION_DK };


#if DK3_ON_WINDOWS


/**	Delete print job information.
	@param	jptr	Print job to delete.
*/
static
void
wprclean_printjob_delete(wprclean_printjob_t *jptr)
{
  dk3_release(jptr);
}



static
wprclean_printjob_t *
wprclean_printjob_new(DWORD pos, DWORD jid, dk3_app_t *app)
{
  wprclean_printjob_t	*back = NULL;
  back = dk3_new_app(wprclean_printjob_t,1,app);
  if(back) {
    back->position = pos;
    back->jobid = jid;
  }
  return  back;
}



/**	Compare print jobs by position, largest number first.
	@param	l	Left job.
	@param	r	Right job.
	@param	cr	Comparison criteria (ignored).
	@return	Comparison result.
*/
static
int
wprclean_compare_printjobs(void const *l, void const *r, int cr)
{
  wprclean_printjob_t const	*pl;	/* Left print job. */
  wprclean_printjob_t const	*pr;	/* Right print job. */
  int			 	 back = 0;
  if(l) {
    if(r) {
      pl = (wprclean_printjob_t const *)l;
      pr = (wprclean_printjob_t const *)r;
      if(pl->position > pr->position) {
        back = 1;
      } else {
        if(pl->position < pr->position) {
	  back = -1;
	}
      }
    } else {
      back = 1;
    }
  } else {
    if(r) {
      back = -1;
    }
  }

  /*	Invert.
  */
  switch(back) {
    case 1: {
      back = -1;
    } break;
    case -1: {
      back = 1;
    } break;
  }
  return back;
}


#endif
/* DK3_ON_WINDOWS */




/**	Delete the current users jobs from the printer.
	@param	job	Job structure.
	@param	pr	Printer.
*/
static
void
wprclean_windows_printer(WPC_JOB *job, dk3_printer_t *pr)
{
#if DK3_ON_WINDOWS
  dkChar		 cn[DK3_MAX_PATH];	/* Current computer name. */
  dkChar		 nb1[64];		/* Job ID as text. */
  dkChar		 nb2[64];		/* Last error as text. */
  char			 peb[WPRCLEAN_QUEUE_JOB_LIST_SIZE];
  			 /* Buffer for enumeration. */
#if DK3_CHAR_SIZE > 1
  JOB_INFO_1W		*jobptr;	/* Current print job. */
#else
  JOB_INFO_1A		*jobptr;	/* Current print job. */
#endif
  wprclean_printjob_t	*pj;		/* Current job to mark for deletion. */
  dk3_sto_t		*sJobs;		/* Storage for jobs to delete. */
  dk3_sto_it_t		*iJobs;		/* Iterator through storage. */
  wprclean_printjob_t	*jptr;		/* Current job to delete. */
  dkChar const		*logname;	/* Current users login name. */
  HANDLE		 hPr;		/* Handle for printer. */
  DWORD			 szpeb;		/* Size of peb buffer. */
  DWORD			 cbNeeded;	/* Number of bytes needed. */
  DWORD			 cbReturned;	/* Number of print jobs returned. */
  DWORD			 startindex;	/* Start index for enumeration. */
  DWORD			 i;		/* Index of current job. */
  DWORD			 le;		/* Last error code. */
  BOOL			 bres;		/* Operation result. */
  BOOL			 cc;		/* Flag: Can continue. */
  BOOL			 mustsave;	/* Flag: Must mark job for deletion. */
  

#line 362 "wprclean.ctr"
  logname = dk3app_get_logname(job->app);
  if(logname) {							

#line 364 "wprclean.ctr"
  if(dk3sf_get_hostname_app(cn,DK3_MAX_PATH,job->app)) {	

#line 365 "wprclean.ctr"
    sJobs = dk3sto_open_app(job->app);
    if(sJobs) {							

#line 367 "wprclean.ctr"
      dk3sto_set_comp(sJobs, wprclean_compare_printjobs, 0);
      iJobs = dk3sto_it_open(sJobs);
      if(iJobs) {						

#line 370 "wprclean.ctr"
        hPr = INVALID_HANDLE_VALUE;
#if DK3_CHAR_SIZE > 1
	bres = OpenPrinterW((dkChar *)(pr->name), &hPr, NULL);
#else
	bres = OpenPrinterA((dkChar *)(pr->name), &hPr, NULL);
#endif
	if(bres) {					

#line 377 "wprclean.ctr"
	  dk3sf_initialize_stdout();
	  dk3sf_fputs(pr->name, stdout);
	  dk3sf_fputc(dkT('\n'), stdout);
	  fflush(stdout);
          /*	Enumerate print jobs.
	  */
	  cc = TRUE;
	  startindex = 0;
	  while(cc) {
	    cbNeeded = 0; cbReturned = 0; szpeb = sizeof(peb);
#if DK3_CHAR_SIZE > 1
	    bres = EnumJobsW(
	      hPr, startindex, (DWORD)WPRCLEAN_NUM_JOBS, 1,
	      (LPBYTE)peb, szpeb, &cbNeeded, &cbReturned
	    );
#else
	    bres = EnumJobsA(
	      hPr, startindex, (DWORD)WPRCLEAN_NUM_JOBS, 1,
	      (LPBYTE)peb, szpeb, &cbNeeded, &cbReturned
	    );
#endif
	    if(bres) {					

#line 399 "wprclean.ctr"
	      if(cbReturned > 0) {			

#line 400 "wprclean.ctr"
#if DK3_CHAR_SIZE > 1
		jobptr = (JOB_INFO_1W *)peb;
#else
		jobptr = (JOB_INFO_1A *)peb;
#endif
		for(i = 0; i < cbReturned; i++) {
		  mustsave = FALSE;
		  if(jobptr->pUserName) {		

#line 408 "wprclean.ctr"
		    if(dk3str_casecmp(logname, jobptr->pUserName) == 0) {
		      

#line 410 "wprclean.ctr"
		      if(job->comp) {
		        if(jobptr->pMachineName) {
			  

#line 413 "wprclean.ctr"
		          if(dk3str_casecmp(cn, jobptr->pMachineName) == 0) {
			    

#line 415 "wprclean.ctr"
			    mustsave = TRUE;
			  } else {
			    if(dkT('\\') == (jobptr->pMachineName)[0]) {
			      if(dkT('\\') == (jobptr->pMachineName)[1]) {
			        if(dk3str_casecmp(cn, &((jobptr->pMachineName)[2])) == 0) {
			          mustsave = TRUE;
			        }
			      }
			    }
			  }
		        } else {		

#line 426 "wprclean.ctr"
		          mustsave = TRUE;
		        }
		      } else {
		        mustsave = TRUE;
		      }
		    }
		  }
		  if(mustsave) {			

#line 434 "wprclean.ctr"
		    pj = wprclean_printjob_new(
		      jobptr->Position, jobptr->JobId, job->app
		    );
		    if(pj) {				

#line 438 "wprclean.ctr"
		      if(!dk3sto_add(sJobs, (void *)pj)) {
		        wprclean_printjob_delete(pj);	

#line 440 "wprclean.ctr"
			job->exval = 1;
		      }
		    } else {				

#line 443 "wprclean.ctr"
		      job->exval = 1;
		    }
		  }
		  jobptr++;
		}
		startindex += cbReturned;
	      } else {				

#line 450 "wprclean.ctr"
	        cc = FALSE;
	      }
	    } else {				

#line 453 "wprclean.ctr"
	      cc = FALSE;
	    }
	  }

	  /*	Remove saved print jobs from queue.
	  */
	  dk3sto_it_reset(iJobs);
	  while(NULL != (jptr = (wprclean_printjob_t *)dk3sto_it_next(iJobs))) {
	    

#line 462 "wprclean.ctr"
#if VERSION_BEFORE_20140716
	    dk3sf_sprintf3(nb1,dkT("%lu"),(unsigned long)(jptr->jobid));
#else
	    if (!(dk3ma_um_to_string(nb1,DK3_SIZEOF(nb1,dkChar),(dk3_um_t)(jptr->jobid)))) {
	      nb1[0] = dkT('\0');
	    }
#endif
#if DK3_CHAR_SIZE > 1
	    bres = SetJobW(hPr, jptr->jobid, 0, NULL, JOB_CONTROL_DELETE);
#else
	    bres = SetJobA(hPr, jptr->jobid, 0, NULL, JOB_CONTROL_DELETE);
#endif
	    if(bres) {
	      /* DELETED */
	      dk3sf_fputc(dkT('\t'), stdout);
	      dk3sf_fputs((job->msg)[0], stdout);
	      dk3sf_fputs(nb1, stdout);
	      dk3sf_fputs((job->msg)[1], stdout);
	      dk3sf_fputc(dkT('\n'), stdout);
	      fflush(stdout);
	    } else {
	      /* FAILED */
	      le = GetLastError();
	      dk3sf_fputc(dkT('\t'), stdout);
#if VERSION_BEFORE_20140716
	      dk3sf_sprintf3(nb2,dkT("%lu"),(unsigned long)le);
#else
	      if(!(dk3ma_um_to_string(nb2,DK3_SIZEOF(nb2,dkChar),(dk3_um_t)le)))
	      {
	        nb2[0] = dkT('\0');
	      }
#endif
	      dk3sf_fputs((job->msg)[2], stdout);
	      dk3sf_fputs(nb1, stdout);
	      dk3sf_fputs((job->msg)[3], stdout);
	      dk3sf_fputs(nb2, stdout);
	      dk3sf_fputs((job->msg)[4], stdout);
	      dk3sf_fputc(dkT('\n'), stdout);
	      fflush(stdout);
	      job->exval = 1;
	    }
	  }

	  ClosePrinter(hPr);
	} else {				

#line 507 "wprclean.ctr"
	  /* ERROR: Failed to open printer! */
	  dk3app_log_3(job->app, DK3_LL_ERROR, job->msg, 5, 6, pr->name);
	  job->exval = 1;
	}

	/*	Release saved jobs.
	*/
        dk3sto_it_reset(iJobs);
	while(NULL != (jptr = (wprclean_printjob_t *)dk3sto_it_next(iJobs))) {
	  wprclean_printjob_delete(jptr);
	}
        dk3sto_it_close(iJobs);
      } else {
        job->exval = 1;
      }
      dk3sto_close(sJobs);
    } else {
      job->exval = 1;
    }
    } else {
      /* ERROR: Failed to find host name */
      job->exval = 1;
    }
  } else {
    /* ERROR: Failed to find log name */
    job->exval = 1;
  } 

#line 534 "wprclean.ctr"
#endif
}



/**	Clean up all printers on the system.
	@param	job	Job structure.
*/
static
void
wprclean_cleanup_all_printers(WPC_JOB *job)
{
  void		*vptr;	/* Current printer as returned from iterator. */
  dk3_printer_t	*pr;	/* Current printer. */

  dk3sto_it_reset((job->pc)->iPrinters);
  while(NULL != (vptr = dk3sto_it_next((job->pc)->iPrinters))) {
    pr = (dk3_printer_t *)vptr;
    if(DK3_PRINTER_TYPE_WINDOWS == pr->t_p) {
      wprclean_windows_printer(job, pr);
    }
  }
}



/**	Clean up the named printers.
	@param	job	Job structure.
	@param	numargs	Number of command line arguments.
*/
static
void
wprclean_cleanup_named_printers(WPC_JOB *job, int numargs)
{
  dkChar const		*pn;		/* Printer name. */
  dk3_printer_t		*pr;		/* Printer. */
  int			 i;		/* Index of current printer. */

  /*	Pass 1:	Local Windows printers.
  */
  for(i = 0; i < numargs; i++) {
    pn = dk3opt_get_arg(job->opt, i);
    if(pn) {
      pr = dk3print_get_printer(job->pc, pn);
      if(pr) {
        if(DK3_PRINTER_TYPE_WINDOWS == pr->t_p) {
          wprclean_windows_printer(job, pr);
	}
      } else {
        job->exval = 1;
        /* ERROR: Printer not found! */
	dk3app_log_3(job->app, DK3_LL_ERROR, job->msg, 7, 8, pn);
      }
    }
  }
}



/**	Do cleanup.
	@param	job	Job structure.
*/
static
void
wprclean_do_cleanup(WPC_JOB *job)
{
  int		numargs;		/* Number of command line arguments. */

  job->pc = dk3print_conf_open(job->app, 1);
  if(job->pc) {
    numargs = dk3opt_get_num_args(job->opt);
    job->exval = 0;
    if(0 == numargs) {
      wprclean_cleanup_all_printers(job);
    } else {
      wprclean_cleanup_named_printers(job, numargs);
    }
    dk3print_conf_close(job->pc); job->pc = NULL;
  }
}



/**	Run with a configured job structure.
	@param	job	Job structure.
*/
static
void
wprclean_run(WPC_JOB *job)
{
  job->opt = dk3opt_open_from_app(
    wprclean_options, wprclean_sz_options,
    dkT('\0'), NULL,
    job->app
  );
  if(job->opt) {
    if(0 == dk3opt_get_error_code(job->opt)) {
      if(dk3opt_is_set(job->opt, dkT('h'))) {
        job->cmd |= DK3_APP_CMD_HELP;
      }
      if(dk3opt_is_set(job->opt, dkT('v'))) {
        job->cmd |= DK3_APP_CMD_VERSION;
      }
      if(dk3opt_is_set(job->opt, dkT('L'))) {
        job->cmd |= DK3_APP_CMD_LICENSE;
      }
      if(dk3opt_is_set(job->opt, dkT('c'))) {
        job->comp = 1;
      }
      if(0 == job->cmd) {
        wprclean_do_cleanup(job);
      } else {
        job->exval = 0;
	dk3sf_initialize_stdout();
	if((job->cmd) & DK3_APP_CMD_VERSION) {
	  dk3sf_fputs(wprclean_version, stdout);
	  dk3sf_fputc(dkT('\n'), stdout);
	}
	if((job->cmd) & DK3_APP_CMD_LICENSE) {
	  dk3sf_fputt(wprclean_text_license, stdout);
	}
	if((job->cmd) & DK3_APP_CMD_HELP) {
	  dk3app_help(job->app, wprclean_nlmsg[2], wprclean_text_help);
	}
      }
    }
    dk3opt_close(job->opt); job->opt = NULL;
  }
}



/**	Program entry point.
	@param	argc	Number of command line arguments.
	@param	argv	Command line arguments array.
	@return	0 on success, any other value indicates an error.
*/
DK3_MAIN
{
  WPC_JOB	job;		/* wprclean job. */
  int		exval = 1;	/* Exit status code. */
  

#line 676 "wprclean.ctr"
  

#line 677 "wprclean.ctr"
  job.app = dk3app_open_command(
    argc, (dkChar const * const *)argv, wprclean_nlmsg[0]
  );
  if(job.app) {
    job.msg = dk3app_messages(
      job.app, wprclean_nlmsg[1], (dkChar const **)wprclean_msg
    );
    job.nlmsg = wprclean_nlmsg;
    job.cmd = 0;
    job.comp = 0;
    job.exval = 1;
    job.pc = NULL;
    wprclean_run(&job);
    exval = job.exval;
    dk3app_close(job.app);
  } else {
    fputs("wprclean: ERROR: Not enough memory!\n", stderr);
    fflush(stderr);
  }
  

#line 697 "wprclean.ctr"
  

#line 698 "wprclean.ctr"
  fflush(stdout);
  exit(exval); return exval;
}