2 * ------+---------+---------+---------+---------+---------+---------+---------*
3 * Copyright (c) 2002 - Garance Alistair Drosehn <gad@FreeBSD.org>.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * The views and conclusions contained in the software and documentation
28 * are those of the authors and should not be interpreted as representing
29 * official policies, either expressed or implied, of the FreeBSD Project
32 * ------+---------+---------+---------+---------+---------+---------+---------*
34 * $FreeBSD: src/usr.sbin/lpr/common_source/matchjobs.c,v 1.2.2.2 2002/08/15 18:53:17 schweikh Exp $
35 * $DragonFly: src/usr.sbin/lpr/common_source/matchjobs.c,v 1.2 2003/06/17 04:29:56 dillon Exp $
39 * movejobs.c - The lpc commands which move jobs around.
43 #include <sys/param.h>
44 #include <sys/queue.h>
47 #include <dirent.h> /* for MAXNAMLEN, for job_cfname in lp.h! */
57 #include "matchjobs.h"
59 #define DEBUG_PARSEJS 0 /* set to 1 when testing */
60 #define DEBUG_SCANJS 0 /* set to 1 when testing */
62 static int match_jobspec(struct jobqueue *_jq, struct jobspec *_jspec);
65 * isdigit is defined to work on an 'int', in the range 0 to 255, plus EOF.
66 * Define a wrapper which can take 'char', either signed or unsigned.
68 #define isdigitch(Anychar) isdigit(((int) Anychar) & 255)
71 * Format a single jobspec into a string fit for printing.
74 format_jobspec(struct jobspec *jspec, int fmt_wanted)
76 char rangestr[40], buildstr[200];
77 const char fromuser[] = "from user ";
78 const char fromhost[] = "from host ";
82 * If the struct already has a fmtstring, then release it
83 * before building a new one.
85 if (jspec->fmtoutput != NULL) {
86 free(jspec->fmtoutput);
87 jspec->fmtoutput = NULL;
90 jspec->pluralfmt = 1; /* assume a "plural result" */
92 if (jspec->startnum >= 0) {
93 if (jspec->startnum != jspec->endrange)
94 snprintf(rangestr, sizeof(rangestr), "%ld-%ld",
95 jspec->startnum, jspec->endrange);
98 snprintf(rangestr, sizeof(rangestr), "%ld",
103 strsize = sizeof(buildstr);
105 switch (fmt_wanted) {
107 /* Build everything but the hostname in a temp string. */
108 if (jspec->wanteduser != NULL)
109 strlcat(buildstr, jspec->wanteduser, strsize);
110 if (rangestr[0] != '\0') {
111 if (buildstr[0] != '\0')
112 strlcat(buildstr, ":", strsize);
113 strlcat(buildstr, rangestr, strsize);
115 if (jspec->wantedhost != NULL)
116 strlcat(buildstr, "@", strsize);
118 /* Get space for the final result, including hostname */
119 strsize = strlen(buildstr) + 1;
120 if (jspec->wantedhost != NULL)
121 strsize += strlen(jspec->wantedhost);
122 jspec->fmtoutput = malloc(strsize);
124 /* Put together the final result */
125 strlcpy(jspec->fmtoutput, buildstr, strsize);
126 if (jspec->wantedhost != NULL)
127 strlcat(jspec->fmtoutput, jspec->wantedhost, strsize);
132 /* Build everything but the hostname in a temp string. */
133 strlcat(buildstr, rangestr, strsize);
134 if (jspec->wanteduser != NULL) {
135 if (rangestr[0] != '\0')
136 strlcat(buildstr, " ", strsize);
137 strlcat(buildstr, fromuser, strsize);
138 strlcat(buildstr, jspec->wanteduser, strsize);
140 if (jspec->wantedhost != NULL) {
141 if (jspec->wanteduser == NULL) {
142 if (rangestr[0] != '\0')
143 strlcat(buildstr, " ", strsize);
144 strlcat(buildstr, fromhost, strsize);
146 strlcat(buildstr, "@", strsize);
149 /* Get space for the final result, including hostname */
150 strsize = strlen(buildstr) + 1;
151 if (jspec->wantedhost != NULL)
152 strsize += strlen(jspec->wantedhost);
153 jspec->fmtoutput = malloc(strsize);
155 /* Put together the final result */
156 strlcpy(jspec->fmtoutput, buildstr, strsize);
157 if (jspec->wantedhost != NULL)
158 strlcat(jspec->fmtoutput, jspec->wantedhost, strsize);
164 * Free all the jobspec-related information.
167 free_jobspec(struct jobspec_hdr *js_hdr)
169 struct jobspec *jsinf;
171 while (!STAILQ_EMPTY(js_hdr)) {
172 jsinf = STAILQ_FIRST(js_hdr);
173 STAILQ_REMOVE_HEAD(js_hdr, nextjs);
174 if (jsinf->fmtoutput)
175 free(jsinf->fmtoutput);
176 if (jsinf->matcheduser)
177 free(jsinf->matcheduser);
183 * This routine takes a string as typed in from the user, and parses it
184 * into a job-specification. A job specification would match one or more
185 * jobs in the queue of some single printer (the specification itself does
186 * not indicate which queue should be searched).
188 * This recognizes a job-number range by itself (all digits, or a range
189 * indicated by "digits-digits"), or a userid by itself. If a `:' is
190 * found, it is treated as a separator between a job-number range and
191 * a userid, where the job number range is the side which has a digit as
192 * the first character. If an `@' is found, everything to the right of
193 * it is treated as the hostname the job originated from.
195 * So, the user can specify:
196 * jobrange userid userid:jobrange jobrange:userid
197 * jobrange@hostname jobrange:userid@hostname
198 * userid@hostname userid:jobrange@hostname
200 * XXX - it would be nice to add "not options" too, such as ^user,
201 * ^jobrange, and @^hostname.
203 * This routine may modify the original input string if that input is
204 * valid. If the input was *not* valid, then this routine should return
205 * with the input string the same as when the routine was called.
208 parse_jobspec(char *jobstr, struct jobspec_hdr *js_hdr)
210 struct jobspec *jsinfo;
211 char *atsign, *colon, *lhside, *numstr, *period, *rhside;
215 printf("\t [ pjs-input = %s ]\n", jobstr);
218 if ((jobstr == NULL) || (*jobstr == '\0'))
221 jsinfo = malloc(sizeof(struct jobspec));
222 memset(jsinfo, 0, sizeof(struct jobspec));
223 jsinfo->startnum = jsinfo->endrange = -1;
225 /* Find the separator characters, and nullify them. */
227 atsign = strchr(jobstr, '@');
228 colon = strchr(jobstr, ':');
234 /* The at-sign always indicates a hostname. */
235 if (atsign != NULL) {
238 jsinfo->wantedhost = rhside;
241 /* Finish splitting the input into three parts. */
253 * If there is a `:' here, then it's either jobrange:userid,
254 * userid:jobrange, or (if @hostname was not given) perhaps it
255 * might be hostname:jobnum. The side which has a digit as the
256 * first character is assumed to be the jobrange. It is an
257 * input error if both sides start with a digit, or if neither
258 * side starts with a digit.
260 if ((lhside != NULL) && (rhside != NULL)) {
261 if (isdigitch(*lhside)) {
262 if (isdigitch(*rhside))
265 jsinfo->wanteduser = rhside;
266 } else if (isdigitch(*rhside)) {
269 * The original implementation of 'lpc topq' accepted
270 * hostname:jobnum. If the input did not include a
271 * @hostname, then assume the userid is a hostname if
274 period = strchr(lhside, '.');
275 if ((atsign == NULL) && (period != NULL))
276 jsinfo->wantedhost = lhside;
278 jsinfo->wanteduser = lhside;
280 /* Neither side is a job number = user error */
283 } else if (lhside != NULL) {
284 if (isdigitch(*lhside))
287 jsinfo->wanteduser = lhside;
288 } else if (rhside != NULL) {
289 if (isdigitch(*rhside))
292 jsinfo->wanteduser = rhside;
296 * Break down the numstr. It should be all digits, or a range
297 * specified as "\d+-\d+".
299 if (numstr != NULL) {
301 jobnum = strtol(numstr, &numstr, 10);
302 if (errno != 0) /* error in conversion */
304 if (jobnum < 0) /* a bogus value for this purpose */
306 if (jobnum > 99999) /* too large for job number */
308 jsinfo->startnum = jsinfo->endrange = jobnum;
310 /* Check for a range of numbers */
311 if ((*numstr == '-') && (isdigitch(*(numstr + 1)))) {
314 jobnum = strtol(numstr, &numstr, 10);
315 if (errno != 0) /* error in conversion */
317 if (jobnum < jsinfo->startnum)
319 if (jobnum > 99999) /* too large for job number */
321 jsinfo->endrange = jobnum;
325 * If there is anything left in the numstr, and if the
326 * original string did not include a userid or a hostname,
327 * then this might be the ancient form of '\d+hostname'
328 * (with no separator between jobnum and hostname). Accept
329 * that for backwards compatibility, but otherwise any
330 * remaining characters mean a user-error. Note that the
331 * ancient form accepted only a single number, but this
332 * will also accept a range of numbers.
334 if (*numstr != '\0') {
337 if (jsinfo->wantedhost != NULL)
339 if (jsinfo->wanteduser != NULL)
341 /* Treat as the rest of the string as a hostname */
342 jsinfo->wantedhost = numstr;
346 if ((jsinfo->startnum < 0) && (jsinfo->wanteduser == NULL) &&
347 (jsinfo->wantedhost == NULL))
351 * The input was valid, in the sense that it could be parsed
352 * into the individual parts. Add this jobspec to the list
355 STAILQ_INSERT_TAIL(js_hdr, jsinfo, nextjs);
358 printf("\t [ will check for");
359 if (jsinfo->startnum >= 0) {
360 if (jsinfo->startnum == jsinfo->endrange)
361 printf(" jobnum = %ld", jsinfo->startnum);
363 printf(" jobrange = %ld to %ld", jsinfo->startnum,
368 if ((jsinfo->wanteduser != NULL) || (jsinfo->wantedhost != NULL)) {
370 if (jsinfo->wanteduser != NULL)
371 printf(" user = %s", jsinfo->wanteduser);
372 if (jsinfo->wantedhost != NULL)
373 printf(" host = %s", jsinfo->wantedhost);
382 * Restore any `@' and `:', in case the calling routine wants to
383 * write an error message which includes the input string.
395 * Check to see if a given job (specified by a jobqueue entry) matches
396 * all of the specifications in a given jobspec.
398 * Returns 0 if no match, 1 if the job does match.
401 match_jobspec(struct jobqueue *jq, struct jobspec *jspec)
403 struct cjobinfo *cfinf;
404 char *cp, *cf_numstr, *cf_hoststr;
408 printf("\t [ match-js checking %s ]\n", jq->job_cfname);
411 if (jspec == NULL || jq == NULL)
415 * Keep track of which jobs have already been matched by this
416 * routine, and thus (probably) already processed.
422 * The standard `cf' file has the job number start in position 4,
423 * but some implementations have that as an extra file-sequence
424 * letter, and start the job number in position 5. The job
425 * number is usually three bytes, but may be as many as five.
427 * XXX - All this nonsense should really be handled in a single
428 * place, like getq()...
430 cf_numstr = jq->job_cfname + 3;
431 if (!isdigitch(*cf_numstr))
434 for (cp = cf_numstr; (cp < cf_numstr + 5) && isdigitch(*cp); cp++)
435 jnum = jnum * 10 + (*cp - '0');
438 match = 0; /* assume the job will not match */
439 jspec->matcheduser = NULL;
442 * Check the job-number range.
444 if (jspec->startnum >= 0) {
445 if (jnum < jspec->startnum)
447 if (jnum > jspec->endrange)
452 * Check the hostname. Strictly speaking this should be done by
453 * reading the control file, but it is less expensive to check
454 * the hostname-part of the control file name. Also, this value
455 * can be easily seen in 'lpq -l', while there is no easy way for
456 * a user/operator to see the hostname in the control file.
458 if (jspec->wantedhost != NULL) {
459 if (fnmatch(jspec->wantedhost, cf_hoststr, 0) != 0)
464 * Check for a match on the user name. This has to be done
465 * by reading the control file.
467 if (jspec->wanteduser != NULL) {
468 cfinf = ctl_readcf("fakeq", jq->job_cfname);
471 if (fnmatch(jspec->wanteduser, cfinf->cji_username, 0) != 0)
475 /* This job matches all of the specified criteria. */
477 jq->job_matched = 1; /* avoid matching the job twice */
479 if (jspec->wanteduser != NULL) {
481 * If the user specified a userid (which may have been a
482 * pattern), then the caller's "doentry()" routine might
483 * want to know the userid of this job that matched.
485 jspec->matcheduser = strdup(cfinf->cji_username);
488 printf("\t [ job matched! ]\n");
498 * Scan a queue for all jobs which match a jobspec. The queue is scanned
499 * from top to bottom.
501 * The caller can provide a routine which will be executed for each job
502 * that does match. Note that the processing routine might do anything
503 * to the matched job -- including the removal of it.
505 * This returns the number of jobs which were matched.
508 scanq_jobspec(int qcount, struct jobqueue **squeue, int sopts, struct
509 jobspec_hdr *js_hdr, process_jqe doentry, void *doentryinfo)
511 struct jobqueue **qent;
512 struct jobspec *jspec;
513 int cnt, matched, total;
520 /* The caller must specify one of the scanning orders */
521 if ((sopts & (SCQ_JSORDER|SCQ_QORDER)) == 0)
525 if (sopts & SCQ_JSORDER) {
527 * For each job specification, scan through the queue
528 * looking for every job that matches.
530 STAILQ_FOREACH(jspec, js_hdr, nextjs) {
531 for (qent = squeue, cnt = 0; cnt < qcount;
533 matched = match_jobspec(*qent, jspec);
538 doentry(doentryinfo, *qent, jspec);
539 if (jspec->matcheduser != NULL) {
540 free(jspec->matcheduser);
541 jspec->matcheduser = NULL;
545 * The entire queue has been scanned for this
546 * jobspec. Call the user's routine again with
547 * a NULL queue-entry, so it can print out any
548 * kind of per-jobspec summary.
551 doentry(doentryinfo, NULL, jspec);
555 * For each job in the queue, check all of the job
556 * specifications to see if any one of them matches
559 for (qent = squeue, cnt = 0; cnt < qcount;
561 STAILQ_FOREACH(jspec, js_hdr, nextjs) {
562 matched = match_jobspec(*qent, jspec);
567 doentry(doentryinfo, *qent, jspec);
568 if (jspec->matcheduser != NULL) {
569 free(jspec->matcheduser);
570 jspec->matcheduser = NULL;
573 * Once there is a match, then there is no
574 * point in checking this same job against
575 * all the other jobspec's.