summaryrefslogtreecommitdiff
path: root/apps/JAWS/clients/WebSTONE/src/parse_file_list.c
blob: 73854d1a056edccaed03a33061265ad18c0e85ce (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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
/**************************************************************************
 *									  *
 * 		 Copyright (C) 1995 Silicon Graphics, Inc.		  *
 *									  *
 *  These coded instructions, statements, and computer programs were	  *
 *  developed by SGI for public use.  If any changes are made to this code*
 *  please try to get the changes back to the author.  Feel free to make  *
 *  modifications and changes to the code and release it.		  *
 *									  *
 **************************************************************************/
#include <stdio.h>
#include <errno.h>
#include <signal.h>
#include <stdlib.h>
#include <string.h>

#ifndef WIN32
#include <netdb.h>
#include <sys/param.h>
#endif /* WIN32 */

#include <sys/types.h>

#ifndef WIN32
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/errno.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <netinet/in.h>
#else
#include <windows.h>
#include <process.h>
#include <time.h>
#include <winsock.h>
#endif /* WIN32 */

#include <ctype.h>

#include "sysdep.h"
#include "bench.h"


/* 
 * count_file_list()
 * given a filename, return a guess at the number of pages
 */
int
count_file_list(const char *url_list_file)
{
  FILE 	*fp;
  long int num_of_pages;
  char 	   a_line[BUFSIZ];
  char     *textvalue;
  int      returnval;

  fp = fopen(url_list_file, "r");
  
  if (fp == NULL)
    {
      D_PRINTF( "Error %d opening filelist %s: %s\n", 
		       errno, url_list_file, strerror(errno) );;
      return(returnerr("Error %d opening filelist %s: %s\n", 
		       errno, url_list_file, strerror(errno)));
    }
  else
    {
      num_of_pages = 0;
      D_PRINTF( "Opened file, about to count\n" );
      /*
       * parse through the file line-by-line 
       * strip out comments, but don't check for URL consistency
       */
      while (fgets(a_line, BUFSIZ, fp) != NULL)
	{
	  textvalue = a_line;
	  /* check for comments */
	  if (strchr(textvalue, '#') != NULL)
	    {
	      /* throw out anything after any '#' */
	      D_PRINTF( "Stripping comment from line: %s\n", textvalue );
	      returnval = strcspn(textvalue, "#");
	      D_PRINTF( "Found first # at %d\n", returnval );
	      if (returnval == 0) 
		{
		  textvalue = NULL;
		}
	    }
	  /* is there more? */
	  if (textvalue != NULL) 
	    {
	      num_of_pages++;
	      D_PRINTF( "Found %ld pages\n", num_of_pages );
	    }
	}
      return(num_of_pages);
    }
  fclose(fp);
} /* end count_file_list() */

/* 
 * parse_file_list()
 * given a filename, a pointer to a page list, and pointers to integers
 * for the number of pages and the number of files, parse its contents.
 */
void
parse_file_list (const char *url_list_file, page_list_t *page_list, 
		 long int *num_of_pages, long int *num_of_files)
{
  FILE 	*fp;
  int         filenum;
  int         returnval;
  int         loadnum;
  char 	      a_line[BUFSIZ];
  char        tempbuf[BUFSIZ];
  char        *textvalue;
  int	      numfiles = 1, numpages = 0;
  int	      maxpages = *num_of_pages;
  page_list_t	*pp;

  extern int haveproxyserver;

  fp = fopen(url_list_file, "r");
  
  if (fp == NULL)
    {
      errexit("Error %d opening filelist: %s\n", errno, strerror(errno));
    }
  else
    {
      /* 
       * GRAB A LINE. FORMAT IS: URL WEIGHTINGFACTOR 
       * EXAMPLE: http://www/file.html 1
       */
      D_PRINTF( "File is open.\n" );
      while(fgets(a_line, BUFSIZ, fp) != NULL)
	{
	  textvalue = a_line;
	  /* check for comments */
	  if (strchr(textvalue, '#') != NULL)
	    {
	      /* throw out anything after a '#' */
	      D_PRINTF( "Stripping comment from line: %s\n", textvalue );
	      returnval = strcspn(textvalue, "#");
	      D_PRINTF( "Found first # at %d\n", returnval );
	      if (returnval == 0) 
		continue;
	    }

	  if (numpages >= *num_of_pages)
	    errexit("Out of space in parse_file_list()\n");

	  pp = &page_list[numpages];

	  D_PRINTF( "Processing page %ld\n", numpages );
	  loadnum = 0;

	  if (textvalue != NULL) { /* is there more? */
	    /* check for weighting factor */
	    D_PRINTF( "Setting page values from: %s\n", textvalue );
	    returnval = sscanf(textvalue, "%s%d", tempbuf, &loadnum);
	    D_PRINTF( "Scan for weighting returns %d, %d\n",
	      returnval, loadnum );
	    if (returnval == EOF || loadnum <= 0)
	      {
		pp->load_num = 1;
	      }
	    else
	      {
		pp->load_num = loadnum;
	      }
	    D_PRINTF( "Setting load=%d for line: %s\n", 
	      pp->load_num, textvalue );

	    /* placeholder for grouping multiple files onto one page */
	    pp->num_of_files = 1;
	    filenum = 0;

	    textvalue = tempbuf;
	    D_PRINTF( "Line is now: %s\n", textvalue );

	    /* 
	     * if we've got a proxy server, we'll assume that the
	     * remaining text is a valid URL, and stuff it into
	     * page_list[numpages].filename[filenum]
	     * Otherwise, we'll have to parse it out.
	     */

	    if (haveproxyserver)
	      {
		pp->servername[filenum] = NULL;
		pp->port_number[filenum] = 0;
		strcpy(pp->filename[filenum], textvalue);
	      }
	    else /* no proxy server, so we have to parse it out... */
	      {
		/* try http://server(:port)/file */
		D_PRINTF( "Trying http://server(:port)/filename\n" );
		returnval = sscanf(textvalue, "http://%[^/]%s",  
				   tempbuf,
				   a_line);
		/* check server string for :port */
		if (returnval != 0 && returnval != EOF) 
		  {
		    D_PRINTF( "Setting filename %s\n", a_line );
		    strcpy(pp->filename[filenum], 
			   a_line);
		    
		    D_PRINTF( "Checking %s for :portnumber\n", tempbuf );
		    returnval = sscanf(tempbuf, "%[^:]:%d",
				       a_line,
				       &pp->port_number[filenum]);
		
		    if (returnval < 2)
		      {
			pp->port_number[filenum] = 80;
		      }
		    if (returnval == EOF)
		      {
			pp->servername[filenum] = NULL;
		      }
		    else
		      {
			D_PRINTF( "Port number %d, setting server %s\n",
			  pp->port_number[filenum],
			  a_line );
			
			strcpy(pp->servername[filenum], a_line);
		      }

		    D_PRINTF( "Server %s, port number %d\n", 
		      pp->servername[filenum],
		      pp->port_number[filenum] );
		  }
		else /* no good - try straight filename */
		  {
		    pp->port_number[filenum] = 80;
		    D_PRINTF( "Trying filename, returnval=%d\n", 
		      returnval );
		    pp->servername[filenum] = NULL;
		    D_PRINTF( "Server %s, port number %d\n", 
		      pp->servername[filenum],
		      pp->port_number[filenum] );
		    returnval = sscanf(textvalue, "%s", a_line);
		    D_PRINTF( "Scan returned filename %s\n", a_line );

		    strcpy(pp->filename[filenum], a_line);
		  } /* end of parsing */
	      } /* end if haveproxyserver */

	    D_PRINTF( "Done parsing line\n" );
	    D_PRINTF( "Got server %s, port %d, file %s, returnval %d\n",
	      pp->servername[filenum],
	      pp->port_number[filenum],
	      pp->filename[filenum],
	      returnval );
	  } /* end if textvalue not NULL */

	  numpages++;
	} /* end while not EOF */
      if (numpages < 1)
	{
	  returnerr("No files are specified by filelist\n");
	}
    } /* end if file ok */
    fclose(fp);
    D_PRINTF( "Returning %ld pages and %ld files\n", 
      numpages, numfiles );

    *num_of_pages = numpages;
    *num_of_files = numfiles;
}
/* end parse_file_list */

long int
load_percent(page_list_t *page_list, long int number_of_pages)
{
  int i;
  long int index_number = 0;

  for (i = 0; i < number_of_pages; i++)
    {    
      index_number += page_list[i].load_num;
    }
  
  D_PRINTF( "load_percent returning %d\n", (index_number) );
  return(index_number);
}