Exemple #1
0
/*
 * append_lines: Insert text from stdin to after given address; stop
 *   when either a single period is read or EOF. Return status.
 */
int
append_lines (off_t after, ed_buffer_t *ed)
{
  char *s;
  size_t len;
  ed_line_node_t *lp;
  ed_undo_node_t *up = NULL;
  int status;

  ed->state->input_is_binary = 0;
  for (ed->state->dot = after;;)
    {
      if (!ed->exec->global)
        {
          if (!(ed->input = get_stdin_line (&len, ed)))
            {
              /* Permit EOF (i.e., key stroke: CTL + D) at beginning of
                 line as alternative to `.' */
              status = feof (stdin) ? 0 : ERR;
              clearerr (stdin);
              return status;
            }
          if (*(ed->input + len - 1) != '\n')
            {
              ed->exec->err = _("End-of-file unexpected");
              clearerr (stdin);
              return ERR;
            }
          ++ed->exec->line_no;
        }
      else if (*ed->input == '\0')
        return 0;
      if (*ed->input == '.' && *(ed->input + 1) == '\n')
        {
          ++ed->input;
          return 0;
        }
      if (ed->exec->global)
        {
          for (s = ed->input; *s++ != '\n';)
            ;
          len = s - ed->input;
        }
      spl1 ();
      ed->state->is_binary |= ed->state->input_is_binary;
      if (after == ed->state->lines)
        ed->state->newline_appended = 0;
      if (!(ed->input = put_buffer_line (ed->input, len, ed)))
        {
          spl0 ();
          return ERR;
        }
      lp = get_line_node (ed->state->dot, ed);
      APPEND_UNDO_NODE (lp, up, ed->state->dot, ed);
      ed->state->is_empty = 0;
      ed->state->is_modified = 1;
      spl0 ();
    }
  /* NOTREACHED */
}
Exemple #2
0
/*
 * exec_global: Apply command list to lines in a range in global
 *   queue. Return command status.
 */
int
exec_global (ed_buffer_t *ed)
{
  static char *gcb = NULL;      /* global command buffer */
  static size_t gcb_size = 0;   /* buffer size */

  ed_line_node_t *lp = NULL;
  size_t len;
  int first_time = 1;
  int interactive = ed->exec->global & GLBI;
  int saved_io_f = ed->display->io_f;
  int status;

  reset_undo_queue (ed);

  /*
   * If non-interactive, read command before entering loop in the
   * event of empty global queue.
   */
  if (!interactive && !(ed->input = get_extended_line (&len, 0, 1, ed)))
    {
      status = ERR;
      clearerr (stdin);
      return status;
    }

  /* Empty command list equivalent to `p' command per SUSv4. */
  if (strlen (ed->input) == 1 && *ed->input == '\n')
    ed->input = "p\n";

  for (ed->exec->first_pass = 1; (lp = next_global_node (ed));
       ed->input = gcb, ed->exec->first_pass = 0)
    {
      if ((status = get_line_node_address (lp, &ed->state->dot, ed)) < 0)
        return status;

      else if (interactive)
        {
          ed->display->io_f = PRNT;
          if ((status = display_lines (ed->state->dot,
                                       ed->state->dot, ed)) < 0)
            return status;
          else if (!(ed->input = get_stdin_line (&len, ed))
                   || !(ed->input = get_extended_line (&len, 0, 1, ed)))
            {
              /* For an interactive global command, permit EOF to cancel. */
              status = feof (stdin) ? 0 : ERR;
              clearerr (stdin);
              return status;
            }
        }

      /* Global non-interactive command already set. */
      if (ed->input == gcb)
        ;

      /* Skip to next line */
      else if (len == 1 && interactive)
        continue;

      /* Repeat previous command. */
      else if (len == 2 && *ed->input == '&' && interactive)
        {
          if (first_time)
            {
              ed->exec->err = _("No previous command");
              return ERR;
            }
        }
      else
        {
          /* get_extended_line () isn't reentrant, so save ed->input. */
          REALLOC_THROW (gcb, gcb_size, len + 1, ERR, ed);

          /* Assert: ed->input is NUL-terminated! */
          strcpy (gcb, ed->input);
          first_time = 0;
        }
      for (ed->input = gcb; *ed->input;)
        if ((status = address_range (ed)) < 0
            || (status = exec_command (ed)) < 0
            || ((ed->display->io_f = status) > 0
                && (status = display_lines (ed->state->dot,
                                            ed->state->dot, ed)) < 0))
          return status;
    }
  return saved_io_f;
}
int main(int argc, char* argv[]) {
    get_stdin_line();
    exit(EXIT_SUCCESS);
}