From 345c1d74630c07cba6ffc4416dd0c72d2ba89625 Mon Sep 17 00:00:00 2001 From: ocoursiere Date: Thu, 18 Oct 2012 22:49:05 +0000 Subject: [PATCH] Update hey.cpp to the one in the Haiku repository. This version has the advantage (at least) to compile with gcc 4. --- bepascal/source/bepascal/cpp/src/hey/hey.cpp | 1231 ++++++++++-------- 1 file changed, 694 insertions(+), 537 deletions(-) diff --git a/bepascal/source/bepascal/cpp/src/hey/hey.cpp b/bepascal/source/bepascal/cpp/src/hey/hey.cpp index 116b4a8..b311ea3 100644 --- a/bepascal/source/bepascal/cpp/src/hey/hey.cpp +++ b/bepascal/source/bepascal/cpp/src/hey/hey.cpp @@ -5,7 +5,24 @@ // // public domain, use it at your own risk // -// 1.2.6: syntax extended by Sander Stoks to allow: +// 1.2.8: (Sander Stoks): Added command-line option -o which will output the "result" value +// in the reply message to stdout, so you can use it in shell scripting more easily: +// "hey Becasso get AspectRatio of Canvas 0" +// outputs +// Reply BMessage(B_REPLY): +// "result" (B_DOUBLE_TYPE) : 0.600 +// but "hey -o Becasso get AspectRatio of Canvas 0" +// outputs 0.600000 directly. +// +// 1.2.7: by Sander Stoks: Made a fork since I don't think Attila still supports "hey", and +// because the latest version on BeBits seems to be 1.2.4. +// Changes w.r.t. 1.2.6: When an application returns an error on a message, hey now +// keeps iterating over applications with the same signature. This is useful because, +// for instance, Terminal starts as a new process for each instance, so it previously +// wouldn't work to move a specific Terminal window using hey. You can now say +// "hey Terminal set Frame of Window foo to BRect[...]". +// +// 1.2.6: syntax extended by Sander Stoks so it's only limited by available memory. // @@ -73,26 +90,40 @@ // // The range specifier sent to the target was 1 greater than it should've been. Fixed. // -// 'hey' made the assumption that the first thread in a team will be the -// application thread (and therefore have the application's name). +// 'hey' made the assumption that the first thread in a team will be the +// application thread (and therefore have the application's name). // This was not always the case. Fix from Scott Lindsey . // -//v1.1.0: Flattened BPropertyInfo is printed if found in the reply of B_GET_SUPPORTED_SUITES -// 1,2,3 and 4 character message constant is supported (e.g. '1', '12', '123', '1234') -// Alpha is sent with rgb_color +//v1.1.0: Flattened BPropertyInfo is printed if found in the reply of B_GET_SUPPORTED_SUITES +// 1,2,3 and 4 character message constant is supported (e.g. '1', '12', '123', '1234') +// Alpha is sent with rgb_color // -//v1.0.0 First public release +//v1.0.0 First public release -#include "hey.h" #include #include #include -#include #include #include +#include -const char VERSION[]="v1.2.6"; +int32 HeyInterpreterThreadHook(void* arg); + +status_t Hey(BMessenger* target, const char* arg, BMessage* reply); +bool isSpace(char c); +status_t Hey(BMessenger* target, char* argv[], int32* argx, int32 argc, BMessage* reply); +status_t add_specifier(BMessage *to_message, char *argv[], int32 *argx, int32 argc); +status_t add_data(BMessage *to_message, char *argv[], int32 *argx); +status_t add_with(BMessage *to_message, char *argv[], int32 *argx, int32 argc); +void add_message_contents(BList *textlist, BMessage *msg, int32 level); +char *get_datatype_string(int32 type); +char *format_data(int32 type, char *ptr, long size); +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"; #define DEBUG_HEY 0 // 1: prints the script message to be sent to the target application, 0: prints only the reply @@ -103,15 +134,111 @@ const char VERSION[]="v1.2.6"; // flag for silent mode bool silent; +// flag for stdout mode +bool output; + +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 B_ERROR; + } + + if (argc < 3) { + if (!silent) + fprintf(stderr, "Cannot find the verb!\n"); + return B_ERROR; + } -int main(int argc, char *argv[]) + BMessage the_reply; + int32 argx = argapp+1; + 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 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")); + } + return 1; + } else { + if (!silent){ + if (output){ + type_code tc; + if (the_reply.GetInfo("result", &tc) == B_OK){ + if(tc==B_INT8_TYPE){ + int8 v; + the_reply.FindInt8("result", &v); + printf("%d\n", v); + } else if(tc==B_INT16_TYPE){ + int16 v; + the_reply.FindInt16("result", &v); + printf("%d\n", v); + } else if(tc==B_INT32_TYPE){ + int32 v; + the_reply.FindInt32("result", &v); + printf("%ld\n", v); + } else if(tc==B_UINT8_TYPE){ + uint8 v; + the_reply.FindInt8("result", (int8*)&v); + printf("%u\n", v); + } else if(tc==B_UINT16_TYPE){ + uint16 v; + the_reply.FindInt16("result", (int16*)&v); + printf("%u\n", v); + } else if(tc==B_UINT32_TYPE){ + uint32 v; + the_reply.FindInt32("result", (int32*)&v); + printf("%lu\n", v); + } else if(tc==B_STRING_TYPE){ + const char* v; + the_reply.FindString("result", &v); + printf("%s\n", v); + } else if(tc==B_FLOAT_TYPE){ + float v; + the_reply.FindFloat("result", &v); + printf("%f\n", v); + } else if(tc==B_DOUBLE_TYPE){ + double v; + the_reply.FindDouble("result", &v); + printf("%f\n", v); + } else if(tc==B_BOOL_TYPE){ + bool v; + the_reply.FindBool("result", &v); + printf("%s\n", v?"true":"false"); + } else { + printf("Unsupported type\n"); + } + } + } else { + printf("Reply "); + print_message(&the_reply); + printf("\n"); + } + } + } + } + return B_OK; +} + +int +main(int argc, char *argv[]) { BApplication app("application/x-amezei-hey"); if (argc < 2) { fprintf(stderr, "hey %s, written by Attila Mezei (attila.mezei@mail.datanet.hu)\n" \ - "usage: hey [-s] [let do] [let do] >* [to ] [with name= [and name=]*]\n" \ "where : DO|GET|SET|COUNT|CREATE|DELETE|GETSUITES|QUIT|SAVE|LOAD|'what'\n" \ " : [the] [ | name | \"name\" | '\"name\"' ]\n" \ @@ -119,19 +246,28 @@ int main(int argc, char *argv[]) " : \"string\" | | | bool(value) | int8(value)\n" \ " | int16(value) | int32(value) | float(value) | double(value)\n" \ " | BPoint(x,y) | BRect(l,t,r,b) | rgb_color(r,g,b,a) | file(path)\n" \ - "options: -s: silent\n\n", VERSION); + "options: -s: silent\n" \ + " -o: output result to stdout for easy parsing\n\n", VERSION); // Updated Usage string to reflect "do", "the", bare -index, and '"name"' changes below // -- pfolk@uni.uiuc.edu 1999-11-03 - - return -1; + + return 1; } - + int32 argapp = 1; - silent=false; - - if(strcmp(argv[1], "-s")==0 || strcmp(argv[1], "-S")==0){ - silent=true; - argapp++; + 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; + argapp++; + } + if (strcmp(argv[i], "-o")==0 || strcmp(argv[i], "-O")==0){ + output=true; + argapp++; + } } // find the application @@ -139,114 +275,94 @@ int main(int argc, char *argv[]) BList team_list; team_id teamid; app_info appinfo; - + + teamid = atoi(argv[argapp]); + if (teamid > 0) { + if (be_roster->GetRunningAppInfo(teamid, &appinfo)!=B_OK) + return 1; + the_application=BMessenger(NULL, teamid); + if (!parse(the_application, argc, argv, argapp)) + return 0; + return 1; + } + 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); - break; - }else{ - if(strcmp(appinfo.ref.name, argv[argapp])==0){ + if (!parse(the_application, argc, argv, argapp)) + return 0; + } else { + if (strcmp(appinfo.ref.name, argv[argapp])==0){ the_application=BMessenger(0, teamid); - break; + if (!parse(the_application, argc, argv, argapp)) + return 0; } } } - - if(!the_application.IsValid()){ - if(!silent) fprintf(stderr, "Cannot find the application (%s)\n", argv[argapp]); - return -1; - } - if (argc < 3) { - if(!silent) fprintf(stderr, "Cannot find the verb!\n"); - return -1; - } + return 1; +} - - 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; - } 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")); - } - }else{ - if(!silent){ - printf("Reply "); - print_message(&the_reply); - printf("\n"); - } +int32 +HeyInterpreterThreadHook(void* arg) +{ + if (!arg) + return 1; + + BMessage environment(*(BMessage*) arg); + const char* prompt = "Hey"; + if (environment.HasString("prompt")) + environment.FindString("prompt", &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; } - -int32 HeyInterpreterThreadHook(void* arg) +status_t +Hey(BMessenger* target, const char* arg, BMessage* reply) { - 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 { - return 1; - } -} - -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 + BList argv; // number of tokens is now limited only by memory -- pfolk@uni.uiuc.edu 1999-11-03 char* tokens = new char[strlen(arg)*2]; char* currentToken = tokens; int32 tokenNdex = 0; int32 argNdex = 0; 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; - argv.push_back(currentToken); + currentToken[tokenNdex] = 0; + argv.AddItem(currentToken); currentToken += tokenNdex+1; tokenNdex=0; argNdex++; } else { // just skip the whitespace - argNdex++; + argNdex++; } } else { // copy char into current token currentToken[tokenNdex] = arg[argNdex]; @@ -254,21 +370,23 @@ status_t Hey(BMessenger* target, const char* arg, BMessage* reply) argNdex++; } } - + if (tokenNdex!=0) { // close off currentToken token - currentToken[tokenNdex] = 0; - argv.push_back(currentToken); + currentToken[tokenNdex] = 0; + argv.AddItem(currentToken); } - argv.push_back(NULL); - + argv.AddItem(NULL); + int32 argx = 0; - status_t ret = Hey(target, argv.begin(), &argx, argv.size()-1, reply); + status_t ret = Hey(target, (char **)argv.Items(), &argx, argv.CountItems()-1, reply); // This used to be "return Hey(...);"---so tokens wasn't delete'd. -- pfolk@uni.uiuc.edu 1999-11-03 - delete tokens; + delete[] tokens; return ret; } -bool isSpace(char c) + +bool +isSpace(char c) { switch (c) { case ' ': @@ -280,167 +398,177 @@ 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){}; - - if(result!=B_ERROR){ // bad syntax - if(!silent) fprintf(stderr, "Bad specifier syntax!\n"); + 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"); 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; - 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])<<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; default: - // maybe this is a user defined command, ask for the supported suites - bool found=false; - if (target && target->IsValid()) { - BMessage rply; - BMessage req(B_GET_SUPPORTED_SUITES); - if(target->SendMessage(&req, &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(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(result!=B_ERROR){ // bad syntax - if(!silent) fprintf(stderr, "Bad specifier syntax!\n"); + 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"); 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; } } add_with(&the_message, argv, argx, argc); - + #if DEBUG_HEY>0 fprintf(stderr, "Send "); print_message(&the_message); @@ -459,33 +587,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); } @@ -493,133 +621,135 @@ status_t add_with(BMessage *to_message, char *argv[], int32 *argx, int32 argc) return result; } -// returns B_OK if successful +// 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; + 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 + 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{ + return B_OK; + } 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; - } + 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{ + return B_OK; + } 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; @@ -627,50 +757,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; } @@ -681,123 +812,122 @@ status_t add_data(BMessage *to_message, char *argv[], int32 *argx) #define MAX_NAME_LENGTH 128 char curname[MAX_NAME_LENGTH]; strcpy (curname, "data"); // This is the default. - + char *s = valuestring; 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; @@ -807,16 +937,16 @@ 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;iwhat); + printf("BMessage(%s):\n", whatString); + free(whatString); + 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; - - 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)); + 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)); 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]; - - switch(type){ + char *str = new char[128]; + + 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; @@ -977,25 +1107,25 @@ char *get_datatype_string(int32 type) case B_NAME_SPECIFIER : strcpy(str, "B_NAME_SPECIFIER"); break; case B_ERROR : strcpy(str, "B_ERROR"); break; - + default: // unknown id_to_string(type, str); break; } - + return str; } -char *format_data(int32 type, char *ptr, long size) +char * +format_data(int32 type, char *ptr, long size) { char idtext[32]; char *str; float *fptr; double *dptr; // BRect *brptr; - long i; entry_ref aref; BEntry entry; BPath path; @@ -1008,324 +1138,350 @@ char *format_data(int32 type, char *ptr, long size) uint16 ui16; uint8 ui8; BMessage anothermsg; - BPropertyInfo propinfo; - const property_info *pinfo; - int32 pinfo_index; - const value_info *vinfo; - 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]; + case B_RGB_COLOR_TYPE: + 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;i "); break; } } + + // pad the rest with spaces + if (strlen(start)<60){ + strcat(str, " "+strlen(start) ); + } else { + strcat(str, " " ); + } + for (int32 i = 0; i < 10 && pinfo[pinfo_index].types[i] != 0; i++) { + uint32 type = pinfo[pinfo_index].types[i]; + char str2[6]; + snprintf(str2, sizeof(str2), "%c%c%c%c ", + int(type & 0xFF000000) >> 24, + int(type & 0xFF0000) >> 16, + int(type & 0xFF00) >> 8, + (int)type & 0xFF); + strcat(str, str2); + } + + for (int32 i = 0; i < 3; i++) { + for (int32 j = 0; j < 5 && pinfo[pinfo_index].ctypes[i].pairs[j].type != 0; j++) { + uint32 type = pinfo[pinfo_index].ctypes[i].pairs[j].type; + char str2[strlen(pinfo[pinfo_index].ctypes[i].pairs[j].name) + 8]; + snprintf(str2, sizeof(str2), + "(%s %c%c%c%c)", + pinfo[pinfo_index].ctypes[i].pairs[j].name, + int(type & 0xFF000000) >> 24, + int(type & 0xFF0000) >> 16, + int(type & 0xFF00) >> 8, + (int)type & 0xFF); + strcat(str, str2); + } + } strcat(str, "\n"); - + // is there usage info? - if(pinfo[pinfo_index].usage){ + if (pinfo[pinfo_index].usage){ strcat(str, " Usage: "); strcat(str, pinfo[pinfo_index].usage); strcat(str, "\n"); } - - pinfo_index++; // take next propertyinfo } - - + + // handle value infos.... - vinfo=propinfo.Values(); - vinfo_index=0; - vinfo_count=propinfo.CountValues(); + const value_info *vinfo = propinfo.Values(); + int32 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 - 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; - uint8 digit2=(ID>>8)&255; - uint8 digit3=(ID)&255; - bool itsvalid=false; - - if(digit0==0){ - if(digit1==0){ - if(digit2==0){ + uint8 digit0 = (ID>>24)& 255; + uint8 digit1 = (ID>>16)& 255; + uint8 digit2 = (ID>>8) & 255; + uint8 digit3 = (ID) & 255; + bool itsvalid = false; + + 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); } @@ -1333,7 +1489,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); }