-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpcl-hpgl.l
186 lines (158 loc) · 3.85 KB
/
pcl-hpgl.l
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
%option noyywrap
%option never-interactive
%option nounistd
%option backup
%option warn
%option reentrant
%option full
%option ecs
%{
#include <io.h>
#define ENA_PRINTING
const char * pclValueExtractor(const char *);
const char * getJobName(const char *);
%}
%x PCL_ESCAPE_SEQ
%x PCL_TWO_ESCAPE_SEQ
%x PJL_COMMAND
%x HPGL_MODE
%%
<INITIAL,PCL_ESCAPE_SEQ,HPGL_MODE>\x1b%-12345X {
#ifdef ENA_PRINTING
printf("Universal Exit Language Command\n");
#endif
BEGIN(INITIAL);
}
<INITIAL,PCL_ESCAPE_SEQ,HPGL_MODE>\x1bE {
#ifdef ENA_PRINTING
printf("PCL Printer Reset Command\n");
#endif
BEGIN(INITIAL);
}
\x1b/[\x21-\x2f] {
#if defined(ENA_PRINTING)
//printf("PCL Parameterized Escape Sequence\n");
printf("PCL Command:\t");
#endif
BEGIN(PCL_ESCAPE_SEQ);
}
\x1b/[\x30-\x7E] {
#ifdef ENA_PRINTING
printf("PCL TWO CHAR ESCAPE SEQ ");
#endif
BEGIN(INITIAL);
}
<INITIAL,PCL_ESCAPE_SEQ,HPGL_MODE,PJL_COMMAND>(?s:.) {
#if defined(ENA_PRINTING) && defined(UNKNOWN_PRINT)
//printf("Unknown Char <%02x>\n",(unsigned char) yytext[0]);
#endif
}
@PJL[ ]{1} {
#ifdef ENA_PRINTING
printf("PJL Command:\t");
#endif
BEGIN(PJL_COMMAND);
}
/* Add PJL commands*/
<PJL_COMMAND>{
JOB[ ]NAME=.*\r?\n {
const char * jobName;
jobName = getJobName(yytext);
#if defined(ENA_PRINTING) || defined(ENA_JOB_NAME)
printf("Job Name String <%s>\n",jobName);
#endif
BEGIN(INITIAL);
}
ENTER[ ]LANGUAGE=PCL[ ]+\r?\n {
#ifdef ENA_PRINTING
printf("Language: PCL\n");
#endif
BEGIN(INITIAL);
}
EOJ[ \r\n]{0,256} {
#if defined(ENA_PRINTING) || defined(ENA_EOJ)
printf("End of job\n");
#endif
BEGIN(INITIAL);
yyterminate();
}
}
<PCL_ESCAPE_SEQ>{
%1B {
#ifdef ENA_PRINTING
printf("OPEN HPGL MODE\n");
#endif
BEGIN(HPGL_MODE);
}
%0B {
#ifdef ENA_PRINTING
printf("CLOSE HPGL MODE -- HACK\n");
#endif
BEGIN(INITIAL);
}
[*]{1}r[0-9]{0,5}T {
#if defined(ENA_PRINTING)
int intValue;
const char * valueStr;
valueStr = pclValueExtractor(yytext);
intValue = atoi(valueStr);
printf("Set Raster Page Height <%d>\n", intValue);
#endif
BEGIN(INITIAL);
}
[*]{1}r[0-9]{0,5}S {
#if defined(ENA_PRINTING)
int intValue;
const char * valueStr;
valueStr = pclValueExtractor(yytext);
intValue = atoi(valueStr);
printf("Set Raster Page Width <%d>\n", intValue);
#endif
BEGIN(INITIAL);
}
}
<HPGL_MODE>{
IN; {
#ifdef ENA_PRINTING
printf("Initialize HP-GL/2\n");
#endif
}
LT[\-0-9]{0,2}[;]? {
#ifdef ENA_PRINTING
printf("Set Line Type\n");
#endif
}
\x1b%0[AB] {
#ifdef ENA_PRINTING
printf("Exit HPGL mode\n");
#endif
BEGIN(INITIAL);
}
}
[ \t\n\r]{0,256} /* skip whitespace */
%%
#define MAX_JOB_NAME_LEN (256)
const char * getJobName(const char * in_str)
{
// Make sure we have a null terminated string
static char name[MAX_JOB_NAME_LEN+1];
name[MAX_JOB_NAME_LEN] = '\0';
// Start figuring out the name location and length
const char * name_start = strstr(in_str,"=")+1;
size_t name_len = strcspn(name_start,"\n\r\t");
// Copy it
strncpy(name,name_start, (name_len > MAX_JOB_NAME_LEN) ? MAX_JOB_NAME_LEN : name_len);
name[name_len] = '\0';
return name;
}
const char * pclValueExtractor(const char * in_str)
{
size_t str_size = strlen(in_str);
if(str_size < 3)
return "\0"; // There is no value to extract
// Make sure we have a null terminated string
static char value[MAX_JOB_NAME_LEN+1];
value[str_size-2-1] = '\0';
strncpy(value,&in_str[2],str_size-2-1);
return value;
}