~ubuntu-branches/ubuntu/karmic/python-scipy/karmic

« back to all changes in this revision

Viewing changes to Lib/xplt/src/play/win/wstdio.c

  • Committer: Bazaar Package Importer
  • Author(s): Daniel T. Chen (new)
  • Date: 2005-03-16 02:15:29 UTC
  • Revision ID: james.westby@ubuntu.com-20050316021529-xrjlowsejs0cijig
Tags: upstream-0.3.2
ImportĀ upstreamĀ versionĀ 0.3.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * wstdio.c -- $Id: wstdio.c,v 1.1 2003/03/08 15:26:49 travo Exp $
 
3
 * p_stdinit, p_stdout, p_stdin for MS Windows
 
4
 *
 
5
 * Copyright (c) 1999.  See accompanying LEGAL file for details.
 
6
 */
 
7
 
 
8
#include "playw.h"
 
9
#include "pstdlib.h"
 
10
 
 
11
int (*w_stdinit)(void(**)(char*,long), void(**)(char*,long))= 0;
 
12
int w_no_mdi = 0;
 
13
 
 
14
static void (*w_on_stdin)(char *input_line)= 0;
 
15
static void w_formout(char *line, void (*func)(char *, long));
 
16
 
 
17
static void (*w_stdout)(char *line, long len)= 0;
 
18
static void (*w_stderr)(char *line, long len)= 0;
 
19
 
 
20
int console_mode = 0;
 
21
 
 
22
void
 
23
p_stdinit(void (*on_stdin)(char *input_line))
 
24
{
 
25
  char *mdi = p_getenv("NO_MDI");
 
26
  if (mdi && mdi[0] && (mdi[0]!='0' || mdi[1])) w_no_mdi |= 1;
 
27
  if (!w_no_mdi && !w_stdinit) w_no_mdi = 1, AllocConsole();
 
28
  if (!w_no_mdi || con_stdinit(&w_stdout, &w_stderr)) {
 
29
    w_stdinit(&w_stdout, &w_stderr);
 
30
  } else if (w_main_window) {
 
31
    /* without this, actual first window created does not show properly */
 
32
    ShowWindow(w_main_window, SW_SHOWNORMAL);
 
33
    ShowWindow(w_main_window, SW_HIDE);
 
34
  }
 
35
  w_on_stdin = on_stdin;
 
36
}
 
37
 
 
38
void p_stdout(char *output_line)
 
39
{
 
40
  w_formout(output_line, w_stdout);
 
41
}
 
42
 
 
43
void p_stderr(char *output_line)
 
44
{
 
45
  w_formout(output_line, w_stderr);
 
46
}
 
47
 
 
48
char *w_sendbuf(long len)
 
49
{
 
50
  /* console app: called by worker to get buffer to hold stdin
 
51
   * gui app: called by boss to get buffer to hold input line
 
52
   *          called by worker w_sendbuf(-1) to retrieve boss buffer
 
53
   * therefore must use raw Windows memory manager routines */
 
54
  static char *buf = 0;
 
55
  if (len >= 0) {
 
56
    HANDLE heap = GetProcessHeap();
 
57
    if (len <= 0x100) len = 0x100;
 
58
    else len = ((len-1)&0x3ff) + 0x100;
 
59
    buf = buf? HeapReAlloc(heap, HEAP_GENERATE_EXCEPTIONS, buf, len+1) :
 
60
              HeapAlloc(heap, HEAP_GENERATE_EXCEPTIONS, len+1);
 
61
  }
 
62
  return buf;
 
63
}
 
64
 
 
65
void
 
66
w_deliver(char *buf)
 
67
{
 
68
  int cr, i, j;
 
69
  for (i=j=0 ; buf[j] ; j=i) {
 
70
    for (; buf[i] ; i++) if (buf[i]=='\r' || buf[i]=='\n') break;
 
71
    cr = (buf[i]=='\r');
 
72
    buf[i++] = '\0';
 
73
    if (cr && buf[i]=='\n') i++;
 
74
    /* deliver one line at a time, not including newline */
 
75
    if (w_on_stdin) w_on_stdin(buf+j);
 
76
  }
 
77
}
 
78
 
 
79
static void
 
80
w_formout(char *line, void (*func)(char *, long))
 
81
{
 
82
  if (!p_signalling && line) {
 
83
    static char *buf = 0;
 
84
    HANDLE heap = GetProcessHeap();
 
85
    long len = 256, j = 0, i = 0;
 
86
    char c = line[i];
 
87
    if (!buf)
 
88
      buf = HeapAlloc(heap, HEAP_GENERATE_EXCEPTIONS, 258);
 
89
    do {
 
90
      if (j >= len) {
 
91
        buf = HeapReAlloc(heap, HEAP_GENERATE_EXCEPTIONS, buf, 2*len+2);
 
92
        len *= 2;
 
93
      }
 
94
      if (line[i]!='\n' || c=='\r' || console_mode) {
 
95
        if (line[i] == '\a') {
 
96
          i++;
 
97
          p_feep(0);
 
98
          continue;
 
99
        } else if (console_mode && line[i]=='\r' && line[i+1]=='\n') {
 
100
          i++;
 
101
          continue;
 
102
        }
 
103
      } else {
 
104
        buf[j++] = '\r';
 
105
      }
 
106
      buf[j++] = c = line[i++];
 
107
    } while (c);
 
108
    func(buf, j-1);
 
109
    if (len > 256)
 
110
      buf = HeapReAlloc(heap, HEAP_GENERATE_EXCEPTIONS, buf, 258);
 
111
  }
 
112
}