parser.go 19 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019
  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 "sudo":
  394. case "sudoflags":
  395. case "requestsplitn":
  396. case "sudoloop":
  397. case "nosudoloop":
  398. case "provides":
  399. case "noprovides":
  400. case "pgpfetch":
  401. case "nopgpfetch":
  402. case "upgrademenu":
  403. case "noupgrademenu":
  404. case "cleanmenu":
  405. case "nocleanmenu":
  406. case "diffmenu":
  407. case "nodiffmenu":
  408. case "editmenu":
  409. case "noeditmenu":
  410. case "useask":
  411. case "nouseask":
  412. case "combinedupgrade":
  413. case "nocombinedupgrade":
  414. case "a", "aur":
  415. case "repo":
  416. case "removemake":
  417. case "noremovemake":
  418. case "askremovemake":
  419. case "complete":
  420. case "stats":
  421. case "news":
  422. case "gendb":
  423. case "currentconfig":
  424. default:
  425. return false
  426. }
  427. return true
  428. }
  429. func handleConfig(option, value string) bool {
  430. switch option {
  431. case "aururl":
  432. config.AURURL = value
  433. case "save":
  434. shouldSaveConfig = true
  435. case "afterclean", "cleanafter":
  436. config.CleanAfter = true
  437. case "noafterclean", "nocleanafter":
  438. config.CleanAfter = false
  439. case "devel":
  440. config.Devel = true
  441. case "nodevel":
  442. config.Devel = false
  443. case "timeupdate":
  444. config.TimeUpdate = true
  445. case "notimeupdate":
  446. config.TimeUpdate = false
  447. case "topdown":
  448. config.SortMode = topDown
  449. case "bottomup":
  450. config.SortMode = bottomUp
  451. case "completioninterval":
  452. n, err := strconv.Atoi(value)
  453. if err == nil {
  454. config.CompletionInterval = n
  455. }
  456. case "sortby":
  457. config.SortBy = value
  458. case "noconfirm":
  459. config.NoConfirm = true
  460. case "config":
  461. config.PacmanConf = value
  462. case "redownload":
  463. config.ReDownload = "yes"
  464. case "redownloadall":
  465. config.ReDownload = "all"
  466. case "noredownload":
  467. config.ReDownload = "no"
  468. case "rebuild":
  469. config.ReBuild = "yes"
  470. case "rebuildall":
  471. config.ReBuild = "all"
  472. case "rebuildtree":
  473. config.ReBuild = "tree"
  474. case "norebuild":
  475. config.ReBuild = "no"
  476. case "answerclean":
  477. config.AnswerClean = value
  478. case "noanswerclean":
  479. config.AnswerClean = ""
  480. case "answerdiff":
  481. config.AnswerDiff = value
  482. case "noanswerdiff":
  483. config.AnswerDiff = ""
  484. case "answeredit":
  485. config.AnswerEdit = value
  486. case "noansweredit":
  487. config.AnswerEdit = ""
  488. case "answerupgrade":
  489. config.AnswerUpgrade = value
  490. case "noanswerupgrade":
  491. config.AnswerUpgrade = ""
  492. case "gitclone":
  493. config.GitClone = true
  494. case "nogitclone":
  495. config.GitClone = false
  496. case "gpgflags":
  497. config.GpgFlags = value
  498. case "mflags":
  499. config.MFlags = value
  500. case "gitflags":
  501. config.GitFlags = value
  502. case "builddir":
  503. config.BuildDir = value
  504. case "editor":
  505. config.Editor = value
  506. case "editorflags":
  507. config.EditorFlags = value
  508. case "makepkg":
  509. config.MakepkgBin = value
  510. case "makepkgconf":
  511. config.MakepkgConf = value
  512. case "nomakepkgconf":
  513. config.MakepkgConf = ""
  514. case "pacman":
  515. config.PacmanBin = value
  516. case "tar":
  517. config.TarBin = value
  518. case "git":
  519. config.GitBin = value
  520. case "gpg":
  521. config.GpgBin = value
  522. case "sudo":
  523. config.SudoBin = value
  524. case "sudoflags":
  525. config.SudoFlags = value
  526. case "requestsplitn":
  527. n, err := strconv.Atoi(value)
  528. if err == nil && n > 0 {
  529. config.RequestSplitN = n
  530. }
  531. case "sudoloop":
  532. config.SudoLoop = true
  533. case "nosudoloop":
  534. config.SudoLoop = false
  535. case "provides":
  536. config.Provides = true
  537. case "noprovides":
  538. config.Provides = false
  539. case "pgpfetch":
  540. config.PGPFetch = true
  541. case "nopgpfetch":
  542. config.PGPFetch = false
  543. case "upgrademenu":
  544. config.UpgradeMenu = true
  545. case "noupgrademenu":
  546. config.UpgradeMenu = false
  547. case "cleanmenu":
  548. config.CleanMenu = true
  549. case "nocleanmenu":
  550. config.CleanMenu = false
  551. case "diffmenu":
  552. config.DiffMenu = true
  553. case "nodiffmenu":
  554. config.DiffMenu = false
  555. case "editmenu":
  556. config.EditMenu = true
  557. case "noeditmenu":
  558. config.EditMenu = false
  559. case "useask":
  560. config.UseAsk = true
  561. case "nouseask":
  562. config.UseAsk = false
  563. case "combinedupgrade":
  564. config.CombinedUpgrade = true
  565. case "nocombinedupgrade":
  566. config.CombinedUpgrade = false
  567. case "a", "aur":
  568. mode = modeAUR
  569. case "repo":
  570. mode = modeRepo
  571. case "removemake":
  572. config.RemoveMake = "yes"
  573. case "noremovemake":
  574. config.RemoveMake = "no"
  575. case "askremovemake":
  576. config.RemoveMake = "ask"
  577. default:
  578. return false
  579. }
  580. return true
  581. }
  582. func isOp(op string) bool {
  583. switch op {
  584. case "V", "version":
  585. case "D", "database":
  586. case "F", "files":
  587. case "Q", "query":
  588. case "R", "remove":
  589. case "S", "sync":
  590. case "T", "deptest":
  591. case "U", "upgrade":
  592. // yay specific
  593. case "Y", "yay":
  594. case "P", "show":
  595. case "G", "getpkgbuild":
  596. default:
  597. return false
  598. }
  599. return true
  600. }
  601. func isGlobal(op string) bool {
  602. switch op {
  603. case "b", "dbpath":
  604. case "r", "root":
  605. case "v", "verbose":
  606. case "arch":
  607. case "cachedir":
  608. case "color":
  609. case "config":
  610. case "debug":
  611. case "gpgdir":
  612. case "hookdir":
  613. case "logfile":
  614. case "noconfirm":
  615. case "confirm":
  616. default:
  617. return false
  618. }
  619. return true
  620. }
  621. func hasParam(arg string) bool {
  622. switch arg {
  623. case "dbpath", "b":
  624. case "root", "r":
  625. case "sysroot":
  626. case "config":
  627. case "ignore":
  628. case "assume-installed":
  629. case "overwrite":
  630. case "ask":
  631. case "cachedir":
  632. case "hookdir":
  633. case "logfile":
  634. case "ignoregroup":
  635. case "arch":
  636. case "print-format":
  637. case "gpgdir":
  638. case "color":
  639. //yay params
  640. case "aururl":
  641. case "mflags":
  642. case "gpgflags":
  643. case "gitflags":
  644. case "builddir":
  645. case "editor":
  646. case "editorflags":
  647. case "makepkg":
  648. case "makepkgconf":
  649. case "pacman":
  650. case "tar":
  651. case "git":
  652. case "gpg":
  653. case "sudo":
  654. case "sudoflags":
  655. case "requestsplitn":
  656. case "answerclean":
  657. case "answerdiff":
  658. case "answeredit":
  659. case "answerupgrade":
  660. case "completioninterval":
  661. case "sortby":
  662. default:
  663. return false
  664. }
  665. return true
  666. }
  667. // Parses short hand options such as:
  668. // -Syu -b/some/path -
  669. func (parser *arguments) parseShortOption(arg string, param string) (usedNext bool, err error) {
  670. if arg == "-" {
  671. err = parser.addArg("-")
  672. return
  673. }
  674. arg = arg[1:]
  675. for k, _char := range arg {
  676. char := string(_char)
  677. if hasParam(char) {
  678. if k < len(arg)-1 {
  679. err = parser.addParam(char, arg[k+1:])
  680. } else {
  681. usedNext = true
  682. err = parser.addParam(char, param)
  683. }
  684. break
  685. } else {
  686. err = parser.addArg(char)
  687. if err != nil {
  688. return
  689. }
  690. }
  691. }
  692. return
  693. }
  694. // Parses full length options such as:
  695. // --sync --refresh --sysupgrade --dbpath /some/path --
  696. func (parser *arguments) parseLongOption(arg string, param string) (usedNext bool, err error) {
  697. if arg == "--" {
  698. err = parser.addArg(arg)
  699. return
  700. }
  701. arg = arg[2:]
  702. switch split := strings.SplitN(arg, "=", 2); {
  703. case len(split) == 2:
  704. err = parser.addParam(split[0], split[1])
  705. case hasParam(arg):
  706. err = parser.addParam(arg, param)
  707. usedNext = true
  708. default:
  709. err = parser.addArg(arg)
  710. }
  711. return
  712. }
  713. func (parser *arguments) parseStdin() error {
  714. scanner := bufio.NewScanner(os.Stdin)
  715. scanner.Split(bufio.ScanLines)
  716. for scanner.Scan() {
  717. parser.addTarget(scanner.Text())
  718. }
  719. return os.Stdin.Close()
  720. }
  721. func (parser *arguments) parseCommandLine() (err error) {
  722. args := os.Args[1:]
  723. usedNext := false
  724. if len(args) < 1 {
  725. parser.parseShortOption("-Syu", "")
  726. } else {
  727. for k, arg := range args {
  728. var nextArg string
  729. if usedNext {
  730. usedNext = false
  731. continue
  732. }
  733. if k+1 < len(args) {
  734. nextArg = args[k+1]
  735. }
  736. switch {
  737. case parser.existsArg("--"):
  738. parser.addTarget(arg)
  739. case strings.HasPrefix(arg, "--"):
  740. usedNext, err = parser.parseLongOption(arg, nextArg)
  741. case strings.HasPrefix(arg, "-"):
  742. usedNext, err = parser.parseShortOption(arg, nextArg)
  743. default:
  744. parser.addTarget(arg)
  745. }
  746. if err != nil {
  747. return
  748. }
  749. }
  750. }
  751. if parser.op == "" {
  752. parser.op = "Y"
  753. }
  754. if parser.existsArg("-") {
  755. var file *os.File
  756. err = parser.parseStdin()
  757. parser.delArg("-")
  758. if err != nil {
  759. return
  760. }
  761. file, err = os.Open("/dev/tty")
  762. if err != nil {
  763. return
  764. }
  765. os.Stdin = file
  766. }
  767. cmdArgs.extractYayOptions()
  768. return
  769. }
  770. func (parser *arguments) extractYayOptions() {
  771. for option, value := range parser.options {
  772. if handleConfig(option, value) {
  773. parser.delArg(option)
  774. }
  775. }
  776. for option, value := range parser.globals {
  777. if handleConfig(option, value) {
  778. parser.delArg(option)
  779. }
  780. }
  781. rpc.AURURL = strings.TrimRight(config.AURURL, "/") + "/rpc.php?"
  782. config.AURURL = strings.TrimRight(config.AURURL, "/")
  783. }
  784. //parses input for number menus split by spaces or commas
  785. //supports individual selection: 1 2 3 4
  786. //supports range selections: 1-4 10-20
  787. //supports negation: ^1 ^1-4
  788. //
  789. //include and excule holds numbers that should be added and should not be added
  790. //respectively. other holds anything that can't be parsed as an int. This is
  791. //intended to allow words inside of number menus. e.g. 'all' 'none' 'abort'
  792. //of course the implementation is up to the caller, this function mearley parses
  793. //the input and organizes it
  794. func parseNumberMenu(input string) (intRanges, intRanges, stringSet, stringSet) {
  795. include := make(intRanges, 0)
  796. exclude := make(intRanges, 0)
  797. otherInclude := make(stringSet)
  798. otherExclude := make(stringSet)
  799. words := strings.FieldsFunc(input, func(c rune) bool {
  800. return unicode.IsSpace(c) || c == ','
  801. })
  802. for _, word := range words {
  803. var num1 int
  804. var num2 int
  805. var err error
  806. invert := false
  807. other := otherInclude
  808. if word[0] == '^' {
  809. invert = true
  810. other = otherExclude
  811. word = word[1:]
  812. }
  813. ranges := strings.SplitN(word, "-", 2)
  814. num1, err = strconv.Atoi(ranges[0])
  815. if err != nil {
  816. other.set(strings.ToLower(word))
  817. continue
  818. }
  819. if len(ranges) == 2 {
  820. num2, err = strconv.Atoi(ranges[1])
  821. if err != nil {
  822. other.set(strings.ToLower(word))
  823. continue
  824. }
  825. } else {
  826. num2 = num1
  827. }
  828. mi := min(num1, num2)
  829. ma := max(num1, num2)
  830. if !invert {
  831. include = append(include, makeIntRange(mi, ma))
  832. } else {
  833. exclude = append(exclude, makeIntRange(mi, ma))
  834. }
  835. }
  836. return include, exclude, otherInclude, otherExclude
  837. }
  838. // Crude html parsing, good enough for the arch news
  839. // This is only displayed in the terminal so there should be no security
  840. // concerns
  841. func parseNews(str string) string {
  842. var buffer bytes.Buffer
  843. var tagBuffer bytes.Buffer
  844. var escapeBuffer bytes.Buffer
  845. inTag := false
  846. inEscape := false
  847. for _, char := range str {
  848. if inTag {
  849. if char == '>' {
  850. inTag = false
  851. switch tagBuffer.String() {
  852. case "code":
  853. buffer.WriteString(cyanCode)
  854. case "/code":
  855. buffer.WriteString(resetCode)
  856. case "/p":
  857. buffer.WriteRune('\n')
  858. }
  859. continue
  860. }
  861. tagBuffer.WriteRune(char)
  862. continue
  863. }
  864. if inEscape {
  865. if char == ';' {
  866. inEscape = false
  867. escapeBuffer.WriteRune(char)
  868. s := html.UnescapeString(escapeBuffer.String())
  869. buffer.WriteString(s)
  870. continue
  871. }
  872. escapeBuffer.WriteRune(char)
  873. continue
  874. }
  875. if char == '<' {
  876. inTag = true
  877. tagBuffer.Reset()
  878. continue
  879. }
  880. if char == '&' {
  881. inEscape = true
  882. escapeBuffer.Reset()
  883. escapeBuffer.WriteRune(char)
  884. continue
  885. }
  886. buffer.WriteRune(char)
  887. }
  888. buffer.WriteString(resetCode)
  889. return buffer.String()
  890. }