int main(int argc, char *argv[]) { if(argc <= 1){ show_usage(); return 0; } //surpress stderr, because dblib.c display ugly error message freopen("/dev/null", "w", stderr); dbinfo = (struct dbconfig *)malloc(sizeof(struct dbconfig)); if (!set_cmd_option( argc, argv, dbinfo)){ return 0; } if (!connect_db(dbinfo)){ return 0; } //set hisotry file path history_file = (char *)malloc(256); strcpy(history_file, getenv("HOME")); strcat(history_file, MSSQL_HISTORY_FILE_NAME); read_history(history_file); //read history for incremental search (ctrl + r) rl_startup_hook = my_startup; //set up readline with original method binding my_readline(); }
void tu5_17str_echo(int sockfd) { long arg1 = 0; long arg2 = 0; ssize_t n; char line[MAXLINE]; while (1) { if ( (n = my_readline(sockfd, line, MAXLINE)) == 0) { return ; } if (sscanf(line, "%ld%ld", &arg1, &arg2) == 2) { snprintf(line, sizeof(line), "%ld\n", arg1 + arg2); } else { snprintf(line, sizeof(line), "input error\n"); } n = strlen(line); my_writen(sockfd, line, n); } }
char *my_get_next_line(int fd) { static char *buffer; char *line; int i; if ((buffer == NULL && (buffer = my_read_dup(fd, SREAD)) == NULL) || (i = my_check_line(BLANK, buffer)) < 0 || fd < 0) return (NULL); while (i != 1) { if ((line = my_strdup(buffer)) == NULL || (i == 0 && (buffer = my_readline(fd, buffer, SREAD)) == NULL) || (i = my_check_line(line, buffer)) < 0) return (NULL); if (i == 0 && my_strcmp(buffer, line) == 0) { free(buffer); return (NULL); } free(line); } if ((line = my_get_line(buffer)) == NULL || (buffer = my_cut_line(buffer)) == NULL || (i = my_strlen(buffer)) < 0) return (NULL); return (line); }
static int lua_readline(lua_State * l, char *b, const char *prompt) { if (my_readline) { return my_readline(l, b, LUA_MAXINPUT, prompt); } else { return default_readline(l, b, prompt); } }
int vsh(int ac, char **av, char **env) { t_shell shell; char *str; (void)ac; (void)av; init_program(&shell, env); while ((shell.flag & SH_EXIT) && (str = my_readline(&shell, generate_prompt()))) { lexor_and_parsor(str, &shell); main_execution(&shell); } exit_42sh(&shell); return (EXIT_SUCCESS); }
int my_read(t_utils *utils) { char *buffer; signal(SIGINT, &my_handler); create_prompt(utils); while ((buffer = my_readline(g_prompt, utils))) { if (main_loop(buffer, utils) == 42) { free(buffer); return (42); } create_prompt(utils); get_path(utils); free(buffer); } write(1, "\n", 1); return (0); }
static int pushline (lua_State *L, int firstline) { char buffer[LUA_MAXINPUT]; char *b; size_t l; const char *prmt = get_prompt(L, firstline); pthread_mutex_unlock(&gsl_shell->exec_mutex); b = my_readline(L, buffer, prmt); pthread_mutex_lock(&gsl_shell->exec_mutex); if (b == NULL) return 0; /* no input */ l = strlen(b); if (l > 0 && b[l-1] == '\n') /* line ends with newline? */ b[l-1] = '\0'; /* remove it */ lua_pushstring(L, b); my_freeline(L, b); return 1; }
int main(int argc, char * const argv[]) { int r, c, long_optind = 0, err = 0; char *line; int cargc; char *cargv[260]; sc_context_param_t ctx_param; int lcycle = SC_CARDCTRL_LIFECYCLE_ADMIN; printf("OpenSC Explorer version %s\n", sc_get_version()); while (1) { c = getopt_long(argc, argv, "r:c:vwm:", options, &long_optind); if (c == -1) break; if (c == '?') util_print_usage_and_die(app_name, options, option_help); switch (c) { case 'r': opt_reader = optarg; break; case 'c': opt_driver = optarg; break; case 'w': opt_wait = 1; break; case 'v': verbose++; break; case 'm': opt_startfile = optarg; break; } } memset(&ctx_param, 0, sizeof(ctx_param)); ctx_param.ver = 0; ctx_param.app_name = app_name; r = sc_context_create(&ctx, &ctx_param); if (r) { fprintf(stderr, "Failed to establish context: %s\n", sc_strerror(r)); return 1; } if (verbose > 1) { ctx->debug = verbose; ctx->debug_file = stderr; } if (opt_driver != NULL) { err = sc_set_card_driver(ctx, opt_driver); if (err) { fprintf(stderr, "Driver '%s' not found!\n", opt_driver); err = 1; goto end; } } err = util_connect_card(ctx, &card, opt_reader, opt_wait, 0); if (err) goto end; if (opt_startfile) { if(*opt_startfile) { char startpath[1024]; char *args[] = { startpath }; strncpy(startpath, opt_startfile, sizeof(startpath)-1); r = do_cd(1, args); if (r) { printf("unable to select file %s: %s\n", opt_startfile, sc_strerror(r)); return -1; } } } else { sc_format_path("3F00", ¤t_path); r = sc_select_file(card, ¤t_path, ¤t_file); if (r) { printf("unable to select MF: %s\n", sc_strerror(r)); return 1; } } r = sc_card_ctl(card, SC_CARDCTL_LIFECYCLE_SET, &lcycle); if (r && r != SC_ERROR_NOT_SUPPORTED) printf("unable to change lifecycle: %s\n", sc_strerror(r)); while (1) { struct command *cmd; char prompt[3*SC_MAX_PATH_STRING_SIZE]; sprintf(prompt, "OpenSC [%s]> ", path_to_filename(¤t_path, '/')); line = my_readline(prompt); if (line == NULL) break; cargc = parse_line(line, cargv, DIM(cargv)); if (cargc < 1) continue; for (r=cargc; r < (int)DIM(cargv); r++) cargv[r] = ""; cmd = ambiguous_match(cmds, cargv[0]); if (cmd == NULL) { do_help(0, NULL); } else { cmd->func(cargc-1, cargv+1); } } end: die(err); return 0; /* not reached */ }
int main( int argc, char **argv ) { int i ; ASBiDirElem *curr; char *command; action_t *a; ASWinCommandState WinCommandState ; InitMyApp (CLASS_WINCOMMAND, argc, argv, NULL, NULL, OPTION_SINGLE|OPTION_RESTART ); ConnectX( ASDefaultScr, 0 ); ASBiDirList *operations = create_asbidirlist( NULL ); /* Initialize State */ memset( &WinCommandState, 0x00, sizeof(WinCommandState)); /* Traverse arguments */ for( i = 1 ; i< argc ; ++i) { if(argv[i] == NULL) continue; /* If it's a flag */ if(argv[i][0] == '-') { switch( set_WinCommandParam( &WinCommandState, argv[i], (i+1<argc)?argv[i+1]:NULL ) ) { case ASWC_BadParam : case ASWC_BadVal : fprintf( stderr, "bad parameter [%s]\n", argv[i] ); break; case ASWC_Ok_ValUsed : ++i; case ASWC_Ok_ValUnused : break ; } }else { LOCAL_DEBUG_OUT("Adding operation: %s", argv[i]); append_bidirelem(operations, argv[i]); } } if( WinCommandState.pattern == NULL) WinCommandState.pattern = mystrdup(DEFAULT_PATTERN); if( operations->count > 0 ) { ascom_init(); ascom_update_winlist(); /* execute default_handlers */ for( curr = operations->head; curr != NULL; curr = curr->next) if ( (a = get_action_by_name( (char *) curr->data)) ) a->init_defaults(&WinCommandState); /* honor flags */ if( get_flags( WinCommandState.flags, WINCOMMAND_Desk)) select_windows_on_desk(False); else if( ! get_flags( WinCommandState.flags, WINCOMMAND_AllDesks)) select_windows_on_screen(False); if ( ! select_windows_by_pattern(WinCommandState.pattern, !get_flags(WinCommandState.flags, WINCOMMAND_ActOnAll), False) ) LOCAL_DEBUG_OUT("warning: invalid pattern. Reverting to default."); /* apply operations */ for( curr = operations->head; curr != NULL; curr = curr->next) { command = (char *) curr->data; LOCAL_DEBUG_OUT("command: %s", command); if ( (a = get_action_by_name( (char *) curr->data)) ) a->exec_wrapper(&WinCommandState, (char *) curr->data); } ascom_wait(); ascom_deinit(); }else/* interactive mode */ { char *line_read = NULL ; while( (line_read = my_readline()) != NULL ) { char *ptr = line_read; char *cmd = NULL ; ptr = parse_token (ptr, &cmd); if( cmd != NULL && cmd[0] != '\0' ) { if( mystrcasecmp(cmd, "quit") == 0 ) break; if( mystrcasecmp(cmd, "set") == 0 ) { char *param = NULL ; ptr = parse_token (ptr, ¶m); while( isspace(*ptr) ) ++ptr ; switch( set_WinCommandParam( &WinCommandState, param, ptr) ) { case ASWC_BadParam : case ASWC_BadVal : printf("bad parameter [%s]\n", argv[i] ); break; case ASWC_Ok_ValUsed : case ASWC_Ok_ValUnused : #ifdef HAVE_READLINE add_history (line_read); #endif printf( "ok\n"); break ; } }else if( (a = get_action_by_name( cmd )) ) { a->init_defaults(&WinCommandState); ascom_init(); ascom_update_winlist(); if( get_flags( WinCommandState.flags, WINCOMMAND_Desk)) select_windows_on_desk(False); else if( ! get_flags( WinCommandState.flags, WINCOMMAND_AllDesks)) select_windows_on_screen(False); if ( ! select_windows_by_pattern(WinCommandState.pattern, !get_flags(WinCommandState.flags, WINCOMMAND_ActOnAll), False) ) LOCAL_DEBUG_OUT("warning: invalid pattern. Reverting to default."); a->exec_wrapper(&WinCommandState, ptr); ascom_wait(); ascom_deinit(); #ifdef HAVE_READLINE add_history (line_read); #endif printf( "ok\n"); }else { /* try to parse it as AS function */ printf( "bad command\n"); } free( cmd ) ; } free( line_read ); } printf( "\nbye bye\n" ); } destroy_asbidirlist( &operations ); return 0 ; }
int main(int argc, char *argv[]) { char fileName[256], oldwd[256], *command, *dimensions, *stringPtr; char prompt[512]; char *inputText; NXname dataName; int status; #if HAVE_LIBREADLINE rl_readline_name = "NXbrowse"; rl_attempted_completion_function = nxbrowse_complete; #if READLINE_VERSION >= 0x500 rl_catch_signals = 0; #else #define rl_crlf() fprintf(rl_outstream, "\r\n"); #define rl_on_new_line() 1 #endif using_history(); #else #define rl_crlf() #define rl_on_new_line() #define add_history(a) #endif printf("NXBrowse %s Copyright (C) 2009-2014 NeXus Data Format\n", NEXUS_VERSION); #if HAVE_LIBREADLINE printf ("Built with readline support - use <TAB> to complete commands and paths\n"); #endif /* HAVE_LIBREADLINE */ /* if there is a filename given on the command line use that, else ask for a filename */ if (argc < 2) { printf("Give name of NeXus file : "); if (fgets(fileName, sizeof(fileName), stdin) == NULL) { printf("Failed to open %s\n", fileName); return NX_ERROR; } if ((stringPtr = strchr(fileName, '\n')) != NULL) *stringPtr = '\0'; } else { strcpy(fileName, argv[1]); } strcpy(nxFile, fileName); /* Open input file and output global attributes */ if (NXopen(fileName, NXACC_READ, &the_fileId) != NX_OK) { printf("NX_ERROR: Can't open %s\n", fileName); return NX_ERROR; } PrintAttributes(the_fileId); iByteAsChar = 0; /* Display remaining NX_INT8 and NX_UINT8 variables as integers by default */ /* Input commands until the EXIT command is given */ strcpy(oldwd, "/"); strcpy(path, "/"); do { sprintf(prompt, "NX%s> ", path); if (getenv("NO_READLINE") != NULL) { inputText = my_readline(prompt); } else { inputText = readline(prompt); } if (inputText == NULL) { inputText = strdup("EXIT"); } if (*inputText) { add_history(inputText); } command = strtok(inputText, " "); /* Check if a command has been given */ if (command == NULL) command = " "; /* Convert it to upper case characters */ ConvertUpperCase(command); if (StrEq(command, "PWD")) { fprintf(rl_outstream, "%s\n", path); } if (StrEq(command, "TEST")) { char a[256], b[256]; stringPtr = strtok(NULL, " "); if (stringPtr != NULL) { parsepath(stringPtr, a, b); fprintf(rl_outstream," you entered >%s< - i think the full path is >%s< and the final component looks like this >%s<.\n", stringPtr, a, b); } else { fprintf(rl_outstream," you entered nothing\n"); } } /* Command is to print a directory of the current group */ if (StrEq(command, "DIR") || StrEq(command, "LS")) { stringPtr = strtok(NULL, " "); if (stringPtr != NULL) { char a[256], b[256]; parsepath(stringPtr, a, b); strcat(a, "/"); strcat(a, b); NXopengrouppath(the_fileId, a); NXBdir(the_fileId); NXopengrouppath(the_fileId, path); } else { NXBdir(the_fileId); } } /* Command is to open the specified group */ if (StrEq(command, "OPEN") || StrEq(command, "CD")) { stringPtr = strtok(NULL, " "); if (stringPtr != NULL) { char a[256], b[256]; if (StrEq(stringPtr, "-")) { stringPtr = oldwd; } parsepath(stringPtr, a, b); strcat(a, "/"); strcat(a, b); status = NXopengrouppath(the_fileId, a); if (status == NX_OK) { strcpy(oldwd, path); strcpy(path, a); } else { fprintf(rl_outstream, "NX_ERROR: cannot change into %s\n", stringPtr); NXopengrouppath(the_fileId, path); /* to be sure */ } } else { fprintf(rl_outstream, "NX_ERROR: Specify a group\n"); } } /* Command is to dump data values to a file */ if (StrEq(command, "DUMP")) { stringPtr = strtok(NULL, " "); if (stringPtr != NULL) { strcpy(dataName, stringPtr); stringPtr = strtok(NULL, " "); if (stringPtr != NULL) { strcpy(fileName, stringPtr); status = NXBdump(the_fileId, dataName, fileName); } else { fprintf(rl_outstream, "NX_ERROR: Specify a dump file name \n"); } } else { fprintf(rl_outstream, "NX_ERROR: Specify a data item\n"); } } /* Command is to print the values of the data */ if (StrEq(command, "READ") || StrEq(command, "CAT")) { stringPtr = strtok(NULL, " ["); if (stringPtr != NULL) { strcpy(dataName, stringPtr); dimensions = strtok(NULL, "[]"); status = NXBread(the_fileId, dataName, dimensions); } else { fprintf(rl_outstream, "NX_ERROR: Specify a data item\n"); } } /* Command is to close the current group */ if (StrEq(command, "CLOSE")) { if (strlen(path) > 1) { if (NXclosegroup(the_fileId) == NX_OK) { /* Remove the group from the prompt string */ strcpy(oldwd, path); stringPtr = strrchr(path, '/'); /* position of last group delimiter */ if (stringPtr != NULL) *stringPtr = '\0'; /* terminate the string there */ } } else { fprintf(rl_outstream, "NX_WARNING: Already at root level of file\n"); } } /* Command is to print help information */ if (StrEq(command, "HELP") || StrEq(command, "INFO")) { printf("NXbrowse commands : DIR\n"); printf(" LS\n"); printf(" OPEN <groupName>\n"); printf(" CD <groupName>\n"); printf(" READ <dataName>\n"); printf(" READ <dataName>[<dimension indices...>]\n"); printf(" DUMP <dataName> <fileName> \n"); printf(" CLOSE\n"); printf(" BYTEASCHAR\n"); printf(" HELP\n"); printf(" EXIT\n"); printf("\n"); #if HAVE_LIBREADLINE printf("Pressing <TAB> after a command or partial nexus object name will complete\n"); printf("possible names. For example:\n"); printf("\n"); printf(" cd ent<TAB KEY PRESSED> # all items starting with ent are listed\n"); printf("\n"); #endif } /* Command is to print byte as char information */ if (StrEq(command, "BYTEASCHAR")) { if (iByteAsChar == 1) iByteAsChar = 0; else iByteAsChar = 1; } /* Command is to exit the program */ if (StrEq(command, "EXIT") || StrEq(command, "QUIT")) { /* for (i = groupLevel; i > 0; i--) NXclosegroup (the_fileId); */ NXclose(&the_fileId); return NX_OK; } status = NX_OK; free(inputText); } while (status == NX_OK); return NX_OK; }
char* prompt_cmd_read() { return my_readline(PROMPT_BUFFER_SIZE); }
/* * ui_readline_loop() * * g³ówna pêtla programu. wczytuje dane z klawiatury w miêdzyczasie * obs³uguj±c sieæ i takie tam. */ int ui_readline_loop(void) { char *line = my_readline(); char *rline = line; /* for freeing */ gchar *out, *p; gint len; if (!line) { /* Ctrl-D handler */ if (window_current->id == 0) { /* debug window */ window_switch(1); } else if (window_current->id == 1) { /* status window */ if (config_ctrld_quits) { return 0; } else { printf("\n"); } } else if (window_current->id > 1) { /* query window */ window_kill(window_current); } return 1; } len = strlen(line); if (G_LIKELY(len > 0)) { if (G_UNLIKELY(line[len - 1] == '\\')) { /* multi line handler */ GString *s = g_string_new_len(line, len-1); free(line); no_prompt = 1; rl_bind_key(9, rl_insert); while ((line = my_readline())) { if (!strcmp(line, ".")) break; g_string_append(s, line); g_string_append_len(s, "\r\n", 2); /* XXX */ free(line); } rl_bind_key(9, rl_complete); no_prompt = 0; if (line) { g_string_free(s, TRUE); free(line); return 1; } line = g_string_free(s, FALSE); } /* if no empty line and we save duplicate lines, add it to history */ if (config_history_savedups || !history_length || strcmp(line, history_get(history_length)->line)) add_history(line); } pager_lines = 0; /* now we can definitely recode */ out = ekg_recode_from_locale(line); if (G_LIKELY(line == rline)) free(rline); /* allocd by readline */ else g_free(line); /* allocd by us */ /* omit leading whitespace */ for (p = out; g_unichar_isspace(g_utf8_get_char(p)); p = g_utf8_next_char(p)); if (*p || config_send_white_lines) command_exec(window_current->target, window_current->session, out, 0); pager_lines = -1; g_free(out); return 1; }
int main(int argc, char * const argv[]) { int r, c, long_optind = 0, err = 0; char *line; int cargc; char *cargv[20]; sc_context_param_t ctx_param; printf("OpenSC Explorer version %s\n", sc_get_version()); while (1) { c = getopt_long(argc, argv, "r:c:vw", options, &long_optind); if (c == -1) break; if (c == '?') print_usage_and_die(app_name, options, option_help); switch (c) { case 'r': opt_reader = atoi(optarg); break; case 'c': opt_driver = optarg; break; case 'w': opt_wait = 1; break; case 'v': verbose++; break; } } memset(&ctx_param, 0, sizeof(ctx_param)); ctx_param.ver = 0; ctx_param.app_name = app_name; r = sc_context_create(&ctx, &ctx_param); if (r) { fprintf(stderr, "Failed to establish context: %s\n", sc_strerror(r)); return 1; } if (verbose > 1) ctx->debug = verbose-1; if (opt_driver != NULL) { err = sc_set_card_driver(ctx, opt_driver); if (err) { fprintf(stderr, "Driver '%s' not found!\n", opt_driver); err = 1; goto end; } } err = connect_card(ctx, &card, opt_reader, 0, opt_wait, 0); if (err) goto end; sc_format_path("3F00", ¤t_path); r = sc_select_file(card, ¤t_path, ¤t_file); if (r) { printf("unable to select MF: %s\n", sc_strerror(r)); return 1; } while (1) { struct command *cmd; size_t i; char prompt[40]; sprintf(prompt, "OpenSC ["); for (i = 0; i < current_path.len; i++) { if ((i & 1) == 0 && i && current_path.type != SC_PATH_TYPE_DF_NAME) sprintf(prompt+strlen(prompt), "/"); sprintf(prompt+strlen(prompt), "%02X", current_path.value[i]); } sprintf(prompt+strlen(prompt), "]> "); line = my_readline(prompt); if (line == NULL) break; cargc = parse_line(line, cargv, DIM(cargv)); if (cargc < 1) continue; for (r=cargc; r < (int)DIM(cargv); r++) cargv[r] = ""; cmd = ambiguous_match(cmds, cargv[0]); if (cmd == NULL) { usage(); } else { cmd->func(cargc-1, cargv+1); } } end: die(err); return 0; /* not reached */ }