syswxwin.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218
  1. /* syswxwin.c Copyright (C) 1989-2003 Codemist Ltd */
  2. /*
  3. * Windows 95, 98, NT and 2000, XP support, using the "wxWindows" package.
  4. */
  5. /*
  6. * This code may be used and modified, and redistributed in binary
  7. * or source form, subject to the "CCL Public License", which should
  8. * accompany it. This license is a variant on the BSD license, and thus
  9. * permits use of code derived from this in either open and commercial
  10. * projects: but it does require that updates to this code be made
  11. * available back to the originators of the package.
  12. * Before merging other code in with this or linking this code
  13. * with other packages or libraries please check that the license terms
  14. * of the other material are compatible with those of this.
  15. */
  16. /* Signature: 2be745d6 17-Feb-2003 */
  17. #include <stdarg.h>
  18. #include <string.h>
  19. #include <ctype.h>
  20. #include <time.h>
  21. #include <math.h>
  22. #include "machine.h"
  23. #include "tags.h"
  24. #include "externs.h"
  25. #include "read.h"
  26. #include "cslerror.h"
  27. #include "sys.h"
  28. #include "cwin.h"
  29. #ifdef TIMEOUT
  30. #include "timeout.h"
  31. #endif
  32. #ifdef __WATCOMC__
  33. #include <float.h>
  34. #endif
  35. #include <errno.h>
  36. #ifdef _MSC_VER
  37. #define strdup(x) _strdup(x)
  38. #endif
  39. #ifndef __linux__
  40. static CSLbool ticker_active = NO;
  41. void add_ticker(void)
  42. {
  43. if (ticker_active) return;
  44. countdown = SOFTWARE_TICKS;
  45. ticker_active = YES;
  46. }
  47. void MS_CDECL remove_ticker(void)
  48. {
  49. if (!ticker_active) return;
  50. ticker_active = NO;
  51. }
  52. #endif
  53. /*
  54. * void poll_for_attn()
  55. * {
  56. * kbhit(); // causes ^C to be noticed!
  57. * }
  58. */
  59. static clock_t prev_clock = 0;
  60. #ifdef UNIX
  61. void accept_tick()
  62. {
  63. }
  64. #else
  65. #ifdef INITIAL_SOFTWARE_TICKS
  66. int32 software_ticks = INITIAL_SOFTWARE_TICKS;
  67. int32 software_tick_count = 0, prev_software_tick_count = 0;
  68. #endif
  69. void accept_tick(void)
  70. {
  71. /*
  72. * This is where I can put things that need to be done regularly. At
  73. * In particular I can poll the window manager here.
  74. */
  75. /*
  76. * Around once per 2 seconds I update the clock display on the menu bar -
  77. * the intent of this is to give a bit of feedback to the user that
  78. * things are happening. NOTE WELL: I tune all this in terms of CPU time
  79. * (measured with clock()) rather than wall-clock time (time()). On
  80. * a multi-tasking system this means that a Lisp task running at low
  81. * priority in the background will hardly poll at all, and one that is
  82. * suspended (if such a possibility can arise) will not do a lot. But
  83. * I HOPE that CPU time is not consumed while one is suspended awaiting
  84. * keyboard input, and so that sort of delay should not cause messups
  85. * in my "learning" of the correct poll rate.
  86. */
  87. clock_t t0 = clock();
  88. #ifdef SOFTWARE_TICKS_PER_SECOND
  89. software_tick_count++;
  90. #endif
  91. if (prev_clock == 0 || t0 > prev_clock + 2*CLOCKS_PER_SEC)
  92. { long int t, gct;
  93. Lisp_Object nil;
  94. ensure_screen(); nil = C_nil;
  95. if (exception_pending()) return;
  96. #ifdef SOFTWARE_TICKS_PER_SECOND
  97. if (prev_clock != 0)
  98. { double t1 = (double)(t0-prev_clock)/(double)CLOCKS_PER_SEC;
  99. double ratio =
  100. (double)(software_tick_count - prev_software_tick_count)/t1;
  101. int32 w;
  102. /*
  103. * t1 is how long since I was last here, ratio is the number of
  104. * ticks per second over that time-span.
  105. */
  106. ratio = ratio / (double)SOFTWARE_TICKS_PER_SECOND;
  107. prev_software_tick_count = software_tick_count;
  108. /*
  109. * Now ratio is the extent by which I was taking ticks too fast.
  110. * To dampen out my correction I will scale software_ticks by the
  111. * square root of this.
  112. */
  113. ratio = sqrt(ratio);
  114. w = (int)(1000.0 * ratio);
  115. /*
  116. * I clamp the correction factor so I never adjust my clock rate by
  117. * a factor of more than (about) 3.
  118. */
  119. if (w > 3000) w = 3000;
  120. else if (w < 300) w = 300;
  121. /*
  122. * Furthermore I attempt to keep software_ticks within integer range.
  123. */
  124. if (software_ticks < (0x7fffffff/3000) &&
  125. software_ticks > 50)
  126. software_ticks = (w*software_ticks)/1000;
  127. }
  128. #endif
  129. t0 = clock();
  130. if (clock_stack == &consolidated_time[0])
  131. { consolidated_time[0] +=
  132. (double)(t0 - base_time)/(double)CLOCKS_PER_SEC;
  133. base_time = t0;
  134. }
  135. t = (long int)(100.0 * consolidated_time[0]);
  136. gct = (long int)(100.0 * gc_time);
  137. report_time(t, gct);
  138. prev_clock = t0;
  139. }
  140. cwin_poll_window_manager(0);
  141. if (cwin_interrupt_pending != 0)
  142. { Lisp_Object nil = C_nil;
  143. interrupt_pending = YES;
  144. if (exception_pending()) nil = (Lisp_Object)((int32)nil ^ 1);
  145. miscflags |= HEADLINE_FLAG | MESSAGES_FLAG;
  146. cwin_interrupt_pending = 0;
  147. }
  148. }
  149. #endif
  150. void putc_stdout(int c)
  151. {
  152. if (alternative_stdout != NULL) putc(c, alternative_stdout);
  153. else cwin_putchar(c);
  154. }
  155. void pause_for_user(void)
  156. {
  157. }
  158. int pipes_today = 0;
  159. int win32s = 0;
  160. void flush_screen(void)
  161. {
  162. cwin_ensure_screen();
  163. }
  164. void start_up_window_manager(int use_wimp)
  165. {
  166. CSL_IGNORE(use_wimp); /* on this platform ALWAYS use the window system */
  167. win32s = 0; /* NT, 2000, XP, Linux!!! */
  168. pipes_today = 1;
  169. }
  170. int wimpget(char *buf)
  171. {
  172. int c, n=0;
  173. Lisp_Object nil;
  174. ensure_screen(); nil = C_nil;
  175. if (exception_pending()) return 0;
  176. while (n < 255)
  177. { c = cwin_getchar();
  178. nil = C_nil;
  179. if (exception_pending() || c == EOF) return 0;
  180. c = c & 0xff;
  181. buf[n++] = (char)c;
  182. if (c == '\n') break;
  183. };
  184. return n;
  185. }
  186. /*
  187. * Slightly optional jollies re GC statistics...
  188. */
  189. static char time_string[32], space_string[32];
  190. void report_time(int32 t, int32 gct)
  191. {
  192. sprintf(time_string, "%ld.%.2ld+%ld.%.2ld secs ",
  193. t/100L, t%100L, gct/100L, gct%100L);
  194. if ((window_heading & 1) == 0) cwin_report_left(time_string);
  195. }
  196. void report_space(int n, double percent)
  197. {
  198. sprintf(space_string, "[GC %d]:%.2f%%", n, percent);
  199. if ((window_heading & 4) == 0) cwin_report_right(space_string);
  200. }
  201. /*
  202. * End of the worst of the window-manager aware code
  203. */
  204. #undef exit
  205. #define exit(a) my_exit(a)
  206. #ifdef UNIX
  207. #include <sys/stat.h>
  208. #ifndef NO_UNISTD_AVAILABLE
  209. /*
  210. * Posix mandates a header <unistd.h>, which is why I feel entitled to
  211. * include it here. But for systems that do not I can assert
  212. * NO_UNISTD_AVAILABLE in machine.h and worry about other ways to
  213. * reference the relevant facilities...
  214. */
  215. #include <unistd.h>
  216. #endif
  217. #ifdef __hpux
  218. /*
  219. * Get the regular headers to define a few more things...
  220. */
  221. #define _SYSCALL_INCLUDED
  222. #endif
  223. #include <stdarg.h>
  224. #include <stdlib.h>
  225. #include <string.h>
  226. #include <ctype.h>
  227. #include <sys/types.h>
  228. #include <dirent.h>
  229. #include <errno.h>
  230. #else
  231. #ifdef __cplusplus
  232. #include <afxwin.h>
  233. #else
  234. #include <windows.h>
  235. #endif
  236. #include <io.h>
  237. #include <dos.h>
  238. #include <direct.h>
  239. #include <sys\stat.h>
  240. #endif
  241. #include "fileops.c"
  242. #include "scandir.c"
  243. #include "filename.c"
  244. int change_directory(char *filename, char *old, size_t n)
  245. {
  246. process_file_name(filename, old, n);
  247. if (*filename == 0) return 1;
  248. #ifdef _MSC_VER
  249. if (_chdir(filename))
  250. #else
  251. if (chdir(filename))
  252. #endif
  253. { char err_buf[LONGEST_LEGAL_FILENAME+100];
  254. switch (errno)
  255. {
  256. case ENOENT:
  257. sprintf(err_buf,"The directory %s does not exist.",filename);
  258. break;
  259. default:
  260. sprintf(err_buf,"Cannot change directory to %s.",filename);
  261. break;
  262. }
  263. aerror0(err_buf);
  264. return 1;
  265. }
  266. else return 0;
  267. }
  268. int create_directory(char *filename, char *old, size_t n)
  269. {
  270. process_file_name(filename, old, n);
  271. if (*filename == 0) return 1;
  272. #ifdef UNIX
  273. return mkdir(filename, 0770);
  274. #else
  275. #ifdef _MSC_VER
  276. return _mkdir(filename);
  277. #else
  278. return mkdir(filename);
  279. #endif
  280. #endif
  281. }
  282. CSLbool file_exists(char *filename, char *old, size_t n, char *tt)
  283. {
  284. #ifdef _MSC_VER
  285. struct _stat statbuff;
  286. #else
  287. struct stat statbuff;
  288. #endif
  289. process_file_name(filename, old, n);
  290. if (*filename == 0) return NO;
  291. #ifdef _MSC_VER
  292. if (_stat(filename, &statbuff) != 0) return NO;
  293. #else
  294. if (stat(filename, &statbuff) != 0) return NO;
  295. #endif
  296. strcpy(tt, ctime(&(statbuff.st_mtime)));
  297. return YES;
  298. }
  299. static void remove_files(char *name, int dirp, long int size)
  300. /*
  301. * Remove a file, or a directory and all its contents. This function
  302. * can still misbehave if you do not have write access to all the files
  303. * involved.
  304. */
  305. {
  306. size = size;
  307. switch (dirp)
  308. {
  309. case SCAN_ENDDIR:
  310. #ifdef _MSC_VER
  311. _rmdir(name); return;
  312. #else
  313. rmdir(name); return;
  314. #endif
  315. case SCAN_FILE:
  316. remove(name); return;
  317. }
  318. }
  319. int delete_file(char *filename, char *old, size_t n)
  320. {
  321. process_file_name(filename, old, n);
  322. if (*filename == 0) return 1;
  323. scan_directory(filename, remove_files);
  324. return 0;
  325. }
  326. long file_length(char *filename, char *old, size_t n)
  327. {
  328. #ifdef _MSC_VER
  329. struct _stat buf;
  330. #else
  331. struct stat buf;
  332. #endif
  333. process_file_name(filename, old, n);
  334. if (*filename == 0) return 0;
  335. #ifdef _MSC_VER
  336. if (_stat(filename,&buf) == -1) return -1;
  337. #else
  338. if (stat(filename,&buf) == -1) return -1;
  339. #endif
  340. return (long)(buf.st_size);
  341. }
  342. #ifdef UNIX
  343. #ifndef S_IFMT
  344. # ifdef __S_IFMT
  345. # define S_IFMT __S_IFMT
  346. # endif
  347. #endif
  348. #ifndef S_IFDIR
  349. # ifdef __S_IFDIR
  350. # define S_IFDIR __S_IFDIR
  351. # endif
  352. #endif
  353. #ifndef S_IFREG
  354. # ifdef __S_IFREG
  355. # define S_IFREG __S_IFREG
  356. # endif
  357. #endif
  358. #ifndef S_ISLNK
  359. # ifdef S_IFLNK
  360. # ifdef S_IFMT
  361. # define S_ISLNK(m) (((m) & S_IFMT) == S_IFLNK)
  362. # endif
  363. # endif
  364. #endif
  365. int directoryp(char *filename, char *old, size_t n)
  366. {
  367. struct stat buf;
  368. process_file_name(filename, old, n);
  369. if (*filename == 0) return 0;
  370. if (stat(filename,&buf) == -1) return 0;
  371. return ((buf.st_mode & S_IFMT) == S_IFDIR);
  372. }
  373. #else
  374. int directoryp(char *filename, char *old, size_t n)
  375. {
  376. process_file_name(filename, old, n);
  377. if (*filename == 0) return 0;
  378. return (GetFileAttributes(filename) == FILE_ATTRIBUTE_DIRECTORY);
  379. }
  380. #endif
  381. #ifdef UNIX
  382. extern char *strdup(char *s);
  383. #endif
  384. extern char *get_truename(char *filename, char *old, size_t n)
  385. {
  386. #ifdef _MSC_VER
  387. struct _stat buf;
  388. #else
  389. struct stat buf;
  390. #endif
  391. char *temp, *fn, *dir, *pwd;
  392. process_file_name(filename, old, n);
  393. if (*filename == 0) aerror("truename");
  394. /* Find out whether we have a file or a directory. Be careful in case
  395. * the first two characters define a drive.
  396. */
  397. if (0 /* strlen(filename) > 2 && *(filename+1) == ':' */)
  398. {
  399. /*
  400. * Again beware the fact that aerror0 does not change the flow of control.
  401. */
  402. #ifdef _MSC_VER
  403. if (_stat(filename+2,&buf) == -1) aerror0("truename: cannot stat file");
  404. #else
  405. if (stat(filename+2,&buf) == -1) aerror0("truename: cannot stat file");
  406. #endif
  407. }
  408. else
  409. {
  410. #ifdef _MSC_VER
  411. if (_stat(filename,&buf) == -1) aerror0("truename: cannot stat file");
  412. #else
  413. if (stat(filename,&buf) == -1) aerror0("truename: cannot stat file");
  414. #endif
  415. }
  416. /* Store current directory */
  417. #ifdef _MSC_VER
  418. if ((pwd = (char *)_getcwd((char *)NULL, LONGEST_LEGAL_FILENAME)) == NULL)
  419. #else
  420. if ((pwd = (char *)getcwd((char *)NULL, LONGEST_LEGAL_FILENAME)) == NULL)
  421. #endif
  422. aerror0("truename: cannot get current working directory");
  423. if ((buf.st_mode & S_IFMT) == S_IFDIR)
  424. { /* We have a directory */
  425. char *dir = (char*)(*malloc_hook)(LONGEST_LEGAL_FILENAME);
  426. #ifdef _MSC_VER
  427. if (_chdir(filename) != 0)
  428. aerror0("truename: cannot change directory");
  429. if (_getcwd(dir ,LONGEST_LEGAL_FILENAME) == NULL)
  430. aerror0("truename: cannot get current working directory");
  431. if (_chdir(pwd) != 0)
  432. aerror0("truename: cannot change directory");
  433. #else
  434. if (chdir(filename) != 0)
  435. aerror0("truename: cannot change directory");
  436. if (getcwd(dir ,LONGEST_LEGAL_FILENAME) == NULL)
  437. aerror0("truename: cannot get current working directory");
  438. if (chdir(pwd) != 0)
  439. aerror0("truename: cannot change directory");
  440. #endif
  441. (*free_hook)(pwd);
  442. /*
  443. * This is an axiom-specific hack --- it expects that truname
  444. * preserves trailing directory characters.
  445. */
  446. if (old[n-1] == '\\' || old[n-1] == '/' &&
  447. dir[strlen(dir)-1] != '\\')
  448. { n = strlen(dir);
  449. dir[n] = '\\';
  450. dir[n+1] = '\0';
  451. }
  452. return dir;
  453. }
  454. else
  455. { /* Assume we have some kind of file */
  456. temp = strrchr(filename,'\\');
  457. if (temp)
  458. { /* Found a directory component */
  459. fn = strdup(temp);
  460. *temp = '\0';
  461. /* fn is now "\file" and filename is the directory */
  462. #ifdef _MSC_VER
  463. if (_chdir(filename) != 0)
  464. aerror0("truename: cannot change directory");
  465. if ((temp = (char *)_getcwd((char *)NULL,LONGEST_LEGAL_FILENAME)) == NULL)
  466. aerror0("truename: cannot get current working directory");
  467. if (_chdir(pwd) != 0)
  468. aerror0("truename: cannot change directory");
  469. #else
  470. if (chdir(filename) != 0)
  471. aerror0("truename: cannot change directory");
  472. if ((temp = (char *)getcwd((char *)NULL,LONGEST_LEGAL_FILENAME)) == NULL)
  473. aerror0("truename: cannot get current working directory");
  474. if (chdir(pwd) != 0)
  475. aerror0("truename: cannot change directory");
  476. #endif
  477. dir = (char *)(*malloc_hook)((strlen(temp) + strlen(fn) + 1)*sizeof(char));
  478. strcpy(dir,temp);
  479. (*free_hook)(temp);
  480. strcat(dir, fn);
  481. (*free_hook)(pwd);
  482. (*free_hook)(fn);
  483. return dir;
  484. }
  485. else
  486. { dir = (char *)(*malloc_hook)((strlen(pwd) +
  487. strlen(filename) + 2)*sizeof(char));
  488. strcpy(dir, pwd);
  489. strcat(dir, "\\");
  490. strcat(dir,filename);
  491. (*free_hook)(pwd);
  492. return dir;
  493. }
  494. }
  495. }
  496. #ifdef UNIX
  497. /* extern char *getcwd(char *s, size_t n); in case unistd not used */
  498. int get_current_directory(char *s, int n)
  499. {
  500. #ifdef NO_GETCWD
  501. aerror0("cannot get current directory name.");
  502. *s = 0;
  503. return 0;
  504. #else
  505. if (getcwd(s, n) == 0)
  506. { switch(errno)
  507. {
  508. case ERANGE:
  509. aerror0("the pathname of the current directory is too long.");
  510. break;
  511. case EACCES:
  512. aerror0("insufficient permission to get pathname.");
  513. break;
  514. default:
  515. aerror0("cannot get current directory name.");
  516. break;
  517. }
  518. *s = 0;
  519. return 0;
  520. }
  521. else return strlen(s);
  522. #endif
  523. }
  524. #else
  525. int get_current_directory(char *s, int n)
  526. {
  527. int n1 = GetCurrentDirectory(n, s);
  528. if (n1 == 0)
  529. { aerror0("cannot get current directory name");
  530. return 0;
  531. }
  532. else if (n1 >= n)
  533. { aerror("the pathname of the current directory is too long");
  534. return 0;
  535. }
  536. else return n1;
  537. }
  538. #endif
  539. #ifdef NAG_VERSION
  540. #define MAX_NUMBER_OF_FILES 2048
  541. int list_directory_members(char *filename, char *old, char **filelist[],
  542. size_t n)
  543. { WIN32_FIND_DATA fileData;
  544. HANDLE fileHandle;
  545. int32 number_of_entries = 1;
  546. char **files;
  547. process_file_name(filename, old, n);
  548. if (*filename == 0) strcpy(filename,"*.*");
  549. else if (filename[strlen(filename)-1] != '\\')
  550. filename=strcat(filename,"\\*.*");
  551. else filename=strcat(filename,"*.*");
  552. fileHandle = FindFirstFile(filename,&fileData);
  553. if (fileHandle == INVALID_HANDLE_VALUE) return 0;
  554. /*
  555. * The fixed allocation size here seems unsatisfactory, but I will leave it
  556. * like that for the moment because altering things to behave better would
  557. * probably involve redesigning the interface to this function.
  558. */
  559. files=(char **)(*malloc_hook)(MAX_NUMBER_OF_FILES*sizeof(char *));
  560. files[0]=strdup(fileData.cFileName);
  561. while(FindNextFile(fileHandle,&fileData))
  562. { files[number_of_entries] = strdup(fileData.cFileName);
  563. number_of_entries++;
  564. }
  565. FindClose(fileHandle);
  566. *filelist = files;
  567. return number_of_entries;
  568. }
  569. #else
  570. void list_directory_members(char *filename, char *old,
  571. size_t n, directory_callback *fn)
  572. {
  573. process_file_name(filename, old, n);
  574. scan_files(filename, fn);
  575. }
  576. #endif
  577. /*
  578. * The following few lines are an attempt to provide compatibility
  579. * between Watcom C 11.0 and Microsoft Visual C++ version 5... Both provide
  580. * a stat() function but they use different names for the bits that
  581. * can be used to test if files are readable or writable.
  582. */
  583. #ifndef S_IRUSR
  584. #define S_IRUSR _S_IREAD
  585. #endif
  586. #ifndef S_IWUSR
  587. #define S_IWUSR _S_IWRITE
  588. #endif
  589. int file_readable(char *filename, char *old, size_t n)
  590. {
  591. #ifdef _MSC_VER
  592. struct _stat buf;
  593. #else
  594. struct stat buf;
  595. #endif
  596. process_file_name(filename, old, n);
  597. if (*filename == 0) return 0;
  598. #ifdef _MSC_VER
  599. if (_stat(filename,&buf) == -1)
  600. #else
  601. if (stat(filename,&buf) == -1)
  602. #endif
  603. return 0; /* File probably does not exist */
  604. else
  605. return (buf.st_mode & S_IRUSR);
  606. }
  607. int file_writeable(char *filename, char *old, size_t n)
  608. {
  609. #ifdef _MSC_VER
  610. struct _stat buf;
  611. #else
  612. struct stat buf;
  613. #endif
  614. process_file_name(filename, old, n);
  615. if (*filename == 0) return 0;
  616. #ifdef _MSC_VER
  617. if (_stat(filename,&buf) == -1)
  618. #else
  619. if (stat(filename,&buf) == -1)
  620. #endif
  621. return 0; /* Should we check to see if the directory is writeable? */
  622. else
  623. return (buf.st_mode & S_IWUSR);
  624. }
  625. int rename_file(char *from_name, char *from_old, size_t from_size,
  626. char *to_name, char *to_old, size_t to_size)
  627. {
  628. process_file_name(from_name, from_old, from_size);
  629. process_file_name(to_name, to_old, to_size);
  630. if (*from_name == 0 || *to_name == 0) return 0;
  631. return rename(from_name,to_name);
  632. }
  633. #ifdef UNIX
  634. int my_system(char *s)
  635. {
  636. return system(s);
  637. }
  638. FILE *my_popen(char *a, char *b)
  639. {
  640. #ifdef NCC_LIB
  641. return NULL;
  642. #else
  643. return (FILE *)popen(a, b);
  644. #endif
  645. }
  646. void my_pclose(FILE *a)
  647. {
  648. #ifndef NCC_LIB
  649. pclose(a);
  650. #endif
  651. }
  652. #else
  653. /*
  654. * It appears to me that "system" as provided by the Windows NT SDK
  655. * libraries is a real mess. This is perhaps because executing a
  656. * command might want to generate terminal output, so when one invokes
  657. * "system" it first pops up a console window running the command
  658. * interpreter, then loads the thing you wanted executed, and then
  659. * closes everything down - probably before you have had any real
  660. * chance to observe any output that was generated. I might therefore
  661. * want to invent a version of my own... as in here using CreateProcess.
  662. * The issues about Win32 vs Win32s incompatibilities here are HORRID.
  663. */
  664. static HANDLE osRunRedirect(char *cmd, BOOL wait, HANDLE hin,
  665. HANDLE hout, HANDLE herr);
  666. static HANDLE osMakeReadPipe(char *cmd, HANDLE *);
  667. static char *osMangleCommand(char *s);
  668. int my_system(char *s)
  669. {
  670. HANDLE h, hproc;
  671. DWORD retCode;
  672. char buf[256];
  673. char *os;
  674. DWORD nread;
  675. BOOL done = FALSE;
  676. os = s;
  677. s = osMangleCommand(s);
  678. h = osMakeReadPipe(s, &hproc);
  679. if (s != os) free(s);
  680. if (h==NULL)
  681. { term_printf("Failed: %d\n", GetLastError());
  682. CloseHandle(hproc);
  683. return 0;
  684. }
  685. while (!done)
  686. { DWORD i;
  687. BOOL res;
  688. res = ReadFile(h, (void*) buf, 256, &nread, NULL);
  689. if (res == TRUE && nread == 0 &&
  690. GetExitCodeProcess(hproc, &retCode) != STILL_ACTIVE)
  691. { done = TRUE;
  692. }
  693. else if (!res)
  694. { int err = GetLastError();
  695. if (GetExitCodeProcess(hproc, &retCode) == STILL_ACTIVE)
  696. { term_printf("Error: %d\n", err);
  697. CloseHandle(hproc);
  698. CloseHandle(h);
  699. return -1;
  700. }
  701. return retCode;
  702. }
  703. else
  704. { for (i=0; i<nread; i++)
  705. { if (buf[i] != '\r')
  706. { stdout_printf("%c", buf[i]);
  707. }
  708. }
  709. }
  710. }
  711. GetExitCodeProcess(hproc, &retCode);
  712. return retCode;
  713. }
  714. static HANDLE osMakeReadPipe(char *cmd, HANDLE *proc)
  715. {
  716. HANDLE hwr, hwr2, hrd, hproc;
  717. SECURITY_ATTRIBUTES attribs;
  718. BOOL ret;
  719. attribs.nLength = sizeof(attribs);
  720. attribs.lpSecurityDescriptor = NULL;
  721. attribs.bInheritHandle = TRUE;
  722. if (!CreatePipe(&hrd, /* address of variable for read handle */
  723. &hwr, /* address of variable for write handle */
  724. &attribs, /* Security sillyness */
  725. 0)) return NULL;
  726. hproc = GetCurrentProcess();
  727. ret = DuplicateHandle(hproc, hwr, /* Source */
  728. hproc, &hwr2, /* Target */
  729. 0, /* Desired access */
  730. TRUE, /* Inherit */
  731. DUPLICATE_SAME_ACCESS);
  732. if (!ret)
  733. { CloseHandle(hrd);
  734. CloseHandle(hwr);
  735. return NULL;
  736. }
  737. hproc = osRunRedirect(cmd, FALSE, NULL, hwr, hwr2);
  738. CloseHandle(hwr);
  739. CloseHandle(hwr2);
  740. if (hproc == NULL) return NULL;
  741. *proc = hproc;
  742. return hrd;
  743. }
  744. static HANDLE osRunRedirect(char *cmd, BOOL wait, HANDLE hin,
  745. HANDLE hout, HANDLE herr)
  746. {
  747. static STARTUPINFO si;
  748. static PROCESS_INFORMATION pi;
  749. BOOL rc;
  750. rc = TRUE;
  751. if (!rc) term_printf("Ooops!");
  752. /* Create process. */
  753. memset(&si, 0, sizeof(si));
  754. si.cb = sizeof(si);
  755. si.lpReserved = NULL;
  756. si.lpDesktop = NULL;
  757. si.lpTitle = "!";
  758. si.cbReserved2 = 0;
  759. si.lpReserved2 = NULL;
  760. si.dwFlags = STARTF_USESTDHANDLES;
  761. si.hStdInput = hin;
  762. si.hStdOutput = hout;
  763. si.hStdError = herr;
  764. rc = CreateProcess(NULL, cmd, NULL, NULL,
  765. TRUE,
  766. DETACHED_PROCESS/*|NORMAL_PRIORITY_CLASS*/,
  767. NULL, NULL,
  768. &si, &pi);
  769. if (rc == FALSE) return NULL;
  770. if (wait == TRUE)
  771. { rc = WaitForSingleObject(pi.hProcess, INFINITE);
  772. if (rc == WAIT_FAILED) return NULL;
  773. }
  774. return pi.hProcess;
  775. }
  776. /* Expand any environment variables inside s */
  777. static char *osFindEnvVar(char *s, int idx, int *new_idx)
  778. {
  779. char *var, c;
  780. int j=idx;
  781. while (isalnum(s[j])) j++;
  782. if (j==idx) return NULL;
  783. c = s[j];
  784. s[j] = '\0';
  785. *new_idx = j;
  786. var = getenv(s+idx);
  787. s[j] = c;
  788. return var;
  789. }
  790. static char *osMangleCommand(char *s)
  791. {
  792. char *var, *res;
  793. int i, j, o, len;
  794. len = strlen(s);
  795. o = i = 0; res = s;
  796. while (s[i] != '\0')
  797. { int new_i;
  798. if (s[i] == '$' && (var = osFindEnvVar(s, i+1, &new_i)) != NULL)
  799. { /* Get new space */
  800. int lv = strlen(var);
  801. char *nres = (char *) malloc(o + lv + len - i + 1);
  802. for (j=0; j<o; j++) nres[j] = res[j];
  803. if (res != s) free(res);
  804. res = nres;
  805. for (j=0; j<lv; j++) res[o++] = var[j];
  806. i = new_i;
  807. }
  808. else if (res != s) res[o++] = s[i++];
  809. else
  810. { i++;
  811. o++;
  812. }
  813. }
  814. if (res != s) res[o] = s[i];
  815. return res;
  816. }
  817. HWND gnuplot_handle = 0;
  818. BOOL CALLBACK find_text(HWND h, LPARAM x)
  819. {
  820. char buffer[24];
  821. CSL_IGNORE(x);
  822. GetClassName(h, buffer, 20);
  823. if (strcmp(buffer, "wgnuplot_text") != 0) return TRUE;
  824. gnuplot_handle = h;
  825. return FALSE;
  826. }
  827. FILE *my_popen(char *command, char *direction)
  828. {
  829. /*
  830. * Here I take a pretty shameless direction and spot the special case of
  831. * opening an output pipe to gnuplot... and hook in a behind-the-scenes
  832. * way.
  833. */
  834. int i = 0, j;
  835. for (;;)
  836. { char *name = "gnuplot";
  837. j = i;
  838. while (*name && tolower(command[j++]) == *name) name++;
  839. if (*name == 0)
  840. { HWND parent = 0;
  841. #ifdef OLD_API
  842. if (WinExec(command, SW_SHOWMINIMIZED) <= 32) return 0;
  843. #else
  844. /*
  845. * Win32 would rather I used the following long-winded version, which provides
  846. * a pile of generality that is irrelevant here!
  847. */
  848. STARTUPINFO startup;
  849. PROCESS_INFORMATION process;
  850. clock_t t0, t1;
  851. memset(&startup, 0, sizeof(STARTUPINFO));
  852. startup.cb = sizeof(startup);
  853. startup.lpReserved = NULL;
  854. startup.lpDesktop = NULL;
  855. startup.lpTitle = NULL;
  856. startup.dwFlags = STARTF_USESHOWWINDOW;
  857. startup.wShowWindow = SW_SHOWMINIMIZED;
  858. startup.cbReserved2 = 0;
  859. startup.lpReserved2 = NULL;
  860. if (!CreateProcess(NULL, command, NULL, NULL, FALSE,
  861. 0, NULL, NULL, &startup, &process)) return 0;
  862. #endif
  863. gnuplot_handle = 0;
  864. t0 = clock();
  865. for (i=0; i<25; i++) /* Give it 5 seconds to appear */
  866. { parent = FindWindow((LPSTR)"wgnuplot_parent",
  867. (LPSTR)"gnuplot");
  868. if (parent != 0) break;
  869. t0 += CLOCKS_PER_SEC/5;
  870. while ((t1 = clock()) < t0) cwin_poll_window_manager(0);
  871. t0 = t1;
  872. }
  873. if (parent != 0)
  874. { for (i=0; i<10; i++) /* 2 more seconds for the child */
  875. { EnumChildWindows(parent, find_text, 0);
  876. if (gnuplot_handle != 0) break;
  877. t0 += CLOCKS_PER_SEC/5;
  878. while ((t1 = clock()) < t0) cwin_poll_window_manager(0);
  879. t0 = t1;
  880. }
  881. }
  882. return (FILE *)-1; // special handle for the gnuplot pipe
  883. }
  884. i++;
  885. if (command[i] == 0) break;
  886. }
  887. #ifdef __WATCOMC__
  888. CSL_IGNORE(command); CSL_IGNORE(direction);
  889. return 0;
  890. #else
  891. return _popen(command, direction);
  892. #endif
  893. }
  894. int my_pipe_putc(int c, FILE *f)
  895. {
  896. if (f == (FILE *)(-1))
  897. { if (gnuplot_handle == 0) return EOF;
  898. if (c == '\n') c = '\r';
  899. SendMessage(gnuplot_handle, WM_CHAR, c, 1L);
  900. return c;
  901. }
  902. else return putc(c, f);
  903. }
  904. int my_pipe_flush(FILE *f)
  905. {
  906. if (f != (FILE *)(-1)) return fflush(f);
  907. return 0;
  908. }
  909. void my_pclose(FILE *stream)
  910. {
  911. if (stream == (FILE *)(-1))
  912. { SendMessage(gnuplot_handle, WM_CHAR, 'q', 1L);
  913. SendMessage(gnuplot_handle, WM_CHAR, 'u', 1L);
  914. SendMessage(gnuplot_handle, WM_CHAR, 'i', 1L);
  915. SendMessage(gnuplot_handle, WM_CHAR, 't', 1L);
  916. SendMessage(gnuplot_handle, WM_CHAR, '\r', 1L);
  917. return;
  918. }
  919. #ifdef __WATCOMC__
  920. CSL_IGNORE(stream);
  921. #else
  922. _pclose(stream);
  923. #endif
  924. }
  925. #endif /* UNIX */
  926. char *my_getenv(char *s)
  927. {
  928. /*
  929. * Case fold for MSDOS
  930. */
  931. char uppercase[LONGEST_LEGAL_FILENAME];
  932. int c;
  933. char *p = uppercase;
  934. while ((c = *s++) != 0) *p++ = (char)toupper(c);
  935. *p = 0;
  936. s = uppercase;
  937. return getenv(s);
  938. }
  939. /*
  940. * MSDOS does not support the idea of home directories for
  941. * users, so in case anybody still wants to use the notation "~" that
  942. * would indicate a home directory under Unix I implement something
  943. * in terms of environment variables.
  944. */
  945. int get_home_directory(char *b, int len)
  946. {
  947. /*
  948. * Worry about "len" here...
  949. */
  950. char *w = my_getenv("home");
  951. if (w != NULL) strcpy(b, w);
  952. else strcpy(b, ".");
  953. return strlen(b);
  954. }
  955. int get_users_home_directory(char *b, int len)
  956. {
  957. /*
  958. * Worry about "len" here...
  959. */
  960. char *w, h[LONGEST_LEGAL_FILENAME];
  961. sprintf(h, "home$%s", b);
  962. w = my_getenv(h);
  963. if (w != NULL) strcpy(b, w);
  964. else strcpy(b, ".");
  965. return strlen(b);
  966. }
  967. /*
  968. * The next bit of mess is jolly - I just want to see if stdin has been
  969. * redirected to come from a file, i.e. whether I am interactive in some
  970. * sense. This may be used to decide what to do about error reports etc.
  971. * The IDEA seems generic across most systems, but the details vary in
  972. * frustrating ways.
  973. */
  974. int batchp(void)
  975. {
  976. return 0; /* !isatty(stdin->_file); */
  977. }
  978. /*
  979. * The next procedure is responsible for establishing information about
  980. * where the main checkpoint image should be recovered from, and where
  981. * and fasl files should come from.
  982. */
  983. char *find_image_directory(int argc, char *argv[])
  984. {
  985. char *w;
  986. int i = strlen(cwin_full_program_name), j;
  987. strcpy(program_name, programName);
  988. CSL_IGNORE(argc); CSL_IGNORE(argv);
  989. /*
  990. * If the current program is called c:\aaa\xxx.exe, then I look
  991. * for a file c:\aaa\xxx.img
  992. */
  993. w = (char *)(*malloc_hook)(i+1);
  994. if (w == NULL)
  995. { fprintf(stderr, "\n+++ Panic - run out of space\n");
  996. my_exit(EXIT_FAILURE);
  997. }
  998. strcpy(w, cwin_full_program_name);
  999. /*
  1000. * Windows file-names are not case-sensitive when you search, but
  1001. * I think names look neater in lower case so I force things here.
  1002. */
  1003. for (j=i-4; j>=0; j--)
  1004. { if (w[j] == '\\' || w[j] == '/') break;
  1005. w[j] = (char)tolower(w[j]);
  1006. }
  1007. w[i-3] = 'i';
  1008. w[i-2] = 'm';
  1009. w[i-1] = 'g';
  1010. return w;
  1011. }
  1012. CSLbool sigint_must_longjmp = NO;
  1013. jmp_buf sigint_buf;
  1014. void MS_CDECL sigint_handler(int code)
  1015. {
  1016. CSL_IGNORE(code);
  1017. interrupt_pending = 1;
  1018. if (sigint_must_longjmp)
  1019. { sigint_must_longjmp = 0;
  1020. longjmp(sigint_buf, 1);
  1021. }
  1022. return;
  1023. }
  1024. /*
  1025. * The following function controls memory allocation policy
  1026. */
  1027. #ifdef UNIX
  1028. int32 ok_to_grab_memory(int32 current)
  1029. {
  1030. #ifdef COMMON
  1031. return current;
  1032. #else
  1033. return 3*current + 2;
  1034. #endif
  1035. }
  1036. #else
  1037. int32 ok_to_grab_memory(int32 current)
  1038. {
  1039. MEMORYSTATUS w;
  1040. memset(&w, 0, sizeof(w));
  1041. w.dwLength = sizeof(w);
  1042. GlobalMemoryStatus(&w);
  1043. #ifdef NOT_CONFIDENT_ENOUGH_HIDE_THIS_TRASH
  1044. term_printf("memStats %d %d %d %d %d %d %d %d\n",
  1045. w.dwLength,
  1046. w.dwMemoryLoad,
  1047. w.dwTotalPhys, w.dwAvailPhys,
  1048. w.dwTotalPageFile, w.dwAvailPageFile,
  1049. w.dwTotalVirtual, w.dwAvailVirtual);
  1050. #endif
  1051. if (w.dwTotalPhys == 0)
  1052. {
  1053. #ifdef COMMON
  1054. return current;
  1055. #else
  1056. return 3*current + 2;
  1057. #endif
  1058. }
  1059. else
  1060. /*
  1061. * Here we seem to be running under win32 (not win32s) and can measure how
  1062. * much memory is currently available for this process. I will grab space
  1063. * until win32 is using all the physical memory there is. After that I will
  1064. * tend to grow much more cautiously.
  1065. */
  1066. { int32 freemem = w.dwAvailPhys / CSL_PAGE_SIZE;
  1067. if (freemem == 0) return current / 2 + 1;
  1068. if (freemem > 3*current + 2) freemem = 3*current + 2;
  1069. return freemem;
  1070. }
  1071. }
  1072. #endif
  1073. /* end of syswxwin.c */