| 1 |
/* ************************************************************************ */ |
| 2 |
/* */ |
| 3 |
/* This file is part of Frama-C. */ |
| 4 |
/* */ |
| 5 |
/* Copyright (C) 2007-2022 */ |
| 6 |
/* CEA (Commissariat à l'énergie atomique et aux énergies */ |
| 7 |
/* alternatives) */ |
| 8 |
/* */ |
| 9 |
/* you can redistribute it and/or modify it under the terms of the GNU */ |
| 10 |
/* Lesser General Public License as published by the Free Software */ |
| 11 |
/* Foundation, version 2.1. */ |
| 12 |
/* */ |
| 13 |
/* It is distributed in the hope that it will be useful, */ |
| 14 |
/* but WITHOUT ANY WARRANTY; without even the implied warranty of */ |
| 15 |
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */ |
| 16 |
/* GNU Lesser General Public License for more details. */ |
| 17 |
/* */ |
| 18 |
/* See the GNU Lesser General Public License version 2.1 */ |
| 19 |
/* for more details (enclosed in the file licenses/LGPLv2.1). */ |
| 20 |
/* */ |
| 21 |
/* ************************************************************************ */ |
| 22 |
|
| 23 |
// -------------------------------------------------------------------------- |
| 24 |
// --- Server Controller |
| 25 |
// -------------------------------------------------------------------------- |
| 26 |
|
| 27 |
import React from 'react'; |
| 28 |
import * as Dome from 'dome'; |
| 29 |
import * as Json from 'dome/data/json'; |
| 30 |
import * as Settings from 'dome/data/settings'; |
| 31 |
import * as Preferences from 'ivette/prefs'; |
| 32 |
import * as Toolbars from 'dome/frame/toolbars'; |
| 33 |
import { IconButton } from 'dome/controls/buttons'; |
| 34 |
import { LED, LEDstatus } from 'dome/controls/displays'; |
| 35 |
import { Label, Code } from 'dome/controls/labels'; |
| 36 |
import { RichTextBuffer } from 'dome/text/buffers'; |
| 37 |
import { Text } from 'dome/text/editors'; |
| 38 |
import { resolve } from 'dome/system'; |
| 39 |
|
| 40 |
import * as Ivette from 'ivette'; |
| 41 |
import * as Server from 'frama-c/server'; |
| 42 |
|
| 43 |
// -------------------------------------------------------------------------- |
| 44 |
// --- Configure Server |
| 45 |
// -------------------------------------------------------------------------- |
| 46 |
|
| 47 |
const quoteRe = new RegExp('^[-_./:a-zA-Z0-9]+$'); |
| 48 |
const quote = (s: string): string => |
| 49 |
(quoteRe.test(s) ? s : `"${s}"`); |
| 50 |
|
| 51 |
const unquoteRe = new RegExp('^".*"$'); |
| 52 |
const unquote = (s: string): string => |
| 53 |
(unquoteRe.test(s) ? s.substring(1, s.length - 1) : s); |
| 54 |
|
| 55 |
function dumpServerConfig(sc: Server.Configuration): string { |
| 56 |
let buffer = ''; |
| 57 |
const { working, command, sockaddr, params } = sc; |
| 58 |
if (working) buffer += `--working ${quote(working)}\n`; |
| 59 |
if (command) buffer += `--command ${quote(command)}\n`; |
| 60 |
if (sockaddr) buffer += `--socket ${sockaddr}\n`; |
| 61 |
if (params) { |
| 62 |
params.forEach((v: string, i: number) => { |
| 63 |
if (i > 0) { |
| 64 |
if (v.startsWith('-') || v.endsWith('.c') |
| 65 |
|| v.endsWith('.h') || v.endsWith('.i')) { |
| 66 |
buffer += '\n'; |
| 67 |
} else |
| 68 |
buffer += ' '; |
| 69 |
} |
| 70 |
buffer += v; |
| 71 |
}); |
| 72 |
} |
| 73 |
return buffer; |
| 74 |
} |
| 75 |
|
| 76 |
function buildServerConfig(argv: string[], cwd?: string): Server.Configuration { |
| 77 |
const params = []; |
| 78 |
let command; |
| 79 |
let sockaddr; |
| 80 |
let working = cwd ? unquote(cwd) : undefined; |
| 81 |
for (let k = 0; k < (argv ? argv.length : 0); k++) { |
| 82 |
const v = argv[k]; |
| 83 |
switch (v) { |
| 84 |
case '-C': |
| 85 |
case '--working': |
| 86 |
case '--cwd': // Deprecated |
| 87 |
k += 1; |
| 88 |
working = resolve(unquote(argv[k])); |
| 89 |
break; |
| 90 |
case '-B': |
| 91 |
case '--command': |
| 92 |
k += 1; |
| 93 |
command = resolve(unquote(argv[k])); |
| 94 |
break; |
| 95 |
case '-U': |
| 96 |
case '--socket': |
| 97 |
k += 1; |
| 98 |
sockaddr = argv[k]; |
| 99 |
break; |
| 100 |
default: |
| 101 |
params.push(v); |
| 102 |
} |
| 103 |
} |
| 104 |
return { |
| 105 |
working, |
| 106 |
command, |
| 107 |
sockaddr, |
| 108 |
params, |
| 109 |
}; |
| 110 |
} |
| 111 |
|
| 112 |
function buildServerCommand(cmd: string): Server.Configuration { |
| 113 |
return buildServerConfig(cmd.trim().split(/[ \t\n]+/)); |
| 114 |
} |
| 115 |
|
| 116 |
/* -------------------------------------------------------------------------- */ |
| 117 |
/* --- History Management --- */ |
| 118 |
/* -------------------------------------------------------------------------- */ |
| 119 |
|
| 120 |
const historySetting = 'Controller.history'; |
| 121 |
const historyDecoder = Json.jList(Json.jString); |
| 122 |
|
| 123 |
function getHistory(): string[] { |
| 124 |
return Settings.getLocalStorage(historySetting, historyDecoder, []); |
| 125 |
} |
| 126 |
|
| 127 |
function setHistory(hs: string[]): void { |
| 128 |
Settings.setLocalStorage(historySetting, hs); |
| 129 |
} |
| 130 |
|
| 131 |
function useHistory(): [string[], ((hs: string[]) => void)] { |
| 132 |
return Settings.useLocalStorage(historySetting, historyDecoder, []); |
| 133 |
} |
| 134 |
|
| 135 |
function insertConfig(hs: string[], cfg: Server.Configuration): string[] { |
| 136 |
const cmd = dumpServerConfig(cfg).trim(); |
| 137 |
const newhs = |
| 138 |
hs.map((h) => h.trim()) |
| 139 |
.filter((h: string) => h !== cmd && h !== '') |
| 140 |
.slice(0, 50); |
| 141 |
newhs.unshift(cmd); |
| 142 |
return newhs; |
| 143 |
} |
| 144 |
|
| 145 |
// -------------------------------------------------------------------------- |
| 146 |
// --- Start Server on Command |
| 147 |
// -------------------------------------------------------------------------- |
| 148 |
|
| 149 |
let reloadCommand: string | undefined; |
| 150 |
|
| 151 |
Dome.reload.on(() => { |
| 152 |
const [lastCmd] = getHistory(); |
| 153 |
reloadCommand = lastCmd; |
| 154 |
}); |
| 155 |
|
| 156 |
Dome.onCommand((argv: string[], cwd: string) => { |
| 157 |
let cfg; |
| 158 |
if (reloadCommand) { |
| 159 |
cfg = buildServerCommand(reloadCommand); |
| 160 |
} else { |
| 161 |
const hs = getHistory(); |
| 162 |
if (argv.find((v) => v === '--reload' || v === '-R')) { |
| 163 |
cfg = buildServerCommand(hs[0]); |
| 164 |
} else { |
| 165 |
cfg = buildServerConfig(argv, cwd); |
| 166 |
setHistory(insertConfig(hs, cfg)); |
| 167 |
} |
| 168 |
} |
| 169 |
Server.setConfig(cfg); |
| 170 |
Server.start(); |
| 171 |
}); |
| 172 |
|
| 173 |
// -------------------------------------------------------------------------- |
| 174 |
// --- Server Control |
| 175 |
// -------------------------------------------------------------------------- |
| 176 |
|
| 177 |
export const Control = (): JSX.Element => { |
| 178 |
const status = Server.useStatus(); |
| 179 |
|
| 180 |
let play = { enabled: false, onClick: () => { /* do nothing */ } }; |
| 181 |
let stop = { enabled: false, onClick: () => { /* do nothing */ } }; |
| 182 |
let reload = { enabled: false, onClick: () => { /* do nothing */ } }; |
| 183 |
|
| 184 |
switch (status) { |
| 185 |
case Server.Status.OFF: |
| 186 |
play = { enabled: true, onClick: Server.start }; |
| 187 |
break; |
| 188 |
case Server.Status.ON: |
| 189 |
case Server.Status.CMD: |
| 190 |
case Server.Status.FAILURE: |
| 191 |
stop = { enabled: true, onClick: Server.stop }; |
| 192 |
reload = { enabled: true, onClick: Server.restart }; |
| 193 |
break; |
| 194 |
default: |
| 195 |
break; |
| 196 |
} |
| 197 |
|
| 198 |
return ( |
| 199 |
<Toolbars.ButtonGroup> |
| 200 |
<Toolbars.Button |
| 201 |
icon="MEDIA.PLAY" |
| 202 |
enabled={play.enabled} |
| 203 |
onClick={play.onClick} |
| 204 |
title="Start the server" |
| 205 |
/> |
| 206 |
<Toolbars.Button |
| 207 |
icon="RELOAD" |
| 208 |
enabled={reload.enabled} |
| 209 |
onClick={reload.onClick} |
| 210 |
title="Re-start the server" |
| 211 |
/> |
| 212 |
<Toolbars.Button |
| 213 |
icon="MEDIA.STOP" |
| 214 |
enabled={stop.enabled} |
| 215 |
onClick={stop.onClick} |
| 216 |
title="Shut down the server" |
| 217 |
/> |
| 218 |
</Toolbars.ButtonGroup> |
| 219 |
); |
| 220 |
}; |
| 221 |
|
| 222 |
// -------------------------------------------------------------------------- |
| 223 |
// --- Server Console |
| 224 |
// -------------------------------------------------------------------------- |
| 225 |
|
| 226 |
const editor = new RichTextBuffer(); |
| 227 |
|
| 228 |
const RenderConsole = (): JSX.Element => { |
| 229 |
const scratch = React.useRef([] as string[]); |
| 230 |
const [cursor, setCursor] = React.useState(-1); |
| 231 |
const [isEmpty, setEmpty] = React.useState(true); |
| 232 |
const [noTrash, setNoTrash] = React.useState(true); |
| 233 |
const [history, setHistory] = useHistory(); |
| 234 |
|
| 235 |
React.useEffect(() => { |
| 236 |
const callback = (): void => { |
| 237 |
const cmd = editor.getValue().trim(); |
| 238 |
setEmpty(cmd === ''); |
| 239 |
setNoTrash(noTrash && cmd === history[0]); |
| 240 |
}; |
| 241 |
editor.on('change', callback); |
| 242 |
return () => { editor.off('change', callback); }; |
| 243 |
}); |
| 244 |
|
| 245 |
const [maxLines] = Settings.useGlobalSettings(Preferences.ConsoleScrollback); |
| 246 |
React.useEffect(() => { |
| 247 |
Server.buffer.setMaxlines(maxLines); |
| 248 |
}); |
| 249 |
|
| 250 |
const doReload = (): void => { |
| 251 |
const cfg = Server.getConfig(); |
| 252 |
const hst = insertConfig(history, cfg); |
| 253 |
const cmd = hst[0]; |
| 254 |
scratch.current = hst.slice(); |
| 255 |
editor.setValue(cmd); |
| 256 |
setEmpty(cmd === ''); |
| 257 |
setHistory(hst); |
| 258 |
setCursor(0); |
| 259 |
}; |
| 260 |
|
| 261 |
const doSwitch = (): void => { |
| 262 |
if (cursor < 0) doReload(); |
| 263 |
else { |
| 264 |
editor.clear(); |
| 265 |
scratch.current = []; |
| 266 |
setCursor(-1); |
| 267 |
} |
| 268 |
}; |
| 269 |
|
| 270 |
const doExec = (): void => { |
| 271 |
const cfg = buildServerCommand(editor.getValue()); |
| 272 |
const hst = insertConfig(history, cfg); |
| 273 |
setHistory(hst); |
| 274 |
setCursor(-1); |
| 275 |
editor.clear(); |
| 276 |
Server.setConfig(cfg); |
| 277 |
Server.restart(); |
| 278 |
}; |
| 279 |
|
| 280 |
const doMove = (target: number): (undefined | (() => void)) => { |
| 281 |
if (0 <= target && target < history.length && target !== cursor) |
| 282 |
return (): void => { |
| 283 |
const cmd = editor.getValue(); |
| 284 |
const pad = scratch.current; |
| 285 |
pad[cursor] = cmd; |
| 286 |
const cmd2 = pad[target]; |
| 287 |
editor.setValue(cmd2); |
| 288 |
setEmpty(cmd2 === ''); |
| 289 |
setCursor(target); |
| 290 |
}; |
| 291 |
return undefined; |
| 292 |
}; |
| 293 |
|
| 294 |
const doRemove = (): void => { |
| 295 |
const n = history.length; |
| 296 |
if (n <= 1) doReload(); |
| 297 |
else { |
| 298 |
const hst = history.slice(); |
| 299 |
const pad = scratch.current; |
| 300 |
hst.splice(cursor, 1); |
| 301 |
pad.splice(cursor, 1); |
| 302 |
setHistory(hst); |
| 303 |
const next = cursor > 0 ? cursor - 1 : 0; |
| 304 |
editor.setValue(pad[next]); |
| 305 |
setCursor(next); |
| 306 |
} |
| 307 |
}; |
| 308 |
|
| 309 |
const doPrev = doMove(cursor + 1); |
| 310 |
const doNext = doMove(cursor - 1); |
| 311 |
const edited = 0 <= cursor; |
| 312 |
const n = history.length; |
| 313 |
|
| 314 |
return ( |
| 315 |
<> |
| 316 |
<Ivette.TitleBar label={edited ? 'Command line' : 'Console'}> |
| 317 |
<IconButton |
| 318 |
icon="TRASH" |
| 319 |
display={edited} |
| 320 |
disabled={noTrash} |
| 321 |
onClick={doRemove} |
| 322 |
title="Discard command from history (irreversible)" |
| 323 |
/> |
| 324 |
<Toolbars.Space /> |
| 325 |
<IconButton |
| 326 |
icon="RELOAD" |
| 327 |
display={edited} |
| 328 |
onClick={doReload} |
| 329 |
title="Discard changes" |
| 330 |
/> |
| 331 |
<IconButton |
| 332 |
icon="ANGLE.LEFT" |
| 333 |
display={edited} |
| 334 |
onClick={doPrev} |
| 335 |
title="Previous command" |
| 336 |
/> |
| 337 |
<IconButton |
| 338 |
icon="ANGLE.RIGHT" |
| 339 |
display={edited} |
| 340 |
onClick={doNext} |
| 341 |
title="Next command" |
| 342 |
/> |
| 343 |
<Toolbars.Space /> |
| 344 |
<Label |
| 345 |
className="component-info" |
| 346 |
title="History (last command first)" |
| 347 |
display={edited} |
| 348 |
> |
| 349 |
{1 + cursor} / {n} |
| 350 |
</Label> |
| 351 |
<Toolbars.Space /> |
| 352 |
<IconButton |
| 353 |
icon="MEDIA.PLAY" |
| 354 |
display={edited} |
| 355 |
disabled={isEmpty} |
| 356 |
onClick={doExec} |
| 357 |
title="Execute command" |
| 358 |
/> |
| 359 |
<IconButton |
| 360 |
icon="TERMINAL" |
| 361 |
selected={edited} |
| 362 |
onClick={doSwitch} |
| 363 |
title="Toggle command line editing" |
| 364 |
/> |
| 365 |
</Ivette.TitleBar> |
| 366 |
<Text |
| 367 |
buffer={edited ? editor : Server.buffer} |
| 368 |
mode="text" |
| 369 |
readOnly={!edited} |
| 370 |
/> |
| 371 |
</> |
| 372 |
); |
| 373 |
}; |
| 374 |
|
| 375 |
Ivette.registerComponent({ |
| 376 |
id: 'frama-c.console', |
| 377 |
group: 'frama-c.kernel', |
| 378 |
label: 'Console', |
| 379 |
title: 'Frama-C Server Output & Command Line', |
| 380 |
rank: -1, |
| 381 |
children: <RenderConsole />, |
| 382 |
}); |
| 383 |
|
| 384 |
Ivette.registerView({ |
| 385 |
id: 'console', |
| 386 |
rank: -1, |
| 387 |
label: 'Console', |
| 388 |
layout: 'frama-c.console', |
| 389 |
}); |
| 390 |
|
| 391 |
// -------------------------------------------------------------------------- |
| 392 |
// --- Status |
| 393 |
// -------------------------------------------------------------------------- |
| 394 |
|
| 395 |
export const Status = (): JSX.Element => { |
| 396 |
const status = Server.useStatus(); |
| 397 |
const pending = Server.getPending(); |
| 398 |
let led: LEDstatus = 'inactive'; |
| 399 |
let title = undefined; |
| 400 |
let icon = undefined; |
| 401 |
let running = 'OFF'; |
| 402 |
let blink = false; |
| 403 |
|
| 404 |
switch (status) { |
| 405 |
case Server.Status.OFF: |
| 406 |
title = 'Server is off'; |
| 407 |
break; |
| 408 |
case Server.Status.STARTING: |
| 409 |
led = 'active'; |
| 410 |
blink = true; |
| 411 |
running = 'BOOT'; |
| 412 |
title = 'Server is starting'; |
| 413 |
break; |
| 414 |
case Server.Status.ON: |
| 415 |
led = 'active'; |
| 416 |
blink = pending > 0; |
| 417 |
running = 'ON'; |
| 418 |
title = 'Server is running'; |
| 419 |
break; |
| 420 |
case Server.Status.CMD: |
| 421 |
led = 'positive'; |
| 422 |
blink = true; |
| 423 |
running = 'CMD'; |
| 424 |
title = 'Command-line processing'; |
| 425 |
break; |
| 426 |
case Server.Status.HALTING: |
| 427 |
led = 'negative'; |
| 428 |
blink = true; |
| 429 |
running = 'HALT'; |
| 430 |
title = 'Server is halting'; |
| 431 |
break; |
| 432 |
case Server.Status.RESTARTING: |
| 433 |
led = 'warning'; |
| 434 |
blink = true; |
| 435 |
running = 'REBOOT'; |
| 436 |
title = 'Server is restarting'; |
| 437 |
break; |
| 438 |
case Server.Status.FAILURE: |
| 439 |
led = 'negative'; |
| 440 |
blink = true; |
| 441 |
running = 'ERR'; |
| 442 |
title = 'Server halted because of failure'; |
| 443 |
icon = 'WARNING'; |
| 444 |
break; |
| 445 |
} |
| 446 |
|
| 447 |
return ( |
| 448 |
<> |
| 449 |
<LED status={led} blink={blink} /> |
| 450 |
<Code icon={icon} label={running} title={title} /> |
| 451 |
<Toolbars.Separator /> |
| 452 |
</> |
| 453 |
); |
| 454 |
}; |
| 455 |
|
| 456 |
// -------------------------------------------------------------------------- |
| 457 |
// --- Server Stats |
| 458 |
// -------------------------------------------------------------------------- |
| 459 |
|
| 460 |
export const Stats = (): (null | JSX.Element) => { |
| 461 |
Server.useStatus(); |
| 462 |
const pending = Server.getPending(); |
| 463 |
return pending > 0 ? <Code>{pending} rq.</Code> : null; |
| 464 |
}; |
| 465 |
|
| 466 |
// -------------------------------------------------------------------------- |