summaryrefslogtreecommitdiff
path: root/Build/source/texk/windvi/winhtex.c
blob: e3dc3477dcabd9b54e95d3a9e28d30ee739cfdb2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
/* 
   winhtex.c : implementation file
   Time-stamp: "99/07/23 17:33:42 popineau"
   
   Copyright (C) 1999
      Fabrice Popineau <Fabrice.Popineau@supelec.fr>

   This file is part of Windvi.

   Windvi is free software; you can redistribute it and/or modify it
   under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2, or (at your option)
   any later version.
   
   Windvi is distributed in the hope that it will be useful, but WITHOUT
   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
   or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
   License for more details.
   
   You should have received a copy of the GNU General Public License
   along with Windvi; if not, write to the Free Software Foundation, 59
   Temple Place - Suite 330, Boston, MA 02111-1307, USA. */

#include "wingui.h"
#include "xdvi-config.h"
#include <wininet.h>

#include <kpathsea/c-proto.h>

static HINTERNET hInternetSession = NULL; 

static BOOL init_htex_features()
{
    /*    __asm int 3; */
  if (!hInternetSession) {
    hInternetSession = InternetOpen(_T("Windvi"), 
				    INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0); 
    if (!hInternetSession) { 
      fprintf(stderr, "Unable to open an Internet Session !n");
    }
  }
  return hInternetSession != NULL;
}

/*
  Windows can get rid of all the libwww stuff, because
  there is no need much than opening remote files, which
  can be done through the ShellExecute() call.
  Anyway, to be still compatible with the code written here, 
  some of the libwww functions have been rewritten.
*/

#include "winhtex.h"
#include <kpathsea/c-pathch.h>

/*
**  Search the URL and determine whether it is a relative or absolute URL.
**  We check to see if there is a ":" before any "/", "?", and "#". If this
**  is the case then we say it is absolute. Otherwise it is relative.
*/
BOOL HTURL_isAbsolute (const char * url)
{    
  /* Given the context, we can't let windvi believe that
     c:/foo.dvi is an absolute url ... */
  if (NAME_BEGINS_WITH_DEVICE(url))
    return NO;

  if (url) {	
    const char * ptr = url;
    while (*ptr) {
      if (*ptr == ':') return YES;
      if (*ptr == '/' || *ptr == '?' || *ptr == '#') break;
      ptr ++;
    }
  }	
  return NO;
}

int
invokeviewer(char *filename)
{
  int ret;
  ret = ShellExecute(NULL, "open", dvi_name, NULL, NULL, SW_SHOWNORMAL);
  if (ret <= 0)
    return 0;
  else 
    return 1;
}

char * HTParse (const char *aName, const char *relatedName, int wanted)
{
  int ret;
  char *result, *abs, *rel;
  DWORD len;
  BOOL bAgain = FALSE; 
  
#if 0
  if (aName && *aName) {
      abs = xmalloc(strlen(aName) + 2);
      strcpy(abs, aName);
      strcat(abs, "/");
  }
  else {
    /* FIXME: what does mean a null base url ? */
    abs = xstrdup("");
  }
#else
  abs = xstrdup(aName ? aName : "");
#endif
  rel = (relatedName ? relatedName : "");

  len = strlen(abs) + strlen(rel) + 45;
  result = xmalloc(len*sizeof(char));

#if 0
  fprintf(stderr, "HTParse called with aName = %s and relatedName = %s\n",
	  aName, relatedName);
#endif
  do {
    if (*rel) {
      ret = InternetCombineUrl(abs, rel, result, &len, ICU_BROWSER_MODE);
    }
    else {
      ret = InternetCanonicalizeUrl(abs, result, &len, ICU_BROWSER_MODE);
    }
    bAgain = FALSE;
    if (!ret) {
      switch (GetLastError()) {
      case ERROR_INSUFFICIENT_BUFFER:
#if 0
	fprintf(stderr, "InternetCombineUrl: buffer was not long enough (needed %d)\n", len+1);
#endif
	result = xrealloc(result, len+1);
	bAgain = TRUE;
	break;
      case ERROR_BAD_PATHNAME:
	fprintf(stderr, "InternetCombineUrl failed, one of the path names is bad:\n\t%s\n\t%s\n",
		aName, relatedName);
	break;
      case ERROR_INTERNET_INVALID_URL:
	fprintf(stderr, "InternetCombineUrl failed, url %s is malformed.\n", aName);
	break;
      case ERROR_INVALID_PARAMETER:
	fprintf(stderr, "InternetCombineUrl failed with `invalid parameter'.\n");
	break;	
      default:
	break;
      }
    }
  } while (bAgain);
  
  /* abs has always been malloc'ed */
  if (abs)
    free(abs);

  if (!ret) {
    /* FIXME: Try this poor thing ! */
    sprintf(result, "%s/%s", aName, relatedName);
  }
  
#if 0
  fprintf(stderr, "HTParse: result is %s\n", result);
#endif
  return result;    
}

int
www_fetch(char *url, char *savefile)
{    
  HINTERNET hUrlDump;
  extern HINTERNET hInternetSession; 
  DWORD dwSize=TRUE;
  LPSTR lpszData;
  LPSTR lpszOutPut;
  LPSTR lpszHolding;
  int nCounter=1;
  int nBufferSize;
  DWORD BigSize=8000;
  FILE *fSave;

  if ((fSave = fopen(savefile, "wb")) == NULL) {
    perror("www_fetch");
    return 0;
  }

  hUrlDump = InternetOpenUrl(hInternetSession, url, NULL, 0, 
			     INTERNET_FLAG_RAW_DATA, 0);
  if (!hUrlDump) {
    Win32Error("www_fetch/InternetOpenUrl");
    fclose(fSave);
    return 0;
  }

  do {

    /* Read the data */
    if(InternetReadFile(hUrlDump,(LPVOID)lpszData,BigSize,&dwSize) == FALSE) {
      Win32Error("www_fetch/InternetReadFile");
      break;
    }
    else {
      fwrite(lpszData, sizeof(char), dwSize , fSave);
      if (dwSize == 0) {
	break;
      }
    }
  }
  while (TRUE);
  
  /* Close the HINTERNET handle */
  InternetCloseHandle(hUrlDump);
  
  /* Set the cursor back to an arrow */
  SetCursor(LoadCursor(NULL,IDC_ARROW));
  
  /* Return */
  return TRUE;
}

void
htex_cleanup(int arg)
{
	/* Delete all the temp files we created */
	for (;nURLs>0; nURLs--) {
	  /* fprintf(stderr,"htex: Unlinking %s\n",filelist[nURLs-1].file); */
	  unlink(filelist[nURLs-1].file);
	}
	if (hInternetSession) 
	  InternetCloseHandle(hInternetSession);
}