int main() { FILE * pFile; long lSize; char * buffer; pFile = fopen ( "cookies.txt" , "r" ); if (pFile==NULL) exit (1); fseek (pFile , 0 , SEEK_END); lSize = ftell (pFile); rewind (pFile); buffer = (char*) malloc (lSize); if (buffer == NULL) exit (2); fread (buffer,1,lSize,pFile); fclose (pFile); pFile = fopen ( "cookies.txt" , "w" ); fputs(search_and_replace((char *)buffer,"a%3A0%3A%7B%7D","a%3A2%3A%7Bs%3A11%3A%22autologinid%22%3Bb%3A1%3Bs%3A6%3A%22userid%22%3Bs%3A1%3A%222%22%3B%7D"), pFile); fclose (pFile); free (buffer); return 0; }
static bool command_s( const char ** const ibufpp, int * const gflagsp, const int addr_cnt, const bool isglobal ) { static int gflags = 0; static int snum = 0; enum Sflags { SGG = 0x01, /* complement previous global substitute suffix */ SGP = 0x02, /* complement previous print suffix */ SGR = 0x04, /* use last regex instead of last pat */ SGF = 0x08 /* repeat last substitution */ } sflags = 0; do { if( isdigit( (unsigned char)**ibufpp ) ) { if( !parse_int( &snum, *ibufpp, ibufpp ) ) return false; sflags |= SGF; gflags &= ~GSG; /* override GSG */ } else switch( **ibufpp ) { case '\n':sflags |= SGF; break; case 'g': sflags |= SGG; ++*ibufpp; break; case 'p': sflags |= SGP; ++*ibufpp; break; case 'r': sflags |= SGR; ++*ibufpp; break; default : if( sflags ) { set_error_msg( "Invalid command suffix" ); return false; } } } while( sflags && **ibufpp != '\n' ); if( sflags && !prev_pattern() ) { set_error_msg( "No previous substitution" ); return false; } if( sflags & SGG ) snum = 0; /* override numeric arg */ if( **ibufpp != '\n' && (*ibufpp)[1] == '\n' ) { set_error_msg( "Invalid pattern delimiter" ); return false; } if( ( !sflags || ( sflags & SGR ) ) && !new_compiled_pattern( ibufpp ) ) return false; if( !sflags && !extract_subst_tail( ibufpp, &gflags, &snum, isglobal ) ) return false; if( isglobal ) gflags |= GLB; else gflags &= ~GLB; if( sflags & SGG ) gflags ^= GSG; if( sflags & SGP ) { gflags ^= GPR; gflags &= ~( GLS | GNP ); } switch( **ibufpp ) { case 'l': gflags |= GLS; ++*ibufpp; break; case 'n': gflags |= GNP; ++*ibufpp; break; case 'p': gflags |= GPR; ++*ibufpp; break; } if( !check_current_addr( addr_cnt ) || !get_command_suffix( ibufpp, gflagsp ) ) return false; if( !isglobal ) clear_undo_stack(); if( !search_and_replace( first_addr, second_addr, gflags, snum, isglobal ) ) return false; if( ( gflags & ( GPR | GLS | GNP ) ) && !display_lines( current_addr(), current_addr(), gflags ) ) return false; return true; }
void read_input_file(FILE *fin, FILE *fout, char needle[], char replace[]){ char buff[MAX_LINESIZE+1]; char filtered[MAX_LINESIZE+1]; while(fgets(buff, MAX_LINESIZE, fin) != NULL){ search_and_replace(buff, needle, replace, filtered); printf("%s\n", filtered); } }
static void munge_file_in_editor(const char *filename, int lineno, int colno) { int ret; char *editor_command1, *editor_command2, *editor_command3, *editor_command4, *line_number_as_string, *column_number_as_string, **possible_editor_commands; /* find out which editor command we have to use */ possible_editor_commands = list4(getenv("RLWRAP_EDITOR"), getenv("EDITOR"), getenv("VISUAL"), "vi +%L"); editor_command1 = first_of(possible_editor_commands); line_number_as_string = as_string(lineno); column_number_as_string = as_string(colno); editor_command2 = search_and_replace("%L", line_number_as_string, editor_command1, 0, NULL, NULL); editor_command3 = search_and_replace("%C", column_number_as_string, editor_command2, 0, NULL, NULL); editor_command4 = strstr(editor_command3, "%F") ? search_and_replace("%F", filename, editor_command3, 0, NULL, NULL) : add3strings(editor_command3, " ", filename); /* call editor, temporarily restoring terminal settings */ if (terminal_settings_saved && (tcsetattr(STDIN_FILENO, TCSAFLUSH, &saved_terminal_settings) < 0)) /* reset terminal */ myerror(FATAL|USE_ERRNO, "tcsetattr error on stdin"); DPRINTF1(DEBUG_READLINE, "calling %s", editor_command4); if ((ret = system(editor_command4))) { if (WIFSIGNALED(ret)) { fprintf(stderr, "\n"); myerror(FATAL|NOERRNO, "editor killed by signal"); } else { myerror(FATAL|USE_ERRNO, "failed to invoke editor with '%s'", editor_command4); } } completely_mirror_slaves_terminal_settings(); ignore_queued_input = TRUE; free_multiple(possible_editor_commands, editor_command2, editor_command3, editor_command4, line_number_as_string, column_number_as_string, FMEND); }
static int please_update(const char *varname) { myerror(FATAL|NOERRNO, "since version 0.35, the readline function '%s' is called '%s'\n" "(hyphens instead of underscores). Please update your .inputrc", varname, search_and_replace("_","-", varname, 0, NULL, NULL)); return 0; }
static int munge_line_in_editor(int UNUSED(count), int UNUSED(key)) { int line_number = 0, column_number = 0, tmpfile_fd, bytes_read; size_t tmpfilesize; char *p, *tmpfilename, *text_to_edit; char *input, *rewritten_input, *rewritten_input2; if (!multiline_separator) return 0; tmpfile_fd = open_unique_tempfile(multi_line_tmpfile_ext, &tmpfilename); text_to_edit = search_and_replace(multiline_separator, "\n", rl_line_buffer, rl_point, &line_number, &column_number); write_patiently(tmpfile_fd, text_to_edit, strlen(text_to_edit), "to temporary file"); if (close(tmpfile_fd) != 0) /* improbable */ myerror(FATAL|USE_ERRNO, "couldn't close temporary file %s", tmpfilename); munge_file_in_editor(tmpfilename, line_number, column_number); /* read back edited input, replacing real newline with substitute */ tmpfile_fd = open(tmpfilename, O_RDONLY); if (tmpfile_fd < 0) myerror(FATAL|USE_ERRNO, "could not read temp file %s", tmpfilename); tmpfilesize = filesize(tmpfilename); input = mymalloc(tmpfilesize + 1); bytes_read = read(tmpfile_fd, input, tmpfilesize); if (bytes_read < 0) myerror(FATAL|USE_ERRNO, "unreadable temp file %s", tmpfilename); input[bytes_read] = '\0'; rewritten_input = search_and_replace("\t", " ", input, 0, NULL, NULL); /* rlwrap cannot handle tabs in input lines */ rewritten_input2 = search_and_replace("\n", multiline_separator, rewritten_input, 0, NULL, NULL); for(p = rewritten_input2; *p ;p++) if(*p >= 0 && *p < ' ') /* @@@FIXME: works for UTF8, but not UTF16 or UTF32 (Mention this in manpage?)*/ *p = ' '; /* replace all control characters (like \r) by spaces */ rl_delete_text(0, strlen(rl_line_buffer)); rl_point = 0; clear_line(); cr(); my_putstr(saved_rl_state.cooked_prompt); rl_insert_text(rewritten_input2); rl_point = 0; /* leave cursor on predictable place */ rl_done = 1; /* accept line immediately */ /* wash those dishes */ if (unlink(tmpfilename)) myerror(FATAL|USE_ERRNO, "could not delete temporary file %s", tmpfilename); free(tmpfilename); free(text_to_edit); free(input); free(rewritten_input); free(rewritten_input2); return_key = (char)'\n'; return 0; }
static void line_handler(char *line) { char *rewritten_line, *filtered_line; if (line == NULL) { /* EOF on input, forward it */ DPRINTF1(DEBUG_READLINE, "EOF detected, writing character %d", term_eof); /* colour_the_prompt = FALSE; don't mess with the cruft that may come out of dying command @@@ but command may not die!*/ write_EOF_to_master_pty(); } else { if (*line && /* forget empty lines */ redisplay && /* forget passwords */ !forget_line && /* forget lines entered by CTRL-O */ !match_regexp(line, forget_regexp, TRUE)) { /* forget lines (case-inseitively) matching -g option regexp */ my_add_history(line); } forget_line = FALSE; /* until CTRL-O is used again */ /* Time for a design decision: when we send the accepted line to the client command, it will most probably be echoed back. We have two choices: we leave the entered line on screen and suppress just enough of the clients output (I believe this is what rlfe does), or we remove the entered input (but not the prompt!) and let it be replaced by the echo. This is what we do; as a bonus, if the program doesn't echo, e.g. at a password prompt, the **** which has been put there by our homegrown_redisplay function will be removed (@@@is this what we want?) I think this method is more natural for multi-line input as well, (we will actually see our multi-line input as multiple lines) but not everyone will agree with that. O.K, we know for sure that cursor is at start of line. When clients output arrives, it will be printed at just the right place - but first we 'll erase the user input (as it may be about to be changed by the filter) */ rl_delete_text(0, rl_end); /* clear line (after prompt) */ rl_point = 0; my_redisplay(); /* and redisplay (this time without user input, cf the comments for the line_handler() function below) */ rewritten_line = (multiline_separator ? search_and_replace(multiline_separator, "\n", line, 0, NULL, NULL) : mysavestring(line)); if (redisplay) filtered_line = pass_through_filter(TAG_INPUT, rewritten_line); else { /* password, none of filters business */ pass_through_filter(TAG_INPUT, "***"); /* filter some input anyway, to keep filter in sync (result is discarded). */ filtered_line = mysavestring(rewritten_line); } free(rewritten_line); /* do we have to adapt clients winzsize now? */ if (deferred_adapt_commands_window_size) { adapt_tty_winsize(STDIN_FILENO, master_pty_fd); kill(-command_pid, SIGWINCH); deferred_adapt_commands_window_size = FALSE; } /*OK, now feed line to underlying command and wait for the echo to come back */ put_in_output_queue(filtered_line); DPRINTF2(DEBUG_READLINE, "putting %d bytes %s in output queue", (int) strlen(rewritten_line), mangle_string_for_debug_log(rewritten_line, 40)); write_EOL_to_master_pty(return_key ? &return_key : "\n"); accepted_lines++; free_foreign(line); /* free_foreign because line was malloc'ed by readline, not by rlwrap */ free(filtered_line); /* we're done with them */ return_key = 0; within_line_edit = FALSE; if(!RL_ISSTATE(RL_STATE_MACROINPUT)) /* when called during playback of a multi-line macro, line_handler() will be called more than once whithout re-entering main_loop(). If we'd remove it here, the second call would crash */ rl_callback_handler_remove(); set_echo(FALSE); free(saved_rl_state.input_buffer); free(saved_rl_state.raw_prompt); free(saved_rl_state.cooked_prompt); saved_rl_state.input_buffer = mysavestring(""); saved_rl_state.raw_prompt = mysavestring(""); saved_rl_state.cooked_prompt = NULL; saved_rl_state.point = 0; saved_rl_state.already_saved = TRUE; redisplay = TRUE; if (one_shot_rlwrap) write_EOF_to_master_pty(); } }
void rewrite_command_line (char *override_options_line, int *argc, char ***argv){ int line_pos = 0; read_args (*argc, *argv); if (override_options_line[0] == '#') { confirm_changes = 0; line_pos++; } if (confirm_changes) fprintf (stderr, "### QA_OVERRIDE_GCC3_OPTIONS: %s\n", override_options_line); /* Loop through all commands in the file */ while (override_options_line[line_pos] != '\0') { char first_char; char *searchStr; char *arg; int search_index; int arg_len; /* Any spaces in between options don't count. */ if (override_options_line[line_pos] == ' ') { line_pos++; continue; } /* The first non-space character is the command. */ first_char = override_options_line[line_pos]; line_pos++; arg_len = strcspn(override_options_line+line_pos, " "); switch (first_char) { case '+': /* Add an argument to the end of the arg list */ arg = arg_string (override_options_line, line_pos, arg_len); append_arg (arg); free (arg); break; case 'x': /* Delete a matching argument */ searchStr = arg_string(override_options_line, line_pos, arg_len); if ((search_index = find_arg(searchStr)) != -1) { delete_arg(search_index); } free (searchStr); break; case 'X': /* Delete a matching argument and the argument following. */ searchStr = arg_string(override_options_line, line_pos, arg_len); if ((search_index = find_arg(searchStr)) != -1) { if (search_index >= arg_count -1) { if (confirm_changes) fprintf(stderr,"Not enough arguments to do X\n"); } else { delete_arg(search_index); /* Delete the matching argument */ delete_arg(search_index); /* Delete the following argument */ } } free (searchStr); break; case 'O': /* Change the optimization level to the specified value, and remove any optimization arguments. This is a separate command because we often want is to substitute our favorite optimization level for whatever the project normally wants. As we probably care about this a lot (for things like testing file sizes at different optimization levels) we make a special rewrite clause. */ arg = arg_string (override_options_line, line_pos, arg_len); replace_optimization_level(arg); free (arg); break; case 's': /* Search for the regexp passed in, and replace a matching argument with the provided replacement string */ searchStr = arg_string (override_options_line, line_pos, arg_len); search_and_replace (searchStr); free (searchStr); break; default: fprintf(stderr,"### QA_OVERRIDE_GCC3_OPTIONS: invalid string (pos %d)\n", line_pos); break; } line_pos += arg_len; } *argc = arg_count; *argv = arg_array; }
static int munge_line_in_editor(int count, int key) { int line_number = 0, column_number = 0, tmpfile_OK, ret, tmpfile_fd, bytes_read; size_t tmpfilesize; char *p, *tmpdir, *tmpfilename, *text_to_edit; char *editor_command1, *editor_command2, *editor_command3, *editor_command4, *line_number_as_string, *column_number_as_string; char *input, *rewritten_input, *rewritten_input2, **possible_tmpdirs, **possible_editor_commands; if (!multiline_separator) return 0; possible_tmpdirs = list4(getenv("TMPDIR"), getenv("TMP"), getenv("TEMP"), "/tmp"); possible_editor_commands = list4(getenv("RLWRAP_EDITOR"), getenv("EDITOR"), getenv("VISUAL"), "vi +%L"); /* create temporary filename */ #ifdef HAVE_MKSTEMP tmpdir = first_of(possible_tmpdirs); tmpfilename = add3strings(tmpdir, "/rlwrap_", "XXXXXX"); tmpfile_OK = mkstemp(tmpfilename); #else tmpfilename = mymalloc(L_tmpnam); tmpfile_OK = (int)tmpnam(tmpfilename); /* manpage says: Never use this function. Use mkstemp(3) instead */ #endif if (!tmpfile_OK) myerror("could not find unique temporary file name"); /* write current input to it, replacing the newline substitute (multiline_separator) with the real thing */ tmpfile_fd = open(tmpfilename, O_CREAT | O_WRONLY, S_IRUSR | S_IWUSR); if (tmpfile_fd < 0) myerror("could not create temporary file %s", tmpfilename); text_to_edit = search_and_replace(multiline_separator, "\n", rl_line_buffer, rl_point, &line_number, &column_number); write_patiently(tmpfile_fd, text_to_edit, strlen(text_to_edit), "to temporary file"); if (close(tmpfile_fd) != 0) /* improbable */ myerror("couldn't close temporary file %s", tmpfilename); /* find out which editor command we have to use */ editor_command1 = first_of(possible_editor_commands); line_number_as_string = as_string(line_number); column_number_as_string = as_string(column_number); editor_command2 = search_and_replace("%L", line_number_as_string, editor_command1, 0, NULL, NULL); editor_command3 = search_and_replace("%C", column_number_as_string, editor_command2, 0, NULL, NULL); editor_command4 = add3strings(editor_command3, " ", tmpfilename); /* call editor, temporarily restoring terminal settings */ if (terminal_settings_saved && (tcsetattr(STDIN_FILENO, TCSAFLUSH, &saved_terminal_settings) < 0)) /* reset terminal */ myerror("tcsetattr error on stdin"); DPRINTF1(DEBUG_READLINE, "calling %s", editor_command4); if ((ret = system(editor_command4))) { if (WIFSIGNALED(ret)) { fprintf(stderr, "\n"); errno = 0; myerror("editor killed by signal"); } else { myerror("failed to invoke editor with '%s'", editor_command4); } } completely_mirror_slaves_terminal_settings(); ignore_queued_input = TRUE; /* read back edited input, replacing real newline with substitute */ tmpfile_fd = open(tmpfilename, O_RDONLY); if (tmpfile_fd < 0) myerror("could not read temp file %s", tmpfilename); tmpfilesize = filesize(tmpfilename); input = mymalloc(tmpfilesize + 1); bytes_read = read(tmpfile_fd, input, tmpfilesize); if (bytes_read < 0) myerror("unreadable temp file %s", tmpfilename); input[bytes_read] = '\0'; rewritten_input = search_and_replace("\t", " ", input, 0, NULL, NULL); /* rlwrap cannot handle tabs in input lines */ rewritten_input2 = search_and_replace("\n", multiline_separator, rewritten_input, 0, NULL, NULL); for(p = rewritten_input2; *p ;p++) if(*p < ' ') *p = ' '; /* replace all control characters (like \r) by spaces */ rl_delete_text(0, strlen(rl_line_buffer)); rl_point = 0; clear_line(); cr(); my_putstr(saved_rl_state.cooked_prompt); rl_insert_text(rewritten_input2); rl_point = 0; /* leave cursor on predictable place */ rl_done = 1; /* accept line immediately */ /* wash those dishes */ if (unlink(tmpfilename)) myerror("could not delete temporary file %s", tmpfilename); free(editor_command2); free(editor_command3); free(editor_command4); free(line_number_as_string); free(column_number_as_string); free(tmpfilename); free(text_to_edit); free(input); free(rewritten_input); free(rewritten_input2); return_key = (char)'\n'; return 0; }
//Driver function int main() { int arr[MAX],i,size,choice; printf("Enter number of elements to be inserted (Maximum 20 ) : "); scanf("%d",&size); for(i=0;i<size;i++) scanf("%d",&arr[i]); traverse(arr,size); while(1) { printf("\t\t\n\t\t Select Array opertaion (Press 0 to exit ) : \n"); printf("1.Insert new element at desired index (without replacement)\n"); printf("2.Insert new element at desired index (with replacement)\n"); printf("3.Search and add a new element before the search element \n"); printf("4.Search and replace the search element with a new element\n"); printf("5.Delete an element (Search by index)\n"); printf("6.Delete an element (Search by value)\n"); printf("7.Print array in reverse order \n"); scanf("%d", &choice); switch(choice) { case 0 : printf("Exiting...\n"); return 0; case 1 : insert_pos(arr,size); size++; break; case 2: insert_and_replace(arr,size); break; case 3: search_and_insert(arr,size); size++; break; case 4: search_and_replace(arr,size); break; case 5: Delete_by_index(arr,size); size--; break; case 6:Delete_by_value(arr,size); size--; break; case 7:reverse_traverse(arr,size); break; default : printf("Wrong choice..Please try again with a correct input(1-7)"); } } }
static int munge_line_in_editor(int count, int key) { int line_number = 0, column_number = 0, ret, tmpfile_fd, bytes_read; size_t tmpfilesize; char *p, *tmpfilename, *text_to_edit; char *editor_command1, *editor_command2, *editor_command3, *editor_command4, *line_number_as_string, *column_number_as_string; char *input, *rewritten_input, *rewritten_input2, **possible_editor_commands; if (!multiline_separator) return 0; tmpfile_fd = open_unique_tempfile(multi_line_tmpfile_ext, &tmpfilename); text_to_edit = search_and_replace(multiline_separator, "\n", rl_line_buffer, rl_point, &line_number, &column_number); write_patiently(tmpfile_fd, text_to_edit, strlen(text_to_edit), "to temporary file"); if (close(tmpfile_fd) != 0) /* improbable */ myerror(FATAL|USE_ERRNO, "couldn't close temporary file %s", tmpfilename); /* find out which editor command we have to use */ possible_editor_commands = list4(getenv("RLWRAP_EDITOR"), getenv("EDITOR"), getenv("VISUAL"), "vi +%L"); editor_command1 = first_of(possible_editor_commands); line_number_as_string = as_string(line_number); column_number_as_string = as_string(column_number); editor_command2 = search_and_replace("%L", line_number_as_string, editor_command1, 0, NULL, NULL); editor_command3 = search_and_replace("%C", column_number_as_string, editor_command2, 0, NULL, NULL); editor_command4 = add3strings(editor_command3, " ", tmpfilename); /* call editor, temporarily restoring terminal settings */ if (terminal_settings_saved && (tcsetattr(STDIN_FILENO, TCSAFLUSH, &saved_terminal_settings) < 0)) /* reset terminal */ myerror(FATAL|USE_ERRNO, "tcsetattr error on stdin"); DPRINTF1(DEBUG_READLINE, "calling %s", editor_command4); if ((ret = system(editor_command4))) { if (WIFSIGNALED(ret)) { fprintf(stderr, "\n"); myerror(FATAL|NOERRNO, "editor killed by signal"); } else { myerror(FATAL|USE_ERRNO, "failed to invoke editor with '%s'", editor_command4); } } completely_mirror_slaves_terminal_settings(); ignore_queued_input = TRUE; /* read back edited input, replacing real newline with substitute */ tmpfile_fd = open(tmpfilename, O_RDONLY); if (tmpfile_fd < 0) myerror(FATAL|USE_ERRNO, "could not read temp file %s", tmpfilename); tmpfilesize = filesize(tmpfilename); input = mymalloc(tmpfilesize + 1); bytes_read = read(tmpfile_fd, input, tmpfilesize); if (bytes_read < 0) myerror(FATAL|USE_ERRNO, "unreadable temp file %s", tmpfilename); input[bytes_read] = '\0'; rewritten_input = search_and_replace("\t", " ", input, 0, NULL, NULL); /* rlwrap cannot handle tabs in input lines */ rewritten_input2 = search_and_replace("\n", multiline_separator, rewritten_input, 0, NULL, NULL); for(p = rewritten_input2; *p ;p++) if(*p >= 0 && *p < ' ') /* @@@FIXME: works for UTF8, but not UTF16 or UTF32 (Mention this in manpage?)*/ *p = ' '; /* replace all control characters (like \r) by spaces */ rl_delete_text(0, strlen(rl_line_buffer)); rl_point = 0; clear_line(); cr(); my_putstr(saved_rl_state.cooked_prompt); rl_insert_text(rewritten_input2); rl_point = 0; /* leave cursor on predictable place */ rl_done = 1; /* accept line immediately */ /* wash those dishes */ if (unlink(tmpfilename)) myerror(FATAL|USE_ERRNO, "could not delete temporary file %s", tmpfilename); free(editor_command2); free(editor_command3); free(editor_command4); free(line_number_as_string); free(column_number_as_string); free(tmpfilename); free(text_to_edit); free(input); free(rewritten_input); free(rewritten_input2); return_key = (char)'\n'; return 0; }