File: comterpserv.cc

package info (click to toggle)
ivtools 1.2.11a2-4
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 13,364 kB
  • sloc: cpp: 174,988; ansic: 12,717; xml: 5,359; perl: 2,164; makefile: 831; sh: 326
file content (521 lines) | stat: -rw-r--r-- 14,492 bytes parent folder | download
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
/*
 * Copyright (c) 2000 IET Inc.
 * Copyright (c) 1994-1997 Vectaport Inc.
 *
 * Permission to use, copy, modify, distribute, and sell this software and
 * its documentation for any purpose is hereby granted without fee, provided
 * that the above copyright notice appear in all copies and that both that
 * copyright notice and this permission notice appear in supporting
 * documentation, and that the names of the copyright holders not be used in
 * advertising or publicity pertaining to distribution of the software
 * without specific, written prior permission.  The copyright holders make
 * no representations about the suitability of this software for any purpose.
 * It is provided "as is" without express or implied warranty.
 *
 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS.
 * IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL,
 * INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
 * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
 * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
 * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 * 
 */

#include <ComTerp/comhandler.h>
#include <ComTerp/_comterp.h>
#include <ComTerp/_comutil.h>
#include <ComTerp/comterpserv.h>
#include <ComTerp/comvalue.h>
#include <ComTerp/ctrlfunc.h>
#include <ComTerp/strmfunc.h>
#include <Attribute/attrlist.h>
#include <OS/math.h>
#include <iostream.h>
#include <string.h>
#include <fstream.h>

#if BUFSIZ>1024
#undef BUFSIZ
#define BUFSIZ 1024
#endif

// #define TIMING_TEST
#if defined(TIMING_TEST)
#include <sys/time.h>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>

/* Return 1 if the difference is negative, otherwise 0.  */
int timeval_subtract(struct timeval *result, struct timeval *t2, struct timeval *t1)
{
  long int diff = (t2->tv_usec + 1000000 * t2->tv_sec) - (t1->tv_usec + 1000000 * t1->tv_sec);
  result->tv_sec = diff / 1000000;
  result->tv_usec = diff % 1000000;

  return (diff<0);
}

/* Return 1 if the sum is negative, otherwise 0.  */
int timeval_add(struct timeval *result, struct timeval *t2, struct timeval *t1)
{
  long int sum = (t2->tv_usec + 1000000 * t2->tv_sec) + (t1->tv_usec + 1000000 * t1->tv_sec);
  result->tv_sec = sum / 1000000;
  result->tv_usec = sum % 1000000;

  return (sum<0);
}

void timeval_print(struct timeval *tv)
{
  char buffer[30];
  time_t curtime;

  printf("%ld.%06ld\n", tv->tv_sec, tv->tv_usec);
  curtime = tv->tv_sec;
#if 0
  strftime(buffer, 30, "%m-%d-%Y  %T", localtime(&curtime));
  printf(" = %s.%06ld\n", buffer, tv->tv_usec);
#endif
}
#endif /* defined(TIMING_TEST) */

ComTerpServ::ComTerpServ(int linesize, int fd)
: ComTerp()
{
    _linesize = linesize;
    _instr = new char[_linesize];
    _outstr = new char[_linesize];
    _inptr = this;
    _infunc = (infuncptr)&ComTerpServ::s_fgets;
    _eoffunc = (eoffuncptr)&ComTerpServ::s_feof;
    _errfunc = (errfuncptr)&ComTerpServ::s_ferror;
    _outptr = this;
    _outfunc = (outfuncptr)&ComTerpServ::s_fputs;
    _fd = fd;
    _outpos = 0;
    if (_fd>=0)
      _fptr = fdopen(_fd, "rw");
    else
      _fptr = stdin;
#ifdef HAVE_ACE
    _handler = nil;
#endif

    /* inform the parser which infunc is the oneshot infunc */
    _oneshot_infunc = (infuncptr)&s_fgets;
  
    /* initialize shadow copy too */
    __oneshot_infunc = (infuncptr)&s_fgets;

    _logger_mode = 0;
    _delete_later = 0;
}

ComTerpServ::~ComTerpServ() {
    delete [] _instr;
    delete [] _outstr;
    if (_fptr != stdin)
      fclose(_fptr);
}

void ComTerpServ::load_string(const char* expr) {
    _inpos = 0;

    /* copy string into buffer, ensuring it ends with a newline */
    int count=0;
    char* inptr = _instr;
    char* exptr = (char*) expr;
    char ch;
    do {
        ch = *exptr++;
	*inptr++ = ch;
    } while (ch && count++<_linesize-2);
    if (!ch && count>0 && *(inptr-2) != '\n') {
	    *(inptr-1) = '\n';
	    *(inptr) = '\0';
    } else if (count==_linesize-2) {
            *(inptr) = '\n';
	    *(inptr+1) = '\0';
    }
}

char* ComTerpServ::s_fgets(char* s, int n, void* serv) {
    ComTerpServ* server = (ComTerpServ*)serv;
    char* instr = server->_instr;
    char* outstr = s;
    int& inpos = server->_inpos;
    int& linesize = server->_linesize;

    int outpos;

    /* copy characters until n-1 characters are transferred, */
    /* the input buffer is exhausted, or a newline is found. */
    for (outpos = 0; outpos < n-1 && inpos < linesize-1 && instr[inpos] != '\n' && instr[inpos] != '\0';)
	outstr[outpos++] = instr[inpos++];

    /* copy the newline character if there is room */
    if (outpos < n-1 && inpos < linesize-1 && instr[inpos] == '\n')
	outstr[outpos++] = instr[inpos++];

    /* append a null byte */
    outstr[outpos] = '\0';

    return s;
}

int ComTerpServ::s_feof(void* serv) {
    ComTerpServ* server = (ComTerpServ*)serv;
    int& inpos = server->_inpos;

    return inpos == -1;
}

int ComTerpServ::s_ferror(void* serv) {
    return 0;
}

int ComTerpServ::s_fputs(const char* s, void* serv) {
    ComTerpServ* server = (ComTerpServ*)serv;
    char* outstr = server->_outstr;
    int& outpos = server->_outpos;
    int& linesize = server->_linesize;

    for (; outpos < linesize-1 && s[outpos]; outpos++)
	outstr[outpos] = s[outpos];
    outstr[outpos] = '\0';
    return 1;
}

char* ComTerpServ::fd_fgets(char* s, int n, void* serv) {
    ComTerpServ* server = (ComTerpServ*)serv;
    int fd = Math::max(server->_fd, 1);
    char instr[BUFSIZ];
    FILE* ifptr = fd==0 ? stdin : server->handler()->rdfptr();
    FILEBUF(fbuf, ifptr, ios_base::in);
    istream in (&fbuf);
    in.get(instr, BUFSIZ, '\n');  // needs to be generalized with <vector.h>
    server->_instat = in.good(); 
  
    char* outstr = s;
    int inpos = 0;
    int& bufsize = server->_bufsiz;

    int outpos;

    /* copy characters until n-1 characters are transferred, */
    /* or the input buffer is exhausted */
    for (outpos = 0; outpos < n-1 && inpos < bufsize-1 && 
	   instr[inpos] != '\n' && instr[inpos] != '\0';)
	outstr[outpos++] = instr[inpos++];

    /* add a newline character if there is room */
    if (outpos < n-1 && inpos < bufsize-1)
	outstr[outpos++] = '\n';

    /* append a null byte */
    outstr[outpos] = '\0';

    return s;
}

int ComTerpServ::fd_fputs(const char* s, void* serv) {
    ComTerpServ* server = (ComTerpServ*)serv;
    char* outstr = server->_outstr;
    int& outpos = server->_outpos;
    int& bufsize = server->_bufsiz;

    int fd = (int)server->_fd;
    FILE* ofptr = fd==1 ? stdout : server->handler()->wrfptr();
    FILEBUF(fbuf, ofptr, ios_base::out);
    ostream out(&fbuf);
    for (; outpos < bufsize-1 && s[outpos]; outpos++)
	out.put(s[outpos]);
    out.flush();
    outpos = 0;
    return 1;
}

int ComTerpServ::run(boolean one_expr, boolean nested) {
    char buffer[_linesize];
    char errbuf[_linesize];
    errbuf[0] = '\0';
    int status = 0;

    _inptr = _fptr;
    _infunc = (infuncptr)&fgets;
    _eoffunc = (eoffuncptr)&ffeof;
    _errfunc = (errfuncptr)&fferror;
    _fd = handler() ? handler()->get_handle() : (_fd > 0 ? _fd : fileno(stdout));
    _outfunc = (outfuncptr)&fd_fputs;
    _linenum = 0;

    ComTerp::run(one_expr, nested);

    _inptr = this;
    _infunc = (infuncptr)&ComTerpServ::s_fgets;
    _eoffunc = (eoffuncptr)&ComTerpServ::s_feof;
    _errfunc = (errfuncptr)&ComTerpServ::s_ferror;
    _outptr = this;
    _outfunc = (outfuncptr)&ComTerpServ::s_fputs;
    return status;
}

int ComTerpServ::runfile(const char* filename, boolean popen_flag) {
    /* save enough state as needed by this interpreter */
    push_servstate();
    _inptr = this;
    _infunc = (infuncptr)&ComTerpServ::s_fgets;
    _eoffunc = (eoffuncptr)&ComTerpServ::s_feof;
    _errfunc = (errfuncptr)&ComTerpServ::s_ferror;
    _outfunc = nil;
    _linenum = 0;

    _linesize = BUFSIZ*BUFSIZ;
    char inbuf[_linesize];
    char outbuf[_linesize];
    inbuf[0] = '\0';
    FILE* ifptr = NULL;
    ifptr = popen_flag ? popen(filename, "r") : fopen(filename, "r");
    if (!ifptr) {
      fprintf(stderr, "Unable to open file %s to run\n", filename);
      pop_servstate();
      return -1;
    }
    FILEBUF(ibuf, ifptr, ios_base::in);
    istream istr(&ibuf);
    ComValue* retval = nil;
    int status = 0;
   

    /* save tokens to restore after the file has run */
    int toklen;
    postfix_token* tokbuf = copy_postfix_tokens(toklen);
    int tokoff = _pfoff;

    int last_status = 0;
    while( istr.good()) {
#if defined(TIMING_TEST)
        static struct timeval tvBefore, tvAfter, tvParse, tvConvert, tvDiff;
#endif
        *inbuf='\0';
        istr.getline(inbuf, _linesize-1);
	if (istr.eof() && !*inbuf)  // deal with last line without new-line
	  break;
        if (_linenum==0 && !*inbuf) { // run a dummy space in to initialize parser
            inbuf[0]=' ';
            inbuf[1]='\0';
        }
	if (*inbuf)
            load_string(inbuf);
        else
            increment_linenum();
	if (*inbuf && (last_status=read_expr())) {
#if defined(TIMING_TEST)
	    static int initialized=0;
	    if (!initialized) {
	      initialized = 1;
	      gettimeofday(&tvBefore, NULL);
	      gettimeofday(&tvAfter, NULL);
	      timeval_subtract(&tvParse, &tvBefore, &tvBefore);
	      timeval_subtract(&tvConvert, &tvBefore, &tvBefore);
	    }
	    gettimeofday(&tvAfter, NULL);
	    timeval_subtract(&tvDiff, &tvAfter, &tvBefore);
	    timeval_add(&tvParse, &tvDiff, &tvParse);
	    tvBefore = tvAfter;
	    fprintf(stderr, "Parse Time:  ");
	    timeval_print(&tvParse);
#endif
	    if (eval_expr(true)) {
	        char buf[BUFSIZ];
	        snprintf(buf, BUFSIZ, "comterp(%s)", filename);
	        err_print( stderr, buf );
                FILE* ofptr = handler() ? handler()->wrfptr() : stdout; 
	        FILEBUF(obuf, ofptr, ios_base::out);
		ostream ostr(&obuf);
		ostr.flush();
		status = -1;
	    } else if (quitflag()) {
                delete retval; retval = nil; // remove prior retval
	        status = 1;
	        break;
	    } else if (!func_for_next_expr() && val_for_next_func().is_null() /* && muted()!=1 */) {
#if defined(TIMING_TEST)
	        gettimeofday(&tvAfter, NULL);
		timeval_subtract(&tvDiff, &tvAfter, &tvBefore);
		timeval_add(&tvConvert, &tvDiff, &tvConvert);
		tvBefore = tvAfter;
		fprintf(stderr, "Convert Time:  ");
		timeval_print(&tvConvert);
#endif
	        if (stack_top().is_stream() && autostream()) {
		  ComValue streamv(stack_top());
		  do {
		    pop_stack();
		    NextFunc::execute_impl(this, streamv);
		  } while (stack_top().is_known());
		  pop_stack();
		} else {
		  /* save last thing on stack */  
		  if(retval) delete retval;
		  retval = new ComValue(pop_stack());
		}
	    }

	    
	} else 	if (*inbuf) {
          char buf[BUFSIZ];
          snprintf(buf, BUFSIZ, "comterp(%s)", filename);
	  err_print( stderr, buf );
          FILE* ofptr = handler() ? handler()->wrfptr() : stdout; 
	  FILEBUF(obuf, ofptr, ios_base::out);
	  ostream ostr(&obuf);
	  ostr.flush();
	  status = -1;
	}

    }

    if(last_status==0 && *inbuf) {
        COMERR_SET1( ERR_UNEXPECTED_EOF, _linenum );
        *inbuf = '\0';
        parser_reset();
    }

    load_postfix(tokbuf, toklen, tokoff);
    delete tokbuf;
    ibuf.close();
    if (ifptr) 
        if(popen_flag)
            pclose(ifptr);
        else
            fclose(ifptr);

    if (retval) {
        push_stack(*retval);
	delete retval;
    } else
        if (!quitflag()) 
            push_stack(ComValue::nullval());

    pop_servstate();

    return status;
}

ComValue ComTerpServ::run(const char* expression, boolean nested) {
    _errbuf[0] = '\0';

    push_servstate();
    _pfcomvals = nil;

    if (expression) {
        load_string(expression);
	_infunc = (infuncptr)&ComTerpServ::s_fgets;
	_eoffunc = (eoffuncptr)&ComTerpServ::s_feof;
	_errfunc = (errfuncptr)&ComTerpServ::s_ferror;
	_inptr = this;
        read_expr();
        err_str(_errbuf, BUFSIZ, "comterp");
    }
    int status;
    if (!*_errbuf) {
	status = eval_expr(nested);
	err_str(_errbuf, BUFSIZ, "comterp");
    }

    pop_servstate();

    return (*_errbuf || status!=FUNCOK) ? ComValue::nullval() : pop_stack();
}

// #include "/usr/include/malloc.h"

ComValue ComTerpServ::run(postfix_token* tokens, int ntokens) {
    _errbuf[0] = '\0';

    push_servstate();

    delete _pfbuf;
    _pfbuf = copy_postfix_tokens(tokens, ntokens);
    _pfnum = ntokens;
    _pfoff = 0;

    eval_expr(/*nested=*/1);
    err_str(_errbuf, BUFSIZ, "comterp");

    ComValue retval(*_errbuf ? ComValue::nullval() : pop_stack());
    delete _pfbuf;
    _pfbuf = nil;
    _pfnum = 0;
    _pfoff = 0;
    pop_servstate();

    return retval;
}

postfix_token* ComTerpServ::gen_code(const char* script, int& ntoken) {
    push_servstate();
    load_string(script);
    read_expr();
    postfix_token* copied_tokens = copy_postfix_tokens(ntoken);
    pop_servstate();
    return copied_tokens;
}

void ComTerpServ::read_string(const char* script) {
    load_string(script);
    read_expr();
}

void ComTerpServ::add_defaults() {
  if (!_defaults_added) {
    ComTerp::add_defaults();
    add_command("remote", new RemoteFunc(this));
    add_command("socket", new SocketFunc(this));
    add_command("eval", new EvalFunc(this));
  }
}

AttributeValueList* ComTerpServ::parse_next_expr(FILE* fptr) {

    /* save state for this interpreter */
    push_servstate();
    
    /* install different inputs */
    _inptr = fptr;
    _infunc = (infuncptr)&fgets;
    _eoffunc = (eoffuncptr)&feof;
    _errfunc = (errfuncptr)&ferror;
    _outfunc = nil;

    /* save tokens to restore after the file has run */
    int toklen;
    postfix_token* tokbuf = copy_postfix_tokens(toklen);
    int tokoff = _pfoff;
    
    /* parse a complete expression */
    int status=0;
    do {
	status = parser(_inptr,	 _infunc, _eoffunc, _errfunc, NULL, NULL,
			_buffer, _bufsiz, &_bufptr, _token, _toksiz, &_linenum,
			&_pfbuf, &_pfsiz, &_pfnum);
	if (status) 
	    err_print( stdout, "parser" );
	else
	    for (int i = 0; i < _pfnum; i++) print_pfbuf(_pfbuf,i);
    } while (status==0 && strlen(_buffer)>_bufptr);
    // return _pfnum==0 || _pfbuf[_pfnum-1].type != TOK_EOF;

    /* restore tokens */
    load_postfix(tokbuf, toklen, tokoff);
    delete tokbuf;
    
    /* restore state for this interpreter */
    pop_servstate();

    return nil;

}