smflite

view smfsh/smfsh.c @ 1:8e535ca4bb86

added smfsh
author John Tsiombikas <nuclear@member.fsf.org>
date Thu, 26 Jan 2012 15:35:18 +0200
parents
children
line source
1 /*-
2 * Copyright (c) 2007, 2008 Edward Tomasz NapieraƂa <trasz@FreeBSD.org>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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.
13 *
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.
25 *
26 */
28 /**
29 * \file
30 *
31 * "SMF shell", command line utility.
32 */
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <unistd.h>
37 #ifdef __MINGW32__
38 #define EX_OK 0
39 #define EX_USAGE 64
40 #else /* ! __MINGW32__ */
41 #include <sysexits.h>
42 #endif /* ! __MINGW32__ */
43 #include <string.h>
44 #include <ctype.h>
45 #include <assert.h>
46 #include "smf.h"
47 #include "fake_glib.h"
49 #ifdef HAVE_LIBREADLINE
50 #include <readline/readline.h>
51 #include <readline/history.h>
52 #endif
54 smf_track_t *selected_track = NULL;
55 smf_event_t *selected_event = NULL;
56 smf_t *smf = NULL;
57 char *last_file_name = NULL;
59 #define COMMAND_LENGTH 10
61 /*static void
62 log_handler(const gchar *log_domain, GLogLevelFlags log_level, const gchar *message, gpointer notused)
63 {
64 if (strcmp(log_domain, "smfsh") == 0)
65 fprintf(stderr, "%s\n", message);
66 else
67 fprintf(stderr, "%s: %s\n", log_domain, message);
68 }*/
70 static int cmd_track(char *arg);
72 static int
73 cmd_load(char *file_name)
74 {
75 char *decoded;
77 if (file_name == NULL) {
78 if (last_file_name == NULL) {
79 fg_critical("Please specify file name.");
80 return (-1);
81 }
83 file_name = strdup(last_file_name);
84 } else {
85 file_name = strdup(file_name);
86 }
88 selected_track = NULL;
89 selected_event = NULL;
91 if (smf != NULL) {
92 smf_delete(smf);
93 smf = NULL;
94 }
96 if (last_file_name != NULL)
97 free(last_file_name);
98 last_file_name = strdup(file_name);
100 smf = smf_load(file_name);
101 if (smf == NULL) {
102 fg_critical("Couldn't load '%s'.", file_name);
104 smf = smf_new();
105 if (smf == NULL) {
106 fg_critical("Cannot initialize smf_t.");
107 return (-1);
108 }
110 return (-2);
111 }
113 fg_message("File '%s' loaded.", file_name);
114 decoded = smf_decode(smf);
115 fg_message("%s.", decoded);
116 free(decoded);
118 cmd_track("1");
120 free(file_name);
122 return (0);
123 }
125 static int
126 cmd_save(char *file_name)
127 {
128 int ret;
130 if (file_name == NULL) {
131 if (last_file_name == NULL) {
132 fg_critical("Please specify file name.");
133 return (-1);
134 }
136 file_name = strdup(last_file_name);
137 } else {
138 file_name = strdup(file_name);
139 }
141 if (last_file_name != NULL)
142 free(last_file_name);
143 last_file_name = strdup(file_name);
145 ret = smf_save(smf, file_name);
146 if (ret) {
147 fg_critical("Couldn't save '%s'", file_name);
148 return (-1);
149 }
151 fg_message("File '%s' saved.", file_name);
153 free(file_name);
155 return (0);
156 }
158 static int
159 cmd_ppqn(char *new_ppqn)
160 {
161 int tmp;
162 char *end;
164 if (new_ppqn == NULL) {
165 fg_message("Pulses Per Quarter Note (aka Division) is %d.", smf->ppqn);
166 } else {
167 tmp = strtol(new_ppqn, &end, 10);
168 if (end - new_ppqn != strlen(new_ppqn)) {
169 fg_critical("Invalid PPQN, garbage characters after the number.");
170 return (-1);
171 }
173 if (tmp <= 0) {
174 fg_critical("Invalid PPQN, valid values are greater than zero.");
175 return (-2);
176 }
178 if (smf_set_ppqn(smf, tmp)) {
179 fg_message("smf_set_ppqn failed.");
180 return (-3);
181 }
183 fg_message("Pulses Per Quarter Note changed to %d.", smf->ppqn);
184 }
186 return (0);
187 }
189 static int
190 cmd_format(char *new_format)
191 {
192 int tmp;
193 char *end;
195 if (new_format == NULL) {
196 fg_message("Format is %d.", smf->format);
197 } else {
198 tmp = strtol(new_format, &end, 10);
199 if (end - new_format != strlen(new_format)) {
200 fg_critical("Invalid format value, garbage characters after the number.");
201 return (-1);
202 }
204 if (tmp < 0 || tmp > 2) {
205 fg_critical("Invalid format value, valid values are in range 0 - 2, inclusive.");
206 return (-2);
207 }
209 if (smf_set_format(smf, tmp)) {
210 fg_critical("smf_set_format failed.");
211 return (-3);
212 }
214 fg_message("Forma changed to %d.", smf->format);
215 }
217 return (0);
218 }
220 static int
221 cmd_tracks(char *notused)
222 {
223 if (smf->number_of_tracks > 0)
224 fg_message("There are %d tracks, numbered from 1 to %d.", smf->number_of_tracks, smf->number_of_tracks);
225 else
226 fg_message("There are no tracks.");
228 return (0);
229 }
231 static int
232 parse_track_number(const char *arg)
233 {
234 int num;
235 char *end;
237 if (arg == NULL) {
238 if (selected_track == NULL) {
239 fg_message("No track currently selected and no track number given.");
240 return (-1);
241 } else {
242 return (selected_track->track_number);
243 }
244 }
246 num = strtol(arg, &end, 10);
247 if (end - arg != strlen(arg)) {
248 fg_critical("Invalid track number, garbage characters after the number.");
249 return (-1);
250 }
252 if (num < 1 || num > smf->number_of_tracks) {
253 if (smf->number_of_tracks > 0) {
254 fg_critical("Invalid track number specified; valid choices are 1 - %d.", smf->number_of_tracks);
255 } else {
256 fg_critical("There are no tracks.");
257 }
259 return (-1);
260 }
262 return (num);
263 }
265 static int
266 cmd_track(char *arg)
267 {
268 int num;
270 if (arg == NULL) {
271 if (selected_track == NULL)
272 fg_message("No track currently selected.");
273 else
274 fg_message("Currently selected is track number %d, containing %d events.",
275 selected_track->track_number, selected_track->number_of_events);
276 } else {
277 if (smf->number_of_tracks == 0) {
278 fg_message("There are no tracks.");
279 return (-1);
280 }
282 num = parse_track_number(arg);
283 if (num < 0)
284 return (-1);
286 selected_track = smf_get_track_by_number(smf, num);
287 if (selected_track == NULL) {
288 fg_critical("smf_get_track_by_number() failed, track not selected.");
289 return (-3);
290 }
292 selected_event = NULL;
294 fg_message("Track number %d selected; it contains %d events.",
295 selected_track->track_number, selected_track->number_of_events);
296 }
298 return (0);
299 }
301 static int
302 cmd_trackadd(char *notused)
303 {
304 selected_track = smf_track_new();
305 if (selected_track == NULL) {
306 fg_critical("smf_track_new() failed, track not created.");
307 return (-1);
308 }
310 smf_add_track(smf, selected_track);
312 selected_event = NULL;
314 fg_message("Created new track; track number %d selected.", selected_track->track_number);
316 return (0);
317 }
319 static int
320 cmd_trackrm(char *arg)
321 {
322 int num = parse_track_number(arg);
324 if (num < 0)
325 return (-1);
327 if (selected_track != NULL && num == selected_track->track_number) {
328 selected_track = NULL;
329 selected_event = NULL;
330 }
332 smf_track_delete(smf_get_track_by_number(smf, num));
334 fg_message("Track %d removed.", num);
336 return (0);
337 }
339 #define BUFFER_SIZE 1024
341 static int
342 show_event(smf_event_t *event)
343 {
344 int off = 0, i;
345 char *decoded, *type;
347 if (smf_event_is_metadata(event))
348 type = "Metadata";
349 else
350 type = "Event";
352 decoded = smf_event_decode(event);
354 if (decoded == NULL) {
355 decoded = malloc(BUFFER_SIZE);
356 if (decoded == NULL) {
357 fg_critical("show_event: malloc failed.");
358 return (-1);
359 }
361 off += snprintf(decoded + off, BUFFER_SIZE - off, "Unknown event:");
363 for (i = 0; i < event->midi_buffer_length && i < 5; i++)
364 off += snprintf(decoded + off, BUFFER_SIZE - off, " 0x%x", event->midi_buffer[i]);
365 }
367 fg_message("%d: %s: %s, %f seconds, %d pulses, %d delta pulses", event->event_number, type, decoded,
368 event->time_seconds, event->time_pulses, event->delta_time_pulses);
370 free(decoded);
372 return (0);
373 }
375 static int
376 cmd_events(char *notused)
377 {
378 smf_event_t *event;
380 if (selected_track == NULL) {
381 fg_critical("No track selected - please use 'track <number>' command first.");
382 return (-1);
383 }
385 if (selected_track->number_of_events == 0) {
386 fg_message("Selected track is empty.");
387 return (0);
388 }
390 fg_message("List of events in track %d follows:", selected_track->track_number);
392 smf_rewind(smf);
394 while ((event = smf_track_get_next_event(selected_track)) != NULL)
395 show_event(event);
397 smf_rewind(smf);
399 return (0);
400 }
402 static int
403 parse_event_number(const char *arg)
404 {
405 int num;
406 char *end;
408 if (selected_track == NULL) {
409 fg_critical("You need to select track first (using 'track <number>').");
410 return (-1);
411 }
413 if (arg == NULL) {
414 if (selected_event == NULL) {
415 fg_message("No event currently selected and no event number given.");
416 return (-1);
417 } else {
418 return (selected_event->event_number);
419 }
420 }
422 num = strtol(arg, &end, 10);
423 if (end - arg != strlen(arg)) {
424 fg_critical("Invalid event number, garbage characters after the number.");
425 return (-1);
426 }
428 if (num < 1 || num > selected_track->number_of_events) {
429 if (selected_track->number_of_events > 0)
430 fg_critical("Invalid event number specified; valid choices are 1 - %d.", selected_track->number_of_events);
431 else
432 fg_critical("There are no events in currently selected track.");
434 return (-1);
435 }
437 return (num);
438 }
440 static int
441 cmd_event(char *arg)
442 {
443 int num;
445 if (arg == NULL) {
446 if (selected_event == NULL) {
447 fg_message("No event currently selected.");
448 } else {
449 fg_message("Currently selected is event %d, track %d.", selected_event->event_number, selected_track->track_number);
450 show_event(selected_event);
451 }
452 } else {
453 num = parse_event_number(arg);
454 if (num < 0)
455 return (-1);
457 selected_event = smf_track_get_event_by_number(selected_track, num);
458 if (selected_event == NULL) {
459 fg_critical("smf_get_event_by_number() failed, event not selected.");
460 return (-2);
461 }
463 fg_message("Event number %d selected.", selected_event->event_number);
464 show_event(selected_event);
465 }
467 return (0);
468 }
470 static int
471 decode_hex(char *str, unsigned char **buffer, int *length)
472 {
473 int i, value, midi_buffer_length;
474 char buf[3];
475 unsigned char *midi_buffer = NULL;
476 char *end = NULL;
478 if ((strlen(str) % 2) != 0) {
479 fg_critical("Hex value should have even number of characters, you know.");
480 goto error;
481 }
483 midi_buffer_length = strlen(str) / 2;
484 midi_buffer = malloc(midi_buffer_length);
485 if (midi_buffer == NULL) {
486 fg_critical("malloc() failed.");
487 goto error;
488 }
490 for (i = 0; i < midi_buffer_length; i++) {
491 buf[0] = str[i * 2];
492 buf[1] = str[i * 2 + 1];
493 buf[2] = '\0';
494 value = strtoll(buf, &end, 16);
496 if (end - buf != 2) {
497 fg_critical("Garbage characters detected after hex.");
498 goto error;
499 }
501 midi_buffer[i] = value;
502 }
504 *buffer = midi_buffer;
505 *length = midi_buffer_length;
507 return (0);
509 error:
510 if (midi_buffer != NULL)
511 free(midi_buffer);
513 return (-1);
514 }
516 static void
517 eventadd_usage(void)
518 {
519 fg_message("Usage: add <time-in-seconds> <midi-in-hex> - for example, 'add 1 903C7F' will add");
520 fg_message("Note On event, note C4, velocity 127, channel 1, one second from the start of song, channel 1.");
521 }
523 static int
524 cmd_eventadd(char *str)
525 {
526 int midi_buffer_length;
527 double seconds;
528 unsigned char *midi_buffer;
529 char *time, *endtime;
531 if (selected_track == NULL) {
532 fg_critical("Please select a track first, using 'track <number>' command.");
533 return (-1);
534 }
536 if (str == NULL) {
537 eventadd_usage();
538 return (-2);
539 }
541 /* Extract the time. Don't use strsep(3), it doesn't work on SunOS. */
542 time = str;
543 str = strchr(str, ' ');
544 if (str != NULL) {
545 *str = '\0';
546 str++;
547 }
549 seconds = strtod(time, &endtime);
550 if (endtime - time != strlen(time)) {
551 fg_critical("Time is supposed to be a number, without trailing characters.");
552 return (-3);
553 }
555 /* Called with one parameter? */
556 if (str == NULL) {
557 eventadd_usage();
558 return (-4);
559 }
561 if (decode_hex(str, &midi_buffer, &midi_buffer_length)) {
562 eventadd_usage();
563 return (-5);
564 }
566 selected_event = smf_event_new();
567 if (selected_event == NULL) {
568 fg_critical("smf_event_new() failed, event not created.");
569 return (-6);
570 }
572 selected_event->midi_buffer = midi_buffer;
573 selected_event->midi_buffer_length = midi_buffer_length;
575 if (smf_event_is_valid(selected_event) == 0) {
576 fg_critical("Event is invalid from the MIDI specification point of view, not created.");
577 smf_event_delete(selected_event);
578 selected_event = NULL;
579 return (-7);
580 }
582 smf_track_add_event_seconds(selected_track, selected_event, seconds);
584 fg_message("Event created.");
586 return (0);
587 }
589 static int
590 cmd_text(char *str)
591 {
592 double seconds, type;
593 char *time, *typestr, *end;
595 if (selected_track == NULL) {
596 fg_critical("Please select a track first, using 'track <number>' command.");
597 return (-1);
598 }
600 if (str == NULL) {
601 fg_critical("Usage: text <time-in-seconds> <event-type> <text-itself>");
602 return (-2);
603 }
605 /* Extract the time. Don't use strsep(3), it doesn't work on SunOS. */
606 time = str;
607 str = strchr(str, ' ');
608 if (str != NULL) {
609 *str = '\0';
610 str++;
611 }
613 seconds = strtod(time, &end);
614 if (end - time != strlen(time)) {
615 fg_critical("Time is supposed to be a number, without trailing characters.");
616 return (-3);
617 }
619 /* Called with one parameter? */
620 if (str == NULL) {
621 fg_critical("Usage: text <time-in-seconds> <event-type> <text-itself>");
622 return (-4);
623 }
625 /* Extract the event type. */
626 typestr = str;
627 str = strchr(str, ' ');
628 if (str != NULL) {
629 *str = '\0';
630 str++;
631 }
633 type = strtod(typestr, &end);
634 if (end - typestr != strlen(typestr)) {
635 fg_critical("Type is supposed to be a number, without trailing characters.");
636 return (-4);
637 }
639 if (type < 1 || type > 9) {
640 fg_critical("Valid values for type are 1 - 9, inclusive.");
641 return (-5);
642 }
644 /* Called with one parameter? */
645 if (str == NULL) {
646 fg_critical("Usage: text <time-in-seconds> <event-type> <text-itself>");
647 return (-4);
648 }
650 selected_event = smf_event_new_textual(type, str);
651 if (selected_event == NULL) {
652 fg_critical("smf_event_new_textual() failed, event not created.");
653 return (-6);
654 }
656 assert(smf_event_is_valid(selected_event));
658 smf_track_add_event_seconds(selected_track, selected_event, seconds);
660 fg_message("Event created.");
662 return (0);
663 }
666 static int
667 cmd_eventaddeot(char *time)
668 {
669 double seconds;
670 char *end;
672 if (selected_track == NULL) {
673 fg_critical("Please select a track first, using 'track <number>' command.");
674 return (-1);
675 }
677 if (time == NULL) {
678 fg_critical("Please specify the time, in seconds.");
679 return (-2);
680 }
682 seconds = strtod(time, &end);
683 if (end - time != strlen(time)) {
684 fg_critical("Time is supposed to be a number, without trailing characters.");
685 return (-3);
686 }
688 if (smf_track_add_eot_seconds(selected_track, seconds)) {
689 fg_critical("smf_track_add_eot() failed.");
690 return (-4);
691 }
693 fg_message("Event created.");
695 return (0);
696 }
698 static int
699 cmd_eventrm(char *number)
700 {
701 int num = parse_event_number(number);
703 if (num < 0)
704 return (-1);
706 if (selected_event != NULL && num == selected_event->event_number)
707 selected_event = NULL;
709 smf_event_delete(smf_track_get_event_by_number(selected_track, num));
711 fg_message("Event #%d removed.", num);
713 return (0);
714 }
716 static int
717 cmd_tempo(char *notused)
718 {
719 int i;
720 smf_tempo_t *tempo;
722 for (i = 0;; i++) {
723 tempo = smf_get_tempo_by_number(smf, i);
724 if (tempo == NULL)
725 break;
727 fg_message("Tempo #%d: Starts at %d pulses, %f seconds, setting %d microseconds per quarter note, %.2f BPM.",
728 i, tempo->time_pulses, tempo->time_seconds, tempo->microseconds_per_quarter_note,
729 60000000.0 / (double)tempo->microseconds_per_quarter_note);
730 fg_message("Time signature: %d/%d, %d clocks per click, %d 32nd notes per quarter note.",
731 tempo->numerator, tempo->denominator, tempo->clocks_per_click, tempo->notes_per_note);
732 }
734 return (0);
735 }
737 static int
738 cmd_length(char *notused)
739 {
740 fg_message("Length: %d pulses, %f seconds.", smf_get_length_pulses(smf), smf_get_length_seconds(smf));
742 return (0);
743 }
745 static int
746 cmd_version(char *notused)
747 {
748 fg_message("libsmf version %s.", smf_get_version());
750 return (0);
751 }
753 static int
754 cmd_exit(char *notused)
755 {
756 fg_debug("Good bye.");
757 exit(0);
758 }
760 static int cmd_help(char *notused);
762 static struct command_struct {
763 char *name;
764 int (*function)(char *command);
765 char *help;
766 } commands[] = {{"help", cmd_help, "Show this help."},
767 {"?", cmd_help, NULL},
768 {"load", cmd_load, "Load named file."},
769 {"open", cmd_load},
770 {"save", cmd_save, "Save to named file."},
771 {"ppqn", cmd_ppqn, "Show ppqn (aka division), or set ppqn if used with parameter."},
772 {"format", cmd_format, "Show format, or set format if used with parameter."},
773 {"tracks", cmd_tracks, "Show number of tracks."},
774 {"track", cmd_track, "Show number of currently selected track, or select a track."},
775 {"trackadd", cmd_trackadd, "Add a track and select it."},
776 {"trackrm", cmd_trackrm, "Remove currently selected track."},
777 {"events", cmd_events, "Show events in the currently selected track."},
778 {"event", cmd_event, "Show number of currently selected event, or select an event."},
779 {"add", cmd_eventadd, "Add an event and select it."},
780 {"text", cmd_text, "Add textual event and select it."},
781 {"eventadd", cmd_eventadd, NULL},
782 {"eot", cmd_eventaddeot, "Add an End Of Track event."},
783 {"eventaddeot", cmd_eventaddeot, NULL},
784 {"eventrm", cmd_eventrm, NULL},
785 {"rm", cmd_eventrm, "Remove currently selected event."},
786 {"tempo", cmd_tempo, "Show tempo map."},
787 {"length", cmd_length, "Show length of the song."},
788 {"version", cmd_version, "Show libsmf version."},
789 {"exit", cmd_exit, "Exit to shell."},
790 {"quit", cmd_exit, NULL},
791 {"bye", cmd_exit, NULL},
792 {NULL, NULL, NULL}};
794 static int
795 cmd_help(char *notused)
796 {
797 int i, padding_length;
798 char padding[COMMAND_LENGTH + 1];
799 struct command_struct *tmp;
801 fg_message("Available commands:");
803 for (tmp = commands; tmp->name != NULL; tmp++) {
804 /* Skip commands with no help string. */
805 if (tmp->help == NULL)
806 continue;
808 padding_length = COMMAND_LENGTH - strlen(tmp->name);
809 assert(padding_length >= 0);
810 for (i = 0; i < padding_length; i++)
811 padding[i] = ' ';
812 padding[i] = '\0';
814 fg_message("%s:%s%s", tmp->name, padding, tmp->help);
815 }
817 return (0);
818 }
820 /**
821 * Removes (in place) all whitespace characters before the first
822 * non-whitespace and all trailing whitespace characters. Replaces
823 * more than one consecutive whitespace characters with one.
824 */
825 static void
826 strip_unneeded_whitespace(char *str, int len)
827 {
828 char *src, *dest;
829 int skip_white = 1;
831 for (src = str, dest = str; src < dest + len; src++) {
832 if (*src == '\n' || *src == '\0') {
833 *dest = '\0';
834 break;
835 }
837 if (isspace(*src)) {
838 if (skip_white)
839 continue;
841 skip_white = 1;
842 } else {
843 skip_white = 0;
844 }
846 *dest = *src;
847 dest++;
848 }
850 /* Remove trailing whitespace. */
851 len = strlen(dest);
852 if (isspace(dest[len - 1]))
853 dest[len - 1] = '\0';
854 }
856 static char *
857 read_command(void)
858 {
859 char *buf;
860 int len;
862 #ifdef HAVE_LIBREADLINE
863 buf = readline("smfsh> ");
864 #else
865 buf = malloc(1024);
866 if (buf == NULL) {
867 fg_critical("Malloc failed.");
868 return (NULL);
869 }
871 fprintf(stdout, "smfsh> ");
872 fflush(stdout);
874 buf = fgets(buf, 1024, stdin);
875 #endif
877 if (buf == NULL) {
878 fprintf(stdout, "exit\n");
879 return (strdup("exit"));
880 }
882 strip_unneeded_whitespace(buf, 1024);
884 len = strlen(buf);
886 if (len == 0)
887 return (read_command());
889 #ifdef HAVE_LIBREADLINE
890 add_history(buf);
891 #endif
893 return (buf);
894 }
896 static int
897 execute_command(char *line)
898 {
899 char *command, *args;
900 struct command_struct *tmp;
902 command = line;
903 args = strchr(line, ' ');
904 if (args != NULL) {
905 *args = '\0';
906 args++;
907 }
909 for (tmp = commands; tmp->name != NULL; tmp++) {
910 if (strcmp(tmp->name, command) == 0)
911 return ((tmp->function)(args));
912 }
914 fg_warning("No such command: '%s'. Type 'help' to see available commands.", command);
916 return (-1);
917 }
919 static void
920 read_and_execute_command(void)
921 {
922 int ret;
923 char *command_line, *command, *next_command;
925 command = command_line = read_command();
927 do {
928 next_command = strchr(command, ';');
929 if (next_command != NULL) {
930 *next_command = '\0';
931 next_command++;
932 }
934 strip_unneeded_whitespace(command, 1024);
935 if (strlen(command) > 0) {
936 ret = execute_command(command);
937 if (ret)
938 fg_warning("Command finished with error.");
939 }
941 command = next_command;
943 } while (command);
945 free(command_line);
946 }
948 #ifdef HAVE_LIBREADLINE
950 static char *
951 smfsh_command_generator(const char *text, int state)
952 {
953 static struct command_struct *command = commands;
954 char *tmp;
956 if (state == 0)
957 command = commands;
959 while (command->name != NULL) {
960 tmp = command->name;
961 command++;
963 if (strncmp(tmp, text, strlen(text)) == 0)
964 return (strdup(tmp));
965 }
967 return (NULL);
968 }
970 static char **
971 smfsh_completion(const char *text, int start, int end)
972 {
973 int i;
975 /* Return NULL if "text" is not the first word in the input line. */
976 if (start != 0) {
977 for (i = 0; i < start; i++) {
978 if (!isspace(rl_line_buffer[i]))
979 return (NULL);
980 }
981 }
983 return (rl_completion_matches(text, smfsh_command_generator));
984 }
986 #endif
988 static void
989 usage(void)
990 {
991 fprintf(stderr, "usage: smfsh [-V | file]\n");
993 exit(EX_USAGE);
994 }
996 int
997 main(int argc, char *argv[])
998 {
999 int ch;
1001 while ((ch = getopt(argc, argv, "V")) != -1) {
1002 switch (ch) {
1003 case 'V':
1004 cmd_version(NULL);
1005 exit(EX_OK);
1007 case '?':
1008 default:
1009 usage();
1013 if (argc > 2)
1014 usage();
1016 /*g_log_set_default_handler(log_handler, NULL);*/
1018 smf = smf_new();
1019 if (smf == NULL) {
1020 fg_critical("Cannot initialize smf_t.");
1021 return (-1);
1024 if (argc == 2)
1025 cmd_load(argv[1]);
1026 else
1027 cmd_trackadd(NULL);
1029 #ifdef HAVE_LIBREADLINE
1030 rl_readline_name = "smfsh";
1031 rl_attempted_completion_function = smfsh_completion;
1032 #endif
1034 for (;;)
1035 read_and_execute_command();
1037 return (0);