diff --git a/src/bin/hey.cpp b/src/bin/hey.cpp index d94b43fef2..b152ca0839 100644 --- a/src/bin/hey.cpp +++ b/src/bin/hey.cpp @@ -124,7 +124,7 @@ void print_message(BMessage *message); char *id_to_string(long ID, char *here); bool is_valid_char(uint8 c); -const char VERSION[]="v1.2.8"; +const char VERSION[] = "v1.2.8"; #define DEBUG_HEY 0 // 1: prints the script message to be sent to the target application, 0: prints only the reply @@ -138,41 +138,45 @@ bool silent; // flag for stdout mode 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(!silent) fprintf(stderr, "Cannot find the application (%s)\n", argv[argapp]); - return -1; + if (!the_application.IsValid()) { + if (!silent) + fprintf(stderr, "Cannot find the application (%s)\n", argv[argapp]); + return B_ERROR; } if (argc < 3) { - if(!silent) fprintf(stderr, "Cannot find the verb!\n"); - return -1; + if (!silent) + fprintf(stderr, "Cannot find the verb!\n"); + return B_ERROR; } BMessage the_reply; 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); - if (err!=B_OK) { - if(!silent) fprintf(stderr, "Error when sending message to %s!\n", argv[argapp]); - return -1; + if (err != B_OK) { + if (!silent) + fprintf(stderr, "Error when sending message to %s!\n", argv[argapp]); + return B_ERROR; } else { - 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(!silent) printf("%s (error 0x%8lX)\n", the_reply.FindString("message"), the_reply.FindInt32("error")); - }else{ - if(!silent) printf("error 0x%8lX\n", the_reply.FindInt32("error")); + 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 (!silent) + printf("%s (error 0x%8lX)\n", the_reply.FindString("message"), the_reply.FindInt32("error")); + } else { + if (!silent) + printf("error 0x%8lX\n", the_reply.FindInt32("error")); } return 1; - }else{ - if(!silent){ - if(output){ + } else { + if (!silent){ + if (output){ type_code tc; - if(the_reply.GetInfo("result", &tc) == B_OK){ + if (the_reply.GetInfo("result", &tc) == B_OK){ if(tc==B_INT8_TYPE){ int8 v; the_reply.FindInt8("result", &v); @@ -217,8 +221,7 @@ int parse(BMessenger& the_application, int argc, char *argv[], int32 argapp) printf("Unsupported type\n"); } } - } - else { + } else { printf("Reply "); print_message(&the_reply); 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"); @@ -252,17 +256,16 @@ int main(int argc, char *argv[]) } int32 argapp = 1; - silent=false; - output=false; + silent = false; + output = false; // Updated option mechanism --SS - for (int i = 0; i < argc; i++) - { - if(strcmp(argv[i], "-s")==0 || strcmp(argv[i], "-S")==0){ - silent=true; + for (int i = 0; i < argc; i++) { + if (strcmp(argv[i], "-s")==0 || strcmp(argv[i], "-S")==0){ + silent = true; argapp++; } - if(strcmp(argv[i], "-o")==0 || strcmp(argv[i], "-O")==0){ + if (strcmp(argv[i], "-o")==0 || strcmp(argv[i], "-O")==0){ output=true; argapp++; } @@ -275,15 +278,15 @@ int main(int argc, char *argv[]) app_info appinfo; be_roster->GetAppList(&team_list); - for(int32 i=0;iGetRunningAppInfo(teamid, &appinfo); - if(strcmp(appinfo.signature, argv[argapp])==0){ + if (strcmp(appinfo.signature, argv[argapp])==0){ the_application=BMessenger(appinfo.signature); if (!parse(the_application, argc, argv, argapp)) return 0; - }else{ - if(strcmp(appinfo.ref.name, argv[argapp])==0){ + } else { + if (strcmp(appinfo.ref.name, argv[argapp])==0){ the_application=BMessenger(0, teamid); if (!parse(the_application, argc, argv, argapp)) return 0; @@ -295,39 +298,41 @@ int main(int argc, char *argv[]) } -int32 HeyInterpreterThreadHook(void* arg) +int32 +HeyInterpreterThreadHook(void* arg) { - if (arg) { - BMessage environment(*(BMessage*) arg); - char* prompt = "Hey"; - if (environment.HasString("prompt")) environment.FindString("prompt", (const char **)&prompt); - printf("%s> ", prompt); - - BMessenger target; - if (environment.HasMessenger("Target")) environment.FindMessenger("Target", &target); - - char command[1024]; - status_t err; - BMessage reply; - while (gets(command)) { - reply.MakeEmpty(); - err = Hey(&target, command, &reply); - if (!err) { - print_message(&reply); - } else { - printf("Error!\n"); - } - printf("%s> ", prompt); - } - - return 0; - - } else { + if (!arg) return 1; + + BMessage environment(*(BMessage*) arg); + char* prompt = "Hey"; + if (environment.HasString("prompt")) + environment.FindString("prompt", (const char **)&prompt); + printf("%s> ", prompt); + + BMessenger target; + if (environment.HasMessenger("Target")) + environment.FindMessenger("Target", &target); + + char command[1024]; + status_t err; + BMessage reply; + while (gets(command)) { + reply.MakeEmpty(); + err = Hey(&target, command, &reply); + if (!err) { + print_message(&reply); + } else { + printf("Error!\n"); + } + printf("%s> ", prompt); } + + return 0; } -status_t Hey(BMessenger* target, const char* arg, BMessage* reply) +status_t +Hey(BMessenger* target, const char* arg, BMessage* reply) { vector argv; // number of tokens is now limited only by memory -- pfolk@uni.uiuc.edu 1999-11-03 char* tokens = new char[strlen(arg)*2]; @@ -337,7 +342,8 @@ status_t Hey(BMessenger* target, const char* arg, BMessage* reply) bool inquotes = false; 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 (tokenNdex!=0) { // close off currentToken token currentToken[tokenNdex] = 0; @@ -368,7 +374,9 @@ status_t Hey(BMessenger* target, const char* arg, BMessage* reply) return ret; } -bool isSpace(char c) + +bool +isSpace(char c) { switch (c) { case ' ': @@ -380,159 +388,170 @@ 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; BMessage the_message; - if(strcasecmp(argv[*argx], "let")==0){ // added "let" -- sander@adamation.com 31may2000 + if (strcasecmp(argv[*argx], "let")==0) { // added "let" -- sander@adamation.com 31may2000 BMessage get_target (B_GET_PROPERTY); get_target.AddSpecifier ("Messenger"); // parse the specifiers (*argx)++; 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(!silent) fprintf(stderr, "Bad specifier syntax!\n"); + if (result!=B_ERROR){ // bad syntax + if (!silent) + fprintf(stderr, "Bad specifier syntax!\n"); return result; } BMessage msgr; if (target && target->IsValid()) { result = target->SendMessage(&get_target, &msgr); - if (result!=B_OK) return result; + if (result!=B_OK) + return result; result = msgr.FindMessenger ("result", target); 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; } } if (!argv[*argx]) { - if (!silent) fprintf(stderr, "Syntax error - forgot \"do\"?\n"); + if (!silent) + fprintf(stderr, "Syntax error - forgot \"do\"?\n"); return B_ERROR; } } - if(strcasecmp(argv[*argx], "do")==0){ // added "do" -- pfolk@uni.uiuc.edu 1999-11-03 + if (strcasecmp(argv[*argx], "do")==0){ // added "do" -- pfolk@uni.uiuc.edu 1999-11-03 the_message.what=B_EXECUTE_PROPERTY; - }else if(strcasecmp(argv[*argx], "get")==0){ + } else if (strcasecmp(argv[*argx], "get")==0){ the_message.what=B_GET_PROPERTY; - }else if(strcasecmp(argv[*argx], "set")==0){ + } else if (strcasecmp(argv[*argx], "set")==0){ the_message.what=B_SET_PROPERTY; - }else if(strcasecmp(argv[*argx], "create")==0){ + } else if (strcasecmp(argv[*argx], "create")==0){ the_message.what=B_CREATE_PROPERTY; - }else if(strcasecmp(argv[*argx], "delete")==0){ + } else if (strcasecmp(argv[*argx], "delete")==0){ the_message.what=B_DELETE_PROPERTY; - }else if(strcasecmp(argv[*argx], "quit")==0){ + } else if (strcasecmp(argv[*argx], "quit")==0){ the_message.what=B_QUIT_REQUESTED; - }else if(strcasecmp(argv[*argx], "save")==0){ + } else if (strcasecmp(argv[*argx], "save")==0){ the_message.what=B_SAVE_REQUESTED; - }else if(strcasecmp(argv[*argx], "load")==0){ + } else if (strcasecmp(argv[*argx], "load")==0){ the_message.what=B_REFS_RECEIVED; - }else if(strcasecmp(argv[*argx], "count")==0){ + } else if(strcasecmp(argv[*argx], "count")==0){ the_message.what=B_COUNT_PROPERTIES; - }else if(strcasecmp(argv[*argx], "getsuites")==0){ + } else if(strcasecmp(argv[*argx], "getsuites")==0){ the_message.what=B_GET_SUPPORTED_SUITES; - }else{ + } else { switch(strlen(argv[*argx])){ // can be a message constant if 1,2,3 or 4 chars case 1: - the_message.what=(int32)argv[*argx][0]; - break; + the_message.what=(int32)argv[*argx][0]; + break; case 2: - the_message.what=(((int32)argv[*argx][0])<<8)|(((int32)argv[*argx][1])); - break; + the_message.what=(((int32)argv[*argx][0])<<8)|(((int32)argv[*argx][1])); + break; case 3: - the_message.what=(((int32)argv[*argx][0])<<16)|(((int32)argv[*argx][1])<<8)|(((int32)argv[*argx][2])); - break; + the_message.what=(((int32)argv[*argx][0])<<16)|(((int32)argv[*argx][1])<<8)|(((int32)argv[*argx][2])); + break; case 4: - the_message.what=(((int32)argv[*argx][0])<<24)|(((int32)argv[*argx][1])<<16)|(((int32)argv[*argx][2])<<8)|(((int32)argv[*argx][3])); - break; + the_message.what=(((int32)argv[*argx][0])<<24)|(((int32)argv[*argx][1])<<16)|(((int32)argv[*argx][2])<<8)|(((int32)argv[*argx][3])); + break; default: - // maybe this is a user defined command, ask for the supported suites - bool found=false; - if (target && target->IsValid()) { - BMessage rply; - if(target->SendMessage(&BMessage(B_GET_SUPPORTED_SUITES), &rply)==B_OK){ - // if all goes well, rply contains all kinds of property infos - int32 j=0; - void *voidptr; - int32 sizefound; - BPropertyInfo propinfo; - const value_info *vinfo; - int32 vinfo_index, vinfo_count; - -// const char *str; -// while (rply.FindString("suites", j++, &str) == B_OK) -// printf ("Suite %ld: %s\n", j, str); -// -// j = 0; - while(rply.FindData("messages", B_PROPERTY_INFO_TYPE, j++, (const void **)&voidptr, &sizefound)==B_OK && !found){ - if(propinfo.Unflatten(B_PROPERTY_INFO_TYPE, (const void *)voidptr, sizefound)==B_OK){ - vinfo=propinfo.Values(); - vinfo_index=0; - vinfo_count=propinfo.CountValues(); + // maybe this is a user defined command, ask for the supported suites + bool found=false; + if (target && target->IsValid()) { + BMessage rply; + if(target->SendMessage(&BMessage(B_GET_SUPPORTED_SUITES), &rply)==B_OK){ + // if all goes well, rply contains all kinds of property infos + int32 j=0; + void *voidptr; + int32 sizefound; + BPropertyInfo propinfo; + const value_info *vinfo; + int32 vinfo_index, vinfo_count; + +// const char *str; +// while (rply.FindString("suites", j++, &str) == B_OK) +// printf ("Suite %ld: %s\n", j, str); +// +// j = 0; + while(rply.FindData("messages", B_PROPERTY_INFO_TYPE, j++, (const void **)&voidptr, &sizefound)==B_OK && !found){ + if(propinfo.Unflatten(B_PROPERTY_INFO_TYPE, (const void *)voidptr, sizefound)==B_OK){ + vinfo=propinfo.Values(); + vinfo_index=0; + vinfo_count=propinfo.CountValues(); #if TEST_VALUEINFO>0 - value_info vinfo[10]={ {"Backup", 'back', B_COMMAND_KIND, "This command backs up your hard drive."}, - {"Abort", 'abor', B_COMMAND_KIND, "Stops the current operation..."}, - {"Type Code", 'type', B_TYPE_CODE_KIND, "Type code info..."} - }; - vinfo_count=3; + value_info vinfo[10]={ {"Backup", 'back', B_COMMAND_KIND, "This command backs up your hard drive."}, + {"Abort", 'abor', B_COMMAND_KIND, "Stops the current operation..."}, + {"Type Code", 'type', B_TYPE_CODE_KIND, "Type code info..."} + }; + vinfo_count=3; #endif - while(vinfo_index0 - printf("FOUND COMMAND \"%s\" = %lX\n", vinfo[vinfo_index].name, the_message.what); + printf("FOUND COMMAND \"%s\" = %lX\n", vinfo[vinfo_index].name, the_message.what); #endif - break; - } - vinfo_index++; - } - + break; } + vinfo_index++; } + } } + } + } - if(!found){ - if(!silent) fprintf(stderr, "Bad verb (\"%s\")\n", argv[*argx]); - return -1; - } + if(!found){ + if(!silent) + fprintf(stderr, "Bad verb (\"%s\")\n", argv[*argx]); + return -1; + } } direct_what = true; } - status_t result=B_OK; + status_t result = B_OK; (*argx)++; // One exception: Single data item at end of line. if (direct_what && *argx == argc - 1 && argv[*argx] != NULL) { add_data(&the_message, argv, argx); - } - else { + } else { // parse the specifiers - if(the_message.what!=B_REFS_RECEIVED){ // LOAD has no specifier - while((result=add_specifier(&the_message, argv, argx, argc))==B_OK){}; + if (the_message.what!=B_REFS_RECEIVED){ // LOAD has no specifier + while ((result=add_specifier(&the_message, argv, argx, argc))==B_OK) + ; - if(result!=B_ERROR){ // bad syntax - if(!silent) fprintf(stderr, "Bad specifier syntax!\n"); + if (result!=B_ERROR){ // bad syntax + if (!silent) + fprintf(stderr, "Bad specifier syntax!\n"); return result; } } } // if verb is SET or LOAD, there should be a to - if((the_message.what==B_SET_PROPERTY || the_message.what==B_REFS_RECEIVED) && argv[*argx]!=NULL){ - if(strcasecmp(argv[*argx], "to")==0){ + if ((the_message.what==B_SET_PROPERTY || the_message.what==B_REFS_RECEIVED) && argv[*argx]!=NULL){ + if (strcasecmp(argv[*argx], "to")==0) { (*argx)++; } - result=add_data(&the_message, argv, argx); - if(result!=B_OK){ - if(result==B_FILE_NOT_FOUND){ - if(!silent) fprintf(stderr, "File not found!\n"); - }else{ - if(!silent) fprintf(stderr, "Invalid 'to...' value format!\n"); + result = add_data(&the_message, argv, argx); + if (result!=B_OK) { + if (result==B_FILE_NOT_FOUND){ + if (!silent) + fprintf(stderr, "File not found!\n"); + } else { + if (!silent) + fprintf(stderr, "Invalid 'to...' value format!\n"); } return result; } @@ -558,33 +577,33 @@ status_t Hey(BMessenger* target, char* argv[], int32* argx, int32 argc, BMessage // There can be a with =() [and = ...] // 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; - if(*argx < argc - 1 && argv[++(*argx)]!=NULL){ + status_t result = B_OK; + if (*argx < argc - 1 && argv[++(*argx)]!=NULL){ // printf ("argv[%ld] = %s\n", *argx, argv[*argx]); - if(strcasecmp(argv[*argx], "with")==0){ + if (strcasecmp(argv[*argx], "with")==0){ // printf ("\"with\" detected!\n"); (*argx)++; bool done = false; - do - { + do { result=add_data(to_message, argv, argx); - if(result!=B_OK){ - if(result==B_FILE_NOT_FOUND){ - if(!silent) fprintf(stderr, "File not found!\n"); - }else{ - if(!silent) fprintf(stderr, "Invalid 'with...' value format!\n"); + if (result!=B_OK){ + if (result==B_FILE_NOT_FOUND){ + if (!silent) + fprintf(stderr, "File not found!\n"); + } else { + if (!silent) + fprintf(stderr, "Invalid 'with...' value format!\n"); } return result; } (*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)++; - } - else + } else done = true; } while (!done); } @@ -595,130 +614,132 @@ status_t add_with(BMessage *to_message, char *argv[], int32 *argx, int32 argc) // returns B_OK if successful // B_ERROR if no more specifiers // 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]; - if(property==NULL) return B_ERROR; // no more specifiers + if (property==NULL) + return B_ERROR; // no more specifiers (*argx)++; - if(strcasecmp(property, "do")==0){ // Part of the "hey App let Specifier do Verb". + if (strcasecmp(property, "do")==0){ // Part of the "hey App let Specifier do Verb". return B_ERROR; // no more specifiers } - if(strcasecmp(property, "to")==0){ // it is the 'to' string!!! + if (strcasecmp(property, "to")==0){ // it is the 'to' string!!! return B_ERROR; // no more specifiers } - if(strcasecmp(property, "with")==0){ // it is the 'with' string!!! + if (strcasecmp(property, "with")==0){ // it is the 'with' string!!! *argx -= 2; add_with (to_message, argv, argx, argc); return B_ERROR; // no more specifiers } - if(strcasecmp(property, "of")==0){ // skip "of", read real property - property=argv[*argx]; - if(property==NULL) return B_BAD_SCRIPT_SYNTAX; // bad syntax + if (strcasecmp(property, "of")==0){ // skip "of", read real property + property = argv[*argx]; + if (property==NULL) + return B_BAD_SCRIPT_SYNTAX; // bad syntax (*argx)++; } - if(strcasecmp(property, "the")==0){ // skip "the", read real property -- pfolk@uni.uiuc.edu 1999-11-03 - property=argv[*argx]; - if(property==NULL) return B_BAD_SCRIPT_SYNTAX; // bad syntax + if (strcasecmp(property, "the")==0){ // skip "the", read real property -- pfolk@uni.uiuc.edu 1999-11-03 + property = argv[*argx]; + if (property==NULL) + return B_BAD_SCRIPT_SYNTAX; // bad syntax (*argx)++; } // decide the specifier - char *specifier=NULL; - if(to_message->what==B_CREATE_PROPERTY) // create is always direct. without this, a "with" would be taken as a specifier + char *specifier = NULL; + if (to_message->what == B_CREATE_PROPERTY) // create is always direct. without this, a "with" would be taken as a specifier (*argx)--; else - specifier=argv[*argx]; - if(specifier==NULL){ // direct specifier + specifier = argv[*argx]; + if (specifier == NULL){ // direct specifier to_message->AddSpecifier(property); return B_ERROR; // no more specifiers } (*argx)++; - if(strcasecmp(specifier, "of")==0){ // direct specifier + if (strcasecmp(specifier, "of")==0){ // direct specifier to_message->AddSpecifier(property); return B_OK; } - if(strcasecmp(specifier, "to")==0){ // direct specifier + if (strcasecmp(specifier, "to")==0){ // direct specifier to_message->AddSpecifier(property); return B_ERROR; // no more specifiers } - if(specifier[0]=='['){ // index, reverse index or range + if (specifier[0]=='['){ // index, reverse index or range char *end; int32 ix1, ix2; - if(specifier[1]=='-'){ // reverse index - ix1=strtoul(specifier+2, &end, 10); + if (specifier[1]=='-'){ // reverse index + ix1 = strtoul(specifier+2, &end, 10); BMessage revspec(B_REVERSE_INDEX_SPECIFIER); revspec.AddString("property", property); revspec.AddInt32("index", ix1); to_message->AddSpecifier(&revspec); - }else{ // index or range - ix1=strtoul(specifier+1, &end, 10); - if(end[0]==']'){ // it was an index + } else { // index or range + ix1 = strtoul(specifier+1, &end, 10); + if (end[0]==']'){ // it was an index to_message->AddSpecifier(property, ix1); return B_OK; - }else{ + } else { specifier=argv[*argx]; - if(specifier==NULL){ + if (specifier==NULL){ // I was wrong, it was just an index to_message->AddSpecifier(property, ix1); return B_OK; } (*argx)++; - if(strcasecmp(specifier, "to")==0){ - specifier=argv[*argx]; - if(specifier==NULL){ + if (strcasecmp(specifier, "to")==0){ + specifier = argv[*argx]; + if (specifier==NULL){ return B_BAD_SCRIPT_SYNTAX; // wrong syntax } (*argx)++; - ix2=strtoul(specifier, &end, 10); + ix2 = strtoul(specifier, &end, 10); to_message->AddSpecifier(property, ix1, ix2-ix1>0 ? ix2-ix1 : 1); return B_OK; - }else{ + } else { return B_BAD_SCRIPT_SYNTAX; // wrong syntax } } } - }else{ // name specifier + } else { // name specifier // if it contains only digits, it will be an index... bool index_spec=true; bool reverse = specifier[0]=='-'; // accept bare reverse-index-specs -- pfolk@uni.uiuc.edu 1999-11-03 size_t speclen = strlen(specifier); for (int32 i=(reverse?1:0); i<(int32)speclen; ++i){ - if(specifier[i]<'0' || specifier[i]>'9'){ + if (specifier[i]<'0' || specifier[i]>'9'){ index_spec=false; break; } } - if(index_spec){ - if (reverse) - { + if (index_spec){ + if (reverse) { // Copied from above -- pfolk@uni.uiuc.edu 1999-11-03 BMessage revspec(B_REVERSE_INDEX_SPECIFIER); revspec.AddString("property", property); revspec.AddInt32("index", atol(specifier+1)); to_message->AddSpecifier(&revspec); } - else to_message->AddSpecifier(property, atol(specifier)); - }else{ + else + to_message->AddSpecifier(property, atol(specifier)); + } else { // Allow any name by counting an initial " as a literal-string indicator // -- pfolk@uni.uiuc.edu 1999-11-03 - if(specifier[0]=='\"') - { + if (specifier[0]=='\"') { if (specifier[speclen-1]=='\"') specifier[speclen-1]='\0'; ++specifier; @@ -726,50 +747,51 @@ status_t add_specifier(BMessage *to_message, char *argv[], int32 *argx, int32 ar } to_message->AddSpecifier(property, specifier); } - } 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]; - if(valuestring==NULL) return B_ERROR; + if (valuestring==NULL) + return B_ERROR; // try to interpret it as an integer or float - bool contains_only_digits=true; - bool is_floating_point=false; - for(int32 i=0;i<(int32)strlen(valuestring);i++){ - if(i!=0 || valuestring[i]!='-') { - if(valuestring[i]<'0' || valuestring[i]>'9'){ - if(valuestring[i]=='.'){ - is_floating_point=true; - }else{ - contains_only_digits=false; + bool contains_only_digits = true; + bool is_floating_point = false; + for (int32 i=0;i<(int32)strlen(valuestring);i++){ + if (i!=0 || valuestring[i]!='-') { + if (valuestring[i]<'0' || valuestring[i]>'9'){ + if (valuestring[i]=='.'){ + is_floating_point = true; + } else { + contains_only_digits = false; break; } } } } //printf("%d %d\n", contains_only_digits,is_floating_point); - if(contains_only_digits){ - if(is_floating_point){ + if (contains_only_digits){ + if (is_floating_point){ to_message->AddFloat("data", atof(valuestring)); return B_OK; - }else{ + } else { to_message->AddInt32("data", atol(valuestring)); return B_OK; } } // if true or false, it is bool - if(strcasecmp(valuestring, "true")==0){ + if (strcasecmp(valuestring, "true")==0){ to_message->AddBool("data", true); return B_OK; - }else if(strcasecmp(valuestring, "false")==0){ + } else if (strcasecmp(valuestring, "false")==0){ to_message->AddBool("data", false); return B_OK; } @@ -785,118 +807,117 @@ status_t add_data(BMessage *to_message, char *argv[], int32 *argx) while (*++s && *s != '=') // Look for a '=' character... ; - if (*s == '=') // We found a = - { + if (*s == '=') { // We found a = *s = 0; strcpy (curname, valuestring); // Use the new valuestring = s + 1; // Reposition the valuestring ptr. } // must begin with a type( value ) - if(strncasecmp(valuestring, "int8", strlen("int8"))==0){ + if (strncasecmp(valuestring, "int8", strlen("int8"))==0){ to_message->AddInt8(curname, atol(valuestring+strlen("int8("))); return B_OK; - }else if(strncasecmp(valuestring, "int16", strlen("int16"))==0){ + } else if (strncasecmp(valuestring, "int16", strlen("int16"))==0){ to_message->AddInt16(curname, atol(valuestring+strlen("int16("))); return B_OK; - }else if(strncasecmp(valuestring, "int32", strlen("int32"))==0){ + } else if (strncasecmp(valuestring, "int32", strlen("int32"))==0){ to_message->AddInt32(curname, atol(valuestring+strlen("int32("))); return B_OK; - }else if(strncasecmp(valuestring, "int64", strlen("int64"))==0){ + } else if (strncasecmp(valuestring, "int64", strlen("int64"))==0){ to_message->AddInt64(curname, atol(valuestring+strlen("int64("))); return B_OK; - }else if(strncasecmp(valuestring, "bool", strlen("bool"))==0){ - if(strncasecmp(valuestring+strlen("bool("), "true", 4)==0){ + } else if (strncasecmp(valuestring, "bool", strlen("bool"))==0){ + if (strncasecmp(valuestring+strlen("bool("), "true", 4)==0){ to_message->AddBool(curname, true); - }else if(strncasecmp(valuestring+strlen("bool("), "false", 5)==0){ + } else if (strncasecmp(valuestring+strlen("bool("), "false", 5)==0){ to_message->AddBool(curname, false); - }else{ + } else { to_message->AddBool(curname, atol(valuestring+strlen("bool("))==0 ? false : true); } return B_OK; - }else if(strncasecmp(valuestring, "float", strlen("float"))==0){ + } else if (strncasecmp(valuestring, "float", strlen("float"))==0){ to_message->AddFloat(curname, atof(valuestring+strlen("float("))); return B_OK; - }else if(strncasecmp(valuestring, "double", strlen("double"))==0){ + } else if (strncasecmp(valuestring, "double", strlen("double"))==0){ to_message->AddDouble(curname, atof(valuestring+strlen("double("))); return B_OK; - }else if(strncasecmp(valuestring, "BPoint", strlen("BPoint"))==0){ + } else if (strncasecmp(valuestring, "BPoint", strlen("BPoint"))==0){ float x,y; - x=atof(valuestring+strlen("BPoint(")); - if(strchr(valuestring, ',')){ - y=atof(strchr(valuestring, ',')+1); - }else if(strchr(valuestring, ' ')){ - y=atof(strchr(valuestring, ' ')+1); - }else{ // bad syntax + x = atof(valuestring+strlen("BPoint(")); + if (strchr(valuestring, ',')){ + y = atof(strchr(valuestring, ',')+1); + } else if (strchr(valuestring, ' ')){ + y = atof(strchr(valuestring, ' ')+1); + } else { // bad syntax y=0.0f; } to_message->AddPoint(curname, BPoint(x,y)); return B_OK; - }else if(strncasecmp(valuestring, "BRect", strlen("BRect"))==0){ + } else if (strncasecmp(valuestring, "BRect", strlen("BRect"))==0){ float l=0.0f, t=0.0f, r=0.0f, b=0.0f; char *ptr; - l=atof(valuestring+strlen("BRect(")); - ptr=strchr(valuestring, ','); - if(ptr){ - t=atof(ptr+1); - ptr=strchr(ptr+1, ','); - if(ptr){ - r=atof(ptr+1); - ptr=strchr(ptr+1, ','); - if(ptr){ - b=atof(ptr+1); + l = atof(valuestring+strlen("BRect(")); + ptr = strchr(valuestring, ','); + if (ptr){ + t = atof(ptr+1); + ptr = strchr(ptr+1, ','); + if (ptr){ + r = atof(ptr+1); + ptr = strchr(ptr+1, ','); + if (ptr){ + b = atof(ptr+1); } } } to_message->AddRect(curname, BRect(l,t,r,b)); return B_OK; - }else if(strncasecmp(valuestring, "rgb_color", strlen("rgb_color"))==0){ + } else if (strncasecmp(valuestring, "rgb_color", strlen("rgb_color"))==0){ rgb_color clr; char *ptr; - clr.red=atol(valuestring+strlen("rgb_color(")); - ptr=strchr(valuestring, ','); - if(ptr){ - clr.green=atol(ptr+1); - ptr=strchr(ptr+1, ','); - if(ptr){ - clr.blue=atol(ptr+1); - ptr=strchr(ptr+1, ','); - if(ptr){ - clr.alpha=atol(ptr+1); + clr.red = atol(valuestring+strlen("rgb_color(")); + ptr = strchr(valuestring, ','); + if (ptr){ + clr.green = atol(ptr+1); + ptr = strchr(ptr+1, ','); + if (ptr){ + clr.blue = atol(ptr+1); + ptr = strchr(ptr+1, ','); + if (ptr){ + clr.alpha = atol(ptr+1); } } } to_message->AddData(curname, B_RGB_COLOR_TYPE, &clr, sizeof(rgb_color)); return B_OK; - }else if(strncasecmp(valuestring, "file", strlen("file"))==0){ + } else if (strncasecmp(valuestring, "file", strlen("file"))==0){ entry_ref file_ref; // remove the last ] or ) - if(valuestring[strlen(valuestring)-1]==')' || valuestring[strlen(valuestring)-1]==']'){ - valuestring[strlen(valuestring)-1]=0; + if (valuestring[strlen(valuestring)-1]==')' || valuestring[strlen(valuestring)-1]==']'){ + valuestring[strlen(valuestring)-1] = 0; } - if(get_ref_for_path(valuestring+5, &file_ref)!=B_OK){ + if (get_ref_for_path(valuestring+5, &file_ref)!=B_OK){ return B_FILE_NOT_FOUND; } // check if the ref is valid BEntry entry; - if(entry.SetTo(&file_ref)!=B_OK) return B_FILE_NOT_FOUND; + if (entry.SetTo(&file_ref)!=B_OK) return B_FILE_NOT_FOUND; //if(!entry.Exists()) return B_FILE_NOT_FOUND; // add both ways, refsreceived needs it as "refs" while scripting needs "data" to_message->AddRef("refs", &file_ref); to_message->AddRef(curname, &file_ref); return B_OK; - }else{ // it is string + } else { // it is string // does it begin with a quote? - if(valuestring[0]=='\"'){ - if(valuestring[strlen(valuestring)-1]=='\"') valuestring[strlen(valuestring)-1]=0; + if (valuestring[0]=='\"'){ + if (valuestring[strlen(valuestring)-1]=='\"') valuestring[strlen(valuestring)-1]=0; to_message->AddString(curname, valuestring+1); - }else{ + } else { to_message->AddString(curname, valuestring); } return B_OK; @@ -906,16 +927,14 @@ status_t add_data(BMessage *to_message, char *argv[], int32 *argx) } - -void print_message(BMessage *message) +void +print_message(BMessage *message) { - BList textlist; add_message_contents(&textlist, message, 0); - printf("BMessage(%s):\n", get_datatype_string(message->what)); - for(int32 i=0;iCountNames(B_ANY_TYPE); - for(i=0;iCountNames(B_ANY_TYPE); + for (i=0; iGetInfo(B_ANY_TYPE, i, &namefound, &typefound); - j=0; + j = 0; - while(msg->FindData(namefound, typefound, j++, (const void **)&voidptr, &sizefound)==B_OK){ - datatype=get_datatype_string(typefound); - content=format_data(typefound, (char*)voidptr, sizefound); - textline=(char*)malloc(20+level*4+strlen(namefound)+strlen(datatype)+strlen(content)); + while (msg->FindData(namefound, typefound, j++, (const void **)&voidptr, &sizefound)==B_OK){ + datatype = get_datatype_string(typefound); + content = format_data(typefound, (char*)voidptr, sizefound); + textline = (char*)malloc(20+level*4+strlen(namefound)+strlen(datatype)+strlen(content)); memset(textline, 32, 20+level*4); sprintf(textline+level*4, "\"%s\" (%s) : %s", namefound, datatype, content); textlist->AddItem(textline); delete [] datatype; delete [] content; - if(typefound==B_MESSAGE_TYPE){ + if (typefound==B_MESSAGE_TYPE){ msg->FindMessage(namefound, j-1, &a_message); add_message_contents(textlist, &a_message, level+1); - }else - if(typefound==B_RAW_TYPE && strcmp(namefound, "_previous_")==0){ - if(a_message.Unflatten((const char *)voidptr)==B_OK){ + } else if (typefound==B_RAW_TYPE && strcmp(namefound, "_previous_")==0){ + if (a_message.Unflatten((const char *)voidptr)==B_OK){ add_message_contents(textlist, &a_message, level+1); } } } - } - } - -char *get_datatype_string(int32 type) +char * +get_datatype_string(int32 type) { - char *str=new char[128]; + char *str = new char[128]; - switch(type){ + switch (type){ case B_ANY_TYPE: strcpy(str, "B_ANY_TYPE"); break; case B_ASCII_TYPE: strcpy(str, "B_ASCII_TYPE"); break; case B_BOOL_TYPE: strcpy(str, "B_BOOL_TYPE"); break; @@ -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 *str; @@ -1114,184 +1130,184 @@ char *format_data(int32 type, char *ptr, long size) int32 vinfo_index, vinfo_count; char *tempstr; - - if(size<=0L){ - str=new char; - *str=0; + if (size<=0L){ + str = new char; + *str = 0; return str; } - switch(type){ + switch (type){ case B_MIME_TYPE: case B_ASCII_TYPE: case B_STRING_TYPE: - if(size>512) size=512; - str=new char[size+4]; + if (size>512) + size=512; + str = new char[size+4]; *str='\"'; strncpy(str+1, ptr, size); strcat(str, "\""); break; case B_POINTER_TYPE: - str=new char[64]; + str = new char[64]; sprintf(str, "%p", *(void**)ptr); break; case B_REF_TYPE: - str=new char[1024]; + str = new char[1024]; anothermsg.AddData("myref", B_REF_TYPE, ptr, size); anothermsg.FindRef("myref", &aref); - if(entry.SetTo(&aref)==B_OK){ + if (entry.SetTo(&aref)==B_OK){ entry.GetPath(&path); strcpy(str, path.Path()); - }else{ + } else { strcpy(str, "invalid entry_ref"); } break; case B_SSIZE_T_TYPE: case B_INT64_TYPE: - str=new char[64]; - i64=*(int64*)ptr; + str = new char[64]; + i64 = *(int64*)ptr; sprintf(str, "%Ld (0x%LX)", i64, i64); break; case B_SIZE_T_TYPE: case B_INT32_TYPE: - str=new char[64]; - i32=*(int32*)ptr; + str = new char[64]; + i32 = *(int32*)ptr; sprintf(str, "%ld (0x%08lX)", i32, i32); break; case B_INT16_TYPE: - str=new char[64]; - i16=*(int16*)ptr; + str = new char[64]; + i16 = *(int16*)ptr; sprintf(str, "%d (0x%04X)", i16, i16); break; case B_CHAR_TYPE: case B_INT8_TYPE: - str=new char[64]; - i8=*(int8*)ptr; + str = new char[64]; + i8 = *(int8*)ptr; sprintf(str, "%d (0x%02X)", i8, i8); break; case B_UINT64_TYPE: - str=new char[64]; - ui64=*(uint64*)ptr; + str = new char[64]; + ui64 = *(uint64*)ptr; sprintf(str, "%Lu (0x%LX)", ui64, ui64); break; case B_UINT32_TYPE: - str=new char[64]; - ui32=*(uint32*)ptr; + str = new char[64]; + ui32 = *(uint32*)ptr; sprintf(str, "%lu (0x%08lX)", ui32, ui32); break; case B_UINT16_TYPE: - str=new char[64]; - ui16=*(uint16*)ptr; + str = new char[64]; + ui16 = *(uint16*)ptr; sprintf(str, "%u (0x%04X)", ui16, ui16); break; case B_UINT8_TYPE: - str=new char[64]; - ui8=*(uint8*)ptr; + str = new char[64]; + ui8 = *(uint8*)ptr; sprintf(str, "%u (0x%02X)", ui8, ui8); break; case B_BOOL_TYPE: - str=new char[10]; - if(*ptr){ + str = new char[10]; + if (*ptr){ strcpy(str, "TRUE"); - }else{ + } else { strcpy(str, "FALSE"); } break; case B_FLOAT_TYPE: - str=new char[40]; - fptr=(float*)ptr; + str = new char[40]; + fptr = (float*)ptr; sprintf(str, "%.3f", *fptr); break; case B_DOUBLE_TYPE: - str=new char[40]; - dptr=(double*)ptr; + str = new char[40]; + dptr = (double*)ptr; sprintf(str, "%.3f", *dptr); break; case B_RECT_TYPE: - str=new char[200]; - fptr=(float*)ptr; + str = new char[200]; + fptr = (float*)ptr; sprintf(str, "BRect(%.1f, %.1f, %.1f, %.1f)", fptr[0], fptr[1], fptr[2], fptr[3]); break; case B_POINT_TYPE: - str=new char[200]; - fptr=(float*)ptr; + str = new char[200]; + fptr = (float*)ptr; sprintf(str, "BPoint(%.1f, %.1f)", fptr[0], fptr[1]); break; case B_RGB_COLOR_TYPE: - str=new char[64]; + str = new char[64]; sprintf(str, "Red=%u Green=%u Blue=%u Alpha=%u", ((uint8*)ptr)[0], ((uint8*)ptr)[1], ((uint8*)ptr)[2], ((uint8*)ptr)[3] ); break; case B_COLOR_8_BIT_TYPE: - str=new char[size*6+4]; - *str=0; - for(i=0;i0 - value_info vinfo[10]={ {"Backup", 'back', B_COMMAND_KIND, "This command backs up your hard drive."}, - {"Abort", 'abor', B_COMMAND_KIND, "Stops the current operation..."}, - {"Type Code", 'type', B_TYPE_CODE_KIND, "Type code info..."} - }; - vinfo_count=3; + value_info vinfo[10] = { {"Backup", 'back', B_COMMAND_KIND, "This command backs up your hard drive."}, + {"Abort", 'abor', B_COMMAND_KIND, "Stops the current operation..."}, + {"Type Code", 'type', B_TYPE_CODE_KIND, "Type code info..."} + }; + vinfo_count = 3; #endif - if(vinfo && vinfo_count>0){ + if (vinfo && vinfo_count>0){ sprintf(str+strlen(str), "\n name value kind\n--------------------------------------------------------------------------------\n"); - while(vinfo_index>24)&255; uint8 digit1=(ID>>16)&255; @@ -1402,29 +1418,33 @@ char *id_to_string(long ID, char *here) uint8 digit3=(ID)&255; bool itsvalid=false; - if(digit0==0){ - if(digit1==0){ - if(digit2==0){ + if (digit0==0){ + if (digit1==0){ + if (digit2==0) { // 1 digits - if(is_valid_char(digit3) ) itsvalid=TRUE; + if (is_valid_char(digit3) ) + itsvalid=TRUE; sprintf(here, "'%c'", digit3); - }else{ + } else { // 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); } - }else{ + } else { // 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); } - }else{ + } else { // 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); } - if(!itsvalid){ + if (!itsvalid){ sprintf(here, "%ldL", ID); } @@ -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); }