0N/A/*
0N/A * CDDL HEADER START
0N/A *
0N/A * The contents of this file are subject to the terms of the
0N/A * Common Development and Distribution License (the "License").
0N/A * You may not use this file except in compliance with the License.
0N/A *
0N/A * See LICENSE.txt included in this distribution for the specific
0N/A * language governing permissions and limitations under the License.
0N/A *
0N/A * When distributing Covered Code, include this CDDL HEADER in each
0N/A * file and include the License file at LICENSE.txt.
0N/A * If applicable, add the following below this CDDL HEADER, with the
0N/A * fields enclosed by brackets "[]" replaced with your own identifying
0N/A * information: Portions Copyright [yyyy] [name of copyright owner]
0N/A *
0N/A * CDDL HEADER END
0N/A */
0N/A
0N/A/*
1166N/A * Copyright (c) 2005, 2011, Oracle and/or its affiliates. All rights reserved.
0N/A */
0N/A
0N/Apackage org.opensolaris.opengrok.analysis.sh;
850N/Aimport org.opensolaris.opengrok.analysis.JFlexXref;
850N/Aimport java.io.IOException;
850N/Aimport java.io.Writer;
850N/Aimport java.io.Reader;
0N/Aimport org.opensolaris.opengrok.web.Util;
850N/Aimport java.util.Stack;
0N/A
0N/A%%
0N/A%public
0N/A%class ShXref
850N/A%extends JFlexXref
0N/A%unicode
0N/A%ignorecase
0N/A%int
0N/A%{
167N/A private final Stack<Integer> stateStack = new Stack<Integer>();
167N/A private final Stack<String> styleStack = new Stack<String>();
167N/A
1166N/A // State variables for the HEREDOC state. They tell what the stop word is,
1166N/A // and whether leading tabs should be removed from the input lines before
1166N/A // comparing with the stop word.
1166N/A private String heredocStopWord;
1166N/A private boolean heredocStripLeadingTabs;
1166N/A
1083N/A @Override
1083N/A public void reInit(char[] contents, int length) {
1083N/A super.reInit(contents, length);
1083N/A stateStack.clear();
1083N/A styleStack.clear();
1083N/A }
1083N/A
1020N/A // TODO move this into an include file when bug #16053 is fixed
1020N/A @Override
1020N/A protected int getLineNumber() { return yyline; }
1020N/A @Override
1020N/A protected void setLineNumber(int x) { yyline = x; }
0N/A
167N/A private void pushstate(int state, String style) throws IOException {
167N/A if (!styleStack.empty()) {
167N/A out.write("</span>");
167N/A }
167N/A if (style == null) {
167N/A out.write("<span>");
167N/A } else {
167N/A out.write("<span class=\"" + style + "\">");
167N/A }
167N/A stateStack.push(yystate());
167N/A styleStack.push(style);
167N/A yybegin(state);
167N/A }
167N/A
167N/A private void popstate() throws IOException {
167N/A out.write("</span>");
167N/A yybegin(stateStack.pop());
167N/A styleStack.pop();
167N/A if (!styleStack.empty()) {
167N/A String style = styleStack.peek();
167N/A if (style == null) {
167N/A out.write("<span>");
167N/A } else {
167N/A out.write("<span class=\"" + style + "\">");
167N/A }
167N/A }
167N/A }
167N/A
1166N/A /**
1166N/A * Check the contents of a line to see if it matches the stop word for a
1166N/A * here-document.
1166N/A *
1166N/A * @param line a line in the input file
1166N/A * @return true if the line terminates a here-document, false otherwise
1166N/A */
1166N/A private boolean isHeredocStopWord(String line) {
1166N/A // Skip leading tabs if heredocStripLeadingTabs is true.
1166N/A int i = 0;
1166N/A while (heredocStripLeadingTabs &&
1166N/A i < line.length() && line.charAt(i) == '\t') {
1166N/A i++;
1166N/A }
1166N/A
1166N/A // Compare remaining characters on the line with the stop word.
1166N/A return line.substring(i).equals(heredocStopWord);
1166N/A }
1166N/A
0N/A%}
0N/A
936N/AWhiteSpace = [ \t\f]
1020N/AEOL = \r|\n|\r\n
0N/AIdentifier = [a-zA-Z_] [a-zA-Z0-9_]+
1074N/ANumber = \$? [0-9]+\.[0-9]+|[0-9][0-9]*|"0x" [0-9a-fA-F]+
0N/A
0N/AURIChar = [\?\+\%\&\:\/\.\@\_\;\=\$\,\-\!\~\*\\]
0N/AFNameChar = [a-zA-Z0-9_\-\.]
0N/AFile = {FNameChar}+ "." ([a-zA-Z]+)
0N/APath = "/"? [a-zA-Z]{FNameChar}* ("/" [a-zA-Z]{FNameChar}*)+[a-zA-Z0-9]
0N/A
1060N/A/*
1060N/A * States:
1060N/A * STRING - double-quoted string, ex: "hello, world!"
1060N/A * SCOMMENT - single-line comment, ex: # this is a comment
1060N/A * QSTRING - single-quoted string, ex: 'hello, world!'
1060N/A * SUBSHELL - commands executed in a sub-shell,
1060N/A * example 1: (echo $header; cat file.txt)
1060N/A * example 2 (command substitution): $(cat file.txt)
1060N/A * BACKQUOTE - command substitution using back-quotes, ex: `cat file.txt`
1060N/A * BRACEGROUP - group of commands in braces, possibly ksh command substitution
1060N/A * extension, ex: ${ cat file.txt; }
1166N/A * HEREDOC - here-document, example: cat<<EOF ... EOF
1060N/A */
1166N/A%state STRING SCOMMENT QSTRING SUBSHELL BACKQUOTE BRACEGROUP HEREDOC
0N/A
0N/A%%
0N/A<STRING>{
1013N/A "$" {Identifier} {
1013N/A String id = yytext();
1013N/A out.write("<a href=\"");
1013N/A out.write(urlPrefix);
1013N/A out.write("refs=");
1013N/A out.write(id);
1013N/A appendProject();
1013N/A out.write("\">");
1013N/A out.write(id);
1013N/A out.write("</a>");
1013N/A }
167N/A
167N/A /* This rule matches associative arrays inside strings,
167N/A for instance "${array["string"]}". Push a new STRING
167N/A state on the stack to prevent premature exit from the
167N/A STRING state. */
167N/A \$\{ {Identifier} \[\" {
167N/A out.write(yytext()); pushstate(STRING, "s");
167N/A }
0N/A}
0N/A
1060N/A<YYINITIAL, SUBSHELL, BACKQUOTE, BRACEGROUP> {
943N/A\$ ? {Identifier} {
943N/A String id = yytext();
1020N/A writeSymbol(id, Consts.shkwd, yyline);
943N/A}
0N/A
974N/A{Number} { out.write("<span class=\"n\">"); out.write(yytext()); out.write("</span>"); }
0N/A
167N/A \$ ? \" { pushstate(STRING, "s"); out.write(yytext()); }
167N/A \$ ? \' { pushstate(QSTRING, "s"); out.write(yytext()); }
167N/A "#" { pushstate(SCOMMENT, "c"); out.write(yytext()); }
1166N/A
1166N/A // Recognize here-documents. At least a subset of them.
1166N/A "<<" "-"? {WhiteSpace}* {Identifier} {WhiteSpace}* {
1166N/A String text = yytext();
1185N/A out.write(Util.htmlize(text));
1166N/A
1166N/A heredocStripLeadingTabs = (text.charAt(2) == '-');
1166N/A heredocStopWord = text.substring(heredocStripLeadingTabs ? 3 : 2).trim();
1166N/A pushstate(HEREDOC, "s");
1166N/A }
1166N/A
1166N/A // Any sequence of more than two < characters should not start HEREDOC. Use
1166N/A // this rule to catch them before the HEREDOC rule.
1166N/A "<<" "<" + {
1185N/A out.write(Util.htmlize(yytext()));
1166N/A }
1166N/A
0N/A}
0N/A
0N/A<STRING> {
1013N/A \" {WhiteSpace}* \" { out.write(yytext()); }
974N/A \" { out.write(yytext()); popstate(); }
1049N/A \\\\ | \\\" | \\\$ | \\` { out.write(yytext()); }
167N/A \$\( { pushstate(SUBSHELL, null); out.write(yytext()); }
167N/A ` { pushstate(BACKQUOTE, null); out.write(yytext()); }
1060N/A
1060N/A /* Bug #15661: Recognize ksh command substitution within strings. According
1060N/A * to ksh man page http://www2.research.att.com/~gsf/man/man1/ksh-man.html#Command%20Substitution
1060N/A * the opening brace must be followed by a blank.
1060N/A */
1060N/A "${" / {WhiteSpace} | {EOL} {
1060N/A pushstate(BRACEGROUP, null); out.write(yytext());
1060N/A }
0N/A}
0N/A
0N/A<QSTRING> {
1013N/A \' {WhiteSpace}* \' { out.write(yytext()); }
0N/A \\' { out.write("\\'"); }
167N/A \' { out.write(yytext()); popstate(); }
0N/A}
0N/A
0N/A<SCOMMENT> {
936N/A{EOL} { popstate();
1020N/A startNewLine();}
0N/A}
0N/A
167N/A<SUBSHELL> {
167N/A \) { out.write(yytext()); popstate(); }
167N/A}
0N/A
167N/A<BACKQUOTE> {
167N/A ` { out.write(yytext()); popstate(); }
167N/A}
167N/A
1060N/A<BRACEGROUP> {
1060N/A /* Bug #15661: Terminate a ksh brace group. According to ksh man page
1060N/A * http://www2.research.att.com/~gsf/man/man1/ksh-man.html#Command%20Substitution
1060N/A * the closing brace must be on beginning of line, or it must be preceeded by
1060N/A * a semi-colon and (optionally) whitespace.
1060N/A */
1060N/A ^ {WhiteSpace}* \} { out.write(yytext()); popstate(); }
1060N/A ; {WhiteSpace}* \} { out.write(yytext()); popstate(); }
1060N/A}
1060N/A
1166N/A<HEREDOC> {
1166N/A .* {
1166N/A String line = yytext();
1166N/A if (isHeredocStopWord(line)) {
1166N/A popstate();
1166N/A }
1185N/A out.write(Util.htmlize(line));
1166N/A }
1166N/A
1166N/A {EOL} { startNewLine(); }
1166N/A}
1166N/A
1060N/A<YYINITIAL, SUBSHELL, BACKQUOTE, BRACEGROUP> {
1049N/A /* Don't enter new state if special character is escaped. */
1060N/A \\` | \\\( | \\\) | \\\\ | \\\{ { out.write(yytext()); }
1049N/A \\\" | \\' | \\\$ | \\\# { out.write(yytext()); }
1049N/A
1049N/A /* $# should not start a comment. */
1049N/A "$#" { out.write(yytext()); }
1049N/A
167N/A \$ ? \( { pushstate(SUBSHELL, null); out.write(yytext()); }
167N/A ` { pushstate(BACKQUOTE, null); out.write(yytext()); }
1060N/A
1060N/A /* Bug #15661: Recognize ksh command substitution within strings. According
1060N/A * to ksh man page http://www2.research.att.com/~gsf/man/man1/ksh-man.html#Command%20Substitution
1060N/A * the opening brace must be followed by a blank. Make the initial dollar sign
1060N/A * optional so that we get the nesting right and don't terminate the brace
1060N/A * group too early if the ${ cmd; } expression contains nested { cmd; } groups.
1060N/A */
1060N/A \$ ? \{ / {WhiteSpace} | {EOL} {
1060N/A pushstate(BRACEGROUP, null); out.write(yytext());
1060N/A }
167N/A}
167N/A
1060N/A<YYINITIAL, SUBSHELL, BACKQUOTE, BRACEGROUP, STRING, SCOMMENT, QSTRING> {
1013N/A{File} {
1013N/A String path = yytext();
1013N/A out.write("<a href=\""+urlPrefix+"path=");
1013N/A out.write(path);
1013N/A appendProject();
1013N/A out.write("\">");
1013N/A out.write(path);
1013N/A out.write("</a>");
1013N/A}
0N/A
0N/A{Path}
974N/A { out.write(Util.breadcrumbPath(urlPrefix+"path=",yytext(),'/'));}
974N/A"&" {out.write( "&amp;");}
974N/A"<" {out.write( "&lt;");}
974N/A">" {out.write( "&gt;");}
1020N/A {EOL} { startNewLine(); }
1013N/A{WhiteSpace}+ { out.write(yytext()); }
974N/A[!-~] { out.write(yycharat(0)); }
974N/A . { writeUnicodeChar(yycharat(0)); }
0N/A}
0N/A
0N/A<STRING, SCOMMENT, QSTRING> {
0N/A
0N/A("http" | "https" | "ftp" ) "://" ({FNameChar}|{URIChar})+[a-zA-Z0-9/]
1013N/A{
1013N/A String url = yytext();
1013N/A out.write("<a href=\"");
1013N/A out.write(url);out.write("\">");
1013N/A out.write(url);out.write("</a>");
1013N/A}
0N/A
0N/A{FNameChar}+ "@" {FNameChar}+ "." {FNameChar}+
974N/A {
1122N/A writeEMailAddress(yytext());
974N/A }
0N/A}
1083N/A
1083N/A<<EOF>> {
1083N/A // If we reach EOF while being in a nested state, pop all the way up
1083N/A // the initial state so that we close open HTML tags.
1083N/A while (!stateStack.isEmpty()) {
1083N/A popstate();
1083N/A }
1083N/A return YYEOF;
1083N/A}