File: iofunc.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 (470 lines) | stat: -rw-r--r-- 13,284 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
/*
 * Copyright (c) 2000 IET Inc.
 * Copyright (c) 1999 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/iofunc.h>
#include <ComTerp/comvalue.h>
#include <ComTerp/comterp.h>
#include <Attribute/aliterator.h>
#include <Attribute/attrlist.h>
#include <OS/math.h>
#include <iostream.h>
#include <strstream>
#if __GNUC__>=3
#include <fstream.h>
#endif
#include <streambuf>
#include <unistd.h>

#define TITLE "IoFunc"

using std::streambuf;

/*****************************************************************************/
int FileObj::_symid = -1;

FileObj::FileObj(const char* filename, const char* mode, int pipeflag) {
  _filename = strnew(filename);
  _mode = strnew(mode);
  _pipe = pipeflag;
  if (strcmp(filename,"-")!=0 ) { 
    _fptr = _pipe ? popen(filename, mode) : fopen(filename, mode);
  } else {
    _fptr = stdin;
  }
}

FileObj::FileObj(FILE* fptr) {
  _filename = NULL;
  _mode = NULL;
  _pipe = false;
  _fptr = fptr;
}

FileObj::~FileObj() { 
  if( _fptr && _fptr!=stdin && _filename) _pipe ? pclose(_fptr) : fclose(_fptr);
  delete _filename;
  delete _mode;
}

/*****************************************************************************/
int PipeObj::_symid = -1;

PipeObj::PipeObj(const char* command) {
  _command = strnew(command);
  _pid = popen2(command, &_wrfd, &_rdfd);
  _wrfptr = _rdfptr = NULL;
}

void PipeObj::close() {
  if(_wrfptr) fclose(_wrfptr);
  if(_rdfptr) fclose(_rdfptr);
  ::close(_wrfd);
  ::close(_rdfd);
  _wrfptr = NULL;
  _rdfptr = NULL;
  _wrfd = -1;
  _rdfd = -1;
}

PipeObj::~PipeObj() { 
  delete _command;
  close();
}

/*****************************************************************************/

PrintFunc::PrintFunc(ComTerp* comterp) : ComFunc(comterp) {
}

void PrintFunc::execute() {
  ComValue formatstr(stack_arg(0));
  static int str_symid = symbol_add("str");
  ComValue strflag(stack_key(str_symid));
  static int string_symid = symbol_add("string");
  ComValue stringflag(stack_key(string_symid));
  static int sym_symid = symbol_add("sym");
  ComValue symflag(stack_key(sym_symid));
  static int symbol_symid = symbol_add("symbol");
  ComValue symbolflag(stack_key(symbol_symid));
  static int err_symid = symbol_add("err");
  ComValue errflag(stack_key(err_symid));
  static int out_symid = symbol_add("out");
  ComValue outflag(stack_key(out_symid));
  static int file_symid = symbol_add("file");
  ComValue fileobjv(stack_key(file_symid));
  static int prefix_symid = symbol_add("prefix");
  ComValue prefixv(stack_key(prefix_symid));

  const char* fstr = formatstr.is_string() ? formatstr.string_ptr() : "nil";
  ComValue::comterp(comterp());

  streambuf* strmbuf = nil;
  if (stringflag.is_false() && strflag.is_false() &&
      symbolflag.is_false() && symflag.is_false()) {
    if (comterp()->handler() && fileobjv.is_unknown() && errflag.is_false() && outflag.is_false()) {
      FILEBUFP(fbuf, comterp()->handler() && comterp()->handler()->wrfptr() 
	       ? comterp()->handler()->wrfptr() : stdout, ios_base::out);
      strmbuf = fbuf;
    } else if (fileobjv.is_known()) {
      FileObj *fileobj = (FileObj*)fileobjv.geta(FileObj::class_symid());
      if (fileobj) {
        FILEBUFP(fbuf, fileobj->fptr(), ios_base::out);
	strmbuf = fbuf;
      } else {
        PipeObj *pipeobj = (PipeObj*)fileobjv.geta(PipeObj::class_symid());
        FILEBUFP(fbuf, pipeobj ? pipeobj->wrfptr() : stdout, ios_base::out);
	strmbuf = fbuf;
      }
    } else {
      FILEBUFP(fbuf, errflag.is_false() ? stdout : stderr, ios_base::out);
      strmbuf = fbuf;
    }
  } else
    strmbuf = new std::strstreambuf();
  ostream out(strmbuf);

  int narg = nargsfixed();
  if (narg==1) {

    if (formatstr.is_string() && !prefixv.is_string()) {
      out << formatstr.symbol_ptr();
      out.flush();
    }
    else {
      if (prefixv.is_string()) out << prefixv.symbol_ptr();
      out << formatstr;  // which could be arbitrary ComValue
      if (prefixv.is_string()) out << "\n";
    }

  } else {
    const char* fstrptr = fstr;
    int curr=1;
    while (curr<narg) {

      char fbuf[BUFSIZ];
      ComValue printval(stack_arg(curr));
      curr++;

      int i=0;
      if(curr<narg) {
        int flen;
        while(*fstrptr && !(flen=format_extent(fstrptr)) && i<BUFSIZ-1) fbuf[i++] = *fstrptr++;
        if(*fstrptr && flen+i<BUFSIZ-1) {
          strncpy(fbuf+i, fstrptr, flen);
          i += flen;
          fstrptr += flen;
        }
        while(*fstrptr && !format_extent(fstrptr) && i<BUFSIZ-1) fbuf[i++] = *fstrptr++;
        fbuf[i] = '\0';
      } else
        strncpy(fbuf, fstrptr, BUFSIZ);

      switch( printval.type() )
      {
      case ComValue::SymbolType:
      case ComValue::StringType:
	out_form(out, fbuf, symbol_pntr( printval.symbol_ref()));
	break;
	
      case ComValue::BooleanType:
	out_form(out, fbuf, printval.boolean_ref());
	break;
	
      case ComValue::CharType:
	out_form(out, fbuf, printval.char_ref());
	break;	    
	
      case ComValue::UCharType:
	out_form(out, fbuf, printval.uchar_ref());
	break;
	
      case ComValue::IntType:
	out_form(out, fbuf, printval.int_ref());
	break;
	
      case ComValue::UIntType:
	out_form(out, fbuf, printval.uint_ref());
	break;
	
      case ComValue::LongType:
	out_form(out, fbuf, printval.long_ref());
	break;
	
      case ComValue::ULongType:
	out_form(out, fbuf, printval.ulong_ref());
	break;
	
      case ComValue::FloatType:
	out_form(out, fbuf, printval.float_ref());
	break;
	
      case ComValue::DoubleType:
	out_form(out, fbuf, printval.double_ref());
	break;
	
      case ComValue::ArrayType: 
      {
	
        ALIterator i;
        AttributeValueList* avl = printval.array_val();
        avl->First(i);
        boolean first = true;
        while (!avl->Done(i)) {
          ComValue val(*avl->GetAttrVal(i));
          push_stack(formatstr);
          push_stack(val);
          exec(2,0);
          avl->Next(i);
          if (!avl->Done(i)) out << "\n";
        }
      }
      break;
      
      case ComValue::BlankType:
	out << "<blank>";
	break;
	
      case ComValue::UnknownType:
	out_form(out, fbuf, nil);
	break;
	
      case ComValue::ObjectType:
	out_form(out, fbuf, symbol_pntr(printval.class_symid()));
	break;
	
      default:
	break;
      }
    }
  }


  reset_stack();
  if (stringflag.is_true() || strflag.is_true()) {
    out << '\0';
    ComValue retval(((std::strstreambuf*)strmbuf)->str());
    push_stack(retval);
  } else if (symbolflag.is_true() || symflag.is_true()) {
    out << '\0';
    int symbol_id = symbol_add(((std::strstreambuf*)strmbuf)->str());
    ComValue retval(symbol_id, ComValue::SymbolType);
    push_stack(retval);
  } else {
    out.flush();
    push_stack(ComValue::blankval());
  }

  delete strmbuf;

}

int PrintFunc::format_extent(const char* fstr) {

  /* %[flags][width][.precision][length]specifier */
  int len=0;

  if (*fstr!='%')
    return 0;
  else
    len++;

  /* flags: minus, plus, space, pound, zero */
  while(fstr[len] == '-' || fstr[len] == '+' || fstr[len] == ' ' || fstr[len] == '#' || fstr[len] == '0')
    len++;

  /* width: 0-9 or star */
  if(fstr[len]=='*') 
    len++;
  else 
    while(fstr[len] >= '0' && fstr[len] <= '9')
      len++;

  /* .precision */
  if(fstr[len]=='.') {
    if(fstr[len]=='*') 
      len+=2;
    else {
      len++;
      if(fstr[len] < '0' || fstr[len] > '9')
        return 0;
      while(fstr[len] >= '0' && fstr[len] <= '9')
        len++;
    }
  }
  
  /* length: h, l, or L */
  while(fstr[len] == 'h' || fstr[len] == 'l' || fstr[len] == 'L')
    len++;

  /* specifier */
  if(fstr[len] == 'c' || fstr[len] == 'd' || fstr[len] == 'i' || fstr[len] == 'e' || fstr[len] == 'E' ||
     fstr[len] == 'f' || fstr[len] == 'g' || fstr[len] == 'G' || fstr[len] == 'o' || fstr[len] == 's' ||
     fstr[len] == 'u' || fstr[len] == 'x' || fstr[len] == 'X' || fstr[len] == 'p' || fstr[len] == 'n' )
    return len+1;
  else
    return 0;
  
}

/*****************************************************************************/

OpenFileFunc::OpenFileFunc(ComTerp* comterp) : ComFunc(comterp) {
}

void OpenFileFunc::execute() {
  ComValue filenamev(stack_arg(0));
  ComValue modev(stack_arg(1));
  static int pipe_symid = symbol_add("pipe");
  ComValue pipeflagv(stack_key(pipe_symid));
  static int in_symid = symbol_add("in");
  ComValue inflagv(stack_key(in_symid));
  static int out_symid = symbol_add("out");
  ComValue outflagv(stack_key(out_symid));
  static int err_symid = symbol_add("err");
  ComValue errflagv(stack_key(err_symid));
  reset_stack();
  
  if (inflagv.is_true()) {
      FileObj* fileobj = new FileObj(stdin);
      ComValue retval(FileObj::class_symid(), (void*)fileobj);
      push_stack(retval);
      return;
  }

  if (outflagv.is_true()) {
      FileObj* fileobj = new FileObj(stdout);
      ComValue retval(FileObj::class_symid(), (void*)fileobj);
      push_stack(retval);
      return;
  }

  if (errflagv.is_true()) {
      FileObj* fileobj = new FileObj(stderr);
      ComValue retval(FileObj::class_symid(), (void*)fileobj);
      push_stack(retval);
      return;
  }

  if (pipeflagv.is_true() && modev.is_string() && 
      (strcmp(modev.string_ptr(),"rw")==0 || strcmp(modev.string_ptr(),"wr")==0)) {
    PipeObj* pipeobj = new PipeObj(filenamev.string_ptr());
    ComValue retval(PipeObj::class_symid(), (void*)pipeobj);
    push_stack(retval);
#ifdef HAVE_ACE
    if (Component::use_unidraw()) {
      ComterpHandler* pipe_handler = new ComterpHandler(comterpserv());
      if (ComterpHandler::reactor_singleton()->register_handler(pipeobj->rdfd(), pipe_handler, 
                                                                ACE_Event_Handler::READ_MASK)==-1) {
        fprintf(stderr, "Trouble opening handler for pipeobj\n");
      exit(1);
      }
      pipe_handler->log_only(1);
    }
#endif
  } else {
    FileObj* fileobj = new FileObj(filenamev.string_ptr(), modev.is_string() ? modev.string_ptr() : "r", pipeflagv.is_true());
    if (fileobj->fptr())  {
      ComValue retval(FileObj::class_symid(), (void*)fileobj);
      push_stack(retval);
    } else {
      delete fileobj;
      push_stack(ComValue::nullval());
    }
  }
}

/*****************************************************************************/

CloseFileFunc::CloseFileFunc(ComTerp* comterp) : ComFunc(comterp) {
}

void CloseFileFunc::execute() {
  ComValue fileobjv(stack_arg(0));
  reset_stack();
  FileObj *fileobj = (FileObj*)fileobjv.geta(FileObj::class_symid());
  if (fileobj && fileobj->fptr())
    fclose(fileobj->fptr());
  else {
    PipeObj *pipeobj = (PipeObj*)fileobjv.geta(PipeObj::class_symid());
    pipeobj->close();
  }
}

/*****************************************************************************/

GetStringFunc::GetStringFunc(ComTerp* comterp) : ComFunc(comterp) {
}

void GetStringFunc::execute() {
  ComValue fileobjv(stack_arg(0));
  reset_stack();
  FileObj *fileobj = (FileObj*)fileobjv.geta(FileObj::class_symid());
  FILE* fptr = NULL;
  if (fileobj && fileobj->fptr()) {
    fptr = fileobj->fptr();
  } else {
    PipeObj *pipeobj = (PipeObj*)fileobjv.geta(PipeObj::class_symid());
    if (pipeobj && pipeobj->rdfptr()) 
      fptr = pipeobj->rdfptr();
  }
  char buffer[BUFSIZ];
  char* ptr = fgets(buffer, BUFSIZ, fptr);
  if (ptr)  {
    ComValue retval(buffer);
    push_stack(retval);
  } else
    push_stack(ComValue::nullval());
}

/*****************************************************************************/

GetArgFunc::GetArgFunc(ComTerp* comterp) : ComFunc(comterp) {
}

void GetArgFunc::execute() {
  ComValue numv(stack_arg(0));
  reset_stack();
  int arg_sym = comterp()->arg_str(numv.int_val());
  if (arg_sym>0) {
    ComValue retval(comterp()->arg_str(numv.int_val()), ComValue::StringType);
    push_stack(retval);
  } else
    push_stack(ComValue::nullval());
  return;
}

/*****************************************************************************/

NumArgFunc::NumArgFunc(ComTerp* comterp) : ComFunc(comterp) {
}

void NumArgFunc::execute() {
  reset_stack();
  ComValue retval(comterp()->narg_str(), ComValue::IntType);
  push_stack(retval);
  return;
}