/usr/share/cinnamon/js/misc/util.js is in cinnamon-common 3.6.7-8ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 | /**
* FILE:util.js
* @short_description: File providing certain utility functions
*
* This file includes certain useful utility functions such as running external
* commands. It is generally a good idea to use the functions defined here
* instead of tapping into GLib directly since this adds some wrappers around
* the functions that make them more Cinnamon-friendly and provides helpful
* error messages.
*/
const GLib = imports.gi.GLib;
const Main = imports.ui.main;
// http://daringfireball.net/2010/07/improved_regex_for_matching_urls
const _balancedParens = '\\((?:[^\\s()<>]+|(?:\\(?:[^\\s()<>]+\\)))*\\)';
const _leadingJunk = '[\\s`(\\[{\'\\"<\u00AB\u201C\u2018]';
const _notTrailingJunk = '[^\\s`!()\\[\\]{};:\'\\".,<>?\u00AB\u00BB\u201C\u201D\u2018\u2019]';
const _urlRegexp = new RegExp(
'(^|' + _leadingJunk + ')' +
'(' +
'(?:' +
'[a-z][\\w-]+://' + // scheme://
'|' +
'www\\d{0,3}[.]' + // www.
'|' +
'[a-z0-9.\\-]+[.][a-z]{2,4}/' + // foo.xx/
')' +
'(?:' + // one or more:
'[^\\s()<>]+' + // run of non-space non-()
'|' + // or
_balancedParens + // balanced parens
')+' +
'(?:' + // end with:
_balancedParens + // balanced parens
'|' + // or
_notTrailingJunk + // last non-junk char
')' +
')', 'gi');
/**
* findUrls:
* @str: string to find URLs in
*
* Searches @str for URLs and returns an array of objects with %url
* properties showing the matched URL string, and %pos properties indicating
* the position within @str where the URL was found.
*
* Returns: the list of match objects, as described above
*/
function findUrls(str) {
let res = [], match;
while ((match = _urlRegexp.exec(str)))
res.push({ url: match[2], pos: match.index + match[1].length });
return res;
}
/**
* spawn:
* @argv: an argv array
*
* Runs @argv in the background, handling any errors that occur
* when trying to start the program.
*/
function spawn(argv) {
let pid;
try {
pid = trySpawn(argv);
} catch (err) {
_handleSpawnError(argv[0], err);
}
return pid;
}
let subprocess_id = 0;
let subprocess_callbacks = {};
/**
* spawn_async:
* @args: an array containing all arguments of the command to be run
* @callback: the callback to run when the command has completed
*
* Asynchronously Runs the command passed to @args. When the command is complete, the callback will
* be called with the contents of stdout from the command passed as the only argument.
*/
function spawn_async(args, callback) {
subprocess_id++;
subprocess_callbacks[subprocess_id] = callback;
spawn(new Array("cinnamon-subprocess-wrapper", subprocess_id.toString()).concat(args));
}
/**
* spawnCommandLine:
* @command_line: a command line
*
* Runs @command_line in the background, handling any errors that
* occur when trying to parse or start the program.
*/
function spawnCommandLine(command_line) {
let pid;
try {
let [success, argv] = GLib.shell_parse_argv(command_line);
pid = trySpawn(argv);
} catch (err) {
_handleSpawnError(command_line, err);
}
return pid;
}
/**
* trySpawn:
* @argv: an argv array
* @doNotReap: whether to set the DO_NOT_REAP_CHILD flag
*
* Runs @argv in the background. If launching @argv fails,
* this will throw an error.
*/
function trySpawn(argv, doNotReap)
{
let spawn_flags = GLib.SpawnFlags.SEARCH_PATH
| GLib.SpawnFlags.STDOUT_TO_DEV_NULL
| GLib.SpawnFlags.STDERR_TO_DEV_NULL;
if (doNotReap) {
spawn_flags |= GLib.SpawnFlags.DO_NOT_REAP_CHILD;
}
let [success, pid] = GLib.spawn_async(null, argv, null, spawn_flags, null);
return pid;
}
/**
* trySpawnCommandLine:
* @command_line: a command line
*
* Runs @command_line in the background. If launching @command_line
* fails, this will throw an error.
*/
function trySpawnCommandLine(command_line) {
let pid;
let [success, argv] = GLib.shell_parse_argv(command_line);
pid = trySpawn(argv);
return pid;
}
/**
* spawnCommandLineAsync:
* @command_line: a command line
* @callback (function): called on success
* @errback (function): called on error
*
* Runs @command_line in the background. If the process exits without
* error, a callback will be called, or an error callback will be
* called if one is provided.
*/
function spawnCommandLineAsync(command_line, callback, errback) {
let pid;
let [success, argv] = GLib.shell_parse_argv(command_line);
pid = trySpawn(argv, true);
GLib.child_watch_add(GLib.PRIORITY_DEFAULT, pid, function(pid, status) {
GLib.spawn_close_pid(pid);
if (status !== 0) {
if (typeof errback === 'function') {
errback();
}
} else {
if (typeof callback === 'function') {
callback();
}
}
});
}
function _handleSpawnError(command, err) {
let title = _("Execution of '%s' failed:").format(command);
Main.notifyError(title, err.message);
}
/**
* killall:
* @processName: a process name
*
* Kills @processName. If no process with the given name is found,
* this will fail silently.
*/
function killall(processName) {
try {
// pkill is more portable than killall, but on Linux at least
// it won't match if you pass more than 15 characters of the
// process name... However, if you use the '-f' flag to match
// the entire command line, it will work, but we have to be
// careful in that case that we can match
// '/usr/bin/processName' but not 'gedit processName.c' or
// whatever...
let argv = ['pkill', '-f', '^([^ ]*/)?' + processName + '($| )'];
GLib.spawn_sync(null, argv, null, GLib.SpawnFlags.SEARCH_PATH, null);
// It might be useful to return success/failure, but we'd need
// a wrapper around WIFEXITED and WEXITSTATUS. Since none of
// the current callers care, we don't bother.
} catch (e) {
logError(e, 'Failed to kill ' + processName);
}
}
// This was ported from network-manager-applet
// Copyright 2007 - 2011 Red Hat, Inc.
// Author: Dan Williams <dcbw@redhat.com>
const _IGNORED_WORDS = [
'Semiconductor',
'Components',
'Corporation',
'Communications',
'Company',
'Corp.',
'Corp',
'Co.',
'Inc.',
'Inc',
'Incorporated',
'Ltd.',
'Limited.',
'Intel',
'chipset',
'adapter',
'[hex]',
'NDIS',
'Module'
];
const _IGNORED_PHRASES = [
'Multiprotocol MAC/baseband processor',
'Wireless LAN Controller',
'Wireless LAN Adapter',
'Wireless Adapter',
'Network Connection',
'Wireless Cardbus Adapter',
'Wireless CardBus Adapter',
'54 Mbps Wireless PC Card',
'Wireless PC Card',
'Wireless PC',
'PC Card with XJACK(r) Antenna',
'Wireless cardbus',
'Wireless LAN PC Card',
'Technology Group Ltd.',
'Communication S.p.A.',
'Business Mobile Networks BV',
'Mobile Broadband Minicard Composite Device',
'Mobile Communications AB',
'(PC-Suite Mode)'
];
function fixupPCIDescription(desc) {
desc = desc.replace(/[_,]/, ' ');
/* Attempt to shorten ID by ignoring certain phrases */
for (let i = 0; i < _IGNORED_PHRASES.length; i++) {
let item = _IGNORED_PHRASES[i];
let pos = desc.indexOf(item);
if (pos != -1) {
let before = desc.substring(0, pos);
let after = desc.substring(pos + item.length, desc.length);
desc = before + after;
}
}
/* Attmept to shorten ID by ignoring certain individual words */
let words = desc.split(' ');
let out = [ ];
for (let i = 0; i < words.length; i++) {
let item = words[i];
// skip empty items (that come out from consecutive spaces)
if (item.length == 0)
continue;
if (_IGNORED_WORDS.indexOf(item) == -1) {
out.push(item);
}
}
return out.join(' ');
}
// key: normal char, value: regex containing all chars with accents
const _LATINISE_REGEX = {
//uppercase
A: /[\xC0-\xC5\u0100\u0102\u0104]/g,
AE: /\xC6/g,
C: /[\xC7\u0106\u0108\u010A\u010C]/g,
D: /[\xD0\u010E\u0110]/g,
E: /[\xC8-\xCB\u0112\u0114\u0116\u0118\u011A]/g,
G: /[\u011C\u011E\u0120\u0122]/g,
H: /[\u0124\u0126]/g,
I: /[\xCC-\xCF\u0128\u012A\u012C\u0130]/g,
IJ: /\u0132/g,
J: /[\u012E\u0134]/g,
K: /\u0136/g,
L: /[\u0139\u013B\u013D\u0130F\u0141]/g,
N: /[\xD1\u0143\u0145\u0147\u014A]/g,
O: /[\xD2-\xD6\xD8\u014C\u014E\u0150]/g,
OE: /\u0152/g,
R: /[\u0154\u0156\u0158]/g,
S: /[\u015A\u015C\u015E\u0160]/g,
T: /[\u0162\u0164\u0166]/g,
U: /[\xD9-\xDC\u0168\u016A\u016C\u016E\u0170\u0172]/g,
W: /\u0174/g,
Y: /[\xDD\u0176\u0178]/g,
Z: /[\u0179\u017B\u017D]/g,
//lowercase
a: /[\xE0-\xE5\u0101\u0103\u0105]/g,
ae: /\xE6/g,
c: /[\xE7\u0107\u0109\u010B\u010D]/g,
d: /[\u010F\u0111]/g,
e: /[\xE8-\xEB\u0113\u0115\u0117\u0119\u011B]/g,
g: /[\u011D\u011F\u0121\u0123]/g,
h: /[\u0125\u0127]/g,
i: /[\xEC-\xEF\u0129\u012B\u012D\u0131]/g,
ij: /\u0133/g,
j: /[\u012F\u0135]/g,
k: /[\u0137\u0138]/g,
l: /[\u013A\u013C\u013E\u0140\u0142]/g,
n: /[\xF1\u0144\u0146\u0148\u0149\u014B]/g,
o: /[\xF2-\xF6\xF8\u014D\u014F\u0151]/g,
oe: /\u0153/g,
r: /[\u0155\u0157\u0159]/g,
s: /[\u015B\u015D\u015F\u0161]/g,
t: /[\u0163\u0165\u0167]/g,
u: /[\xF9-\xFC\u0169\u016B\u016D\u016F\u0171\u0173]/g,
w: /\u0175/g,
y: /[\xFD\xFF\u0177]/g,
z: /[\u017A\u017C\u017E]/g
};
/**
* latinise:
* @string (string): a string
*
* Returns (string): @string, replaced accented chars
*/
function latinise(string){
//call every regex to replace chars
for(var i in _LATINISE_REGEX){
string = string.replace(_LATINISE_REGEX[i], i);
}
return string;
}
|