Debian Codegraph @

utils/autosp/autosp-src/rebar.c

raw
1 char version[12] = "2019-01-08";
2
3 /* Copyright (C) 2015-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 *
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 /* rebar - corrects bar numbers in a MusiXTeX score
25 */
26
27 # include "utils.h"
28
29 # define SHORT_LEN 256
30
31 PRIVATE char infilename[SHORT_LEN];
32 PRIVATE char *infilename_n = infilename;
33 PRIVATE char outfilename[SHORT_LEN];
34 PRIVATE char *outfilename_n = outfilename;
35
36 PRIVATE FILE *infile, *outfile;
37
38
39 PRIVATE int barno = 0;
40 PRIVATE int startbarno = 1;
41
42 PRIVATE char line[LINE_LEN]; /* line of input */
43
44 PRIVATE void
45 usage (FILE *f)
46 {
47 fprintf (f, "Usage: rebar [-v | --version | -h | --help]\n");
48 fprintf (f, " rebar [ infile.tex [outfile[.tex] ] ]\n");
49 fprintf (f, " rebar [ infile.aspc [outfile[.aspc]] ]\n");
50 }
51
52 PRIVATE void
53 error_at (const char msg[]) /* abort with stderr message msg */
54 {
55 fprintf (stderr, "Error on line %d: %s\n", lineno, msg);
56 exit (EXIT_FAILURE);
57 }
58
59
60 PRIVATE void
61 process_command (char **ln)
62 { char *t;
63 if ( prefix ("\\startbarno", *ln ) )
64 {
65 t = *ln + 11;
66 if (*t == '=') t++;
67 if ( sscanf (t, "%d", &startbarno) != 1)
68 error ("Expected startbarno");
69 fprintf (outfile, "%s=%d", "\\startbarno", startbarno);
70 t = strpbrk (t+1, "%\\\n");
71 if (t == NULL) t = *ln + strlen (*ln);
72 *ln = t;
73 }
74 else if ( prefix("\\startpiece", *ln) )
75 {
76 barno = startbarno;
77 fprintf (outfile, "%s%%%d\n", "\\startpiece", barno);
78 t = strpbrk (*ln+1, "%\\\n");
79 if (t == NULL) t = *ln + strlen (*ln);
80 *ln = t;
81 }
82 else if ( prefix("\\startextract", *ln) )
83 {
84 barno = startbarno;
85 fprintf (outfile, "%s%%%d\n", "\\startextract", barno);
86 t = strpbrk (*ln+1, "%\\\n");
87 if (t == NULL) t = *ln + strlen (*ln);
88 *ln = t;
89 }
90 else if ( prefix ("\\barno", *ln) )
91 {
92 t = *ln + 6;
93 if (*t == '=') t++;
94 if ( sscanf (t, "%d", &barno) != 1)
95 error ("Expected barno");
96 t = strpbrk (*ln+6, "%\\\n");
97 if (t == NULL) t = *ln + strlen (*ln);
98 while (*ln < t)
99 { fputc (**ln, outfile);
100 (*ln)++;
101 }
102 }
103 else if ( prefix ("\\advance\\barno", *ln) )
104 {
105 int barno_increment = 0;
106 t = strpbrk (*ln, "-0123456789");
107 if ( (*ln == NULL) || (sscanf (t, "%d", &barno_increment) != 1 ) )
108 error ("Expected barno increment/decrement");
109 barno = barno + barno_increment;
110 t = strpbrk (*ln+13, "%\\\n");
111 if (t == NULL) t = *ln + strlen (*ln);
112 while (*ln < t)
113 { fputc (**ln, outfile);
114 (*ln)++;
115 }
116 }
117 else if ( prefix("\\bar", *ln)
118 || prefix("\\alaligne", *ln)
119 || prefix("\\alapage", *ln)
120 || prefix("\\changecontext", *ln)
121 || prefix("\\Changecontext", *ln)
122 || prefix("\\contpiece", *ln)
123 || prefix("\\Contpiece", *ln)
124 || prefix("\\xbar", *ln)
125 || prefix("\\leftrepeat", *ln)
126 || prefix("\\rightrepeat", *ln)
127 || prefix("\\leftrightrepeat", *ln) )
128 {
129 barno++;
130 t = strpbrk (*ln+1, "%\\\n");
131 if (t == NULL) t = *ln + strlen (*ln);
132 while (*ln < t)
133 {
134 putc (**ln, outfile);
135 (*ln)++;
136 }
137 fprintf (outfile, "%%%d\n", barno);
138 t = strpbrk (*ln+1, "\\\n");
139 if (t != NULL && *t == '\n')
140 t++; /* to prevent blank lines */
141 else if (t == NULL) t = *ln + strlen (*ln);
142 *ln = t;
143 }
144
145 else /* everything else */
146 { t = strpbrk (*ln+1, "%\\");
147 if (t == NULL || *t == '%') t = *ln + strlen (*ln);
148 while (*ln < t)
149 { fputc (**ln, outfile);
150 (*ln)++;
151 }
152 }
153 }
154
155 PRIVATE
156 void process_line ()
157 {
158 char *ln;
159 ln = &line[0];
160 while ( *ln != '\0')
161 {
162 while (*ln == ' ') { ln++; fputc (' ', outfile); }
163 if (*ln == '%') {
164 fprintf (outfile, "%s", ln);
165 return;
166 }
167 process_command (&ln);
168 }
169 }
170
171
172 PRIVATE void
173 process_score (void)
174 /* process .tex file */
175 {
176 int c;
177 while ( (c = getc (infile)) != EOF )
178 {
179 ungetc (c, infile);
180 lineno++;
181 if ( fgets(line, LINE_LEN, infile) == NULL)
182 error_at("Unexpected EOF.");
183 if (strlen (line) == LINE_LEN-1)
184 error_at("Line too long.");
185 process_line ();
186 } /* c == EOF */
187 }
188
189
190 int
191 main (int argc, char *argv[])
192 {
193 int c;
194 char today[12];
195 time_t mytime;
196 # define NOPTS 3
197 struct option longopts[NOPTS] =
198 { { "help", 0, NULL, 'h'},
199 { "version", 0, NULL, 'v'},
200 { NULL, 0, NULL, 0}
201 };
202
203 time (&mytime);
204 strftime (today, 11, "%Y-%m-%d", localtime (&mytime) );
205 c = getopt_long (argc, argv, "hv", longopts, NULL);
206 while (c != -1)
207 {
208 switch (c)
209 {
210 case 'h':
211 fprintf (stdout, "This is rebar, version %s.\n", version);
212 usage (stdout);
213 fprintf (stdout, "Please report bugs to [email protected].\n" );
214 exit (0);
215 case 'v':
216 fprintf (stdout, "This is rebar, version %s.\n", version);
217 exit (0);
218 case '?':
219 exit (EXIT_FAILURE);
220 default:
221 fprintf (stderr,"Function getopt returned character code 0%o.\n",
222 (unsigned int) c);
223 exit (EXIT_FAILURE);
224 }
225 c = getopt_long (argc, argv, "hv", longopts, NULL);
226 }
227 fprintf (stderr, "This is rebar, version %s.\n", version);
228 fprintf (stderr, "Copyright (C) 2015-16 R. D. Tennent\n" );
229 fprintf (stderr, "School of Computing, Queen's University, [email protected]\n" );
230 fprintf (stderr, "License GNU GPL version 2 or later <http://gnu.org/licences/gpl.html>.\n" );
231 fprintf (stderr, "There is NO WARRANTY, to the extent permitted by law.\n\n" );
232
233 if (optind < argc)
234 {
235 infilename[0] = '\0';
236 infilename_n = infilename;
237 append (infilename, &infilename_n, argv[optind], sizeof (infilename));
238 if ( !suffix (".aspc", infilename) && !suffix (".tex", infilename) )
239 { usage( stderr );
240 exit (EXIT_FAILURE);
241 }
242 infile = fopen (infilename, "r");
243 if (infile == NULL )
244 { fprintf (stderr, "Can't open %s\n", infilename);
245 exit (EXIT_FAILURE);
246 }
247 fprintf (stderr, "Reading from %s.", infilename);
248 }
249 else
250 { infile = stdin;
251 fprintf (stderr, "Reading from stdin.");
252 }
253
254 optind++;
255 if (optind < argc)
256 {
257 outfilename[0] = '\0';
258 outfilename_n = outfilename;
259 append (outfilename, &outfilename_n, argv[optind], sizeof (outfilename));
260 if (!suffix (".tex", outfilename) && !suffix (".aspc", outfilename) )
261 {
262 if ( suffix (".tex", infilename) )
263 append (outfilename, &outfilename_n, ".tex", sizeof (outfilename));
264 else
265 append (outfilename, &outfilename_n, ".aspc", sizeof (outfilename));
266 }
267 outfile = fopen (outfilename, "w");
268 if (outfile == NULL)
269 { fprintf (stderr,"Can't open %s\n", outfilename);
270 exit (EXIT_FAILURE);
271 }
272 fprintf (stderr, " Writing to %s.\n\n", outfilename);
273 }
274 else
275 { outfile = stdout;
276 fprintf (stderr, " Writing to stdout.\n\n");
277 }
278
279 process_score ();
280
281 return 0;
282 }