ibmize.clu 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. %
  2. % IBMIZE -- Extract underline and boldface info. from a
  3. % lineprinter file (and convert for the IBM)
  4. %
  5. % Control chararacters handled: TAB, NL, FF, CR
  6. % Other control characters assumed to be printing.
  7. % Tab stops assumed every 8 columns.
  8. % 9/14/82 Added handling of empty lines at end of page.
  9. % Somewhat ugly change.
  10. % The pgstream represents the state of output. Pgline
  11. % is the current line within the page, beginning at 1.
  12. % Emptycount keeps track of saved up lines with no visible
  13. % contents. These will be output if a nonempty line arrives
  14. % before end of page.
  15. pgstream = record[pgline: int, s: stream, emptycount: int]
  16. ac = array[char]
  17. % Line with possible underscore and/or boldface
  18. u_b_line = record[line: array[char],
  19. underscore: array[bool],
  20. bold: array[bool]]
  21. LINE_LENGTH = 150 % maximum printing length of output line
  22. main = proc ()
  23. sin: stream := get_io("read", "Input file: ", "lpt")
  24. except others: return end
  25. sout: stream := get_io("write", "Output file: ", "ibm")
  26. except others: return end
  27. process_file(sin, pgstream${s: sout, pgline: 1, emptycount: 0})
  28. stream$close(sin)
  29. stream$close(sout)
  30. end main
  31. % process_file(sin: stream, lout: pgstream)
  32. % Reads from sin until end of file, process each line to make
  33. % overstriking work, and keeps track of the position on the current
  34. % page, inserting form feeds as it deems necessary.
  35. process_file = proc (sin: stream, lout: pgstream)
  36. oline: u_b_line :=
  37. u_b_line${line: ac$fill(0, LINE_LENGTH, ' '),
  38. underscore: array[bool]$fill(0, LINE_LENGTH, false),
  39. bold: array[bool]$fill(0, LINE_LENGTH, false)}
  40. sout: stream := lout.s
  41. while true do
  42. process_line(sin, lout, oline)
  43. end except others: end
  44. %% stream$putc(sout,'\p')
  45. end process_file
  46. process_line = proc (sin: stream, lout: pgstream, oline: u_b_line)
  47. signals (done)
  48. sout: stream := lout.s
  49. line: string := get_line(sin)
  50. except others: signal done end
  51. %% Insert FF if needed.
  52. %% if lout.pgline > 60 cand ~ char$equal(string$fetch(line,1),'\p')
  53. %% then
  54. %% stream$putc (sout, '\p')
  55. %% lout.pgline := 1
  56. %% lout.emptycount := 0
  57. %% end
  58. for i: int in int$from_to(0,LINE_LENGTH - 1) do
  59. oline.line[i] := ' '
  60. oline.underscore[i] := false
  61. oline.bold[i] := false
  62. end
  63. col: int := 0
  64. for c: char in string$chars (line) do
  65. %% Special handling for non-printing chars and '_'
  66. if c = ' ' then col := col + 1
  67. elseif c = '\r' then col := 0
  68. elseif c = '\n' then lout.pgline := lout.pgline + 1
  69. elseif c = '\b' then col := col - 1
  70. elseif c = '\t' then col := col + 8 - (col // 8)
  71. elseif c = '\p' then
  72. col := 0
  73. lout.pgline := 1
  74. elseif c = '_' then
  75. oline.underscore[col] := true
  76. col := col + 1
  77. else
  78. oc: char := oline.line[col]
  79. if oc = ' ' then
  80. oline.line[col] := c
  81. elseif oc = c then
  82. oline.bold[col] := true
  83. end
  84. col := col + 1
  85. end
  86. end
  87. emptyp: bool := true
  88. for i: int in int$from_to(0,LINE_LENGTH - 1) do
  89. if oline.line[i] ~= ' ' cor
  90. oline.underscore[i] then
  91. emptyp := false
  92. break;
  93. end
  94. end
  95. if emptyp then
  96. lout.emptycount := lout.emptycount + 1
  97. else
  98. %% Put out any saved-up empty lines first
  99. for i:int in int$from_to(1,lout.emptycount) do
  100. stream$putc(sout,'\n')
  101. end
  102. lout.emptycount := 0
  103. %% Print out everything involved in the line.
  104. output_line(oline, sout)
  105. end
  106. %% Print the formfeed that came with (terminating) the line.
  107. if char$equal('\p',string$fetch(line,string$size(line))) then
  108. stream$putc(sout,'\p')
  109. %% Throw away any empty lines just preceding \p
  110. lout.emptycount := 0
  111. elseif ~emptyp then
  112. stream$putc(sout,'\n')
  113. end
  114. end process_line
  115. % output_line(oline, sout: stream)
  116. output_line = proc(oline: u_b_line, sout: stream)
  117. high: int := line_high(oline)
  118. for i: int in int$from_to (0, high) do
  119. stream$putc(sout, oline.line[i])
  120. if oline.underscore[i] then
  121. stream$putc(sout, '\b')
  122. stream$putc(sout, '_')
  123. end
  124. end
  125. %% stream$putc (sout, '\n')
  126. end output_line
  127. % line_high (line: u_b_line) returns (int)
  128. % Returns the index in the line of the last printing character.
  129. % If none exists, returns the minimum index minus 1.
  130. line_high = proc(oline: u_b_line) returns (int)
  131. for i: int in
  132. int$from_to_by(ac$high(oline.line), ac$low(oline.line), -1)
  133. do
  134. if oline.line[i] ~= ' '
  135. cor oline.underscore[i]
  136. then return(i) end
  137. end
  138. return(ac$low(oline.line) - 1)
  139. end line_high
  140. % get_line (sin: stream) returns (string) signals (end_of_file)
  141. % Reads from the stream characters through the first \n or \p.
  142. % If end of file is reached before any characters are entered,
  143. % end of file is signalled, otherwise not.
  144. % All characters read are returned.
  145. get_line = proc (sin: stream) returns (string) signals (end_of_file)
  146. a: ac := ac$new ()
  147. while true do
  148. c: char := stream$getc_image (sin)
  149. except others:
  150. if ac$size (a) = 0 then signal end_of_file end
  151. break
  152. end
  153. ac$addh (a, c)
  154. if c = '\n' cor c = '\p' then break end
  155. end
  156. %% if ac$top (a) = '\r' then ac$remh (a) end except when bounds: end
  157. return (string$ac2s (a))
  158. end get_line
  159. %%% Defines: get_line line_high main output_line process_file process_line
  160. %%% Edited: 14 September 1982 10:41:36
  161. %%% Uses: get_io
  162. %%% Written: 14 September 1982 10:45:04