Subversion Repositories SvarDOS

Rev

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

Rev 2199 Rev 2213
Line 26... Line 26...
26
 * a variety of helper functions
26
 * a variety of helper functions
27
 */
27
 */
28
 
28
 
29
#include <i86.h>    /* MK_FP() */
29
#include <i86.h>    /* MK_FP() */
30
#include <stdio.h>  /* sprintf() */
30
#include <stdio.h>  /* sprintf() */
31
#include <string.h> /* memcpy() */
-
 
32
 
31
 
33
#include "svarlang.lib\svarlang.h"
32
#include "svarlang.lib\svarlang.h"
34
 
33
 
35
#include "env.h"
34
#include "env.h"
36
#include "rmodinit.h"
35
#include "rmodinit.h"
Line 66... Line 65...
66
    mov [bx], dh
65
    mov [bx], dh
67
  }
66
  }
68
}
67
}
69
 
68
 
70
 
69
 
-
 
70
/* like strlen() */
-
 
71
unsigned short sv_strlen(const char *s) {
-
 
72
  unsigned short len = 0;
-
 
73
  while (*s != 0) {
-
 
74
    s++;
-
 
75
    len++;
-
 
76
  }
-
 
77
  return(len);
-
 
78
}
-
 
79
 
-
 
80
 
71
/* case-insensitive comparison of strings, compares up to maxlen characters.
81
/* case-insensitive comparison of strings, compares up to maxlen characters.
72
 * returns non-zero on equality. */
82
 * returns non-zero on equality. */
73
int imatchlim(const char *s1, const char *s2, unsigned short maxlen) {
83
int imatchlim(const char *s1, const char *s2, unsigned short maxlen) {
74
  while (maxlen--) {
84
  while (maxlen--) {
75
    char c1, c2;
85
    char c1, c2;
Line 540... Line 550...
540
 
550
 
541
  return(r);
551
  return(r);
542
}
552
}
543
 
553
 
544
 
554
 
-
 
555
/* like strcpy() but returns the string's length */
-
 
556
unsigned short sv_strcpy(char *dst, const char *s) {
-
 
557
  unsigned short len = 0;
-
 
558
  for (;;) {
-
 
559
    *dst = *s;
-
 
560
    if (*s == 0) return(len);
-
 
561
    dst++;
-
 
562
    s++;
-
 
563
    len++;
-
 
564
  }
-
 
565
}
-
 
566
 
-
 
567
 
-
 
568
/* like sv_strcpy() but operates on far pointers */
-
 
569
unsigned short sv_strcpy_far(char far *dst, const char far *s) {
-
 
570
  unsigned short len = 0;
-
 
571
  for (;;) {
-
 
572
    *dst = *s;
-
 
573
    if (*s == 0) return(len);
-
 
574
    dst++;
-
 
575
    s++;
-
 
576
    len++;
-
 
577
  }
-
 
578
}
-
 
579
 
-
 
580
 
-
 
581
/* like strcat() */
-
 
582
void sv_strcat(char *dst, const char *s) {
-
 
583
  /* advance dst to end of string */
-
 
584
  while (*dst != 0) dst++;
-
 
585
  /* append string */
-
 
586
  sv_strcpy(dst, s);
-
 
587
}
-
 
588
 
-
 
589
 
-
 
590
/* like strcat() but operates on far pointers */
-
 
591
void sv_strcat_far(char far *dst, const char far *s) {
-
 
592
  /* advance dst to end of string */
-
 
593
  while (*dst != 0) dst++;
-
 
594
  /* append string */
-
 
595
  sv_strcpy_far(dst, s);
-
 
596
}
-
 
597
 
-
 
598
 
545
/* fills a nls_patterns struct with current NLS patterns, returns 0 on success, DOS errcode otherwise */
599
/* fills a nls_patterns struct with current NLS patterns, returns 0 on success, DOS errcode otherwise */
546
unsigned short nls_getpatterns(struct nls_patterns *p) {
600
unsigned short nls_getpatterns(struct nls_patterns *p) {
547
  unsigned short r = 0;
601
  unsigned short r = 0;
548
 
602
 
549
  _asm {
603
  _asm {
Line 671... Line 725...
671
 
725
 
672
    pop dx
726
    pop dx
673
    pop ax
727
    pop ax
674
  }
728
  }
675
 
729
 
676
  /* rely on OpenWatcom's strupr() if DOS has no NLS support */
730
  /* do a naive upcase if DOS has no NLS support */
677
  if (errcode != 0) strupr(buff);
731
  if (errcode != 0) {
-
 
732
    while (*buff) {
-
 
733
      if (*buff < 128) { /* apply only to 7bit (low ascii) values */
-
 
734
        *buff &= 0xDF;
-
 
735
      }
-
 
736
      buff++;
-
 
737
    }
-
 
738
  }
678
}
739
}
679
 
740
 
680
 
741
 
681
/* reload nls ressources from svarcom.lng into svarlang_mem and rmod */
742
/* reload nls ressources from svarcom.lng into svarlang_mem and rmod */
682
void nls_langreload(char *buff, unsigned short rmodseg) {
743
void nls_langreload(char *buff, unsigned short rmodseg) {
Line 689... Line 750...
689
  int i;
750
  int i;
690
 
751
 
691
  /* look up the LANG env variable, upcase it and copy to lang */
752
  /* look up the LANG env variable, upcase it and copy to lang */
692
  lang = env_lookup_val(rmodenvseg, "LANG");
753
  lang = env_lookup_val(rmodenvseg, "LANG");
693
  if ((lang == NULL) || (lang[0] == 0)) return;
754
  if ((lang == NULL) || (lang[0] == 0)) return;
694
  _fmemcpy(buff, lang, 2);
755
  memcpy_ltr_far(buff, lang, 2);
695
  buff[2] = 0;
756
  buff[2] = 0;
696
 
757
 
697
  /* check if there is need to reload at all */
758
  /* check if there is need to reload at all */
698
  if (memcmp(&lastlang, buff, 2) == 0) return;
759
  if (lastlang == *((unsigned short *)buff)) return;
699
 
760
 
700
  buff[4] = 0;
761
  buff[4] = 0;
701
  dosdir = env_lookup_val(rmodenvseg, "DOSDIR");
762
  dosdir = env_lookup_val(rmodenvseg, "DOSDIR");
702
  if (dosdir == NULL) return;
763
  if (dosdir == NULL) return;
703
 
764
 
704
  _fstrcpy(buff + 4, dosdir);
765
  sv_strcpy_far(buff + 4, dosdir);
705
  dosdirlen = strlen(buff + 4);
766
  dosdirlen = sv_strlen(buff + 4);
706
  if (buff[4 + dosdirlen - 1] == '\\') dosdirlen--;
767
  if (buff[4 + dosdirlen - 1] == '\\') dosdirlen--;
707
  memcpy(buff + 4 + dosdirlen, "\\SVARCOM.LNG", 13);
768
  memcpy_ltr(buff + 4 + dosdirlen, "\\SVARCOM.LNG", 13);
708
 
769
 
709
  /* try loading %DOSDIR%\SVARCOM.LNG */
770
  /* try loading %DOSDIR%\SVARCOM.LNG */
710
  if (svarlang_load(buff + 4, buff) != 0) {
771
  if (svarlang_load(buff + 4, buff) != 0) {
711
    /* failed! try %DOSDIR%\BIN\SVARCOM.LNG */
772
    /* failed! try %DOSDIR%\BIN\SVARCOM.LNG */
712
    memcpy(buff + 4 + dosdirlen, "\\BIN\\SVARCOM.LNG", 17);
773
    memcpy_ltr(buff + 4 + dosdirlen, "\\BIN\\SVARCOM.LNG", 17);
713
    if (svarlang_load(buff + 4, buff) != 0) return;
774
    if (svarlang_load(buff + 4, buff) != 0) return;
714
  }
775
  }
715
 
776
 
716
  _fmemcpy(&lastlang, lang, 2);
777
  memcpy_ltr_far(&lastlang, lang, 2);
717
 
778
 
718
  /* update RMOD's critical handler with new strings */
779
  /* update RMOD's critical handler with new strings */
719
  for (i = 0; i < 9; i++) {
780
  for (i = 0; i < 9; i++) {
720
    int len;
781
    int len;
721
    len = strlen(svarlang_str(3, i));
782
    len = sv_strlen(svarlang_str(3, i));
722
    if (len > 15) len = 15;
783
    if (len > 15) len = 15;
723
    _fmemcpy(rmodcritmsg + (i * 16), svarlang_str(3, i), len);
784
    memcpy_ltr_far(rmodcritmsg + (i * 16), svarlang_str(3, i), len);
724
    _fmemcpy(rmodcritmsg + (i * 16) + len, "$", 1);
785
    memcpy_ltr_far(rmodcritmsg + (i * 16) + len, "$", 1);
725
  }
786
  }
726
  /* The ARIF string is special: always 4 bytes long and no $ terminator */
787
  /* The ARIF string is special: always 4 bytes long and no $ terminator */
727
  _fmemcpy(rmodcritmsg + (9 * 16), svarlang_str(3,9), 4);
788
  memcpy_ltr_far(rmodcritmsg + (9 * 16), svarlang_str(3,9), 4);
728
}
789
}
729
 
790
 
730
 
791
 
731
/* locates executable fname in path and fill res with result. returns 0 on success,
792
/* locates executable fname in path and fill res with result. returns 0 on success,
732
 * -1 on failed match and -2 on failed match + "don't even try with other paths"
793
 * -1 on failed match and -2 on failed match + "don't even try with other paths"
Line 778... Line 839...
778
  /* printf("lastbslash=%u\r\n", lastbslash); */
839
  /* printf("lastbslash=%u\r\n", lastbslash); */
779
 
840
 
780
  /* if no path prefix was found in fname (no colon or backslash) AND we have
841
  /* if no path prefix was found in fname (no colon or backslash) AND we have
781
   * a path arg, then assemble path+filename */
842
   * a path arg, then assemble path+filename */
782
  if ((!explicitpath) && (path != NULL) && (path[0] != 0)) {
843
  if ((!explicitpath) && (path != NULL) && (path[0] != 0)) {
783
    i = strlen(path);
844
    i = sv_strlen(path);
784
    if (path[i - 1] != '\\') i++; /* add a byte for inserting a bkslash after path */
845
    if (path[i - 1] != '\\') i++; /* add a byte for inserting a bkslash after path */
785
    /* move the filename at the place where path will end */
846
    /* move the filename at the place where path will end */
786
    memmove(res + i, res + lastbslash + 1, len - lastbslash);
847
    memcpy_rtl(res + i, res + lastbslash + 1, len - lastbslash);
787
    /* copy path in front of the filename and make sure there is a bkslash sep */
848
    /* copy path in front of the filename and make sure there is a bkslash sep */
788
    memmove(res, path, i);
849
    memcpy_ltr(res, path, i);
789
    res[i - 1] = '\\';
850
    res[i - 1] = '\\';
790
  }
851
  }
791
 
852
 
792
  /* if no extension was initially provided, try matching COM, EXE, BAT */
853
  /* if no extension was initially provided, try matching COM, EXE, BAT */
793
  if (*extptr == NULL) {
854
  if (*extptr == NULL) {
794
    int attr;
855
    int attr;
795
    len = strlen(res);
856
    len = sv_strlen(res);
796
    res[len++] = '.';
857
    res[len++] = '.';
797
    for (i = 0; exec_ext[i] != NULL; i++) {
858
    for (i = 0; exec_ext[i] != NULL; i++) {
798
      strcpy(res + len, exec_ext[i]);
859
      sv_strcpy(res + len, exec_ext[i]);
799
      /* printf("? '%s'\r\n", res); */
860
      /* printf("? '%s'\r\n", res); */
800
      *extptr = exec_ext[i];
861
      *extptr = exec_ext[i];
801
      attr = file_getattr(res);
862
      attr = file_getattr(res);
802
      if (attr < 0) continue; /* file not found */
863
      if (attr < 0) continue; /* file not found */
803
      if (attr & DOS_ATTR_DIR) continue; /* this is a directory */
864
      if (attr & DOS_ATTR_DIR) continue; /* this is a directory */
Line 874... Line 935...
874
    ss++;
935
    ss++;
875
    dd++;
936
    dd++;
876
  }
937
  }
877
}
938
}
878
 
939
 
-
 
940
 
-
 
941
/* like memcpy_ltr() but operates on far pointers */
-
 
942
void memcpy_ltr_far(void far *d, const void far *s, unsigned short len) {
-
 
943
  unsigned char const far *ss = s;
-
 
944
  unsigned char far *dd = d;
-
 
945
 
-
 
946
  while (len--) {
-
 
947
    *dd = *ss;
-
 
948
    ss++;
-
 
949
    dd++;
-
 
950
  }
-
 
951
}
-
 
952
 
-
 
953
 
879
/* like memcpy() but guarantees to copy from right to left */
954
/* like memcpy() but guarantees to copy from right to left */
880
void memcpy_rtl(void *d, const void *s, unsigned short len) {
955
void memcpy_rtl(void *d, const void *s, unsigned short len) {
881
  unsigned char const *ss = s;
956
  unsigned char const *ss = s;
882
  unsigned char *dd = d;
957
  unsigned char *dd = d;
883
 
958
 
Line 887... Line 962...
887
    *dd = *ss;
962
    *dd = *ss;
888
    ss--;
963
    ss--;
889
    dd--;
964
    dd--;
890
  }
965
  }
891
}
966
}
-
 
967
 
-
 
968
 
-
 
969
/* like bzero(), but accepts far pointers */
-
 
970
void sv_bzero(void far *dst, unsigned short len) {
-
 
971
  char far *d = dst;
-
 
972
  while (len--) {
-
 
973
    *d = 0;
-
 
974
    d++;
-
 
975
  }
-
 
976
}