| | @@ -7,13 +7,14 @@ |
| 7 | 7 | #include "config.h" |
| 8 | 8 | #include "th.h" |
| 9 | 9 | #include <string.h> |
| 10 | 10 | #include <assert.h> |
| 11 | 11 | |
| 12 | | -typedef struct Th_Command Th_Command; |
| 13 | | -typedef struct Th_Frame Th_Frame; |
| 14 | | -typedef struct Th_Variable Th_Variable; |
| 12 | +typedef struct Th_Command Th_Command; |
| 13 | +typedef struct Th_Frame Th_Frame; |
| 14 | +typedef struct Th_Variable Th_Variable; |
| 15 | +typedef struct Th_InterpAndList Th_InterpAndList; |
| 15 | 16 | |
| 16 | 17 | /* |
| 17 | 18 | ** Interpreter structure. |
| 18 | 19 | */ |
| 19 | 20 | struct Th_Interp { |
| | @@ -87,10 +88,21 @@ |
| 87 | 88 | int nRef; /* Number of references to this structure */ |
| 88 | 89 | int nData; /* Number of bytes at Th_Variable.zData */ |
| 89 | 90 | char *zData; /* Data for scalar variables */ |
| 90 | 91 | Th_Hash *pHash; /* Data for array variables */ |
| 91 | 92 | }; |
| 93 | + |
| 94 | +/* |
| 95 | +** This structure is used to pass complete context information to the |
| 96 | +** hash iteration callback functions that need a Th_Interp and a list |
| 97 | +** to operate on, e.g. thListAppend(). |
| 98 | +*/ |
| 99 | +struct Th_InterpAndList { |
| 100 | + Th_Interp *interp; /* Associated interpreter context */ |
| 101 | + char **pzList; /* IN/OUT: Ptr to ptr to list */ |
| 102 | + int *pnList; /* IN/OUT: Current length of *pzList */ |
| 103 | +}; |
| 92 | 104 | |
| 93 | 105 | /* |
| 94 | 106 | ** Hash table API: |
| 95 | 107 | */ |
| 96 | 108 | #define TH_HASHSIZE 257 |
| | @@ -298,10 +310,25 @@ |
| 298 | 310 | } |
| 299 | 311 | Th_Free((Th_Interp *)pContext, pEntry->pData); |
| 300 | 312 | pEntry->pData = 0; |
| 301 | 313 | return 1; |
| 302 | 314 | } |
| 315 | + |
| 316 | +/* |
| 317 | +** Argument pEntry points to an entry in a hash table. The key is |
| 318 | +** the list element to be added. |
| 319 | +** |
| 320 | +** Argument pContext is a pointer to the Th_InterpAndList structure. |
| 321 | +** |
| 322 | +** Always returns non-zero. |
| 323 | +*/ |
| 324 | +static int thListAppend(Th_HashEntry *pEntry, void *pContext){ |
| 325 | + Th_InterpAndList *pInterpAndList = (Th_InterpAndList *)pContext; |
| 326 | + Th_ListAppend(pInterpAndList->interp, pInterpAndList->pzList, |
| 327 | + pInterpAndList->pnList, pEntry->zKey, pEntry->nKey); |
| 328 | + return 1; |
| 329 | +} |
| 303 | 330 | |
| 304 | 331 | /* |
| 305 | 332 | ** Push a new frame onto the stack. |
| 306 | 333 | */ |
| 307 | 334 | static int thPushFrame(Th_Interp *interp, Th_Frame *pFrame){ |
| | @@ -2832,5 +2859,44 @@ |
| 2832 | 2859 | } |
| 2833 | 2860 | |
| 2834 | 2861 | *z = '\0'; |
| 2835 | 2862 | return Th_SetResult(interp, zBuf, -1); |
| 2836 | 2863 | } |
| 2864 | + |
| 2865 | +/* |
| 2866 | +** Appends all currently registered command names to the specified list |
| 2867 | +** and returns TH_OK upon success. Any other return value indicates an |
| 2868 | +** error. |
| 2869 | +*/ |
| 2870 | +int Th_ListAppendCommands(Th_Interp *interp, char **pzList, int *pnList){ |
| 2871 | + Th_InterpAndList *p = (Th_InterpAndList *)Th_Malloc( |
| 2872 | + interp, sizeof(Th_InterpAndList) |
| 2873 | + ); |
| 2874 | + p->interp = interp; |
| 2875 | + p->pzList = pzList; |
| 2876 | + p->pnList = pnList; |
| 2877 | + Th_HashIterate(interp, interp->paCmd, thListAppend, p); |
| 2878 | + Th_Free(interp, p); |
| 2879 | + return TH_OK; |
| 2880 | +} |
| 2881 | + |
| 2882 | +/* |
| 2883 | +** Appends all variable names for the current frame to the specified list |
| 2884 | +** and returns TH_OK upon success. Any other return value indicates an |
| 2885 | +** error. If the current frame cannot be obtained, TH_ERROR is returned. |
| 2886 | +*/ |
| 2887 | +int Th_ListAppendVariables(Th_Interp *interp, char **pzList, int *pnList){ |
| 2888 | + Th_Frame *pFrame = getFrame(interp, 0); |
| 2889 | + if( pFrame ){ |
| 2890 | + Th_InterpAndList *p = (Th_InterpAndList *)Th_Malloc( |
| 2891 | + interp, sizeof(Th_InterpAndList) |
| 2892 | + ); |
| 2893 | + p->interp = interp; |
| 2894 | + p->pzList = pzList; |
| 2895 | + p->pnList = pnList; |
| 2896 | + Th_HashIterate(interp, pFrame->paVar, thListAppend, p); |
| 2897 | + Th_Free(interp, p); |
| 2898 | + return TH_OK; |
| 2899 | + }else{ |
| 2900 | + return TH_ERROR; |
| 2901 | + } |
| 2902 | +} |
| 2837 | 2903 | |