/* we process an entire file at a time... */ static void ex_hexdump_read_fd_write_stdout(Vstr_base *s1, Vstr_base *s2, int fd) { /* read/process/write loop */ while (TRUE) { int io_w_state = IO_OK; int io_r_state = io_get(s2, fd); if (io_r_state == IO_EOF) break; ex_hexdump_process(s1, s1->len, s2, 1, s2->len, prnt_high_chars, EX_MAX_W_DATA_INCORE, TRUE, FALSE); if (s1->conf->malloc_bad) errno = ENOMEM, err(EXIT_FAILURE, "adding data"); io_w_state = io_put(s1, STDOUT_FILENO); io_limit(io_r_state, fd, io_w_state, STDOUT_FILENO, s1); } /* write out all of the end of the file, * so the next file starts on a new line */ ex_hexdump_process_limit(s1, s2, 0); }
static void ex_cat_limit(Vstr_base *s1) { while ((s1->len >= EX_MAX_W_DATA_INCORE) || (s1->len >= EX_MAX_R_DATA_INCORE)) { if (io_put(s1, STDOUT_FILENO) == IO_BLOCK) io_block(-1, STDOUT_FILENO); } }
static void read_string(JSONSource* self) { Input* in = self->in; string_output_reset(self->buf); for (;;) { int ch = io_get(in); if (ch == '"') { break; } else if (ch == -1) { RAISE(EOF); } if (ch == '\\') { ch = io_get(in); switch (ch) { case '\\': case '/': case '"': break; case 'b': ch = '\b'; break; case 'f': ch = '\f'; break; case 'n': ch = '\n'; break; case 'r': ch = '\r'; break; case 't': ch = '\t'; break; case 'u': // Not supported yet; I'm not sure how to handle unicode characters etc RAISE(MALFORMED); default: RAISE(MALFORMED); } } io_put(self->buf, ch); } io_put(self->buf, 0); self->sval.ptr = (char*)string_output_data(self->buf, &self->sval.size); self->sval.size--; // Don't count the NULL-character }
static void value_run(void* udata, Coroutine* co, void* _arg) { coroutine_pop(co); SinkArg* arg = _arg; Output* out = arg->out; char cbuf[100]; int n; switch (arg->atom) { case RICH_NIL: io_writelit(out, "null"); break; case RICH_BOOL: if (*(bool*)arg->data) { io_writelit(out, "true"); } else { io_writelit(out, "false"); } break; case RICH_INT: n = snprintf(cbuf, sizeof(cbuf), "%ld", *(int64_t*)arg->data); io_write(out, cbuf, n); break; case RICH_FLOAT: n = snprintf(cbuf, sizeof(cbuf), "%lg", *(double*)arg->data); io_write(out, cbuf, n); break; case RICH_STRING: encode_string(out, arg->data); break; case RICH_ARRAY: io_put(out, '['); *(bool*)coroutine_push(co, &array_state, sizeof(bool)) = false; break; case RICH_MAP: io_put(out, '{'); *(bool*)coroutine_push(co, &map_state, sizeof(bool)) = false; break; default: RAISE(MALFORMED); } }
static void ex_phones_name_process_limit(Vstr_base *s1, Vstr_base *s2, unsigned int lim) { while (s2->len > lim) { int proc_data = ex_phones_name_process(s1, s2, !lim); if (!proc_data && (io_put(s1, STDOUT_FILENO) == IO_BLOCK)) io_block(-1, STDOUT_FILENO); } }
static void ex_sock_filter_process_limit(Vstr_base *s1, Vstr_base *s2, unsigned int lim) { while (s2->len > lim) { /* Finish processing read data (try writing if we need memory) */ int proc_data = ex_sock_filter_process(s1, s2); if (!proc_data && (io_put(s1, STDOUT_FILENO) == IO_BLOCK)) io_block(-1, STDOUT_FILENO); } }
static void ex_dir_list2html_process_limit(Vstr_base *s1, Vstr_base *s2, int *parsed_header, int *row_num, int fd) { while (s2->len) { /* Finish processing read data (try writing if we need memory) */ int proc_data = ex_dir_list2html_process(s1, s2, parsed_header, row_num); if (!proc_data && (io_put(s1, fd) == IO_BLOCK)) io_block(-1, fd); } }
static void array_run(void* udata, Coroutine* co, void* _arg) { SinkArg* arg = _arg; Output* out = arg->out; if (arg->atom == RICH_ENDARRAY) { io_put(out, ']'); coroutine_pop(co); return; } // Print comma between elements bool* comma = udata; if (*comma) { io_put(out, ','); } else { *comma = true; } // Delegate to the value sink coroutine_push(co, &value_state, 0); coroutine_run(co, arg); }
static void encode_string(Output* out, Bytes* str) { io_put(out, '"'); for (unsigned i = 0; i < str->size; i++) { char ch = ((char*)str->ptr)[i]; switch (ch) { case '/': case '\\': case '"': io_put(out, '\\'); io_put(out, ch); break; case '\b': io_writelit(out, "\\b"); break; case '\f': io_writelit(out, "\\f"); break; case '\n': io_writelit(out, "\\n"); break; case '\r': io_writelit(out, "\\r"); break; case '\t': io_writelit(out, "\\t"); break; default: if (isprint(ch)) { io_put(out, ch); } else { char cbuf[20]; snprintf(cbuf, sizeof(cbuf), "\\u00%02x", ch); io_write(out, cbuf, 6); } } } io_put(out, '"'); }
/* Keep reading on the file descriptor until there is no more data (ERR_EOF) * abort if there is an error reading or writing */ static void ex_cat_read_fd_write_stdout(Vstr_base *s1, int fd) { while (TRUE) { int io_w_state = IO_OK; int io_r_state = io_get(s1, fd); if (io_r_state == IO_EOF) break; io_w_state = io_put(s1, STDOUT_FILENO); io_limit(io_r_state, fd, io_w_state, STDOUT_FILENO, s1); } }
static void map_run(void* udata, Coroutine* co, void* _arg) { SinkArg* arg = _arg; Output* out = arg->out; if (arg->atom == RICH_ENDMAP) { io_put(out, '}'); coroutine_pop(co); return; } else if (arg->atom == RICH_KEY) { // Print comma between elements bool* comma = udata; if (*comma) { io_put(out, ','); } else { *comma = true; } encode_string(out, arg->data); io_put(out, ':'); } else { coroutine_push(co, &value_state, 0); coroutine_run(co, arg); } }
static void ex_hexdump_process_limit(Vstr_base *s1, Vstr_base *s2, unsigned int lim) { while (s2->len > lim) { /* Finish processing read data (try writing if we need memory) */ int proc_data = ex_hexdump_process(s1, s1->len, s2, 1, s2->len, prnt_high_chars, EX_MAX_W_DATA_INCORE, TRUE, !lim); if (s1->conf->malloc_bad) errno = ENOMEM, err(EXIT_FAILURE, "adding data"); if (!proc_data && (io_put(s1, STDOUT_FILENO) == IO_BLOCK)) io_block(-1, STDOUT_FILENO); } }
int main(void) { Vstr_base *s1 = hw_init(); vstr_add_cstr_ptr(s1, s1->len, "Hello"); vstr_add_cstr_ptr(s1, s1->len, " "); vstr_add_cstr_ptr(s1, s1->len, "World\n"); /* we are checking whether any of the above three functions failed here */ if (s1->conf->malloc_bad) errno = ENOMEM, err(EXIT_FAILURE, "Add string data"); /* loop until all data is output... */ while (io_put(s1, STDOUT_FILENO) != IO_NONE) {} exit (hw_exit(s1)); }
/* files are merged */ static void ex_phones_name_read_fd_write_stdout(Vstr_base *s1, Vstr_base *s2, int fd) { while (TRUE) { int io_w_state = IO_OK; int io_r_state = io_get(s2, fd); if (io_r_state == IO_EOF) break; ex_phones_name_process(s1, s2, FALSE); io_w_state = io_put(s1, 1); io_limit(io_r_state, fd, io_w_state, 1, s1); } }
static void ex_sock_filter_read_fd_write_stdout(Vstr_base *s1, Vstr_base *s2, int fd) { /* read/process/write loop */ while (TRUE) { int io_w_state = IO_OK; int io_r_state = io_get(s2, fd); if (io_r_state == IO_EOF) break; ex_sock_filter_process(s1, s2); io_w_state = io_put(s1, STDOUT_FILENO); io_limit(io_r_state, fd, io_w_state, STDOUT_FILENO, s1); } /* write out all of the end of the file, * so the next file starts on a new line */ ex_sock_filter_process_limit(s1, s2, 0); }
static void ex_dir_list2html_read_fd_write_fd(Vstr_base *s1, Vstr_base *s2, int rfd, int wfd) { int parsed_header[1] = {FALSE}; int row_num[1] = {0}; while (TRUE) { int io_w_state = IO_OK; int io_r_state = io_get(s2, rfd); if (io_r_state == IO_EOF) break; ex_dir_list2html_process(s1, s2, parsed_header, row_num); io_w_state = io_put(s1, wfd); io_limit(io_r_state, rfd, io_w_state, wfd, s1); } ex_dir_list2html_process_limit(s1, s2, parsed_header, row_num, wfd); }
static void unclosable_put(void* _self, char ch) { UnclosableOutput* self = _self; io_put(self->wrap, ch); }
void io_put_int8(Output* out, int8_t i) { io_put(out, (char)i); }
/** * Register query instruction handler function. * * Used to ask the system from the outside about interesting * information, like the current order or the current speed. * @param[in,out] order The order that specifies which register * (not real register) should be returned. */ void query_instruction(order_t *order) { // Extract the instrution code int instruction = (order->data[0] & 0xf0); order_t *current_order = 0; uint8_t current_order_size = 0; if (DEBUG_ENABLE) { debug_write_string_p(PSTR("order_functions.c : query_instruction()\n")); } switch (instruction) { case 0x10: // left wheel Speed io_obj_start(); io_put(motor_read_speed(WHEEL_LEFT)); io_obj_end(); break; case 0x20: // right wheel Speed io_obj_start(); io_put(motor_read_speed(WHEEL_RIGHT)); io_obj_end(); break; case 0x30: // number of Orders in the Queue io_obj_start(); io_put(queue_order_available() - 1); // -1 because Query will be counted io_obj_end(); break; case 0x40: // current Order // Get the normal order (just get_current_order() would return this priority order) if ((current_order = queue_get_current_normal_order())) { // Find out how long this order is current_order_size = order_size(current_order); } // Put the Size of the order as own object io_obj_start(); io_put(current_order_size); io_obj_end(); if (DEBUG_ENABLE) { debug_write_integer(PSTR("order_functions.c : query_instruction() : current_order_size = "), current_order_size); } // send the Order data if (current_order) { uint8_t i = 0; io_obj_start(); for (;i < current_order_size;i++) { io_put(current_order->data[i]); if (DEBUG_ENABLE) { debug_write_integer(PSTR("order_functions.c : query_instruction() : put = "), current_order->data[i]); } } io_obj_end(); } break; case 0x50: // left wheel time trigger value io_obj_start(); io_put(timer_t_trigger_counter[WHEEL_LEFT] >> 8); io_put(timer_t_trigger_counter[WHEEL_LEFT] & 0x00ff); io_obj_end(); break; case 0x60: // left wheel position trigger value io_obj_start(); io_put(irq_p_trigger_position[WHEEL_LEFT] >> 8); io_put(irq_p_trigger_position[WHEEL_LEFT] & 0x00ff); io_obj_end(); break; case 0x70: // right wheel time trigger value io_obj_start(); io_put(timer_t_trigger_counter[WHEEL_RIGHT] >> 8); io_put(timer_t_trigger_counter[WHEEL_RIGHT] & 0x00ff); io_obj_end(); break; case 0x80: // right wheel position trigger value io_obj_start(); io_put(irq_p_trigger_position[WHEEL_RIGHT] >> 8); io_put(irq_p_trigger_position[WHEEL_RIGHT] & 0x00ff); io_obj_end(); break; case 0x90: // running time io_obj_start(); io_put(timer_1s_counter >> 8); io_put(timer_1s_counter & 0x00ff); io_obj_end(); break; } // Remove the priority order from the Queue queue_clear_priority(); }