| 1 | @[has_globals] |
| 2 | module main |
| 3 | |
| 4 | import flag |
| 5 | import os |
| 6 | import vmakepkg |
| 7 | |
| 8 | struct ConfigSearch { |
| 9 | pub mut: |
| 10 | show_help bool @[long: help; short: h] |
| 11 | noconfirm bool @[long: noconfirm] |
| 12 | local bool @[long: local; short: l] |
| 13 | remote bool @[long: remote; short: r] |
| 14 | pkgbuild bool @[long: pkgbuild; short: b] |
| 15 | quiet bool @[long: quiet; short: q] |
| 16 | fuzzy bool @[long: fuzzy; short: f] |
| 17 | thold f64 = 0.9 @[long: thold; short: t] |
| 18 | sync_dbs bool @[long: sync; short: s] |
| 19 | deps bool @[long: deps; short: d] |
| 20 | explicit bool @[long: explicit; short: e] |
| 21 | foreign bool @[long: foreign; short: m] |
| 22 | native bool @[long: native; short: n] |
| 23 | unrequired bool @[long: unrequired] |
| 24 | // --unrequired given twice is pacman's -tt; the flag parser counts short |
| 25 | // flags only, so parse_mode counts the long one itself |
| 26 | unrequired_level int @[ignore] |
| 27 | upgrades bool @[long: upgrades; short: u] |
| 28 | groups bool @[long: groups] |
| 29 | groups_level int @[only: g; repeats] |
| 30 | installed bool @[long: installed; short: i] |
| 31 | fields string @[long: fields; short: F] |
| 32 | query []string @[tail] |
| 33 | } |
| 34 | |
| 35 | struct ConfigInstall { |
| 36 | pub mut: |
| 37 | show_help bool @[long: help; short: h] |
| 38 | noconfirm bool @[long: noconfirm] |
| 39 | no_upgrade_deps bool @[long: no_upgrade_deps] |
| 40 | pkgbuild bool @[long: pkgbuild; short: b] |
| 41 | no_pkgbuild_confirm bool @[long: nopkgbuildconfirm] |
| 42 | download_only bool @[long: download_only; short: w] |
| 43 | print_only bool @[long: print; short: p] |
| 44 | needed bool @[long: needed] |
| 45 | asdeps bool @[long: asdeps] |
| 46 | asexplicit bool @[long: asexplicit] |
| 47 | dbonly bool @[long: dbonly] |
| 48 | nodeps bool @[long: nodeps] |
| 49 | nodeps_level int @[only: d; repeats] |
| 50 | overwrite []string @[long: overwrite] |
| 51 | ignore []string @[long: ignore] |
| 52 | ignoregroup []string @[long: ignoregroup] |
| 53 | assume_installed []string @[long: assume_installed] |
| 54 | disable_timeout bool @[long: disable_timeout] |
| 55 | query []string @[tail] |
| 56 | } |
| 57 | |
| 58 | // trans_options are the transaction options a command line carries: |
| 59 | // install's and upgrade's are the same four flags, read through one body |
| 60 | fn trans_options[T](c T) TransOptions { |
| 61 | return TransOptions{ |
| 62 | overwrite: c.overwrite |
| 63 | ignore: c.ignore |
| 64 | ignoregroup: c.ignoregroup |
| 65 | assume_installed: c.assume_installed |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | struct ConfigInfo { |
| 70 | pub mut: |
| 71 | show_help bool @[long: help; short: h] |
| 72 | noconfirm bool @[long: noconfirm] |
| 73 | fields string @[long: fields; short: F] |
| 74 | changelog bool @[long: changelog; short: c] |
| 75 | file bool @[long: file; short: p] |
| 76 | query []string @[tail] |
| 77 | } |
| 78 | |
| 79 | struct ConfigWhich { |
| 80 | pub mut: |
| 81 | show_help bool @[long: help; short: h] |
| 82 | query []string @[tail] |
| 83 | } |
| 84 | |
| 85 | struct ConfigRank { |
| 86 | pub mut: |
| 87 | show_help bool @[long: help; short: h] |
| 88 | stdin bool @[long: stdin] |
| 89 | top int = 10 @[long: top; short: n] |
| 90 | mirrorlist bool @[long: mirrorlist; short: m] |
| 91 | output string @[long: output; short: o] |
| 92 | path string @[long: path] |
| 93 | write bool @[long: write; short: w] |
| 94 | query []string @[tail] |
| 95 | } |
| 96 | |
| 97 | struct ConfigVerify { |
| 98 | pub mut: |
| 99 | show_help bool @[long: help; short: h] |
| 100 | checksums bool @[long: checksums] |
| 101 | checksums_level int @[only: k; repeats] |
| 102 | full bool @[long: full] |
| 103 | db bool @[long: db] |
| 104 | db_level int @[only: D; repeats] |
| 105 | query []string @[tail] |
| 106 | } |
| 107 | |
| 108 | struct ConfigFind { |
| 109 | pub mut: |
| 110 | show_help bool @[long: help; short: h] |
| 111 | noconfirm bool @[long: noconfirm] |
| 112 | refresh bool @[long: refresh; short: y] |
| 113 | list bool @[long: list; short: l] |
| 114 | quiet bool @[long: quiet; short: q] |
| 115 | regex bool @[long: regex; short: x] |
| 116 | machinereadable bool @[long: machinereadable] |
| 117 | repos string @[long: repos; short: r] |
| 118 | query []string @[tail] |
| 119 | } |
| 120 | |
| 121 | struct ConfigMark { |
| 122 | pub mut: |
| 123 | show_help bool @[long: help; short: h] |
| 124 | noconfirm bool @[long: noconfirm] |
| 125 | reason string @[long: reason; short: r] |
| 126 | query []string @[tail] |
| 127 | } |
| 128 | |
| 129 | struct ConfigRemove { |
| 130 | pub mut: |
| 131 | show_help bool @[long: help; short: h] |
| 132 | noconfirm bool @[long: noconfirm] |
| 133 | print_only bool @[long: print; short: p] |
| 134 | recursive bool @[long: recursive] |
| 135 | recursive_level int @[only: s; repeats] |
| 136 | unneeded bool @[long: unneeded; short: u] |
| 137 | cascade bool @[long: cascade; short: c] |
| 138 | nosave bool @[long: nosave; short: n] |
| 139 | dbonly bool @[long: dbonly] |
| 140 | nodeps bool @[long: nodeps] |
| 141 | nodeps_level int @[only: d; repeats] |
| 142 | assume_installed []string @[long: assume_installed] |
| 143 | query []string @[tail] |
| 144 | } |
| 145 | |
| 146 | struct ConfigUpgrade { |
| 147 | pub mut: |
| 148 | show_help bool @[long: help; short: h] |
| 149 | noconfirm bool @[long: noconfirm] |
| 150 | print_only bool @[long: print; short: p] |
| 151 | sync_dbs bool @[long: sync; short: s] |
| 152 | download_only bool @[long: download_only; short: w] |
| 153 | dbonly bool @[long: dbonly] |
| 154 | nodeps bool @[long: nodeps] |
| 155 | nodeps_level int @[only: d; repeats] |
| 156 | downgrade bool @[long: downgrade] |
| 157 | sysupgrade_level int @[only: u; repeats] |
| 158 | overwrite []string @[long: overwrite] |
| 159 | ignore []string @[long: ignore] |
| 160 | ignoregroup []string @[long: ignoregroup] |
| 161 | disable_timeout bool @[long: disable_timeout] |
| 162 | assume_installed []string @[long: assume_installed] |
| 163 | pkgbuild bool @[long: pkgbuild; short: b] |
| 164 | // nopkgbuildconfirm is install's: the pkgbuild pass asks about every |
| 165 | // candidate, and this takes the yes without asking, review still shown |
| 166 | no_pkgbuild_confirm bool @[long: nopkgbuildconfirm] |
| 167 | } |
| 168 | |
| 169 | struct ConfigFiles { |
| 170 | pub mut: |
| 171 | show_help bool @[long: help; short: h] |
| 172 | noconfirm bool @[long: noconfirm] |
| 173 | query []string @[tail] |
| 174 | } |
| 175 | |
| 176 | struct ConfigOwner { |
| 177 | pub mut: |
| 178 | show_help bool @[long: help; short: h] |
| 179 | quiet bool @[long: quiet; short: q] |
| 180 | query []string @[tail] |
| 181 | } |
| 182 | |
| 183 | struct ConfigOrphans { |
| 184 | pub mut: |
| 185 | show_help bool @[long: help; short: h] |
| 186 | // pacman's -Qdt and -Qdtt: twice lists the dependencies something still |
| 187 | // names as optional too |
| 188 | level int @[only: t; repeats] |
| 189 | } |
| 190 | |
| 191 | struct ConfigClean { |
| 192 | pub mut: |
| 193 | show_help bool @[long: help; short: h] |
| 194 | noconfirm bool @[long: noconfirm] |
| 195 | print_only bool @[long: print; short: p] |
| 196 | all bool @[long: all; short: a] |
| 197 | keep int @[long: keep; short: k] |
| 198 | keep_current bool @[long: keep_current] |
| 199 | // pacman's -Sc and -Scc: once is the default clean, twice every archive |
| 200 | level int @[only: c; repeats] |
| 201 | } |
| 202 | |
| 203 | struct ConfigTree { |
| 204 | pub mut: |
| 205 | show_help bool @[long: help; short: h] |
| 206 | noconfirm bool @[long: noconfirm] |
| 207 | reverse bool @[long: reverse; short: r] |
| 208 | optional bool @[long: optional; short: o] |
| 209 | depth int @[long: depth; short: d] |
| 210 | query []string @[tail] |
| 211 | } |
| 212 | |
| 213 | struct ConfigAutoRemove { |
| 214 | pub mut: |
| 215 | show_help bool @[long: help; short: h] |
| 216 | noconfirm bool @[long: noconfirm] |
| 217 | } |
| 218 | |
| 219 | @[noreturn] |
| 220 | fn print_err(e string) { |
| 221 | if ipc_on() { |
| 222 | // an error before the run started ends it all the same: the channel |
| 223 | // opens for it (once: ipc_start is guarded), so every stream starts |
| 224 | // with its hello and ends with its exit |
| 225 | ipc_start() |
| 226 | emit(ErrorEvent{ |
| 227 | msg: e |
| 228 | }) |
| 229 | } else { |
| 230 | eprintln(error_text(e)) |
| 231 | } |
| 232 | end_run(1) |
| 233 | } |
| 234 | |
| 235 | // error_text is an error line the way vpac prints them, 'error: ' in bold red |
| 236 | fn error_text(e string) string { |
| 237 | return erole('error', 'error: ') + e |
| 238 | } |
| 239 | |
| 240 | // failed_targets counts the targets a query reported bad and skipped. like |
| 241 | // pacman's queries, it goes on with the rest, and main exits 1 at the end |
| 242 | // when any failed |
| 243 | __global failed_targets = 0 |
| 244 | |
| 245 | // target_error reports one bad target of a query that goes on with the next |
| 246 | fn target_error(e string) { |
| 247 | failed_targets++ |
| 248 | if ipc_on() { |
| 249 | emit(ErrorEvent{ |
| 250 | msg: e |
| 251 | target: true |
| 252 | }) |
| 253 | return |
| 254 | } |
| 255 | os.flush() |
| 256 | eprintln(error_text(e)) |
| 257 | } |
| 258 | |
| 259 | // need_query stops a command that acts on targets when none were given |
| 260 | fn need_query(query []string, what string) ! { |
| 261 | if query.len == 0 { |
| 262 | return error('no ${what} given') |
| 263 | } |
| 264 | } |
| 265 | |
| 266 | // scan_globals collects vpac's own flags, wherever they stand in the command |
| 267 | // line: --config chooses the config file, --eval extends the config with |
| 268 | // code run after it. both repeatable, both also usable as --flag=value. the |
| 269 | // flags are stripped from the args (the program name stays first, the mode |
| 270 | // second), so a mode never sees them; escalation still gets the original |
| 271 | // args, so the root process reads the same config and evals |
| 272 | fn scan_globals(args []string) !(string, []string, []string) { |
| 273 | // the frontend is settled before any other flag can fail: an error is |
| 274 | // told over the channel whenever --ui ipc is on the command line, not |
| 275 | // only when it happens to come before the flag that is wrong |
| 276 | if kind := scan_ui(args) { |
| 277 | ui_kind = kind |
| 278 | } |
| 279 | mut chosen := '' |
| 280 | mut evals := []string{} |
| 281 | mut rest := [args[0]] |
| 282 | mut i := 1 |
| 283 | for i < args.len { |
| 284 | arg := args[i] |
| 285 | if arg == '--config' || arg == '--eval' { |
| 286 | if i + 1 >= args.len { |
| 287 | return error('${arg} needs a value') |
| 288 | } |
| 289 | value := args[i + 1] |
| 290 | if value == '' { |
| 291 | return error('${arg} needs a value') |
| 292 | } |
| 293 | if arg == '--config' { |
| 294 | chosen = value |
| 295 | } else { |
| 296 | evals << value |
| 297 | } |
| 298 | i += 2 |
| 299 | } else if arg.starts_with('--config=') { |
| 300 | chosen = arg.all_after('=') |
| 301 | if chosen == '' { |
| 302 | return error('--config needs a file') |
| 303 | } |
| 304 | i++ |
| 305 | } else if arg.starts_with('--eval=') { |
| 306 | expr := arg.all_after('=') |
| 307 | if expr == '' { |
| 308 | return error('--eval needs config code') |
| 309 | } |
| 310 | evals << expr |
| 311 | i++ |
| 312 | } else if arg == '--color' || arg.starts_with('--color=') { |
| 313 | // pacman's --color: auto (the default) styles a terminal only |
| 314 | mut value := arg.all_after('=') |
| 315 | if arg == '--color' { |
| 316 | if i + 1 >= args.len { |
| 317 | return error('--color needs a value: auto, always or never') |
| 318 | } |
| 319 | i++ |
| 320 | value = args[i] |
| 321 | } |
| 322 | if value !in ['auto', 'always', 'never'] { |
| 323 | return error('--color takes auto, always or never, not "${value}"') |
| 324 | } |
| 325 | color_flag = value |
| 326 | color_given = true |
| 327 | i++ |
| 328 | } else if arg == '--verbose' { |
| 329 | // the verbose log: the housekeeping failures a run goes on from |
| 330 | verbose_flag = true |
| 331 | i++ |
| 332 | } else if arg == '--ui' || arg.starts_with('--ui=') { |
| 333 | // the frontend: tty, a person at a terminal, or ipc, a program |
| 334 | // speaking json lines (ipc.v) |
| 335 | mut value := arg.all_after('=') |
| 336 | if arg == '--ui' { |
| 337 | if i + 1 >= args.len { |
| 338 | return error('--ui needs a value: tty or ipc') |
| 339 | } |
| 340 | i++ |
| 341 | value = args[i] |
| 342 | } |
| 343 | if value !in ['tty', 'ipc'] { |
| 344 | return error('--ui takes tty or ipc, not "${value}"') |
| 345 | } |
| 346 | ui_kind = value |
| 347 | i++ |
| 348 | } else { |
| 349 | rest << arg |
| 350 | i++ |
| 351 | } |
| 352 | } |
| 353 | return chosen, evals, rest |
| 354 | } |
| 355 | |
| 356 | // global_value_flags are vpac's own flags that take the next argument as |
| 357 | // their value when spelled without '=' |
| 358 | const global_value_flags = ['--config', '--eval', '--color', '--ui'] |
| 359 | |
| 360 | // scan_ui finds the --ui the command line settles on — the last valid one, |
| 361 | // as scan_globals lets the last win — stepping over the values of the other |
| 362 | // globals the way scan_globals reads them, so an --eval whose code is the |
| 363 | // text '--ui' is not taken for the flag. none when there is no valid one: |
| 364 | // a bad value is scan_globals' error to give, on the terminal |
| 365 | fn scan_ui(args []string) ?string { |
| 366 | mut found := ?string(none) |
| 367 | mut i := 1 |
| 368 | for i < args.len { |
| 369 | arg := args[i] |
| 370 | if arg.starts_with('--ui=') { |
| 371 | value := arg.all_after('=') |
| 372 | if value in ['tty', 'ipc'] { |
| 373 | found = value |
| 374 | } |
| 375 | } else if arg == '--ui' && i + 1 < args.len && args[i + 1] in ['tty', 'ipc'] { |
| 376 | found = args[i + 1] |
| 377 | } |
| 378 | i += if arg in global_value_flags { 2 } else { 1 } |
| 379 | } |
| 380 | return found |
| 381 | } |
| 382 | |
| 383 | // ModeConfig is a mode's command line, parsed: which mode, with its flags |
| 384 | type ModeConfig = ConfigAutoRemove |
| 385 | | ConfigBuild |
| 386 | | ConfigClean |
| 387 | | ConfigDeptest |
| 388 | | ConfigFiles |
| 389 | | ConfigFind |
| 390 | | ConfigInfo |
| 391 | | ConfigInstall |
| 392 | | ConfigMark |
| 393 | | ConfigOrphans |
| 394 | | ConfigOwner |
| 395 | | ConfigRank |
| 396 | | ConfigRemove |
| 397 | | ConfigSearch |
| 398 | | ConfigTree |
| 399 | | ConfigUpgrade |
| 400 | | ConfigVerify |
| 401 | | ConfigWhich |
| 402 | |
| 403 | // mode_flags reads a mode's flags into its struct, then lays the config's |
| 404 | // options under them: a flag the config turns on (options #:noconfirm #t) |
| 405 | // is on for every mode that has it, as if it stood on the command line. |
| 406 | // the command line can only add to that; --eval turns one off for a run. |
| 407 | // an option the mode's struct has no field for is an error |
| 408 | fn mode_flags[T](args []string) !T { |
| 409 | mut c, unknown := flag.to_struct[T](args, skip: 2)! |
| 410 | // what the parser could not place is an option the mode does not have: |
| 411 | // said, as pacman says it, instead of a run that quietly leaves a |
| 412 | // mistyped flag out of a transaction |
| 413 | bad := unknown.filter(it.len > 1 && it.starts_with('-')) |
| 414 | if bad.len > 0 { |
| 415 | return error('unknown option: ${bad.map('"${it}"').join(', ')}') |
| 416 | } |
| 417 | // the rest of it are targets that stand before a flag: the parser's |
| 418 | // tail takes only the ones after the last flag, and these lead them |
| 419 | early := unknown.filter(!(it.len > 1 && it.starts_with('-'))) |
| 420 | if early.len > 0 { |
| 421 | mut placed := false |
| 422 | $for field in T.fields { |
| 423 | $if field.name == 'query' { |
| 424 | mut query := early.clone() |
| 425 | query << c.query |
| 426 | c.query = query |
| 427 | placed = true |
| 428 | } |
| 429 | } |
| 430 | if !placed { |
| 431 | return error('this mode takes no targets: ${early.map('"${it}"').join(', ')}') |
| 432 | } |
| 433 | } |
| 434 | $for field in T.fields { |
| 435 | $if field.typ is bool { |
| 436 | if opt_flags[field.name] { |
| 437 | c.$(field.name) = true |
| 438 | } |
| 439 | } |
| 440 | } |
| 441 | return c |
| 442 | } |
| 443 | |
| 444 | // flag_given is whether the command line spells a flag, by its long name or |
| 445 | // its short letter: what settles that a config default steps back. a letter |
| 446 | // is looked for in any bundle of short flags, so a doubt reads as given and |
| 447 | // leaves the mode's own default |
| 448 | fn flag_given(args []string, long string, short string) bool { |
| 449 | for a in args#[2..] { |
| 450 | if a == '--${long}' || a.starts_with('--${long}=') { |
| 451 | return true |
| 452 | } |
| 453 | if a.len > 1 && a[0] == `-` && a[1] != `-` && a.contains(short) { |
| 454 | return true |
| 455 | } |
| 456 | } |
| 457 | return false |
| 458 | } |
| 459 | |
| 460 | // parse_mode reads the mode and its flags. it runs before any escalation, so |
| 461 | // a flag that does not parse is an error the user sees before a root prompt |
| 462 | fn parse_mode(args []string) !ModeConfig { |
| 463 | return match args[1] { |
| 464 | 's', 'S', 'search' { |
| 465 | // a long flag given twice is the flag parser's error, so the |
| 466 | // repeats of --unrequired are counted and folded into one here |
| 467 | level := args.count(it == '--unrequired') |
| 468 | mut once := []string{cap: args.len} |
| 469 | for i, arg in args { |
| 470 | if arg == '--unrequired' && args[..i].contains(arg) { |
| 471 | continue |
| 472 | } |
| 473 | once << arg |
| 474 | } |
| 475 | mut c := mode_flags[ConfigSearch](once)! |
| 476 | if !flag_given(once, 'thold', 't') { |
| 477 | c.thold = cfg_search_thold |
| 478 | } else if c.thold < 0.0 || c.thold > 1.0 { |
| 479 | // the config's (search #:thold) is held to the same range |
| 480 | return error('--thold must be between 0 and 1, not ${c.thold}') |
| 481 | } |
| 482 | c.unrequired_level = level |
| 483 | c.groups = c.groups || c.groups_level > 0 |
| 484 | if !c.local && !c.remote && !c.pkgbuild { |
| 485 | // the filters only work on the installed packages; the |
| 486 | // groups are the sync dbs' unless -l asks for the local ones |
| 487 | c.local = c.filtered() && !c.groups |
| 488 | c.remote = !c.filtered() || c.groups |
| 489 | } |
| 490 | ModeConfig(c) |
| 491 | } |
| 492 | 'q', 'Q', 'info' { |
| 493 | c := mode_flags[ConfigInfo](args)! |
| 494 | ModeConfig(c) |
| 495 | } |
| 496 | 'w', 'W', 'which' { |
| 497 | c := mode_flags[ConfigWhich](args)! |
| 498 | ModeConfig(c) |
| 499 | } |
| 500 | 'n', 'N', 'deptest' { |
| 501 | c := mode_flags[ConfigDeptest](args)! |
| 502 | ModeConfig(c) |
| 503 | } |
| 504 | 'm', 'M', 'mark' { |
| 505 | c := mode_flags[ConfigMark](args)! |
| 506 | ModeConfig(c) |
| 507 | } |
| 508 | 'b', 'B', 'build' { |
| 509 | c := mode_flags[ConfigBuild](args)! |
| 510 | ModeConfig(c) |
| 511 | } |
| 512 | 'i', 'I', 'install' { |
| 513 | c := mode_flags[ConfigInstall](args)! |
| 514 | ModeConfig(c) |
| 515 | } |
| 516 | 'r', 'R', 'remove' { |
| 517 | c := mode_flags[ConfigRemove](args)! |
| 518 | ModeConfig(c) |
| 519 | } |
| 520 | 'u', 'U', 'upgrade' { |
| 521 | c := mode_flags[ConfigUpgrade](args)! |
| 522 | ModeConfig(c) |
| 523 | } |
| 524 | 'l', 'L', 'files' { |
| 525 | c := mode_flags[ConfigFiles](args)! |
| 526 | ModeConfig(c) |
| 527 | } |
| 528 | 'o', 'O', 'owner' { |
| 529 | c := mode_flags[ConfigOwner](args)! |
| 530 | ModeConfig(c) |
| 531 | } |
| 532 | 'd', 'D', 'orphans' { |
| 533 | c := mode_flags[ConfigOrphans](args)! |
| 534 | ModeConfig(c) |
| 535 | } |
| 536 | 'c', 'C', 'clean' { |
| 537 | mut c := mode_flags[ConfigClean](args)! |
| 538 | if !flag_given(args, 'keep', 'k') { |
| 539 | c.keep = cfg_clean_keep |
| 540 | } |
| 541 | ModeConfig(c) |
| 542 | } |
| 543 | 't', 'T', 'tree' { |
| 544 | mut c := mode_flags[ConfigTree](args)! |
| 545 | if !flag_given(args, 'depth', 'd') { |
| 546 | c.depth = cfg_tree_depth |
| 547 | } |
| 548 | ModeConfig(c) |
| 549 | } |
| 550 | 'a', 'A', 'autoremove' { |
| 551 | c := mode_flags[ConfigAutoRemove](args)! |
| 552 | ModeConfig(c) |
| 553 | } |
| 554 | 'k', 'K', 'rank' { |
| 555 | mut c := mode_flags[ConfigRank](args)! |
| 556 | if !flag_given(args, 'top', 'n') { |
| 557 | c.top = rank_top |
| 558 | } else if c.top < 1 { |
| 559 | // the config's (rank #:top) is held to the same floor |
| 560 | return error('--top must be at least 1, not ${c.top}') |
| 561 | } |
| 562 | ModeConfig(c) |
| 563 | } |
| 564 | 'v', 'V', 'verify' { |
| 565 | c := mode_flags[ConfigVerify](args)! |
| 566 | ModeConfig(c) |
| 567 | } |
| 568 | 'f', 'F', 'find' { |
| 569 | c := mode_flags[ConfigFind](args)! |
| 570 | ModeConfig(c) |
| 571 | } |
| 572 | else { |
| 573 | error('unknown subcommand: "${args[1]}"') |
| 574 | } |
| 575 | } |
| 576 | } |
| 577 | |
| 578 | // needs_root is whether a mode runs as root: the ones that change the |
| 579 | // system, except a --print preview, which changes nothing and stays the |
| 580 | // user's as pacman's --print does; find -y, which writes the file dbs; and |
| 581 | // rank when the file it writes is not the user's to write. rank -w writes |
| 582 | // the config's mirror file under /etc, so that is known before any ranking |
| 583 | fn needs_root(c ModeConfig) !bool { |
| 584 | return match c { |
| 585 | ConfigInstall { !c.print_only } |
| 586 | ConfigRemove { !c.print_only } |
| 587 | ConfigUpgrade { !c.print_only } |
| 588 | ConfigClean { !c.print_only } |
| 589 | ConfigBuild, ConfigMark, ConfigAutoRemove { true } |
| 590 | ConfigFind { c.refresh } |
| 591 | ConfigRank { |
| 592 | mut target := c.output |
| 593 | if target == '' && c.write { |
| 594 | target = rank_write_target(rank_os_name(c.query)!) |
| 595 | } |
| 596 | target != '' && !can_write(target) |
| 597 | } |
| 598 | else { false } |
| 599 | } |
| 600 | } |
| 601 | |
| 602 | // run_mode runs a parsed mode and returns its exit status: 0, or what the |
| 603 | // mode's outcome asks for |
| 604 | fn run_mode(c ModeConfig) !int { |
| 605 | match c { |
| 606 | ConfigSearch { |
| 607 | search(c)! |
| 608 | } |
| 609 | ConfigInfo { |
| 610 | info(c)! |
| 611 | } |
| 612 | ConfigWhich { |
| 613 | which(c)! |
| 614 | } |
| 615 | ConfigDeptest { |
| 616 | // pacman's -T: 127 tells makepkg a dependency is missing |
| 617 | if deptest(c)!.len > 0 { |
| 618 | return 127 |
| 619 | } |
| 620 | } |
| 621 | ConfigMark { |
| 622 | mark(c)! |
| 623 | } |
| 624 | ConfigBuild { |
| 625 | build_repo(c)! |
| 626 | } |
| 627 | ConfigInstall { |
| 628 | install(c)! |
| 629 | } |
| 630 | ConfigRemove { |
| 631 | remove(c)! |
| 632 | } |
| 633 | ConfigUpgrade { |
| 634 | upgrade(c)! |
| 635 | } |
| 636 | ConfigFiles { |
| 637 | files(c)! |
| 638 | } |
| 639 | ConfigOwner { |
| 640 | owner(c)! |
| 641 | } |
| 642 | ConfigOrphans { |
| 643 | orphans(c)! |
| 644 | } |
| 645 | ConfigClean { |
| 646 | clean(c)! |
| 647 | } |
| 648 | ConfigTree { |
| 649 | tree(c)! |
| 650 | } |
| 651 | ConfigAutoRemove { |
| 652 | autoremove(c)! |
| 653 | } |
| 654 | ConfigRank { |
| 655 | rank(c)! |
| 656 | } |
| 657 | ConfigVerify { |
| 658 | if verify(c)! > 0 { |
| 659 | return 1 |
| 660 | } |
| 661 | } |
| 662 | ConfigFind { |
| 663 | find(c)! |
| 664 | } |
| 665 | } |
| 666 | return 0 |
| 667 | } |
| 668 | |
| 669 | fn main() { |
| 670 | if os.args.len > 1 && os.args[1] == '--makepkg-query' { |
| 671 | code := makepkg_query(os.args[2..]) or { print_err(err.msg()) } |
| 672 | exit(code) |
| 673 | } |
| 674 | if os.args.len > 1 && os.args[1] == '--package-hook' { |
| 675 | vmakepkg.package_hook(os.args[2..]) or { print_err(err.msg()) } |
| 676 | return |
| 677 | } |
| 678 | if os.args.len > 1 && os.args[1] == '--native-build' { |
| 679 | if os.args.len !in [4, 5] || (os.args.len == 5 && os.args[4] != '--sign') { |
| 680 | print_err('native build expects a recipe, an output list and optional --sign') |
| 681 | } |
| 682 | archives := vmakepkg.native_build(os.args[2], os.args.len == 5) or { print_err(err.msg()) } |
| 683 | os.write_file(os.args[3], archives.join('\n') + '\n') or { print_err(err.msg()) } |
| 684 | return |
| 685 | } |
| 686 | // recipe workers start without loading the system config. |
| 687 | if os.args.len > 3 && os.args[1] == '--recipe-run' { |
| 688 | vmakepkg.recipe_run_phase(os.args[2], os.args[3], os.args[4..]) or { |
| 689 | eprintln(error_text('recipe: ${err.msg()}')) |
| 690 | exit(1) |
| 691 | } |
| 692 | exit(0) |
| 693 | } |
| 694 | chosen, evals, args := scan_globals(os.args) or { print_err(err.msg()) } |
| 695 | // the config comes first: it is required, and everything downstream reads |
| 696 | // it, including the escalation to root |
| 697 | load_config(chosen, evals) or { print_err(err.msg()) } |
| 698 | // the config's color is the default a --color on the command line beats |
| 699 | if !color_given && opt_color != '' { |
| 700 | color_flag = opt_color |
| 701 | } |
| 702 | // a killed run lets go of the db lock it holds (on_interrupt) |
| 703 | install_interrupt_handlers() |
| 704 | // no mode, or help: the overview. an option where the mode belongs is |
| 705 | // one no global scan took, and an error as pacman's unknown options are |
| 706 | if args.len < 2 || args[1] in ['h', 'help', '-h', '--help'] { |
| 707 | if ipc_on() { |
| 708 | ipc_start() |
| 709 | } |
| 710 | show_help(help_text()) |
| 711 | end_run(0) |
| 712 | } |
| 713 | if args[1].starts_with('-') { |
| 714 | print_err('unknown option "${args[1]}" before the mode (vpac --help lists the modes)') |
| 715 | } |
| 716 | // the mode is parsed before anything else happens: a bad flag is an error |
| 717 | // here, not a panic in a root process. 'vpac <mode> -h' prints that |
| 718 | // mode's options and never runs the command; the flag parser reads the |
| 719 | // -h, so one bundled with others (-lh) counts and one that is another |
| 720 | // flag's value does not. a help request beside flags that do not parse |
| 721 | // is still answered with the help |
| 722 | config := parse_mode(args) or { |
| 723 | if args[2..].any(it == '-h' || it == '--help') { |
| 724 | mode_help(args[1]) |
| 725 | } |
| 726 | print_err(err.msg()) |
| 727 | } |
| 728 | if config.show_help { |
| 729 | mode_help(args[1]) |
| 730 | } |
| 731 | // a mode that changes the system runs as root, through the root |
| 732 | // provider; the original command line is passed, globals included, so |
| 733 | // the root process reads the same config and evals |
| 734 | root := needs_root(config) or { print_err(err.msg()) } |
| 735 | if root { |
| 736 | reexec_as_root(os.args[1..]) or { print_err(err.msg()) } |
| 737 | } |
| 738 | // this is the process that does the work: a frontend's channel opens here |
| 739 | if ipc_on() { |
| 740 | ipc_start() |
| 741 | } |
| 742 | mut status := run_mode(config) or { print_err(err.msg()) } |
| 743 | // a query that skipped bad targets still fails, after showing the rest |
| 744 | if failed_targets > 0 && status == 0 { |
| 745 | status = 1 |
| 746 | } |
| 747 | end_run(status) |
| 748 | } |
| 749 | |
| 750 | // mode_help prints one mode's options and ends the run |
| 751 | @[noreturn] |
| 752 | fn mode_help(name string) { |
| 753 | if ipc_on() { |
| 754 | ipc_start() |
| 755 | } |
| 756 | mode := find_mode(name) or { print_err('unknown subcommand: "${name}"') } |
| 757 | show_help(mode_help_text(mode)) |
| 758 | end_run(0) |
| 759 | } |
| 760 | |
| 761 | // end_run is the one exit of a run, an error's included: the status, told |
| 762 | // to a frontend first as the exit event that closes every stream |
| 763 | @[noreturn] |
| 764 | fn end_run(status int) { |
| 765 | if ipc_on() { |
| 766 | emit(ExitEvent{ |
| 767 | status: status |
| 768 | }) |
| 769 | } |
| 770 | exit(status) |
| 771 | } |
| 772 | |