1bb4632f1SStefano Ceccherini /*
2*dace9eadSlazybullfrog * Copyright 2005-2019, Haiku, Inc. All rights reserved.
3*dace9eadSlazybullfrog * Copyright 2005, Ingo Weinhold, <bonefish@users.sf.net>
4*dace9eadSlazybullfrog *
5bb4632f1SStefano Ceccherini * Distributed under the terms of the MIT License.
6*dace9eadSlazybullfrog *
7*dace9eadSlazybullfrog * Authors:
8*dace9eadSlazybullfrog * Jeremiah Bailey, <jjbailey@gmail.com>
9*dace9eadSlazybullfrog * Ingo Weinhold, <bonefish@users.sf.net>
10bb4632f1SStefano Ceccherini */
11bb4632f1SStefano Ceccherini
12bfa1c029SStephan Aßmus
13bfa1c029SStephan Aßmus #include "Arguments.h"
14bfa1c029SStephan Aßmus
15bb4632f1SStefano Ceccherini #include <stdio.h>
16bb4632f1SStefano Ceccherini #include <stdlib.h>
17bb4632f1SStefano Ceccherini #include <string.h>
18bb4632f1SStefano Ceccherini
198989d8a5SStephan Aßmus #include <Catalog.h>
208989d8a5SStephan Aßmus #include <Locale.h>
21bb4632f1SStefano Ceccherini
22bfa1c029SStephan Aßmus
23546208a5SOliver Tappe #undef B_TRANSLATION_CONTEXT
24546208a5SOliver Tappe #define B_TRANSLATION_CONTEXT "Terminal arguments parsing"
25bfa1c029SStephan Aßmus
26bfa1c029SStephan Aßmus
Arguments(int defaultArgsNum,const char * const * defaultArgs)27f7c51167SStefano Ceccherini Arguments::Arguments(int defaultArgsNum, const char* const* defaultArgs)
28bb4632f1SStefano Ceccherini : fUsageRequested(false),
29bb4632f1SStefano Ceccherini fBounds(50, 50, 630, 435),
30bb4632f1SStefano Ceccherini fStandardShell(true),
31bb4632f1SStefano Ceccherini fFullScreen(false),
32bb4632f1SStefano Ceccherini fShellArgumentCount(0),
33bb4632f1SStefano Ceccherini fShellArguments(NULL),
34*dace9eadSlazybullfrog fTitle(NULL),
35*dace9eadSlazybullfrog fWorkingDirectory(NULL)
36bb4632f1SStefano Ceccherini {
37f7c51167SStefano Ceccherini _SetShellArguments(defaultArgsNum, defaultArgs);
38bb4632f1SStefano Ceccherini }
39bb4632f1SStefano Ceccherini
40bb4632f1SStefano Ceccherini
~Arguments()41bb4632f1SStefano Ceccherini Arguments::~Arguments()
42bb4632f1SStefano Ceccherini {
43bb4632f1SStefano Ceccherini _SetShellArguments(0, NULL);
44bb4632f1SStefano Ceccherini }
45bb4632f1SStefano Ceccherini
46004b2edbSStefano Ceccherini
47bb4632f1SStefano Ceccherini void
Parse(int argc,const char * const * argv)48bb4632f1SStefano Ceccherini Arguments::Parse(int argc, const char* const* argv)
49bb4632f1SStefano Ceccherini {
50a8e3f944SAdrien Destugues int argi;
51a8e3f944SAdrien Destugues for (argi = 1; argi < argc; argi ++) {
52a8e3f944SAdrien Destugues const char* arg = argv[argi];
53bb4632f1SStefano Ceccherini
54bb4632f1SStefano Ceccherini if (*arg == '-') {
55a8e3f944SAdrien Destugues if (strcmp(arg, "-h") == 0 || strcmp(arg, "--help") == 0)
56bb4632f1SStefano Ceccherini fUsageRequested = true;
57a8e3f944SAdrien Destugues else if (strcmp(arg, "-t") == 0 || strcmp(arg, "--title") == 0) {
58bb4632f1SStefano Ceccherini // title
59bb4632f1SStefano Ceccherini if (argi >= argc)
60bb4632f1SStefano Ceccherini fUsageRequested = true;
61bb4632f1SStefano Ceccherini else
62289159dfSAdrien Destugues fTitle = argv[++argi];
63*dace9eadSlazybullfrog } else if (strcmp(arg, "-w") == 0
64*dace9eadSlazybullfrog || strcmp(arg, "--working-directory") == 0) {
65*dace9eadSlazybullfrog if (argi >= argc)
66*dace9eadSlazybullfrog fUsageRequested = true;
67*dace9eadSlazybullfrog else
68*dace9eadSlazybullfrog fWorkingDirectory = argv[++argi];
69*dace9eadSlazybullfrog } else if (strcmp(arg, "-f") == 0
70*dace9eadSlazybullfrog || strcmp(arg, "--fullscreen") == 0)
71bb4632f1SStefano Ceccherini fFullScreen = true;
72a8e3f944SAdrien Destugues else {
73bb4632f1SStefano Ceccherini // illegal option
746c514d3eSMatt Madia fprintf(stderr, B_TRANSLATE("Unrecognized option \"%s\"\n"),
756c514d3eSMatt Madia arg);
76bb4632f1SStefano Ceccherini fUsageRequested = true;
77bb4632f1SStefano Ceccherini }
78bb4632f1SStefano Ceccherini } else {
79bb4632f1SStefano Ceccherini // no option, so the remainder is the shell program with arguments
80a8e3f944SAdrien Destugues _SetShellArguments(argc - argi, argv + argi);
81bb4632f1SStefano Ceccherini argi = argc;
82bb4632f1SStefano Ceccherini fStandardShell = false;
83bb4632f1SStefano Ceccherini }
84bb4632f1SStefano Ceccherini }
85bb4632f1SStefano Ceccherini }
86bb4632f1SStefano Ceccherini
87bb4632f1SStefano Ceccherini
88bb4632f1SStefano Ceccherini void
GetShellArguments(int & argc,const char * const * & argv) const89bb4632f1SStefano Ceccherini Arguments::GetShellArguments(int& argc, const char* const*& argv) const
90bb4632f1SStefano Ceccherini {
91bb4632f1SStefano Ceccherini argc = fShellArgumentCount;
92bb4632f1SStefano Ceccherini argv = fShellArguments;
93bb4632f1SStefano Ceccherini }
94bb4632f1SStefano Ceccherini
95bb4632f1SStefano Ceccherini
96bb4632f1SStefano Ceccherini void
_SetShellArguments(int argc,const char * const * argv)97bb4632f1SStefano Ceccherini Arguments::_SetShellArguments(int argc, const char* const* argv)
98bb4632f1SStefano Ceccherini {
99bb4632f1SStefano Ceccherini // delete old arguments
100bb4632f1SStefano Ceccherini for (int32 i = 0; i < fShellArgumentCount; i++)
101bb4632f1SStefano Ceccherini free((void *)fShellArguments[i]);
102bb4632f1SStefano Ceccherini delete[] fShellArguments;
103bb4632f1SStefano Ceccherini
104bb4632f1SStefano Ceccherini fShellArguments = NULL;
105bb4632f1SStefano Ceccherini fShellArgumentCount = 0;
106bb4632f1SStefano Ceccherini
107bb4632f1SStefano Ceccherini // copy new ones
108bb4632f1SStefano Ceccherini if (argc > 0 && argv) {
109bb4632f1SStefano Ceccherini fShellArguments = new const char*[argc + 1];
110bb4632f1SStefano Ceccherini for (int i = 0; i < argc; i++)
111bb4632f1SStefano Ceccherini fShellArguments[i] = strdup(argv[i]);
112bb4632f1SStefano Ceccherini
113bb4632f1SStefano Ceccherini fShellArguments[argc] = NULL;
114bb4632f1SStefano Ceccherini fShellArgumentCount = argc;
115bb4632f1SStefano Ceccherini }
116bb4632f1SStefano Ceccherini }
117bb4632f1SStefano Ceccherini
118