My Marlin configs for Fabrikator Mini and CTC i3 Pro B
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

queue.cpp 19KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655
  1. /**
  2. * Marlin 3D Printer Firmware
  3. * Copyright (c) 2019 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 <http://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/ultralcd.h"
  29. #include "../sd/cardreader.h"
  30. #include "../module/planner.h"
  31. #include "../module/temperature.h"
  32. #include "../Marlin.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_protocol.h"
  38. #endif
  39. /**
  40. * GCode line number handling. Hosts may opt to include line numbers when
  41. * sending commands to Marlin, and lines will be checked for sequentiality.
  42. * M110 N<int> sets the current line number.
  43. */
  44. long gcode_N, GCodeQueue::last_N, GCodeQueue::stopped_N = 0;
  45. /**
  46. * GCode Command Queue
  47. * A simple ring buffer of BUFSIZE command strings.
  48. *
  49. * Commands are copied into this buffer by the command injectors
  50. * (immediate, serial, sd card) and they are processed sequentially by
  51. * the main loop. The gcode.process_next_command method parses the next
  52. * command and hands off execution to individual handler functions.
  53. */
  54. uint8_t GCodeQueue::length = 0, // Count of commands in the queue
  55. GCodeQueue::index_r = 0, // Ring buffer read position
  56. GCodeQueue::index_w = 0; // Ring buffer write position
  57. char GCodeQueue::command_buffer[BUFSIZE][MAX_CMD_SIZE];
  58. /*
  59. * The port that the command was received on
  60. */
  61. #if NUM_SERIAL > 1
  62. int16_t GCodeQueue::port[BUFSIZE];
  63. #endif
  64. /**
  65. * Serial command injection
  66. */
  67. // Number of characters read in the current line of serial input
  68. static int serial_count[NUM_SERIAL] = { 0 };
  69. bool send_ok[BUFSIZE];
  70. /**
  71. * Next Injected Command pointer. nullptr if no commands are being injected.
  72. * Used by Marlin internally to ensure that commands initiated from within
  73. * are enqueued ahead of any pending serial or sd card commands.
  74. */
  75. static PGM_P injected_commands_P = nullptr;
  76. GCodeQueue::GCodeQueue() {
  77. // Send "ok" after commands by default
  78. for (uint8_t i = 0; i < COUNT(send_ok); i++) send_ok[i] = true;
  79. }
  80. /**
  81. * Check whether there are any commands yet to be executed
  82. */
  83. bool GCodeQueue::has_commands_queued() {
  84. return queue.length || injected_commands_P;
  85. }
  86. /**
  87. * Clear the Marlin command queue
  88. */
  89. void GCodeQueue::clear() {
  90. index_r = index_w = length = 0;
  91. }
  92. /**
  93. * Once a new command is in the ring buffer, call this to commit it
  94. */
  95. void GCodeQueue::_commit_command(bool say_ok
  96. #if NUM_SERIAL > 1
  97. , int16_t p/*=-1*/
  98. #endif
  99. ) {
  100. send_ok[index_w] = say_ok;
  101. #if NUM_SERIAL > 1
  102. port[index_w] = p;
  103. #endif
  104. if (++index_w >= BUFSIZE) index_w = 0;
  105. length++;
  106. }
  107. /**
  108. * Copy a command from RAM into the main command buffer.
  109. * Return true if the command was successfully added.
  110. * Return false for a full buffer, or if the 'command' is a comment.
  111. */
  112. bool GCodeQueue::_enqueue(const char* cmd, bool say_ok/*=false*/
  113. #if NUM_SERIAL > 1
  114. , int16_t pn/*=-1*/
  115. #endif
  116. ) {
  117. if (*cmd == ';' || length >= BUFSIZE) return false;
  118. strcpy(command_buffer[index_w], cmd);
  119. _commit_command(say_ok
  120. #if NUM_SERIAL > 1
  121. , pn
  122. #endif
  123. );
  124. return true;
  125. }
  126. /**
  127. * Enqueue with Serial Echo
  128. * Return true if the command was consumed
  129. */
  130. bool GCodeQueue::enqueue_one(const char* cmd) {
  131. //SERIAL_ECHOPGM("enqueue_one(\"");
  132. //SERIAL_ECHO(cmd);
  133. //SERIAL_ECHOPGM("\") \n");
  134. if (*cmd == 0 || *cmd == '\n' || *cmd == '\r') return true;
  135. if (_enqueue(cmd)) {
  136. SERIAL_ECHO_START();
  137. SERIAL_ECHOLNPAIR(MSG_ENQUEUEING, cmd, "\"");
  138. return true;
  139. }
  140. return false;
  141. }
  142. /**
  143. * Process the next "immediate" command.
  144. * Return 'true' if any commands were processed,
  145. * or remain to process.
  146. */
  147. bool GCodeQueue::process_injected_command() {
  148. if (injected_commands_P == nullptr) return false;
  149. char c;
  150. size_t i = 0;
  151. while ((c = pgm_read_byte(&injected_commands_P[i])) && c != '\n') i++;
  152. // Extract current command and move pointer to next command
  153. char cmd[i + 1];
  154. memcpy_P(cmd, injected_commands_P, i);
  155. cmd[i] = '\0';
  156. injected_commands_P = c ? injected_commands_P + i + 1 : nullptr;
  157. // Execute command if non-blank
  158. if (i) {
  159. parser.parse(cmd);
  160. PORT_REDIRECT(SERIAL_PORT);
  161. gcode.process_parsed_command();
  162. }
  163. return true;
  164. }
  165. /**
  166. * Enqueue one or many commands to run from program memory.
  167. * Do not inject a comment or use leading spaces!
  168. * Aborts the current queue, if any.
  169. * Note: process_injected_command() will be called to drain any commands afterwards
  170. */
  171. void GCodeQueue::inject_P(PGM_P const pgcode) { injected_commands_P = pgcode; }
  172. /**
  173. * Enqueue and return only when commands are actually enqueued.
  174. * Never call this from a G-code handler!
  175. */
  176. void GCodeQueue::enqueue_one_now(const char* cmd) { while (!enqueue_one(cmd)) idle(); }
  177. /**
  178. * Enqueue from program memory and return only when commands are actually enqueued
  179. * Never call this from a G-code handler!
  180. */
  181. void GCodeQueue::enqueue_now_P(PGM_P const pgcode) {
  182. size_t i = 0;
  183. PGM_P p = pgcode;
  184. for (;;) {
  185. char c;
  186. while ((c = pgm_read_byte(&p[i])) && c != '\n') i++;
  187. char cmd[i + 1];
  188. memcpy_P(cmd, p, i);
  189. cmd[i] = '\0';
  190. enqueue_one_now(cmd);
  191. if (!c) break;
  192. p += i + 1;
  193. }
  194. }
  195. /**
  196. * Send an "ok" message to the host, indicating
  197. * that a command was successfully processed.
  198. *
  199. * If ADVANCED_OK is enabled also include:
  200. * N<int> Line number of the command, if any
  201. * P<int> Planner space remaining
  202. * B<int> Block queue space remaining
  203. */
  204. void GCodeQueue::ok_to_send() {
  205. #if NUM_SERIAL > 1
  206. const int16_t pn = port[index_r];
  207. if (pn < 0) return;
  208. PORT_REDIRECT(pn);
  209. #endif
  210. if (!send_ok[index_r]) return;
  211. SERIAL_ECHOPGM(MSG_OK);
  212. #if ENABLED(ADVANCED_OK)
  213. char* p = command_buffer[index_r];
  214. if (*p == 'N') {
  215. SERIAL_ECHO(' ');
  216. SERIAL_ECHO(*p++);
  217. while (NUMERIC_SIGNED(*p))
  218. SERIAL_ECHO(*p++);
  219. }
  220. SERIAL_ECHOPGM(" P"); SERIAL_ECHO(int(BLOCK_BUFFER_SIZE - planner.movesplanned() - 1));
  221. SERIAL_ECHOPGM(" B"); SERIAL_ECHO(BUFSIZE - length);
  222. #endif
  223. SERIAL_EOL();
  224. }
  225. /**
  226. * Send a "Resend: nnn" message to the host to
  227. * indicate that a command needs to be re-sent.
  228. */
  229. void GCodeQueue::flush_and_request_resend() {
  230. #if NUM_SERIAL > 1
  231. const int16_t p = port[index_r];
  232. if (p < 0) return;
  233. PORT_REDIRECT(p);
  234. #endif
  235. SERIAL_FLUSH();
  236. SERIAL_ECHOPGM(MSG_RESEND);
  237. SERIAL_ECHOLN(last_N + 1);
  238. ok_to_send();
  239. }
  240. inline bool serial_data_available() {
  241. return false
  242. || MYSERIAL0.available()
  243. #if NUM_SERIAL > 1
  244. || MYSERIAL1.available()
  245. #endif
  246. ;
  247. }
  248. inline int read_serial(const uint8_t index) {
  249. switch (index) {
  250. case 0: return MYSERIAL0.read();
  251. #if NUM_SERIAL > 1
  252. case 1: return MYSERIAL1.read();
  253. #endif
  254. default: return -1;
  255. }
  256. }
  257. void GCodeQueue::gcode_line_error(PGM_P const err, const int8_t port) {
  258. PORT_REDIRECT(port);
  259. SERIAL_ERROR_START();
  260. serialprintPGM(err);
  261. SERIAL_ECHOLN(last_N);
  262. while (read_serial(port) != -1); // clear out the RX buffer
  263. flush_and_request_resend();
  264. serial_count[port] = 0;
  265. }
  266. FORCE_INLINE bool is_M29(const char * const cmd) { // matches "M29" & "M29 ", but not "M290", etc
  267. const char * const m29 = strstr_P(cmd, PSTR("M29"));
  268. return m29 && !NUMERIC(m29[3]);
  269. }
  270. /**
  271. * Get all commands waiting on the serial port and queue them.
  272. * Exit when the buffer is full or when no more characters are
  273. * left on the serial port.
  274. */
  275. void GCodeQueue::get_serial_commands() {
  276. static char serial_line_buffer[NUM_SERIAL][MAX_CMD_SIZE];
  277. static bool serial_comment_mode[NUM_SERIAL] = { false }
  278. #if ENABLED(PAREN_COMMENTS)
  279. , serial_comment_paren_mode[NUM_SERIAL] = { false }
  280. #endif
  281. ;
  282. #if ENABLED(BINARY_FILE_TRANSFER)
  283. if (card.flag.binary_mode) {
  284. /**
  285. * For binary stream file transfer, use serial_line_buffer as the working
  286. * receive buffer (which limits the packet size to MAX_CMD_SIZE).
  287. * The receive buffer also limits the packet size for reliable transmission.
  288. */
  289. binaryStream[card.transfer_port_index].receive(serial_line_buffer[card.transfer_port_index]);
  290. return;
  291. }
  292. #endif
  293. // If the command buffer is empty for too long,
  294. // send "wait" to indicate Marlin is still waiting.
  295. #if NO_TIMEOUTS > 0
  296. static millis_t last_command_time = 0;
  297. const millis_t ms = millis();
  298. if (length == 0 && !serial_data_available() && ELAPSED(ms, last_command_time + NO_TIMEOUTS)) {
  299. SERIAL_ECHOLNPGM(MSG_WAIT);
  300. last_command_time = ms;
  301. }
  302. #endif
  303. /**
  304. * Loop while serial characters are incoming and the queue is not full
  305. */
  306. while (length < BUFSIZE && serial_data_available()) {
  307. for (uint8_t i = 0; i < NUM_SERIAL; ++i) {
  308. int c;
  309. if ((c = read_serial(i)) < 0) continue;
  310. char serial_char = c;
  311. /**
  312. * If the character ends the line
  313. */
  314. if (serial_char == '\n' || serial_char == '\r') {
  315. // Start with comment mode off
  316. serial_comment_mode[i] = false;
  317. #if ENABLED(PAREN_COMMENTS)
  318. serial_comment_paren_mode[i] = false;
  319. #endif
  320. // Skip empty lines and comments
  321. if (!serial_count[i]) { thermalManager.manage_heater(); continue; }
  322. serial_line_buffer[i][serial_count[i]] = 0; // Terminate string
  323. serial_count[i] = 0; // Reset buffer
  324. char* command = serial_line_buffer[i];
  325. while (*command == ' ') command++; // Skip leading spaces
  326. char *npos = (*command == 'N') ? command : nullptr; // Require the N parameter to start the line
  327. if (npos) {
  328. bool M110 = strstr_P(command, PSTR("M110")) != nullptr;
  329. if (M110) {
  330. char* n2pos = strchr(command + 4, 'N');
  331. if (n2pos) npos = n2pos;
  332. }
  333. gcode_N = strtol(npos + 1, nullptr, 10);
  334. if (gcode_N != last_N + 1 && !M110)
  335. return gcode_line_error(PSTR(MSG_ERR_LINE_NO), i);
  336. char *apos = strrchr(command, '*');
  337. if (apos) {
  338. uint8_t checksum = 0, count = uint8_t(apos - command);
  339. while (count) checksum ^= command[--count];
  340. if (strtol(apos + 1, nullptr, 10) != checksum)
  341. return gcode_line_error(PSTR(MSG_ERR_CHECKSUM_MISMATCH), i);
  342. }
  343. else
  344. return gcode_line_error(PSTR(MSG_ERR_NO_CHECKSUM), i);
  345. last_N = gcode_N;
  346. }
  347. #if ENABLED(SDSUPPORT)
  348. // Pronterface "M29" and "M29 " has no line number
  349. else if (card.flag.saving && !is_M29(command))
  350. return gcode_line_error(PSTR(MSG_ERR_NO_CHECKSUM), i);
  351. #endif
  352. // Movement commands alert when stopped
  353. if (IsStopped()) {
  354. char* gpos = strchr(command, 'G');
  355. if (gpos) {
  356. switch (strtol(gpos + 1, nullptr, 10)) {
  357. case 0:
  358. case 1:
  359. #if ENABLED(ARC_SUPPORT)
  360. case 2:
  361. case 3:
  362. #endif
  363. #if ENABLED(BEZIER_CURVE_SUPPORT)
  364. case 5:
  365. #endif
  366. SERIAL_ECHOLNPGM(MSG_ERR_STOPPED);
  367. LCD_MESSAGEPGM(MSG_STOPPED);
  368. break;
  369. }
  370. }
  371. }
  372. #if DISABLED(EMERGENCY_PARSER)
  373. // Process critical commands early
  374. if (strcmp(command, "M108") == 0) {
  375. wait_for_heatup = false;
  376. #if HAS_LCD_MENU
  377. wait_for_user = false;
  378. #endif
  379. }
  380. if (strcmp(command, "M112") == 0) kill();
  381. if (strcmp(command, "M410") == 0) quickstop_stepper();
  382. #endif
  383. #if defined(NO_TIMEOUTS) && NO_TIMEOUTS > 0
  384. last_command_time = ms;
  385. #endif
  386. // Add the command to the queue
  387. _enqueue(serial_line_buffer[i], true
  388. #if NUM_SERIAL > 1
  389. , i
  390. #endif
  391. );
  392. }
  393. else if (serial_count[i] >= MAX_CMD_SIZE - 1) {
  394. // Keep fetching, but ignore normal characters beyond the max length
  395. // The command will be injected when EOL is reached
  396. }
  397. else if (serial_char == '\\') { // Handle escapes
  398. // if we have one more character, copy it over
  399. if ((c = read_serial(i)) >= 0 && !serial_comment_mode[i]
  400. #if ENABLED(PAREN_COMMENTS)
  401. && !serial_comment_paren_mode[i]
  402. #endif
  403. )
  404. serial_line_buffer[i][serial_count[i]++] = (char)c;
  405. }
  406. else { // it's not a newline, carriage return or escape char
  407. if (serial_char == ';') serial_comment_mode[i] = true;
  408. #if ENABLED(PAREN_COMMENTS)
  409. else if (serial_char == '(') serial_comment_paren_mode[i] = true;
  410. else if (serial_char == ')') serial_comment_paren_mode[i] = false;
  411. #endif
  412. else if (!serial_comment_mode[i]
  413. #if ENABLED(PAREN_COMMENTS)
  414. && ! serial_comment_paren_mode[i]
  415. #endif
  416. ) serial_line_buffer[i][serial_count[i]++] = serial_char;
  417. }
  418. } // for NUM_SERIAL
  419. } // queue has space, serial has data
  420. }
  421. #if ENABLED(SDSUPPORT)
  422. /**
  423. * Get commands from the SD Card until the command buffer is full
  424. * or until the end of the file is reached. The special character '#'
  425. * can also interrupt buffering.
  426. */
  427. inline void GCodeQueue::get_sdcard_commands() {
  428. static bool stop_buffering = false,
  429. sd_comment_mode = false
  430. #if ENABLED(PAREN_COMMENTS)
  431. , sd_comment_paren_mode = false
  432. #endif
  433. ;
  434. if (!IS_SD_PRINTING()) return;
  435. /**
  436. * '#' stops reading from SD to the buffer prematurely, so procedural
  437. * macro calls are possible. If it occurs, stop_buffering is triggered
  438. * and the buffer is run dry; this character _can_ occur in serial com
  439. * due to checksums, however, no checksums are used in SD printing.
  440. */
  441. if (length == 0) stop_buffering = false;
  442. uint16_t sd_count = 0;
  443. bool card_eof = card.eof();
  444. while (length < BUFSIZE && !card_eof && !stop_buffering) {
  445. const int16_t n = card.get();
  446. char sd_char = (char)n;
  447. card_eof = card.eof();
  448. if (card_eof || n == -1
  449. || sd_char == '\n' || sd_char == '\r'
  450. || ((sd_char == '#' || sd_char == ':') && !sd_comment_mode
  451. #if ENABLED(PAREN_COMMENTS)
  452. && !sd_comment_paren_mode
  453. #endif
  454. )
  455. ) {
  456. if (card_eof) {
  457. card.printingHasFinished();
  458. if (IS_SD_PRINTING())
  459. sd_count = 0; // If a sub-file was printing, continue from call point
  460. else {
  461. SERIAL_ECHOLNPGM(MSG_FILE_PRINTED);
  462. #if ENABLED(PRINTER_EVENT_LEDS)
  463. printerEventLEDs.onPrintCompleted();
  464. #if HAS_RESUME_CONTINUE
  465. inject_P(PSTR("M0 S"
  466. #if HAS_LCD_MENU
  467. "1800"
  468. #else
  469. "60"
  470. #endif
  471. ));
  472. #endif
  473. #endif // PRINTER_EVENT_LEDS
  474. }
  475. }
  476. else if (n == -1)
  477. SERIAL_ERROR_MSG(MSG_SD_ERR_READ);
  478. if (sd_char == '#') stop_buffering = true;
  479. sd_comment_mode = false; // for new command
  480. #if ENABLED(PAREN_COMMENTS)
  481. sd_comment_paren_mode = false;
  482. #endif
  483. // Skip empty lines and comments
  484. if (!sd_count) { thermalManager.manage_heater(); continue; }
  485. command_buffer[index_w][sd_count] = '\0'; // terminate string
  486. sd_count = 0; // clear sd line buffer
  487. _commit_command(false);
  488. }
  489. else if (sd_count >= MAX_CMD_SIZE - 1) {
  490. /**
  491. * Keep fetching, but ignore normal characters beyond the max length
  492. * The command will be injected when EOL is reached
  493. */
  494. }
  495. else {
  496. if (sd_char == ';') sd_comment_mode = true;
  497. #if ENABLED(PAREN_COMMENTS)
  498. else if (sd_char == '(') sd_comment_paren_mode = true;
  499. else if (sd_char == ')') sd_comment_paren_mode = false;
  500. #endif
  501. else if (!sd_comment_mode
  502. #if ENABLED(PAREN_COMMENTS)
  503. && ! sd_comment_paren_mode
  504. #endif
  505. ) command_buffer[index_w][sd_count++] = sd_char;
  506. }
  507. }
  508. }
  509. #endif // SDSUPPORT
  510. /**
  511. * Add to the circular command queue the next command from:
  512. * - The command-injection queue (injected_commands_P)
  513. * - The active serial input (usually USB)
  514. * - The SD card file being actively printed
  515. */
  516. void GCodeQueue::get_available_commands() {
  517. get_serial_commands();
  518. #if ENABLED(SDSUPPORT)
  519. get_sdcard_commands();
  520. #endif
  521. }
  522. /**
  523. * Get the next command in the queue, optionally log it to SD, then dispatch it
  524. */
  525. void GCodeQueue::advance() {
  526. // Process immediate commands
  527. if (process_injected_command()) return;
  528. // Return if the G-code buffer is empty
  529. if (!length) return;
  530. #if ENABLED(SDSUPPORT)
  531. if (card.flag.saving) {
  532. char* command = command_buffer[index_r];
  533. if (is_M29(command)) {
  534. // M29 closes the file
  535. card.closefile();
  536. SERIAL_ECHOLNPGM(MSG_FILE_SAVED);
  537. #if !defined(__AVR__) || !defined(USBCON)
  538. #if ENABLED(SERIAL_STATS_DROPPED_RX)
  539. SERIAL_ECHOLNPAIR("Dropped bytes: ", MYSERIAL0.dropped());
  540. #endif
  541. #if ENABLED(SERIAL_STATS_MAX_RX_QUEUED)
  542. SERIAL_ECHOLNPAIR("Max RX Queue Size: ", MYSERIAL0.rxMaxEnqueued());
  543. #endif
  544. #endif // !defined(__AVR__) || !defined(USBCON)
  545. ok_to_send();
  546. }
  547. else {
  548. // Write the string from the read buffer to SD
  549. card.write_command(command);
  550. if (card.flag.logging)
  551. gcode.process_next_command(); // The card is saving because it's logging
  552. else
  553. ok_to_send();
  554. }
  555. }
  556. else
  557. gcode.process_next_command();
  558. #else
  559. gcode.process_next_command();
  560. #endif // SDSUPPORT
  561. // The queue may be reset by a command handler or by code invoked by idle() within a handler
  562. if (length) {
  563. --length;
  564. if (++index_r >= BUFSIZE) index_r = 0;
  565. }
  566. }