forked from lewdlime/abcm2ps
-
Notifications
You must be signed in to change notification settings - Fork 0
/
abcparse.c
2935 lines (2772 loc) · 59.3 KB
/
abcparse.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Generic ABC parser.
*
* Copyright (C) 1998-2014 Jean-François Moine
* Adapted from abc2ps, Copyright (C) 1996, 1997 Michael Methfessel
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335 USA
*/
#include "config.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "abcparse.h"
/* interface */
static void *(*alloc_f)(int size);
static void (*free_f)(void *);
static void (*level_f)(int level);
static int client_sz;
static int keep_comment;
/* global values */
char *deco_tb[128]; /* decoration names */
int severity; /* error severity */
static int abc_vers; /* abc version */
static short abc_state; /* parse state */
static short ulen; /* unit note length set by M: or L: */
static short meter; /* upper value of time sig for n-plets */
static unsigned char microscale; /* current microtone scale */
static signed char vover; /* voice overlay (1: single bar, -1: multi-bar */
static char lyric_started; /* lyric started */
static char *gchord; /* guitar chord */
static struct deco dc; /* decorations */
static struct abcsym *deco_start; /* 1st note of the line for d: / s: */
static struct abcsym *deco_cont; /* current symbol when d: / s: continuation */
static unsigned short *p_micro; /* ptr to the microtone table of the tune */
#define VOICE_NAME_SZ 64 /* max size of a voice name */
static char *file; /* remaining abc file */
static int linenum; /* current source line number */
static int colnum; /* current source column number */
static char *abc_line; /* line being parsed */
static struct abcsym *last_sym; /* last symbol for errors */
static short nvoice; /* number of voices (0..n-1) */
static struct { /* voice table and current pointer */
char id[VOICE_ID_SZ]; /* voice ID */
struct abcsym *last_note; /* last note or rest */
short ulen; /* unit note length */
unsigned char microscale; /* microtone scale */
unsigned char mvoice; /* main voice when voice overlay */
} voice_tb[MAXVOICE], *curvoice;
/* char table for note line parsing */
#define CHAR_BAD 0
#define CHAR_IGN 1
#define CHAR_NOTE 2
#define CHAR_REST 3
#define CHAR_ACC 4
#define CHAR_GR_ST 5
#define CHAR_DECO 6
#define CHAR_GCHORD 7
#define CHAR_BSLASH 8
#define CHAR_OBRA 9
#define CHAR_BAR 10
#define CHAR_OPAR 11
#define CHAR_VOV 12
#define CHAR_SPAC 13
#define CHAR_MINUS 14
#define CHAR_CPAR 15
#define CHAR_BRHY 16
#define CHAR_DECOS 17
#define CHAR_SLASH 18
#define CHAR_GR_EN 19
#define CHAR_LINEBREAK 20
static char char_tb[256] = {
0, 0, 0, 0, 0, 0, 0, 0, /* 00 - 07 */
0, CHAR_SPAC, CHAR_LINEBREAK, 0, 0, 0, 0, 0, /* 08 - 0f */
0, 0, 0, 0, 0, 0, 0, 0, /* 10 - 17 */
0, 0, 0, 0, 0, 0, 0, 0, /* 18 - 1f */
CHAR_SPAC, CHAR_DECOS, CHAR_GCHORD, CHAR_BAD, /* (sp) ! " # */
CHAR_BAD, CHAR_BAD, CHAR_VOV, CHAR_BAD, /* $ % & ' */
CHAR_OPAR, CHAR_CPAR, CHAR_BAD, CHAR_DECOS, /* ( ) * + */
CHAR_BAD, CHAR_MINUS, CHAR_DECO, CHAR_SLASH, /* , - . / */
CHAR_BAD, CHAR_BAD, CHAR_BAD, CHAR_BAD, /* 0 1 2 3 */
CHAR_BAD, CHAR_BAD, CHAR_BAD, CHAR_BAD, /* 4 5 6 7 */
CHAR_BAD, CHAR_BAD, CHAR_BAR, CHAR_BAD, /* 8 9 : ; */
CHAR_BRHY, CHAR_ACC, CHAR_BRHY, CHAR_BAD, /* < = > ? */
CHAR_BAD, CHAR_NOTE, CHAR_NOTE, CHAR_NOTE, /* @ A B C */
CHAR_NOTE, CHAR_NOTE, CHAR_NOTE, CHAR_NOTE, /* D E F G */
CHAR_DECO, CHAR_DECO, CHAR_DECO, CHAR_DECO, /* H I J K */
CHAR_DECO, CHAR_DECO, CHAR_DECO, CHAR_DECO, /* L M N O */
CHAR_DECO, CHAR_DECO, CHAR_DECO, CHAR_DECO, /* P Q R S */
CHAR_DECO, CHAR_DECO, CHAR_DECO, CHAR_DECO, /* T U V W */
CHAR_REST, CHAR_DECO, CHAR_REST, CHAR_OBRA, /* X Y Z [ */
CHAR_BSLASH, CHAR_BAR, CHAR_ACC, CHAR_ACC, /* \ ] ^ _ */
CHAR_IGN, CHAR_NOTE, CHAR_NOTE, CHAR_NOTE, /* ` a b c */
CHAR_NOTE, CHAR_NOTE, CHAR_NOTE, CHAR_NOTE, /* d e f g */
CHAR_DECO, CHAR_DECO, CHAR_DECO, CHAR_DECO, /* h i j k */
CHAR_DECO, CHAR_DECO, CHAR_DECO, CHAR_DECO, /* l m n o */
CHAR_DECO, CHAR_DECO, CHAR_DECO, CHAR_DECO, /* p q r s */
CHAR_DECO, CHAR_DECO, CHAR_DECO, CHAR_DECO, /* t u v w */
CHAR_REST, CHAR_REST, CHAR_REST, CHAR_GR_ST, /* x y z { */
CHAR_BAR, CHAR_GR_EN, CHAR_DECO, CHAR_BAD, /* | } ~ (del) */
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 80 - 8f */
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 90 - 9f */
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* a0 - af */
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* b0 - bf */
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* c0 - cf */
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* d0 - df */
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* e0 - ef */
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* f0 - ff */
};
static const char all_notes[] = "CDEFGABcdefgab";
static char *get_line(void);
static char *parse_len(char *p,
int *p_len);
static char *parse_basic_note(char *p,
int *pitch,
int *length,
int *accidental,
int *stemless);
static int parse_info(struct abctune *t,
char *p,
char *comment);
static char *parse_gchord(char *p);
static int parse_line(struct abctune *t,
char *p);
static char *parse_note(struct abctune *t,
char *p,
int flags);
static void syntax(char *msg, char *q);
static void vover_new(void);
/* -- abcMIDI like errors -- */
static void print_error(char *s, int col)
{
if (col >= 0)
fprintf(stderr, "Error in line %d.%d: %s\n", linenum, col, s);
else
fprintf(stderr, "Error in line %d: %s\n", linenum, s);
}
/* -- delete an ABC symbol -- */
void abc_delete(struct abcsym *as)
{
switch (as->type) {
case ABC_T_INFO:
switch (as->text[0]) {
case 'Q':
if (as->u.tempo.str1)
free_f(as->u.tempo.str1);
if (as->u.tempo.value)
free_f(as->u.tempo.value);
if (as->u.tempo.str2)
free_f(as->u.tempo.str2);
break;
case 'V':
if (as->u.voice.fname)
free_f(as->u.voice.fname);
if (as->u.voice.nname)
free_f(as->u.voice.nname);
break;
}
break;
}
if (as->text)
free_f(as->text);
if (as->comment)
free_f(as->comment);
if (as->prev)
as->prev->next = as->next;
if (as->next)
as->next->prev = as->prev;
if (as->tune->first_sym == as)
as->tune->first_sym = as->next;
if (as->tune->last_sym == as)
as->tune->last_sym = as->prev;
free_f(as);
}
/* -- free the memory areas of all tunes -- */
void abc_free(struct abctune *t)
{
struct abctune *tn;
struct abcsym *s, *sn;
if (!free_f)
return;
for (;;) {
if (!t)
break;
s = t->first_sym;
/* free the associated symbols */
for (;;) {
sn = s->next;
abc_delete(s);
if ((s = sn) == NULL)
break;
}
/* free the tune */
tn = t->next;
free_f(t);
t = tn;
}
}
/* -- initialize the parser -- */
void abc_init(void *alloc_f_api(int size),
void free_f_api(void *ptr),
void level_f_api(int level),
int client_sz_api,
int keep_comment_api)
{
if (alloc_f) {
fprintf(stderr, "abc_init already initialized\n");
return;
}
alloc_f = alloc_f_api;
free_f = free_f_api;
level_f = level_f_api;
client_sz = client_sz_api;
keep_comment = keep_comment_api;
}
/* -- insert an ABC description -- */
void abc_insert(char *file_api,
struct abcsym *s)
{
struct abctune *t;
char *p;
/* initialize */
file = file_api;
if (level_f)
level_f(abc_state != ABC_S_GLOBAL);
abc_state = ABC_S_TUNE;
linenum = 0;
t = s->tune;
t->last_sym = s;
/* scan till end of description */
for (;;) {
if ((p = get_line()) == NULL)
break; /* done */
if (*p == '\0')
break; /* blank line --> done */
/*fixme-insert: don't accept X:*/
/* parse the music line */
if (parse_line(t, p))
break;
}
}
/* -- new symbol -- */
struct abcsym *abc_new(struct abctune *t,
char *text,
char *comment)
{
struct abcsym *s;
s = alloc_f(sizeof *s + client_sz);
memset(s, 0, sizeof *s + client_sz);
s->tune = t;
if (text) {
s->text = alloc_f(strlen(text) + 1);
strcpy(s->text, text);
}
if (comment) {
s->comment = alloc_f(strlen(comment) + 1);
strcpy(s->comment, comment);
}
if (!t->last_sym) {
t->first_sym = s;
} else {
if ((s->next = t->last_sym->next) != NULL)
s->next->prev = s;
t->last_sym->next = s;
s->prev = t->last_sym;
}
last_sym = t->last_sym = s;
s->state = abc_state;
s->linenum = linenum;
s->colnum = colnum;
return s;
}
/* get the ABC version */
static void get_vers(char *p)
{
int i, j,k;
i = j = k = 0;
if (sscanf(p, "%d.%d.%d", &i, &j, &k) != 3)
if (sscanf(p, "%d.%d", &i, &j) != 2)
sscanf(p, "%d", &i);
abc_vers = (i << 16) + (j << 8) + k;
}
/* -- parse an ABC file -- */
struct abctune *abc_parse(char *file_api)
{
struct abctune *first_tune = NULL;
struct abctune *t, *last_tune;
/* saved global variables */
int g_abc_vers, g_ulen, g_microscale;
char *p;
char g_char_tb[128];
/* initialize */
file = file_api;
t = NULL;
abc_state = ABC_S_GLOBAL;
if (level_f)
level_f(0);
linenum = 0;
last_tune = NULL;
g_abc_vers = g_ulen = g_microscale = 0; /* (have gcc happy) */
/* scan till end of file */
for (;;) {
if ((p = get_line()) == NULL) {
if (abc_state == ABC_S_HEAD) {
syntax("Unexpected EOF in header definition",
p);
severity = 1;
}
if (t)
t->abc_vers = abc_vers;
if (abc_state != ABC_S_GLOBAL) {
abc_vers = g_abc_vers;
ulen = g_ulen;
microscale = g_microscale;
memcpy(char_tb, g_char_tb, sizeof g_char_tb);
}
break; /* done */
}
/* start a new tune if not done */
if (!t) {
if (*p == '\0')
continue;
t = alloc_f(sizeof *t);
memset(t, 0 , sizeof *t);
if (!last_tune)
first_tune = t;
else
last_tune->next = t;
last_tune = t;
p_micro = t->micro_tb;
meter = 0;
}
/* parse the music line */
switch (parse_line(t, p)) {
case 2: /* start of tune */
g_abc_vers = abc_vers;
g_ulen = ulen;
g_microscale = microscale;
memcpy(g_char_tb, char_tb, sizeof g_char_tb);
break;
case 1: /* end of tune */
t->abc_vers = abc_vers;
abc_state = ABC_S_GLOBAL;
t = NULL;
abc_vers = g_abc_vers;
ulen = g_ulen;
microscale = g_microscale;
memcpy(char_tb, g_char_tb, sizeof g_char_tb);
if (level_f)
level_f(0);
if (dc.n > 0)
syntax("Decoration without symbol", 0);
dc.n = dc.h = dc.s = 0;
break;
}
}
return first_tune;
}
/* -- cut off after % and remove trailing blanks -- */
static char *decomment_line(char *p)
{
char *q, c, *comment = NULL;
q = p;
for (;;) {
c = *p;
if (c == '\0')
break;
if (c == '\\') {
p++;
if (*p == '\0')
return 0;
p++;
continue;
}
if (c == '%') {
if (keep_comment) {
comment = p + 1;
break;
}
break;
}
if (c == '"') {
for (;;) {
p++;
if (*p == '\0')
break;
if (*p == '"' && p[-1] != '\\') {
p++;
break;
}
}
} else {
p++;
}
}
/* remove the trailing blanks */
while (p > q) {
c = *--p;
if (!isspace((unsigned char) c)) {
p[1] = '\0';
break;
}
}
return comment;
}
/* -- treat the broken rhythm '>' and '<' -- */
static void broken_rhythm(struct note *note,
int num) /* >0: do dot, <0: do half */
{
int l, m, n;
num *= 2;
if (num > 0) {
if (num == 6)
num = 8;
n = num * 2 - 1;
for (m = 0; m <= note->nhd; m++)
note->lens[m] = (note->lens[m] * n) / num;
} else {
n = -num;
if (n == 6)
n = 8;
for (m = 0; m <= note->nhd; m++)
note->lens[m] /= n;
}
l = note->lens[0];
for (m = 1; m <= note->nhd; m++)
if (note->lens[m] < l)
l = note->lens[m];
}
/* -- check for the '!' as end of line (ABC2Win) -- */
static int check_nl(char *p)
{
while (*p != '\0') {
switch (*p++) {
case '!':
return 0;
case '|':
case '[':
case ':':
case ']':
case ' ':
case '\t':
return 1;
}
}
return 1;
}
/* -- parse extra K: or V: definitions (clef, octave and microscale -- */
static char *parse_extra(char *p,
char **p_name,
char **p_middle,
char **p_lines,
char **p_scale,
char **p_octave)
{
for (;;) {
if (strncmp(p, "clef=", 5) == 0
|| strncmp(p, "bass", 4) == 0
|| strncmp(p, "treble", 6) == 0
|| strncmp(p, "alto", 4) == 0
|| strncmp(p, "tenor", 5) == 0
|| strncmp(p, "perc", 4) == 0) {
if (*p_name)
syntax("Double clef name", p);
*p_name = p;
} else if (strncmp(p, "microscale=", 11) == 0) {
int i;
i = atoi(p + 11);
if (i < 4 || i > 256)
syntax("Invalid value in microscale=", p);
else
microscale = i;
} else if (strncmp(p, "middle=", 7) == 0
|| strncmp(p, "m=", 2) == 0) {
if (*p_middle)
syntax("Double clef middle", p);
*p_middle = p + (p[1] == '=' ? 2 : 7);
} else if (strncmp(p, "octave=", 7) == 0) {
if (*p_octave)
syntax("Double octave=", p);
*p_octave = p + 7;
} else if (strncmp(p, "stafflines=", 11) == 0) {
if (*p_lines)
syntax("Double stafflines", p);
*p_lines = p + 11;
} else if (strncmp(p, "staffscale=", 11) == 0) {
if (*p_scale)
syntax("Double staffscale", p);
*p_scale = p + 11;
} else if (strncmp(p, "transpose=", 10) == 0
|| strncmp(p, "t=", 2) == 0) {
; /* ignored */
} else {
break;
}
while (!isspace((unsigned char) *p) && *p != '\0')
p++;
while (isspace((unsigned char) *p))
p++;
if (*p == '\0')
break;
}
return p;
}
/* -- parse a decoration 'xxx<decosep>' -- */
static char *get_deco(char *p,
unsigned char *p_deco)
{
char *q, sep, **t;
unsigned i, l;
*p_deco = 0;
q = p;
sep = q[-1];
if (char_tb[(unsigned char) sep] == CHAR_DECOS) {
if (sep == '+') {
if (*p == '+' && p[1] == '+')
p++; /* special case "+++" */
}
} else {
sep = '\0'; /* Barfly U: */
}
while (*p != sep) {
if (*p == '\0') {
syntax("Decoration not terminated", q);
return p;
}
p++;
}
l = p - q;
if (*p == sep)
p++;
for (i = 1, t = &deco_tb[1];
*t && i < 128;
i++, t++) {
if (strlen(*t) == l
&& strncmp(*t, q, l) == 0) {
*p_deco = i + 128;
return p;
}
}
/* new decoration */
if (i < 128) {
if (level_f && abc_state != ABC_S_GLOBAL)
level_f(0);
*t = alloc_f(l + 1);
if (level_f && abc_state != ABC_S_GLOBAL)
level_f(1);
memcpy(*t, q, l);
(*t)[l] = '\0';
*p_deco = i + 128;
} else {
syntax("Too many decoration types", q);
}
return p;
}
/* -- parse a list of accidentals (K:) -- */
static char *parse_acc(char *p,
struct abcsym *s)
{
int pit, len, acc, nostem;
unsigned nacc;
if (s->u.key.empty == 2)
syntax("cannot have 'none' and a list of accidentals", p);
nacc = 0;
for (;;) {
if (nacc >= sizeof s->u.key.pits) {
syntax("Too many accidentals", p);
break;
}
p = parse_basic_note(p, &pit, &len, &acc, &nostem);
s->u.key.pits[nacc] = pit;
s->u.key.accs[nacc++] = acc;
while (isspace((unsigned char) *p))
p++;
if (*p == '\0')
break;
if (*p != '^' && *p != '_' && *p != '=')
break;
}
s->u.key.microscale = microscale;
if (s->u.key.empty != 2)
s->u.key.nacc = nacc;
return p;
}
/* -- parse a clef (K: or V:) -- */
static void parse_clef(struct abcsym *s,
char *name,
char *middle,
char *lines,
char *scale)
{
int clef = -1;
int transpose = 0;
int clef_line = 2;
char *warn = NULL;
char str[80];
str[0] = '\0';
if (name && strncmp(name, "clef=", 5) == 0) {
name += 5;
switch (*name) {
case '\"':
name = get_str(str, name, sizeof str);
s->u.clef.name = alloc_f(strlen(str) + 1);
strcpy(s->u.clef.name, str);
clef = TREBLE;
break;
case 'g':
warn = name;
transpose = -7;
case 'G':
clef = TREBLE;
break;
case 'f':
warn = name;
transpose = -14;
clef = BASS;
clef_line = 4;
break;
case 'F':
if (name[1] == ',') /* abc2.1.1 clef=F == clef=F, */
transpose = -7;
clef = BASS;
clef_line = 4;
break;
case 'c':
warn = name;
transpose = -7;
case 'C':
clef = ALTO;
clef_line = 3;
break;
case 'P':
clef = PERC;
break;
}
if (clef >= 0) {
name++;
if (*name == ',' || *name== '\'')
warn = name;
while (*name == ',') {
transpose += 7;
name++;
}
while (*name == '\'') {
transpose -= 7;
name++;
}
}
}
if (name && clef < 0) {
if (!strncmp(name, "bass", 4)) {
clef = BASS;
clef_line = 4;
s->u.clef.check_pitch = 1;
name += 4;
} else if (!strncmp(name, "treble", 6)) {
clef = TREBLE;
name += 6;
} else if (!strncmp(name, "alto", 4)
|| !strncmp(name, "tenor", 5)) {
clef = ALTO;
clef_line = *name == 'a' ? 3 : 4;
s->u.clef.check_pitch = 1;
if (*name == 'a')
name += 4;
else
name += 5;
} else if (!strncmp(name, "perc", 4)) {
clef = PERC;
name += 4;
} else if (strncmp(name, "none", 4) == 0) {
clef = TREBLE;
s->u.clef.invis = 1;
s->flags |= ABC_F_INVIS;
name += 4;
} else {
syntax("Unknown clef", name);
}
}
if (clef >= 0) {
switch (*name) {
case '1':
case '2':
case '3':
case '4':
case '5':
clef_line = *name++ - '0';
break;
}
if (name[1] == '8') {
switch (*name) {
case '^':
transpose -= 7;
case '+':
s->u.clef.octave = 1;
break;
case '_':
transpose += 7;
case '-':
s->u.clef.octave = -1;
break;
}
}
}
if (middle) {
int pit, len, acc, nostem, l;
static const char line_tb[7] =
{ALTO, TREBLE, ALTO, BASS, ALTO, BASS, ALTO};
warn = middle;
/* 'middle=<note pitch>' */
parse_basic_note(middle, &pit, &len, &acc, &nostem);
if (clef < 0)
clef = line_tb[(pit + 7) % 7];
switch (clef) {
default:
l = 20 + 4;
break;
case ALTO:
l = 16 + 4;
break;
case BASS:
l = 12 + 4;
break;
}
clef_line = (l - pit + 28) % 7;
if (clef_line & 1) {
syntax("Bad 'middle' value for the clef", middle);
pit++;
}
clef_line = clef_line / 2 + 1;
transpose = l - (clef_line - 1) * 2 - pit;
s->u.clef.check_pitch = 0;
}
s->u.clef.type = clef;
s->u.clef.line = clef_line;
s->u.clef.transpose = transpose;
s->u.clef.stafflines = -1;
s->u.clef.staffscale = 0;
if (lines) {
int l;
l = atoi(lines);
if ((unsigned) l < 10)
s->u.clef.stafflines = l;
else
syntax("Bad value of stafflines", lines);
}
if (scale) {
float sc;
sc = atof(scale);
if (sc >= 0.5 && sc <= 3)
s->u.clef.staffscale = sc;
else
syntax("Bad value of staffscale", scale);
}
if (warn) {
int sev_sav;
sev_sav = severity;
syntax("Warning: Deprecated or non-standard item", warn);
severity = sev_sav;
}
}
/* get the octave= value */
static int parse_octave(char *p)
{
int oct;
if (p) {
oct = 1;
if (*p == '-') {
oct = -1;
p++;
}
if (*p >= '0' && *p <= '4')
return oct * (*p - '0');
syntax("Bad octave value", p);
}
return NO_OCTAVE;
}
/* -- parse a 'K:' -- */
static void parse_key(char *p,
struct abcsym *s)
{
int sf, mode;
char *clef_name, *clef_middle, *clef_lines, *clef_scale;
char *p_octave;
if (*p == '\0') {
s->u.key.empty = 2;
return;
}
if (strncasecmp(p, "none", 4) == 0) {
s->u.key.empty = 2;
p += 4;
while (isspace((unsigned char) *p))
p++;
if (*p == '\0')
return;
}
clef_name = clef_middle = clef_lines = clef_scale = NULL;
p_octave = NULL;
p = parse_extra(p, &clef_name, &clef_middle, &clef_lines,
&clef_scale, &p_octave);
sf = 0;
mode = MAJOR;
switch (*p++) {
case 'F': sf = -1; break;
case 'B': sf++;
case 'E': sf++;
case 'A': sf++;
case 'D': sf++;
case 'G': sf++;
case 'C': break;
case 'H':
if (*p == 'P') {
mode = BAGPIPE;
p++;
} else if (*p == 'p') {
mode = BAGPIPE + 1;
sf = 2;
p++;
} else {
syntax("Unknown bagpipe-like key", p);
}
break;
case '^':
case '_':
case '=':
p--; /* explicit accidentals */
break;
case '\0':
if (s->u.key.empty == 0)
s->u.key.empty = 1;
p--;
break;
default:
p--;
if (s->u.key.empty != 2)
syntax("Key not recognized", p);
break;
}
if (*p == '#') {
sf += 7;
p++;
} else if (*p == 'b') {
sf -= 7;
p++;
}
while (*p != '\0') {
while (isspace((unsigned char) *p))
p++;
if (*p == '\0')
break;
p = parse_extra(p, &clef_name, &clef_middle, &clef_lines,
&clef_scale, &p_octave);
if (*p == '\0')
break;
switch (*p) {
case 'a':
case 'A':
if (strncasecmp(p, "aeo", 3) == 0) {
sf -= 3;
mode = 5;
break;
}
goto unk;
case 'd':
case 'D':
if (strncasecmp(p, "dor", 3) == 0) {
sf -= 2;
mode = 1;
break;
}
goto unk;
case 'e':
case 'E':
if (strncasecmp(p, "exp", 3) == 0) {
s->u.key.exp = 1;
break;
}
goto unk;
case 'i':
case 'I':
if (strncasecmp(p, "ion", 3) == 0) {
mode = 0;
break;
}
goto unk;
case 'l':
case 'L':
if (strncasecmp(p, "loc", 3) == 0) {
sf -= 5;
mode = 6;
break;
}
if (strncasecmp(p, "lyd", 3) == 0) {
sf += 1;
mode = 3;
break;
}
goto unk;
case 'm':
case 'M':
if (strncasecmp(p, "maj", 3) == 0)
break;
if (strncasecmp(p, "mix", 3) == 0) {
sf -= 1;
mode = 4;
break;
}
if (strncasecmp(p, "min", 3) == 0
|| !isalpha((unsigned char) p[1])) { /* 'm' alone */
sf -= 3;
mode = MINOR;
break;
}
goto unk;
case 'p':
case 'P':
if (strncasecmp(p, "phr", 3) == 0) {
sf -= 4;
mode = 2;
break;
}
goto unk;
case '^':
case '_':
case '=':
p = parse_acc(p, s); /* explicit accidentals */
continue;