yummy command line parsing

git-svn-id: file:///srv/svn/repos/haiku/trunk/current@4252 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
shatty 2003-08-09 10:08:56 +00:00
parent 30550660e2
commit e70f15f55a
3 changed files with 94 additions and 9 deletions

View File

@ -141,7 +141,7 @@ resource(101, "BEOS:M:STD_ICON") #'MICN' array {
$"FFFFFFFFFFFFFF0000000EFF00000EFF" $"FFFFFFFFFFFFFF0000000EFF00000EFF"
}; };
resource(1, "BEOS:APP_SIG") (#'MIMS') "application/x-vnd.Be-SHEL"; resource(1, "BEOS:APP_SIG") (#'MIMS') "application/x-vnd.obos.terminal";
resource(2, "BEOS:TYPE") (#'MIMS') "application/x-vnd.Be-elfexecutable"; resource(2, "BEOS:TYPE") (#'MIMS') "application/x-vnd.Be-elfexecutable";

View File

@ -1,10 +1,13 @@
#include <iostream>
#include <Autolock.h> #include <Autolock.h>
#include <Path.h> #include <Path.h>
#include <Point.h> #include <Point.h>
#include "Constants.h"
#include "TerminalApp.h" #include <Constants.h>
#include "TerminalWindow.h" #include <TerminalApp.h>
#include <TerminalWindow.h>
#include <stdio.h>
#include <getopt.h>
BPoint windowPoint(7,26); BPoint windowPoint(7,26);
BPoint cascadeOffset(15,15); BPoint cascadeOffset(15,15);
@ -101,12 +104,93 @@ TerminalApp::RefsReceived(BMessage *message)
} while (true); } while (true);
} }
// TODO: find the arguments for Terminal and implement them all void
TerminalApp::PrintUsage(const char * execname) {
if (execname == 0) {
execname = "Terminal";
}
fprintf(stderr,"Usage: %s [OPTIONS] [SHELL]\n",execname);
fprintf(stderr,"Open a terminal window.\n");
fprintf(stderr,"\n");
fprintf(stderr," -curbg COLOR use COLOR as the cursor background color\n");
fprintf(stderr," -curfg COLOR use COLOR as the cursor foreground (text) color\n");
fprintf(stderr," -bg COLOR use COLOR as the background color\n");
fprintf(stderr," -fg COLOR use COLOR as the foreground (text) color\n");
fprintf(stderr," -g, -geom NxM use geometry N columns by M rows\n");
fprintf(stderr," -h, -help print this help\n");
fprintf(stderr," -m, -meta pass through the META key to the shell\n");
fprintf(stderr," -p, -pref FILE use FILE as a Terminal preference file\n");
fprintf(stderr," -selbg COLOR use COLOR as the selection background color\n");
fprintf(stderr," -selfg COLOR use COLOR as the selection foreground (text) color\n");
fprintf(stderr," -t, -title TITLE use TITLE as the window title\n");
fprintf(stderr,"\n");
fprintf(stderr,"Report bugs to shatty@myrealbox.com\n");
fprintf(stderr,"\n");
}
struct option curbg_opt = { "curbg", required_argument, 0, 1 } ;
struct option curfg_opt = { "curfg", required_argument, 0, 2 } ;
struct option bg_opt = { "bg", required_argument, 0, 3 } ;
struct option fg_opt = { "fg", required_argument, 0, 4 } ;
struct option geom_opt = { "geom", required_argument, 0, 'g' } ;
struct option help_opt = { "help", no_argument, 0, 'h' } ;
struct option meta_opt = { "meta", no_argument, 0, 'm' } ;
struct option pref_opt = { "pref", required_argument, 0, 'p' } ;
struct option selbg_opt = { "selbg", required_argument, 0, 5 } ;
struct option selfg_opt = { "selfg", required_argument, 0, 6 } ;
struct option title_opt = { "title", required_argument, 0, 't' } ;
struct option options[] =
{ curbg_opt, curfg_opt, bg_opt, fg_opt,
geom_opt, help_opt, meta_opt, pref_opt,
selbg_opt, selfg_opt, title_opt, 0
};
// TODO: implement the arguments for Terminal
void void
TerminalApp::ArgvReceived(int32 argc, const char *argv[], const char * cwd) TerminalApp::ArgvReceived(int32 argc, const char *argv[], const char * cwd)
{ {
const char * execname = (argc >= 1 ? argv[0] : ""); if (argc <= 1) {
cout << execname << ": command line arguments not yet implemented" << endl; OpenTerminal();
} else if (argv[1][0] != '-') {
argc--;
printf("execvp(%ld,%s ...)\n",argc,argv[1]);
OpenTerminal();
} else {
const char * execname = (argc >= 1 ? argv[0] : "");
const char * title = 0;
int indexptr = 0;
int ch;
char * const * optargv = (char * const *)argv;
while ((ch = getopt_long_only(argc, optargv, "g:hmp:t:", options, &indexptr)) != -1) {
switch (ch) {
case 'h':
PrintUsage(execname);
return;
break;
case 't':
title = optarg;
printf("title is %s\n", title);
break;
case ':':
switch (optopt) {
case 't':
printf("-t without title\n");
break;
default:
printf("-%c missing argument\n", optopt);
}
break;
case '?':
printf("unknown arg %c\n", optopt);
break;
default:
printf("%s\n",argv[optind]);
return;
}
}
OpenTerminal();
}
return; return;
} }
@ -114,7 +198,7 @@ void
TerminalApp::ReadyToRun() TerminalApp::ReadyToRun()
{ {
if (fWindowCount == 0) { if (fWindowCount == 0) {
OpenTerminal(); Quit();
} }
} }

View File

@ -24,6 +24,7 @@ public:
void CloseTerminal(); void CloseTerminal();
private: private:
void PrintUsage(const char * execname);
int32 fWindowCount; int32 fWindowCount;
int32 fNext_Terminal_Window; int32 fNext_Terminal_Window;