Subversion Repositories SvarDOS

Rev

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

Rev 577 Rev 949
Line 428... Line 428...
428
 
428
 
429
  RUNCMDFILE:
429
  RUNCMDFILE:
430
 
430
 
431
  /* special handling of batch files */
431
  /* special handling of batch files */
432
  if ((ext != NULL) && (imatch(ext, "bat"))) {
432
  if ((ext != NULL) && (imatch(ext, "bat"))) {
-
 
433
    /* free the bat-context linked list, if present, and replace it with a new entry */
-
 
434
    while (rmod->bat != NULL) {
-
 
435
      struct batctx far *victim = rmod->bat;
-
 
436
      rmod->bat = rmod->bat->parent;
-
 
437
      rmod_ffree(victim);
-
 
438
    }
-
 
439
    rmod->bat = rmod_fmalloc(sizeof(struct batctx), rmod->rmodseg, "SVBATCTX");
-
 
440
    if (rmod->bat == NULL) {
-
 
441
      outputnl("INTERNAL ERR: OUT OF MEMORY");
-
 
442
      return;
-
 
443
    }
-
 
444
    _fmemset(rmod->bat, 0, sizeof(struct batctx));
-
 
445
 
433
    /* copy truename of the bat file to rmod buff */
446
    /* copy truename of the bat file to rmod buff */
434
    _fstrcpy(rmod->batfile, cmdfile);
447
    _fstrcpy(rmod->bat->fname, cmdfile);
435
 
448
 
436
    /* explode args of the bat file and store them in rmod buff */
449
    /* explode args of the bat file and store them in rmod buff */
437
    cmd_explode(buff, cmdline, NULL);
450
    cmd_explode(buff, cmdline, NULL);
438
    _fmemcpy(rmod->batargv, buff, sizeof(rmod->batargv));
451
    _fmemcpy(rmod->bat->argv, buff, sizeof(rmod->bat->argv));
439
 
452
 
440
    /* reset the 'next line to execute' counter */
453
    /* reset the 'next line to execute' counter */
441
    rmod->batnextline = 0;
454
    rmod->bat->nextline = 0;
442
    /* remember the echo flag (in case bat file disables echo) */
455
    /* remember the echo flag (in case bat file disables echo) */
443
    rmod->flags &= ~FLAG_ECHO_BEFORE_BAT;
456
    rmod->flags &= ~FLAG_ECHO_BEFORE_BAT;
444
    if (rmod->flags & FLAG_ECHOFLAG) rmod->flags |= FLAG_ECHO_BEFORE_BAT;
457
    if (rmod->flags & FLAG_ECHOFLAG) rmod->flags |= FLAG_ECHO_BEFORE_BAT;
445
    return;
458
    return;
446
  }
459
  }
Line 566... Line 579...
566
 
579
 
567
/* fetches a line from batch file and write it to buff (NULL-terminated),
580
/* fetches a line from batch file and write it to buff (NULL-terminated),
568
 * increments rmod counter and returns 0 on success. */
581
 * increments rmod counter and returns 0 on success. */
569
static int getbatcmd(char *buff, unsigned char buffmaxlen, struct rmod_props far *rmod) {
582
static int getbatcmd(char *buff, unsigned char buffmaxlen, struct rmod_props far *rmod) {
570
  unsigned short i;
583
  unsigned short i;
571
  unsigned short batname_seg = FP_SEG(rmod->batfile);
584
  unsigned short batname_seg = FP_SEG(rmod->bat->fname);
572
  unsigned short batname_off = FP_OFF(rmod->batfile);
585
  unsigned short batname_off = FP_OFF(rmod->bat->fname);
573
  unsigned short filepos_cx = rmod->batnextline >> 16;
586
  unsigned short filepos_cx = rmod->bat->nextline >> 16;
574
  unsigned short filepos_dx = rmod->batnextline & 0xffff;
587
  unsigned short filepos_dx = rmod->bat->nextline & 0xffff;
575
  unsigned char blen = 0;
588
  unsigned char blen = 0;
576
  unsigned short errv = 0;
589
  unsigned short errv = 0;
577
 
590
 
578
  /* open file, jump to offset filpos, and read data into buff.
591
  /* open file, jump to offset filpos, and read data into buff.
579
   * result in blen (unchanged if EOF or failure). */
592
   * result in blen (unchanged if EOF or failure). */
Line 638... Line 651...
638
 
651
 
639
  /* find nearest \n to inc batch offset and replace \r by NULL terminator
652
  /* find nearest \n to inc batch offset and replace \r by NULL terminator
640
   * I support all CR/LF, CR- and LF-terminated batch files */
653
   * I support all CR/LF, CR- and LF-terminated batch files */
641
  for (i = 0; i < blen; i++) {
654
  for (i = 0; i < blen; i++) {
642
    if ((buff[i] == '\r') || (buff[i] == '\n')) {
655
    if ((buff[i] == '\r') || (buff[i] == '\n')) {
643
      if ((buff[i] == '\r') && ((i+1) < blen) && (buff[i+1] == '\n')) rmod->batnextline += 1;
656
      if ((buff[i] == '\r') && ((i+1) < blen) && (buff[i+1] == '\n')) rmod->bat->nextline += 1;
644
      break;
657
      break;
645
    }
658
    }
646
  }
659
  }
647
  buff[i] = 0;
660
  buff[i] = 0;
648
  rmod->batnextline += i + 1;
661
  rmod->bat->nextline += i + 1;
649
 
662
 
650
  return(0);
663
  return(0);
651
 
664
 
652
  OOPS:
665
  OOPS:
653
  rmod->batfile[0] = 0;
666
  rmod->bat->fname[0] = 0;
654
  rmod->batnextline = 0;
667
  rmod->bat->nextline = 0;
655
  return(-1);
668
  return(-1);
656
}
669
}
657
 
670
 
658
 
671
 
659
/* replaces %-variables in a BAT line with resolved values:
672
/* replaces %-variables in a BAT line with resolved values:
Line 700... Line 713...
700
 
713
 
701
    /* digit? (bat arg) */
714
    /* digit? (bat arg) */
702
    if ((line[1] >= '0') && (line[1] <= '9')) {
715
    if ((line[1] >= '0') && (line[1] <= '9')) {
703
      unsigned short argid = line[1] - '0';
716
      unsigned short argid = line[1] - '0';
704
      unsigned short i;
717
      unsigned short i;
705
      const char far *argv = rmod->batargv;
718
      const char far *argv = "";
-
 
719
      if ((rmod != NULL) && (rmod->bat != NULL)) argv = rmod->bat->argv;
706
 
720
 
707
      /* locate the proper arg */
721
      /* locate the proper arg */
708
      for (i = 0; i != argid; i++) {
722
      for (i = 0; i != argid; i++) {
709
        /* if string is 0, then end of list reached */
723
        /* if string is 0, then end of list reached */
710
        if (*argv == 0) break;
724
        if (*argv == 0) break;
Line 795... Line 809...
795
    if (file_getattr("AUTOEXEC.BAT") >= 0) cfg.execcmd = "AUTOEXEC.BAT";
809
    if (file_getattr("AUTOEXEC.BAT") >= 0) cfg.execcmd = "AUTOEXEC.BAT";
796
  }
810
  }
797
 
811
 
798
  do {
812
  do {
799
    /* terminate previous command with a CR/LF if ECHO ON (but not during BAT processing) */
813
    /* terminate previous command with a CR/LF if ECHO ON (but not during BAT processing) */
800
    if ((rmod->flags & FLAG_ECHOFLAG) && (rmod->batfile[0] == 0)) outputnl("");
814
    if ((rmod->flags & FLAG_ECHOFLAG) && (rmod->bat == NULL)) outputnl("");
801
 
815
 
802
    SKIP_NEWLINE:
816
    SKIP_NEWLINE:
803
 
817
 
804
    /* memory check */
818
    /* memory check */
805
    memguard_check(rmod->rmodseg, cmdlinebuf);
819
    memguard_check(rmod->rmodseg, cmdlinebuf);
Line 826... Line 840...
826
      cfg.execcmd = NULL;
840
      cfg.execcmd = NULL;
827
      goto EXEC_CMDLINE;
841
      goto EXEC_CMDLINE;
828
    }
842
    }
829
 
843
 
830
    /* if batch file is being executed -> fetch next line */
844
    /* if batch file is being executed -> fetch next line */
831
    if (rmod->batfile[0] != 0) {
845
    if (rmod->bat != NULL) {
832
      if (getbatcmd(BUFFER, CMDLINE_MAXLEN, rmod) != 0) { /* end of batch */
846
      if (getbatcmd(BUFFER, CMDLINE_MAXLEN, rmod) != 0) { /* end of batch */
-
 
847
        struct batctx far *victim = rmod->bat;
-
 
848
        rmod->bat = rmod->bat->parent;
-
 
849
        rmod_ffree(victim);
833
        /* restore echo flag as it was before running the bat file */
850
        /* end of batch? then restore echo flag as it was before running the bat file */
-
 
851
        if (rmod->bat == NULL) {
834
        rmod->flags &= ~FLAG_ECHOFLAG;
852
          rmod->flags &= ~FLAG_ECHOFLAG;
835
        if (rmod->flags & FLAG_ECHO_BEFORE_BAT) rmod->flags |= FLAG_ECHOFLAG;
853
          if (rmod->flags & FLAG_ECHO_BEFORE_BAT) rmod->flags |= FLAG_ECHOFLAG;
-
 
854
        }
836
        continue;
855
        continue;
837
      }
856
      }
838
      /* %-decoding of variables (%PATH%, %1, %%...), result in cmdline */
857
      /* %-decoding of variables (%PATH%, %1, %%...), result in cmdline */
839
      batpercrepl(cmdline, CMDLINE_MAXLEN, BUFFER, rmod, *rmod_envseg);
858
      batpercrepl(cmdline, CMDLINE_MAXLEN, BUFFER, rmod, *rmod_envseg);
840
      /* skip any leading spaces */
859
      /* skip any leading spaces */
Line 887... Line 906...
887
      goto EXEC_CMDLINE;
906
      goto EXEC_CMDLINE;
888
    } else if (cmdres == CMD_NOTFOUND) {
907
    } else if (cmdres == CMD_NOTFOUND) {
889
      /* this was not an internal command, try matching an external command */
908
      /* this was not an internal command, try matching an external command */
890
      run_as_external(BUFFER, cmdline, *rmod_envseg, rmod, &redirprops, delete_stdin_file);
909
      run_as_external(BUFFER, cmdline, *rmod_envseg, rmod, &redirprops, delete_stdin_file);
891
      /* perhaps this is a newly launched BAT file */
910
      /* perhaps this is a newly launched BAT file */
892
      if ((rmod->batfile[0] != 0) && (rmod->batnextline == 0)) goto SKIP_NEWLINE;
911
      if ((rmod->bat != NULL) && (rmod->bat->nextline == 0)) goto SKIP_NEWLINE;
893
      /* run_as_external() does not return on success, if I am still alive then
912
      /* run_as_external() does not return on success, if I am still alive then
894
       * external command failed to execute */
913
       * external command failed to execute */
895
      outputnl("Bad command or file name");
914
      outputnl("Bad command or file name");
896
      continue;
915
      continue;
897
    }
916
    }