Debian Codegraph @

texk/musixtnt/musixtnt-src/msxlint.c

raw
1 char version[12] = "2016-01-30";
2
3 /* Copyright (C) 2014-16 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 *
18 * You should have received a copy of the GNU General
19 * Public License along with this program; if not, write to
20 * the Free Software Foundation, Inc., 51 Franklin Street,
21 * Fifth Floor, Boston, MA 02110-1301, USA.
22 */
23
24 /* msxlint - detects incorrectly formatted notes lines
25 * in a MusiXTeX source file.
26 *
27 * Usage: msxlint [-v | --version | -h | --help]
28 * msxlint [-d | --debug] { infile[.tex] } ...
29 *
30 */
31
32
33 # include "utils.h"
34
35 PRIVATE int debug = 0;
36 PRIVATE char infilename[LINE_LEN];
37 PRIVATE char *infilename_n = infilename;
38
39 PRIVATE FILE *infile;
40
41 PRIVATE char line[LINE_LEN]; /* line of input */
42 PRIVATE int lineno; /* line number */
43 PRIVATE int ninstr; /* number of instruments */
44 PRIVATE int staffs[12]; /* staffs[i] is the number of staffs
45 for instrument i */
46 PRIVATE char terminator[8]; /* "\en" or "\enotes" or "" (either) */
47 PRIVATE int nerrors; /* number of error messages */
48 # define NERRORSMAX 10
49
50 PRIVATE void
51 checkc (char *s, char c)
52 { if (*s != c)
53 printf ("Expected %c but found %c in line %d:\n%s\n", c, *s, lineno, line);
54 }
55
56 PRIVATE void
57 checkn (char *s)
58 { if (strpbrk (s, "0123456789") != s)
59 printf ("Expected digit but found %c in line %d:\n%s\n", *s, lineno, line);
60 }
61
62 PRIVATE void
63 usage (void)
64 {
65 puts ("Usage: msxlint [-v | --version | -h | --help]");
66 puts (" msxlint [-d | --debug] ... { infile[.tex] } ...");
67 }
68
69 PRIVATE void
70 analyze_notes ( char **ln) {
71 int i,j;
72 int newlines = 0;
73 char *s; char *t;
74 s = strpbrk (*ln+1, "|&\\\n"); /* skip initial command */
75 while (true)
76 { /* look for \en */
77 t = strstr(s, "\\en");
78 if (t != NULL) break;
79 else
80 /* replace EOL by a blank and append another line of input */
81 { char new_line[LINE_LEN];
82 if (debug > 2) printf (" Reading additional line of input:\n");
83 if ( fgets (new_line, LINE_LEN, infile) == NULL)
84 error("Unexpected EOF.");
85 if ( (t = strchr (s, '\n')) == NULL )
86 error("Missing EOL.");
87 *t = ' ';
88 t++;
89 *t = '\0';
90 if (append (line, &t, new_line, LINE_LEN) >= LINE_LEN)
91 error ("Line too Long.");
92 if (debug > 2) printf (" %s\n", t+1);
93 newlines++;
94 }
95 }
96 for (i=1; i <= ninstr; i++)
97 {
98 for (j=1; j < staffs[i]; j++)
99 { s = strpbrk (s, "|&");
100 if (s == NULL || *s == '&' || s > t)
101 {
102 printf ("Too few staffs for instrument %d in notes on line %d:\n%s", i, lineno, line);
103 nerrors++;
104 *ln = t+3;
105 return;
106 }
107 checkc (s, '|'); s++;
108 }
109 if (i == ninstr) break;
110 s = strpbrk (s, "&|");
111 if (s == NULL || s > t)
112 {
113 printf ("Too few instruments in notes on line %d:\n%s", lineno, line);
114 nerrors++;
115 *ln = t+3;
116 return;
117 }
118 if (*s == '|')
119 {
120 printf ("Too many staffs for instrument %d in notes on line %d:\n%s", i, lineno, line);
121 nerrors++;
122 *ln = t+3;
123 return;
124 }
125 checkc (s, '&'); s++;
126 }
127 s = strpbrk (s, "|&");
128 if (s != NULL && s < t)
129 {
130 printf ("Too many fields in notes on line %d:\n%s", lineno, line);
131 nerrors++;
132 *ln = t+3;
133 return;
134 }
135 s = t; /* \en */
136 if (terminator[0] != '\0')
137 { if (!prefix (terminator, s) )
138 {
139 printf ("Expected %s at end of notes on line %d:\n%s", terminator, lineno, line);
140 nerrors++;
141 t = strchr(s, '\\');
142 if (t == NULL) t = *ln + strlen (*ln);
143 *ln = t;
144 return;
145 }
146 if ( terminator[3] == '\0' && prefix ("otes", s+3) )
147 {
148 printf ("Expected %s at end of notes on line %d:\n%s", terminator, lineno, line);
149 nerrors++;
150 t = strchr(s, '\\');
151 if (t == NULL) t = *ln + strlen (*ln);
152 *ln = t;
153 return;
154 }
155 }
156 lineno = lineno + newlines;
157 t = strchr(s+1, '\\');
158 if (t == NULL) t = *ln + strlen (*ln);
159 *ln = t;
160 return;
161 }
162
163 PRIVATE void
164 process_command (char **ln)
165 { char *s, *t;
166 if ( prefix("\\instrumentnumber", *ln) )
167 {
168 if (debug > 1) printf ("%s\n", " Processing \\instrumentnumber");
169 s = strpbrk (*ln, "123456789");
170 if ( s == NULL ) error ("\\instrumentnumber command unreadable.");
171 ninstr = atoi (s);
172 t = strpbrk (*ln+1, "\\");
173 if (t == NULL)
174 { *ln = *ln + strlen (*ln); return; }
175 *ln = t;
176 }
177
178 if ( prefix("\\def\\nbinstrument", *ln) )
179 {
180 if (debug > 1) printf ("%s\n", " Processing \\def\\nbinstrument");
181 s = strpbrk (*ln, "123456789");
182 if ( s == NULL ) error ("\\def\\nbinstrument command unreadable.");
183 ninstr = atoi (s);
184 t = strpbrk (*ln+1, "\\");
185 if (t == NULL)
186 { *ln = *ln + strlen (*ln); return; }
187 *ln = t;
188 }
189
190 else if ( prefix("\\setstaffs", *ln) )
191 {
192 int n, p;
193 if (debug > 1) printf ("%s\n", " Processing \\setstaffs");
194 s = strpbrk (*ln, "123456789");
195 if ( s == NULL ) error ("\\setstaffs command unreadable.");
196 n = (int)(*s) - (int)('0'); /* instrument number */
197 s = strpbrk (s+1, "123456789");
198 if ( s == NULL ) error ("\\setstaffs command unreadable.");
199 p = (int)(*s) - (int)('0'); /* number of staffs */
200 staffs[n] = p;
201 t = strpbrk (*ln+1, "\\");
202 if (t == NULL)
203 { *ln = *ln + strlen (*ln); return; }
204 *ln = t;
205 }
206
207 else if ( prefix("\\def\\vnotes#1\\elemskip", *ln) )
208 {
209 if (debug > 1) printf ("%s\n", " Processing \\def\\vnotes");
210 s = *ln + 22; /* first parameter */
211 ninstr = 0;
212 while (true)
213 { ninstr++;
214 staffs[ninstr] = 1;
215 checkc (s, '#'); s++;
216 checkn (s); s++;
217 while (*s == '|')
218 { staffs[ninstr]++;
219 checkc (s, '|'); s++;
220 checkc (s, '#'); s++;
221 checkn (s); s++;
222 }
223 if (*s != '&') break;
224 checkc (s, '&'); s++;
225 }
226 t = &terminator[0];
227 while (*s != '{')
228 { *t = *s; s++; t++; }
229 *t = '\0';
230 *ln = strchr (*ln, '}') ;
231 (*ln)++;
232 }
233
234 else if ( prefix("\\TransformNotes", *ln) )
235 {
236 if (debug > 1) printf ("%s\n", " Processing \\TransformNotes");
237 s = *ln + 16; /* first parameter */
238 ninstr = 0;
239 while (true)
240 { ninstr++;
241 staffs[ninstr] = 1;
242 checkc (s, '#'); s++;
243 checkn (s); s++;
244 while (*s == '|')
245 { staffs[ninstr]++;
246 checkc (s, '|'); s++;
247 checkc (s, '#'); s++;
248 checkn (s); s++;
249 }
250 if (*s != '&') break;
251 checkc (s, '&'); s++;
252 }
253 t = &terminator[0];
254 terminator[0] = '\0';
255 append ( t, &t, "\\en", sizeof (terminator) );
256 do s++; while (*s != '{');
257 *ln = strchr (s, '}') ;
258 if (*ln == NULL) error ("Can't find '}' after \\TransformNotes.");
259 (*ln)++;
260 }
261
262 else if ( prefix("\\notes", *ln) ||
263 prefix("\\Notes", *ln) ||
264 prefix("\\NOtes", *ln) ||
265 prefix("\\NOTes", *ln) ||
266 prefix("\\NOTEs", *ln) ||
267 prefix("\\nnotes", *ln) ||
268 prefix("\\vnotes", *ln) ||
269 prefix("\\znotes", *ln)
270 )
271 {
272 if (debug > 1) printf ("%s\n", " Processing notes");
273 analyze_notes (ln);
274 t = strpbrk (*ln, "\\\n");
275 if (t == NULL)
276 { *ln = *ln + strlen (*ln); return; }
277 *ln = t;
278 }
279
280 else if ( prefix("\\def\\atnextbar", *ln))
281 {
282 if (debug > 1) printf ("%s\n", " Processing \\def\\atnextbar");
283 s = strstr (*ln, "\\znotes");
284 if (s != NULL)
285 {
286 *ln = s;
287 analyze_notes (ln);
288 }
289 t = strpbrk (*ln, "\\");
290 if (t == NULL)
291 { *ln = *ln + strlen (*ln); return; }
292 *ln = t;
293 }
294
295 else if ( prefix("\\def", *ln) )
296 *ln = *ln + strlen (*ln);
297
298 else /* anything else */
299 {
300 if (debug > 1) printf (" Processing ");
301 t = strpbrk (*ln+1, "\\\n");
302 if (t == NULL) t = *ln + strlen (*ln);
303 if (debug > 1)
304 { char *s = *ln; do {putchar (*s); s++;} while (s != t); puts ("");}
305 *ln = t;
306 }
307 }
308
309 PRIVATE void
310 process_line (void)
311 {
312 char *ln;
313 ln = &line[0];
314 while ( *ln != '\0')
315 {
316 while (*ln == ' ') { ln++;}
317 if (*ln == '%' || *ln == '\n') return;
318 process_command (&ln);
319 } /* *ln == '\0' */
320 }
321
322 PRIVATE void
323 process_score (void)
324 /* process .tex file */
325 {
326 int c; int i;
327 lineno = 0;
328 ninstr = 1;
329 nerrors = 0;
330 staffs[0] = 0;
331 for (i = 1; i<12; i++) staffs[i] = 1;
332 terminator[0] = '\0'; /* either \en or \enotes */
333 while ( (c = getc (infile)) != EOF )
334 {
335 ungetc (c, infile);
336 if (fgets(line, LINE_LEN, infile) == NULL) error("IO error");
337 if (strlen (line) == LINE_LEN-1) error("Line too long.");
338 lineno++;
339 if (debug > 0) printf ("Processing line %d.\n", lineno);
340 process_line ();
341 if (nerrors > NERRORSMAX) {
342 puts ("\nToo many errors; processing of this file aborted.");
343 return;
344 }
345 } /* c == EOF */
346 }
347
348
349 int
350 main (int argc, char *argv[])
351 {
352 int c;
353 char today[12];
354 time_t mytime;
355 # define NOPTS 4
356 struct option longopts[NOPTS] =
357 { { "help", 0, NULL, 'h'},
358 { "version", 0, NULL, 'v'},
359 { "debug", 0, NULL, 'd'},
360 { NULL, 0, NULL, 0}
361 };
362
363 time (&mytime);
364 strftime (today, 11, "%Y-%m-%d", localtime (&mytime) );
365 printf ("This is msxlint, version %s.\n", version);
366 c = getopt_long (argc, argv, "hvd", longopts, NULL);
367 while (c != -1)
368 {
369 switch (c)
370 {
371 case 'h':
372 usage ();
373 puts ( "Please report bugs to [email protected]." );
374 exit (0);
375 case 'v':
376 exit (0);
377 case 'd':
378 debug++;
379 break;
380 case '?':
381 exit (EXIT_FAILURE);
382 default:
383 fprintf (stderr,"Function getopt returned character code 0%o.\n",
384 (unsigned int) c);
385 exit (EXIT_FAILURE);
386 }
387 c = getopt_long (argc, argv, "hvd", longopts, NULL);
388 }
389 puts ( "Copyright (C) 2014-16 R. D. Tennent" );
390 puts ( "School of Computing, Queen's University, [email protected]" );
391 puts ( "License GNU GPL version 2 or later <http://gnu.org/licences/gpl.html>." );
392 puts ( "There is NO WARRANTY, to the extent permitted by law." );
393
394 if (optind == argc)
395 {
396 infile = stdin;
397 printf ("\nProcessing stdin.\n\n");
398 process_score ();
399 }
400 else
401 do
402 {
403 infilename[0] = '\0';
404 infilename_n = infilename;
405 append (infilename, &infilename_n, argv[optind], sizeof (infilename));
406 if (!suffix (".tex", infilename))
407 append (infilename, &infilename_n, ".tex", sizeof (infilename));
408 infile = fopen (infilename, "r");
409 if (infile == NULL)
410 {
411 fprintf (stderr,"Can't open %s\n", infilename);
412 optind++;
413 continue;
414 }
415 printf ("\nProcessing %s.\n\n", infilename);
416 process_score ();
417 optind++;
418 }
419 while (optind < argc);
420
421 return 0;
422 }