File: sh.c

package info (click to toggle)
codelite 17.0.0%2Bdfsg-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 136,384 kB
  • sloc: cpp: 491,550; ansic: 280,393; php: 10,259; sh: 8,930; lisp: 7,664; vhdl: 6,518; python: 6,020; lex: 4,920; yacc: 3,123; perl: 2,385; javascript: 1,715; cs: 1,193; xml: 1,110; makefile: 805; cobol: 741; sql: 709; ruby: 620; f90: 566; ada: 534; asm: 464; fortran: 350; objc: 289; tcl: 258; java: 157; erlang: 61; pascal: 51; ml: 49; awk: 44; haskell: 36
file content (816 lines) | stat: -rw-r--r-- 18,220 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
/*
*   Copyright (c) 2000-2002, Darren Hiebert
*
*   This source code is released for free distribution under the terms of the
*   GNU General Public License version 2 or (at your option) any later version.
*
*   This module contains functions for generating tags for scripts for the
*   Bourne shell (and its derivatives, the Korn and Z shells).
*/

/*
*   INCLUDE FILES
*/
#include "general.h"  /* must always come first */

#include <string.h>

#include "debug.h"
#include "entry.h"
#include "keyword.h"
#include "kind.h"
#include "parse.h"
#include "read.h"
#include "promise.h"
#include "routines.h"
#include "vstring.h"
#include "xtag.h"

#include "sh.h"

/*
*   DATA DEFINITIONS
*/
typedef enum {
	K_SUBPARSER = -2,
	K_NOTHING = -1,		/* place holder. Never appears on tags file. */
	K_ALIAS,
	K_FUNCTION,
	K_SCRIPT,
	K_HEREDOCLABEL,
} shKind;

typedef enum {
	R_SCRIPT_LOADED,
	LAST_R_SCRIPT,
} shScriptRole;

typedef enum {
	R_ZSH_SCRIPT_AUTOLOADED = LAST_R_SCRIPT,
} zshScriptRole;

#define SH_SCRIPT_ROLES_COMMON 	{ true, "loaded", "loaded" }
static roleDefinition ShScriptRoles [] = {
	SH_SCRIPT_ROLES_COMMON,
};

static roleDefinition ZshScriptRoles [] = {
	SH_SCRIPT_ROLES_COMMON,
	{ true, "autoloaded", "autoloaded" },
};

typedef enum {
	R_HEREDOC_ENDMARKER,
} shHeredocRole;

#define SH_HEREDOC_ROLES_COMMON { true, "endmarker", "end marker" }
static roleDefinition ShHeredocRoles [] = {
	SH_HEREDOC_ROLES_COMMON,
};

static roleDefinition ZshHeredocRoles [] = {
	SH_HEREDOC_ROLES_COMMON,
};

typedef enum {
	R_ZSH_FUNCTION_AUTOLOADED,
} zshFunctionRole;

static roleDefinition ZshFunctionRoles [] = {
	{ true, "autoloaded", "function name passed to autoload built-in command" },
};

#define SH_KINDS_COMMON(SCRIPT_ROLES,HEREDOC_ROLES, FUNCTION_ROLES_SPEC) \
	{ true, 'a', "alias", "aliases"},							\
	{ true, 'f', "function", "functions",						\
	  .referenceOnly = false, FUNCTION_ROLES_SPEC },			\
	{ true, 's', "script", "script files",						\
	  .referenceOnly = true, ATTACH_ROLES (SCRIPT_ROLES) },		\
	{ true, 'h', "heredoc", "label for here document",			\
	  .referenceOnly = false, ATTACH_ROLES (HEREDOC_ROLES) }

static kindDefinition ShKinds [] = {
	SH_KINDS_COMMON(ShScriptRoles, ShHeredocRoles,),
};

static kindDefinition ZshKinds [] = {
	SH_KINDS_COMMON(ZshScriptRoles, ZshHeredocRoles,
					ATTACH_ROLES(ZshFunctionRoles)),
};

enum eShKeywordId {
	KEYWORD_function,
	KEYWORD_alias,
	KEYWORD_source,
	KEYWORD_autoload,
};

const char *dialectMap [] = {
	"Sh", "Zsh",
};

static const struct dialectalKeyword KeywordTable [] = {
	{ "function",  KEYWORD_function, { 1, 1 } },
	{ "alias",     KEYWORD_alias,    { 1, 1 } },
	{ "source",    KEYWORD_source,   { 1, 1 } },
	{ "autoload",  KEYWORD_autoload, { 0, 1 } },
};

/*
* FUNCTION DECLARATIONS
*/
static subparser *notifyLineToSubparsers (const unsigned char *cp,
										  int *n);
static int extractNameToSubparser (subparser *sub, const unsigned char *cp,
								   vString *name);
static int makeTagInSubparser (subparser *sub, vString *name);

/*
*   FUNCTION DEFINITIONS
*/

static bool isFileChar  (int c)
{
	return (isalnum (c)
		|| c == '_' || c == '-'
		|| c == '/' || c == '.'
		|| c == '+' || c == '^'
		|| c == '%' || c == '@'
		|| c == '~');
}

static bool isIdentChar0 (int c)
{
	return (isalpha (c) || c == '_' || c == '-');
}

static bool isIdentChar (int c)
{
	return (isalnum (c) || c == '_' || c == '-');
}

/* bash allows all kinds of crazy stuff as the identifier after 'function' */
static bool isBashFunctionChar (int c)
{
	return (c > 1 /* NUL and SOH are disallowed */ && c != 0x7f &&
	        /* blanks are disallowed, but VT and FF (and CR to some extent, but
	         * let's not fall into the pit of craziness) */
	        c != ' ' && c != '\t' && c != '\n' && c != '\r' &&
	        c != '"' && c != '\'' && c != '$' && c != '`' && c != '\\' &&
	        c != '&' && c != ';' &&
	        c != '(' && c != ')' &&
	        c != '<' && c != '>');
}

static const unsigned char *skipDoubleString (const unsigned char *cp)
{
	const unsigned char* prev = cp;
	cp++;
	while ((*cp != '"' || *prev == '\\') && *cp != '\0')
	{
		prev = cp;
		cp++;
	}
	return cp;
}

static const unsigned char *skipSingleString (const unsigned char *cp)
{
	cp++;
	while (*cp != '\'' && *cp != '\0')
		cp++;
	return cp;
}

static bool isEnvCommand (const vString *cmd)
{
	const char *lc = vStringValue(cmd);
	const char * tmp = baseFilename (lc);

	return (strcmp(tmp, "env") == 0);
}

static int readDestfileName (const unsigned char *cp, vString *destfile)
{
	const unsigned char *origin = cp;

	while (isspace ((int) *cp))
		++cp;

	/* >... */
	if (*cp != '>')
		return 0;

	/* >>... */
	if (*cp == '>')
		++cp;

	while (isspace ((int) *cp))
		++cp;

	if (!isFileChar ((int) *cp))
		return 0;

	vStringClear(destfile);
	do {
		vStringPut (destfile, (int) *cp);
		++cp;
	} while (isFileChar ((int) *cp));

	if (vStringLength(destfile) > 0)
		return cp - origin;

	return 0;
}

struct hereDocParsingState {
	vString *args[2];
	vString *destfile;
	langType sublang;
	unsigned long startLine;

	int corkIndex;
};

static void hdocStateInit (struct hereDocParsingState *hstate)
{
	hstate->args[0] = vStringNew ();
	hstate->args[1] = vStringNew ();
	hstate->destfile = vStringNew ();

	hstate->corkIndex = CORK_NIL;
	hstate->sublang = LANG_IGNORE;
}

static void hdocStateClear (struct hereDocParsingState *hstate)
{
	vStringClear (hstate->args[0]);
	vStringClear (hstate->args[1]);
	vStringClear (hstate->destfile);
}

static void hdocStateFini (struct hereDocParsingState *hstate)
{
	vStringDelete (hstate->args[0]);
	vStringDelete (hstate->args[1]);
	vStringDelete (hstate->destfile);
}

static void hdocStateUpdateArgs (struct hereDocParsingState *hstate,
										   vString *name)
{
	if (vStringIsEmpty(hstate->args[0]))
		vStringCopy(hstate->args[0], name);
	else if (vStringIsEmpty(hstate->args[1]))
		vStringCopy(hstate->args[1], name);
}

static void hdocStateMakePromiseMaybe (struct hereDocParsingState *hstate)
{
	if (hstate->sublang != LANG_IGNORE)
		makePromise (getLanguageName(hstate->sublang),
					 hstate->startLine, 0,
					 getInputLineNumber(), 0,
					 0);
	hstate->sublang = LANG_IGNORE;
}

static void hdocStateRecordStartlineFromDestfileMaybe (struct hereDocParsingState *hstate)
{
	const char *f = vStringValue(hstate->destfile);

	if (hstate->sublang != LANG_IGNORE)
		return;

	hstate->sublang = getLanguageForFilename (f, 0);
	if (hstate->sublang != LANG_IGNORE)
		hstate->startLine = getInputLineNumber () + 1;
	vStringClear (hstate->destfile);
}

static void hdocStateRecordStatelineMaybe (struct hereDocParsingState *hstate)
{
	if (!vStringIsEmpty(hstate->args[0]))
	{
		const char *cmd;

		cmd = vStringValue(hstate->args[0]);
		if (isEnvCommand (hstate->args[0]))
		{
			cmd = NULL;
			if (!vStringIsEmpty(hstate->args[1]))
				cmd = vStringValue(hstate->args[1]);
		}

		if (cmd)
		{
			hstate->sublang = getLanguageForCommand (cmd, 0);
			if (hstate->sublang != LANG_IGNORE)
				hstate->startLine = getInputLineNumber () + 1;
		}
	}

	if (vStringLength(hstate->destfile) > 0)
		hdocStateRecordStartlineFromDestfileMaybe (hstate);
}

static int hdocStateReadDestfileName (struct hereDocParsingState *hstate,
									  const unsigned char* cp,
									  const vString *const hereDocDelimiter)
{
	int d = readDestfileName (cp, hstate->destfile);

	if (d > 0 && hereDocDelimiter)
		hdocStateRecordStartlineFromDestfileMaybe (hstate);

	return d;
}

static void hdocStateUpdateTag (struct hereDocParsingState *hstate, unsigned long endLine)
{
	tagEntryInfo *tag = getEntryInCorkQueue (hstate->corkIndex);
	if (tag)
	{
		tag->extensionFields.endLine = endLine;
		hstate->corkIndex = CORK_NIL;
	}
}

static size_t handleShKeyword (int keyword,
							   vString *token,
							   int *kind,
							   int *role,
							   bool (** check_char)(int))
{
	switch (keyword)
	{
	case KEYWORD_function:
		*kind = K_FUNCTION;
		break;
	case KEYWORD_alias:
		*kind = K_ALIAS;
		*check_char = isIdentChar;
		break;
	case KEYWORD_source:
		*kind = K_SCRIPT;
		*role = R_SCRIPT_LOADED;
		*check_char = isFileChar;
		break;
	default:
		AssertNotReached();
		break;
	}

	return vStringLength(token);
}

static int makeShTag (vString *name, const unsigned char ** cp CTAGS_ATTR_UNUSED,
					  int found_kind, int found_role)
{
	return makeSimpleRefTag (name, found_kind, found_role);
}

static int makeZshAutoloadTag(vString *name, const unsigned char ** cp)
{
	const unsigned char *p = *cp;

	int r = CORK_NIL;
	do
	{
		if (vStringChar(name, 0) != '-' && vStringChar(name, 0) != '+')
		{
			do
			{
				r = makeSimpleRefTag (name, K_SCRIPT, R_ZSH_SCRIPT_AUTOLOADED);

				tagEntryInfo e;
				const char *func = strrchr(vStringValue (name), '/');
				func = func? func + 1: vStringValue (name);
				if (*func != '\0')
				{
					initRefTagEntry (&e, func, K_FUNCTION, R_ZSH_FUNCTION_AUTOLOADED);
					r = makeTagEntry(&e);
				}

				while (isspace (*p))
					p++;

				if (*p == '\0')
					break;

				vStringClear (name);
				while (*p != '\0' && !isspace (*p))
					vStringPut (name, *p++);
			}
			while (1);
			break;
		}

		if (*p == '\0')
			break;

		vStringClear(name);
		while (*p != '\0' && !isspace(*p))
			vStringPut (name, *p++);

		while (isspace (*p))
			++p;
	}
	while (1);

	*cp = p;
	return r;
}

static int makeZshTag (vString *name, const unsigned char ** cp,
					  int found_kind, int found_role)
{
	const unsigned char *p = *cp;

	if (found_kind == K_SCRIPT && found_role == R_ZSH_SCRIPT_AUTOLOADED)
	{
		int r = makeZshAutoloadTag(name, &p);
		*cp = p;
		return r;
	}

	return makeShTag (name, cp, found_kind, found_role);
}

static size_t handleZshKeyword (int keyword,
								vString *token,
								int *kind,
								int *role,
								bool (** check_char)(int))
{
	switch (keyword)
	{
	case KEYWORD_autoload:
		*kind = K_SCRIPT;
		*role = R_ZSH_SCRIPT_AUTOLOADED;
		break;
	default:
		return handleShKeyword (keyword, token, kind, role, check_char);
	}

	return vStringLength(token);
}

typedef bool (* checkCharFunc) (int);
static void findShTagsCommon (size_t (* keyword_handler) (int,
														  vString *,
														  int *,
														  int *,
														  checkCharFunc *check_char),
							  int (* make_tag_handler) (vString *, const unsigned char **, int, int))

{
	vString *name = vStringNew ();
	const unsigned char *line;
	vString *hereDocDelimiter = NULL;
	bool hereDocIndented = false;
	checkCharFunc check_char;

	struct hereDocParsingState hstate;
	hdocStateInit (&hstate);

	while ((line = readLineFromInputFile ()) != NULL)
	{
		const unsigned char* cp = line;
		int found_kind = K_NOTHING;
		int found_role = ROLE_DEFINITION_INDEX;

		if (hereDocDelimiter)
		{
			if (hereDocIndented)
			{
				while (*cp == '\t')
					cp++;
			}
			if ((strncmp ((const char *) cp, vStringValue (hereDocDelimiter), vStringLength (hereDocDelimiter)) == 0)
				&& ((*(cp + vStringLength (hereDocDelimiter)) == '\0')
					|| isspace (*(cp + vStringLength (hereDocDelimiter)) )))
			{
				hdocStateUpdateTag (&hstate, getInputLineNumber ());
				hdocStateMakePromiseMaybe (&hstate);

				if (!vStringIsEmpty(hereDocDelimiter))
					makeSimpleRefTag(hereDocDelimiter, K_HEREDOCLABEL, R_HEREDOC_ENDMARKER);
				vStringDelete (hereDocDelimiter);
				hereDocDelimiter = NULL;
			}
			continue;
		}

		hdocStateClear (&hstate);
		while (*cp != '\0')
		{
			subparser *sub = NULL;
			int sub_n = 0;

			/* jump over whitespace */
			while (isspace ((int)*cp))
				cp++;

			/* jump over strings */
			if (*cp == '"')
				cp = skipDoubleString (cp);
			else if (*cp == '\'')
				cp = skipSingleString (cp);
			/* jump over comments */
			else if (*cp == '#')
				break;
			/* jump over here-documents */
			else if (cp[0] == '<' && cp[1] == '<')
			{
				const unsigned char *start, *end;
				bool trimEscapeSequences = false;
				bool quoted = false;
				cp += 2;
				/* an optional "-" strips leading tabulations from the heredoc lines */
				if (*cp != '-')
					hereDocIndented = false;
				else
				{
					hereDocIndented = true;
					cp++;
				}
				while (isspace (*cp))
					cp++;
				start = end = cp;
				/* the delimiter can be surrounded by quotes */
				if (*cp == '"')
				{
					start++;
					end = cp = skipDoubleString (cp);
					/* we need not to worry about variable substitution, they
					 * don't happen in heredoc delimiter definition */
					trimEscapeSequences = true;
					quoted = true;
				}
				else if (*cp == '\'')
				{
					start++;
					end = cp = skipSingleString (cp);
					quoted = true;
				}
				else
				{
					while (isIdentChar ((int) *cp))
						cp++;
					end = cp;
				}
				if (end > start || quoted)
				{
					/* The input may be broken as a shell script but we need to avoid
					   memory leaking. */
					if (hereDocDelimiter)
						vStringClear(hereDocDelimiter);
					else
						hereDocDelimiter = vStringNew ();
					for (; end > start; start++)
					{
						if (trimEscapeSequences && *start == '\\')
							start++;
						vStringPut (hereDocDelimiter, *start);
					}
					if (vStringLength(hereDocDelimiter) > 0)
						hstate.corkIndex = makeSimpleTag(hereDocDelimiter, K_HEREDOCLABEL);

					hdocStateRecordStatelineMaybe(&hstate);
				}
			}

			check_char = isBashFunctionChar;

			if (cp [0] == '.'
				    && isspace((int) cp [1]))
			{
				found_kind = K_SCRIPT;
				found_role = R_SCRIPT_LOADED;
				++cp;
				check_char = isFileChar;
			}
			else if ((sub = notifyLineToSubparsers (cp, &sub_n)))
			{
				found_kind = K_SUBPARSER;
				cp += sub_n;
			}
			else
			{
				vString *token = vStringNew ();
				const unsigned char *tmp = cp;
				while (*tmp && (tmp == cp
								? (isIdentChar0 (*tmp))
								: (isIdentChar (*tmp))))
				{
					vStringPut (token, *tmp);
					tmp++;
				}

				int keyword = lookupKeyword (vStringValue (token),
											 getInputLanguage ());
				if (keyword != KEYWORD_NONE)
					cp += (* keyword_handler) (keyword, token,
											   &found_kind, &found_role, &check_char);
				vStringDelete (token);
			}

			if (found_kind != K_NOTHING)
				while (isspace ((int) *cp))
					++cp;

			if (found_kind == K_SUBPARSER)
			{
				sub_n = extractNameToSubparser (sub, cp, name);
				if (!vStringIsEmpty (name))
					cp += sub_n;
			}
			else
			{
				// Get the name of the function, alias or file to be read by source
				if (! check_char ((int) *cp))
				{
					found_kind = K_NOTHING;
					found_role = ROLE_DEFINITION_INDEX;

					int d = hdocStateReadDestfileName (&hstate, cp,
													   hereDocDelimiter);
					if (d > 0)
						cp += d;
					else if (*cp != '\0')
						++cp;
					continue;
				}

				while (check_char ((int) *cp))
				{
					vStringPut (name, (int) *cp);
					++cp;
				}
			}

			while (isspace ((int) *cp))
				++cp;

			if ((found_kind != K_SCRIPT)
			    && *cp == '(')
			{
				++cp;
				while (isspace ((int) *cp))
					++cp;
				if (*cp == ')')
				{
					/* A function definiton can look like an array initialization:
					 *
					 *   ... f=()
					 *
					 * We use followng heuristics to distinguish a function definition
					 * from an array initialization:
					 *
					 *   preceding "function" keyword: function f=()
					 *   followed by '{': f=() {
					 *
					 */
					if (! ((vStringLast(name) == '=')
						   /* Have we found "function" yet? */
						   && (found_kind != K_FUNCTION)
						   && (strchr ((char *)(cp + 1), '{') == NULL)))
						found_kind = K_FUNCTION;
					++cp;
				}
			}

			if (found_kind != K_NOTHING)
			{
				if (found_kind == K_SUBPARSER)
				{
					if (!vStringIsEmpty (name))
						makeTagInSubparser (sub, name);
				}
				else
					make_tag_handler (name, &cp, found_kind, found_role);
				found_kind = K_NOTHING;
				found_role = ROLE_DEFINITION_INDEX;
			}
			else if (!hereDocDelimiter)
				hdocStateUpdateArgs (&hstate, name);
			vStringClear (name);
		}
	}
	hdocStateFini (&hstate);
	vStringDelete (name);
	if (hereDocDelimiter)
		vStringDelete (hereDocDelimiter);
}

static void findShTags (void)
{
	findShTagsCommon (handleShKeyword, makeShTag);
}

static void findZshTags (void)
{
	findShTagsCommon (handleZshKeyword, makeZshTag);
}

static subparser *notifyLineToSubparsers (const unsigned char *cp,
										  int *n)
{
	int r = 0;
	subparser *sub;

	foreachSubparser (sub, false)
	{
		shSubparser *shsub = (shSubparser *)sub;

		if(shsub->lineNotify)
		{
			enterSubparser(sub);
			r = shsub->lineNotify (shsub, cp);
			leaveSubparser();
			if (r > 0)
				break;
		}
	}

	if (r > 0)
	{
		*n = r;
		return sub;
	}
	return NULL;
}

static int extractNameToSubparser(subparser *sub, const unsigned char *cp,
								  vString *name)
{
	int n;
	shSubparser *shsub = (shSubparser *)sub;

	enterSubparser(sub);
	n = shsub->extractName(shsub, cp, name);
	leaveSubparser();

	return n;
}

static int makeTagInSubparser (subparser *sub, vString *name)
{
	int r;
	shSubparser *shsub = (shSubparser *)sub;

	enterSubparser(sub);
	r = shsub->makeTag(shsub, name);
	leaveSubparser();

	return r;
}

static void initializeSh (const langType language)
{
	addDialectalKeywords (KeywordTable, ARRAY_SIZE (KeywordTable),
						  language,
						  dialectMap, ARRAY_SIZE (dialectMap));
}

extern parserDefinition* ShParser (void)
{
	static const char *const extensions [] = {
		"sh", "SH", "bsh", "bash", "ksh", "ash", NULL
	};
	static const char *const aliases [] = {
		"sh", "bash", "ksh", "ash", "dash",
		/* major mode name in emacs */
		"shell-script",
		NULL
	};
	parserDefinition* def = parserNew ("Sh");
	def->kindTable      = ShKinds;
	def->kindCount  = ARRAY_SIZE (ShKinds);
	def->extensions = extensions;
	def->aliases = aliases;
	def->parser     = findShTags;
	def->initialize = initializeSh;
	def->useCork    = CORK_QUEUE;
	return def;
}

extern parserDefinition* ZshParser (void)
{
	static const char *const extensions [] = {
		"zsh", NULL
	};
	static const char *const aliases [] = {
		"zsh", NULL,
	};
	parserDefinition* def = parserNew ("Zsh");
	def->kindTable      = ZshKinds;
	def->kindCount  = ARRAY_SIZE (ZshKinds);
	def->extensions = extensions;
	def->aliases = aliases;
	def->parser     = findZshTags;
	def->initialize = initializeSh;
	def->useCork    = CORK_QUEUE;
	return def;
}