parser.go 17 KB

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