1bb4632f1SStefano Ceccherini /* 2bb4632f1SStefano Ceccherini * Copyright 2005, Ingo Weinhold, bonefish@users.sf.net. 3bb4632f1SStefano Ceccherini * Distributed under the terms of the MIT License. 4bb4632f1SStefano Ceccherini */ 5bb4632f1SStefano Ceccherini 6bfa1c029SStephan Aßmus 7bfa1c029SStephan Aßmus #include "Arguments.h" 8bfa1c029SStephan Aßmus 9bb4632f1SStefano Ceccherini #include <stdio.h> 10bb4632f1SStefano Ceccherini #include <stdlib.h> 11bb4632f1SStefano Ceccherini #include <string.h> 12bb4632f1SStefano Ceccherini 138989d8a5SStephan Aßmus #include <Catalog.h> 148989d8a5SStephan Aßmus #include <Locale.h> 15bb4632f1SStefano Ceccherini 16bfa1c029SStephan Aßmus 174eb0cbb0SMatt Madia #undef B_TRANSLATE_CONTEXT 184eb0cbb0SMatt Madia #define B_TRANSLATE_CONTEXT "Terminal arguments parsing" 19bfa1c029SStephan Aßmus 20bfa1c029SStephan Aßmus 21f7c51167SStefano Ceccherini Arguments::Arguments(int defaultArgsNum, const char* const* defaultArgs) 22bb4632f1SStefano Ceccherini : fUsageRequested(false), 23bb4632f1SStefano Ceccherini fBounds(50, 50, 630, 435), 24bb4632f1SStefano Ceccherini fStandardShell(true), 25bb4632f1SStefano Ceccherini fFullScreen(false), 26bb4632f1SStefano Ceccherini fShellArgumentCount(0), 27bb4632f1SStefano Ceccherini fShellArguments(NULL), 28bb4632f1SStefano Ceccherini fTitle(NULL) 29bb4632f1SStefano Ceccherini { 30f7c51167SStefano Ceccherini _SetShellArguments(defaultArgsNum, defaultArgs); 31bb4632f1SStefano Ceccherini } 32bb4632f1SStefano Ceccherini 33bb4632f1SStefano Ceccherini 34bb4632f1SStefano Ceccherini Arguments::~Arguments() 35bb4632f1SStefano Ceccherini { 36bb4632f1SStefano Ceccherini _SetShellArguments(0, NULL); 37bb4632f1SStefano Ceccherini } 38bb4632f1SStefano Ceccherini 39*004b2edbSStefano Ceccherini 40bb4632f1SStefano Ceccherini void 41bb4632f1SStefano Ceccherini Arguments::Parse(int argc, const char* const* argv) 42bb4632f1SStefano Ceccherini { 43bb4632f1SStefano Ceccherini int argi = 1; 44bb4632f1SStefano Ceccherini while (argi < argc) { 45bb4632f1SStefano Ceccherini const char* arg = argv[argi++]; 46bb4632f1SStefano Ceccherini 47bb4632f1SStefano Ceccherini if (*arg == '-') { 48bb4632f1SStefano Ceccherini if (strcmp(arg, "-h") == 0 || strcmp(arg, "--help") == 0) { 49bb4632f1SStefano Ceccherini fUsageRequested = true; 50bb4632f1SStefano Ceccherini 51bb4632f1SStefano Ceccherini /*} else if (strcmp(arg, "-l") == 0) { 52bb4632f1SStefano Ceccherini // location 53bb4632f1SStefano Ceccherini float x, y; 54bb4632f1SStefano Ceccherini if (argi + 1 >= argc 55bb4632f1SStefano Ceccherini || sscanf(argv[argi++], "%f", &x) != 1 56bb4632f1SStefano Ceccherini || sscanf(argv[argi++], "%f", &y) != 1) { 57bb4632f1SStefano Ceccherini print_usage_and_exit(true); 58bb4632f1SStefano Ceccherini } 59bb4632f1SStefano Ceccherini 60bb4632f1SStefano Ceccherini fBounds.OffsetTo(x, y); 61bb4632f1SStefano Ceccherini 62bb4632f1SStefano Ceccherini } else if (strcmp(arg, "-s") == 0) { 63bb4632f1SStefano Ceccherini // size 64bb4632f1SStefano Ceccherini float width, height; 65bb4632f1SStefano Ceccherini if (argi + 1 >= argc 66bb4632f1SStefano Ceccherini || sscanf(argv[argi++], "%f", &width) != 1 67bb4632f1SStefano Ceccherini || sscanf(argv[argi++], "%f", &height) != 1) { 68bb4632f1SStefano Ceccherini print_usage_and_exit(true); 69bb4632f1SStefano Ceccherini } 70bb4632f1SStefano Ceccherini 71bb4632f1SStefano Ceccherini fBounds.right = fBounds.left + width; 72bb4632f1SStefano Ceccherini fBounds.bottom = fBounds.top + height; 73bb4632f1SStefano Ceccherini */ 74bb4632f1SStefano Ceccherini } else if (strcmp(arg, "-t") == 0 || strcmp(arg, "--title") == 0) { 75bb4632f1SStefano Ceccherini // title 76bb4632f1SStefano Ceccherini if (argi >= argc) 77bb4632f1SStefano Ceccherini fUsageRequested = true; 78bb4632f1SStefano Ceccherini else 79bb4632f1SStefano Ceccherini fTitle = argv[argi++]; 80bb4632f1SStefano Ceccherini 81bb4632f1SStefano Ceccherini } else if (strcmp(arg, "-f") == 0 || strcmp(arg, "--fullscreen") == 0) { 82bb4632f1SStefano Ceccherini fFullScreen = true; 83bb4632f1SStefano Ceccherini argi++; 84bb4632f1SStefano Ceccherini } else { 85bb4632f1SStefano Ceccherini // illegal option 866c514d3eSMatt Madia fprintf(stderr, B_TRANSLATE("Unrecognized option \"%s\"\n"), 876c514d3eSMatt Madia arg); 88bb4632f1SStefano Ceccherini fUsageRequested = true; 89bb4632f1SStefano Ceccherini } 90bb4632f1SStefano Ceccherini 91bb4632f1SStefano Ceccherini } else { 92bb4632f1SStefano Ceccherini // no option, so the remainder is the shell program with arguments 93bb4632f1SStefano Ceccherini _SetShellArguments(argc - argi + 1, argv + argi - 1); 94bb4632f1SStefano Ceccherini argi = argc; 95bb4632f1SStefano Ceccherini fStandardShell = false; 96bb4632f1SStefano Ceccherini } 97bb4632f1SStefano Ceccherini } 98bb4632f1SStefano Ceccherini } 99bb4632f1SStefano Ceccherini 100bb4632f1SStefano Ceccherini 101bb4632f1SStefano Ceccherini void 102bb4632f1SStefano Ceccherini Arguments::GetShellArguments(int& argc, const char* const*& argv) const 103bb4632f1SStefano Ceccherini { 104bb4632f1SStefano Ceccherini argc = fShellArgumentCount; 105bb4632f1SStefano Ceccherini argv = fShellArguments; 106bb4632f1SStefano Ceccherini } 107bb4632f1SStefano Ceccherini 108bb4632f1SStefano Ceccherini 109bb4632f1SStefano Ceccherini void 110bb4632f1SStefano Ceccherini Arguments::_SetShellArguments(int argc, const char* const* argv) 111bb4632f1SStefano Ceccherini { 112bb4632f1SStefano Ceccherini // delete old arguments 113bb4632f1SStefano Ceccherini for (int32 i = 0; i < fShellArgumentCount; i++) 114bb4632f1SStefano Ceccherini free((void *)fShellArguments[i]); 115bb4632f1SStefano Ceccherini delete[] fShellArguments; 116bb4632f1SStefano Ceccherini 117bb4632f1SStefano Ceccherini fShellArguments = NULL; 118bb4632f1SStefano Ceccherini fShellArgumentCount = 0; 119bb4632f1SStefano Ceccherini 120bb4632f1SStefano Ceccherini // copy new ones 121bb4632f1SStefano Ceccherini if (argc > 0 && argv) { 122bb4632f1SStefano Ceccherini fShellArguments = new const char*[argc + 1]; 123bb4632f1SStefano Ceccherini for (int i = 0; i < argc; i++) 124bb4632f1SStefano Ceccherini fShellArguments[i] = strdup(argv[i]); 125bb4632f1SStefano Ceccherini 126bb4632f1SStefano Ceccherini fShellArguments[argc] = NULL; 127bb4632f1SStefano Ceccherini fShellArgumentCount = argc; 128bb4632f1SStefano Ceccherini } 129bb4632f1SStefano Ceccherini } 130bb4632f1SStefano Ceccherini 131