parser.go 17 KB

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