⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 print_cups.c

📁 samba-3.0.22.tar.gz 编译smb服务器的源码
💻 C
📖 第 1 页 / 共 2 页
字号:
/* * Support code for the Common UNIX Printing System ("CUPS") * * Copyright 1999-2003 by Michael R Sweet. * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. *  * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the * GNU General Public License for more details. *  * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */#include "includes.h"#include "printing.h"#ifdef HAVE_CUPS#include <cups/cups.h>#include <cups/language.h>/* * 'cups_passwd_cb()' - The CUPS password callback... */static const char *				/* O - Password or NULL */cups_passwd_cb(const char *prompt)	/* I - Prompt */{	/*	 * Always return NULL to indicate that no password is available...	 */	return (NULL);}static const char *cups_server(void){	if ((lp_cups_server() != NULL) && (strlen(lp_cups_server()) > 0)) {		DEBUG(10, ("cups server explicitly set to %s\n",			   lp_cups_server()));		return lp_cups_server();	}	DEBUG(10, ("cups server left to default %s\n", cupsServer()));	return cupsServer();}BOOL cups_cache_reload(void){	http_t		*http = NULL;		/* HTTP connection to server */	ipp_t		*request = NULL,	/* IPP Request */			*response = NULL;	/* IPP Response */	ipp_attribute_t	*attr;		/* Current attribute */	cups_lang_t	*language = NULL;	/* Default language */	char		*name,		/* printer-name attribute */			*info;		/* printer-info attribute */	static const char *requested[] =/* Requested attributes */			{			  "printer-name",			  "printer-info"			};       	BOOL ret = False;	DEBUG(5, ("reloading cups printcap cache\n"));       /*        * Make sure we don't ask for passwords...	*/        cupsSetPasswordCB(cups_passwd_cb);       /*	* Try to connect to the server...	*/	if ((http = httpConnect(cups_server(), ippPort())) == NULL) {		DEBUG(0,("Unable to connect to CUPS server %s - %s\n", 			 cups_server(), strerror(errno)));		goto out;	}       /*	* Build a CUPS_GET_PRINTERS request, which requires the following	* attributes:	*	*    attributes-charset	*    attributes-natural-language	*    requested-attributes	*/	request = ippNew();	request->request.op.operation_id = CUPS_GET_PRINTERS;	request->request.op.request_id   = 1;	language = cupsLangDefault();	ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,                     "attributes-charset", NULL, cupsLangEncoding(language));	ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,                     "attributes-natural-language", NULL, language->language);        ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_NAME,	              "requested-attributes",		      (sizeof(requested) / sizeof(requested[0])),		      NULL, requested);       /*	* Do the request and get back a response...	*/	if ((response = cupsDoRequest(http, request, "/")) == NULL) {		DEBUG(0,("Unable to get printer list - %s\n",			 ippErrorString(cupsLastError())));		goto out;	}	for (attr = response->attrs; attr != NULL;) {	       /*		* Skip leading attributes until we hit a printer...		*/		while (attr != NULL && attr->group_tag != IPP_TAG_PRINTER)			attr = attr->next;		if (attr == NULL)        		break;	       /*		* Pull the needed attributes from this printer...		*/		name       = NULL;		info       = NULL;		while (attr != NULL && attr->group_tag == IPP_TAG_PRINTER) {        		if (strcmp(attr->name, "printer-name") == 0 &&			    attr->value_tag == IPP_TAG_NAME)				name = attr->values[0].string.text;        		if (strcmp(attr->name, "printer-info") == 0 &&			    attr->value_tag == IPP_TAG_TEXT)				info = attr->values[0].string.text;        		attr = attr->next;		}	       /*		* See if we have everything needed...		*/		if (name == NULL)			break;		if (!pcap_cache_add(name, info)) {			goto out;		}	}	ippDelete(response);	response = NULL;       /*	* Build a CUPS_GET_CLASSES request, which requires the following	* attributes:	*	*    attributes-charset	*    attributes-natural-language	*    requested-attributes	*/	request = ippNew();	request->request.op.operation_id = CUPS_GET_CLASSES;	request->request.op.request_id   = 1;	ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,                     "attributes-charset", NULL, cupsLangEncoding(language));	ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,                     "attributes-natural-language", NULL, language->language);        ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_NAME,	              "requested-attributes",		      (sizeof(requested) / sizeof(requested[0])),		      NULL, requested);       /*	* Do the request and get back a response...	*/	if ((response = cupsDoRequest(http, request, "/")) == NULL) {		DEBUG(0,("Unable to get printer list - %s\n",			 ippErrorString(cupsLastError())));		goto out;	}	for (attr = response->attrs; attr != NULL;) {	       /*		* Skip leading attributes until we hit a printer...		*/		while (attr != NULL && attr->group_tag != IPP_TAG_PRINTER)			attr = attr->next;		if (attr == NULL)        		break;	       /*		* Pull the needed attributes from this printer...		*/		name       = NULL;		info       = NULL;		while (attr != NULL && attr->group_tag == IPP_TAG_PRINTER) {        		if (strcmp(attr->name, "printer-name") == 0 &&			    attr->value_tag == IPP_TAG_NAME)				name = attr->values[0].string.text;        		if (strcmp(attr->name, "printer-info") == 0 &&			    attr->value_tag == IPP_TAG_TEXT)				info = attr->values[0].string.text;        		attr = attr->next;		}	       /*		* See if we have everything needed...		*/		if (name == NULL)			break;		if (!pcap_cache_add(name, info)) {			goto out;		}	}	ret = True; out:	if (response)		ippDelete(response);	if (language)		cupsLangFree(language);	if (http)		httpClose(http);	return ret;}/* * 'cups_job_delete()' - Delete a job. */static int cups_job_delete(const char *sharename, const char *lprm_command, struct printjob *pjob){	int		ret = 1;		/* Return value */	http_t		*http = NULL;		/* HTTP connection to server */	ipp_t		*request = NULL,	/* IPP Request */			*response = NULL;	/* IPP Response */	cups_lang_t	*language = NULL;	/* Default language */	char		uri[HTTP_MAX_URI]; /* printer-uri attribute */	DEBUG(5,("cups_job_delete(%s, %p (%d))\n", sharename, pjob, pjob->sysjob));       /*        * Make sure we don't ask for passwords...	*/        cupsSetPasswordCB(cups_passwd_cb);       /*	* Try to connect to the server...	*/	if ((http = httpConnect(cups_server(), ippPort())) == NULL) {		DEBUG(0,("Unable to connect to CUPS server %s - %s\n", 			 cups_server(), strerror(errno)));		goto out;	}       /*	* Build an IPP_CANCEL_JOB request, which requires the following	* attributes:	*	*    attributes-charset	*    attributes-natural-language	*    job-uri	*    requesting-user-name	*/	request = ippNew();	request->request.op.operation_id = IPP_CANCEL_JOB;	request->request.op.request_id   = 1;	language = cupsLangDefault();	ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,        	     "attributes-charset", NULL, cupsLangEncoding(language));	ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,        	     "attributes-natural-language", NULL, language->language);	slprintf(uri, sizeof(uri) - 1, "ipp://localhost/jobs/%d", pjob->sysjob);	ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "job-uri", NULL, uri);	ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name",        	     NULL, pjob->user);       /*	* Do the request and get back a response...	*/	if ((response = cupsDoRequest(http, request, "/jobs")) != NULL) {		if (response->request.status.status_code >= IPP_OK_CONFLICT) {			DEBUG(0,("Unable to cancel job %d - %s\n", pjob->sysjob,				ippErrorString(cupsLastError())));		} else {			ret = 0;		}	} else {		DEBUG(0,("Unable to cancel job %d - %s\n", pjob->sysjob,			ippErrorString(cupsLastError())));	} out:	if (response)		ippDelete(response);	if (language)		cupsLangFree(language);	if (http)		httpClose(http);	return ret;}/* * 'cups_job_pause()' - Pause a job. */static int cups_job_pause(int snum, struct printjob *pjob){	int		ret = 1;		/* Return value */	http_t		*http = NULL;		/* HTTP connection to server */	ipp_t		*request = NULL,	/* IPP Request */			*response = NULL;	/* IPP Response */	cups_lang_t	*language = NULL;	/* Default language */	char		uri[HTTP_MAX_URI]; /* printer-uri attribute */	DEBUG(5,("cups_job_pause(%d, %p (%d))\n", snum, pjob, pjob->sysjob));       /*        * Make sure we don't ask for passwords...	*/        cupsSetPasswordCB(cups_passwd_cb);       /*	* Try to connect to the server...	*/	if ((http = httpConnect(cups_server(), ippPort())) == NULL) {		DEBUG(0,("Unable to connect to CUPS server %s - %s\n", 			 cups_server(), strerror(errno)));		goto out;	}       /*	* Build an IPP_HOLD_JOB request, which requires the following	* attributes:	*	*    attributes-charset	*    attributes-natural-language	*    job-uri	*    requesting-user-name	*/	request = ippNew();	request->request.op.operation_id = IPP_HOLD_JOB;	request->request.op.request_id   = 1;	language = cupsLangDefault();	ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,        	     "attributes-charset", NULL, cupsLangEncoding(language));	ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,        	     "attributes-natural-language", NULL, language->language);	slprintf(uri, sizeof(uri) - 1, "ipp://localhost/jobs/%d", pjob->sysjob);	ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "job-uri", NULL, uri);	ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name",        	     NULL, pjob->user);       /*	* Do the request and get back a response...	*/	if ((response = cupsDoRequest(http, request, "/jobs")) != NULL) {		if (response->request.status.status_code >= IPP_OK_CONFLICT) {			DEBUG(0,("Unable to hold job %d - %s\n", pjob->sysjob,				ippErrorString(cupsLastError())));		} else {			ret = 0;		}	} else {		DEBUG(0,("Unable to hold job %d - %s\n", pjob->sysjob,			ippErrorString(cupsLastError())));	} out:	if (response)		ippDelete(response);	if (language)		cupsLangFree(language);	if (http)		httpClose(http);	return ret;}/* * 'cups_job_resume()' - Resume a paused job. */static int cups_job_resume(int snum, struct printjob *pjob){	int		ret = 1;		/* Return value */	http_t		*http = NULL;		/* HTTP connection to server */	ipp_t		*request = NULL,	/* IPP Request */			*response = NULL;	/* IPP Response */	cups_lang_t	*language = NULL;	/* Default language */	char		uri[HTTP_MAX_URI]; /* printer-uri attribute */	DEBUG(5,("cups_job_resume(%d, %p (%d))\n", snum, pjob, pjob->sysjob));       /*        * Make sure we don't ask for passwords...	*/        cupsSetPasswordCB(cups_passwd_cb);       /*	* Try to connect to the server...	*/	if ((http = httpConnect(cups_server(), ippPort())) == NULL) {		DEBUG(0,("Unable to connect to CUPS server %s - %s\n", 			 cups_server(), strerror(errno)));		goto out;	}       /*	* Build an IPP_RELEASE_JOB request, which requires the following	* attributes:	*	*    attributes-charset	*    attributes-natural-language	*    job-uri	*    requesting-user-name	*/	request = ippNew();	request->request.op.operation_id = IPP_RELEASE_JOB;	request->request.op.request_id   = 1;	language = cupsLangDefault();	ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,        	     "attributes-charset", NULL, cupsLangEncoding(language));	ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,        	     "attributes-natural-language", NULL, language->language);	slprintf(uri, sizeof(uri) - 1, "ipp://localhost/jobs/%d", pjob->sysjob);	ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "job-uri", NULL, uri);	ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name",        	     NULL, pjob->user);       /*	* Do the request and get back a response...	*/	if ((response = cupsDoRequest(http, request, "/jobs")) != NULL) {		if (response->request.status.status_code >= IPP_OK_CONFLICT) {			DEBUG(0,("Unable to release job %d - %s\n", pjob->sysjob,				ippErrorString(cupsLastError())));		} else {			ret = 0;		}	} else {		DEBUG(0,("Unable to release job %d - %s\n", pjob->sysjob,			ippErrorString(cupsLastError())));	} out:	if (response)		ippDelete(response);	if (language)		cupsLangFree(language);	if (http)		httpClose(http);	return ret;}/* * 'cups_job_submit()' - Submit a job for printing. */static int cups_job_submit(int snum, struct printjob *pjob){	int		ret = 1;		/* Return value */	http_t		*http = NULL;		/* HTTP connection to server */	ipp_t		*request = NULL,	/* IPP Request */			*response = NULL;	/* IPP Response */	cups_lang_t	*language = NULL;	/* Default language */	char		uri[HTTP_MAX_URI]; /* printer-uri attribute */	char 		*clientname = NULL; 	/* hostname of client for job-originating-host attribute */	pstring		new_jobname;	int		num_options = 0; 	cups_option_t 	*options = NULL;	DEBUG(5,("cups_job_submit(%d, %p (%d))\n", snum, pjob, pjob->sysjob));       /*        * Make sure we don't ask for passwords...	*/        cupsSetPasswordCB(cups_passwd_cb);       /*	* Try to connect to the server...	*/	if ((http = httpConnect(cups_server(), ippPort())) == NULL) {		DEBUG(0,("Unable to connect to CUPS server %s - %s\n", 			 cups_server(), strerror(errno)));		goto out;	}       /*	* Build an IPP_PRINT_JOB request, which requires the following	* attributes:	*	*    attributes-charset	*    attributes-natural-language	*    printer-uri	*    requesting-user-name	*    [document-data]	*/	request = ippNew();	request->request.op.operation_id = IPP_PRINT_JOB;	request->request.op.request_id   = 1;	language = cupsLangDefault();	ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,        	     "attributes-charset", NULL, cupsLangEncoding(language));	ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,        	     "attributes-natural-language", NULL, language->language);	slprintf(uri, sizeof(uri) - 1, "ipp://localhost/printers/%s",	         PRINTERNAME(snum));	ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI,

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -