Fossil SCM
Clean up the command-search logic.
Commit
bcb7c7a9b1e947035ca41469de37e93bc7595bb8
Parent
fe6d393021d9205…
2 files changed
+7
-8
+2
-1
+7
-8
| --- src/main.c | ||
| +++ src/main.c | ||
| @@ -298,15 +298,16 @@ | ||
| 298 | 298 | */ |
| 299 | 299 | static int name_search( |
| 300 | 300 | const char *zName, /* The name we are looking for */ |
| 301 | 301 | const NameMap *aMap, /* Search in this array */ |
| 302 | 302 | int nMap, /* Number of slots in aMap[] */ |
| 303 | + int iBegin, /* Lower bound on the array search */ | |
| 303 | 304 | int *pIndex /* OUT: The index in aMap[] of the match */ |
| 304 | 305 | ){ |
| 305 | 306 | int upr, lwr, cnt, m, i; |
| 306 | 307 | int n = strlen(zName); |
| 307 | - lwr = 0; | |
| 308 | + lwr = iBegin; | |
| 308 | 309 | upr = nMap-1; |
| 309 | 310 | while( lwr<=upr ){ |
| 310 | 311 | int mid, c; |
| 311 | 312 | mid = (upr+lwr)/2; |
| 312 | 313 | c = fossil_strcmp(zName, aMap[mid].zName); |
| @@ -318,11 +319,11 @@ | ||
| 318 | 319 | }else{ |
| 319 | 320 | lwr = mid + 1; |
| 320 | 321 | } |
| 321 | 322 | } |
| 322 | 323 | for(m=cnt=0, i=upr-2; cnt<2 && i<=upr+3 && i<nMap; i++){ |
| 323 | - if( i<0 ) continue; | |
| 324 | + if( i<iBegin ) continue; | |
| 324 | 325 | if( strncmp(zName, aMap[i].zName, n)==0 ){ |
| 325 | 326 | m = i; |
| 326 | 327 | cnt++; |
| 327 | 328 | } |
| 328 | 329 | } |
| @@ -655,13 +656,11 @@ | ||
| 655 | 656 | } |
| 656 | 657 | #ifndef _WIN32 |
| 657 | 658 | if( !is_valid_fd(2) ) fossil_panic("file descriptor 2 not open"); |
| 658 | 659 | /* if( is_valid_fd(3) ) fossil_warning("file descriptor 3 is open"); */ |
| 659 | 660 | #endif |
| 660 | - rc = name_search(zCmdName, aCommand + count(aWebpage), | |
| 661 | - count(aCommand) - count(aWebpage), &idx); | |
| 662 | - idx += count(aWebpage); | |
| 661 | + rc = name_search(zCmdName, aCommand, count(aCommand), FOSSIL_FIRST_CMD, &idx); | |
| 663 | 662 | if( rc==1 ){ |
| 664 | 663 | #ifdef FOSSIL_ENABLE_TH1_HOOKS |
| 665 | 664 | if( !g.isHTTP && !g.fNoThHook ){ |
| 666 | 665 | rc = Th_CommandHook(zCmdName, 0); |
| 667 | 666 | }else{ |
| @@ -1017,11 +1016,11 @@ | ||
| 1017 | 1016 | zCmdOrPagePlural = "pages"; |
| 1018 | 1017 | }else{ |
| 1019 | 1018 | zCmdOrPage = "command"; |
| 1020 | 1019 | zCmdOrPagePlural = "commands"; |
| 1021 | 1020 | } |
| 1022 | - rc = name_search(g.argv[2], aCommand, count(aCommand), &idx); | |
| 1021 | + rc = name_search(g.argv[2], aCommand, count(aCommand), 0, &idx); | |
| 1023 | 1022 | if( rc==1 ){ |
| 1024 | 1023 | fossil_print("unknown %s: %s\nAvailable %s:\n", |
| 1025 | 1024 | zCmdOrPage, g.argv[2], zCmdOrPagePlural); |
| 1026 | 1025 | command_list(0, isPage ? CMDFLAG_WEBPAGE : (0xff & ~CMDFLAG_WEBPAGE)); |
| 1027 | 1026 | fossil_exit(1); |
| @@ -1061,11 +1060,11 @@ | ||
| 1061 | 1060 | int rc, idx; |
| 1062 | 1061 | char *z, *s, *d; |
| 1063 | 1062 | char const * zCmdOrPage = ('/'==*zCmd) ? "page" : "command"; |
| 1064 | 1063 | style_submenu_element("Command-List", "Command-List", "%s/help", g.zTop); |
| 1065 | 1064 | @ <h1>The "%s(zCmd)" %s(zCmdOrPage):</h1> |
| 1066 | - rc = name_search(zCmd, aCommand, count(aCommand), &idx); | |
| 1065 | + rc = name_search(zCmd, aCommand, count(aCommand), 0, &idx); | |
| 1067 | 1066 | if( rc==1 ){ |
| 1068 | 1067 | @ unknown command: %s(zCmd) |
| 1069 | 1068 | }else if( rc==2 ){ |
| 1070 | 1069 | @ ambiguous command prefix: %s(zCmd) |
| 1071 | 1070 | }else{ |
| @@ -1563,11 +1562,11 @@ | ||
| 1563 | 1562 | } |
| 1564 | 1563 | |
| 1565 | 1564 | /* Locate the method specified by the path and execute the function |
| 1566 | 1565 | ** that implements that method. |
| 1567 | 1566 | */ |
| 1568 | - if( name_search(g.zPath, aWebpage, count(aWebpage), &idx) ){ | |
| 1567 | + if( name_search(g.zPath, aWebpage, count(aWebpage), 0, &idx) ){ | |
| 1569 | 1568 | #ifdef FOSSIL_ENABLE_JSON |
| 1570 | 1569 | if(g.json.isJsonMode){ |
| 1571 | 1570 | json_err(FSL_JSON_E_RESOURCE_NOT_FOUND,NULL,0); |
| 1572 | 1571 | }else |
| 1573 | 1572 | #endif |
| 1574 | 1573 |
| --- src/main.c | |
| +++ src/main.c | |
| @@ -298,15 +298,16 @@ | |
| 298 | */ |
| 299 | static int name_search( |
| 300 | const char *zName, /* The name we are looking for */ |
| 301 | const NameMap *aMap, /* Search in this array */ |
| 302 | int nMap, /* Number of slots in aMap[] */ |
| 303 | int *pIndex /* OUT: The index in aMap[] of the match */ |
| 304 | ){ |
| 305 | int upr, lwr, cnt, m, i; |
| 306 | int n = strlen(zName); |
| 307 | lwr = 0; |
| 308 | upr = nMap-1; |
| 309 | while( lwr<=upr ){ |
| 310 | int mid, c; |
| 311 | mid = (upr+lwr)/2; |
| 312 | c = fossil_strcmp(zName, aMap[mid].zName); |
| @@ -318,11 +319,11 @@ | |
| 318 | }else{ |
| 319 | lwr = mid + 1; |
| 320 | } |
| 321 | } |
| 322 | for(m=cnt=0, i=upr-2; cnt<2 && i<=upr+3 && i<nMap; i++){ |
| 323 | if( i<0 ) continue; |
| 324 | if( strncmp(zName, aMap[i].zName, n)==0 ){ |
| 325 | m = i; |
| 326 | cnt++; |
| 327 | } |
| 328 | } |
| @@ -655,13 +656,11 @@ | |
| 655 | } |
| 656 | #ifndef _WIN32 |
| 657 | if( !is_valid_fd(2) ) fossil_panic("file descriptor 2 not open"); |
| 658 | /* if( is_valid_fd(3) ) fossil_warning("file descriptor 3 is open"); */ |
| 659 | #endif |
| 660 | rc = name_search(zCmdName, aCommand + count(aWebpage), |
| 661 | count(aCommand) - count(aWebpage), &idx); |
| 662 | idx += count(aWebpage); |
| 663 | if( rc==1 ){ |
| 664 | #ifdef FOSSIL_ENABLE_TH1_HOOKS |
| 665 | if( !g.isHTTP && !g.fNoThHook ){ |
| 666 | rc = Th_CommandHook(zCmdName, 0); |
| 667 | }else{ |
| @@ -1017,11 +1016,11 @@ | |
| 1017 | zCmdOrPagePlural = "pages"; |
| 1018 | }else{ |
| 1019 | zCmdOrPage = "command"; |
| 1020 | zCmdOrPagePlural = "commands"; |
| 1021 | } |
| 1022 | rc = name_search(g.argv[2], aCommand, count(aCommand), &idx); |
| 1023 | if( rc==1 ){ |
| 1024 | fossil_print("unknown %s: %s\nAvailable %s:\n", |
| 1025 | zCmdOrPage, g.argv[2], zCmdOrPagePlural); |
| 1026 | command_list(0, isPage ? CMDFLAG_WEBPAGE : (0xff & ~CMDFLAG_WEBPAGE)); |
| 1027 | fossil_exit(1); |
| @@ -1061,11 +1060,11 @@ | |
| 1061 | int rc, idx; |
| 1062 | char *z, *s, *d; |
| 1063 | char const * zCmdOrPage = ('/'==*zCmd) ? "page" : "command"; |
| 1064 | style_submenu_element("Command-List", "Command-List", "%s/help", g.zTop); |
| 1065 | @ <h1>The "%s(zCmd)" %s(zCmdOrPage):</h1> |
| 1066 | rc = name_search(zCmd, aCommand, count(aCommand), &idx); |
| 1067 | if( rc==1 ){ |
| 1068 | @ unknown command: %s(zCmd) |
| 1069 | }else if( rc==2 ){ |
| 1070 | @ ambiguous command prefix: %s(zCmd) |
| 1071 | }else{ |
| @@ -1563,11 +1562,11 @@ | |
| 1563 | } |
| 1564 | |
| 1565 | /* Locate the method specified by the path and execute the function |
| 1566 | ** that implements that method. |
| 1567 | */ |
| 1568 | if( name_search(g.zPath, aWebpage, count(aWebpage), &idx) ){ |
| 1569 | #ifdef FOSSIL_ENABLE_JSON |
| 1570 | if(g.json.isJsonMode){ |
| 1571 | json_err(FSL_JSON_E_RESOURCE_NOT_FOUND,NULL,0); |
| 1572 | }else |
| 1573 | #endif |
| 1574 |
| --- src/main.c | |
| +++ src/main.c | |
| @@ -298,15 +298,16 @@ | |
| 298 | */ |
| 299 | static int name_search( |
| 300 | const char *zName, /* The name we are looking for */ |
| 301 | const NameMap *aMap, /* Search in this array */ |
| 302 | int nMap, /* Number of slots in aMap[] */ |
| 303 | int iBegin, /* Lower bound on the array search */ |
| 304 | int *pIndex /* OUT: The index in aMap[] of the match */ |
| 305 | ){ |
| 306 | int upr, lwr, cnt, m, i; |
| 307 | int n = strlen(zName); |
| 308 | lwr = iBegin; |
| 309 | upr = nMap-1; |
| 310 | while( lwr<=upr ){ |
| 311 | int mid, c; |
| 312 | mid = (upr+lwr)/2; |
| 313 | c = fossil_strcmp(zName, aMap[mid].zName); |
| @@ -318,11 +319,11 @@ | |
| 319 | }else{ |
| 320 | lwr = mid + 1; |
| 321 | } |
| 322 | } |
| 323 | for(m=cnt=0, i=upr-2; cnt<2 && i<=upr+3 && i<nMap; i++){ |
| 324 | if( i<iBegin ) continue; |
| 325 | if( strncmp(zName, aMap[i].zName, n)==0 ){ |
| 326 | m = i; |
| 327 | cnt++; |
| 328 | } |
| 329 | } |
| @@ -655,13 +656,11 @@ | |
| 656 | } |
| 657 | #ifndef _WIN32 |
| 658 | if( !is_valid_fd(2) ) fossil_panic("file descriptor 2 not open"); |
| 659 | /* if( is_valid_fd(3) ) fossil_warning("file descriptor 3 is open"); */ |
| 660 | #endif |
| 661 | rc = name_search(zCmdName, aCommand, count(aCommand), FOSSIL_FIRST_CMD, &idx); |
| 662 | if( rc==1 ){ |
| 663 | #ifdef FOSSIL_ENABLE_TH1_HOOKS |
| 664 | if( !g.isHTTP && !g.fNoThHook ){ |
| 665 | rc = Th_CommandHook(zCmdName, 0); |
| 666 | }else{ |
| @@ -1017,11 +1016,11 @@ | |
| 1016 | zCmdOrPagePlural = "pages"; |
| 1017 | }else{ |
| 1018 | zCmdOrPage = "command"; |
| 1019 | zCmdOrPagePlural = "commands"; |
| 1020 | } |
| 1021 | rc = name_search(g.argv[2], aCommand, count(aCommand), 0, &idx); |
| 1022 | if( rc==1 ){ |
| 1023 | fossil_print("unknown %s: %s\nAvailable %s:\n", |
| 1024 | zCmdOrPage, g.argv[2], zCmdOrPagePlural); |
| 1025 | command_list(0, isPage ? CMDFLAG_WEBPAGE : (0xff & ~CMDFLAG_WEBPAGE)); |
| 1026 | fossil_exit(1); |
| @@ -1061,11 +1060,11 @@ | |
| 1060 | int rc, idx; |
| 1061 | char *z, *s, *d; |
| 1062 | char const * zCmdOrPage = ('/'==*zCmd) ? "page" : "command"; |
| 1063 | style_submenu_element("Command-List", "Command-List", "%s/help", g.zTop); |
| 1064 | @ <h1>The "%s(zCmd)" %s(zCmdOrPage):</h1> |
| 1065 | rc = name_search(zCmd, aCommand, count(aCommand), 0, &idx); |
| 1066 | if( rc==1 ){ |
| 1067 | @ unknown command: %s(zCmd) |
| 1068 | }else if( rc==2 ){ |
| 1069 | @ ambiguous command prefix: %s(zCmd) |
| 1070 | }else{ |
| @@ -1563,11 +1562,11 @@ | |
| 1562 | } |
| 1563 | |
| 1564 | /* Locate the method specified by the path and execute the function |
| 1565 | ** that implements that method. |
| 1566 | */ |
| 1567 | if( name_search(g.zPath, aWebpage, count(aWebpage), 0, &idx) ){ |
| 1568 | #ifdef FOSSIL_ENABLE_JSON |
| 1569 | if(g.json.isJsonMode){ |
| 1570 | json_err(FSL_JSON_E_RESOURCE_NOT_FOUND,NULL,0); |
| 1571 | }else |
| 1572 | #endif |
| 1573 |
+2
-1
| --- src/mkindex.c | ||
| +++ src/mkindex.c | ||
| @@ -283,11 +283,11 @@ | ||
| 283 | 283 | ); |
| 284 | 284 | for(i=0; i<nFixed /*&& aEntry[i].eType==1*/; i++){ |
| 285 | 285 | const char *z = aEntry[i].zPath; |
| 286 | 286 | int n = strlen(z); |
| 287 | 287 | int cmdFlags = (1==aEntry[i].eType) ? 0x01 : 0x08; |
| 288 | - if(0x01==cmdFlags){ | |
| 288 | + if( 0x01==cmdFlags ){ | |
| 289 | 289 | if( z[n-1]=='*' ){ |
| 290 | 290 | n--; |
| 291 | 291 | cmdFlags = 0x02; |
| 292 | 292 | }else if( memcmp(z, "test-", 5)==0 ){ |
| 293 | 293 | cmdFlags = 0x04; |
| @@ -303,10 +303,11 @@ | ||
| 303 | 303 | cmdFlags |
| 304 | 304 | ); |
| 305 | 305 | if( aEntry[i].zIf ) printf("#endif\n"); |
| 306 | 306 | } |
| 307 | 307 | printf("};\n"); |
| 308 | + printf("#define FOSSIL_FIRST_CMD (sizeof(aWebpage)/sizeof(aWebpage[0]))\n"); | |
| 308 | 309 | for(i=0; i<nFixed; i++){ |
| 309 | 310 | char *z = aEntry[i].zHelp; |
| 310 | 311 | if( z && z[0] ){ |
| 311 | 312 | if( aEntry[i].zIf ) printf("%s", aEntry[i].zIf); |
| 312 | 313 | printf("static const char zHelp_%s[] = \n", aEntry[i].zFunc); |
| 313 | 314 |
| --- src/mkindex.c | |
| +++ src/mkindex.c | |
| @@ -283,11 +283,11 @@ | |
| 283 | ); |
| 284 | for(i=0; i<nFixed /*&& aEntry[i].eType==1*/; i++){ |
| 285 | const char *z = aEntry[i].zPath; |
| 286 | int n = strlen(z); |
| 287 | int cmdFlags = (1==aEntry[i].eType) ? 0x01 : 0x08; |
| 288 | if(0x01==cmdFlags){ |
| 289 | if( z[n-1]=='*' ){ |
| 290 | n--; |
| 291 | cmdFlags = 0x02; |
| 292 | }else if( memcmp(z, "test-", 5)==0 ){ |
| 293 | cmdFlags = 0x04; |
| @@ -303,10 +303,11 @@ | |
| 303 | cmdFlags |
| 304 | ); |
| 305 | if( aEntry[i].zIf ) printf("#endif\n"); |
| 306 | } |
| 307 | printf("};\n"); |
| 308 | for(i=0; i<nFixed; i++){ |
| 309 | char *z = aEntry[i].zHelp; |
| 310 | if( z && z[0] ){ |
| 311 | if( aEntry[i].zIf ) printf("%s", aEntry[i].zIf); |
| 312 | printf("static const char zHelp_%s[] = \n", aEntry[i].zFunc); |
| 313 |
| --- src/mkindex.c | |
| +++ src/mkindex.c | |
| @@ -283,11 +283,11 @@ | |
| 283 | ); |
| 284 | for(i=0; i<nFixed /*&& aEntry[i].eType==1*/; i++){ |
| 285 | const char *z = aEntry[i].zPath; |
| 286 | int n = strlen(z); |
| 287 | int cmdFlags = (1==aEntry[i].eType) ? 0x01 : 0x08; |
| 288 | if( 0x01==cmdFlags ){ |
| 289 | if( z[n-1]=='*' ){ |
| 290 | n--; |
| 291 | cmdFlags = 0x02; |
| 292 | }else if( memcmp(z, "test-", 5)==0 ){ |
| 293 | cmdFlags = 0x04; |
| @@ -303,10 +303,11 @@ | |
| 303 | cmdFlags |
| 304 | ); |
| 305 | if( aEntry[i].zIf ) printf("#endif\n"); |
| 306 | } |
| 307 | printf("};\n"); |
| 308 | printf("#define FOSSIL_FIRST_CMD (sizeof(aWebpage)/sizeof(aWebpage[0]))\n"); |
| 309 | for(i=0; i<nFixed; i++){ |
| 310 | char *z = aEntry[i].zHelp; |
| 311 | if( z && z[0] ){ |
| 312 | if( aEntry[i].zIf ) printf("%s", aEntry[i].zIf); |
| 313 | printf("static const char zHelp_%s[] = \n", aEntry[i].zFunc); |
| 314 |