Debian Codegraph @

utils/m-tx/mtx-src/files.pas

raw
1 unit files;
2 { Open and close files, interpret command line, do input, do most output}
3
4 interface uses globals, multfile;
5
6 procedure readParagraph (var P: paragraph; var no: line_nos;
7 var L: paragraph_index0);
8 function styleFileFound: boolean;
9 procedure processOption(j: char);
10 procedure OpenFiles;
11 procedure CloseFiles;
12 procedure putLine (line: string);
13 procedure put(line: string; putspace: boolean);
14 procedure TeXtype2 (s: string);
15 function endOfInfile: boolean;
16
17 implementation uses control, strings, utility;
18
19 const param_leader = '-';
20 stylefilename: string = 'mtxstyle.txt';
21
22 var teststyle: integer;
23
24 function endOfInfile: boolean;
25 begin endOfInfile:=eofAll; end;
26
27 procedure putTeXlines(s: string);
28 var p: integer;
29 begin p:=pos1(#10,s);
30 while p>0 do
31 begin
32 putLine('\'+substr(s,1,p-1)+'\');
33 predelete(s,p);
34 p:=pos1(#10,s)
35 end;
36 if length(s)>0 then putLine('\'+s+'\');
37 end;
38
39 procedure TeXtype2 (s: string);
40 begin
41 if pmx_preamble_done then
42 if s[1]='%' then putLine(s)
43 else if first_paragraph then putTeXLines(s)
44 else putLine('\\'+s+'\')
45 else putLine(s);
46 end;
47
48 procedure putLine (line: string);
49 begin if outlen+length(line) > pmxlinelength-1 then writeln(outfile);
50 writeln(outfile,line); outlen:=0;
51 end;
52
53 procedure put(line: string; putspace: boolean);
54 var l: integer;
55 begin l:=length(line);
56 if l>pmxlinelength then error('Item longer than PMX line length',print);
57 if outlen+l > pmxlinelength-1 then
58 begin putLine(''); put(line,false) end
59 else begin if putspace and (outlen>0) and (line[1]<>' ') then
60 line:=' '+line;
61 write(outfile,line); inc(outlen,l);
62 end
63 end;
64
65 function styleFileFound: boolean;
66 begin styleFileFound := teststyle<>0; end;
67
68 procedure helpmessage;
69 begin
70 writeln('Usage: prepmx [-bcfnhimtuvwDH0123456789] MTXFILE [TEXDIR] [STYLEFILE]')
71 end;
72
73 procedure bighelpmessage;
74 begin helpmessage; writeln;
75 writeln('MTXFILE: name of .mtx file without its extension');
76 writeln('TEXDIR: directory where the TeX file made by PMX goes, default is ./');
77 writeln('STYLEFILE: name of a file containing style definitions. Default is');
78 writeln(' mtxstyle.txt. This feature is now deprecated; use Include: STYLEFILE');
79 writeln(' in the preamble of the .mtx file instead.');
80 writeln('Options: (can also be entered separately: -b -c ...)');
81 writeln(' -b: disable unbeamVocal');
82 writeln(' -c: disable doChords');
83 writeln(' -f: enable solfaNoteNames');
84 writeln(' -h: display this message and quit');
85 writeln(' -i: enable ignoreErrors');
86 writeln(' -m: disable doLyrics');
87 writeln(' -n: enable instrumentNames');
88 writeln(' -t: disable doUptext');
89 writeln(' -u: disable uptextOnRests');
90 writeln(' -v: enable beVerbose');
91 writeln(' -w: enable pedanticWarnings');
92 writeln(' -D: enable debugMode');
93 writeln(' -0123456789: select Case');
94 writeln(' -H: print enabled status of all options');
95 writeln('All the above, and some other, options can be enabled or disabled');
96 writeln(' in the preamble. What you do there overrides what you do here.')
97 end;
98
99 procedure processOption(j: char);
100 begin case j of
101 'b': setFeature('unbeamVocal',false);
102 'c': setFeature('doChords',false);
103 'f': setFeature('solfaNoteNames',true);
104 'h': begin bighelpmessage; halt(255) end;
105 'i': setFeature('ignoreErrors',true);
106 'm': setFeature('doLyrics',false);
107 'n': setFeature('instrumentNames',true);
108 't': setFeature('doUptext',false);
109 'u': setFeature('uptextOnRests',false);
110 'v': setFeature('beVerbose',true);
111 'w': setFeature('pedanticWarnings',true);
112 'D': setFeature('debugMode',true);
113 '0'..'9': choice:=j;
114 'H': printFeatures(true);
115 else write(j); error(': invalid option',not print);
116 end;
117 end;
118
119 procedure OpenFiles;
120 var i, j, l, fileid, testin: integer;
121 infilename, outfilename, basename, param, ext: string;
122 procedure checkExistingFile;
123 var tryfile: file;
124 begin
125 {$I-}
126 assign(tryfile,basename); reset(tryfile); testin := ioresult;
127 {$I+}
128 if testin<>0 then exit else close(tryfile);
129 writeln('There exists a file named ',basename,'. I am treating this');
130 error(' as a fatal error unless you specify -i',not print);
131 end;
132 begin
133 fileid:=0; line_no:=0; paragraph_no:=0;
134 for i:=1 to ParamCount do
135 begin param:=ParamStr(i);
136 if param[1]=param_leader then
137 for j:=2 to length(param) do processOption(param[j])
138 else if fileid=0 then fileid:=i
139 else if texdir='' then texdir:=param
140 else stylefilename:=param;
141 end;
142 if fileid=0 then
143 begin helpmessage; writeln('Try "prepmx -h" for more information.'); halt(255) end
144 else basename:=paramstr(fileid);
145 l:=length(basename);
146 if (l>4) and (basename[l-3]='.') then
147 begin ext:=substr(basename,l-2,3); toUpper(ext); if ext='MTX' then
148 begin warning('.mtx extension deleted from basename',not print);
149 shorten(basename,l-4);
150 end;
151 end;
152 if pos1('.',basename)>0 then checkExistingFile;
153 infilename := basename+'.mtx'; outfilename := basename+'.pmx';
154 {$I-}
155 pushFile(infilename);
156 assign(outfile,outfilename); rewrite(outfile);
157 assign(stylefile,stylefilename); reset(stylefile); teststyle := ioresult;
158 if (teststyle<>0) and (stylefilename<>'mtxstyle.txt') then
159 writeln('Can''t read ',stylefilename);
160 {$I+}
161 if fileError then fatalError('Input file '+infilename+' not found');
162 outfile_open := true; writeln('Writing to ',basename,'.pmx');
163 end;
164
165 procedure CloseFiles;
166 begin close(outfile); closeAll; if teststyle=0 then close(stylefile);
167 end;
168
169 procedure readParagraph (var P: paragraph; var no: line_nos;
170 var L: paragraph_index0);
171 var another: boolean;
172 filename, buffer: string;
173 begin
174 L:=0; buffer:=readData; line_no:=currentLineNo;
175 if isEmpty(buffer) then exit;
176 if debugMode then writeln('>>>> ',buffer);
177 inc(paragraph_no);
178 { Handle directives affecting the processing of the input file }
179 repeat another:=false;
180 if startsWithIgnoreCase(buffer,'SUSPEND') then
181 begin ignore_input:=true; another:=true; if beVerbose then
182 writeln('-->> Suspending input file ',currentFilename,
183 ' at line ', line_no);
184 end;
185 if startsWithIgnoreCase(buffer,'RESUME') then
186 begin ignore_input:=false; another:=true; if beVerbose then
187 writeln('-->> Resuming input file ',currentFilename,
188 ' at line ', line_no);
189 end;
190 if startsWithIgnoreCase(buffer,'INCLUDE:') then
191 begin predelete(buffer,8); filename:=nextWord(buffer,' ',' ');
192 pushfile(filename); another:=true;
193 end;
194 if another then begin buffer:=readLine; line_no:=currentLineNo; end;
195 until not another;
196 { Normal paragraph input}
197 repeat
198 if L<lines_in_paragraph then
199 begin inc(L); P[L]:=buffer; buffer:=''; no[L]:=line_no;
200 end
201 else warning('Paragraph too long: skipping line',not print);
202 buffer:=readLine; line_no := currentLineNo;
203 if debugMode then writeln(line_no,' >> ', buffer);
204 until isEmpty(buffer);
205 skipBlanks; { needed to identify final paragraph }
206 end;
207
208 end.