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
|
// -*- Mode: Go; indent-tabs-mode: t -*-
/*
* Copyright (C) 2023 Canonical Ltd
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
package desktopentry
import (
"bytes"
"fmt"
"net/url"
"path/filepath"
"strings"
"github.com/snapcore/snapd/strutil/shlex"
)
func shellQuote(s string) string {
return "'" + strings.Replace(s, "'", "'\\''", -1) + "'"
}
func toFilePath(uri string) (string, error) {
u, err := url.Parse(uri)
if err != nil {
return "", err
}
if !u.IsAbs() {
return "", fmt.Errorf("%q is not an absolute URI", uri)
}
if u.Scheme != "file" {
return "", fmt.Errorf("%q is not a file URI", uri)
}
if !filepath.IsAbs(u.Path) {
return "", fmt.Errorf("%q does not have an absolute file path", uri)
}
return u.Path, nil
}
func expandMacro(r rune, buf *bytes.Buffer, de *DesktopEntry, uris []string) ([]string, error) {
switch r {
case 'u':
if len(uris) > 0 {
// Format as a file path, falling back to a URI
arg, err := toFilePath(uris[0])
if err != nil {
arg = uris[0]
}
buf.WriteString(shellQuote(arg))
uris = uris[1:]
}
case 'U':
first := true
for _, u := range uris {
if !first {
buf.WriteRune(' ')
}
first = false
// Format as a file path, falling back to a URI
arg, err := toFilePath(u)
if err != nil {
arg = u
}
buf.WriteString(shellQuote(arg))
}
uris = nil
case 'f':
if len(uris) > 0 {
arg, err := toFilePath(uris[0])
if err != nil {
return nil, err
}
buf.WriteString(shellQuote(arg))
uris = uris[1:]
}
case 'F':
first := true
for _, u := range uris {
if !first {
buf.WriteRune(' ')
}
first = false
arg, err := toFilePath(u)
if err != nil {
return nil, err
}
buf.WriteString(shellQuote(arg))
}
uris = nil
case 'i':
if de.Icon != "" {
buf.WriteString("--icon ")
buf.WriteString(shellQuote(de.Icon))
}
case 'c':
if de.Name != "" {
buf.WriteString(shellQuote(de.Name))
}
case 'k':
if de.Filename != "" {
buf.WriteString(shellQuote(de.Filename))
}
case '%':
buf.WriteRune('%')
}
return uris, nil
}
// expandExec expands macros within a desktop entry exec variable.
//
// The format is described in the Desktop Entry Specification:
//
// https://specifications.freedesktop.org/desktop-entry-spec/desktop-entry-spec-latest.html#exec-variables
//
// It uses glib implementation as a reference where the spec is
// ambiguous.
func expandExec(de *DesktopEntry, exec string, uris []string) (args []string, err error) {
var buf bytes.Buffer
isMacro := false
nUris := len(uris)
for _, r := range exec {
if isMacro {
if uris, err = expandMacro(r, &buf, de, uris); err != nil {
return nil, err
}
isMacro = false
} else if r == '%' {
isMacro = true
} else {
buf.WriteRune(r)
}
}
// If no URI substitutions have happened, add a single
// implicit %f argument
if len(uris) > 0 && len(uris) == nUris {
buf.WriteRune(' ')
if _, err = expandMacro('f', &buf, de, uris); err != nil {
return nil, err
}
}
return shlex.Split(buf.String())
}
|