Debian Codegraph @

utils/autosp/autosp-src/process_command.c

raw
1
2 /* Copyright (C) 2014-24 R. D. Tennent School of Computing,
3 * Queen's University, [email protected]
4 *
5 * This program is free software; you can redistribute it
6 * and/or modify it under the terms of the GNU General
7 * Public License as published by the Free Software
8 * Foundation; either version 2 of the License, or (at your
9 * option) any later version.
10 *
11 * This program is distributed in the hope that it will
12 * be useful, but WITHOUT ANY WARRANTY; without even the
13 * implied warranty of MERCHANTABILITY or FITNESS FOR A
14 * PARTICULAR PURPOSE. See the GNU General Public License
15 * for more details.
16 *
17 * You should have received a copy of the GNU General
18 * Public License along with this program; if not, write to
19 * the Free Software Foundation, Inc., 51 Franklin Street,
20 * Fifth Floor, Boston, MA 02110-1301, USA.
21 */
22
23 # include "process_score.h"
24 # include <math.h> /* need fabs() */
25
26 # define APPOGG_SPACING SP(64)+SP(256) /* not a legitimate spacing */
27 # define notespp "\\vnotes2.95\\elemskip"
28 # define Notespp "\\vnotes3.95\\elemskip"
29 # define NOtespp "\\vnotes4.95\\elemskip"
30 # define NOTespp "\\vnotes6.95\\elemskip"
31 # define NOTEsp "\\vnotes9.52\\elemskip"
32 # define APPOGG_NOTES "\\vnotes1.45\\elemskip"
33
34 char terminator[MAX_STAFFS]; /* one of '&' "|', '$' */
35
36 char *notes[MAX_STAFFS]; /* note segment for ith staff */
37 char *current[MAX_STAFFS];
38
39 int spacings[MAX_STAFFS]; /* spacing for ith staff */
40
41 int vspacing[MAX_STAFFS]; /* virtual-note (skip) spacing */
42 bool vspacing_active[MAX_STAFFS]; /* virtual-note spacing active? */
43 /* used to preclude unnecessary pre-accidental skips */
44
45 bool nonvirtual_notes; /* used to preclude output of *only* virtual notes */
46
47 int cspacing[MAX_STAFFS]; /* nominal collective-note spacing */
48 char collective[MAX_STAFFS][SHORT_LEN];
49 /* prefixes for collective note sequences */
50 bool first_collective[MAX_STAFFS];
51
52 char deferred_bar[SHORT_LEN]; /* deferred \bar (or \endpiece etc.) */
53
54 int beaming[MAX_STAFFS]; /* spacing for beamed notes */
55 int new_beaming;
56 int semiauto_beam_notes[MAX_STAFFS]; /* semi-automatic beam notes */
57
58 int spacing_staff; /* staff that determines current spacing */
59
60 /* save-restore state for a staff; used in process_xtuplet */
61 int beamingi;
62 char *currenti;
63 int cspacingi;
64 int vspacingi;
65 char collectivei[SHORT_LEN];
66 bool first_collectivei;
67
68 int xtuplet[MAX_STAFFS]; /* x for xtuplet in staff i */
69
70 bool appoggiatura;
71
72 char outstrings[MAX_STAFFS][LINE_LEN];
73 /* accumulate commands to be output */
74 char *n_outstrings[MAX_STAFFS];
75
76 int global_skip;
77 /* = 1, 2, 3, or 4 for (non-standard) commands \QQsk \HQsk \TQsk \Qsk */
78 /* = 5 for five commas and double-flat accidental spacing */
79 /* = 6 for six commas */
80
81
82 char s[SHORT_LEN]; /* string for ps() */
83 char *s_n;
84 char *ps(int spacing);
85 void note_segment (char *s);
86 void status (int i);
87 void status_spacing (void);
88 void status_all (void);
89 void status_collective (int i);
90 void status_beam (int i);
91
92
93 int spacing_note (int i);
94 void output_notes (int i);
95 void initialize_notes ();
96 void process_xtuplet (void);
97
98 PRIVATE
99 void analyze_notes (char **ln)
100 /* divide material from *ln to \en into notes[i] segments
101 and initialize terminator[i] etc. */
102 {
103 int i; char *s; char *t;
104 int instr=1;
105 int newlines = 0;
106 s = *ln+1; /* skip "/" */
107 while (isalpha(*s)) {s++;} /* skip rest of the initial command */
108 while (true)
109 { /* look for \en */
110 t = strstr(s, "\\en");
111 if (t != NULL) break;
112 /* replace EOL by a blank and append another line of input */
113 { char new_line[LINE_LEN];
114 char *nl;
115 if (fgets (new_line, LINE_LEN, infile) == NULL)
116 error ("Unexpected EOF.");
117 if (strlen (new_line) == LINE_LEN-1)
118 error ("Line too long.");
119 nl = new_line;
120 while (*nl == ' ') nl++; /* avoid spaces */
121 t = strpbrk (s, "\n%");
122 if (t == NULL) error ("Missing EOL.");
123 if (*t == '\n')
124 { *t = ' '; t++;}
125 *t = '\0';
126 if (append (line, &t, nl, LINE_LEN) >= LINE_LEN)
127 error ("Line too long.");
128 newlines++;
129 }
130 }
131 /* t -> "\en" */
132 if ( prefix("\\enotes", t) ) *ln = t+7;
133 else *ln = t+3;
134 *t = '$'; /* replace '\' by '$' (a distinctive terminating character) */
135 for (i=1; i <= nstaffs; i++)
136 { char *tt;
137 while (*s == ' ') s++; /* skip spaces */
138 if (active[i])
139 { notes[i] = s;
140 current[i] = s;
141 }
142 tt = strpbrk (s, "|&$");
143 if (tt == NULL) error ("can't parse note-spacing command.");
144 s = tt;
145 terminator[i] = *s;
146 staff_instr[i] = instr;
147 if (terminator[i] == '&') instr++;
148 if (*s != '$') s++;
149 }
150 lineno = lineno + newlines;
151
152 /* initialize: */
153 spacing = MAX_SPACING;
154 old_spacing = MAX_SPACING;
155 appoggiatura = false;
156 global_skip = 0;
157 for (i=1; i <= nstaffs; i++)
158 {
159 if (active[i])
160 { n_outstrings[i] = outstrings[i];
161 *n_outstrings[i] = '\0';
162 vspacing[i] = 0;
163 vspacing_active[i] = false;
164 collective[i][0] = '\0';
165 cspacing[i] = MAX_SPACING;
166 first_collective[i] = false;
167 xtuplet[i] = 1;
168 }
169 }
170 if (debug)
171 { fprintf (logfile, "\nAfter analyze_notes:\n");
172 status_all ();
173 }
174 }
175
176 PRIVATE
177 void checkc (char *s, char c)
178 { if (*s != c)
179 {
180 printf ("Error on line %d: Expected %c but found %d:\n%s\n", lineno, c, *s, line);
181 exit (EXIT_FAILURE);
182 }
183 }
184
185 PRIVATE
186 void checkn (char *s)
187 { if (strpbrk (s, "0123456789") != s)
188 {
189 printf ("Error on line %d: Expected digit but found %c:\n%s\n", lineno, *s, line);
190 exit (EXIT_FAILURE);
191 }
192 }
193
194 PRIVATE
195 void filter_output (int i)
196 { /* discard \sk \bsk \Qsk \TQsk \HQsk \QQsk \Cp and \Cpause */
197 char *s = notes[i];
198 while (s < current[i])
199 { char *t;
200 t = strpbrk (s+1, "\\&|$");
201 if (t == NULL || t > current[i]) t = current[i];
202 if (!prefix ("\\sk", s)
203 && !prefix ("\\bsk", s)
204 && !prefix ("\\Cpause", s)
205 && !prefix ("\\Cp", s)
206 && !prefix ("\\Qsk", s)
207 && !prefix ("\\HQsk", s)
208 && !prefix ("\\TQsk", s)
209 && !prefix ("\\QQsk", s)
210 && !prefix ("\\Triolet", s)
211 && !prefix ("\\Xtuplet", s) )
212 {
213 while (s < t)
214 { while (*s == ',') s++; /* global skips */
215 *n_outstrings[i] = *s;
216 n_outstrings[i]++; s++;
217 }
218 *(n_outstrings[i]) = '\0';
219 }
220 s = t;
221 }
222 notes[i] = s;
223 }
224
225 void output_notes (int i)
226 /* append from notes[i] up to current[i] to outstrings[i] */
227 { if (debug)
228 { fprintf (logfile, "\nEntering output_notes:\n");
229 status (i);
230 }
231 if (debug)
232 {
233 fprintf (logfile, "\nAppending %s for global_skip=%i\n", global_skip_str[global_skip], global_skip);
234 }
235 append (outstrings[i], &(n_outstrings[i]), global_skip_str[global_skip], LINE_LEN);
236 if (vspacing[i] > 0)
237 {
238 if (nonvirtual_notes)
239 append (outstrings[i], &(n_outstrings[i]), "\\sk", LINE_LEN);
240 vspacing[i] = vspacing[i] - spacing;
241 }
242 else if (collective[i][0])
243 {
244 if (debug)
245 { fprintf (logfile, "\nBefore outputting collective note:\n");
246 status (i);
247 status_collective (i);
248 }
249 if (!first_collective[i])
250 append (outstrings[i], &(n_outstrings[i]), collective[i], LINE_LEN);
251 first_collective[i] = false;
252 filter_output (i);
253 append (outstrings[i], &(n_outstrings[i]), "}", LINE_LEN);
254 if (*notes[i] == '}')
255 { collective[i][0] = '\0';
256 cspacing[i] = 0;
257 notes[i]++;
258 }
259 if (*notes[i] == '}') /* close of {\tinynotesize..{}}? */
260 { append (outstrings[i], &(n_outstrings[i]), "}", LINE_LEN);
261 notes[i]++;
262 }
263 if (debug)
264 { fprintf (logfile, "\nAfter outputting collective note:\n");
265 status (i);
266 status_collective (i);
267 }
268 }
269 else
270 filter_output (i);
271 if (debug)
272 { fprintf (logfile, "\nAfter output_notes:\n");
273 status (i);
274 status_spacing ();
275 }
276 }
277
278
279 int spacing_note (int i);
280 /* Search from current[i] for the next spacing note in the ith staff.
281 * Returns the spacing value and updates current[i].
282 */
283
284 PRIVATE void
285 output_rests (void)
286 { /* outputs a multi-bar rest and the deferred_bar command */
287 int i;
288 fprintf ( outfile, "\\NOTes\\sk\\en%%\n" );
289 fprintf (outfile, "\\def\\atnextbar{\\znotes");
290 if (restbars == 1)
291 fprintf ( outfile, "\\centerpause" );
292 else
293 fprintf ( outfile, "\\centerHpause{%d}", restbars );
294 fprintf (outfile, "\\en}%%\n");
295 if (Changeclefs) /* \Changeclefs has to be output after \def\atnextbar... */
296 {
297 fprintf ( outfile, "\\Changeclefs%%\n");
298 Changeclefs = false;
299 }
300 if (restbars > 1)
301 fprintf ( outfile, "\\advance\\barno%d%%\n", restbars-1 );
302 restbars = 0;
303 for (i=1; i <= nstaffs; i++) bar_rest[i] = false;
304 fprintf ( outfile, "%s%%\n", deferred_bar);
305 deferred_bar[0] = '\0';
306 }
307
308 void initialize_notes ()
309 { int i;
310 if (debug)
311 { fprintf (logfile, "\nEntering initialize_notes\n");
312 status_all ();
313 }
314 if ( nastaffs == 1 && spacing != MAX_SPACING && restbars > 0)
315 output_rests ();
316
317 oldspacing_staff = spacing_staff;
318
319 if (spacing == MAX_SPACING)
320 fprintf (outfile, "\\znotes");
321 else if (spacing == SP(1)+SP(2) || spacing == SP(1)+SP(2)+SP(4))
322 fprintf (outfile, NOTEsp);
323 else if (spacing == SP(1))
324 fprintf (outfile, "\\NOTEs");
325 else if (spacing == SP(2)+SP(4)+SP(8))
326 fprintf (outfile, "%s", NOTespp);
327 else if (spacing == SP(2)+SP(4) )
328 fprintf (outfile, "\\NOTesp");
329 else if (spacing == SP(2))
330 fprintf (outfile, "\\NOTes");
331 else if (spacing == SP(4)+SP(8) )
332 fprintf (outfile, "\\NOtesp");
333 else if (spacing == SP(4)+SP(8)+SP(16) )
334 fprintf (outfile, "%s", NOtespp);
335 else if (spacing == SP(4))
336 fprintf (outfile, "\\NOtes");
337 else if (spacing == SP(8)+SP(16) )
338 fprintf (outfile, "\\Notesp");
339 else if (spacing == SP(8)+SP(16)+SP(32))
340 fprintf (outfile, "%s", Notespp);
341 else if (spacing == SP(8))
342 fprintf (outfile, "\\Notes");
343 else if (spacing == SP(16)+SP(32)+SP(64))
344 fprintf (outfile, notespp);
345 else if (spacing == SP(16)+SP(32) )
346 fprintf (outfile, "\\notesp");
347 else if (spacing == SP(16) || spacing == SP(32)+SP(64) || spacing == SP(32)+SP(64)+SP(128))
348 fprintf (outfile, "\\notes");
349 else if (spacing == SP(32) || spacing == SP(64)+SP(128) || spacing == SP(64)+SP(128)+SP(256))
350 fprintf (outfile, "\\nnotes");
351 else if (spacing == SP(64) )
352 fprintf (outfile, "\\nnnotes");
353 else if (spacing == APPOGG_SPACING)
354 fprintf (outfile, "%s", APPOGG_NOTES);
355 else
356 { printf ("Error on line %d: spacing %s not recognized.\n", lineno, ps(spacing));
357 exit (EXIT_FAILURE);
358 }
359 if (debug)
360 { fprintf (logfile, "\noutputting \\Notes command for spacing=%s.\n", ps(spacing));
361 status_all();
362 }
363 for (i=1; i <= nstaffs; i++)
364 { if (active[i])
365 { n_outstrings[i] = outstrings[i];
366 *n_outstrings[i] = '\0';
367 }
368 }
369 }
370
371 PRIVATE
372 void terminate_notes ()
373 { int i;
374 char *s, *t;
375 if (debug)
376 { fprintf (logfile, "\nEntering terminate_notes:\n");
377 status_all ();
378 }
379 t = TransformNotes2;
380 while (true)
381 { s = strchr (t, '#');
382 if (s == NULL)
383 break;
384 while (t < s) /* output any initial \transpose etc. */
385 { putc (*t, outfile); t++; }
386 t++; /* skip # */
387 i = atoi (t) -1; t++;
388 if (spacing == MAX_SPACING)
389 { /* output any commands left in notes[i] */
390 filter_output (i);
391 }
392 fprintf (outfile, "%s", outstrings[i]);
393 if (debug) fprintf (logfile, "\noutputting %s from outstrings[%d].\n", outstrings[i], i);
394 outstrings[i][0] = '\0';
395 n_outstrings[i] = outstrings[i];
396 if (spacing < MAX_SPACING && spacing > 2 * old_spacing)
397 { /* add extra space *before* much longer notes */
398 fprintf (outfile, "\\hqsk");
399 if (debug) fprintf (logfile, "\nExtra half-notehead space before longer notes.\n");
400 }
401 if (*t != '\0')
402 { putc (*t, outfile); t++; } /* terminator */
403 }
404 if (spacing == MAX_SPACING && old_spacing < SP(8) )
405 { /* add extra space before \en */
406 fprintf (outfile, "\\hqsk");
407 if (debug) fprintf (logfile, "\nExtra half-notehead space before \\en.\n");
408 }
409 fprintf (outfile, "\\en");
410 if (debug)
411 { fprintf (logfile, "\noutputting \\en\n");
412 fprintf (logfile, "\nAfter terminate_notes:\n");
413 status_all ();
414 }
415 }
416
417 PRIVATE
418 void process_appogg (void)
419 { int i;
420 if (debug)
421 { fprintf (logfile, "\nEntering process_appogg:\n");
422 status_all();
423 }
424
425 if (old_spacing < MAX_SPACING)
426 { char *s, *t;
427 if (debug) fprintf (logfile, "Terminate current notes command:\n");
428 t = TransformNotes2;
429 while (true)
430 { s = strchr (t, '#');
431 if (s == NULL) break;
432 while (t < s) /* output any initial \transpose etc. */
433 { putc (*t, outfile); t++; }
434 t++; /* skip # */
435 i = atoi (t) - 1; t++;
436 fprintf (outfile, "%s", outstrings[i]);
437 if (debug) fprintf (logfile, "\noutputting %s from outstrings[%d].\n", outstrings[i], i);
438 outstrings[i][0] = '\0';
439 n_outstrings[i] = outstrings[i];
440 if (*t != '\0')
441 { putc (*t, outfile); t++; } /* terminator */
442 }
443 fprintf (outfile, "\\en");
444 if (debug)
445 { fprintf (logfile, "\\en");
446 fprintf (logfile, "\nAfter termination of notes command:\n");
447 status_all ();
448 }
449 }
450
451 if (debug) fprintf (logfile, "\nCreate a new notes command for the appoggiatura:\n");
452 spacing = APPOGG_SPACING;
453 initialize_notes ();
454 spacing = MAX_SPACING;
455 for (i=1; i <= nstaffs; i++)
456 if (active[i])
457 {
458 if (spacings[i] == APPOGG_SPACING )
459 {
460 output_notes (i);
461 spacings[i] = spacing_note (i);
462 }
463 if (spacings[i] < spacing)
464 spacing = spacings[i];
465 }
466 appoggiatura = false;
467 nonvirtual_notes = true;
468 old_spacing = APPOGG_SPACING;
469 if (debug)
470 { fprintf (logfile, "\nAfter process_appogg:\n");
471 status_all ();
472 }
473 return;
474 }
475
476
477 PRIVATE
478 void generate_notes ()
479 { int i;
480 bool xtuplet_flag;
481 while (true)
482 { old_spacing = spacing;
483 spacing = MAX_SPACING;
484 spacing_staff = 0;
485 global_skip = 0;
486 nonvirtual_notes = false;
487 if (debug)
488 { fprintf (logfile, "\nIn generate_notes:\n");
489 status_all ();
490 }
491 for (i=1; i <= nstaffs; i++)
492 if (active[i])
493 {
494 spacings[i] = spacing_note (i);
495 if (spacings[i] < spacing)
496 {
497 spacing = spacings[i];
498 spacing_staff = i;
499 }
500 else if (spacings[i] == spacing &&
501 (vspacing[spacing_staff] > 0 ||
502 /* for staffs with equal spacing, use the one with larger instrument size */
503 instr_numsize[staff_instr[i]] > instr_numsize[staff_instr[spacing_staff]]))
504 spacing_staff = i;
505 }
506 if (appoggiatura)
507 {
508 process_appogg ();
509 }
510 xtuplet_flag = false;
511 for (i=1; i <= nstaffs; i++)
512 if (xtuplet[i] > 1) xtuplet_flag = true;
513 if (xtuplet_flag)
514 {
515 process_xtuplet ();
516 continue;
517 }
518
519 if (spacing != old_spacing || spacing == MAX_SPACING || spacing_staff != oldspacing_staff)
520 {
521 if (old_spacing < MAX_SPACING)
522 terminate_notes ();
523 if (spacing == MAX_SPACING || nonvirtual_notes == false)
524 {
525 if (debug)
526 { fprintf (logfile, "\nAfter generate_notes:\n");
527 status_all ();
528 }
529 return;
530 }
531 if (old_spacing < MAX_SPACING) putc ('\n', outfile);
532
533 initialize_notes ();
534 }
535
536 for (i=1; i <= nstaffs; i++) /* append current notes to outstrings */
537 if (active[i])
538 output_notes (i);
539 for (i=1; i <= nstaffs; i++)
540 {
541 /* virtual notes needed? */
542 if (active[i] && spacings[i] != MAX_SPACING && spacings[i] != spacing && vspacing[i] == 0 )
543 {
544 vspacing[i] = spacings[i];
545 vspacing_active[i] = true;
546 vspacing[i] = vspacing[i] - spacing;
547 if (debug)
548 { fprintf (logfile, "\nAfter vspacing initialization:\n");
549 status (i);
550 }
551 }
552 }
553 }
554 }
555
556
557 void process_command (char **ln)
558 { char *s, *t;
559
560 if (debug)
561 { fprintf (logfile, "\nProcessing command:%s\n", *ln);
562 fflush (logfile);
563 }
564 if ( prefix("\\instrumentnumber", *ln) )
565 {
566 s = strpbrk (*ln, "123456789");
567 if ( s == NULL ) error ("\\instrumentnumber command unreadable.");
568 ninstr = atoi (s);
569 while (*ln <= s) { putc (**ln, outfile); (*ln)++;}
570 }
571
572 if ( prefix("\\def\\nbinstrument", *ln) )
573 {
574 s = strpbrk (*ln, "123456789");
575 if ( s == NULL ) error ("\\def\\nbinstrument command unreadable.");
576 ninstr = atoi (s);
577 while (*ln <= s) { putc (**ln, outfile); (*ln)++;}
578 }
579
580 else if ( prefix("\\setstaffs", *ln) )
581 { /* for now, just record in staffs[i] */
582 int n, p;
583 s = strpbrk (*ln, "123456789");
584 if ( s == NULL ) error ("\\setstaffs command unreadable.");
585 n = (int)(*s) - (int)('0'); /* instrument number */
586 s = strpbrk (s+1, "123456789");
587 if ( s == NULL ) error ("\\setstaffs command unreadable.");
588 p = (int)(*s) - (int)('0'); /* number of staffs */
589 staffs[n] = p;
590 while (*ln <= s) { putc (**ln, outfile); (*ln)++;}
591 }
592
593 else if ( prefix("\\setsize", *ln) )
594 { int n; char *p;
595 s = strpbrk (*ln, "123456789");
596 if ( s == NULL ) error ("\\setsize command unreadable.");
597 n = (int)(*s) - (int)('0'); /* instrument number */
598 s++;
599 if (*s == '}') s++;
600 if (*s == '{') s++;
601 t = strpbrk (s, " }\n");
602 if ( t == NULL ) error ("\\setsize command unreadable.");
603 instrument_size[n][0] = '\0';
604 p = instrument_size[n];
605 while (s < t)
606 { *p = *s;
607 p++; s++;
608 }
609 *p = '\0';
610 if (debug)
611 {
612 fprintf (logfile, "instrument_size[%d] = %s\n", n, instrument_size[n]);
613 fflush (logfile);
614 }
615 /* determine numerical instrument size to allow numerical comparison */
616 if (prefix ("\\normalvalue", instrument_size[n]))
617 instr_numsize[n] = 1.0;
618 else if (prefix ("\\smallvalue", instrument_size[n]))
619 instr_numsize[n] = 0.8;
620 else if (prefix ("\\tinyvalue", instrument_size[n]))
621 instr_numsize[n] = 0.64;
622 else if (prefix ("\\largevalue", instrument_size[n]))
623 instr_numsize[n] = 1.2;
624 else if (prefix ("\\Largevalue", instrument_size[n]))
625 instr_numsize[n] = 1.44;
626 else
627 /* number? */
628 if (debug)
629 {
630 fprintf (logfile, "instr_numsize[%d] = %f\n", n, instr_numsize[n]);
631 fflush (logfile);
632 }
633 while (*ln <= s) { putc (**ln, outfile); (*ln)++;}
634 }
635
636 else if ( prefix("\\startpiece", *ln) )
637 {
638 if (!TransformNotesDefined) /* create default TransformNotes2: */
639 { int i, j;
640 t = TransformNotes2;
641 nstaffs = 1;
642 sprintf (t, "#%1i", nstaffs+1); t = t+2;
643 for (j=2; j <= staffs[1]; j++)
644 { nstaffs++; sprintf (t, "|#%1i", nstaffs+1); t = t+3;
645 active[nstaffs] = true;
646 }
647 for (i=2; i <= ninstr; i++)
648 { nstaffs++; sprintf (t, "&#%1i", nstaffs+1); t = t+3;
649 for (j=2; j <= staffs[i]; j++)
650 { nstaffs++; sprintf (t, "|#%1i", nstaffs+1); t = t+3;
651 active[nstaffs] = true;
652 }
653 }
654 nastaffs = nstaffs;
655 if (debug)
656 fprintf (logfile, "default TransformNotes2=%s\n", TransformNotes2);
657 }
658 if (nstaffs == 1) fprintf (outfile, "\\nostartrule\n");
659 if (debug)
660 {
661 int j;
662 fprintf (logfile, "ninstr=%d nstaffs=%d nastaffs=%d\n", ninstr, nstaffs, nastaffs);
663 for (j=1; j <= nstaffs; j++)
664 fprintf (logfile, "active[%d]=%d\n", j, active[j]);
665 }
666 fprintf (outfile, "\\startpiece");
667 t = strpbrk (*ln+1, "\\%\n");
668 *ln = t;
669 }
670
671 else if ( prefix("\\startextract", *ln) )
672 {
673 if (!TransformNotesDefined) /* create default TransformNotes2: */
674 { int i, j;
675 t = TransformNotes2;
676 nstaffs = 1;
677 sprintf (t, "#%1i", nstaffs+1); t = t+2;
678 for (j=2; j <= staffs[1]; j++)
679 { nstaffs++; sprintf (t, "|#%1i", nstaffs+1); t = t+3;
680 active[nstaffs] = true;
681 }
682 for (i=2; i <= ninstr; i++)
683 { nstaffs++; sprintf (t, "&#%1i", nstaffs+1); t = t+3;
684 for (j=2; j <= staffs[i]; j++)
685 { nstaffs++; sprintf (t, "|#%1i", nstaffs+1); t = t+3;
686 active[nstaffs] = true;
687 }
688 }
689 nastaffs = nstaffs;
690 if (debug)
691 fprintf (logfile, "default TransformNotes2=%s\n", TransformNotes2);
692 }
693 if (nstaffs == 1) fprintf (outfile, "\\nostartrule\n");
694 fprintf (outfile, "\\startextract");
695 t = strpbrk (*ln+1, "\\%\n");
696 *ln = t;
697 }
698
699 else if ( prefix("\\TransformNotes", *ln) )
700 { /* determine ninstr, nstaffs, nastaffs, staffs[i], and active[i] */
701 int i;
702 s = *ln + 16; /* first parameter */
703 ninstr = 0;
704 nstaffs = 0;
705 while (true)
706 { ninstr++; nstaffs++;
707 staffs[ninstr] = 1;
708 checkc (s, '#'); s++;
709 checkn (s); s++;
710 while (*s == '|')
711 { staffs[ninstr]++; nstaffs++;
712 s++;
713 checkc (s, '#'); s++;
714 checkn (s); s++;
715 }
716 if (*s != '&') break;
717 s++;
718 }
719 checkc (s, '}'); s++;
720 s = strchr (s, '{');
721 if (s == NULL) error ("Can't parse \\TransformNotes");
722 s++;
723 /* determine TransformNotes2: */
724 t = TransformNotes2;
725 do { *t = *s; t++; s++; }
726 while (*s != '}');
727 *t = '\0'; /* terminate TransformNotes2 */
728 TransformNotesDefined = true;
729 if (debug)
730 fprintf (logfile, "defined TransformNotes2=%s\n", TransformNotes2);
731
732 /* determine active staffs: */
733 for (i=1; i <= nstaffs; i++) active[i] = false;
734 nastaffs = 0;
735 t = strpbrk (TransformNotes2, "#}"); /* may have \transpose etc. before # */
736 if (t == NULL) error ("Can't parse second argument of \\TransformNotes");
737 while (*t == '#')
738 { if (sscanf (t, "#%d", &i) != 1)
739 error ("sscanf for argument number fails");
740 active[i-1] = true; /* parameters start at 2 */
741 nastaffs++;
742 t = strpbrk (t+1, "#}");
743 if (t == NULL) break;
744 }
745 if (debug)
746 {
747 int j;
748 fprintf (logfile, "ninstr=%d nstaffs=%d nastaffs=%d\n", ninstr, nstaffs, nastaffs);
749 for (j=1; j <= nstaffs; j++)
750 fprintf (logfile, "active[%d]=%d\n", j, active[j]);
751 }
752
753 /* output \TransformNotes... as a comment: */
754 putc ('%', outfile);
755 while (*ln <= s)
756 { putc (**ln, outfile); (*ln)++; }
757 }
758
759 else if (prefix("\\def\\atnextbar{\\znotes", *ln))
760 { /* whole-bar or multi-bar rest? */
761 int i;
762 bool centerpause = false;
763 *ln = *ln + 15; /* skip "\def\atnextbar{" */
764 analyze_notes(ln);
765 for (i=1; i <= nstaffs; i++)
766 {
767 if (active[i])
768 { char *t;
769 char *s;
770 t = strpbrk (notes[i], "&|$");
771 if (t == NULL) t = notes[i] + strlen (notes[i]);
772 s = strstr (notes[i], "\\centerpause");
773 if (s != NULL && s < t && nastaffs == 1)
774 {
775 bar_rest[i] = true;
776 centerpause = true;
777 }
778 break;
779 }
780 }
781 if (!centerpause) /* generate transformed \znotes ... \en */
782 {
783 fprintf ( outfile, "\\def\\atnextbar{\\znotes");
784 t = TransformNotes2;
785 while (true)
786 {
787 /* output TransformNotes2 prefix (\transpose etc.): */
788 s = strchr (t, '#');
789 if (s == NULL) break;
790 while (t < s)
791 { putc (*t, outfile); t++; }
792 t++; /* skip '#' */
793 /* output notes: */
794 i = atoi (t) - 1; t++;
795 s = notes[i];
796 while (*s != '&' && *s != '|' && *s != '$')
797 { putc (*s, outfile); s++; }
798 if (*t != '\0')
799 { putc (*t, outfile); t++; } /* output terminator */
800 }
801 fprintf (outfile, "\\en}%%\n");
802 }
803 t = strchr (*ln, '}');
804 if (t == NULL) error ("Can't find }.");
805 *ln = t+1;
806 t = strpbrk (*ln, "%\\");
807 if (t == NULL ) t = *ln + strlen(*ln);
808 *ln = t;
809 }
810
811 else if ( prefix("\\def", *ln) ) /* copy to output */
812 { t = *ln + strlen(*ln);
813 while (*ln < t)
814 { fputc (**ln, outfile);
815 (*ln)++;
816 }
817 }
818
819 else if ( prefix("\\an", *ln) )
820 { if (debug)
821 { fprintf (logfile, "\nProcessing %s", *ln);
822 fprintf (logfile, "lineno=%d\n", lineno);
823 fflush (logfile);
824 }
825 analyze_notes (ln);
826 if (debug)
827 { fprintf (logfile, "\nBefore generate_notes *ln=%s\n", *ln);
828 fflush (logfile);
829 }
830 generate_notes ();
831 if (debug)
832 { fprintf (logfile, "\nAfter generate_notes *ln=%s\n", *ln);
833 fflush (logfile);
834 }
835 t = strpbrk (*ln, "%\\\n");
836 if (t == NULL) t = *ln + strlen (*ln);
837 if (*t == '\n') putc ('%', outfile);
838 *ln = t;
839 }
840 else if ( prefix("\\nnnotes", *ln) ||
841 prefix("\\nnnotes", *ln) ||
842 prefix("\\nnotes", *ln) ||
843 prefix("\\notes", *ln) ||
844 prefix("\\Notes", *ln) ||
845 prefix("\\NOtes", *ln) ||
846 prefix("\\NOTes", *ln) ||
847 prefix("\\NOTEs", *ln) ||
848 prefix("\\znotes", *ln) )
849 {
850 char *s, *t;
851 int nstaff;
852 if (debug)
853 { fprintf (logfile, "\nProcessing %s", *ln);
854 fprintf (logfile, "lineno=%d\n", lineno);
855 fflush (logfile);
856 }
857 if (nastaffs == 1 && restbars > 0)
858 output_rests ();
859 t = *ln+1;
860 while ( isalpha (*t) ) t++;
861 s = *ln;
862 while (s < t) { putc (*s, outfile); s++; }
863
864 analyze_notes (ln);
865
866 t = TransformNotes2;
867 while (true)
868 {
869 /* output TransformNotes2 prefix (\transpose etc.): */
870 s = strchr (t, '#');
871 if (s == NULL) break;
872 while (t < s)
873 { putc (*t, outfile); t++; }
874
875 t++; /* skip '#' */
876
877 /* output notes: */
878 nstaff = atoi (t) - 1; t++;
879 s = notes[nstaff];
880 while (*s != '&' && *s != '|' && *s != '$')
881 { putc (*s, outfile); s++; }
882 if (*t != '\0')
883 { putc (*t, outfile); t++; } /* output terminator */
884 }
885 fprintf (outfile, "\\en\n");
886
887 t = strpbrk (*ln, "%\\\n");
888 if (t == NULL) t = *ln + strlen (*ln);
889 if (*t == '\n') putc ('%', outfile);
890 *ln = t;
891 }
892
893 else if (prefix("\\vnotes", *ln) )
894 {
895 char *s, *t;
896 int nstaff;
897 if (debug)
898 { fprintf (logfile, "\nProcessing %s", *ln);
899 fprintf (logfile, "lineno=%d\n", lineno);
900 fflush (logfile);
901 }
902 s = *ln;
903 t = strchr (*ln+1, '\\'); /* find \elemskip */
904 *ln = t;
905 t = t + 9;
906 while (s < t) { putc (*s, outfile); s++; }
907
908
909 analyze_notes (ln);
910
911 t = TransformNotes2;
912 while (true)
913 {
914 /* output TransformNotes2 prefix (\transpose etc.): */
915 s = strchr (t, '#');
916 if (s == NULL) break;
917 while (t < s)
918 { putc (*t, outfile); t++; }
919
920 t++; /* skip '#' */
921
922 /* output notes: */
923 nstaff = atoi (t) - 1; t++;
924 s = notes[nstaff];
925 while (*s != '&' && *s != '|' && *s != '$')
926 { putc (*s, outfile); s++; }
927 if (*t != '\0')
928 { putc (*t, outfile); t++; } /* output terminator */
929 }
930 fprintf (outfile, "\\en\n");
931
932 t = strpbrk (*ln, "%\\\n");
933 if (t == NULL) t = *ln + strlen (*ln);
934 if (*t == '\n') putc ('%', outfile);
935 *ln = t;
936 }
937
938 else if ( (prefix ("\\bar", *ln) && !prefix ("\\barno", *ln))
939 || prefix ("\\doublebar", *ln) )
940 { int i;
941 char *s, *t;
942 bool atnextbar = false;
943 if (debug)
944 {
945 fprintf (logfile, "Processing \\bar");
946 fflush (logfile);
947 }
948 for (i=1; i <= nstaffs; i++)
949 if (active[i] && bar_rest[i])
950 { atnextbar = true; break; }
951 if (nastaffs == 1 && atnextbar)
952 { restbars++;
953 sprintf (deferred_bar, "\\bar");
954 if (debug)
955 {
956 fprintf (logfile, "\nrestbars increased to %d\n", restbars);
957 fflush (logfile);
958 }
959 for (i=1; i <= nstaffs; i++)
960 bar_rest[i] = false;
961 *ln = *ln+4;
962 t = strpbrk (*ln, "\\\n");
963 if (t == NULL) t = *ln + strlen (*ln);
964 if (*t == '\n') putc ('%', outfile);
965 *ln = t;
966 }
967 else
968 { if (atnextbar)
969 {
970 if (debug)
971 {
972 fprintf (logfile, "Processing atnextbar");
973 fflush (logfile);
974 }
975 fprintf (outfile, "\\def\\atnextbar{\\znotes");
976 t = TransformNotes2;
977 while (true)
978 {
979 s = strchr (t, '#');
980 if (s == NULL)
981 break;
982 while (t < s) /* output any initial \transpose etc. */
983 { putc (*t, outfile); t++; }
984 t++; /* skip # */
985 i = atoi (t) -1; t++;
986 if (active[i])
987 {
988 if (bar_rest[i])
989 fprintf (outfile, "\\centerpause");
990 bar_rest[i] = false;
991 }
992 if (*t != '\0')
993 { putc (*t, outfile); t++; } /* terminator */
994 }
995 fprintf (outfile, "\\en}%%\n");
996 }
997 if (Changeclefs) /* \Changeclefs has to be output after \def\atnextbar... */
998 {
999 fprintf ( outfile, "\\Changeclefs%%\n");
1000 Changeclefs = false;
1001 }
1002 t = strpbrk (*ln+1, "%\\\n");
1003 while (*ln < t)
1004 { fputc (**ln, outfile);
1005 (*ln)++;
1006 }
1007 if (*t == '\n') putc ('%', outfile);
1008 *ln = t;
1009 }
1010 if (debug)
1011 { fprintf (logfile, "\nAfter \\bar processing, *ln=%s\n", *ln);
1012 fflush (logfile);
1013 }
1014 }
1015
1016
1017 else if ( prefix ("\\endpiece", *ln)
1018 || prefix ("\\Endpiece", *ln)
1019 || prefix ("\\endextract", *ln)
1020 || prefix ("\\stoppiece", *ln)
1021 || prefix ("\\raggedstoppiece", *ln)
1022 || prefix ("\\rightrepeat", *ln)
1023 || prefix ("\\leftrepeat", *ln)
1024 || prefix ("\\alaligne", *ln)
1025 || prefix ("\\alapage", *ln)
1026 || prefix ("\\changecontext", *ln)
1027 || prefix ("\\Changecontext", *ln)
1028 || prefix ("\\zchangecontext", *ln)
1029 || prefix ("\\zalaligne", *ln)
1030 || prefix ("\\zalapage", *ln) )
1031 { int i;
1032 char *t;
1033 bool atnextbar = false;
1034 for (i=1; i <= nstaffs; i++)
1035 if (active[i] && bar_rest[i])
1036 { atnextbar = true; break; }
1037 if (nastaffs == 1 && atnextbar)
1038 { restbars++;
1039 sprintf (deferred_bar, "%s", *ln);
1040 output_rests ();
1041 t = strpbrk (*ln+1, "%\\\n");
1042 if (*t == '\n') putc ('%', outfile);
1043 *ln = t;
1044 }
1045 else if (atnextbar)
1046 {
1047 fprintf (outfile, "\\def\\atnextbar{\\znotes");
1048 for (i=1; i <= nstaffs; i++)
1049 {
1050 if (active[i])
1051 {
1052 if (bar_rest[i])
1053 fprintf (outfile, "\\centerpause");
1054 bar_rest[i] = false;
1055 }
1056 if ( terminator[i] != '$') putc (terminator[i], outfile);
1057 }
1058 fprintf (outfile, "\\en}%%\n");
1059 if (Changeclefs) /* \Changeclefs has to be output after \def\atnextbar... */
1060 {
1061 fprintf ( outfile, "\\Changeclefs%%\n");
1062 Changeclefs = false;
1063 }
1064 t = strpbrk (*ln+1, "%\\\n");
1065 while (*ln < t)
1066 { fputc (**ln, outfile);
1067 (*ln)++;
1068 }
1069 if (*t == '\n') putc ('%', outfile);
1070 *ln = t;
1071 }
1072 else
1073 { if (nastaffs == 1 && restbars > 0)
1074 output_rests ();
1075 t = strpbrk (*ln+1, "%\\\n");
1076 while (*ln < t)
1077 { fputc (**ln, outfile);
1078 (*ln)++;
1079 }
1080 if (*t == '\n') putc ('%', outfile);
1081 *ln = t;
1082 }
1083 if (debug)
1084 { fprintf (logfile, "\nAfter \\bar processing, *ln=%s\n", *ln);
1085 fflush (logfile);
1086 }
1087 }
1088
1089 else if ( prefix ("\\znotes", *ln) )
1090 {
1091 if (nastaffs == 1 && restbars > 0)
1092 output_rests ();
1093 fputs (*ln, outfile);
1094 *ln = *ln + strlen(*ln);
1095 }
1096
1097 else if ( prefix ("\\Changeclefs", *ln) )
1098 { /* defer till after \def\atnextbar ... */
1099 Changeclefs = true;
1100 *ln = *ln + strlen(*ln);
1101 }
1102
1103 else if ( prefix ("\\documentclass", *ln) )
1104 {
1105
1106 if ( suffix( ".tex", outfilename) )
1107 { char newoutfilename[SHORT_LEN];
1108 char *newoutfilename_n = newoutfilename;
1109 append (newoutfilename, &newoutfilename_n, infilename, sizeof (newoutfilename));
1110 newoutfilename_n -= 4;
1111 *newoutfilename_n = '\0';
1112 append (newoutfilename, &newoutfilename_n, "ltx", sizeof (newoutfilename));
1113 if (rename ( outfilename, newoutfilename) > 0)
1114 { printf ("Can't rename %s as %s\n", outfilename, newoutfilename);
1115 exit (EXIT_FAILURE);
1116 }
1117 printf ("\\documentclass detected; now writing to %s.", newoutfilename);
1118 printf ("\n\n");
1119
1120 }
1121 fputs (*ln, outfile);
1122 *ln = *ln + strlen(*ln);
1123 }
1124
1125 else if ( prefix ("\\end ", *ln)
1126 || prefix ("\\end%", *ln)
1127 || prefix ("\\end\n", *ln)
1128 || prefix ("\\end{document}", *ln) )
1129 {
1130 fprintf (outfile, "%s", *ln);
1131 exit(0);
1132 }
1133
1134 else if ( prefix ("\\startmuflex", *ln) )
1135 {
1136 fputs ("\\startmuflex", outfile);
1137 *ln = *ln + 12;
1138 }
1139
1140 else /* everything else */
1141 {
1142 fputs (*ln, outfile);
1143 *ln = *ln + strlen(*ln);
1144 }
1145 }
1146