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