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
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
|
/*
============LICENSE_START==========================================
===================================================================
Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
===================================================================
Unless otherwise specified, all software contained herein is licensed
under the Apache License, Version 2.0 (the License);
you may not use this software except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
============LICENSE_END============================================ */
//.. subnproc.js -to be started by parent server process
const fs = require('fs');
const Chproc = require('child_process');
var clName= "subnproc";
var thePid= 0;
var taskId= '';
//.. assuming all these files are in CDT_HOME
var inpFilePfx= "posted_data_";
var outFilePfx= "sync_template_res_"; //.. processing results
var errFilePfx= "sync_template_err_"; //.. error found while processing
var parmFilePfx= "template_params_";
var pstatfNamePfx= "proc_status_";
//.. pDlm -delimiter of params
// (cant be: alphanumeric,underbar,dollar,dot,comma,brackets,dash,equal sign,etc)
var pDlm= '\n';
var logfname= clName+".log";
var logfname_o= logfname+".save"; //.. to keep old content
var fdL;
var fdIn;
var fdOut;
var errMsgBadParm= ''; //.. create error message to report to client
//var checkSpecialCharsReg= new RegExp(/[^\w\s-]/);
var checkSpecialCharsReg= new RegExp(/[^\w\s-\.]/);
var doStopOnSpecChars= 'Y';
var doCheckComments= 'Y';
//var SYNC_T_KEY_EXPRES= '\\${.+?\\}';
var VSTART= '${';
var VEND= '}';
var lenVS= VSTART.length, lenVE= VEND.length;
var KEY_START= '${(';
var KEY_MID= ')=(';
var KEY_END= ')}';
var LD= "\n"; //.. LD -line delimeter in the input file
var lenLD= LD.length;
var percAtRngs= 55; //.. percentage defining report to client
var traceLvl= 1;
//var CCnt= 52;
var CCnt= 2;
var taskIdArgstr= process.argv[ 2 ];
var CDT_HOME= process.argv[ 3 ];
var LOG_DIR= process.argv[ 4 ];
this.thePid= process.pid;
console.log(clName+": Start: thePid="+this.thePid );
console.log(clName+": CDT_HOME:["+CDT_HOME+"]" );
console.log(clName+": LOG_DIR:["+LOG_DIR+"]" );
try {
//.. need renameSync (not asynchronous) !
fs.renameSync( LOG_DIR+"/"+logfname, LOG_DIR+"/"+logfname_o);
}
catch( err ) {
console.log(clName+": log rename error: code="+ err.code );
console.log(clName+": log rename error.msg:["+err.message+"]");
//throw err;
};
fs.open( LOG_DIR+"/"+logfname, 'a', (err, fd) => {
if( err ) {
console.log(clName+": log file open error:"+err.message );
throw err;
}
this.fdL= fd;
console.log(clName+": log opened. fdL="+this.fdL);
this.logWr( clName+": Start: thePid="+this.thePid+"\n");
this.logWr( clName+
": execArgv:["+process.execArgv+"]\n argv:["+process.argv+"]\n");
var indT= taskIdArgstr.indexOf('=', 0);
if( indT < 0 ) {
// this.logWr(clName+": Error: Failed to extract taskId from args:["+
// taskIdArgstr+"]\n");
// this.taskId= "000";
this.taskId= taskIdArgstr;
} else {
this.taskId= taskIdArgstr.substr( indT+1 );
}
this.logWr(clName+": taskId:["+this.taskId+"]\n");
if( this.taskId == null || this.taskId == undefined || this.taskId.length < 1 )
{
var errMsg= clName+": Error: taskId is empty !\n";
console.log( errMsg );
this.logWr( errMsg );
throw new Error( errMsg );
};
this.logWr(clName+": CDT_HOME:["+CDT_HOME+"]\n");
var timeStampI = Date.now();
this.logWr(clName+": timeStampI="+timeStampI+" \n");
var timeStamp = new Date( timeStampI ).toISOString();
this.logWr(clName+": timeStamp:["+timeStamp+"]\n");
//.. setup callback waiting for message from parent
// process.on('message', (msg) => {
// console.log('subnp: got message:', msg);
// });
//this.logWr("subnp: sending msg to parent.\n");
// var msg2parent = { taskid: taskId };
// this.logWr("subnp: msg2parent:["+JSON.stringify(msg2parent)+"]\n");
// process.send( msg2parent );
var inpFile= CDT_HOME+"/"+ inpFilePfx +this.taskId +".txt";
this.logWr(clName+": opening inpFile for reading:["+inpFile+"]\n");
fs.open( inpFile, 'r', (err, fd0) => {
if( err ) {
console.log(clName+": inpFile file open: Error: code="+err.code );
this.logWr(clName+": inpFile file open: Error: code="+err.code+"\n");
throw err;
}
this.fdIn= fd0;
console.log(clName+": Input file opened. fdIn="+this.fdIn);
fs.readFile( this.fdIn, (err, data) => {
if( err ) {
console.log(clName+": Failed to read inpFile: Error: code="+err.code );
this.logWr(clName+": Failed to read inpFile: Error: code="+err.code+"\n");
throw err;
}
this.processData( data );
//.. close log
console.log(clName+": close: fdL="+this.fdL );
fs.closeSync( this.fdL );
this.fdL= -1;
});
});
});
exports.processData = function ( data ) {
//.. processing data content from already opened input file
var methName= "processData";
this.logWr(methName+": start...\n");
var timeStamp = new Date().toISOString();
this.logWr( methName+": timeStamp:["+timeStamp+"]\n");
var fcontStr= new String( data );
let fcontLen= fcontStr.length;
this.logWr(methName+": fcontLen="+fcontLen+"\n");
this.logWr(methName+": trying to close inpFile: fdIn="+this.fdIn+"\n");
fs.closeSync( this.fdIn );
this.logWr(methName+": inpFile closed.\n");
var pstatfName= CDT_HOME+"/"+ pstatfNamePfx +this.taskId+".json";
var percent= 1;
var stmsg= '{\"percentage\":'+percent+'}';
this.updateProcStatus( pstatfName, stmsg );
timeStamp = new Date().toISOString();
this.logWr( methName+": timeStamp:["+timeStamp+"]\n");
var perrorfName= CDT_HOME+"/"+ errFilePfx+ this.taskId+".txt";
var formatErr= '';
var lineEnds= new Array( 0 ); //.. indexes of new-line chars
var selecRanges= new Array( 0 ); //.. indexes of ${variables}
var parmNamVals= new Map(); //.. parameter Name-Value pairs from inside ${...}
var ind0= 0;
var ind1= fcontStr.indexOf( LD,ind0 ); //.. line delimeter
var rn= 0, li= 0;
while( ind1 > -1 ) {
lineEnds.push( ind1 );
this.logWr(
methName+":--line #:"+li+" beg: ind0="+ind0+" end: ind1="+ind1+
" array:"+lineEnds[li]+"\n");
var line0= fcontStr.substring( ind0, ind1 ); //.. not including LD
var liLen= line0.length;
this.logWr( methName+": liLen="+liLen+"\n");
if( liLen > 60 ) {
this.logWr( methName+": line(p):["+line0.substr(0,60)+"]\n");
} else {
this.logWr( methName+": line:["+line0+"]\n");
}
//.. look for ${param variables}
rn= this.getRangesForVars( line0, li, rn, selecRanges );
if( rn < 0 ) {
this.logWr( methName+": got Error:\n["+errMsgBadParm+"]");
this.reportParamBadFmt( li, '', errMsgBadParm, perrorfName, pstatfName );
this.logWr( methName+": Stopping processing !\n");
return;
}
if( traceLvl > 1 )
this.logWr( methName+": added ranges at li="+li+": next rn="+rn+"\n");
ind0= ind1 +lenLD; //.. for next search of LD
if( ind0 >= fcontLen ) { break; };
li++;
ind1= fcontStr.indexOf( LD,ind0 ); //.. line delimeter
}; //.. loop
//.. process the last line
if( ind0 < fcontLen ) {
this.logWr( methName+": have the last line: ind0="+ind0+"\n");
lineEnds.push( fcontLen );
var line0= fcontStr.substring( ind0 ); //.. up to the end
var liLen= line0.length;
this.logWr( methName+": liLen="+liLen+"\n");
if( liLen > 60 ) {
this.logWr( methName+": line(p):["+line0.substr(0,60)+"]\n");
} else {
this.logWr( methName+": line:["+line0+"]\n");
}
//.. look for ${param variables}
rn= this.getRangesForVars( line0, li, rn, selecRanges );
if( rn < 0 ) {
this.logWr( methName+": got Error in getRangesForVars:\n["+
errMsgBadParm+"]");
this.reportParamBadFmt( li, '', errMsgBadParm, perrorfName, pstatfName );
this.logWr( methName+": Stopping processing !\n");
return;
}
};
this.logWr( methName+": -=- lineEnds array length="+lineEnds.length+"\n");
timeStamp = new Date().toISOString();
this.logWr( methName+": timeStamp:["+timeStamp+"]\n");
percent= percAtRngs;
stmsg= '{\"percentage\":'+percent+'}';
this.updateProcStatus( pstatfName, stmsg );
//.. Cycle through Ranges
this.logWr( methName+": selecRanges array length="+selecRanges.length+"\n");
if( doCheckComments ) {
this.logWr( methName+": doCheckComments is True.\n");
} else {
this.logWr( methName+": doCheckComments is False\n");
};
var rnAtUpd= Math.floor( selecRanges.length/ 3.);
var percStep= (100.0 - percAtRngs);
this.logWr( methName+": range number for proc.status update="+rnAtUpd+
"\n percentage step="+percStep );
var outLinBuf= new Array( 0 ); //.. array of output lines
var outLnLast=-1; //.. last processed
var rngLnLast=-1;
for( var rn0=0; rn0 < selecRanges.length; rn0++ ) {
var rng= selecRanges[rn0];
this.logWr( methName+":--- range #:"+rn0+" lineNm="+rng.lineNm+
" colBg="+rng.colBg+" colEn="+rng.colEn+"\n");
this.logWr( methName+": outLnLast="+outLnLast+"\n");
if( rn0 > 0 && (rn0 % rnAtUpd) == 0 && rn0 < selecRanges.length -rnAtUpd )
{
percent= Math.floor( percAtRngs + percStep *(rn0/selecRanges.length) );
this.logWr( methName+": need to update proc.status: percent="+percent+"\n");
stmsg= '{\"percentage\":'+percent+'}';
this.updateProcStatus( pstatfName, stmsg );
};
rngLnLast= rng.lineNm;
//.. prepare previous lines with no ranges
if( rng.lineNm - outLnLast > 1 ) {
this.logWr( methName+": prepare previous lines...\n");
var plinNm0= outLnLast +1;
var plinesIndB= 0;
if( plinNm0 > 0 ) { plinesIndB= lineEnds[outLnLast] + lenLD; };
var plinesIndE= lineEnds[rng.lineNm -1] + lenLD;
this.logWr( methName+": plinesIndB="+plinesIndB+
" plinesIndE="+plinesIndE+"\n");
var plinStr= fcontStr.substring( plinesIndB, plinesIndE );
if( traceLvl > 1 ) {
if( plinStr.length > 60 ) {
this.logWr( methName+": plinStr(p):["+plinStr.substr(0, 60)+"]\n");
} else {
this.logWr( methName+": plinStr:["+plinStr+"]\n");
}
};
outLinBuf.push( plinStr );
outLnLast= rng.lineNm -1;
};
var linIndB= 0;
var linIndE= lineEnds[rng.lineNm];
if( rng.lineNm > 0 ) { linIndB= lineEnds[rng.lineNm - 1] + lenLD; };
var linLen= linIndE - linIndB;
var line0= fcontStr.substr( linIndB, linLen );
this.logWr( methName+": linIndB="+linIndB+" linIndE="+linIndE+
" line length="+linLen+"\n");
if( traceLvl > 1 ) {
if( linLen > 60 ) {
this.logWr( methName+": line0(p):\n["+line0.substr(0,60)+"]\n");
} else {
this.logWr( methName+": line0:\n["+line0+"]\n");
}
};
//.. content between ${ and }
var vword= line0.substring( rng.colBg+lenVS, rng.colEn+1-lenVE );
this.logWr( methName+": vword:["+vword+"]\n");
//.. try to check for (pvalue)=(pname)
let specialKeys= null;
var parmNamAndVal= Array( 0 );
var pName= null;
var pValue= null;
formatErr= this.parseParmValAndName( vword, parmNamAndVal )
if( formatErr ) {
this.reportParamBadFmt(
rng.lineNm, vword, formatErr, perrorfName, pstatfName );
this.logWr( methName+": Stopping processing !\n");
return;
}
//.. No errors - parsing done OK
pName= parmNamAndVal[0]; //.. to be added to parmNamVals
pValue= parmNamAndVal[1];
this.logWr( methName+": parsing done: param.name:["+
pName+"] value:["+pValue+"]\n");
if( pName != null && pName.length > 0 ) {
this.logWr( methName+": check spec.chars in Non empty pName.\n");
specialKeys= checkSpecialCharsReg.exec( pName );
if( specialKeys == null ) {
this.logWr( methName+": specialKeys obj is null\n");
}
else
if( specialKeys.length > 0 ) {
this.logWr( methName+": specialKeys obj Not null:["+specialKeys+
"] length="+specialKeys.length+"\n");
if( doStopOnSpecChars ) {
formatErr=
"The parameter name("+pName+") contains prohibited character(s):("+
specialKeys+") !";
this.logWr( methName+": formatErr:["+formatErr+"]\n");
this.reportParamBadFmt(
rng.lineNm, vword, formatErr, perrorfName, pstatfName );
this.logWr( methName+": Stopping processing !\n");
return;
};
};
};
var haveComment= '';
if( doCheckComments ) {
this.logWr( methName+": doCheckComments is True.\n");
if( line0.trim().startsWith("//") ) haveComment= 'Y';
};
if( haveComment ) {
this.logWr( methName+": haveComment is True !\n");
};
//.. prepare parameter name and value
if( pName != null && pName.length > 0 && !specialKeys && !haveComment )
{
this.logWr( methName+": checking non-empty param.name:["+pName+"]\n");
if( parmNamVals.has(pName) ) {
this.logWr( methName+": the param. is already accounted.\n");
} else {
this.logWr( methName+": adding new param. with value:["+pValue+"]\n");
parmNamVals.set( pName, pValue );
};
};
//.. prepare for template output
var colB= 0;
if( rn0 > 0 ) {
var rngP= selecRanges[rn0 - 1];
if( rngP.lineNm == rng.lineNm ) { colB= rngP.colEn+lenVE; };
};
var colE= rng.colEn;
//.. check range content for replacement
let checkApplForNamOn=
this.checkAppliedForNamesOnly( line0, rng.colBg, rng.colEn );
this.logWr( methName+": checkApplForNamOn:["+checkApplForNamOn+"]\n");
if( checkApplForNamOn && !specialKeys && !haveComment )
{
this.logWr( methName+": Need Name Replacement... \n");
var replacWrd= KEY_START +KEY_MID +vword +KEY_END; // ${()=(...)}
this.logWr( methName+": replacWrd:["+replacWrd+"]\n");
var colM= rng.colBg; //.. not including beginning of VSTART
var brngStr= line0.substring( colB, colM );
this.logWr( methName+": part before word:["+brngStr+"]\n");
outLinBuf.push( brngStr + replacWrd );
}
else {
this.logWr( methName+": No Replacement for this range !\n");
var rsubs= line0.substring( colB, colE+lenVE );
this.logWr( methName+": rsubs:["+rsubs+"]\n");
outLinBuf.push( rsubs );
};
outLnLast= rng.lineNm;
//.. check tail of the line
if( rn0 == selecRanges.length - 1 ) {
var tailStr= line0.substring(colE+lenVE) + LD;
this.logWr( methName+": last range: adding line tail:\n["+tailStr+"]\n");
outLinBuf.push( tailStr );
}
else { //.. not the last range
var rngN= selecRanges[rn0 + 1];
if( rngN.lineNm == rng.lineNm ) {
//.. next range on the same line - no tail yet
}
else { //.. next range on different line
var tailStr= line0.substring(colE+lenVE) + LD;
this.logWr( methName+": adding line tail:\n["+tailStr+"]\n");
outLinBuf.push( tailStr );
};
};
}; //... loop
if( rngLnLast < lineEnds.length - 1) {
this.logWr( methName+": adding last lines having no ranges...\n");
var llinesIndB= 0;
if( rngLnLast > 0 ) { llinesIndB= lineEnds[rngLnLast] + lenLD; };
var llinesIndE= lineEnds[lineEnds.length -1] + lenLD;
this.logWr( methName+": llinesIndB="+llinesIndB+
" llinesIndE="+llinesIndE+"\n");
var llinStr= fcontStr.substring( llinesIndB, llinesIndE );
if( traceLvl > 1 ) {
if( llinStr.length > 60 ) {
this.logWr( methName+": llinStr(p):\n["+llinStr.substr(0,60)+"]\n");
} else {
this.logWr( methName+": llinStr:["+llinStr+"]\n");
}
}
outLinBuf.push( llinStr );
};
this.logWr( methName+": -=- outLinBuf: array length="+outLinBuf.length+"\n");
timeStamp = new Date().toISOString();
this.logWr( methName+": timeStamp:["+timeStamp+"]\n");
var outFile= CDT_HOME+"/"+ outFilePfx+ this.taskId+".txt";
var outFile_o= outFile+".save";
try {
fs.renameSync( outFile, outFile_o );
}
catch( err ) {
this.logWr(clName+": old output file rename: error:["+err.message+"]\n");
// throw err;
};
try {
this.fdOut= fs.openSync( outFile, 'a' );
}
catch( err ) {
this.logWr(clName+": output file open: error:["+err.message+"]\n");
throw err;
}
this.logWr(methName+": output file opened: fdOut="+this.fdOut+"\n");
for( let outLine of outLinBuf ) {
try {
fs.appendFileSync( this.fdOut, outLine, 'utf8' );
}
catch( err ) {
this.logWr(clName+": output file append: error: code="+err.code );
throw err;
}
}; //... loop
this.logWr(methName+": closing output file: fdOut="+this.fdOut+"\n");
fs.closeSync( this.fdOut );
this.logWr(methName+": output file closed.\n");
timeStamp = new Date().toISOString();
this.logWr( methName+": timeStamp:["+timeStamp+"]\n");
this.logWr( methName+": -=- parmNamVals (Parameters): map.size="+
parmNamVals.size+"\n");
var parmFile= CDT_HOME+"/"+ parmFilePfx+ this.taskId+".txt";
var parmFile_o= parmFile+".save";
try {
fs.renameSync( parmFile, parmFile_o );
}
catch( err ) {
this.logWr(clName+": old Params file rename: error:["+err.message+"]\n");
// throw err;
};
try {
this.fdParm= fs.openSync( parmFile, 'a' );
}
catch( err ) {
this.logWr(clName+": Params file open: error:["+err.message+"]\n");
throw err;
};
this.logWr(methName+": Params file opened: fdParm="+this.fdParm+"\n");
//.. writing parameters into parameter-output file
try {
// fs.appendFileSync( this.fdParm, "[\n", 'utf8' );
var iw= 0;
for( let pnm of parmNamVals.keys() ) {
let pv= parmNamVals.get( pnm );
let pnv= pnm+"="+pv;
this.logWr( methName+": iw="+iw+" param.Name-Value:["+pnv+"]\n");
//.. need delimiter after each item !
fs.appendFileSync( this.fdParm, pnv+pDlm, 'utf8' );
iw++;
}; //... loop
// fs.appendFileSync( this.fdParm, "]", 'utf8' );
}
catch( err ) {
this.logWr(clName+": Params file append: error: code="+err.code );
throw err;
}
this.logWr(methName+": closing Params file: fdParm="+this.fdParm+"\n");
fs.closeSync( this.fdParm );
this.logWr(methName+": Params file closed.\n");
timeStamp = new Date().toISOString();
this.logWr( methName+": timeStamp:["+timeStamp+"]\n");
percent= 100;
stmsg= '{\"percentage\":'+percent+'}';
this.updateProcStatus( pstatfName, stmsg );
//.. simulated updates
/*
for( var i1=0; i1 < this.CCnt; i1++ ) {
this.wait( 2 );
percent += 2;
if( percent > 100 ) { percent= 100; };
stmsg= '{\"percentage\":'+percent+'}';
this.updateProcStatus( pstatfName, stmsg );
};
this.wait( 1 );
*/
this.logWr(methName+": finishing.\n");
timeStamp = new Date().toISOString();
this.logWr( methName+": timeStamp:["+timeStamp+"]\n");
};
exports.checkAppliedForNamesOnly = function( line, colBg, colEn )
{
var methName= "checkAppliedForNamesOnly";
this.logWr( methName+": start: colBg="+colBg+" colEn="+colEn+"\n");
if( traceLvl > 1 ) {
if( line.length > 60 ) {
this.logWr( methName+": line(p):\n["+line.substr(0,60)+"]\n");
} else {
this.logWr( methName+": line:\n["+line+"]\n");
}
};
this.logWr( methName+": check for: KEY_START:["+KEY_START+"]\n");
var lenKS= KEY_START.length;
var indMax= colBg + lenKS
var subline= line.substring( 0, indMax );
var indKS= subline.lastIndexOf( KEY_START, subline.length ); //.. ${(
if( indKS < 0 ) {
if( traceLvl > 1 )
this.logWr( methName+": No KEY_START at all - return true.\n");
return true; //.. no KEY_START at all
}
else { //.. have KEY_START, check KEY_END
this.logWr( methName+": check for: KEY_END:["+KEY_END+"]\n");
indMax= colBg + KEY_END.length;
subline= line.substring( indKS +lenKS, indMax );
var indKE= subline.indexOf( KEY_END, 0 ); //.. )}
if( indKE < 0 ) {
if( traceLvl > 1 )
this.logWr( methName+": Only KEY_START there - return false.\n");
return false; //..only KEY_START there
}
else {
if( traceLvl > 1 )
this.logWr( methName+": Have both - return true.\n");
return true; //.. have both: KEY_START and then KEY_END
}
}
}
exports.getRangesForVars = function( line, lnum, rnum, selRangesArr )
{
var methName= "getRangesForVars";
this.logWr( methName+": start: lnum="+lnum+" rnum="+rnum+"\n");
if( traceLvl > 0 ) {
if( line.length > 60 ) {
this.logWr( methName+": line(p):["+line.substr(0,60)+"]\n");
} else {
this.logWr( methName+": line:["+line+"]\n");
}
};
var haveComment= '';
if( line.trim().startsWith("//") ) haveComment= 'Y';
//.. look for ${variables}
var indV0= 0;
var indV1= line.indexOf( VSTART, indV0 );
while( indV1 > -1 ) {
var indB= indV1;
indV0= indB + 1;
indV1= line.indexOf( VEND, indV0 );
if( indV1 < 0 ) {
errMsgBadParm=
methName+": Parameter ending delimiter ("+VEND+
") not found in line #="+lnum+" after pos.#="+indV0+" !\n"
this.logWr( "Error: "+errMsgBadParm);
if( ! haveComment ) {
return -1;
} else { //.. haveComment - do not stop processing
this.logWr( methName+": This line is commented out -ignore the error.");
break;
};
};
var indE= indV1 + lenVE-1;
//.. check for VSTART inside the range (must find either old or new one)
indB= line.lastIndexOf( VSTART, indE );
this.logWr( methName+": found a ${param} at:"+indB+" - "+indE+"\n");
//.. lineNm - line number in the input file
//.. colBg - begin index of VSTART
//.. colEn - after end index of VEND (both within the line)
var range = { lineNm: lnum, colBg: indB, colEn: indE };
selRangesArr.push( range );
this.logWr( methName+": range #:"+rnum+
" param.string:["+JSON.stringify(selRangesArr[rnum])+"]\n");
rnum++;
indV0= indV1 + lenVE;
indV1= line.indexOf( VSTART, indV0 );
};
return rnum;
};
//.. assuming the word format is like: "(value)=(name)" or "name"
// returning error message or empty string (which means no errors)
exports.parseParmValAndName = function( word, parmNamAndVal )
{
var methName= "parseParmValAndName";
this.logWr( methName+": start: input word:["+word+"]\n");
var lenW= word.length;
var errorMsg= '';
if( traceLvl > 0 )
this.logWr( methName+": word length="+lenW+"\n");
if( lenW < 1 ) {
errorMsg= methName+": the parameter string is empty !\n";
errMsgBadParm= errorMsg;
this.logWr( "Error: "+errorMsg );
return errorMsg;
};
var lastCh= lenW - 1;
var OBR= '(';
var lenOBR= OBR.length;
var CBR= ')';
var lenCBR= CBR.length;
var EQS= '=';
var lenEQS= EQS.length;
var name, val;
var ic0= 0;
var ic1= word.indexOf( OBR, ic0 );
if( ic1 < 0 ) {
if( traceLvl > 0 )
this.logWr( methName+": no open round brackets -assuming the word has "+
"param.name only.\n");
parmNamAndVal.push( word );
parmNamAndVal.push( '' );
return ''; //.. empty errorMsg means OK
};
//.. got 1-st open round bracket
if( traceLvl > 0 )
this.logWr( methName+": found 1-st open round bracket at:"+ic1+"\n");
if( ic1 > 0 ) {
errorMsg= methName+": Unexpected chars before open round bracket at:"+ic1+"\n";
errMsgBadParm= errorMsg;
this.logWr( "Error: "+errorMsg );
return errorMsg;
};
ic0= ic1 + lenOBR;
if( ic0 >= lastCh ) {
errorMsg=
methName+": missing next expected round brackets after pos.#="+ic1+
" -bad format\n";
errMsgBadParm= errorMsg;
this.logWr( "Error: "+errorMsg );
return errorMsg;
};
var ic2= word.indexOf( CBR, ic0 );
if( ic2 < 0 ) {
errorMsg=
methName+": no closing round bracket after 1-st open bracket -bad format\n";
errMsgBadParm= errorMsg;
this.logWr( "Error: "+errorMsg );
return errorMsg;
};
//.. got 1-st closing round bracket (after Value)
if( traceLvl > 0 )
this.logWr( methName+": found 1-st closing round bracket at:"+ic2+"\n");
val= word.substring( ic0, ic2 );
if( traceLvl > 0 )
this.logWr( methName+": got val:["+val+"]\n");
ic0= ic2 + lenCBR;
if( ic0 >= lastCh ) {
errorMsg=
methName+": missing next expected round brackets after pos.#="+ic2+
" -bad format\n";
errMsgBadParm= errorMsg;
this.logWr( "Error: "+errorMsg );
return errorMsg;
};
var ic3= -1;
if( word.substr(ic0).startsWith(EQS) ) {
//.. found EQS
ic3= ic0;
}
else {
errorMsg=
methName+": no equal sign after 1-st closing round bracket at pos.#="+ic2+
" -bad format";
errMsgBadParm= errorMsg;
this.logWr( "Error: "+errorMsg+"\n" );
return errorMsg;
};
//.. got Equal sign after 1-st closing round bracket
ic0= ic3 + lenEQS;
if( ic0 >= lastCh ) {
errorMsg= methName+": missing next expected round brackets after pos.#="+ic3+
" -bad format";
errMsgBadParm= errorMsg;
this.logWr( "Error: "+errorMsg+"\n" );
return errorMsg;
};
var ic4= -1;
if( word.substr(ic0).startsWith(OBR) ) {
//.. found
ic4= ic0;
}
else {
errorMsg=
methName+": no 2-nd open round bracket after equal sign at pos.#="+ic3+
" -bad format";
errMsgBadParm= errorMsg;
this.logWr( "Error: "+errorMsg+"\n" );
return errorMsg;
};
//.. got 2-nd open round bracket
ic0= ic4 + lenOBR;
if( ic0 >= lastCh ) {
errorMsg=
methName+": missing next expected round bracket after pos.#="+ic4+
" -bad format";
errMsgBadParm= errorMsg;
this.logWr( "Error: "+errorMsg+"\n" );
return errorMsg;
};
var ic5= word.indexOf( CBR, ic0 );
if( ic5 < 0 ) {
errorMsg=
methName+": no 2-nd closing round bracket after 2-nd open bracket at pos.#="+
ic4+" -bad format";
errMsgBadParm= errorMsg;
this.logWr( "Error: "+errorMsg+"\n" );
return errorMsg;
};
//.. got 2-nd closing round bracket
// (also assuming the remaining chars are whitespaces)
parmNamAndVal.push( word.substring(ic0, ic5) );
parmNamAndVal.push( val );
if( traceLvl > 1 )
this.logWr( methName+": got param: name:["+parmNamAndVal[0]+"]\n value:["+
parmNamAndVal[1]+"]\n");
return ''; //.. empty error message means OK
};
exports.reportParamBadFmt = function (
lineNm, pword, formatErr, perrorFName, pstatFName )
{
var methName= "reportParamBadFmt";
this.logWr( methName+": start: the param.name-value format is bad -Error!\n");
this.logWr( methName+": signalling error via percent = -1 \n");
var percent=-1;
var stmsg= '{\"percentage\":'+percent+'}';
this.updateProcStatus( pstatFName, stmsg );
this.logWr( methName+": writing the error message into a file "+
"for next request from the server...");
procErrorMsg= "Error: Bad Format of the parameter string";
if( pword != null && pword.length > 0 ) {
procErrorMsg += ":["+pword+"]";
};
var lineNmE= lineNm + 1; //.. line numbering in screen editor starts from 1
procErrorMsg += "\n Error found at line #:"+lineNmE;
procErrorMsg += "\n Error Details:["+formatErr+"]";
procErrorMsg += "\n Please, correct the error and try again.";
//.. using updateProcStatus to save the error message in a file
this.updateProcStatus( perrorFName, procErrorMsg );
};
exports.logWr = function ( msg ) {
// console.log(clName+": logWr: fdL="+this.fdL+" msg:["+msg+"]");
try {
fs.appendFileSync( this.fdL, msg, 'utf8' );
}
catch( err ) {
console.log(clName+": log uppend: error:"+err.message );
throw err;
};
// fs.appendFile( this.fdL, msg, 'utf8', (err) => {
// if( err ) {
// throw err;
// }
// });
};
//.. processing percentage or error message
exports.updateProcStatus = function ( pstfname, statusMsg ) {
var methName= "updateProcStatus";
this.logWr(methName+": start: pstfname:["+pstfname+"]\n");
var pstfname_p= pstfname+".pre";
this.logWr(methName+": new status: message["+statusMsg+"]\n");
var timeStamp = new Date().toISOString();
this.logWr( methName+": timeStamp:["+timeStamp+"]\n");
//.. pre-file
try {
this.fdPSt= fs.openSync( pstfname_p, 'a' );
}
catch( err ) {
this.logWr(clName+": status file open: error:["+err.message+"]\n");
throw err;
}
this.logWr(methName+": writing status: fdPSt="+this.fdPSt+"\n");
try {
fs.appendFileSync( this.fdPSt, statusMsg, 'utf8' );
}
catch( err ) {
this.logWr(clName+": status file append: error: code="+err.code );
throw err;
}
this.logWr(methName+": closing status file: fdPSt="+this.fdPSt+"\n");
fs.closeSync( this.fdPSt );
this.logWr(methName+": status file closed - final renaming...\n");
fs.renameSync( pstfname_p, pstfname );
this.logWr(methName+": status file renamed - finished.\n");
};
exports.wait = function ( secs ) {
var methName= "wait";
this.logWr(methName+": start: secs="+secs+"\n");
var inStr= String( secs );
// this.logWr(methName+": inStr:["+inStr+"]\n");
var s_args= new Array( 0 );
s_args.push( inStr );
var tmoutms= 500 +1000*secs;
var mopts= { timeout: tmoutms };
// this.logWr(methName+": mopts:["+ JSON.stringify(mopts)+"]\n");
var moutObj= Chproc.spawnSync( 'sleep', s_args, mopts);
this.logWr(methName+": moutObj: status:["+moutObj.status+"](0=ok)\n");
if( moutObj.error != null ) {
console.log(methName+": moutObj: Error:["+JSON.stringify(moutObj.error)+"]\n"); }
};
|