fill_argv_message() now takes a reference as parameter.

Also fixed BApplication::do_argv() which didn't NULL terminate the array
as it should have done, and would call ArgvReceived() with argc == 0 and
a NULL parameter for argv.
Removed strange trailing spaces at the end of the lines for those two functions.


git-svn-id: file:///srv/svn/repos/haiku/trunk/current@11199 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Axel Dörfler 2005-02-02 04:40:11 +00:00
parent 9465cd7465
commit bee723280c
1 changed files with 56 additions and 57 deletions

View File

@ -1,5 +1,5 @@
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
// Copyright (c) 2001-2004, Haiku, inc. // Copyright (c) 2001-2005, Haiku, inc.
// //
// Permission is hereby granted, free of charge, to any person obtaining a // Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"), // copy of this software and associated documentation files (the "Software"),
@ -186,7 +186,7 @@ extern const char * const *__libc_argv;
// prototypes of helper functions // prototypes of helper functions
static const char* looper_name_for(const char *signature); static const char* looper_name_for(const char *signature);
static status_t check_app_signature(const char *signature); static status_t check_app_signature(const char *signature);
static void fill_argv_message(BMessage *message); static void fill_argv_message(BMessage &message);
BApplication::BApplication(const char *signature) BApplication::BApplication(const char *signature)
@ -375,7 +375,7 @@ DBG(OUT("BApplication::InitData(`%s', %p)\n", signature, _error));
&& !(otherAppInfo.flags & B_ARGV_ONLY)) { && !(otherAppInfo.flags & B_ARGV_ONLY)) {
// create an B_ARGV_RECEIVED message // create an B_ARGV_RECEIVED message
BMessage argvMessage(B_ARGV_RECEIVED); BMessage argvMessage(B_ARGV_RECEIVED);
fill_argv_message(&argvMessage); fill_argv_message(argvMessage);
// replace the first argv string with the path of the // replace the first argv string with the path of the
// other application // other application
@ -398,7 +398,7 @@ DBG(OUT("BApplication::InitData(`%s', %p)\n", signature, _error));
if (__libc_argc > 1) { if (__libc_argc > 1) {
BMessage argvMessage(B_ARGV_RECEIVED); BMessage argvMessage(B_ARGV_RECEIVED);
fill_argv_message(&argvMessage); fill_argv_message(argvMessage);
PostMessage(&argvMessage, this); PostMessage(&argvMessage, this);
} }
// send a B_READY_TO_RUN message as well // send a B_READY_TO_RUN message as well
@ -1188,43 +1188,43 @@ BApplication::quit_all_windows(bool force)
void void
BApplication::do_argv(BMessage *message) BApplication::do_argv(BMessage *message)
{ {
// TODO: Consider renaming this function to something // TODO: Consider renaming this function to something
// a bit more descriptive, like "handle_argv_message()", // a bit more descriptive, like "handle_argv_message()",
// or "HandleArgvMessage()" // or "HandleArgvMessage()"
ASSERT(message != NULL); ASSERT(message != NULL);
// build the argv vector // build the argv vector
status_t error = B_OK; status_t error = B_OK;
int32 argc; int32 argc = 0;
char **argv = NULL; char **argv = NULL;
if (message->FindInt32("argc", &argc) == B_OK && argc > 0) { if (message->FindInt32("argc", &argc) == B_OK && argc > 0) {
argv = new char*[argc]; // allocate a NULL terminated array
for (int32 i = 0; i < argc; i++) argv = new char*[argc + 1];
argv[i] = NULL; argv[argc] = NULL;
// copy the arguments
for (int32 i = 0; error == B_OK && i < argc; i++) {
const char *arg = NULL;
error = message->FindString("argv", i, &arg);
if (error == B_OK && arg) {
argv[i] = strdup(arg);
if (argv[i] == NULL)
error = B_NO_MEMORY;
}
}
}
// call the hook // copy the arguments
if (error == B_OK) for (int32 i = 0; error == B_OK && i < argc; i++) {
ArgvReceived(argc, argv); const char *arg = NULL;
error = message->FindString("argv", i, &arg);
if (error == B_OK && arg) {
argv[i] = strdup(arg);
if (argv[i] == NULL)
error = B_NO_MEMORY;
}
}
}
// cleanup // call the hook
if (argv) { if (error == B_OK && argc > 0)
for (int32 i = 0; i < argc; i++) ArgvReceived(argc, argv);
free(argv[i]);
delete[] argv; // cleanup
} if (argv) {
for (int32 i = 0; i < argc; i++)
free(argv[i]);
delete[] argv;
}
} }
@ -1332,7 +1332,7 @@ check_app_signature(const char *signature)
*/ */
static static
const char* const char *
looper_name_for(const char *signature) looper_name_for(const char *signature)
{ {
if (signature && !strcasecmp(signature, kRegistrarSignature)) if (signature && !strcasecmp(signature, kRegistrarSignature))
@ -1345,26 +1345,25 @@ looper_name_for(const char *signature)
/*! \brief Fills the passed BMessage with B_ARGV_RECEIVED infos. /*! \brief Fills the passed BMessage with B_ARGV_RECEIVED infos.
*/ */
static void static
fill_argv_message(BMessage *message) void
{ fill_argv_message(BMessage &message)
if (message) { {
message->what = B_ARGV_RECEIVED; message.what = B_ARGV_RECEIVED;
int32 argc = __libc_argc; int32 argc = __libc_argc;
const char * const *argv = __libc_argv; const char * const *argv = __libc_argv;
// add argc // add argc
message->AddInt32("argc", argc); message.AddInt32("argc", argc);
// add argv // add argv
for (int32 i = 0; i < argc; i++) for (int32 i = 0; i < argc; i++)
message->AddString("argv", argv[i]); message.AddString("argv", argv[i]);
// add current working directory
char cwd[B_PATH_NAME_LENGTH];
if (getcwd(cwd, B_PATH_NAME_LENGTH))
message.AddString("cwd", cwd);
}
// add current working directory
char cwd[B_PATH_NAME_LENGTH];
if (getcwd(cwd, B_PATH_NAME_LENGTH))
message->AddString("cwd", cwd);
}
}