summaryrefslogtreecommitdiffstats
path: root/cmdline.cpp
blob: 4dbbe60943c2c94bc52efc3729c706df1979cd2a (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
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
/*  Time Based Text - Commandline Player and Recorder
 *
 *  (C) Copyright 2006 - 2008 Denis Roio <[email protected]>
 *
 * This source code is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Public License as published 
 * by the Free Software Foundation; either version 2 of the License,
 * or (at your option) any later version.
 *
 * This source code 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.
 * Please refer to the GNU Public License for more details.
 *
 * You should have received a copy of the GNU Public License along with
 * this source code; if not, write to:
 * Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 */


//// system includes
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include <inttypes.h>

#include <getopt.h>

#include <sys/types.h>
#include <sys/param.h>
#include <sys/time.h>

#include <assert.h>
#include <signal.h>

//////////////////////


// Time Based Text
#include <tbt.h>



// S-Lang widgets
#include <slw_console.h>
#include <slw_text.h>


// my lovely utils
#include <jutils.h>



// Time Based Text global object
TBT tbt;



///////////////// commandline stuff

void set_status(SLangWidget *s);

static const char *help =
"Synopsis: tbt -(r|p) [options] [file]\n"
"Commands:\n"
"  -r   record\n"
"  -p   playback\n"
"Options:\n"
"  -h   print this help\n"
"  -v   version information\n"
"  -D   debug verbosity level - default 1\n"
"  -c   console interface mode (S-Lang)\n"
"  -s   save format in [ bin | ascii | html ]\n"
#ifdef linux
"  -t   timing mode    [ posix | rtc ]\n"
#endif
"  -x   convert tbt to [ bin | html | ascii]\n";

static const char *short_options = "-hvD:crps:t:x:";

int debug;
char filename[512];


// act as commandline tool by default
bool console = false;

// timing modes
#define POSIX 1
#define RTC   2
int timing = POSIX;

// operation modes
#define REC        1
#define PLAY       2

#define CONV       4
int operation = REC;


// rendering formats
#define BIN        1
#define ASCII      2
#define HTML       3
int render = BIN;


uint64_t key;

int c, len;


void cmdline(int argc, char **argv) {
  int res;

  debug = 1;
  filename[0] = 0x0;

  fprintf(stderr, "TBT - Time Based Text - %s\n", VERSION);

  /// adjust the operation to the way the binary is called
  if ( ! strstr(argv[0], "tbt") )
    console = true; // use console by default when binary is aliased
  if      ( strstr(argv[0],"rectext") )
    operation = REC;
  else if ( strstr(argv[0],"playtext") )
    operation = PLAY;


  do {
    res = getopt(argc, argv, short_options);
    switch(res) {
    case 'h':
      fprintf(stderr, "%s", help);
      exit(0);
      break;

    case 'v':
      fprintf(stderr,"\n");
      exit(0);
      break;

    case 'D':
      debug = atoi(optarg);
      if(debug>3) {
	warning("debug verbosity ranges from 1 to 3\n");
	debug = 3;
      }
      break;

    case 'c':
      console = true;
      break;

    case 'r':
      operation = REC;
      break;

    case 'p':
      operation = PLAY;
      break;

    case 't':
      if(strncasecmp(optarg, "rtc", 3) ==0)
	timing = RTC;
      else
	timing = POSIX;
      break;

    case 's':
      if( strcasecmp(optarg, "BIN") ==0)
	      render = BIN;
      else if( strcasecmp(optarg, "ASCII") ==0)
	      render = ASCII;
      else if( strcasecmp(optarg, "HTML") ==0)
	      render = HTML;
      else {
	      error ("render format not recognized: %s", optarg);
	      act ("using default binary format render");
	      render = BIN;
      }
      break;

    case 'x':
      operation = CONV;
      if( strcasecmp(optarg, "BIN") ==0)
	render = BIN;
      else if( strcasecmp(optarg, "ASCII") ==0)
	render = ASCII;
      else if( strcasecmp(optarg, "HTML") ==0)
	render = HTML;
      else {
	error ("render format not recognized: %s", optarg);
	act ("using default binary format render");
	render = BIN;
      }
      break;
      
    case 1:  snprintf(filename,511,"%s", optarg);
      
    default: break;
      
    }
  } while (res != -1);

  for(;optind<argc;optind++)
    snprintf(filename, 511, "%s", argv[optind]);

  if(!filename[0])
    sprintf(filename, "record.tbt");

  set_debug(debug);

}

///////////// end of commandline stuff




//// console based operations using S-Lang interface

// S-Lang console
SLangConsole *con;

// S-Lang widgets
SLW_Text     *txt;
SLW_Text     *status;

int record_console() {

  // initialize the text console
  if(! con->init() ) return(-1);
  
  //  initialize the status line
  status->border = false;
  status->set_name("status box");
  if(! con->place(status, 0, con->h-10, con->w, con->h) ) {
    error("error placing the status widget");
    return(-1);
  }
  assert ( status->init() );
  
  //  set the status widget *** only after placing it! ***
  set_status(status);
  
  // initialize the text canvas
  txt->set_name("editor");
  // txt->border = true;
  if(! con->place(txt, 0, 0, con->w, con->h-6) ) { //  con->w, con->h-20) ) {
    error("error placing the text widget");
    return(-1);
  }
  assert ( txt->init() );
  
  // focus the text canvas
  con->focused = txt;
  
  
  // write out to the status widget
  notice("TBT - console ready");
  
  while(!tbt.quit) {
    
    key = con->getkey();
    
    if(key) {
      
      // save the key and timestamp
      tbt.append(key);
      
      // display the key
      con->feed(key);
      
    }
    
    if( ! con->refresh() ) tbt.quit = true;
    
    jsleep(0,10);
    
  }

  return 1;
}


int play_console() {
  

  //  initialize the status line
  status->border = false;
  status->set_name("status box");
  if(! con->place(status, 0, con->h-10, con->w, con->h) ) {
    error("error placing the status widget");
    return(-1);
  }
  assert ( status->init() );
  
  //  set the status widget *** only after placing it! ***
  set_status(status);
 
  // place the text canvas
  if(! con->place(txt, 0, 0, con->w, con->h-1) ) {
	  error("error placing the text widget");
	  return(-1);
  }
  txt->set_name("player");
  assert ( txt->init() );
  // focus the text console
  con->focused = txt;



  // main playback loop
  for(c=0; c<len && !tbt.quit; c++) {
    
    // tbt.getkey is a blocking call
    // will wait N time before returning
    key = tbt.getkey();

    con->feed(key);
    if( ! con->refresh() ) tbt.quit = true;

  }
  
  return 1;
}

//////////////////////////////////// end of console based operations


/* signal handling */
void quitproc (int Sig) {
  act("interrupt caught, exiting.");
  tbt.quit = true;
}



int main(int argc, char** argv)
{

  cmdline(argc, argv);
  
  if(timing == RTC)
    tbt.rtc = true;

  // start the TBT engine
  if(! tbt.init() )  exit(0);

  /* register signal traps */
  if (signal (SIGINT, quitproc) == SIG_ERR) {
    perror ("Couldn't install SIGINT handler"); exit(0); }

  // if playing, load the recording
  if(operation == PLAY) {
    if( ! tbt.load( filename ) ) {
      error("no entries found in file %s",filename);
      exit(1);
    } else
      len = tbt.buffer->len();
  }

  if(console) { // initialize the s-lang console
    
    con = new SLangConsole();
    
    // initialize the text console
    if(! con->init() ) {
      error("cannot initialize S-Lang console on this terminal.");
      exit(0);
    }
    
    txt    = new SLW_Text();
    status = new SLW_Text();
    
    switch(operation) {
      
    case REC:
      record_console();
      
      switch(render) {
	
      case BIN:
	tbt.save_bin( filename );
	act("TBT file %s rendered in binary format",filename);
	break;
	
      case ASCII:
	tbt.save_ascii( filename );
	act("TBT file %s rendered in ascii format",filename);
	break;
	
      case HTML:
	tbt.save_html( filename );
	act("TBT file %s rendered in html format",filename);
	break;
	
      }
      
      break;
      
    case PLAY:
      play_console();
      break;
      
    }
    
    // cleanly close the console
    con->refresh();
    jsleep(1,0);
    con->close();


  } else {
    ////////////// commandline operations
    
    switch(operation){
      
    case REC:
      // append from 0 (stdin) each 1 byte
      tbt.fdappend(0, 1);
      
      switch(render) {
	
      case BIN:
	tbt.save_bin( filename );
	act("TBT file %s rendered in binary format",filename);
	break;
	
      case ASCII:
	tbt.save_ascii( filename );
	act("TBT file %s rendered in ascii format",filename);
	break;
	
      case HTML:
	tbt.save_html( filename );
	act("TBT file %s rendered in html format",filename);
	break;
	
      }
      
      break;
      
    case PLAY:
      
      // main playback loop
      for(c=0; c<len && !tbt.quit; c++) {
	// tbt.getkey is a blocking call
	// will wait N time before returning
	key = tbt.getkey();
	
	// print out on stdout
	write(1, (void*)&key, 1);
      }
      break;




    case CONV:
      // convert to other formats
      tbt.load(filename);
      
      switch(render) {
	
      case ASCII:
	{
	  char tmp[512];
	  snprintf(tmp,511,"%s.asc",filename);	
	  tbt.save_ascii( tmp );
	  act("TBT file %s rendered in ascii format",tmp);
	}
	break;
	
      case HTML:
	{
	  char tmp[512];
	  snprintf(tmp,511,"%s.html",filename);	
	  tbt.save_html( tmp );
	  act("TBT file %s rendered in html format",filename);
	}
	break;
      case BIN:
	  char tmp[512];
	  snprintf(tmp,511,"%s.tbt",filename);	
	  tbt.save_bin( tmp );
	  act("TBT file %s rendered in binary format",filename);
      }
    }
  }    
  
  notice("Closing Time Based Recorder");
  act("%u entries streamed.", tbt.buffer->len());

  exit(0);  
}