smflite

changeset 1:8e535ca4bb86

added smfsh
author John Tsiombikas <nuclear@member.fsf.org>
date Thu, 26 Jan 2012 15:35:18 +0200
parents 4264abea8b06
children d9e0d0500a78
files smfsh/Makefile smfsh/smfsh.c src/fake_glib.c src/fake_glib.h
diffstat 4 files changed, 1067 insertions(+), 1 deletions(-) [+]
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/smfsh/Makefile	Thu Jan 26 15:35:18 2012 +0200
     1.3 @@ -0,0 +1,12 @@
     1.4 +obj = smfsh.o
     1.5 +bin = smfsh
     1.6 +
     1.7 +CFLAGS = -pedantic -Wall -g -I../src
     1.8 +LDFLAGS = -L.. -lsmflite -lreadline -lm
     1.9 +
    1.10 +$(bin): $(obj)
    1.11 +	$(CC) -o $@ $(obj) $(LDFLAGS)
    1.12 +
    1.13 +.PHONY: clean
    1.14 +clean:
    1.15 +	rm -f $(obj) $(bin)
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/smfsh/smfsh.c	Thu Jan 26 15:35:18 2012 +0200
     2.3 @@ -0,0 +1,1039 @@
     2.4 +/*-
     2.5 + * Copyright (c) 2007, 2008 Edward Tomasz NapieraƂa <trasz@FreeBSD.org>
     2.6 + * All rights reserved.
     2.7 + *
     2.8 + * Redistribution and use in source and binary forms, with or without
     2.9 + * modification, are permitted provided that the following conditions
    2.10 + * are met:
    2.11 + * 1. Redistributions of source code must retain the above copyright
    2.12 + *    notice, this list of conditions and the following disclaimer.
    2.13 + * 2. Redistributions in binary form must reproduce the above copyright
    2.14 + *    notice, this list of conditions and the following disclaimer in the
    2.15 + *    documentation and/or other materials provided with the distribution.
    2.16 + *
    2.17 + * ALTHOUGH THIS SOFTWARE IS MADE OF WIN AND SCIENCE, IT IS PROVIDED BY THE
    2.18 + * AUTHOR AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
    2.19 + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
    2.20 + * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
    2.21 + * THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
    2.22 + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
    2.23 + * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
    2.24 + * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
    2.25 + * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
    2.26 + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
    2.27 + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    2.28 + *
    2.29 + */
    2.30 +
    2.31 +/**
    2.32 + * \file
    2.33 + *
    2.34 + * "SMF shell", command line utility.
    2.35 + */
    2.36 +
    2.37 +#include <stdio.h>
    2.38 +#include <stdlib.h>
    2.39 +#include <unistd.h>
    2.40 +#ifdef __MINGW32__
    2.41 +#define EX_OK 0
    2.42 +#define EX_USAGE 64
    2.43 +#else /* ! __MINGW32__ */
    2.44 +#include <sysexits.h>
    2.45 +#endif /* ! __MINGW32__ */
    2.46 +#include <string.h>
    2.47 +#include <ctype.h>
    2.48 +#include <assert.h>
    2.49 +#include "smf.h"
    2.50 +#include "fake_glib.h"
    2.51 +
    2.52 +#ifdef HAVE_LIBREADLINE
    2.53 +#include <readline/readline.h>
    2.54 +#include <readline/history.h>
    2.55 +#endif
    2.56 +
    2.57 +smf_track_t *selected_track = NULL;
    2.58 +smf_event_t *selected_event = NULL;
    2.59 +smf_t *smf = NULL;
    2.60 +char *last_file_name = NULL;
    2.61 +
    2.62 +#define COMMAND_LENGTH 10
    2.63 +
    2.64 +/*static void
    2.65 +log_handler(const gchar *log_domain, GLogLevelFlags log_level, const gchar *message, gpointer notused)
    2.66 +{
    2.67 +	if (strcmp(log_domain, "smfsh") == 0)
    2.68 +		fprintf(stderr, "%s\n", message);
    2.69 +	else
    2.70 +		fprintf(stderr, "%s: %s\n", log_domain, message);
    2.71 +}*/
    2.72 +
    2.73 +static int cmd_track(char *arg);
    2.74 +
    2.75 +static int
    2.76 +cmd_load(char *file_name)
    2.77 +{
    2.78 +	char *decoded;
    2.79 +
    2.80 +	if (file_name == NULL) {
    2.81 +		if (last_file_name == NULL) {
    2.82 +			fg_critical("Please specify file name.");
    2.83 +			return (-1);
    2.84 +		}
    2.85 +
    2.86 +		file_name = strdup(last_file_name);
    2.87 +	} else {
    2.88 +		file_name = strdup(file_name);
    2.89 +	}
    2.90 +
    2.91 +	selected_track = NULL;
    2.92 +	selected_event = NULL;
    2.93 +
    2.94 +	if (smf != NULL) {
    2.95 +		smf_delete(smf);
    2.96 +		smf = NULL;
    2.97 +	}
    2.98 +
    2.99 +	if (last_file_name != NULL)
   2.100 +		free(last_file_name);
   2.101 +	last_file_name = strdup(file_name);
   2.102 +
   2.103 +	smf = smf_load(file_name);
   2.104 +	if (smf == NULL) {
   2.105 +		fg_critical("Couldn't load '%s'.", file_name);
   2.106 +
   2.107 +		smf = smf_new();
   2.108 +		if (smf == NULL) {
   2.109 +			fg_critical("Cannot initialize smf_t.");
   2.110 +			return (-1);
   2.111 +		}
   2.112 +
   2.113 +		return (-2);
   2.114 +	}
   2.115 +
   2.116 +	fg_message("File '%s' loaded.", file_name);
   2.117 +	decoded = smf_decode(smf);
   2.118 +	fg_message("%s.", decoded);
   2.119 +	free(decoded);
   2.120 +
   2.121 +	cmd_track("1");
   2.122 +
   2.123 +	free(file_name);
   2.124 +
   2.125 +	return (0);
   2.126 +}
   2.127 +
   2.128 +static int
   2.129 +cmd_save(char *file_name)
   2.130 +{
   2.131 +	int ret;
   2.132 +
   2.133 +	if (file_name == NULL) {
   2.134 +		if (last_file_name == NULL) {
   2.135 +			fg_critical("Please specify file name.");
   2.136 +			return (-1);
   2.137 +		}
   2.138 +
   2.139 +		file_name = strdup(last_file_name);
   2.140 +	} else {
   2.141 +		file_name = strdup(file_name);
   2.142 +	}
   2.143 +
   2.144 +	if (last_file_name != NULL)
   2.145 +		free(last_file_name);
   2.146 +	last_file_name = strdup(file_name);
   2.147 +
   2.148 +	ret = smf_save(smf, file_name);
   2.149 +	if (ret) {
   2.150 +		fg_critical("Couldn't save '%s'", file_name);
   2.151 +		return (-1);
   2.152 +	}
   2.153 +
   2.154 +	fg_message("File '%s' saved.", file_name);
   2.155 +
   2.156 +	free(file_name);
   2.157 +
   2.158 +	return (0);
   2.159 +}
   2.160 +
   2.161 +static int
   2.162 +cmd_ppqn(char *new_ppqn)
   2.163 +{
   2.164 +	int tmp;
   2.165 +	char *end;
   2.166 +
   2.167 +	if (new_ppqn == NULL) {
   2.168 +		fg_message("Pulses Per Quarter Note (aka Division) is %d.", smf->ppqn);
   2.169 +	} else {
   2.170 +		tmp = strtol(new_ppqn, &end, 10);
   2.171 +		if (end - new_ppqn != strlen(new_ppqn)) {
   2.172 +			fg_critical("Invalid PPQN, garbage characters after the number.");
   2.173 +			return (-1);
   2.174 +		}
   2.175 +
   2.176 +		if (tmp <= 0) {
   2.177 +			fg_critical("Invalid PPQN, valid values are greater than zero.");
   2.178 +			return (-2);
   2.179 +		}
   2.180 +
   2.181 +		if (smf_set_ppqn(smf, tmp)) {
   2.182 +			fg_message("smf_set_ppqn failed.");
   2.183 +			return (-3);
   2.184 +		}
   2.185 +
   2.186 +		fg_message("Pulses Per Quarter Note changed to %d.", smf->ppqn);
   2.187 +	}
   2.188 +	
   2.189 +	return (0);
   2.190 +}
   2.191 +
   2.192 +static int
   2.193 +cmd_format(char *new_format)
   2.194 +{
   2.195 +	int tmp;
   2.196 +	char *end;
   2.197 +
   2.198 +	if (new_format == NULL) {
   2.199 +		fg_message("Format is %d.", smf->format);
   2.200 +	} else {
   2.201 +		tmp = strtol(new_format, &end, 10);
   2.202 +		if (end - new_format != strlen(new_format)) {
   2.203 +			fg_critical("Invalid format value, garbage characters after the number.");
   2.204 +			return (-1);
   2.205 +		}
   2.206 +
   2.207 +		if (tmp < 0 || tmp > 2) {
   2.208 +			fg_critical("Invalid format value, valid values are in range 0 - 2, inclusive.");
   2.209 +			return (-2);
   2.210 +		}
   2.211 +
   2.212 +		if (smf_set_format(smf, tmp)) {
   2.213 +			fg_critical("smf_set_format failed.");
   2.214 +			return (-3);
   2.215 +		}
   2.216 +
   2.217 +		fg_message("Forma changed to %d.", smf->format);
   2.218 +	}
   2.219 +	
   2.220 +	return (0);
   2.221 +}
   2.222 +
   2.223 +static int
   2.224 +cmd_tracks(char *notused)
   2.225 +{
   2.226 +	if (smf->number_of_tracks > 0)
   2.227 +		fg_message("There are %d tracks, numbered from 1 to %d.", smf->number_of_tracks, smf->number_of_tracks);
   2.228 +	else
   2.229 +		fg_message("There are no tracks.");
   2.230 +
   2.231 +	return (0);
   2.232 +}
   2.233 +
   2.234 +static int
   2.235 +parse_track_number(const char *arg)
   2.236 +{
   2.237 +	int num;
   2.238 +	char *end;
   2.239 +
   2.240 +	if (arg == NULL) {
   2.241 +		if (selected_track == NULL) {
   2.242 +			fg_message("No track currently selected and no track number given.");
   2.243 +			return (-1);
   2.244 +		} else {
   2.245 +			return (selected_track->track_number);
   2.246 +		}
   2.247 +	}
   2.248 +
   2.249 +	num = strtol(arg, &end, 10);
   2.250 +	if (end - arg != strlen(arg)) {
   2.251 +		fg_critical("Invalid track number, garbage characters after the number.");
   2.252 +		return (-1);
   2.253 +	}
   2.254 +
   2.255 +	if (num < 1 || num > smf->number_of_tracks) {
   2.256 +		if (smf->number_of_tracks > 0) {
   2.257 +			fg_critical("Invalid track number specified; valid choices are 1 - %d.", smf->number_of_tracks);
   2.258 +		} else {
   2.259 +			fg_critical("There are no tracks.");
   2.260 +		}
   2.261 +
   2.262 +		return (-1);
   2.263 +	}
   2.264 +
   2.265 +	return (num);
   2.266 +}
   2.267 +
   2.268 +static int
   2.269 +cmd_track(char *arg)
   2.270 +{
   2.271 +	int num;
   2.272 +
   2.273 +	if (arg == NULL) {
   2.274 +		if (selected_track == NULL)
   2.275 +			fg_message("No track currently selected.");
   2.276 +		else
   2.277 +			fg_message("Currently selected is track number %d, containing %d events.",
   2.278 +				selected_track->track_number, selected_track->number_of_events);
   2.279 +	} else {
   2.280 +		if (smf->number_of_tracks == 0) {
   2.281 +			fg_message("There are no tracks.");
   2.282 +			return (-1);
   2.283 +		}
   2.284 +
   2.285 +		num = parse_track_number(arg);
   2.286 +		if (num < 0)
   2.287 +			return (-1);
   2.288 +
   2.289 +		selected_track = smf_get_track_by_number(smf, num);
   2.290 +		if (selected_track == NULL) {
   2.291 +			fg_critical("smf_get_track_by_number() failed, track not selected.");
   2.292 +			return (-3);
   2.293 +		}
   2.294 +
   2.295 +		selected_event = NULL;
   2.296 +
   2.297 +		fg_message("Track number %d selected; it contains %d events.",
   2.298 +				selected_track->track_number, selected_track->number_of_events);
   2.299 +	}
   2.300 +
   2.301 +	return (0);
   2.302 +}
   2.303 +
   2.304 +static int
   2.305 +cmd_trackadd(char *notused)
   2.306 +{
   2.307 +	selected_track = smf_track_new();
   2.308 +	if (selected_track == NULL) {
   2.309 +		fg_critical("smf_track_new() failed, track not created.");
   2.310 +		return (-1);
   2.311 +	}
   2.312 +
   2.313 +	smf_add_track(smf, selected_track);
   2.314 +
   2.315 +	selected_event = NULL;
   2.316 +
   2.317 +	fg_message("Created new track; track number %d selected.", selected_track->track_number);
   2.318 +
   2.319 +	return (0);
   2.320 +}
   2.321 +
   2.322 +static int
   2.323 +cmd_trackrm(char *arg)
   2.324 +{
   2.325 +	int num = parse_track_number(arg);
   2.326 +
   2.327 +	if (num < 0)
   2.328 +		return (-1);
   2.329 +
   2.330 +	if (selected_track != NULL && num == selected_track->track_number) {
   2.331 +		selected_track = NULL;
   2.332 +		selected_event = NULL;
   2.333 +	}
   2.334 +
   2.335 +	smf_track_delete(smf_get_track_by_number(smf, num));
   2.336 +
   2.337 +	fg_message("Track %d removed.", num);
   2.338 +
   2.339 +	return (0);
   2.340 +}
   2.341 +
   2.342 +#define BUFFER_SIZE 1024
   2.343 +
   2.344 +static int
   2.345 +show_event(smf_event_t *event)
   2.346 +{
   2.347 +	int off = 0, i;
   2.348 +	char *decoded, *type;
   2.349 +
   2.350 +	if (smf_event_is_metadata(event))
   2.351 +		type = "Metadata";
   2.352 +	else
   2.353 +		type = "Event";
   2.354 +	
   2.355 +	decoded = smf_event_decode(event);
   2.356 +
   2.357 +	if (decoded == NULL) {
   2.358 +		decoded = malloc(BUFFER_SIZE);
   2.359 +		if (decoded == NULL) {
   2.360 +			fg_critical("show_event: malloc failed.");
   2.361 +			return (-1);
   2.362 +		}
   2.363 +
   2.364 +		off += snprintf(decoded + off, BUFFER_SIZE - off, "Unknown event:");
   2.365 +
   2.366 +		for (i = 0; i < event->midi_buffer_length && i < 5; i++)
   2.367 +			off += snprintf(decoded + off, BUFFER_SIZE - off, " 0x%x", event->midi_buffer[i]);
   2.368 +	}
   2.369 +
   2.370 +	fg_message("%d: %s: %s, %f seconds, %d pulses, %d delta pulses", event->event_number, type, decoded,
   2.371 +	    event->time_seconds, event->time_pulses, event->delta_time_pulses);
   2.372 +
   2.373 +	free(decoded);
   2.374 +
   2.375 +	return (0);
   2.376 +}
   2.377 +
   2.378 +static int
   2.379 +cmd_events(char *notused)
   2.380 +{
   2.381 +	smf_event_t *event;
   2.382 +
   2.383 +	if (selected_track == NULL) {
   2.384 +		fg_critical("No track selected - please use 'track <number>' command first.");
   2.385 +		return (-1);
   2.386 +	}
   2.387 +
   2.388 +	if (selected_track->number_of_events == 0) {
   2.389 +		fg_message("Selected track is empty.");
   2.390 +		return (0);
   2.391 +	}
   2.392 +
   2.393 +	fg_message("List of events in track %d follows:", selected_track->track_number);
   2.394 +
   2.395 +	smf_rewind(smf);
   2.396 +
   2.397 +	while ((event = smf_track_get_next_event(selected_track)) != NULL)
   2.398 +		show_event(event);
   2.399 +
   2.400 +	smf_rewind(smf);
   2.401 +
   2.402 +	return (0);
   2.403 +}
   2.404 +
   2.405 +static int
   2.406 +parse_event_number(const char *arg)
   2.407 +{
   2.408 +	int num;
   2.409 +	char *end;
   2.410 +
   2.411 +	if (selected_track == NULL) {
   2.412 +		fg_critical("You need to select track first (using 'track <number>').");
   2.413 +		return (-1);
   2.414 +	}
   2.415 +
   2.416 +	if (arg == NULL) {
   2.417 +		if (selected_event == NULL) {
   2.418 +			fg_message("No event currently selected and no event number given.");
   2.419 +			return (-1);
   2.420 +		} else {
   2.421 +			return (selected_event->event_number);
   2.422 +		}
   2.423 +	}
   2.424 +
   2.425 +	num = strtol(arg, &end, 10);
   2.426 +	if (end - arg != strlen(arg)) {
   2.427 +		fg_critical("Invalid event number, garbage characters after the number.");
   2.428 +		return (-1);
   2.429 +	}
   2.430 +
   2.431 +	if (num < 1 || num > selected_track->number_of_events) {
   2.432 +		if (selected_track->number_of_events > 0)
   2.433 +			fg_critical("Invalid event number specified; valid choices are 1 - %d.", selected_track->number_of_events);
   2.434 +		else
   2.435 +			fg_critical("There are no events in currently selected track.");
   2.436 +
   2.437 +		return (-1);
   2.438 +	}
   2.439 +
   2.440 +	return (num);
   2.441 +}
   2.442 +
   2.443 +static int
   2.444 +cmd_event(char *arg)
   2.445 +{
   2.446 +	int num;
   2.447 +
   2.448 +	if (arg == NULL) {
   2.449 +		if (selected_event == NULL) {
   2.450 +			fg_message("No event currently selected.");
   2.451 +		} else {
   2.452 +			fg_message("Currently selected is event %d, track %d.", selected_event->event_number, selected_track->track_number);
   2.453 +			show_event(selected_event);
   2.454 +		}
   2.455 +	} else {
   2.456 +		num = parse_event_number(arg);
   2.457 +		if (num < 0)
   2.458 +			return (-1);
   2.459 +
   2.460 +		selected_event = smf_track_get_event_by_number(selected_track, num);
   2.461 +		if (selected_event == NULL) {
   2.462 +			fg_critical("smf_get_event_by_number() failed, event not selected.");
   2.463 +			return (-2);
   2.464 +		}
   2.465 +
   2.466 +		fg_message("Event number %d selected.", selected_event->event_number);
   2.467 +		show_event(selected_event);
   2.468 +	}
   2.469 +
   2.470 +	return (0);
   2.471 +}
   2.472 +
   2.473 +static int
   2.474 +decode_hex(char *str, unsigned char **buffer, int *length)
   2.475 +{
   2.476 +	int i, value, midi_buffer_length;
   2.477 +	char buf[3];
   2.478 +	unsigned char *midi_buffer = NULL;
   2.479 +	char *end = NULL;
   2.480 +
   2.481 +	if ((strlen(str) % 2) != 0) {
   2.482 +		fg_critical("Hex value should have even number of characters, you know.");
   2.483 +		goto error;
   2.484 +	}
   2.485 +
   2.486 +	midi_buffer_length = strlen(str) / 2;
   2.487 +	midi_buffer = malloc(midi_buffer_length);
   2.488 +	if (midi_buffer == NULL) {
   2.489 +		fg_critical("malloc() failed.");
   2.490 +		goto error;
   2.491 +	}
   2.492 +
   2.493 +	for (i = 0; i < midi_buffer_length; i++) {
   2.494 +		buf[0] = str[i * 2];
   2.495 +		buf[1] = str[i * 2 + 1];
   2.496 +		buf[2] = '\0';
   2.497 +		value = strtoll(buf, &end, 16);
   2.498 +
   2.499 +		if (end - buf != 2) {
   2.500 +			fg_critical("Garbage characters detected after hex.");
   2.501 +			goto error;
   2.502 +		}
   2.503 +
   2.504 +		midi_buffer[i] = value;
   2.505 +	}
   2.506 +
   2.507 +	*buffer = midi_buffer;
   2.508 +	*length = midi_buffer_length;
   2.509 +
   2.510 +	return (0);
   2.511 +
   2.512 +error:
   2.513 +	if (midi_buffer != NULL)
   2.514 +		free(midi_buffer);
   2.515 +
   2.516 +	return (-1);
   2.517 +}
   2.518 +
   2.519 +static void
   2.520 +eventadd_usage(void)
   2.521 +{
   2.522 +	fg_message("Usage: add <time-in-seconds> <midi-in-hex> - for example, 'add 1 903C7F' will add");
   2.523 +	fg_message("Note On event, note C4, velocity 127, channel 1, one second from the start of song, channel 1.");
   2.524 +}
   2.525 +
   2.526 +static int
   2.527 +cmd_eventadd(char *str)
   2.528 +{
   2.529 +	int midi_buffer_length;
   2.530 +	double seconds;
   2.531 +	unsigned char *midi_buffer;
   2.532 +	char *time, *endtime;
   2.533 +
   2.534 +	if (selected_track == NULL) {
   2.535 +		fg_critical("Please select a track first, using 'track <number>' command.");
   2.536 +		return (-1);
   2.537 +	}
   2.538 +
   2.539 +	if (str == NULL) {
   2.540 +		eventadd_usage();
   2.541 +		return (-2);
   2.542 +	}
   2.543 +
   2.544 +	/* Extract the time.  Don't use strsep(3), it doesn't work on SunOS. */
   2.545 +	time = str;
   2.546 +	str = strchr(str, ' ');
   2.547 +	if (str != NULL) {
   2.548 +		*str = '\0';
   2.549 +		str++;
   2.550 +	}
   2.551 +
   2.552 +	seconds = strtod(time, &endtime);
   2.553 +	if (endtime - time != strlen(time)) {
   2.554 +		fg_critical("Time is supposed to be a number, without trailing characters.");
   2.555 +		return (-3);
   2.556 +	}
   2.557 +
   2.558 +	/* Called with one parameter? */
   2.559 +	if (str == NULL) {
   2.560 +		eventadd_usage();
   2.561 +		return (-4);
   2.562 +	}
   2.563 +
   2.564 +	if (decode_hex(str, &midi_buffer, &midi_buffer_length)) {
   2.565 +		eventadd_usage();
   2.566 +		return (-5);
   2.567 +	}
   2.568 +
   2.569 +	selected_event = smf_event_new();
   2.570 +	if (selected_event == NULL) {
   2.571 +		fg_critical("smf_event_new() failed, event not created.");
   2.572 +		return (-6);
   2.573 +	}
   2.574 +
   2.575 +	selected_event->midi_buffer = midi_buffer;
   2.576 +	selected_event->midi_buffer_length = midi_buffer_length;
   2.577 +
   2.578 +	if (smf_event_is_valid(selected_event) == 0) {
   2.579 +		fg_critical("Event is invalid from the MIDI specification point of view, not created.");
   2.580 +		smf_event_delete(selected_event);
   2.581 +		selected_event = NULL;
   2.582 +		return (-7);
   2.583 +	}
   2.584 +
   2.585 +	smf_track_add_event_seconds(selected_track, selected_event, seconds);
   2.586 +
   2.587 +	fg_message("Event created.");
   2.588 +
   2.589 +	return (0);
   2.590 +}
   2.591 +
   2.592 +static int
   2.593 +cmd_text(char *str)
   2.594 +{
   2.595 +	double seconds, type;
   2.596 +	char *time, *typestr, *end;
   2.597 +
   2.598 +	if (selected_track == NULL) {
   2.599 +		fg_critical("Please select a track first, using 'track <number>' command.");
   2.600 +		return (-1);
   2.601 +	}
   2.602 +
   2.603 +	if (str == NULL) {
   2.604 +		fg_critical("Usage: text <time-in-seconds> <event-type> <text-itself>");
   2.605 +		return (-2);
   2.606 +	}
   2.607 +
   2.608 +	/* Extract the time.  Don't use strsep(3), it doesn't work on SunOS. */
   2.609 +	time = str;
   2.610 +	str = strchr(str, ' ');
   2.611 +	if (str != NULL) {
   2.612 +		*str = '\0';
   2.613 +		str++;
   2.614 +	}
   2.615 +
   2.616 +	seconds = strtod(time, &end);
   2.617 +	if (end - time != strlen(time)) {
   2.618 +		fg_critical("Time is supposed to be a number, without trailing characters.");
   2.619 +		return (-3);
   2.620 +	}
   2.621 +
   2.622 +	/* Called with one parameter? */
   2.623 +	if (str == NULL) {
   2.624 +		fg_critical("Usage: text <time-in-seconds> <event-type> <text-itself>");
   2.625 +		return (-4);
   2.626 +	}
   2.627 +
   2.628 +	/* Extract the event type. */
   2.629 +	typestr = str;
   2.630 +	str = strchr(str, ' ');
   2.631 +	if (str != NULL) {
   2.632 +		*str = '\0';
   2.633 +		str++;
   2.634 +	}
   2.635 +
   2.636 +	type = strtod(typestr, &end);
   2.637 +	if (end - typestr != strlen(typestr)) {
   2.638 +		fg_critical("Type is supposed to be a number, without trailing characters.");
   2.639 +		return (-4);
   2.640 +	}
   2.641 +
   2.642 +	if (type < 1 || type > 9) {
   2.643 +		fg_critical("Valid values for type are 1 - 9, inclusive.");
   2.644 +		return (-5);
   2.645 +	}
   2.646 +
   2.647 +	/* Called with one parameter? */
   2.648 +	if (str == NULL) {
   2.649 +		fg_critical("Usage: text <time-in-seconds> <event-type> <text-itself>");
   2.650 +		return (-4);
   2.651 +	}
   2.652 +
   2.653 +	selected_event = smf_event_new_textual(type, str);
   2.654 +	if (selected_event == NULL) {
   2.655 +		fg_critical("smf_event_new_textual() failed, event not created.");
   2.656 +		return (-6);
   2.657 +	}
   2.658 +
   2.659 +	assert(smf_event_is_valid(selected_event));
   2.660 +
   2.661 +	smf_track_add_event_seconds(selected_track, selected_event, seconds);
   2.662 +
   2.663 +	fg_message("Event created.");
   2.664 +
   2.665 +	return (0);
   2.666 +}
   2.667 +
   2.668 +
   2.669 +static int
   2.670 +cmd_eventaddeot(char *time)
   2.671 +{
   2.672 +	double seconds;
   2.673 +	char *end;
   2.674 +
   2.675 +	if (selected_track == NULL) {
   2.676 +		fg_critical("Please select a track first, using 'track <number>' command.");
   2.677 +		return (-1);
   2.678 +	}
   2.679 +
   2.680 +	if (time == NULL) {
   2.681 +		fg_critical("Please specify the time, in seconds.");
   2.682 +		return (-2);
   2.683 +	}
   2.684 +
   2.685 +	seconds = strtod(time, &end);
   2.686 +	if (end - time != strlen(time)) {
   2.687 +		fg_critical("Time is supposed to be a number, without trailing characters.");
   2.688 +		return (-3);
   2.689 +	}
   2.690 +
   2.691 +	if (smf_track_add_eot_seconds(selected_track, seconds)) {
   2.692 +		fg_critical("smf_track_add_eot() failed.");
   2.693 +		return (-4);
   2.694 +	}
   2.695 +
   2.696 +	fg_message("Event created.");
   2.697 +
   2.698 +	return (0);
   2.699 +}
   2.700 +
   2.701 +static int
   2.702 +cmd_eventrm(char *number)
   2.703 +{
   2.704 +	int num = parse_event_number(number);
   2.705 +
   2.706 +	if (num < 0)
   2.707 +		return (-1);
   2.708 +
   2.709 +	if (selected_event != NULL && num == selected_event->event_number)
   2.710 +		selected_event = NULL;
   2.711 +
   2.712 +	smf_event_delete(smf_track_get_event_by_number(selected_track, num));
   2.713 +
   2.714 +	fg_message("Event #%d removed.", num);
   2.715 +
   2.716 +	return (0);
   2.717 +}
   2.718 +
   2.719 +static int
   2.720 +cmd_tempo(char *notused)
   2.721 +{
   2.722 +	int i;
   2.723 +	smf_tempo_t *tempo;
   2.724 +
   2.725 +	for (i = 0;; i++) {
   2.726 +		tempo = smf_get_tempo_by_number(smf, i);
   2.727 +		if (tempo == NULL)
   2.728 +			break;
   2.729 +
   2.730 +		fg_message("Tempo #%d: Starts at %d pulses, %f seconds, setting %d microseconds per quarter note, %.2f BPM.",
   2.731 +		    i, tempo->time_pulses, tempo->time_seconds, tempo->microseconds_per_quarter_note,
   2.732 +		    60000000.0 / (double)tempo->microseconds_per_quarter_note);
   2.733 +		fg_message("Time signature: %d/%d, %d clocks per click, %d 32nd notes per quarter note.",
   2.734 +		    tempo->numerator, tempo->denominator, tempo->clocks_per_click, tempo->notes_per_note);
   2.735 +	}
   2.736 +
   2.737 +	return (0);
   2.738 +}
   2.739 +
   2.740 +static int
   2.741 +cmd_length(char *notused)
   2.742 +{
   2.743 +	fg_message("Length: %d pulses, %f seconds.", smf_get_length_pulses(smf), smf_get_length_seconds(smf));
   2.744 +
   2.745 +	return (0);
   2.746 +}
   2.747 +
   2.748 +static int
   2.749 +cmd_version(char *notused)
   2.750 +{
   2.751 +	fg_message("libsmf version %s.", smf_get_version());
   2.752 +
   2.753 +	return (0);
   2.754 +}
   2.755 +
   2.756 +static int
   2.757 +cmd_exit(char *notused)
   2.758 +{
   2.759 +	fg_debug("Good bye.");
   2.760 +	exit(0);
   2.761 +}
   2.762 +
   2.763 +static int cmd_help(char *notused);
   2.764 +
   2.765 +static struct command_struct {
   2.766 +	char *name;
   2.767 +	int (*function)(char *command);
   2.768 +	char *help;
   2.769 +} commands[] = {{"help", cmd_help, "Show this help."},
   2.770 +		{"?", cmd_help, NULL},
   2.771 +		{"load", cmd_load, "Load named file."},
   2.772 +		{"open", cmd_load},
   2.773 +		{"save", cmd_save, "Save to named file."},
   2.774 +		{"ppqn", cmd_ppqn, "Show ppqn (aka division), or set ppqn if used with parameter."},
   2.775 +		{"format", cmd_format, "Show format, or set format if used with parameter."},
   2.776 +		{"tracks", cmd_tracks, "Show number of tracks."},
   2.777 +		{"track", cmd_track, "Show number of currently selected track, or select a track."},
   2.778 +		{"trackadd", cmd_trackadd, "Add a track and select it."},
   2.779 +		{"trackrm", cmd_trackrm, "Remove currently selected track."},
   2.780 +		{"events", cmd_events, "Show events in the currently selected track."},
   2.781 +		{"event", cmd_event, "Show number of currently selected event, or select an event."},
   2.782 +		{"add", cmd_eventadd, "Add an event and select it."},
   2.783 +		{"text", cmd_text, "Add textual event and select it."},
   2.784 +		{"eventadd", cmd_eventadd, NULL},
   2.785 +		{"eot", cmd_eventaddeot, "Add an End Of Track event."},
   2.786 +		{"eventaddeot", cmd_eventaddeot, NULL},
   2.787 +		{"eventrm", cmd_eventrm, NULL},
   2.788 +		{"rm", cmd_eventrm, "Remove currently selected event."},
   2.789 +		{"tempo", cmd_tempo, "Show tempo map."},
   2.790 +		{"length", cmd_length, "Show length of the song."},
   2.791 +		{"version", cmd_version, "Show libsmf version."},
   2.792 +		{"exit", cmd_exit, "Exit to shell."},
   2.793 +		{"quit", cmd_exit, NULL},
   2.794 +		{"bye", cmd_exit, NULL},
   2.795 +		{NULL, NULL, NULL}};
   2.796 +
   2.797 +static int
   2.798 +cmd_help(char *notused)
   2.799 +{
   2.800 +	int i, padding_length;
   2.801 +	char padding[COMMAND_LENGTH + 1];
   2.802 +	struct command_struct *tmp;
   2.803 +
   2.804 +	fg_message("Available commands:");
   2.805 +
   2.806 +	for (tmp = commands; tmp->name != NULL; tmp++) {
   2.807 +		/* Skip commands with no help string. */
   2.808 +		if (tmp->help == NULL)
   2.809 +			continue;
   2.810 +
   2.811 +		padding_length = COMMAND_LENGTH - strlen(tmp->name);
   2.812 +		assert(padding_length >= 0);
   2.813 +		for (i = 0; i < padding_length; i++)
   2.814 +			padding[i] = ' ';
   2.815 +		padding[i] = '\0';
   2.816 +
   2.817 +		fg_message("%s:%s%s", tmp->name, padding, tmp->help);
   2.818 +	}
   2.819 +
   2.820 +	return (0);
   2.821 +}
   2.822 +
   2.823 +/**
   2.824 + * Removes (in place) all whitespace characters before the first
   2.825 + * non-whitespace and all trailing whitespace characters.  Replaces
   2.826 + * more than one consecutive whitespace characters with one.
   2.827 + */
   2.828 +static void
   2.829 +strip_unneeded_whitespace(char *str, int len)
   2.830 +{
   2.831 +	char *src, *dest;
   2.832 +	int skip_white = 1;
   2.833 +
   2.834 +	for (src = str, dest = str; src < dest + len; src++) {
   2.835 +		if (*src == '\n' || *src == '\0') {
   2.836 +			*dest = '\0';
   2.837 +			break;
   2.838 +		}
   2.839 +
   2.840 +		if (isspace(*src)) {
   2.841 +			if (skip_white)
   2.842 +				continue;
   2.843 +
   2.844 +			skip_white = 1;
   2.845 +		} else {
   2.846 +			skip_white = 0;
   2.847 +		}
   2.848 +
   2.849 +		*dest = *src;
   2.850 +		dest++;
   2.851 +	}
   2.852 +
   2.853 +	/* Remove trailing whitespace. */
   2.854 +	len = strlen(dest);
   2.855 +	if (isspace(dest[len - 1]))
   2.856 +		dest[len - 1] = '\0';
   2.857 +}
   2.858 +
   2.859 +static char *
   2.860 +read_command(void)
   2.861 +{
   2.862 +	char *buf;
   2.863 +	int len;
   2.864 +
   2.865 +#ifdef HAVE_LIBREADLINE
   2.866 +	buf = readline("smfsh> ");
   2.867 +#else
   2.868 +	buf = malloc(1024);
   2.869 +	if (buf == NULL) {
   2.870 +		fg_critical("Malloc failed.");
   2.871 +		return (NULL);
   2.872 +	}
   2.873 +
   2.874 +	fprintf(stdout, "smfsh> ");
   2.875 +	fflush(stdout);
   2.876 +
   2.877 +	buf = fgets(buf, 1024, stdin);
   2.878 +#endif
   2.879 +
   2.880 +	if (buf == NULL) {
   2.881 +		fprintf(stdout, "exit\n");
   2.882 +		return (strdup("exit"));
   2.883 +	}
   2.884 +
   2.885 +	strip_unneeded_whitespace(buf, 1024);
   2.886 +
   2.887 +	len = strlen(buf);
   2.888 +
   2.889 +	if (len == 0)
   2.890 +		return (read_command());
   2.891 +
   2.892 +#ifdef HAVE_LIBREADLINE
   2.893 +	add_history(buf);
   2.894 +#endif
   2.895 +
   2.896 +	return (buf);
   2.897 +}
   2.898 +
   2.899 +static int
   2.900 +execute_command(char *line)
   2.901 +{
   2.902 +	char *command, *args;
   2.903 +	struct command_struct *tmp;
   2.904 +
   2.905 +	command = line;
   2.906 +	args = strchr(line, ' ');
   2.907 +	if (args != NULL) {
   2.908 +		*args = '\0';
   2.909 +		args++;
   2.910 +	}
   2.911 +
   2.912 +	for (tmp = commands; tmp->name != NULL; tmp++) {
   2.913 +		if (strcmp(tmp->name, command) == 0)
   2.914 +			return ((tmp->function)(args));
   2.915 +	}
   2.916 +
   2.917 +	fg_warning("No such command: '%s'.  Type 'help' to see available commands.", command);
   2.918 +
   2.919 +	return (-1);
   2.920 +}
   2.921 +
   2.922 +static void
   2.923 +read_and_execute_command(void)
   2.924 +{
   2.925 +	int ret;
   2.926 +	char *command_line, *command, *next_command;
   2.927 +
   2.928 +	command = command_line = read_command();
   2.929 +
   2.930 +	do {
   2.931 +		next_command = strchr(command, ';');
   2.932 +		if (next_command != NULL) {
   2.933 +			*next_command = '\0';
   2.934 +			next_command++;
   2.935 +		}
   2.936 +
   2.937 +		strip_unneeded_whitespace(command, 1024);
   2.938 +		if (strlen(command) > 0) {
   2.939 +			ret = execute_command(command);
   2.940 +			if (ret)
   2.941 +				fg_warning("Command finished with error.");
   2.942 +		}
   2.943 +
   2.944 +		command = next_command;
   2.945 +
   2.946 +	} while (command);
   2.947 +
   2.948 +	free(command_line);
   2.949 +}
   2.950 +
   2.951 +#ifdef HAVE_LIBREADLINE
   2.952 +
   2.953 +static char *
   2.954 +smfsh_command_generator(const char *text, int state)
   2.955 +{
   2.956 +	static struct command_struct *command = commands;
   2.957 +	char *tmp;
   2.958 +
   2.959 +	if (state == 0)
   2.960 +		command = commands;
   2.961 +
   2.962 +	while (command->name != NULL) {
   2.963 +		tmp = command->name;
   2.964 +		command++;
   2.965 +
   2.966 +		if (strncmp(tmp, text, strlen(text)) == 0)
   2.967 +			return (strdup(tmp));
   2.968 +	}
   2.969 +
   2.970 +	return (NULL);
   2.971 +}
   2.972 +
   2.973 +static char **
   2.974 +smfsh_completion(const char *text, int start, int end)
   2.975 +{
   2.976 +	int i;
   2.977 +
   2.978 +	/* Return NULL if "text" is not the first word in the input line. */
   2.979 +	if (start != 0) {
   2.980 +		for (i = 0; i < start; i++) {
   2.981 +			if (!isspace(rl_line_buffer[i]))
   2.982 +				return (NULL);
   2.983 +		}
   2.984 +	}
   2.985 +
   2.986 +	return (rl_completion_matches(text, smfsh_command_generator));
   2.987 +}
   2.988 +
   2.989 +#endif
   2.990 +
   2.991 +static void
   2.992 +usage(void)
   2.993 +{
   2.994 +	fprintf(stderr, "usage: smfsh [-V | file]\n");
   2.995 +
   2.996 +	exit(EX_USAGE);
   2.997 +}
   2.998 +
   2.999 +int
  2.1000 +main(int argc, char *argv[])
  2.1001 +{
  2.1002 +	int ch;
  2.1003 +
  2.1004 +	while ((ch = getopt(argc, argv, "V")) != -1) {
  2.1005 +		switch (ch) {
  2.1006 +		case 'V':
  2.1007 +			cmd_version(NULL);
  2.1008 +			exit(EX_OK);
  2.1009 +
  2.1010 +		case '?':
  2.1011 +		default:
  2.1012 +			usage();
  2.1013 +		}
  2.1014 +	}
  2.1015 +
  2.1016 +	if (argc > 2)
  2.1017 +		usage();
  2.1018 +
  2.1019 +	/*g_log_set_default_handler(log_handler, NULL);*/
  2.1020 +
  2.1021 +	smf = smf_new();
  2.1022 +	if (smf == NULL) {
  2.1023 +		fg_critical("Cannot initialize smf_t.");
  2.1024 +		return (-1);
  2.1025 +	}
  2.1026 +
  2.1027 +	if (argc == 2)
  2.1028 +		cmd_load(argv[1]);
  2.1029 +	else
  2.1030 +		cmd_trackadd(NULL);
  2.1031 +
  2.1032 +#ifdef HAVE_LIBREADLINE
  2.1033 +	rl_readline_name = "smfsh";
  2.1034 +	rl_attempted_completion_function = smfsh_completion;
  2.1035 +#endif
  2.1036 +
  2.1037 +	for (;;)
  2.1038 +		read_and_execute_command();
  2.1039 +
  2.1040 +	return (0);
  2.1041 +}
  2.1042 +
     3.1 --- a/src/fake_glib.c	Thu Jan 26 11:25:11 2012 +0200
     3.2 +++ b/src/fake_glib.c	Thu Jan 26 15:35:18 2012 +0200
     3.3 @@ -76,6 +76,16 @@
     3.4  
     3.5  /* -- logging -- */
     3.6  
     3.7 +void fg_message(const char *fmt, ...)
     3.8 +{
     3.9 +	va_list ap;
    3.10 +
    3.11 +	va_start(ap, fmt);
    3.12 +	vprintf(fmt, ap);
    3.13 +	va_end(ap);
    3.14 +	putchar('\n');
    3.15 +}
    3.16 +
    3.17  void fg_warning(const char *fmt, ...)
    3.18  {
    3.19  	va_list ap;
    3.20 @@ -85,6 +95,7 @@
    3.21  	va_start(ap, fmt);
    3.22  	vprintf(fmt, ap);
    3.23  	va_end(ap);
    3.24 +	putchar('\n');
    3.25  }
    3.26  
    3.27  void fg_critical(const char *fmt, ...)
    3.28 @@ -96,6 +107,7 @@
    3.29  	va_start(ap, fmt);
    3.30  	vfprintf(stderr, fmt, ap);
    3.31  	va_end(ap);
    3.32 +	putchar('\n');
    3.33  
    3.34  	if(getenv("G_DEBUG")) {
    3.35  		abort();
    3.36 @@ -111,6 +123,7 @@
    3.37  	va_start(ap, fmt);
    3.38  	vfprintf(stderr, fmt, ap);
    3.39  	va_end(ap);
    3.40 +	putchar('\n');
    3.41  	abort();
    3.42  }
    3.43  
    3.44 @@ -123,4 +136,5 @@
    3.45  	va_start(ap, fmt);
    3.46  	vprintf(fmt, ap);
    3.47  	va_end(ap);
    3.48 +	putchar('\n');
    3.49  }
     4.1 --- a/src/fake_glib.h	Thu Jan 26 11:25:11 2012 +0200
     4.2 +++ b/src/fake_glib.h	Thu Jan 26 15:35:18 2012 +0200
     4.3 @@ -15,6 +15,7 @@
     4.4  #define FALSE	0
     4.5  #endif
     4.6  
     4.7 +typedef char gchar;
     4.8  typedef int gint;
     4.9  typedef void* gpointer;
    4.10  typedef const void* gconstpointer;
    4.11 @@ -31,7 +32,7 @@
    4.12  void fg_ptr_array_sort(FakeGPtrArray *arr, FakeGCompareFunc cmp);
    4.13  
    4.14  /* -- logging -- */
    4.15 -#define fg_message	printf
    4.16 +void fg_message(const char *fmt, ...);
    4.17  void fg_warning(const char *fmt, ...);
    4.18  void fg_critical(const char *fmt, ...);
    4.19  void fg_error(const char *fmt, ...);