Debian Codegraph @

utils/m-tx/mtx-src/mtxline.c

raw
1 /* Output from p2c 1.21alpha-07.Dec.93, the Pascal-to-C translator */
2 /* From input file "mtxline.pas" */
3
4
5 #include "p2c.h"
6
7
8 #define MTXLINE_G
9 #include "mtxline.h"
10
11
12 boolean bind_left[22] = {
13 false, false, true, false, false, true, true, false, true, true, true,
14 false, true, false, true, false, true, false, false, false, false, false
15 };
16
17 #ifndef STRINGS_H
18 #include "strings.h"
19 #endif
20
21 #ifndef CONTROL_H
22 #include "control.h"
23 #endif
24
25 #ifndef NOTES_H
26 #include "notes.h"
27 #endif
28
29 #ifndef UTILITY_H
30 #include "utility.h"
31 #endif
32
33
34 typedef music_word word_scan[max_words];
35
36 typedef struct line_info {
37 word_index0 here, nword;
38 bar_index0 nbar;
39 paragraph_index0 voice_pos, voice_stave, mus, chord;
40 short extra;
41 boolean vocal;
42 word_index0 bar_bound[max_bars + 1];
43 short word_bound[max_words + 1], orig_word_bound[max_words + 1];
44 word_scan scan;
45 } line_info;
46
47
48 Static Char name[22][10] = {
49 "?", "note", "znote", "lyricsTag", "(", ")", ")(", "[", "]", "_", "PMX<",
50 "macro", "endMacro", "meter", "ornament", "rest", "//", "BAR", "TeX", "@",
51 "firstonly", "ERROR"
52 };
53
54 Static line_info info[maxvoices];
55
56
57 /* ------------------------------------------------------------- */
58
59 short beatsPerLine(void)
60 {
61 short Result;
62 voice_index voice, FORLIM;
63 line_info *WITH;
64
65 FORLIM = nvoices;
66 for (voice = 1; voice <= FORLIM; voice++) {
67 WITH = &info[voice-1];
68 if (WITH->nbar > 0 || WITH->extra > 0) {
69 if (WITH->extra % one_beat > 0) {
70 /* p2c: mtxline.pas, line 99:
71 * Note: Using % for possibly-negative arguments [317] */
72 error3(voice, "Line length not an integer number of beats");
73 }
74 Result = WITH->nbar * meternum + WITH->extra / one_beat;
75 }
76 }
77 return Result;
78 }
79
80
81 void skipChordBar(voice_index voice)
82 {
83 line_info *WITH;
84 Char STR1[256];
85
86 WITH = &info[voice-1];
87 if (WITH->chord > 0) {
88 sprintf(STR1, "%c", barsym);
89 if (!strcmp(P[WITH->chord - 1], STR1))
90 predelete(P[WITH->chord - 1], 1);
91 }
92 }
93
94
95 Char *getBar(Char *Result, voice_index voice, short bar)
96 {
97 line_info *WITH;
98
99 WITH = &info[voice-1];
100 return (substr_(Result, P[WITH->mus - 1],
101 WITH->word_bound[WITH->bar_bound[bar-1]] + 1,
102 WITH->word_bound[WITH->bar_bound[bar]] -
103 WITH->word_bound[WITH->bar_bound[bar-1]]));
104 }
105
106
107 Char *musicLine(Char *Result, voice_index voice)
108 {
109 return strcpy(Result, P[musicLineNo(voice) - 1]);
110 }
111
112
113 paragraph_index0 musicLineNo(voice_index voice)
114 {
115 return (info[voice-1].mus);
116 }
117
118
119 void setMusicLineNo(voice_index voice, paragraph_index lno)
120 {
121 info[voice-1].mus = lno;
122 }
123
124
125 paragraph_index0 chordLineNo(voice_index voice)
126 {
127 return (info[voice-1].chord);
128 }
129
130
131 void setChordLineNo(voice_index voice, paragraph_index lno)
132 {
133 info[voice-1].chord = lno;
134 }
135
136
137 void setVocal(voice_index voice, boolean voc)
138 {
139 info[voice-1].vocal = voc;
140 }
141
142
143 boolean isVocal(voice_index voice)
144 {
145 return (info[voice-1].vocal);
146 }
147
148
149 void setStavePos(voice_index voice, stave_index stave, stave_index pos)
150 {
151 line_info *WITH;
152
153 WITH = &info[voice-1];
154 WITH->voice_pos = pos;
155 WITH->voice_stave = stave;
156 }
157
158
159 stave_index voiceStave(voice_index voice)
160 {
161 return (info[voice-1].voice_stave);
162 }
163
164
165 stave_index voicePos(voice_index voice)
166 {
167 return (info[voice-1].voice_pos);
168 }
169
170
171 voice_index companion(voice_index voice)
172 {
173 short s;
174
175 s = info[voice-1].voice_stave;
176 if (number_on_stave[s-1] == 1)
177 return voice;
178 else if (info[voice-1].voice_pos == 1)
179 return (voice + 1);
180 else
181 return (voice - 1);
182 }
183
184
185 void regroup(voice_index voice)
186 {
187 word_index0 i, j;
188 word_index0 j2 = 0;
189 line_info *WITH;
190 word_index0 FORLIM;
191
192 WITH = &info[voice-1];
193 if (debugMode())
194 printf("Voice %d has %d bars at ", voice, WITH->nbar);
195 if (debugMode()) {
196 FORLIM = WITH->nbar + 1;
197 for (i = 1; i <= FORLIM; i++)
198 printf("%d ", WITH->bar_bound[i]);
199 }
200 FORLIM = WITH->nbar;
201 for (i = 1; i <= FORLIM; i++) {
202 j2 = WITH->bar_bound[i];
203 j = j2 + 1;
204 while (j <= WITH->here &&
205 (bind_left[(long)WITH->scan[j-1]] || WITH->scan[j-1] == barword)) {
206 WITH->bar_bound[i]++;
207 j++;
208 }
209 }
210 if (WITH->extra > 0)
211 WITH->bar_bound[WITH->nbar + 1] = WITH->here;
212 if (debugMode()) {
213 printf(" regrouped to ");
214 FORLIM = WITH->nbar + 1;
215 for (i = 1; i <= FORLIM; i++)
216 printf("%d ", WITH->bar_bound[i]);
217 }
218 if (debugMode())
219 putchar('\n');
220 WITH->nword = WITH->here;
221 }
222
223
224 void resetInfo(voice_index voice, Char *buf)
225 {
226 line_info *WITH;
227
228 WITH = &info[voice-1];
229 strcpy(buf, P[WITH->mus - 1]);
230 *P[WITH->mus - 1] = '\0';
231 WITH->bar_bound[0] = 0;
232 WITH->word_bound[0] = 0;
233 WITH->orig_word_bound[0] = 0;
234 WITH->nbar = 0;
235 WITH->here = 0;
236 }
237
238
239 void clearLabels(void)
240 {
241 voice_index voice, FORLIM;
242 line_info *WITH;
243
244 FORLIM = nvoices;
245 for (voice = 0; voice <= FORLIM - 1; voice++) {
246 WITH = &info[voice];
247 WITH->chord = 0;
248 WITH->mus = 0;
249 }
250 }
251
252
253 void appendNote(voice_index voice, music_word nscan)
254 {
255 line_info *WITH;
256
257 WITH = &info[voice-1];
258 WITH->here++;
259 if (WITH->here > max_words)
260 error3(voice, "Words per line limit exceeded");
261 WITH->scan[WITH->here - 1] = nscan;
262 }
263
264
265 void appendToLine(voice_index voice, Char *note)
266 {
267 line_info *WITH;
268
269 if (*note == '\0')
270 return;
271 WITH = &info[voice-1];
272 sprintf(P[WITH->mus - 1] + strlen(P[WITH->mus - 1]), "%s%c", note, blank);
273 WITH->word_bound[WITH->here] = strlen(P[WITH->mus - 1]);
274 WITH->orig_word_bound[WITH->here] = nextWordBound(orig_P[WITH->mus - 1],
275 note[0], WITH->orig_word_bound[WITH->here - 1]);
276 }
277
278
279 void markBar(voice_index voice)
280 {
281 line_info *WITH;
282
283 WITH = &info[voice-1];
284 if (WITH->nbar == 0)
285 error3(voice, "Empty bar");
286 else
287 WITH->bar_bound[WITH->nbar] = WITH->here;
288 }
289
290
291 short numberOfBars(voice_index voice)
292 {
293 return (info[voice-1].nbar);
294 }
295
296
297 void barForward(voice_index voice, short nbars)
298 {
299 line_info *WITH;
300
301 WITH = &info[voice-1];
302 if (WITH->nbar + nbars < 0)
303 error3(voice, "Next voice before bar is full");
304 if (WITH->nbar + nbars > max_bars)
305 error3(voice, "Bars per line limit exceeded");
306 WITH->nbar += nbars;
307 if (nbars > 0)
308 WITH->bar_bound[WITH->nbar] = WITH->here;
309 }
310
311
312 void setExtraLength(voice_index voice, short ext)
313 {
314 line_info *WITH;
315
316 WITH = &info[voice-1];
317 WITH->extra = ext;
318 WITH->scan[WITH->here] = other;
319 }
320
321
322 short ExtraLength(voice_index voice)
323 {
324 return (info[voice-1].extra);
325 }
326
327
328 voice_index0 findVoice(Char *w_)
329 {
330 voice_index0 Result = 0;
331 Char w[256];
332 short i, FORLIM;
333
334 strcpy(w, w_);
335 curtail(w, ':');
336 FORLIM = nvoices;
337 for (i = 1; i <= FORLIM; i++) {
338 if (!strcmp(w, voice_label[i-1]))
339 return i;
340 }
341 getNum(w, &i);
342 if (i == 0)
343 return Result;
344 if (i > 0 && i <= nvoices)
345 return i;
346 error("Numeric label outside range 1..nvoices", print);
347 return Result;
348 }
349
350
351 Static void info3(voice_index voice)
352 {
353 short p;
354 line_info *WITH;
355
356 WITH = &info[voice-1];
357 printf("In voice \"%s\" near word %d:\n", voice_label[voice-1], WITH->here);
358 p = WITH->orig_word_bound[WITH->here - 1] - 1;
359 if (p < 0)
360 p = 0;
361 printf("%*cV\n", p, ' ');
362 }
363
364
365 void error3(voice_index voice, Char *message)
366 {
367 Char STR1[256];
368
369 info3(voice);
370 sprintf(STR1, " %s", message);
371 error(STR1, print);
372 }
373
374
375 void warning3(voice_index voice, Char *message)
376 {
377 Char STR1[256];
378
379 info3(voice);
380 sprintf(STR1, " %s", message);
381 warning(STR1, print);
382 }
383
384
385 Char *nextMusicWord(Char *Result, voice_index voice)
386 {
387 return (MusicWord(Result, voice, info[voice-1].here));
388 }
389
390
391 Char *MusicWord(Char *Result, short voice, short n)
392 {
393 line_info *WITH;
394
395 WITH = &info[voice-1];
396 if (n > 0 && n <= WITH->nword)
397 return (substr_(Result, P[WITH->mus - 1], WITH->word_bound[n-1] + 1,
398 WITH->word_bound[n] - WITH->word_bound[n-1] - 1));
399 else
400 return strcpy(Result, "");
401 }
402
403
404 music_word thisNote(voice_index voice)
405 {
406 line_info *WITH;
407
408 WITH = &info[voice-1];
409 return (WITH->scan[WITH->here - 2]);
410 }
411
412
413 music_word nextNote(voice_index voice)
414 {
415 line_info *WITH;
416
417 WITH = &info[voice-1];
418 return (WITH->scan[WITH->here - 1]);
419 }
420
421
422 boolean endOfBar(voice_index voice, short bar_no)
423 {
424 line_info *WITH;
425
426 WITH = &info[voice-1];
427 return (WITH->here > WITH->bar_bound[bar_no]);
428 }
429
430
431 void gotoBar(voice_index voice, short bar_no)
432 {
433 line_info *WITH;
434
435 WITH = &info[voice-1];
436 WITH->here = WITH->bar_bound[bar_no-1] + 1;
437 }
438
439
440 Char *getMusicWord(Char *Result, voice_index voice)
441 {
442 line_info *WITH;
443
444 WITH = &info[voice-1];
445 line_no = orig_line_no[WITH->mus - 1];
446 MusicWord(Result, voice, WITH->here);
447 WITH->here++;
448 return Result;
449 }
450
451
452 boolean maybeMusicLine(Char *l_)
453 {
454 Char l[256];
455 Char w[256];
456 music_word nscan;
457
458 strcpy(l, l_);
459 GetNextWord(w, l, blank, dummy);
460 /* p2c: mtxline.pas: Note: Eliminated unused assignment statement [338] */
461 if (pos1(w[0], "abcdefgr()[]{}CMm") == 0)
462 return false;
463 if (pos1(':', w) == 0)
464 return true;
465 getNextMusWord(w, l, &nscan);
466 return (nscan == abcdefg);
467 }
468
469
470 #define nmacros 99
471
472
473 Static boolean macro_initialized = false;
474 Static Char macro_text[nmacros][256];
475
476
477 Static void macroInit(void)
478 {
479 short i;
480
481 if (macro_initialized)
482 return;
483 macro_initialized = true;
484 for (i = 0; i <= nmacros - 1; i++)
485 *macro_text[i] = '\0';
486 }
487
488
489 Static short identifyMacro(Char *s_)
490 {
491 Char s[256];
492 short k;
493
494 strcpy(s, s_);
495 predelete(s, 2);
496 getNum(s, &k);
497 return k;
498 }
499
500
501 /* Local variables for getNextMusWord: */
502 struct LOC_getNextMusWord {
503 Char *buf, *note;
504 } ;
505
506 Local void expandThisMacro(struct LOC_getNextMusWord *LINK)
507 {
508 Char playtag[11];
509 short playID;
510 Char w[256];
511 Char STR1[72];
512 Char STR2[42];
513 Char STR3[68];
514 Char STR4[54];
515 Char STR5[256], STR7[256];
516
517 macroInit();
518 if (strlen(LINK->note) == 1)
519 error("Can't terminate a macro that has not been started", print);
520 playID = identifyMacro(LINK->note);
521 toString(playtag, playID);
522 /* p2c: mtxline.pas, line 317:
523 * Note: Possible string truncation in assignment [145] */
524 if (playID < 1 || playID > 99) {
525 sprintf(STR2, "Macro ID %s is not in range 1..99", playtag);
526 error(STR2, print);
527 }
528 if (LINK->note[1] == 'P') {
529 if (*macro_text[playID-1] == '\0') {
530 sprintf(STR3,
531 "Macro %s inserts empty text: did you define it before use?",
532 playtag);
533 warning(STR3, print);
534 }
535 if (strlen(macro_text[playID-1]) + strlen(LINK->buf) <= 255) {
536 if (debugMode()) {
537 printf("Inserting macro %s text \"%s\"\n",
538 playtag, macro_text[playID-1]);
539 printf("Buffer before insert: %s\n", LINK->buf);
540 }
541 sprintf(LINK->buf, "%s%s",
542 macro_text[playID-1], strcpy(STR5, LINK->buf));
543 return;
544 }
545 sprintf(STR4, "Expansion of macro %s causes buffer overflow", playtag);
546 error(STR4, print);
547 }
548 if (pos1(LINK->note[1], "SR") == 0) {
549 sprintf(STR4, "Second character %c of macro word should be in \"PRS\"",
550 LINK->note[1]);
551 error(STR4, print);
552 }
553 *macro_text[playID-1] = '\0';
554 do {
555 GetNextWord(w, LINK->buf, blank, dummy);
556 if (*w == '\0') {
557 sprintf(STR1,
558 "Macro definition %s should be terminated on the same input line",
559 playtag);
560 error(STR1, print);
561 }
562 if (!strcmp(w, "M")) {
563 if (debugMode())
564 printf("Macro %s is: %s\n", playtag, macro_text[playID-1]);
565 break;
566 }
567 if (w[0] == 'M' && strlen(w) > 1) {
568 if (w[1] != 'P' || identifyMacro(w) == playID) {
569 sprintf(STR7, "%s not allowed inside macro definition %s", w, playtag);
570 error(STR7, print);
571 }
572 }
573 sprintf(macro_text[playID-1] + strlen(macro_text[playID-1]), "%s ", w);
574 } while (true);
575 if (LINK->note[1] == 'R') {
576 LINK->note[1] = 'P';
577 sprintf(LINK->buf, "%s %s", LINK->note, strcpy(STR7, LINK->buf));
578 }
579 }
580
581
582 void getNextMusWord(Char *buf_, Char *note_, music_word *nscan)
583 {
584 struct LOC_getNextMusWord V;
585 Char maybe_error[256];
586 Char STR2[256];
587
588 V.buf = buf_;
589 V.note = note_;
590 GetNextWord(V.note, V.buf, blank, dummy);
591 if (*V.note == '\0')
592 return;
593 if (V.note[0] == 'M' && expandMacro()) {
594 expandThisMacro(&V);
595 getNextMusWord(V.buf, V.note, nscan);
596 return;
597 }
598 if (V.note[0] == '\\') {
599 *maybe_error = '\0';
600 if (V.note[strlen(V.note) - 1] != '\\')
601 strcpy(maybe_error, V.note);
602 while (*V.buf != '\0' && V.note[strlen(V.note) - 1] != '\\')
603 sprintf(V.note + strlen(V.note), " %s",
604 GetNextWord(STR2, V.buf, blank, dummy));
605 if (V.note[strlen(V.note) - 1] != '\\')
606 error("Unterminated TeX literal", print);
607 *nscan = texword;
608 if (*maybe_error != '\0') {
609 sprintf(STR2, "Possible unterminated TeX literal: %s", maybe_error);
610 warning(STR2, print);
611 }
612 return;
613 }
614 if (solfaNoteNames()) {
615 translateSolfa(V.note);
616 if (V.note[0] == '"')
617 predelete(V.note, 1);
618 }
619 switch (V.note[0]) {
620
621 case '_':
622 *nscan = pmxprefix;
623 delete1(V.note, 1);
624 break;
625
626 case 'a':
627 case 'b':
628 case 'c':
629 case 'd':
630 case 'e':
631 case 'f':
632 case 'g':
633 *nscan = abcdefg;
634 break;
635
636 case 'z':
637 *nscan = zword;
638 break;
639
640 case '(':
641 *nscan = lparen;
642 break;
643
644 case '{':
645 if (V.note[strlen(V.note) - 1] == '}')
646 *nscan = lyrtag;
647 else
648 *nscan = lparen;
649 break;
650
651 case ')':
652 if (pos1('(', V.note) == 0)
653 *nscan = rparen;
654 else
655 *nscan = rlparen;
656 break;
657
658 case '}':
659 if (pos1('{', V.note) == 0)
660 *nscan = rparen;
661 else
662 *nscan = rlparen;
663 break;
664
665 case '[':
666 *nscan = lbrac;
667 break;
668
669 case ']':
670 if (!strcmp(V.note, "]["))
671 *nscan = other;
672 else
673 *nscan = rbrac;
674 break;
675
676 case '@':
677 *nscan = atword;
678 break;
679
680 case 'm':
681 *nscan = mword;
682 break;
683
684 case 'r':
685 *nscan = rword;
686 break;
687
688 case '#':
689 case '-':
690 case 'n':
691 case 'x':
692 case '?':
693 case 's':
694 case 't':
695 case 'D':
696 *nscan = pmxl;
697 break;
698
699 case 'M':
700 if (!strcmp(V.note, "M"))
701 *nscan = endmacro;
702 else
703 *nscan = macro;
704 break;
705
706 case 'G':
707 if (pos1('A', V.note) > 0)
708 *nscan = pmxl;
709 else
710 *nscan = other;
711 break;
712
713 case '1':
714 case '2':
715 case '3':
716 case '4':
717 case '5':
718 case '6':
719 case '7':
720 case '8':
721 case '9':
722 if (pos1('/', V.note) > 0)
723 *nscan = mword;
724 else
725 *nscan = pmxl;
726 break;
727
728 case 'o':
729 *nscan = oword;
730 break;
731
732 case 'A':
733 case 'V':
734 *nscan = FirstOnly;
735 break;
736
737 case '/':
738 if (!strcmp(V.note, "//"))
739 *nscan = nextvoice;
740 else if (!strcmp(V.note, "/")) {
741 strcpy(V.note, "//");
742 warning("/ changed to //", print);
743 *nscan = nextvoice;
744 } else {
745 error("Word starts with /: do you mean \\?", print);
746 *nscan = err;
747 }
748 break;
749
750 default:
751 if (pos1('|', V.note) > 0)
752 *nscan = barword;
753 else
754 *nscan = other;
755 break;
756 }
757 }
758
759
760 boolean upper(voice_index voice)
761 {
762 line_info *WITH;
763
764 WITH = &info[voice-1];
765 if (WITH->voice_pos == 1 && voice < nvoices)
766 return (WITH->voice_stave == info[voice].voice_stave);
767 else
768 return false;
769 }
770
771
772 void describeVoice(voice_index voice, Char *describe_lyr)
773 {
774 short bar, w;
775 line_info *WITH;
776 Char STR1[256];
777 line_info *WITH1;
778 short FORLIM;
779
780 WITH = &info[voice-1];
781 printf("Voice `%s' is on line %d", voice_label[voice-1], WITH->mus);
782 if (WITH->vocal) {
783 printf(", is vocal");
784 puts(describe_lyr);
785 } else {
786 if (WITH->chord > 0)
787 printf(" and has chords on line %d", WITH->chord);
788 putchar('\n');
789 }
790 if (!debugMode())
791 return;
792 WITH1 = &info[voice-1];
793 printf("Line has %d bars", WITH1->nbar);
794 if (WITH1->extra > 0)
795 printf(" + %d/64 notes\n", WITH1->extra);
796 else
797 putchar('\n');
798 printf("Bars:");
799 FORLIM = WITH1->nbar;
800 for (bar = 1; bar <= FORLIM; bar++)
801 printf(" %s ", getBar(STR1, voice, bar));
802 printf("\nWord types:");
803 FORLIM = WITH1->nword;
804 for (w = 0; w <= FORLIM - 1; w++)
805 printf(" %s", name[(long)WITH1->scan[w]]);
806 putchar('\n');
807 }
808
809
810 boolean aloneOnStave(stave_index stave)
811 {
812 voice_index v;
813
814 if (number_on_stave[stave-1] != 2)
815 return true;
816 else {
817 v = first_on_stave[stave-1];
818 return (info[v-1].mus == 0 || info[v].mus == 0);
819 }
820 }
821
822
823 void selectVoices(Char *line_)
824 {
825 Char line[256];
826 short i = 0;
827 short k;
828 Char word[256];
829 short FORLIM;
830
831 strcpy(line, line_);
832 FORLIM = nvoices;
833 for (k = 0; k <= FORLIM - 1; k++)
834 selected[k] = false;
835 printf("Voice change to: %s = ", line);
836 while (i < nvoices) {
837 GetNextWord(word, line, blank, dummy);
838 if (*word == '\0') {
839 putchar('\n');
840 return;
841 }
842 i++;
843 printf("%s ", word);
844 k = findVoice(word);
845 if (k == 0)
846 error("This voice is not in the style", print);
847 selected[k-1] = true;
848 }
849 putchar('\n');
850 }
851
852
853
854
855 /* End. */