int GetDevices ( http_t * http, char *** list, oyAlloc_f allocateFunc ) { cups_dest_t *dests, *dest; int num_dests; int i, p; int len; char ** texts; /* Open access to printer(s) installed on system. */ num_dests = cupsGetDests2(http, &dests); len = sizeof(char*) * num_dests; texts = allocateFunc( len ); memset( texts, 0, len ); /* Use CUPS to obtain printer name(s) on the default server. */ for (p = 0, i = num_dests, dest = dests; i > 0; i--, dest++, p++) { texts[p] = allocateFunc(strlen(dest->name)+1); sprintf( texts[p], "%s", dest->name ); } /* Free memory that is used by CUPS. */ cupsFreeDests (num_dests, dests); /* Return printer name(s) and the number of printers installed on system. */ *list = texts; return num_dests; }
static void _pp_host_get_remote_cups_devices_thread (GSimpleAsyncResult *res, GObject *object, GCancellable *cancellable) { cups_dest_t *dests = NULL; GSDData *data; PpHost *host = (PpHost *) object; PpHostPrivate *priv = host->priv; PpPrintDevice *device; http_t *http; gint num_of_devices = 0; gint port; gint i; data = g_simple_async_result_get_op_res_gpointer (res); data->devices = g_new0 (PpDevicesList, 1); data->devices->devices = NULL; if (priv->port == PP_HOST_UNSET_PORT) port = PP_HOST_DEFAULT_IPP_PORT; else port = priv->port; /* Connect to remote CUPS server and get its devices */ http = httpConnect (priv->hostname, port); if (http) { num_of_devices = cupsGetDests2 (http, &dests); if (num_of_devices > 0) { for (i = 0; i < num_of_devices; i++) { device = g_new0 (PpPrintDevice, 1); device->device_class = g_strdup ("network"); device->device_uri = g_strdup_printf ("ipp://%s:%d/printers/%s", priv->hostname, port, dests[i].name); device->device_name = g_strdup (dests[i].name); device->device_location = g_strdup (cupsGetOption ("printer-location", dests[i].num_options, dests[i].options)); device->host_name = g_strdup (priv->hostname); device->host_port = port; device->acquisition_method = ACQUISITION_METHOD_REMOTE_CUPS_SERVER; data->devices->devices = g_list_append (data->devices->devices, device); } } httpClose (http); } }
int /* O - Exit status */ main(int argc, /* I - Number of command-line arguments */ char *argv[]) /* I - Command-line arguments */ { int i, j; /* Looping vars */ int status; /* Status from export_dest() */ int export_all; /* Export all printers? */ http_t *http; /* Connection to server */ int num_dests; /* Number of printers */ cups_dest_t *dests; /* Printers */ _cupsSetLocale(argv); /* * Parse command-line arguments... */ export_all = 0; http = NULL; SAMBAUser = cupsUser(); SAMBAPassword = NULL; SAMBAServer = NULL; for (i = 1; i < argc; i ++) if (!strcmp(argv[i], "-E")) { #ifdef HAVE_SSL cupsSetEncryption(HTTP_ENCRYPT_REQUIRED); #else _cupsLangPrintf(stderr, _("%s: Sorry, no encryption support."), argv[0]); #endif /* HAVE_SSL */ } else if (!strcmp(argv[i], "-H")) { i ++; if (i >= argc) usage(); SAMBAServer = argv[i]; } else if (!strcmp(argv[i], "-U")) { char *sep; /* Separator for password */ i ++; if (i >= argc) usage(); SAMBAUser = argv[i]; if ((sep = strchr(argv[i], '%')) != NULL) { /* * Nul-terminate the username at the first % and point the * password at the rest... */ *sep++ = '\0'; SAMBAPassword = sep; } } else if (!strcmp(argv[i], "-a")) export_all = 1; else if (!strcmp(argv[i], "-h")) { i ++; if (i >= argc) usage(); cupsSetServer(argv[i]); } else if (!strcmp(argv[i], "-v")) Verbosity = 1; else if (argv[i][0] != '-') { if (!http) { /* * Connect to the server... */ if ((http = httpConnectEncrypt(cupsServer(), ippPort(), cupsEncryption())) == NULL) { _cupsLangPrintf(stderr, _("%s: Unable to connect to server."), argv[0]); exit(1); } } if (SAMBAServer == NULL) { SAMBAServer = cupsServer(); if (SAMBAServer[0] == '/') /* Use localhost instead of domain socket */ SAMBAServer = "localhost"; } if ((status = export_dest(http, argv[i])) != 0) return (status); } else usage(); /* * Connect to the server... */ if ((http = httpConnectEncrypt(cupsServer(), ippPort(), cupsEncryption())) == NULL) { _cupsLangPrintf(stderr, _("%s: Unable to connect to server."), argv[0]); exit(1); } /* * See if the user specified "-a"... */ if (export_all) { /* * Export all printers... */ if (SAMBAServer == NULL) { SAMBAServer = cupsServer(); if (SAMBAServer[0] == '/') /* Use localhost instead of domain socket */ SAMBAServer = "localhost"; } num_dests = cupsGetDests2(http, &dests); for (j = 0, status = 0; j < num_dests; j ++) if (!dests[j].instance) { if ((status = export_dest(http, dests[j].name)) != 0) break; } cupsFreeDests(num_dests, dests); if (status) return (status); } return (0); }
int /* O - Exit status */ main(int argc, /* I - Number of command-line arguments */ char *argv[]) /* I - Command-line arguments */ { int i; /* Looping var */ http_t *http; /* Connection to destination */ cups_dest_t *dest = NULL; /* Destination */ cups_dinfo_t *dinfo; /* Destination info */ unsigned dflags = CUPS_DEST_FLAGS_NONE; /* Destination flags */ if (argc < 2) return (0); if (!strcmp(argv[1], "--get")) { cups_dest_t *dests; /* Destinations */ int num_dests = cupsGetDests2(CUPS_HTTP_DEFAULT, &dests); /* Number of destinations */ for (i = 0; i < num_dests; i ++) enum_cb(NULL, 0, dests + i); cupsFreeDests(num_dests, dests); return (0); } else if (!strcmp(argv[1], "--enum")) { cups_ptype_t type = 0, /* Printer type filter */ mask = 0; /* Printer type mask */ for (i = 2; i < argc; i ++) { if (!strcmp(argv[i], "grayscale")) { type |= CUPS_PRINTER_BW; mask |= CUPS_PRINTER_BW; } else if (!strcmp(argv[i], "color")) { type |= CUPS_PRINTER_COLOR; mask |= CUPS_PRINTER_COLOR; } else if (!strcmp(argv[i], "duplex")) { type |= CUPS_PRINTER_DUPLEX; mask |= CUPS_PRINTER_DUPLEX; } else if (!strcmp(argv[i], "staple")) { type |= CUPS_PRINTER_STAPLE; mask |= CUPS_PRINTER_STAPLE; } else if (!strcmp(argv[i], "small")) { type |= CUPS_PRINTER_SMALL; mask |= CUPS_PRINTER_SMALL; } else if (!strcmp(argv[i], "medium")) { type |= CUPS_PRINTER_MEDIUM; mask |= CUPS_PRINTER_MEDIUM; } else if (!strcmp(argv[i], "large")) { type |= CUPS_PRINTER_LARGE; mask |= CUPS_PRINTER_LARGE; } else usage(argv[i]); } cupsEnumDests(CUPS_DEST_FLAGS_NONE, 5000, NULL, type, mask, enum_cb, NULL); return (0); } i = 1; if (!strcmp(argv[i], "--device")) { dflags = CUPS_DEST_FLAGS_DEVICE; i ++; } if (!strncmp(argv[i], "ipp://", 6) || !strncmp(argv[i], "ipps://", 7)) dest = cupsGetDestWithURI(NULL, argv[i]); else if (!strcmp(argv[i], "default")) { dest = cupsGetNamedDest(CUPS_HTTP_DEFAULT, NULL, NULL); if (dest && dest->instance) printf("default is \"%s/%s\".\n", dest->name, dest->instance); else if (dest) printf("default is \"%s\".\n", dest->name); else puts("no default destination."); } else dest = cupsGetNamedDest(CUPS_HTTP_DEFAULT, argv[i], NULL); if (!dest) { printf("testdest: Unable to get destination \"%s\": %s\n", argv[i], cupsLastErrorString()); return (1); } i ++; if ((http = cupsConnectDest(dest, dflags, 30000, NULL, NULL, 0, NULL, NULL)) == NULL) { printf("testdest: Unable to connect to destination \"%s\": %s\n", dest->name, cupsLastErrorString()); return (1); } if ((dinfo = cupsCopyDestInfo(http, dest)) == NULL) { printf("testdest: Unable to get information for destination \"%s\": %s\n", dest->name, cupsLastErrorString()); return (1); } if (i == argc || !strcmp(argv[i], "supported")) { i ++; if ((i + 1) < argc) show_supported(http, dest, dinfo, argv[i], argv[i + 1]); else if (argc > 2) show_supported(http, dest, dinfo, argv[i], NULL); else show_supported(http, dest, dinfo, NULL, NULL); } else if (!strcmp(argv[i], "conflicts") && (i + 1) < argc) { int num_options = 0;/* Number of options */ cups_option_t *options = NULL;/* Options */ for (i ++; i < argc; i ++) num_options = cupsParseOptions(argv[i], num_options, &options); show_conflicts(http, dest, dinfo, num_options, options); } else if (!strcmp(argv[i], "default") && (i + 1) < argc) { show_default(http, dest, dinfo, argv[i + 1]); } else if (!strcmp(argv[i], "localize")) { i ++; if ((i + 1) < argc) localize(http, dest, dinfo, argv[i], argv[i + 1]); else if (argc > 2) localize(http, dest, dinfo, argv[i], NULL); else localize(http, dest, dinfo, NULL, NULL); } else if (!strcmp(argv[i], "media")) { const char *name = NULL; /* Media name, if any */ unsigned flags = CUPS_MEDIA_FLAGS_DEFAULT; /* Media selection flags */ for (i ++; i < argc; i ++) { if (!strcmp(argv[i], "borderless")) flags = CUPS_MEDIA_FLAGS_BORDERLESS; else if (!strcmp(argv[i], "duplex")) flags = CUPS_MEDIA_FLAGS_DUPLEX; else if (!strcmp(argv[i], "exact")) flags = CUPS_MEDIA_FLAGS_EXACT; else if (!strcmp(argv[i], "ready")) flags = CUPS_MEDIA_FLAGS_READY; else if (name) usage(argv[i]); else name = argv[i]; } show_media(http, dest, dinfo, flags, name); } else if (!strcmp(argv[i], "print") && (i + 1) < argc) { int num_options = 0;/* Number of options */ cups_option_t *options = NULL;/* Options */ const char *filename = argv[i + 1]; for (i += 2; i < argc; i ++) num_options = cupsParseOptions(argv[i], num_options, &options); print_file(http, dest, dinfo, filename, num_options, options); } else usage(argv[i]); return (0); }
int /* O - Exit status */ main(int argc, /* I - Number of command-line arguments */ char *argv[]) /* I - Command-line arguments */ { http_t *http; /* HTTP connection to server */ int i; /* Looping var */ int job_id; /* Job ID */ const char *dest; /* Destination printer */ char *instance; /* Pointer to instance name */ char uri[1024]; /* Printer or job URI */ ipp_t *request; /* IPP request */ ipp_t *response; /* IPP response */ ipp_op_t op; /* Operation */ int num_dests; /* Number of destinations */ cups_dest_t *dests, /* Destinations */ *defdest; /* Default destination */ http_encryption_t encryption; /* Encryption? */ _cupsSetLocale(argv); /* * Setup to cancel individual print jobs... */ op = IPP_CANCEL_JOB; job_id = 0; dest = NULL; response = NULL; http = NULL; encryption = cupsEncryption(); /* * Open a connection to the server... */ if ((http = httpConnectEncrypt(cupsServer(), ippPort(), encryption)) == NULL) { _cupsLangPuts(stderr, _("lprm: Unable to contact server!\n")); return (1); } num_dests = cupsGetDests2(http, &dests); defdest = cupsGetDest(NULL, NULL, num_dests, dests); dest = defdest ? defdest->name : NULL; /* * Process command-line arguments... */ for (i = 1; i < argc; i ++) if (argv[i][0] == '-' && argv[i][1] != '\0') switch (argv[i][1]) { case 'E' : /* Encrypt */ #ifdef HAVE_SSL encryption = HTTP_ENCRYPT_REQUIRED; httpEncryption(http, encryption); cupsSetEncryption(encryption); #else _cupsLangPrintf(stderr, _("%s: Sorry, no encryption support compiled in!\n"), argv[0]); #endif /* HAVE_SSL */ break; case 'P' : /* Cancel jobs on a printer */ if (argv[i][2]) dest = argv[i] + 2; else { i ++; dest = argv[i]; } if ((instance = strchr(dest, '/')) != NULL) *instance = '\0'; if (cupsGetDest(dest, NULL, num_dests, dests) == NULL) { _cupsLangPrintf(stderr, _("%s: Error - unknown destination \"%s\"!\n"), argv[0], dest); cupsFreeDests(num_dests, dests); httpClose(http); return(1); } break; case 'U' : /* Username */ if (argv[i][2] != '\0') cupsSetUser(argv[i] + 2); else { i ++; if (i >= argc) { _cupsLangPrintf(stderr, _("%s: Error - expected username after " "\'-U\' option!\n"), argv[0]); return (1); } cupsSetUser(argv[i]); } break; case 'h' : /* Connect to host */ if (argv[i][2] != '\0') cupsSetServer(argv[i] + 2); else { i ++; if (i >= argc) { _cupsLangPrintf(stderr, _("%s: Error - expected hostname after " "\'-h\' option!\n"), argv[0]); return (1); } else cupsSetServer(argv[i]); } httpClose(http); cupsFreeDests(num_dests, dests); if ((http = httpConnectEncrypt(cupsServer(), ippPort(), encryption)) == NULL) { _cupsLangPuts(stderr, _("lprm: Unable to contact server!\n")); return (1); } num_dests = cupsGetDests2(http, &dests); defdest = cupsGetDest(NULL, NULL, num_dests, dests); dest = defdest ? defdest->name : NULL; break; default : _cupsLangPrintf(stderr, _("%s: Error - unknown option \'%c\'!\n"), argv[0], argv[i][1]); cupsFreeDests(num_dests, dests); httpClose(http); return (1); } else { /* * Cancel a job or printer... */ if (isdigit(argv[i][0] & 255) && cupsGetDest(argv[i], NULL, num_dests, dests) == NULL) { dest = NULL; op = IPP_CANCEL_JOB; job_id = atoi(argv[i]); } else if (!strcmp(argv[i], "-")) { /* * Cancel all jobs */ op = IPP_PURGE_JOBS; } else { dest = argv[i]; job_id = 0; } /* * Build an IPP request, which requires the following * attributes: * * attributes-charset * attributes-natural-language * printer-uri + job-id *or* job-uri * [requesting-user-name] */ request = ippNewRequest(op); if (dest) { httpAssembleURIf(HTTP_URI_CODING_ALL, uri, sizeof(uri), "ipp", NULL, "localhost", 0, "/printers/%s", dest); ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", NULL, uri); ippAddInteger(request, IPP_TAG_OPERATION, IPP_TAG_INTEGER, "job-id", job_id); } else { sprintf(uri, "ipp://localhost/jobs/%d", job_id); ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "job-uri", NULL, uri); } ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name", NULL, cupsUser()); /* * Do the request and get back a response... */ if (op == IPP_PURGE_JOBS) response = cupsDoRequest(http, request, "/admin/"); else response = cupsDoRequest(http, request, "/jobs/"); ippDelete(response); if (cupsLastError() > IPP_OK_CONFLICT) { _cupsLangPrintf(stderr, "%s: %s\n", argv[0], cupsLastErrorString()); cupsFreeDests(num_dests, dests); httpClose(http); return (1); } } /* * If nothing has been canceled yet, cancel the current job on the specified * (or default) printer... */ if (response == NULL) if (!cupsCancelJob(dest, 0)) { _cupsLangPrintf(stderr, "%s: %s\n", argv[0], cupsLastErrorString()); cupsFreeDests(num_dests, dests); httpClose(http); return (1); } cupsFreeDests(num_dests, dests); httpClose(http); return (0); }