geom_gate userland utility improvements
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

517 lines
13 KiB

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <ctype.h>
  5. #include <errno.h>
  6. #include <starlet.h>
  7. #include <lib$routines.h>
  8. #include <ssdef.h>
  9. #include <descrip.h>
  10. #include <rms.h>
  11. typedef struct manl{
  12. struct manl *next;
  13. char *filename;
  14. }man, *manPtr;
  15. typedef struct pf_fabnam{
  16. struct FAB dfab;
  17. struct RAB drab;
  18. struct namldef dnam;
  19. char expanded_filename[NAM$C_MAXRSS + 1];
  20. } pfn, *pfnPtr;
  21. /*----------------------------------------------------------*/
  22. fpcopy( char *output, char *input, int len )
  23. {
  24. char *is, *os;
  25. int i;
  26. if ( len ){
  27. for ( is = input, os = output, i = 0; i < len ; ++i, ++is, ++os){
  28. *os = *is;
  29. }
  30. *os = 0;
  31. }else{
  32. output[0] = 0;
  33. }
  34. }
  35. /*----------------------------------------------------------*/
  36. /* give part of ilename in partname. See code for proper
  37. value of i ( 0 = node, 1 = dev, 2 = dir,3 = name etc.
  38. */
  39. int fnamepart( char *inputfile, char *part, int whatpart )
  40. {
  41. pfnPtr pf;
  42. int status;
  43. char ipart[6][256], *i, *p;
  44. pf = calloc( 1, sizeof( pfn ) );
  45. pf->dfab = cc$rms_fab;
  46. pf->drab = cc$rms_rab;
  47. pf->dnam = cc$rms_naml;
  48. pf->dfab.fab$l_naml = &pf->dnam;
  49. pf->dfab.fab$l_fna = (char *) -1;
  50. pf->dfab.fab$l_dna = (char *) -1;
  51. pf->dfab.fab$b_fns = 0;
  52. pf->dfab.fab$w_ifi = 0;
  53. pf->dnam.naml$l_long_defname = NULL; //inputfile;
  54. pf->dnam.naml$l_long_defname_size = 0;//strlen( inputfile );
  55. pf->dnam.naml$l_long_filename = inputfile;
  56. pf->dnam.naml$l_long_filename_size = strlen( inputfile);
  57. pf->dnam.naml$l_long_expand = pf->expanded_filename;
  58. pf->dnam.naml$l_long_expand_alloc = NAM$C_MAXRSS ;
  59. pf->dnam.naml$b_nop |= NAML$M_SYNCHK | NAML$M_PWD;
  60. status = sys$parse( &pf->dfab, 0,0);
  61. if ( !(status&1) ){
  62. free( pf );
  63. return( status );
  64. }
  65. fpcopy ( ipart[0], pf->dnam.naml$l_long_node , pf->dnam.naml$l_long_node_size);
  66. fpcopy ( ipart[1], pf->dnam.naml$l_long_dev , pf->dnam.naml$l_long_dev_size);
  67. fpcopy ( ipart[2], pf->dnam.naml$l_long_dir , pf->dnam.naml$l_long_dir_size);
  68. fpcopy ( ipart[3], pf->dnam.naml$l_long_name , pf->dnam.naml$l_long_name_size);
  69. fpcopy ( ipart[4], pf->dnam.naml$l_long_type , pf->dnam.naml$l_long_type_size);
  70. fpcopy ( ipart[5], pf->dnam.naml$l_long_ver , pf->dnam.naml$l_long_ver_size);
  71. for( i = ipart[ whatpart ], p = part; *i; ++i, ++p){
  72. if ( p == part ){
  73. *p = toupper( *i );
  74. }else{
  75. *p = tolower( *i );
  76. }
  77. }
  78. *p = 0;
  79. free( pf );
  80. return(1);
  81. }
  82. /*----------------------------------------------------------*/
  83. int find_file(char *filename,char *gevonden,int *findex)
  84. {
  85. int status;
  86. struct dsc$descriptor gevondend;
  87. struct dsc$descriptor filespec;
  88. char gevonden_file[NAM$C_MAXRSS + 1];
  89. filespec.dsc$w_length = strlen(filename);
  90. filespec.dsc$b_dtype = DSC$K_DTYPE_T;
  91. filespec.dsc$b_class = DSC$K_CLASS_S;
  92. filespec.dsc$a_pointer = filename;
  93. gevondend.dsc$w_length = NAM$C_MAXRSS;
  94. gevondend.dsc$b_dtype = DSC$K_DTYPE_T;
  95. gevondend.dsc$b_class = DSC$K_CLASS_S;
  96. gevondend.dsc$a_pointer = gevonden_file;
  97. status=lib$find_file(&filespec,&gevondend,findex,0,0,0,0);
  98. if ( (status & 1) == 1 ){
  99. strcpy(gevonden,strtok(gevonden_file," "));
  100. }else{
  101. gevonden[0] = 0;
  102. }
  103. return(status);
  104. }
  105. /*--------------------------------------------*/
  106. manPtr addman( manPtr *manroot,char *filename )
  107. {
  108. manPtr m,f;
  109. m = calloc( 1, sizeof( man) );
  110. if ( !m ) return( NULL );
  111. m->filename = strdup( filename );
  112. if ( *manroot == NULL ){
  113. *manroot = m;
  114. }else{
  115. for( f = *manroot; f->next ; f = f->next );
  116. f->next = m;
  117. }
  118. return(m);
  119. }
  120. /*--------------------------------------------*/
  121. void freeman( manPtr *manroot )
  122. {
  123. manPtr m,n;
  124. for( m = *manroot; m ; m = n ){
  125. free( m->filename );
  126. n = m->next;
  127. free ( m );
  128. }
  129. *manroot = NULL;
  130. }
  131. /*--------------------------------------------*/
  132. int listofmans( char *filespec, manPtr *manroot )
  133. {
  134. manPtr r;
  135. int status;
  136. int ffindex=0;
  137. char gevonden[NAM$C_MAXRSS + 1];
  138. while(1){
  139. status = find_file( filespec, gevonden, &ffindex );
  140. if ( (status&1) ){
  141. r = addman( manroot, gevonden );
  142. if ( r == NULL ) return(2);
  143. }else{
  144. if ( !( status&1)) break;
  145. }
  146. }
  147. lib$find_file_end( &ffindex);
  148. if ( status == RMS$_NMF) status = 1;
  149. return( status );
  150. }
  151. /*--------------------------------------------*/
  152. int convertman ( char *filespec, FILE *hlp , int base_level, int add_parentheses )
  153. {
  154. FILE *man;
  155. char *in, *uit;
  156. char *m,*h;
  157. size_t len, thislen, maxlen= 50000;
  158. int bol,mode, return_status=1;
  159. char subjectname[ NAM$C_MAXRSS + 1 ];
  160. in = calloc( 1, maxlen + 1 );
  161. uit = calloc( 1, maxlen + 1 );
  162. if ( in == NULL || uit == NULL ) return(2);
  163. man = fopen( filespec, "r");
  164. if ( man == NULL ) return(vaxc$errno);
  165. for( len = 0; !feof( man ) && len < maxlen ; len += thislen ){
  166. thislen = fread( in + len, 1, maxlen - len, man );
  167. }
  168. fclose (man);
  169. m = in;
  170. h = uit;
  171. *(m + len ) = 0;
  172. for ( mode = 0, bol = 1 ; *m; ++m ){
  173. switch ( mode ){
  174. case 0:
  175. switch(*m){
  176. case '.':
  177. if ( bol ){
  178. mode = 1;
  179. }else{
  180. *h = *m;
  181. ++h;
  182. }
  183. break;
  184. case '\\':
  185. if ( bol ){
  186. *h = ' ';++h;
  187. *h = ' ';++h;
  188. }
  189. mode = 2;
  190. break;
  191. default:
  192. if ( bol ){
  193. *h = ' ';++h;
  194. *h = ' ';++h;
  195. }
  196. *h = *m;
  197. ++h;
  198. break;
  199. }
  200. break;
  201. case 1: /* after . at bol */
  202. switch(*m){
  203. case '\\':
  204. while( *m != '\n' && *m != '\r' && *m )++m;
  205. mode = 0;
  206. break;
  207. case 'B':
  208. ++m;
  209. *h = ' ';++h;
  210. mode = 0;
  211. break;
  212. case 'I':
  213. /* remove preceding eol */
  214. if ( *(m+1) != 'P' ){
  215. --h;
  216. while ( (*h == '\n' || *h == '\r') && h > uit )--h;
  217. ++h;
  218. }
  219. /* skip .Ix */
  220. for(;*m != ' ' && *m != '\n' && *m != '\r'; ++m);
  221. /* copy line up to EOL */
  222. for(;*m != '\n' && *m != '\r' && *m; ++m, ++h)*h = *m;
  223. /* if line ends in ., this is an EOL */
  224. if ( *(h-1) == '.'){
  225. --h;
  226. --m;
  227. }else{
  228. /* if line does not end in ., skip EOL in source */
  229. if ( *(m+1) == '\n' || *(m+1) == '\r')++m;
  230. }
  231. mode = 0;
  232. break;
  233. case 'S':
  234. if ( *(m+1) == 'H' ){
  235. *h = '\n';++h;
  236. if ( strncmp( m+3 ,"NAME",4) == 0 ||
  237. strncmp( m+3 ,"SYNOPSIS",8) == 0 ||
  238. strncmp( m+3 ,"DESCRIPTION",11) == 0 ){
  239. while( *m != '\n' && *m != '\r')++m;
  240. mode = 0;
  241. }else{
  242. ++m;
  243. /* write help level, and flag it */
  244. *h = '0' + base_level + 1;++h;
  245. return_status |= 2;
  246. *h = ' ';++h;
  247. /* skip H (or whatever after S) and blank */
  248. ++m;++m;
  249. for(;*m != '\n' && *m != '\r' && *m; ++m, ++h){
  250. /* write help label in lowercase, skip quotes */
  251. /* fill blanks with underscores */
  252. if ( *m != '\"' ){
  253. *h = tolower( *m );
  254. if (*h == ' ') *h = '_';
  255. }else{
  256. --h;
  257. }
  258. }
  259. /* Add a linefeed or two */
  260. *h = *m;++h;
  261. *h = *m;++h;
  262. mode = 0;
  263. }
  264. }
  265. break;
  266. case 'T':
  267. if ( *(m+1) == 'H' ){
  268. *h = '0' + base_level; ++h;
  269. return_status |= 2;
  270. *h = ' ';++h;
  271. for ( m = m + 3; *m != ' ' && *m ; ++m, ++h ){
  272. *h = *m;
  273. }
  274. if ( add_parentheses ){
  275. *h = '(';++h;
  276. *h = ')';++h;
  277. }
  278. while( *m != '\n' && *m != '\r' && *m )++m;
  279. mode = 0;
  280. }
  281. break;
  282. default:
  283. ++m;
  284. mode = 0;
  285. break;
  286. }
  287. break;
  288. case 2: /* after \ skip two characters or print the backslash */
  289. switch(*m){
  290. case '\\':
  291. *h = *m;
  292. ++h;
  293. mode = 0;
  294. break;
  295. default:
  296. ++m;
  297. mode = 0;
  298. break;
  299. }
  300. break;
  301. } /*end switch mode */
  302. bol = 0;
  303. if ( *m == '\n' || *m == '\r') bol = 1;
  304. }/* end for mode */
  305. *h = 0;
  306. if ( (return_status&2) ){
  307. fprintf( hlp, "%s\n\n", uit);
  308. }else{
  309. fnamepart( filespec, subjectname,3);
  310. if ( *subjectname ){
  311. fprintf( hlp, "%d %s\n\n%s\n\n", base_level, subjectname, uit);
  312. }else{
  313. /* No filename (as is the case with a logical), use first word as subject name */
  314. char *n,*s;
  315. for(n = in; isspace( *n );++n);
  316. for(s = subjectname; !(isspace( *n )); ++n,++s)*s = *n;
  317. *s = 0;
  318. fprintf( hlp, "%d %s\n\n%s\n\n", base_level, subjectname, uit);
  319. }
  320. }
  321. /*
  322. printf( "read %d from %s, written %d to helpfile, return_status = %d\n",
  323. len, filespec, strlen(uit), return_status );
  324. */
  325. free( m );
  326. free( h );
  327. return ( 1);
  328. }
  329. /*--------------------------------------------*/
  330. int convertmans( char *filespec, char *hlpfilename, int base_level, int append, int add_parentheses )
  331. {
  332. int status=1;
  333. manPtr manroot=NULL, m;
  334. FILE *hlp;
  335. if ( append ){
  336. hlp = fopen( hlpfilename,"a+");
  337. }else{
  338. hlp = fopen( hlpfilename,"w");
  339. }
  340. if ( hlp == NULL ) return( vaxc$errno );
  341. status = listofmans( filespec, &manroot );
  342. if ( !(status&1) ) return( status );
  343. for ( m = manroot ; m ; m = m->next ){
  344. status = convertman( m->filename, hlp , base_level, add_parentheses );
  345. if ( !(status&1) ){
  346. fprintf(stderr,"Convertman of %s went wrong\n", m->filename);
  347. break;
  348. }
  349. }
  350. freeman( &manroot );
  351. return( status );
  352. }
  353. /*--------------------------------------------*/
  354. void print_help()
  355. {
  356. fprintf( stderr, "Usage: [-a] [-b x] convertman <manfilespec> <helptextfile>\n" );
  357. fprintf( stderr, " -a append <manfilespec> to <helptextfile>\n" );
  358. fprintf( stderr, " -b <baselevel> if no headers found create one with level <baselevel>\n" );
  359. fprintf( stderr, " and the filename as title.\n" );
  360. fprintf( stderr, " -p add parentheses() to baselevel help items.\n" );
  361. }
  362. /*--------------------------------------------*/
  363. main ( int argc, char **argv )
  364. {
  365. int status;
  366. int i,j;
  367. int append, base_level, basechange, add_parentheses;
  368. char *manfile=NULL;
  369. char *helpfile=NULL;
  370. if ( argc < 3 ){
  371. print_help();
  372. return( 1 ) ;
  373. }
  374. append = 0;
  375. base_level = 1;
  376. basechange = 0;
  377. add_parentheses = 0;
  378. for ( i = 1; i < argc; ++i){
  379. if ( argv[i][0] == '-' ){
  380. for( j = 1; argv[i][j] ; ++j ){
  381. switch( argv[i][j] ){
  382. case 'a':
  383. append = 1;
  384. break;
  385. case 'b':
  386. if ( (i+1) < argc ){
  387. base_level = atoi( argv[ i + 1 ] );
  388. basechange = 1;
  389. }
  390. break;
  391. case 'p':
  392. add_parentheses = 1;
  393. break;
  394. }
  395. }
  396. if ( basechange){
  397. basechange = 0;
  398. i = i + 1;
  399. }
  400. }else{
  401. if ( manfile == NULL ){
  402. manfile = strdup( argv[i]);
  403. } else if ( helpfile == NULL ){
  404. helpfile = strdup( argv[i]);
  405. } else {
  406. fprintf( stderr, "Unrecognized parameter : %s\n", argv[i]);
  407. }
  408. }
  409. }
  410. /* fprintf( stderr,"manfile: %s, helpfile: %s, append: %d, base_level : %d\n",
  411. manfile, helpfile, append, base_level);
  412. */
  413. status = convertmans( manfile, helpfile, base_level, append, add_parentheses );
  414. free( manfile );
  415. free( helpfile );
  416. return( status );
  417. }