Exemplo n.º 1
0
void cmd_insert (char *cp)

{
    Line *line;
    String *string;

    if (range_single (cp, &cp, &cur_position) < 0) return;		/* see where to insert */
    if (*cp == ';')  							/* if ;, insert the remaining string before current line */
    {
        cur_position.offset = 0;
        string = string_create (strlen (cp + 1), cp + 1);
        string_concat (string, 1, "\n");
        buffer_dirty (cur_position.buffer, 1);
        line_insert (cur_position.buffer, cur_position.line, string);
        return;
    }
    if (!eoltest (cp)) return;						/* otherwise, that's all there should be */

    /* Read tty input until eof into the current buffer just before the current line */

    cur_position.offset = 0;
    while ((string = jnl_readprompt ("\r\n               >")) != NULL)
    {
        string_concat (string, 1, "\n");					/* put line terminator on string */
        buffer_dirty (cur_position.buffer, 1);
        line_insert (cur_position.buffer, cur_position.line, string);	/* insert line just before current line */
    }
}
Exemplo n.º 2
0
void cmd_resequence (char *cp)

{
  Line *line;

  count = 0;

  /* If no range given, resequence the whole current buffer */

  if (*cp == 0) {
    for (line = buffer_first_line (cur_position.buffer); line != NULL; line = line_next (line)) {
      line_reseq (line);
      count ++;
    }
  }

  /* Otherwise, process the requested lines */

  else if (range_multiple (cp, &cp, resequence_range, NULL) >= 0) eoltest (cp);

  /* Either way, print out summary */

  if (count == 0) outerr (0, "no lines resequenced\n");
  else outerr (12, "%u line%s resequenced\n", count, (count == 1) ? "" : "s");
}
Exemplo n.º 3
0
void cmd_include (char *cp)

{
    char *input_name;
    FILE *input_file;
    Line *line;
    String *string;

    if (*cp == 0)  						/* make sure there's a filename there */
    {
        outerr (0, "no filename specified\n");
        return;
    }
    input_name = cp;						/* get input filename */
    cp = uptospace (cp);						/* it goes up to next space or eol */
    if (range_single (cp, &cp, &cur_position) < 0) return;	/* decode the range to get what line to insert before */
    if (!eoltest (cp)) return;

    /* Open the file */

    input_file = fopen (input_name, "r");
    if (input_file == NULL)
    {
        outerr (strlen (input_name) + strlen (strerror (errno)), "error opening %s: %s\n", input_name, strerror (errno));
        return;
    }

    /* Read it into the current buffer just before the current line */

    buffer_dirty (cur_position.buffer, 1);				/* it will soon be dirty */
    cur_position.offset = 0;						/* insert before beginning of current line */
    read_file (input_file, cur_position.buffer, cur_position.line);	/* read file in */
}
Exemplo n.º 4
0
void cmd_delete (char *cp)

{

  first = 1;
  count = 0;
  if (range_multiple (cp, &cp, delete_range, &first) >= 0) eoltest (cp);
  if (count == 0) outerr (0, "no lines deleted\n");
  else outerr (12, "%u line%s deleted\n", count, (count == 1) ? "" : "s");
  if (cur_position.line != NULL) line_print (cur_position.line);
  else outfmt (strlen (buffer_name (cur_position.buffer)), "[EOB=%s]\n", buffer_name (cur_position.buffer));
}
Exemplo n.º 5
0
void cmd_write (char *cp)

{
  int rc;
  Line *line;
  Position beg_write, end_write;

  out_file  = NULL;
  linecount = 0;

  /* Get filename */

  out_name = cp;
  cp = uptospace (cp);
  if (*cp != 0) {
    *(cp ++) = 0;
    cp = skipspaces (cp);
  }

  /* Get optional range.  If not specified, use the whole current buffer. */

  if (*cp != 0) {
    rc = range_multiple (cp, &cp, write_range, out_name);
    if (rc == 0) eoltest (cp);
  } else {
    for (line = buffer_first_line (cur_position.buffer); line != NULL; line = line_next (line)) {
      rc = write_range (out_name, cur_position.buffer, line);
      if (rc != 0) break;
    }
  }

  /* Close output file */

  if (out_file == NULL) outerr (0, "no file created\n");
  else {
    if (linecount == 0) outerr (strlen (out_name), "no lines written to %s\n", out_name);
    else outerr (12 + strlen (out_name), "%u line%s written to %s\n", linecount, (linecount == 1) ? "" : "s", out_name);
    if (fclose (out_file) < 0) outerr (strlen (out_name) + strlen (strerror (errno)), "error closing file %s: %s\n", out_name, strerror (errno));
    out_file = NULL;
  }
}
Exemplo n.º 6
0
void cmd_set (char *cp)

{
    Buffer *buffer;
    char c, *p;
    int i, v;

    /* Set autoshift count */

    if (i = matchkeyword (cp, "autoshift", 1))
    {
        if (cp[i] > ' ') goto usage;
        cp = skipspaces (cp + i);
        v = strtol (cp, &p, 10);
        if ((p == cp) || !eoltest (p)) goto usage;
        autoshift = v;
        return;
    }

    /* Set lfs to hide or show */

    if (i = matchkeyword (cp, "lfs", 1))
    {
        if (cp[i] > ' ') goto usage;
        cp = skipspaces (cp + i);
        if (i = matchkeyword (cp, "hide", 1)) showlfs = 0;
        else if (i = matchkeyword (cp, "show", 1)) showlfs = 1;
        else goto usage;
        if (!eoltest (cp + i)) goto usage;
        return;
    }

    /* Set numbers to hide or show */

    if (i = matchkeyword (cp, "numbers", 1))
    {
        if (cp[i] > ' ') goto usage;
        cp = skipspaces (cp + i);
        if (i = matchkeyword (cp, "auto", 1)) shownums = -1;
        else if (i = matchkeyword (cp, "hide", 1)) shownums = 0;
        else if (i = matchkeyword (cp, "show", 1)) shownums = 1;
        else goto usage;
        if (!eoltest (cp + i)) goto usage;
        return;
    }

    /* Set search to exact or generic */

    if (i = matchkeyword (cp, "search", 1))
    {
        if (cp[i] > ' ') goto usage;
        cp = skipspaces (cp + i);
        if (i = matchkeyword (cp, "exact", 1))
        {
            xstrstr  = strstr;
            xstrncmp = strncmp;
        }
        else if (i = matchkeyword (cp, "generic", 1))
        {
            xstrstr  = strcasestr;
            xstrncmp = strncasecmp;
        }
        else goto usage;
        if (!eoltest (cp + i)) goto usage;
        return;
    }

    /* Set buffer attributes (-output filename or -readonly) */

    if (*cp == '=')
    {
        for (p = ++ cp; (c = *cp) != 0; cp ++) if (strchr (bufnamechars, c) == NULL) break;
        if (cp == p) goto usage;
        if (*cp > ' ') goto usage;
        buffer = buffer_create (cp - p, p);
        cp = skipspaces (cp);
        if ((cp[7] <= ' ') && (strncasecmp (cp, "-output", 7) == 0))
        {
            cp = skipspaces (cp + 7);
            p  = uptospace (cp);
            if (*p != 0) goto usage;
            buffer_setfile (buffer, cp);
            return;
        }
        if ((cp[9] <= ' ') && (strncasecmp (cp, "-readonly", 9) == 0))
        {
            cp = skipspaces (cp + 9);
            if (*cp != 0) goto usage;
            buffer_setfile (buffer, NULL);
            return;
        }
        goto usage;
    }

usage:
    outerr (0, "set autoshift <count>\n\n");
    outerr (0, "set lfs {hide | show}\n\n");
    outerr (0, "set numbers {auto | hide | show}\n\n");
    outerr (0, "set search {exact | generic}\n\n");
    outerr (0, "set =<buffer> -output <filename>\n");
    outerr (0, "              -readonly\n\n");
}