summaryrefslogtreecommitdiff
path: root/Build/source/utils/m-tx/mtx-src/files.pas
blob: 446a7041508a11fba74932bb345f23b1bbd604f2 (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
unit files;
{ Open and close files, interpret command line, do input, do most output}

interface  uses globals, multfile;

procedure readParagraph (var P: paragraph; var no: line_nos;
  var L: paragraph_index0);
function styleFileFound: boolean;
procedure processOption(j: char);
procedure OpenFiles;
procedure CloseFiles;
procedure putLine (line: string);
procedure put(line: string; putspace: boolean);
procedure TeXtype2 (s: string);
function endOfInfile: boolean;

implementation uses control, strings, utility;

const  param_leader = '-';
       stylefilename: string = 'mtxstyle.txt';

var teststyle: integer;

function endOfInfile: boolean;
  begin endOfInfile:=eofAll; end;

procedure putTeXlines(s: string);
  var p: integer;
begin p:=pos1(#10,s);
  while p>0 do
  begin
    putLine('\'+substr(s,1,p-1)+'\');
    predelete(s,p);
    p:=pos1(#10,s)
  end; 
  if length(s)>0 then putLine('\'+s+'\');
end;

procedure TeXtype2 (s: string);
  begin
    if pmx_preamble_done then
      if s[1]='%' then putLine(s)
      else if first_paragraph then putTeXLines(s)
      else putLine('\\'+s+'\')
    else putLine(s);
  end;

procedure putLine (line: string);
  begin  if outlen+length(line) > pmxlinelength-1 then writeln(outfile);
    writeln(outfile,line); outlen:=0;
  end;

procedure put(line: string; putspace: boolean);
  var l: integer;
  begin l:=length(line);
    if l>pmxlinelength then error('Item longer than PMX line length',print);
    if outlen+l > pmxlinelength-1 then
    begin putLine(''); put(line,false) end
    else begin if putspace and (outlen>0) and (line[1]<>' ') then
        line:=' '+line;
      write(outfile,line); inc(outlen,l);
    end
  end;

function styleFileFound: boolean;
begin styleFileFound := teststyle<>0; end;

procedure helpmessage;
begin 
  writeln('Usage: prepmx [-bcfnhimtuvwDH0123456789] MTXFILE [TEXDIR] [STYLEFILE]')
end;

procedure bighelpmessage;
begin helpmessage; writeln;
  writeln('MTXFILE: name of .mtx file without its extension');
  writeln('TEXDIR: directory where the TeX file made by PMX goes, default is ./');
  writeln('STYLEFILE: name of a file containing style definitions.  Default is');
  writeln('  mtxstyle.txt.  This feature is now deprecated; use Include: STYLEFILE');
  writeln('  in the preamble of the .mtx file instead.'); 
  writeln('Options:  (can also be entered separately: -b -c ...)');
  writeln('  -b: disable unbeamVocal');
  writeln('  -c: disable doChords');
  writeln('  -f: enable solfaNoteNames');
  writeln('  -h: display this message and quit');
  writeln('  -i: enable ignoreErrors');
  writeln('  -m: disable doLyrics');
  writeln('  -n: enable instrumentNames');
  writeln('  -t: disable doUptext');
  writeln('  -u: disable uptextOnRests');
  writeln('  -v: enable beVerbose');
  writeln('  -w: enable pedanticWarnings');
  writeln('  -D: enable debugMode');
  writeln('  -0123456789: select Case');
  writeln('  -H: print enabled status of all options');
  writeln('All the above, and some other, options can be enabled or disabled');
  writeln('  in the preamble.  What you do there overrides what you do here.')  
end;

procedure processOption(j: char);
  begin  case j of
  'b': setFeature('unbeamVocal',false);
  'c': setFeature('doChords',false);
  'f': setFeature('solfaNoteNames',true);
  'h': begin bighelpmessage; halt(255) end;
  'i': setFeature('ignoreErrors',true);
  'm': setFeature('doLyrics',false);
  'n': setFeature('instrumentNames',true);
  't': setFeature('doUptext',false);
  'u': setFeature('uptextOnRests',false);
  'v': setFeature('beVerbose',true);
  'w': setFeature('pedanticWarnings',true);
  'D': setFeature('debugMode',true);
  '0'..'9': choice:=j;
  'H': printFeatures(true);
  else write(j); error(': invalid option',not print);
  end;
end;

procedure OpenFiles;
var i, j, l, fileid, testin: integer;
    infilename, outfilename, basename, param, ext: string;
  procedure checkExistingFile;
    var tryfile: file;
  begin
{$I-}
    assign(tryfile,basename);  reset(tryfile);  testin := ioresult;
{$I+}
    if testin<>0 then exit else close(tryfile);
    writeln('There exists a file named ',basename,'.  I am treating this');
    error('  as a fatal error unless you specify -i',not print);
  end;
begin
  fileid:=0;  line_no:=0;  paragraph_no:=0;
  for i:=1 to ParamCount do
  begin param:=ParamStr(i);
    if param[1]=param_leader then
      for j:=2 to length(param) do processOption(param[j])
    else if fileid=0 then fileid:=i
    else if texdir='' then texdir:=param
    else stylefilename:=param;
  end;
  if fileid=0 then 
  begin helpmessage; writeln('Try "prepmx -h" for more information.'); halt(255) end
  else basename:=paramstr(fileid);
  l:=length(basename);
  if (l>4) and (basename[l-3]='.') then
  begin ext:=substr(basename,l-2,3); toUpper(ext); if ext='MTX' then
    begin warning('.mtx extension deleted from basename',not print);
      shorten(basename,l-4);
    end;
  end;
  if pos1('.',basename)>0 then checkExistingFile;
  infilename := basename+'.mtx';  outfilename := basename+'.pmx';
{$I-}
  pushFile(infilename); 
  assign(outfile,outfilename);  rewrite(outfile);
  assign(stylefile,stylefilename);  reset(stylefile);  teststyle := ioresult;
  if (teststyle<>0) and (stylefilename<>'mtxstyle.txt') then 
     writeln('Can''t read ',stylefilename);
{$I+}
  if fileError then fatalError('Input file '+infilename+' not found');
  outfile_open := true;  writeln('Writing to ',basename,'.pmx');
end;

procedure CloseFiles;
begin  close(outfile); closeAll; if teststyle=0 then close(stylefile);
end;

procedure readParagraph (var P: paragraph; var no: line_nos;
                         var L: paragraph_index0);
  var another: boolean;
      filename, buffer: string;
begin
  L:=0; buffer:=readData; line_no:=currentLineNo; 
  if isEmpty(buffer) then exit;
  if debugMode then writeln('>>>> ',buffer);
  inc(paragraph_no);  
{ Handle directives affecting the processing of the input file }
  repeat another:=false; 
    if startsWithIgnoreCase(buffer,'SUSPEND') then
      begin ignore_input:=true; another:=true; if beVerbose then
        writeln('-->> Suspending input file ',currentFilename, 
          ' at line ', line_no);
      end;
    if startsWithIgnoreCase(buffer,'RESUME') then
      begin ignore_input:=false; another:=true; if beVerbose then
        writeln('-->> Resuming input file ',currentFilename, 
          ' at line ', line_no);
    end;
    if startsWithIgnoreCase(buffer,'INCLUDE:') then 
    begin predelete(buffer,8); filename:=nextWord(buffer,' ',' ');
      pushfile(filename); another:=true; 
    end;
    if another then begin buffer:=readLine; line_no:=currentLineNo; end;
  until not another;
{ Normal paragraph input}
  repeat
    if L<lines_in_paragraph then 
      begin inc(L); P[L]:=buffer; buffer:=''; no[L]:=line_no; 
      end
    else warning('Paragraph too long: skipping line',not print);
    buffer:=readLine; line_no := currentLineNo;
    if debugMode then writeln(line_no,' >> ', buffer);
  until isEmpty(buffer);
  skipBlanks;  { needed to identify final paragraph }
end;

end.