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         struct stat st;
00131         int e=stat(filename, &st);
00132         if (e) return e;
00133         s->caps=pstat_mtime|pstat_mode_usr|pstat_mode_w|pstat_size/*|pstat_cluster*/|pstat_mode_type;
00134         s->mtime=st.st_mtime;
00135         s->mode=sys_ht_mode(st.st_mode);
00136         s->size=st.st_size;
00137         s->size_high=0;
00138         s->fsid=st.st_ino;
00139         return 0;
00140 }
00141 
00142 void sys_suspend()
00143 {
00144         __dpmi_yield();
00145 }
00146 
00147 int sys_get_free_mem()
00148 {
00149         _go32_dpmi_meminfo info;
00150         _go32_dpmi_get_free_memory_information(&info);
00151         return info.available_memory;
00152 }
00153 
00154 int sys_truncate(const char *filename, FILEOFS ofs)
00155 {
00156         return truncate(filename, ofs);
00157 }
00158 
00159 int sys_deletefile(const char *filename)
00160 {
00161         return unlink(filename);
00162 }
00163 
00164 bool sys_is_path_delim(const char c)
00165 {
00166         return (c == '/') || (c == '\\');
00167 }
00168 
00169 int sys_filename_cmp(const char *a, const char *b)
00170 {
00171         while (*a && *b) {
00172                 if (sys_is_path_delim(*a) && sys_is_path_delim(*b)) {
00173                 } else if (tolower(*a) != tolower(*b)) {
00174                         break;
00175                 } else if (*a != *b) {
00176                         break;
00177                 }
00178                 a++;
00179                 b++;
00180         }
00181         return tolower(*a) - tolower(*b);
00182 }
00183 
00184 int sys_ipc_exec(ht_streamfile **in, ht_streamfile **out, ht_streamfile **err, int *handle, const char *cmd)
00185 {
00186         int save_stdout = dup(STDOUT_FILENO);
00187         int save_stderr = dup(STDERR_FILENO);
00188         int fdout = sys_tmpfile();
00189         int fderr = sys_tmpfile();
00190         dup2(fdout, STDOUT_FILENO);
00191         dup2(fderr, STDERR_FILENO);
00192         /*int r = */system(cmd);
00193         dup2(save_stdout, STDOUT_FILENO);
00194         dup2(save_stderr, STDERR_FILENO);
00195         close(save_stdout);
00196         close(save_stderr);
00197         lseek(fdout, 0, SEEK_SET);
00198         lseek(fderr, 0, SEEK_SET);
00199         ht_null_file *nf = new ht_null_file();
00200         nf->init();
00201         *in = nf;
00202         ht_sys_file *sf;
00203         sf = new ht_sys_file();
00204         sf->init(fdout, true, FAM_READ);
00205         *out = sf;
00206         sf = new ht_sys_file();
00207         sf->init(fderr, true, FAM_READ);
00208         *err = sf;
00209         return 0;
00210 }
00211 
00212 bool sys_ipc_is_valid(int handle)
00213 {
00214 // no multitasking, never valid
00215         return false;
00216 }
00217 
00218 int sys_ipc_terminate(int handle)
00219 {
00220 // do nothing
00221         return 0;
00222 }
00223 
00224 /*
00225  *      Clipboard functions
00226  */
00227  
00228 static bool open_clipboard()
00229 {
00230         __dpmi_regs r;
00231         r.x.ax = 0x1700;   // version
00232         __dpmi_int(0x2f, &r);
00233         if (r.x.ax == 0x1700) return false;
00234         r.x.ax = 0x1701;  // open
00235         __dpmi_int(0x2f, &r);     
00236         return (r.x.ax != 0);
00237 }
00238 
00239 static void close_clipboard()
00240 {
00241         __dpmi_regs r;
00242         r.x.ax = 0x1708;
00243         __dpmi_int(0x2f, &r);
00244 }
00245 
00246 bool sys_write_data_to_native_clipboard(const void *data, int size)
00247 {
00248         if (size > 0xffff) return false;
00249         if (!open_clipboard()) return false;
00250         int sel;
00251         word seg = __dpmi_allocate_dos_memory((size+15)>>4, &sel);
00252         if (seg == 0xffff) {
00253                 close_clipboard();
00254                 return false;
00255         }
00256         dosmemput(data, size, seg*16);
00257         
00258         __dpmi_regs r;
00259         r.x.ax = 0x1703;
00260         
00261         r.x.dx = 0x01; // text
00262         r.x.es = seg;
00263         r.x.bx = 0;
00264         r.x.si = size >> 16;
00265         r.x.cx = size & 0xffff;
00266         
00267         __dpmi_int(0x2f, &r);
00268         __dpmi_free_dos_memory(sel);
00269         close_clipboard();
00270         return (r.x.ax != 0);
00271 }
00272 
00273 int sys_get_native_clipboard_data_size()
00274 {
00275         return 10000;
00276         if (!open_clipboard()) return 0;
00277         __dpmi_regs r;
00278         r.x.ax = 0x1704;
00279         r.x.dx = 0x07; // text
00280         __dpmi_int(0x2f, &r);
00281         close_clipboard();
00282         return ((dword)r.x.dx)<<16+r.x.ax;
00283 }
00284 
00285 bool sys_read_data_from_native_clipboard(void *data, int max_size)
00286 {
00287         int dz = sys_get_native_clipboard_data_size();
00288         if (!open_clipboard()) return false;
00289         if (!dz) {
00290                 close_clipboard();
00291                 return false;
00292         }
00293         int sel;
00294         word seg = __dpmi_allocate_dos_memory((dz+15)>>4, &sel);
00295         if (seg == 0xffff) {
00296                 close_clipboard();
00297                 return false;
00298         }
00299         
00300         __dpmi_regs r;
00301         r.x.ax = 0x1705;
00302         r.x.dx = 0x1;
00303         r.x.es = seg;
00304         r.x.bx = 0;
00305         __dpmi_int(0x2f, &r);
00306         if (r.x.ax) {
00307                 dosmemget(seg*16, MIN(max_size, dz), data);
00308         }
00309         __dpmi_free_dos_memory(sel);
00310         close_clipboard();
00311         return (r.x.ax != 0);
00312 }
00313 
00314 /*
00315  *      INIT
00316  */
00317 
00318 bool init_system()
00319 {
00320         return true;
00321 }
00322 
00323 /*
00324  *      DONE
00325  */
00326 
00327 void done_system()
00328 {
00329 }
00330 

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