| 1 |
char version[12] = "2019-01-08"; |
| 2 |
|
| 3 |
/* Copyright (C) 2017-19 R. D. Tennent School of Computing, |
| 4 |
* Queen's University, [email protected] |
| 5 |
* |
| 6 |
* This program is free software; you can redistribute it |
| 7 |
* and/or modify it under the terms of the GNU General |
| 8 |
* Public License as published by the Free Software |
| 9 |
* Foundation; either version 2 of the License, or (at your |
| 10 |
* option) any later version. |
| 11 |
* |
| 12 |
* This program is distributed in the hope that it will |
| 13 |
* be useful, but WITHOUT ANY WARRANTY; without even the |
| 14 |
* implied warranty of MERCHANTABILITY or FITNESS FOR A |
| 15 |
* PARTICULAR PURPOSE. See the GNU General Public License |
| 16 |
* for more details. |
| 17 |
* You should have received a copy of the GNU General |
| 18 |
* Public License along with this program; if not, write to |
| 19 |
* the Free Software Foundation, Inc., 51 Franklin Street, |
| 20 |
* Fifth Floor, Boston, MA 02110-1301, USA. |
| 21 |
*/ |
| 22 |
|
| 23 |
/* tex2aspc - converts conventional MusiXTeX code to autosp-compatible format */ |
| 24 |
|
| 25 |
/* Usage: tex2aspc infile[.tex] [outfile[.aspc]] |
| 26 |
* tex2aspc [-v | --version | -h | --help] |
| 27 |
*/ |
| 28 |
|
| 29 |
|
| 30 |
# include "utils.h" |
| 31 |
|
| 32 |
#ifdef HAVE_CONFIG_H /* for TeXLive */ |
| 33 |
#include <config.h> |
| 34 |
#endif |
| 35 |
|
| 36 |
# ifdef KPATHSEA |
| 37 |
# include <kpathsea/getopt.h> |
| 38 |
# else |
| 39 |
# include <getopt.h> |
| 40 |
# endif |
| 41 |
|
| 42 |
# ifdef HAVE_STDBOOL_H |
| 43 |
# include <stdbool.h> |
| 44 |
# else |
| 45 |
# ifndef HAVE__BOOL |
| 46 |
# define _Bool signed char |
| 47 |
# endif |
| 48 |
# define bool _Bool |
| 49 |
# define true 1 |
| 50 |
# define false 0 |
| 51 |
# endif |
| 52 |
|
| 53 |
# define PRIVATE static |
| 54 |
|
| 55 |
# define SHORT_LEN 256 |
| 56 |
# define MAX_STAFFS 9 |
| 57 |
|
| 58 |
|
| 59 |
PRIVATE char infilename[SHORT_LEN]; |
| 60 |
PRIVATE char *infilename_n = infilename; |
| 61 |
PRIVATE char outfilename[SHORT_LEN]; |
| 62 |
PRIVATE char *outfilename_n = outfilename; |
| 63 |
PRIVATE FILE *infile, *outfile; |
| 64 |
|
| 65 |
PRIVATE char line[LINE_LEN]; /* line of input */ |
| 66 |
|
| 67 |
PRIVATE int ninstr = 1; /* number of instruments */ |
| 68 |
PRIVATE int staffs[MAX_STAFFS] = {0,1}; /* number of staffs for ith instrument*/ |
| 69 |
PRIVATE int nstaffs = 1; /* number of staffs */ |
| 70 |
PRIVATE char terminator[MAX_STAFFS]; /* one of '&' "|', '$' */ |
| 71 |
|
| 72 |
PRIVATE char notes[MAX_STAFFS][LINE_LEN]; |
| 73 |
/* accumulate commands to be output */ |
| 74 |
PRIVATE char *n_notes[MAX_STAFFS]; |
| 75 |
PRIVATE bool outputs = false; /* are there commands to be output? */ |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
PRIVATE void |
| 80 |
usage () |
| 81 |
{ |
| 82 |
printf ("Usage: tex2aspc [-v | --version | -h | --help]\n"); |
| 83 |
printf (" tex2aspc infile[.tex] [outfile[.aspc]]\n"); |
| 84 |
} |
| 85 |
|
| 86 |
|
| 87 |
PRIVATE |
| 88 |
void analyze_notes (char **ln) |
| 89 |
/* append material from *ln to \en to notes[*] and initialize terminator[*]. */ |
| 90 |
{ |
| 91 |
int i; char *s; char *t; |
| 92 |
int newlines = 0; |
| 93 |
s = *ln+1; /* skip "/" */ |
| 94 |
while (isalpha(*s)) {s++;} /* skip rest of the initial command */ |
| 95 |
while (true) |
| 96 |
{ /* look for \en */ |
| 97 |
t = strstr(s, "\\en"); |
| 98 |
if (t != NULL) break; |
| 99 |
/* replace EOL by a blank and append another line of input */ |
| 100 |
{ char new_line[LINE_LEN]; |
| 101 |
char *nl; |
| 102 |
if (fgets (new_line, LINE_LEN, infile) == NULL) |
| 103 |
error ("Unexpected EOF."); |
| 104 |
if (strlen (new_line) == LINE_LEN-1) |
| 105 |
error ("Line too long."); |
| 106 |
nl = new_line; |
| 107 |
while (*nl == ' ') nl++; /* avoid spaces */ |
| 108 |
t = strpbrk (s, "\n%"); |
| 109 |
if (t == NULL) error ("Missing EOL."); |
| 110 |
if (*t == '\n') |
| 111 |
{ *t = ' '; t++;} |
| 112 |
*t = '\0'; |
| 113 |
if (append (line, &t, nl, LINE_LEN) >= LINE_LEN) |
| 114 |
error ("Line too long."); |
| 115 |
newlines++; |
| 116 |
} |
| 117 |
} |
| 118 |
/* t -> "\en" */ |
| 119 |
if ( prefix("\\enotes", t) ) *ln = t+7; |
| 120 |
else *ln = t+3; |
| 121 |
*t = '$'; /* replace '\' by '$' (a distinctive terminating character) */ |
| 122 |
for (i=1; i <= nstaffs; i++) |
| 123 |
{ char *tt; |
| 124 |
while (*s == ' ') s++; /* skip spaces */ |
| 125 |
tt = strpbrk (s, "|&$"); |
| 126 |
if (tt == NULL) error ("can't parse note-spacing command."); |
| 127 |
while (s < tt) |
| 128 |
{ *n_notes[i] = *s; |
| 129 |
n_notes[i]++; s++; |
| 130 |
} |
| 131 |
*n_notes[i] = '\0'; |
| 132 |
terminator[i] = *s; |
| 133 |
if (*s != '$') s++; |
| 134 |
} |
| 135 |
lineno = lineno + newlines; |
| 136 |
} |
| 137 |
|
| 138 |
PRIVATE |
| 139 |
void process_command (char **ln) |
| 140 |
{ char *s; |
| 141 |
if ( prefix("\\def\\nbinstrument", *ln) || |
| 142 |
prefix("\\instrumentnumber", *ln) ) |
| 143 |
{ |
| 144 |
s = strpbrk (*ln, "123456789"); |
| 145 |
if ( s == NULL ) error ("instrument number command unreadable."); |
| 146 |
ninstr = atoi (s); |
| 147 |
while (*ln <= s) { putc (**ln, outfile); (*ln)++;} |
| 148 |
} |
| 149 |
|
| 150 |
else if ( prefix("\\setstaffs", *ln) ) |
| 151 |
{ /* for now, just record in staffs[i] */ |
| 152 |
int n, p; |
| 153 |
s = strpbrk (*ln, "123456789"); |
| 154 |
if ( s == NULL ) error ("\\setstaffs command unreadable."); |
| 155 |
n = (int)(*s) - (int)('0'); /* instrument number */ |
| 156 |
s = strpbrk (s+1, "123456789"); |
| 157 |
if ( s == NULL ) error ("\\setstaffs command unreadable."); |
| 158 |
p = (int)(*s) - (int)('0'); /* number of staffs */ |
| 159 |
staffs[n] = p; |
| 160 |
while (*ln <= s) { putc (**ln, outfile); (*ln)++;} |
| 161 |
} |
| 162 |
|
| 163 |
else if ( prefix("\\startpiece", *ln) || |
| 164 |
prefix("\\startextract", *ln) ) |
| 165 |
{ int i, j; |
| 166 |
nstaffs = 1; |
| 167 |
for (j=2; j <= staffs[1]; j++) |
| 168 |
nstaffs++; |
| 169 |
for (i=2; i <= ninstr; i++) |
| 170 |
{ nstaffs++; |
| 171 |
for (j=2; j <= staffs[i]; j++) |
| 172 |
nstaffs++; |
| 173 |
} |
| 174 |
fputs (*ln, outfile); |
| 175 |
*ln =*ln + strlen(*ln); |
| 176 |
} |
| 177 |
|
| 178 |
else if ( prefix("\\nnnotes", *ln) || |
| 179 |
prefix("\\nnotes", *ln) || |
| 180 |
prefix("\\notes", *ln) || |
| 181 |
prefix("\\Notes", *ln) || |
| 182 |
prefix("\\NOtes", *ln) || |
| 183 |
prefix("\\NOTes", *ln) || |
| 184 |
prefix("\\NOTEs", *ln) ) |
| 185 |
{ char *s; |
| 186 |
analyze_notes (ln); |
| 187 |
outputs = true; |
| 188 |
s = strchr (*ln, '\n'); |
| 189 |
if (s == NULL) s = *ln + strlen (*ln); |
| 190 |
*ln = s+1; |
| 191 |
} |
| 192 |
|
| 193 |
else |
| 194 |
{ /* if there is output, generate it and re-initialize n_notes[*] */ |
| 195 |
if (outputs) |
| 196 |
{ int i; |
| 197 |
fputs ("\\anotes\n", outfile); |
| 198 |
for (i=1; i <= nstaffs; i++) |
| 199 |
{ |
| 200 |
fputs (" ", outfile); |
| 201 |
fputs (notes[i], outfile); |
| 202 |
if (terminator[i] == '$') |
| 203 |
fputs ("\\en", outfile); |
| 204 |
else |
| 205 |
fputc (terminator[i], outfile); |
| 206 |
fputc ('\n', outfile); |
| 207 |
n_notes[i] = notes[i]; |
| 208 |
*n_notes[i] = '\0'; |
| 209 |
} |
| 210 |
outputs = false; |
| 211 |
} |
| 212 |
fputs (*ln, outfile); |
| 213 |
*ln =*ln + strlen(*ln); |
| 214 |
} |
| 215 |
|
| 216 |
} |
| 217 |
|
| 218 |
PRIVATE |
| 219 |
void process_line () |
| 220 |
{ |
| 221 |
char *ln; |
| 222 |
ln = &line[0]; |
| 223 |
while ( *ln != '\0') |
| 224 |
{ |
| 225 |
while (*ln == ' ') { ln++; fputc (' ', outfile); } |
| 226 |
if (*ln == '%') { |
| 227 |
fprintf (outfile, "%s", ln); |
| 228 |
return; |
| 229 |
} |
| 230 |
process_command (&ln); |
| 231 |
} |
| 232 |
} |
| 233 |
|
| 234 |
|
| 235 |
PRIVATE |
| 236 |
void process_score () |
| 237 |
{ |
| 238 |
int c; int i; |
| 239 |
lineno = 0; |
| 240 |
for (i=1; i < MAX_STAFFS; i++) |
| 241 |
{ |
| 242 |
staffs[i] = 1; |
| 243 |
n_notes[i] = notes[i]; |
| 244 |
*n_notes[i] = '\0'; |
| 245 |
} |
| 246 |
c = getc (infile); |
| 247 |
|
| 248 |
while ( c != EOF ) |
| 249 |
{ |
| 250 |
ungetc (c, infile); |
| 251 |
if ( fgets(line, LINE_LEN, infile) == NULL) |
| 252 |
error ("Unexpected EOF."); |
| 253 |
if (strlen (line) == LINE_LEN-1) |
| 254 |
error ("Line too long."); |
| 255 |
lineno++; |
| 256 |
process_line (); |
| 257 |
c = getc (infile); |
| 258 |
} /* c == EOF */ |
| 259 |
} |
| 260 |
|
| 261 |
int main (int argc, char *argv[]) |
| 262 |
{ |
| 263 |
int c; |
| 264 |
char today[12]; |
| 265 |
time_t mytime; |
| 266 |
# define NOPTS 3 |
| 267 |
struct option longopts[NOPTS] = |
| 268 |
{ { "help", 0, NULL, 'h'}, |
| 269 |
{ "version", 0, NULL, 'v'}, |
| 270 |
{ NULL, 0, NULL, 0} |
| 271 |
}; |
| 272 |
|
| 273 |
time (&mytime); |
| 274 |
strftime (today, 11, "%Y-%m-%d", localtime (&mytime) ); |
| 275 |
printf ("This is tex2aspc, version %s.\n", version); |
| 276 |
printf ("Copyright (C) 2017 R. D. Tennent\n" ); |
| 277 |
printf ("School of Computing, Queen's University, [email protected]\n" ); |
| 278 |
printf ("License GNU GPL version 2 or later <http://gnu.org/licences/gpl.html>.\n" ); |
| 279 |
printf ("There is NO WARRANTY, to the extent permitted by law.\n\n" ); |
| 280 |
|
| 281 |
c = getopt_long (argc, argv, "hv", longopts, NULL); |
| 282 |
while (c != -1) |
| 283 |
{ |
| 284 |
switch (c) |
| 285 |
{ |
| 286 |
case 'h': |
| 287 |
usage (); |
| 288 |
printf ("Please report bugs to [email protected].\n" ); |
| 289 |
exit (0); |
| 290 |
case 'v': |
| 291 |
exit (0); |
| 292 |
case '?': |
| 293 |
exit (EXIT_FAILURE); |
| 294 |
default: |
| 295 |
printf ("Function getopt returned character code 0%o.\n", |
| 296 |
(unsigned int) c); |
| 297 |
exit (EXIT_FAILURE); |
| 298 |
} |
| 299 |
c = getopt_long (argc, argv, "hv", longopts, NULL); |
| 300 |
} |
| 301 |
|
| 302 |
infilename[0] = '\0'; |
| 303 |
infilename_n = infilename; |
| 304 |
if (optind < argc) |
| 305 |
append (infilename, &infilename_n, argv[optind], sizeof (infilename)); |
| 306 |
else |
| 307 |
{ usage(); |
| 308 |
exit (EXIT_FAILURE); |
| 309 |
} |
| 310 |
infile = fopen (infilename, "r"); |
| 311 |
if (infile == NULL) |
| 312 |
{ |
| 313 |
if (suffix (".tex", infilename) ) |
| 314 |
{ printf ("Can't open %s\n", infilename); |
| 315 |
exit (EXIT_FAILURE); |
| 316 |
} |
| 317 |
else |
| 318 |
{ append (infilename, &infilename_n, ".tex", sizeof (infilename)); |
| 319 |
infile = fopen (infilename, "r"); |
| 320 |
if (infile == NULL) |
| 321 |
{ printf ("Can't open %s\n", infilename); |
| 322 |
exit (EXIT_FAILURE); |
| 323 |
} |
| 324 |
} |
| 325 |
} |
| 326 |
printf ("Reading from %s.", infilename); |
| 327 |
|
| 328 |
optind++; |
| 329 |
outfilename[0] = '\0'; |
| 330 |
outfilename_n = outfilename; |
| 331 |
if (optind < argc) /* user-provided outfilename */ |
| 332 |
{ |
| 333 |
append (outfilename, &outfilename_n, argv[optind], sizeof (outfilename)); |
| 334 |
if (!suffix (".aspc", outfilename)) |
| 335 |
append (outfilename, &outfilename_n, ".aspc", sizeof (outfilename)); |
| 336 |
} |
| 337 |
else |
| 338 |
{ /* output to infile.aspc */ |
| 339 |
append (outfilename, &outfilename_n, infilename, sizeof (outfilename)); |
| 340 |
outfilename_n -= 4; |
| 341 |
*outfilename_n = '\0'; |
| 342 |
append (outfilename, &outfilename_n, ".aspc", sizeof (outfilename)); |
| 343 |
} |
| 344 |
|
| 345 |
outfile = fopen (outfilename, "w"); |
| 346 |
if (outfile == NULL) |
| 347 |
{ printf ("Can't open %s\n", outfilename); |
| 348 |
exit (EXIT_FAILURE); |
| 349 |
} |
| 350 |
printf (" Writing to %s.", outfilename); |
| 351 |
|
| 352 |
printf ("\n\n"); |
| 353 |
|
| 354 |
fprintf (outfile, "%% Generated by tex2aspc (%s).\n", version); |
| 355 |
process_score (); |
| 356 |
|
| 357 |
return 0; |
| 358 |
} |