Пример #1
0
static int				/* O - 0 on success, 1 on failure */
check_file(const char *filename)	/* I - File to read from */
{
  int		i;			/* Looping var */
  cups_file_t	*fp;			/* File */
  char		line[1024];		/* Line from file */
  int		ch;			/* Current character */
  size_t	bytes;			/* Length of line */
  int		status;			/* Status of test */
  int		linenum;		/* Line number */
  int		binary;			/* File contains binary data? */
  float		version;		/* DSC version */
  int		lbrt[4];		/* Bounding box */
  char		page_label[256];	/* Page label string */
  int		page_number;		/* Page number */
  int		last_page_number;	/* Last page number seen */
  int		level;			/* Embedded document level */
  int		saw_bounding_box,	/* %%BoundingBox seen? */
		saw_pages,		/* %%Pages seen? */
		saw_end_comments,	/* %%EndComments seen? */
		saw_begin_prolog,	/* %%BeginProlog seen? */
		saw_end_prolog,		/* %%EndProlog seen? */
		saw_begin_setup,	/* %%BeginSetup seen? */
		saw_end_setup,		/* %%EndSetup seen? */
		saw_page,		/* %%Page seen? */
		saw_trailer,		/* %%Trailer seen? */
		saw_long_line;		/* Saw long lines? */


 /*
  * Open the file...
  */

  if (!strcmp(filename, "(stdin)"))
    fp = cupsFileStdin();
  else
    fp = cupsFileOpen(filename, "r");

  if (!fp)
  {
    perror(filename);
    return (1);
  }

 /*
  * Scan the file...
  */

  binary           = 0;
  last_page_number = 0;
  level            = 0;
  linenum          = 0;
  saw_begin_prolog = 0;
  saw_begin_setup  = 0;
  saw_bounding_box = 0;
  saw_end_comments = 0;
  saw_end_prolog   = 0;
  saw_end_setup    = 0;
  saw_long_line    = 0;
  saw_page         = 0;
  saw_pages        = 0;
  saw_trailer      = 0;
  status           = 0;
  version          = 0.0f;

  /* TODO: Fixme */
  printf("%s: ", filename);
  fflush(stdout);

  while ((bytes = cupsFileGetLine(fp, line, sizeof(line))) > 0)
  {
    linenum ++;

    if (bytes > 255)
    {
      if (!saw_long_line)
      {
	if (!status)
          _cupsLangPuts(stdout, _("FAIL"));

	status ++;
	_cupsLangPrintf(stdout,
                	_("    Line %d is longer than 255 characters (%d).\n"
		          "        REF: Page 25, Line Length"),
			linenum, (int)bytes);
      }

      saw_long_line ++;
    }

    if (linenum == 1)
    {
      if (strncmp(line, "%!PS-Adobe-", 11))
      {
	if (!status)
          _cupsLangPuts(stdout, _("FAIL"));

	_cupsLangPuts(stdout,
	              _("    Missing %!PS-Adobe-3.0 on first line.\n"
		        "        REF: Page 17, 3.1 Conforming Documents"));
	cupsFileClose(fp);
	return (1);
      }
      else
        version = atof(line + 11);
    }
    else if (level > 0)
    {
      if (!strncmp(line, "%%BeginDocument:", 16))
        level ++;
      else if (!strncmp(line, "%%EndDocument", 13))
        level --;
    }
    else if (saw_trailer)
    {
      if (!strncmp(line, "%%Pages:", 8))
      {
        if (atoi(line + 8) <= 0)
	{
	  if (!status)
            _cupsLangPuts(stdout, _("FAIL"));

	  status ++;
	  _cupsLangPrintf(stdout,
	                  _("    Bad %%%%Pages: on line %d.\n"
		            "        REF: Page 43, %%%%Pages:"),
			  linenum);
	}
	else
          saw_pages = 1;
      }
      else if (!strncmp(line, "%%BoundingBox:", 14))
      {
	if (sscanf(line + 14, "%d%d%d%d", lbrt + 0, lbrt + 1, lbrt + 2,
	           lbrt + 3) != 4)
        {
	  if (!status)
            _cupsLangPuts(stdout, _("FAIL"));

	  status ++;
	  _cupsLangPrintf(stdout, _("    Bad %%%%BoundingBox: on line %d.\n"
		        	    "        REF: Page 39, %%%%BoundingBox:"),
			  linenum);
	}
	else
          saw_bounding_box = 1;
      }
    }
    else if (!saw_end_comments)
    {
      if (!strncmp(line, "%%EndComments", 13))
        saw_end_comments = 1;
      else if (line[0] != '%')
        saw_end_comments = -1;
      else if (!strncmp(line, "%%Pages:", 8))
      {
        if (strstr(line + 8, "(atend)"))
	  saw_pages = -1;
	else if (atoi(line + 8) <= 0)
	{
	  if (!status)
            _cupsLangPuts(stdout, _("FAIL"));

	  status ++;
	  _cupsLangPrintf(stdout, _("    Bad %%%%Pages: on line %d.\n"
		        	    "        REF: Page 43, %%%%Pages:"),
			  linenum);
	}
	else
          saw_pages = 1;
      }
      else if (!strncmp(line, "%%BoundingBox:", 14))
      {
        if (strstr(line, "(atend)"))
	  saw_bounding_box = -1;
	else if (sscanf(line + 14, "%d%d%d%d", lbrt + 0, lbrt + 1, lbrt + 2,
	                lbrt + 3) != 4)
        {
	  if (!status)
            _cupsLangPuts(stdout, _("FAIL"));

	  status ++;
	  _cupsLangPrintf(stdout, _("    Bad %%%%BoundingBox: on line %d.\n"
		        	    "        REF: Page 39, %%%%BoundingBox:"),
			  linenum);
	}
	else
          saw_bounding_box = 1;
      }
    }
    else if (saw_begin_prolog && !saw_end_prolog)
    {
      if (!strncmp(line, "%%EndProlog", 11))
        saw_end_prolog = 1;
    }
    else if (saw_begin_setup && !saw_end_setup)
    {
      if (!strncmp(line, "%%EndSetup", 10))
        saw_end_setup = 1;
    }
    else if (saw_end_comments)
    {
      if (!strncmp(line, "%%Page:", 7))
      {
        if (sscanf(line + 7, "%255s%d", page_label, &page_number) != 2 ||
	    page_number != (last_page_number + 1) || page_number < 1)
	{
	  if (!status)
            _cupsLangPuts(stdout, _("FAIL"));

	  status ++;
	  _cupsLangPrintf(stdout, _("    Bad %%%%Page: on line %d.\n"
		        	    "        REF: Page 53, %%%%Page:"),
		          linenum);
	}
	else
	{
	  last_page_number = page_number;
	  saw_page         = 1;
	}
      }
      else if (!strncmp(line, "%%BeginProlog", 13))
        saw_begin_prolog = 1;
      else if (!strncmp(line, "%%BeginSetup", 12))
        saw_begin_setup = 1;
      else if (!strncmp(line, "%%BeginDocument:", 16))
        level ++;
      else if (!strncmp(line, "%%EndDocument", 13))
        level --;
      else if (!strncmp(line, "%%Trailer", 9))
        saw_trailer = 1;
    }

    for (i = 0; !binary && i < bytes; i ++)
    {
      ch = line[i];

      if ((ch < ' ' || (ch & 0x80)) && ch != '\n' && ch != '\r' && ch != '\t')
        binary = 1;
    }
  }

  if (saw_bounding_box <= 0)
  {
    if (!status)
      _cupsLangPuts(stdout, _("FAIL"));

    status ++;
    _cupsLangPuts(stdout, _("    Missing or bad %%BoundingBox: comment.\n"
		            "        REF: Page 39, %%BoundingBox:"));
  }

  if (saw_pages <= 0)
  {
    if (!status)
      _cupsLangPuts(stdout, _("FAIL"));

    status ++;
    _cupsLangPuts(stdout, _("    Missing or bad %%Pages: comment.\n"
		            "        REF: Page 43, %%Pages:"));
  }

  if (!saw_end_comments)
  {
    if (!status)
      _cupsLangPuts(stdout, _("FAIL"));

    status ++;
    _cupsLangPuts(stdout, _("    Missing %%EndComments comment."
		            "        REF: Page 41, %%EndComments"));
  }

  if (!saw_page)
  {
    if (!status)
      _cupsLangPuts(stdout, _("FAIL"));

    status ++;
    _cupsLangPuts(stdout, _("    Missing or bad %%Page: comments.\n"
		            "        REF: Page 53, %%Page:"));
  }

  if (level < 0)
  {
    if (!status)
      _cupsLangPuts(stdout, _("FAIL"));

    status ++;
    _cupsLangPuts(stdout, _("    Too many %%EndDocument comments."));
  }
  else if (level > 0)
  {
    if (!status)
      _cupsLangPuts(stdout, _("FAIL"));

    status ++;
    _cupsLangPuts(stdout, _("    Too many %%BeginDocument comments."));
  }

  if (saw_long_line > 1)
    _cupsLangPrintf(stderr,
                    _("    Saw %d lines that exceeded 255 characters."),
                    saw_long_line);

  if (!status)
    _cupsLangPuts(stdout, _("PASS"));

  if (binary)
    _cupsLangPuts(stdout, _("    Warning: file contains binary data."));

  if (version < 3.0f)
    _cupsLangPrintf(stdout,
                    _("    Warning: obsolete DSC version %.1f in file."),
		    version);

  if (saw_end_comments < 0)
    _cupsLangPuts(stdout, _("    Warning: no %%EndComments comment in file."));

  cupsFileClose(fp);

  return (status);
}
Пример #2
0
int main (int argc, char **argv)
{
	cups_file_t	*fp_input =  NULL;			/* input file: stdin or physical file */	
    char   line[LINE_SIZE] = {0};
	
	get_LogLevel();
	setbuf (stderr, NULL);
	fprintf (stderr, "HP PS filter starting : %s \n", *argv);
	
	int i =  0;
	/* Logging cups filter arguments */
	for (   i = 0; i < argc; i++)
	{
		fprintf (stderr, "DEBUG: hppsfilter: argv[%d] = %s\n", i, argv[i]);
	}
	
	/* Logging debug information and creating outfile for debug */
	if (g_savepsfile & SAVE_PS_FILE)
	{
		/* opening ps debug file */
		open_dbg_outfile(argv[1]);
	}
	
	/* Check command-line...  */
	if (argc < 6 || argc > 7)
	{
		fputs("ERROR: hppsfilter job-id user title copies options [file]\n", stderr);
		return (1);
	}

	/* If we have 7 arguments, print the file named on the command-line.
	* Otherwise, send stdin instead...  */
	if (argc == 6)
		fp_input = cupsFileStdin();
	else
	{
		/* Try to open the print file...    */
		if ((fp_input = cupsFileOpen(argv[6], "r")) == NULL)
		{
  			fprintf(stderr, "ERROR: Unable to open print file \"%s\" - %s\n",
              argv[6], strerror);
  			return (1);
		}
	}

	int len  =  0;
	int ibodwritten = 0;
	int ireqire_bod = require_bod();
    while ( ( len = cupsFileGetLine(fp_input, line, sizeof(line) )) > 0)
    {
		hpwrite (line, len);
		/* if BOD is reqire, writting BOD command */
		if ( !ibodwritten && (!strncmp(line, "@PJL JOB NAME", 13)) && ireqire_bod)
		{
			fprintf (stderr, "HP PS filter: Matched start PJL\n");				
			ibodwritten = 1;
			emmit_bod_command();
		}
    }

    if ( (argc == 7) && (fp_input != NULL) )
        cupsFileClose (fp_input);
	
	if(g_fp_outdbgps != NULL)
	{
	    fclose (g_fp_outdbgps);		
	}
			   
	fprintf (stderr, "HP PS filter Ends\n");	
    return 0;
}