| 1 | /* |
| 2 | * PPD file generation program for the CUPS drivers. |
| 3 | * |
| 4 | * Copyright 1993-2008 by Mike Sweet and Robert Krawitz. |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify it |
| 7 | * under the terms of the GNU General Public License as published by the Free |
| 8 | * Software Foundation; either version 2 of the License, or (at your option) |
| 9 | * any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, but |
| 12 | * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY |
| 13 | * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 14 | * for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 18 | * |
| 19 | * Contents: |
| 20 | * |
| 21 | * main() - Process files on the command-line... |
| 22 | * cat_ppd() - Copy the named PPD to stdout. |
| 23 | * generate_ppd() - Generate a PPD file. |
| 24 | * getlangs() - Get a list of available translations. |
| 25 | * help() - Show detailed help. |
| 26 | * is_special_option() - Determine if an option should be grouped. |
| 27 | * list_ppds() - List the available drivers. |
| 28 | * print_group_close() - Close a UI group. |
| 29 | * print_group_open() - Open a new UI group. |
| 30 | * printlangs() - Print list of available translations. |
| 31 | * printmodels() - Print a list of available models. |
| 32 | * usage() - Show program usage. |
| 33 | * write_ppd() - Write a PPD file. |
| 34 | */ |
| 35 | |
| 36 | /* |
| 37 | * 'main()' - Process files on the command-line... |
| 38 | */ |
| 39 | |
| 40 | #include "genppd.h" |
| 41 | |
| 42 | static int cat_ppd(const char *uri); |
| 43 | static int list_ppds(const char *argv0); |
| 44 | |
| 45 | |
| 46 | int /* O - Exit status */ |
| 47 | main(int argc, /* I - Number of command-line arguments */ |
| 48 | char *argv[]) /* I - Command-line arguments */ |
| 49 | { |
| 50 | /* |
| 51 | * Force POSIX locale, since stp_init incorrectly calls setlocale... |
| 52 | */ |
| 53 | |
| 54 | #if defined(HAVE_SETENV) |
| 55 | (void) setenv("LANG", "C", 1); |
| 56 | (void) setenv("LC_ALL", "C", 1); |
| 57 | (void) setenv("LC_NUMERIC", "C", 1); |
| 58 | #else |
| 59 | putenv("LANG=C"); |
| 60 | putenv("LC_ALL=C"); |
| 61 | putenv("LC_NUMERIC=C"); |
| 62 | #endif |
| 63 | |
| 64 | /* |
| 65 | * Initialise libgutenprint |
| 66 | */ |
| 67 | |
| 68 | stp_init(); |
| 69 | |
| 70 | /* |
| 71 | * Process command-line... |
| 72 | */ |
| 73 | |
| 74 | if (argc == 2 && !strcmp(argv[1], "list")) |
| 75 | return (list_ppds(argv[0])); |
| 76 | else if (argc == 3 && !strcmp(argv[1], "cat")) |
| 77 | return (cat_ppd(argv[2])); |
| 78 | else if (argc == 2 && !strcmp(argv[1], "org.gutenprint.multicat")) |
| 79 | { |
| 80 | char buf[1024]; |
| 81 | int status = 0; |
| 82 | while (fgets(buf, sizeof(buf) - 1, stdin)) |
| 83 | { |
| 84 | size_t len = strlen(buf); |
| 85 | if (len == 0) |
| 86 | continue; |
| 87 | if (buf[len - 1] == '\n') |
| 88 | buf[len - 1] = '\0'; |
| 89 | status |= cat_ppd(buf); |
| 90 | fputs("*%*%EOFEOF\n", stdout); |
| 91 | (void) fflush(stdout); |
| 92 | } |
| 93 | } |
| 94 | else if (argc == 2 && !strcmp(argv[1], "VERSION")) |
| 95 | { |
| 96 | printf("%s\n", VERSION); |
| 97 | return (0); |
| 98 | } |
| 99 | else if (argc == 2 && !strcasecmp(argv[1], "org.gutenprint.extensions")) |
| 100 | { |
| 101 | printf("org.gutenprint.multicat"); |
| 102 | return (0); |
| 103 | } |
| 104 | else |
| 105 | { |
| 106 | fprintf(stderr, "Usage: %s list\n", argv[0]); |
| 107 | fprintf(stderr, " %s cat URI\n", argv[0]); |
| 108 | return (1); |
| 109 | } |
| 110 | return (0); |
| 111 | } |
| 112 | |
| 113 | |
| 114 | /* |
| 115 | * 'cat_ppd()' - Copy the named PPD to stdout. |
| 116 | */ |
| 117 | |
| 118 | static int /* O - Exit status */ |
| 119 | cat_ppd(const char *uri) /* I - Driver URI */ |
| 120 | { |
| 121 | char scheme[64], /* URI scheme */ |
| 122 | userpass[32], /* URI user/pass (unused) */ |
| 123 | hostname[32], /* URI hostname */ |
| 124 | resource[1024]; /* URI resource */ |
| 125 | int port; /* URI port (unused) */ |
| 126 | http_uri_status_t status; /* URI decode status */ |
| 127 | const stp_printer_t *p; /* Printer driver */ |
| 128 | const char *lang = NULL; |
| 129 | char *s; |
| 130 | char filename[1024], /* Filename */ |
| 131 | ppd_location[2048]; /* Installed location */ |
| 132 | const char *infix = ""; |
| 133 | ppd_type_t ppd_type = PPD_STANDARD; |
| 134 | gpfile outFD; |
| 135 | |
| 136 | if ((status = httpSeparateURI(HTTP_URI_CODING_ALL, uri, |
| 137 | scheme, sizeof(scheme), |
| 138 | userpass, sizeof(userpass), |
| 139 | hostname, sizeof(hostname), |
| 140 | &port, resource, sizeof(resource))) |
| 141 | < HTTP_URI_OK) |
| 142 | { |
| 143 | fprintf(stderr, "ERROR: Bad ppd-name \"%s\" (%d)!\n", uri, status); |
| 144 | return (1); |
| 145 | } |
| 146 | |
| 147 | if (strcmp(scheme, "gutenprint." GUTENPRINT_RELEASE_VERSION) != 0) |
| 148 | { |
| 149 | fprintf(stderr, "ERROR: Gutenprint version mismatch!\n"); |
| 150 | return(1); |
| 151 | } |
| 152 | |
| 153 | s = strchr(resource + 1, '/'); |
| 154 | if (s) |
| 155 | { |
| 156 | lang = s + 1; |
| 157 | *s = '\0'; |
| 158 | } |
| 159 | |
| 160 | if ((p = stp_get_printer_by_driver(hostname)) == NULL) |
| 161 | { |
| 162 | fprintf(stderr, "ERROR: Unable to find driver \"%s\"!\n", hostname); |
| 163 | return (1); |
| 164 | } |
| 165 | |
| 166 | if (strcmp(resource + 1, "simple") == 0) |
| 167 | { |
| 168 | infix = ".sim"; |
| 169 | ppd_type = PPD_SIMPLIFIED; |
| 170 | } |
| 171 | else if (strcmp(resource + 1, "nocolor") == 0) |
| 172 | { |
| 173 | infix = ".nc"; |
| 174 | ppd_type = PPD_NO_COLOR_OPTS; |
| 175 | } |
| 176 | |
| 177 | /* |
| 178 | * This isn't really the right thing to do. We really shouldn't |
| 179 | * be embedding filenames in automatically generated PPD files, but |
| 180 | * if the user ever decides to go back from generated PPD files to |
| 181 | * static PPD files we'll need to have this for genppdupdate to work. |
| 182 | */ |
| 183 | snprintf(filename, sizeof(filename) - 1, "stp-%s.%s%s%s", |
| 184 | hostname, GUTENPRINT_RELEASE_VERSION, infix, ppdext); |
| 185 | snprintf(ppd_location, sizeof(ppd_location) - 1, "%s%s%s/ppd/%s%s", |
| 186 | cups_modeldir, |
| 187 | cups_modeldir[strlen(cups_modeldir) - 1] == '/' ? "" : "/", |
| 188 | lang ? lang : "C", |
| 189 | filename, gpext); |
| 190 | |
| 191 | outFD.f = stdout; |
| 192 | return (write_ppd(&outFD, p, lang, ppd_location, ppd_type, filename, 0)); |
| 193 | } |
| 194 | |
| 195 | /* |
| 196 | * 'list_ppds()' - List the available drivers. |
| 197 | */ |
| 198 | |
| 199 | static int /* O - Exit status */ |
| 200 | list_ppds(const char *argv0) /* I - Name of program */ |
| 201 | { |
| 202 | const char *scheme; /* URI scheme */ |
| 203 | int i; /* Looping var */ |
| 204 | const stp_printer_t *printer; /* Pointer to printer driver */ |
| 205 | |
| 206 | if ((scheme = strrchr(argv0, '/')) != NULL) |
| 207 | scheme ++; |
| 208 | else |
| 209 | scheme = argv0; |
| 210 | |
| 211 | for (i = 0; i < stp_printer_model_count(); i++) |
| 212 | if ((printer = stp_get_printer_by_index(i)) != NULL) |
| 213 | { |
| 214 | const char *device_id; |
| 215 | if (!strcmp(stp_printer_get_family(printer), "ps") || |
| 216 | !strcmp(stp_printer_get_family(printer), "raw")) |
| 217 | continue; |
| 218 | |
| 219 | device_id = stp_printer_get_device_id(printer); |
| 220 | printf("\"%s://%s/expert\" " |
| 221 | "%s " |
| 222 | "\"%s\" " |
| 223 | "\"%s" CUPS_PPD_NICKNAME_STRING VERSION "\" " |
| 224 | "\"%s\"\n", |
| 225 | scheme, stp_printer_get_driver(printer), |
| 226 | "en", |
| 227 | stp_printer_get_manufacturer(printer), |
| 228 | stp_printer_get_long_name(printer), |
| 229 | device_id ? device_id : ""); |
| 230 | |
| 231 | #ifdef GENERATE_SIMPLIFIED_PPDS |
| 232 | printf("\"%s://%s/simple\" " |
| 233 | "%s " |
| 234 | "\"%s\" " |
| 235 | "\"%s" CUPS_PPD_NICKNAME_STRING VERSION " Simplified\" " |
| 236 | "\"%s\"\n", |
| 237 | scheme, stp_printer_get_driver(printer), |
| 238 | "en", |
| 239 | stp_printer_get_manufacturer(printer), |
| 240 | stp_printer_get_long_name(printer), |
| 241 | device_id ? device_id : ""); |
| 242 | #endif |
| 243 | |
| 244 | #ifdef GENERATE_NOCOLOR_PPDS |
| 245 | printf("\"%s://%s/nocolor\" " |
| 246 | "%s " |
| 247 | "\"%s\" " |
| 248 | "\"%s" CUPS_PPD_NICKNAME_STRING VERSION " No color options\" " |
| 249 | "\"%s\"\n", |
| 250 | scheme, stp_printer_get_driver(printer), |
| 251 | "en", |
| 252 | stp_printer_get_manufacturer(printer), |
| 253 | stp_printer_get_long_name(printer), |
| 254 | device_id ? device_id : ""); |
| 255 | #endif |
| 256 | } |
| 257 | |
| 258 | return (0); |
| 259 | } |