Debian Codegraph @

util1/csd_util/cs.c

raw
1
2 /* Csound launcher utility - written by Istvan Varga, Jan 2003 */
3
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <string.h>
7 #include <errno.h>
8 #include <sys/types.h>
9 #include <dirent.h>
10 #include <ctype.h>
11 #ifdef HAVE_UNISTD_H
12 #include <unistd.h>
13 #endif
14 #if defined(WIN32)
15 #include <process.h>
16 #endif
17
18 /* default Csound executable */
19 #ifdef WIN32
20 char default_csnd[] = "csound32 -W";
21 char default_csnd_r[] = "csound32 -h";
22 #else
23 char default_csnd[] = "csound -W";
24 char default_csnd_r[] = "csound32 -h";
25 #endif
26
27 /* default Csound flags */
28 char default_csfl[] = "-d -m135 -H1 -s";
29 char default_csfl_r[] = "-m128 -H0 -o dac";
30
31 char *orcname = NULL;
32 char *sconame = NULL;
33 char *midname = NULL;
34 char *csdname = NULL;
35
36 char dir_name[256], prefix[256];
37
38 int nr_files = 0, max_files = 0;
39 char **file_names = NULL;
40
41 /* returns non-zero if the specified file name has .orc extension */
42
43 int is_orc(char *s)
44 {
45 int n = strlen(s);
46 if (n < 5) return 0;
47 --n; if (s[n] != 'C' && s[n] != 'c') return 0;
48 --n; if (s[n] != 'R' && s[n] != 'r') return 0;
49 --n; if (s[n] != 'O' && s[n] != 'o') return 0;
50 --n; if (s[n] != '.') return 0;
51 return 1;
52 }
53
54 /* returns non-zero if the specified file name has .sco extension */
55
56 int is_sco(char *s)
57 {
58 int n = strlen(s);
59 if (n < 5) return 0;
60 --n; if (s[n] != 'O' && s[n] != 'o') return 0;
61 --n; if (s[n] != 'C' && s[n] != 'c') return 0;
62 --n; if (s[n] != 'S' && s[n] != 's') return 0;
63 --n; if (s[n] != '.') return 0;
64 return 1;
65 }
66
67 /* returns non-zero if the specified file name has .mid extension */
68
69 int is_mid(char *s)
70 {
71 int n = strlen(s);
72 if (n < 5) return 0;
73 --n; if (s[n] != 'D' && s[n] != 'd') return 0;
74 --n; if (s[n] != 'I' && s[n] != 'i') return 0;
75 --n; if (s[n] != 'M' && s[n] != 'm') return 0;
76 --n; if (s[n] != '.') return 0;
77 return 1;
78 }
79
80 /* returns non-zero if the specified file name has .csd extension */
81
82 int is_csd(char *s)
83 {
84 int n = strlen(s);
85 if (n < 5) return 0;
86 --n; if (s[n] != 'D' && s[n] != 'd') return 0;
87 --n; if (s[n] != 'S' && s[n] != 's') return 0;
88 --n; if (s[n] != 'C' && s[n] != 'c') return 0;
89 --n; if (s[n] != '.') return 0;
90 return 1;
91 }
92
93 /* split filename to directory and base name */
94
95 void split_filename(char *fullname, char *dir, char *bas)
96 {
97 int m;
98 /* if no filename was given */
99 if (fullname == NULL || strlen(fullname) == 0) {
100 *dir = '\0';
101 *bas = '\0';
102 return;
103 }
104 m = strlen(fullname);
105 while (--m >= 0 &&
106 fullname[m] != '/' && fullname[m] != '\\' && fullname[m] != ':');
107 /* directory name */
108 if (m < 0)
109 #ifdef WIN32
110 strcpy(dir, ".\\");
111 #else
112 strcpy(dir, "./");
113 #endif
114 else {
115 strncpy(dir, fullname, m + 1);
116 dir[m + 1] = '\0';
117 }
118 /* base name */
119 strcpy(bas, fullname + (m + 1));
120 }
121
122 #if 0 && defined(__MACH__)
123 /* There is something odd on OSX about dirent.h */
124 typedef void* DIR;
125 DIR opendir(const char *);
126 struct dirent *readdir(DIR*);
127 int closedir(DIR*);
128 #endif
129
130 void create_file_list(void)
131 {
132 DIR *d;
133 struct dirent *ep;
134 char tmp[256];
135
136 d = opendir(dir_name);
137 if (d == NULL) {
138 fprintf(stderr, "cs: error opening directory %s: %s\n",
139 dir_name, strerror(errno));
140 exit(-1);
141 }
142 /* find all files in directory */
143 while ((ep = readdir(d)) != NULL) {
144 if (nr_files >= max_files) {
145 /* expand file name array */
146 max_files = max_files + (max_files >> 2) + 16;
147 file_names = (char**) realloc(file_names, sizeof(char*) * max_files);
148 if (file_names == NULL) {
149 fprintf(stderr, "cs: not enough memory\n"); exit(-1);
150 }
151 }
152 file_names[nr_files] = (char*) malloc(sizeof(char) * 256);
153 if (file_names[nr_files] == NULL) {
154 fprintf(stderr, "cs: not enough memory\n"); exit(-1);
155 }
156 /* store new file name */
157 split_filename(ep->d_name, tmp, file_names[nr_files]);
158 nr_files++;
159 }
160 closedir(d);
161 }
162
163 int chr_cmp(char a, char b)
164 {
165 if (a == b) return 2; /* characters are exactly the same */
166 if (isupper(a)) a = tolower(a);
167 if (isupper(b)) b = tolower(b);
168 if (a == b) return 1; /* characters are the same if case is ignored */
169 return 0; /* different characters */
170 }
171
172 int find_best_match(int file_type, char **s)
173 {
174 int chars_match = 0, n = -1, m, i, j;
175 *s = NULL;
176 while (++n < nr_files) {
177 /* first, check if the file is of the right type */
178 if (file_type == 0) {
179 if (!is_orc(file_names[n])) continue; /* file type 0: orchestra */
180 }
181 else if (file_type == 1) {
182 if (!is_sco(file_names[n])) continue; /* file type 1: score */
183 }
184 else if (file_type == 2) {
185 if (!is_mid(file_names[n])) continue; /* file type 2: MIDI */
186 }
187 else if (file_type == 3) {
188 if (!is_csd(file_names[n])) continue; /* file type 3: CSD */
189 }
190 /* now find the number of characters matching the prefix */
191 i = 2; j = -1;
192 for (m = 0; m < (int) strlen(prefix); m++) {
193 j = chr_cmp(file_names[n][m], prefix[m]);
194 if (!j) {
195 if (m < ((int) strlen(file_names[n]) - 4))
196 j = -1; /* does not match */
197 break;
198 }
199 if (j < i) i = j; /* if case was ignored */
200 }
201 if (j < 0) continue;
202 /* an exact match is always preferred */
203 if (m == (int) strlen(prefix) && m == (int) (strlen(file_names[n]) - 4))
204 m++;
205 /* the final value is the number of matching characters * 2 */
206 /* +1 if case is also correct */
207 /* +2 for exact match */
208 /* +3 if both the above */
209 m = (m << 1) + (i - 1);
210 /* if this is the best match so far, record it */
211 if (m >= chars_match) {
212 chars_match = m;
213 *s = file_names[n];
214 }
215 }
216 return chars_match;
217 }
218
219 void find_files(char *name)
220 {
221 int base_match = -1, n;
222 int orcmatch = 0, scomatch = 0, midmatch = 0, csdmatch = 0;
223 char *s = NULL;
224
225 split_filename(name, dir_name, prefix);
226 if (dir_name[0] == '\0' || prefix[0] == '\0') {
227 fprintf(stderr, "cs: invalid or empty name\n");
228 exit(-1);
229 }
230 create_file_list();
231 /* minimum number of characters to match */
232 while (++base_match < (int) strlen(prefix) && isalpha(prefix[base_match]));
233 base_match <<= 1; /* because find_best_match() returns *2 */
234 /* try to find CSD file first */
235 n = find_best_match(3, &s);
236 if (n && n >= base_match) {
237 csdname = (char*) malloc(sizeof(char) * 256);
238 if (csdname == NULL) {
239 fprintf(stderr, "cs: not enough memory\n"); exit(-1);
240 }
241 strcpy(csdname, dir_name);
242 strncat(csdname, s, 255); csdname[255] = '\0';
243 csdmatch = n;
244 }
245 /* now find orchestra or score */
246 n = find_best_match(0, &s);
247 if (n && n >= base_match) {
248 orcname = (char*) malloc(sizeof(char) * 256);
249 if (orcname == NULL) {
250 fprintf(stderr, "cs: not enough memory\n"); exit(-1);
251 }
252 strcpy(orcname, dir_name);
253 strcat(orcname, s);
254 orcmatch = n;
255 }
256 n = find_best_match(1, &s);
257 if (n && n >= base_match) {
258 sconame = (char*) malloc(sizeof(char) * 256);
259 if (sconame == NULL) {
260 fprintf(stderr, "cs: not enough memory\n"); exit(-1);
261 }
262 strcpy(sconame, dir_name);
263 strcat(sconame, s);
264 scomatch = n;
265 }
266 /* decide whether to use the CSD file or the orc/sco pair */
267 if ((orcmatch >= csdmatch && scomatch >= csdmatch) &&
268 (orcmatch > csdmatch || scomatch > csdmatch)) {
269 csdmatch = 0;
270 if (csdname != NULL) {
271 free(csdname);
272 csdname = NULL;
273 }
274 }
275 if (csdname == NULL && (orcname == NULL || sconame == NULL)) {
276 fprintf(stderr, "cs: cannot find a CSD file or an orc/sco pair with ");
277 fprintf(stderr, "the specified name\n");
278 exit(-1);
279 }
280 if (csdname != NULL) {
281 fprintf(stderr, "found CSD file: %s\n", csdname);
282 if (orcname != NULL) { /* do not use both CSD and orc/sco */
283 free(orcname);
284 orcname = NULL;
285 }
286 if (sconame != NULL) {
287 free(sconame);
288 sconame = NULL;
289 }
290 }
291 else {
292 fprintf(stderr, "found orchestra file: %s\n", orcname);
293 fprintf(stderr, "found score file: %s\n", sconame);
294 }
295 /* finally, search for a MIDI file */
296 n = find_best_match(2, &s);
297 if (n && n >= base_match) {
298 midname = (char*) malloc(sizeof(char) * 256);
299 if (midname == NULL) {
300 fprintf(stderr, "cs: not enough memory\n"); exit(-1);
301 }
302 strcpy(midname, dir_name);
303 strcat(midname, s);
304 midmatch = n;
305 }
306 if (midmatch < csdmatch || midmatch < orcmatch || midmatch < scomatch) {
307 midmatch = 0;
308 if (midname != NULL) {
309 free(midname);
310 midname = NULL;
311 }
312 }
313 if (midname != NULL) {
314 fprintf(stderr, "found MIDI file: %s\n", midname);
315 }
316 /* free file name list */
317 for (n = 0; n < nr_files; n++) {
318 free(file_names[n]);
319 }
320 if (file_names != NULL) {
321 free(file_names);
322 file_names = NULL;
323 }
324 }
325
326 void copy_options(char **dst, char *src)
327 {
328 int i = -1;
329 int j = 0; /* 1 if copying an option (2 if quoted) */
330 char *s = *dst;
331
332 while (++i < (int) strlen(src)) {
333 if (!j) {
334 /* skip whitespace between options */
335 if (isblank(src[i])) continue;
336 /* if quoted option */
337 if (src[i] == '"') {
338 j = 2; *s++ = ' '; *s++ = '"'; continue;
339 }
340 /* otherwise simple option */
341 *s++ = ' '; *s++ = '"'; j = 1;
342 }
343 if (j == 1 && isblank(src[i])) {
344 /* whitespace can terminate simple option, but not quoted one */
345 *s++ = '"'; j = 0; continue;
346 }
347 if (src[i] == '"') {
348 /* quote: terminates option */
349 *s++ = '"';
350 if (j == 1) {
351 /* and if it was a simple option, also begins a new (quoted) one */
352 *s++ = ' '; *s++ = '"'; j = 2;
353 }
354 else
355 j = 0; /* otherwise no longer in an option */
356 continue;
357 }
358 /* copy option text */
359 *s++ = src[i];
360 }
361 /* terminate last option if necessary */
362 if (j)
363 *s++ = '"';
364 /* store new pointer */
365 *dst = s;
366 }
367
368 int main(int argc, char **argv)
369 {
370 int i, j, k, opts[26];
371 char tmp[256], cmdline[1024], optlst[256], extra_opts[1024], c, *s, *s2;
372 char tmp2[256], *cs_argv[256];
373
374 if (argc < 2) {
375 fprintf(stderr, "usage: cs [-OPTIONS] <name> [CSOUND OPTIONS ... ]\n");
376 exit(-1);
377 }
378 i = 0; j = argc;
379 tmp[0] = '\0';
380 cmdline[0] = '\0';
381 optlst[0] = '\0';
382 extra_opts[0] = '\0';
383 memset(opts, 0, sizeof(int) * 26);
384 /* parse command line options */
385 while (--j) {
386 i++;
387 if (argv[i][0] != '-' && i < 3 && tmp[0] == '\0') {
388 strncpy(tmp, argv[i],255);
389 /* strip extension */
390 if (is_orc(tmp) || is_sco(tmp) || is_mid(tmp) || is_csd(tmp))
391 tmp[strlen(tmp) - 4] = '\0';
392 if (tmp[0] == '\0') {
393 fprintf(stderr, "cs: invalid or empty name\n");
394 exit(-1);
395 }
396 }
397 else if (argv[i][0] == '-' && i == 1) {
398 int n = 0;
399 /* option list */
400 while (++n < (int) strlen(argv[i])) {
401 c = argv[i][n];
402 if (islower(c)) c = toupper(c);
403 if (c < 'A' || c > 'Z') {
404 fprintf(stderr, "cs: invalid option: %c\n", argv[i][n]);
405 exit(-1);
406 }
407 opts[(int) c - 'A'] = 1;
408 optlst[n - 1] = c;
409 }
410 optlst[n] = '\0';
411 }
412 else {
413 /* anything else is Csound flags */
414 strcat(extra_opts, " \"");
415 strncat(extra_opts, argv[i], 1018);
416 strcat(extra_opts, "\"");
417 }
418 }
419 if (tmp[0] == '\0') {
420 fprintf(stderr, "cs: no name was specified\n");
421 exit(-1);
422 }
423 /* set filenames */
424 find_files(tmp);
425 /* find csound executable */
426 tmp[0] = '\0';
427 /* first, search environment variables, based on option list */
428 /* (the last option has the highest precedence) */
429 s = getenv("CSOUND");
430 if (s != NULL) /* get default setting from CSOUND, if available */
431 strncpy(tmp, s, 255);
432 for (i = (int) strlen(optlst); --i >= 0; ) {
433 sprintf(tmp2, "CSOUND_%c", optlst[i]);
434 s = getenv(tmp2);
435 if (s != NULL) {
436 strncpy(tmp, s, 255);
437 if (tmp[0] != '\0') break;
438 }
439 }
440 /* if nothing was found, use default settings */
441 if (tmp[0] == '\0') {
442 if (opts[17]) strncpy(tmp, default_csnd_r, 256);
443 else strncpy(tmp, default_csnd, 256);
444 }
445 /* convert this to a list of quoted options */
446 s2 = cmdline;
447 copy_options(&s2, tmp);
448 /* add default flags, */
449 s = getenv("CSFLAGS"); /* from CSFLAGS, if possible */
450 if (s == NULL) s = default_csfl;
451 copy_options(&s2, s);
452 /* any options from the environment, */
453 for (i = 0; i < (int) strlen(optlst); i++) {
454 sprintf(tmp2, "CSFLAGS_%c", optlst[i]);
455 s = getenv(tmp2);
456 if (s != NULL)
457 copy_options(&s2, s);
458 else if (optlst[i] == 'R') /* built-in defaults for */
459 copy_options(&s2, default_csfl_r); /* realtime use */
460 }
461 /* and those specified on the command line */
462 copy_options(&s2, extra_opts);
463 /* add flag for MIDI file if there is any */
464 /* IV - Jan 30 2003: this is not done if the user has specified any */
465 /* MIDI device or file, so that the automatic use of MIDI file can */
466 /* be overridden */
467 *s2 = '\0';
468 if (midname != NULL &&
469 strstr(cmdline, "\"-M\"") == NULL &&
470 strstr(cmdline, "\"-F\"") == NULL) {
471 copy_options(&s2, "-F");
472 copy_options(&s2, midname);
473 }
474 /* unless there is any user specified output (e.g. stdout or real-time), */
475 /* or output is disabled, add output file */
476 *s2 = '\0';
477 if (strstr(cmdline, "\"-n\"") == NULL &&
478 strstr(cmdline, "\"-o\"") == NULL) { /* IV - Jan 30 2003 */
479 char tmp3[256];
480 /* of all the input files, find the one with the longest name */
481 /* for determining filename prefix */
482 tmp[0] = '\0';
483 if (orcname != NULL && strlen(orcname) > strlen(tmp))
484 strcpy(tmp, orcname);
485 if (sconame != NULL && strlen(sconame) > strlen(tmp))
486 strcpy(tmp, sconame);
487 if (midname != NULL && strlen(midname) > strlen(tmp))
488 strcpy(tmp, midname);
489 if (csdname != NULL && strlen(csdname) > strlen(tmp))
490 strcpy(tmp, csdname);
491 /* set extension depending on file type */
492 i = strlen(tmp) - 4;
493 tmp[i] = '\0';
494 if (strstr(cmdline, "\"-J\"") != NULL) /* IRCAM */
495 strcat(tmp, ".sf");
496 else if (strstr(cmdline, "\"-A\"") != NULL) /* AIFF */
497 strcat(tmp, ".aif");
498 else if (strstr(cmdline, "\"-h\"") != NULL) /* RAW */
499 strcat(tmp, ".pcm");
500 else /* WAVE */
501 strcat(tmp, ".wav");
502 /* add output file to options */
503 copy_options(&s2, "-o");
504 split_filename(tmp, tmp2, tmp3); /* always write to current directory */
505 copy_options(&s2, tmp3);
506 }
507 /* finally, add the input files */
508 if (csdname != NULL) copy_options(&s2, csdname);
509 if (orcname != NULL) copy_options(&s2, orcname);
510 if (sconame != NULL) copy_options(&s2, sconame);
511 *s2 = '\0';
512 /* split command line to separate strings */
513 s2 = cmdline;
514 j = k = 0;
515 while (*s2 != '\0') {
516 if (*s2 == '"') {
517 if (k == 0) {
518 cs_argv[j] = s2 + 1; j++; /* begin option */
519 }
520 else
521 *s2 = '\0'; /* end of option */
522 k = 1 - k;
523 }
524 s2++;
525 }
526 cs_argv[j] = NULL; /* terminate list */
527 if (j < 1) { /* assertion only */
528 fprintf(stderr, "cs: internal error: no command to execute\n");
529 exit(-1);
530 }
531 /* print the command line with all options */
532 fprintf(stderr, "command line: ");
533 for (i = 0; i < j; i++) {
534 if (strchr(cs_argv[i], ' ') || strchr(cs_argv[i], '\t') ||
535 strchr(cs_argv[i], '\r') || strchr(cs_argv[i], '\n'))
536 fprintf(stderr, " \"%s\"", cs_argv[i]); /* if need to quote */
537 else
538 fprintf(stderr, " %s", cs_argv[i]);
539 }
540 fprintf(stderr, "\n");
541 /* program name */
542 split_filename(cs_argv[0], tmp, tmp2);
543 strncpy(tmp, cs_argv[0], 255); tmp[255] = '\0'; /* with path */
544 cs_argv[0] = tmp2; /* and without it */
545 /* free all memory */
546 if (orcname != NULL) free(orcname);
547 if (sconame != NULL) free(sconame);
548 if (midname != NULL) free(midname);
549 if (csdname != NULL) free(csdname);
550 /* execute command */
551 if (execvp(tmp, cs_argv)) {
552 fprintf(stderr, "cs: error executing Csound command: %s\n",
553 strerror(errno));
554 exit(-1);
555 }
556
557 return 0;
558 }
559