/* $Id: ipret.c,v 1.10 2006/09/16 10:17:33 toad32767 Exp $ */ /** ** 2005, 2006 by Marco Trillo ** This file is part of UModPlayer, and is released by ** its autors to the Public Domain. ** In case it's not legally possible, its autors grant ** anyone the right to use, redistribute and modify ** this software for any purpose, without any conditions, ** unless such conditions are required by law. ** ** THIS FILE COMES WITHOUT ANY WARRANTY. THE AUTHORS ** SHALL NOT BE LIABLE FOR ANY DAMAGE RESULTING BY THE ** USE OR MISUSE OF THIS SOFTWARE. **/ #include #include #include #include #include #include #include #include /* * User commands */ void CommandOpen(char *filenam) { if (filenam == NULL || *filenam == '\0') { fputs("usage: open \n", stderr); return; } /* free the current file */ UFreeFile(); file.name = strcopyof(filenam); if (file.name == NULL) { error("%s", MESSAGE_NO_MEMORY); return; } file.malloc = TRUE; ULoadFile(); } void CommandConfig(char *unused) { WTable table; rr = (char **) malloc(8 * sizeof(char *)); rr[0] = "Sampling Rate:"; rr[1] = "Resampling Mode:"; rr[2] = "Noise Reduction:"; rr[3] = "Reverb:"; rr[4] = "Megabass:"; rr[5] = "Surround:"; rr[6] = "Channels:"; rr[7] = "Master Volume:"; TableSetOptions(&table, 0, 8, 2, CalcLen(rr, 8) + 1, 0, TABLE_LEFT_ALIGN); TableUseTheme(&table, sets.appareance); TableInitCallback(&table, MyTextCallback); DrawTable(table); free(rr); } void CommandHelp(char *unused) { puts("Commands available on interactive mode:"); puts(" quit config play"); puts(" position noise reverb"); puts(" megabass surround reset"); puts(" loadconfig saveconfig info"); puts(" list / ls message samples"); puts(" instruments export advanced"); puts(" setadvanced playlist volume"); puts(" display - goto samplerate "); puts(" open volume savemessage "); puts(" resampling channels (1|2) cd "); puts(" exec version"); puts(""); puts("Hint: The '!' command prints the last executed command,"); puts(" and the '!!' command (without quotes) runs it."); } void CommandVolume(char *volstr) { if (volstr == NULL || *volstr == '\0') { printf("volume: %u\n", sets.vol); } else { sets.vol = (unsigned int) strtoul(volstr, NULL, 10); if (sets.vol > 768) /* XXX - what's the ModPlug limit ? */ sets.vol = 768; else if (sets.vol < 1) sets.vol = 1; } } void CommandDisplay(char *range) { if (range == NULL || *range == '\0') { NoteDisplay_Start(-1, -1); } else { int x, y; GetRangeValues(range, &x, &y); NoteDisplay_Start(x, y); } } void CommandSampleRate(char *rate) { int r; if (rate == NULL || *rate == '\0') fputs("usage: samplerate \n", stderr); else { r = (int) strtoul(rate, NULL, 10); switch (r) { case 11025: case 16000: case 22050: case 24000: case 32000: case 44100: sets.samplerate = r; /* XXX -- hack for ModPlug 0.7/0.8 configuration oddness (bug or feature?) */ if (file.name != NULL) { CoreSound_InitOptions(); } break; default: warning("invalid sampling rate -- %d. not changing.\n", r); } } } void CommandReSampling(char *mode) { if (mode == NULL) { usage: fputs("usage: resampling (NO_INTERPOLATE|LINEAR|SPLINE|FIR_FILTER)\n", stderr); return; } if (strcasecmp(mode, "NO_INTERPOLATE") == 0 || strcmp(mode, "RESAMPLE") == 0) /* deprecated, compat. only */ sets.resampling = 0; else if (strcasecmp(mode, "LINEAR") == 0) sets.resampling = 1; else if (strcasecmp(mode, "SPLINE") == 0) sets.resampling = 2; else if (strcasecmp(mode, "FIR_FILTER") == 0) sets.resampling = 3; else goto usage; } void CommandChannels(char *ch) { if (ch == NULL || *ch == '\0') { fputs("usage: channels (1|2)\n", stderr); } else { if (*ch == '1') sets.channels = 1; else sets.channels = 2; } } void CommandGoTo(char *to) { if (to == NULL || *to == '\0') { usage: fputs("usage: goto \n", stderr); } else { int pos; pos = atoi(to); if (pos > -1) { ModPlug_SeekOrder(file.mod, pos); } else { goto usage; } } } void CommandChDir(char *dir) { if (dir == NULL || *dir == '\0') { char *home = getenv("HOME"); if (home != NULL) { chdir(home); } } else { chdir(dir); } } void CommandQuit(char *unused) { exit(UM_OK); } void CommandInfo(char *unused) { DrawInfoTable(); } void CommandPlay(char *unused) { (void) CoreSound_StartMonitor(); } void CommandPosition(char *unused) { DisplayCurPos(); } void CommandNoise(char *unused) { sets.flags ^= MODPLUG_ENABLE_NOISE_REDUCTION; } void CommandReverb(char *unused) { sets.flags ^= MODPLUG_ENABLE_REVERB; } void CommandMegabass(char *unused) { sets.flags ^= MODPLUG_ENABLE_MEGABASS; } void CommandSurround(char *unused) { sets.flags ^= MODPLUG_ENABLE_SURROUND; } void CommandReset(char *unused) { DefaultOptions(&sets); } void CommandLoadConfig(char *unused) { InitOptions(&sets); } void CommandSaveConfig(char *unused) { SaveOptions(&sets); } void CommandList(char *unused) { SongList(); } void CommandMessage(char *unused) { DisplaySongComments(); } void CommandInstruments(char *unused) { DisplayInstruments(0); } void CommandSamples(char *unused) { DisplaySamples(0); } void CommandExport(char *unused) { ExportSong(); } void CommandAdvanced(char *unused) { AdvancedConfig(); } void CommandSetAdvanced(char *unused) { AdvancedConfig(); SetAdvancedConfig(); } void CommandPlayList(char *unused) { PlayList(); } void CommandExec(char *command) { if (command == NULL || *command == '\0') fputs("usage: exec \n", stderr); else system(command); } void CommandVersion(char *unused) { PrintBanner(); } /* * Command interpreter */ struct command { char *name; int moduleNeeded; void (*action) (char *); }; LOCAL struct command commands[] = { {"open", 0, CommandOpen}, {"load", 0, CommandOpen}, {"config", 0, CommandConfig}, {"help", 0, CommandHelp}, {"?", 0, CommandHelp}, {"volume", 0, CommandVolume}, {"display", 1, CommandDisplay}, {"samplerate", 0, CommandSampleRate}, {"resampling", 0, CommandReSampling}, {"channels", 0, CommandChannels}, {"goto", 1, CommandGoTo}, {"chdir", 0, CommandChDir}, {"cd", 0, CommandChDir}, {"quit", 0, CommandQuit}, {"exit", 0, CommandQuit}, {"info", 1, CommandInfo}, {"play", 1, CommandPlay}, {"position", 1, CommandPosition}, {"noise", 0, CommandNoise}, {"reverb", 0, CommandReverb}, {"megabass", 0, CommandMegabass}, {"surround", 0, CommandSurround}, {"reset", 0, CommandReset}, {"loadconfig", 0, CommandLoadConfig}, {"saveconfig", 0, CommandSaveConfig}, {"list", 0, CommandList}, {"ls", 0, CommandList}, {"message", 1, CommandMessage}, {"instruments", 1, CommandInstruments}, {"samples", 1, CommandSamples}, {"export", 1, CommandExport}, {"advanced", 0, CommandAdvanced}, {"setadvanced", 0, CommandSetAdvanced}, {"playlist", 0, CommandPlayList}, {"exec", 0, CommandExec}, {"version", 0, CommandVersion}, {NULL, 0, NULL} }; struct cline { char *command; char *params; }; LOCAL struct cline * ParseCommandLine(char *com) { char *t; static struct cline cl; TrimString(com); t = com; while (*t != '\0' && *t != ' ' && *t != '\t' && *t != '#') { ++t; } if (*t == '#') { /* skip comments */ *t = '\0'; cl.command = com; cl.params = NULL; } else if (t > com && (*t == ' ' || *t == '\t')) { *t = '\0'; do ++t; while (*t == ' ' || *t == '\t'); cl.command = com; cl.params = t; } else { cl.command = com; cl.params = NULL; } return &cl; } LOCAL void RunCommand(char *line) { struct cline *l; struct command *c; l = ParseCommandLine(line); /* null command */ if (l->command[0] == '\0') return; for (c = commands; c->name != NULL; ++c) { if (strcmp(c->name, l->command) == 0) { if (c->moduleNeeded && file.name == NULL) { warning("%s: no module loaded\n", l->command); } else { c->action(l->params); } return; } } warning("%s: invalid command\nTry: `help'\n", l->command); } EXPORT void CommandInterpreter() { char *str, *last = NULL, pathbuf[1024]; for (;;) { getcwd(pathbuf, 1024); printf("umodplayer[%s]%% ", pathbuf); fflush(stdout); str = ReadString(); if (str == NULL) { fputs(MESSAGE_NO_MEMORY, stderr); continue; } if (str[0] == '!') { if (last != NULL) { if (str[1] == '!') { RunCommand(last); } else { puts(last); } } free(str); } else { if (last != NULL) free(last); last = strcopyof(str); /* RunCommand() modifies 'str' */ RunCommand(str); free(str); } } }