]>
git.ipfire.org Git - thirdparty/cups.git/blob - filter/textcommon.c
2 * "$Id: textcommon.c 177 2006-06-21 00:20:03Z jlovell $"
4 * Common text filter routines for the Common UNIX Printing System (CUPS).
6 * Copyright 1997-2005 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 USA
20 * Voice: (301) 373-9600
21 * EMail: cups-info@cups.org
22 * WWW: http://www.cups.org
24 * This file is subject to the Apple OS-Developed Software exception.
28 * TextMain() - Standard main entry for text filters.
29 * compare_keywords() - Compare two C/C++ keywords.
30 * getutf8() - Get a UTF-8 encoded wide character...
34 * Include necessary headers...
37 #include "textcommon.h"
44 int WrapLines
= 1, /* Wrap text in lines */
45 SizeLines
= 60, /* Number of lines on a page */
46 SizeColumns
= 80, /* Number of columns on a line */
47 PageColumns
= 1, /* Number of columns on a page */
48 ColumnGutter
= 0, /* Number of characters between text columns */
49 ColumnWidth
= 80, /* Width of each column */
50 PrettyPrint
= 0, /* Do pretty code formatting */
51 Copies
= 1; /* Number of copies */
52 lchar_t
**Page
= NULL
; /* Page characters */
53 int NumPages
= 0; /* Number of pages in document */
54 float CharsPerInch
= 10; /* Number of character columns per inch */
55 float LinesPerInch
= 6; /* Number of lines per inch */
56 int UTF8
= 0; /* Use UTF-8 encoding? */
57 int NumKeywords
= 0; /* Number of known keywords */
58 char **Keywords
= NULL
; /* List of known keywords */
65 static char *code_keywords
[] = /* List of known C/C++ keywords... */
138 *sh_keywords
[] = /* List of known Boure/Korn/zsh/bash keywords... */
194 *csh_keywords
[] = /* List of known csh/tcsh keywords... */
262 *perl_keywords
[] = /* List of known perl keywords... */
482 static int compare_keywords(const void *, const void *);
483 static int getutf8(FILE *fp
);
487 * 'TextMain()' - Standard main entry for text filters.
490 int /* O - Exit status */
491 TextMain(const char *name
, /* I - Name of filter */
492 int argc
, /* I - Number of command-line arguments */
493 char *argv
[]) /* I - Command-line arguments */
495 FILE *fp
; /* Print file */
496 ppd_file_t
*ppd
; /* PPD file */
497 int i
, /* Looping var */
498 ch
, /* Current char from file */
499 lastch
, /* Previous char from file */
500 attr
, /* Current attribute */
501 line
, /* Current line */
502 column
, /* Current column */
503 page_column
; /* Current page column */
504 int num_options
; /* Number of print options */
505 cups_option_t
*options
; /* Print options */
506 const char *val
; /* Option value */
507 char keyword
[64], /* Keyword string */
508 *keyptr
; /* Pointer into string */
509 int keycol
; /* Column where keyword starts */
510 int ccomment
; /* Inside a C-style comment? */
511 int cstring
; /* Inside a C string */
515 * Make sure status messages are not buffered...
518 setbuf(stderr
, NULL
);
521 * Check command-line...
524 if (argc
< 6 || argc
> 7)
526 fprintf(stderr
, "ERROR: %s job-id user title copies options [file]\n",
532 * If we have 7 arguments, print the file named on the command-line.
533 * Otherwise, send stdin instead...
541 * Try to open the print file...
544 if ((fp
= fopen(argv
[6], "rb")) == NULL
)
546 perror("ERROR: unable to open print file - ");
552 * Process command-line options and write the prolog...
556 num_options
= cupsParseOptions(argv
[5], 0, &options
);
558 if ((val
= cupsGetOption("prettyprint", num_options
, options
)) != NULL
&&
559 strcasecmp(val
, "no") && strcasecmp(val
, "off") &&
560 strcasecmp(val
, "false"))
563 PageRight
= PageWidth
- 36.0f
;
564 PageBottom
= PageBottom
> 36.0f
? PageBottom
: 36.0f
;
565 PageTop
= PageLength
- 36.0f
;
569 if ((val
= getenv("CONTENT_TYPE")) == NULL
)
571 PrettyPrint
= PRETTY_PLAIN
;
575 else if (strcasecmp(val
, "application/x-cshell") == 0)
577 PrettyPrint
= PRETTY_SHELL
;
578 NumKeywords
= sizeof(csh_keywords
) / sizeof(csh_keywords
[0]);
579 Keywords
= csh_keywords
;
581 else if (strcasecmp(val
, "application/x-csource") == 0)
583 PrettyPrint
= PRETTY_CODE
;
584 NumKeywords
= sizeof(code_keywords
) / sizeof(code_keywords
[0]);
585 Keywords
= code_keywords
;
587 else if (strcasecmp(val
, "application/x-perl") == 0)
589 PrettyPrint
= PRETTY_PERL
;
590 NumKeywords
= sizeof(perl_keywords
) / sizeof(perl_keywords
[0]);
591 Keywords
= perl_keywords
;
593 else if (strcasecmp(val
, "application/x-shell") == 0)
595 PrettyPrint
= PRETTY_SHELL
;
596 NumKeywords
= sizeof(sh_keywords
) / sizeof(sh_keywords
[0]);
597 Keywords
= sh_keywords
;
601 PrettyPrint
= PRETTY_PLAIN
;
607 ppd
= SetCommonOptions(num_options
, options
, 1);
609 if ((val
= cupsGetOption("wrap", num_options
, options
)) == NULL
)
612 WrapLines
= !strcasecmp(val
, "true") || !strcasecmp(val
, "on") ||
613 !strcasecmp(val
, "yes");
615 if ((val
= cupsGetOption("columns", num_options
, options
)) != NULL
)
616 PageColumns
= atoi(val
);
618 if ((val
= cupsGetOption("cpi", num_options
, options
)) != NULL
)
619 CharsPerInch
= atof(val
);
621 if ((val
= cupsGetOption("lpi", num_options
, options
)) != NULL
)
622 LinesPerInch
= atof(val
);
625 PageTop
-= 216.0f
/ LinesPerInch
;
627 Copies
= atoi(argv
[4]);
629 WriteProlog(argv
[3], argv
[2], getenv("CLASSIFICATION"),
630 cupsGetOption("page-label", num_options
, options
), ppd
);
633 * Read text from the specified source and print it...
646 while ((ch
= getutf8(fp
)) >= 0)
651 * BS Backspace (0x08)
652 * HT Horizontal tab; next 8th column (0x09)
653 * LF Line feed; forward full line (0x0a)
654 * VT Vertical tab; reverse full line (0x0b)
655 * FF Form feed (0x0c)
656 * CR Carriage return (0x0d)
657 * ESC 7 Reverse full line (0x1b 0x37)
658 * ESC 8 Reverse half line (0x1b 0x38)
659 * ESC 9 Forward half line (0x1b 0x39)
664 case 0x08 : /* BS - backspace for boldface & underline */
672 case 0x09 : /* HT - tab to next 8th column */
673 if (PrettyPrint
&& keyptr
> keyword
)
678 if (bsearch(&keyptr
, Keywords
, NumKeywords
, sizeof(char *),
682 * Put keywords in boldface...
685 i
= page_column
* (ColumnWidth
+ ColumnGutter
);
687 while (keycol
< column
)
689 Page
[line
][keycol
+ i
].attr
|= ATTR_BOLD
;
695 column
= (column
+ 8) & ~7;
697 if (column
>= ColumnWidth
&& WrapLines
)
698 { /* Wrap text to margins */
702 if (line
>= SizeLines
)
707 if (page_column
>= PageColumns
)
723 * All but MacOS/Darwin treat CR as was intended by ANSI
724 * folks, namely to move to column 0/1. Some programs still
725 * use this to do boldfacing and underlining...
732 * MacOS/Darwin still need to treat CR as a line ending.
737 if ((nextch
= getc(fp
)) != 0x0a)
742 #endif /* !__APPLE__ */
744 case 0x0a : /* LF - output current line */
745 if (PrettyPrint
&& keyptr
> keyword
)
750 if (bsearch(&keyptr
, Keywords
, NumKeywords
, sizeof(char *),
754 * Put keywords in boldface...
757 i
= page_column
* (ColumnWidth
+ ColumnGutter
);
759 while (keycol
< column
)
761 Page
[line
][keycol
+ i
].attr
|= ATTR_BOLD
;
771 if (!ccomment
&& !cstring
)
772 attr
&= ~(ATTR_ITALIC
| ATTR_BOLD
| ATTR_RED
| ATTR_GREEN
| ATTR_BLUE
);
774 if (line
>= SizeLines
)
779 if (page_column
>= PageColumns
)
787 case 0x0b : /* VT - move up 1 line */
794 if (!ccomment
&& !cstring
)
795 attr
&= ~(ATTR_ITALIC
| ATTR_BOLD
| ATTR_RED
| ATTR_GREEN
| ATTR_BLUE
);
798 case 0x0c : /* FF - eject current page... */
799 if (PrettyPrint
&& keyptr
> keyword
)
804 if (bsearch(&keyptr
, Keywords
, NumKeywords
, sizeof(char *),
808 * Put keywords in boldface...
811 i
= page_column
* (ColumnWidth
+ ColumnGutter
);
813 while (keycol
< column
)
815 Page
[line
][keycol
+ i
].attr
|= ATTR_BOLD
;
826 if (!ccomment
&& !cstring
)
827 attr
&= ~(ATTR_ITALIC
| ATTR_BOLD
| ATTR_RED
| ATTR_GREEN
| ATTR_BLUE
);
829 if (page_column
>= PageColumns
)
836 case 0x1b : /* Escape sequence */
841 * ESC 7 Reverse full line (0x1b 0x37)
850 * ESC 8 Reverse half line (0x1b 0x38)
853 if ((attr
& ATTR_RAISED
) && line
> 0)
855 attr
&= ~ATTR_RAISED
;
858 else if (attr
& ATTR_LOWERED
)
859 attr
&= ~ATTR_LOWERED
;
866 * ESC 9 Forward half line (0x1b 0x39)
869 if ((attr
& ATTR_LOWERED
) && line
< (SizeLines
- 1))
871 attr
&= ~ATTR_LOWERED
;
874 else if (attr
& ATTR_RAISED
)
875 attr
&= ~ATTR_RAISED
;
877 attr
|= ATTR_LOWERED
;
881 default : /* All others... */
883 break; /* Ignore other control chars */
885 if (PrettyPrint
> PRETTY_PLAIN
)
888 * Do highlighting of C/C++ keywords, preprocessor commands,
892 if (ch
== ' ' && (attr
& ATTR_BOLD
))
895 * Stop bolding preprocessor command...
900 else if (!(isalnum(ch
& 255) || ch
== '_') && keyptr
> keyword
)
903 * Look for a keyword...
909 if (!(attr
& ATTR_ITALIC
) &&
910 bsearch(&keyptr
, Keywords
, NumKeywords
, sizeof(char *),
914 * Put keywords in boldface...
917 i
= page_column
* (ColumnWidth
+ ColumnGutter
);
919 while (keycol
< column
)
921 Page
[line
][keycol
+ i
].attr
|= ATTR_BOLD
;
926 else if ((isalnum(ch
& 255) || ch
== '_') && !ccomment
&& !cstring
)
929 * Add characters to the current keyword (if they'll fit).
932 if (keyptr
== keyword
)
935 if (keyptr
< (keyword
+ sizeof(keyword
) - 1))
938 else if (ch
== '\"' && lastch
!= '\\' && !ccomment
&& !cstring
)
941 * Start a C string constant...
947 else if (ch
== '*' && lastch
== '/' && !cstring
&&
948 PrettyPrint
!= PRETTY_SHELL
)
951 * Start a C-style comment...
955 attr
= ATTR_ITALIC
| ATTR_GREEN
;
957 else if (ch
== '/' && lastch
== '/' && !cstring
&&
958 PrettyPrint
== PRETTY_CODE
)
961 * Start a C++-style comment...
964 attr
= ATTR_ITALIC
| ATTR_GREEN
;
966 else if (ch
== '#' && !cstring
&& PrettyPrint
!= PRETTY_CODE
)
969 * Start a shell-style comment...
972 attr
= ATTR_ITALIC
| ATTR_GREEN
;
974 else if (ch
== '#' && column
== 0 && !ccomment
&& !cstring
&&
975 PrettyPrint
== PRETTY_CODE
)
978 * Start a preprocessor command...
981 attr
= ATTR_BOLD
| ATTR_RED
;
985 if (column
>= ColumnWidth
&& WrapLines
)
986 { /* Wrap text to margins */
990 if (line
>= SizeLines
)
995 if (page_column
>= PageColumns
)
1004 * Add text to the current column & line...
1007 if (column
< ColumnWidth
)
1009 i
= column
+ page_column
* (ColumnWidth
+ ColumnGutter
);
1012 Page
[line
][i
].attr
= attr
;
1013 else if (ch
== ' ' && Page
[line
][i
].ch
)
1014 ch
= Page
[line
][i
].ch
;
1015 else if (ch
== Page
[line
][i
].ch
)
1016 Page
[line
][i
].attr
|= ATTR_BOLD
;
1017 else if (Page
[line
][i
].ch
== '_')
1018 Page
[line
][i
].attr
|= ATTR_UNDERLINE
;
1021 Page
[line
][i
].attr
|= ATTR_UNDERLINE
;
1023 if (Page
[line
][i
].ch
)
1024 ch
= Page
[line
][i
].ch
;
1027 Page
[line
][i
].attr
= attr
;
1029 Page
[line
][i
].ch
= ch
;
1034 if ((ch
== '{' || ch
== '}') && !ccomment
&& !cstring
&&
1035 column
< ColumnWidth
)
1038 * Highlight curley braces...
1041 Page
[line
][column
].attr
|= ATTR_BOLD
;
1043 else if ((ch
== '/' || ch
== '*') && lastch
== '/' &&
1044 column
< ColumnWidth
&& PrettyPrint
!= PRETTY_SHELL
)
1047 * Highlight first comment character...
1050 Page
[line
][column
- 1].attr
= attr
;
1052 else if (ch
== '\"' && lastch
!= '\\' && !ccomment
&& cstring
> 0)
1055 * End a C string constant...
1061 else if (ch
== '/' && lastch
== '*' && ccomment
)
1064 * End a C-style comment...
1068 attr
&= ~(ATTR_ITALIC
| ATTR_GREEN
);
1080 * Save this character for the next cycle.
1087 * Write any remaining page data...
1090 if (line
> 0 || page_column
> 0 || column
> 0)
1094 * Write the epilog and return...
1107 * 'compare_keywords()' - Compare two C/C++ keywords.
1110 static int /* O - Result of strcmp */
1111 compare_keywords(const void *k1
, /* I - First keyword */
1112 const void *k2
) /* I - Second keyword */
1114 return (strcmp(*((const char **)k1
), *((const char **)k2
)));
1119 * 'getutf8()' - Get a UTF-8 encoded wide character...
1122 static int /* O - Character or -1 on error */
1123 getutf8(FILE *fp
) /* I - File to read from */
1125 int ch
; /* Current character value */
1126 int next
; /* Next character from file */
1130 * Read the first character and process things accordingly...
1132 * UTF-8 maps 16-bit characters to:
1134 * 0 to 127 = 0xxxxxxx
1135 * 128 to 2047 = 110xxxxx 10yyyyyy (xxxxxyyyyyy)
1136 * 2048 to 65535 = 1110xxxx 10yyyyyy 10zzzzzz (xxxxyyyyyyzzzzzz)
1140 * 128 to 191 = 10xxxxxx
1142 * since this range of values is otherwise undefined unless you are
1143 * in the middle of a multi-byte character...
1145 * This code currently does not support anything beyond 16-bit
1146 * characters, in part because PostScript doesn't support more than
1147 * 16-bit characters...
1150 if ((ch
= getc(fp
)) == EOF
)
1153 if (ch
< 0xc0 || !UTF8
) /* One byte character? */
1155 else if ((ch
& 0xe0) == 0xc0)
1158 * Two byte character...
1161 if ((next
= getc(fp
)) == EOF
)
1164 return (((ch
& 0x1f) << 6) | (next
& 0x3f));
1166 else if ((ch
& 0xf0) == 0xe0)
1169 * Three byte character...
1172 if ((next
= getc(fp
)) == EOF
)
1175 ch
= ((ch
& 0x0f) << 6) | (next
& 0x3f);
1177 if ((next
= getc(fp
)) == EOF
)
1180 return ((ch
<< 6) | (next
& 0x3f));
1185 * More than three bytes... We don't support that...
1194 * End of "$Id: textcommon.c 177 2006-06-21 00:20:03Z jlovell $".