xref: /titanic_50/usr/src/cmd/print/printmgr/com/sun/admin/pm/server/DoPrinterUtil.java (revision 62d717f5277d7b19b63db2d800310f877b57c197)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * ident	"%Z%%M%	%I%	%E% SMI"
23  *
24  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
25  * Use is subject to license terms.
26  *
27  * DoPrinterUtil class
28  * Worker utility class.
29  */
30 
31 package com.sun.admin.pm.server;
32 
33 import java.io.*;
34 import java.util.*;
35 
36 public class  DoPrinterUtil {
37 
getDefault(String ns)38     public static String getDefault(String ns) throws Exception
39     {
40 	Debug.message("SVR: DoPrinterUtil.getDefault()");
41 	Debug.message("SVR: name service equals " + ns);
42 
43 	String o = null;
44 	String cmd = "/usr/bin/lpget -n " + ns + " _default";
45 	SysCommand syscmd = new SysCommand();
46 	syscmd.exec(cmd);
47 	o = syscmd.getOutput();
48 	syscmd = null;
49 
50 	if (o == null) {
51 		return (null);
52 	}
53 	int i = o.indexOf("use=");
54 	if (i == -1) {
55 		return (null);
56 	}
57 	o = o.substring(i);
58 	String dflt = DoPrinterView.getToken(o + "\n", "use=");
59 
60 	Debug.message("SVR: default is " + dflt);
61 	return (new String(dflt));
62     }
63 
getDevices()64     public static String[] getDevices() throws Exception
65     {
66 	Debug.message("SVR: DoPrinterUtil.getDevices()");
67 
68 	int i = 0;
69 	String dev = "";
70 	String devices = "";
71 
72 	String serial_possibilities[] = {"a", "b", "c", "d",
73 		"e", "f", "g", "h", "i", "j", "k", "l", "m",
74 		"n", "o", "p", "q", "r", "s", "t", "u", "v",
75 		"w", "x", "y", "z"};
76 
77 	String cmd = "/usr/bin/find /dev -print";
78 	SysCommand syscmd = new SysCommand();
79 	syscmd.exec(cmd);
80 	if (syscmd.getExitValue() != 0) {
81 		String errstr = syscmd.getError();
82 		syscmd = null;
83 		throw new pmCmdFailedException(errstr);
84 	}
85 
86 	String o = syscmd.getOutput();
87 	syscmd = null;
88 
89 	if (o == null) {
90 		return (null);
91 	}
92 	o = o.concat("\n");
93 
94 	for (i = 0; i < serial_possibilities.length; i++) {
95 		dev = "/dev/term/" + serial_possibilities[i] + "\n";
96 		if (o.indexOf(dev) != -1) {
97 			devices = devices.concat(" " + dev + " ");
98 		}
99 	}
100 	// sparc bpp parallel ports
101 	for (i = 0; i < 100; i++) {
102 		dev = "/dev/bpp" + i + "\n";
103 		if (o.indexOf(dev) != -1) {
104 			devices = devices.concat(" " + dev + " ");
105 		}
106 	}
107 	// sparc ecpp parallel ports
108 	for (i = 0; i < 100; i++) {
109 		dev = "/dev/ecpp" + i + "\n";
110 		if (o.indexOf(dev) != -1) {
111 			devices = devices.concat(" " + dev + " ");
112 		}
113 	}
114 	// intel parallel ports
115 	for (i = 0; i < 100; i++) {
116 		dev = "/dev/lp" + i + "\n";
117 		if (o.indexOf(dev) != -1) {
118 			devices = devices.concat(" " + dev + " ");
119 		}
120 	}
121 
122 	// USB
123 	for (i = 0; i < 100; i++) {
124 		dev = "/dev/printers/" + i + "\n";
125 		if (o.indexOf(dev) != -1) {
126 			devices = devices.concat(" " + dev + " ");
127 		}
128 	}
129 
130 	// SunPics
131 	dev = "/dev/lpvi\n";
132 	if (o.indexOf(dev) != -1) {
133 		devices = devices.concat(" " + dev + " ");
134 	}
135 
136 	o = null;
137 
138 	if (devices.equals("")) {
139 		return (null);
140 	}
141 
142 	String ret[];
143 	StringTokenizer st = new StringTokenizer(devices);
144 	if (st.countTokens() == 0) {
145 		return (null);
146 	} else {
147 		ret = new String[st.countTokens()];
148 		for (i = 0; st.hasMoreTokens(); i++) {
149 			ret[i] = st.nextToken();
150 		}
151 	}
152 	return (ret);
153     }
154 
getMakes()155     public static String[] getMakes() throws Exception
156     {
157 	int i;
158 
159 	Debug.message("SVR: DoPrinterUtil.getMakes()");
160 
161 	String cmd = "/usr/lib/lp/bin/getmakes";
162 	SysCommand syscmd = new SysCommand();
163 	syscmd.exec(cmd);
164 	if (syscmd.getExitValue() != 0) {
165 		String errstr = syscmd.getError();
166 		syscmd = null;
167 		throw new pmCmdFailedException(errstr);
168 	}
169 	String makes = syscmd.getOutput();
170 
171 	String ret[];
172 	StringTokenizer st = new StringTokenizer(makes);
173 	if (st.countTokens() == 0) {
174 		return (null);
175 	} else {
176 		ret = new String[st.countTokens()];
177 		for (i = 0; st.hasMoreTokens(); i++) {
178 			ret[i] = st.nextToken();
179 
180 		}
181 	}
182 	return (ret);
183 
184     }
getModels(String make)185     public static String[] getModels(String make) throws Exception
186     {
187 	int i;
188 	String ret[];
189 
190 	Debug.message("SVR:getModels()");
191 
192 	if (make == null) {
193 		Debug.message("SVR:getModels: make is null");
194 		return (null);
195 	}
196 	// Make call for models for this make
197 	String cmd = "/usr/lib/lp/bin/getmodels " +  make;
198 	SysCommand syscmd = new SysCommand();
199 	syscmd.exec(cmd);
200 	if (syscmd.getExitValue() != 0) {
201 		String errstr = syscmd.getError();
202 		syscmd = null;
203 		throw new pmCmdFailedException(errstr);
204 	}
205 	String models = syscmd.getOutput();
206 
207 	if (models != null) {
208 	    StringTokenizer st = new StringTokenizer(models, "\n");
209 	    if (st.countTokens() == 0) {
210 		Debug.message("SVR:String tokenizer count is zero");
211 		return (null);
212 	    } else {
213 		ret = new String[st.countTokens()];
214 		for (i = 0; st.hasMoreTokens(); i++) {
215 			ret[i] = st.nextToken();
216 
217 		}
218 	    }
219 	    return (ret);
220 	} else
221 		return (null);
222     }
223 
getPPDs(String make, String model)224     public static String[] getPPDs(String make, String model) throws Exception
225     {
226 	int i;
227 	String ret[];
228 	ret = new String[2];
229 	if ((make == null) || (model == null)) {
230 		return null;
231 	}
232 	// get ppd files for this make/model
233 	String cmd = "/usr/lib/lp/bin/getppds " + make + " " +  model;
234 	SysCommand syscmd = new SysCommand();
235 	syscmd.exec(cmd);
236 	if (syscmd.getExitValue() != 0) {
237 		String errstr = syscmd.getError();
238 		syscmd = null;
239 		throw new pmCmdFailedException(errstr);
240 	}
241 	String ppds = syscmd.getOutput();
242 	StringTokenizer st = new StringTokenizer(ppds, "\n");
243 	if (st.countTokens() == 0) {
244 		return (null);
245 	} else {
246 		ret = new String[st.countTokens()];
247 		for (i = 0; st.hasMoreTokens(); i++) {
248 			ret[i] = st.nextToken();
249 
250 		}
251 	}
252 	return (ret);
253     }
254 
getMakeModelNick(String ppdfilename)255     public static String[] getMakeModelNick(String ppdfilename) throws Exception
256     {
257 	int i;
258 	String ret[] = null;
259 	if (ppdfilename == null) {
260 		return (null);
261 	}
262 	// get ppd files for this make/model
263 	String cmd = "/usr/lib/lp/bin/ppdfilename2mmp " + ppdfilename;
264 	SysCommand syscmd = new SysCommand();
265 	syscmd.exec(cmd);
266 	if (syscmd.getExitValue() != 0) {
267 		String errstr = syscmd.getError();
268 		syscmd = null;
269 		throw new pmCmdFailedException(errstr);
270 	}
271 	String mmp  = syscmd.getOutput();
272 
273 	if (mmp != null) {
274 		ret = new String[2];
275 
276 	    StringTokenizer st = new StringTokenizer(mmp, "\n");
277 	    if (st.countTokens() == 0) {
278 		    return (null);
279 	    } else {
280 		    ret = new String[st.countTokens()];
281 		    for (i = 0; st.hasMoreTokens(); i++) {
282 			    ret[i] = st.nextToken();
283 
284 		    }
285 	    }
286 	}
287 	return (ret);
288     }
289 
getPPDFile( String make, String model, String ppd)290     public static String getPPDFile(
291 		String make, String model, String ppd) throws Exception
292     {
293 	int i;
294 	String ret[];
295 	ret = new String[2];
296 	if (ppd == null)  {
297 		return (null);
298 	}
299 	// get ppd path/filename for this ppd
300 	String cmd = "/usr/lib/lp/bin/getppdfile " +
301 		make + ":" + " " + model + ":" +  " " +  ppd + ":";
302 	SysCommand syscmd = new SysCommand();
303 	syscmd.exec(cmd);
304 	if (syscmd.getExitValue() != 0) {
305 		String errstr = syscmd.getError();
306 		syscmd = null;
307 		throw new pmCmdFailedException(errstr);
308 	}
309 	String ppdfile = syscmd.getOutput();
310 
311 	return (ppdfile);
312     }
313 
314 
getProbe(String device)315     public static String[] getProbe(String device)
316     {
317         int i;
318         String pmake = null;
319         String pmodel = null;
320         String tokens[] = null;
321         String ret[];
322         ret = new String[2];
323 
324         if (device == null)
325                 return (null);
326 
327         Debug.message("SVR: DoPrinterUtil.getProbe()");
328 
329         // Get Manufacturer and Model for printer in this port
330         String cmd = "/usr/lib/lp/bin/printer-info -M -m " + device;
331         SysCommand syscmd = new SysCommand();
332         try {
333                 syscmd.exec(cmd);
334         } catch (Exception e) {
335                 System.out.println(e);
336         }
337         if (syscmd.getExitValue() != 0) {
338                 String errstr = syscmd.getError();
339                 syscmd = null;
340                 return (null);
341         }
342 
343         String mm = syscmd.getOutput();
344         if (mm != null) {
345                 int numtokens;
346                 StringTokenizer st = new StringTokenizer(mm, ":" + "\n");
347                 if (st.countTokens() == 0) {
348                         return (null);
349                 } else {
350                         numtokens = st.countTokens();
351                         tokens = new String[st.countTokens()];
352                         for (i = 0; st.hasMoreTokens(); i++) {
353                                 tokens[i] = st.nextToken();
354                         }
355                 }
356                 for (i = 0; i < numtokens; i++) {
357                         if ((tokens[i].trim()).equals("Manufacturer")) {
358                                 pmake =  new String(tokens[i + 1].trim());
359                         } else { if ((tokens[i].trim()).equals("Model"))
360                                 pmodel =  new String(tokens[i + 1].trim());
361                         }
362                 }
363 
364                 if (pmake != null)
365                         ret[0] = pmake;
366                 if (pmodel != null)
367                         ret[1] = pmodel;
368 
369                 return (ret);
370         }
371         return (null);
372 
373     }
374 
isMakeModel( String make, String model)375     public static boolean isMakeModel(
376         String make,
377         String model)
378     {
379         int exitvalue;
380 
381         Debug.message("SVR: DoPrinterUtil.isMakeModel() " + make + " " + model);
382 
383         SysCommand syscmd = new SysCommand();
384         // syscmd.exec("/usr/bin/lpget -n " + ns + " " + name);
385         exitvalue = syscmd.getExitValue();
386         syscmd = null;
387         if (exitvalue == 0) {
388                 return (true);
389         }
390         return (false);
391     }
392 
393 
getList(String nsarg)394     public static String[] getList(String nsarg)
395 	throws Exception
396     {
397 	Debug.message("SVR: DoPrinterUtil.getList()");
398 
399 	int i = 0;
400 	int j = 0;
401 	int listi = 0;
402 
403 	String cmd = null;
404 	String printername = "";
405 	String printserver = "";
406 	String comment = "";
407 	String nameservice;
408 	String list[];
409 
410 	String o = null;
411 	cmd = "/usr/bin/lpget -n " + nsarg + " list";
412 	SysCommand syscmd = new SysCommand();
413 	syscmd.exec(cmd);
414 	if (syscmd.getExitValue() != 0) {
415 		String errstr = syscmd.getError();
416 		syscmd = null;
417 		throw new pmCmdFailedException(errstr);
418 	}
419 	o = syscmd.getOutput();
420 	syscmd = null;
421 
422 	if (o == null) {
423 		return (null);
424 	}
425 
426 	// Count entries
427 	int index = 0;
428 	while ((index = o.indexOf("bsdaddr=", index)) != -1) {
429 		index = index + 8;
430 		i++;
431 	}
432 	if (i <= 0)
433 		return (null);
434 
435 	list = new String [i*3];
436 
437 	int colon = 0;
438 	int nextcolon = 0;
439 	while ((colon = o.indexOf(":\n", colon + 1)) != -1) {
440 		nextcolon = o.indexOf(":\n", colon + 1);
441 		if (nextcolon == -1)
442 			nextcolon = o.length();
443 		// Extract printername
444 		i = colon;
445 		while ((o.charAt(i) != '\n') && (i != 0)) {
446 			i--;
447 		}
448 		if (i == 0)
449 			printername = o.substring(i, colon);
450 		else
451 			printername = o.substring(i + 1, colon);
452 
453 		// Skip _all and _default keywords
454 		if (printername.equals("_all")) {
455 			continue;
456 		}
457 		if (printername.equals("_default")) {
458 			continue;
459 		}
460 
461 		// Extract servername
462 		i = o.indexOf("bsdaddr=", colon);
463 		if ((i != -1) && (i < nextcolon)) {
464 			j = o.indexOf(",", i);
465 			if (j != -1)
466 				printserver = o.substring(i + 8, j);
467 		}
468 		// Skip entries without a server.
469 		if (printserver.equals("")) {
470 			Debug.warning(
471 			    "SVR: printer does not have a server: "
472 			    + printername);
473 			continue;
474 		}
475 
476 		// Extract description
477 		i = o.indexOf("description=", colon);
478 		if ((i != -1) && (i < nextcolon)) {
479 			j = i;
480 			while (j < o.length()) {
481 				if (o.charAt(j) == '\n')
482 					break;
483 				j++;
484 			}
485 			comment = o.substring(i + 12, j);
486 		}
487 
488 		list[listi++] = printername;
489 		list[listi++] = printserver;
490 		list[listi++] = comment;
491 		printername = "";
492 		printserver = "";
493 		comment = "";
494 	}
495 	return (list);
496     }
497 
exists( String name, String ns)498     public static boolean exists(
499 	String name,
500 	String ns) throws Exception
501     {
502 	int exitvalue;
503 
504 	Debug.message("SVR: DoPrinterUtil.exists() " + ns);
505 
506 	SysCommand syscmd = new SysCommand();
507 	syscmd.exec("/usr/bin/lpget -n " + ns + " " + name);
508 	exitvalue = syscmd.getExitValue();
509 	syscmd = null;
510 	if (exitvalue == 0) {
511 		return (true);
512 	}
513 	return (false);
514     }
515 
isLocal( String pn)516     public static boolean isLocal(
517 	String pn) throws Exception
518     {
519 	int exitvalue;
520 
521 	Debug.message("SVR: DoPrinterUtil.isLocal()");
522 
523 	SysCommand syscmd = new SysCommand();
524 	syscmd.exec("/usr/bin/test -d /etc/lp/printers/" + pn);
525 	exitvalue = syscmd.getExitValue();
526 	syscmd = null;
527 	if (exitvalue != 0) {
528 		return (false);
529 	}
530 	return (true);
531     }
532 
isLocalhost( String queue)533     public static boolean isLocalhost(
534 	String queue) throws Exception
535     {
536 	int exitvalue;
537 	String o = null;
538 
539 	Debug.message("SVR: DoPrinterUtil.isLocalhost():queue " + queue);
540 
541 	SysCommand syscmd = new SysCommand();
542 	syscmd.exec("/usr/bin/grep " + queue +  " /etc/printers.conf");
543 	exitvalue = syscmd.getExitValue();
544 	if (exitvalue != 0) {
545 	    Debug.message(
546 		"SVR:DoPrinterUtil:isLocalhost:failed:queue: " + queue);
547 	    return (false);
548 	}
549 	o = syscmd.getOutput();
550 	syscmd = null;
551 	Debug.message("SVR:DoPrinterUtil.java:isLocalhost: output: " + o);
552 	if (o.indexOf("localhost") != -1)
553 		return (true);
554 	else
555 		return (false);
556     }
557 }
558