parser.go 19 KB

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