xref: /haiku/src/apps/terminal/Arguments.cpp (revision a5bf12376daeded4049521eb17a6cc41192250d9)
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_TRANSLATE_CONTEXT
18 #define B_TRANSLATE_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 void
40 Arguments::Parse(int argc, const char *const *argv)
41 {
42 	int argi = 1;
43 	while (argi < argc) {
44 		const char *arg = argv[argi++];
45 
46 		if (*arg == '-') {
47 			if (strcmp(arg, "-h") == 0 || strcmp(arg, "--help") == 0) {
48 				fUsageRequested = true;
49 
50 			/*} else if (strcmp(arg, "-l") == 0) {
51 				// location
52 				float x, y;
53 				if (argi + 1 >= argc
54 					|| sscanf(argv[argi++], "%f", &x) != 1
55 					|| sscanf(argv[argi++], "%f", &y) != 1) {
56 					print_usage_and_exit(true);
57 				}
58 
59 				fBounds.OffsetTo(x, y);
60 
61 			} else if (strcmp(arg, "-s") == 0) {
62 				// size
63 				float width, height;
64 				if (argi + 1 >= argc
65 					|| sscanf(argv[argi++], "%f", &width) != 1
66 					|| sscanf(argv[argi++], "%f", &height) != 1) {
67 					print_usage_and_exit(true);
68 				}
69 
70 				fBounds.right = fBounds.left + width;
71 				fBounds.bottom = fBounds.top + height;
72 */
73 			} else if (strcmp(arg, "-t") == 0 || strcmp(arg, "--title") == 0) {
74 				// title
75 				if (argi >= argc)
76 					fUsageRequested = true;
77 				else
78 					fTitle = argv[argi++];
79 
80 			} else if (strcmp(arg, "-f") == 0 || strcmp(arg, "--fullscreen") == 0) {
81 				fFullScreen = true;
82 				argi++;
83 			} else {
84 				// illegal option
85 				fprintf(stderr, B_TRANSLATE("Unrecognized option \"%s\"\n"),
86 					arg);
87 				fUsageRequested = true;
88 			}
89 
90 		} else {
91 			// no option, so the remainder is the shell program with arguments
92 			_SetShellArguments(argc - argi + 1, argv + argi - 1);
93 			argi = argc;
94 			fStandardShell = false;
95 		}
96 	}
97 }
98 
99 
100 void
101 Arguments::GetShellArguments(int &argc, const char *const *&argv) const
102 {
103 	argc = fShellArgumentCount;
104 	argv = fShellArguments;
105 }
106 
107 
108 void
109 Arguments::_SetShellArguments(int argc, const char *const *argv)
110 {
111 	// delete old arguments
112 	for (int32 i = 0; i < fShellArgumentCount; i++)
113 		free((void *)fShellArguments[i]);
114 	delete[] fShellArguments;
115 
116 	fShellArguments = NULL;
117 	fShellArgumentCount = 0;
118 
119 	// copy new ones
120 	if (argc > 0 && argv) {
121 		fShellArguments = new const char*[argc + 1];
122 		for (int i = 0; i < argc; i++)
123 			fShellArguments[i] = strdup(argv[i]);
124 
125 		fShellArguments[argc] = NULL;
126 		fShellArgumentCount = argc;
127 	}
128 }
129 
130