Subversion Repositories SvarDOS

Rev

Rev 1319 | Rev 1321 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 1319 Rev 1320
Line 52... Line 52...
52
  unsigned short len;
52
  unsigned short len;
53
  char payload[1];
53
  char payload[1];
54
};
54
};
55
 
55
 
56
struct file {
56
struct file {
-
 
57
  int fd;
57
  struct line far *cursor;
58
  struct line far *cursor;
58
  unsigned short xoffset;
59
  unsigned short xoffset;
59
  unsigned char cursorposx;
60
  unsigned char cursorposx;
60
  unsigned char cursorposy;
61
  unsigned char cursorposy;
61
  char lfonly; /* set if line endings are LF (CR/LF otherwise) */
62
  char lfonly; /* set if line endings are LF (CR/LF otherwise) */
-
 
63
  char fname[1]; /* dynamically sized */
62
};
64
};
63
 
65
 
64
 
66
 
65
/* returns non-zero on error */
67
/* returns non-zero on error */
66
static int line_add(struct file *db, const char far *line) {
68
static int line_add(struct file *db, const char far *line) {
Line 106... Line 108...
106
  scheme[COL_STATUSBAR2] = 0x78;
108
  scheme[COL_STATUSBAR2] = 0x78;
107
  scheme[COL_SCROLLBAR] = 0x70;
109
  scheme[COL_SCROLLBAR] = 0x70;
108
}
110
}
109
 
111
 
110
 
112
 
111
static void ui_basic(unsigned char screenw, unsigned char screenh, const char *fname, const struct file *db) {
113
static void ui_basic(unsigned char screenw, unsigned char screenh, const struct file *db) {
112
  unsigned char i;
114
  unsigned char i;
113
  const char *s = svarlang_strid(0); /* HELP */
115
  const char *s = svarlang_strid(0); /* HELP */
114
  unsigned char helpcol = screenw - (strlen(s) + 4);
116
  unsigned char helpcol = screenw - (strlen(s) + 4);
115
 
117
 
116
  /* fill status bar with background */
118
  /* fill status bar with background */
117
  mdr_cout_char_rep(screenh - 1, 0, ' ', scheme[COL_STATUSBAR1], screenw);
119
  mdr_cout_char_rep(screenh - 1, 0, ' ', scheme[COL_STATUSBAR1], screenw);
118
 
120
 
119
  /* filename */
121
  /* filename */
120
  mdr_cout_str(screenh - 1, 0, fname, scheme[COL_STATUSBAR1], screenw);
122
  mdr_cout_str(screenh - 1, 0, db->fname, scheme[COL_STATUSBAR1], screenw);
121
 
123
 
122
  /* eol type */
124
  /* eol type */
123
  if (db->lfonly) {
125
  if (db->lfonly) {
124
    mdr_cout_str(screenh - 1, helpcol - 3, "LF", scheme[COL_STATUSBAR1], 5);
126
    mdr_cout_str(screenh - 1, helpcol - 3, "LF", scheme[COL_STATUSBAR1], 5);
125
  } else {
127
  } else {
Line 385... Line 387...
385
 
387
 
386
  return(argv[0]);
388
  return(argv[0]);
387
}
389
}
388
 
390
 
389
 
391
 
390
static int loadfile(struct file *db, const char *fname) {
392
static struct file *loadfile(const char *fname) {
391
  char buff[1024];
393
  char buff[1024];
392
  unsigned int prevlen = 0, len, llen;
394
  unsigned int prevlen = 0, len, llen;
393
  int fd;
395
  int fd;
394
  int r = 0;
396
  struct file *db;
395
 
397
 
396
  if (_dos_open(fname, O_RDONLY, &fd) != 0) {
398
  if (_dos_open(fname, O_RDONLY, &fd) != 0) {
397
    mdr_coutraw_puts("Failed to open file:");
399
    mdr_coutraw_puts("Failed to open file:");
398
    mdr_coutraw_puts(fname);
400
    mdr_coutraw_puts(fname);
399
    return(-1);
401
    return(NULL);
400
  }
402
  }
401
 
403
 
-
 
404
  len = strlen(fname) + 1;
-
 
405
  db = calloc(1, sizeof(struct file) + len);
-
 
406
  if (db == NULL) {
-
 
407
    _dos_close(fd);
-
 
408
    return(NULL);
-
 
409
  }
-
 
410
  memcpy(db->fname, fname, len);
-
 
411
 
402
  db->lfonly = 1;
412
  db->lfonly = 1;
403
 
413
 
404
  do {
414
  do {
405
    if (_dos_read(fd, buff + prevlen, sizeof(buff) - prevlen, &len) == 0) {
415
    if (_dos_read(fd, buff + prevlen, sizeof(buff) - prevlen, &len) == 0) {
406
      len += prevlen;
416
      len += prevlen;
Line 408... Line 418...
408
      len = prevlen;
418
      len = prevlen;
409
    }
419
    }
410
 
420
 
411
    /* look for nearest \n and replace with 0*/
421
    /* look for nearest \n and replace with 0*/
412
    for (llen = 0; buff[llen] != '\n'; llen++) {
422
    for (llen = 0; buff[llen] != '\n'; llen++) {
413
      if (llen == sizeof(buff)) break;
423
      if (llen == sizeof(buff)) { /* TODO line too long: handle it in some classy way */
-
 
424
        break;
-
 
425
      }
414
    }
426
    }
415
    buff[llen] = 0;
427
    buff[llen] = 0;
416
    if ((llen > 0) && (buff[llen - 1] == '\r')) {
428
    if ((llen > 0) && (buff[llen - 1] == '\r')) {
417
      buff[llen - 1] = 0; /* trim \r if line ending is cr/lf */
429
      buff[llen - 1] = 0; /* trim \r if line ending is cr/lf */
418
      db->lfonly = 0;
430
      db->lfonly = 0;
419
    }
431
    }
420
    if (line_add(db, buff) != 0) {
432
    if (line_add(db, buff) != 0) {
421
      mdr_coutraw_puts("out of memory");
433
      mdr_coutraw_puts("out of memory");
-
 
434
      free(db);
422
      r = -1;
435
      db = NULL;
423
      break;
436
      break;
424
    }
437
    }
425
 
438
 
426
    len -= llen + 1;
439
    len -= llen + 1;
427
    memmove(buff, buff + llen + 1, len);
440
    memmove(buff, buff + llen + 1, len);
428
    prevlen = len;
441
    prevlen = len;
429
  } while (len > 0);
442
  } while (len > 0);
430
 
443
 
431
  _dos_close(fd);
444
  _dos_close(fd);
432
 
445
 
-
 
446
  /* add an empty line at end if not present already, also rewind cursor to top of file */
-
 
447
  if (db != NULL) {
-
 
448
    if (db->cursor->len != 0) line_add(db, "");
-
 
449
    db_rewind(db);
-
 
450
  }
-
 
451
 
433
  return(r);
452
  return(db);
434
}
453
}
435
 
454
 
436
 
455
 
437
static int savefile(const struct file *db, const char *fname) {
456
static int savefile(const struct file *db) {
438
  int fd;
457
  int fd;
439
  const struct line far *l;
458
  const struct line far *l;
440
  unsigned bytes;
459
  unsigned bytes;
441
  unsigned char eollen;
460
  unsigned char eollen;
442
  unsigned char eolbuf[2];
461
  unsigned char eolbuf[2];
443
 
462
 
444
  if (_dos_open(fname, O_WRONLY, &fd) != 0) {
463
  if (_dos_open(db->fname, O_WRONLY, &fd) != 0) {
445
    return(-1);
464
    return(-1);
446
  }
465
  }
447
 
466
 
448
  l = db->cursor;
467
  l = db->cursor;
449
  while (l->prev) l = l->prev;
468
  while (l->prev) l = l->prev;
Line 490... Line 509...
490
}
509
}
491
 
510
 
492
 
511
 
493
int main(void) {
512
int main(void) {
494
  const char *fname;
513
  const char *fname;
495
  struct file db;
514
  struct file *db;
496
  unsigned char screenw = 0, screenh = 0;
515
  unsigned char screenw = 0, screenh = 0;
497
  unsigned char uidirtyfrom = 0, uidirtyto = 0xff; /* make sure to redraw entire UI at first run */
516
  unsigned char uidirtyfrom = 0, uidirtyto = 0xff; /* make sure to redraw entire UI at first run */
498
 
517
 
499
  bzero(&db, sizeof(db));
-
 
500
 
-
 
501
  {
518
  {
502
    char nlspath[128], lang[8];
519
    char nlspath[128], lang[8];
503
    svarlang_autoload_pathlist("sved", mdr_dos_getenv(nlspath, "NLSPATH", sizeof(nlspath)), mdr_dos_getenv(lang, "LANG", sizeof(lang)));
520
    svarlang_autoload_pathlist("sved", mdr_dos_getenv(nlspath, "NLSPATH", sizeof(nlspath)), mdr_dos_getenv(lang, "LANG", sizeof(lang)));
504
  }
521
  }
505
 
522
 
Line 509... Line 526...
509
    mdr_coutraw_puts(svarlang_str(1,0)); /* usage: sved file.txt */
526
    mdr_coutraw_puts(svarlang_str(1,0)); /* usage: sved file.txt */
510
    return(0);
527
    return(0);
511
  }
528
  }
512
 
529
 
513
  /* load file */
530
  /* load file */
514
  if (loadfile(&db, fname) != 0) return(1);
531
  db = loadfile(fname);
515
 
-
 
516
  /* add an empty line at end if not present already */
-
 
517
  if (db.cursor->len != 0) line_add(&db, "");
532
  if (db == NULL) return(1);
518
 
533
 
519
  if (mdr_cout_init(&screenw, &screenh)) load_colorscheme();
534
  if (mdr_cout_init(&screenw, &screenh)) load_colorscheme();
520
  ui_basic(screenw, screenh, fname, &db);
535
  ui_basic(screenw, screenh, db);
521
 
-
 
522
  db_rewind(&db);
-
 
523
 
536
 
524
  for (;;) {
537
  for (;;) {
525
    int k;
538
    int k;
526
 
539
 
527
    check_cursor_not_after_eol(&db, &uidirtyfrom, &uidirtyto);
540
    check_cursor_not_after_eol(db, &uidirtyfrom, &uidirtyto);
528
    mdr_cout_locate(db.cursorposy, db.cursorposx);
541
    mdr_cout_locate(db->cursorposy, db->cursorposx);
529
 
542
 
530
    if (uidirtyfrom != 0xff) {
543
    if (uidirtyfrom != 0xff) {
531
      ui_refresh(&db, screenw, screenh, uidirtyfrom, uidirtyto);
544
      ui_refresh(db, screenw, screenh, uidirtyfrom, uidirtyto);
532
      uidirtyfrom = 0xff;
545
      uidirtyfrom = 0xff;
533
    }
546
    }
534
 
547
 
535
    k = keyb_getkey();
548
    k = keyb_getkey();
536
 
549
 
537
    if (k == 0x150) { /* down */
550
    if (k == 0x150) { /* down */
538
      cursor_down(&db, screenh, &uidirtyfrom, &uidirtyto);
551
      cursor_down(db, screenh, &uidirtyfrom, &uidirtyto);
539
 
552
 
540
    } else if (k == 0x148) { /* up */
553
    } else if (k == 0x148) { /* up */
541
      cursor_up(&db, &uidirtyfrom, &uidirtyto);
554
      cursor_up(db, &uidirtyfrom, &uidirtyto);
542
 
555
 
543
    } else if (k == 0x14D) { /* right */
556
    } else if (k == 0x14D) { /* right */
544
      cursor_right(&db, screenw, screenh, &uidirtyfrom, &uidirtyto);
557
      cursor_right(db, screenw, screenh, &uidirtyfrom, &uidirtyto);
545
 
558
 
546
    } else if (k == 0x14B) { /* left */
559
    } else if (k == 0x14B) { /* left */
547
      cursor_left(&db, screenw, &uidirtyfrom, &uidirtyto);
560
      cursor_left(db, screenw, &uidirtyfrom, &uidirtyto);
548
 
561
 
549
    } else if (k == 0x149) { /* pgup */
562
    } else if (k == 0x149) { /* pgup */
550
      // TODO
563
      // TODO
551
 
564
 
552
    } else if (k == 0x151) { /* pgdown */
565
    } else if (k == 0x151) { /* pgdown */
553
      // TODO
566
      // TODO
554
 
567
 
555
    } else if (k == 0x147) { /* home */
568
    } else if (k == 0x147) { /* home */
556
       cursor_home(&db, &uidirtyfrom, &uidirtyto);
569
       cursor_home(db, &uidirtyfrom, &uidirtyto);
557
 
570
 
558
    } else if (k == 0x14F) { /* end */
571
    } else if (k == 0x14F) { /* end */
559
       cursor_eol(&db, screenw, &uidirtyfrom, &uidirtyto);
572
       cursor_eol(db, screenw, &uidirtyfrom, &uidirtyto);
560
 
573
 
561
    } else if (k == 0x1B) { /* ESC */
574
    } else if (k == 0x1B) { /* ESC */
562
      break;
575
      break;
563
 
576
 
564
    } else if (k == 0x0D) { /* ENTER */
577
    } else if (k == 0x0D) { /* ENTER */
565
      /* add a new line */
578
      /* add a new line */
566
      if (line_add(&db, db.cursor->payload + db.xoffset + db.cursorposx) == 0) {
579
      if (line_add(db, db->cursor->payload + db->xoffset + db->cursorposx) == 0) {
567
        /* trim the line above */
580
        /* trim the line above */
568
        db.cursor->prev->len = db.xoffset + db.cursorposx;
581
        db->cursor->prev->len = db->xoffset + db->cursorposx;
569
        db.cursor->prev->payload[db.cursor->prev->len] = 0;
582
        db->cursor->prev->payload[db->cursor->prev->len] = 0;
570
        /* move cursor to the (new) line below */
583
        /* move cursor to the (new) line below */
571
        db.cursorposx = 0;
584
        db->cursorposx = 0;
572
        if (db.cursorposy < screenh - 2) {
585
        if (db->cursorposy < screenh - 2) {
573
          uidirtyfrom = db.cursorposy;
586
          uidirtyfrom = db->cursorposy;
574
          db.cursorposy++;
587
          db->cursorposy++;
575
        } else {
588
        } else {
576
          uidirtyfrom = 0;
589
          uidirtyfrom = 0;
577
        }
590
        }
578
        uidirtyto = 0xff;
591
        uidirtyto = 0xff;
579
      } else {
592
      } else {
580
        /* ERROR: OUT OF MEMORY */
593
        /* ERROR: OUT OF MEMORY */
581
      }
594
      }
582
 
595
 
583
    } else if (k == 0x153) {  /* DEL */
596
    } else if (k == 0x153) {  /* DEL */
584
      del(&db, &uidirtyfrom, &uidirtyto);
597
      del(db, &uidirtyfrom, &uidirtyto);
585
 
598
 
586
    } else if (k == 0x008) { /* BKSPC */
599
    } else if (k == 0x008) { /* BKSPC */
587
      bkspc(&db, screenw, &uidirtyfrom, &uidirtyto);
600
      bkspc(db, screenw, &uidirtyfrom, &uidirtyto);
588
 
601
 
589
    } else if ((k >= 0x20) && (k <= 0xff)) { /* "normal" character */
602
    } else if ((k >= 0x20) && (k <= 0xff)) { /* "normal" character */
590
      char c = k;
603
      char c = k;
591
      insert_in_line(&db, &c, 1, screenw, screenh, &uidirtyfrom, &uidirtyto);
604
      insert_in_line(db, &c, 1, screenw, screenh, &uidirtyfrom, &uidirtyto);
592
 
605
 
593
    } else if (k == 0x009) { /* TAB */
606
    } else if (k == 0x009) { /* TAB */
594
      const char *tab = "        ";
607
      const char *tab = "        ";
595
      insert_in_line(&db, tab, 8, screenw, screenh, &uidirtyfrom, &uidirtyto);
608
      insert_in_line(db, tab, 8, screenw, screenh, &uidirtyfrom, &uidirtyto);
596
 
609
 
597
    } else if (k == 0x13b) { /* F1 */
610
    } else if (k == 0x13b) { /* F1 */
598
      ui_help(screenw);
611
      ui_help(screenw);
599
      uidirtyfrom = 0;
612
      uidirtyfrom = 0;
600
      uidirtyto = 0xff;
613
      uidirtyto = 0xff;
601
 
614
 
602
    } else if (k == 0x13f) { /* F5 */
615
    } else if (k == 0x13f) { /* F5 */
603
      const char *m[] = {"SAVED", "SAVING FILE FAILED"};
616
      const char *m[] = {"SAVED", "SAVING FILE FAILED"};
604
      int i;
617
      int i;
605
      i = !!savefile(&db, fname);
618
      i = !!savefile(db);
606
      ui_msg(m[i], screenw, screenh, &uidirtyfrom, &uidirtyto);
619
      ui_msg(m[i], screenw, screenh, &uidirtyfrom, &uidirtyto);
607
      mdr_bios_tickswait(11); /* 11 ticks is about 600 ms */
620
      mdr_bios_tickswait(11); /* 11 ticks is about 600 ms */
608
 
621
 
609
    } else if (k == 0x144) { /* F10 */
622
    } else if (k == 0x144) { /* F10 */
610
      db.lfonly ^= 1;
623
      db->lfonly ^= 1;
611
      ui_basic(screenw, screenh, fname, &db);
624
      ui_basic(screenw, screenh, db);
612
 
625
 
613
    } else { /* UNHANDLED KEY - TODO IGNORE THIS IN PRODUCTION RELEASE */
626
    } else { /* UNHANDLED KEY - TODO IGNORE THIS IN PRODUCTION RELEASE */
614
      char buff[4];
627
      char buff[4];
615
      const char *HEX = "0123456789ABCDEF";
628
      const char *HEX = "0123456789ABCDEF";
616
      buff[0] = HEX[(k >> 8) & 15];
629
      buff[0] = HEX[(k >> 8) & 15];
Line 623... Line 636...
623
    }
636
    }
624
  }
637
  }
625
 
638
 
626
  mdr_cout_close();
639
  mdr_cout_close();
627
 
640
 
628
  /* TODO free memory */
641
  /* no need to free memory, DOS will do it for me */
629
 
642
 
630
  return(0);
643
  return(0);
631
}
644
}