[d24f17c] | 1 | 'use strict'
|
---|
| 2 |
|
---|
| 3 | module.exports = bash
|
---|
| 4 | bash.displayName = 'bash'
|
---|
| 5 | bash.aliases = ['shell']
|
---|
| 6 | function bash(Prism) {
|
---|
| 7 | ;(function (Prism) {
|
---|
| 8 | // $ set | grep '^[A-Z][^[:space:]]*=' | cut -d= -f1 | tr '\n' '|'
|
---|
| 9 | // + LC_ALL, RANDOM, REPLY, SECONDS.
|
---|
| 10 | // + make sure PS1..4 are here as they are not always set,
|
---|
| 11 | // - some useless things.
|
---|
| 12 | var envVars =
|
---|
| 13 | '\\b(?:BASH|BASHOPTS|BASH_ALIASES|BASH_ARGC|BASH_ARGV|BASH_CMDS|BASH_COMPLETION_COMPAT_DIR|BASH_LINENO|BASH_REMATCH|BASH_SOURCE|BASH_VERSINFO|BASH_VERSION|COLORTERM|COLUMNS|COMP_WORDBREAKS|DBUS_SESSION_BUS_ADDRESS|DEFAULTS_PATH|DESKTOP_SESSION|DIRSTACK|DISPLAY|EUID|GDMSESSION|GDM_LANG|GNOME_KEYRING_CONTROL|GNOME_KEYRING_PID|GPG_AGENT_INFO|GROUPS|HISTCONTROL|HISTFILE|HISTFILESIZE|HISTSIZE|HOME|HOSTNAME|HOSTTYPE|IFS|INSTANCE|JOB|LANG|LANGUAGE|LC_ADDRESS|LC_ALL|LC_IDENTIFICATION|LC_MEASUREMENT|LC_MONETARY|LC_NAME|LC_NUMERIC|LC_PAPER|LC_TELEPHONE|LC_TIME|LESSCLOSE|LESSOPEN|LINES|LOGNAME|LS_COLORS|MACHTYPE|MAILCHECK|MANDATORY_PATH|NO_AT_BRIDGE|OLDPWD|OPTERR|OPTIND|ORBIT_SOCKETDIR|OSTYPE|PAPERSIZE|PATH|PIPESTATUS|PPID|PS1|PS2|PS3|PS4|PWD|RANDOM|REPLY|SECONDS|SELINUX_INIT|SESSION|SESSIONTYPE|SESSION_MANAGER|SHELL|SHELLOPTS|SHLVL|SSH_AUTH_SOCK|TERM|UID|UPSTART_EVENTS|UPSTART_INSTANCE|UPSTART_JOB|UPSTART_SESSION|USER|WINDOWID|XAUTHORITY|XDG_CONFIG_DIRS|XDG_CURRENT_DESKTOP|XDG_DATA_DIRS|XDG_GREETER_DATA_DIR|XDG_MENU_PREFIX|XDG_RUNTIME_DIR|XDG_SEAT|XDG_SEAT_PATH|XDG_SESSION_DESKTOP|XDG_SESSION_ID|XDG_SESSION_PATH|XDG_SESSION_TYPE|XDG_VTNR|XMODIFIERS)\\b'
|
---|
| 14 | var commandAfterHeredoc = {
|
---|
| 15 | pattern: /(^(["']?)\w+\2)[ \t]+\S.*/,
|
---|
| 16 | lookbehind: true,
|
---|
| 17 | alias: 'punctuation',
|
---|
| 18 | // this looks reasonably well in all themes
|
---|
| 19 | inside: null // see below
|
---|
| 20 | }
|
---|
| 21 | var insideString = {
|
---|
| 22 | bash: commandAfterHeredoc,
|
---|
| 23 | environment: {
|
---|
| 24 | pattern: RegExp('\\$' + envVars),
|
---|
| 25 | alias: 'constant'
|
---|
| 26 | },
|
---|
| 27 | variable: [
|
---|
| 28 | // [0]: Arithmetic Environment
|
---|
| 29 | {
|
---|
| 30 | pattern: /\$?\(\([\s\S]+?\)\)/,
|
---|
| 31 | greedy: true,
|
---|
| 32 | inside: {
|
---|
| 33 | // If there is a $ sign at the beginning highlight $(( and )) as variable
|
---|
| 34 | variable: [
|
---|
| 35 | {
|
---|
| 36 | pattern: /(^\$\(\([\s\S]+)\)\)/,
|
---|
| 37 | lookbehind: true
|
---|
| 38 | },
|
---|
| 39 | /^\$\(\(/
|
---|
| 40 | ],
|
---|
| 41 | number:
|
---|
| 42 | /\b0x[\dA-Fa-f]+\b|(?:\b\d+(?:\.\d*)?|\B\.\d+)(?:[Ee]-?\d+)?/,
|
---|
| 43 | // Operators according to https://www.gnu.org/software/bash/manual/bashref.html#Shell-Arithmetic
|
---|
| 44 | operator:
|
---|
| 45 | /--|\+\+|\*\*=?|<<=?|>>=?|&&|\|\||[=!+\-*/%<>^&|]=?|[?~:]/,
|
---|
| 46 | // If there is no $ sign at the beginning highlight (( and )) as punctuation
|
---|
| 47 | punctuation: /\(\(?|\)\)?|,|;/
|
---|
| 48 | }
|
---|
| 49 | }, // [1]: Command Substitution
|
---|
| 50 | {
|
---|
| 51 | pattern: /\$\((?:\([^)]+\)|[^()])+\)|`[^`]+`/,
|
---|
| 52 | greedy: true,
|
---|
| 53 | inside: {
|
---|
| 54 | variable: /^\$\(|^`|\)$|`$/
|
---|
| 55 | }
|
---|
| 56 | }, // [2]: Brace expansion
|
---|
| 57 | {
|
---|
| 58 | pattern: /\$\{[^}]+\}/,
|
---|
| 59 | greedy: true,
|
---|
| 60 | inside: {
|
---|
| 61 | operator: /:[-=?+]?|[!\/]|##?|%%?|\^\^?|,,?/,
|
---|
| 62 | punctuation: /[\[\]]/,
|
---|
| 63 | environment: {
|
---|
| 64 | pattern: RegExp('(\\{)' + envVars),
|
---|
| 65 | lookbehind: true,
|
---|
| 66 | alias: 'constant'
|
---|
| 67 | }
|
---|
| 68 | }
|
---|
| 69 | },
|
---|
| 70 | /\$(?:\w+|[#?*!@$])/
|
---|
| 71 | ],
|
---|
| 72 | // Escape sequences from echo and printf's manuals, and escaped quotes.
|
---|
| 73 | entity:
|
---|
| 74 | /\\(?:[abceEfnrtv\\"]|O?[0-7]{1,3}|U[0-9a-fA-F]{8}|u[0-9a-fA-F]{4}|x[0-9a-fA-F]{1,2})/
|
---|
| 75 | }
|
---|
| 76 | Prism.languages.bash = {
|
---|
| 77 | shebang: {
|
---|
| 78 | pattern: /^#!\s*\/.*/,
|
---|
| 79 | alias: 'important'
|
---|
| 80 | },
|
---|
| 81 | comment: {
|
---|
| 82 | pattern: /(^|[^"{\\$])#.*/,
|
---|
| 83 | lookbehind: true
|
---|
| 84 | },
|
---|
| 85 | 'function-name': [
|
---|
| 86 | // a) function foo {
|
---|
| 87 | // b) foo() {
|
---|
| 88 | // c) function foo() {
|
---|
| 89 | // but not “foo {”
|
---|
| 90 | {
|
---|
| 91 | // a) and c)
|
---|
| 92 | pattern: /(\bfunction\s+)[\w-]+(?=(?:\s*\(?:\s*\))?\s*\{)/,
|
---|
| 93 | lookbehind: true,
|
---|
| 94 | alias: 'function'
|
---|
| 95 | },
|
---|
| 96 | {
|
---|
| 97 | // b)
|
---|
| 98 | pattern: /\b[\w-]+(?=\s*\(\s*\)\s*\{)/,
|
---|
| 99 | alias: 'function'
|
---|
| 100 | }
|
---|
| 101 | ],
|
---|
| 102 | // Highlight variable names as variables in for and select beginnings.
|
---|
| 103 | 'for-or-select': {
|
---|
| 104 | pattern: /(\b(?:for|select)\s+)\w+(?=\s+in\s)/,
|
---|
| 105 | alias: 'variable',
|
---|
| 106 | lookbehind: true
|
---|
| 107 | },
|
---|
| 108 | // Highlight variable names as variables in the left-hand part
|
---|
| 109 | // of assignments (“=” and “+=”).
|
---|
| 110 | 'assign-left': {
|
---|
| 111 | pattern: /(^|[\s;|&]|[<>]\()\w+(?=\+?=)/,
|
---|
| 112 | inside: {
|
---|
| 113 | environment: {
|
---|
| 114 | pattern: RegExp('(^|[\\s;|&]|[<>]\\()' + envVars),
|
---|
| 115 | lookbehind: true,
|
---|
| 116 | alias: 'constant'
|
---|
| 117 | }
|
---|
| 118 | },
|
---|
| 119 | alias: 'variable',
|
---|
| 120 | lookbehind: true
|
---|
| 121 | },
|
---|
| 122 | string: [
|
---|
| 123 | // Support for Here-documents https://en.wikipedia.org/wiki/Here_document
|
---|
| 124 | {
|
---|
| 125 | pattern: /((?:^|[^<])<<-?\s*)(\w+)\s[\s\S]*?(?:\r?\n|\r)\2/,
|
---|
| 126 | lookbehind: true,
|
---|
| 127 | greedy: true,
|
---|
| 128 | inside: insideString
|
---|
| 129 | }, // Here-document with quotes around the tag
|
---|
| 130 | // → No expansion (so no “inside”).
|
---|
| 131 | {
|
---|
| 132 | pattern: /((?:^|[^<])<<-?\s*)(["'])(\w+)\2\s[\s\S]*?(?:\r?\n|\r)\3/,
|
---|
| 133 | lookbehind: true,
|
---|
| 134 | greedy: true,
|
---|
| 135 | inside: {
|
---|
| 136 | bash: commandAfterHeredoc
|
---|
| 137 | }
|
---|
| 138 | }, // “Normal” string
|
---|
| 139 | {
|
---|
| 140 | // https://www.gnu.org/software/bash/manual/html_node/Double-Quotes.html
|
---|
| 141 | pattern:
|
---|
| 142 | /(^|[^\\](?:\\\\)*)"(?:\\[\s\S]|\$\([^)]+\)|\$(?!\()|`[^`]+`|[^"\\`$])*"/,
|
---|
| 143 | lookbehind: true,
|
---|
| 144 | greedy: true,
|
---|
| 145 | inside: insideString
|
---|
| 146 | },
|
---|
| 147 | {
|
---|
| 148 | // https://www.gnu.org/software/bash/manual/html_node/Single-Quotes.html
|
---|
| 149 | pattern: /(^|[^$\\])'[^']*'/,
|
---|
| 150 | lookbehind: true,
|
---|
| 151 | greedy: true
|
---|
| 152 | },
|
---|
| 153 | {
|
---|
| 154 | // https://www.gnu.org/software/bash/manual/html_node/ANSI_002dC-Quoting.html
|
---|
| 155 | pattern: /\$'(?:[^'\\]|\\[\s\S])*'/,
|
---|
| 156 | greedy: true,
|
---|
| 157 | inside: {
|
---|
| 158 | entity: insideString.entity
|
---|
| 159 | }
|
---|
| 160 | }
|
---|
| 161 | ],
|
---|
| 162 | environment: {
|
---|
| 163 | pattern: RegExp('\\$?' + envVars),
|
---|
| 164 | alias: 'constant'
|
---|
| 165 | },
|
---|
| 166 | variable: insideString.variable,
|
---|
| 167 | function: {
|
---|
| 168 | pattern:
|
---|
| 169 | /(^|[\s;|&]|[<>]\()(?:add|apropos|apt|apt-cache|apt-get|aptitude|aspell|automysqlbackup|awk|basename|bash|bc|bconsole|bg|bzip2|cal|cat|cfdisk|chgrp|chkconfig|chmod|chown|chroot|cksum|clear|cmp|column|comm|composer|cp|cron|crontab|csplit|curl|cut|date|dc|dd|ddrescue|debootstrap|df|diff|diff3|dig|dir|dircolors|dirname|dirs|dmesg|docker|docker-compose|du|egrep|eject|env|ethtool|expand|expect|expr|fdformat|fdisk|fg|fgrep|file|find|fmt|fold|format|free|fsck|ftp|fuser|gawk|git|gparted|grep|groupadd|groupdel|groupmod|groups|grub-mkconfig|gzip|halt|head|hg|history|host|hostname|htop|iconv|id|ifconfig|ifdown|ifup|import|install|ip|jobs|join|kill|killall|less|link|ln|locate|logname|logrotate|look|lpc|lpr|lprint|lprintd|lprintq|lprm|ls|lsof|lynx|make|man|mc|mdadm|mkconfig|mkdir|mke2fs|mkfifo|mkfs|mkisofs|mknod|mkswap|mmv|more|most|mount|mtools|mtr|mutt|mv|nano|nc|netstat|nice|nl|node|nohup|notify-send|npm|nslookup|op|open|parted|passwd|paste|pathchk|ping|pkill|pnpm|podman|podman-compose|popd|pr|printcap|printenv|ps|pushd|pv|quota|quotacheck|quotactl|ram|rar|rcp|reboot|remsync|rename|renice|rev|rm|rmdir|rpm|rsync|scp|screen|sdiff|sed|sendmail|seq|service|sftp|sh|shellcheck|shuf|shutdown|sleep|slocate|sort|split|ssh|stat|strace|su|sudo|sum|suspend|swapon|sync|tac|tail|tar|tee|time|timeout|top|touch|tr|traceroute|tsort|tty|umount|uname|unexpand|uniq|units|unrar|unshar|unzip|update-grub|uptime|useradd|userdel|usermod|users|uudecode|uuencode|v|vcpkg|vdir|vi|vim|virsh|vmstat|wait|watch|wc|wget|whereis|which|who|whoami|write|xargs|xdg-open|yarn|yes|zenity|zip|zsh|zypper)(?=$|[)\s;|&])/,
|
---|
| 170 | lookbehind: true
|
---|
| 171 | },
|
---|
| 172 | keyword: {
|
---|
| 173 | pattern:
|
---|
| 174 | /(^|[\s;|&]|[<>]\()(?:case|do|done|elif|else|esac|fi|for|function|if|in|select|then|until|while)(?=$|[)\s;|&])/,
|
---|
| 175 | lookbehind: true
|
---|
| 176 | },
|
---|
| 177 | // https://www.gnu.org/software/bash/manual/html_node/Shell-Builtin-Commands.html
|
---|
| 178 | builtin: {
|
---|
| 179 | pattern:
|
---|
| 180 | /(^|[\s;|&]|[<>]\()(?:\.|:|alias|bind|break|builtin|caller|cd|command|continue|declare|echo|enable|eval|exec|exit|export|getopts|hash|help|let|local|logout|mapfile|printf|pwd|read|readarray|readonly|return|set|shift|shopt|source|test|times|trap|type|typeset|ulimit|umask|unalias|unset)(?=$|[)\s;|&])/,
|
---|
| 181 | lookbehind: true,
|
---|
| 182 | // Alias added to make those easier to distinguish from strings.
|
---|
| 183 | alias: 'class-name'
|
---|
| 184 | },
|
---|
| 185 | boolean: {
|
---|
| 186 | pattern: /(^|[\s;|&]|[<>]\()(?:false|true)(?=$|[)\s;|&])/,
|
---|
| 187 | lookbehind: true
|
---|
| 188 | },
|
---|
| 189 | 'file-descriptor': {
|
---|
| 190 | pattern: /\B&\d\b/,
|
---|
| 191 | alias: 'important'
|
---|
| 192 | },
|
---|
| 193 | operator: {
|
---|
| 194 | // Lots of redirections here, but not just that.
|
---|
| 195 | pattern:
|
---|
| 196 | /\d?<>|>\||\+=|=[=~]?|!=?|<<[<-]?|[&\d]?>>|\d[<>]&?|[<>][&=]?|&[>&]?|\|[&|]?/,
|
---|
| 197 | inside: {
|
---|
| 198 | 'file-descriptor': {
|
---|
| 199 | pattern: /^\d/,
|
---|
| 200 | alias: 'important'
|
---|
| 201 | }
|
---|
| 202 | }
|
---|
| 203 | },
|
---|
| 204 | punctuation: /\$?\(\(?|\)\)?|\.\.|[{}[\];\\]/,
|
---|
| 205 | number: {
|
---|
| 206 | pattern: /(^|\s)(?:[1-9]\d*|0)(?:[.,]\d+)?\b/,
|
---|
| 207 | lookbehind: true
|
---|
| 208 | }
|
---|
| 209 | }
|
---|
| 210 | commandAfterHeredoc.inside = Prism.languages.bash
|
---|
| 211 | /* Patterns in command substitution. */
|
---|
| 212 | var toBeCopied = [
|
---|
| 213 | 'comment',
|
---|
| 214 | 'function-name',
|
---|
| 215 | 'for-or-select',
|
---|
| 216 | 'assign-left',
|
---|
| 217 | 'string',
|
---|
| 218 | 'environment',
|
---|
| 219 | 'function',
|
---|
| 220 | 'keyword',
|
---|
| 221 | 'builtin',
|
---|
| 222 | 'boolean',
|
---|
| 223 | 'file-descriptor',
|
---|
| 224 | 'operator',
|
---|
| 225 | 'punctuation',
|
---|
| 226 | 'number'
|
---|
| 227 | ]
|
---|
| 228 | var inside = insideString.variable[1].inside
|
---|
| 229 | for (var i = 0; i < toBeCopied.length; i++) {
|
---|
| 230 | inside[toBeCopied[i]] = Prism.languages.bash[toBeCopied[i]]
|
---|
| 231 | }
|
---|
| 232 | Prism.languages.shell = Prism.languages.bash
|
---|
| 233 | })(Prism)
|
---|
| 234 | }
|
---|