parser.go 19 KB

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