chiark / gitweb /
Import upstream version 5.3.
[mup] / mup / mup / grpsyl.c
1
2 /* Copyright (c) 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006 by Arkkra Enterprises */
3 /* All rights reserved */
4
5 /* functions for building up lists of GRPSYL structs,
6  * error checking them, etc. These functions are called at parse
7  * time from gram.y */
8
9
10 #include <string.h>
11 #include "defines.h"
12 #include "structs.h"
13 #include "globals.h"
14
15 /* 1-line staff allows for default pitch on first chord of measure. However,
16  * at the time we are gathering note information, it may not be known whether
17  * we are on a 1-line staff or not, so it is temporarily set to PP_NO_PITCH,
18  * then later if we have a 1-line staff, it gets converted to default of C4 */
19 #define DFLT_PITCH      'c'
20 #define DFLT_OCT        4
21
22 /* how many NOTE structs to allocate at a time. We don't know in advance
23  * how many we'll need, so get a bunch and realloc as necessary. */
24 #define NOTE_CHUNK (4)
25
26 static int Tuplet_state = NOITEM;       /* current tuplet state */
27 static struct GRPSYL *Tuplet_list_p;    /* first GRPSYL in a tuplet group */
28 static short Slur_begin;        /* slurtolist index at which <> began */
29
30 /* static functions */
31 static void clone_notelist P((struct GRPSYL *new_p, struct GRPSYL *old_p,
32                 int copy_acc_etc));
33 static void finish_bar P((void));
34 static void restart_bar P((void));
35 static void fix_grpsyl_list P((struct MAINLL *mainll_item_p));
36 static void fix_a_group_list P((struct GRPSYL *grpsyllist_p, int staffno,
37                 int vno, char *fname, int lineno, int mrptnum,
38                 struct TIMEDSSV * tssv_p));
39 static void chk_a_syl_list P((struct GRPSYL *grpsyl_p, char *fname,
40                 int lineno));
41
42 /* change relative octaves to absolute */
43 static void resolve_octaves P((struct GRPSYL *grpsyl_item_p,
44                 int default_octave));
45 static void fix_frets P((struct GRPSYL *grpsyl_item_p));
46 static int string_number P((struct STRINGINFO *stringinfo_p, int nstrings,
47                 int letter, int accidental, int nticks));
48
49 static void check_grace P((struct GRPSYL *grpsyl_item_p, char *fname,
50                 int lineno));
51 static void fix_strlist P((struct GRPSYL *gs_p, int font, int size,
52                 char *fname, int lineno));
53 static void check4missing_voices P((struct MAINLL *list_p));
54 static void fix_mrpt P((struct MAINLL *mll_p));
55 static int chk_mrpt_ssv_interactions P((struct SSV *ssv_p, struct GRPSYL *gs_p,
56                 int staffno));
57 static int is_ms P((struct GRPSYL *gs_p));
58 static int voices_override P((int staff, int field));
59 static void sort_notes P((struct GRPSYL *grpsyl_p, char *fname, int lineno));
60 static int parse_bend_string P((char *bendstring));
61 static void check_bends P((struct GRPSYL *gs_p, struct MAINLL *mll_p));
62 \f
63
64 /* allocate a new GRPSYL struct and return a pointer to it */
65
66 struct GRPSYL *
67 newGRPSYL(grp_or_syl)
68
69 int grp_or_syl;         /* GS_GROUP or GS_SYLLABLE */
70
71 {
72         struct GRPSYL *new_p;
73
74
75         /* allocate a new structure */
76         CALLOC(GRPSYL, new_p, 1);
77
78         /* fill in default values */
79         new_p->grpsize = GS_NORMAL;
80         new_p->grpvalue = GV_NORMAL;
81         new_p->grpcont = GC_NOTES;
82         new_p->beamloc = NOITEM;
83         new_p->breakbeam = NO;
84         new_p->tuploc = NOITEM;
85         new_p->grpsyl = (short) grp_or_syl;
86         new_p->roll = NOITEM;
87         new_p->inputfile = Curr_filename;
88         new_p->inputlineno = (short) yylineno;
89         new_p->normwith = YES;
90         new_p->stemdir = (short) UNKNOWN;
91         new_p->beamto = CS_SAME;
92         new_p->stemto = CS_SAME;
93         /* default to no user-specified stem length */
94         new_p->stemlen = STEMLEN_UNKNOWN;
95         /* default to no user-specified rest dist */
96         new_p->restdist = NORESTDIST;
97         /* default to no user-specified syllable position */
98         new_p->sylposition = NOSYLPOSITION;
99         /* no clef change before this group */
100         new_p->clef = NOCLEF;
101         /* by default, placement will figure out the note head shape */
102         new_p->headshape = HS_UNKNOWN;
103         /* let Mup figure out angle for beams */
104         new_p->beamslope = NOBEAMANGLE;
105         /* everything else zero-ed is proper default */
106
107         return(new_p);
108 }
109 \f
110
111 /* Copy group attributes (grpsize, grpvalue, "with" list, etc)
112  * from one GRPSYL to another */
113
114 void
115 copy_attributes(newgrp_p, oldgrp_p)
116
117 struct GRPSYL *newgrp_p;        /* copy info into this one */
118 struct GRPSYL *oldgrp_p;        /* copy from this one */
119
120 {
121         if (oldgrp_p == 0) {
122                 yyerror("empty [] not allowed on the first chord of a measure");
123                 return;
124         }
125
126         newgrp_p->grpvalue = oldgrp_p->grpvalue;
127         newgrp_p->grpsize = oldgrp_p->grpsize;
128         newgrp_p->headshape = oldgrp_p->headshape;
129         newgrp_p->stemdir = oldgrp_p->stemdir;
130         newgrp_p->stemlen = oldgrp_p->stemlen;
131         newgrp_p->ho_usage = oldgrp_p->ho_usage;
132         newgrp_p->ho_value = oldgrp_p->ho_value;
133
134         clone_withlist(newgrp_p, oldgrp_p);
135
136         if (oldgrp_p->slash_alt > 0) {
137                 newgrp_p->slash_alt = oldgrp_p->slash_alt;
138         }
139         newgrp_p->padding = oldgrp_p->padding;
140         /* restdist is only used for rests, but easiest to just always copy,
141          * since grpcont hasn't been set yet.  */
142         newgrp_p->restdist = oldgrp_p->restdist;
143 }
144 \f
145
146 /* make a copy of withlist */
147
148 void
149 clone_withlist(newgrp_p, oldgrp_p)
150
151 struct GRPSYL *newgrp_p;
152 struct GRPSYL *oldgrp_p;
153
154 {
155         register int n;         /* index through with list */
156
157
158         newgrp_p->nwith = oldgrp_p->nwith;
159         if (oldgrp_p->nwith > 0) {
160                 MALLOCA(char *, newgrp_p->withlist, oldgrp_p->nwith);
161                 for (n = 0; n < oldgrp_p->nwith; n++) {
162                         newgrp_p->withlist[n] =
163                                         copy_string(oldgrp_p->withlist[n] + 2,
164                                         Curr_font, Curr_size);
165                 }
166         }
167 }
168 \f
169
170 /* Copy timeunit info from one GRPSYL to another,
171  * or use default if source is NULL. If needed to use default timeunit,
172  * and that included additive times, returns pointer to the list of those
173  * times, else return NULL. */
174
175 struct TIMELIST *
176 copy_timeunit(newgrp_p, oldgrp_p, timelist_p)
177
178 struct GRPSYL *newgrp_p;        /* copy info into here */
179 struct GRPSYL *oldgrp_p;        /* copy from here. If NULL, use default value */
180 struct TIMELIST *timelist_p;    /* If non-null use this instead of oldgrp_p */
181
182 {
183         struct SSV *ssv_p;
184
185         ssv_p = 0;
186
187         if (oldgrp_p == (struct GRPSYL *) 0) {
188
189                 /* use default timeunit. Have to figure out differently for
190                  * lyrics than notes, because for lyrics the voice depends
191                  * on the lyrics place. */
192                 if (newgrp_p->grpsyl == GS_GROUP) {
193                         ssv_p = get_dflt_timeunit_ssv();
194                 }
195                 else {
196                         ssv_p = get_lyr_dflt_timeunit_ssv();
197                 }
198                 newgrp_p->fulltime = ssv_p->timeunit;
199
200                 newgrp_p->basictime = reconstruct_basictime(newgrp_p->fulltime);
201                 newgrp_p->dots = recalc_dots(newgrp_p->fulltime,
202                                                         newgrp_p->basictime);
203                 return(ssv_p->timelist_p);
204         }
205
206         else {
207                 RATIONAL basictime;
208
209                 newgrp_p->basictime = oldgrp_p->basictime;
210                 newgrp_p->dots = oldgrp_p->dots;
211
212                 /* actual time value is (2 * basictime)
213                  *      - (basic_time x 1/2 to the (dots) power) */
214                 /* can't just copy the fulltime from oldgrp, because that
215                  * may have been altered if it was a tuplet */
216                 if (newgrp_p->basictime == 0) {
217                         /* strange case of double whole, stored as zero */
218                         basictime.n = 2;
219                         basictime.d = 1;
220                 }
221                 else if (newgrp_p->basictime == -1) {
222                         /* strange case of quad whole, stored as -1 */
223                         basictime.n = 4;
224                         basictime.d = 1;
225                 }
226                 else {
227                         basictime.n = 1;
228                         basictime.d = newgrp_p->basictime;
229                 }
230
231                 newgrp_p->fulltime = calcfulltime(basictime, newgrp_p->dots);
232         }
233
234         /* note that tuplet values are dealt with in end_tuplet(). Once we
235          * collect an entire tuplet, we can go through and multiply
236          * the fulltimes by the appropriate amount */
237         return(timelist_p);
238 }
239 \f
240
241 /* copy notes (list of NOTE structs) from one GRPSYL to another, along with
242  * a few group attributes that should be copied when a GRPSYL is copied. */
243
244 void
245 copy_notes(newgrp_p, oldgrp_p)
246
247 struct GRPSYL *newgrp_p;        /* copy into here */
248 struct GRPSYL *oldgrp_p;        /* copy from here */
249
250 {
251         /* first GRPSYL in bar must have notes specified */
252         if (oldgrp_p == (struct GRPSYL *) 0) {
253                 add_note(newgrp_p, PP_NO_PITCH, '\0', DFLT_OCT, 0, NO, (char *) 0);
254                 newgrp_p->notelist [ newgrp_p->nnotes ].tie = NO;
255                 newgrp_p->notelist [ newgrp_p->nnotes ].notesize = GS_NORMAL;
256         }
257
258         else {
259                 /* there was a previous GRPSYL -- use it for defaults */
260                 newgrp_p->grpcont = oldgrp_p->grpcont;
261                 clone_notelist(newgrp_p, oldgrp_p, NO);
262                 newgrp_p->is_meas = oldgrp_p->is_meas;
263                 newgrp_p->uncompressible = oldgrp_p->uncompressible;
264         }
265 }
266 \f
267
268 /* once the info about a group has been gathered,
269  * add it to list of GRPSYL structs */
270
271 void
272 link_notegroup(newgrp_p, last_grp_p)
273
274 struct GRPSYL *newgrp_p;                /* one to add on */
275 struct GRPSYL *last_grp_p;              /* one to add it onto */
276
277 {
278         int n;
279
280         debug(4, "link_notegroup file=%s line=%d", newgrp_p->inputfile,
281                                 newgrp_p->inputlineno);
282
283         if (last_grp_p == (struct GRPSYL *) 0) {
284                 /* first one: we need to keep a pointer to this one to
285                  * link onto one or more STAFF structs later */
286                 Curr_gs_list_p = newgrp_p;
287         }
288
289         else {
290                 /* link onto list */
291                 last_grp_p->next = newgrp_p;
292         }
293         newgrp_p->prev = last_grp_p;
294
295         /* fill in tuplet information, based on current state. Adjust tuplet
296          * state if necessary for use with later groups */
297         newgrp_p->tuploc = (short) Tuplet_state;
298         if (Tuplet_state == STARTITEM) {
299                 Tuplet_list_p = newgrp_p;
300                 Tuplet_state = INITEM;
301         }
302
303         /* We don't have a quad whole note character,
304          * so don't allow 1/4 on notes */
305         if (EQ(newgrp_p->fulltime, Four)) {
306                 for (n = 0; n < newgrp_p->nnotes; n++) {
307                         if (newgrp_p->notelist[n].letter >= 'a' &&
308                                         newgrp_p->notelist[n].letter <= 'g') {
309                                 yyerror("1/4 not allowed on notes");
310                                 break;
311                         }
312                 }
313         }
314
315         /* dist is only allowed on rests */
316         if (newgrp_p->restdist != NORESTDIST) {
317                 for (n = 0; n < newgrp_p->nnotes; n++) {
318                         if (newgrp_p->notelist[n].letter != PP_REST) {
319                                 yyerror("dist only allowed on rests");
320                         }
321                 }
322         }
323 }
324 \f
325
326 /* make a copy of a linked list of GRPSYL structs and return a pointer to it */
327 /* Note that while this function is called for both groups and syllables,
328  * lyric syllables are not copied by this function. Lyrics syllables are
329  * handled in lyrics.c  */
330
331 struct GRPSYL *
332 clone_gs_list(list_p, copy_noteinfo)
333
334 struct GRPSYL *list_p;  /* the list to be cloned */
335 int copy_noteinfo;      /* if YES, copy notes and with lists */
336
337 {
338         struct GRPSYL *new_p, *newlist_p;
339         struct GRPSYL *prev_p = (struct GRPSYL *) 0;    /* to remember last one,
340                                  to know where to link on current one */
341
342
343
344         /* walk down the existing list */
345         for (newlist_p = (struct GRPSYL *) 0; list_p != (struct GRPSYL *) 0;
346                                         list_p = list_p->next) {
347
348                 /* allocate space for copy of list element and fill it in */
349                 MALLOC(GRPSYL, new_p, 1);
350
351                 (void) memcpy(new_p, list_p, sizeof(struct GRPSYL) );
352
353                 /* we can't just copy the links--they have to be recalculated */
354                 if (newlist_p == (struct GRPSYL *) 0) {
355
356                         /* first in new list: keep track of where list begins,
357                          * since we need to return this value, and keep track
358                          * of where we are, so we can link the next element
359                          * onto this one, which will be its prev element */
360                         newlist_p = new_p;
361                         prev_p = new_p;
362                 }
363
364                 else {
365                         prev_p->next = new_p;
366                         new_p->prev = prev_p;
367                         prev_p = new_p;
368                 }
369
370                 if (copy_noteinfo == YES) {
371                         /* also need to make copies of the notelist, since
372                          * they contain COORDS that are unique
373                         * for each instance */
374                         clone_notelist(new_p, list_p, YES);
375
376                         /* with lists cannot be shared,
377                          * because otherwise fix_string
378                          * could get called on an already fixed string. */
379                         clone_withlist(new_p, list_p);
380                 }
381         }
382
383         return(newlist_p);
384 }
385 \f
386
387 /* make a copy of the notelist in one GRPSYL struct into another */
388
389 static void
390 clone_notelist(new_p, old_p, copy_acc_etc)
391
392 struct GRPSYL *new_p;   /* copy into here */
393 struct GRPSYL *old_p;   /* from here */
394 int copy_acc_etc;       /* if YES, copy accidentals.  If just reusing a
395                          * group on the same staff, we don't want to
396                          * copy these things */
397
398 {
399         register int n;         /* index through note list */
400
401
402         /* mark the number of notes in the new list */
403         new_p->nnotes = old_p->nnotes;
404
405         /* copy cross-staff stem info */
406         new_p->stemto = old_p->stemto;
407         new_p->stemto_idx = old_p->stemto_idx;
408
409         if (old_p->nnotes > 0) {
410
411                 /* allocate new memory and copy from old to new */
412                 MALLOC(NOTE, new_p->notelist, old_p->nnotes);
413
414                 (void)memcpy((char *) new_p->notelist, (char *) old_p->notelist,
415                         (unsigned) (old_p->nnotes * sizeof(struct NOTE)));
416
417         }
418
419         else if ((new_p->grpsyl == GS_GROUP) && (new_p->grpcont == GC_NOTES) &&
420                                 is_mrpt(new_p) == NO) {
421                 yyerror("tried to use previous chord for defaults, but it had no notes");
422                 return;
423         }
424
425         for (n = 0; n < old_p->nnotes; n++) {
426
427                 if (copy_acc_etc == YES) {
428                         new_p->notelist[n].nslurto = old_p->notelist[n].nslurto;
429                         /* It isn't necessarily safe to share slurto lists.
430                          * In particular, if multiple staffs are specified,
431                          * and they have different clefs, the default octave
432                          * will be different. So make a copy to be safe. */
433                         if (new_p->notelist[n].nslurto > 0) {
434                                 MALLOC(SLURTO, new_p->notelist[n].slurtolist, 
435                                                 new_p->notelist[n].nslurto);
436                                 memcpy(new_p->notelist[n].slurtolist,
437                                         old_p->notelist[n].slurtolist,
438                                         sizeof(struct SLURTO) *
439                                         old_p->notelist[n].nslurto);
440                         }
441                         else {
442                                 new_p->notelist[n].slurtolist = (struct SLURTO *) 0;
443                         }
444                 }
445                 else {
446                         new_p->notelist[n].nslurto = 0;
447                         new_p->notelist[n].slurtolist = (struct SLURTO *) 0;
448                         new_p->notelist[n].is_bend = NO;
449                         new_p->notelist[n].smallbend = NO;
450                 }
451
452                 /* alloc space for coordinates */
453                 MALLOCA(float, new_p->notelist[n].c, NUMCTYPE);
454         }
455
456         /* if a note that was cloned had an accidental on it,
457          * the clone doesn't need the accidental since we are in
458          * the same measure. Ties also do not get copied. But on tab staff,
459          * the accidental does need to get copied since it's part of the
460          * name of the string. */
461         for (n = 0; n < old_p->nnotes; n++) {
462                 if(copy_acc_etc == NO) {
463                         if (Doing_tab_staff == NO) {
464                                 new_p->notelist[n].accidental = '\0';
465                                 new_p->notelist[n].acc_has_paren = NO;
466                         }
467                         new_p->notelist[n].tie = NO;
468                 }
469         }
470 }
471 \f
472
473 /* add a NOTE to a list of notes off a GRPSYL */
474
475 void
476 add_note(grpsyl_p, pitch, accidental, octave, nticks, has_paren, bendstring)
477
478 struct GRPSYL *grpsyl_p;        /* what group to associate note with */
479 int     pitch;                  /* 'a' to 'g' */
480                                 /* can also be pseudo-pitches for rest, space and rpt */
481 int     accidental;             /* '\0', '#', '&', 'n', 'x', or 'B' */
482 int     octave;                 /* octave for normal staffs,
483                                  * fret for tab staffs, uncompressibility
484                                  * for space pseudo-notes */
485 int nticks;                     /* number of tick marks, for tab staffs */
486 int has_paren;                  /* YES if accidental has parentheses (for
487                                  * non-tab staffs) or if fret has parentheses
488                                  * (for tab staffs) */
489 char *bendstring;               /* bend specification, or null if no bend */
490
491 {
492         register int index;             /* into notelist */
493
494
495         if (grpsyl_p->nnotes == 0) {
496                 /* first time--need to allocate */
497                 CALLOC(NOTE, grpsyl_p->notelist, NOTE_CHUNK);
498         }
499
500         /* see if overflowed and thus need to re-allocate more space */
501         else if ((grpsyl_p->nnotes % NOTE_CHUNK) == 0) {
502                 REALLOC(NOTE, grpsyl_p->notelist, grpsyl_p->nnotes + NOTE_CHUNK);
503                 memset((void *) (& grpsyl_p->notelist[grpsyl_p->nnotes]),
504                         0, NOTE_CHUNK * sizeof(struct NOTE));
505         }
506
507         /* find index into notelist where we put this note */
508         index = grpsyl_p->nnotes;
509
510         /* allocate space for coordinates. The coordinates must be in a
511          * malloc-ed array and not a static array. At one point, we thought
512          * we could save a few bytes by changing back to array, but that
513          * would be a lot of work. The reason is that the
514          * coordinates are pointed to whenever there is a location tag on a
515          * note. The array must not move or else the location tag reference
516          * would be wrong. Since we qsort the notes later on, the coords would
517          * move if they were a static array rather than a malloc-ed array.
518          * In order to allow moving them, we'd have to keep track of all note
519          * location tags for much longer than we currently do, and then go
520          * back and change their values when notes are sorted. It would require
521          * finding entries in a hash table by other than their key (i.e,
522          * require a linear search or another whole indexing scheme), and
523          * also require either going back to patch up any lines, curves, and
524          * prints that used the coords, or delaying their definition by saving
525          * lots of information around. */
526         MALLOCA(float, grpsyl_p->notelist[index].c, NUMCTYPE);
527
528         grpsyl_p->notelist [ index ].letter = (short) pitch;
529         grpsyl_p->notelist [ index ].accidental = (char) accidental;
530         grpsyl_p->notelist [ index ].acc_has_paren = has_paren;
531         if (Doing_tab_staff == YES && pitch >= 'a' && pitch <= 'g') {
532                 int fret;
533
534                 fret = octave;
535
536                 /* do error checks */
537                 if (accidental != '\0' && accidental != '#' && accidental != '&') {
538                         yyerror("accidental on tab staff can only be # or &");
539                 }
540
541                 if (fret == NOFRET && bendstring == (char *) 0) {
542                         yyerror("a fret number and/or bend must be specified");
543                         /* put in something valid so later functions won't
544                          * complain */
545                         fret = MINFRET;
546                 }
547                 else if ((fret < MINFRET || fret > MAXFRET) && fret != NOFRET) {
548                         l_yyerror(grpsyl_p->inputfile, grpsyl_p->inputlineno,
549                                 "invalid fret number %d, must be %d-%d or have a bend",
550                                 fret, MINFRET, MAXFRET);
551                         fret = MINFRET;
552                 }
553
554                 /* Cram fret and nticks in stepsup field for now. Later
555                  * we will put fret in its proper place, but right now that
556                  * is used for something else */
557                 TMP_SAVE( (&(grpsyl_p->notelist[index])), nticks, fret);
558
559                 /* cram the bend info into octave field */
560                 grpsyl_p->notelist [ index ].octave
561                                 = parse_bend_string(bendstring);
562         }
563         else {
564                 if (nticks > 0) {
565                         yyerror("' not allowed on non-tablature staff");
566                 }
567                 grpsyl_p->notelist [ index ].octave = (short) octave;
568         }
569         grpsyl_p->notelist [ index ].nslurto = 0;
570         grpsyl_p->notelist [ index ].tie = NO;
571         grpsyl_p->notelist [ index ].tiestyle = L_NORMAL;
572         grpsyl_p->notelist [ index ].tiedir = UNKNOWN;
573         grpsyl_p->notelist [ index ].slurtolist = (struct SLURTO *)0;
574         grpsyl_p->notelist [ index ].notesize = GS_NORMAL;
575
576         /* don't need to initialize anything else in NOTE */
577
578         (grpsyl_p->nnotes)++;
579 }
580 \f
581
582 /* once an entire chord is gathered, release any extra space gotten for
583  * notes */
584
585 void
586 resize_notelist(gs_p)
587
588 struct GRPSYL *gs_p;
589
590 {
591         int extra;      /* how many extra NOTE structs there are */
592
593
594         extra = NOTE_CHUNK - (gs_p->nnotes % NOTE_CHUNK);
595
596         if (extra > 0 && extra < NOTE_CHUNK) {
597                 /* release extra NOTE space */
598                 REALLOC(NOTE, gs_p->notelist, gs_p->nnotes);
599         }
600 }
601 \f
602
603 /* when we get to a bar line, we have to go back and rearrange things and
604  * do some error checks that couldn't be done until we had everything
605  * collected. All the non-STAFF structures have to be put after the STAFFs.
606  */
607
608 void
609 do_bar(bartype)
610
611 int bartype;
612
613 {
614         struct MAINLL *mll_p;           /* to walk through list */
615         static short firstbar = YES;    /* if first time this func was called */
616         short v;                        /* voice number */
617         short found_above_for[MAXSTAFFS+1];     /* how many times this staff 
618                                          * was succesfully matched up with a
619                                          * "bm with staff below" */
620         int i;                          /* index though above array */
621         int other_staff;                /* other staff for cross-staff beam */
622         int with_above;                 /* how many "bm with staff above"
623                                          * found on current staff */
624
625
626         debug(2, "do_bar");
627
628         /* The "restart" is a strange thing only vaguely like a regular bar,
629          * so handle it separately */
630         if (bartype == RESTART) {
631                 restart_bar();
632                 return;
633         }
634
635         if (List_of_staffs_p == (struct MAINLL *) 0) {
636                 yyerror("empty measure");
637                 return;
638         }
639
640         /* Make sure all voices are present; fill in measure space for
641          * any that are missing */
642         check4missing_voices(List_of_staffs_p);
643
644         /* attach all the lyrics to the right places */
645         attach_lyrics2staffs(List_of_staffs_p);
646
647         /* set up all the cross-staff beams */
648         for (i = 1; i <= MAXSTAFFS; i++) {
649                 found_above_for[i] = 0;
650         }
651         for (mll_p = List_of_staffs_p; mll_p != (struct MAINLL *) 0;
652                                 mll_p = mll_p->next) {
653
654                 if (mll_p->str != S_STAFF) {
655                         break;
656                 }
657
658                 with_above = 0;
659                 /* check every group of every voice */
660                 for (v = 0; v < MAXVOICES; v++) {
661                         struct GRPSYL *gs_p;
662
663                         /* Only check visible voices */
664                         if (vvpath(mll_p->u.staff_p->staffno, v+1, VISIBLE)->visible == NO) {
665                                 continue;
666                         }
667
668                         for (gs_p = mll_p->u.staff_p->groups_p[v];
669                                         gs_p != (struct GRPSYL *) 0;
670                                         gs_p = gs_p->next) {
671
672                                 /* if this is the first group in a cross-staff
673                                  * beam, take care to that */
674                                 if (gs_p->beamloc == STARTITEM &&
675                                                 gs_p->beamto == CS_BELOW) {
676                                         /* before calling chk_crossbeams, make
677                                          * sure the gs_p we are passing has its
678                                          * vno set properly, since normally
679                                          * that doesn't happen till later.
680                                          * Maybe this cross-staff stuff should
681                                          * be done later, but I don't want to
682                                          * move it without a lot of thought to
683                                          * make sure that wouldn't break
684                                          * something, so this will work for now.
685                                          */
686                                         gs_p->vno = v;
687                                         other_staff = chk_crossbeam(gs_p, mll_p);
688                                         if (other_staff > 0) {
689                                                 (found_above_for[other_staff])++;
690                                         }
691                                 }
692                                 else if (gs_p->beamloc == STARTITEM &&
693                                                 gs_p->beamto == CS_ABOVE) {
694                                         with_above++;
695                                 }
696                         }
697                 }
698                 if (found_above_for[mll_p->u.staff_p->staffno] < with_above) {
699                         l_yyerror(mll_p->inputfile, mll_p->inputlineno,
700                                 "'bm with staff above' has no visible matching 'bm with staff below' (may be missing, invisible, or on wrong voice)");
701                 }
702         }
703
704         /* if first measure, check if "pickup" measure. If so, it doesn't
705          * count toward measure number */
706         if (firstbar == YES && bartype != INVISBAR) {
707                 if (has_pickup() == YES) {
708                         /* don't count pickup measure */
709                         Meas_num--;
710                 }
711                 /* set flag so we won't check again */
712                 firstbar = NO;
713         }
714
715         /* increment measure number for rehearsal numbers */
716         if (bartype != INVISBAR && Got_multirest == 0) {
717                 Meas_num++;
718         }
719
720         finish_bar();
721 }
722 \f
723
724 /* Returns YES if piece begins with a pickup measure. */
725
726 int
727 has_pickup()
728 {
729         int v;                  /* voice index */
730         struct MAINLL *mll_p;   /* to step through main list */
731
732         /* go through each voice of each staff looking for non-space */
733         for (mll_p = Mainllhc_p; mll_p != (struct MAINLL *) 0;
734                                                         mll_p = mll_p->next) {
735
736                 /* Measures where the entire measure is compressible space
737                  * and which end with an invisbar were probably just for the
738                  * purpose of kludging in a funny time signature or something
739                  * like that, so we ignore those measures while checking for a
740                  * pickup measure.
741                  */
742                 if (mll_p->str == S_BAR && mll_p->u.bar_p->bartype != INVISBAR) {
743                         /* got through entire first measure */
744                         break;
745                 }
746                 if (mll_p->str != S_STAFF) {
747                         continue;
748                 }
749
750                 for (v = 0; v < MAXVOICES; v++) {
751                         if (mll_p->u.staff_p->groups_p[v] != 0 &&
752                                                 (mll_p->u.staff_p->groups_p[v]
753                                                 ->grpcont != GC_SPACE ||
754                                                 mll_p->u.staff_p->groups_p[v]
755                                                 ->uncompressible == YES) ) {
756                                 /* Found a voice that exists,
757                                  * but begins with a non-space or an
758                                  * uncompressible space. So it's not a pickup */
759                                 return(NO);
760                         }
761                 }
762         }
763         return(YES);
764 }
765 \f
766
767 /* Do final cleanup in preparation for next bar of input */
768
769 static void
770 finish_bar()
771
772 {
773         struct MAINLL *mll_p;
774
775         /* Walk thru the list, fixing everything that is left to fix */
776         for (mll_p = List_of_staffs_p; mll_p != (struct MAINLL *) 0;
777                                 mll_p = mll_p->next) {
778
779                 if (mll_p->str == S_BAR) {
780                         break;
781                 }
782
783                 if (mll_p->str == S_STAFF) {
784                         /* Walk down each GRPSYL list and
785                          * look for various errors and fix up things that
786                          * couldn't be determined before */
787                         fix_grpsyl_list(mll_p);
788
789                         /* Set the visibility attribute for staff */
790                         mll_p->u.staff_p->visible =
791                                         (svpath(mll_p->u.staff_p->staffno,
792                                         VISIBLE))->visible;
793                 }
794
795         }
796
797         /* add padding etc for any rolls in the measure */
798         do_rolls(Mainlltc_p);
799
800         /* in case things are screwed up because of user input errors,
801          * reset tuplet state. */
802         Tuplet_state = NOITEM;
803
804         /* do measure level checks on "stuff" */
805         meas_stuff_chk();
806
807         /* get ready for next measure */
808         List_of_staffs_p = (struct MAINLL *) 0;
809         Got_multirest = 0;
810         Got_group = NO;
811         reset_input_style();
812         lyr_new_bar();
813 }
814 \f
815
816 /* A "restart" is a funny sort of bar.
817  * We create a measure of space before it. */
818
819 static void
820 restart_bar()
821
822 {
823         struct MAINLL *mll_p;
824         struct MAINLL *mainbar_p;       /* the restart */
825
826         if (List_of_staffs_p != 0) {
827                 yyerror("restart cannot be preceeded by music data");
828                 return;
829         }
830         if (Got_some_data == NO) {
831                 yyerror("restart cannot be used at the beginning");
832                 return;
833         }
834
835         /* It doesn't make sense to begin a repeat right before a restart,
836          * so block that. */
837         for (mll_p = Mainlltc_p->prev; mll_p != (struct MAINLL *) 0;
838                                                         mll_p = mll_p->prev) {
839                 if (mll_p->str == S_BAR) {
840                         if (mll_p->u.bar_p->bartype == REPEATSTART) {
841                                 yyerror("repeatstart not allowed immediately before restart");
842                         }
843                         else if (mll_p->u.bar_p->bartype == REPEATBOTH) {
844                                 yyerror("repeatboth not allowed immediately before restart");
845                         }
846                         break;
847                 }
848         }
849
850         /* save the bar so we can move after the staffs */
851         mainbar_p = Mainlltc_p;
852         /* create a "measure" of all space */
853         create_staffs();
854         check4missing_voices(List_of_staffs_p);
855         /* move the restart BAR after the empty measure */
856         unlinkMAINLL(mainbar_p);
857         insertMAINLL(mainbar_p, Mainlltc_p);
858         finish_bar();
859 }
860 \f
861
862 /* walk down the linked lists of GRPSYLs and fix everything that is left to
863  * fix: font/size, octaves, default timeunits, etc */
864
865 static void
866 fix_grpsyl_list(mainll_item_p)
867
868 struct MAINLL *mainll_item_p;
869
870 {
871         register int v;
872         struct STAFF *staff_p;
873         struct MAINLL *mll_p;           /* for finding timed SSVs */
874         struct TIMEDSSV *tssv_p;
875         int numvoices;
876
877
878         /* error checks */
879         if (mainll_item_p->str != S_STAFF) {
880                 pfatal("wrong struct type passed to fix_grpsyl_list()");
881         }
882         staff_p = mainll_item_p->u.staff_p;
883
884
885         /* verify voice is valid for current vscheme */
886         numvoices = vscheme_voices(svpath(staff_p->staffno,VSCHEME)->vscheme);
887         if (staff_p->groups_p[1] != (struct GRPSYL *) 0 && numvoices == 1) {
888                 l_yyerror(staff_p->groups_p[1]->inputfile,
889                                 staff_p->groups_p[1]->inputlineno,
890                                 "can't have voice 2 when vscheme=1");
891         }
892         if (staff_p->groups_p[2] != (struct GRPSYL *) 0 && numvoices < 3) {
893                 l_yyerror(staff_p->groups_p[2]->inputfile,
894                                 staff_p->groups_p[2]->inputlineno,
895                                 "can't have voice 3 unless vscheme is 3o or 3f");
896         }
897
898         /* Determine if there are timed SSVs to deal with */
899         tssv_p = 0;
900         for (mll_p = mainll_item_p; mll_p != 0; mll_p = mll_p->next) {
901                 if (mll_p->str == S_BAR) {
902                         tssv_p = mll_p->u.bar_p->timedssv_p;
903                         break;
904                 }
905         }
906
907         /* fix the GROUPS */
908         for (v = 0; v < MAXVOICES; v++) {
909                 if (staff_p->groups_p[v] != (struct GRPSYL *) 0) {
910                         if (tssv_p != 0) {
911                                 setssvstate(mainll_item_p);
912                         }
913                         fix_a_group_list(staff_p->groups_p[v],
914                                 staff_p->staffno, v + 1,
915                                 staff_p->groups_p[v]->inputfile,
916                                 staff_p->groups_p[v]->inputlineno,
917                                 staff_p->mrptnum,
918                                 tssv_p);
919
920                         /* on tablature staffs, must deal with bends */
921                         if (is_tab_staff(staff_p->staffno) == YES) {
922                                 check_bends(staff_p->groups_p[v], mainll_item_p);
923                         }
924                 }
925         }
926
927         /* make sure timed SSVs are all up to date at end of measure */
928         for (  ; tssv_p != 0; tssv_p = tssv_p->next) {
929                 asgnssv(&tssv_p->ssv);
930         }
931
932         /* then check syllables */
933         for (v = 0; v < staff_p->nsyllists; v++) {
934                 chk_a_syl_list(staff_p->syls_p[v],
935                                 staff_p->syls_p[v]->inputfile,
936                                 staff_p->syls_p[v]->inputlineno);
937         }
938 }
939 \f
940
941 /* walk through a list of GRPSYLs (GROUPs, not SYLLABLESs), and fix up things
942  * like octaves, etc */
943
944
945 static void
946 fix_a_group_list(grpsyllist_p, staffno, vno, fname, lineno, mrptnum, tssv_p)
947
948 struct GRPSYL *grpsyllist_p;
949 int staffno;
950 int vno;                /* voice number */
951 char *fname;            /* file name for error messages */
952 int lineno;             /* input line number, for error messages */
953 int mrptnum;            /* to decide whether to run fix_string or not */
954 struct TIMEDSSV *tssv_p;        /* points to any timed SSVs in this measure */
955
956 {
957         int font, size;
958         int n;                  /* index through notelist */
959         RATIONAL total_time;    /* to add up all fulltimes in current voice/verse */
960         short default_octave;
961         struct GRPSYL *grpsyl_p;
962         struct GRPSYL *gs_p;            /* for walking through grace runs */
963         int nn;
964
965
966
967         if (grpsyllist_p == (struct GRPSYL *) 0) {
968                 /* this is un undefined voice, so nothing to do */
969                 return;
970         }
971
972         debug(4, "fix_a_group_list file=%s lineno=%d staffno=%d vno=%d",
973                         grpsyllist_p->inputfile, grpsyllist_p->inputlineno,
974                         staffno, vno);
975
976         /* will need to fix up the font and size of strings in the
977          * "with" lists, so figure out
978          * which font and size to use for that */
979         font = (svpath(staffno, FONT))->font
980                                 + (svpath(staffno, FONTFAMILY))->fontfamily;
981         size = (svpath(staffno, SIZE))->size;
982
983         /* initialize total time for this voice in this meas */
984         total_time = Zero;
985
986         /* find default octave for this staff/voice */
987         default_octave = (vvpath(staffno, vno, DEFOCT))->defoct;
988
989         grpsyl_p = grpsyllist_p;
990
991         Doing_tab_staff = is_tab_staff(staffno);
992
993         /* do each group */
994         for (   ; grpsyl_p != (struct GRPSYL *) 0; grpsyl_p = grpsyl_p->next) {
995
996                 /* fill in staffno and vno */
997                 grpsyl_p->staffno = (short) staffno;
998                 grpsyl_p->vno = (short) vno;
999
1000                 /* Apply any timed SSVs */
1001                 while (tssv_p != 0 && LE(tssv_p->time_off, total_time)) {
1002                         asgnssv(&tssv_p->ssv);
1003                         tssv_p = tssv_p->next;
1004                         /* the default octave might have changed,
1005                          * so look it up again.
1006                          */
1007                         default_octave = (vvpath(staffno, vno, DEFOCT))->defoct;
1008                 }
1009                 /* if no pitch given on first group of measure, use default
1010                  * pitch if 1-line staff, otherwise error */
1011                 if (grpsyl_p->nnotes == 1 && grpsyl_p->notelist[0].letter
1012                                                                 == PP_NO_PITCH) {
1013                         if (svpath(staffno, STAFFLINES)->stafflines == 1 &&
1014                                         Doing_tab_staff == NO) {
1015                                 grpsyl_p->notelist[0].letter = DFLT_PITCH;
1016                         }
1017                         else {
1018                                 l_yyerror(fname, lineno,
1019                                         "must have note(s) specified for first group in bar");
1020                         }
1021                 }
1022
1023                 /* make sure "with" lists are in right size/font */
1024                 /* When groups are copied to expand measure repeats for
1025                  * MIDI purposes, any 'with' lists would already be in
1026                  * internal format, so we don't want to try to fix them,
1027                  * but otherwise strings need to be put in internal format */
1028                 if (mrptnum == 0) {
1029                         fix_strlist(grpsyl_p, font, size, fname, lineno);
1030                 }
1031
1032                 if (mrptnum == 0) {
1033                         if (Doing_tab_staff == YES) {
1034                                 /* determine proper STRINGNO and FRETNO information */
1035                                 fix_frets(grpsyl_p);
1036                         }
1037                         else {
1038                                 /* take care of relative octaves */
1039                                 resolve_octaves(grpsyl_p, default_octave);
1040                         }
1041                 }
1042
1043                 if (svpath(staffno, STAFFLINES)->stafflines == 1
1044                                                 && grpsyl_p->nnotes > 1) {
1045                         l_warning(fname, lineno,
1046                                 "more than one note in chord on 1-line staff");
1047                 }
1048
1049                 /* put notes in order top to bottom */
1050                 sort_notes(grpsyl_p, fname, lineno);
1051
1052                 /* if group is small notes, mark each note as small */
1053                 if (grpsyl_p->grpsize != GS_NORMAL) {
1054
1055                         for (n = 0; n < grpsyl_p->nnotes; n++) {
1056
1057                                 grpsyl_p->notelist[n].notesize
1058                                                         = grpsyl_p->grpsize;
1059                         }
1060                 }
1061
1062 #ifdef GRPSMALL
1063         /* at first we thought we should mark the whole group small if all
1064          * notes were small, but later decided not, so this is ifdef-ed out */
1065                 /* on the other hand, if all notes are explicitly marked
1066                  * small, make the whole group small. This is handy in case,
1067                  * for example, the user uses ? instead of [cue] on a
1068                  * single-note group */
1069                 else if (grpsyl_p->grpcont == GC_NOTES) {
1070                         int numsmall;   /* how many small notes */
1071
1072                         for (numsmall = n = 0; n < grpsyl_p->nnotes; n++) {
1073                                 if (grpsyl_p->notelist[n].notesize
1074                                                                 == GS_SMALL) {
1075                                         numsmall++;
1076                                 }
1077                         }
1078                         if (grpsyl_p->nnotes > 0 &&
1079                                                 numsmall == grpsyl_p->nnotes) {
1080                                 grpsyl_p->grpsize = GS_SMALL;
1081                         }
1082                 }
1083 #endif
1084
1085                 /* error check rests */
1086                 if (grpsyl_p->grpcont == GC_REST) {
1087                         if ( (grpsyl_p->prev != (struct GRPSYL *) 0)
1088                                         && (grpsyl_p->prev->grpcont
1089                                         == GC_NOTES)
1090                                         && (grpsyl_p->prev->grpvalue
1091                                         == GV_ZERO) ) {
1092                                 l_yyerror(fname, lineno,
1093                                         "can't have grace notes before rest");
1094                         }
1095
1096                         /* We used to disallow stemdir on rests, but it could
1097                          * come from an earlier []... and doesn't really hurt;
1098                          * it doesn't matter which direction the non-existent
1099                          * stem is, so we silently ignore.
1100                          */
1101                         if (IS_STEMLEN_KNOWN(grpsyl_p->stemlen)) {
1102                                 /* Might be just from a [ ]... on an earlier
1103                                  * group, and doesn't really hurt anything,
1104                                  * so silently ignore.
1105                                  */
1106                                 grpsyl_p->stemlen = 0.0;
1107                         }
1108                 }
1109
1110                 /* error check space */
1111                 else if (grpsyl_p->grpcont == GC_SPACE) {
1112                         if (IS_STEMLEN_KNOWN(grpsyl_p->stemlen)) {
1113                                 grpsyl_p->stemlen = 0.0;
1114                         }
1115                 }
1116
1117                 /* error check measure repeats */
1118                 if (is_mrpt(grpsyl_p) == YES) {
1119                         if (grpsyl_p->nwith != 0
1120                                         || grpsyl_p->grpvalue != GV_NORMAL
1121                                         || grpsyl_p->headshape != HS_UNKNOWN
1122                                         || grpsyl_p->grpsize != GS_NORMAL
1123                                         || IS_STEMLEN_KNOWN(grpsyl_p->stemlen)
1124                                         || grpsyl_p->stemdir != UNKNOWN
1125                                         || grpsyl_p->slash_alt > 0) {
1126                                 l_yyerror(fname, lineno,
1127                                         "can't specify items in [ ] with measure repeat (except padding and tag)");
1128                         }
1129                         if (grpsyl_p->slash_alt < 0) {
1130                                 l_yyerror(fname, lineno,
1131                                         "can't specify alt with measure repeat");
1132                         }
1133                         if (grpsyl_p->tie == YES) {
1134                                 l_yyerror(fname, lineno,
1135                                         "can't specify tie with measure repeat");
1136                         }
1137                 }
1138                 /* grace notes need special handling */
1139                 if (grpsyl_p->grpvalue == GV_ZERO && is_mrpt(grpsyl_p) == NO) {
1140
1141                         /* grace notes really take no time */
1142                         grpsyl_p->fulltime = Zero;
1143
1144                         /* do some error checks */
1145                         check_grace(grpsyl_p, fname, lineno);
1146
1147                         /* if more than one grace note in a row, beam them */
1148                         if (grpsyl_p->beamloc == NOITEM
1149                                                 && grpsyl_p->basictime > 4) {
1150
1151                                 /* if there is another grace following, will
1152                                  * need to beam them */
1153                                 if ((grpsyl_p->next != (struct GRPSYL *)0) &&
1154                                                 (grpsyl_p->next->grpvalue
1155                                                 == GV_ZERO) &&
1156                                                 (grpsyl_p->next->basictime
1157                                                 > 4) ) {
1158
1159                                         /* mark the first one as STARTITEM */
1160                                         grpsyl_p->beamloc = STARTITEM;
1161
1162                                         /* mark intermediates, if any,
1163                                          * as INITEM, and last in run
1164                                          * of grace as ENDITEM */
1165                                         for (gs_p = grpsyl_p->next;
1166                                                         gs_p->next
1167                                                         != (struct GRPSYL *) 0;
1168                                                         gs_p = gs_p->next) {
1169                                                 if (gs_p->next->grpvalue
1170                                                                 == GV_ZERO &&
1171                                                                 gs_p->next->basictime
1172                                                                 > 4) {
1173                                                         gs_p->beamloc = INITEM;
1174                                                 }
1175                                                 else {
1176                                                         gs_p->beamloc = ENDITEM;
1177                                                         break;
1178                                                 }
1179                                         }
1180                                         if (gs_p->next == (struct GRPSYL *) 0){
1181                                                 /* grace at end of measure is
1182                                                  * illegal, but we catch that
1183                                                  * in check_grace(). So just
1184                                                  * mark the beam as ended */
1185                                                 gs_p->beamloc = ENDITEM;
1186                                         }
1187                                 }
1188                         }
1189                 }
1190
1191                 /* if this group is alternating with the next, need to
1192                  * adjust fulltimes to be 1/2 of their current values. */
1193                 if (grpsyl_p->slash_alt < 0 && is_mrpt(grpsyl_p) == NO) {
1194                         /* need to error check first. First, there must
1195                          * be another group in this measure. Moreover, it
1196                          * must have the same fulltime value, and both
1197                          * must be either NORMAL or SMALL size.  Also, the
1198                          * next group can't have an alt. */
1199                         if (grpsyl_p->next == (struct GRPSYL *) 0) {
1200                                 l_yyerror(fname, lineno,
1201                                         "cannot specify 'alt' on last group in measure");
1202                                 continue;
1203                         }
1204
1205                         else if (grpsyl_p->next->grpvalue == GV_ZERO ||
1206                                         grpsyl_p->grpvalue == GV_ZERO) {
1207                                 l_yyerror(fname, lineno,
1208                                         "can't use 'alt' on grace notes");
1209                         }
1210
1211                         else if ( ! EQ(grpsyl_p->fulltime,
1212                                                 grpsyl_p->next->fulltime) ) {
1213                                 l_yyerror(fname, lineno,
1214                                         "groups in 'alt' pair must have identical time values");
1215                         }
1216
1217                         else if (grpsyl_p->next->grpsize
1218                                         != grpsyl_p->grpsize) {
1219                                 l_yyerror(fname, lineno,
1220                                         "can't mix normal and cue notes for 'alt'");
1221                         }
1222
1223                         else if (grpsyl_p->next->slash_alt < 0) {
1224                                 l_yyerror(fname, lineno,
1225                                         "can't have 'alt' on consecutive chords");
1226
1227                         }
1228
1229                         else if (grpsyl_p->next->slash_alt > 0) {
1230                                 l_yyerror(fname, lineno,
1231                                         "can't have 'slash' on second chord of 'alt' pair");
1232                         }
1233
1234                         else if (grpsyl_p->grpcont != GC_NOTES ||
1235                                         grpsyl_p->next->grpcont != GC_NOTES) {
1236                                 l_yyerror(fname, lineno,
1237                                         "'alt' must be preceeded and followed by notes, not rest or space");
1238                         }
1239
1240                         else {
1241                                 grpsyl_p->fulltime =
1242                                                 rdiv(grpsyl_p->fulltime, Two);
1243                                 grpsyl_p->next->fulltime =
1244                                                 rdiv(grpsyl_p->next->fulltime, Two);
1245                         }
1246
1247                         if (grpsyl_p->stemdir != grpsyl_p->next->stemdir) {
1248                                 l_yyerror(fname, lineno,
1249                                         "'alt' pair chords must have identical stem directions");
1250                         }
1251
1252                         if (grpsyl_p->beamto != CS_SAME) {
1253                                 l_yyerror(fname, lineno,
1254                                         "'alt' not allowed on cross-staff beams");
1255                         }
1256                 }
1257                 else if (grpsyl_p->grpvalue != GV_ZERO &&
1258                                                 grpsyl_p->slash_alt > 0 &&
1259                                                 is_mrpt(grpsyl_p) == NO) {
1260
1261                         /* need to make sure number of slashes is valid */
1262                         /* figure out how many actual chords are represented
1263                          * by the slashed chord */
1264                         switch (grpsyl_p->basictime) {
1265                         case 0:
1266                                 nn = 16;
1267                                 break;
1268                         case 1:
1269                                 nn = 8;
1270                                 break;
1271                         case 2:
1272                                 nn = 4;
1273                                 break;
1274                         default:
1275                                 nn = 2;
1276                                 break;
1277                         }
1278                         /* multiply by two for each additional slash beyond
1279                          * the first. We shouldn't really need this IF, since
1280                          * if should be okay to shift by 0, but for some reason,
1281                          * on my system, if slash_alt is 1 and the optimizer
1282                          * is run on this code, the following "if (n == 0)"
1283                          * statement doesn't work right. It works fine if
1284                          * the optimizer isn't run! (YIKES!!!!) */
1285                         if (grpsyl_p->slash_alt >  1) {
1286                                 nn = nn << (grpsyl_p->slash_alt - 1);
1287                         }
1288
1289                         if (nn == 0) {
1290                                 /* shifted left into oblivion */
1291                                 yyerror("too many slashes");
1292                         }
1293                         /* We are okay as long as the number of dots is no
1294                          * more than the number of zero bits on the right end
1295                          * of the number. If there are more, user is trying
1296                          * to subdivide too much, so disallow */
1297                         else if (drmo(nn) < grpsyl_p->dots) {
1298                                 yyerror("illegal number of slashes");
1299                         }
1300
1301                         if (grpsyl_p->beamto != CS_SAME) {
1302                                 l_yyerror(fname, lineno,
1303                                         "'slash' not allowed on cross-staff beams");
1304                         }
1305                 }
1306
1307                 /* check for unclosed custom beams */
1308                 if (grpsyl_p->next == (struct GRPSYL *) 0 &&
1309                                 (grpsyl_p->beamloc == STARTITEM ||
1310                                 grpsyl_p->beamloc == INITEM) ) {
1311                         yyerror("missing ebm");
1312                 }
1313
1314                 if (grpsyl_p->is_meas == YES && (grpsyl_p->prev != 0 ||
1315                                                 grpsyl_p->next != 0) ) {
1316                         yyerror("measure duration item must be the only thing in the measure");
1317                 }
1318
1319                 /* add up the total time in the measure */
1320                 total_time = radd(total_time, grpsyl_p->fulltime);
1321         }
1322
1323         /* check for not equal to time signature. */
1324         if ( NE(total_time, Score.time) && (grpsyllist_p->is_meas == NO
1325                         || grpsyllist_p->next != 0) ) {
1326                 l_yyerror(fname, lineno,
1327                         "time in measure (%ld/%ld) does not add up to time signature",
1328                         total_time.n, total_time.d);
1329         }
1330
1331         /* set up any beaming */
1332         if (has_cust_beaming(grpsyllist_p) == NO) {
1333                 do_beaming(grpsyllist_p, GS_NORMAL, staffno, vno);
1334                 do_beaming(grpsyllist_p, GS_SMALL, staffno, vno);
1335         }
1336         set_alt_beams(grpsyllist_p);
1337
1338         for (grpsyl_p = grpsyllist_p; grpsyl_p != (struct GRPSYL *) 0;
1339                                                 grpsyl_p = grpsyl_p->next) {
1340                 /* We only allow specifying slope for beam on first group
1341                  * of a beam. To be very certain of avoiding any possiblity
1342                  * of floating point roundoff errors,
1343                  * we compare for being close to NOBEAMANGLE.
1344                  */
1345                 if (fabs(grpsyl_p->beamslope - NOBEAMANGLE) > 0.01) {
1346                         if (grpsyl_p->beamloc != STARTITEM) {
1347                                 l_yyerror(fname, lineno,
1348                                         "slope can only be specified on the first chord of a beam");
1349                         }
1350                         else if (grpsyl_p->grpcont == GC_SPACE) {
1351                                 if (grpsyl_p->beamto != CS_SAME) {
1352                                         l_yyerror(fname, lineno,
1353                                         "on cross-staff beam, specify slope on staff with notes, not with space");
1354                                 }
1355                                 else {
1356                                         /* Don't think it should really be
1357                                          * possible to get here... */
1358                                         l_yyerror(fname, lineno,
1359                                         "slope can't be specified on space");
1360                                 }
1361                         }
1362                 }
1363         }
1364 }
1365 \f
1366
1367 /* make sure all the syllable times add up to time signature */
1368
1369 static void
1370 chk_a_syl_list(grpsyl_p, fname, lineno)
1371
1372 struct GRPSYL *grpsyl_p;        /* list to check */
1373 char *fname;                    /* file name for error messages */
1374 int lineno;                     /* input line for error messages */
1375
1376 {
1377         RATIONAL total_time;
1378
1379
1380         total_time = Zero;
1381
1382         for (   ; grpsyl_p != (struct GRPSYL *) 0; grpsyl_p = grpsyl_p->next) {
1383                 total_time = radd(total_time, grpsyl_p->fulltime);
1384         }
1385
1386         /* check for not equal to time signature. */
1387         if ( NE(total_time, Score.time) ) {
1388                 l_yyerror(fname, lineno,
1389                         "time in measure (%ld/%ld) does not add up to time signature",
1390                         total_time.n, total_time.d);
1391         }
1392 }
1393 \f
1394
1395 /* resolve all the relative octaves to absolute octaves.
1396  * Here's the deal: if the relative octave is negative,
1397  * add (the negative value) to the default to get the
1398  * real octave. If the relative is >= USE_DFLT_OCTAVE,
1399  * add (octave - USE_DFLT_OCT) to the default to get the real
1400  * value. Otherwise the relative is the absolute.  */
1401
1402 static void
1403 resolve_octaves(grpsyl_item_p, default_octave)
1404
1405 struct GRPSYL *grpsyl_item_p;   /* GRPSYL containing notes to resolve */
1406 int default_octave;             /* default octave to use */
1407
1408 {
1409         register int n;         /* walk through note list */
1410         register int sl;        /* walk through slurtolist */
1411         int octave;
1412         struct NOTE *note_p;    /* current note */
1413
1414
1415         for (n = 0; n < grpsyl_item_p->nnotes; n++) {
1416
1417                 note_p = &(grpsyl_item_p->notelist[n]);
1418                 octave = note_p->octave;
1419
1420                 if (octave < 0) {
1421                         /* add (the negative value) to default octave */
1422                         note_p->octave = (short)(default_octave + octave);
1423                 }
1424                 else if (octave >= USE_DFLT_OCTAVE) {
1425                         /* this means 0 or more to add to default octave */
1426                         note_p->octave = (short)(default_octave + octave
1427                                                         - USE_DFLT_OCTAVE);
1428                 }
1429                 else if (octave > MAXOCTAVE) {
1430                         l_yyerror(grpsyl_item_p->inputfile,
1431                                 grpsyl_item_p->inputlineno,
1432                                 "octave %d out of range (%d-%d)",
1433                                 octave, MINOCTAVE, MAXOCTAVE);
1434                 }
1435
1436                 /* also adjust any slurto */
1437                 if (note_p->nslurto > 0) {
1438                         for (sl = note_p->nslurto - 1; sl >= 0; sl--) {
1439                                 octave = note_p->slurtolist[sl].octave;
1440                                 if (octave < 0) {
1441                                         note_p->slurtolist[sl].octave =
1442                                                         default_octave + octave;
1443                                 }
1444                                 else if (octave >= USE_DFLT_OCTAVE) {
1445                                         note_p->slurtolist[sl].octave =
1446                                                         default_octave + octave
1447                                                         - USE_DFLT_OCTAVE;
1448                                 }
1449                         }
1450                 }
1451         }
1452 }
1453 \f
1454
1455 /* given a fulltime value, figure out what its basictime value was.
1456  * Here's the deal: First we find the power of 2 that is less than or
1457  * equal to the numerator of fulltime. Then we take that number over the
1458  * denominator of fulltime and reduce to lowest terms. The new denominator
1459  * is the basictime. Note that this won't work for tuplets, but we don't
1460  * call this for tuplets, so we're safe... */
1461
1462 int
1463 reconstruct_basictime(fulltime)
1464
1465 RATIONAL fulltime;      /* find the basic time of this */
1466
1467 {
1468         RATIONAL newtime;       /* RATIONAL of basictime */
1469
1470
1471         if (fulltime.n < 0) {
1472                 pfatal("negative fulltime numerator");
1473         }
1474
1475         /* guess MAXBASICTIME and work down by powers of 2
1476          * till we get <= fulltime numerator */
1477         for (newtime.n = MAXBASICTIME; newtime.n > fulltime.n; newtime.n >>= 1) {
1478                 ;
1479         }
1480         newtime.d = fulltime.d;
1481         rred( &newtime );
1482
1483         /* special case -- double whole basictime is 0 */
1484         if (EQ(newtime, Two)) {
1485                 return(0);
1486         }
1487         /* special case -- quad whole basictime is -1 */
1488         else if (EQ(newtime, Four)) {
1489                 return(-1);
1490         }
1491         else {
1492                 return ( (int) newtime.d);
1493         }
1494 }
1495 \f
1496
1497 /* Given a fulltime and basictime,
1498  * figure out how many dots there must have been.
1499  * Start with basictime as a RATIONAL. If equal to fulltime, fine, we are
1500  * done. Otherwise, keep adding 50% of previous value until it is equal.
1501  * The number of times we have to do this is the number of dots */
1502
1503 int
1504 recalc_dots(fulltime, basictime)
1505
1506 RATIONAL fulltime;
1507 int basictime;
1508
1509 {
1510         RATIONAL rat_basictime;
1511         RATIONAL halftime;      /* half of previous note or dot */
1512         int dots;
1513
1514         if (basictime == 0) {
1515                 /* special case of double whole */
1516                 rat_basictime.n = 2;
1517                 rat_basictime.d = 1;
1518         }
1519         else if (basictime == -1) {
1520                 /* special case of quad whole */
1521                 rat_basictime.n = 4;
1522                 rat_basictime.d = 1;
1523         }
1524         else {
1525                 rat_basictime.n = 1;
1526                 rat_basictime.d = basictime;
1527         }
1528
1529         halftime = rmul(rat_basictime, One_half);
1530         for (dots = 0; LT(rat_basictime, fulltime); dots++) {
1531                 rat_basictime = radd(rat_basictime, halftime);
1532                 halftime = rmul(halftime, One_half);
1533         }
1534         return(dots);
1535 }
1536 \f
1537
1538 /* make sure all the GRPSYL fields for a grace group are valid */
1539
1540 static void
1541 check_grace(grpsyl_item_p, fname, lineno)
1542
1543 struct GRPSYL *grpsyl_item_p;   /* which grace group to check */
1544 char *fname;                    /* file name for error messages */
1545 int lineno;                     /* in input, for error messages */
1546
1547 {
1548         int n;          /* index through notelist  */
1549
1550         if (grpsyl_item_p->dots != 0) {
1551                 l_yyerror(fname, lineno, "can't have dots on grace notes");
1552         }
1553
1554         else if (grpsyl_item_p->slash_alt == 1) {
1555                 if ( (grpsyl_item_p->prev != (struct GRPSYL *) 0
1556                                 && grpsyl_item_p->prev->grpvalue == GV_ZERO)
1557                                 || (grpsyl_item_p->next != (struct GRPSYL *) 0
1558                                 && grpsyl_item_p->next->grpvalue == GV_ZERO)) {
1559                         l_yyerror(fname, lineno,
1560                                 "slash only allowed on single grace note");
1561                 }
1562         }
1563
1564         else if (grpsyl_item_p->slash_alt > 1) {
1565                 l_yyerror(fname, lineno, "only 1 slash allowed on grace note");
1566         }
1567
1568         if (grpsyl_item_p->basictime < 4) {
1569                 l_yyerror(fname, lineno,
1570                                 "grace note must be quarter note or shorter");
1571         }
1572
1573         /* can't mix quarter grace notes (which don't have stems) with
1574          * shorter grace notes (that do have stems and get beamed) */
1575         if (grpsyl_item_p->basictime == 4 &&
1576                         ((grpsyl_item_p->prev != (struct GRPSYL *) 0
1577                         && grpsyl_item_p->prev->grpvalue == GV_ZERO
1578                         && grpsyl_item_p->prev->basictime > 4) ||
1579                         (grpsyl_item_p->next != (struct GRPSYL *) 0
1580                         && grpsyl_item_p->next->grpvalue == GV_ZERO
1581                         && grpsyl_item_p->next->basictime > 4))) {
1582                 l_yyerror(fname, lineno,
1583                                 "can't mix quarter and shorter grace notes");
1584                 /* set to shorter to avoid any future error messages */
1585                 grpsyl_item_p->basictime = 8;
1586         }
1587
1588         if (is_tab_staff(grpsyl_item_p->staffno) == YES) {
1589                 for (n = 0; n < grpsyl_item_p->nnotes; n++) {
1590                         if (HASBEND(grpsyl_item_p->notelist[n])) {
1591                                 l_yyerror(fname, lineno,
1592                                         "can't have bend on grace note");
1593                                 break;
1594                         }
1595                 }
1596         }
1597         else if (grpsyl_item_p->prev != (struct GRPSYL *) 0) {
1598                 
1599                 for (n = 0; n < grpsyl_item_p->prev->nnotes; n++) {
1600                         if (grpsyl_item_p->prev->notelist[n].is_bend) {
1601                                 l_yyerror(fname, lineno,
1602                                         "can't have bend on grace note");
1603                                 break;
1604                         }
1605                 }
1606         }
1607
1608
1609         if (grpsyl_item_p->next == (struct GRPSYL *) 0) {
1610                 l_yyerror(fname, lineno,
1611                                 "grace note cannot be last thing in measure");
1612         }
1613
1614         if (grpsyl_item_p->beamloc == STARTITEM) {
1615                 l_yyerror(fname, lineno,
1616                                 "custom beaming not allowed on grace notes");
1617         }
1618
1619         if (grpsyl_item_p->stemdir != UNKNOWN) {
1620                 l_warning(fname, lineno,
1621                                 "stem direction not allowed on grace notes");
1622         }
1623
1624         /* Only first and last in a series of grace are allowed
1625          * to have stem length specified, and then both have to be.
1626          * If this one is first of a series, do the check.
1627          */
1628         if ( (grpsyl_item_p->prev == 0
1629                         || grpsyl_item_p->prev->grpvalue != GV_ZERO)
1630                         && grpsyl_item_p->next != 0
1631                         && grpsyl_item_p->next->grpvalue == GV_ZERO) {
1632                 struct GRPSYL *gs_p;
1633                 for (gs_p = grpsyl_item_p->next;
1634                                         gs_p->next != 0 &&
1635                                         gs_p->next->grpvalue == GV_ZERO;
1636                                         gs_p = gs_p->next) {
1637                         if (gs_p->stemlen != STEMLEN_UNKNOWN) {
1638                                 l_warning(fname, lineno,
1639                                         "stem length cannot be specified in the middle of a series of grace notes");
1640                                 gs_p->stemlen = STEMLEN_UNKNOWN;
1641                         }
1642                 }
1643                 if ( (gs_p->stemlen == STEMLEN_UNKNOWN) !=
1644                                 (grpsyl_item_p->stemlen == STEMLEN_UNKNOWN) ) {
1645                         l_warning(fname, lineno,
1646                                 "stem length must be specified on both first and last grace or on neither");
1647                         gs_p->stemlen = STEMLEN_UNKNOWN;
1648                         grpsyl_item_p->stemlen = STEMLEN_UNKNOWN;
1649                 }
1650         }
1651 }
1652 \f
1653
1654 /* given a GRPSYL struct, fill in the font and size for all the
1655  * strings in the with list */
1656
1657 static void
1658 fix_strlist(gs_p, font, size, fname, lineno)
1659
1660 struct GRPSYL *gs_p;    /* fix all with lists in this list of GRPSYLS */
1661 int font;               /* set their default font to this */
1662 int size;               /* set their default size to this */
1663 char *fname;            /* file name for error messages */
1664 int lineno;             /* input line number for error messages */
1665
1666 {
1667         register int n;
1668
1669
1670         if (gs_p->grpcont != GC_NOTES && gs_p->nwith > 0) {
1671                 l_warning(fname, lineno,
1672                                 " 'with' items on rest or space ignored");
1673                 gs_p->nwith = 0;
1674                 return;
1675         }
1676
1677         for (n = 0; n < gs_p->nwith; n++) {
1678                 (void) fix_string(gs_p->withlist[n], font, size,
1679                                                         fname, lineno);
1680         }
1681 }
1682 \f
1683
1684 /* when a tuplet begins, mark tuplet state accordingly, and keep track to
1685  * mark subsequent notes till the end of the tuplet. */
1686
1687 void
1688 begin_tuplet()
1689
1690 {
1691         if ((Tuplet_state == INITEM) || (Tuplet_state == STARTITEM)) {
1692                 yyerror("nested tuplets not allowed");
1693                 return;
1694         }
1695
1696         /* just remember we are starting a tuplet. When we actual get
1697          * a note, we will mark it appropriately (in link_notegroup() ) */
1698         Tuplet_state = STARTITEM;
1699 }
1700 \f
1701
1702 /* once we reach the end of a tuplet, adjust all the time values */
1703
1704 void
1705 end_tuplet(tupcont, tuptime, printtup, tupside)
1706
1707 int tupcont;            /* number to print on top of tuplet */
1708 RATIONAL tuptime;       /* what time value to stuff tuplet into. If 0,
1709                          * then use the next lower power of 2 of tupcont */
1710 int printtup;           /* one of the PT_* values, specifying whether to print
1711                          * number and bracket */
1712 int tupside;            /* side at which to print number/bracket */
1713
1714 {
1715         RATIONAL mult_factor;   /* how much to adjust due to tuplet */
1716         int num_non_grace = 0;  /* how many non-grace notes in tuplet */
1717         int factor;             /* numerator of mult_factor */
1718         RATIONAL tot_time;      /* total time taken by tuplet by adding up
1719                                  * the un-adjusted times. */
1720         struct GRPSYL *gs_p;    /* walk through tuplet list */
1721
1722
1723
1724         /* future notes that come in are not in this tuplet */
1725         Tuplet_state = NOITEM;
1726
1727         if (tupcont <= 1) {
1728                 yyerror("tuplet number must be greater than 1");
1729                 return;
1730         }
1731
1732         if ( (printtup == PT_NEITHER) && (tupside != PL_UNKNOWN) ) {
1733                 l_warning(Curr_filename, yylineno,
1734                         "when tuplet number is not printed, tuplet side is ignored");
1735         }
1736
1737         if (EQ(tuptime, Zero)) {
1738                 /* this means user didn't specify, so we have to use
1739                  * the next lower power of two */
1740                 if ( (tupcont & (tupcont - 1)) == 0) {
1741                         yyerror("if tuplet number is a power of 2, tuplet time must be given as well");
1742                         return;
1743                 }
1744
1745                 /* guess MAXBASICTIME, then keep taking the next
1746                  * lower power of 2  until we get under the tupcont:
1747                  * that's the right value to use */
1748                 for (factor = MAXBASICTIME; factor > tupcont; factor >>= 1) {
1749                         ;
1750                 }
1751                 /* determine what to multiple each fulltime by */
1752                 mult_factor.n = factor;
1753                 mult_factor.d = tupcont;
1754                 rred(&mult_factor);
1755         }
1756         else {
1757                 /* figure out adjustment based on user-specified amount of
1758                  * time the tuplet is supposed to take */
1759                 tot_time = Zero;
1760                 for (gs_p = Tuplet_list_p; gs_p != (struct GRPSYL *) 0;
1761                                                 gs_p = gs_p->next) {
1762                         if (gs_p->grpvalue != GV_ZERO) {
1763                                 tot_time = radd(tot_time, gs_p->fulltime);
1764                         }
1765                 }
1766                 rred(&tuptime);
1767                 rred(&tot_time);
1768                 mult_factor = rdiv(tuptime, tot_time);
1769                 rred(&mult_factor);
1770
1771                 /* if factor is <= 1/2 or >= 2, the user must have specified
1772                  * the wrong note lengths */
1773                 if (LE(mult_factor, One_half) || GE(mult_factor, Two)) {
1774                         yyerror("illegal tuplet: values couldn't add up to specified tuplet time value");
1775                 }
1776         }
1777
1778         /* go through the list, filling in tupcont, tuptime, and adjusting
1779          * fulltime of each note */
1780         for (  ; Tuplet_list_p != (struct GRPSYL *) 0;
1781                                         Tuplet_list_p = Tuplet_list_p->next) {
1782
1783                 Tuplet_list_p->tupcont = (short) tupcont;
1784                 Tuplet_list_p->printtup = printtup;
1785                 Tuplet_list_p->tupside = tupside;
1786
1787                 if (Tuplet_list_p->grpvalue != GV_ZERO) {
1788                         num_non_grace++;
1789                         if (Tuplet_list_p->next != (struct GRPSYL *) 0 ||
1790                                         num_non_grace >= 1) {
1791                                 /* adjust fulltime */
1792                                 Tuplet_list_p->fulltime =
1793                                                 rmul(Tuplet_list_p->fulltime,
1794                                                 mult_factor);
1795                         }
1796                 }
1797                 else if (num_non_grace == 0) {
1798                         /* don't include leading grace notes in tuplet */
1799                         Tuplet_list_p->tuploc = NOITEM;
1800                         Tuplet_list_p->next->tuploc = STARTITEM;
1801                 }
1802
1803                 /* if this is the end of the list, mark end of tuplet */
1804                 if (Tuplet_list_p->next == (struct GRPSYL *) 0) {
1805
1806                         if (Tuplet_list_p->grpvalue == GV_ZERO) {
1807                                 yyerror("can't end tuplet with grace note");
1808                         }
1809
1810                         if(num_non_grace == 1) {
1811                                 Tuplet_list_p->tuploc = LONEITEM;
1812                                 break;
1813                         }
1814                         else {
1815                                 Tuplet_list_p->tuploc = ENDITEM;
1816                         }
1817                 }
1818         }
1819 }
1820 \f
1821
1822 /* check that all voices are actually present in a measure, add a measure
1823  * space for any that are missing. Also make sure any measure repeats
1824  * are correct: if more than one voice and one is a measure repeat,
1825  * either all must be measure repeats, or those that aren't must be
1826  * measure spaces, in which case the spaces are turned into measure repeats.
1827  * Also fill in the mrptnum field of measure repeat STAFF
1828  * to be the count of repeated measures.
1829  */
1830
1831 static void
1832 check4missing_voices(list_p)
1833
1834 struct MAINLL *list_p;  /* list of STAFFs */
1835
1836 {
1837         register int s;         /* walk through staff list */
1838         int v;                  /* index through voices */
1839         int numvoices;          /* how many voices on current staff */
1840
1841
1842         debug(4, "check4missing_voices");
1843
1844         if (list_p == (struct MAINLL *) 0) {
1845                 pfatal("null list passed to check4missing_voices()");
1846         }
1847
1848         /* check each staff */
1849         for ( s = 1; s <= Score.staffs; s++) {
1850
1851                 if (list_p->str != S_STAFF || list_p->u.staff_p->staffno != s) {
1852                         if (Errorcount > 0) {
1853                                 /* if the input is garbage, we might get called
1854                                  * with strange things in the main list. We
1855                                  * would have already exclaimed about
1856                                  * the errors, so don't bother to try to
1857                                  * deal with the mess, just return */
1858                                 return;
1859                         }
1860                         else {
1861                                 /* if input was good, we shouldn't get here */
1862                                 pfatal("info about staff %d not in list", s);
1863                         }
1864                 }
1865
1866                 /* insert space for any missing voices */
1867                 numvoices = vscheme_voices(svpath(s, VSCHEME)->vscheme);
1868                 for (v = 0; v < numvoices; v++) {
1869                         if (list_p->u.staff_p->groups_p[v]
1870                                                 == (struct GRPSYL *) 0) {
1871                                 add_meas_space( &(list_p->u.staff_p->groups_p[v]),
1872                                                         s, v + 1);
1873                         }
1874                 }
1875
1876                 /* fix up any measure repeats */
1877                 fix_mrpt(list_p);
1878
1879                 if (list_p->next != (struct MAINLL *) 0) {
1880                         list_p = list_p->next;
1881                 }
1882         }
1883 }
1884 \f
1885
1886 /* fill in measure space for empty voice */
1887
1888 void
1889 add_meas_space(gs_p_p, staff, voice)
1890
1891 struct GRPSYL **gs_p_p; /* where to put space */
1892 int staff;
1893 int voice;
1894
1895 {
1896         struct GRPSYL *grpsyl_p;
1897
1898
1899         grpsyl_p = newGRPSYL(GS_GROUP);
1900         *gs_p_p = grpsyl_p;
1901
1902         grpsyl_p->staffno = (short) staff;
1903         grpsyl_p->vno = (short) voice;
1904         grpsyl_p->basictime = -1;
1905         grpsyl_p->is_meas = YES;
1906
1907         /* We are creating a line that doesn't appear in the user's input.
1908          * However, if we make it an anonymous line, if this grpsyl ever
1909          * gets referenced to print an error message (which it might--
1910          * like if user tries to tie something into this generated measure
1911          * space) the user will have no clue as to where the problem is in
1912          * their input. So put in the current file/line which will point to
1913          * the bar line at the end of the measure, which will at least point
1914          * the user to the right general area. */
1915         grpsyl_p->inputfile = Curr_filename;
1916         grpsyl_p->inputlineno = (short) yylineno;
1917         grpsyl_p->grpcont = GC_SPACE;
1918
1919         /* in another place, one compiler didn't fill in properly
1920          * when I assigned Score.time directly in one step, so to
1921          * take no chances, do numerator and denominator separately */
1922         grpsyl_p->fulltime.n = Score.time.n;
1923         grpsyl_p->fulltime.d = Score.time.d;
1924 }
1925 \f
1926 /* given a MAINLL that points to a STAFF, check if that staff is a measure
1927  * repeat, and if so, validate and normalize it
1928  */
1929
1930 static void
1931 fix_mrpt(mll_p)
1932
1933 struct MAINLL *mll_p;   /* points to a STAFF */
1934
1935 {
1936         struct GRPSYL *gs_voice_p[MAXVOICES];   /* groups for each voice */
1937         struct GRPSYL *prevgrp_p;
1938         struct STAFF *staff_p;
1939         struct MAINLL *prevmll_p;               /* measure being repeated */
1940         int voice_is_mrpt[MAXVOICES];           /* boolean for each voice */
1941         int found_mrpt;
1942         int found_non_mrpt;                     /* something other than mrpt or ms */
1943         int staff;
1944         int v;
1945
1946
1947         /* find out if any voice is a measure repeat */
1948         found_mrpt = NO;
1949         found_non_mrpt = NO;
1950         for (v = 0; v < MAXVOICES; v++) {
1951                 gs_voice_p[v] = mll_p->u.staff_p->groups_p[v];
1952                 voice_is_mrpt[v] = is_mrpt(gs_voice_p[v]);
1953                 if (voice_is_mrpt[v] == YES) {
1954                         found_mrpt = YES;
1955                 }
1956                 else if (is_ms(gs_voice_p[v]) == NO) {
1957                         found_non_mrpt = YES;
1958                 }
1959         }
1960
1961         /* if none are a measure repeat, we are done */
1962         if (found_mrpt == NO) {
1963                 return;
1964         }
1965
1966         /* At least one must be a meas repeat. If the others aren't
1967          * either also a measure repeat or a measure space, user error. */
1968         if (found_non_mrpt == YES) {
1969                 l_yyerror(gs_voice_p[0]->inputfile, gs_voice_p[0]->inputlineno,
1970                         "if one voice is mrpt, other voices cannot contain notes or rests");
1971                 return;
1972         }
1973
1974         staff_p = mll_p->u.staff_p;
1975         staff = staff_p->staffno;
1976
1977         /* At this point, we have at least one voice that is mrpt, and
1978          * any other voices that exist are either mrpt or ms.
1979          * Find the previous measure to find out how many measures of
1980          * mrpt we have in a row, to fill in the mrptnum field.
1981          * But before calling prevgrpsyl(), need to make sure the staffno and
1982          * vno are filled in on the GRSPYL we pass to it, since we're so early
1983          * in parsing that hasn't happened yet, but prevgrpsyl() needs them.
1984          * We use the GRPSYL for voice 1, since we know that always exists.
1985          */
1986         gs_voice_p[0]->staffno = staff;
1987         gs_voice_p[0]->vno = 1;
1988         prevmll_p = mll_p;
1989         if ((prevgrp_p = prevgrpsyl(gs_voice_p[0], &prevmll_p)) == (struct GRPSYL *) 0) {
1990                 /* We fell off the top of the main list */
1991                 l_yyerror(gs_voice_p[0]->inputfile, gs_voice_p[0]->inputlineno,
1992                                 "mrpt cannot be used on the first measure");
1993
1994                 /* Force to measure space, so that if there are subsequent
1995                  * mrpt measures, they don't give more errors; no reason
1996                  * to complain more than once */
1997                 for (v = 0; v < MAXVOICES; v++) {
1998                         if (gs_voice_p[v] != (struct GRPSYL *) 0) {
1999                                 gs_voice_p[v]->grpcont = GC_SPACE;
2000                         }
2001                 }
2002                 return;
2003         }
2004
2005         /* Having a mrpt after a multirest doesn't make sense. At best,
2006          * it is ambiguous: should we repeat the multirest or the last
2007          * measure of rest? If the former, we have big problems if one
2008          * staff has a mrpt and another doesn't. If it's the latter, why
2009          * doesn't the user just use mr? That would be smaller and clearer. */
2010         if (prevgrp_p->basictime < -1) {
2011                 l_yyerror(gs_voice_p[0]->inputfile, gs_voice_p[0]->inputlineno,
2012                                 "mrpt cannot be used in measure after multirest");
2013                 for (v = 0; v < MAXVOICES; v++) {
2014                         if (gs_voice_p[v] != (struct GRPSYL *) 0) {
2015                                 gs_voice_p[v]->grpcont = GC_SPACE;
2016                         }
2017                 }
2018                 return;
2019         }
2020
2021         if (is_mrpt(prevgrp_p) == YES) {
2022                 /* Our current one must be numbered
2023                  * one more than the previous one */
2024                 staff_p->mrptnum = prevmll_p->u.staff_p->mrptnum + 1;
2025         }
2026         else {
2027                 /* if this happens to be the first mrpt, it is numbered "2" */
2028                 staff_p->mrptnum = 2;
2029         }
2030
2031         /* Certain parameter changes are not allowed in the middle of
2032          * runs of measure repeats because they cause the contents of 
2033          * the measure to be too different to make sense. These
2034          * include changes in time signature, defoct, transpose.
2035          * So back up from the mrpt measure to its preceeding measure,
2036          * checking for any SSVs that might have something illegal in them.
2037          */
2038         for (mll_p = mll_p->prev; mll_p != prevmll_p; mll_p = mll_p->prev) {
2039
2040                 if (mll_p->str == S_SSV) {
2041                         if (chk_mrpt_ssv_interactions(mll_p->u.ssv_p,
2042                                                 gs_voice_p[0], staff) == NO) {
2043                                 /* Force to measure space, so that if
2044                                  * there are subsequent mrpt measures,
2045                                  * they don't give more errors; no reason
2046                                  * to complain more than once */
2047                                 for (v = 0; v < MAXVOICES; v++) {
2048                                         if (gs_voice_p[v] != (struct GRPSYL *) 0) {
2049                                                 gs_voice_p[v]->grpcont = GC_SPACE;
2050                                         }
2051                                 }
2052                                 return;
2053                         }
2054                 }
2055         }
2056
2057         /* If doing MIDI, expand the measure repeat to what it is repeating */
2058         if (Doing_MIDI == YES) {
2059                 for (v = 0; v < MAXVOICES; v++) {
2060                         struct GRPSYL *g_p;
2061
2062                         free_grpsyls(staff_p->groups_p[v]);
2063                         /* Note that the notelist does need to be copied,
2064                          * (arg 2 == YES) because even though coords
2065                          * are not used in MIDI, vcombine code may delete
2066                          * the list, so it cannot be shared across groups.
2067                          */
2068                         staff_p->groups_p[v] = clone_gs_list(
2069                                         prevmll_p->u.staff_p->groups_p[v], YES);
2070                         /* The cloned list may already have breakbeam set,
2071                          * which will confuse has_cust_beaming(). For MIDI
2072                          * purposes, we don't care about beams anyway,
2073                          * much less subbeams, and breakbeam will get set
2074                          * properly later anyway on these groups,
2075                          * so just set to NO here. */
2076                         for (g_p = staff_p->groups_p[v]; g_p != 0; g_p = g_p->next) {
2077                                 g_p->breakbeam = NO;
2078                         }
2079                 }
2080         }
2081         else {
2082                 /* We force all to GC_NOTES to make sure they are all mrpt,
2083                  * even if some were originally ms. */
2084                 for (v = 0; v < MAXVOICES; v++) {
2085                         if (gs_voice_p[v] != (struct GRPSYL *) 0) {
2086                                 gs_voice_p[v]->grpcont = GC_NOTES;
2087                         }
2088                 }
2089         }
2090 }
2091 \f
2092
2093 /* given an SSV found while searching up the main list for a measure repeat
2094  * defining measure, figure out whether having a measure repeat is valid or
2095  * not. Some SSV changes alter things so much that a measure repeat no longer
2096  * makes sense, so we block it. Return YES if all seems well, NO if not.
2097  */
2098
2099 /* Here is the list of things we do NOT allow user to change between a
2100  * mrpt and the (earlier) measure that defines it */
2101 static struct Disallowed_ssv_fields {
2102         int     field;          /* index of the field in SSV "used" array */
2103         char *  name;           /* to use in error message */
2104 } mrpt_ssv_list[] = {
2105         { TIME,                 "time" },
2106         { DEFOCT,               "defoct" },
2107         { CLEF,                 "clef" },
2108         { TRANSPOSITION,        "transpose" },
2109         { ADDTRANSPOSITION,     "addtranspose" },
2110         { NUMSTAFF,             "staffs" },
2111         { 0,                    (char *) 0 }    /* end the list */
2112 };
2113
2114 static int
2115 chk_mrpt_ssv_interactions(ssv_p, gs_p, staffno)
2116
2117 struct SSV *ssv_p;
2118 struct GRPSYL *gs_p;
2119 int staffno;
2120
2121 {
2122         int ret = YES;  /* return value after all checks */
2123         int err = NO;   /* did we find error this time through loop? */
2124         int field;      /* index into used array in SSV */
2125         int i;          /* loop through list of disallowed things */
2126
2127
2128         /* check all possible thing that we don't allow. Complain about
2129          * as many errors as we find. Some of these things only occur in
2130          * score or score/staff context, but it's easier to just make
2131          * the code very general; if the field isn't used, things will still
2132          * work just fine.
2133          */
2134         for (i = 0; mrpt_ssv_list[i].name != (char *) 0; i++) {
2135                 field = mrpt_ssv_list[i].field;
2136                 err = NO;
2137
2138                 switch (ssv_p->context) {
2139
2140                 case C_SCORE:
2141                         /* for things that can be overriden on staff
2142                          * and/or voice, the score one will only apply
2143                          * if there isn't an override in effect */
2144                         if (ssv_p->used[field] == YES) {
2145                                 if (Staff[staffno-1].used[field] == YES) {
2146                                         /* overridden by staff, so ignore */
2147                                         break;
2148                                 }
2149                                 if (voices_override(staffno, field) == YES) {
2150                                         /* overridden by all voices; ignore */
2151                                         break;
2152                                 }
2153                                 err = YES;
2154                         }
2155                         break;
2156
2157                 case C_STAFF:
2158                 case C_VOICE:
2159                         if (ssv_p->staffno != staffno) {
2160                                 /* This applies to some other staff. Since this
2161                                  * SSV is totally irrrelevant, we can return,
2162                                  * rather than loop through checking for other
2163                                  * possible errors. */
2164                                 return(YES);
2165                         }
2166                         if (ssv_p->used[field] == YES) {
2167                                 if (ssv_p->context == C_STAFF &&
2168                                                 voices_override(staffno, field) == YES) {
2169                                         /* overridden on all voices, so ignore */
2170                                         break;
2171                                 }
2172                                 err = YES;
2173                         }
2174                         break;
2175
2176                 default:
2177                         pfatal("bad context in mrpt/ssv check");
2178                         break;
2179                 }
2180
2181                 if (err == YES) {
2182                         /* this field change not allowed */
2183                         l_yyerror(gs_p->inputfile, gs_p->inputlineno,
2184                                 "can't use mrpt after %s change",
2185                                 mrpt_ssv_list[i].name);
2186                         ret = NO;
2187                 }
2188         }
2189
2190         return(ret);
2191 }
2192 \f
2193
2194 /* Returns YES if the given SSV field is overriden by all voices
2195  * on the given staff, NO if not. */
2196
2197 static int
2198 voices_override(staff, field)
2199
2200 int staff;      /* check this staff */
2201 int field;      /* check this field */
2202
2203 {
2204         int numvoices;
2205         int v;
2206
2207         numvoices = vscheme_voices(svpath(staff, VSCHEME)->vscheme);
2208         for (v = 0; v < numvoices; v++) {
2209                 if (Voice[staff - 1][v].used[field] == NO) {
2210                         /* this voice does not override */
2211                         return(NO);
2212                 }
2213         }
2214         return(YES);
2215 }
2216 \f
2217
2218 /* return YES if given GRPSYL represents a measure repeat */
2219
2220 int
2221 is_mrpt(gs_p)
2222
2223 struct GRPSYL *gs_p;
2224
2225 {
2226         return (gs_p != (struct GRPSYL *) 0 && gs_p->is_meas == YES 
2227                 && gs_p->grpcont == GC_NOTES && gs_p->nnotes == 0) ? YES : NO;
2228 }
2229
2230 /* return YES if given GRPSYL represents a measure space */
2231
2232 static int
2233 is_ms(gs_p)
2234 struct GRPSYL *gs_p;
2235
2236 {
2237         /* non-existent is like measure space */
2238         if (gs_p == (struct GRPSYL *) 0) {
2239                 return(YES);
2240         }
2241         return (gs_p->is_meas == YES && gs_p->grpcont == GC_SPACE) ? YES : NO;
2242 }
2243 \f
2244
2245 /* sort the notes to be top to bottom */
2246
2247 static void
2248 sort_notes(grpsyl_p, fname, lineno)
2249
2250 struct GRPSYL *grpsyl_p;        /* sort the notes off this GRPSYL */
2251 char *fname;                    /* file name for error messages */
2252 int lineno;                     /* input line number */
2253
2254 {
2255         register int n;
2256         struct NOTE *highest_p; /* note with highest pitch on a staff */
2257         struct NOTE *lowest_p;  /* note with lowest pitch on a staff */
2258         int othervis = -1;              /* staff number of adjacent visible staff */
2259
2260
2261         if (grpsyl_p->nnotes < 2) {
2262                 /* nothing to sort! */
2263                 return;
2264         }
2265
2266         /* If have cross-staff stemming, do extra error checks */
2267         if (grpsyl_p->stemto != CS_SAME) {
2268
2269                 if (Doing_tab_staff == YES) {
2270                         l_yyerror(grpsyl_p->inputfile, grpsyl_p->inputlineno,
2271                         "stemming with another staff is not allowed on a tablature staff");
2272                 }
2273                 else if (svpath(grpsyl_p->staffno, STAFFLINES)->stafflines != 5) {
2274                         l_yyerror(grpsyl_p->inputfile, grpsyl_p->inputlineno,
2275                         "stemming to another staff only allowed from a 5-line staff");
2276                 }
2277                 if (input_style(grpsyl_p->staffno, grpsyl_p->vno)
2278                                         == IS_CHORD_INPUT) {
2279                         l_yyerror(grpsyl_p->inputfile, grpsyl_p->inputlineno,
2280                         "stemming to another staff not allowed on chord-at-a-time input");
2281                 }
2282
2283                 if (grpsyl_p->stemto == CS_ABOVE) {
2284                         for (othervis = grpsyl_p->staffno - 1; othervis > 0; othervis--) {
2285                                 if (svpath(othervis, VISIBLE)->visible == YES) {
2286                                         if (svpath(grpsyl_p->staffno, STAFFLINES)->stafflines != 5
2287                                                     || is_tab_staff(othervis)) {
2288                                                 l_yyerror(grpsyl_p->inputfile, grpsyl_p->inputlineno,
2289                                                 "for stem with staff above, preceeding visible staff must be a normal 5-line staff");
2290                                         }
2291                                         break;
2292                                 }
2293                         }
2294                         if (othervis <= 0) {
2295                                 l_yyerror(grpsyl_p->inputfile, grpsyl_p->inputlineno,
2296                                 "can't stem with above from top visible staff");
2297                         }
2298                 }
2299                 else if (grpsyl_p->stemto == CS_BELOW) {
2300                         for (othervis = grpsyl_p->staffno + 1; othervis <= Score.staffs; othervis++) {
2301                                 if (svpath(othervis, VISIBLE)->visible == YES) {
2302                                         if (svpath(grpsyl_p->staffno, STAFFLINES)->stafflines != 5
2303                                                     || is_tab_staff(othervis)) {
2304                                                 l_yyerror(grpsyl_p->inputfile, grpsyl_p->inputlineno,
2305                                                 "for stem with staff below, following visible staff must be a normal 5-line staff");
2306                                         }
2307                                         break;
2308                                 }
2309                         }
2310                         if (othervis >= Score.staffs + 1) {
2311                                 l_yyerror(grpsyl_p->inputfile, grpsyl_p->inputlineno,
2312                                 "can't stem with below from bottom visible staff");
2313                         }
2314                 }
2315
2316                 /* The two staffs must have the same staffscale.
2317                  * Use floating point comparision for "close enough" */
2318                 if (othervis > 0 && othervis <= Score.staffs) {
2319                         if (fabs(svpath(othervis,STAFFSCALE)->staffscale -
2320                         svpath(grpsyl_p->staffno, STAFFSCALE)->staffscale) > 0.001) {
2321                                 l_yyerror(grpsyl_p->inputfile, grpsyl_p->inputlineno,
2322                                 "staff being stemed to must have same staffscale");
2323                         }
2324                 }
2325         }
2326
2327         /* If this group is stemmed to another staff,
2328          * we have to make sure the notes on the two staffs don't overlap,
2329          * since we can't handle duplicate notes,
2330          * and it's kind of silly anyway.
2331          * At this point, the stemto_idx will be at the first
2332          * "other staff" note. In the case of stem with above,
2333          * we need to adjust that to be at the last normal staff group,
2334          * because that is the convention we use. In any case, once
2335          * we verify there is no overlap, we can go ahead and sort the
2336          * group as usual, since we know that will end up sorting each
2337          * staff's notes properly.
2338          */
2339         if (grpsyl_p->stemto == CS_ABOVE) {
2340                 /* Find highest note on normal staff */
2341                 if (grpsyl_p->stemto_idx == 0) {
2342                         /* no notes at all on normal staff */
2343                         highest_p = 0;
2344                         n = 0;
2345                 }
2346                 else {
2347                         highest_p = &(grpsyl_p->notelist[0]);
2348                         for (n = 1; n < grpsyl_p->stemto_idx; n++) {
2349                                 if (notecomp(&(grpsyl_p->notelist[n]), highest_p) > 0) {
2350                                         highest_p = &(grpsyl_p->notelist[n]);
2351                                 }
2352                         }
2353                 }
2354                 /* Find lowest note on above staff */
2355                 if (grpsyl_p->stemto_idx == grpsyl_p->nnotes) {
2356                         /* Actually, we don't currently allow this case
2357                          * (blocked in parsing code),
2358                          * but if we ever do, this code should handle it...
2359                          */
2360                         lowest_p = 0;
2361                 }
2362                 else {
2363                         lowest_p = &(grpsyl_p->notelist[n]);
2364                         for ( ; n < grpsyl_p->nnotes; n++) {
2365                                 if (notecomp(&(grpsyl_p->notelist[n]), lowest_p) < 0) {
2366                                         lowest_p = &(grpsyl_p->notelist[n]);
2367                                 }
2368                         }
2369                 }
2370                 /* Make sure there is no overlap */
2371                 if (highest_p != 0 && lowest_p != 0 &&
2372                                         notecomp(highest_p, lowest_p) <= 0) {
2373                         l_yyerror(grpsyl_p->inputfile, grpsyl_p->inputlineno,
2374                         "the 'with above' notes are not allowed to overlap with notes on the normal staff");
2375                 }
2376
2377                 /* With CS_ABOVE, the index is supposed to point to the last on
2378                  * the normal, so adjust to do that. */
2379                 grpsyl_p->stemto_idx = grpsyl_p->nnotes - grpsyl_p->stemto_idx - 1;
2380         }
2381         else if (grpsyl_p->stemto == CS_BELOW) {
2382                 /* Do analogous for below */
2383                 /* Find lowest note on normal staff */
2384                 if (grpsyl_p->stemto_idx == 0) {
2385                         /* no notes at all on normal staff */
2386                         lowest_p = 0;
2387                         n = 0;
2388                 }
2389                 else {
2390                         lowest_p = &(grpsyl_p->notelist[0]);
2391                         for (n = 1; n < grpsyl_p->stemto_idx; n++) {
2392                                 if (notecomp(&(grpsyl_p->notelist[n]), lowest_p) < 0) {
2393                                         lowest_p = &(grpsyl_p->notelist[n]);
2394                                 }
2395                         }
2396                 }
2397                 /* Find highest note on below staff */
2398                 if (grpsyl_p->stemto_idx == grpsyl_p->nnotes) {
2399                         highest_p = 0;
2400                         n = 0;
2401                 }
2402                 else {
2403                         highest_p = &(grpsyl_p->notelist[n]);
2404                         for (n++ ; n < grpsyl_p->nnotes; n++) {
2405                                 if (notecomp(&(grpsyl_p->notelist[n]), highest_p) > 0) {
2406                                         highest_p = &(grpsyl_p->notelist[n]);
2407                                 }
2408                         }
2409                 }
2410                 /* Make sure there is no overlap */
2411                 if (highest_p != 0 && lowest_p != 0 &&
2412                                         notecomp(highest_p, lowest_p) <= 0) {
2413                         l_yyerror(grpsyl_p->inputfile, grpsyl_p->inputlineno,
2414                         "the 'with below' notes are not allowed to overlap with notes on the normal staff");
2415                 }
2416         }
2417
2418         /* sort top to bottom */
2419         qsort( (char *) grpsyl_p->notelist, (unsigned int) grpsyl_p->nnotes,
2420                                         sizeof (struct NOTE), notecomp);
2421
2422         /* now that they are sorted, check for duplicates. */
2423         for (n = 0; n < grpsyl_p->nnotes - 1; n++) {
2424                 if (notecomp(&(grpsyl_p->notelist[n]),
2425                                         &(grpsyl_p->notelist[n+1])) == 0) {
2426                         /* For voice-at-a-time, duplicate is an error.
2427                          * For chord-at-a-time, we merge them, except that
2428                          * for tablature, if the frets don't match we
2429                          * can't merge them. */
2430                         if (input_style(grpsyl_p->staffno, grpsyl_p->vno)
2431                                         == IS_VOICE_INPUT ||
2432                                         (Doing_tab_staff == YES &&
2433                                         grpsyl_p->notelist[n].FRETNO !=
2434                                         grpsyl_p->notelist[n+1].FRETNO)
2435                                         ) {
2436                                 if (Doing_tab_staff == YES) {
2437                                         struct STRINGINFO *strinfo_p;
2438
2439                                         strinfo_p = &(svpath(grpsyl_p->staffno,
2440                                                 STAFFLINES)->strinfo[ (int)
2441                                                 grpsyl_p->notelist[n].STRINGNO]);
2442                                         l_yyerror(fname, lineno,
2443                                                 "string %s occurred more than once in a single chord",
2444                                                 format_string_name(
2445                                                 strinfo_p->letter,
2446                                                 strinfo_p->accidental,
2447                                                 strinfo_p->nticks));
2448                                 }
2449                                 else {
2450                                         l_yyerror(fname, lineno,
2451                                                 "pitch %c%d occurred more than once in a single chord",
2452                                                 grpsyl_p->notelist[n].letter,
2453                                                 grpsyl_p->notelist[n].octave);
2454                                 }
2455                         }
2456                         else {
2457                                 merge_dup_notes(grpsyl_p, n);
2458                                 /* in case there was more than one duplicate,
2459                                  * arrange to check this same one again,
2460                                  * against the group that has taken the place
2461                                  * of the removed group (if any) */
2462                                 n--;
2463                         }
2464                 }
2465         }
2466 }
2467 \f
2468
2469 /* Compare notes. Return 1 if first is lower, -1 if higher, 0 if same.
2470  * For tablature, this just compares the strings; for non-tablature,
2471  * it just compares pitch and octave, not counting accidentals. */
2472
2473 int
2474 notecomp(item1_p, item2_p)
2475
2476 #ifdef __STDC__
2477 const void *item1_p;            /* the notes to compare */
2478 const void *item2_p;
2479 #else
2480 char *item1_p;          /* the notes to compare */
2481 char *item2_p;
2482 #endif
2483
2484 {
2485         struct NOTE *note1_p;
2486         struct NOTE *note2_p;
2487
2488         /* cast to proper type */
2489         note1_p = (struct NOTE *) item1_p;
2490         note2_p = (struct NOTE *) item2_p;
2491
2492         if (Doing_tab_staff == YES) {
2493                 /* for tablature, just need to compare the string numbers. */
2494                 if (note1_p->STRINGNO < note2_p->STRINGNO) {
2495                         return(-1);
2496                 }
2497                 else if (note1_p->STRINGNO > note2_p->STRINGNO) {
2498                         return(1);
2499                 }
2500                 else {
2501                         return(0);
2502                 }
2503         }
2504
2505         /* first compare octaves */
2506         if ( note1_p->octave < note2_p->octave) {
2507                 return(1);
2508         }
2509
2510         if (note1_p->octave > note2_p->octave) {
2511                 return(-1);
2512         }
2513         
2514         /* if same octaves, compare pitches */
2515         if (Letshift[note1_p->letter - 'a'] < Letshift[note2_p->letter - 'a']) {
2516                 return(1);
2517         }
2518
2519         if (Letshift[note1_p->letter - 'a'] > Letshift[note2_p->letter - 'a']) {
2520                 return(-1);
2521         }
2522
2523         return(0);
2524 }
2525 \f
2526
2527 /* Make sure data ends with some kind of bar line (which can optionally
2528  * be followed by blocks). Start at end of  main list and go backwards.
2529  * If we hit a S_BAR, then all is well.
2530  * If we hit a S_STAFF first or beginning of list, we have a problem.
2531  * Also does error checks on any FEED at the end of the piece.
2532  * It does a similar check on any FEED at the beginning. (It doesn't
2533  * really fit the name of the function, but keeps the checks together.)
2534  */
2535
2536 void
2537 check4barline_at_end()
2538
2539 {
2540         struct MAINLL *list_p;  /* walk through main list */
2541         int saw_block = NO;     /* if saw an S_BLOCKHEAD */
2542
2543
2544         /* first check for FEED at the beginning */
2545         for (list_p = Mainllhc_p; list_p != (struct MAINLL *) NULL;
2546                                 list_p = list_p->next) {
2547                 if (list_p->str == S_STAFF) {
2548                         /* no user-supplied feed at beginning */
2549                         break;
2550                 }
2551                 else if (list_p->str == S_FEED) {
2552                         /* rightmargin applies to previous feed,
2553                          * but there is no previous feed to the first score */
2554                         if (list_p->u.feed_p->rightmargin >= 0.0) {
2555                                 l_warning(list_p->inputfile, list_p->inputlineno,
2556                                         "rightmargin on newscore at beginning of piece is ignored");
2557                         }
2558                         /* Note: we probably ought to give a
2559                          * warning for pagefeed here and ignore it,
2560                          * to be like at the end of a piece.
2561                          * But this was inadvertently allowed in the past
2562                          * (it produces a blank page),
2563                          * so we leave it that way
2564                          * for backward compatibility.
2565                          */
2566                 }
2567         }
2568
2569         for (list_p = Mainlltc_p; list_p != (struct MAINLL *) NULL;
2570                                 list_p = list_p->prev) {
2571
2572                 if (list_p->str == S_BAR) {
2573                         /* found BAR before STAFF. Good. */
2574                         /* if user forgot to end an ending, do it for them */
2575                         if (list_p->u.bar_p->endingloc == INITEM) {
2576                                 list_p->u.bar_p->endingloc = ENDITEM;
2577                         }
2578                         else if (list_p->u.bar_p->endingloc == STARTITEM) {
2579                                 l_yyerror(list_p->inputfile, list_p->inputlineno,
2580                                         "can't begin ending on final bar line");
2581                         }
2582
2583                         if (list_p->u.bar_p->bartype == RESTART) {
2584                                 l_yyerror(list_p->inputfile, list_p->inputlineno,
2585                                         "final bar line cannot be a restart");
2586                         }
2587                         return;
2588                 }
2589
2590                 else if (list_p->str == S_STAFF) {
2591                         /* Oh-oh. No ending bar line */
2592                         yyerror("ending bar line missing");
2593                         return;
2594                 }
2595
2596                 else if (list_p->str == S_BLOCKHEAD) {
2597                         saw_block = YES;
2598                 }
2599                 else if (list_p->str == S_FEED) {
2600                         if (list_p->u.feed_p->leftmargin >= 0.0) {
2601                                 l_warning(list_p->inputfile, list_p->inputlineno,
2602                                         "leftmargin on newscore at end of piece is ignored");
2603                         }
2604                 }
2605         }
2606
2607         /* if we are here, we went all through the list */
2608         if (saw_block == NO) {
2609                 yyerror("no music data or blocks found in input");
2610         }
2611 }
2612 \f
2613
2614 /* when user wants a multi-rest, create a STAFF for each staff. Then for
2615  * each of them, fill in a GRPSYL with basictime as the negative of the
2616  * number of measure, and fulltime as the length of a measure. Attach this
2617  * GRPSYL as the only item off of voice 1, and to other voices as well,
2618  * if there are other voice and we are doing MIDI. */
2619
2620 void
2621 add_multirest(nummeas)
2622
2623 int nummeas;            /* how many measures in the multi-rest */
2624
2625 {
2626         register int s;         /*staff index */
2627         struct GRPSYL *new_p;   /* newly allocated GRPSYL for multirest */
2628         int v;                  /* voice index */
2629         int numvoices;
2630
2631
2632         /* if already have notes in this measure, user goofed */
2633         if (Got_group == YES) {
2634                 report_mix_error();
2635                 return;
2636         }
2637
2638         if (Got_multirest == 1) {
2639                 yyerror("can't have consecutive multirests (maybe missing bar?)");
2640                 return;
2641         }
2642
2643         /* we could allow up to 32767 and still fit in a short, but huge
2644          * numbers can lead to rational overflow in midi, and who in their
2645          * right mind would want that many measures of multirest anyway? */
2646         if (rangecheck(nummeas, 2, MAXMULTINUM, "multirest measures") == NO) {
2647                 return;
2648         }
2649
2650         /* create all the staffs and fill them in */
2651         create_staffs();
2652         for (s = 1; s <= Score.staffs; s++) {
2653                 numvoices = vscheme_voices(svpath(s, VSCHEME)->vscheme);
2654                 for (v = 0; v < numvoices; v++) {
2655                         if (v == 0 || Doing_MIDI == YES) {
2656                                 Staffmap_p[s]->u.staff_p->groups_p[v] = new_p
2657                                                 = newGRPSYL(GS_GROUP);
2658                                 new_p->grpcont = GC_REST;
2659                                 new_p->basictime = -nummeas;
2660                                 new_p->fulltime = Score.time;
2661                         }
2662                 }
2663         }
2664
2665         Got_multirest = 1;
2666         Got_some_data = YES;
2667
2668         /* update measure numbers for rehearsal mark and stuff use */
2669         Meas_num += nummeas;
2670         multi_stuff(nummeas);
2671 }
2672 \f
2673
2674 /* this function gets called when we discover user has mixed multi-rest and
2675  * music data in a single measure. If we've already reported this on the
2676  * current measure, Got_multirest will be 2, so we just return,
2677  * otherwise report it and set Got_multirest to 2 */
2678
2679 void
2680 report_mix_error()
2681
2682 {
2683         if (Got_multirest != 2) {
2684                 yyerror( "can't mix music data and multi-rest in same measure");
2685                 Got_multirest = 2;
2686         }
2687 }
2688 \f
2689
2690 /* Recursively free a list of GRPSYLs. If this is a GS_GROUP,
2691  * also free the withlist and notelist if they exist,
2692  * and anything hanging off the notelist. If this is a GS_SYLLABLE, anything
2693  * that is hanging of the grpsyl will NOT be freed.
2694  */
2695
2696 void
2697 free_grpsyls(gs_p)
2698
2699 struct GRPSYL *gs_p;            /* what to free */
2700
2701 {
2702         int n;                  /* to index through lists */
2703
2704         if (gs_p == (struct GRPSYL *) 0) {
2705                 /* end of recursion */
2706                 return;
2707         }
2708
2709         if (gs_p->grpsyl == GS_GROUP) {
2710                 free_notelist(gs_p);
2711
2712                 /* free the withlist */
2713                 for (n = 0; n < gs_p->nwith; n++) {
2714                         if (gs_p->withlist[n] != (char *) 0) {
2715                                 FREE(gs_p->withlist[n]);
2716                         }
2717                 }
2718                 if (gs_p->withlist != (char **) 0 && gs_p->nwith > 0) {
2719                         FREE(gs_p->withlist);
2720                 }
2721         }
2722
2723         free_grpsyls(gs_p->next);
2724         FREE(gs_p);
2725 }
2726 \f
2727
2728 /* Free things off of NOTEs in the notelist, if any, plus the list itself */
2729
2730 void
2731 free_notelist(gs_p)
2732
2733 struct GRPSYL *gs_p;
2734
2735 {
2736         int n;
2737
2738         for (n = 0; n < gs_p->nnotes; n++) {
2739                 /* free coordinate array, if any */
2740                 if (gs_p->notelist[n].c != (float *) 0) {
2741                         FREE(gs_p->notelist[n].c);
2742                 }
2743
2744                 /* free any slurto lists */
2745                 if (gs_p->notelist[n].slurtolist != (struct SLURTO *) 0) {
2746                         FREE(gs_p->notelist[n].slurtolist);
2747                 }
2748         }
2749
2750         /* free the notelist itself, if any */
2751         if (gs_p->notelist != (struct NOTE *) 0 && gs_p->nnotes > 0) {
2752                 FREE(gs_p->notelist);
2753         }
2754 }
2755 \f
2756
2757 /* add a slurto note to list */
2758
2759 void
2760 add_slurto(grpsyl_p, pitch, octave, note_index, slurstyle)
2761
2762 struct GRPSYL *grpsyl_p;        /* what group to associate slurto with */
2763 int     pitch;                  /* 'a' to 'g' */
2764 int     octave;
2765 int     note_index;             /* which note in the chord to slur from */
2766 int     slurstyle;              /* L_NORMAL, L_DOTTED, or L_DASHED */
2767
2768 {
2769         struct NOTE *note_p;    /* note to attach to */
2770
2771
2772         if (note_index < 0) {
2773                 /* user tried to slur from a rest or something like that */
2774                 yyerror("no note to slur from");
2775                 return;
2776         }
2777
2778         note_p = &(grpsyl_p->notelist [ note_index ]);
2779         if (note_p->nslurto == 0) {
2780                 /* first time--need to allocate */
2781                 MALLOC(SLURTO, note_p->slurtolist, 1);
2782         }
2783
2784         /* else re-allocate more space */
2785         else {
2786                 REALLOC(SLURTO, note_p->slurtolist, note_p->nslurto + 1);
2787         }
2788
2789         note_p->slurtolist [ note_p->nslurto ].letter = (char) pitch;
2790         note_p->slurtolist [ note_p->nslurto ].octave = (short) octave;
2791         note_p->slurtolist [ note_p->nslurto ].slurstyle = (short) slurstyle;
2792         /* we'll fix up this value later when we know the right one */
2793         note_p->slurtolist [ note_p->nslurto ].slurdir = (short) UNKNOWN;
2794
2795         (note_p->nslurto)++;
2796 }
2797 \f
2798
2799 /* At the beginning of a <> which indicates slurs,
2800  * save which slurtolist index we are are on.
2801  * When at the end of the slur, we will discover if the user specified a
2802  * bend direction for the slurs, and if so, have to go back and fill in
2803  * that direction for all the slurtolist items from the saved index to
2804  * the end of the list. This is needed since user may have several <>
2805  * items with different directions, e.g.,
2806  *      a<c+>up<g>down<e>
2807  */
2808
2809 void
2810 begin_slur(grpsyl_p, note_index)
2811
2812 struct GRPSYL *grpsyl_p;
2813 int note_index;
2814
2815 {
2816         if (grpsyl_p == 0 || note_index < 0 || note_index >= grpsyl_p->nnotes) {
2817                 /* must have been bad input */
2818                 return;
2819         }
2820         Slur_begin = grpsyl_p->notelist[note_index].nslurto;
2821 }
2822
2823 /* At the end of a slur, either <> style or "slur" keyword style,
2824  * go back and fill in the bulge direction. If note_index is -1,
2825  * this is a "slur" that applies to all notes of the group, and
2826  * there should be one slurto item on each note.
2827  */
2828
2829 void
2830 set_slurdir(grpsyl_p, note_index, dir)
2831
2832 struct GRPSYL *grpsyl_p;
2833 int note_index;         /* which note's slurtolist to use */
2834 int dir;                /* UP, DOWN, UNKNOWN */
2835
2836 {
2837         int n;
2838         int i;
2839
2840         if (note_index == -1) {
2841                 for (n = 0; n < grpsyl_p->nnotes; n++) {
2842                         if (grpsyl_p->notelist[n].nslurto >= 1) {
2843                                 grpsyl_p->notelist[n]
2844                                         .slurtolist[grpsyl_p->notelist[n].nslurto-1]
2845                                         .slurdir = dir;
2846                         }
2847                         /* If no slurto, should already have
2848                          * error generated elsewhere */
2849                 }
2850         }
2851         else {
2852                 for (i = Slur_begin; i < grpsyl_p->notelist[note_index].nslurto; i++) {
2853                         grpsyl_p->notelist[note_index].slurtolist[i].slurdir = dir;
2854                 }
2855         }
2856 }
2857 \f
2858
2859 /* given a bend string, return what should be crammed into the
2860  * octave field to save this info, using the TABOCT macro. If the string
2861  * is incomprehensible, print error, and do what we can. */
2862
2863 /* define states for the simple parser */
2864 #define DOING_INTEGER           1
2865 #define DOING_NUMERATOR         2
2866 #define DOING_DENOMINATOR       3
2867 #define GOT_ERROR               4
2868
2869 static int
2870 parse_bend_string(bendstring)
2871
2872 char *bendstring;
2873
2874 {
2875         int intpart = 0;        /* integer part of bend */
2876         int num = 0, den = 1;   /* numerator and denominator
2877                                  * of fractional part of bend */
2878
2879
2880         /* if null string, easy to parse */
2881         if (bendstring == (char *) 0) {
2882                 return (TABOCT(0, 0, 0));
2883         }
2884
2885         /* string is in internal format, so skip first 2 bytes, plus any
2886          * leading white space */
2887         for (bendstring += 2; *bendstring != '\0'; bendstring++) {
2888                 if (isgraph(*bendstring)) {
2889                         break;
2890                 }
2891         }
2892
2893         /* the text "full" is a special case */
2894         if (strcmp(bendstring, "full") == 0) {
2895                 intpart = 1;
2896         }
2897         else {
2898                 /* the text better be a number and/or fractional part,
2899                  * so parse that */
2900                 int state;      /* DOING_INTEGER, DOING_NUMERATOR,
2901                                  * DOING_DENOMINATOR, or GOT_ERROR */
2902
2903                 for (state = DOING_INTEGER;
2904                                 *bendstring != '\0' && state < GOT_ERROR;
2905                                 bendstring++) {
2906
2907                         if (isdigit(*bendstring)) {
2908                                 /* add to the correct item */
2909                                 switch(state) {
2910                                 case DOING_INTEGER:
2911                                         intpart = (intpart * 10) + (*bendstring - '0');
2912                                         break;
2913                                 case DOING_NUMERATOR:
2914                                         num = (num * 10) + (*bendstring - '0');
2915                                         break;
2916                                 case DOING_DENOMINATOR:
2917                                         den = (den * 10) + (*bendstring - '0');
2918                                         break;
2919                                 default:
2920                                         pfatal("bad state in parse_bend_string");
2921                                         break;
2922                                 }
2923                         }
2924
2925                         /* handle white space */
2926                         else if (*bendstring == ' ' || *bendstring == '\t') {
2927                                 if (state == DOING_INTEGER) {
2928                                         /* end of integer part */
2929                                         state = DOING_NUMERATOR;
2930                                 }
2931                                 /* ignore any other white space */
2932                         }
2933
2934                         else if (*bendstring == '/') {
2935                                 /* starting parsing of denominator */
2936                                 den = 0;
2937
2938                                 switch (state) {
2939
2940                                 case DOING_INTEGER:
2941                                         /* oops. what we thought was the integer
2942                                          * part was really the numerator of the
2943                                          * fractional part. Adjust accordingly. */
2944                                         num = intpart;
2945                                         intpart = 0;
2946                                         state = DOING_DENOMINATOR;
2947                                         break;
2948
2949                                 case DOING_NUMERATOR:
2950                                         state = DOING_DENOMINATOR;
2951                                         break;
2952
2953                                 case DOING_DENOMINATOR:
2954                                         yyerror("more than one / in bend string");
2955                                         state = GOT_ERROR;
2956                                         break;
2957
2958                                 default:
2959                                         pfatal("bad state in parse_bend_string");
2960                                         break;
2961                                 }
2962                         }
2963
2964                         else {
2965                                 yyerror("invalid character in bend string");
2966                                 state = GOT_ERROR;
2967                         }
2968                 }
2969         }
2970
2971         /* make sure we can fit them in our cramped space */
2972         (void) rangecheck(intpart, MINBENDINT, MAXBENDINT,
2973                                         "integer part of bend");
2974         (void) rangecheck(num, MINBENDNUM, MAXBENDNUM,
2975                                         "numerator of bend fraction");
2976         (void) rangecheck(den, MINBENDDEN, MAXBENDDEN,
2977                                         "denominator of bend fraction");
2978
2979         return (TABOCT(intpart, num, den));
2980 }
2981 \f
2982
2983 /* given a tablature GRPSYL, look up the proper string number based on the
2984  * letter/accidental that is currently there, and fill in the STRINGNO and
2985  * FRETNO fields with their correct values. The slurto items also get
2986  * their string letter/accidental values translated to string number. */
2987
2988 static void
2989 fix_frets(grpsyl_item_p)
2990
2991 struct GRPSYL *grpsyl_item_p;
2992
2993 {
2994         int n;                  /* index through note list */
2995         int s;                  /* index through slurto */
2996         struct NOTE *note_p;    /* current note */
2997         struct STRINGINFO *string_info_p;       /* string translation table,
2998                                  * to map between pitch/accidental and
2999                                  * string number */
3000         int nstrings;           /* how many strings there are */
3001
3002
3003         /* find appropriate string mapping table and its size */
3004         string_info_p = Staff[grpsyl_item_p->staffno - 1].strinfo;
3005         nstrings = Staff[grpsyl_item_p->staffno - 1].stafflines;
3006
3007         /* for each note, translate to string number, and put fret in its
3008          * proper field */
3009         for (n = 0; n < grpsyl_item_p->nnotes; n++) {
3010                 /* get pointer to the current note in the group */
3011                 note_p = &(grpsyl_item_p->notelist[n]);
3012
3013                 /* doing mapping from pitch/accidental to string number */
3014                 note_p->STRINGNO = string_number(string_info_p, nstrings,
3015                         note_p->letter, note_p->accidental, TMP_NTICKS(note_p));
3016
3017                 /* fill in the correct fret number, now that its space is
3018                  * available */
3019                 note_p->FRETNO = TMP_FRET(note_p);
3020                 note_p->stepsup = 0;    /* no longer need this temp space */
3021
3022                 /* also fix up any slurto items */
3023                 for (s = 0; s < note_p->nslurto; s++) {
3024                         if (note_p->slurtolist[s].octave == USE_DFLT_OCTAVE) {
3025                                 note_p->slurtolist[s].STRINGNO
3026                                                         = note_p->STRINGNO;
3027                         }
3028                 }
3029         }
3030 }
3031 \f
3032
3033 /* look up a letter/acc/nticks combination in the string_info_p table of
3034  * size nstrings, and return the index of the string that matched. If none
3035  * match, print an error and return 0. However, if the pitch is PP_NO_PITCH,
3036  * don't print the error, because an error would already have been printed
3037  * earlier, and it just adds confusion to print another one.
3038  * Also don't complain about rest and space, because they are legal.
3039  */
3040
3041 static int
3042 string_number(string_info_p, nstrings, letter, accidental, nticks)
3043
3044 struct STRINGINFO *string_info_p;       /* string translation table */
3045 int nstrings;                           /* number of entries in table */
3046 int letter;
3047 int accidental;
3048 int nticks;                             /* number of ' marks */
3049
3050 {
3051         register int i;
3052
3053         if (string_info_p == (struct STRINGINFO *) 0) {
3054                 pfatal("null stringinfo");
3055         }
3056
3057         /* look through string_info list for a matching string */
3058         for (i = 0; i < nstrings; i++) {
3059                 if (string_info_p[i].letter == letter &&
3060                                 string_info_p[i].accidental == accidental &&
3061                                 string_info_p[i].nticks == nticks) {
3062                         /* found it. return its index */
3063                         return(i);
3064                 }
3065         }
3066
3067         /* If letter is PP_NO_PITCH, that means user failed to put a letter
3068          * for the first group in the measure. We would have already
3069          * exclaimed about that in fix_a_grpsyl_list(), so rather than
3070          * generate lots more messages due to the same error,
3071          * just silently return the default string of 0.
3072          * Rests, spaces, and rpts are also fine--they will get converted to
3073          * groups later. Otherwise the error message is needed. */
3074         if (letter >= 'a' && letter <= 'g') {
3075                 /* no match found */
3076                 l_yyerror(Curr_filename, yylineno, "no '%s' string",
3077                                 format_string_name(letter, accidental, nticks));
3078         }
3079
3080         /* use string 0 as default, to have something to return */
3081         return(0);
3082 }
3083 \f
3084
3085 /* given a string number and staff number, return the text representation
3086  * of that string. Returns pointer to static area. */
3087
3088 char *
3089 stringname(stringno, staffno)
3090
3091 int stringno;
3092 int staffno;
3093
3094 {
3095         struct STRINGINFO *stringinfo_p;
3096
3097
3098         /* make sure we have a valid string number */
3099         if (stringno < 0 || stringno >= Staff[staffno - 1].stafflines) {
3100                 pfatal("string number %d out of range", stringno);
3101         }
3102
3103         /* get pointer to proper table entry for this string */
3104         stringinfo_p = & (Staff[staffno - 1].strinfo[stringno] );
3105
3106         /* return text representation */
3107         return(format_string_name(stringinfo_p->letter,
3108                         stringinfo_p->accidental,
3109                         stringinfo_p->nticks));
3110 }
3111 \f
3112
3113 /* to print some ticks, print as much of this string as necessary */
3114 static char tickstring[MAXTICKS + 1];
3115
3116 /* given string letter, accidental, and number of tick marks, return
3117  * pointer to static area that contains the text representation of the
3118  * string name */
3119
3120 char *
3121 format_string_name(letter, accidental, nticks)
3122
3123 int letter;
3124 int accidental;
3125 int nticks;
3126
3127 {
3128         static char name[MAXTICKS + 3]; /* this is what will get returned */
3129         int tickoffset = 1;     /* where in name that ticks begin */
3130         int i;
3131
3132         /* fill in string letter plus accidental if any */
3133         name[0] = (char) letter;
3134         if (accidental != 0) {
3135                 name[1] = (char) accidental;
3136                 tickoffset++;
3137         }
3138
3139         /* first time through, fill in maximum number of ticks. This makes
3140          * it possible to have the right number, even if MAXTICKS changes
3141          * some day */
3142         if (tickstring[0] == '\0') {
3143                 for (i = 0; i < MAXTICKS; i++) {
3144                         tickstring[i] = '\'';
3145                 }
3146         }
3147
3148         /* add appropriate number of ticks to name */
3149         (void) sprintf(name + tickoffset, tickstring + strlen(tickstring) - nticks);
3150
3151         return(name);
3152 }
3153 \f
3154
3155 /* given a list of grpsyls, check that every non-prebend bend has a previous
3156  * groups which contains the same string as the bend is on. Also disallow
3157  * slashes on groups that just have a bend. */
3158
3159 static void
3160 check_bends(gs_p, mll_p)
3161
3162 struct GRPSYL *gs_p;
3163 struct MAINLL *mll_p;
3164
3165 {
3166         int n;                          /* notelist index */
3167         int ns;                         /* slurto index */
3168         struct GRPSYL *prevgrp_p;       /* previous group in same staff/voice */
3169         struct NOTE *prevnote_p;        /* note in previous group */
3170         int has_bend, has_prebend;      /* YES or NO */
3171         struct MAINLL *nmll_p;
3172
3173
3174         /* check each group in the list */
3175         for (  ; gs_p != (struct GRPSYL *) 0; gs_p = gs_p->next) {
3176
3177                 /* check each note in the group */
3178                 has_bend = has_prebend = NO;
3179                 for (n = 0; n < gs_p->nnotes; n++) {
3180
3181                         if (HASBEND(gs_p->notelist[n])
3182                                         && gs_p->notelist[n].FRETNO == NOFRET) {
3183
3184                                 /* this is a non-prebend bend, so it needs to
3185                                  * be checked. Find the previous group */
3186                                 has_prebend = YES;
3187                                 nmll_p = mll_p;
3188                                 if ((prevgrp_p = prevgrpsyl(gs_p, &nmll_p))
3189                                                 != (struct GRPSYL *) 0) {
3190
3191                                         /* find the matching note (string),
3192                                          * If not found, the function will
3193                                          * print an error. */
3194                                         prevnote_p =
3195                                                 find_matching_note(prevgrp_p,
3196                                                 gs_p->notelist[n].letter,
3197                                                 gs_p->notelist[n].octave,
3198                                                 "bend");
3199
3200                                         if (prevnote_p == (struct NOTE *) 0) {
3201                                                 continue;
3202                                         }
3203
3204                                         /* previous group not allowed to have
3205                                          * slide other than inward nowhere */
3206                                         for (ns = 0; ns < prevnote_p->nslurto;
3207                                                                         ns++) {
3208                                                 switch (prevnote_p->slurtolist
3209                                                                 [ns].octave) {
3210                                                 case IN_UPWARD:
3211                                                 case IN_DOWNWARD:
3212                                                         /* these are okay */
3213                                                         break;
3214                                                 default:
3215                                                         l_yyerror(gs_p->inputfile,
3216                                                         gs_p->inputlineno,
3217                                                         "can't have bend on note following a slide");
3218                                                         break;
3219                                                 }
3220                                         }
3221                                 }
3222                                 else {
3223                                         yyerror("can't do bend--no previous group");
3224                                         continue;
3225                                 }
3226
3227                                 /* slashes are not allowed on bend groups */
3228                                 if (gs_p->slash_alt != 0) {
3229                                         yyerror("slash not allowed with bend");
3230                                         gs_p->slash_alt = 0;
3231                                 }
3232                         }
3233                         else if (HASBEND(gs_p->notelist[n])
3234                                         && gs_p->notelist[n].FRETNO != NOFRET) {
3235                                 has_bend = YES;
3236                         }
3237                 }
3238
3239                 if (has_bend == YES && has_prebend == YES) {
3240                         yyerror("mixture of prebend and bend not allowed on the same chord");
3241                 }
3242         }
3243 }
3244 \f
3245
3246 /*
3247  * When the input includes additive time values like 2+8 or 2.+32
3248  * on groups, we need to create a group for each extra time value.
3249  * For notes, the groups are all tied together to make them effectively
3250  * a single group. If there are any subtractions in the list of time values,
3251  * we have no way to determine what time values the user really wants,
3252  * so we keep using as big as possible (including dots) until we get enough.
3253  * Should be called with the group currently last in the GRPSYL list.
3254  * Returns the (possibly new) last group.
3255  */
3256
3257 struct GRPSYL *
3258 expandgrp(grpsyl_p, timelist_p)
3259
3260 struct GRPSYL *grpsyl_p;        /* the first group; pattern to clone */
3261 struct TIMELIST *timelist_p;    /* the list of additional time values */
3262
3263 {
3264         struct GRPSYL *gs_p;    /* group being processed */
3265         struct TIMELIST *tl_p;  /* index through list to check for subtracts */
3266         int had_neg;            /* YES if there was at least one subtraction */
3267         RATIONAL totaltime;     /* all times added together */
3268         int n;                  /* index through notelist */
3269
3270
3271         if (timelist_p == 0) {
3272                 /* nothing to expand */
3273                 return(grpsyl_p);
3274         }
3275
3276         /* disallow on grace */
3277         if (grpsyl_p->grpvalue == GV_ZERO) {
3278                 yyerror("can't use additive time values on grace notes");
3279                 return(grpsyl_p);
3280         }
3281         /* disallow if alt on group */
3282         if (grpsyl_p->slash_alt < 0) {
3283                 yyerror("can't use additive time values and alt on same chord");
3284                 return(grpsyl_p);
3285         }
3286
3287         /* If there were any subtraction, need to do that differently
3288          * than if all are additions. Check if any subtractions, and
3289          * add up total time in case we need it. */
3290         for (had_neg = NO, totaltime = grpsyl_p->fulltime, tl_p = timelist_p;
3291                                         tl_p != 0; tl_p = tl_p->next) {
3292                 if (MI(tl_p->fulltime)) {
3293                         had_neg = YES;
3294                 }
3295                 totaltime = radd(totaltime, tl_p->fulltime);
3296         }
3297         if (had_neg == YES) {
3298                 /* It's impossible to know what time values the user
3299                  * really wants us to use, so we keep using the largest
3300                  * possible values until we get enough, using as many
3301                  * dots as possible along the way.
3302                  */
3303                 RATIONAL try;   /* The time value we're currently attempting
3304                                  * to use; will be used if remaining time
3305                                  * is at least this long, else take half
3306                                  * and try again. */
3307                 int needgrp;    /* YES if need to alloc a new group.
3308                                  * (Have one group to start with,
3309                                  * so first time through loop don't need
3310                                  * to allocate.) */
3311
3312                 /* We start with double whole, and keep using smaller times
3313                  * from there until we get enough, using dots if possible.
3314                  * I suppose for rests we really *should* start with quad
3315                  * whole rather than double whole, but since we don't know
3316                  * what the user really wants anyway, and the cases where
3317                  * this would make a difference are so rare, and since the
3318                  * user can always specify exactly what they do want,
3319                  * don't bother. (They'll just get two double whole rests.) */
3320                 gs_p = grpsyl_p;
3321                 for (needgrp = NO, try = Two; PL(totaltime);   ) {
3322                         if (GE(totaltime, try)) {
3323                                 /* We can use this trial time value */
3324                                 if (needgrp == YES) {
3325                                         gs_p->next = clone_gs_list(gs_p, YES);
3326                                         gs_p->next->prev = gs_p;
3327                                         gs_p = gs_p->next;
3328                                         /* If the original group started a beam,
3329                                          * added groups must be inside */
3330                                         if (gs_p->beamloc == STARTITEM) {
3331                                                 gs_p->beamloc = INITEM;
3332                                         }
3333                                 }
3334                                 gs_p->fulltime = try;
3335                                 gs_p->dots = 0;
3336                                 gs_p->basictime = reconstruct_basictime(gs_p->fulltime);
3337                                 totaltime = rsub(totaltime, try);
3338                                 if (GE(totaltime, try)) {
3339                                         /* Total time is so long we can use
3340                                          * another one of this time value. */
3341                                         needgrp = YES;
3342                                         continue;
3343                                 }
3344                         }
3345                         else {
3346                                 /* Trial time is too long. Try half as much. */
3347                                 try = rdiv(try, Two);
3348                                 continue;
3349                         }
3350
3351                         /* If still some time left, see if we can add one
3352                          * or more dots to use up more time. */
3353                         for (try = rdiv(try, Two); GE(totaltime, try);
3354                                                         try = rdiv(try, Two)) {
3355                                 (gs_p->dots)++;
3356                                 gs_p->fulltime = radd(gs_p->fulltime,
3357                                                                 try);
3358                                 totaltime = rsub(totaltime, try);
3359                         }
3360                         needgrp = YES;
3361                 }
3362         }
3363
3364         else {           /* (No subtractions) */
3365                 /* We will add a group for each added time value */
3366                 for (gs_p = grpsyl_p; timelist_p != 0;
3367                                                 timelist_p = timelist_p->next) {
3368                         /* Make a copy the GRPSYL. This function is to be called
3369                          * with the group currently at the end of a list,
3370                          * so the "list" being cloned is only a single group,
3371                          * but might as well use the existing function... */
3372                         gs_p->next = clone_gs_list(gs_p, YES);
3373                         gs_p->next->prev = gs_p;
3374                         gs_p = gs_p->next;
3375                         /* Now fix up the time values in the cloned group */
3376                         gs_p->fulltime = timelist_p->fulltime;
3377                         gs_p->basictime = reconstruct_basictime(gs_p->fulltime);
3378                         gs_p->dots = recalc_dots(gs_p->fulltime,
3379                                                 gs_p->basictime);
3380                         /* If the original group started a beam,
3381                          * added groups must be inside */
3382                         if (gs_p->beamloc == STARTITEM) {
3383                                 gs_p->beamloc = INITEM;
3384                         }
3385                 }
3386         }
3387
3388         /* We need to tie the note groups together. Rests are not yet in
3389          * final form, so have to look for PP_REST. */
3390         if (grpsyl_p->grpcont == GC_NOTES && (grpsyl_p->nnotes != 1
3391                                 || grpsyl_p->notelist[0].letter != PP_REST)) {
3392                 for (gs_p = grpsyl_p; gs_p->next != 0; gs_p = gs_p->next) {
3393                         gs_p->tie = YES;
3394                         for (n = 0; n < gs_p->nnotes; n++) {
3395                                 gs_p->notelist[n].tie = YES;
3396                         }
3397                 }
3398                 /* tied-to notes accidentals must be implied, not explicit */
3399                 for (n = 0; n < grpsyl_p->nnotes; n++) {
3400                         if (grpsyl_p->notelist[n].accidental != '\0') {
3401                                 struct GRPSYL *ngs_p;
3402                                 for (ngs_p = grpsyl_p->next; ngs_p != 0;
3403                                                         ngs_p = ngs_p->next) {
3404                                         ngs_p->notelist[n].accidental = '\0';
3405                                 }
3406                         }
3407                 }
3408         }
3409         return(gs_p);
3410 }
3411 \f
3412
3413 /*
3414  * Given a RATIONAL basictime and number of dots,
3415  * return the corresponding fulltime. That is calculated by 
3416  *      (2 * basictime) - (basictime x 1/2 to the (dots) power)
3417  */
3418
3419 RATIONAL
3420 calcfulltime(basictime, dots)
3421
3422 RATIONAL basictime;
3423 int dots;
3424
3425 {
3426         return( rsub (rmul(Two, basictime), rmul(basictime, rrai(One_half, dots))) );
3427 }