Example #1
0
void XttMotif::open_change_value()
{
  Arg 		args[1];
  int		sts;

  if ( input_open) {
    XtUnmanageChild( cmd_input);
    set_prompt( "");
    input_open = 0;
    return;
  }

  sts = xnav->check_attr_value();
  if ( EVEN(sts)) {
    message( 'E', XNav::get_message(sts));
    return;
  }

  if ( command_open)
    command_open = 0;
  else
    XtManageChild( cmd_input);

  message( ' ', "");
  XtSetKeyboardFocus( toplevel, cmd_input);

  XtSetArg(args[0],XmNvalue, "");
  XtSetValues( cmd_input, args, 1);
  set_prompt( Lng::translate("value >"));
  input_open = 1;
}
Example #2
0
static t_stat ipc_set_node (UNIT UNUSED *uptr, int32 UNUSED val, char *cval, void UNUSED *desc)
{
    if (cval == NULL || *cval == 0)
    {
        strcpy(fnpName, IPC_NODE);
#ifdef VM_FNP
        set_prompt (0, "sim>");     // reset prompt to default
#endif
    }
    else
    {
        stripquotes(cval);
        
        if (!startsWith(cval, "cpu"))
            sim_printf("WARNING: Node name <%s> does not begin with 'cpu'\n", cval);
        
        strcpy(fnpName, cval);
#ifdef VM_FNP
        char temp[132];
        sprintf(temp, "%s>", fnpName);
        set_prompt(0, temp);
#endif
        
    }
    
    // if IPC is already running, resrart it with new node
    if (actor)
    {
        ipc(ipcStop, 0, 0, 0, 0);    // ki;; IPC
        ipc(ipcStart, fnpName, 0, 0, 0);
    }
    
    return SCPE_OK;
}
Example #3
0
void WdaMotif::change_value_close()
{
  char *text;
  int sts;

  text = XmTextGetString( cmd_scrolledinput);
  if ( input_open) {
    if ( input_multiline) {
      sts = ((WdaNav *)wdanav)->set_attr_value( input_node,
		input_name, text);
      XtUnmanageChild( cmd_scrolledinput);
      XtUnmanageChild( cmd_scrolled_ok);
      XtUnmanageChild( cmd_scrolled_ca);
      set_prompt( "");
      input_open = 0;

      ((WdaNav *)wdanav)->redraw();
      ((WdaNav *)wdanav)->set_inputfocus();
    }
    else {
      text = XmTextGetString( cmd_input);

      sts = ((WdaNav *)wdanav)->set_attr_value( input_node, 
		input_name, text);
      XtUnmanageChild( cmd_input);
      set_prompt( "");
      input_open = 0;
      if ( redraw_cb)
        (redraw_cb)( this);

      ((WdaNav *)wdanav)->set_inputfocus();
    }
  }
}
Example #4
0
/* Handle user input, return 0 if the program should quit, or 1 if not */
static int handle_key(int ch)
{
	if(prompt == NULL) {
		/* Not prompting for input; handle interactive commands */
		switch(ch) {
		case 'n': case 'N':
			show_networks ^= 1;
			break;
		case 'b': case 'B':
			show_vbds ^= 1;
			break;
		case 't': case 'T':
			show_tmem ^= 1;
			break;
		case 'r': case 'R':
			repeat_header ^= 1;
			break;
		case 's': case 'S':
			sort_field = (sort_field + 1) % NUM_FIELDS;
			break;
		case 'v': case 'V':
			show_vcpus ^= 1;
			break;
		case KEY_DOWN:
			first_domain_index++;
			break;
		case KEY_UP:
			if(first_domain_index > 0)
				first_domain_index--;
			break;
		case 'd': case 'D':
			set_prompt("Delay(sec)", set_delay);
			break;
		case 'q': case 'Q': case KEY_ESCAPE:
			return 0;
		}
	} else {
		/* Prompting for input; handle line editing */
		switch(ch) {
		case '\r':
			prompt_complete_func(prompt_val);
			set_prompt(NULL, NULL);
			break;
		case KEY_ESCAPE:
			set_prompt(NULL, NULL);
			break;
		case KEY_BACKSPACE:
			if(prompt_val_len > 0)
				prompt_val[--prompt_val_len] = '\0';
		default:
			if((prompt_val_len+1) < PROMPT_VAL_LEN
			   && isprint(ch)) {
				prompt_val[prompt_val_len++] = (char)ch;
				prompt_val[prompt_val_len] = '\0';
			}
		}
	}

	return 1;
}
Example #5
0
void WdaGtk::change_value_close()
{
  unsigned char *s;
  int sts;

  if ( input_open) {
    if ( input_multiline) {
      gchar *text;
      GtkTextIter start_iter, end_iter;
      gtk_text_buffer_get_start_iter( cmd_scrolled_buffer, &start_iter);
      gtk_text_buffer_get_end_iter( cmd_scrolled_buffer, &end_iter);

      text = gtk_text_buffer_get_text( cmd_scrolled_buffer, &start_iter, &end_iter,
				       FALSE);
      // Replace ctrl characters with space
      for ( s = (unsigned char *) text; *s; s++) {
	if ( *s < ' ' && *s != 10 && *s != 13)
	  *s = ' ';
      }

      sts = wdanav->set_attr_value( input_node,
				    input_name, text);
      g_free( text);
      g_object_set( cmd_scrolledinput, "visible", FALSE, NULL);
      set_prompt( "");
      input_open = 0;

      int w, h;
      gdk_drawable_get_size( pane->window, &w, &h);
      gtk_paned_set_position( GTK_PANED(pane), h - 50);

      wdanav->redraw();
      wdanav->set_inputfocus();
    }
    else {
      char *text, *textutf8;
      textutf8 = gtk_editable_get_chars( GTK_EDITABLE(cmd_input), 0, -1);
      text = g_convert( textutf8, -1, "ISO8859-1", "UTF-8", NULL, NULL, NULL);
      g_free( textutf8);

      if ( text) {
	sts = wdanav->set_attr_value( input_node, 
				      input_name, text);
	g_free( text);
      }
      g_object_set( cmd_input, "visible", FALSE, NULL);
      set_prompt( "");
      input_open = 0;
      if ( redraw_cb)
        (redraw_cb)( this);

      wdanav->set_inputfocus();

      if ( !text)
	message( 'E', "Input error, invalid character");
    }
  }
}
Example #6
0
void AttrMotif::change_value()
{
  int sts;
  Widget text_w;
  int multiline;
  char* value;
  int size;

  if (input_open) {
    XtUnmanageChild(cmd_input);
    set_prompt("");
    input_open = 0;
    return;
  }

  sts = ((AttrNav*)attrnav)->check_attr_value(&multiline, &size, &value);
  if (EVEN(sts)) {
    if (sts == GE__NOATTRSEL)
      message('E', "No attribute is selected");
    else
      message('E', "Attribute can't be modified");
    return;
  }

  if (multiline) {
    text_w = cmd_scrolledinput;
    XtManageChild(text_w);
    XtManageChild(cmd_scrolled_ok);
    XtManageChild(cmd_scrolled_ca);
  } else {
    text_w = cmd_input;
    XtManageChild(text_w);
  }

  message(' ', "");
  flow_SetInputFocus(text_w);
  //  XtSetKeyboardFocus( toplevel, text_w);
  //  XtCallAcceptFocus( text_w, CurrentTime);

  if (value) {
    XmTextSetString(text_w, value);
    if (!multiline) {
      XmTextSetInsertionPosition(text_w, strlen(value));
      XmTextSetSelection(text_w, 0, strlen(value), CurrentTime);
    }
  } else {
    XmTextSetString(cmd_input, (char*)"");
  }
  //  i = 0;
  //  XtSetArg(args[i],XmNeditMode, XmMULTI_LINE_EDIT); i++;
  //  XtSetValues( text_w, args, i);
  set_prompt("value >");
  input_open = 1;
}
Example #7
0
static void
on_event(int bp, ldb_t *ldb, lua_State *state, lua_Debug *ar) {
  int depth;

  depth = get_calldepth(state);
  if (bp < 0 && ldb->call_depth != -1 && depth > ldb->call_depth) {
    /* next command, just return */
    return;
  }
  ldb->call_depth = depth;

  if (bp >= 0) {
    ldb_output("Breakpoint %d hit!\n", bp);
  }

  set_prompt();
  char *buff = NULL;
  input_t input;;
  int ret, i, len;
  while ((len = get_input(&buff)) >= 0) {
    if (len == 0) {
      free(buff);
      continue;
    }
    if (split_input(buff, &input) < 0) {
      set_prompt();
      free(buff);
      continue;
    }

    ret = 0;
    for (i = 0; commands[i].handler != NULL; ++i) {
      if (!strcmp(input.buffer[0], commands[i].short_name) ||
        !strcmp(input.buffer[0], commands[i].name)) {
        ldb->call_depth = get_calldepth(state);
        ret = (*commands[i].handler)(state, ldb, ar, &input);
        break;
      }
    }
    if (commands[i].name == NULL) {
      ldb_output("bad command: %s, ldb_output h for help\n", buff);
	} 

	//free(buff);

    if (ret < 0) {
      break;
    }
    set_prompt();
  }
}
Example #8
0
/*
 * window_write()
 *
 * dopisuje liniê do bufora danego okna.
 */
int window_write(int id, const /*locale*/ char *line)
{
	window_t *w = window_exist(id);
	readline_window_t *r = readline_window(w);
	int i = 1;

	if (!line || !w)
		return -1;

	/* je¶li ca³y bufor zajêty, zwolnij pierwsz± liniê i przesuñ do góry */
	if (r->line[MAX_LINES_PER_SCREEN - 1]) {
		xfree(r->line[0]);
		memmove(&(r->line[0]), &(r->line[1]), sizeof(char *) * (MAX_LINES_PER_SCREEN - 1));
		r->line[MAX_LINES_PER_SCREEN - 1] = xstrdup(line);
	} else {
		/* znajd¼ pierwsz± woln± liniê i siê wpisz. */
		for (i = 0; i < MAX_LINES_PER_SCREEN; i++)
			if (!r->line[i]) {
				r->line[i] = xstrdup(line);
				break;
			}
	}

	if (w != window_current) {
		set_prompt(current_prompt());
		rl_redisplay();
	}
	
	return 0;
}
Example #9
0
int end_mode (char **args)
{
	args = args;
	set_prompt ("");
	set_curr_mode (USER_EXEC_MODE);
	return 0;
}
Example #10
0
int change_config_mode (char **args)
{
	args = args;
	set_prompt ("(config)");
	set_curr_mode (GLOBAL_CONFIG_MODE);
	return 0;
}
Example #11
0
bool telnet_console_connection::authenticate(const char *in) {
	bool denied = false;
	std::string password;

	writeclient("\r\n");

	authenticate_state++;

	if (authenticate_state == WaitingPassword) {
		username = in;
		set_prompt("Password: "******"*") {
				authenticate_state++;
			}
		}
	}

	if (denied) {
		clearline();
		writeclient("Your connection is not permited. Contact the system administrator.\r\n");
		if (console->should_log(VERBOSE))
			console->log().xprintf("(CONSOLE) denied connection"
					       " from %{Addr}\n", c_peeraddr);
		console->release_connection(this);
		return false;
	} else if (authenticate_state == GotAllData) {
		writeclient("\r\n");

		g_mrd->show_mrd_version(_output);
		_output.newl();

		set_prompt("# ");
		should_echo = true;
	}

	return true;
}
Example #12
0
static void connect_notify(srs_t *srs, int status, const char *msg,
                           void *user_data)
{
    client_t *c = (client_t *)user_data;

    MRP_UNUSED(srs);

    if (status == 1) {
        set_prompt(c, "connected");
        print(c, "Connection to server established.");
    }
    else {
        set_prompt(c, "disconnected");
        print(c, "Server connection down (error: %d, %s).", status,
              msg ? msg : "<unknown>");
    }
}
Example #13
0
int logon(int flag)
{
    enable_telnet(0);
    set_prompt("");
    
    if(active)
	ed("/dummy","ed_ends");
    return 1;
}
Example #14
0
main()
{
char *line;
char prompt[200];
while(1)
{
	set_prompt(prompt);
	if(!(line = readline(prompt))
	break;
}
}
Example #15
0
object connect()
{
    enable_telnet(0);
    set_prompt("");

    write("!test\n=\n=\n");
    call_out(#'shutdown, 1, 1); // If something goes wrong.
    input_to("receive", 0, 0);

    return clone_object(this_object()); // Just a dummy object.
}
Example #16
0
object connect()
{
    enable_telnet(0);
    set_prompt("");

    fnamecounter = random(1000);
    write_file("/dummy"+fnamecounter, "");
    write("r /dummy"+fnamecounter+"\nQ\n");

    return clone_object(this_object()); // Just a dummy object.
}
Example #17
0
bool telnet_console_connection::check_startup() {
	if (!console_connection::check_startup())
		return false;

	conn_timer.start();

	cmd(WILL, TELOPT_ECHO);
	cmd(WILL, TELOPT_SGA);

	set_prompt("Username: ");

	return true;
}
Example #18
0
int set_hostname (const char *Hostname)
{
      int this_session = cli_get_cli_session_id ();

      if (this_session  < 0)
              return -1;

      sprintf (this_cli[this_session].hostname, "%s", Hostname);

      set_prompt ("(config)");
      get_prompt (this_cli[this_session].parser.cfg.prompt);

      return 0;
}
Example #19
0
bool
ReplWindow::on_key(GdkEventKey *e)
{

    g_print("ReplWindow key press\n");

    if (e->keyval == GDK_Return)
    {
        Glib::ustring exp = get_current_expression(); 
        evaluate(exp);

        g_print("expression: %s\n", exp.data());
        set_prompt();
    }

    return false;
}
Example #20
0
int change_vlan_mode (char **args)
{
	char prmpt[MAX_PMP_LEN];
	int vlan_id = 0;
	memset (prmpt, 0, sizeof (prmpt));
	if (args) {
		vlan_id = atoi (args[0]);
		cli_set_vlan_id (atoi(args[0]));
	}
	else {
		vlan_id = cli_get_vlan_id ();
	}
	sprintf (prmpt, "%s%d%s","(config-vlan-",vlan_id, ")");
	set_prompt (prmpt);
	set_curr_mode (VLAN_CONFIG_MODE);
	return 0;
}
Example #21
0
int change_to_interface_mode (char **args)
{
	char prmpt[MAX_PMP_LEN];
	int port = 0;
	memset (prmpt, 0, sizeof (prmpt));
	if (args) {
		port = atoi (args[0]);
		cli_set_port (atoi(args[0]));
	}
	else {
		port = cli_get_port ();
	}
	sprintf (prmpt, "%s%d%s","(config-if-",port, ")");
	set_prompt (prmpt);
	set_curr_mode (INTERFACE_MODE);
	return 0;
}
Example #22
0
int create_cmdline_interface (char *prmt)
{
	/* init the tty promt properties*/
	init_tty_prompt ();
	/*signal handlers for SIGINT, SIGSEGV, SIGUSR1, SIGUSR2*/
	init_signals();
	/*XXX:set the tty prompt - we take control from shell 
	  don't forget to call reset_tty () once your job is done*/
	set_tty();

	set_prompt(prmt, strlen(prmt));

	create_sync_lock (&cmd_root.lock);
	/*finally kick-start the shell thread*/
	spawn_cli_thread ();
	return 0;
}
Example #23
0
bool MsgScroll::init(char *player_name)
{
 std::string prompt_string;

 prompt_string.append(player_name);
 if(game_type==NUVIE_GAME_U6)
 {
   prompt_string.append(":\n");
 }

 prompt_string.append(">");

 if(set_prompt((char *)prompt_string.c_str(),font) == false)
   return false;

 set_input_mode(false);

 return true;
}
Example #24
0
void initialise(object o, string prompt, string func, int type,
    int hide_input, mixed args) {
/* This function initialises the input handler with the provided values after
 * ensuring the return function is valid. */
  return_object = o;
  return_function = func;

  if(!function_object(return_function, return_object)) {
    error("Specified function '" + return_function +
        "' does not exist in object '" + object_name(return_object));
  }

  set_prompt(prompt);
  if(hide_input) {
    set_mode(MODE_NOECHO);
  }
  input_type = type;
  opt_args = args;
}
Example #25
0
/*
 * Name:    save_strokes
 * Purpose: save strokes to a file
 * Date:    April 1, 1992
 * Passed:  window:  pointer to current window
 */
int  save_strokes( WINDOW *window )
{
FILE *fp;                       /* file to be written */
char name[MAX_COLS+2];          /* file name */
char line_buff[(MAX_COLS+1)*2]; /* buffer for char and attribute  */
register int rc;
int  prompt_line;
int  fattr;

   name[0] = '\0';
   prompt_line = window->bottom_line;
   save_screen_line( 0, prompt_line, line_buff );
   /*
    * name for macro file
    */
   if ((rc = get_name( main19, prompt_line, name,
                 g_display.message_color )) == OK  &&  *name != '\0') {

      /*
       * make sure it is OK to overwrite any existing file
       */
      rc = get_fattr( name, &fattr );
      if (rc == OK) {
         /*
          * overwrite existing file
          */
         set_prompt( main20, prompt_line );
         if (get_yn( ) != A_YES  ||  change_mode( name, prompt_line ) == ERROR)
            rc = ERROR;
      }
      if (rc != ERROR) {
         if ((fp = fopen( name, "wb" )) != NULL) {
            fwrite( &macro.first_stroke[0], sizeof(int), MAX_KEYS, fp );
            fwrite( &macro.strokes[0], sizeof(STROKES), STROKE_LIMIT, fp );
            fclose( fp );
         }
      }
   }
   restore_screen_line( 0, prompt_line, line_buff );
   return( OK );
}
Example #26
0
ReplWindow::ReplWindow() : Gtk::VBox()
{
    m_scrollView.add(m_repl);
    m_scrollView.set_policy(Gtk::POLICY_AUTOMATIC, 
                            Gtk::POLICY_AUTOMATIC);
    m_buffer = Gtk::TextBuffer::create();
    m_repl.set_buffer( m_buffer );

    pack_start(m_scrollView, true, true);

    m_repl.signal_key_press_event().connect( 
        sigc::mem_fun(*this, &ReplWindow::on_key ), false );


    m_prompt_tag = Gtk::TextTag::create( "prompt" );
    m_prompt_tag->property_editable() = false;

    m_buffer->get_tag_table()->add(m_prompt_tag);
    m_buffer->create_mark(PROMPT_MARK_NAME,
            m_buffer->begin());

    set_prompt();
}
Example #27
0
int XAttOneGtk::change_value(int set_focus)
{
  int sts;
  GtkWidget* text_w;
  char* value = 0;
  int input_size;
  char aval[1024];
  char buf[1024];
  int len;

  sts = gdh_GetAttributeCharAttrref(&aref, &atype, &asize, &aoffs, &aelem);
  if (EVEN(sts))
    return sts;

  switch (atype) {
  case pwr_eType_String:
  case pwr_eType_Text:
    input_size = asize;
    break;
  default:
    input_size = 80;
  }

  sts = gdh_GetObjectInfoAttrref(&aref, aval, sizeof(aval));
  if (EVEN(sts))
    return sts;

  if (atype == pwr_eType_Text)
    value = aval;
  else {
    XNav::attrvalue_to_string(
        atype, atype, &aval, buf, sizeof(buf), &len, NULL, 0);
    value = buf;
  }

  if (!access_rw) {
    gtk_label_set_text(GTK_LABEL(cmd_label), buf);
  } else {
    if (atype == pwr_eType_Text) {
      text_w = cmd_scrolledinput;
      g_object_set(cmd_input, "visible", FALSE, NULL);
      g_object_set(cmd_scrolledinput, "visible", TRUE, NULL);

      // int w, h;
      // gdk_drawable_get_size( pane->window, &w, &h);
      // gtk_paned_set_position( GTK_PANED(pane), h - 170);
      if (set_focus)
        gtk_widget_grab_focus(cmd_scrolledtextview);
      input_multiline = 1;

      if (value) {
        GtkTextIter start_iter, end_iter;
        gtk_text_buffer_get_start_iter(cmd_scrolled_buffer, &start_iter);
        gtk_text_buffer_get_end_iter(cmd_scrolled_buffer, &end_iter);
        gtk_text_buffer_delete(cmd_scrolled_buffer, &start_iter, &end_iter);

        gtk_text_buffer_get_start_iter(cmd_scrolled_buffer, &start_iter);
        gtk_text_buffer_insert(cmd_scrolled_buffer, &start_iter, value, -1);
      } else {
        GtkTextIter start_iter, end_iter;
        gtk_text_buffer_get_start_iter(cmd_scrolled_buffer, &start_iter);
        gtk_text_buffer_get_end_iter(cmd_scrolled_buffer, &end_iter);
        gtk_text_buffer_delete(cmd_scrolled_buffer, &start_iter, &end_iter);
      }
    } else {
      text_w = cmd_input;
      g_object_set(cmd_input, "visible", TRUE, NULL);
      g_object_set(cmd_scrolledinput, "visible", FALSE, NULL);
      if (set_focus)
        gtk_widget_grab_focus(cmd_input);
      input_multiline = 0;

      gtk_entry_set_max_length(GTK_ENTRY(text_w), input_size - 1);

      gint pos = 0;
      gtk_editable_delete_text(GTK_EDITABLE(cmd_input), 0, -1);

      if (value) {
        gtk_editable_insert_text(
            GTK_EDITABLE(text_w), value, strlen(value), &pos);

        // Select the text
        gtk_editable_set_position(GTK_EDITABLE(cmd_input), -1);
        gtk_editable_select_region(GTK_EDITABLE(cmd_input), 0, -1);
      }
    }
    message(' ', "");
    set_prompt(Lng::translate("value >"));
    input_open = 1;
  }
  return XATT__SUCCESS;
}
Example #28
0
void WdaMotif::change_value( int set_focus)
{
  int		sts;
  Widget	text_w;
  int		multiline;
  char		*value;
  Arg 		args[5];
  int		input_size;

  if ( input_open) {
    XtUnmanageChild( cmd_input);
    set_prompt( "");
    input_open = 0;
    return;
  }

  sts = ((WdaNav *)wdanav)->check_attr( &multiline, &input_node, input_name,
		&value, &input_size);
  if ( EVEN(sts)) {
    if ( sts == WDA__NOATTRSEL)
      message( 'E', "No attribute is selected");
    else
      message( 'E', wnav_get_message( sts));
    return;
  }

  if ( multiline) {
    text_w = cmd_scrolledinput;
    XtManageChild( text_w);
    XtManageChild( cmd_scrolled_ok);
    XtManageChild( cmd_scrolled_ca);

    // XtSetArg(args[0], XmNpaneMaximum, 300);
    // XtSetValues( wdanav_form, args, 1);

    XtSetArg(args[0], XmNmaxLength, input_size-1);
    XtSetValues( text_w, args, 1);
    if ( value) {
      XmTextSetString( text_w, value);
//    XmTextSetInsertionPosition( text_w, strlen(value));
    }
    else
      XmTextSetString( text_w, (char*) "");

    input_multiline = 1;
  }
  else {
    text_w = cmd_input;
    XtManageChild( text_w);
    XtSetArg(args[0],XmNmaxLength, input_size-1);
    XtSetValues( text_w, args, 1);
    if ( value) {
      XmTextSetString( text_w, value);
      XmTextSetInsertionPosition( text_w, strlen(value));
    }
    else
      XmTextSetString( text_w, (char*) "");

    input_multiline = 0;
  }

  if ( value)
    free( value);

  message( ' ', "");
  if ( set_focus)
    flow_SetInputFocus( text_w);
  set_prompt( "value >");
  input_open = 1;
}
Example #29
0
int
c_read(char **wp)
{
	int c = 0;
	int expand = 1, history = 0;
	int expanding;
	int ecode = 0;
	char *cp;
	int fd = 0;
	struct shf *shf;
	int optc;
	const char *emsg;
	XString cs, xs;
	struct tbl *vp;
	char *xp = NULL;

	while ((optc = ksh_getopt(wp, &builtin_opt, "prsu,")) != -1)
		switch (optc) {
		case 'p':
			if ((fd = coproc_getfd(R_OK, &emsg)) < 0) {
				bi_errorf("-p: %s", emsg);
				return 1;
			}
			break;
		case 'r':
			expand = 0;
			break;
		case 's':
			history = 1;
			break;
		case 'u':
			if (!*(cp = builtin_opt.optarg))
				fd = 0;
			else if ((fd = check_fd(cp, R_OK, &emsg)) < 0) {
				bi_errorf("-u: %s: %s", cp, emsg);
				return 1;
			}
			break;
		case '?':
			return 1;
		}
	wp += builtin_opt.optind;

	if (*wp == NULL)
		*--wp = "REPLY";

	/* Since we can't necessarily seek backwards on non-regular files,
	 * don't buffer them so we can't read too much.
	 */
	shf = shf_reopen(fd, SHF_RD | SHF_INTERRUPT | can_seek(fd), shl_spare);

	if ((cp = strchr(*wp, '?')) != NULL) {
		*cp = 0;
		if (isatty(fd)) {
			/* at&t ksh says it prints prompt on fd if it's open
			 * for writing and is a tty, but it doesn't do it
			 * (it also doesn't check the interactive flag,
			 * as is indicated in the Kornshell book).
			 */
			shellf("%s", cp+1);
		}
	}

	/* If we are reading from the co-process for the first time,
	 * make sure the other side of the pipe is closed first.  This allows
	 * the detection of eof.
	 *
	 * This is not compatible with at&t ksh... the fd is kept so another
	 * coproc can be started with same output, however, this means eof
	 * can't be detected...  This is why it is closed here.
	 * If this call is removed, remove the eof check below, too.
	 * coproc_readw_close(fd);
	 */

	if (history)
		Xinit(xs, xp, 128, ATEMP);
	expanding = 0;
	Xinit(cs, cp, 128, ATEMP);
	for (; *wp != NULL; wp++) {
		for (cp = Xstring(cs, cp); ; ) {
			if (c == '\n' || c == EOF)
				break;
			while (1) {
				c = shf_getc(shf);
				if (c == '\0')
					continue;
				if (c == EOF && shf_error(shf) &&
				    shf_errno(shf) == EINTR) {
					/* Was the offending signal one that
					 * would normally kill a process?
					 * If so, pretend the read was killed.
					 */
					ecode = fatal_trap_check();

					/* non fatal (eg, CHLD), carry on */
					if (!ecode) {
						shf_clearerr(shf);
						continue;
					}
				}
				break;
			}
			if (history) {
				Xcheck(xs, xp);
				Xput(xs, xp, c);
			}
			Xcheck(cs, cp);
			if (expanding) {
				expanding = 0;
				if (c == '\n') {
					c = 0;
					if (Flag(FTALKING_I) && isatty(fd)) {
						/* set prompt in case this is
						 * called from .profile or $ENV
						 */
						set_prompt(PS2, NULL);
						pprompt(prompt, 0);
					}
				} else if (c != EOF)
					Xput(cs, cp, c);
				continue;
			}
			if (expand && c == '\\') {
				expanding = 1;
				continue;
			}
			if (c == '\n' || c == EOF)
				break;
			if (ctype(c, C_IFS)) {
				if (Xlength(cs, cp) == 0 && ctype(c, C_IFSWS))
					continue;
				if (wp[1])
					break;
			}
			Xput(cs, cp, c);
		}
		/* strip trailing IFS white space from last variable */
		if (!wp[1])
			while (Xlength(cs, cp) && ctype(cp[-1], C_IFS) &&
			    ctype(cp[-1], C_IFSWS))
				cp--;
		Xput(cs, cp, '\0');
		vp = global(*wp);
		/* Must be done before setting export. */
		if (vp->flag & RDONLY) {
			shf_flush(shf);
			bi_errorf("%s is read only", *wp);
			return 1;
		}
		if (Flag(FEXPORT))
			typeset(*wp, EXPORT, 0, 0, 0);
		if (!setstr(vp, Xstring(cs, cp), KSH_RETURN_ERROR)) {
		    shf_flush(shf);
		    return 1;
		}
	}

	shf_flush(shf);
	if (history) {
		Xput(xs, xp, '\0');
		source->line++;
		histsave(source->line, Xstring(xs, xp), 1);
		Xfree(xs, xp);
	}
	/* if this is the co-process fd, close the file descriptor
	 * (can get eof if and only if all processes are have died, ie,
	 * coproc.njobs is 0 and the pipe is closed).
	 */
	if (c == EOF && !ecode)
		coproc_read_close(fd);

	return ecode ? ecode : c == EOF;
}
Example #30
0
/*
 * run the commands from the input source, returning status.
 */
int
shell(Source * volatile s, volatile int level)
{
	struct op *t;
	volatile bool wastty = tobool(s->flags & SF_TTY);
	volatile uint8_t attempts = 13;
	volatile bool interactive = (level == 0) && Flag(FTALKING);
	volatile bool sfirst = true;
	Source *volatile old_source = source;
	int i;

	newenv(level == 2 ? E_EVAL : E_PARSE);
	if (interactive)
		really_exit = false;
	switch ((i = kshsetjmp(e->jbuf))) {
	case 0:
		break;
	case LBREAK:
	case LCONTIN:
		if (level != 2) {
			source = old_source;
			quitenv(NULL);
			internal_errorf(Tf_cant_s, Tshell,
			    i == LBREAK ? Tbreak : Tcontinue);
			/* NOTREACHED */
		}
		/* assert: interactive == false */
		/* FALLTHROUGH */
	case LINTR:
		/* we get here if SIGINT not caught or ignored */
	case LERROR:
	case LSHELL:
		if (interactive) {
			if (i == LINTR)
				shellf("\n");
			/*
			 * Reset any eof that was read as part of a
			 * multiline command.
			 */
			if (Flag(FIGNOREEOF) && s->type == SEOF && wastty)
				s->type = SSTDIN;
			/*
			 * Used by exit command to get back to
			 * top level shell. Kind of strange since
			 * interactive is set if we are reading from
			 * a tty, but to have stopped jobs, one only
			 * needs FMONITOR set (not FTALKING/SF_TTY)...
			 */
			/* toss any input we have so far */
			yyrecursive_pop(true);
			s->start = s->str = null;
			retrace_info = NULL;
			herep = heres;
			break;
		}
		/* FALLTHROUGH */
	case LEXIT:
	case LLEAVE:
	case LRETURN:
		source = old_source;
		quitenv(NULL);
		/* keep on going */
		unwind(i);
		/* NOTREACHED */
	default:
		source = old_source;
		quitenv(NULL);
		internal_errorf(Tunexpected_type, Tunwind, Tshell, i);
		/* NOTREACHED */
	}
	while (/* CONSTCOND */ 1) {
		if (trap)
			runtraps(0);

		if (s->next == NULL) {
			if (Flag(FVERBOSE))
				s->flags |= SF_ECHO;
			else
				s->flags &= ~SF_ECHO;
		}
		if (interactive) {
			j_notify();
			set_prompt(PS1, s);
		}
		t = compile(s, sfirst, true);
		if (interactive)
			histsave(&s->line, NULL, HIST_FLUSH, true);
		sfirst = false;
		if (!t)
			goto source_no_tree;
		if (t->type == TEOF) {
			if (wastty && Flag(FIGNOREEOF) && --attempts > 0) {
				shellf("Use 'exit' to leave mksh\n");
				s->type = SSTDIN;
			} else if (wastty && !really_exit &&
			    j_stopped_running()) {
				really_exit = true;
				s->type = SSTDIN;
			} else {
				/*
				 * this for POSIX which says EXIT traps
				 * shall be taken in the environment
				 * immediately after the last command
				 * executed.
				 */
				if (level == 0)
					unwind(LEXIT);
				break;
			}
		} else if ((s->flags & SF_MAYEXEC) && t->type == TCOM)
			t->u.evalflags |= DOTCOMEXEC;
		if (!Flag(FNOEXEC) || (s->flags & SF_TTY))
			exstat = execute(t, 0, NULL) & 0xFF;

		if (t->type != TEOF && interactive && really_exit)
			really_exit = false;

 source_no_tree:
		reclaim();
	}
	quitenv(NULL);
	source = old_source;
	return (exstat & 0xFF);
}