My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

queue.cpp 18KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660
  1. /**
  2. * Marlin 3D Printer Firmware
  3. * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
  4. *
  5. * Based on Sprinter and grbl.
  6. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  20. *
  21. */
  22. /**
  23. * queue.cpp - The G-code command queue
  24. */
  25. #include "queue.h"
  26. GCodeQueue queue;
  27. #include "gcode.h"
  28. #include "../lcd/marlinui.h"
  29. #include "../sd/cardreader.h"
  30. #include "../module/planner.h"
  31. #include "../module/temperature.h"
  32. #include "../MarlinCore.h"
  33. #if ENABLED(PRINTER_EVENT_LEDS)
  34. #include "../feature/leds/printer_event_leds.h"
  35. #endif
  36. #if ENABLED(BINARY_FILE_TRANSFER)
  37. #include "../feature/binary_stream.h"
  38. #endif
  39. #if ENABLED(POWER_LOSS_RECOVERY)
  40. #include "../feature/powerloss.h"
  41. #endif
  42. /**
  43. * GCode line number handling. Hosts may opt to include line numbers when
  44. * sending commands to Marlin, and lines will be checked for sequentiality.
  45. * M110 N<int> sets the current line number.
  46. */
  47. long GCodeQueue::last_N[NUM_SERIAL];
  48. /**
  49. * GCode Command Queue
  50. * A simple ring buffer of BUFSIZE command strings.
  51. *
  52. * Commands are copied into this buffer by the command injectors
  53. * (immediate, serial, sd card) and they are processed sequentially by
  54. * the main loop. The gcode.process_next_command method parses the next
  55. * command and hands off execution to individual handler functions.
  56. */
  57. uint8_t GCodeQueue::length = 0, // Count of commands in the queue
  58. GCodeQueue::index_r = 0, // Ring buffer read position
  59. GCodeQueue::index_w = 0; // Ring buffer write position
  60. char GCodeQueue::command_buffer[BUFSIZE][MAX_CMD_SIZE];
  61. /*
  62. * The port that the command was received on
  63. */
  64. #if HAS_MULTI_SERIAL
  65. int16_t GCodeQueue::port[BUFSIZE];
  66. #endif
  67. /**
  68. * Serial command injection
  69. */
  70. // Number of characters read in the current line of serial input
  71. static int serial_count[NUM_SERIAL] = { 0 };
  72. bool send_ok[BUFSIZE];
  73. /**
  74. * Next Injected PROGMEM Command pointer. (nullptr == empty)
  75. * Internal commands are enqueued ahead of serial / SD commands.
  76. */
  77. PGM_P GCodeQueue::injected_commands_P; // = nullptr
  78. /**
  79. * Injected SRAM Commands
  80. */
  81. char GCodeQueue::injected_commands[64]; // = { 0 }
  82. GCodeQueue::GCodeQueue() {
  83. // Send "ok" after commands by default
  84. LOOP_L_N(i, COUNT(send_ok)) send_ok[i] = true;
  85. }
  86. /**
  87. * Check whether there are any commands yet to be executed
  88. */
  89. bool GCodeQueue::has_commands_queued() {
  90. return queue.length || injected_commands_P || injected_commands[0];
  91. }
  92. /**
  93. * Clear the Marlin command queue
  94. */
  95. void GCodeQueue::clear() {
  96. index_r = index_w = length = 0;
  97. }
  98. /**
  99. * Once a new command is in the ring buffer, call this to commit it
  100. */
  101. void GCodeQueue::_commit_command(bool say_ok
  102. #if HAS_MULTI_SERIAL
  103. , int16_t p/*=-1*/
  104. #endif
  105. ) {
  106. send_ok[index_w] = say_ok;
  107. TERN_(HAS_MULTI_SERIAL, port[index_w] = p);
  108. TERN_(POWER_LOSS_RECOVERY, recovery.commit_sdpos(index_w));
  109. if (++index_w >= BUFSIZE) index_w = 0;
  110. length++;
  111. }
  112. /**
  113. * Copy a command from RAM into the main command buffer.
  114. * Return true if the command was successfully added.
  115. * Return false for a full buffer, or if the 'command' is a comment.
  116. */
  117. bool GCodeQueue::_enqueue(const char* cmd, bool say_ok/*=false*/
  118. #if HAS_MULTI_SERIAL
  119. , int16_t pn/*=-1*/
  120. #endif
  121. ) {
  122. if (*cmd == ';' || length >= BUFSIZE) return false;
  123. strcpy(command_buffer[index_w], cmd);
  124. _commit_command(say_ok
  125. #if HAS_MULTI_SERIAL
  126. , pn
  127. #endif
  128. );
  129. return true;
  130. }
  131. #define ISEOL(C) ((C) == '\n' || (C) == '\r')
  132. /**
  133. * Enqueue with Serial Echo
  134. * Return true if the command was consumed
  135. */
  136. bool GCodeQueue::enqueue_one(const char* cmd) {
  137. //SERIAL_ECHOPGM("enqueue_one(\"");
  138. //SERIAL_ECHO(cmd);
  139. //SERIAL_ECHOPGM("\") \n");
  140. if (*cmd == 0 || ISEOL(*cmd)) return true;
  141. if (_enqueue(cmd)) {
  142. SERIAL_ECHO_MSG(STR_ENQUEUEING, cmd, "\"");
  143. return true;
  144. }
  145. return false;
  146. }
  147. /**
  148. * Process the next "immediate" command from PROGMEM.
  149. * Return 'true' if any commands were processed.
  150. */
  151. bool GCodeQueue::process_injected_command_P() {
  152. if (injected_commands_P == nullptr) return false;
  153. char c;
  154. size_t i = 0;
  155. while ((c = pgm_read_byte(&injected_commands_P[i])) && c != '\n') i++;
  156. // Extract current command and move pointer to next command
  157. char cmd[i + 1];
  158. memcpy_P(cmd, injected_commands_P, i);
  159. cmd[i] = '\0';
  160. injected_commands_P = c ? injected_commands_P + i + 1 : nullptr;
  161. // Execute command if non-blank
  162. if (i) {
  163. parser.parse(cmd);
  164. gcode.process_parsed_command();
  165. }
  166. return true;
  167. }
  168. /**
  169. * Process the next "immediate" command from SRAM.
  170. * Return 'true' if any commands were processed.
  171. */
  172. bool GCodeQueue::process_injected_command() {
  173. if (injected_commands[0] == '\0') return false;
  174. char c;
  175. size_t i = 0;
  176. while ((c = injected_commands[i]) && c != '\n') i++;
  177. // Execute a non-blank command
  178. if (i) {
  179. injected_commands[i] = '\0';
  180. parser.parse(injected_commands);
  181. gcode.process_parsed_command();
  182. }
  183. // Copy the next command into place
  184. for (
  185. uint8_t d = 0, s = i + !!c; // dst, src
  186. (injected_commands[d] = injected_commands[s]); // copy, exit if 0
  187. d++, s++ // next dst, src
  188. );
  189. return true;
  190. }
  191. /**
  192. * Enqueue and return only when commands are actually enqueued.
  193. * Never call this from a G-code handler!
  194. */
  195. void GCodeQueue::enqueue_one_now(const char* cmd) { while (!enqueue_one(cmd)) idle(); }
  196. /**
  197. * Attempt to enqueue a single G-code command
  198. * and return 'true' if successful.
  199. */
  200. bool GCodeQueue::enqueue_one_P(PGM_P const pgcode) {
  201. size_t i = 0;
  202. PGM_P p = pgcode;
  203. char c;
  204. while ((c = pgm_read_byte(&p[i])) && c != '\n') i++;
  205. char cmd[i + 1];
  206. memcpy_P(cmd, p, i);
  207. cmd[i] = '\0';
  208. return _enqueue(cmd);
  209. }
  210. /**
  211. * Enqueue from program memory and return only when commands are actually enqueued
  212. * Never call this from a G-code handler!
  213. */
  214. void GCodeQueue::enqueue_now_P(PGM_P const pgcode) {
  215. size_t i = 0;
  216. PGM_P p = pgcode;
  217. for (;;) {
  218. char c;
  219. while ((c = pgm_read_byte(&p[i])) && c != '\n') i++;
  220. char cmd[i + 1];
  221. memcpy_P(cmd, p, i);
  222. cmd[i] = '\0';
  223. enqueue_one_now(cmd);
  224. if (!c) break;
  225. p += i + 1;
  226. }
  227. }
  228. /**
  229. * Send an "ok" message to the host, indicating
  230. * that a command was successfully processed.
  231. *
  232. * If ADVANCED_OK is enabled also include:
  233. * N<int> Line number of the command, if any
  234. * P<int> Planner space remaining
  235. * B<int> Block queue space remaining
  236. */
  237. void GCodeQueue::ok_to_send() {
  238. #if HAS_MULTI_SERIAL
  239. const int16_t pn = command_port();
  240. if (pn < 0) return;
  241. PORT_REDIRECT(pn); // Reply to the serial port that sent the command
  242. #endif
  243. if (!send_ok[index_r]) return;
  244. SERIAL_ECHOPGM(STR_OK);
  245. #if ENABLED(ADVANCED_OK)
  246. char* p = command_buffer[index_r];
  247. if (*p == 'N') {
  248. SERIAL_ECHO(' ');
  249. SERIAL_ECHO(*p++);
  250. while (NUMERIC_SIGNED(*p))
  251. SERIAL_ECHO(*p++);
  252. }
  253. SERIAL_ECHOPAIR_P(SP_P_STR, int(planner.moves_free()),
  254. SP_B_STR, int(BUFSIZE - length));
  255. #endif
  256. SERIAL_EOL();
  257. }
  258. /**
  259. * Send a "Resend: nnn" message to the host to
  260. * indicate that a command needs to be re-sent.
  261. */
  262. void GCodeQueue::flush_and_request_resend() {
  263. const int16_t pn = command_port();
  264. #if HAS_MULTI_SERIAL
  265. if (pn < 0) return;
  266. PORT_REDIRECT(pn); // Reply to the serial port that sent the command
  267. #endif
  268. SERIAL_FLUSH();
  269. SERIAL_ECHOPGM(STR_RESEND);
  270. SERIAL_ECHOLN(last_N[pn] + 1);
  271. ok_to_send();
  272. }
  273. inline bool serial_data_available() {
  274. return MYSERIAL0.available() || TERN0(HAS_MULTI_SERIAL, MYSERIAL1.available());
  275. }
  276. inline int read_serial(const uint8_t index) {
  277. switch (index) {
  278. case 0: return MYSERIAL0.read();
  279. #if HAS_MULTI_SERIAL
  280. case 1: return MYSERIAL1.read();
  281. #endif
  282. default: return -1;
  283. }
  284. }
  285. void GCodeQueue::gcode_line_error(PGM_P const err, const int8_t pn) {
  286. PORT_REDIRECT(pn); // Reply to the serial port that sent the command
  287. SERIAL_ERROR_START();
  288. serialprintPGM(err);
  289. SERIAL_ECHOLN(last_N[pn]);
  290. while (read_serial(pn) != -1); // Clear out the RX buffer
  291. flush_and_request_resend();
  292. serial_count[pn] = 0;
  293. }
  294. FORCE_INLINE bool is_M29(const char * const cmd) { // matches "M29" & "M29 ", but not "M290", etc
  295. const char * const m29 = strstr_P(cmd, PSTR("M29"));
  296. return m29 && !NUMERIC(m29[3]);
  297. }
  298. #define PS_NORMAL 0
  299. #define PS_EOL 1
  300. #define PS_QUOTED 2
  301. #define PS_PAREN 3
  302. #define PS_ESC 4
  303. inline void process_stream_char(const char c, uint8_t &sis, char (&buff)[MAX_CMD_SIZE], int &ind) {
  304. if (sis == PS_EOL) return; // EOL comment or overflow
  305. #if ENABLED(PAREN_COMMENTS)
  306. else if (sis == PS_PAREN) { // Inline comment
  307. if (c == ')') sis = PS_NORMAL;
  308. return;
  309. }
  310. #endif
  311. else if (sis >= PS_ESC) // End escaped char
  312. sis -= PS_ESC;
  313. else if (c == '\\') { // Start escaped char
  314. sis += PS_ESC;
  315. if (sis == PS_ESC) return; // Keep if quoting
  316. }
  317. #if ENABLED(GCODE_QUOTED_STRINGS)
  318. else if (sis == PS_QUOTED) {
  319. if (c == '"') sis = PS_NORMAL; // End quoted string
  320. }
  321. else if (c == '"') // Start quoted string
  322. sis = PS_QUOTED;
  323. #endif
  324. else if (c == ';') { // Start end-of-line comment
  325. sis = PS_EOL;
  326. return;
  327. }
  328. #if ENABLED(PAREN_COMMENTS)
  329. else if (c == '(') { // Start inline comment
  330. sis = PS_PAREN;
  331. return;
  332. }
  333. #endif
  334. // Backspace erases previous characters
  335. if (c == 0x08) {
  336. if (ind) buff[--ind] = '\0';
  337. }
  338. else {
  339. buff[ind++] = c;
  340. if (ind >= MAX_CMD_SIZE - 1)
  341. sis = PS_EOL; // Skip the rest on overflow
  342. }
  343. }
  344. /**
  345. * Handle a line being completed. For an empty line
  346. * keep sensor readings going and watchdog alive.
  347. */
  348. inline bool process_line_done(uint8_t &sis, char (&buff)[MAX_CMD_SIZE], int &ind) {
  349. sis = PS_NORMAL;
  350. buff[ind] = 0;
  351. if (ind) { ind = 0; return false; }
  352. thermalManager.manage_heater();
  353. return true;
  354. }
  355. /**
  356. * Get all commands waiting on the serial port and queue them.
  357. * Exit when the buffer is full or when no more characters are
  358. * left on the serial port.
  359. */
  360. void GCodeQueue::get_serial_commands() {
  361. static char serial_line_buffer[NUM_SERIAL][MAX_CMD_SIZE];
  362. static uint8_t serial_input_state[NUM_SERIAL] = { PS_NORMAL };
  363. #if ENABLED(BINARY_FILE_TRANSFER)
  364. if (card.flag.binary_mode) {
  365. /**
  366. * For binary stream file transfer, use serial_line_buffer as the working
  367. * receive buffer (which limits the packet size to MAX_CMD_SIZE).
  368. * The receive buffer also limits the packet size for reliable transmission.
  369. */
  370. binaryStream[card.transfer_port_index].receive(serial_line_buffer[card.transfer_port_index]);
  371. return;
  372. }
  373. #endif
  374. // If the command buffer is empty for too long,
  375. // send "wait" to indicate Marlin is still waiting.
  376. #if NO_TIMEOUTS > 0
  377. static millis_t last_command_time = 0;
  378. const millis_t ms = millis();
  379. if (length == 0 && !serial_data_available() && ELAPSED(ms, last_command_time + NO_TIMEOUTS)) {
  380. SERIAL_ECHOLNPGM(STR_WAIT);
  381. last_command_time = ms;
  382. }
  383. #endif
  384. /**
  385. * Loop while serial characters are incoming and the queue is not full
  386. */
  387. while (length < BUFSIZE && serial_data_available()) {
  388. LOOP_L_N(i, NUM_SERIAL) {
  389. const int c = read_serial(i);
  390. if (c < 0) continue;
  391. const char serial_char = c;
  392. if (ISEOL(serial_char)) {
  393. // Reset our state, continue if the line was empty
  394. if (process_line_done(serial_input_state[i], serial_line_buffer[i], serial_count[i]))
  395. continue;
  396. char* command = serial_line_buffer[i];
  397. while (*command == ' ') command++; // Skip leading spaces
  398. char *npos = (*command == 'N') ? command : nullptr; // Require the N parameter to start the line
  399. if (npos) {
  400. bool M110 = strstr_P(command, PSTR("M110")) != nullptr;
  401. if (M110) {
  402. char* n2pos = strchr(command + 4, 'N');
  403. if (n2pos) npos = n2pos;
  404. }
  405. const long gcode_N = strtol(npos + 1, nullptr, 10);
  406. if (gcode_N != last_N[i] + 1 && !M110)
  407. return gcode_line_error(PSTR(STR_ERR_LINE_NO), i);
  408. char *apos = strrchr(command, '*');
  409. if (apos) {
  410. uint8_t checksum = 0, count = uint8_t(apos - command);
  411. while (count) checksum ^= command[--count];
  412. if (strtol(apos + 1, nullptr, 10) != checksum)
  413. return gcode_line_error(PSTR(STR_ERR_CHECKSUM_MISMATCH), i);
  414. }
  415. else
  416. return gcode_line_error(PSTR(STR_ERR_NO_CHECKSUM), i);
  417. last_N[i] = gcode_N;
  418. }
  419. #if ENABLED(SDSUPPORT)
  420. // Pronterface "M29" and "M29 " has no line number
  421. else if (card.flag.saving && !is_M29(command))
  422. return gcode_line_error(PSTR(STR_ERR_NO_CHECKSUM), i);
  423. #endif
  424. //
  425. // Movement commands give an alert when the machine is stopped
  426. //
  427. if (IsStopped()) {
  428. char* gpos = strchr(command, 'G');
  429. if (gpos) {
  430. switch (strtol(gpos + 1, nullptr, 10)) {
  431. case 0: case 1:
  432. #if ENABLED(ARC_SUPPORT)
  433. case 2: case 3:
  434. #endif
  435. #if ENABLED(BEZIER_CURVE_SUPPORT)
  436. case 5:
  437. #endif
  438. PORT_REDIRECT(i); // Reply to the serial port that sent the command
  439. SERIAL_ECHOLNPGM(STR_ERR_STOPPED);
  440. LCD_MESSAGEPGM(MSG_STOPPED);
  441. break;
  442. }
  443. }
  444. }
  445. #if DISABLED(EMERGENCY_PARSER)
  446. // Process critical commands early
  447. if (strcmp_P(command, PSTR("M108")) == 0) {
  448. wait_for_heatup = false;
  449. TERN_(HAS_LCD_MENU, wait_for_user = false);
  450. }
  451. if (strcmp_P(command, PSTR("M112")) == 0) kill(M112_KILL_STR, nullptr, true);
  452. if (strcmp_P(command, PSTR("M410")) == 0) quickstop_stepper();
  453. #endif
  454. #if defined(NO_TIMEOUTS) && NO_TIMEOUTS > 0
  455. last_command_time = ms;
  456. #endif
  457. // Add the command to the queue
  458. _enqueue(serial_line_buffer[i], true
  459. #if HAS_MULTI_SERIAL
  460. , i
  461. #endif
  462. );
  463. }
  464. else
  465. process_stream_char(serial_char, serial_input_state[i], serial_line_buffer[i], serial_count[i]);
  466. } // for NUM_SERIAL
  467. } // queue has space, serial has data
  468. }
  469. #if ENABLED(SDSUPPORT)
  470. /**
  471. * Get lines from the SD Card until the command buffer is full
  472. * or until the end of the file is reached. Because this method
  473. * always receives complete command-lines, they can go directly
  474. * into the main command queue.
  475. */
  476. inline void GCodeQueue::get_sdcard_commands() {
  477. static uint8_t sd_input_state = PS_NORMAL;
  478. if (!IS_SD_PRINTING()) return;
  479. int sd_count = 0;
  480. bool card_eof = card.eof();
  481. while (length < BUFSIZE && !card_eof) {
  482. const int16_t n = card.get();
  483. card_eof = card.eof();
  484. if (n < 0 && !card_eof) { SERIAL_ERROR_MSG(STR_SD_ERR_READ); continue; }
  485. const char sd_char = (char)n;
  486. const bool is_eol = ISEOL(sd_char);
  487. if (is_eol || card_eof) {
  488. // Reset stream state, terminate the buffer, and commit a non-empty command
  489. if (!is_eol && sd_count) ++sd_count; // End of file with no newline
  490. if (!process_line_done(sd_input_state, command_buffer[index_w], sd_count)) {
  491. _commit_command(false);
  492. #if ENABLED(POWER_LOSS_RECOVERY)
  493. recovery.cmd_sdpos = card.getIndex(); // Prime for the NEXT _commit_command
  494. #endif
  495. }
  496. if (card_eof) card.fileHasFinished(); // Handle end of file reached
  497. }
  498. else
  499. process_stream_char(sd_char, sd_input_state, command_buffer[index_w], sd_count);
  500. }
  501. }
  502. #endif // SDSUPPORT
  503. /**
  504. * Add to the circular command queue the next command from:
  505. * - The command-injection queues (injected_commands_P, injected_commands)
  506. * - The active serial input (usually USB)
  507. * - The SD card file being actively printed
  508. */
  509. void GCodeQueue::get_available_commands() {
  510. get_serial_commands();
  511. TERN_(SDSUPPORT, get_sdcard_commands());
  512. }
  513. /**
  514. * Get the next command in the queue, optionally log it to SD, then dispatch it
  515. */
  516. void GCodeQueue::advance() {
  517. // Process immediate commands
  518. if (process_injected_command_P() || process_injected_command()) return;
  519. // Return if the G-code buffer is empty
  520. if (!length) return;
  521. #if ENABLED(SDSUPPORT)
  522. if (card.flag.saving) {
  523. char* command = command_buffer[index_r];
  524. if (is_M29(command)) {
  525. // M29 closes the file
  526. card.closefile();
  527. SERIAL_ECHOLNPGM(STR_FILE_SAVED);
  528. #if !defined(__AVR__) || !defined(USBCON)
  529. #if ENABLED(SERIAL_STATS_DROPPED_RX)
  530. SERIAL_ECHOLNPAIR("Dropped bytes: ", MYSERIAL0.dropped());
  531. #endif
  532. #if ENABLED(SERIAL_STATS_MAX_RX_QUEUED)
  533. SERIAL_ECHOLNPAIR("Max RX Queue Size: ", MYSERIAL0.rxMaxEnqueued());
  534. #endif
  535. #endif
  536. ok_to_send();
  537. }
  538. else {
  539. // Write the string from the read buffer to SD
  540. card.write_command(command);
  541. if (card.flag.logging)
  542. gcode.process_next_command(); // The card is saving because it's logging
  543. else
  544. ok_to_send();
  545. }
  546. }
  547. else
  548. gcode.process_next_command();
  549. #else
  550. gcode.process_next_command();
  551. #endif // SDSUPPORT
  552. // The queue may be reset by a command handler or by code invoked by idle() within a handler
  553. --length;
  554. if (++index_r >= BUFSIZE) index_r = 0;
  555. }