parser.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850
  1. package main
  2. import (
  3. "bufio"
  4. "os"
  5. "strconv"
  6. "strings"
  7. "github.com/leonelquinteros/gotext"
  8. rpc "github.com/mikkeloscar/aur"
  9. "github.com/pkg/errors"
  10. "github.com/Jguer/yay/v10/pkg/settings"
  11. "github.com/Jguer/yay/v10/pkg/stringset"
  12. )
  13. // Parses command line arguments in a way we can interact with programmatically but
  14. // also in a way that can easily be passed to pacman later on.
  15. type arguments struct {
  16. op string
  17. options map[string]string
  18. globals map[string]string
  19. doubles stringset.StringSet // Tracks args passed twice such as -yy and -dd
  20. targets []string
  21. }
  22. func makeArguments() *arguments {
  23. return &arguments{
  24. "",
  25. make(map[string]string),
  26. make(map[string]string),
  27. make(stringset.StringSet),
  28. make([]string, 0),
  29. }
  30. }
  31. func (parser *arguments) copyGlobal() (cp *arguments) {
  32. cp = makeArguments()
  33. for k, v := range parser.globals {
  34. cp.globals[k] = v
  35. }
  36. return
  37. }
  38. func (parser *arguments) copy() (cp *arguments) {
  39. cp = makeArguments()
  40. cp.op = parser.op
  41. for k, v := range parser.options {
  42. cp.options[k] = v
  43. }
  44. for k, v := range parser.globals {
  45. cp.globals[k] = v
  46. }
  47. cp.targets = make([]string, len(parser.targets))
  48. copy(cp.targets, parser.targets)
  49. for k, v := range parser.doubles {
  50. cp.doubles[k] = v
  51. }
  52. return
  53. }
  54. func (parser *arguments) delArg(options ...string) {
  55. for _, option := range options {
  56. delete(parser.options, option)
  57. delete(parser.globals, option)
  58. delete(parser.doubles, option)
  59. }
  60. }
  61. func (parser *arguments) needRoot() bool {
  62. if parser.existsArg("h", "help") {
  63. return false
  64. }
  65. switch parser.op {
  66. case "D", "database":
  67. if parser.existsArg("k", "check") {
  68. return false
  69. }
  70. return true
  71. case "F", "files":
  72. if parser.existsArg("y", "refresh") {
  73. return true
  74. }
  75. return false
  76. case "Q", "query":
  77. if parser.existsArg("k", "check") {
  78. return true
  79. }
  80. return false
  81. case "R", "remove":
  82. if parser.existsArg("p", "print", "print-format") {
  83. return false
  84. }
  85. return true
  86. case "S", "sync":
  87. if parser.existsArg("y", "refresh") {
  88. return true
  89. }
  90. if parser.existsArg("p", "print", "print-format") {
  91. return false
  92. }
  93. if parser.existsArg("s", "search") {
  94. return false
  95. }
  96. if parser.existsArg("l", "list") {
  97. return false
  98. }
  99. if parser.existsArg("g", "groups") {
  100. return false
  101. }
  102. if parser.existsArg("i", "info") {
  103. return false
  104. }
  105. if parser.existsArg("c", "clean") && mode == modeAUR {
  106. return false
  107. }
  108. return true
  109. case "U", "upgrade":
  110. return true
  111. default:
  112. return false
  113. }
  114. }
  115. func (parser *arguments) addOP(op string) (err error) {
  116. if parser.op != "" {
  117. err = errors.New(gotext.Get("only one operation may be used at a time"))
  118. return
  119. }
  120. parser.op = op
  121. return
  122. }
  123. func (parser *arguments) addParam(option, arg string) (err error) {
  124. if !isArg(option) {
  125. return errors.New(gotext.Get("invalid option '%s'", option))
  126. }
  127. if isOp(option) {
  128. err = parser.addOP(option)
  129. return
  130. }
  131. switch {
  132. case parser.existsArg(option):
  133. parser.doubles[option] = struct{}{}
  134. case isGlobal(option):
  135. parser.globals[option] = arg
  136. default:
  137. parser.options[option] = arg
  138. }
  139. return
  140. }
  141. func (parser *arguments) addArg(options ...string) (err error) {
  142. for _, option := range options {
  143. err = parser.addParam(option, "")
  144. if err != nil {
  145. return
  146. }
  147. }
  148. return
  149. }
  150. // Multiple args acts as an OR operator
  151. func (parser *arguments) existsArg(options ...string) bool {
  152. for _, option := range options {
  153. _, exists := parser.options[option]
  154. if exists {
  155. return true
  156. }
  157. _, exists = parser.globals[option]
  158. if exists {
  159. return true
  160. }
  161. }
  162. return false
  163. }
  164. func (parser *arguments) getArg(options ...string) (arg string, double, exists bool) {
  165. existCount := 0
  166. for _, option := range options {
  167. var value string
  168. value, exists = parser.options[option]
  169. if exists {
  170. arg = value
  171. existCount++
  172. _, exists = parser.doubles[option]
  173. if exists {
  174. existCount++
  175. }
  176. }
  177. value, exists = parser.globals[option]
  178. if exists {
  179. arg = value
  180. existCount++
  181. _, exists = parser.doubles[option]
  182. if exists {
  183. existCount++
  184. }
  185. }
  186. }
  187. double = existCount >= 2
  188. exists = existCount >= 1
  189. return arg, double, exists
  190. }
  191. func (parser *arguments) addTarget(targets ...string) {
  192. parser.targets = append(parser.targets, targets...)
  193. }
  194. func (parser *arguments) clearTargets() {
  195. parser.targets = make([]string, 0)
  196. }
  197. // Multiple args acts as an OR operator
  198. func (parser *arguments) existsDouble(options ...string) bool {
  199. for _, option := range options {
  200. _, exists := parser.doubles[option]
  201. if exists {
  202. return true
  203. }
  204. }
  205. return false
  206. }
  207. func (parser *arguments) formatArgs() (args []string) {
  208. var op string
  209. if parser.op != "" {
  210. op = formatArg(parser.op)
  211. }
  212. args = append(args, op)
  213. for option, arg := range parser.options {
  214. if option == "--" {
  215. continue
  216. }
  217. formattedOption := formatArg(option)
  218. args = append(args, formattedOption)
  219. if hasParam(option) {
  220. args = append(args, arg)
  221. }
  222. if parser.existsDouble(option) {
  223. args = append(args, formattedOption)
  224. }
  225. }
  226. return
  227. }
  228. func (parser *arguments) formatGlobals() (args []string) {
  229. for option, arg := range parser.globals {
  230. formattedOption := formatArg(option)
  231. args = append(args, formattedOption)
  232. if hasParam(option) {
  233. args = append(args, arg)
  234. }
  235. if parser.existsDouble(option) {
  236. args = append(args, formattedOption)
  237. }
  238. }
  239. return
  240. }
  241. func formatArg(arg string) string {
  242. if len(arg) > 1 {
  243. arg = "--" + arg
  244. } else {
  245. arg = "-" + arg
  246. }
  247. return arg
  248. }
  249. func isArg(arg string) bool {
  250. switch arg {
  251. case "-", "--":
  252. case "ask":
  253. case "D", "database":
  254. case "Q", "query":
  255. case "R", "remove":
  256. case "S", "sync":
  257. case "T", "deptest":
  258. case "U", "upgrade":
  259. case "F", "files":
  260. case "V", "version":
  261. case "h", "help":
  262. case "Y", "yay":
  263. case "P", "show":
  264. case "G", "getpkgbuild":
  265. case "b", "dbpath":
  266. case "r", "root":
  267. case "v", "verbose":
  268. case "arch":
  269. case "cachedir":
  270. case "color":
  271. case "config":
  272. case "debug":
  273. case "gpgdir":
  274. case "hookdir":
  275. case "logfile":
  276. case "noconfirm":
  277. case "confirm":
  278. case "disable-download-timeout":
  279. case "sysroot":
  280. case "d", "nodeps":
  281. case "assume-installed":
  282. case "dbonly":
  283. case "absdir":
  284. case "noprogressbar":
  285. case "noscriptlet":
  286. case "p", "print":
  287. case "print-format":
  288. case "asdeps":
  289. case "asexplicit":
  290. case "ignore":
  291. case "ignoregroup":
  292. case "needed":
  293. case "overwrite":
  294. case "f", "force":
  295. case "c", "changelog":
  296. case "deps":
  297. case "e", "explicit":
  298. case "g", "groups":
  299. case "i", "info":
  300. case "k", "check":
  301. case "l", "list":
  302. case "m", "foreign":
  303. case "n", "native":
  304. case "o", "owns":
  305. case "file":
  306. case "q", "quiet":
  307. case "s", "search":
  308. case "t", "unrequired":
  309. case "u", "upgrades":
  310. case "cascade":
  311. case "nosave":
  312. case "recursive":
  313. case "unneeded":
  314. case "clean":
  315. case "sysupgrade":
  316. case "w", "downloadonly":
  317. case "y", "refresh":
  318. case "x", "regex":
  319. case "machinereadable":
  320. // yay options
  321. case "aururl":
  322. case "save":
  323. case "afterclean", "cleanafter":
  324. case "noafterclean", "nocleanafter":
  325. case "devel":
  326. case "nodevel":
  327. case "timeupdate":
  328. case "notimeupdate":
  329. case "topdown":
  330. case "bottomup":
  331. case "completioninterval":
  332. case "sortby":
  333. case "searchby":
  334. case "redownload":
  335. case "redownloadall":
  336. case "noredownload":
  337. case "rebuild":
  338. case "rebuildall":
  339. case "rebuildtree":
  340. case "norebuild":
  341. case "batchinstall":
  342. case "nobatchinstall":
  343. case "answerclean":
  344. case "noanswerclean":
  345. case "answerdiff":
  346. case "noanswerdiff":
  347. case "answeredit":
  348. case "noansweredit":
  349. case "answerupgrade":
  350. case "noanswerupgrade":
  351. case "gpgflags":
  352. case "mflags":
  353. case "gitflags":
  354. case "builddir":
  355. case "editor":
  356. case "editorflags":
  357. case "makepkg":
  358. case "makepkgconf":
  359. case "nomakepkgconf":
  360. case "pacman":
  361. case "git":
  362. case "gpg":
  363. case "sudo":
  364. case "sudoflags":
  365. case "requestsplitn":
  366. case "sudoloop":
  367. case "nosudoloop":
  368. case "provides":
  369. case "noprovides":
  370. case "pgpfetch":
  371. case "nopgpfetch":
  372. case "upgrademenu":
  373. case "noupgrademenu":
  374. case "cleanmenu":
  375. case "nocleanmenu":
  376. case "diffmenu":
  377. case "nodiffmenu":
  378. case "editmenu":
  379. case "noeditmenu":
  380. case "useask":
  381. case "nouseask":
  382. case "combinedupgrade":
  383. case "nocombinedupgrade":
  384. case "a", "aur":
  385. case "repo":
  386. case "removemake":
  387. case "noremovemake":
  388. case "askremovemake":
  389. case "complete":
  390. case "stats":
  391. case "news":
  392. case "gendb":
  393. case "currentconfig":
  394. default:
  395. return false
  396. }
  397. return true
  398. }
  399. func handleConfig(option, value string) bool {
  400. switch option {
  401. case "aururl":
  402. config.AURURL = value
  403. case "save":
  404. shouldSaveConfig = true
  405. case "afterclean", "cleanafter":
  406. config.CleanAfter = true
  407. case "noafterclean", "nocleanafter":
  408. config.CleanAfter = false
  409. case "devel":
  410. config.Devel = true
  411. case "nodevel":
  412. config.Devel = false
  413. case "timeupdate":
  414. config.TimeUpdate = true
  415. case "notimeupdate":
  416. config.TimeUpdate = false
  417. case "topdown":
  418. config.SortMode = settings.TopDown
  419. case "bottomup":
  420. config.SortMode = settings.BottomUp
  421. case "completioninterval":
  422. n, err := strconv.Atoi(value)
  423. if err == nil {
  424. config.CompletionInterval = n
  425. }
  426. case "sortby":
  427. config.SortBy = value
  428. case "searchby":
  429. config.SearchBy = value
  430. case "noconfirm":
  431. config.NoConfirm = true
  432. case "config":
  433. config.PacmanConf = value
  434. case "redownload":
  435. config.ReDownload = "yes"
  436. case "redownloadall":
  437. config.ReDownload = "all"
  438. case "noredownload":
  439. config.ReDownload = "no"
  440. case "rebuild":
  441. config.ReBuild = "yes"
  442. case "rebuildall":
  443. config.ReBuild = "all"
  444. case "rebuildtree":
  445. config.ReBuild = "tree"
  446. case "norebuild":
  447. config.ReBuild = "no"
  448. case "batchinstall":
  449. config.BatchInstall = true
  450. case "nobatchinstall":
  451. config.BatchInstall = false
  452. case "answerclean":
  453. config.AnswerClean = value
  454. case "noanswerclean":
  455. config.AnswerClean = ""
  456. case "answerdiff":
  457. config.AnswerDiff = value
  458. case "noanswerdiff":
  459. config.AnswerDiff = ""
  460. case "answeredit":
  461. config.AnswerEdit = value
  462. case "noansweredit":
  463. config.AnswerEdit = ""
  464. case "answerupgrade":
  465. config.AnswerUpgrade = value
  466. case "noanswerupgrade":
  467. config.AnswerUpgrade = ""
  468. case "gpgflags":
  469. config.GpgFlags = value
  470. case "mflags":
  471. config.MFlags = value
  472. case "gitflags":
  473. config.GitFlags = value
  474. case "builddir":
  475. config.BuildDir = value
  476. case "absdir":
  477. config.ABSDir = value
  478. case "editor":
  479. config.Editor = value
  480. case "editorflags":
  481. config.EditorFlags = value
  482. case "makepkg":
  483. config.MakepkgBin = value
  484. case "makepkgconf":
  485. config.MakepkgConf = value
  486. case "nomakepkgconf":
  487. config.MakepkgConf = ""
  488. case "pacman":
  489. config.PacmanBin = value
  490. case "git":
  491. config.GitBin = value
  492. case "gpg":
  493. config.GpgBin = value
  494. case "sudo":
  495. config.SudoBin = value
  496. case "sudoflags":
  497. config.SudoFlags = value
  498. case "requestsplitn":
  499. n, err := strconv.Atoi(value)
  500. if err == nil && n > 0 {
  501. config.RequestSplitN = n
  502. }
  503. case "sudoloop":
  504. config.SudoLoop = true
  505. case "nosudoloop":
  506. config.SudoLoop = false
  507. case "provides":
  508. config.Provides = true
  509. case "noprovides":
  510. config.Provides = false
  511. case "pgpfetch":
  512. config.PGPFetch = true
  513. case "nopgpfetch":
  514. config.PGPFetch = false
  515. case "upgrademenu":
  516. config.UpgradeMenu = true
  517. case "noupgrademenu":
  518. config.UpgradeMenu = false
  519. case "cleanmenu":
  520. config.CleanMenu = true
  521. case "nocleanmenu":
  522. config.CleanMenu = false
  523. case "diffmenu":
  524. config.DiffMenu = true
  525. case "nodiffmenu":
  526. config.DiffMenu = false
  527. case "editmenu":
  528. config.EditMenu = true
  529. case "noeditmenu":
  530. config.EditMenu = false
  531. case "useask":
  532. config.UseAsk = true
  533. case "nouseask":
  534. config.UseAsk = false
  535. case "combinedupgrade":
  536. config.CombinedUpgrade = true
  537. case "nocombinedupgrade":
  538. config.CombinedUpgrade = false
  539. case "a", "aur":
  540. mode = modeAUR
  541. case "repo":
  542. mode = modeRepo
  543. case "removemake":
  544. config.RemoveMake = "yes"
  545. case "noremovemake":
  546. config.RemoveMake = "no"
  547. case "askremovemake":
  548. config.RemoveMake = "ask"
  549. default:
  550. return false
  551. }
  552. return true
  553. }
  554. func isOp(op string) bool {
  555. switch op {
  556. case "V", "version":
  557. case "D", "database":
  558. case "F", "files":
  559. case "Q", "query":
  560. case "R", "remove":
  561. case "S", "sync":
  562. case "T", "deptest":
  563. case "U", "upgrade":
  564. // yay specific
  565. case "Y", "yay":
  566. case "P", "show":
  567. case "G", "getpkgbuild":
  568. default:
  569. return false
  570. }
  571. return true
  572. }
  573. func isGlobal(op string) bool {
  574. switch op {
  575. case "b", "dbpath":
  576. case "r", "root":
  577. case "v", "verbose":
  578. case "arch":
  579. case "cachedir":
  580. case "color":
  581. case "config":
  582. case "debug":
  583. case "gpgdir":
  584. case "hookdir":
  585. case "logfile":
  586. case "noconfirm":
  587. case "confirm":
  588. default:
  589. return false
  590. }
  591. return true
  592. }
  593. func hasParam(arg string) bool {
  594. switch arg {
  595. case "dbpath", "b":
  596. case "root", "r":
  597. case "sysroot":
  598. case "config":
  599. case "ignore":
  600. case "assume-installed":
  601. case "overwrite":
  602. case "ask":
  603. case "cachedir":
  604. case "hookdir":
  605. case "logfile":
  606. case "ignoregroup":
  607. case "arch":
  608. case "print-format":
  609. case "gpgdir":
  610. case "color":
  611. // yay params
  612. case "aururl":
  613. case "mflags":
  614. case "gpgflags":
  615. case "gitflags":
  616. case "builddir":
  617. case "absdir":
  618. case "editor":
  619. case "editorflags":
  620. case "makepkg":
  621. case "makepkgconf":
  622. case "pacman":
  623. case "git":
  624. case "gpg":
  625. case "sudo":
  626. case "sudoflags":
  627. case "requestsplitn":
  628. case "answerclean":
  629. case "answerdiff":
  630. case "answeredit":
  631. case "answerupgrade":
  632. case "completioninterval":
  633. case "sortby":
  634. case "searchby":
  635. default:
  636. return false
  637. }
  638. return true
  639. }
  640. // Parses short hand options such as:
  641. // -Syu -b/some/path -
  642. func (parser *arguments) parseShortOption(arg, param string) (usedNext bool, err error) {
  643. if arg == "-" {
  644. err = parser.addArg("-")
  645. return
  646. }
  647. arg = arg[1:]
  648. for k, _char := range arg {
  649. char := string(_char)
  650. if hasParam(char) {
  651. if k < len(arg)-1 {
  652. err = parser.addParam(char, arg[k+1:])
  653. } else {
  654. usedNext = true
  655. err = parser.addParam(char, param)
  656. }
  657. break
  658. } else {
  659. err = parser.addArg(char)
  660. if err != nil {
  661. return
  662. }
  663. }
  664. }
  665. return
  666. }
  667. // Parses full length options such as:
  668. // --sync --refresh --sysupgrade --dbpath /some/path --
  669. func (parser *arguments) parseLongOption(arg, param string) (usedNext bool, err error) {
  670. if arg == "--" {
  671. err = parser.addArg(arg)
  672. return
  673. }
  674. arg = arg[2:]
  675. switch split := strings.SplitN(arg, "=", 2); {
  676. case len(split) == 2:
  677. err = parser.addParam(split[0], split[1])
  678. case hasParam(arg):
  679. err = parser.addParam(arg, param)
  680. usedNext = true
  681. default:
  682. err = parser.addArg(arg)
  683. }
  684. return
  685. }
  686. func (parser *arguments) parseStdin() error {
  687. scanner := bufio.NewScanner(os.Stdin)
  688. scanner.Split(bufio.ScanLines)
  689. for scanner.Scan() {
  690. parser.addTarget(scanner.Text())
  691. }
  692. return os.Stdin.Close()
  693. }
  694. func (parser *arguments) parseCommandLine() error {
  695. args := os.Args[1:]
  696. usedNext := false
  697. if len(args) < 1 {
  698. if _, err := parser.parseShortOption("-Syu", ""); err != nil {
  699. return err
  700. }
  701. } else {
  702. for k, arg := range args {
  703. var nextArg string
  704. if usedNext {
  705. usedNext = false
  706. continue
  707. }
  708. if k+1 < len(args) {
  709. nextArg = args[k+1]
  710. }
  711. var err error
  712. switch {
  713. case parser.existsArg("--"):
  714. parser.addTarget(arg)
  715. case strings.HasPrefix(arg, "--"):
  716. usedNext, err = parser.parseLongOption(arg, nextArg)
  717. case strings.HasPrefix(arg, "-"):
  718. usedNext, err = parser.parseShortOption(arg, nextArg)
  719. default:
  720. parser.addTarget(arg)
  721. }
  722. if err != nil {
  723. return err
  724. }
  725. }
  726. }
  727. if parser.op == "" {
  728. parser.op = "Y"
  729. }
  730. if parser.existsArg("-") {
  731. if err := parser.parseStdin(); err != nil {
  732. return err
  733. }
  734. parser.delArg("-")
  735. file, err := os.Open("/dev/tty")
  736. if err != nil {
  737. return err
  738. }
  739. os.Stdin = file
  740. }
  741. cmdArgs.extractYayOptions()
  742. return nil
  743. }
  744. func (parser *arguments) extractYayOptions() {
  745. for option, value := range parser.options {
  746. if handleConfig(option, value) {
  747. parser.delArg(option)
  748. }
  749. }
  750. for option, value := range parser.globals {
  751. if handleConfig(option, value) {
  752. parser.delArg(option)
  753. }
  754. }
  755. rpc.AURURL = strings.TrimRight(config.AURURL, "/") + "/rpc.php?"
  756. config.AURURL = strings.TrimRight(config.AURURL, "/")
  757. }