Make parser includes closer to uctags and sync parser license header
[geany-mirror.git] / ctags / parsers / sh.c
blob37261ae40311265f5b82e42f9f03b6e135013476
1 /*
2 * Copyright (c) 2000-2002, Darren Hiebert
4 * This source code is released for free distribution under the terms of the
5 * GNU General Public License version 2 or (at your option) any later version.
7 * This module contains functions for generating tags for scripts for the
8 * Bourne shell (and its derivatives, the Korn and Z shells).
9 */
12 * INCLUDE FILES
14 #include "general.h" /* must always come first */
16 #include <string.h>
18 #include "parse.h"
19 #include "read.h"
20 #include "main.h"
21 #include "routines.h"
22 #include "vstring.h"
23 #include "xtag.h"
26 * DATA DEFINITIONS
28 typedef enum {
29 K_FUNCTION
30 } shKind;
32 static kindOption ShKinds [] = {
33 { TRUE, 'f', "function", "functions"}
37 * FUNCTION DEFINITIONS
40 /* Reject any tag "main" from a file named "configure". These appear in
41 * here-documents in GNU autoconf scripts and will add a haystack to the
42 * needle.
44 static boolean hackReject (const vString* const tagName)
46 const char *const scriptName = baseFilename (vStringValue (File.name));
47 boolean result = (boolean) (strcmp (scriptName, "configure") == 0 &&
48 strcmp (vStringValue (tagName), "main") == 0);
49 return result;
52 static void findShTags (void)
54 vString *name = vStringNew ();
55 const unsigned char *line;
57 while ((line = fileReadLine ()) != NULL)
59 const unsigned char* cp = line;
60 boolean functionFound = FALSE;
62 if (line [0] == '#')
63 continue;
65 while (isspace (*cp))
66 cp++;
67 if (strncmp ((const char*) cp, "function", (size_t) 8) == 0 &&
68 isspace ((int) cp [8]))
70 functionFound = TRUE;
71 cp += 8;
72 if (! isspace ((int) *cp))
73 continue;
74 while (isspace ((int) *cp))
75 ++cp;
77 if (! (isalnum ((int) *cp) || *cp == '_'))
78 continue;
79 while (isalnum ((int) *cp) || *cp == '_')
81 vStringPut (name, (int) *cp);
82 ++cp;
84 vStringTerminate (name);
85 while (isspace ((int) *cp))
86 ++cp;
87 if (*cp++ == '(')
89 while (isspace ((int) *cp))
90 ++cp;
91 if (*cp == ')' && ! hackReject (name))
92 functionFound = TRUE;
94 if (functionFound)
95 makeSimpleTag (name, ShKinds, K_FUNCTION);
96 vStringClear (name);
98 vStringDelete (name);
101 extern parserDefinition* ShParser (void)
103 static const char *const extensions [] = {
104 "sh", "SH", "bsh", "bash", "ksh", "zsh", "ash", NULL
106 parserDefinition* def = parserNew ("Sh");
107 def->kinds = ShKinds;
108 def->kindCount = KIND_COUNT (ShKinds);
109 def->extensions = extensions;
110 def->parser = findShTags;
111 return def;
114 /* vi:set tabstop=8 shiftwidth=4: */