Debian Codegraph @

utils/autosp/autosp-src/autosp.c

raw
1 char version[12] = "2025-10-26";
2
3 /* Copyright (C) 2014-25 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 /* autosp - preprocessor to generate note-spacing commands for MusiXTeX scores */
25
26 /* Usage: autosp [-v | --version | -h | --help]
27 * autosp [-d | --dotted] [-l | --log] infile[.aspc] [outfile[.tex]]
28 *
29 * Options
30 * --dotted (-d) suppresses extra spacing for dotted beam notes.
31 * --log (-l) generate a log file
32 *
33 *
34 *
35 * Key functions:
36 *
37 * process_command, which processes commands
38 * \instrumentnumber
39 * \nbinstrument
40 * \setstaffs
41 * \startpiece
42 * \TransformNotes
43 * \def\vnotes
44 * \def\atnextbar
45 * \anotes etc.
46 * \bar
47 * \endpiece etc.
48 *
49 * analyze_notes, which determines pointers notes[i] and
50 * characters terminator[i] for each staff; '$' is used
51 * as the terminator for the last staff.
52 *
53 * generate_notes, which repeatedly uses spacing_note (i) to
54 * determine spacings[i] of the next spacing note in
55 * each staff; the current spacing is then the smallest
56 * of these. If necessary, the current notes command
57 * is terminated by terminate_notes() and a new notes
58 * command initiated by initialize_notes(). The current global
59 * skip and note commands for each staff are then copied to
60 * outstrings[i] using output_notes (i). When necessary,
61 * vspacing[i] is set to generate subsequent skips.
62 * Appoggiaturas and xtuplets are treated separately.
63 *
64 * spacing_note (i), which searches for the next spacing note
65 * in the i'th staff, unless a "virtual note" (skip) is
66 * needed.
67 *
68 * output_notes (i), which copies a global_skip string and
69 * outstrings[i] to output, using filter_output (i) to discard
70 * commas and global and large skips, and insert collective-coding
71 * prefixes and closing braces.
72 *
73 * terminate_notes, which copies outstrings[i] to output,
74 * in the order determined by TransformNotes2.
75 *
76 * Spacing values are expressed as sums of binary multiples of
77 * SMALL_NOTE (by default, 1/256th); for example, half-note
78 * spacing = SP(2) = SMALL_NOTE/2. Special small or large
79 * values are used for appoggiaturas and bar-rests.
80 *
81 */
82
83 # include "utils.h"
84
85 #ifdef HAVE_CONFIG_H /* for TeXLive */
86 #include <config.h>
87 #endif
88
89 # ifdef KPATHSEA
90 # include <kpathsea/getopt.h>
91 # else
92 # include <getopt.h>
93 # endif
94
95 # ifdef HAVE_STDBOOL_H
96 # include <stdbool.h>
97 # else
98 # ifndef HAVE__BOOL
99 # define _Bool signed char
100 # endif
101 # define bool _Bool
102 # define true 1
103 # define false 0
104 # endif
105
106 # define PRIVATE static
107
108 # define SHORT_LEN 256
109
110 char infilename[SHORT_LEN];
111 char *infilename_n;
112 char outfilename[SHORT_LEN];
113 char *outfilename_n;
114 char logfilename[SHORT_LEN];
115 char *logfilename_n;
116 FILE *infile, *outfile, *logfile;
117
118
119 void process_score ();
120
121 PRIVATE void
122 usage ()
123 {
124 printf ("Usage: autosp [-v | --version | -h | --help]\n");
125 printf (" autosp [-d | --dotted] [-l | --log] infile[.aspc] [outfile[.tex]]\n");
126 }
127
128
129 bool dottedbeamnotes = false; /* dotted beam notes ignored? */
130 bool debug = false;
131
132 int main (int argc, char *argv[])
133 {
134 int c;
135 # define NOPTS 5
136 struct option longopts[NOPTS] =
137 { { "help", 0, NULL, 'h'},
138 { "version", 0, NULL, 'v'},
139 { "dotted", 0, NULL, 'd'},
140 { "log", 0, NULL, 'l'},
141 { NULL, 0, NULL, 0}
142 };
143
144 printf ("This is autosp, version %s.\n", version);
145 printf ("Copyright (C) 2014-24 R. D. Tennent\n" );
146 printf ("School of Computing, Queen's University, [email protected]\n" );
147 printf ("License GNU GPL version 2 or later <http://gnu.org/licences/gpl.html>.\n" );
148 printf ("There is NO WARRANTY, to the extent permitted by law.\n\n" );
149
150 c = getopt_long (argc, argv, "hvdl", longopts, NULL);
151 while (c != -1)
152 {
153 switch (c)
154 {
155 case 'h':
156 usage ();
157 printf ("Please report bugs to [email protected].\n" );
158 exit (0);
159 case 'v':
160 exit (0);
161 case 'd':
162 dottedbeamnotes = true;
163 break;
164 case 'l':
165 debug = true;
166 break;
167 case '?':
168 exit (EXIT_FAILURE);
169 default:
170 printf ("Function getopt returned character code 0%o.\n",
171 (unsigned int) c);
172 exit (EXIT_FAILURE);
173 }
174 c = getopt_long (argc, argv, "hvd", longopts, NULL);
175 }
176
177 infilename[0] = '\0';
178 infilename_n = infilename;
179 if (optind < argc)
180 { append (infilename, &infilename_n, argv[optind], sizeof (infilename));
181 if (!suffix (".aspc", infilename))
182 append (infilename, &infilename_n, ".aspc", sizeof (infilename));
183 }
184 else
185 { usage();
186 exit (EXIT_FAILURE);
187 }
188 infile = fopen (infilename, "r");
189 if (infile == NULL )
190 { printf ("Can't open %s\n", infilename);
191 exit (EXIT_FAILURE);
192 }
193
194 printf ("Reading from %s.", infilename);
195
196 optind++;
197 outfilename[0] = '\0';
198 outfilename_n = outfilename;
199 if (optind < argc) /* user-provided outfilename */
200 {
201 append (outfilename, &outfilename_n, argv[optind], sizeof (outfilename));
202 if (!suffix (".tex", outfilename))
203 append (outfilename, &outfilename_n, ".tex", sizeof (outfilename));
204 }
205 else if (suffix (".aspc", infilename) )
206 { /* output to infile.tex */
207 append (outfilename, &outfilename_n, infilename, sizeof (outfilename));
208 outfilename_n -= 4;
209 *outfilename_n = '\0';
210 append (outfilename, &outfilename_n, "tex", sizeof (outfilename));
211 }
212
213 if (*outfilename == '\0')
214 {
215 printf ("\nPlease provide a filename for the output.\n\n");
216 exit (EXIT_FAILURE);
217 }
218 else
219 {
220 outfile = fopen (outfilename, "w");
221 if (outfile == NULL)
222 { printf ("Can't open %s\n", outfilename);
223 exit (EXIT_FAILURE);
224 }
225 printf (" Writing to %s.", outfilename);
226 }
227
228 logfilename[0] = '\0';
229 logfilename_n = logfilename;
230 append (logfilename, &logfilename_n, infilename, sizeof (logfilename));
231 if (suffix (".aspc", infilename) )
232 logfilename_n -= 4;
233 else
234 logfilename_n -= 3;
235 *logfilename_n = '\0';
236 append (logfilename, &logfilename_n, "alog", sizeof (logfilename));
237 if (debug)
238 { logfile = fopen (logfilename, "w");
239 if (logfile == NULL)
240 { printf ("Can't open %s\n", logfilename);
241 exit (EXIT_FAILURE);
242 }
243 printf (" Log file %s.", logfilename);
244 }
245 printf ("\n\n");
246
247 fprintf (outfile, "%% Generated by autosp (%s).\n", version);
248
249 process_score ();
250
251 return 0;
252 }