Main Page | Class Hierarchy | Class List | File List | Class Members | File Members

htsys.cc

Go to the documentation of this file.
00001 /* 
00002  *      HT Editor
00003  *      htsys.cc (DJGPP implementation)
00004  *
00005  *      Copyright (C) 1999-2002 Stefan Weyergraf (stefan@weyergraf.de)
00006  *
00007  *      This program is free software; you can redistribute it and/or modify
00008  *      it under the terms of the GNU General Public License version 2 as
00009  *      published by the Free Software Foundation.
00010  *
00011  *      This program is distributed in the hope that it will be useful,
00012  *      but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  *      GNU General Public License for more details.
00015  *
00016  *      You should have received a copy of the GNU General Public License
00017  *      along with this program; if not, write to the Free Software
00018  *      Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00019  */
00020 
00021 #include "htsys.h"
00022 
00023 #include <ctype.h>
00024 #include <dir.h>
00025 #include <dpmi.h>
00026 #include <errno.h>
00027 #include <limits.h>
00028 #include <string.h>
00029 #include <stdlib.h>
00030 #include <sys/stat.h>
00031 #include <unistd.h>
00032 
00033 #define MIN(a, b) ((a) < (b) ? (a) : (b))
00034 #define MAX(a, b) ((a) > (b) ? (a) : (b))
00035 
00036 int sys_canonicalize(char *result, const char *filename)
00037 {
00038         _fixpath(filename, result);
00039         return 0;
00040 }
00041 
00042 struct djfindstate {
00043         struct ffblk fstate;
00044 };
00045 
00046 char sys_find_name[HT_NAME_MAX];
00047 
00048 int sys_findclose(pfind_t *pfind)
00049 {
00050         free(pfind->findstate);
00051         return 0;
00052 }
00053 
00054 time_t dostime2time_t(unsigned short time, unsigned short date)
00055 {
00056         struct tm t;
00057         t.tm_sec=(time & 0x1f) * 2;
00058         t.tm_min=(time >>  5) & 0x3f;
00059         t.tm_hour=(time >> 11) & 0x1f;
00060         t.tm_mday=date & 0x1f;
00061         t.tm_mon=((date >>  5) & 0x0f)-1;
00062         t.tm_year=((date >> 9) & 0x7f) + 80;
00063         t.tm_wday=1;
00064         t.tm_yday=1;
00065         t.tm_isdst=0;
00066         return mktime(&t);
00067 }
00068 
00069 void ff2pstat(struct ffblk *f, pstat_t *s)
00070 {
00071         s->caps=pstat_size | pstat_mtime | pstat_mode_type | pstat_mode_usr;
00072         s->size=f->ff_fsize;
00073         s->mtime=dostime2time_t(f->ff_ftime, f->ff_fdate);
00074         s->mode=HT_S_IRUSR;
00075         if (f->ff_attrib & FA_DIREC) {
00076                 s->mode|=HT_S_IFDIR;
00077         } else {
00078                 s->mode|=HT_S_IFREG;
00079         }
00080         if (!(f->ff_attrib & FA_RDONLY)) {
00081                 s->mode|=HT_S_IWUSR;
00082         }
00083         if ((f->lfn_magic[0]=='L') && (f->lfn_magic[1]=='F') && (f->lfn_magic[2]=='N') && 
00084         (f->lfn_magic[3]=='3') && (f->lfn_magic[4]=='2') && (f->lfn_magic[5]==0)) {
00085                 s->caps|=pstat_ctime | pstat_atime;
00086                 s->ctime=dostime2time_t(f->lfn_ctime, f->lfn_cdate);
00087                 s->atime=dostime2time_t(f->lfn_atime, f->lfn_adate);
00088         }
00089 }
00090 
00091 int sys_findfirst(const char *dirname, pfind_t *pfind)
00092 {
00093         int dnl=strlen(dirname);
00094         strcpy(sys_find_name, dirname);
00095         if ((dirname[dnl-1]!='\\') && (dirname[dnl-1]!='/')) {
00096                 sys_find_name[dnl]='\\';
00097                 sys_find_name[dnl+1]=0;
00098         }
00099         strcat(sys_find_name, "*.*");
00100         char *s=sys_find_name;
00101         while ((s=strchr(s, '/'))) *s='\\';
00102         
00103         pfind->findstate=malloc(sizeof (djfindstate));
00104         djfindstate *dfs=(djfindstate*)pfind->findstate;
00105         
00106         int e=findfirst(sys_find_name, &dfs->fstate, FA_RDONLY | FA_HIDDEN | FA_SYSTEM | FA_DIREC | FA_ARCH);
00107         if (e) {
00108                 free(pfind->findstate);
00109                 return e;
00110         }
00111         strcpy(sys_find_name, dfs->fstate.ff_name);
00112         pfind->name=sys_find_name;
00113         ff2pstat(&dfs->fstate, &pfind->stat);
00114         return 0;
00115 }
00116 
00117 int sys_findnext(pfind_t *pfind)
00118 {
00119         djfindstate *dfs=(djfindstate*)pfind->findstate;
00120         int e=findnext(&dfs->fstate);
00121         if (e) return e;
00122         strcpy(sys_find_name, dfs->fstate.ff_name);
00123         pfind->name=sys_find_name;
00124         ff2pstat(&dfs->fstate, &pfind->stat);
00125         return 0;
00126 }
00127 
00128 int sys_pstat(pstat_t *s, const char *filename)
00129 {
00130         char fn[HT_NAME_MAX];
00131         strncpy(fn, filename, sizeof fn);
00132         int flen = strlen(fn);
00133         if (flen && sys_is_path_delim(fn[flen-1]) && (flen !=3) || (fn[1]!=':')) fn[flen-1] = 0;
00134         struct stat st;
00135         errno = 0;
00136         int e = stat(fn, &st);
00137         if (e) return errno ? errno : ENOENT;
00138         s->caps = pstat_mtime|pstat_mode_usr|pstat_mode_w|pstat_size/*|pstat_cluster*/|pstat_mode_type;
00139         s->mtime = st.st_mtime;
00140         s->mode = sys_ht_mode(st.st_mode);
00141         s->size = st.st_size;
00142         s->size_high = 0;
00143         s->fsid = st.st_ino;
00144         return 0;
00145 }
00146 
00147 void sys_suspend()
00148 {
00149         __dpmi_yield();
00150 }
00151 
00152 int sys_get_free_mem()
00153 {
00154         _go32_dpmi_meminfo info;
00155         _go32_dpmi_get_free_memory_information(&info);
00156         return info.available_memory;
00157 }
00158 
00159 int sys_truncate(const char *filename, FILEOFS ofs)
00160 {
00161         return truncate(filename, ofs);
00162 }
00163 
00164 int sys_deletefile(const char *filename)
00165 {
00166         return unlink(filename);
00167 }
00168 
00169 bool sys_is_path_delim(const char c)
00170 {
00171         return (c == '/') || (c == '\\');
00172 }
00173 
00174 int sys_filename_cmp(const char *a, const char *b)
00175 {
00176         while (*a && *b) {
00177                 if (sys_is_path_delim(*a) && sys_is_path_delim(*b)) {
00178                 } else if (tolower(*a) != tolower(*b)) {
00179                         break;
00180                 } else if (*a != *b) {
00181                         break;
00182                 }
00183                 a++;
00184                 b++;
00185         }
00186         return tolower(*a) - tolower(*b);
00187 }
00188 
00189 int sys_ipc_exec(ht_streamfile **in, ht_streamfile **out, ht_streamfile **err, int *handle, const char *cmd, int options)
00190 {
00191         if (options & HT_IPC_NONBLOCKING) return ENOSYS;
00192         int save_stdin = dup(STDIN_FILENO);
00193         int save_stdout = dup(STDOUT_FILENO);
00194         int save_stderr = dup(STDERR_FILENO);
00195         int fdout = sys_tmpfile();
00196         int fderr = sys_tmpfile();
00197         close(STDIN_FILENO);
00198         dup2(fdout, STDOUT_FILENO);
00199         dup2(fderr, STDERR_FILENO);
00200         /*int r = */system(cmd);
00201         dup2(save_stdin, STDIN_FILENO);
00202         dup2(save_stdout, STDOUT_FILENO);
00203         dup2(save_stderr, STDERR_FILENO);
00204         close(save_stdout);
00205         close(save_stderr);
00206         lseek(fdout, 0, SEEK_SET);
00207         lseek(fderr, 0, SEEK_SET);
00208         ht_null_file *nf = new ht_null_file();
00209         nf->init();
00210         *in = nf;
00211         ht_sys_file *sf;
00212         sf = new ht_sys_file();
00213         sf->init(fdout, true, FAM_READ);
00214         *out = sf;
00215         sf = new ht_sys_file();
00216         sf->init(fderr, true, FAM_READ);
00217         *err = sf;
00218         return 0;
00219 }
00220 
00221 bool sys_ipc_is_valid(int handle)
00222 {
00223 // no multitasking, never valid
00224         return false;
00225 }
00226 
00227 int sys_ipc_terminate(int handle)
00228 {
00229 // already terminated, do nothing
00230         return 0;
00231 }
00232 
00233 int sys_get_caps()
00234 {
00235         return 0;
00236 }
00237 
00238 /*
00239  *      Clipboard functions
00240  */
00241  
00242 static bool open_clipboard()
00243 {
00244         __dpmi_regs r;
00245         r.x.ax = 0x1700;   // version
00246         __dpmi_int(0x2f, &r);
00247         if (r.x.ax == 0x1700) return false;
00248         r.x.ax = 0x1701;  // open
00249         __dpmi_int(0x2f, &r);     
00250         return (r.x.ax != 0);
00251 }
00252 
00253 static void close_clipboard()
00254 {
00255         __dpmi_regs r;
00256         r.x.ax = 0x1708;
00257         __dpmi_int(0x2f, &r);
00258 }
00259 
00260 bool sys_write_data_to_native_clipboard(const void *data, int size)
00261 {
00262         if (size > 0xffff) return false;
00263         if (!open_clipboard()) return false;
00264         int sel;
00265         word seg = __dpmi_allocate_dos_memory((size+15)>>4, &sel);
00266         if (seg == 0xffff) {
00267                 close_clipboard();
00268                 return false;
00269         }
00270         dosmemput(data, size, seg*16);
00271         
00272         __dpmi_regs r;
00273         r.x.ax = 0x1703;
00274         
00275         r.x.dx = 0x01; // text
00276         r.x.es = seg;
00277         r.x.bx = 0;
00278         r.x.si = size >> 16;
00279         r.x.cx = size & 0xffff;
00280         
00281         __dpmi_int(0x2f, &r);
00282         __dpmi_free_dos_memory(sel);
00283         close_clipboard();
00284         return (r.x.ax != 0);
00285 }
00286 
00287 int sys_get_native_clipboard_data_size()
00288 {
00289         return 10000;
00290         if (!open_clipboard()) return 0;
00291         __dpmi_regs r;
00292         r.x.ax = 0x1704;
00293         r.x.dx = 0x07; // text
00294         __dpmi_int(0x2f, &r);
00295         close_clipboard();
00296         return ((dword)r.x.dx)<<16+r.x.ax;
00297 }
00298 
00299 bool sys_read_data_from_native_clipboard(void *data, int max_size)
00300 {
00301         int dz = sys_get_native_clipboard_data_size();
00302         if (!open_clipboard()) return false;
00303         if (!dz) {
00304                 close_clipboard();
00305                 return false;
00306         }
00307         int sel;
00308         word seg = __dpmi_allocate_dos_memory((dz+15)>>4, &sel);
00309         if (seg == 0xffff) {
00310                 close_clipboard();
00311                 return false;
00312         }
00313         
00314         __dpmi_regs r;
00315         r.x.ax = 0x1705;
00316         r.x.dx = 0x1;
00317         r.x.es = seg;
00318         r.x.bx = 0;
00319         __dpmi_int(0x2f, &r);
00320         if (r.x.ax) {
00321                 dosmemget(seg*16, MIN(max_size, dz), data);
00322         }
00323         __dpmi_free_dos_memory(sel);
00324         close_clipboard();
00325         return (r.x.ax != 0);
00326 }
00327 
00328 /*
00329  *      INIT
00330  */
00331 
00332 bool init_system()
00333 {
00334         return true;
00335 }
00336 
00337 /*
00338  *      DONE
00339  */
00340 
00341 void done_system()
00342 {
00343 }
00344 

Generated on Fri May 7 21:15:37 2004 by doxygen 1.3.5