Philip P. Moltmann | 25aee82 | 2016-12-15 12:16:46 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Backend functions for CUPS. |
| 3 | * |
| 4 | * Copyright 2007-2015 by Apple Inc. |
| 5 | * Copyright 2006 by Easy Software Products. |
| 6 | * |
Haibo Huang | 265f757 | 2019-12-13 16:47:52 -0800 | [diff] [blame^] | 7 | * Licensed under Apache License v2.0. See the file "LICENSE" for more information. |
Philip P. Moltmann | 25aee82 | 2016-12-15 12:16:46 -0800 | [diff] [blame] | 8 | */ |
| 9 | |
| 10 | /* |
| 11 | * Include necessary headers... |
| 12 | */ |
| 13 | |
| 14 | #include "cups-private.h" |
| 15 | #include "backend.h" |
| 16 | #include "ppd.h" |
| 17 | |
| 18 | |
| 19 | /* |
| 20 | * Local functions... |
| 21 | */ |
| 22 | |
| 23 | static void quote_string(const char *s); |
| 24 | |
| 25 | |
| 26 | /* |
| 27 | * 'cupsBackendDeviceURI()' - Get the device URI for a backend. |
| 28 | * |
| 29 | * The "argv" argument is the argv argument passed to main(). This |
| 30 | * function returns the device URI passed in the DEVICE_URI environment |
| 31 | * variable or the device URI passed in argv[0], whichever is found |
| 32 | * first. |
| 33 | * |
| 34 | * @since CUPS 1.2/macOS 10.5@ |
| 35 | */ |
| 36 | |
| 37 | const char * /* O - Device URI or @code NULL@ */ |
| 38 | cupsBackendDeviceURI(char **argv) /* I - Command-line arguments */ |
| 39 | { |
| 40 | const char *device_uri, /* Device URI */ |
| 41 | *auth_info_required; /* AUTH_INFO_REQUIRED env var */ |
| 42 | _cups_globals_t *cg = _cupsGlobals(); /* Global info */ |
| 43 | int options; /* Resolve options */ |
| 44 | ppd_file_t *ppd; /* PPD file */ |
| 45 | ppd_attr_t *ppdattr; /* PPD attribute */ |
| 46 | |
| 47 | |
| 48 | if ((device_uri = getenv("DEVICE_URI")) == NULL) |
| 49 | { |
| 50 | if (!argv || !argv[0] || !strchr(argv[0], ':')) |
| 51 | return (NULL); |
| 52 | |
| 53 | device_uri = argv[0]; |
| 54 | } |
| 55 | |
| 56 | options = _HTTP_RESOLVE_STDERR; |
| 57 | if ((auth_info_required = getenv("AUTH_INFO_REQUIRED")) != NULL && |
| 58 | !strcmp(auth_info_required, "negotiate")) |
| 59 | options |= _HTTP_RESOLVE_FQDN; |
| 60 | |
| 61 | if ((ppd = ppdOpenFile(getenv("PPD"))) != NULL) |
| 62 | { |
| 63 | if ((ppdattr = ppdFindAttr(ppd, "cupsIPPFaxOut", NULL)) != NULL && |
| 64 | !_cups_strcasecmp(ppdattr->value, "true")) |
| 65 | options |= _HTTP_RESOLVE_FAXOUT; |
| 66 | |
| 67 | ppdClose(ppd); |
| 68 | } |
| 69 | |
| 70 | return (_httpResolveURI(device_uri, cg->resolved_uri, |
| 71 | sizeof(cg->resolved_uri), options, NULL, NULL)); |
| 72 | } |
| 73 | |
| 74 | |
| 75 | /* |
| 76 | * 'cupsBackendReport()' - Write a device line from a backend. |
| 77 | * |
| 78 | * This function writes a single device line to stdout for a backend. |
| 79 | * It handles quoting of special characters in the device-make-and-model, |
| 80 | * device-info, device-id, and device-location strings. |
| 81 | * |
| 82 | * @since CUPS 1.4/macOS 10.6@ |
| 83 | */ |
| 84 | |
| 85 | void |
| 86 | cupsBackendReport( |
| 87 | const char *device_scheme, /* I - device-scheme string */ |
| 88 | const char *device_uri, /* I - device-uri string */ |
| 89 | const char *device_make_and_model, /* I - device-make-and-model string or @code NULL@ */ |
| 90 | const char *device_info, /* I - device-info string or @code NULL@ */ |
| 91 | const char *device_id, /* I - device-id string or @code NULL@ */ |
| 92 | const char *device_location) /* I - device-location string or @code NULL@ */ |
| 93 | { |
| 94 | if (!device_scheme || !device_uri) |
| 95 | return; |
| 96 | |
| 97 | printf("%s %s", device_scheme, device_uri); |
| 98 | if (device_make_and_model && *device_make_and_model) |
| 99 | quote_string(device_make_and_model); |
| 100 | else |
| 101 | quote_string("unknown"); |
| 102 | quote_string(device_info); |
| 103 | quote_string(device_id); |
| 104 | quote_string(device_location); |
| 105 | putchar('\n'); |
| 106 | fflush(stdout); |
| 107 | } |
| 108 | |
| 109 | |
| 110 | /* |
| 111 | * 'quote_string()' - Write a quoted string to stdout, escaping \ and ". |
| 112 | */ |
| 113 | |
| 114 | static void |
| 115 | quote_string(const char *s) /* I - String to write */ |
| 116 | { |
| 117 | fputs(" \"", stdout); |
| 118 | |
| 119 | if (s) |
| 120 | { |
| 121 | while (*s) |
| 122 | { |
| 123 | if (*s == '\\' || *s == '\"') |
| 124 | putchar('\\'); |
| 125 | |
| 126 | if (((*s & 255) < ' ' && *s != '\t') || *s == 0x7f) |
| 127 | putchar(' '); |
| 128 | else |
| 129 | putchar(*s); |
| 130 | |
| 131 | s ++; |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | putchar('\"'); |
| 136 | } |