]>
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-2009 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 NumKeywords
= 0; /* Number of known keywords */
49 char **Keywords
= NULL
; /* List of known keywords */
56 static char *code_keywords
[] = /* List of known C/C++ keywords... */
129 *sh_keywords
[] = /* List of known Boure/Korn/zsh/bash keywords... */
185 *csh_keywords
[] = /* List of known csh/tcsh keywords... */
253 *perl_keywords
[] = /* List of known perl keywords... */
473 static int compare_keywords(const void *, const void *);
474 static int getutf8(FILE *fp
);
478 * 'TextMain()' - Standard main entry for text filters.
481 int /* O - Exit status */
482 TextMain(const char *name
, /* I - Name of filter */
483 int argc
, /* I - Number of command-line arguments */
484 char *argv
[]) /* I - Command-line arguments */
486 FILE *fp
; /* Print file */
487 ppd_file_t
*ppd
; /* PPD file */
488 int i
, /* Looping var */
489 ch
, /* Current char from file */
490 lastch
, /* Previous char from file */
491 attr
, /* Current attribute */
492 line
, /* Current line */
493 column
, /* Current column */
494 page_column
; /* Current page column */
495 int num_options
; /* Number of print options */
496 cups_option_t
*options
; /* Print options */
497 const char *val
; /* Option value */
498 char keyword
[64], /* Keyword string */
499 *keyptr
; /* Pointer into string */
500 int keycol
; /* Column where keyword starts */
501 int ccomment
; /* Inside a C-style comment? */
502 int cstring
; /* Inside a C string */
506 * Make sure status messages are not buffered...
509 setbuf(stderr
, NULL
);
512 * Check command-line...
515 if (argc
< 6 || argc
> 7)
517 _cupsLangPrintf(stderr
,
518 _("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
)
609 PageColumns
= atoi(val
);
613 _cupsLangPrintf(stderr
, _("ERROR: Bad columns value %d\n"), PageColumns
);
618 if ((val
= cupsGetOption("cpi", num_options
, options
)) != NULL
)
620 CharsPerInch
= atof(val
);
622 if (CharsPerInch
<= 0.0)
624 _cupsLangPrintf(stderr
, _("ERROR: Bad cpi value %f\n"), CharsPerInch
);
629 if ((val
= cupsGetOption("lpi", num_options
, options
)) != NULL
)
631 LinesPerInch
= atof(val
);
633 if (LinesPerInch
<= 0.0)
635 _cupsLangPrintf(stderr
, _("ERROR: Bad lpi value %f\n"), LinesPerInch
);
641 PageTop
-= 216.0f
/ LinesPerInch
;
643 Copies
= atoi(argv
[4]);
645 WriteProlog(argv
[3], argv
[2], getenv("CLASSIFICATION"),
646 cupsGetOption("page-label", num_options
, options
), ppd
);
649 * Read text from the specified source and print it...
662 while ((ch
= getutf8(fp
)) >= 0)
667 * BS Backspace (0x08)
668 * HT Horizontal tab; next 8th column (0x09)
669 * LF Line feed; forward full line (0x0a)
670 * VT Vertical tab; reverse full line (0x0b)
671 * FF Form feed (0x0c)
672 * CR Carriage return (0x0d)
673 * ESC 7 Reverse full line (0x1b 0x37)
674 * ESC 8 Reverse half line (0x1b 0x38)
675 * ESC 9 Forward half line (0x1b 0x39)
680 case 0x08 : /* BS - backspace for boldface & underline */
688 case 0x09 : /* HT - tab to next 8th column */
689 if (PrettyPrint
&& keyptr
> keyword
)
694 if (bsearch(&keyptr
, Keywords
, NumKeywords
, sizeof(char *),
698 * Put keywords in boldface...
701 i
= page_column
* (ColumnWidth
+ ColumnGutter
);
703 while (keycol
< column
)
705 Page
[line
][keycol
+ i
].attr
|= ATTR_BOLD
;
711 column
= (column
+ 8) & ~7;
713 if (column
>= ColumnWidth
&& WrapLines
)
714 { /* Wrap text to margins */
718 if (line
>= SizeLines
)
723 if (page_column
>= PageColumns
)
739 * All but MacOS/Darwin treat CR as was intended by ANSI
740 * folks, namely to move to column 0/1. Some programs still
741 * use this to do boldfacing and underlining...
748 * MacOS/Darwin still need to treat CR as a line ending.
753 if ((nextch
= getc(fp
)) != 0x0a)
758 #endif /* !__APPLE__ */
760 case 0x0a : /* LF - output current line */
761 if (PrettyPrint
&& keyptr
> keyword
)
766 if (bsearch(&keyptr
, Keywords
, NumKeywords
, sizeof(char *),
770 * Put keywords in boldface...
773 i
= page_column
* (ColumnWidth
+ ColumnGutter
);
775 while (keycol
< column
)
777 Page
[line
][keycol
+ i
].attr
|= ATTR_BOLD
;
787 if (!ccomment
&& !cstring
)
788 attr
&= ~(ATTR_ITALIC
| ATTR_BOLD
| ATTR_RED
| ATTR_GREEN
| ATTR_BLUE
);
790 if (line
>= SizeLines
)
795 if (page_column
>= PageColumns
)
803 case 0x0b : /* VT - move up 1 line */
810 if (!ccomment
&& !cstring
)
811 attr
&= ~(ATTR_ITALIC
| ATTR_BOLD
| ATTR_RED
| ATTR_GREEN
| ATTR_BLUE
);
814 case 0x0c : /* FF - eject current page... */
815 if (PrettyPrint
&& keyptr
> keyword
)
820 if (bsearch(&keyptr
, Keywords
, NumKeywords
, sizeof(char *),
824 * Put keywords in boldface...
827 i
= page_column
* (ColumnWidth
+ ColumnGutter
);
829 while (keycol
< column
)
831 Page
[line
][keycol
+ i
].attr
|= ATTR_BOLD
;
842 if (!ccomment
&& !cstring
)
843 attr
&= ~(ATTR_ITALIC
| ATTR_BOLD
| ATTR_RED
| ATTR_GREEN
| ATTR_BLUE
);
845 if (page_column
>= PageColumns
)
852 case 0x1b : /* Escape sequence */
857 * ESC 7 Reverse full line (0x1b 0x37)
866 * ESC 8 Reverse half line (0x1b 0x38)
869 if ((attr
& ATTR_RAISED
) && line
> 0)
871 attr
&= ~ATTR_RAISED
;
874 else if (attr
& ATTR_LOWERED
)
875 attr
&= ~ATTR_LOWERED
;
882 * ESC 9 Forward half line (0x1b 0x39)
885 if ((attr
& ATTR_LOWERED
) && line
< (SizeLines
- 1))
887 attr
&= ~ATTR_LOWERED
;
890 else if (attr
& ATTR_RAISED
)
891 attr
&= ~ATTR_RAISED
;
893 attr
|= ATTR_LOWERED
;
897 default : /* All others... */
899 break; /* Ignore other control chars */
901 if (PrettyPrint
> PRETTY_PLAIN
)
904 * Do highlighting of C/C++ keywords, preprocessor commands,
908 if (ch
== ' ' && (attr
& ATTR_BOLD
))
911 * Stop bolding preprocessor command...
916 else if (!(isalnum(ch
& 255) || ch
== '_') && keyptr
> keyword
)
919 * Look for a keyword...
925 if (!(attr
& ATTR_ITALIC
) &&
926 bsearch(&keyptr
, Keywords
, NumKeywords
, sizeof(char *),
930 * Put keywords in boldface...
933 i
= page_column
* (ColumnWidth
+ ColumnGutter
);
935 while (keycol
< column
)
937 Page
[line
][keycol
+ i
].attr
|= ATTR_BOLD
;
942 else if ((isalnum(ch
& 255) || ch
== '_') && !ccomment
&& !cstring
)
945 * Add characters to the current keyword (if they'll fit).
948 if (keyptr
== keyword
)
951 if (keyptr
< (keyword
+ sizeof(keyword
) - 1))
954 else if (ch
== '\"' && lastch
!= '\\' && !ccomment
&& !cstring
)
957 * Start a C string constant...
963 else if (ch
== '*' && lastch
== '/' && !cstring
&&
964 PrettyPrint
!= PRETTY_SHELL
)
967 * Start a C-style comment...
971 attr
= ATTR_ITALIC
| ATTR_GREEN
;
973 else if (ch
== '/' && lastch
== '/' && !cstring
&&
974 PrettyPrint
== PRETTY_CODE
)
977 * Start a C++-style comment...
980 attr
= ATTR_ITALIC
| ATTR_GREEN
;
982 else if (ch
== '#' && !cstring
&& PrettyPrint
!= PRETTY_CODE
)
985 * Start a shell-style comment...
988 attr
= ATTR_ITALIC
| ATTR_GREEN
;
990 else if (ch
== '#' && column
== 0 && !ccomment
&& !cstring
&&
991 PrettyPrint
== PRETTY_CODE
)
994 * Start a preprocessor command...
997 attr
= ATTR_BOLD
| ATTR_RED
;
1001 if (column
>= ColumnWidth
&& WrapLines
)
1002 { /* Wrap text to margins */
1006 if (line
>= SizeLines
)
1011 if (page_column
>= PageColumns
)
1020 * Add text to the current column & line...
1023 if (column
< ColumnWidth
)
1025 i
= column
+ page_column
* (ColumnWidth
+ ColumnGutter
);
1028 Page
[line
][i
].attr
= attr
;
1029 else if (ch
== ' ' && Page
[line
][i
].ch
)
1030 ch
= Page
[line
][i
].ch
;
1031 else if (ch
== Page
[line
][i
].ch
)
1032 Page
[line
][i
].attr
|= ATTR_BOLD
;
1033 else if (Page
[line
][i
].ch
== '_')
1034 Page
[line
][i
].attr
|= ATTR_UNDERLINE
;
1037 Page
[line
][i
].attr
|= ATTR_UNDERLINE
;
1039 if (Page
[line
][i
].ch
)
1040 ch
= Page
[line
][i
].ch
;
1043 Page
[line
][i
].attr
= attr
;
1045 Page
[line
][i
].ch
= ch
;
1050 if ((ch
== '{' || ch
== '}') && !ccomment
&& !cstring
&&
1051 column
< ColumnWidth
)
1054 * Highlight curley braces...
1057 Page
[line
][column
].attr
|= ATTR_BOLD
;
1059 else if ((ch
== '/' || ch
== '*') && lastch
== '/' &&
1060 column
< ColumnWidth
&& PrettyPrint
!= PRETTY_SHELL
)
1063 * Highlight first comment character...
1066 Page
[line
][column
- 1].attr
= attr
;
1068 else if (ch
== '\"' && lastch
!= '\\' && !ccomment
&& cstring
> 0)
1071 * End a C string constant...
1077 else if (ch
== '/' && lastch
== '*' && ccomment
)
1080 * End a C-style comment...
1084 attr
&= ~(ATTR_ITALIC
| ATTR_GREEN
);
1096 * Save this character for the next cycle.
1103 * Write any remaining page data...
1106 if (line
> 0 || page_column
> 0 || column
> 0)
1110 * Write the epilog and return...
1123 * 'compare_keywords()' - Compare two C/C++ keywords.
1126 static int /* O - Result of strcmp */
1127 compare_keywords(const void *k1
, /* I - First keyword */
1128 const void *k2
) /* I - Second keyword */
1130 return (strcmp(*((const char **)k1
), *((const char **)k2
)));
1135 * 'getutf8()' - Get a UTF-8 encoded wide character...
1138 static int /* O - Character or -1 on error */
1139 getutf8(FILE *fp
) /* I - File to read from */
1141 int ch
; /* Current character value */
1142 int next
; /* Next character from file */
1146 * Read the first character and process things accordingly...
1148 * UTF-8 maps 16-bit characters to:
1150 * 0 to 127 = 0xxxxxxx
1151 * 128 to 2047 = 110xxxxx 10yyyyyy (xxxxxyyyyyy)
1152 * 2048 to 65535 = 1110xxxx 10yyyyyy 10zzzzzz (xxxxyyyyyyzzzzzz)
1156 * 128 to 191 = 10xxxxxx
1158 * since this range of values is otherwise undefined unless you are
1159 * in the middle of a multi-byte character...
1161 * This code currently does not support anything beyond 16-bit
1162 * characters, in part because PostScript doesn't support more than
1163 * 16-bit characters...
1166 if ((ch
= getc(fp
)) == EOF
)
1169 if (ch
< 0xc0) /* One byte character? */
1171 else if ((ch
& 0xe0) == 0xc0)
1174 * Two byte character...
1177 if ((next
= getc(fp
)) == EOF
)
1180 return (((ch
& 0x1f) << 6) | (next
& 0x3f));
1182 else if ((ch
& 0xf0) == 0xe0)
1185 * Three byte character...
1188 if ((next
= getc(fp
)) == EOF
)
1191 ch
= ((ch
& 0x0f) << 6) | (next
& 0x3f);
1193 if ((next
= getc(fp
)) == EOF
)
1196 return ((ch
<< 6) | (next
& 0x3f));
1201 * More than three bytes... We don't support that...
1210 * End of "$Id: textcommon.c 6649 2007-07-11 21:46:42Z mike $".