]>
git.ipfire.org Git - thirdparty/cups.git/blob - filter/textcommon.c
0700d211b6da2a59a8a6588a0bbb6ea9003937a8
2 * "$Id: textcommon.c,v 1.16.2.1 2001/05/13 18:38:21 mike Exp $"
4 * Common text filter routines for the Common UNIX Printing System (CUPS).
6 * Copyright 1997-2001 by Easy Software Products.
8 * These coded instructions, statements, and computer programs are the
9 * property of Easy Software Products and are protected by Federal
10 * copyright law. Distribution and use rights are outlined in the file
11 * "LICENSE.txt" which should have been included with this file. If this
12 * file is missing or damaged please contact Easy Software Products
15 * Attn: CUPS Licensing Information
16 * Easy Software Products
17 * 44141 Airport View Drive, Suite 204
18 * Hollywood, Maryland 20636-3111 USA
20 * Voice: (301) 373-9603
21 * EMail: cups-info@cups.org
22 * WWW: http://www.cups.org
26 * TextMain() - Standard main entry for text filters.
27 * compare_keywords() - Compare two C/C++ keywords.
28 * getutf8() - Get a UTF-8 encoded wide character...
32 * Include necessary headers...
35 #include "textcommon.h"
42 int WrapLines
= 1, /* Wrap text in lines */
43 SizeLines
= 60, /* Number of lines on a page */
44 SizeColumns
= 80, /* Number of columns on a line */
45 PageColumns
= 1, /* Number of columns on a page */
46 ColumnGutter
= 0, /* Number of characters between text columns */
47 ColumnWidth
= 80, /* Width of each column */
48 PrettyPrint
= 0, /* Do pretty code formatting */
49 Copies
= 1; /* Number of copies */
50 lchar_t
**Page
= NULL
; /* Page characters */
51 int NumPages
= 0; /* Number of pages in document */
52 int CharsPerInch
= 10; /* Number of character columns per inch */
53 int LinesPerInch
= 6; /* Number of lines per inch */
54 int UTF8
= 0; /* Use UTF-8 encoding? */
55 int NumKeywords
= 0; /* Number of known keywords */
56 char **Keywords
= NULL
; /* List of known keywords */
63 static char *code_keywords
[] = /* List of known C/C++ keywords... */
132 *sh_keywords
[] = /* List of known Boure/Korn/zsh/bash keywords... */
188 *csh_keywords
[] = /* List of known csh/tcsh keywords... */
256 *perl_keywords
[] = /* List of known perl keywords... */
476 static int compare_keywords(const void *, const void *);
477 static int getutf8(FILE *fp
);
481 * 'TextMain()' - Standard main entry for text filters.
484 int /* O - Exit status */
485 TextMain(const char *name
, /* I - Name of filter */
486 int argc
, /* I - Number of command-line arguments */
487 char *argv
[]) /* I - Command-line arguments */
489 FILE *fp
; /* Print file */
490 ppd_file_t
*ppd
; /* PPD file */
491 int i
, /* Looping var */
492 ch
, /* Current char from file */
493 lastch
, /* Previous char from file */
494 attr
, /* Current attribute */
495 line
, /* Current line */
496 column
, /* Current column */
497 page_column
; /* Current page column */
498 int num_options
; /* Number of print options */
499 cups_option_t
*options
; /* Print options */
500 const char *val
; /* Option value */
501 char keyword
[64], /* Keyword string */
502 *keyptr
; /* Pointer into string */
503 int keycol
; /* Column where keyword starts */
504 int ccomment
; /* Inside a C-style comment? */
505 int cstring
; /* Inside a C string */
509 * Make sure status messages are not buffered...
512 setbuf(stderr
, NULL
);
515 * Check command-line...
518 if (argc
< 6 || argc
> 7)
520 fprintf(stderr
, "ERROR: %s job-id user title copies options [file]\n",
526 * If we have 7 arguments, print the file named on the command-line.
527 * Otherwise, send stdin instead...
535 * Try to open the print file...
538 if ((fp
= fopen(argv
[6], "rb")) == NULL
)
540 perror("ERROR: unable to open print file - ");
546 * Process command-line options and write the prolog...
550 num_options
= cupsParseOptions(argv
[5], 0, &options
);
552 if ((val
= cupsGetOption("prettyprint", num_options
, options
)) != NULL
)
555 PageRight
= PageWidth
- 36.0f
;
556 PageBottom
= PageBottom
> 36.0f
? PageBottom
: 36.0f
;
557 PageTop
= PageLength
- 36.0f
;
561 if ((val
= getenv("CONTENT_TYPE")) == NULL
)
563 PrettyPrint
= PRETTY_CODE
;
564 NumKeywords
= sizeof(code_keywords
) / sizeof(code_keywords
[0]);
565 Keywords
= code_keywords
;
567 else if (strcasecmp(val
, "application/x-cshell") == 0)
569 PrettyPrint
= PRETTY_SHELL
;
570 NumKeywords
= sizeof(csh_keywords
) / sizeof(csh_keywords
[0]);
571 Keywords
= csh_keywords
;
573 else if (strcasecmp(val
, "application/x-perl") == 0)
575 PrettyPrint
= PRETTY_PERL
;
576 NumKeywords
= sizeof(perl_keywords
) / sizeof(perl_keywords
[0]);
577 Keywords
= perl_keywords
;
579 else if (strcasecmp(val
, "application/x-shell") == 0)
581 PrettyPrint
= PRETTY_SHELL
;
582 NumKeywords
= sizeof(sh_keywords
) / sizeof(sh_keywords
[0]);
583 Keywords
= sh_keywords
;
587 PrettyPrint
= PRETTY_CODE
;
588 NumKeywords
= sizeof(code_keywords
) / sizeof(code_keywords
[0]);
589 Keywords
= code_keywords
;
593 ppd
= SetCommonOptions(num_options
, options
, 1);
595 WrapLines
= cupsGetOption("nowrap", num_options
, options
) == NULL
;
597 if ((val
= cupsGetOption("columns", num_options
, options
)) != NULL
)
598 PageColumns
= atoi(val
);
600 if ((val
= cupsGetOption("cpi", num_options
, options
)) != NULL
)
601 CharsPerInch
= atoi(val
);
603 if ((val
= cupsGetOption("lpi", num_options
, options
)) != NULL
)
604 LinesPerInch
= atoi(val
);
606 if ((val
= cupsGetOption("prettyprint", num_options
, options
)) != NULL
)
607 PageTop
-= 216.0f
/ LinesPerInch
;
609 Copies
= atoi(argv
[4]);
611 WriteProlog(argv
[3], argv
[2], getenv("CLASSIFICATION"),
612 cupsGetOption("page-label", num_options
, options
), ppd
);
615 * Read text from the specified source and print it...
628 while ((ch
= getutf8(fp
)) >= 0)
633 * BS Backspace (0x08)
634 * HT Horizontal tab; next 8th column (0x09)
635 * LF Line feed; forward full line (0x0a)
636 * VT Vertical tab; reverse full line (0x0b)
637 * FF Form feed (0x0c)
638 * CR Carriage return (0x0d)
639 * ESC 7 Reverse full line (0x1b 0x37)
640 * ESC 8 Reverse half line (0x1b 0x38)
641 * ESC 9 Forward half line (0x1b 0x39)
646 case 0x08 : /* BS - backspace for boldface & underline */
654 case 0x09 : /* HT - tab to next 8th column */
655 if (PrettyPrint
&& keyptr
> keyword
)
660 if (bsearch(&keyptr
, Keywords
, NumKeywords
, sizeof(char *),
664 * Put keywords in boldface...
667 i
= page_column
* (ColumnWidth
+ ColumnGutter
);
669 while (keycol
< column
)
671 Page
[line
][keycol
+ i
].attr
|= ATTR_BOLD
;
677 column
= (column
+ 8) & ~7;
679 if (column
>= ColumnWidth
&& WrapLines
)
680 { /* Wrap text to margins */
684 if (line
>= SizeLines
)
689 if (page_column
>= PageColumns
)
700 case 0x0a : /* LF - output current line */
701 if (PrettyPrint
&& keyptr
> keyword
)
706 if (bsearch(&keyptr
, Keywords
, NumKeywords
, sizeof(char *),
710 * Put keywords in boldface...
713 i
= page_column
* (ColumnWidth
+ ColumnGutter
);
715 while (keycol
< column
)
717 Page
[line
][keycol
+ i
].attr
|= ATTR_BOLD
;
727 if (!ccomment
&& !cstring
)
728 attr
&= ~(ATTR_ITALIC
| ATTR_BOLD
| ATTR_RED
| ATTR_GREEN
| ATTR_BLUE
);
730 if (line
>= SizeLines
)
735 if (page_column
>= PageColumns
)
743 case 0x0b : /* VT - move up 1 line */
750 if (!ccomment
&& !cstring
)
751 attr
&= ~(ATTR_ITALIC
| ATTR_BOLD
| ATTR_RED
| ATTR_GREEN
| ATTR_BLUE
);
754 case 0x0c : /* FF - eject current page... */
755 if (PrettyPrint
&& keyptr
> keyword
)
760 if (bsearch(&keyptr
, Keywords
, NumKeywords
, sizeof(char *),
764 * Put keywords in boldface...
767 i
= page_column
* (ColumnWidth
+ ColumnGutter
);
769 while (keycol
< column
)
771 Page
[line
][keycol
+ i
].attr
|= ATTR_BOLD
;
782 if (!ccomment
&& !cstring
)
783 attr
&= ~(ATTR_ITALIC
| ATTR_BOLD
| ATTR_RED
| ATTR_GREEN
| ATTR_BLUE
);
785 if (page_column
>= PageColumns
)
796 case 0x1b : /* Escape sequence */
801 * ESC 7 Reverse full line (0x1b 0x37)
810 * ESC 8 Reverse half line (0x1b 0x38)
813 if ((attr
& ATTR_RAISED
) && line
> 0)
815 attr
&= ~ATTR_RAISED
;
818 else if (attr
& ATTR_LOWERED
)
819 attr
&= ~ATTR_LOWERED
;
826 * ESC 9 Forward half line (0x1b 0x39)
829 if ((attr
& ATTR_LOWERED
) && line
< (SizeLines
- 1))
831 attr
&= ~ATTR_LOWERED
;
834 else if (attr
& ATTR_RAISED
)
835 attr
&= ~ATTR_RAISED
;
837 attr
|= ATTR_LOWERED
;
841 default : /* All others... */
843 break; /* Ignore other control chars */
848 * Do highlighting of C/C++ keywords, preprocessor commands,
852 if ((ch
== ' ' || ch
== '\t') && (attr
& ATTR_BOLD
))
855 * Stop bolding preprocessor command...
860 else if (!(isalnum(ch
) || ch
== '_') && keyptr
> keyword
)
863 * Look for a keyword...
869 if (!(attr
& ATTR_ITALIC
) &&
870 bsearch(&keyptr
, Keywords
, NumKeywords
, sizeof(char *),
874 * Put keywords in boldface...
877 i
= page_column
* (ColumnWidth
+ ColumnGutter
);
879 while (keycol
< column
)
881 Page
[line
][keycol
+ i
].attr
|= ATTR_BOLD
;
886 else if ((isalnum(ch
) || ch
== '_') && !ccomment
&& !cstring
)
889 * Add characters to the current keyword (if they'll fit).
892 if (keyptr
== keyword
)
895 if (keyptr
< (keyword
+ sizeof(keyword
) - 1))
898 else if (ch
== '\"' && lastch
!= '\\' && !ccomment
&& !cstring
)
901 * Start a C string constant...
907 else if (ch
== '*' && lastch
== '/' && !cstring
&&
908 PrettyPrint
!= PRETTY_SHELL
)
911 * Start a C-style comment...
915 attr
|= ATTR_ITALIC
| ATTR_GREEN
;
917 else if (ch
== '/' && lastch
== '/' && !cstring
&&
918 PrettyPrint
== PRETTY_CODE
)
921 * Start a C++-style comment...
924 attr
|= ATTR_ITALIC
| ATTR_GREEN
;
926 else if (ch
== '#' && !cstring
&& PrettyPrint
!= PRETTY_CODE
)
929 * Start a shell-style comment...
932 attr
|= ATTR_ITALIC
| ATTR_GREEN
;
934 else if (ch
== '#' && column
== 0 && !ccomment
&& !cstring
&&
935 PrettyPrint
== PRETTY_CODE
)
938 * Start a preprocessor command...
941 attr
|= ATTR_BOLD
| ATTR_RED
;
945 if (column
>= ColumnWidth
&& WrapLines
)
946 { /* Wrap text to margins */
950 if (line
>= SizeLines
)
955 if (page_column
>= PageColumns
)
964 * Add text to the current column & line...
967 if (column
< ColumnWidth
)
969 i
= column
+ page_column
* (ColumnWidth
+ ColumnGutter
);
972 Page
[line
][i
].attr
= attr
;
973 else if (ch
== Page
[line
][i
].ch
)
974 Page
[line
][i
].attr
|= ATTR_BOLD
;
975 else if (Page
[line
][i
].ch
== '_')
976 Page
[line
][i
].attr
|= ATTR_UNDERLINE
;
978 Page
[line
][i
].attr
= attr
;
980 Page
[line
][i
].ch
= ch
;
985 if ((ch
== '{' || ch
== '}') && !ccomment
&& !cstring
&&
986 column
< ColumnWidth
)
989 * Highlight curley braces...
992 Page
[line
][column
].attr
|= ATTR_BOLD
;
994 else if ((ch
== '/' || ch
== '*') && lastch
== '/' &&
995 column
< ColumnWidth
&& PrettyPrint
!= PRETTY_SHELL
)
998 * Highlight first comment character...
1001 Page
[line
][column
- 1].attr
= attr
;
1003 else if (ch
== '\"' && lastch
!= '\\' && !ccomment
&& cstring
> 0)
1006 * End a C string constant...
1012 else if (ch
== '/' && lastch
== '*' && ccomment
)
1015 * End a C-style comment...
1019 attr
&= ~(ATTR_ITALIC
| ATTR_GREEN
);
1031 * Save this character for the next cycle.
1038 * Write any remaining page data...
1041 if (line
> 0 || page_column
> 0 || column
> 0)
1045 * Write the epilog and return...
1058 * 'compare_keywords()' - Compare two C/C++ keywords.
1061 static int /* O - Result of strcmp */
1062 compare_keywords(const void *k1
, /* I - First keyword */
1063 const void *k2
) /* I - Second keyword */
1065 return (strcmp(*((const char **)k1
), *((const char **)k2
)));
1070 * 'getutf8()' - Get a UTF-8 encoded wide character...
1073 static int /* O - Character or -1 on error */
1074 getutf8(FILE *fp
) /* I - File to read from */
1076 int ch
; /* Current character value */
1077 int next
; /* Next character from file */
1081 * Read the first character and process things accordingly...
1083 * UTF-8 maps 16-bit characters to:
1085 * 0 to 127 = 0xxxxxxx
1086 * 128 to 2047 = 110xxxxx 10yyyyyy (xxxxxyyyyyy)
1087 * 2048 to 65535 = 1110xxxx 10yyyyyy 10zzzzzz (xxxxyyyyyyzzzzzz)
1091 * 128 to 191 = 10xxxxxx
1093 * since this range of values is otherwise undefined unless you are
1094 * in the middle of a multi-byte character...
1096 * This code currently does not support anything beyond 16-bit
1097 * characters, in part because PostScript doesn't support more than
1098 * 16-bit characters...
1101 if ((ch
= getc(fp
)) == EOF
)
1104 if (ch
< 0xc0 || !UTF8
) /* One byte character? */
1106 else if ((ch
& 0xe0) == 0xc0)
1109 * Two byte character...
1112 if ((next
= getc(fp
)) == EOF
)
1115 return (((ch
& 0x1f) << 6) | (next
& 0x3f));
1117 else if ((ch
& 0xf0) == 0xe0)
1120 * Three byte character...
1123 if ((next
= getc(fp
)) == EOF
)
1126 ch
= ((ch
& 0x0f) << 6) | (next
& 0x3f);
1128 if ((next
= getc(fp
)) == EOF
)
1131 return ((ch
<< 6) | (next
& 0x3f));
1136 * More than three bytes... We don't support that...
1145 * End of "$Id: textcommon.c,v 1.16.2.1 2001/05/13 18:38:21 mike Exp $".