]>
git.ipfire.org Git - thirdparty/cups.git/blob - ppdc/drv.cxx
4 // DDK driver interface main entry for the CUPS PPD Compiler.
6 // Copyright 2007 by Apple Inc.
7 // Copyright 2002-2006 by Easy Software Products.
9 // These coded instructions, statements, and computer programs are the
10 // property of Apple Inc. and are protected by Federal copyright
11 // law. Distribution and use rights are outlined in the file "LICENSE.txt"
12 // which should have been included with this file. If this file is
13 // file is missing or damaged, see the license at "http://www.cups.org/".
17 // main() - Enumerate or display PPD files.
18 // cat_ppd() - Display a PPD file.
19 // list_ppds() - List PPDs.
23 // Include necessary headers...
27 #include <cups/cups.h>
30 #include <sys/types.h>
37 static int cat_ppd(ppdcSource
*src
, const char *name
);
38 static int list_drvs(const char *pathname
, const char *prefix
);
39 static int list_ppds(ppdcSource
*src
, const char *name
);
43 // 'main()' - Enumerate or display PPD files.
46 int // O - Exit status
47 main(int argc
, // I - Number of command-line arguments
48 char *argv
[]) // I - Command-line arguments
50 const char *datadir
; // CUPS_DATADIR
51 ppdcSource
*src
; // PPD source file data
52 char filename
[1024], // Full path to .drv file(s)
53 scheme
[32], // URI scheme ("drv")
54 userpass
[256], // User/password info (unused)
55 host
[2], // Hostname (unused)
56 resource
[1024], // Resource path (/dir/to/filename.drv)
57 *pc_file_name
; // Filename portion of URI
58 int port
, // Port number (unused)
59 status
; // Exit status
62 // Determine where CUPS has installed the data files...
63 if ((datadir
= getenv("CUPS_DATADIR")) == NULL
)
64 datadir
= CUPS_DATADIR
;
66 // List all available PPDs or cat a single PPD...
67 if (argc
== 2 && !strcmp(argv
[1], "list"))
69 snprintf(filename
, sizeof(filename
), "%s/drv", datadir
);
70 return (list_drvs(filename
, "/"));
72 else if (argc
== 3 && !strcmp(argv
[1], "cat"))
74 httpSeparateURI(HTTP_URI_CODING_ALL
, argv
[2], scheme
, sizeof(scheme
),
75 userpass
, sizeof(userpass
), host
, sizeof(host
), &port
,
76 resource
, sizeof(resource
));
78 if (strstr(resource
, "../") ||
79 (pc_file_name
= strrchr(resource
, '/')) == NULL
||
80 pc_file_name
== resource
)
82 fprintf(stderr
, "ERROR: Bad driver info URI \"%s\"!\n", argv
[2]);
86 *pc_file_name
++ = '\0';
88 snprintf(filename
, sizeof(filename
), "%s/drv%s", datadir
, resource
);
90 src
= new ppdcSource(filename
);
92 status
= cat_ppd(src
, pc_file_name
);
99 fprintf(stderr
, "ERROR: Usage: %s cat URI\n", argv
[0]);
100 fprintf(stderr
, "ERROR: Usage: %s list\n", argv
[0]);
107 // 'cat_ppd()' - Display a PPD file.
110 static int // O - Exit status
111 cat_ppd(ppdcSource
*src
, // I - Driver info file
112 const char *name
) // I - PC filename
114 ppdcDriver
*d
; // Current driver
115 cups_file_t
*out
; // Stdout via CUPS file API
118 for (d
= (ppdcDriver
*)src
->drivers
->first();
120 d
= (ppdcDriver
*)src
->drivers
->next())
121 if (!strcmp(name
, d
->pc_file_name
->value
))
123 out
= cupsFileStdout();
125 d
->write_ppd_file(out
, NULL
, NULL
, src
, PPDC_LFONLY
);
135 // 'list_drvs()' - List all drv files in the given path...
138 static int // O - Exit status
139 list_drvs(const char *pathname
, // I - Full path to directory
140 const char *prefix
) // I - Prefix for directory
142 char *ext
, // Extension on file
143 filename
[1024], // Full path to .drv file(s)
144 newprefix
[1024]; // New prefix for directory
145 cups_dir_t
*dir
; // Current directory
146 cups_dentry_t
*dent
; // Current directory entry
149 if ((dir
= cupsDirOpen(pathname
)) == NULL
)
152 while ((dent
= cupsDirRead(dir
)) != NULL
)
154 // Skip "dot" files...
155 if (dent
->filename
[0] == '.')
158 // See if this is a file or directory...
159 snprintf(filename
, sizeof(filename
), "%s/%s", pathname
, dent
->filename
);
161 if (S_ISDIR(dent
->fileinfo
.st_mode
))
163 // Descend into the subdirectory...
164 snprintf(newprefix
, sizeof(newprefix
), "%s%s/", prefix
, dent
->filename
);
166 if (list_drvs(filename
, newprefix
))
172 else if ((ext
= strrchr(dent
->filename
, '.')) != NULL
&&
173 (!strcmp(ext
, ".drv") || !strcmp(ext
, ".drv.gz")))
175 // List the PPDs in this driver info file...
176 ppdcSource
*src
= new ppdcSource(filename
);
179 snprintf(newprefix
, sizeof(newprefix
), "%s%s", prefix
, dent
->filename
);
180 list_ppds(src
, newprefix
);
192 // 'list_ppds()' - List PPDs in a driver info file.
195 static int // O - Exit status
196 list_ppds(ppdcSource
*src
, // I - Driver info file
197 const char *name
) // I - Name of driver info file
199 ppdcDriver
*d
; // Current driver
200 ppdcAttr
*attr
; // 1284DeviceID attribute
201 char uri
[1024]; // Driver URI
204 for (d
= (ppdcDriver
*)src
->drivers
->first();
206 d
= (ppdcDriver
*)src
->drivers
->next())
208 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "drv", "", "", 0,
209 "%s/%s", name
, d
->pc_file_name
->value
);
211 attr
= d
->find_attr("1284DeviceID", NULL
);
213 printf("\"%s\" en \"%s\" \"%s\" \"%s\"\n", uri
, d
->manufacturer
->value
,
214 d
->model_name
->value
, attr
? attr
->value
->value
: "");
227 // Scan the command-line...
233 comp
= PPDC_NO_COMPRESSION
;
236 for (i
= 1; i
< argc
; i
++)
237 if (argv
[i
][0] == '-')
239 for (opt
= argv
[i
] + 1; *opt
; opt
++)
242 case 'c' : // Message catalog...
248 printf("ppdc: Loading messages from \"%s\"...\n", argv
[i
]);
251 catalog
= new ppdcCatalog("en");
253 if (catalog
->load_messages(argv
[i
]))
256 "ppdc: Unable to load localization file \"%s\" - %s\n",
257 argv
[i
], strerror(errno
));
262 case 'd' : // Output directory...
268 printf("ppdc: Writing PPD files to directory \"%s\"...\n",
274 case 'l' : // Language(s)...
279 if (strchr(argv
[i
], ','))
281 // Comma-delimited list of languages...
282 char temp
[1024], // Copy of language list
283 *start
, // Start of current locale name
284 *end
; // End of current locale name
287 locales
= new ppdcArray();
289 strlcpy(temp
, argv
[i
], sizeof(temp
));
290 for (start
= temp
; *start
; start
= end
)
292 if ((end
= strchr(start
, ',')) != NULL
)
295 end
= start
+ strlen(start
);
298 locales
->add(new ppdcString(start
));
304 printf("ppdc: Loading messages for locale \"%s\"...\n",
310 catalog
= new ppdcCatalog(argv
[i
]);
312 if (catalog
->messages
->count
== 0)
315 "ppdc: Unable to find localization for \"%s\" - %s\n",
316 argv
[i
], strerror(errno
));
322 case 'I' : // Include directory...
328 printf("ppdc: Adding include directory \"%s\"...\n", argv
[i
]);
330 ppdcSource::add_include(argv
[i
]);
333 case 'v' : // Be verbose...
337 case 'z' : // Compress files...
338 comp
= PPDC_GZIP_COMPRESSION
;
341 case '-' : // --option
342 if (!strcmp(opt
, "-lf"))
345 opt
+= strlen(opt
) - 1;
348 else if (!strcmp(opt
, "-cr"))
351 opt
+= strlen(opt
) - 1;
354 else if (!strcmp(opt
, "-crlf"))
357 opt
+= strlen(opt
) - 1;
368 // Open and load the driver info file...
370 printf("ppdc: Loading driver information file \"%s\"...\n", argv
[i
]);
372 src
= new ppdcSource(argv
[i
]);
374 // Create the output directory...
375 if (mkdir(outdir
, 0777))
379 fprintf(stderr
, "ppdc: Unable to create output directory %s: %s\n",
380 outdir
, strerror(errno
));
385 // Write PPD files...
386 for (d
= (ppdcDriver
*)src
->drivers
->first();
388 d
= (ppdcDriver
*)src
->drivers
->next())
390 // Write the PPD file for this driver...
391 for (j
= 0; d
->pc_file_name
->value
[j
]; j
++)
392 pcfilename
[j
] = tolower(d
->pc_file_name
->value
[j
]);
394 pcfilename
[j
] = '\0';
396 if (comp
== PPDC_GZIP_COMPRESSION
)
397 snprintf(filename
, sizeof(filename
), "%s/%s.gz", outdir
, pcfilename
);
399 snprintf(filename
, sizeof(filename
), "%s/%s", outdir
, pcfilename
);
402 printf("ppdc: Writing %s...\n", filename
);
404 if (d
->write_ppd_file(filename
, catalog
, locales
, src
, le
, comp
))
408 // Delete the printer driver information...
415 // If no drivers have been loaded, display the program usage message.
419 // Return with no errors.