summaryrefslogtreecommitdiff
path: root/libssh2-sys/libssh2-1.5.0/vms/man2help.c
blob: 92e5f2b94157a2760a649b8a4fba034062213aa6 (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
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <errno.h>

#include <starlet.h>
#include <lib$routines.h>
#include <ssdef.h>
#include <descrip.h>
#include <rms.h>

typedef struct manl{
    struct manl *next;
    char *filename;
}man, *manPtr;
 
typedef struct pf_fabnam{
    struct FAB dfab;
    struct RAB drab;
    struct namldef dnam;
    char   expanded_filename[NAM$C_MAXRSS + 1]; 
} pfn, *pfnPtr;

/*----------------------------------------------------------*/

fpcopy( char *output, char *input, int len )
{
char    *is, *os;
int i;

if ( len ){
    for ( is = input, os = output, i = 0; i < len ; ++i, ++is, ++os){
            *os = *is;
    }
    *os = 0;
}else{
    output[0] = 0;
}
}           


/*----------------------------------------------------------*/
/* give part of ilename in partname. See code for proper
   value of i ( 0 = node, 1 = dev, 2 = dir,3 = name etc.
*/ 

int fnamepart( char *inputfile, char *part, int whatpart )
{
pfnPtr pf;
int     status;
char    ipart[6][256], *i, *p;

pf = calloc( 1, sizeof( pfn ) );

pf->dfab = cc$rms_fab;
pf->drab = cc$rms_rab;
pf->dnam = cc$rms_naml;

pf->dfab.fab$l_naml = &pf->dnam;

pf->dfab.fab$l_fna = (char *) -1; 
pf->dfab.fab$l_dna = (char *) -1; 
pf->dfab.fab$b_fns = 0;
pf->dfab.fab$w_ifi = 0;

pf->dnam.naml$l_long_defname = NULL; //inputfile;
pf->dnam.naml$l_long_defname_size = 0;//strlen( inputfile );

pf->dnam.naml$l_long_filename = inputfile;
pf->dnam.naml$l_long_filename_size = strlen( inputfile);

pf->dnam.naml$l_long_expand = pf->expanded_filename;
pf->dnam.naml$l_long_expand_alloc = NAM$C_MAXRSS ;

pf->dnam.naml$b_nop |= NAML$M_SYNCHK | NAML$M_PWD;

status = sys$parse( &pf->dfab, 0,0);
if ( !(status&1) ){
    free( pf );
    return( status );
}

fpcopy ( ipart[0], pf->dnam.naml$l_long_node , pf->dnam.naml$l_long_node_size);
fpcopy ( ipart[1], pf->dnam.naml$l_long_dev , pf->dnam.naml$l_long_dev_size);
fpcopy ( ipart[2], pf->dnam.naml$l_long_dir , pf->dnam.naml$l_long_dir_size);
fpcopy ( ipart[3], pf->dnam.naml$l_long_name , pf->dnam.naml$l_long_name_size);
fpcopy ( ipart[4], pf->dnam.naml$l_long_type , pf->dnam.naml$l_long_type_size);                                               
fpcopy ( ipart[5], pf->dnam.naml$l_long_ver , pf->dnam.naml$l_long_ver_size);

for( i = ipart[ whatpart ], p = part; *i; ++i, ++p){
   if ( p == part ){
      *p = toupper( *i );
   }else{
      *p = tolower( *i );
   }        
}
*p = 0;

free( pf );
return(1);
}
/*----------------------------------------------------------*/

int find_file(char *filename,char *gevonden,int *findex)
{
int     status;
struct  dsc$descriptor gevondend;
struct  dsc$descriptor filespec;
char    gevonden_file[NAM$C_MAXRSS + 1];

filespec.dsc$w_length = strlen(filename);
filespec.dsc$b_dtype  = DSC$K_DTYPE_T;
filespec.dsc$b_class  = DSC$K_CLASS_S; 
filespec.dsc$a_pointer = filename;

gevondend.dsc$w_length = NAM$C_MAXRSS;
gevondend.dsc$b_dtype  = DSC$K_DTYPE_T;
gevondend.dsc$b_class  = DSC$K_CLASS_S; 
gevondend.dsc$a_pointer = gevonden_file;

status=lib$find_file(&filespec,&gevondend,findex,0,0,0,0);
    
if ( (status & 1) == 1 ){
       strcpy(gevonden,strtok(gevonden_file," "));
}else{
       gevonden[0] = 0;
}

return(status);
}


/*--------------------------------------------*/

manPtr addman( manPtr *manroot,char *filename )
{
manPtr m,f;

m = calloc( 1, sizeof( man) );
if ( !m ) return( NULL );

m->filename = strdup( filename );

if ( *manroot == NULL ){
   *manroot = m;    
}else{
   for( f = *manroot; f->next ; f = f->next );
   f->next = m;
}
return(m);
}

/*--------------------------------------------*/
void freeman( manPtr *manroot )
{
manPtr m,n;

for( m = *manroot; m ; m = n ){
     free( m->filename );
     n = m->next;
     free ( m );
}
*manroot = NULL;
}

/*--------------------------------------------*/

int listofmans( char *filespec, manPtr *manroot )
{
manPtr  r;
int     status;
int     ffindex=0;
char    gevonden[NAM$C_MAXRSS + 1];

while(1){
    status = find_file( filespec, gevonden, &ffindex );

    if ( (status&1) ){
        r = addman( manroot, gevonden );
        if ( r == NULL ) return(2);
    }else{
        if ( !( status&1)) break;
    }
}

lib$find_file_end( &ffindex);
if ( status == RMS$_NMF) status = 1;


return( status );
}

/*--------------------------------------------*/

int convertman ( char *filespec, FILE *hlp , int base_level, int add_parentheses )
{
FILE    *man;
char    *in, *uit;
char    *m,*h;
size_t  len, thislen, maxlen= 50000;
int     bol,mode, return_status=1;
char subjectname[ NAM$C_MAXRSS + 1 ];

in  = calloc( 1, maxlen + 1 );
uit = calloc( 1, maxlen + 1 );

if ( in == NULL || uit == NULL ) return(2);

man = fopen( filespec, "r");
if ( man == NULL ) return(vaxc$errno);

for( len = 0; !feof( man ) && len < maxlen ; len += thislen ){
    thislen = fread( in + len, 1, maxlen - len, man );
}

fclose (man);

m = in;
h = uit;

*(m + len ) = 0;

for ( mode = 0, bol = 1 ; *m; ++m ){

    switch ( mode ){
        case 0:
          switch(*m){
            case '.':
                if ( bol ){
                    mode = 1;
                }else{
                    *h = *m;
                    ++h;
                }
                break;
            case '\\':
                if ( bol ){
                   *h = ' ';++h;
                   *h = ' ';++h;
                }
                mode = 2;
                break;
            default:
                if ( bol ){
                   *h = ' ';++h;
                   *h = ' ';++h;
                }
                *h = *m;
                ++h;
                break;
          }
          break;
        case 1: /* after . at bol */

          switch(*m){
            case '\\':
                while( *m != '\n' && *m != '\r' && *m )++m;
                mode = 0;
                break;
            case 'B':
                   ++m; 
                   *h = ' ';++h;
                   mode = 0;
                   break;   
            case 'I':
                    /* remove preceding eol */
                    if ( *(m+1) != 'P' ){
                        --h;
                        while ( (*h == '\n' || *h == '\r') && h > uit )--h;
                        ++h;
                    }

                    /* skip .Ix */
                    for(;*m != ' ' && *m != '\n' && *m != '\r'; ++m); 

                    /* copy line up to EOL */

                    for(;*m != '\n' && *m != '\r' && *m; ++m, ++h)*h = *m;

                    /* if line ends in ., this is an EOL */

                    if ( *(h-1) == '.'){
                         --h; 
                         --m;
                    }else{
                        /* if line does not end in ., skip EOL in source */

                        if ( *(m+1) == '\n' || *(m+1) == '\r')++m;
                    }
                    mode = 0;
                    break;
            case 'S':
                 if ( *(m+1) == 'H' ){
                    *h = '\n';++h;
                    if ( strncmp( m+3 ,"NAME",4) == 0 || 
                         strncmp( m+3 ,"SYNOPSIS",8) == 0 ||
                         strncmp( m+3 ,"DESCRIPTION",11) == 0 ){
                        while( *m != '\n' && *m != '\r')++m;
                        mode = 0;
                    }else{
                        ++m;

                        /* write help level, and flag it */

                        *h = '0' + base_level + 1;++h;
                        return_status |= 2;

                        *h = ' ';++h; 

                        /* skip H (or whatever after S) and blank */
                        ++m;++m;

                        for(;*m != '\n' && *m != '\r' && *m; ++m, ++h){

                           /* write help label in lowercase, skip quotes */
                           /* fill blanks with underscores */

                           if ( *m != '\"' ){
                                *h = tolower( *m );
                                if (*h == ' ') *h = '_';    
                           }else{
                                --h;
                           }    
                        } 

                        /* Add a linefeed or two */

                        *h = *m;++h;
                        *h = *m;++h;

                        mode = 0;
                    }   
                 }
                 break;
            case 'T':
                 if ( *(m+1) == 'H' ){
                    *h = '0' + base_level; ++h;
                    return_status |= 2;
                    *h = ' ';++h;
                    for ( m = m + 3; *m != ' ' && *m ; ++m, ++h ){
                          *h = *m;
                    }
					if ( add_parentheses ){
						 *h = '(';++h;
						 *h = ')';++h;
					}
                    while( *m != '\n' && *m != '\r' && *m )++m;
                    mode = 0;
                 }
                 break;
            default:
                ++m;
                mode = 0;
                break;
           }
           break;
        case 2: /* after \ skip two characters or print the backslash */            
          switch(*m){
            case '\\':
                *h = *m;
                ++h;
                mode = 0;
                break;
            default:
                ++m;
                mode = 0;
                break;
           }
           break;   
    } /*end switch mode */

    bol = 0;
    if ( *m == '\n' || *m == '\r') bol = 1;

}/* end for mode */

*h = 0;


if ( (return_status&2) ){
    fprintf( hlp, "%s\n\n", uit);
}else{
    fnamepart( filespec, subjectname,3);
    if ( *subjectname ){
        fprintf( hlp, "%d %s\n\n%s\n\n", base_level, subjectname, uit);
    }else{
        /* No filename (as is the case with a logical), use first word as subject name */
        char *n,*s;

        for(n = in; isspace( *n );++n);
        for(s = subjectname; !(isspace( *n )); ++n,++s)*s = *n;
        *s = 0;

        fprintf( hlp, "%d %s\n\n%s\n\n", base_level, subjectname, uit);
    }
}

/*
 printf( "read %d from %s, written %d to helpfile, return_status = %d\n",
    len, filespec, strlen(uit), return_status );
*/

free( m ); 
free( h ); 

return ( 1);
}

/*--------------------------------------------*/

int convertmans( char *filespec, char *hlpfilename, int base_level, int append, int add_parentheses )
{
int status=1;
manPtr  manroot=NULL, m;
FILE    *hlp;

if ( append ){
    hlp = fopen( hlpfilename,"a+");
}else{
    hlp = fopen( hlpfilename,"w");
}

if ( hlp == NULL ) return( vaxc$errno );

status = listofmans( filespec, &manroot );
if ( !(status&1) ) return( status );

for ( m = manroot ; m ; m = m->next ){
    status = convertman( m->filename, hlp , base_level, add_parentheses );
    if ( !(status&1) ){
        fprintf(stderr,"Convertman of %s went wrong\n", m->filename);
        break;
    }
}
freeman( &manroot );
return( status );
}

/*--------------------------------------------*/
void print_help()
{
   fprintf( stderr, "Usage: [-a] [-b x] convertman <manfilespec> <helptextfile>\n" );
   fprintf( stderr, "       -a append <manfilespec> to <helptextfile>\n" );
   fprintf( stderr, "       -b <baselevel> if no headers found create one with level <baselevel>\n" );
   fprintf( stderr, "          and the filename as title.\n" );
   fprintf( stderr, "       -p add parentheses() to baselevel help items.\n" );

}
/*--------------------------------------------*/

main ( int argc, char **argv )
{
int     status;
int     i,j;
int     append, base_level, basechange, add_parentheses;
char    *manfile=NULL;
char    *helpfile=NULL;

if ( argc < 3 ){
   print_help();
   return( 1 ) ;
}

append     = 0;
base_level = 1;
basechange = 0;
add_parentheses = 0;

for ( i = 1; i < argc; ++i){
    if ( argv[i][0] == '-' ){
        for( j = 1; argv[i][j] ; ++j ){
            switch( argv[i][j] ){
                case 'a':
                    append = 1;
                    break;
                case 'b':   
                    if ( (i+1) < argc ){
                        base_level = atoi( argv[ i + 1 ] );
                        basechange = 1;
                    }
                    break;
                case 'p':
                    add_parentheses = 1;
                    break;
            }
        }
        if ( basechange){
            basechange = 0;
            i = i + 1;
        }
    }else{
        if ( manfile == NULL ){
            manfile = strdup( argv[i]);
        } else if ( helpfile == NULL ){
            helpfile = strdup( argv[i]);
        } else {
            fprintf( stderr, "Unrecognized parameter : %s\n", argv[i]);
        }
    }
}


/* fprintf( stderr,"manfile: %s, helpfile: %s, append: %d, base_level : %d\n",
        manfile, helpfile, append, base_level);
*/

status = convertmans( manfile, helpfile, base_level, append, add_parentheses );

free( manfile );
free( helpfile );

return( status );
}