Changes in / [8d04f709:84b14e2] in mainline
- Files:
-
- 7 edited
Legend:
- Unmodified
- Added
- Removed
-
kernel/generic/include/string.h
r8d04f709 r84b14e2 87 87 extern void str_cpy(char *dest, size_t size, const char *src); 88 88 extern void str_ncpy(char *dest, size_t size, const char *src, size_t n); 89 extern void wstr_ nstr(char *dst, const wchar_t *src, size_t size);89 extern void wstr_to_str(char *dest, size_t size, const wchar_t *src); 90 90 91 91 extern char *str_chr(const char *str, wchar_t ch); -
kernel/generic/src/console/kconsole.c
r8d04f709 r84b14e2 289 289 290 290 char tmp[STR_BOUNDS(MAX_CMDLINE)]; 291 wstr_ nstr(tmp, current + beg, position - beg + 1);291 wstr_to_str(tmp, position - beg + 1, current + beg); 292 292 293 293 int found; … … 665 665 666 666 char cmdline[STR_BOUNDS(MAX_CMDLINE)]; 667 wstr_ nstr(cmdline, tmp, STR_BOUNDS(MAX_CMDLINE));667 wstr_to_str(cmdline, STR_BOUNDS(MAX_CMDLINE), tmp); 668 668 669 669 if ((!kcon) && (len == 4) && (str_lcmp(cmdline, "exit", 4) == 0)) -
kernel/generic/src/lib/string.c
r8d04f709 r84b14e2 537 537 * null-terminated and containing only complete characters. 538 538 * 539 * @param d st Destination buffer.539 * @param dest Destination buffer. 540 540 * @param count Size of the destination buffer (must be > 0). 541 541 * @param src Source string. … … 571 571 * have to be null-terminated. 572 572 * 573 * @param d st Destination buffer.573 * @param dest Destination buffer. 574 574 * @param count Size of the destination buffer (must be > 0). 575 575 * @param src Source string. … … 596 596 } 597 597 598 /** Copy NULL-terminated wide string to string 599 * 600 * Copy source wide string @a src to destination buffer @a dst. 601 * No more than @a size bytes are written. NULL-terminator is always 602 * written after the last succesfully copied character (i.e. if the 603 * destination buffer is has at least 1 byte, it will be always 604 * NULL-terminated). 605 * 606 * @param src Source wide string. 607 * @param dst Destination buffer. 608 * @param count Size of the destination buffer. 609 * 610 */ 611 void wstr_nstr(char *dst, const wchar_t *src, size_t size) 612 { 613 /* No space for the NULL-terminator in the buffer */ 614 if (size == 0) 615 return; 616 598 /** Convert wide string to string. 599 * 600 * Convert wide string @a src to string. The output is written to the buffer 601 * specified by @a dest and @a size. @a size must be non-zero and the string 602 * written will always be well-formed. 603 * 604 * @param dest Destination buffer. 605 * @param size Size of the destination buffer. 606 * @param src Source wide string. 607 */ 608 void wstr_to_str(char *dest, size_t size, const wchar_t *src) 609 { 617 610 wchar_t ch; 618 size_t src_idx = 0; 619 size_t dst_off = 0; 611 size_t src_idx; 612 size_t dest_off; 613 614 /* There must be space for a null terminator in the buffer. */ 615 ASSERT(size > 0); 616 617 src_idx = 0; 618 dest_off = 0; 620 619 621 620 while ((ch = src[src_idx++]) != 0) { 622 if (chr_encode(ch, d st, &dst_off, size) != EOK)621 if (chr_encode(ch, dest, &dest_off, size - 1) != EOK) 623 622 break; 624 623 } 625 626 if (dst_off >= size) 627 dst[size - 1] = 0; 628 else 629 dst[dst_off] = 0; 624 625 dest[dest_off] = '\0'; 630 626 } 631 627 -
uspace/app/bdsh/input.c
r8d04f709 r84b14e2 125 125 int i; 126 126 127 console_goto(fphone(stdout), ti->col0 + start, ti->row0); 127 console_goto(fphone(stdout), (ti->col0 + start) % ti->con_cols, 128 ti->row0 + (ti->col0 + start) / ti->con_cols); 128 129 printf("%ls", ti->buffer + start); 129 130 for (i = 0; i < pad; ++i) … … 134 135 static char *tinput_get_str(tinput_t *ti) 135 136 { 136 char *str; 137 138 str = malloc(STR_BOUNDS(ti->nc) + 1); 139 if (str == NULL) 140 return NULL; 141 142 wstr_nstr(str, ti->buffer, STR_BOUNDS(ti->nc) + 1); 143 144 return str; 137 return wstr_to_astr(ti->buffer); 145 138 } 146 139 147 140 static void tinput_position_caret(tinput_t *ti) 148 141 { 149 console_goto(fphone(stdout), ti->col0 + ti->pos, ti->row0); 142 console_goto(fphone(stdout), (ti->col0 + ti->pos) % ti->con_cols, 143 ti->row0 + (ti->col0 + ti->pos) / ti->con_cols); 144 } 145 146 /** Update row0 in case the screen could have scrolled. */ 147 static void tinput_update_origin(tinput_t *ti) 148 { 149 int width, rows; 150 151 width = ti->col0 + ti->nc; 152 rows = (width / ti->con_cols) + 1; 153 154 /* Update row0 if the screen scrolled. */ 155 if (ti->row0 + rows > ti->con_rows) 156 ti->row0 = ti->con_rows - rows; 150 157 } 151 158 … … 153 160 { 154 161 int i; 162 int new_width, new_height; 155 163 156 164 if (ti->nc == INPUT_MAX) 157 165 return; 158 166 159 if (ti->col0 + ti->nc >= ti->con_cols - 1) 160 return; 167 new_width = ti->col0 + ti->nc + 1; 168 if (new_width % ti->con_cols == 0) { 169 /* Advancing to new line. */ 170 new_height = (new_width / ti->con_cols) + 1; 171 if (new_height >= ti->con_rows) 172 return; /* Disallow text longer than 1 page for now. */ 173 } 161 174 162 175 for (i = ti->nc; i > ti->pos; --i) … … 169 182 170 183 tinput_display_tail(ti, ti->pos - 1, 0); 184 tinput_update_origin(ti); 171 185 tinput_position_caret(ti); 172 186 } … … 247 261 } 248 262 263 static void tinput_seek_vertical(tinput_t *ti, seek_dir_t dir) 264 { 265 if (dir == seek_forward) { 266 if (ti->pos + ti->con_cols <= ti->nc) 267 ti->pos = ti->pos + ti->con_cols; 268 } else { 269 if (ti->pos - ti->con_cols >= 0) 270 ti->pos = ti->pos - ti->con_cols; 271 } 272 273 tinput_position_caret(ti); 274 } 275 249 276 static void tinput_seek_max(tinput_t *ti, seek_dir_t dir) 250 277 { … … 306 333 tinput_set_str(ti, ti->history[ti->hpos]); 307 334 tinput_display_tail(ti, 0, pad); 335 tinput_update_origin(ti); 308 336 tinput_position_caret(ti); 309 337 } … … 330 358 ti->pos = 0; 331 359 ti->nc = 0; 360 ti->buffer[0] = '\0'; 332 361 333 362 while (true) { … … 348 377 tinput_seek_word(ti, seek_forward); 349 378 break; 379 case KC_UP: 380 tinput_seek_vertical(ti, seek_backward); 381 break; 382 case KC_DOWN: 383 tinput_seek_vertical(ti, seek_forward); 384 break; 350 385 } 351 386 } … … 389 424 390 425 done: 426 ti->pos = ti->nc; 427 tinput_position_caret(ti); 391 428 putchar('\n'); 392 429 -
uspace/app/edit/edit.c
r8d04f709 r84b14e2 102 102 #define ED_INFTY 65536 103 103 104 /** Maximum filename length that can be entered. */ 105 #define INFNAME_MAX_LEN 128 106 104 107 static void key_handle_unmod(console_event_t const *ev); 105 108 static void key_handle_ctrl(console_event_t const *ev); … … 331 334 } 332 335 333 #define INPUT_MAX_LEN 64334 335 336 /** Ask for a file name. */ 336 337 static char *filename_prompt(char const *prompt, char const *init_value) … … 338 339 console_event_t ev; 339 340 char *str; 340 wchar_t buffer[INPUT_MAX_LEN + 1]; 341 wchar_t buffer[INFNAME_MAX_LEN + 1]; 342 int max_len; 341 343 int nc; 342 344 bool done; … … 349 351 console_set_color(con, COLOR_WHITE, COLOR_BLACK, 0); 350 352 351 str_to_wstr(buffer, INPUT_MAX_LEN + 1, init_value); 353 max_len = min(INFNAME_MAX_LEN, scr_columns - 4 - str_length(prompt)); 354 str_to_wstr(buffer, max_len + 1, init_value); 352 355 nc = wstr_length(buffer); 353 356 done = false; … … 376 379 break; 377 380 default: 378 if (ev.c >= 32 && nc < INPUT_MAX_LEN) {381 if (ev.c >= 32 && nc < max_len) { 379 382 putchar(ev.c); 380 383 fflush(stdout); … … 388 391 389 392 buffer[nc] = '\0'; 390 391 str = malloc(STR_BOUNDS(wstr_length(buffer)) + 1); 392 if (str == NULL) 393 return NULL; 394 395 wstr_nstr(str, buffer, STR_BOUNDS(wstr_length(buffer)) + 1); 393 str = wstr_to_astr(buffer); 396 394 397 395 console_set_color(con, COLOR_BLACK, COLOR_WHITE, 0); -
uspace/lib/libc/generic/string.c
r8d04f709 r84b14e2 471 471 * null-terminated and containing only complete characters. 472 472 * 473 * @param d st Destination buffer.473 * @param dest Destination buffer. 474 474 * @param count Size of the destination buffer (must be > 0). 475 475 * @param src Source string. … … 505 505 * have to be null-terminated. 506 506 * 507 * @param d st Destination buffer.507 * @param dest Destination buffer. 508 508 * @param count Size of the destination buffer (must be > 0). 509 509 * @param src Source string. … … 537 537 * null-terminated and containing only complete characters. 538 538 * 539 * @param d st Destination buffer.539 * @param dest Destination buffer. 540 540 * @param count Size of the destination buffer. 541 541 * @param src Source string. … … 549 549 } 550 550 551 /** Copy NULL-terminated wide string to string 552 * 553 * Copy source wide string @a src to destination buffer @a dst. 554 * No more than @a size bytes are written. NULL-terminator is always 555 * written after the last succesfully copied character (i.e. if the 556 * destination buffer is has at least 1 byte, it will be always 557 * NULL-terminated). 558 * 559 * @param src Source wide string. 560 * @param dst Destination buffer. 561 * @param count Size of the destination buffer. 562 * 563 */ 564 void wstr_nstr(char *dst, const wchar_t *src, size_t size) 565 { 566 /* No space for the NULL-terminator in the buffer */ 567 if (size == 0) 568 return; 569 551 /** Convert wide string to string. 552 * 553 * Convert wide string @a src to string. The output is written to the buffer 554 * specified by @a dest and @a size. @a size must be non-zero and the string 555 * written will always be well-formed. 556 * 557 * @param dest Destination buffer. 558 * @param size Size of the destination buffer. 559 * @param src Source wide string. 560 */ 561 void wstr_to_str(char *dest, size_t size, const wchar_t *src) 562 { 570 563 wchar_t ch; 571 size_t src_idx = 0; 572 size_t dst_off = 0; 573 564 size_t src_idx; 565 size_t dest_off; 566 567 /* There must be space for a null terminator in the buffer. */ 568 assert(size > 0); 569 570 src_idx = 0; 571 dest_off = 0; 572 574 573 while ((ch = src[src_idx++]) != 0) { 575 if (chr_encode(ch, d st, &dst_off, size) != EOK)574 if (chr_encode(ch, dest, &dest_off, size - 1) != EOK) 576 575 break; 577 576 } 578 579 if (dst_off >= size) 580 dst[size - 1] = 0; 581 else 582 dst[dst_off] = 0; 583 } 577 578 dest[dest_off] = '\0'; 579 } 580 581 /** Convert wide string to new string. 582 * 583 * Convert wide string @a src to string. Space for the new string is allocated 584 * on the heap. 585 * 586 * @param src Source wide string. 587 * @return New string. 588 */ 589 char *wstr_to_astr(const wchar_t *src) 590 { 591 char dbuf[STR_BOUNDS(1)]; 592 char *str; 593 wchar_t ch; 594 595 size_t src_idx; 596 size_t dest_off; 597 size_t dest_size; 598 599 /* Compute size of encoded string. */ 600 601 src_idx = 0; 602 dest_size = 0; 603 604 while ((ch = src[src_idx++]) != 0) { 605 dest_off = 0; 606 if (chr_encode(ch, dbuf, &dest_off, STR_BOUNDS(1)) != EOK) 607 break; 608 dest_size += dest_off; 609 } 610 611 str = malloc(dest_size + 1); 612 if (str == NULL) 613 return NULL; 614 615 /* Encode string. */ 616 617 src_idx = 0; 618 dest_off = 0; 619 620 while ((ch = src[src_idx++]) != 0) { 621 if (chr_encode(ch, str, &dest_off, dest_size) != EOK) 622 break; 623 } 624 625 str[dest_size] = '\0'; 626 return str; 627 } 628 584 629 585 630 /** Convert string to wide string. 586 631 * 587 632 * Convert string @a src to wide string. The output is written to the 588 * buffer specified by @a dest and @a size, which must have non-zero589 * size. The outputwill always be null-terminated.633 * buffer specified by @a dest and @a dlen. @a dlen must be non-zero 634 * and the wide string written will always be null-terminated. 590 635 * 591 636 * @param dest Destination buffer. -
uspace/lib/libc/include/string.h
r8d04f709 r84b14e2 73 73 extern void str_append(char *dest, size_t size, const char *src); 74 74 75 extern void wstr_nstr(char *dst, const wchar_t *src, size_t size); 75 extern void wstr_to_str(char *dest, size_t size, const wchar_t *src); 76 extern char *wstr_to_astr(const wchar_t *src); 76 77 extern void str_to_wstr(wchar_t *dest, size_t dlen, const char *src); 77 78
Note:
See TracChangeset
for help on using the changeset viewer.