2 * Copyright (c) 2007, 2008 Edward Tomasz NapieraĆa <trasz@FreeBSD.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * ALTHOUGH THIS SOFTWARE IS MADE OF WIN AND SCIENCE, IT IS PROVIDED BY THE
15 * AUTHOR AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
16 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
17 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
18 * THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
19 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
20 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
21 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
22 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
23 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 * "SMF shell", command line utility.
44 #ifdef HAVE_LIBREADLINE
45 #include <readline/readline.h>
46 #include <readline/history.h>
49 smf_track_t *selected_track = NULL;
50 smf_event_t *selected_event = NULL;
52 char *last_file_name = NULL;
54 #define COMMAND_LENGTH 10
57 log_handler(const gchar *log_domain, GLogLevelFlags log_level, const gchar *message, gpointer notused)
59 if (strcmp(log_domain, "smfsh") == 0)
60 fprintf(stderr, "%s\n", message);
62 fprintf(stderr, "%s: %s\n", log_domain, message);
65 static int cmd_track(char *arg);
68 cmd_load(char *file_name)
72 if (file_name == NULL) {
73 if (last_file_name == NULL) {
74 g_critical("Please specify file name.");
78 file_name = strdup(last_file_name);
80 file_name = strdup(file_name);
83 selected_track = NULL;
84 selected_event = NULL;
91 if (last_file_name != NULL)
93 last_file_name = strdup(file_name);
95 smf = smf_load(file_name);
97 g_critical("Couldn't load '%s'.", file_name);
101 g_critical("Cannot initialize smf_t.");
108 g_message("File '%s' loaded.", file_name);
109 decoded = smf_decode(smf);
110 g_message("%s.", decoded);
121 cmd_save(char *file_name)
125 if (file_name == NULL) {
126 if (last_file_name == NULL) {
127 g_critical("Please specify file name.");
131 file_name = strdup(last_file_name);
133 file_name = strdup(file_name);
136 if (last_file_name != NULL)
137 free(last_file_name);
138 last_file_name = strdup(file_name);
140 ret = smf_save(smf, file_name);
142 g_critical("Couldn't save '%s'", file_name);
146 g_message("File '%s' saved.", file_name);
154 cmd_ppqn(char *new_ppqn)
159 if (new_ppqn == NULL) {
160 g_message("Pulses Per Quarter Note (aka Division) is %d.", smf->ppqn);
162 tmp = strtol(new_ppqn, &end, 10);
163 if (end - new_ppqn != strlen(new_ppqn)) {
164 g_critical("Invalid PPQN, garbage characters after the number.");
169 g_critical("Invalid PPQN, valid values are greater than zero.");
173 if (smf_set_ppqn(smf, tmp)) {
174 g_message("smf_set_ppqn failed.");
178 g_message("Pulses Per Quarter Note changed to %d.", smf->ppqn);
185 cmd_format(char *new_format)
190 if (new_format == NULL) {
191 g_message("Format is %d.", smf->format);
193 tmp = strtol(new_format, &end, 10);
194 if (end - new_format != strlen(new_format)) {
195 g_critical("Invalid format value, garbage characters after the number.");
199 if (tmp < 0 || tmp > 2) {
200 g_critical("Invalid format value, valid values are in range 0 - 2, inclusive.");
204 if (smf_set_format(smf, tmp)) {
205 g_critical("smf_set_format failed.");
209 g_message("Forma changed to %d.", smf->format);
216 cmd_tracks(char *notused)
218 if (smf->number_of_tracks > 0)
219 g_message("There are %d tracks, numbered from 1 to %d.", smf->number_of_tracks, smf->number_of_tracks);
221 g_message("There are no tracks.");
227 parse_track_number(const char *arg)
233 if (selected_track == NULL) {
234 g_message("No track currently selected and no track number given.");
237 return (selected_track->track_number);
241 num = strtol(arg, &end, 10);
242 if (end - arg != strlen(arg)) {
243 g_critical("Invalid track number, garbage characters after the number.");
247 if (num < 1 || num > smf->number_of_tracks) {
248 if (smf->number_of_tracks > 0) {
249 g_critical("Invalid track number specified; valid choices are 1 - %d.", smf->number_of_tracks);
251 g_critical("There are no tracks.");
266 if (selected_track == NULL)
267 g_message("No track currently selected.");
269 g_message("Currently selected is track number %d, containing %d events.",
270 selected_track->track_number, selected_track->number_of_events);
272 if (smf->number_of_tracks == 0) {
273 g_message("There are no tracks.");
277 num = parse_track_number(arg);
281 selected_track = smf_get_track_by_number(smf, num);
282 if (selected_track == NULL) {
283 g_critical("smf_get_track_by_number() failed, track not selected.");
287 selected_event = NULL;
289 g_message("Track number %d selected; it contains %d events.",
290 selected_track->track_number, selected_track->number_of_events);
297 cmd_trackadd(char *notused)
299 selected_track = smf_track_new();
300 if (selected_track == NULL) {
301 g_critical("smf_track_new() failed, track not created.");
305 smf_add_track(smf, selected_track);
307 selected_event = NULL;
309 g_message("Created new track; track number %d selected.", selected_track->track_number);
315 cmd_trackrm(char *arg)
317 int num = parse_track_number(arg);
322 if (selected_track != NULL && num == selected_track->track_number) {
323 selected_track = NULL;
324 selected_event = NULL;
327 smf_track_delete(smf_get_track_by_number(smf, num));
329 g_message("Track %d removed.", num);
334 #define BUFFER_SIZE 1024
337 show_event(smf_event_t *event)
340 char *decoded, *type;
342 if (smf_event_is_metadata(event))
347 decoded = smf_event_decode(event);
349 if (decoded == NULL) {
350 decoded = malloc(BUFFER_SIZE);
351 if (decoded == NULL) {
352 g_critical("show_event: malloc failed.");
356 off += snprintf(decoded + off, BUFFER_SIZE - off, "Unknown event:");
358 for (i = 0; i < event->midi_buffer_length && i < 5; i++)
359 off += snprintf(decoded + off, BUFFER_SIZE - off, " 0x%x", event->midi_buffer[i]);
362 g_message("%d: %s: %s, %f seconds, %d pulses, %d delta pulses", event->event_number, type, decoded,
363 event->time_seconds, event->time_pulses, event->delta_time_pulses);
371 cmd_events(char *notused)
375 if (selected_track == NULL) {
376 g_critical("No track selected - please use 'track <number>' command first.");
380 if (selected_track->number_of_events == 0) {
381 g_message("Selected track is empty.");
385 g_message("List of events in track %d follows:", selected_track->track_number);
389 while ((event = smf_track_get_next_event(selected_track)) != NULL)
398 parse_event_number(const char *arg)
403 if (selected_track == NULL) {
404 g_critical("You need to select track first (using 'track <number>').");
409 if (selected_event == NULL) {
410 g_message("No event currently selected and no event number given.");
413 return (selected_event->event_number);
417 num = strtol(arg, &end, 10);
418 if (end - arg != strlen(arg)) {
419 g_critical("Invalid event number, garbage characters after the number.");
423 if (num < 1 || num > selected_track->number_of_events) {
424 if (selected_track->number_of_events > 0)
425 g_critical("Invalid event number specified; valid choices are 1 - %d.", selected_track->number_of_events);
427 g_critical("There are no events in currently selected track.");
441 if (selected_event == NULL) {
442 g_message("No event currently selected.");
444 g_message("Currently selected is event %d, track %d.", selected_event->event_number, selected_track->track_number);
445 show_event(selected_event);
448 num = parse_event_number(arg);
452 selected_event = smf_track_get_event_by_number(selected_track, num);
453 if (selected_event == NULL) {
454 g_critical("smf_get_event_by_number() failed, event not selected.");
458 g_message("Event number %d selected.", selected_event->event_number);
459 show_event(selected_event);
466 decode_hex(char *str, unsigned char **buffer, int *length)
468 int i, value, midi_buffer_length;
470 unsigned char *midi_buffer = NULL;
473 if ((strlen(str) % 2) != 0) {
474 g_critical("Hex value should have even number of characters, you know.");
478 midi_buffer_length = strlen(str) / 2;
479 midi_buffer = malloc(midi_buffer_length);
480 if (midi_buffer == NULL) {
481 g_critical("malloc() failed.");
485 for (i = 0; i < midi_buffer_length; i++) {
487 buf[1] = str[i * 2 + 1];
489 value = strtoll(buf, &end, 16);
491 if (end - buf != 2) {
492 g_critical("Garbage characters detected after hex.");
496 midi_buffer[i] = value;
499 *buffer = midi_buffer;
500 *length = midi_buffer_length;
505 if (midi_buffer != NULL)
514 g_message("Usage: add <time-in-seconds> <midi-in-hex> - for example, 'add 1 903C7F' will add");
515 g_message("Note On event, note C4, velocity 127, channel 1, one second from the start of song, channel 1.");
519 cmd_eventadd(char *str)
521 int midi_buffer_length;
523 unsigned char *midi_buffer;
524 char *time, *endtime;
526 if (selected_track == NULL) {
527 g_critical("Please select a track first, using 'track <number>' command.");
536 /* Extract the time. Don't use strsep(3), it doesn't work on SunOS. */
538 str = strchr(str, ' ');
544 seconds = strtod(time, &endtime);
545 if (endtime - time != strlen(time)) {
546 g_critical("Time is supposed to be a number, without trailing characters.");
550 /* Called with one parameter? */
556 if (decode_hex(str, &midi_buffer, &midi_buffer_length)) {
561 selected_event = smf_event_new();
562 if (selected_event == NULL) {
563 g_critical("smf_event_new() failed, event not created.");
567 selected_event->midi_buffer = midi_buffer;
568 selected_event->midi_buffer_length = midi_buffer_length;
570 if (smf_event_is_valid(selected_event) == 0) {
571 g_critical("Event is invalid from the MIDI specification point of view, not created.");
572 smf_event_delete(selected_event);
573 selected_event = NULL;
577 smf_track_add_event_seconds(selected_track, selected_event, seconds);
579 g_message("Event created.");
587 double seconds, type;
588 char *time, *typestr, *end;
590 if (selected_track == NULL) {
591 g_critical("Please select a track first, using 'track <number>' command.");
596 g_critical("Usage: text <time-in-seconds> <event-type> <text-itself>");
600 /* Extract the time. Don't use strsep(3), it doesn't work on SunOS. */
602 str = strchr(str, ' ');
608 seconds = strtod(time, &end);
609 if (end - time != strlen(time)) {
610 g_critical("Time is supposed to be a number, without trailing characters.");
614 /* Called with one parameter? */
616 g_critical("Usage: text <time-in-seconds> <event-type> <text-itself>");
620 /* Extract the event type. */
622 str = strchr(str, ' ');
628 type = strtod(typestr, &end);
629 if (end - typestr != strlen(typestr)) {
630 g_critical("Type is supposed to be a number, without trailing characters.");
634 if (type < 1 || type > 9) {
635 g_critical("Valid values for type are 1 - 9, inclusive.");
639 /* Called with one parameter? */
641 g_critical("Usage: text <time-in-seconds> <event-type> <text-itself>");
645 selected_event = smf_event_new_textual(type, str);
646 if (selected_event == NULL) {
647 g_critical("smf_event_new_textual() failed, event not created.");
651 assert(smf_event_is_valid(selected_event));
653 smf_track_add_event_seconds(selected_track, selected_event, seconds);
655 g_message("Event created.");
662 cmd_eventaddeot(char *time)
667 if (selected_track == NULL) {
668 g_critical("Please select a track first, using 'track <number>' command.");
673 g_critical("Please specify the time, in seconds.");
677 seconds = strtod(time, &end);
678 if (end - time != strlen(time)) {
679 g_critical("Time is supposed to be a number, without trailing characters.");
683 if (smf_track_add_eot_seconds(selected_track, seconds)) {
684 g_critical("smf_track_add_eot() failed.");
688 g_message("Event created.");
694 cmd_eventrm(char *number)
696 int num = parse_event_number(number);
701 if (selected_event != NULL && num == selected_event->event_number)
702 selected_event = NULL;
704 smf_event_delete(smf_track_get_event_by_number(selected_track, num));
706 g_message("Event #%d removed.", num);
712 cmd_tempo(char *notused)
718 tempo = smf_get_tempo_by_number(smf, i);
722 g_message("Tempo #%d: Starts at %d pulses, %f seconds, setting %d microseconds per quarter note, %.2f BPM.",
723 i, tempo->time_pulses, tempo->time_seconds, tempo->microseconds_per_quarter_note,
724 60000000.0 / (double)tempo->microseconds_per_quarter_note);
725 g_message("Time signature: %d/%d, %d clocks per click, %d 32nd notes per quarter note.",
726 tempo->numerator, tempo->denominator, tempo->clocks_per_click, tempo->notes_per_note);
733 cmd_length(char *notused)
735 g_message("Length: %d pulses, %f seconds.", smf_get_length_pulses(smf), smf_get_length_seconds(smf));
741 cmd_version(char *notused)
743 g_message("libsmf version %s.", smf_get_version());
749 cmd_exit(char *notused)
751 g_debug("Good bye.");
755 static int cmd_help(char *notused);
757 static struct command_struct {
759 int (*function)(char *command);
761 } commands[] = {{"help", cmd_help, "Show this help."},
762 {"?", cmd_help, NULL},
763 {"load", cmd_load, "Load named file."},
765 {"save", cmd_save, "Save to named file."},
766 {"ppqn", cmd_ppqn, "Show ppqn (aka division), or set ppqn if used with parameter."},
767 {"format", cmd_format, "Show format, or set format if used with parameter."},
768 {"tracks", cmd_tracks, "Show number of tracks."},
769 {"track", cmd_track, "Show number of currently selected track, or select a track."},
770 {"trackadd", cmd_trackadd, "Add a track and select it."},
771 {"trackrm", cmd_trackrm, "Remove currently selected track."},
772 {"events", cmd_events, "Show events in the currently selected track."},
773 {"event", cmd_event, "Show number of currently selected event, or select an event."},
774 {"add", cmd_eventadd, "Add an event and select it."},
775 {"text", cmd_text, "Add textual event and select it."},
776 {"eventadd", cmd_eventadd, NULL},
777 {"eot", cmd_eventaddeot, "Add an End Of Track event."},
778 {"eventaddeot", cmd_eventaddeot, NULL},
779 {"eventrm", cmd_eventrm, NULL},
780 {"rm", cmd_eventrm, "Remove currently selected event."},
781 {"tempo", cmd_tempo, "Show tempo map."},
782 {"length", cmd_length, "Show length of the song."},
783 {"version", cmd_version, "Show libsmf version."},
784 {"exit", cmd_exit, "Exit to shell."},
785 {"quit", cmd_exit, NULL},
786 {"bye", cmd_exit, NULL},
790 cmd_help(char *notused)
792 int i, padding_length;
793 char padding[COMMAND_LENGTH + 1];
794 struct command_struct *tmp;
796 g_message("Available commands:");
798 for (tmp = commands; tmp->name != NULL; tmp++) {
799 /* Skip commands with no help string. */
800 if (tmp->help == NULL)
803 padding_length = COMMAND_LENGTH - strlen(tmp->name);
804 assert(padding_length >= 0);
805 for (i = 0; i < padding_length; i++)
809 g_message("%s:%s%s", tmp->name, padding, tmp->help);
816 * Removes (in place) all whitespace characters before the first
817 * non-whitespace and all trailing whitespace characters. Replaces
818 * more than one consecutive whitespace characters with one.
821 strip_unneeded_whitespace(char *str, int len)
826 for (src = str, dest = str; src < dest + len; src++) {
827 if (*src == '\n' || *src == '\0') {
845 /* Remove trailing whitespace. */
847 if (isspace(dest[len - 1]))
848 dest[len - 1] = '\0';
857 #ifdef HAVE_LIBREADLINE
858 buf = readline("smfsh> ");
862 g_critical("Malloc failed.");
866 fprintf(stdout, "smfsh> ");
869 buf = fgets(buf, 1024, stdin);
873 fprintf(stdout, "exit\n");
874 return (strdup("exit"));
877 strip_unneeded_whitespace(buf, 1024);
882 return (read_command());
884 #ifdef HAVE_LIBREADLINE
892 execute_command(char *line)
894 char *command, *args;
895 struct command_struct *tmp;
898 args = strchr(line, ' ');
904 for (tmp = commands; tmp->name != NULL; tmp++) {
905 if (strcmp(tmp->name, command) == 0)
906 return ((tmp->function)(args));
909 g_warning("No such command: '%s'. Type 'help' to see available commands.", command);
915 read_and_execute_command(void)
918 char *command_line, *command, *next_command;
920 command = command_line = read_command();
923 next_command = strchr(command, ';');
924 if (next_command != NULL) {
925 *next_command = '\0';
929 strip_unneeded_whitespace(command, 1024);
930 if (strlen(command) > 0) {
931 ret = execute_command(command);
933 g_warning("Command finished with error.");
936 command = next_command;
943 #ifdef HAVE_LIBREADLINE
946 smfsh_command_generator(const char *text, int state)
948 static struct command_struct *command = commands;
954 while (command->name != NULL) {
958 if (strncmp(tmp, text, strlen(text)) == 0)
959 return (strdup(tmp));
966 smfsh_completion(const char *text, int start, int end)
970 /* Return NULL if "text" is not the first word in the input line. */
972 for (i = 0; i < start; i++) {
973 if (!isspace(rl_line_buffer[i]))
978 return (rl_completion_matches(text, smfsh_command_generator));
986 fprintf(stderr, "usage: smfsh [-V | file]\n");
992 main(int argc, char *argv[])
996 while ((ch = getopt(argc, argv, "V")) != -1) {
1011 g_log_set_default_handler(log_handler, NULL);
1015 g_critical("Cannot initialize smf_t.");
1024 #ifdef HAVE_LIBREADLINE
1025 rl_readline_name = "smfsh";
1026 rl_attempted_completion_function = smfsh_completion;
1030 read_and_execute_command();