Fossil SCM

Add a regexp command to TH1.

mistachkin 2013-03-14 22:16 trunk merge
Commit d772ff72199d65f3bdf6f025635d0fdd7860d4f8
1 file changed +44
--- src/th_main.c
+++ src/th_main.c
@@ -663,10 +663,53 @@
663663
return TH_ERROR;
664664
}
665665
}
666666
return res;
667667
}
668
+
669
+/*
670
+** TH1 command: regexp ?-nocase? ?--? exp string
671
+**
672
+** Checks the string against the specified regular expression and returns
673
+** non-zero if it matches. If the regular expression is invalid or cannot
674
+** be compiled, an error will be generated.
675
+*/
676
+#define REGEXP_WRONGNUMARGS "regexp ?-nocase? ?--? exp string"
677
+static int regexpCmd(
678
+ Th_Interp *interp,
679
+ void *p,
680
+ int argc,
681
+ const char **argv,
682
+ int *argl
683
+){
684
+ int rc;
685
+ int noCase = 0;
686
+ int nArg = 1;
687
+ ReCompiled *pRe = 0;
688
+ const char *zErr;
689
+ if( argc<3 || argc>5 ){
690
+ return Th_WrongNumArgs(interp, REGEXP_WRONGNUMARGS);
691
+ }
692
+ if( fossil_strcmp(argv[nArg], "-nocase")==0 ){
693
+ noCase = 1; nArg++;
694
+ }
695
+ if( fossil_strcmp(argv[nArg], "--")==0 ) nArg++;
696
+ if( nArg+2!=argc ){
697
+ return Th_WrongNumArgs(interp, REGEXP_WRONGNUMARGS);
698
+ }
699
+ zErr = re_compile(&pRe, argv[nArg], noCase);
700
+ if( !zErr ){
701
+ Th_SetResultInt(interp, re_match(pRe,
702
+ (const unsigned char *)argv[nArg+1], argl[nArg+1]));
703
+ rc = TH_OK;
704
+ }else{
705
+ Th_SetResult(interp, zErr, -1);
706
+ rc = TH_ERROR;
707
+ }
708
+ re_free(pRe);
709
+ return rc;
710
+}
668711
669712
/*
670713
** Make sure the interpreter has been initialized. Initialize it if
671714
** it has not been already.
672715
**
@@ -691,10 +734,11 @@
691734
{"htmlize", htmlizeCmd, 0},
692735
{"linecount", linecntCmd, 0},
693736
{"puts", putsCmd, (void*)&aFlags[1]},
694737
{"query", queryCmd, 0},
695738
{"randhex", randhexCmd, 0},
739
+ {"regexp", regexpCmd, 0},
696740
{"repository", repositoryCmd, 0},
697741
{"stime", stimeCmd, 0},
698742
{"utime", utimeCmd, 0},
699743
{"wiki", wikiCmd, (void*)&aFlags[0]},
700744
{0, 0, 0}
701745
--- src/th_main.c
+++ src/th_main.c
@@ -663,10 +663,53 @@
663 return TH_ERROR;
664 }
665 }
666 return res;
667 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
668
669 /*
670 ** Make sure the interpreter has been initialized. Initialize it if
671 ** it has not been already.
672 **
@@ -691,10 +734,11 @@
691 {"htmlize", htmlizeCmd, 0},
692 {"linecount", linecntCmd, 0},
693 {"puts", putsCmd, (void*)&aFlags[1]},
694 {"query", queryCmd, 0},
695 {"randhex", randhexCmd, 0},
 
696 {"repository", repositoryCmd, 0},
697 {"stime", stimeCmd, 0},
698 {"utime", utimeCmd, 0},
699 {"wiki", wikiCmd, (void*)&aFlags[0]},
700 {0, 0, 0}
701
--- src/th_main.c
+++ src/th_main.c
@@ -663,10 +663,53 @@
663 return TH_ERROR;
664 }
665 }
666 return res;
667 }
668
669 /*
670 ** TH1 command: regexp ?-nocase? ?--? exp string
671 **
672 ** Checks the string against the specified regular expression and returns
673 ** non-zero if it matches. If the regular expression is invalid or cannot
674 ** be compiled, an error will be generated.
675 */
676 #define REGEXP_WRONGNUMARGS "regexp ?-nocase? ?--? exp string"
677 static int regexpCmd(
678 Th_Interp *interp,
679 void *p,
680 int argc,
681 const char **argv,
682 int *argl
683 ){
684 int rc;
685 int noCase = 0;
686 int nArg = 1;
687 ReCompiled *pRe = 0;
688 const char *zErr;
689 if( argc<3 || argc>5 ){
690 return Th_WrongNumArgs(interp, REGEXP_WRONGNUMARGS);
691 }
692 if( fossil_strcmp(argv[nArg], "-nocase")==0 ){
693 noCase = 1; nArg++;
694 }
695 if( fossil_strcmp(argv[nArg], "--")==0 ) nArg++;
696 if( nArg+2!=argc ){
697 return Th_WrongNumArgs(interp, REGEXP_WRONGNUMARGS);
698 }
699 zErr = re_compile(&pRe, argv[nArg], noCase);
700 if( !zErr ){
701 Th_SetResultInt(interp, re_match(pRe,
702 (const unsigned char *)argv[nArg+1], argl[nArg+1]));
703 rc = TH_OK;
704 }else{
705 Th_SetResult(interp, zErr, -1);
706 rc = TH_ERROR;
707 }
708 re_free(pRe);
709 return rc;
710 }
711
712 /*
713 ** Make sure the interpreter has been initialized. Initialize it if
714 ** it has not been already.
715 **
@@ -691,10 +734,11 @@
734 {"htmlize", htmlizeCmd, 0},
735 {"linecount", linecntCmd, 0},
736 {"puts", putsCmd, (void*)&aFlags[1]},
737 {"query", queryCmd, 0},
738 {"randhex", randhexCmd, 0},
739 {"regexp", regexpCmd, 0},
740 {"repository", repositoryCmd, 0},
741 {"stime", stimeCmd, 0},
742 {"utime", utimeCmd, 0},
743 {"wiki", wikiCmd, (void*)&aFlags[0]},
744 {0, 0, 0}
745

Keyboard Shortcuts

Open search /
Next entry (timeline) j
Previous entry (timeline) k
Open focused entry Enter
Show this help ?
Toggle theme Top nav button