Exemplo n.º 1
0
char* make_filter_param_line(char *model, int reso, ParamList *pl)
{
	ParamList *cmd_list = NULL;
	char *cmd_buf = NULL;
	char **cmd_param;

	cmd_param = make_filter_param_list(model, reso, pl, &cmd_list);

	if( cmd_param )
	{
		int buf_len = 0;
		int i;

		for( i = 0 ; cmd_param[i] != NULL ; i++ )
		{
			if( i == 1 )
				continue;
			buf_len += strlen(cmd_param[i]) + 2;
		}

		cmd_buf = malloc(buf_len);

		if( cmd_buf != NULL )
		{
			*cmd_buf = 0;

			for( i = 0 ; cmd_param[i] != NULL ; i++ )
			{
				if( i == 1 )
					continue;
				strcat(cmd_buf, cmd_param[i]);
				strcat(cmd_buf, " ");
			}
			cmd_buf[buf_len - 2] = 0;
		}
		free(cmd_param);
	}

	if( cmd_list != NULL )
		param_list_free(cmd_list);

	return cmd_buf;
}
Exemplo n.º 2
0
int main(int argc, char *argv[])
{
	cups_option_t *p_cups_opt = NULL;
	int num_opt = 0;
	ParamList *p_ps_param = NULL;
	BufList *p_ps_data = NULL;
	char *p_data_buf;
	int ifd = 0;
	int fds[2];
	struct sigaction sigact;
	char *cmd_buf = NULL;

	char product[256];
#ifdef	DEBUG_SLEEP
	sleep(30);
#endif

	setbuf(stderr, NULL);
	fprintf(stderr, "DEBUG: pstocanonbj start.\n");

	memset(&sigact, 0, sizeof(sigact));
	sigact.sa_handler = sigterm_handler;

	if( sigaction(SIGTERM, &sigact, NULL) )
	{
		fputs("ERROR: pstocanonbj can't register signal hander.\n", stderr);
		return 1;
	}

	if( argc < 6 || argc > 7 )
	{
		fputs("ERROR: pstocanonbj illegal parameter number.\n", stderr);
		return 1;
	}

	if( argv[5] != NULL )
	{
		num_opt = cupsParseOptions(argv[5], 0, &p_cups_opt);
		if( num_opt < 0 )
		{
			fputs("ERROR: illegal option.\n", stderr);
			return 1;
		}
	}

	if( argc == 7 )
	{
		if( (ifd = open(argv[6], O_RDONLY)) == -1 )
		{
			fputs("ERROR: can't open file.\n", stderr);
			return 1;
		}
	}

#ifdef	DEBUG_IN_PS
	g_in_log_fd = open("/tmp/debug_in_log.ps", O_WRONLY);
#endif
	p_ps_param = get_ps_params(ifd, &p_ps_data);

	if( (cmd_buf = make_cmd_param(p_cups_opt, num_opt, p_ps_param, product, 256)) == NULL )
	{
		fputs("ERROR: can't make parameter.\n", stderr);
		goto error_return;
	}

	if( g_signal_received )
		goto error_return;

	if( (p_data_buf = (char*)malloc(DATA_BUF_SIZE)) != NULL )
	{
#ifdef	DEBUG_PS
		int log_fd = open("/tmp/debug_data_log.ps", O_WRONLY);
#endif
		// Execute extended filter.
		g_filter_pid = exec_filter(cmd_buf, 1, fds);

		// Write retained PS header and data.
		buflist_write(p_ps_data, fds[1]);

#ifdef	DEBUG_PS
		buflist_write(p_ps_data, log_fd);
#endif

		// Write remaining PS data.
		while( !g_signal_received )
		{
			int read_bytes = read(ifd, p_data_buf, DATA_BUF_SIZE);

			if( read_bytes > 0 )
			{
				int write_bytes;
				char *p_data = p_data_buf;
#ifdef	DEBUG_PS
				write(log_fd, p_data_buf, read_bytes);
#endif
#ifdef	DEBUG_IN_PS
				if( g_in_log_fd > 0 )
					write(g_in_log_fd, p_data_buf, read_bytes);
#endif
				do
				{
					write_bytes = write(fds[1], p_data, read_bytes);

					if( write_bytes < 0 )
					{
						if( errno == EINTR )
							continue;
						fprintf(stderr,
							"ERROR: pstocanonbj write error,%d.\n", errno);
						goto error_exit;
					}
					read_bytes -= write_bytes;
					p_data += write_bytes;
				}
				while( !g_signal_received && read_bytes > 0 );
			}
			else if( read_bytes < 0 )
			{
				if( errno == EINTR )
					continue;
				fprintf(stderr, "ERROR: pstocanonbj read error,%d.\n", errno);
				goto error_exit;
			}
			else
				break;
		}

		free(p_data_buf);

#ifdef	DEBUG_PS
		if( log_fd != -1 ) close(log_fd);
#endif
	}

#ifdef	DEBUG_IN_PS
	if( g_in_log_fd > 0 )
		close(g_in_log_fd);
#endif

	free(cmd_buf);

	if( p_ps_param != NULL )
		param_list_free(p_ps_param);
	if( p_ps_data != NULL )
		buflist_destroy(p_ps_data);
	if( ifd != 0 )
		close(ifd);

	close(fds[1]);

	if( g_filter_pid != -1 )
		waitpid(g_filter_pid, NULL, 0);

	return 0;

error_exit:
	if( p_data_buf != NULL )
		free(p_data_buf);

error_return:
	free(cmd_buf);

	if( p_ps_param != NULL )
		param_list_free(p_ps_param);
	if( p_ps_data != NULL )
		buflist_destroy(p_ps_data);
	if( ifd != 0 )
		close(ifd);

	return 1;
}
Exemplo n.º 3
0
static
char* make_cmd_param(cups_option_t *p_cups_opt, int num_opt,
	ParamList *p_param, char *p_product, int len)
{
#ifdef DEBUG_PPD
	char *p_ppd_name = "debug.ppd";
#else
	char *p_ppd_name = getenv("PPD");
#endif
	ppd_file_t *p_ppd;
	char *cmd_buf = NULL;
	ppd_choice_t *p_choice;
	ppd_size_t *p_size;
	ppd_size_t *p_default_size;
	int reso;
	char gs_exec_buf[256];
	char gs_cmd_buf[1024];
	char *flt_cmd_buf = NULL;

//For InkJet
	long minw_mm, maxw_mm;
	long minl_mm, maxl_mm;
	PpdToOptKey *p_opt_key_table = alloc_opt_key_table(p_ppd_name);
	PpdToOptKey *p_table = p_opt_key_table;
	ParamList *p_list = NULL;

	if( (p_ppd = ppdOpenFile(p_ppd_name)) == NULL )
		return NULL;

	if( p_opt_key_table == NULL)
		return NULL;

	ppdMarkDefaults(p_ppd);

	// Obtain default page size setting.
	p_choice = ppdFindMarkedChoice(p_ppd, "PageSize");
	p_default_size = ppdPageSize(p_ppd, p_choice->choice);

	mark_ps_param_options(p_ppd, p_param);
	cupsMarkOptions(p_ppd, num_opt, p_cups_opt);

	// Obtain page size setting.
	p_choice = ppdFindMarkedChoice(p_ppd, "PageSize");
	p_size = ppdPageSize(p_ppd, p_choice->choice);

	if (!p_size || ((int)(p_size->width == 0)) || ((int)(p_size->length == 0))) {
	    p_size = p_default_size;
	}

	// Obtain resolution setting.
	p_choice = ppdFindMarkedChoice(p_ppd, "Resolution");
	reso = atoi(p_choice->choice);

	//Obtain product name.
	parse_product_name(p_ppd->product, p_product, len);

	get_min_max_width_length(p_opt_key_table, p_cups_opt, num_opt,
		&minw_mm, &maxw_mm, &minl_mm, &maxl_mm);

// gs command
	strncpy(gs_exec_buf, GS_PATH, 256);
	strncat(gs_exec_buf, "/", 1);
	strncat(gs_exec_buf, GS_BIN, strlen(GS_BIN));
	snprintf(gs_cmd_buf, 1023,
	"%s -r%d -g%dx%d -q -dNOPROMPT -dSAFER -sDEVICE=ppmraw -sOutputFile=- -| ",
			gs_exec_buf, reso, (int)(p_size->width * (float)reso / 72.0),
			(int)(p_size->length * (float)reso / 72.0));

// For InkJet bjfilter Command 
	while( p_table->ppd_key != NULL )
	{
		p_choice = ppdFindMarkedChoice(p_ppd, p_table->ppd_key);
		if( p_choice != NULL )
		{
			char choice[256];

			strncpy(choice, p_choice->choice, 256);

			if( !strcmp(p_table->ppd_key, "PageSize")
			 && !strcmp(choice, "Custom") )
			{
				char pw[256];
				char pl[256];
				long pw_mm, pl_mm;
				pw_mm = (long)((double)p_size->width * 2540.0 / 72.0);
				pl_mm = (long)((double)p_size->length * 2540.0 / 72.0);
				if( minw_mm != -1 && pw_mm < minw_mm )
					pw_mm = minw_mm;
				if( maxw_mm != -1 && pw_mm > maxw_mm )
					pw_mm = maxw_mm;
				if( minl_mm != -1 && pl_mm < minl_mm )
					pl_mm = minl_mm;
				if( maxl_mm != -1 && pl_mm > maxl_mm )
					pl_mm = maxw_mm;
				snprintf(pw, 255, "%ld", pw_mm);
				snprintf(pl, 255, "%ld", pl_mm);
				param_list_add(&p_list, p_table->opt_key, "user", 5);
				param_list_add(&p_list,
					"--paperwidth", pw, strlen(pw) + 1);
				param_list_add(&p_list,
					"--paperheight", pl, strlen(pl) + 1);
			}
			else
			{
				if( !strcmp(p_table->ppd_key, "PageSize")
				 && is_borderless(choice) )
				{
					param_list_add(&p_list, "--borderless", "", 1);
				}
				param_list_add(&p_list, p_table->opt_key,
					choice, strlen(choice) + 1);
			}
		}
		p_table++;
	}

	while( num_opt-- > 0 )
	{
		char *opt_key = ppd_to_opt_key(p_opt_key_table, p_cups_opt->name);
		if( opt_key != NULL )
		{
			char *value = p_cups_opt->value;
			param_list_add(&p_list, opt_key, value, strlen(value) + 1);
		}
		p_cups_opt++;
	}

	{
		char bbox_buf[256];
		float dd = 1.0 - 36.0 / (float)reso;
		int left   = (int)p_size->left;
		int top    = (int)(p_size->top + dd);
		int right  = left + (int)(p_size->right - p_size->left + dd);
		int bottom = top - (int)(p_size->top - p_size->bottom + dd);

		snprintf(bbox_buf, 255, "%-d,%-d,%-d,%-d", left, bottom, right, top);
		param_list_add(&p_list, "--bbox",
				bbox_buf, strlen(bbox_buf) + 1);
	}

	flt_cmd_buf = make_filter_param_line(p_product, reso, p_list);

	if( p_list != NULL ){
		param_list_free(p_list);
		free_opt_key_table(p_opt_key_table);
	}

	ppdClose(p_ppd);

	if( flt_cmd_buf )
	{
		int buf_len;			
		
		buf_len = strlen(gs_cmd_buf) + strlen(flt_cmd_buf) + 1;

		if( (cmd_buf = (char*)malloc(buf_len)) != NULL )
		{
			strcpy(cmd_buf, gs_cmd_buf);
			strcat(cmd_buf, flt_cmd_buf);
		}
		free(flt_cmd_buf);
fprintf(stderr, "pstocanonbj: %s\n", cmd_buf);
		return cmd_buf;
	}
	else
		return NULL;
}
Exemplo n.º 4
0
int main(int argc, char *argv[])
{
	cups_option_t *p_cups_opt = NULL;
	ParamList *p_list = NULL;
	ParamList *p_list2 = NULL;
	char *p_data_buf = NULL;
	char *cmd_buf = NULL;
	int cmd_buf_size = 0;
	struct sigaction sigact;
	int num_opt = 0,out_num = 0;
	int ifd = 0;
	int fds[2];
	int result = -1;

#if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET)
	struct sigaction action;
#endif

#ifdef HAVE_SIGSET
	sigset(SIGPIPE, SIG_IGN);
#elif defined(HAVE_SIGACTION)
	memset(&action, 0, sizeof(action));
	action.sa_handler = SIG_IGN;
	sigaction(SIGPIPE, &action, NULL);
#else
	signal(SIGPIPE, SIG_IGN);
#endif /* HAVE_SIGSET */
	/* register signal */
	memset(&sigact, 0, sizeof(sigact));
	sigact.sa_handler = sigterm_handler;

	if( sigaction(SIGTERM, &sigact, NULL) )
	{
		fputs("DEBUG:[rastertocanonij] pstocanonij can't register signal hander.\n", stderr);
		goto onErr1;
	}

	if( argc < 6 || argc > 7 ) {
		fputs("DEBUG:[rastertocanonij] pstocanonij illegal parameter number.\n", stderr);
		goto onErr1;
	}

	if( argv[5] != NULL ) {
		num_opt = cupsParseOptions(argv[5], 0, &p_cups_opt);
		if( num_opt < 0 ) {
			fputs("DEBUG:[rastertocanonij] illegal option.\n", stderr);
			goto onErr1;
		}
	}

	if( g_signal_received ) goto onErr1;


	/* Allocate buffer to communicate */
	if( (p_data_buf = (char*)malloc(DATA_BUF_SIZE)) == NULL ) goto onErr1;

	/* Parse Option, and get option list for tocanonij filter */
	if ( GetPrinterSettings( p_cups_opt, num_opt, &p_list, &out_num ) != 0 ){
		fprintf( stderr, "DEBUG:Error in GetPrinterSettings\n" );
		goto onErr2;
	}

	/* Parse PPD, and get option list for tocnpwg filter */
	if ( GetPrinterableAreaOptionFromPPD( ref_value_from_list(p_list, "--papersize"), &p_list2, &out_num ) != 0 ){
		fprintf( stderr, "DEBUG:Error in GetPrinterableAreaOptionFromPPD\n" );
		goto onErr3;
	}

	DEBUG_PRINT( "DEBUG:[rastertocanonij] rastertocanonij main <0>\n" );
	/* Get BufSize */
	if ( (cmd_buf_size = GetOptionBufSize( p_list )) < 0 ){
		fprintf( stderr, "DEBUG:[rastertocanonij] GetOptionBufSize in Error\n" );
		goto onErr4;
	}

	/* Allocate Command Buffer */
	cmd_buf_size = CMD_BUF_SIZE;
	if ( (cmd_buf = (char *)malloc( cmd_buf_size )) == NULL ) goto onErr4;

	DEBUG_PRINT( "DEBUG:[rastertocanonij] rastertocanonij main <1>\n" );
	/* Make execute command */
	if ( MakeExecCommand( cmd_buf, cmd_buf_size, p_list, p_list2 ) != 0 ){
		fprintf( stderr, "DEBUG:[rastertocanonij] MakeExecCommand in Error\n" );
		goto onErr5;
	}
	
	DEBUG_PRINT( "DEBUG:[rastertocanonij] rastertocanonij main <2>\n" );
	/*	set cmd_buf here .....*/
	DEBUG_PRINT2( "DEBUG:[rastertocanonij] cmd_buf : %s\n", cmd_buf );
	if ( (g_filter_pid = exec_filter(cmd_buf, 1, fds)) == -1 ){
		fprintf( stderr, "DEBUG:[rastertocanonij] exec_filter in Error\n" );
		goto onErr5;
	}

	DEBUG_PRINT( "DEBUG:[rastertocanonij] rastertocanonij main <3>\n" );
	/* WriteData */
	while( !g_signal_received ) {
		int read_bytes = read(ifd, p_data_buf, DATA_BUF_SIZE);
		if ( read_bytes > 0 ) {
			int write_bytes;
			char *p_data = p_data_buf;
	
			do {
				write_bytes = write(fds[1], p_data, read_bytes);
				if( write_bytes < 0 ) {
					if( errno == EINTR ) continue;
					fprintf(stderr, "DEBUG:[rastertocanonij] pstocanonij write error,%d.\n", errno);
					goto onErr6;
				}
				read_bytes -= write_bytes;
				p_data += write_bytes;
			} while( !g_signal_received && read_bytes > 0 );
		}
		else if( read_bytes < 0 ) {
			if( errno == EINTR ) continue;
			fprintf(stderr, "DEBUG:[rastertocanonij] pstocanonij read error,%d.\n", errno);
			goto onErr6;
		}
		else {
			DEBUG_PRINT( "DEBUG:[rastertocanonij] rastertocanonij main <break>\n" );
			break;
		}
	}

	DEBUG_PRINT( "DEBUG:[rastertocanonij] rastertocanonij main <4>\n" );
	result = 0;
onErr6:
	close( fds[1] );

	/* wait process */	
	if ( g_filter_pid != -1 ){
		waitpid( g_filter_pid, NULL, 0 );
	}	
onErr5:
	if( cmd_buf != NULL ){
		free( cmd_buf );
	}
onErr4:
	param_list_free( p_list2 );
onErr3:
	param_list_free( p_list );
onErr2:
	if( p_data_buf != NULL ){
		free( p_data_buf );
	}
onErr1:
	return result;
}