summaryrefslogtreecommitdiff
path: root/contrib/fixnt.l
blob: 36254649961a0510e0d72cc66555496d6d5f5b6e (plain)
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
%p 3000
%{ /* -*- c -*- */ /* <--- EMACS knows now about what mode to use ... */
  /* Time-stamp: "99/06/29 14:48:25 bauer" */

  /* Filter for Windows NT 4.0 postscript printer driver. Enables
   * the use of psnup from Angus Duggan's psutils package
   * ( http://www.dcs.ed.ac.uk/home/ajcd/psutils/ )
   * Copyright: GNU General Public License (GPL)
   * Authors:
   *         Holger Bauer 1998, 1999    bauer@itsm.uni-stuttgart.de
   *         Michael Rath 1998, 1999    rath@itsm.uni-stuttgart.de
   *         Akim Demaille      1999    demaille@inf.enst.fr
   * Version: 0.1c
   * Release Date: 1999/02/04
   *
   * Compilation:
   *
   * flex fixnt.l
   * cc -O -o fixnt lex.yy.c
   *
   * Usage:
   * csh-prompt> cat BAD_NT_Postscript_FILE.ps | fixnt | psnup -4 | gs -
   * or:
   * csh-prompt> cat BAD_NT_Postscript_FILE.ps | fixnt | psnup -4 >GOOD.ps
   *
   * Windows NT 3.5 users may be happy with:
   * csh-prompt> cat BAD_NT_Postscript_FILE.ps | \
   *             sed 's/NTPSOct94/NTPSOct95/g' | fixnt | psnup -4 >GOOD.ps
   *
   * TODO-List:
   * - check for NTPSOct94 (in the meantime use the sed command above)
   *
   *
   * BUG-Reports: to Authors (above) (please, no 20MB postscript files !!!!!!)
   * Patches (welcome) to code maintainer:   bauer@itsm.uni-stuttgart.de
   *
   */


#ifdef HAVE_CONFIG_H
# include "config.h"
# include <stdio.h>
# if defined STDC_HEADERS || defined _LIBC || defined HAVE_STDLIB_H
#  include <stdlib.h>
# endif
# if HAVE_SYS_TYPES_H
#  include <sys/types.h>
# endif
# if HAVE_UNISTD_H
#  include <unistd.h>
# endif
#else /* !HAVE_CONFIG_H */
# include <stdio.h>
# ifdef _WIN32
#  include <stdlib.h>
#  include <process.h>
#  include <wtypes.h>
#  include <winbase.h>
# else /* !_WIN32 */
#  include <unistd.h>
# endif
# include <sys/types.h>
char *getenv ();
#endif /* !HAVE_CONFIG_H */

/* Support of prototyping when possible */
#ifndef PARAMS
#  if PROTOTYPES
#    define PARAMS(protos) protos
#  else /* no PROTOTYPES */
#    define PARAMS(protos) ()
#  endif /* no PROTOTYPES */
#endif

/* Do not allow redefinition of malloc and realloc. */
#undef malloc
#undef realloc

#if defined (YYLMAX)
# undef YYLMAX
# define YYLMAX 1024
#endif

void reassemble PARAMS ((FILE*,FILE*,FILE*,char*,char*,char*));
static int first_time = 1;
FILE *font;
FILE *adobe;
FILE *body;
int ifpagesv   = 0;
int ifcorel    = 0;
int ifendsetup = 0;
int pid;
char  fontfname[255];
char adobefname[255];
char  bodyfname[255];
#ifdef _WIN32
char tmpdir[_MAX_PATH];
#else
const char *tmpdir;
#endif
%}

%Start DUMMY NORMAL BODY ADOBE FONT

%%

%{
  if(first_time){
    BEGIN DUMMY;
    first_time = 0;
  }
%}

<DUMMY>%!.*\n { /* regular Postscript starts here ... */
      ECHO;
      BEGIN NORMAL;
}

<DUMMY>^.*\n { /* nothing to do */
}

<NORMAL>%%Title:.*CorelDRAW.*\n { /* Corel Draw modus recognized */
  ifcorel = 1;
  ECHO;
}

<NORMAL>%%EndSetup.*\nNTPSOct95[ ]+begin.*\n { /* suppress EndSetup */
  fprintf(yyout,"NTPSOct95 begin\n");
  ifendsetup = 1;
}

<NORMAL>%%Page:.*\n { /* read up to first page and just output everyting */
  sprintf(bodyfname, "%s/fixnt_Body_%d", tmpdir, pid);
  body = fopen(bodyfname,"w+");
  fprintf(body,"%s",yytext);
  BEGIN BODY;
}

<NORMAL>.*\n {    /* o.k. */
                 fprintf(yyout,"%s",yytext);
}

<BODY>[/]Adobe_WinNT_Driver_Gfx[ ]175[ ]dict[ ]dup[ ]begin.*\n { /* Adobe Stuff */
  sprintf(adobefname, "%s/fixnt_Adobe_%d", tmpdir, pid);
  adobe = fopen(adobefname,"w+");
  fprintf(adobe,"%s",yytext);
  BEGIN ADOBE;
}

<ADOBE>end[ ]reinitialize.*\n { /* End of Adobe definition stuff */
  fprintf(adobe,"%s",yytext);
  BEGIN BODY;
}

<ADOBE>.*\n { /* print just everything ... */
  fprintf(adobe,"%s",yytext);
}


<BODY>^[ ]@gs[ ]spg[ ]@gr.*\n { ;
  /* remove showpage definition of CorelDraw defintions */
  if( ifcorel == 1 ){
    fprintf(body," @gs @gr\n"); /* Corel draw defines spg as showpage */
  } else {
    fprintf(body,"%s",yytext);  /* Somebody else defined spg, leave it in */
  }
}

<BODY>(NTPSOct95[ ]){0,1}[/]FontSV[ ]save[ ](put|def).*\n { ;
  /* font definitions from here on  */
  sprintf(fontfname, "%s/fixnt_Font_%d", tmpdir, pid);
  font = fopen(fontfname,"w+");
  fprintf(font,"%s",yytext);
  BEGIN FONT;
}

<FONT,BODY>%%BeginFont:.*\n { /* Font definitions go to FILE* font */
  fprintf(font,"%s",yytext);
  BEGIN FONT;
}

<FONT>%%EndFont.*\n { /* End of Font Definition */
  fprintf(font,"%s",yytext);
  BEGIN BODY;
}

<FONT,BODY>%%BeginResource:[ ]font.*\n { ;
/* NT 3.5 Postscript Files have different FontDefinitions */
 fprintf(font,"%s",yytext);
 BEGIN FONT;
}

<FONT>%%EndResource.*\n { ;
/* NT 3.5 Postscript Files have different FontDefinitions */
 fprintf(font,"%s",yytext);
 BEGIN BODY;
}

<FONT>.*\n { /* goes all to FILE *font ... */
  fprintf(font,"%s",yytext);
}

<BODY>(NTPSOct95[ ]){0,1}[/]PageSV[ ]save[ ](put|def).*\n { ;
  ifpagesv = 1;
}

<BODY>Adobe_WinNT_Driver_Gfx[ ]dup[ ][/]terminate[ ]get[ ]exec.*\nFontSV[ ]restore.*\nPageSV[ ]restore.*\n%%Trailer.*\n {
  fprintf(body,"%%%%Trailer\nAdobe_WinNT_Driver_Gfx dup /terminate get exec\nPageSV restore\nFontSV restore\n");
}

<BODY>FontSV[ ]restore.*\nPageSV[ ]restore.*\n    { ;
  /* switch FontSV/PageSV */
  fprintf(body,"PageSV restore\nFontSV restore\n");
}

<BODY>PageSV[ ]restore.*\n%%Trailer.*\n    { ;
/* put PageSV after Trailer */
  fprintf(body,"%%%%Trailer\nPageSV restore\n");
}


<BODY>FontSV[ ]restore.*\nPageSV[ ]restore.*\n%%Trailer.*\n    { ;
/* put PageSV/FontSV after Trailer */
  fprintf(body,"%%%%Trailer\nPageSV restore\nFontSV restore\n");
}


<BODY>PageSV[ ]restore.*\n { ;
/* do not allow any PageSV's to appear within the actual body */
}


<BODY>%%EOF.*\n { ;
/* What to do at the end of the file (either recognized by %%EOF or
   reg. end of file */
 fprintf(body,"%s",yytext);
 return 0;
}

<BODY><<EOF>> { ;
/* This section is obsolete, could be removed ..., assume there is no
   string <<EOF>> in a Postscript file ... */

/* Only flex knows about the <<EOF>> symbol therefore put the
   reassembling process into the main program. Plain lex does return 0
   for regular end of file marks. */
 return 0;
}

<BODY>.*\n { /* put pages in FILE *body */
  fprintf(body,"%s",yytext);
}

%%
int
yywrap ()
{
  return 1;
}

int
main ()
{
#ifdef _WIN32
  GetTempPath(_MAX_PATH, tmpdir);
#else
  if (!(tmpdir = getenv("TMPDIR")))
    tmpdir = "/tmp";
#endif
  pid = (int) getpid ();
  yylex();
  /* at the end always reassemble the file and clean up ... */
  reassemble(adobe, font, body, adobefname, fontfname, bodyfname);

  return 0;
}

void
reassemble (adobe, font, body, adobefname, fontfname, bodyfname)
  FILE* adobe;
  FILE* font;
  FILE* body;
  char *adobefname;
  char *fontfname;
  char *bodyfname;
{
  int xx;

  /* put everything in right order ... */
  if( adobe != NULL ){
    rewind(adobe);
    while( (xx = getc( adobe )) != EOF ){
      putc(xx,yyout);
    }
    fclose(adobe);
    remove(adobefname);
  }

  if( font != NULL){
    rewind(font);
    while( (xx = getc( font )) != EOF ){
      putc(xx,yyout);
    }
    fclose(font);
    remove(fontfname);
  }

  if( ifendsetup == 1){
    fprintf(yyout,"%%%%EndSetup:\n");
  }

  if( body != NULL){
    rewind(body);
    if(ifpagesv == 1)
      fprintf(yyout,"NTPSOct95 /PageSV save put\n");
    while( (xx = getc( body )) != EOF ){
      putc(xx,yyout);
    }
    fclose(body);
    remove(bodyfname);
  }
  return;
}

/*
  Local Variables:
  Mode: C
  End:
*/