summaryrefslogtreecommitdiff
path: root/dviware/crudetype/version3/unix-pas.ch
blob: 898d7af4ef0254f8eec2effd26da0b45eeef25c4 (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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
% unix-pas.ch -*-mode: change; webfile: crudetype.web version 3.01;-*-
% UNIX changes by Peter King for Berkeley Pascal
% modified by RMD for Crudetype version 2 and for v3
%
%
% COPYRIGHT ( C ) P.King,  Heriot-Watt University, 1988.
%
% Permission is given to any person to make and distribute copies of this
% software, subject to the following conditions:
%
% 1. All copies of the software must carry an exact copy of this notice.
%
% 2. This software is distributed free of charge, "AS IS" with absolutely no
% guarantee of performance. Any persons receiving or using this software must do
% so entirely at their own risk. Neither the authors nor their institutions
% accept any liability for any defects of this software, or for any consequential
% loss or damage however caused.
%
% 3. Any person who changes this software must clearly mark it as modified and
% add a note describing the changes made.
%

% WEAVE: print changes only
@x  Module 0; Lines 42 -- 42
\pageno=\contentspagenumber \advance\pageno by 1
@y
\pageno=\contentspagenumber \advance\pageno by 1
\def\title{Crudetype for {\mc UNIX }  }
@z

@x  Module 4; Lines 213 -- 213
@d banner=='This is Crudetype, Version 3.01, copyright, experimental'
@y
@d banner=='This is Crudetype, Version 3.01, copyright, experimental UNIX'
@z

% UNIX writeln moves you down a line
% Suppress unwanted control-M's at ends of lines
@x  Module 6; Lines 242 -- 242
  @<Set initial values@>
@y
  @<Set initial values@>
  setpaths; { set up the correct search path for fonts }
  wl_feed_dist := 1 ;
  wl_does_cr := true ;
@z

% add variables for runtime arguments, and to suppress all the non-error
% information messages
% stdin is not yet used, but is a handle for an extension to read from
% input ( a UNIX pipe )
@x  Module 7; Lines 253 -- 253
  in_i, in_j :integer; {loop index for initializations}
@y
  in_i, in_j :integer; {loop index for initializations}
  stdin : boolean; { read from standard input }
@z

% Change the types
@x  Module 11; Lines 310 -- 312
@<Types...@>=
  byte = 0..255 ;
  i_word = -max_half-1 .. max_half ;
@y
@<Types...@>=
  byte = 0..255 ;
  bite = -128..127 ; { signed ! }
  i_word = -max_half-1..max_half ;
@z

@x  Module 12; Lines 334 -- 336
@d Q_string == packed array[ first..last:integer] of char
@d be_string(#) == set_string( #, buffer)
@d set_j_to_length == j := last
@y
@d Q_string == fix_string
@d be_string(#) == hack := # ; set_string( hack, buffer)
@d set_j_to_length == j := max_string
@d first = 1
@z

@x  Module 14; Lines 360 -- 361
@<Lowest...@>=
  {Declare |parse_file|}
@y
@<Lowest...@>=
  procedure  parse_file( name: var_string; var dir, nam, ex: var_string) ;
  var p,r,s: s_ptr ;
  begin
    dir := blank; nam := blank; ex := blank;
    s := name.len ;
    if ( s>0) then begin
      p := s_search( name, '/', -s);
      if ( p>0) then substring( dir, name, 1, p) ;
      r := s_search( name, '.', -s);
      if ( r>p) then substring( ex, name, r, s-r+1)
      else r := s +1 ;
      substring( nam, name, p+1, r-p-1) ;
    end;
  end;
@z

@x  Module 16; Lines 408 -- 408
@d print_ln ==write_ln(printfile )
@y
@d print_ln == write_ln(printfile );
  if inspection then flush(printfile )
@z

% Changes to incorporate UNIX conventions
% lower case file names, default to putting the .lpr file in the
% same directory as the .dvi file
@x  Module 18; Lines 429 -- 436
@d block_length = 512
@d same_dir == false

@<Set init...@>=
  be_string( '.DVI' ) ; dvi_def := buffer ;
  be_string( 'TEX$FONTS:.TFM' ) ; tfm_def := buffer ;
  be_string( 'TEX$GF:.&DGF' ) ; raster_def := buffer ;
  be_string( '.PRI' ) ; print_ex := buffer ;
@y

The Unix version of \.{Crudetype} uses ``paths''.

@d block_length = 1
@d same_dir == true

@<Set init...@>=
  be_string( '.dvi' ) ;  dvi_def := buffer ;
  be_string( '.tfm' ) ;  tfm_def := buffer ;
  be_string( '/tex/gfdir/.&Dgf' ) ;  raster_def := buffer ;
  be_string( '.lpr' ) ;  print_ex := buffer ;
@z

% More of the UNIX simple minded view of files.
% but including the (optional) directory search for .tfm files
@x  Module 19; Lines 442 -- 443
@<Lowest...@>=
  {Declare |open_binary|}
@y
@d read_access_mode=4  {``read'' mode for |test_access|}
@d write_access_mode=2 {``write'' mode for |test_access|}

@d close_binary(#)== do_nothing ;

@<Lowest...@>=
  procedure setpaths;
    external;
  function test_access(
    accessmode:integer; filepath:integer;
    var name, realname: fixstring ) : boolean;
    external;
@#
  function open_binary (var f_f: byte_file; name: var_string;
    search_path: integer ): boolean;
  begin
    cur_name := name.data ; real_name := blank.data ;
    if test_access(read_access_mode,search_path, cur_name, real_name) then
    begin
      reset(f_f, real_name);
      be_string( real_name ) ;  name := buffer ;
      open_binary := true
    end
    else
    open_binary := false
  end;
@z

@x  Module 20; Lines 446 -- 446
  rewrite(printfile) ;
@y
  if inspection then
    rewrite(printfile, '/dev/tty' )
  else rewrite(printfile, print_name.data ) ;
@z

@x  Module 21; Lines 452 -- 466
@d get_val( #) == # := s_to_i( #, true)
@d prefix == "/"
@d got_cl == ( command.len > 0)
@d read_command_line( #) == do_nothing

@<Lowest...@>=
  {Declare |read_command_line| }
@#
  procedure get_command ;
  var ss: fix_string ;
  begin
    ss := blank.data ;
    read_command_line( ss) ;
    be_string( ss ) ; command := buffer ;
  end;
@y

Unix \P\ does not supply a command line; instead it chops it up and supplies
the pieces using two predeclared procedures |argc| and |argv|. It seemed to me
that the least messy way to fit this onto the rest of the program is to
re-assemble the command line.

@d get_val( #) == # := s_to_i( #, true)
@d prefix == "-"
@d got_cl == ( argc > 1)
@d read_command_line( #) == do_nothing

@<Lowest...@>=
  procedure get_command ;
  var ss: fix_string ; nn: integer ; tt: var_string ;
  begin
    command := blank ;
    for nn := 1 to argc-1 do begin
      ss := blank.data ;
      argv( nn, ss) ;
      be_string( ss  ) ;  tt := buffer ;
      append( command, tt) ;
      incr( command.len) ;
    end;
  end;
@z

@x  Module 27; Lines 560 -- 572
@ If there was no command line, try to send messages to the user at a
terminal. This requires nonstandard \PASCAL\ constructions to handle the
online interaction. So it may be necessary on some systems to omit the dialog.
First, the \.{DVI} file name.

@<Get \.{DVI} file name and open it@>=
  if can_interact and ( dvi_name.len = 0) then repeat
    ask_prompt('DVI file name? ') ;
    get_name( dvi_name) ;
  until ( dvi_name.len > 0) ;
  if not open_and_ask(dvi_file, dvi_indx, dvi_name, dvi_def, true)
  then abort('Couldnt open DVI file')
    @.Fatal: Couldnt open@>
@y
@ If there was no command line, try to send messages to the user at a
terminal. This requires nonstandard \PASCAL\ constructions to handle the
online interaction. So it may be necessary on some systems to omit the dialog.
First, the (\.{DVI}) file name. This part is altered to use Unix style path
searching.

@d no_file_path=0    {no path searching should be done}
@d font_file_path=3  {path specifier for \.{TFM} files}

@<Get \.{DVI} file name and open it@>=
  if can_interact and ( dvi_name.len = 0) then repeat
    ask_prompt('DVI file name? ') ;
    get_name( dvi_name) ;
  until ( dvi_name.len > 0) ;
  dvi_indx := no_file_path ;
  if not open_and_ask(dvi_file, dvi_indx, dvi_name, dvi_def, true)
  then abort('Couldnt open DVI file')
    @.Fatal: Couldnt open@>
@z

@x  Module 28; Lines 574 -- 577
@ But when we come to open a font file, we merely report a failure:

@<Open font file@>=
  font_ok := open_and_ask (tfm_file, tfm_indx, tfm_name, tfm_def, true) ;
@y
@ When we try to open a font file, we search the path specified by the user's
environment variable before we report a failure:

@<Open font file@>=
  tfm_indx := font_file_path ;
  font_ok := open_and_ask (tfm_file, tfm_indx, tfm_name, tfm_def, true) ;
@z

% Binary file handling
@x  Module 33; Lines 661 -- 671
The main input file is the \.{DVI} file. Logically, this is just a stream of
8-bit bytes, with no record or block structure. However VMS \PASCAL\
apparently cannot handle files of this type; so I have adopted the blocking
scheme (due to D.R.Fuchs) from the VMS \.{DVItype} change file. But a lot of
the code has been rewritten. Some other operating systems use similar
blocking schemes; so this code may possibly work without much change. The
program deals with two binary file variables: |@!dvi_file| is the main input
file that we are printing, and |@!tfm_file| the current font metric file from
which character-width information is being read. Each of these has a name and
a counter, declared here; also a default name (system dependent, and so
declared previously).
@y
The main input file is the \.{DVI} file. Logically, this is just a stream of
8-bit bytes, with no record or block structure. UNIX \PASCAL\ can happily
handle these, except that it treats each byte as a signed quantity, so we do
some fudging to make them unsigned. The program deals with two binary file
variables: |@!dvi_file| is the main input file that we are printing, and
|@!tfm_file| the current font metric file from which character-width
information is being read. Each of these has a name declared here.
@z

% Binary file types
@x  Module 33; Lines 675 -- 676
  @!byte_block=packed array [0..block_length-1] of byte ;
  @!byte_file= packed file of byte_block;
@y
  @!byte_file= file of bite;
@z

%   The filenames used by |test_access| are added here.
@x  Module 34; Lines 685 -- 685
  printfile: text ;
@y
  printfile: text ;
  @!cur_name,@!real_name:fix_string ;
   {external name}
@z

% open_and_ask must pass the search path into open_binary
@x  Module 35; Lines 701 -- 701
      success := open_binary(f_f, name ) ;
@y
      success := open_binary(f_f, name, f_c ) ;
@z

% P. King deleted all the VMS indx stuff, since all the files are (logically)
% read one character at a time. I have re-instated it because it is very
% useful when debugging.
%
@x  Module 38; Lines 745 -- 776
@d get_real(#) == read_real(# @& file, # @& indx)

@<Lowest...@>=
  function read_byte(var f_file: byte_file; var f_indx: integer) : byte;
  begin
    if eof(f_file) then
      warn('End of file' )
    else begin
        read_byte := f_file^[f_indx] ;
        incr(f_indx);
        if f_indx =block_length then begin
            get(f_file );
            f_indx:=0;
          end;
      end;
  end ;

@#
  procedure skip_bytes(var f_file: byte_file; var f_indx: integer; n:integer);
    {discard n bytes from |f_file|}
  begin
    if n < 0 then abort('Skip_bytes called with negative number');
    f_indx := f_indx + n;
    while( f_indx >= block_length) do
      begin
        if eof(f_file) then
          warn('End of file' )
        else get(f_file );
        f_indx := f_indx - block_length ;
      end ;
  end;
    @.Error: End of file@> @.Fatal: Skip_bytes...@>
@y
@d read_real == rr_read_real    {|read_real| is  Unix \PASCAL\ procedure}
@d get_real(#) == read_real(# @& file, # @& indx)

@<Lowest...@>=
  function read_byte(var f_file: byte_file; var f_indx: integer) : byte;
  var x:bite;
  begin
    if eof(f_file) then begin
      warn('End of file' ) ;
      read_byte := 0 ;     { return some value }
    end
    else begin
      read(f_file,x);
      if x < 0 then read_byte := x + 256
      else read_byte := x ;
      incr(f_indx);
    end;
  end;

@#
  procedure skip_bytes(var f_file: byte_file; var f_indx: integer; n:integer);
  {discard n bytes from |f_file|}
  var k:integer;
      x:bite;
  begin
    if n < 0 then abort('Skip_bytes called with negative number');
    f_indx := f_indx + n;
    for k:=1 to n do
    begin
      if eof(f_file) then
      warn('End of file' )
      else read(f_file, x );
    end ;
  end;
    @.Error: End of file@> @.Fatal: Skip_bytes...@>
@z