1 /* 2 * Copyright 2005, Ingo Weinhold, bonefish@users.sf.net. 3 * Distributed under the terms of the MIT License. 4 */ 5 6 7 #include "Arguments.h" 8 9 #include <stdio.h> 10 #include <stdlib.h> 11 #include <string.h> 12 13 #include <Catalog.h> 14 #include <Locale.h> 15 16 17 #undef B_TRANSLATION_CONTEXT 18 #define B_TRANSLATION_CONTEXT "Terminal arguments parsing" 19 20 21 Arguments::Arguments(int defaultArgsNum, const char* const* defaultArgs) 22 : fUsageRequested(false), 23 fBounds(50, 50, 630, 435), 24 fStandardShell(true), 25 fFullScreen(false), 26 fShellArgumentCount(0), 27 fShellArguments(NULL), 28 fTitle(NULL) 29 { 30 _SetShellArguments(defaultArgsNum, defaultArgs); 31 } 32 33 34 Arguments::~Arguments() 35 { 36 _SetShellArguments(0, NULL); 37 } 38 39 40 void 41 Arguments::Parse(int argc, const char* const* argv) 42 { 43 int argi; 44 for (argi = 1; argi < argc; argi ++) { 45 const char* arg = argv[argi]; 46 47 if (*arg == '-') { 48 if (strcmp(arg, "-h") == 0 || strcmp(arg, "--help") == 0) 49 fUsageRequested = true; 50 else if (strcmp(arg, "-t") == 0 || strcmp(arg, "--title") == 0) { 51 // title 52 if (argi >= argc) 53 fUsageRequested = true; 54 else 55 fTitle = argv[++argi]; 56 57 } else if (strcmp(arg, "-f") == 0 || strcmp(arg, "--fullscreen") 58 == 0) 59 fFullScreen = true; 60 else { 61 // illegal option 62 fprintf(stderr, B_TRANSLATE("Unrecognized option \"%s\"\n"), 63 arg); 64 fUsageRequested = true; 65 } 66 } else { 67 // no option, so the remainder is the shell program with arguments 68 _SetShellArguments(argc - argi, argv + argi); 69 argi = argc; 70 fStandardShell = false; 71 } 72 } 73 } 74 75 76 void 77 Arguments::GetShellArguments(int& argc, const char* const*& argv) const 78 { 79 argc = fShellArgumentCount; 80 argv = fShellArguments; 81 } 82 83 84 void 85 Arguments::_SetShellArguments(int argc, const char* const* argv) 86 { 87 // delete old arguments 88 for (int32 i = 0; i < fShellArgumentCount; i++) 89 free((void *)fShellArguments[i]); 90 delete[] fShellArguments; 91 92 fShellArguments = NULL; 93 fShellArgumentCount = 0; 94 95 // copy new ones 96 if (argc > 0 && argv) { 97 fShellArguments = new const char*[argc + 1]; 98 for (int i = 0; i < argc; i++) 99 fShellArguments[i] = strdup(argv[i]); 100 101 fShellArguments[argc] = NULL; 102 fShellArgumentCount = argc; 103 } 104 } 105 106