]>
git.ipfire.org Git - thirdparty/cups.git/blob - filter/textcommon.c
2 * "$Id: textcommon.c,v 1.16.2.14 2004/02/25 20:01:37 mike Exp $"
4 * Common text filter routines for the Common UNIX Printing System (CUPS).
6 * Copyright 1997-2003 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
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... */
137 *sh_keywords
[] = /* List of known Boure/Korn/zsh/bash keywords... */
193 *csh_keywords
[] = /* List of known csh/tcsh keywords... */
261 *perl_keywords
[] = /* List of known perl keywords... */
481 static int compare_keywords(const void *, const void *);
482 static int getutf8(FILE *fp
);
486 * 'TextMain()' - Standard main entry for text filters.
489 int /* O - Exit status */
490 TextMain(const char *name
, /* I - Name of filter */
491 int argc
, /* I - Number of command-line arguments */
492 char *argv
[]) /* I - Command-line arguments */
494 FILE *fp
; /* Print file */
495 ppd_file_t
*ppd
; /* PPD file */
496 int i
, /* Looping var */
497 ch
, /* Current char from file */
498 lastch
, /* Previous char from file */
499 attr
, /* Current attribute */
500 line
, /* Current line */
501 column
, /* Current column */
502 page_column
; /* Current page column */
503 int num_options
; /* Number of print options */
504 cups_option_t
*options
; /* Print options */
505 const char *val
; /* Option value */
506 char keyword
[64], /* Keyword string */
507 *keyptr
; /* Pointer into string */
508 int keycol
; /* Column where keyword starts */
509 int ccomment
; /* Inside a C-style comment? */
510 int cstring
; /* Inside a C string */
514 * Make sure status messages are not buffered...
517 setbuf(stderr
, NULL
);
520 * Check command-line...
523 if (argc
< 6 || argc
> 7)
525 fprintf(stderr
, "ERROR: %s job-id user title copies options [file]\n",
531 * If we have 7 arguments, print the file named on the command-line.
532 * Otherwise, send stdin instead...
540 * Try to open the print file...
543 if ((fp
= fopen(argv
[6], "rb")) == NULL
)
545 perror("ERROR: unable to open print file - ");
551 * Process command-line options and write the prolog...
555 num_options
= cupsParseOptions(argv
[5], 0, &options
);
557 if ((val
= cupsGetOption("prettyprint", num_options
, options
)) != NULL
&&
558 strcasecmp(val
, "no") && strcasecmp(val
, "off") &&
559 strcasecmp(val
, "false"))
562 PageRight
= PageWidth
- 36.0f
;
563 PageBottom
= PageBottom
> 36.0f
? PageBottom
: 36.0f
;
564 PageTop
= PageLength
- 36.0f
;
568 if ((val
= getenv("CONTENT_TYPE")) == NULL
)
570 PrettyPrint
= PRETTY_CODE
;
571 NumKeywords
= sizeof(code_keywords
) / sizeof(code_keywords
[0]);
572 Keywords
= code_keywords
;
574 else if (strcasecmp(val
, "application/x-cshell") == 0)
576 PrettyPrint
= PRETTY_SHELL
;
577 NumKeywords
= sizeof(csh_keywords
) / sizeof(csh_keywords
[0]);
578 Keywords
= csh_keywords
;
580 else if (strcasecmp(val
, "application/x-perl") == 0)
582 PrettyPrint
= PRETTY_PERL
;
583 NumKeywords
= sizeof(perl_keywords
) / sizeof(perl_keywords
[0]);
584 Keywords
= perl_keywords
;
586 else if (strcasecmp(val
, "application/x-shell") == 0)
588 PrettyPrint
= PRETTY_SHELL
;
589 NumKeywords
= sizeof(sh_keywords
) / sizeof(sh_keywords
[0]);
590 Keywords
= sh_keywords
;
594 PrettyPrint
= PRETTY_CODE
;
595 NumKeywords
= sizeof(code_keywords
) / sizeof(code_keywords
[0]);
596 Keywords
= code_keywords
;
600 ppd
= SetCommonOptions(num_options
, options
, 1);
602 if ((val
= cupsGetOption("wrap", num_options
, options
)) == NULL
)
605 WrapLines
= !strcasecmp(val
, "true") || !strcasecmp(val
, "on") ||
606 !strcasecmp(val
, "yes");
608 if ((val
= cupsGetOption("columns", num_options
, options
)) != NULL
)
609 PageColumns
= atoi(val
);
611 if ((val
= cupsGetOption("cpi", num_options
, options
)) != NULL
)
612 CharsPerInch
= atof(val
);
614 if ((val
= cupsGetOption("lpi", num_options
, options
)) != NULL
)
615 LinesPerInch
= atof(val
);
618 PageTop
-= 216.0f
/ LinesPerInch
;
620 Copies
= atoi(argv
[4]);
622 WriteProlog(argv
[3], argv
[2], getenv("CLASSIFICATION"),
623 cupsGetOption("page-label", num_options
, options
), ppd
);
626 * Read text from the specified source and print it...
639 while ((ch
= getutf8(fp
)) >= 0)
644 * BS Backspace (0x08)
645 * HT Horizontal tab; next 8th column (0x09)
646 * LF Line feed; forward full line (0x0a)
647 * VT Vertical tab; reverse full line (0x0b)
648 * FF Form feed (0x0c)
649 * CR Carriage return (0x0d)
650 * ESC 7 Reverse full line (0x1b 0x37)
651 * ESC 8 Reverse half line (0x1b 0x38)
652 * ESC 9 Forward half line (0x1b 0x39)
657 case 0x08 : /* BS - backspace for boldface & underline */
665 case 0x09 : /* HT - tab to next 8th column */
666 if (PrettyPrint
&& keyptr
> keyword
)
671 if (bsearch(&keyptr
, Keywords
, NumKeywords
, sizeof(char *),
675 * Put keywords in boldface...
678 i
= page_column
* (ColumnWidth
+ ColumnGutter
);
680 while (keycol
< column
)
682 Page
[line
][keycol
+ i
].attr
|= ATTR_BOLD
;
688 column
= (column
+ 8) & ~7;
690 if (column
>= ColumnWidth
&& WrapLines
)
691 { /* Wrap text to margins */
695 if (line
>= SizeLines
)
700 if (page_column
>= PageColumns
)
714 * All but MacOS/Darwin treat CR as was intended by ANSI
715 * folks, namely to move to column 0/1. Some programs still
716 * use this to do boldfacing and underlining...
723 * MacOS/Darwin still need to treat CR as a line ending.
728 if ((nextch
= getc(fp
)) != 0x0a)
733 #endif /* !__APPLE__ */
735 case 0x0a : /* LF - output current line */
736 if (PrettyPrint
&& keyptr
> keyword
)
741 if (bsearch(&keyptr
, Keywords
, NumKeywords
, sizeof(char *),
745 * Put keywords in boldface...
748 i
= page_column
* (ColumnWidth
+ ColumnGutter
);
750 while (keycol
< column
)
752 Page
[line
][keycol
+ i
].attr
|= ATTR_BOLD
;
762 if (!ccomment
&& !cstring
)
763 attr
&= ~(ATTR_ITALIC
| ATTR_BOLD
| ATTR_RED
| ATTR_GREEN
| ATTR_BLUE
);
765 if (line
>= SizeLines
)
770 if (page_column
>= PageColumns
)
778 case 0x0b : /* VT - move up 1 line */
785 if (!ccomment
&& !cstring
)
786 attr
&= ~(ATTR_ITALIC
| ATTR_BOLD
| ATTR_RED
| ATTR_GREEN
| ATTR_BLUE
);
789 case 0x0c : /* FF - eject current page... */
790 if (PrettyPrint
&& keyptr
> keyword
)
795 if (bsearch(&keyptr
, Keywords
, NumKeywords
, sizeof(char *),
799 * Put keywords in boldface...
802 i
= page_column
* (ColumnWidth
+ ColumnGutter
);
804 while (keycol
< column
)
806 Page
[line
][keycol
+ i
].attr
|= ATTR_BOLD
;
817 if (!ccomment
&& !cstring
)
818 attr
&= ~(ATTR_ITALIC
| ATTR_BOLD
| ATTR_RED
| ATTR_GREEN
| ATTR_BLUE
);
820 if (page_column
>= PageColumns
)
827 case 0x1b : /* Escape sequence */
832 * ESC 7 Reverse full line (0x1b 0x37)
841 * ESC 8 Reverse half line (0x1b 0x38)
844 if ((attr
& ATTR_RAISED
) && line
> 0)
846 attr
&= ~ATTR_RAISED
;
849 else if (attr
& ATTR_LOWERED
)
850 attr
&= ~ATTR_LOWERED
;
857 * ESC 9 Forward half line (0x1b 0x39)
860 if ((attr
& ATTR_LOWERED
) && line
< (SizeLines
- 1))
862 attr
&= ~ATTR_LOWERED
;
865 else if (attr
& ATTR_RAISED
)
866 attr
&= ~ATTR_RAISED
;
868 attr
|= ATTR_LOWERED
;
872 default : /* All others... */
874 break; /* Ignore other control chars */
879 * Do highlighting of C/C++ keywords, preprocessor commands,
883 if ((ch
== ' ' || ch
== '\t') && (attr
& ATTR_BOLD
))
886 * Stop bolding preprocessor command...
891 else if (!(isalnum(ch
& 255) || ch
== '_') && keyptr
> keyword
)
894 * Look for a keyword...
900 if (!(attr
& ATTR_ITALIC
) &&
901 bsearch(&keyptr
, Keywords
, NumKeywords
, sizeof(char *),
905 * Put keywords in boldface...
908 i
= page_column
* (ColumnWidth
+ ColumnGutter
);
910 while (keycol
< column
)
912 Page
[line
][keycol
+ i
].attr
|= ATTR_BOLD
;
917 else if ((isalnum(ch
& 255) || ch
== '_') && !ccomment
&& !cstring
)
920 * Add characters to the current keyword (if they'll fit).
923 if (keyptr
== keyword
)
926 if (keyptr
< (keyword
+ sizeof(keyword
) - 1))
929 else if (ch
== '\"' && lastch
!= '\\' && !ccomment
&& !cstring
)
932 * Start a C string constant...
938 else if (ch
== '*' && lastch
== '/' && !cstring
&&
939 PrettyPrint
!= PRETTY_SHELL
)
942 * Start a C-style comment...
946 attr
|= ATTR_ITALIC
| ATTR_GREEN
;
948 else if (ch
== '/' && lastch
== '/' && !cstring
&&
949 PrettyPrint
== PRETTY_CODE
)
952 * Start a C++-style comment...
955 attr
|= ATTR_ITALIC
| ATTR_GREEN
;
957 else if (ch
== '#' && !cstring
&& PrettyPrint
!= PRETTY_CODE
)
960 * Start a shell-style comment...
963 attr
|= ATTR_ITALIC
| ATTR_GREEN
;
965 else if (ch
== '#' && column
== 0 && !ccomment
&& !cstring
&&
966 PrettyPrint
== PRETTY_CODE
)
969 * Start a preprocessor command...
972 attr
|= ATTR_BOLD
| ATTR_RED
;
976 if (column
>= ColumnWidth
&& WrapLines
)
977 { /* Wrap text to margins */
981 if (line
>= SizeLines
)
986 if (page_column
>= PageColumns
)
995 * Add text to the current column & line...
998 if (column
< ColumnWidth
)
1000 i
= column
+ page_column
* (ColumnWidth
+ ColumnGutter
);
1003 Page
[line
][i
].attr
= attr
;
1004 else if (ch
== ' ' && Page
[line
][i
].ch
)
1005 ch
= Page
[line
][i
].ch
;
1006 else if (ch
== Page
[line
][i
].ch
)
1007 Page
[line
][i
].attr
|= ATTR_BOLD
;
1008 else if (Page
[line
][i
].ch
== '_')
1009 Page
[line
][i
].attr
|= ATTR_UNDERLINE
;
1012 Page
[line
][i
].attr
|= ATTR_UNDERLINE
;
1014 if (Page
[line
][i
].ch
)
1015 ch
= Page
[line
][i
].ch
;
1018 Page
[line
][i
].attr
= attr
;
1020 Page
[line
][i
].ch
= ch
;
1025 if ((ch
== '{' || ch
== '}') && !ccomment
&& !cstring
&&
1026 column
< ColumnWidth
)
1029 * Highlight curley braces...
1032 Page
[line
][column
].attr
|= ATTR_BOLD
;
1034 else if ((ch
== '/' || ch
== '*') && lastch
== '/' &&
1035 column
< ColumnWidth
&& PrettyPrint
!= PRETTY_SHELL
)
1038 * Highlight first comment character...
1041 Page
[line
][column
- 1].attr
= attr
;
1043 else if (ch
== '\"' && lastch
!= '\\' && !ccomment
&& cstring
> 0)
1046 * End a C string constant...
1052 else if (ch
== '/' && lastch
== '*' && ccomment
)
1055 * End a C-style comment...
1059 attr
&= ~(ATTR_ITALIC
| ATTR_GREEN
);
1071 * Save this character for the next cycle.
1078 * Write any remaining page data...
1081 if (line
> 0 || page_column
> 0 || column
> 0)
1085 * Write the epilog and return...
1098 * 'compare_keywords()' - Compare two C/C++ keywords.
1101 static int /* O - Result of strcmp */
1102 compare_keywords(const void *k1
, /* I - First keyword */
1103 const void *k2
) /* I - Second keyword */
1105 return (strcmp(*((const char **)k1
), *((const char **)k2
)));
1110 * 'getutf8()' - Get a UTF-8 encoded wide character...
1113 static int /* O - Character or -1 on error */
1114 getutf8(FILE *fp
) /* I - File to read from */
1116 int ch
; /* Current character value */
1117 int next
; /* Next character from file */
1121 * Read the first character and process things accordingly...
1123 * UTF-8 maps 16-bit characters to:
1125 * 0 to 127 = 0xxxxxxx
1126 * 128 to 2047 = 110xxxxx 10yyyyyy (xxxxxyyyyyy)
1127 * 2048 to 65535 = 1110xxxx 10yyyyyy 10zzzzzz (xxxxyyyyyyzzzzzz)
1131 * 128 to 191 = 10xxxxxx
1133 * since this range of values is otherwise undefined unless you are
1134 * in the middle of a multi-byte character...
1136 * This code currently does not support anything beyond 16-bit
1137 * characters, in part because PostScript doesn't support more than
1138 * 16-bit characters...
1141 if ((ch
= getc(fp
)) == EOF
)
1144 if (ch
< 0xc0 || !UTF8
) /* One byte character? */
1146 else if ((ch
& 0xe0) == 0xc0)
1149 * Two byte character...
1152 if ((next
= getc(fp
)) == EOF
)
1155 return (((ch
& 0x1f) << 6) | (next
& 0x3f));
1157 else if ((ch
& 0xf0) == 0xe0)
1160 * Three byte character...
1163 if ((next
= getc(fp
)) == EOF
)
1166 ch
= ((ch
& 0x0f) << 6) | (next
& 0x3f);
1168 if ((next
= getc(fp
)) == EOF
)
1171 return ((ch
<< 6) | (next
& 0x3f));
1176 * More than three bytes... We don't support that...
1185 * End of "$Id: textcommon.c,v 1.16.2.14 2004/02/25 20:01:37 mike Exp $".