Skip to content

Commit 5fede00

Browse files
committed
Sh: make the way to extract variables more robust
Signed-off-by: Masatake YAMATO <[email protected]>
1 parent ff7e88e commit 5fede00

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

Tmain/ptag-in-optlib-parser.d/stdout-expected.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
!_TAG_KIND_DESCRIPTION!Sh f,function /functions/
1111
!_TAG_KIND_DESCRIPTION!Sh h,heredoc /labels for here document/
1212
!_TAG_KIND_DESCRIPTION!Sh s,script /script files/
13+
!_TAG_KIND_DESCRIPTION!Sh v,variable /variables assigment (experimental)/
1314
!_TAG_OUTPUT_EXCMD mixed /number, pattern, mixed, or combineV2/
1415
!_TAG_OUTPUT_FILESEP slash /slash or backslash/
1516
!_TAG_OUTPUT_MODE u-ctags /u-ctags or e-ctags/

parsers/sh.c

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -459,6 +459,27 @@ static size_t handleZshKeyword (int keyword,
459459
return vStringLength(token);
460460
}
461461

462+
static bool doesLineCotinue(const unsigned char *start, const unsigned char *cp)
463+
{
464+
cp--;
465+
if (start >= cp)
466+
return false;
467+
468+
if (*cp != '\\')
469+
return false;
470+
471+
while (start < cp)
472+
{
473+
if (*cp == ';' || *cp == '|' || *cp == '&')
474+
return false;
475+
else if (isspace(*cp))
476+
cp--;
477+
else
478+
return true;
479+
}
480+
return false;
481+
}
482+
462483
static bool handleVariableAssignment (vString *input)
463484
{
464485
const char *base = vStringValue (input);
@@ -503,6 +524,7 @@ static void findShTagsCommon (size_t (* keyword_handler) (int,
503524
struct hereDocParsingState hstate;
504525
hdocStateInit (&hstate);
505526

527+
bool cont_line = false;
506528
while ((line = readLineFromInputFile ()) != NULL)
507529
{
508530
const unsigned char* cp = line;
@@ -528,10 +550,12 @@ static void findShTagsCommon (size_t (* keyword_handler) (int,
528550
vStringDelete (hereDocDelimiter);
529551
hereDocDelimiter = NULL;
530552
}
553+
cont_line = false;
531554
continue;
532555
}
533556

534557
hdocStateClear (&hstate);
558+
bool beginning_of_line = !cont_line;
535559
while (*cp != '\0')
536560
{
537561
subparser *sub = NULL;
@@ -669,6 +693,7 @@ static void findShTagsCommon (size_t (* keyword_handler) (int,
669693
cp += d;
670694
else if (*cp != '\0')
671695
++cp;
696+
beginning_of_line = false;
672697
continue;
673698
}
674699

@@ -710,7 +735,8 @@ static void findShTagsCommon (size_t (* keyword_handler) (int,
710735
++cp;
711736
}
712737
}
713-
else if (found_kind == K_NOTHING
738+
else if (beginning_of_line
739+
&& found_kind == K_NOTHING
714740
&& handleVariableAssignment (name))
715741
found_kind = K_VARIABLE;
716742

@@ -729,7 +755,11 @@ static void findShTagsCommon (size_t (* keyword_handler) (int,
729755
else if (!hereDocDelimiter)
730756
hdocStateUpdateArgs (&hstate, name);
731757
vStringClear (name);
758+
beginning_of_line = false;
732759
}
760+
if (*cp == '#')
761+
cont_line = false;
762+
cont_line = doesLineCotinue (line, cp);
733763
}
734764
hdocStateFini (&hstate);
735765
vStringDelete (name);

0 commit comments

Comments
 (0)