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