diff --git a/security/nss/cmd/pk11util/pk11table.c b/security/nss/cmd/pk11util/pk11table.c index 69710f85e661..52ff7fa148e0 100644 --- a/security/nss/cmd/pk11util/pk11table.c +++ b/security/nss/cmd/pk11util/pk11table.c @@ -1311,6 +1311,11 @@ const Commands _commands[] = { "reads filename as script of commands to execute\n", {ArgVar|ArgNew, ArgNone, ArgNone, ArgNone, ArgNone, ArgNone, ArgNone, ArgNone, ArgNone, ArgNone }}, + {"Time", F_Time, +"Time pkcs11 command\n\n" +"Execute a pkcs #11 command and time the results\n", + {ArgVar|ArgFull, ArgNone, ArgNone, ArgNone, ArgNone, + ArgNone, ArgNone, ArgNone, ArgNone, ArgNone }}, {"System", F_System, "Fix Me... ", {ArgULong, ArgNone, ArgNone, ArgNone, ArgNone, @@ -1329,4 +1334,38 @@ const Commands _commands[] = { const Commands *commands= &_commands[0]; const int commandCount = sizeof(_commands) / sizeof(_commands[0]); +const Topics _topics[] = { + { "variables", +"Variables are random strings of characters. These should begin with alpha\n" +" characters, and should not contain any spaces, nor should they match any\n" +" built-in constants. There is some checking in the code for these things,\n" +" but it's not 100% and using invalid variable names can cause problems.\n" +" Variables are created by any 'OUT' parameter. If the variable does not\n" +" exist, it will be created. For in parameters variables must already exist.\n" + }, + { "constants", +"pk11util recognizes *lots* of constants. All CKA_, CKF_, CKO_, CKU_, CKS_,\n" +" CKC_, CKK_, CKH_, CKM_, CKT_ values from the PKCS #11 spec are recognized.\n" +" Constants can be specified with their fully qualified CK?_ value, or the\n" +" prefix can be dropped. Constants are matched case insensitve.\n" + }, + { "arrays", +"Arrays are special variables which represent 'C' arrays. Each array \n" +" variable can be referenced as a group (using just the name), or as \n" +" individual elements (with the [int] operator). Example:\n" +" print myArray # prints the full array.\n" +" print myArray[3] # prints the 3rd elemement of the array \n" + }, + { "sizes", +"Size operaters returns the size in bytes of a variable, or the number of\n" +" elements in an array.\n" +" size(var) and sizeof(var) return the size of var in bytes.\n" +" sizea(var) and sizeofarray(var) return the number of elements in var.\n" +" If var is not an array, sizea(var) returns 1.\n" + }, +}; + +const Topics *topics=&_topics[0]; +const int topicCount = sizeof(_topics)/sizeof(_topics[0]); + diff --git a/security/nss/cmd/pk11util/pk11util.c b/security/nss/cmd/pk11util/pk11util.c index 9e4e7b379ec3..ef5e2db12469 100644 --- a/security/nss/cmd/pk11util/pk11util.c +++ b/security/nss/cmd/pk11util/pk11util.c @@ -55,6 +55,7 @@ #include "prtypes.h" #include "prtime.h" #include "prlong.h" +#include "prinrval.h" #include "pkcs11.h" @@ -68,6 +69,7 @@ CK_ULONG systemFlags; #define FLAG_NEGATE 0x80000000 #define FLAG_Verify 0x00000001 #define FLAG_VerifyFile 0x00000002 +#define CKR_QUIT 0x80000000 int ArgSize(ArgType type); const char *constLookup(const char *bp, CK_ULONG *value, ConstType *type); @@ -1178,6 +1180,15 @@ parseArgs(int index, const char * bp) return NULL; } + /* collect all the rest of the command line and send + * it as a single argument */ + if (cp->args[i] & ArgFull) { + int size = strlen(bp)+1; + argList[i] = NewValue(type, size); + memcpy(argList[i]->data, bp, size); + break; + } + /* * look up the argument in our variable list first... only * exception is the new argument type for set... @@ -1420,24 +1431,47 @@ loadModule(Module *module, char *library) static void printHelp(int index, int full) { - int j; - printf(" %s", commands[index].fname); - for (j=0; j < MAX_ARGS; j++) { + int j; + printf(" %s", commands[index].fname); + for (j=0; j < MAX_ARGS; j++) { ArgType type = commands[index].args[j] & ArgMask; if (type == ArgNone) { break; } printf(" %s", valueString[type]); - } - printf("\n"); - printf(" %s\n",commands[index].helpString); + } + printf("\n"); + printf(" %s\n",commands[index].helpString); } /* add Topical help here ! */ static CK_RV printTopicHelp(char *topic) { - return CKR_DATA_INVALID; + int size,i; + int topicLen; + + topicLen = PL_strlen(topic); + + for ( i = 0; i < topicCount; i++) { + size = PL_strlen(topics[i].name); + + if (size <= topicLen) { + if (PL_strncasecmp(topic,topics[i].name,size) == 0) { + break; + } + } + } + + if (i == topicCount) { + fprintf(stderr,"Can't find topic '%s'\n", topic); + return CKR_DATA_INVALID; + } + + printf(" %s", topic); + printf("\n"); + printf(" %s\n",topics[i].helpString); + return CKR_OK; } static CK_RV @@ -1451,10 +1485,17 @@ printGeneralHelp(void) } printf("\n"); /* print help topics */ + printf(" To get help on a topic, select from the list below:"); + for ( i = 0; i < topicCount; i++) { + if (i % 5 == 0) printf("\n"); + printf("%s,", topics[i].name); + } + printf("\n"); return CKR_OK; } -CK_RV run(char *); +CK_RV run(const char *); +CK_RV timeCommand(const char *); /* * Actually dispatch the function... Bad things happen @@ -1864,6 +1905,8 @@ do_func(int index, Value **a) return list(); case F_Run: return run(a[0]->data); + case F_Time: + return timeCommand(a[0]->data); case F_Load: return loadModule(&module,a[0]->data); case F_Unload: @@ -1902,7 +1945,7 @@ do_func(int index, Value **a) } return printGeneralHelp(); case F_Quit: - return 0x80000000; + return CKR_QUIT; default: fprintf(stderr, "Function %s not yet supported\n",commands[index].fname ); @@ -1912,55 +1955,80 @@ do_func(int index, Value **a) return CKR_OK; } +CK_RV +processCommand(const char * buf) +{ + CK_RV error = CKR_OK; + int index; + const char *bp; + Value **arglist; + + bp = strip(buf); + /* allow comments and blank lines in scripts */ + if ((*bp == '#') || (*bp == 0) || (*bp == '\n')){ + return CKR_OK; + } + + index = lookup(bp); + + if (index < 0) { + return CKR_OK; + } + + arglist = parseArgs(index,bp); + if (arglist == NULL) { + return CKR_OK; + } + + error = do_func(index,arglist); + if (error == CKR_OK) { + putOutput(arglist); + } else if (error != CKR_QUIT) { + printf(">> Error : "); + printConst(error, ConstResult, 1); + } + + parseFree(arglist); + return error; +} + +CK_RV timeCommand(const char *command) +{ + CK_RV ckrv; + PRIntervalTime startTime = PR_IntervalNow(); + PRIntervalTime endTime; + PRIntervalTime elapsedTime; + + ckrv = processCommand(command); + + endTime = PR_IntervalNow(); + elapsedTime = endTime - startTime; + printf("Time -- %d msec \n", + PR_IntervalToMilliseconds(elapsedTime)); + + return ckrv; +} + + CK_RV process(FILE *inFile,int user) { char buf[2048]; - Value **arglist; CK_RV error; CK_RV ckrv = CKR_OK; if (user) { printf("pkcs11> "); fflush(stdout); } while (fgets(buf,2048,inFile) != NULL) { - int index; - const char *bp; if (!user) printf("* %s",buf); - bp = strip(buf); - /* allow comments in scripts */ - if (*bp == '#') { - goto done; - } - - - index = lookup(bp); - - if (index < 0) { - goto done; - } - - arglist = parseArgs(index,bp); - if (arglist == NULL) { - goto done; - } - - error = do_func(index,arglist); - if (error == 0x80000000) { - parseFree(arglist); + error = processCommand(buf); + if (error == CKR_QUIT) { break; - } - if (error) { + } else if (error != CKR_OK) { ckrv = error; - printf(">> Error : "); - printConst(error, ConstResult, 1); } - - putOutput(arglist); - - parseFree(arglist); -done: if (user) { printf("pkcs11> "); fflush(stdout); } @@ -1968,7 +2036,7 @@ done: return ckrv; } -CK_RV run(char *filename) +CK_RV run(const char *filename) { FILE *infile; CK_RV ckrv; diff --git a/security/nss/cmd/pk11util/pk11util.h b/security/nss/cmd/pk11util/pk11util.h index cdb4970c9664..aed07c32579e 100644 --- a/security/nss/cmd/pk11util/pk11util.h +++ b/security/nss/cmd/pk11util/pk11util.h @@ -29,6 +29,7 @@ typedef enum { F_Load, F_Unload, F_System, + F_Time, F_Help, F_Quit, } FunctionType; @@ -59,6 +60,7 @@ typedef enum { ArgFile = 0x800, ArgStatic = 0x1000, ArgOpt = 0x2000, + ArgFull = 0x4000, } ArgType; typedef enum _constType @@ -135,6 +137,10 @@ typedef struct _module { CK_FUNCTION_LIST *functionList; } Module; +typedef struct _topics { + char *name; + char *helpString; +} Topics; /* * the command array itself. Make name to function and it's arguments @@ -148,4 +154,6 @@ extern const Constant *consts; extern const int constCount; extern const Commands *commands; extern const int commandCount; +extern const Topics *topics; +extern const int topicCount;