static int test_trim(ibuf s) { /* Test #1: Empty string. */ ibuf_clear(s); ibuf_trim(s); if (strcmp(ibuf_get(s), "") != 0) { debug("test_trim: 1: expected empty string, got: %s\n", ibuf_get(s)); return 1; } /* Test #2: Single space. */ ibuf_clear(s); ibuf_addchar(s, ' '); ibuf_trim(s); if (strcmp(ibuf_get(s), "") != 0) { debug("test_trim: 2: expected empty string, got: %s\n", ibuf_get(s)); return 2; } /* Test #3: "hello world" (no leading or trailing spaces) */ ibuf_clear(s); ibuf_add(s, "hello world"); ibuf_trim(s); if (strcmp(ibuf_get(s), "hello world") != 0) { debug("test_trim: 3: expected \"hello world\", got: %s\n", ibuf_get(s)); return 3; } /* Test #4: " hello world \t" (leading and trailing spaces) */ ibuf_clear(s); ibuf_add(s, " hello world \t"); ibuf_trim(s); if (strcmp(ibuf_get(s), "hello world") != 0) { debug("test_trim: 4: expected \"hello world\", got: %s\n", ibuf_get(s)); return 3; } debug("test_trim: Succeeded.\n"); return 0; }
int highlight_node(const char *filename, struct buffer *buf) { int ret; int length = 0; int lasttype = -1; struct ibuf *ibuf = ibuf_init(); struct tokenizer *t = tokenizer_init(); if (tokenizer_set_file(t, filename, buf->language) == -1) { if_print_message("%s:%d tokenizer_set_file error", __FILE__, __LINE__); return -1; } while ((ret = tokenizer_get_token(t)) > 0) { enum tokenizer_type e = tokenizer_get_packet_type(t); /*if_print_message ( "TOKEN(%d:%s)\n", e, tokenizer_get_printable_enum ( e ) ); */ if (e == TOKENIZER_NEWLINE) { sbpush(buf->tlines, strdup(ibuf_get(ibuf))); if (length > buf->max_width) buf->max_width = length; length = 0; lasttype = -1; ibuf_clear(ibuf); } else { const char *tok_data = tokenizer_get_data(t); enum hl_group_kind hlg = hlg_from_tokenizer_type(e, tok_data); if (hlg == HLG_LAST) { logger_write_pos(logger, __FILE__, __LINE__, "Bad hlg_type for '%s', e==%d\n", tok_data, e); hlg = HLG_TEXT; } /* Set the highlight group type */ add_type(ibuf, &lasttype, hlg); /* Add the text and bump our length */ length += ibuf_add(ibuf, tok_data); } } ibuf_free(ibuf); tokenizer_destroy(t); return 0; }
void filedlg_clear(struct filedlg *fd) { int i; ibuf_clear(fd->G_line_number); for (i = 0; i < sbcount(fd->buf->files); i++) free(fd->buf->files[i]); sbfree(fd->buf->files); fd->buf->files = NULL; fd->buf->max_width = 0; fd->buf->sel_line = 0; fd->buf->sel_col = 0; fd->buf->sel_rline = 0; }
static int test_dup(ibuf s) { ibuf t; /* Test duplicating a string. */ ibuf_add(s, "test string 1"); t = ibuf_dup(s); if (t == NULL) { debug("test_dup: ibuf_dup (attempt #1) returned NULL\n"); return 1; } if (strcmp(ibuf_get(s), ibuf_get(t)) != 0) { debug("test_dup: Strings mismatched: \"%s\" != \"%s\"\n", ibuf_get(s), ibuf_get(t)); return 1; } ibuf_free(t); /* Corner case: duplicate an empty string */ ibuf_clear(s); t = ibuf_dup(s); if (t == NULL) { debug("test_dup: ibuf_dup (attempt #2) returned NULL\n"); return 1; } if (strcmp(ibuf_get(t), "") != 0) { debug("test_dup: Expected empty string, got: %s\n", ibuf_get(t)); return 1; } ibuf_free(t); debug("test_dup: Succeeded.\n"); return 0; }
/* Please forgive me for adding all the comment below. This function * has some strange bahaviors that I thought should be well explained. */ void rlctx_send_user_command(char *line) { char *cline; int length; char *rline_prompt; tgdb_request_ptr request_ptr; if (line == NULL) { /* NULL line means rl_callback_read_char received EOF */ ibuf_add(current_line, "quit"); } else { /* Add the line passed in to the current line */ ibuf_add(current_line, line); } /* Get current line, and current line length */ cline = ibuf_get(current_line); length = ibuf_length(current_line); /* Check to see if the user is escaping the line, to use a * multi line command. If so, return so that the user can * continue the command. This data should not go into the history * buffer, or be sent to gdb yet. * * Also, notice the length > 0 condition. (length == 0) can happen * when the user simply hits Enter on the keyboard. */ if (length > 0 && cline[length - 1] == '\\') { /* The \ char is for continuation only, it is not meant to be sent * to GDB as a character. */ ibuf_delchar(current_line); /* Only need to change the prompt the first time the \ char is used. * Doing it a second time would erase the real rline_last_prompt, * since the prompt has already been set to "". */ if (!rline_last_prompt) { rline_get_prompt(rline, &rline_prompt); rline_last_prompt = strdup(rline_prompt); /* GDB set's the prompt to nothing when doing continuation. * This is here just for compatibility. */ rline_set_prompt(rline, ""); } return; } /* If here, a full command has been sent. Restore the prompt. */ if (rline_last_prompt) { rline_set_prompt(rline, rline_last_prompt); free(rline_last_prompt); rline_last_prompt = NULL; } /* Don't add the enter command to the history */ if (length > 0) rline_add_history(rline, cline); request_ptr = tgdb_request_run_console_command(tgdb, cline); if (!request_ptr) logger_write_pos(logger, __FILE__, __LINE__, "rlctx_send_user_command\n"); /* Send this command to TGDB */ handle_request(tgdb, request_ptr); ibuf_clear(current_line); }
int filedlg_recv_char(struct filedlg *fd, int key, char *file, int last_key_pressed) { /* Initialize size variables */ int height = swin_getmaxy(fd->win); filedlg_display(fd); switch (key) { case 'q': return -1; /* Vertical scrolling */ case CGDB_KEY_DOWN: case 'j': filedlg_vscroll(fd, 1); break; case CGDB_KEY_NPAGE: case CGDB_KEY_CTRL_F: /* VI-style page down */ filedlg_vscroll(fd, height - 1); break; case CGDB_KEY_CTRL_D: /* VI-style 1/2 page down */ filedlg_vscroll(fd, height / 2); break; case CGDB_KEY_CTRL_U: /* VI-style 1/2 page up */ filedlg_vscroll(fd, -height / 2); break; case CGDB_KEY_UP: case 'k': filedlg_vscroll(fd, -1); break; case CGDB_KEY_PPAGE: case CGDB_KEY_CTRL_B: /* VI-style page up */ filedlg_vscroll(fd, -(height - 1)); break; /* Horizontal scrolling */ case CGDB_KEY_RIGHT: case 'l': filedlg_hscroll(fd, 1); break; case CGDB_KEY_LEFT: case 'h': filedlg_hscroll(fd, -1); break; case '/': case '?': regex_direction = ('/' == key); /* Capturing regular expressions */ filedlg_search_regex_init(fd); capture_regex(fd); break; case 'n': filedlg_search_regex(fd, regex_line, 2, regex_direction, 1); break; case 'N': filedlg_search_regex(fd, regex_line, 2, !regex_direction, 1); break; /* User selected a file */ case '\n': case '\r': case CGDB_KEY_CTRL_M: strcpy(file, fd->buf->files[fd->buf->sel_line]); return 1; case 'g': /* beginning of file */ if (last_key_pressed == 'g') filedlg_set_sel_line(fd, 0); break; case 'G': { /* end of file, or a line number*/ int lineno = -1, result; result = cgdb_string_to_int(ibuf_get(fd->G_line_number), &lineno); if (result == 0) { filedlg_set_sel_line(fd, lineno -1); } break; } default: break; } /* Store digits into G_line_number for 'G' command. */ if (key >= '0' && key <= '9') { ibuf_addchar(fd->G_line_number, key); } else { ibuf_clear(fd->G_line_number); } filedlg_display(fd); return 0; }
/* source_input: Handles user input to the source window. * ------------- * * sview: Source viewer object * key: Keystroke received. */ static void source_input(struct sviewer *sview, int key) { switch (key) { case CGDB_KEY_UP: case 'k': { /* VI-style up-arrow */ int lineno = 1; cgdb_string_to_int(ibuf_get(G_line_number), &lineno); source_vscroll(sview, -lineno); break; } case CGDB_KEY_DOWN: case 'j': { /* VI-style down-arrow */ int lineno = 1; cgdb_string_to_int(ibuf_get(G_line_number), &lineno); source_vscroll(sview, lineno); break; } case CGDB_KEY_LEFT: case 'h': source_hscroll(sview, -1); break; case CGDB_KEY_RIGHT: case 'l': source_hscroll(sview, 1); break; case CGDB_KEY_CTRL_U: /* VI-style 1/2 page up */ source_vscroll(sview, -(get_src_height() / 2)); break; case CGDB_KEY_PPAGE: case CGDB_KEY_CTRL_B: /* VI-style page up */ source_vscroll(sview, -(get_src_height() - 1)); break; case CGDB_KEY_CTRL_D: /* VI-style 1/2 page down */ source_vscroll(sview, (get_src_height() / 2)); break; case CGDB_KEY_NPAGE: case CGDB_KEY_CTRL_F: /* VI-style page down */ source_vscroll(sview, get_src_height() - 1); break; case 'g': /* beginning of file */ if (last_key_pressed == 'g') source_set_sel_line(sview, 1); break; case 'G': { /* end of file or a line number */ int lineno = -1; cgdb_string_to_int(ibuf_get(G_line_number), &lineno); source_set_sel_line(sview, lineno); break; } case '=': /* inc window by 1 */ increase_win_height(0); break; case '-': /* dec window by 1 */ decrease_win_height(0); break; case '+': increase_win_height(1); break; case '_': decrease_win_height(1); break; case 'o': /* Causes file dialog to be opened */ { extern int kui_input_acceptable; kui_input_acceptable = 0; tgdb_request_inferiors_source_files(tgdb); } break; case ' ': { enum tgdb_breakpoint_action t = TGDB_BREAKPOINT_ADD; toggle_breakpoint(sview, t); } break; case 't': { enum tgdb_breakpoint_action t = TGDB_TBREAKPOINT_ADD; toggle_breakpoint(sview, t); } break; default: break; } /* Store digits into G_line_number for 'G' command. */ if (key >= '0' && key <= '9') { ibuf_addchar(G_line_number, key); } else { ibuf_clear(G_line_number); } /* Some extended features that are set by :set sc */ if_draw(); }
static int highlight_node ( struct list_node *node ) { struct tokenizer *t = tokenizer_init (); int ret; struct ibuf *ibuf = ibuf_init (); ibuf_addchar ( ibuf, HL_CHAR ); ibuf_addchar ( ibuf, HLG_TEXT ); /* Initialize */ node->buf.length = 0; node->buf.tlines = NULL; node->buf.max_width = 0; if ( tokenizer_set_file ( t, node->path, node->language ) == -1 ) { if_print_message ("%s:%d tokenizer_set_file error", __FILE__, __LINE__); return -1; } while ( ( ret = tokenizer_get_token ( t ) ) > 0 ) { enum tokenizer_type e = tokenizer_get_packet_type ( t ); /*if_print_message ( "TOKEN(%d:%s)\n", e, tokenizer_get_printable_enum ( e ) );*/ switch ( e ) { case TOKENIZER_KEYWORD: ibuf_addchar ( ibuf, HL_CHAR ); ibuf_addchar ( ibuf, HLG_KEYWORD ); ibuf_add ( ibuf, tokenizer_get_data ( t ) ); ibuf_addchar ( ibuf, HL_CHAR ); ibuf_addchar ( ibuf, HLG_TEXT ); break; case TOKENIZER_TYPE: ibuf_addchar ( ibuf, HL_CHAR ); ibuf_addchar ( ibuf, HLG_TYPE ); ibuf_add ( ibuf, tokenizer_get_data ( t ) ); ibuf_addchar ( ibuf, HL_CHAR ); ibuf_addchar ( ibuf, HLG_TEXT ); break; case TOKENIZER_LITERAL: ibuf_addchar ( ibuf, HL_CHAR ); ibuf_addchar ( ibuf, HLG_LITERAL ); ibuf_add ( ibuf, tokenizer_get_data ( t ) ); ibuf_addchar ( ibuf, HL_CHAR ); ibuf_addchar ( ibuf, HLG_TEXT ); break; case TOKENIZER_NUMBER: ibuf_add ( ibuf, tokenizer_get_data ( t ) ); break; case TOKENIZER_COMMENT: ibuf_addchar ( ibuf, HL_CHAR ); ibuf_addchar ( ibuf, HLG_COMMENT ); ibuf_add ( ibuf, tokenizer_get_data ( t ) ); ibuf_addchar ( ibuf, HL_CHAR ); ibuf_addchar ( ibuf, HLG_TEXT ); break; case TOKENIZER_DIRECTIVE: ibuf_addchar ( ibuf, HL_CHAR ); ibuf_addchar ( ibuf, HLG_DIRECTIVE ); ibuf_add ( ibuf, tokenizer_get_data ( t ) ); ibuf_addchar ( ibuf, HL_CHAR ); ibuf_addchar ( ibuf, HLG_TEXT ); break; case TOKENIZER_TEXT: ibuf_add ( ibuf, tokenizer_get_data ( t ) ); break; case TOKENIZER_NEWLINE: node->buf.length++; node->buf.tlines = realloc ( node->buf.tlines, sizeof ( char *) * node->buf.length ); node->buf.tlines[node->buf.length-1] = strdup ( ibuf_get ( ibuf ) ); if ( ibuf_length ( ibuf ) > node->buf.max_width ) node->buf.max_width = ibuf_length ( ibuf ); ibuf_clear ( ibuf ); ibuf_addchar ( ibuf, HL_CHAR ); ibuf_addchar ( ibuf, HLG_TEXT ); break; case TOKENIZER_ERROR: ibuf_add ( ibuf, tokenizer_get_data ( t ) ); break; default: return -1; break; } } return 0; }
int a2_handle_data( struct annotate_two *a2, struct state_machine *sm, const char *data, const size_t size, char *gui_data, size_t *gui_size, struct tgdb_list *command_list){ int i, counter = 0; /* track state to find next file and line number */ for(i = 0; i < size; ++i){ switch(data[i]){ /* Ignore all car returns outputted by gdb */ case '\r': break; case '\n': switch(sm->tgdb_state){ case DATA: sm->tgdb_state = NEW_LINE; break; case NEW_LINE: sm->tgdb_state = NEW_LINE; data_process(a2, '\n', gui_data, &counter, command_list); break; case CONTROL_Z: sm->tgdb_state = DATA; data_process(a2, '\n', gui_data, &counter, command_list); data_process(a2, '\032', gui_data, &counter, command_list); break; case ANNOTATION: /* Found an annotation */ sm->tgdb_state = NL_DATA; tgdb_parse_annotation(a2, ibuf_get ( sm->tgdb_buffer ), ibuf_length ( sm->tgdb_buffer ), command_list); ibuf_clear ( sm->tgdb_buffer ); break; case NL_DATA: sm->tgdb_state = NEW_LINE; break; default: logger_write_pos ( logger, __FILE__, __LINE__, "Bad state transition"); break; } /* end switch */ break; case '\032': switch(sm->tgdb_state){ case DATA: sm->tgdb_state = DATA; data_process(a2, '\032', gui_data, &counter, command_list); break; case NEW_LINE: sm->tgdb_state = CONTROL_Z; break; case NL_DATA: sm->tgdb_state = CONTROL_Z; break; case CONTROL_Z: sm->tgdb_state = ANNOTATION; break; case ANNOTATION: ibuf_addchar ( sm->tgdb_buffer, data[i] ); break; default: logger_write_pos ( logger, __FILE__, __LINE__, "Bad state transition"); break; } /* end switch */ break; default: switch(sm->tgdb_state){ case DATA: data_process(a2, data[i], gui_data, &counter, command_list); break; case NL_DATA: sm->tgdb_state = DATA; data_process(a2, data[i], gui_data, &counter, command_list); break; case NEW_LINE: sm->tgdb_state = DATA; data_process(a2, '\n', gui_data, &counter, command_list); data_process(a2, data[i], gui_data, &counter, command_list); break; case CONTROL_Z: sm->tgdb_state = DATA; data_process(a2, '\n', gui_data, &counter, command_list); data_process(a2, '\032', gui_data, &counter, command_list); data_process(a2, data[i], gui_data, &counter, command_list); break; case ANNOTATION: ibuf_addchar ( sm->tgdb_buffer, data[i] ); break; default: logger_write_pos ( logger, __FILE__, __LINE__, "Bad state transition"); break; } /* end switch */ break; } /* end switch */ } /* end for */ gui_data[counter] = '\0'; *gui_size = counter; return 0; }