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 */ }
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; } }
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"); }
void cmd_show (char *cp) { char c, curbf, dirty, *p; const char *fname, *name; int l, rf; Buffer *buffer; uLong lines; if (*cp == 0) goto usage; do { p = uptospace (cp); /* Show buffer info */ if (strncasecmp ("buffers", cp, p - cp) == 0) { outfmt (0, "\nBuffers:\n"); l = 0; for (buffer = NULL; (buffer = buffer_next (buffer)) != NULL;) { name = buffer_name (buffer); if (strlen (name) > l) l = strlen (name); } for (buffer = NULL; (buffer = buffer_next (buffer)) != NULL;) { name = buffer_name (buffer); fname = buffer_filename (buffer); lines = buffer_linecount (buffer); rf = (buffer_getreadfile (buffer) != NULL); curbf = (buffer == cur_position.buffer) ? '>' : ' '; dirty = buffer_dirty (buffer, -1) ? '*' : ' '; outfmt (strlen (name) + 16, " %c %c %*.*s: %5u%c line%c", curbf, dirty, l, l, name, lines, rf ? '+' : ' ', (lines == 1) ? ' ' : 's'); if (fname != NULL) outfmt (strlen (fname), " => %s", fname); if (buffer == main_buffer) outstr (" (main buffer)"); outchr ('\n'); } continue; } /* Show info about files */ if (strncasecmp ("files", cp, p - cp) == 0) { outfmt (0, "\nFiles:\n"); outfmt (strlen (help_name), " Help: %s\n", help_name); outfmt (strlen (journal_name), " Journal: %s\n", journal_name[0] == 0 ? "<none>" : journal_name); continue; } /* Show keypad definitions */ if (strncasecmp ("keypad", cp, p - cp) == 0) { show_keypad (); continue; } goto usage; } while (*(cp = skipspaces (p)) != 0); return; usage: outerr (0, "specify BUFFERS, FILES, KEYPAD\n"); }