| | @@ -130,10 +130,15 @@ |
| 130 | 130 | /* |
| 131 | 131 | ** Number of errors |
| 132 | 132 | */ |
| 133 | 133 | int nErr = 0; |
| 134 | 134 | |
| 135 | +/* |
| 136 | +** File name for completion bash script |
| 137 | +*/ |
| 138 | +char *zComp; |
| 139 | + |
| 135 | 140 | /* |
| 136 | 141 | ** Duplicate N characters of a string. |
| 137 | 142 | */ |
| 138 | 143 | char *string_dup(const char *zSrc, int n){ |
| 139 | 144 | char *z; |
| | @@ -315,24 +320,36 @@ |
| 315 | 320 | } |
| 316 | 321 | |
| 317 | 322 | /* |
| 318 | 323 | ** Compare two entries |
| 319 | 324 | */ |
| 320 | | -int e_compare(const void *a, const void *b){ |
| 325 | +int e_compare_path(const void *a, const void *b){ |
| 321 | 326 | const Entry *pA = (const Entry*)a; |
| 322 | 327 | const Entry *pB = (const Entry*)b; |
| 323 | 328 | return strcmp(pA->zPath, pB->zPath); |
| 324 | 329 | } |
| 330 | + |
| 331 | +/* |
| 332 | +** Compare two entries by type |
| 333 | +*/ |
| 334 | +int e_compare_type(const void *a, const void *b){ |
| 335 | + const Entry *pA = (const Entry*)a; |
| 336 | + const Entry *pB = (const Entry*)b; |
| 337 | + if( pA->eType < pB->eType ) return -1; |
| 338 | + if( pA->eType > pB->eType ) return 1; |
| 339 | + return 0; |
| 340 | +} |
| 325 | 341 | |
| 326 | 342 | /* |
| 327 | 343 | ** Build the binary search table. |
| 328 | 344 | */ |
| 329 | 345 | void build_table(void){ |
| 330 | 346 | int i; |
| 331 | 347 | int nWeb = 0; |
| 348 | + FILE *comp; |
| 332 | 349 | |
| 333 | | - qsort(aEntry, nFixed, sizeof(aEntry[0]), e_compare); |
| 350 | + qsort(aEntry, nFixed, sizeof(aEntry[0]), e_compare_path); |
| 334 | 351 | |
| 335 | 352 | printf( |
| 336 | 353 | "/* Automatically generated code\n" |
| 337 | 354 | "** DO NOT EDIT!\n" |
| 338 | 355 | "**\n" |
| | @@ -388,10 +405,51 @@ |
| 388 | 405 | ); |
| 389 | 406 | if( aEntry[i].zIf ) printf("#endif\n"); |
| 390 | 407 | } |
| 391 | 408 | printf("};\n"); |
| 392 | 409 | printf("#define FOSSIL_FIRST_CMD %d\n", nWeb); |
| 410 | + |
| 411 | + /* Generate bash completion script */ |
| 412 | + comp = fopen(zComp, "w"); |
| 413 | + if( comp==0 ){ |
| 414 | + fprintf(stderr,"%s: cannot open completion script file\n", zComp); |
| 415 | + }else{ |
| 416 | + qsort(aEntry, nFixed, sizeof(aEntry[0]), e_compare_type); |
| 417 | + |
| 418 | + fprintf(comp, |
| 419 | + "_fossil_comp(){\n" |
| 420 | + "local cur opts1 opts2\n" |
| 421 | + "COMPREPLY=()\n" |
| 422 | + "cur=\"${COMP_WORDS[COMP_CWORD]}\"\n" |
| 423 | + ); |
| 424 | + fprintf(comp, "opts1=\""); |
| 425 | + for(i=nWeb+1; |
| 426 | + i<nFixed && aEntry[i].eType==(CMDFLAG_COMMAND|CMDFLAG_1ST_TIER); |
| 427 | + i++) |
| 428 | + { |
| 429 | + fprintf(comp, "%s ", aEntry[i].zPath); |
| 430 | + } |
| 431 | + fprintf(comp, "\"\n"); |
| 432 | + fprintf(comp, "opts2=\""); |
| 433 | + for( ; |
| 434 | + i<nFixed && aEntry[i].eType==(CMDFLAG_COMMAND|CMDFLAG_2ND_TIER); |
| 435 | + i++) |
| 436 | + { |
| 437 | + fprintf(comp, "%s ", aEntry[i].zPath); |
| 438 | + } |
| 439 | + fprintf(comp, "\"\n"); |
| 440 | + fprintf(comp, |
| 441 | + "COMPREPLY=($(compgen -W \"${opts1}\" -- ${cur}))\n" |
| 442 | + "if [ ${#COMPREPLY[@]} -eq 0 ]; then\n" |
| 443 | + "COMPREPLY=($(compgen -W \"${opts2}\" -- ${cur}))\n" |
| 444 | + "fi\n" |
| 445 | + "return 0\n" |
| 446 | + "}\n" |
| 447 | + "complete -F _fossil_comp fossil" |
| 448 | + ); |
| 449 | + fclose(comp); |
| 450 | + } |
| 393 | 451 | } |
| 394 | 452 | |
| 395 | 453 | /* |
| 396 | 454 | ** Process a single file of input |
| 397 | 455 | */ |
| | @@ -415,11 +473,18 @@ |
| 415 | 473 | } |
| 416 | 474 | |
| 417 | 475 | int main(int argc, char **argv){ |
| 418 | 476 | int i; |
| 419 | 477 | for(i=1; i<argc; i++){ |
| 478 | + if( argv[i][0]=='-' && strcmp(argv[i], "--compfile")==0 ){ |
| 479 | + if( ++i < argc ){ |
| 480 | + zComp = argv[i]; |
| 481 | + }else{ |
| 482 | + fprintf(stderr,"no file name supplied to --compfile\n"); |
| 483 | + } |
| 484 | + } |
| 420 | 485 | zFile = argv[i]; |
| 421 | 486 | process_file(); |
| 422 | 487 | } |
| 423 | 488 | build_table(); |
| 424 | 489 | return nErr; |
| 425 | 490 | } |
| 426 | 491 | |