| 1 |
/* |
| 2 |
* $Id: sh.c 443 2006-05-30 04:37:13Z darren $ |
| 3 |
* |
| 4 |
* Copyright (c) 2000-2002, Darren Hiebert |
| 5 |
* |
| 6 |
* This source code is released for free distribution under the terms of the |
| 7 |
* GNU General Public License. |
| 8 |
* |
| 9 |
* This module contains functions for generating tags for scripts for the |
| 10 |
* Bourne shell (and its derivatives, the Korn and Z shells). |
| 11 |
*/ |
| 12 |
|
| 13 |
/* |
| 14 |
* INCLUDE FILES |
| 15 |
*/ |
| 16 |
#include "general.h" /* must always come first */ |
| 17 |
|
| 18 |
#include <string.h> |
| 19 |
|
| 20 |
#include "parse.h" |
| 21 |
#include "read.h" |
| 22 |
#include "routines.h" |
| 23 |
#include "vstring.h" |
| 24 |
|
| 25 |
/* |
| 26 |
* DATA DEFINITIONS |
| 27 |
*/ |
| 28 |
typedef enum { |
| 29 |
K_FUNCTION |
| 30 |
} shKind; |
| 31 |
|
| 32 |
static kindOption ShKinds [] = { |
| 33 |
{ TRUE, 'f', "function", "functions"} |
| 34 |
}; |
| 35 |
|
| 36 |
/* |
| 37 |
* FUNCTION DEFINITIONS |
| 38 |
*/ |
| 39 |
|
| 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. |
| 43 |
*/ |
| 44 |
static boolean hackReject (const vString* const tagName) |
| 45 |
{ |
| 46 |
const char *const scriptName = baseFilename (vStringValue (File.name)); |
| 47 |
boolean result = (boolean) ( |
| 48 |
strcmp (scriptName, "configure") == 0 && |
| 49 |
strcmp (vStringValue (tagName), "main") == 0); |
| 50 |
return result; |
| 51 |
} |
| 52 |
|
| 53 |
static void findShTags (void) |
| 54 |
{ |
| 55 |
vString *name = vStringNew (); |
| 56 |
const unsigned char *line; |
| 57 |
|
| 58 |
while ((line = fileReadLine ()) != NULL) |
| 59 |
{ |
| 60 |
const unsigned char* cp = line; |
| 61 |
boolean functionFound = FALSE; |
| 62 |
|
| 63 |
if (line [0] == '#') |
| 64 |
continue; |
| 65 |
|
| 66 |
while (isspace (*cp)) |
| 67 |
cp++; |
| 68 |
if (strncmp ((const char*) cp, "function", (size_t) 8) == 0 && |
| 69 |
isspace ((int) cp [8])) |
| 70 |
{ |
| 71 |
functionFound = TRUE; |
| 72 |
cp += 8; |
| 73 |
if (! isspace ((int) *cp)) |
| 74 |
continue; |
| 75 |
while (isspace ((int) *cp)) |
| 76 |
++cp; |
| 77 |
} |
| 78 |
if (! (isalnum ((int) *cp) || *cp == '_')) |
| 79 |
continue; |
| 80 |
while (isalnum ((int) *cp) || *cp == '_') |
| 81 |
{ |
| 82 |
vStringPut (name, (int) *cp); |
| 83 |
++cp; |
| 84 |
} |
| 85 |
vStringTerminate (name); |
| 86 |
while (isspace ((int) *cp)) |
| 87 |
++cp; |
| 88 |
if (*cp++ == '(') |
| 89 |
{ |
| 90 |
while (isspace ((int) *cp)) |
| 91 |
++cp; |
| 92 |
if (*cp == ')' && ! hackReject (name)) |
| 93 |
functionFound = TRUE; |
| 94 |
} |
| 95 |
if (functionFound) |
| 96 |
makeSimpleTag (name, ShKinds, K_FUNCTION); |
| 97 |
vStringClear (name); |
| 98 |
} |
| 99 |
vStringDelete (name); |
| 100 |
} |
| 101 |
|
| 102 |
extern parserDefinition* ShParser (void) |
| 103 |
{ |
| 104 |
static const char *const extensions [] = { |
| 105 |
"sh", "SH", "bsh", "bash", "ksh", "zsh", NULL |
| 106 |
}; |
| 107 |
parserDefinition* def = parserNew ("Sh"); |
| 108 |
def->kinds = ShKinds; |
| 109 |
def->kindCount = KIND_COUNT (ShKinds); |
| 110 |
def->extensions = extensions; |
| 111 |
def->parser = findShTags; |
| 112 |
return def; |
| 113 |
} |
| 114 |
|
| 115 |
/* vi:set tabstop=4 shiftwidth=4: */ |