Beispiel #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 */
    }
}
Beispiel #2
0
void cmd_exit (char *cp)

{
  const char *bn, *fn, *ynanswer;
  Buffer *buffer;
  int prompt, saveflag, ynflag;
  String *ynstring;

  /* Check for -save qualifier - it inhibits deleting the journal file */

  saveflag = 0;
  if ((strncasecmp (cp, "-save", 5) == 0) && (cp[5] <= ' ')) {
    if ((journal_name == NULL) || (journal_name[0] == 0)) {
      outerr (0, "there is no journal file to save\n");
      return;
    }
    saveflag = 1;
    cp = skipspaces (cp + 5);
  }

  /* Maybe use a different name for main buffer */

  if (*cp != 0) buffer_setfile (main_buffer, cp);

  /* Don't bother if recovery mode, we want them to be able to do more.  They can re-type exit command */

  if (recover_file != NULL) {
    outerr (0, "EXIT command ignored in recovery mode\n");
    return;
  }

  /* Check for buffers that have filename of "" (opened -readonly) but were modified */

  prompt = 0;
  for (buffer = NULL; (buffer = buffer_next (buffer)) != NULL;) {
    fn = buffer_filename (buffer);
    if (fn == NULL) continue;
    if (fn[0] != 0) continue;
    if (buffer_dirty (buffer, -1)) {
      bn = buffer_name (buffer);
      if (!prompt) outerr (0, "\n");
      outerr (strlen (bn), "buffer %s was modified but has no output file\n", bn);
      prompt = 1;
    }
  }
  if (prompt) {
    do {
      ynstring = jnl_readprompt ("\r\n  do you still want to exit (yes or no)? ");
      if (ynstring == NULL) return;
      ynflag   = -1;
      ynanswer = string_getval (ynstring);
      if (strcasecmp (ynanswer, "no")  == 0) ynflag = 0;
      if (strcasecmp (ynanswer, "yes") == 0) ynflag = 1;
      string_delete (ynstring);
    } while (ynflag < 0);
    if (!ynflag) return;
  }

  /* Write the entire contents of all the buffers that have files and that have been modified to their respective files */

  for (buffer = NULL; (buffer = buffer_next (buffer)) != NULL;) {
    fn = buffer_filename (buffer);							/* get output filename */
    if (fn == NULL) continue;								/* if none, don't try to write */
    bn = buffer_name (buffer);								/* ok, write it out */
    if (!buffer_dirty (buffer, -1)) {
      outerr (strlen (fn) + strlen (bn), "not writing %s from =%s because it is unmodified\n", fn, bn);
      continue;
    }
    outerr (strlen (fn) + strlen (bn), "writing %s from =%s: ", fn, bn);
    if (!write_file (fn, buffer_first_line (buffer), buffer_last_line (buffer))) {
      outerr (strlen (fn), "output file %s not written, not exiting\n", fn);		/* if error, don't exit */
      return;
    }
    buffer_dirty (buffer, 0);
  }

  /* Write successful, maybe delete journal file and terminate process */

  jnl_close (!saveflag);
  output ();
  exit (0);
}
Beispiel #3
0
int main (int argc, char *argv[])

{
  setvbuf(stdout, NULL, _IONBF, 0);      // need this to see i/o at all
  char *buf, *init_name, *output_name, *p;
  const char *cmdpnt;
  FILE *init_file;
  int i, len, rdonly, recover, siz;
  String *cmdstr;
  uLong sts;

  if (argc > 0) pn = argv[0];

  fprintf (stderr, 
	"Copyright (C) 2001,2002,2003,2004 Mike Rieker, Beverly, MA USA\n"
	"Version 2004-06-10, EDT comes with ABSOLUTELY NO WARRANTY\n"
	"EXPECT it to FAIL when someone's HeALTh or PROpeRTy is at RISk\n\n");
  fflush (stderr);

  /* Parse command line */

  init_name    = NULL;
  input_name   = NULL;
  journal_file = NULL;
  journal_name = NULL;
  output_name  = NULL;
  rdonly       = 0;
  recover      = 0;
  recover_file = NULL;
  recover_name = NULL;

  for (i = 1; i < argc; i ++) {

    /* -init <file> : process the given initialization file */

    if (strcasecmp (argv[i], "-init") == 0) {
      if (++ i >= argc) goto usage;
      if (argv[i][0] == '-') goto usage;
      if (init_name != NULL) goto usage;
      init_name = argv[i];
      continue;
    }

    /* -journal <file> : write journal to specified file */
    /* by default, it gets written to <output_file>.edtj */

    if (strcasecmp (argv[i], "-journal") == 0) {
      if (++ i >= argc) goto usage;
      if (argv[i][0] == '-') goto usage;
      journal_name = argv[i];
      continue;
    }

    /* -noinit : don't process default init file */

    if (strcasecmp (argv[i], "-noinit") == 0) {
      if (init_name != NULL) goto usage;
      init_name = "";
      continue;
    }

    /* -output <file> : write output to specified file */
    /* by default, it gets written to <input_file>     */

    if (strcasecmp (argv[i], "-output") == 0) {
      if (++ i >= argc) goto usage;
      if (argv[i][0] == '-') goto usage;
      output_name = argv[i];
      continue;
    }

    /* -readonly : don't write an output file */

    if (strcasecmp (argv[i], "-readonly") == 0) {
      if (output_name == input_name) output_name = NULL;
      rdonly = 1;
      continue;
    }

    /* -recover [<file>] : process recovery from file           */
    /* by default, recovery is processed from <input_file>.edtj */

    if (strcasecmp (argv[i], "-recover") == 0) {
      recover = 1;
      if (i + 1 == argc) continue;
      if (argv[i+1][0] == '-') continue;
      if ((input_name != NULL) || (i + 2 <= argc)) recover_name = argv[++i];
      continue;
    }

    /* No more options */

    if (argv[i][0] == '-') goto usage;

    /* first and only parameter <file> : input filename */

    if (input_name == NULL) {
      input_name = argv[i];
      if (!rdonly) output_name = input_name;
      continue;
    }
    goto usage;
  }

  /* Open recovery file */

  if (recover && (input_name == NULL)) {
    fprintf (stderr, "no input file specified to recover\n");
    return (-1);
  }
  if (recover && (recover_name == NULL)) recover_name = os_makejnlname (input_name);
  if (recover_name != NULL) {
    recover_file = fopen (recover_name, "r");
    if (recover_file == NULL) {
      fprintf (stderr, "error opening recovery file %s: %s\n", recover_name, strerror (errno));
      return (-1);
    }
  }

  /* Create journal file */

  if ((output_name != NULL) && (journal_name == NULL)) journal_name = os_makejnlname (output_name);
  if (journal_name != NULL) {
    journal_file = os_crenewfile (journal_name);
    if (journal_file == NULL) {
      fprintf (stderr, "error creating journal file %s: %s\n", journal_name, strerror (errno));
      return (-1);
    }
  }

  /* Initialize os dependent routines.  No using stdin/stdout/stderr from now on. */

  os_initialization ();

  /* If input file was given, read it into a buffer and mark that buffer for writing on exit */

  if (input_name != NULL) {
    p = input_name;
    if (rdonly) {
      p = malloc (strlen (input_name) + 12);
      strcpy (p, "-readonly ");
      strcat (p, input_name);
    }
    cmd_open (p);
    if (p != input_name) free (p);
    cur_position.line   = buffer_first_line (cur_position.buffer);
    cur_position.offset = 0;
  }

  /* Otherwise, allocate initial 'MAIN' buffer with no lines in it */

  else {
    cur_position.buffer = buffer_create (4, "MAIN");
    cur_position.line   = NULL;
    cur_position.offset = 0;
  }

  /* Either way, that is the one used by the EXIT command */

  main_buffer = cur_position.buffer;
  if (output_name != NULL) buffer_setfile (main_buffer, output_name);

  /* Process initialization file */

  if (init_name == NULL) init_name = os_defaultinitname ();	/* if no -noinit or -init, get default name */
  if ((init_name != NULL) && (init_name[0] != 0)) {
    init_file = fopen (init_name, "r");				/* try to open init file */
    if (init_file != NULL) {
      siz = 256;						/* start with 256 byte buffer */
      buf = malloc (siz);
      len = 0;							/* don't have anything in it */
      while (fgets (buf + len, siz - len, init_file) != NULL) {	/* read onto end of what's there */
        len += strlen (buf + len);				/* get total length */
        if (len == 0) break;					/* stop if nothing there (eof?) */
        if (buf[len-1] != '\n') {				/* check for line terminator */
          siz += 256;						/* didn't get the whole thing, increase buffer */
          buf  = realloc (buf, siz);				/* ... then loop to read more of the line */
        } else {
          buf[--len] = 0;					/* got terminator, remove it from buffer */
          cmdpnt = skipspaces (buf);				/* skip leading spaces */
          if (*cmdpnt != 0) ln_command (cmdpnt);		/* process command line */
          len = 0;						/* empty line buffer for next read */
        }
      }
      fclose (init_file);
      free (buf);
    } else if (errno != ENOENT) {
      outerr (strlen (init_name) + strlen (strerror (errno)), "error opening init file %s: %s\n", init_name, strerror (errno));
      exit (-1);
    }
  }

  /* Type out line at current position to begin with */

  cmd_type (".");

  /* Read and process commands until eof */

  i = 0;
  while (1) {
    cmdstr = jnl_readprompt ("\r\n*");
    if (cmdstr == NULL) {						/* prompt then read command line */
      if (++ i < 3) outerr (0, "use either EXIT or QUIT to terminate\n");
      else {
        outerr (12, "%d EOF's in a row or fatal terminal error, exiting ...\n", i);
        cmd_exit ("");
        exit (-1);
      }
      continue;
    }
    i = 0;
    for (cmdpnt = string_getval (cmdstr); *cmdpnt != 0; cmdpnt ++) if (*cmdpnt > ' ') break;
    if (*cmdpnt == 0) cmd_type (".+1");					/* blank line means 'type .+1' */
    else ln_command (cmdpnt);						/* process command */
    string_delete (cmdstr);						/* free the command string off */
  }

  /* Bad command line parameter */

usage:
  fprintf (stderr, "usage: %s [-init <init_file>] [-journal <journal_output>] [-noinit] [-output <output_file>] [-readonly] [-recover [<journal_input>]] [<input_file>]\n", pn);
  return (-1);
}