parser.go 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011
  1. package main
  2. import (
  3. "bufio"
  4. "bytes"
  5. "fmt"
  6. "html"
  7. "os"
  8. "strconv"
  9. "strings"
  10. "unicode"
  11. rpc "github.com/mikkeloscar/aur"
  12. )
  13. // A basic set implementation for strings.
  14. // This is used a lot so it deserves its own type.
  15. // Other types of sets are used throughout the code but do not have
  16. // their own typedef.
  17. // String sets and <type>sets should be used throughout the code when applicable,
  18. // they are a lot more flexible than slices and provide easy lookup.
  19. type stringSet map[string]struct{}
  20. func (set stringSet) set(v string) {
  21. set[v] = struct{}{}
  22. }
  23. func (set stringSet) get(v string) bool {
  24. _, exists := set[v]
  25. return exists
  26. }
  27. func (set stringSet) remove(v string) {
  28. delete(set, v)
  29. }
  30. func (set stringSet) toSlice() []string {
  31. slice := make([]string, 0, len(set))
  32. for v := range set {
  33. slice = append(slice, v)
  34. }
  35. return slice
  36. }
  37. func (set stringSet) copy() stringSet {
  38. newSet := make(stringSet)
  39. for str := range set {
  40. newSet.set(str)
  41. }
  42. return newSet
  43. }
  44. func sliceToStringSet(in []string) stringSet {
  45. set := make(stringSet)
  46. for _, v := range in {
  47. set.set(v)
  48. }
  49. return set
  50. }
  51. func makeStringSet(in ...string) stringSet {
  52. return sliceToStringSet(in)
  53. }
  54. // Parses command line arguments in a way we can interact with programmatically but
  55. // also in a way that can easily be passed to pacman later on.
  56. type arguments struct {
  57. op string
  58. options map[string]string
  59. globals map[string]string
  60. doubles stringSet // Tracks args passed twice such as -yy and -dd
  61. targets []string
  62. }
  63. func makeArguments() *arguments {
  64. return &arguments{
  65. "",
  66. make(map[string]string),
  67. make(map[string]string),
  68. make(stringSet),
  69. make([]string, 0),
  70. }
  71. }
  72. func (parser *arguments) copy() (cp *arguments) {
  73. cp = makeArguments()
  74. cp.op = parser.op
  75. for k, v := range parser.options {
  76. cp.options[k] = v
  77. }
  78. for k, v := range parser.globals {
  79. cp.globals[k] = v
  80. }
  81. cp.targets = make([]string, len(parser.targets))
  82. copy(cp.targets, parser.targets)
  83. for k, v := range parser.doubles {
  84. cp.doubles[k] = v
  85. }
  86. return
  87. }
  88. func (parser *arguments) delArg(options ...string) {
  89. for _, option := range options {
  90. delete(parser.options, option)
  91. delete(parser.globals, option)
  92. delete(parser.doubles, option)
  93. }
  94. }
  95. func (parser *arguments) needRoot() bool {
  96. if parser.existsArg("h", "help") {
  97. return false
  98. }
  99. switch parser.op {
  100. case "D", "database":
  101. if parser.existsArg("k", "check") {
  102. return false
  103. }
  104. return true
  105. case "F", "files":
  106. if parser.existsArg("y", "refresh") {
  107. return true
  108. }
  109. return false
  110. case "Q", "query":
  111. if parser.existsArg("k", "check") {
  112. return true
  113. }
  114. return false
  115. case "R", "remove":
  116. return true
  117. case "S", "sync":
  118. if parser.existsArg("y", "refresh") {
  119. return true
  120. }
  121. if parser.existsArg("p", "print", "print-format") {
  122. return false
  123. }
  124. if parser.existsArg("s", "search") {
  125. return false
  126. }
  127. if parser.existsArg("l", "list") {
  128. return false
  129. }
  130. if parser.existsArg("g", "groups") {
  131. return false
  132. }
  133. if parser.existsArg("i", "info") {
  134. return false
  135. }
  136. if parser.existsArg("c", "clean") && mode == modeAUR {
  137. return false
  138. }
  139. return true
  140. case "U", "upgrade":
  141. return true
  142. default:
  143. return false
  144. }
  145. }
  146. func (parser *arguments) addOP(op string) (err error) {
  147. if parser.op != "" {
  148. err = fmt.Errorf("only one operation may be used at a time")
  149. return
  150. }
  151. parser.op = op
  152. return
  153. }
  154. func (parser *arguments) addParam(option string, arg string) (err error) {
  155. if !isArg(option) {
  156. return fmt.Errorf("invalid option '%s'", option)
  157. }
  158. if isOp(option) {
  159. err = parser.addOP(option)
  160. return
  161. }
  162. switch {
  163. case parser.existsArg(option):
  164. parser.doubles[option] = struct{}{}
  165. case isGlobal(option):
  166. parser.globals[option] = arg
  167. default:
  168. parser.options[option] = arg
  169. }
  170. return
  171. }
  172. func (parser *arguments) addArg(options ...string) (err error) {
  173. for _, option := range options {
  174. err = parser.addParam(option, "")
  175. if err != nil {
  176. return
  177. }
  178. }
  179. return
  180. }
  181. // Multiple args acts as an OR operator
  182. func (parser *arguments) existsArg(options ...string) bool {
  183. for _, option := range options {
  184. _, exists := parser.options[option]
  185. if exists {
  186. return true
  187. }
  188. _, exists = parser.globals[option]
  189. if exists {
  190. return true
  191. }
  192. }
  193. return false
  194. }
  195. func (parser *arguments) getArg(options ...string) (arg string, double bool, exists bool) {
  196. existCount := 0
  197. for _, option := range options {
  198. var value string
  199. value, exists = parser.options[option]
  200. if exists {
  201. arg = value
  202. existCount++
  203. _, exists = parser.doubles[option]
  204. if exists {
  205. existCount++
  206. }
  207. }
  208. value, exists = parser.globals[option]
  209. if exists {
  210. arg = value
  211. existCount++
  212. _, exists = parser.doubles[option]
  213. if exists {
  214. existCount++
  215. }
  216. }
  217. }
  218. double = existCount >= 2
  219. exists = existCount >= 1
  220. return
  221. }
  222. func (parser *arguments) addTarget(targets ...string) {
  223. parser.targets = append(parser.targets, targets...)
  224. }
  225. func (parser *arguments) clearTargets() {
  226. parser.targets = make([]string, 0)
  227. }
  228. // Multiple args acts as an OR operator
  229. func (parser *arguments) existsDouble(options ...string) bool {
  230. for _, option := range options {
  231. _, exists := parser.doubles[option]
  232. if exists {
  233. return true
  234. }
  235. }
  236. return false
  237. }
  238. func (parser *arguments) formatArgs() (args []string) {
  239. var op string
  240. if parser.op != "" {
  241. op = formatArg(parser.op)
  242. }
  243. args = append(args, op)
  244. for option, arg := range parser.options {
  245. if option == "--" {
  246. continue
  247. }
  248. formattedOption := formatArg(option)
  249. args = append(args, formattedOption)
  250. if hasParam(option) {
  251. args = append(args, arg)
  252. }
  253. if parser.existsDouble(option) {
  254. args = append(args, formattedOption)
  255. }
  256. }
  257. return
  258. }
  259. func (parser *arguments) formatGlobals() (args []string) {
  260. for option, arg := range parser.globals {
  261. formattedOption := formatArg(option)
  262. args = append(args, formattedOption)
  263. if hasParam(option) {
  264. args = append(args, arg)
  265. }
  266. if parser.existsDouble(option) {
  267. args = append(args, formattedOption)
  268. }
  269. }
  270. return
  271. }
  272. func formatArg(arg string) string {
  273. if len(arg) > 1 {
  274. arg = "--" + arg
  275. } else {
  276. arg = "-" + arg
  277. }
  278. return arg
  279. }
  280. func isArg(arg string) bool {
  281. switch arg {
  282. case "-", "--":
  283. case "ask":
  284. case "D", "database":
  285. case "Q", "query":
  286. case "R", "remove":
  287. case "S", "sync":
  288. case "T", "deptest":
  289. case "U", "upgrade":
  290. case "F", "files":
  291. case "V", "version":
  292. case "h", "help":
  293. case "Y", "yay":
  294. case "P", "show":
  295. case "G", "getpkgbuild":
  296. case "b", "dbpath":
  297. case "r", "root":
  298. case "v", "verbose":
  299. case "arch":
  300. case "cachedir":
  301. case "color":
  302. case "config":
  303. case "debug":
  304. case "gpgdir":
  305. case "hookdir":
  306. case "logfile":
  307. case "noconfirm":
  308. case "confirm":
  309. case "disable-download-timeout":
  310. case "sysroot":
  311. case "d", "nodeps":
  312. case "assume-installed":
  313. case "dbonly":
  314. case "noprogressbar":
  315. case "noscriptlet":
  316. case "p", "print":
  317. case "print-format":
  318. case "asdeps":
  319. case "asexplicit":
  320. case "ignore":
  321. case "ignoregroup":
  322. case "needed":
  323. case "overwrite":
  324. case "f", "force":
  325. case "c", "changelog":
  326. case "deps":
  327. case "e", "explicit":
  328. case "g", "groups":
  329. case "i", "info":
  330. case "k", "check":
  331. case "l", "list":
  332. case "m", "foreign":
  333. case "n", "native":
  334. case "o", "owns":
  335. case "file":
  336. case "q", "quiet":
  337. case "s", "search":
  338. case "t", "unrequired":
  339. case "u", "upgrades":
  340. case "cascade":
  341. case "nosave":
  342. case "recursive":
  343. case "unneeded":
  344. case "clean":
  345. case "sysupgrade":
  346. case "w", "downloadonly":
  347. case "y", "refresh":
  348. case "x", "regex":
  349. case "machinereadable":
  350. //yay options
  351. case "aururl":
  352. case "save":
  353. case "afterclean", "cleanafter":
  354. case "noafterclean", "nocleanafter":
  355. case "devel":
  356. case "nodevel":
  357. case "timeupdate":
  358. case "notimeupdate":
  359. case "topdown":
  360. case "bottomup":
  361. case "completioninterval":
  362. case "sortby":
  363. case "redownload":
  364. case "redownloadall":
  365. case "noredownload":
  366. case "rebuild":
  367. case "rebuildall":
  368. case "rebuildtree":
  369. case "norebuild":
  370. case "answerclean":
  371. case "noanswerclean":
  372. case "answerdiff":
  373. case "noanswerdiff":
  374. case "answeredit":
  375. case "noansweredit":
  376. case "answerupgrade":
  377. case "noanswerupgrade":
  378. case "gitclone":
  379. case "nogitclone":
  380. case "gpgflags":
  381. case "mflags":
  382. case "gitflags":
  383. case "builddir":
  384. case "editor":
  385. case "editorflags":
  386. case "makepkg":
  387. case "makepkgconf":
  388. case "nomakepkgconf":
  389. case "pacman":
  390. case "tar":
  391. case "git":
  392. case "gpg":
  393. case "requestsplitn":
  394. case "sudoloop":
  395. case "nosudoloop":
  396. case "provides":
  397. case "noprovides":
  398. case "pgpfetch":
  399. case "nopgpfetch":
  400. case "upgrademenu":
  401. case "noupgrademenu":
  402. case "cleanmenu":
  403. case "nocleanmenu":
  404. case "diffmenu":
  405. case "nodiffmenu":
  406. case "editmenu":
  407. case "noeditmenu":
  408. case "useask":
  409. case "nouseask":
  410. case "combinedupgrade":
  411. case "nocombinedupgrade":
  412. case "a", "aur":
  413. case "repo":
  414. case "removemake":
  415. case "noremovemake":
  416. case "askremovemake":
  417. case "complete":
  418. case "stats":
  419. case "news":
  420. case "gendb":
  421. case "currentconfig":
  422. default:
  423. return false
  424. }
  425. return true
  426. }
  427. func handleConfig(option, value string) bool {
  428. switch option {
  429. case "aururl":
  430. config.AURURL = value
  431. case "save":
  432. shouldSaveConfig = true
  433. case "afterclean", "cleanafter":
  434. config.CleanAfter = true
  435. case "noafterclean", "nocleanafter":
  436. config.CleanAfter = false
  437. case "devel":
  438. config.Devel = true
  439. case "nodevel":
  440. config.Devel = false
  441. case "timeupdate":
  442. config.TimeUpdate = true
  443. case "notimeupdate":
  444. config.TimeUpdate = false
  445. case "topdown":
  446. config.SortMode = topDown
  447. case "bottomup":
  448. config.SortMode = bottomUp
  449. case "completioninterval":
  450. n, err := strconv.Atoi(value)
  451. if err == nil {
  452. config.CompletionInterval = n
  453. }
  454. case "sortby":
  455. config.SortBy = value
  456. case "noconfirm":
  457. config.NoConfirm = true
  458. case "config":
  459. config.PacmanConf = value
  460. case "redownload":
  461. config.ReDownload = "yes"
  462. case "redownloadall":
  463. config.ReDownload = "all"
  464. case "noredownload":
  465. config.ReDownload = "no"
  466. case "rebuild":
  467. config.ReBuild = "yes"
  468. case "rebuildall":
  469. config.ReBuild = "all"
  470. case "rebuildtree":
  471. config.ReBuild = "tree"
  472. case "norebuild":
  473. config.ReBuild = "no"
  474. case "answerclean":
  475. config.AnswerClean = value
  476. case "noanswerclean":
  477. config.AnswerClean = ""
  478. case "answerdiff":
  479. config.AnswerDiff = value
  480. case "noanswerdiff":
  481. config.AnswerDiff = ""
  482. case "answeredit":
  483. config.AnswerEdit = value
  484. case "noansweredit":
  485. config.AnswerEdit = ""
  486. case "answerupgrade":
  487. config.AnswerUpgrade = value
  488. case "noanswerupgrade":
  489. config.AnswerUpgrade = ""
  490. case "gitclone":
  491. config.GitClone = true
  492. case "nogitclone":
  493. config.GitClone = false
  494. case "gpgflags":
  495. config.GpgFlags = value
  496. case "mflags":
  497. config.MFlags = value
  498. case "gitflags":
  499. config.GitFlags = value
  500. case "builddir":
  501. config.BuildDir = value
  502. case "editor":
  503. config.Editor = value
  504. case "editorflags":
  505. config.EditorFlags = value
  506. case "makepkg":
  507. config.MakepkgBin = value
  508. case "makepkgconf":
  509. config.MakepkgConf = value
  510. case "nomakepkgconf":
  511. config.MakepkgConf = ""
  512. case "pacman":
  513. config.PacmanBin = value
  514. case "tar":
  515. config.TarBin = value
  516. case "git":
  517. config.GitBin = value
  518. case "gpg":
  519. config.GpgBin = value
  520. case "requestsplitn":
  521. n, err := strconv.Atoi(value)
  522. if err == nil && n > 0 {
  523. config.RequestSplitN = n
  524. }
  525. case "sudoloop":
  526. config.SudoLoop = true
  527. case "nosudoloop":
  528. config.SudoLoop = false
  529. case "provides":
  530. config.Provides = true
  531. case "noprovides":
  532. config.Provides = false
  533. case "pgpfetch":
  534. config.PGPFetch = true
  535. case "nopgpfetch":
  536. config.PGPFetch = false
  537. case "upgrademenu":
  538. config.UpgradeMenu = true
  539. case "noupgrademenu":
  540. config.UpgradeMenu = false
  541. case "cleanmenu":
  542. config.CleanMenu = true
  543. case "nocleanmenu":
  544. config.CleanMenu = false
  545. case "diffmenu":
  546. config.DiffMenu = true
  547. case "nodiffmenu":
  548. config.DiffMenu = false
  549. case "editmenu":
  550. config.EditMenu = true
  551. case "noeditmenu":
  552. config.EditMenu = false
  553. case "useask":
  554. config.UseAsk = true
  555. case "nouseask":
  556. config.UseAsk = false
  557. case "combinedupgrade":
  558. config.CombinedUpgrade = true
  559. case "nocombinedupgrade":
  560. config.CombinedUpgrade = false
  561. case "a", "aur":
  562. mode = modeAUR
  563. case "repo":
  564. mode = modeRepo
  565. case "removemake":
  566. config.RemoveMake = "yes"
  567. case "noremovemake":
  568. config.RemoveMake = "no"
  569. case "askremovemake":
  570. config.RemoveMake = "ask"
  571. default:
  572. return false
  573. }
  574. return true
  575. }
  576. func isOp(op string) bool {
  577. switch op {
  578. case "V", "version":
  579. case "D", "database":
  580. case "F", "files":
  581. case "Q", "query":
  582. case "R", "remove":
  583. case "S", "sync":
  584. case "T", "deptest":
  585. case "U", "upgrade":
  586. // yay specific
  587. case "Y", "yay":
  588. case "P", "show":
  589. case "G", "getpkgbuild":
  590. default:
  591. return false
  592. }
  593. return true
  594. }
  595. func isGlobal(op string) bool {
  596. switch op {
  597. case "b", "dbpath":
  598. case "r", "root":
  599. case "v", "verbose":
  600. case "arch":
  601. case "cachedir":
  602. case "color":
  603. case "config":
  604. case "debug":
  605. case "gpgdir":
  606. case "hookdir":
  607. case "logfile":
  608. case "noconfirm":
  609. case "confirm":
  610. default:
  611. return false
  612. }
  613. return true
  614. }
  615. func hasParam(arg string) bool {
  616. switch arg {
  617. case "dbpath", "b":
  618. case "root", "r":
  619. case "sysroot":
  620. case "config":
  621. case "ignore":
  622. case "assume-installed":
  623. case "overwrite":
  624. case "ask":
  625. case "cachedir":
  626. case "hookdir":
  627. case "logfile":
  628. case "ignoregroup":
  629. case "arch":
  630. case "print-format":
  631. case "gpgdir":
  632. case "color":
  633. //yay params
  634. case "aururl":
  635. case "mflags":
  636. case "gpgflags":
  637. case "gitflags":
  638. case "builddir":
  639. case "editor":
  640. case "editorflags":
  641. case "makepkg":
  642. case "makepkgconf":
  643. case "pacman":
  644. case "tar":
  645. case "git":
  646. case "gpg":
  647. case "requestsplitn":
  648. case "answerclean":
  649. case "answerdiff":
  650. case "answeredit":
  651. case "answerupgrade":
  652. case "completioninterval":
  653. case "sortby":
  654. default:
  655. return false
  656. }
  657. return true
  658. }
  659. // Parses short hand options such as:
  660. // -Syu -b/some/path -
  661. func (parser *arguments) parseShortOption(arg string, param string) (usedNext bool, err error) {
  662. if arg == "-" {
  663. err = parser.addArg("-")
  664. return
  665. }
  666. arg = arg[1:]
  667. for k, _char := range arg {
  668. char := string(_char)
  669. if hasParam(char) {
  670. if k < len(arg)-1 {
  671. err = parser.addParam(char, arg[k+1:])
  672. } else {
  673. usedNext = true
  674. err = parser.addParam(char, param)
  675. }
  676. break
  677. } else {
  678. err = parser.addArg(char)
  679. if err != nil {
  680. return
  681. }
  682. }
  683. }
  684. return
  685. }
  686. // Parses full length options such as:
  687. // --sync --refresh --sysupgrade --dbpath /some/path --
  688. func (parser *arguments) parseLongOption(arg string, param string) (usedNext bool, err error) {
  689. if arg == "--" {
  690. err = parser.addArg(arg)
  691. return
  692. }
  693. arg = arg[2:]
  694. switch split := strings.SplitN(arg, "=", 2); {
  695. case len(split) == 2:
  696. err = parser.addParam(split[0], split[1])
  697. case hasParam(arg):
  698. err = parser.addParam(arg, param)
  699. usedNext = true
  700. default:
  701. err = parser.addArg(arg)
  702. }
  703. return
  704. }
  705. func (parser *arguments) parseStdin() error {
  706. scanner := bufio.NewScanner(os.Stdin)
  707. scanner.Split(bufio.ScanLines)
  708. for scanner.Scan() {
  709. parser.addTarget(scanner.Text())
  710. }
  711. return os.Stdin.Close()
  712. }
  713. func (parser *arguments) parseCommandLine() (err error) {
  714. args := os.Args[1:]
  715. usedNext := false
  716. if len(args) < 1 {
  717. parser.parseShortOption("-Syu", "")
  718. } else {
  719. for k, arg := range args {
  720. var nextArg string
  721. if usedNext {
  722. usedNext = false
  723. continue
  724. }
  725. if k+1 < len(args) {
  726. nextArg = args[k+1]
  727. }
  728. switch {
  729. case parser.existsArg("--"):
  730. parser.addTarget(arg)
  731. case strings.HasPrefix(arg, "--"):
  732. usedNext, err = parser.parseLongOption(arg, nextArg)
  733. case strings.HasPrefix(arg, "-"):
  734. usedNext, err = parser.parseShortOption(arg, nextArg)
  735. default:
  736. parser.addTarget(arg)
  737. }
  738. if err != nil {
  739. return
  740. }
  741. }
  742. }
  743. if parser.op == "" {
  744. parser.op = "Y"
  745. }
  746. if parser.existsArg("-") {
  747. var file *os.File
  748. err = parser.parseStdin()
  749. parser.delArg("-")
  750. if err != nil {
  751. return
  752. }
  753. file, err = os.Open("/dev/tty")
  754. if err != nil {
  755. return
  756. }
  757. os.Stdin = file
  758. }
  759. cmdArgs.extractYayOptions()
  760. return
  761. }
  762. func (parser *arguments) extractYayOptions() {
  763. for option, value := range parser.options {
  764. if handleConfig(option, value) {
  765. parser.delArg(option)
  766. }
  767. }
  768. for option, value := range parser.globals {
  769. if handleConfig(option, value) {
  770. parser.delArg(option)
  771. }
  772. }
  773. rpc.AURURL = strings.TrimRight(config.AURURL, "/") + "/rpc.php?"
  774. config.AURURL = strings.TrimRight(config.AURURL, "/")
  775. }
  776. //parses input for number menus split by spaces or commas
  777. //supports individual selection: 1 2 3 4
  778. //supports range selections: 1-4 10-20
  779. //supports negation: ^1 ^1-4
  780. //
  781. //include and excule holds numbers that should be added and should not be added
  782. //respectively. other holds anything that can't be parsed as an int. This is
  783. //intended to allow words inside of number menus. e.g. 'all' 'none' 'abort'
  784. //of course the implementation is up to the caller, this function mearley parses
  785. //the input and organizes it
  786. func parseNumberMenu(input string) (intRanges, intRanges, stringSet, stringSet) {
  787. include := make(intRanges, 0)
  788. exclude := make(intRanges, 0)
  789. otherInclude := make(stringSet)
  790. otherExclude := make(stringSet)
  791. words := strings.FieldsFunc(input, func(c rune) bool {
  792. return unicode.IsSpace(c) || c == ','
  793. })
  794. for _, word := range words {
  795. var num1 int
  796. var num2 int
  797. var err error
  798. invert := false
  799. other := otherInclude
  800. if word[0] == '^' {
  801. invert = true
  802. other = otherExclude
  803. word = word[1:]
  804. }
  805. ranges := strings.SplitN(word, "-", 2)
  806. num1, err = strconv.Atoi(ranges[0])
  807. if err != nil {
  808. other.set(strings.ToLower(word))
  809. continue
  810. }
  811. if len(ranges) == 2 {
  812. num2, err = strconv.Atoi(ranges[1])
  813. if err != nil {
  814. other.set(strings.ToLower(word))
  815. continue
  816. }
  817. } else {
  818. num2 = num1
  819. }
  820. mi := min(num1, num2)
  821. ma := max(num1, num2)
  822. if !invert {
  823. include = append(include, makeIntRange(mi, ma))
  824. } else {
  825. exclude = append(exclude, makeIntRange(mi, ma))
  826. }
  827. }
  828. return include, exclude, otherInclude, otherExclude
  829. }
  830. // Crude html parsing, good enough for the arch news
  831. // This is only displayed in the terminal so there should be no security
  832. // concerns
  833. func parseNews(str string) string {
  834. var buffer bytes.Buffer
  835. var tagBuffer bytes.Buffer
  836. var escapeBuffer bytes.Buffer
  837. inTag := false
  838. inEscape := false
  839. for _, char := range str {
  840. if inTag {
  841. if char == '>' {
  842. inTag = false
  843. switch tagBuffer.String() {
  844. case "code":
  845. buffer.WriteString(cyanCode)
  846. case "/code":
  847. buffer.WriteString(resetCode)
  848. case "/p":
  849. buffer.WriteRune('\n')
  850. }
  851. continue
  852. }
  853. tagBuffer.WriteRune(char)
  854. continue
  855. }
  856. if inEscape {
  857. if char == ';' {
  858. inEscape = false
  859. escapeBuffer.WriteRune(char)
  860. s := html.UnescapeString(escapeBuffer.String())
  861. buffer.WriteString(s)
  862. continue
  863. }
  864. escapeBuffer.WriteRune(char)
  865. continue
  866. }
  867. if char == '<' {
  868. inTag = true
  869. tagBuffer.Reset()
  870. continue
  871. }
  872. if char == '&' {
  873. inEscape = true
  874. escapeBuffer.Reset()
  875. escapeBuffer.WriteRune(char)
  876. continue
  877. }
  878. buffer.WriteRune(char)
  879. }
  880. buffer.WriteString(resetCode)
  881. return buffer.String()
  882. }