smflite

diff 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 diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/smfsh/smfsh.c	Thu Jan 26 15:35:18 2012 +0200
     1.3 @@ -0,0 +1,1039 @@
     1.4 +/*-
     1.5 + * Copyright (c) 2007, 2008 Edward Tomasz NapieraƂa <trasz@FreeBSD.org>
     1.6 + * All rights reserved.
     1.7 + *
     1.8 + * Redistribution and use in source and binary forms, with or without
     1.9 + * modification, are permitted provided that the following conditions
    1.10 + * are met:
    1.11 + * 1. Redistributions of source code must retain the above copyright
    1.12 + *    notice, this list of conditions and the following disclaimer.
    1.13 + * 2. Redistributions in binary form must reproduce the above copyright
    1.14 + *    notice, this list of conditions and the following disclaimer in the
    1.15 + *    documentation and/or other materials provided with the distribution.
    1.16 + *
    1.17 + * ALTHOUGH THIS SOFTWARE IS MADE OF WIN AND SCIENCE, IT IS PROVIDED BY THE
    1.18 + * AUTHOR AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
    1.19 + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
    1.20 + * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
    1.21 + * THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
    1.22 + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
    1.23 + * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
    1.24 + * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
    1.25 + * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
    1.26 + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
    1.27 + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    1.28 + *
    1.29 + */
    1.30 +
    1.31 +/**
    1.32 + * \file
    1.33 + *
    1.34 + * "SMF shell", command line utility.
    1.35 + */
    1.36 +
    1.37 +#include <stdio.h>
    1.38 +#include <stdlib.h>
    1.39 +#include <unistd.h>
    1.40 +#ifdef __MINGW32__
    1.41 +#define EX_OK 0
    1.42 +#define EX_USAGE 64
    1.43 +#else /* ! __MINGW32__ */
    1.44 +#include <sysexits.h>
    1.45 +#endif /* ! __MINGW32__ */
    1.46 +#include <string.h>
    1.47 +#include <ctype.h>
    1.48 +#include <assert.h>
    1.49 +#include "smf.h"
    1.50 +#include "fake_glib.h"
    1.51 +
    1.52 +#ifdef HAVE_LIBREADLINE
    1.53 +#include <readline/readline.h>
    1.54 +#include <readline/history.h>
    1.55 +#endif
    1.56 +
    1.57 +smf_track_t *selected_track = NULL;
    1.58 +smf_event_t *selected_event = NULL;
    1.59 +smf_t *smf = NULL;
    1.60 +char *last_file_name = NULL;
    1.61 +
    1.62 +#define COMMAND_LENGTH 10
    1.63 +
    1.64 +/*static void
    1.65 +log_handler(const gchar *log_domain, GLogLevelFlags log_level, const gchar *message, gpointer notused)
    1.66 +{
    1.67 +	if (strcmp(log_domain, "smfsh") == 0)
    1.68 +		fprintf(stderr, "%s\n", message);
    1.69 +	else
    1.70 +		fprintf(stderr, "%s: %s\n", log_domain, message);
    1.71 +}*/
    1.72 +
    1.73 +static int cmd_track(char *arg);
    1.74 +
    1.75 +static int
    1.76 +cmd_load(char *file_name)
    1.77 +{
    1.78 +	char *decoded;
    1.79 +
    1.80 +	if (file_name == NULL) {
    1.81 +		if (last_file_name == NULL) {
    1.82 +			fg_critical("Please specify file name.");
    1.83 +			return (-1);
    1.84 +		}
    1.85 +
    1.86 +		file_name = strdup(last_file_name);
    1.87 +	} else {
    1.88 +		file_name = strdup(file_name);
    1.89 +	}
    1.90 +
    1.91 +	selected_track = NULL;
    1.92 +	selected_event = NULL;
    1.93 +
    1.94 +	if (smf != NULL) {
    1.95 +		smf_delete(smf);
    1.96 +		smf = NULL;
    1.97 +	}
    1.98 +
    1.99 +	if (last_file_name != NULL)
   1.100 +		free(last_file_name);
   1.101 +	last_file_name = strdup(file_name);
   1.102 +
   1.103 +	smf = smf_load(file_name);
   1.104 +	if (smf == NULL) {
   1.105 +		fg_critical("Couldn't load '%s'.", file_name);
   1.106 +
   1.107 +		smf = smf_new();
   1.108 +		if (smf == NULL) {
   1.109 +			fg_critical("Cannot initialize smf_t.");
   1.110 +			return (-1);
   1.111 +		}
   1.112 +
   1.113 +		return (-2);
   1.114 +	}
   1.115 +
   1.116 +	fg_message("File '%s' loaded.", file_name);
   1.117 +	decoded = smf_decode(smf);
   1.118 +	fg_message("%s.", decoded);
   1.119 +	free(decoded);
   1.120 +
   1.121 +	cmd_track("1");
   1.122 +
   1.123 +	free(file_name);
   1.124 +
   1.125 +	return (0);
   1.126 +}
   1.127 +
   1.128 +static int
   1.129 +cmd_save(char *file_name)
   1.130 +{
   1.131 +	int ret;
   1.132 +
   1.133 +	if (file_name == NULL) {
   1.134 +		if (last_file_name == NULL) {
   1.135 +			fg_critical("Please specify file name.");
   1.136 +			return (-1);
   1.137 +		}
   1.138 +
   1.139 +		file_name = strdup(last_file_name);
   1.140 +	} else {
   1.141 +		file_name = strdup(file_name);
   1.142 +	}
   1.143 +
   1.144 +	if (last_file_name != NULL)
   1.145 +		free(last_file_name);
   1.146 +	last_file_name = strdup(file_name);
   1.147 +
   1.148 +	ret = smf_save(smf, file_name);
   1.149 +	if (ret) {
   1.150 +		fg_critical("Couldn't save '%s'", file_name);
   1.151 +		return (-1);
   1.152 +	}
   1.153 +
   1.154 +	fg_message("File '%s' saved.", file_name);
   1.155 +
   1.156 +	free(file_name);
   1.157 +
   1.158 +	return (0);
   1.159 +}
   1.160 +
   1.161 +static int
   1.162 +cmd_ppqn(char *new_ppqn)
   1.163 +{
   1.164 +	int tmp;
   1.165 +	char *end;
   1.166 +
   1.167 +	if (new_ppqn == NULL) {
   1.168 +		fg_message("Pulses Per Quarter Note (aka Division) is %d.", smf->ppqn);
   1.169 +	} else {
   1.170 +		tmp = strtol(new_ppqn, &end, 10);
   1.171 +		if (end - new_ppqn != strlen(new_ppqn)) {
   1.172 +			fg_critical("Invalid PPQN, garbage characters after the number.");
   1.173 +			return (-1);
   1.174 +		}
   1.175 +
   1.176 +		if (tmp <= 0) {
   1.177 +			fg_critical("Invalid PPQN, valid values are greater than zero.");
   1.178 +			return (-2);
   1.179 +		}
   1.180 +
   1.181 +		if (smf_set_ppqn(smf, tmp)) {
   1.182 +			fg_message("smf_set_ppqn failed.");
   1.183 +			return (-3);
   1.184 +		}
   1.185 +
   1.186 +		fg_message("Pulses Per Quarter Note changed to %d.", smf->ppqn);
   1.187 +	}
   1.188 +	
   1.189 +	return (0);
   1.190 +}
   1.191 +
   1.192 +static int
   1.193 +cmd_format(char *new_format)
   1.194 +{
   1.195 +	int tmp;
   1.196 +	char *end;
   1.197 +
   1.198 +	if (new_format == NULL) {
   1.199 +		fg_message("Format is %d.", smf->format);
   1.200 +	} else {
   1.201 +		tmp = strtol(new_format, &end, 10);
   1.202 +		if (end - new_format != strlen(new_format)) {
   1.203 +			fg_critical("Invalid format value, garbage characters after the number.");
   1.204 +			return (-1);
   1.205 +		}
   1.206 +
   1.207 +		if (tmp < 0 || tmp > 2) {
   1.208 +			fg_critical("Invalid format value, valid values are in range 0 - 2, inclusive.");
   1.209 +			return (-2);
   1.210 +		}
   1.211 +
   1.212 +		if (smf_set_format(smf, tmp)) {
   1.213 +			fg_critical("smf_set_format failed.");
   1.214 +			return (-3);
   1.215 +		}
   1.216 +
   1.217 +		fg_message("Forma changed to %d.", smf->format);
   1.218 +	}
   1.219 +	
   1.220 +	return (0);
   1.221 +}
   1.222 +
   1.223 +static int
   1.224 +cmd_tracks(char *notused)
   1.225 +{
   1.226 +	if (smf->number_of_tracks > 0)
   1.227 +		fg_message("There are %d tracks, numbered from 1 to %d.", smf->number_of_tracks, smf->number_of_tracks);
   1.228 +	else
   1.229 +		fg_message("There are no tracks.");
   1.230 +
   1.231 +	return (0);
   1.232 +}
   1.233 +
   1.234 +static int
   1.235 +parse_track_number(const char *arg)
   1.236 +{
   1.237 +	int num;
   1.238 +	char *end;
   1.239 +
   1.240 +	if (arg == NULL) {
   1.241 +		if (selected_track == NULL) {
   1.242 +			fg_message("No track currently selected and no track number given.");
   1.243 +			return (-1);
   1.244 +		} else {
   1.245 +			return (selected_track->track_number);
   1.246 +		}
   1.247 +	}
   1.248 +
   1.249 +	num = strtol(arg, &end, 10);
   1.250 +	if (end - arg != strlen(arg)) {
   1.251 +		fg_critical("Invalid track number, garbage characters after the number.");
   1.252 +		return (-1);
   1.253 +	}
   1.254 +
   1.255 +	if (num < 1 || num > smf->number_of_tracks) {
   1.256 +		if (smf->number_of_tracks > 0) {
   1.257 +			fg_critical("Invalid track number specified; valid choices are 1 - %d.", smf->number_of_tracks);
   1.258 +		} else {
   1.259 +			fg_critical("There are no tracks.");
   1.260 +		}
   1.261 +
   1.262 +		return (-1);
   1.263 +	}
   1.264 +
   1.265 +	return (num);
   1.266 +}
   1.267 +
   1.268 +static int
   1.269 +cmd_track(char *arg)
   1.270 +{
   1.271 +	int num;
   1.272 +
   1.273 +	if (arg == NULL) {
   1.274 +		if (selected_track == NULL)
   1.275 +			fg_message("No track currently selected.");
   1.276 +		else
   1.277 +			fg_message("Currently selected is track number %d, containing %d events.",
   1.278 +				selected_track->track_number, selected_track->number_of_events);
   1.279 +	} else {
   1.280 +		if (smf->number_of_tracks == 0) {
   1.281 +			fg_message("There are no tracks.");
   1.282 +			return (-1);
   1.283 +		}
   1.284 +
   1.285 +		num = parse_track_number(arg);
   1.286 +		if (num < 0)
   1.287 +			return (-1);
   1.288 +
   1.289 +		selected_track = smf_get_track_by_number(smf, num);
   1.290 +		if (selected_track == NULL) {
   1.291 +			fg_critical("smf_get_track_by_number() failed, track not selected.");
   1.292 +			return (-3);
   1.293 +		}
   1.294 +
   1.295 +		selected_event = NULL;
   1.296 +
   1.297 +		fg_message("Track number %d selected; it contains %d events.",
   1.298 +				selected_track->track_number, selected_track->number_of_events);
   1.299 +	}
   1.300 +
   1.301 +	return (0);
   1.302 +}
   1.303 +
   1.304 +static int
   1.305 +cmd_trackadd(char *notused)
   1.306 +{
   1.307 +	selected_track = smf_track_new();
   1.308 +	if (selected_track == NULL) {
   1.309 +		fg_critical("smf_track_new() failed, track not created.");
   1.310 +		return (-1);
   1.311 +	}
   1.312 +
   1.313 +	smf_add_track(smf, selected_track);
   1.314 +
   1.315 +	selected_event = NULL;
   1.316 +
   1.317 +	fg_message("Created new track; track number %d selected.", selected_track->track_number);
   1.318 +
   1.319 +	return (0);
   1.320 +}
   1.321 +
   1.322 +static int
   1.323 +cmd_trackrm(char *arg)
   1.324 +{
   1.325 +	int num = parse_track_number(arg);
   1.326 +
   1.327 +	if (num < 0)
   1.328 +		return (-1);
   1.329 +
   1.330 +	if (selected_track != NULL && num == selected_track->track_number) {
   1.331 +		selected_track = NULL;
   1.332 +		selected_event = NULL;
   1.333 +	}
   1.334 +
   1.335 +	smf_track_delete(smf_get_track_by_number(smf, num));
   1.336 +
   1.337 +	fg_message("Track %d removed.", num);
   1.338 +
   1.339 +	return (0);
   1.340 +}
   1.341 +
   1.342 +#define BUFFER_SIZE 1024
   1.343 +
   1.344 +static int
   1.345 +show_event(smf_event_t *event)
   1.346 +{
   1.347 +	int off = 0, i;
   1.348 +	char *decoded, *type;
   1.349 +
   1.350 +	if (smf_event_is_metadata(event))
   1.351 +		type = "Metadata";
   1.352 +	else
   1.353 +		type = "Event";
   1.354 +	
   1.355 +	decoded = smf_event_decode(event);
   1.356 +
   1.357 +	if (decoded == NULL) {
   1.358 +		decoded = malloc(BUFFER_SIZE);
   1.359 +		if (decoded == NULL) {
   1.360 +			fg_critical("show_event: malloc failed.");
   1.361 +			return (-1);
   1.362 +		}
   1.363 +
   1.364 +		off += snprintf(decoded + off, BUFFER_SIZE - off, "Unknown event:");
   1.365 +
   1.366 +		for (i = 0; i < event->midi_buffer_length && i < 5; i++)
   1.367 +			off += snprintf(decoded + off, BUFFER_SIZE - off, " 0x%x", event->midi_buffer[i]);
   1.368 +	}
   1.369 +
   1.370 +	fg_message("%d: %s: %s, %f seconds, %d pulses, %d delta pulses", event->event_number, type, decoded,
   1.371 +	    event->time_seconds, event->time_pulses, event->delta_time_pulses);
   1.372 +
   1.373 +	free(decoded);
   1.374 +
   1.375 +	return (0);
   1.376 +}
   1.377 +
   1.378 +static int
   1.379 +cmd_events(char *notused)
   1.380 +{
   1.381 +	smf_event_t *event;
   1.382 +
   1.383 +	if (selected_track == NULL) {
   1.384 +		fg_critical("No track selected - please use 'track <number>' command first.");
   1.385 +		return (-1);
   1.386 +	}
   1.387 +
   1.388 +	if (selected_track->number_of_events == 0) {
   1.389 +		fg_message("Selected track is empty.");
   1.390 +		return (0);
   1.391 +	}
   1.392 +
   1.393 +	fg_message("List of events in track %d follows:", selected_track->track_number);
   1.394 +
   1.395 +	smf_rewind(smf);
   1.396 +
   1.397 +	while ((event = smf_track_get_next_event(selected_track)) != NULL)
   1.398 +		show_event(event);
   1.399 +
   1.400 +	smf_rewind(smf);
   1.401 +
   1.402 +	return (0);
   1.403 +}
   1.404 +
   1.405 +static int
   1.406 +parse_event_number(const char *arg)
   1.407 +{
   1.408 +	int num;
   1.409 +	char *end;
   1.410 +
   1.411 +	if (selected_track == NULL) {
   1.412 +		fg_critical("You need to select track first (using 'track <number>').");
   1.413 +		return (-1);
   1.414 +	}
   1.415 +
   1.416 +	if (arg == NULL) {
   1.417 +		if (selected_event == NULL) {
   1.418 +			fg_message("No event currently selected and no event number given.");
   1.419 +			return (-1);
   1.420 +		} else {
   1.421 +			return (selected_event->event_number);
   1.422 +		}
   1.423 +	}
   1.424 +
   1.425 +	num = strtol(arg, &end, 10);
   1.426 +	if (end - arg != strlen(arg)) {
   1.427 +		fg_critical("Invalid event number, garbage characters after the number.");
   1.428 +		return (-1);
   1.429 +	}
   1.430 +
   1.431 +	if (num < 1 || num > selected_track->number_of_events) {
   1.432 +		if (selected_track->number_of_events > 0)
   1.433 +			fg_critical("Invalid event number specified; valid choices are 1 - %d.", selected_track->number_of_events);
   1.434 +		else
   1.435 +			fg_critical("There are no events in currently selected track.");
   1.436 +
   1.437 +		return (-1);
   1.438 +	}
   1.439 +
   1.440 +	return (num);
   1.441 +}
   1.442 +
   1.443 +static int
   1.444 +cmd_event(char *arg)
   1.445 +{
   1.446 +	int num;
   1.447 +
   1.448 +	if (arg == NULL) {
   1.449 +		if (selected_event == NULL) {
   1.450 +			fg_message("No event currently selected.");
   1.451 +		} else {
   1.452 +			fg_message("Currently selected is event %d, track %d.", selected_event->event_number, selected_track->track_number);
   1.453 +			show_event(selected_event);
   1.454 +		}
   1.455 +	} else {
   1.456 +		num = parse_event_number(arg);
   1.457 +		if (num < 0)
   1.458 +			return (-1);
   1.459 +
   1.460 +		selected_event = smf_track_get_event_by_number(selected_track, num);
   1.461 +		if (selected_event == NULL) {
   1.462 +			fg_critical("smf_get_event_by_number() failed, event not selected.");
   1.463 +			return (-2);
   1.464 +		}
   1.465 +
   1.466 +		fg_message("Event number %d selected.", selected_event->event_number);
   1.467 +		show_event(selected_event);
   1.468 +	}
   1.469 +
   1.470 +	return (0);
   1.471 +}
   1.472 +
   1.473 +static int
   1.474 +decode_hex(char *str, unsigned char **buffer, int *length)
   1.475 +{
   1.476 +	int i, value, midi_buffer_length;
   1.477 +	char buf[3];
   1.478 +	unsigned char *midi_buffer = NULL;
   1.479 +	char *end = NULL;
   1.480 +
   1.481 +	if ((strlen(str) % 2) != 0) {
   1.482 +		fg_critical("Hex value should have even number of characters, you know.");
   1.483 +		goto error;
   1.484 +	}
   1.485 +
   1.486 +	midi_buffer_length = strlen(str) / 2;
   1.487 +	midi_buffer = malloc(midi_buffer_length);
   1.488 +	if (midi_buffer == NULL) {
   1.489 +		fg_critical("malloc() failed.");
   1.490 +		goto error;
   1.491 +	}
   1.492 +
   1.493 +	for (i = 0; i < midi_buffer_length; i++) {
   1.494 +		buf[0] = str[i * 2];
   1.495 +		buf[1] = str[i * 2 + 1];
   1.496 +		buf[2] = '\0';
   1.497 +		value = strtoll(buf, &end, 16);
   1.498 +
   1.499 +		if (end - buf != 2) {
   1.500 +			fg_critical("Garbage characters detected after hex.");
   1.501 +			goto error;
   1.502 +		}
   1.503 +
   1.504 +		midi_buffer[i] = value;
   1.505 +	}
   1.506 +
   1.507 +	*buffer = midi_buffer;
   1.508 +	*length = midi_buffer_length;
   1.509 +
   1.510 +	return (0);
   1.511 +
   1.512 +error:
   1.513 +	if (midi_buffer != NULL)
   1.514 +		free(midi_buffer);
   1.515 +
   1.516 +	return (-1);
   1.517 +}
   1.518 +
   1.519 +static void
   1.520 +eventadd_usage(void)
   1.521 +{
   1.522 +	fg_message("Usage: add <time-in-seconds> <midi-in-hex> - for example, 'add 1 903C7F' will add");
   1.523 +	fg_message("Note On event, note C4, velocity 127, channel 1, one second from the start of song, channel 1.");
   1.524 +}
   1.525 +
   1.526 +static int
   1.527 +cmd_eventadd(char *str)
   1.528 +{
   1.529 +	int midi_buffer_length;
   1.530 +	double seconds;
   1.531 +	unsigned char *midi_buffer;
   1.532 +	char *time, *endtime;
   1.533 +
   1.534 +	if (selected_track == NULL) {
   1.535 +		fg_critical("Please select a track first, using 'track <number>' command.");
   1.536 +		return (-1);
   1.537 +	}
   1.538 +
   1.539 +	if (str == NULL) {
   1.540 +		eventadd_usage();
   1.541 +		return (-2);
   1.542 +	}
   1.543 +
   1.544 +	/* Extract the time.  Don't use strsep(3), it doesn't work on SunOS. */
   1.545 +	time = str;
   1.546 +	str = strchr(str, ' ');
   1.547 +	if (str != NULL) {
   1.548 +		*str = '\0';
   1.549 +		str++;
   1.550 +	}
   1.551 +
   1.552 +	seconds = strtod(time, &endtime);
   1.553 +	if (endtime - time != strlen(time)) {
   1.554 +		fg_critical("Time is supposed to be a number, without trailing characters.");
   1.555 +		return (-3);
   1.556 +	}
   1.557 +
   1.558 +	/* Called with one parameter? */
   1.559 +	if (str == NULL) {
   1.560 +		eventadd_usage();
   1.561 +		return (-4);
   1.562 +	}
   1.563 +
   1.564 +	if (decode_hex(str, &midi_buffer, &midi_buffer_length)) {
   1.565 +		eventadd_usage();
   1.566 +		return (-5);
   1.567 +	}
   1.568 +
   1.569 +	selected_event = smf_event_new();
   1.570 +	if (selected_event == NULL) {
   1.571 +		fg_critical("smf_event_new() failed, event not created.");
   1.572 +		return (-6);
   1.573 +	}
   1.574 +
   1.575 +	selected_event->midi_buffer = midi_buffer;
   1.576 +	selected_event->midi_buffer_length = midi_buffer_length;
   1.577 +
   1.578 +	if (smf_event_is_valid(selected_event) == 0) {
   1.579 +		fg_critical("Event is invalid from the MIDI specification point of view, not created.");
   1.580 +		smf_event_delete(selected_event);
   1.581 +		selected_event = NULL;
   1.582 +		return (-7);
   1.583 +	}
   1.584 +
   1.585 +	smf_track_add_event_seconds(selected_track, selected_event, seconds);
   1.586 +
   1.587 +	fg_message("Event created.");
   1.588 +
   1.589 +	return (0);
   1.590 +}
   1.591 +
   1.592 +static int
   1.593 +cmd_text(char *str)
   1.594 +{
   1.595 +	double seconds, type;
   1.596 +	char *time, *typestr, *end;
   1.597 +
   1.598 +	if (selected_track == NULL) {
   1.599 +		fg_critical("Please select a track first, using 'track <number>' command.");
   1.600 +		return (-1);
   1.601 +	}
   1.602 +
   1.603 +	if (str == NULL) {
   1.604 +		fg_critical("Usage: text <time-in-seconds> <event-type> <text-itself>");
   1.605 +		return (-2);
   1.606 +	}
   1.607 +
   1.608 +	/* Extract the time.  Don't use strsep(3), it doesn't work on SunOS. */
   1.609 +	time = str;
   1.610 +	str = strchr(str, ' ');
   1.611 +	if (str != NULL) {
   1.612 +		*str = '\0';
   1.613 +		str++;
   1.614 +	}
   1.615 +
   1.616 +	seconds = strtod(time, &end);
   1.617 +	if (end - time != strlen(time)) {
   1.618 +		fg_critical("Time is supposed to be a number, without trailing characters.");
   1.619 +		return (-3);
   1.620 +	}
   1.621 +
   1.622 +	/* Called with one parameter? */
   1.623 +	if (str == NULL) {
   1.624 +		fg_critical("Usage: text <time-in-seconds> <event-type> <text-itself>");
   1.625 +		return (-4);
   1.626 +	}
   1.627 +
   1.628 +	/* Extract the event type. */
   1.629 +	typestr = str;
   1.630 +	str = strchr(str, ' ');
   1.631 +	if (str != NULL) {
   1.632 +		*str = '\0';
   1.633 +		str++;
   1.634 +	}
   1.635 +
   1.636 +	type = strtod(typestr, &end);
   1.637 +	if (end - typestr != strlen(typestr)) {
   1.638 +		fg_critical("Type is supposed to be a number, without trailing characters.");
   1.639 +		return (-4);
   1.640 +	}
   1.641 +
   1.642 +	if (type < 1 || type > 9) {
   1.643 +		fg_critical("Valid values for type are 1 - 9, inclusive.");
   1.644 +		return (-5);
   1.645 +	}
   1.646 +
   1.647 +	/* Called with one parameter? */
   1.648 +	if (str == NULL) {
   1.649 +		fg_critical("Usage: text <time-in-seconds> <event-type> <text-itself>");
   1.650 +		return (-4);
   1.651 +	}
   1.652 +
   1.653 +	selected_event = smf_event_new_textual(type, str);
   1.654 +	if (selected_event == NULL) {
   1.655 +		fg_critical("smf_event_new_textual() failed, event not created.");
   1.656 +		return (-6);
   1.657 +	}
   1.658 +
   1.659 +	assert(smf_event_is_valid(selected_event));
   1.660 +
   1.661 +	smf_track_add_event_seconds(selected_track, selected_event, seconds);
   1.662 +
   1.663 +	fg_message("Event created.");
   1.664 +
   1.665 +	return (0);
   1.666 +}
   1.667 +
   1.668 +
   1.669 +static int
   1.670 +cmd_eventaddeot(char *time)
   1.671 +{
   1.672 +	double seconds;
   1.673 +	char *end;
   1.674 +
   1.675 +	if (selected_track == NULL) {
   1.676 +		fg_critical("Please select a track first, using 'track <number>' command.");
   1.677 +		return (-1);
   1.678 +	}
   1.679 +
   1.680 +	if (time == NULL) {
   1.681 +		fg_critical("Please specify the time, in seconds.");
   1.682 +		return (-2);
   1.683 +	}
   1.684 +
   1.685 +	seconds = strtod(time, &end);
   1.686 +	if (end - time != strlen(time)) {
   1.687 +		fg_critical("Time is supposed to be a number, without trailing characters.");
   1.688 +		return (-3);
   1.689 +	}
   1.690 +
   1.691 +	if (smf_track_add_eot_seconds(selected_track, seconds)) {
   1.692 +		fg_critical("smf_track_add_eot() failed.");
   1.693 +		return (-4);
   1.694 +	}
   1.695 +
   1.696 +	fg_message("Event created.");
   1.697 +
   1.698 +	return (0);
   1.699 +}
   1.700 +
   1.701 +static int
   1.702 +cmd_eventrm(char *number)
   1.703 +{
   1.704 +	int num = parse_event_number(number);
   1.705 +
   1.706 +	if (num < 0)
   1.707 +		return (-1);
   1.708 +
   1.709 +	if (selected_event != NULL && num == selected_event->event_number)
   1.710 +		selected_event = NULL;
   1.711 +
   1.712 +	smf_event_delete(smf_track_get_event_by_number(selected_track, num));
   1.713 +
   1.714 +	fg_message("Event #%d removed.", num);
   1.715 +
   1.716 +	return (0);
   1.717 +}
   1.718 +
   1.719 +static int
   1.720 +cmd_tempo(char *notused)
   1.721 +{
   1.722 +	int i;
   1.723 +	smf_tempo_t *tempo;
   1.724 +
   1.725 +	for (i = 0;; i++) {
   1.726 +		tempo = smf_get_tempo_by_number(smf, i);
   1.727 +		if (tempo == NULL)
   1.728 +			break;
   1.729 +
   1.730 +		fg_message("Tempo #%d: Starts at %d pulses, %f seconds, setting %d microseconds per quarter note, %.2f BPM.",
   1.731 +		    i, tempo->time_pulses, tempo->time_seconds, tempo->microseconds_per_quarter_note,
   1.732 +		    60000000.0 / (double)tempo->microseconds_per_quarter_note);
   1.733 +		fg_message("Time signature: %d/%d, %d clocks per click, %d 32nd notes per quarter note.",
   1.734 +		    tempo->numerator, tempo->denominator, tempo->clocks_per_click, tempo->notes_per_note);
   1.735 +	}
   1.736 +
   1.737 +	return (0);
   1.738 +}
   1.739 +
   1.740 +static int
   1.741 +cmd_length(char *notused)
   1.742 +{
   1.743 +	fg_message("Length: %d pulses, %f seconds.", smf_get_length_pulses(smf), smf_get_length_seconds(smf));
   1.744 +
   1.745 +	return (0);
   1.746 +}
   1.747 +
   1.748 +static int
   1.749 +cmd_version(char *notused)
   1.750 +{
   1.751 +	fg_message("libsmf version %s.", smf_get_version());
   1.752 +
   1.753 +	return (0);
   1.754 +}
   1.755 +
   1.756 +static int
   1.757 +cmd_exit(char *notused)
   1.758 +{
   1.759 +	fg_debug("Good bye.");
   1.760 +	exit(0);
   1.761 +}
   1.762 +
   1.763 +static int cmd_help(char *notused);
   1.764 +
   1.765 +static struct command_struct {
   1.766 +	char *name;
   1.767 +	int (*function)(char *command);
   1.768 +	char *help;
   1.769 +} commands[] = {{"help", cmd_help, "Show this help."},
   1.770 +		{"?", cmd_help, NULL},
   1.771 +		{"load", cmd_load, "Load named file."},
   1.772 +		{"open", cmd_load},
   1.773 +		{"save", cmd_save, "Save to named file."},
   1.774 +		{"ppqn", cmd_ppqn, "Show ppqn (aka division), or set ppqn if used with parameter."},
   1.775 +		{"format", cmd_format, "Show format, or set format if used with parameter."},
   1.776 +		{"tracks", cmd_tracks, "Show number of tracks."},
   1.777 +		{"track", cmd_track, "Show number of currently selected track, or select a track."},
   1.778 +		{"trackadd", cmd_trackadd, "Add a track and select it."},
   1.779 +		{"trackrm", cmd_trackrm, "Remove currently selected track."},
   1.780 +		{"events", cmd_events, "Show events in the currently selected track."},
   1.781 +		{"event", cmd_event, "Show number of currently selected event, or select an event."},
   1.782 +		{"add", cmd_eventadd, "Add an event and select it."},
   1.783 +		{"text", cmd_text, "Add textual event and select it."},
   1.784 +		{"eventadd", cmd_eventadd, NULL},
   1.785 +		{"eot", cmd_eventaddeot, "Add an End Of Track event."},
   1.786 +		{"eventaddeot", cmd_eventaddeot, NULL},
   1.787 +		{"eventrm", cmd_eventrm, NULL},
   1.788 +		{"rm", cmd_eventrm, "Remove currently selected event."},
   1.789 +		{"tempo", cmd_tempo, "Show tempo map."},
   1.790 +		{"length", cmd_length, "Show length of the song."},
   1.791 +		{"version", cmd_version, "Show libsmf version."},
   1.792 +		{"exit", cmd_exit, "Exit to shell."},
   1.793 +		{"quit", cmd_exit, NULL},
   1.794 +		{"bye", cmd_exit, NULL},
   1.795 +		{NULL, NULL, NULL}};
   1.796 +
   1.797 +static int
   1.798 +cmd_help(char *notused)
   1.799 +{
   1.800 +	int i, padding_length;
   1.801 +	char padding[COMMAND_LENGTH + 1];
   1.802 +	struct command_struct *tmp;
   1.803 +
   1.804 +	fg_message("Available commands:");
   1.805 +
   1.806 +	for (tmp = commands; tmp->name != NULL; tmp++) {
   1.807 +		/* Skip commands with no help string. */
   1.808 +		if (tmp->help == NULL)
   1.809 +			continue;
   1.810 +
   1.811 +		padding_length = COMMAND_LENGTH - strlen(tmp->name);
   1.812 +		assert(padding_length >= 0);
   1.813 +		for (i = 0; i < padding_length; i++)
   1.814 +			padding[i] = ' ';
   1.815 +		padding[i] = '\0';
   1.816 +
   1.817 +		fg_message("%s:%s%s", tmp->name, padding, tmp->help);
   1.818 +	}
   1.819 +
   1.820 +	return (0);
   1.821 +}
   1.822 +
   1.823 +/**
   1.824 + * Removes (in place) all whitespace characters before the first
   1.825 + * non-whitespace and all trailing whitespace characters.  Replaces
   1.826 + * more than one consecutive whitespace characters with one.
   1.827 + */
   1.828 +static void
   1.829 +strip_unneeded_whitespace(char *str, int len)
   1.830 +{
   1.831 +	char *src, *dest;
   1.832 +	int skip_white = 1;
   1.833 +
   1.834 +	for (src = str, dest = str; src < dest + len; src++) {
   1.835 +		if (*src == '\n' || *src == '\0') {
   1.836 +			*dest = '\0';
   1.837 +			break;
   1.838 +		}
   1.839 +
   1.840 +		if (isspace(*src)) {
   1.841 +			if (skip_white)
   1.842 +				continue;
   1.843 +
   1.844 +			skip_white = 1;
   1.845 +		} else {
   1.846 +			skip_white = 0;
   1.847 +		}
   1.848 +
   1.849 +		*dest = *src;
   1.850 +		dest++;
   1.851 +	}
   1.852 +
   1.853 +	/* Remove trailing whitespace. */
   1.854 +	len = strlen(dest);
   1.855 +	if (isspace(dest[len - 1]))
   1.856 +		dest[len - 1] = '\0';
   1.857 +}
   1.858 +
   1.859 +static char *
   1.860 +read_command(void)
   1.861 +{
   1.862 +	char *buf;
   1.863 +	int len;
   1.864 +
   1.865 +#ifdef HAVE_LIBREADLINE
   1.866 +	buf = readline("smfsh> ");
   1.867 +#else
   1.868 +	buf = malloc(1024);
   1.869 +	if (buf == NULL) {
   1.870 +		fg_critical("Malloc failed.");
   1.871 +		return (NULL);
   1.872 +	}
   1.873 +
   1.874 +	fprintf(stdout, "smfsh> ");
   1.875 +	fflush(stdout);
   1.876 +
   1.877 +	buf = fgets(buf, 1024, stdin);
   1.878 +#endif
   1.879 +
   1.880 +	if (buf == NULL) {
   1.881 +		fprintf(stdout, "exit\n");
   1.882 +		return (strdup("exit"));
   1.883 +	}
   1.884 +
   1.885 +	strip_unneeded_whitespace(buf, 1024);
   1.886 +
   1.887 +	len = strlen(buf);
   1.888 +
   1.889 +	if (len == 0)
   1.890 +		return (read_command());
   1.891 +
   1.892 +#ifdef HAVE_LIBREADLINE
   1.893 +	add_history(buf);
   1.894 +#endif
   1.895 +
   1.896 +	return (buf);
   1.897 +}
   1.898 +
   1.899 +static int
   1.900 +execute_command(char *line)
   1.901 +{
   1.902 +	char *command, *args;
   1.903 +	struct command_struct *tmp;
   1.904 +
   1.905 +	command = line;
   1.906 +	args = strchr(line, ' ');
   1.907 +	if (args != NULL) {
   1.908 +		*args = '\0';
   1.909 +		args++;
   1.910 +	}
   1.911 +
   1.912 +	for (tmp = commands; tmp->name != NULL; tmp++) {
   1.913 +		if (strcmp(tmp->name, command) == 0)
   1.914 +			return ((tmp->function)(args));
   1.915 +	}
   1.916 +
   1.917 +	fg_warning("No such command: '%s'.  Type 'help' to see available commands.", command);
   1.918 +
   1.919 +	return (-1);
   1.920 +}
   1.921 +
   1.922 +static void
   1.923 +read_and_execute_command(void)
   1.924 +{
   1.925 +	int ret;
   1.926 +	char *command_line, *command, *next_command;
   1.927 +
   1.928 +	command = command_line = read_command();
   1.929 +
   1.930 +	do {
   1.931 +		next_command = strchr(command, ';');
   1.932 +		if (next_command != NULL) {
   1.933 +			*next_command = '\0';
   1.934 +			next_command++;
   1.935 +		}
   1.936 +
   1.937 +		strip_unneeded_whitespace(command, 1024);
   1.938 +		if (strlen(command) > 0) {
   1.939 +			ret = execute_command(command);
   1.940 +			if (ret)
   1.941 +				fg_warning("Command finished with error.");
   1.942 +		}
   1.943 +
   1.944 +		command = next_command;
   1.945 +
   1.946 +	} while (command);
   1.947 +
   1.948 +	free(command_line);
   1.949 +}
   1.950 +
   1.951 +#ifdef HAVE_LIBREADLINE
   1.952 +
   1.953 +static char *
   1.954 +smfsh_command_generator(const char *text, int state)
   1.955 +{
   1.956 +	static struct command_struct *command = commands;
   1.957 +	char *tmp;
   1.958 +
   1.959 +	if (state == 0)
   1.960 +		command = commands;
   1.961 +
   1.962 +	while (command->name != NULL) {
   1.963 +		tmp = command->name;
   1.964 +		command++;
   1.965 +
   1.966 +		if (strncmp(tmp, text, strlen(text)) == 0)
   1.967 +			return (strdup(tmp));
   1.968 +	}
   1.969 +
   1.970 +	return (NULL);
   1.971 +}
   1.972 +
   1.973 +static char **
   1.974 +smfsh_completion(const char *text, int start, int end)
   1.975 +{
   1.976 +	int i;
   1.977 +
   1.978 +	/* Return NULL if "text" is not the first word in the input line. */
   1.979 +	if (start != 0) {
   1.980 +		for (i = 0; i < start; i++) {
   1.981 +			if (!isspace(rl_line_buffer[i]))
   1.982 +				return (NULL);
   1.983 +		}
   1.984 +	}
   1.985 +
   1.986 +	return (rl_completion_matches(text, smfsh_command_generator));
   1.987 +}
   1.988 +
   1.989 +#endif
   1.990 +
   1.991 +static void
   1.992 +usage(void)
   1.993 +{
   1.994 +	fprintf(stderr, "usage: smfsh [-V | file]\n");
   1.995 +
   1.996 +	exit(EX_USAGE);
   1.997 +}
   1.998 +
   1.999 +int
  1.1000 +main(int argc, char *argv[])
  1.1001 +{
  1.1002 +	int ch;
  1.1003 +
  1.1004 +	while ((ch = getopt(argc, argv, "V")) != -1) {
  1.1005 +		switch (ch) {
  1.1006 +		case 'V':
  1.1007 +			cmd_version(NULL);
  1.1008 +			exit(EX_OK);
  1.1009 +
  1.1010 +		case '?':
  1.1011 +		default:
  1.1012 +			usage();
  1.1013 +		}
  1.1014 +	}
  1.1015 +
  1.1016 +	if (argc > 2)
  1.1017 +		usage();
  1.1018 +
  1.1019 +	/*g_log_set_default_handler(log_handler, NULL);*/
  1.1020 +
  1.1021 +	smf = smf_new();
  1.1022 +	if (smf == NULL) {
  1.1023 +		fg_critical("Cannot initialize smf_t.");
  1.1024 +		return (-1);
  1.1025 +	}
  1.1026 +
  1.1027 +	if (argc == 2)
  1.1028 +		cmd_load(argv[1]);
  1.1029 +	else
  1.1030 +		cmd_trackadd(NULL);
  1.1031 +
  1.1032 +#ifdef HAVE_LIBREADLINE
  1.1033 +	rl_readline_name = "smfsh";
  1.1034 +	rl_attempted_completion_function = smfsh_completion;
  1.1035 +#endif
  1.1036 +
  1.1037 +	for (;;)
  1.1038 +		read_and_execute_command();
  1.1039 +
  1.1040 +	return (0);
  1.1041 +}
  1.1042 +