2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * ------+---------+---------+---------+---------+---------+---------+---------*
5 * Copyright (c) 2002 - Garance Alistair Drosehn <gad@FreeBSD.org>.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * The views and conclusions contained in the software and documentation
30 * are those of the authors and should not be interpreted as representing
31 * official policies, either expressed or implied, of the FreeBSD Project
34 * ------+---------+---------+---------+---------+---------+---------+---------*
37 #include "lp.cdefs.h" /* A cross-platform version of <sys/cdefs.h> */
38 __FBSDID("$FreeBSD$");
41 * movejobs.c - The lpc commands which move jobs around.
45 #include <sys/param.h>
46 #include <sys/queue.h>
51 #include <dirent.h> /* just for MAXNAMLEN, for job_cfname in lp.h! */
59 #include "matchjobs.h"
62 /* Values for origcmd in tqbq_common() */
66 static int process_jobs(int _argc, char *_argv[], process_jqe
67 _process_rtn, void *myinfo);
68 static process_jqe touch_jqe;
69 static void tqbq_common(int _argc, char *_argv[], int _origcmd);
72 * isdigit is defined to work on an 'int', in the range 0 to 255, plus EOF.
73 * Define a wrapper which can take 'char', either signed or unsigned.
75 #define isdigitch(Anychar) isdigit(((int) Anychar) & 255)
77 struct touchjqe_info { /* for topq/bottomq */
82 static struct jobqueue **queue;
85 * Process all the jobs, as specified by the user.
88 process_jobs(int argc, char *argv[], process_jqe process_rtn, void *myinfo)
90 struct jobspec_hdr jobs_wanted;
91 int i, matchcnt, pjres;
93 STAILQ_INIT(&jobs_wanted);
94 for (i = 0; i < argc; i++) {
95 pjres = parse_jobspec(argv[i], &jobs_wanted);
97 printf("\tinvalid job specifier: %s\n", argv[i]);
101 matchcnt = scanq_jobspec(nitems, queue, SCQ_JSORDER, &jobs_wanted,
102 process_rtn, myinfo);
104 free_jobspec(&jobs_wanted);
109 * Reposition the job by changing the modification time of the
113 touch_jqe(void *myinfo, struct jobqueue *jq, struct jobspec *jspec)
115 struct timeval tvp[2];
116 struct touchjqe_info *touch_info;
120 * If the entire queue has been scanned for the current jobspec,
121 * then let the user know if there were no jobs matched by that
125 if (jspec->matchcnt == 0) {
126 format_jobspec(jspec, FMTJS_VERBOSE);
127 if (jspec->pluralfmt)
128 printf("\tjobs %s are not in the queue\n",
131 printf("\tjob %s is not in the queue\n",
138 * Do a little juggling with "matched" vs "processed", so a single
139 * job can be matched by multiple specifications, and yet it will
140 * be moved only once. This is so, eg, 'topq lp 7 7' will not
141 * complain "job 7 is not in queue" for the second specification.
144 if (jq->job_processed) {
145 printf("\tmoved %s earlier\n", jq->job_cfname);
148 jq->job_processed = 1;
151 tvp[0].tv_sec = tvp[1].tv_sec = ++touch_info->newtime;
152 tvp[0].tv_usec = tvp[1].tv_usec = 0;
154 ret = utimes(jq->job_cfname, tvp);
158 if (jspec->matcheduser)
159 printf("\tmoved %s (user %s)\n", jq->job_cfname,
162 printf("\tmoved %s\n", jq->job_cfname);
168 * Put the specified jobs at the bottom of printer queue.
171 bottomq_cmd(int argc, char *argv[])
175 printf("usage: bottomq printer [jobspec ...]\n");
178 --argc; /* First argv was the command name */
181 tqbq_common(argc, argv, IS_BOTQ);
185 * Put the specified jobs at the top of printer queue.
188 topq_cmd(int argc, char *argv[])
192 printf("usage: topq printer [jobspec ...]\n");
195 --argc; /* First argv was the command name */
198 tqbq_common(argc, argv, IS_TOPQ);
202 * Processing in common between topq and bottomq commands.
205 tqbq_common(int argc, char *argv[], int origcmd)
207 struct printer myprinter, *pp;
208 struct touchjqe_info touch_info;
209 int i, movecnt, setres;
211 pp = setup_myprinter(*argv, &myprinter, SUMP_CHDIR_SD);
214 --argc; /* Second argv was the printer name */
217 nitems = getq(pp, &queue);
219 printf("\tthere are no jobs in the queue\n");
225 * The only real difference between topq and bottomq is the
226 * initial value used for newtime.
231 * When moving jobs to the bottom of the queue, pick a
232 * starting value which is one second after the last job
235 touch_info.newtime = queue[nitems - 1]->job_time + 1;
239 * When moving jobs to the top of the queue, the greatest
240 * number of jobs which could be moved is all the jobs
241 * that are in the queue. Pick a starting value which
242 * leaves plenty of room for all existing jobs.
244 touch_info.newtime = queue[0]->job_time - nitems - 5;
247 printf("\ninternal error in topq/bottomq processing.\n");
251 movecnt = process_jobs(argc, argv, touch_jqe, &touch_info);
254 * If any jobs were moved, then chmod the lock file to notify any
255 * active process for this queue that the queue has changed, so
256 * it will rescan the queue to find out the new job order.
259 printf("\tqueue order unchanged\n");
261 setres = set_qstate(SQS_QCHANGED, pp->lock_file);
263 printf("\t* queue order changed for %s, but the\n"
264 "\t* attempt to set_qstate() failed [%d]!\n",
265 pp->printer, setres);
268 for (i = 0; i < nitems; i++)