]>
git.ipfire.org Git - thirdparty/cups.git/blob - filter/textcommon.c
2 * "$Id: textcommon.c 6649 2007-07-11 21:46:42Z mike $"
4 * Common text filter routines for the Common UNIX Printing System (CUPS).
6 * Copyright 2007 by Apple Inc.
7 * Copyright 1997-2007 by Easy Software Products.
9 * These coded instructions, statements, and computer programs are the
10 * property of Apple Inc. and are protected by Federal copyright
11 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
12 * which should have been included with this file. If this file is
13 * file is missing or damaged, see the license at "http://www.cups.org/".
15 * This file is subject to the Apple OS-Developed Software exception.
19 * TextMain() - Standard main entry for text filters.
20 * compare_keywords() - Compare two C/C++ keywords.
21 * getutf8() - Get a UTF-8 encoded wide character...
25 * Include necessary headers...
28 #include "textcommon.h"
29 #include <cups/i18n.h>
36 int WrapLines
= 1, /* Wrap text in lines */
37 SizeLines
= 60, /* Number of lines on a page */
38 SizeColumns
= 80, /* Number of columns on a line */
39 PageColumns
= 1, /* Number of columns on a page */
40 ColumnGutter
= 0, /* Number of characters between text columns */
41 ColumnWidth
= 80, /* Width of each column */
42 PrettyPrint
= 0, /* Do pretty code formatting */
43 Copies
= 1; /* Number of copies */
44 lchar_t
**Page
= NULL
; /* Page characters */
45 int NumPages
= 0; /* Number of pages in document */
46 float CharsPerInch
= 10; /* Number of character columns per inch */
47 float LinesPerInch
= 6; /* Number of lines per inch */
48 int UTF8
= 0; /* Use UTF-8 encoding? */
49 int NumKeywords
= 0; /* Number of known keywords */
50 char **Keywords
= NULL
; /* List of known keywords */
57 static char *code_keywords
[] = /* List of known C/C++ keywords... */
130 *sh_keywords
[] = /* List of known Boure/Korn/zsh/bash keywords... */
186 *csh_keywords
[] = /* List of known csh/tcsh keywords... */
254 *perl_keywords
[] = /* List of known perl keywords... */
474 static int compare_keywords(const void *, const void *);
475 static int getutf8(FILE *fp
);
479 * 'TextMain()' - Standard main entry for text filters.
482 int /* O - Exit status */
483 TextMain(const char *name
, /* I - Name of filter */
484 int argc
, /* I - Number of command-line arguments */
485 char *argv
[]) /* I - Command-line arguments */
487 FILE *fp
; /* Print file */
488 ppd_file_t
*ppd
; /* PPD file */
489 int i
, /* Looping var */
490 ch
, /* Current char from file */
491 lastch
, /* Previous char from file */
492 attr
, /* Current attribute */
493 line
, /* Current line */
494 column
, /* Current column */
495 page_column
; /* Current page column */
496 int num_options
; /* Number of print options */
497 cups_option_t
*options
; /* Print options */
498 const char *val
; /* Option value */
499 char keyword
[64], /* Keyword string */
500 *keyptr
; /* Pointer into string */
501 int keycol
; /* Column where keyword starts */
502 int ccomment
; /* Inside a C-style comment? */
503 int cstring
; /* Inside a C string */
507 * Make sure status messages are not buffered...
510 setbuf(stderr
, NULL
);
513 * Check command-line...
516 if (argc
< 6 || argc
> 7)
518 fprintf(stderr
, _("Usage: %s job-id user title copies options [file]\n"),
524 * If we have 7 arguments, print the file named on the command-line.
525 * Otherwise, send stdin instead...
533 * Try to open the print file...
536 if ((fp
= fopen(argv
[6], "rb")) == NULL
)
538 perror("DEBUG: unable to open print file - ");
544 * Process command-line options and write the prolog...
548 num_options
= cupsParseOptions(argv
[5], 0, &options
);
550 if ((val
= cupsGetOption("prettyprint", num_options
, options
)) != NULL
&&
551 strcasecmp(val
, "no") && strcasecmp(val
, "off") &&
552 strcasecmp(val
, "false"))
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_PLAIN
;
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-csource") == 0)
575 PrettyPrint
= PRETTY_CODE
;
576 NumKeywords
= sizeof(code_keywords
) / sizeof(code_keywords
[0]);
577 Keywords
= code_keywords
;
579 else if (strcasecmp(val
, "application/x-perl") == 0)
581 PrettyPrint
= PRETTY_PERL
;
582 NumKeywords
= sizeof(perl_keywords
) / sizeof(perl_keywords
[0]);
583 Keywords
= perl_keywords
;
585 else if (strcasecmp(val
, "application/x-shell") == 0)
587 PrettyPrint
= PRETTY_SHELL
;
588 NumKeywords
= sizeof(sh_keywords
) / sizeof(sh_keywords
[0]);
589 Keywords
= sh_keywords
;
593 PrettyPrint
= PRETTY_PLAIN
;
599 ppd
= SetCommonOptions(num_options
, options
, 1);
601 if ((val
= cupsGetOption("wrap", num_options
, options
)) == NULL
)
604 WrapLines
= !strcasecmp(val
, "true") || !strcasecmp(val
, "on") ||
605 !strcasecmp(val
, "yes");
607 if ((val
= cupsGetOption("columns", num_options
, options
)) != NULL
)
608 PageColumns
= atoi(val
);
610 if ((val
= cupsGetOption("cpi", num_options
, options
)) != NULL
)
611 CharsPerInch
= atof(val
);
613 if ((val
= cupsGetOption("lpi", num_options
, options
)) != NULL
)
614 LinesPerInch
= atof(val
);
617 PageTop
-= 216.0f
/ LinesPerInch
;
619 Copies
= atoi(argv
[4]);
621 WriteProlog(argv
[3], argv
[2], getenv("CLASSIFICATION"),
622 cupsGetOption("page-label", num_options
, options
), ppd
);
625 * Read text from the specified source and print it...
638 while ((ch
= getutf8(fp
)) >= 0)
643 * BS Backspace (0x08)
644 * HT Horizontal tab; next 8th column (0x09)
645 * LF Line feed; forward full line (0x0a)
646 * VT Vertical tab; reverse full line (0x0b)
647 * FF Form feed (0x0c)
648 * CR Carriage return (0x0d)
649 * ESC 7 Reverse full line (0x1b 0x37)
650 * ESC 8 Reverse half line (0x1b 0x38)
651 * ESC 9 Forward half line (0x1b 0x39)
656 case 0x08 : /* BS - backspace for boldface & underline */
664 case 0x09 : /* HT - tab to next 8th column */
665 if (PrettyPrint
&& keyptr
> keyword
)
670 if (bsearch(&keyptr
, Keywords
, NumKeywords
, sizeof(char *),
674 * Put keywords in boldface...
677 i
= page_column
* (ColumnWidth
+ ColumnGutter
);
679 while (keycol
< column
)
681 Page
[line
][keycol
+ i
].attr
|= ATTR_BOLD
;
687 column
= (column
+ 8) & ~7;
689 if (column
>= ColumnWidth
&& WrapLines
)
690 { /* Wrap text to margins */
694 if (line
>= SizeLines
)
699 if (page_column
>= PageColumns
)
715 * All but MacOS/Darwin treat CR as was intended by ANSI
716 * folks, namely to move to column 0/1. Some programs still
717 * use this to do boldfacing and underlining...
724 * MacOS/Darwin still need to treat CR as a line ending.
729 if ((nextch
= getc(fp
)) != 0x0a)
734 #endif /* !__APPLE__ */
736 case 0x0a : /* LF - output current line */
737 if (PrettyPrint
&& keyptr
> keyword
)
742 if (bsearch(&keyptr
, Keywords
, NumKeywords
, sizeof(char *),
746 * Put keywords in boldface...
749 i
= page_column
* (ColumnWidth
+ ColumnGutter
);
751 while (keycol
< column
)
753 Page
[line
][keycol
+ i
].attr
|= ATTR_BOLD
;
763 if (!ccomment
&& !cstring
)
764 attr
&= ~(ATTR_ITALIC
| ATTR_BOLD
| ATTR_RED
| ATTR_GREEN
| ATTR_BLUE
);
766 if (line
>= SizeLines
)
771 if (page_column
>= PageColumns
)
779 case 0x0b : /* VT - move up 1 line */
786 if (!ccomment
&& !cstring
)
787 attr
&= ~(ATTR_ITALIC
| ATTR_BOLD
| ATTR_RED
| ATTR_GREEN
| ATTR_BLUE
);
790 case 0x0c : /* FF - eject current page... */
791 if (PrettyPrint
&& keyptr
> keyword
)
796 if (bsearch(&keyptr
, Keywords
, NumKeywords
, sizeof(char *),
800 * Put keywords in boldface...
803 i
= page_column
* (ColumnWidth
+ ColumnGutter
);
805 while (keycol
< column
)
807 Page
[line
][keycol
+ i
].attr
|= ATTR_BOLD
;
818 if (!ccomment
&& !cstring
)
819 attr
&= ~(ATTR_ITALIC
| ATTR_BOLD
| ATTR_RED
| ATTR_GREEN
| ATTR_BLUE
);
821 if (page_column
>= PageColumns
)
828 case 0x1b : /* Escape sequence */
833 * ESC 7 Reverse full line (0x1b 0x37)
842 * ESC 8 Reverse half line (0x1b 0x38)
845 if ((attr
& ATTR_RAISED
) && line
> 0)
847 attr
&= ~ATTR_RAISED
;
850 else if (attr
& ATTR_LOWERED
)
851 attr
&= ~ATTR_LOWERED
;
858 * ESC 9 Forward half line (0x1b 0x39)
861 if ((attr
& ATTR_LOWERED
) && line
< (SizeLines
- 1))
863 attr
&= ~ATTR_LOWERED
;
866 else if (attr
& ATTR_RAISED
)
867 attr
&= ~ATTR_RAISED
;
869 attr
|= ATTR_LOWERED
;
873 default : /* All others... */
875 break; /* Ignore other control chars */
877 if (PrettyPrint
> PRETTY_PLAIN
)
880 * Do highlighting of C/C++ keywords, preprocessor commands,
884 if (ch
== ' ' && (attr
& ATTR_BOLD
))
887 * Stop bolding preprocessor command...
892 else if (!(isalnum(ch
& 255) || ch
== '_') && keyptr
> keyword
)
895 * Look for a keyword...
901 if (!(attr
& ATTR_ITALIC
) &&
902 bsearch(&keyptr
, Keywords
, NumKeywords
, sizeof(char *),
906 * Put keywords in boldface...
909 i
= page_column
* (ColumnWidth
+ ColumnGutter
);
911 while (keycol
< column
)
913 Page
[line
][keycol
+ i
].attr
|= ATTR_BOLD
;
918 else if ((isalnum(ch
& 255) || ch
== '_') && !ccomment
&& !cstring
)
921 * Add characters to the current keyword (if they'll fit).
924 if (keyptr
== keyword
)
927 if (keyptr
< (keyword
+ sizeof(keyword
) - 1))
930 else if (ch
== '\"' && lastch
!= '\\' && !ccomment
&& !cstring
)
933 * Start a C string constant...
939 else if (ch
== '*' && lastch
== '/' && !cstring
&&
940 PrettyPrint
!= PRETTY_SHELL
)
943 * Start a C-style comment...
947 attr
= ATTR_ITALIC
| ATTR_GREEN
;
949 else if (ch
== '/' && lastch
== '/' && !cstring
&&
950 PrettyPrint
== PRETTY_CODE
)
953 * Start a C++-style comment...
956 attr
= ATTR_ITALIC
| ATTR_GREEN
;
958 else if (ch
== '#' && !cstring
&& PrettyPrint
!= PRETTY_CODE
)
961 * Start a shell-style comment...
964 attr
= ATTR_ITALIC
| ATTR_GREEN
;
966 else if (ch
== '#' && column
== 0 && !ccomment
&& !cstring
&&
967 PrettyPrint
== PRETTY_CODE
)
970 * Start a preprocessor command...
973 attr
= ATTR_BOLD
| ATTR_RED
;
977 if (column
>= ColumnWidth
&& WrapLines
)
978 { /* Wrap text to margins */
982 if (line
>= SizeLines
)
987 if (page_column
>= PageColumns
)
996 * Add text to the current column & line...
999 if (column
< ColumnWidth
)
1001 i
= column
+ page_column
* (ColumnWidth
+ ColumnGutter
);
1004 Page
[line
][i
].attr
= attr
;
1005 else if (ch
== ' ' && Page
[line
][i
].ch
)
1006 ch
= Page
[line
][i
].ch
;
1007 else if (ch
== Page
[line
][i
].ch
)
1008 Page
[line
][i
].attr
|= ATTR_BOLD
;
1009 else if (Page
[line
][i
].ch
== '_')
1010 Page
[line
][i
].attr
|= ATTR_UNDERLINE
;
1013 Page
[line
][i
].attr
|= ATTR_UNDERLINE
;
1015 if (Page
[line
][i
].ch
)
1016 ch
= Page
[line
][i
].ch
;
1019 Page
[line
][i
].attr
= attr
;
1021 Page
[line
][i
].ch
= ch
;
1026 if ((ch
== '{' || ch
== '}') && !ccomment
&& !cstring
&&
1027 column
< ColumnWidth
)
1030 * Highlight curley braces...
1033 Page
[line
][column
].attr
|= ATTR_BOLD
;
1035 else if ((ch
== '/' || ch
== '*') && lastch
== '/' &&
1036 column
< ColumnWidth
&& PrettyPrint
!= PRETTY_SHELL
)
1039 * Highlight first comment character...
1042 Page
[line
][column
- 1].attr
= attr
;
1044 else if (ch
== '\"' && lastch
!= '\\' && !ccomment
&& cstring
> 0)
1047 * End a C string constant...
1053 else if (ch
== '/' && lastch
== '*' && ccomment
)
1056 * End a C-style comment...
1060 attr
&= ~(ATTR_ITALIC
| ATTR_GREEN
);
1072 * Save this character for the next cycle.
1079 * Write any remaining page data...
1082 if (line
> 0 || page_column
> 0 || column
> 0)
1086 * Write the epilog and return...
1099 * 'compare_keywords()' - Compare two C/C++ keywords.
1102 static int /* O - Result of strcmp */
1103 compare_keywords(const void *k1
, /* I - First keyword */
1104 const void *k2
) /* I - Second keyword */
1106 return (strcmp(*((const char **)k1
), *((const char **)k2
)));
1111 * 'getutf8()' - Get a UTF-8 encoded wide character...
1114 static int /* O - Character or -1 on error */
1115 getutf8(FILE *fp
) /* I - File to read from */
1117 int ch
; /* Current character value */
1118 int next
; /* Next character from file */
1122 * Read the first character and process things accordingly...
1124 * UTF-8 maps 16-bit characters to:
1126 * 0 to 127 = 0xxxxxxx
1127 * 128 to 2047 = 110xxxxx 10yyyyyy (xxxxxyyyyyy)
1128 * 2048 to 65535 = 1110xxxx 10yyyyyy 10zzzzzz (xxxxyyyyyyzzzzzz)
1132 * 128 to 191 = 10xxxxxx
1134 * since this range of values is otherwise undefined unless you are
1135 * in the middle of a multi-byte character...
1137 * This code currently does not support anything beyond 16-bit
1138 * characters, in part because PostScript doesn't support more than
1139 * 16-bit characters...
1142 if ((ch
= getc(fp
)) == EOF
)
1145 if (ch
< 0xc0 || !UTF8
) /* One byte character? */
1147 else if ((ch
& 0xe0) == 0xc0)
1150 * Two byte character...
1153 if ((next
= getc(fp
)) == EOF
)
1156 return (((ch
& 0x1f) << 6) | (next
& 0x3f));
1158 else if ((ch
& 0xf0) == 0xe0)
1161 * Three byte character...
1164 if ((next
= getc(fp
)) == EOF
)
1167 ch
= ((ch
& 0x0f) << 6) | (next
& 0x3f);
1169 if ((next
= getc(fp
)) == EOF
)
1172 return ((ch
<< 6) | (next
& 0x3f));
1177 * More than three bytes... We don't support that...
1186 * End of "$Id: textcommon.c 6649 2007-07-11 21:46:42Z mike $".