applied a bit of our code guidelines

git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@17750 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Jérôme Duval
2006-06-07 09:14:21 +00:00
parent 84d89d2ae0
commit e730e7730b
+109 -88
View File
@@ -138,34 +138,38 @@ bool silent;
// flag for stdout mode // flag for stdout mode
bool output; bool output;
int parse(BMessenger& the_application, int argc, char *argv[], int32 argapp) status_t
parse(BMessenger& the_application, int argc, char *argv[], int32 argapp)
{ {
if (!the_application.IsValid()) { if (!the_application.IsValid()) {
if(!silent) fprintf(stderr, "Cannot find the application (%s)\n", argv[argapp]); if (!silent)
return -1; fprintf(stderr, "Cannot find the application (%s)\n", argv[argapp]);
return B_ERROR;
} }
if (argc < 3) { if (argc < 3) {
if(!silent) fprintf(stderr, "Cannot find the verb!\n"); if (!silent)
return -1; fprintf(stderr, "Cannot find the verb!\n");
return B_ERROR;
} }
BMessage the_reply; BMessage the_reply;
int32 argx = argapp+1; int32 argx = argapp+1;
// const char *test_string = "set File of Window Sample to file(/boot/home/media/images/BeLogo.psd)";
// status_t err = Hey(&the_application, test_string, &the_reply);
status_t err = Hey(&the_application, argv, &argx, argc, &the_reply); status_t err = Hey(&the_application, argv, &argx, argc, &the_reply);
if (err != B_OK) { if (err != B_OK) {
if(!silent) fprintf(stderr, "Error when sending message to %s!\n", argv[argapp]); if (!silent)
return -1; fprintf(stderr, "Error when sending message to %s!\n", argv[argapp]);
return B_ERROR;
} else { } else {
if (the_reply.what == (uint32)B_MESSAGE_NOT_UNDERSTOOD || the_reply.what==(uint32)B_ERROR){ // I do it myself if (the_reply.what == (uint32)B_MESSAGE_NOT_UNDERSTOOD || the_reply.what==(uint32)B_ERROR){ // I do it myself
if (the_reply.HasString("message")){ if (the_reply.HasString("message")){
if(!silent) printf("%s (error 0x%8lX)\n", the_reply.FindString("message"), the_reply.FindInt32("error")); if (!silent)
printf("%s (error 0x%8lX)\n", the_reply.FindString("message"), the_reply.FindInt32("error"));
} else { } else {
if(!silent) printf("error 0x%8lX\n", the_reply.FindInt32("error")); if (!silent)
printf("error 0x%8lX\n", the_reply.FindInt32("error"));
} }
return 1; return 1;
} else { } else {
@@ -217,8 +221,7 @@ int parse(BMessenger& the_application, int argc, char *argv[], int32 argapp)
printf("Unsupported type\n"); printf("Unsupported type\n");
} }
} }
} } else {
else {
printf("Reply "); printf("Reply ");
print_message(&the_reply); print_message(&the_reply);
printf("\n"); printf("\n");
@@ -226,10 +229,11 @@ int parse(BMessenger& the_application, int argc, char *argv[], int32 argapp)
} }
} }
} }
return 0; return B_OK;
} }
int main(int argc, char *argv[]) int
main(int argc, char *argv[])
{ {
BApplication app("application/x-amezei-hey"); BApplication app("application/x-amezei-hey");
@@ -256,8 +260,7 @@ int main(int argc, char *argv[])
output = false; output = false;
// Updated option mechanism --SS // Updated option mechanism --SS
for (int i = 0; i < argc; i++) for (int i = 0; i < argc; i++) {
{
if (strcmp(argv[i], "-s")==0 || strcmp(argv[i], "-S")==0){ if (strcmp(argv[i], "-s")==0 || strcmp(argv[i], "-S")==0){
silent = true; silent = true;
argapp++; argapp++;
@@ -295,16 +298,21 @@ int main(int argc, char *argv[])
} }
int32 HeyInterpreterThreadHook(void* arg) int32
HeyInterpreterThreadHook(void* arg)
{ {
if (arg) { if (!arg)
return 1;
BMessage environment(*(BMessage*) arg); BMessage environment(*(BMessage*) arg);
char* prompt = "Hey"; char* prompt = "Hey";
if (environment.HasString("prompt")) environment.FindString("prompt", (const char **)&prompt); if (environment.HasString("prompt"))
environment.FindString("prompt", (const char **)&prompt);
printf("%s> ", prompt); printf("%s> ", prompt);
BMessenger target; BMessenger target;
if (environment.HasMessenger("Target")) environment.FindMessenger("Target", &target); if (environment.HasMessenger("Target"))
environment.FindMessenger("Target", &target);
char command[1024]; char command[1024];
status_t err; status_t err;
@@ -321,13 +329,10 @@ int32 HeyInterpreterThreadHook(void* arg)
} }
return 0; return 0;
} else {
return 1;
}
} }
status_t Hey(BMessenger* target, const char* arg, BMessage* reply) status_t
Hey(BMessenger* target, const char* arg, BMessage* reply)
{ {
vector<char*> argv; // number of tokens is now limited only by memory -- [email protected] 1999-11-03 vector<char*> argv; // number of tokens is now limited only by memory -- [email protected] 1999-11-03
char* tokens = new char[strlen(arg)*2]; char* tokens = new char[strlen(arg)*2];
@@ -337,7 +342,8 @@ status_t Hey(BMessenger* target, const char* arg, BMessage* reply)
bool inquotes = false; bool inquotes = false;
while (arg[argNdex] != 0) { // for each character in arg while (arg[argNdex] != 0) { // for each character in arg
if (arg[argNdex] == '\"') inquotes = !inquotes; if (arg[argNdex] == '\"')
inquotes = !inquotes;
if (!inquotes && isSpace(arg[argNdex])) { // if the character is white space if (!inquotes && isSpace(arg[argNdex])) { // if the character is white space
if (tokenNdex!=0) { // close off currentToken token if (tokenNdex!=0) { // close off currentToken token
currentToken[tokenNdex] = 0; currentToken[tokenNdex] = 0;
@@ -368,7 +374,9 @@ status_t Hey(BMessenger* target, const char* arg, BMessage* reply)
return ret; return ret;
} }
bool isSpace(char c)
bool
isSpace(char c)
{ {
switch (c) { switch (c) {
case ' ': case ' ':
@@ -380,7 +388,9 @@ bool isSpace(char c)
} }
} }
status_t Hey(BMessenger* target, char* argv[], int32* argx, int32 argc, BMessage* reply)
status_t
Hey(BMessenger* target, char* argv[], int32* argx, int32 argc, BMessage* reply)
{ {
bool direct_what = false; bool direct_what = false;
BMessage the_message; BMessage the_message;
@@ -390,24 +400,29 @@ status_t Hey(BMessenger* target, char* argv[], int32* argx, int32 argc, BMessage
// parse the specifiers // parse the specifiers
(*argx)++; (*argx)++;
status_t result=B_OK; status_t result=B_OK;
while((result=add_specifier(&get_target, argv, argx, argc))==B_OK){}; while ((result = add_specifier(&get_target, argv, argx, argc))==B_OK)
;
if (result!=B_ERROR){ // bad syntax if (result!=B_ERROR){ // bad syntax
if(!silent) fprintf(stderr, "Bad specifier syntax!\n"); if (!silent)
fprintf(stderr, "Bad specifier syntax!\n");
return result; return result;
} }
BMessage msgr; BMessage msgr;
if (target && target->IsValid()) { if (target && target->IsValid()) {
result = target->SendMessage(&get_target, &msgr); result = target->SendMessage(&get_target, &msgr);
if (result!=B_OK) return result; if (result!=B_OK)
return result;
result = msgr.FindMessenger ("result", target); result = msgr.FindMessenger ("result", target);
if (result!=B_OK) { if (result!=B_OK) {
if (!silent) fprintf(stderr, "Couldn't retrieve the BMessenger!\n"); if (!silent)
fprintf(stderr, "Couldn't retrieve the BMessenger!\n");
return result; return result;
} }
} }
if (!argv[*argx]) { if (!argv[*argx]) {
if (!silent) fprintf(stderr, "Syntax error - forgot \"do\"?\n"); if (!silent)
fprintf(stderr, "Syntax error - forgot \"do\"?\n");
return B_ERROR; return B_ERROR;
} }
} }
@@ -496,7 +511,8 @@ status_t Hey(BMessenger* target, char* argv[], int32* argx, int32 argc, BMessage
if(!found){ if(!found){
if(!silent) fprintf(stderr, "Bad verb (\"%s\")\n", argv[*argx]); if(!silent)
fprintf(stderr, "Bad verb (\"%s\")\n", argv[*argx]);
return -1; return -1;
} }
} }
@@ -509,14 +525,15 @@ status_t Hey(BMessenger* target, char* argv[], int32* argx, int32 argc, BMessage
// One exception: Single data item at end of line. // One exception: Single data item at end of line.
if (direct_what && *argx == argc - 1 && argv[*argx] != NULL) { if (direct_what && *argx == argc - 1 && argv[*argx] != NULL) {
add_data(&the_message, argv, argx); add_data(&the_message, argv, argx);
} } else {
else {
// parse the specifiers // parse the specifiers
if (the_message.what!=B_REFS_RECEIVED){ // LOAD has no specifier if (the_message.what!=B_REFS_RECEIVED){ // LOAD has no specifier
while((result=add_specifier(&the_message, argv, argx, argc))==B_OK){}; while ((result=add_specifier(&the_message, argv, argx, argc))==B_OK)
;
if (result!=B_ERROR){ // bad syntax if (result!=B_ERROR){ // bad syntax
if(!silent) fprintf(stderr, "Bad specifier syntax!\n"); if (!silent)
fprintf(stderr, "Bad specifier syntax!\n");
return result; return result;
} }
} }
@@ -530,9 +547,11 @@ status_t Hey(BMessenger* target, char* argv[], int32* argx, int32 argc, BMessage
result = add_data(&the_message, argv, argx); result = add_data(&the_message, argv, argx);
if (result!=B_OK) { if (result!=B_OK) {
if (result==B_FILE_NOT_FOUND){ if (result==B_FILE_NOT_FOUND){
if(!silent) fprintf(stderr, "File not found!\n"); if (!silent)
fprintf(stderr, "File not found!\n");
} else { } else {
if(!silent) fprintf(stderr, "Invalid 'to...' value format!\n"); if (!silent)
fprintf(stderr, "Invalid 'to...' value format!\n");
} }
return result; return result;
} }
@@ -558,7 +577,8 @@ status_t Hey(BMessenger* target, char* argv[], int32* argx, int32 argc, BMessage
// There can be a with <name>=<type>() [and <name>=<type> ...] // There can be a with <name>=<type>() [and <name>=<type> ...]
// I treat "and" just the same as "with", it's just to make the script syntax more English-like. // I treat "and" just the same as "with", it's just to make the script syntax more English-like.
status_t add_with(BMessage *to_message, char *argv[], int32 *argx, int32 argc) status_t
add_with(BMessage *to_message, char *argv[], int32 *argx, int32 argc)
{ {
status_t result = B_OK; status_t result = B_OK;
if (*argx < argc - 1 && argv[++(*argx)]!=NULL){ if (*argx < argc - 1 && argv[++(*argx)]!=NULL){
@@ -567,24 +587,23 @@ status_t add_with(BMessage *to_message, char *argv[], int32 *argx, int32 argc)
// printf ("\"with\" detected!\n"); // printf ("\"with\" detected!\n");
(*argx)++; (*argx)++;
bool done = false; bool done = false;
do do {
{
result=add_data(to_message, argv, argx); result=add_data(to_message, argv, argx);
if (result!=B_OK){ if (result!=B_OK){
if (result==B_FILE_NOT_FOUND){ if (result==B_FILE_NOT_FOUND){
if(!silent) fprintf(stderr, "File not found!\n"); if (!silent)
fprintf(stderr, "File not found!\n");
} else { } else {
if(!silent) fprintf(stderr, "Invalid 'with...' value format!\n"); if (!silent)
fprintf(stderr, "Invalid 'with...' value format!\n");
} }
return result; return result;
} }
(*argx)++; (*argx)++;
// printf ("argc = %d, argv[%d] = %s\n", argc, *argx, argv[*argx]); // printf ("argc = %d, argv[%d] = %s\n", argc, *argx, argv[*argx]);
if (*argx < argc - 1 && strcasecmp(argv[*argx], "and")==0) if (*argx < argc - 1 && strcasecmp(argv[*argx], "and")==0) {
{
(*argx)++; (*argx)++;
} } else
else
done = true; done = true;
} while (!done); } while (!done);
} }
@@ -595,12 +614,13 @@ status_t add_with(BMessage *to_message, char *argv[], int32 *argx, int32 argc)
// returns B_OK if successful // returns B_OK if successful
// B_ERROR if no more specifiers // B_ERROR if no more specifiers
// B_BAD_SCRIPT_SYNTAX if syntax error // B_BAD_SCRIPT_SYNTAX if syntax error
status_t add_specifier(BMessage *to_message, char *argv[], int32 *argx, int32 argc) status_t
add_specifier(BMessage *to_message, char *argv[], int32 *argx, int32 argc)
{ {
char *property=argv[*argx]; char *property=argv[*argx];
if(property==NULL) return B_ERROR; // no more specifiers if (property==NULL)
return B_ERROR; // no more specifiers
(*argx)++; (*argx)++;
@@ -620,13 +640,15 @@ status_t add_specifier(BMessage *to_message, char *argv[], int32 *argx, int32 ar
if (strcasecmp(property, "of")==0){ // skip "of", read real property if (strcasecmp(property, "of")==0){ // skip "of", read real property
property = argv[*argx]; property = argv[*argx];
if(property==NULL) return B_BAD_SCRIPT_SYNTAX; // bad syntax if (property==NULL)
return B_BAD_SCRIPT_SYNTAX; // bad syntax
(*argx)++; (*argx)++;
} }
if (strcasecmp(property, "the")==0){ // skip "the", read real property -- [email protected] 1999-11-03 if (strcasecmp(property, "the")==0){ // skip "the", read real property -- [email protected] 1999-11-03
property = argv[*argx]; property = argv[*argx];
if(property==NULL) return B_BAD_SCRIPT_SYNTAX; // bad syntax if (property==NULL)
return B_BAD_SCRIPT_SYNTAX; // bad syntax
(*argx)++; (*argx)++;
} }
@@ -705,20 +727,19 @@ status_t add_specifier(BMessage *to_message, char *argv[], int32 *argx, int32 ar
} }
if (index_spec){ if (index_spec){
if (reverse) if (reverse) {
{
// Copied from above -- [email protected] 1999-11-03 // Copied from above -- [email protected] 1999-11-03
BMessage revspec(B_REVERSE_INDEX_SPECIFIER); BMessage revspec(B_REVERSE_INDEX_SPECIFIER);
revspec.AddString("property", property); revspec.AddString("property", property);
revspec.AddInt32("index", atol(specifier+1)); revspec.AddInt32("index", atol(specifier+1));
to_message->AddSpecifier(&revspec); to_message->AddSpecifier(&revspec);
} }
else to_message->AddSpecifier(property, atol(specifier)); else
to_message->AddSpecifier(property, atol(specifier));
} else { } else {
// Allow any name by counting an initial " as a literal-string indicator // Allow any name by counting an initial " as a literal-string indicator
// -- [email protected] 1999-11-03 // -- [email protected] 1999-11-03
if(specifier[0]=='\"') if (specifier[0]=='\"') {
{
if (specifier[speclen-1]=='\"') if (specifier[speclen-1]=='\"')
specifier[speclen-1]='\0'; specifier[speclen-1]='\0';
++specifier; ++specifier;
@@ -726,18 +747,19 @@ status_t add_specifier(BMessage *to_message, char *argv[], int32 *argx, int32 ar
} }
to_message->AddSpecifier(property, specifier); to_message->AddSpecifier(property, specifier);
} }
} }
return B_OK; return B_OK;
} }
status_t add_data(BMessage *to_message, char *argv[], int32 *argx) status_t
add_data(BMessage *to_message, char *argv[], int32 *argx)
{ {
char *valuestring=argv[*argx]; char *valuestring=argv[*argx];
if(valuestring==NULL) return B_ERROR; if (valuestring==NULL)
return B_ERROR;
// try to interpret it as an integer or float // try to interpret it as an integer or float
bool contains_only_digits = true; bool contains_only_digits = true;
@@ -785,8 +807,7 @@ status_t add_data(BMessage *to_message, char *argv[], int32 *argx)
while (*++s && *s != '=') while (*++s && *s != '=')
// Look for a '=' character... // Look for a '=' character...
; ;
if (*s == '=') // We found a <name>= if (*s == '=') { // We found a <name>=
{
*s = 0; *s = 0;
strcpy (curname, valuestring); // Use the new <name> strcpy (curname, valuestring); // Use the new <name>
valuestring = s + 1; // Reposition the valuestring ptr. valuestring = s + 1; // Reposition the valuestring ptr.
@@ -906,14 +927,12 @@ status_t add_data(BMessage *to_message, char *argv[], int32 *argx)
} }
void
void print_message(BMessage *message) print_message(BMessage *message)
{ {
BList textlist; BList textlist;
add_message_contents(&textlist, message, 0); add_message_contents(&textlist, message, 0);
printf("BMessage(%s):\n", get_datatype_string(message->what)); printf("BMessage(%s):\n", get_datatype_string(message->what));
for (int32 i=0;i<textlist.CountItems();i++){ for (int32 i=0;i<textlist.CountItems();i++){
printf(" %s\n", (char*)textlist.ItemAt(i)); printf(" %s\n", (char*)textlist.ItemAt(i));
@@ -923,8 +942,8 @@ void print_message(BMessage *message)
} }
void
void add_message_contents(BList *textlist, BMessage *msg, int32 level) add_message_contents(BList *textlist, BMessage *msg, int32 level)
{ {
int32 count; int32 count;
int32 i, sizefound, j; int32 i, sizefound, j;
@@ -934,7 +953,6 @@ void add_message_contents(BList *textlist, BMessage *msg, int32 level)
BMessage a_message; BMessage a_message;
char *textline, *datatype, *content; char *textline, *datatype, *content;
// go though all message data // go though all message data
count = msg->CountNames(B_ANY_TYPE); count = msg->CountNames(B_ANY_TYPE);
for (i=0; i<count; i++){ for (i=0; i<count; i++){
@@ -954,21 +972,18 @@ void add_message_contents(BList *textlist, BMessage *msg, int32 level)
if (typefound==B_MESSAGE_TYPE){ if (typefound==B_MESSAGE_TYPE){
msg->FindMessage(namefound, j-1, &a_message); msg->FindMessage(namefound, j-1, &a_message);
add_message_contents(textlist, &a_message, level+1); add_message_contents(textlist, &a_message, level+1);
}else } else if (typefound==B_RAW_TYPE && strcmp(namefound, "_previous_")==0){
if(typefound==B_RAW_TYPE && strcmp(namefound, "_previous_")==0){
if (a_message.Unflatten((const char *)voidptr)==B_OK){ if (a_message.Unflatten((const char *)voidptr)==B_OK){
add_message_contents(textlist, &a_message, level+1); add_message_contents(textlist, &a_message, level+1);
} }
} }
} }
} }
} }
char *
char *get_datatype_string(int32 type) get_datatype_string(int32 type)
{ {
char *str = new char[128]; char *str = new char[128];
@@ -1087,7 +1102,8 @@ char *get_datatype_string(int32 type)
} }
char *format_data(int32 type, char *ptr, long size) char *
format_data(int32 type, char *ptr, long size)
{ {
char idtext[32]; char idtext[32];
char *str; char *str;
@@ -1114,7 +1130,6 @@ char *format_data(int32 type, char *ptr, long size)
int32 vinfo_index, vinfo_count; int32 vinfo_index, vinfo_count;
char *tempstr; char *tempstr;
if (size<=0L){ if (size<=0L){
str = new char; str = new char;
*str = 0; *str = 0;
@@ -1125,7 +1140,8 @@ char *format_data(int32 type, char *ptr, long size)
case B_MIME_TYPE: case B_MIME_TYPE:
case B_ASCII_TYPE: case B_ASCII_TYPE:
case B_STRING_TYPE: case B_STRING_TYPE:
if(size>512) size=512; if (size>512)
size=512;
str = new char[size+4]; str = new char[size+4];
*str='\"'; *str='\"';
strncpy(str+1, ptr, size); strncpy(str+1, ptr, size);
@@ -1393,8 +1409,8 @@ char *format_data(int32 type, char *ptr, long size)
} }
char *
char *id_to_string(long ID, char *here) id_to_string(long ID, char *here)
{ {
uint8 digit0=(ID>>24)&255; uint8 digit0=(ID>>24)&255;
uint8 digit1=(ID>>16)&255; uint8 digit1=(ID>>16)&255;
@@ -1406,21 +1422,25 @@ char *id_to_string(long ID, char *here)
if (digit1==0){ if (digit1==0){
if (digit2==0) { if (digit2==0) {
// 1 digits // 1 digits
if(is_valid_char(digit3) ) itsvalid=TRUE; if (is_valid_char(digit3) )
itsvalid=TRUE;
sprintf(here, "'%c'", digit3); sprintf(here, "'%c'", digit3);
} else { } else {
// 2 digits // 2 digits
if(is_valid_char(digit2) && is_valid_char(digit3) ) itsvalid=TRUE; if (is_valid_char(digit2) && is_valid_char(digit3) )
itsvalid=TRUE;
sprintf(here, "'%c%c'", digit2, digit3); sprintf(here, "'%c%c'", digit2, digit3);
} }
} else { } else {
// 3 digits // 3 digits
if(is_valid_char(digit1) && is_valid_char(digit2) && is_valid_char(digit3) ) itsvalid=TRUE; if (is_valid_char(digit1) && is_valid_char(digit2) && is_valid_char(digit3) )
itsvalid=TRUE;
sprintf(here, "'%c%c%c'", digit1, digit2, digit3); sprintf(here, "'%c%c%c'", digit1, digit2, digit3);
} }
} else { } else {
// 4 digits // 4 digits
if(is_valid_char(digit0) && is_valid_char(digit1) && is_valid_char(digit2) && is_valid_char(digit3) ) itsvalid=TRUE; if (is_valid_char(digit0) && is_valid_char(digit1) && is_valid_char(digit2) && is_valid_char(digit3) )
itsvalid=TRUE;
sprintf(here, "'%c%c%c%c'", digit0, digit1, digit2, digit3); sprintf(here, "'%c%c%c%c'", digit0, digit1, digit2, digit3);
} }
@@ -1432,7 +1452,8 @@ char *id_to_string(long ID, char *here)
} }
bool is_valid_char(uint8 c) bool
is_valid_char(uint8 c)
{ {
return (c>=32 && c<128); return (c>=32 && c<128);
} }