| 1 |
Notes for maintainers of M-Tx |
| 2 |
============================= |
| 3 |
|
| 4 |
This file is for someone who wishes to fix a bug, without actually being |
| 5 |
willing to master the whole program of well over 4000 lines spread over |
| 6 |
16 files. |
| 7 |
|
| 8 |
1. Quick start |
| 9 |
-------------- |
| 10 |
|
| 11 |
The main program is `prepmx.pas`. The other files are units. |
| 12 |
|
| 13 |
There are three places in each file that give a lot of useful information. |
| 14 |
When you have a spare moment, look at those. It will save time later. |
| 15 |
|
| 16 |
1. Immediately after the first line, there should be a comment |
| 17 |
containing a short description of what is in the file. |
| 18 |
2. After the keyword `interface`, there may be a `uses` statement |
| 19 |
listing the units needed in order that the interface (the Pascal |
| 20 |
equivalent of a C header file) can be defined. |
| 21 |
3. After the keyword `implementation`, there may be a `uses` statement |
| 22 |
listing the further units needed in order that the implementation |
| 23 |
(the equivalent of a C code file) can be defined. |
| 24 |
|
| 25 |
Instead of some dull generalities, here is a blow-by-blow account of how an |
| 26 |
actual bug reported by Wojtek Chemijewski on the Werner Icking Music Archive |
| 27 |
mailing list `<[email protected]>` on 2014-01-29 was found and "corrected". |
| 28 |
|
| 29 |
2. Case study |
| 30 |
------------- |
| 31 |
|
| 32 |
I'm doing this from a command line in Ubuntu Linux, so I use `grep`. |
| 33 |
Any other tool that can search for text in the Pascal source files |
| 34 |
will do equally well. |
| 35 |
|
| 36 |
In M-Tx 0.60, the following behaviour was considered to be a bug: |
| 37 |
|
| 38 |
> If the piece starts with a pickup, then the bar separator at the end |
| 39 |
of the pickup is only recognized as terminating a pickup when it is |
| 40 |
a plain bar line `|`, not when it is a left repeat `|:`. |
| 41 |
|
| 42 |
There are workarounds for this, the best one being to not use plain bar |
| 43 |
lines but to use the PMX command `Rl` in the lowest voice (which becomes |
| 44 |
the first voice in PMX). That is not the point. We want M-Tx to do this |
| 45 |
right. |
| 46 |
|
| 47 |
We start by looking for the string `|:` in all the source files. |
| 48 |
|
| 49 |
$ grep '|:' *.pas |
| 50 |
prepmx.pas: else if bar='|:' then repcode:='Rl' |
| 51 |
prepmx.pas: else if bar=':|:' then repcode:='Rlr' |
| 52 |
|
| 53 |
(BTW, although I continue using `grep`, I won't again insult your |
| 54 |
intelligence by quoting the exact invocation.) |
| 55 |
|
| 56 |
On reading the source, we note that both references are inside |
| 57 |
a procedure called `writeRepeat`. |
| 58 |
|
| 59 |
procedure writeRepeat(var bar: string); |
| 60 |
var repcode: string; |
| 61 |
begin if bar='' then exit; |
| 62 |
repcode:=''; |
| 63 |
if bar='||' then repcode:='Rd' |
| 64 |
else if (bar='|]') then repcode:='RD' |
| 65 |
else if bar='|:' then repcode:='Rl' |
| 66 |
else if bar=':|:' then repcode:='Rlr' |
| 67 |
else if bar=':|' then repcode:='Rr' |
| 68 |
else if last_bar and (bar='|') then repcode:='Rb'; |
| 69 |
if repcode<>'' then putLine(' '+repcode); |
| 70 |
bar:=''; |
| 71 |
end; |
| 72 |
|
| 73 |
This clearly writes to the PMX file the PMX code to which the M-Tx |
| 74 |
bar symbol translates. It can't be the cause of the bug. |
| 75 |
|
| 76 |
Where is `writeRepeat` used? Another `grep` says: only in `prepmx.pas` |
| 77 |
itself, four times. |
| 78 |
|
| 79 |
The first one is in the following context: |
| 80 |
|
| 81 |
barword: begin |
| 82 |
if voice=nvoices then |
| 83 |
if endOfBar(voice,bar_no) then repeat_sign := note |
| 84 |
else writeRepeat(note); |
| 85 |
if note<>barsym then note:=''; |
| 86 |
no_chords:=false; |
| 87 |
end; |
| 88 |
|
| 89 |
This code tests whether we are at the end of a bar; if so, the bar |
| 90 |
word is remembered as `repeat_sign`, otherwise it is written |
| 91 |
immediately. We are following a breadth-first strategy, so we don't |
| 92 |
yet look at `endOfBar`, but continue with examining the references |
| 93 |
to `writeRepeat`. |
| 94 |
|
| 95 |
The second reference in context: |
| 96 |
|
| 97 |
if bar_of_line>1 then putLine(comment+'Bar '+toString(bar_no)); |
| 98 |
last_bar := (bar_of_line=nbars) and final_paragraph; |
| 99 |
if last_bar and (repeat_sign='|') then repeat_sign:=''; |
| 100 |
writeRepeat(repeat_sign); new_meter := ''; |
| 101 |
|
| 102 |
This occurs in a procedure called `processOneBar`. The first thing is |
| 103 |
to write out the `repeat_sign` previously saved. It also does not |
| 104 |
seem to be the cause of the bug. |
| 105 |
|
| 106 |
The third reference is inside a loop over all voices. If that is the |
| 107 |
problem, we may have to dig very deeply, so we postpone looking at it. |
| 108 |
|
| 109 |
The last reference is inside the procedure `doMusic`. |
| 110 |
|
| 111 |
if startsWithBracedWord(P[1]) then lyricsParagraph else |
| 112 |
begin musicParagraph; first_paragraph:=false; |
| 113 |
writeRepeat(repeat_sign); |
| 114 |
end |
| 115 |
|
| 116 |
That boolean `first_paragraph` sticks out like a sore thumb. The |
| 117 |
program is doing something differently, depending on whether we are |
| 118 |
doing the first paragraph or a later one! That cannot be unrelated |
| 119 |
to the question of a pickup. |
| 120 |
|
| 121 |
Let's look for where `first_paragraph` is used. In `files.pas` there |
| 122 |
is something that appears to be handling inline TeX, the others are |
| 123 |
in `prepmx.pas`. It appears in this conditional: |
| 124 |
|
| 125 |
if first_paragraph then includeStartString; |
| 126 |
|
| 127 |
Let's find that procedure. It's in `analyze.pas`. The procedure itself |
| 128 |
is clearly blameless, but also near the top of `analyze.pas` our eye |
| 129 |
alights on `pickup:=0;`. The statement appears twice, the second time |
| 130 |
probably needlessly, but that's beside the point. We now know there is |
| 131 |
a variable named `pickup` and unless the author of the program is |
| 132 |
deliberately misleading us, it must be relevant to the pickup. |
| 133 |
|
| 134 |
Further on in `analyze.pas`, we find: |
| 135 |
|
| 136 |
scanMusic(voice,l); |
| 137 |
if multi_bar_rest and (nv>1) then error( |
| 138 |
'Multi-bar rest allows only one voice',print); |
| 139 |
if not pmx_preamble_done then |
| 140 |
if voice=top then pickup:=l |
| 141 |
else if pickup<>l then |
| 142 |
error3(voice,'The same pickup must appear in all voices'); |
| 143 |
|
| 144 |
I.e. the procedure `scanMusic` sets a value `l` that will be assigned |
| 145 |
to `pickup`. We're getting warm! |
| 146 |
|
| 147 |
That procedure is in `mtx.pas`. It is a very long procedure, but the |
| 148 |
argument `l` is now the parameter `left_over`, easy to search for. |
| 149 |
The only place where it seems to be used after being initialized is |
| 150 |
in the following compound statement: |
| 151 |
|
| 152 |
if (note=barsym) then |
| 153 |
if meternum=0 then |
| 154 |
error3(voice,'You may not use bar lines in barless music') |
| 155 |
else if bar_length=0 then markBar(voice) |
| 156 |
else if (numberOfBars(voice)=0) and (bar_length<bar) then |
| 157 |
begin if has_next then |
| 158 |
has_next:=false {Should check whether pickups are equal} |
| 159 |
else if left_over>0 then error3(voice,'Bar is too short'); |
| 160 |
left_over:=bar_length; bar_length := 0; |
| 161 |
end; |
| 162 |
|
| 163 |
What is `barsym`? These are the places where the name appears. |
| 164 |
|
| 165 |
analyze.pas: is_labelled := (w[l]=colon) and (w[l-1]<>barsym); |
| 166 |
globals.pas: barsym = '|'; |
| 167 |
mtxline.pas: if chord>0 then if P[chord]=barsym then predelete(P[chord],1); |
| 168 |
mtx.pas: if (note=barsym) then |
| 169 |
prepmx.pas: if (w=barsym) or (w='') then no_chords:=true; |
| 170 |
prepmx.pas: if note<>barsym then note:=''; |
| 171 |
uptext.pas: if (w=barsym) or (w='') then no_uptext:=true; |
| 172 |
|
| 173 |
Our quest is over! This is the bug. The whole code starts with |
| 174 |
`if (note=barsym) then`. The parentheses indicate that at some |
| 175 |
previous stage of the code, the programmer thought that there may |
| 176 |
have been extra clauses, such as testing for left repeat, but forgot |
| 177 |
to put them in. We also note further evidence of not-quite-completed |
| 178 |
code in the comment `{Should check whether pickups are equal}`. |
| 179 |
|
| 180 |
How will we fix this? Should we have `if isBarSym(note) then` and supply |
| 181 |
a function `isBarSym`, or merely add a clause? It appears as if the |
| 182 |
other cases do not require the same fixing, so the function will only be |
| 183 |
called this once. Let's try: |
| 184 |
|
| 185 |
if note[1]=barsym then |
| 186 |
|
| 187 |
That will cover `|`, `|:` and `||` but not `:|`, which seems fair |
| 188 |
enough. It will also cover illegal cases like `|-`, but let's hope |
| 189 |
those are caught elsewhere. |
| 190 |
|
| 191 |
The change is enough to cure the reported bug, so we document it at the |
| 192 |
top of `prepmx.pas` and in `Corrections`, changing the version number to |
| 193 |
`0.60e`. |
| 194 |
|
| 195 |
### It was not a bug |
| 196 |
|
| 197 |
After carrying out the above exercise, I carefully reread the M-Tx |
| 198 |
manual and found: |
| 199 |
|
| 200 |
> If your piece starts with a pickup, it is defined by a bar line at the |
| 201 |
> end of “bar 0”, i.e. the incomplete bar containing the pickup. Even |
| 202 |
> when there is a repeat sign after the pickup, so that you don’t |
| 203 |
> actually see a bar line in the printed music, you still need a bar |
| 204 |
> line before the repeat sign, otherwise M-Tx cannot know where the |
| 205 |
> pickup stops. The bar line defining the pickup is compulsory in the |
| 206 |
> first voice found, and optional but recommended in the others. |
| 207 |
|
| 208 |
Be warned, therefore, that M-Tx 0.60e is a dead end. The next version |
| 209 |
of M-Tx will branch from 0.60d again. |
| 210 |
|
| 211 |
3. Adding a preamble command |
| 212 |
---------------------------- |
| 213 |
|
| 214 |
In M-Tx 0.61, the `TeX` preamble command was added. Obviously we start |
| 215 |
in `preamble.pas`. There are some pretty obvious lists contained in |
| 216 |
`type` and `const` statements. |
| 217 |
|
| 218 |
The name lower-case name `tex` is inserted in the list of names |
| 219 |
`command_type`, the upper case version `TEX` in the corresponding |
| 220 |
position in `commands`, a default empty value in `cline` and an |
| 221 |
extra `false` in `redefined`. This is enough to get the `TeX` command |
| 222 |
recognized, to save the value given, and to record the fact that there |
| 223 |
was a change. |
| 224 |
|
| 225 |
We want the TeX string to appear immediately after `readmod`. If you |
| 226 |
examine a `.tex` file made by PMX from source coded by M-Tx, you will |
| 227 |
see that `\mtxInterInstrument` is there. The only line containing that |
| 228 |
word is in `preamble.pas`: |
| 229 |
|
| 230 |
if nspace[j]<>unspec then TeXtype2('\mtxInterInstrument{'+toString(i-1)+ |
| 231 |
|
| 232 |
I.e. it was inserted as a "Type 2 TeX string". The line in question is |
| 233 |
inside the function `respace`, which we see is executed immediately |
| 234 |
after the statement `pmx_preamble_done:=true;`. We need to squeeze |
| 235 |
in between, adding a call |
| 236 |
|
| 237 |
insertTeX; |
| 238 |
|
| 239 |
At some respectable point before that, we define this procedure. |
| 240 |
|
| 241 |
procedure insertTeX; |
| 242 |
begin |
| 243 |
if redefined[tex] then TeXtype2(cline[tex]); |
| 244 |
end; |
| 245 |
|
| 246 |
Test it; it works. However, if we issue more than one `TeX` command, |
| 247 |
only the last one takes effect, whereas the user will want all. |
| 248 |
Looking for assignments into `cline`, we find in procedure `doCommand` |
| 249 |
the statement: |
| 250 |
|
| 251 |
cline[last_command]:=line; |
| 252 |
|
| 253 |
The entire loop containing that statement needs a bit of reorganization, |
| 254 |
since we must not only allow for appending new text but also bypass the |
| 255 |
test for redefinition warnings. First we supply a function |
| 256 |
|
| 257 |
function mustAppend(command: command_type): boolean; |
| 258 |
begin mustAppend := command=tex end; |
| 259 |
|
| 260 |
which allows for possible future preamble commands that also append |
| 261 |
rather than redefine. The new code of the loop starting with |
| 262 |
|
| 263 |
if mustAppend(last_command) and redefined[last_command] then |
| 264 |
|
| 265 |
is straightforward, almost. Do we put in `LineEnding` when we append? |
| 266 |
If we don't, we may run into problems with `PMXlinelength` which is |
| 267 |
only 128. If we do, `TeXtype2` needs to be made a little smarter, |
| 268 |
i.e. able to re-split the lines. Actually, in that case an explicit |
| 269 |
`#10` is better, since on some systems `LineEnding` has two characters. |
| 270 |
|
| 271 |
`TeXtype2` is in `files.pas`. There is a line |
| 272 |
|
| 273 |
else if first_paragraph then putLine('\'+s+'\') |
| 274 |
|
| 275 |
that we replace by a call to a routine `putTeXlines` which does |
| 276 |
the necessary. |
| 277 |
|
| 278 |
|