"Fossies" - the Fresh Open Source Software Archive

Member "xorriso-1.5.4/xorriso/make_xorriso_1.c" (30 Jan 2021, 13430 Bytes) of package /linux/misc/xorriso-1.5.4.pl02.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "make_xorriso_1.c" see the Fossies "Dox" file reference documentation and the last Fossies "Diffs" side-by-side code changes report: 1.5.2_vs_1.5.4.

    1 
    2 /*
    3   ( cd xorriso ; cc -g -Wall -o make_xorriso_1 make_xorriso_1.c )
    4 */
    5 /*
    6    Specialized converter from xorriso/xorriso.texi to xorriso/xorriso.1,
    7    or from xorriso/xorrisofs.texi to xorriso/xorrisofs.1,
    8    or from xorriso/xorrecord.texi to xorriso/xorrecord.1.
    9    or from xorriso/xorriso-tcltk.texi to xorriso/xorriso-tcltk.1.
   10    or from xorriso/xorriso-dd-target.texi to xorriso/xorriso-dd-target.1.
   11 
   12    The conversion rules are described at the beginning of xorriso/xorriso.texi
   13 
   14    Copyright 2010 - 2019 Thomas Schmitt, <scdbackup@gmx.net>
   15 
   16    Provided under GPL version 2 or later.
   17 */
   18 
   19 #ifdef HAVE_CONFIG_H
   20 #include "../config.h"
   21 #endif
   22 
   23 #include <unistd.h>
   24 #include <stdlib.h>
   25 #include <stdio.h>
   26 #include <string.h>
   27 #include <ctype.h>
   28 #include <errno.h>
   29 
   30 
   31 /* The conversion state
   32 */
   33 struct Mx1 {
   34 
   35  char prog[4096];
   36 
   37  int count_in;
   38  int count_out;
   39 
   40  int skipping;          /* <0 stacked skipping , 0= no , >0 counting down */
   41  
   42 };
   43 
   44 int Mx1_substitute(struct Mx1 *m, char line_in[256], char line_out[256],
   45                    int raw, int upto, int flag);
   46 
   47 
   48 
   49 int Mx1_init(struct Mx1 *m, char *prog, int flag)
   50 {
   51  strncpy(m->prog, prog, sizeof(m->prog) - 1);
   52  m->prog[sizeof(m->prog) - 1]= 0;
   53  m->count_in= 0;
   54  m->count_out= 0;
   55  m->skipping= 0;
   56  return(1);
   57 }
   58 
   59 
   60 int Mx1_report_error(struct Mx1 *m, char *text, int flag)
   61 {
   62  fprintf(stderr, "%s : line %d : %s\n", m->prog, m->count_in, text);
   63  return(1);
   64 }
   65 
   66 
   67 int Mx1__get_word(char *line, char word[256], char **remainder, int flag)
   68 {
   69  char *cpt, *start;
   70  int l;
   71 
   72  word[0]= 0;
   73  *remainder= NULL;
   74  for(cpt= line; *cpt != 0 && isspace(*cpt); cpt++);
   75  if(*cpt == 0)
   76    return(0);
   77  start= cpt;
   78  for(cpt= line; *cpt != 0 && ! isspace(*cpt); cpt++);
   79  l= cpt - start;
   80  if(l > 0)
   81    strncpy(word, start, l);
   82  word[l]= 0;
   83  *remainder= cpt;
   84  return(1);
   85 }
   86 
   87 
   88 int Mx1_is_wrap(struct Mx1 *m, char wraps[][20], char *start, char **found,
   89                 int flag)
   90 {
   91  int i;
   92 
   93  for(i= 0; wraps[i][0] != 0; i++)
   94    if(strncmp(start, wraps[i], strlen(wraps[i])) == 0)
   95  break;
   96  if(wraps[i][0] != 0) {
   97    if(found != NULL)
   98       *found= wraps[i];
   99    return(1);
  100  }
  101  return(0);
  102 }
  103 
  104 
  105 int Mx1_is_bold_wrap(struct Mx1 *m, char *start, char **found, int flag)
  106 {
  107  int ret;
  108  static char bold_wraps[][20]= {
  109    "@b{", "@dfn{", "@emph{", "@strong{", "@command{",
  110    "" };
  111  
  112  ret= Mx1_is_wrap(m, bold_wraps, start, found, 0);
  113  return(ret);
  114 }
  115 
  116 
  117 int Mx1_is_normal_wrap(struct Mx1 *m, char *start, char **found, int flag)
  118 {
  119  int ret;
  120  static char normal_wraps[][20]= {
  121    "@var{", "@code{", "@i{", "@abbr{", "@file{", "@option{", "@samp{", "@r{",
  122     "" };
  123  
  124  ret= Mx1_is_wrap(m, normal_wraps, start, found, 0);
  125  return(ret);
  126 }
  127 
  128 
  129 int Mx1_is_ignored_wrap(struct Mx1 *m, char *start, char **found, int flag)
  130 {
  131  int ret;
  132  static char ignored_wraps[][20]= {
  133    "@ref{", "@xref{",
  134     "" };
  135  
  136  ret= Mx1_is_wrap(m, ignored_wraps, start, found, 0);
  137  return(ret);
  138 }
  139 
  140 
  141 int Mx1_is_any_wrap(struct Mx1 *m, char *start, char **found, int flag)
  142 {
  143  int ret;
  144 
  145  ret= Mx1_is_bold_wrap(m, start, found, 0);
  146  if(ret > 0)
  147    return(1);
  148  ret= Mx1_is_normal_wrap(m, start, found, 0);
  149  if(ret > 0)
  150    return(2);
  151  ret= Mx1_is_ignored_wrap(m, start, found, 0);
  152  if(ret > 0)
  153    return(3);
  154  return(0);
  155 }
  156 
  157 
  158 /* @param flag bit0= recursion
  159                bit1= drop content of brackets
  160 */
  161 int Mx1_rewrap(struct Mx1 *m, char **read_pt, char **write_pt,
  162                 char *write_base, char *envelope,
  163                 char *front, char *back, int flag)
  164 {
  165  char *rpt, *wpt, *ept, content[256], msg[256];
  166  int l, ret;
  167 
  168  rpt= *read_pt;
  169  wpt= *write_pt;
  170 
  171  ept= strchr(rpt, '}');
  172  if(ept == NULL) {
  173    sprintf(msg, "No closing bracket found for '%s'", envelope);
  174    Mx1_report_error(m, msg, 0);
  175    return(-1);
  176  }
  177  /* Mapped {...} content is subject to the rules except {...} mapping. */
  178  l= ept - rpt;
  179  if(flag & 2)
  180    l= 0;
  181  if(l > 0) {
  182    ret= Mx1_substitute(m, rpt, content, 0, l, 1);
  183    if(ret <= 0)
  184      return(ret);
  185    l= strlen(content);
  186  }
  187  if((wpt - write_base) + l + strlen(front) + strlen(back) > 255) {
  188    Mx1_report_error(m, "Line length overflow while text substitution", 0);
  189    return(-1);
  190  }
  191  strcpy(wpt, front);
  192  wpt+= strlen(front);
  193  if(l > 0)
  194    memcpy(wpt, content, l);
  195  wpt+= l;
  196  strcpy(wpt, back);
  197  wpt+= strlen(back);
  198 
  199  (*read_pt)+= ept - rpt;
  200  (*write_pt)= wpt;
  201  return(1);
  202 }
  203 
  204 
  205 /* @param flag bit0= recursion
  206 */
  207 int Mx1_substitute(struct Mx1 *m, char line_in[256], char line_out[256],
  208                    int raw, int upto, int flag)
  209 {
  210  char *rpt, *wpt, *found;
  211  int ret, typ= 0;
  212 
  213  wpt= line_out;
  214  for(rpt= line_in; rpt - line_in < upto && *rpt != 0; rpt++) {
  215    if(rpt - line_in < raw) {
  216      *(wpt++)= *rpt;
  217  continue;
  218    } 
  219    if(*rpt == '@') {
  220      typ= 0;
  221      if(!(flag & 1))
  222        typ= Mx1_is_any_wrap(m, rpt, &found, 0);
  223      if(typ == 1) {
  224        /* @b{...}, @command{...}, @dfn{...}, @emph{...}, @strong{...}
  225             get mapped to \fB...\fR .
  226        */
  227        rpt+= strlen(found);
  228        ret= Mx1_rewrap(m, &rpt, &wpt, line_out,
  229                         found , "\\fB", "\\fR", flag & 1);
  230        if(ret <= 0)
  231          return(ret);
  232 
  233      } else if(typ == 2) {
  234        /*  @abbr{...}, @code{...}, @file{...}, @i{...}, @option{...}, @r{...},
  235              @ref{...}, @samp{...},@var{...}, get mapped to ... .
  236        */
  237        rpt+= strlen(found);
  238        ret= Mx1_rewrap(m, &rpt, &wpt, line_out, found, "", "", flag & 1);
  239        if(ret <= 0)
  240          return(ret);
  241 
  242      } else if(typ == 3) {
  243        /* @ref{...}, @xref{...} get mapped to empty text.
  244        */
  245        rpt+= strlen(found);
  246        ret= Mx1_rewrap(m, &rpt, &wpt, line_out, found , "", "",
  247                        (flag & 1) | 2);
  248        if(ret <= 0)
  249          return(ret);
  250 
  251      } else if(strncmp(rpt, "@email{", 7) == 0 && !(flag & 1)) {
  252        /* @email{...} gets mapped to <...> . */
  253        rpt+= 7;
  254        ret= Mx1_rewrap(m, &rpt, &wpt, line_out, "@email{", "<", ">", 0);
  255        if(ret <= 0)
  256          return(ret);
  257 
  258      } else if(strncmp(rpt, "@minus{}", 8) == 0) {
  259        /* @minus{} will become "-". */
  260        if((wpt - line_out) + 1 > 255)
  261          goto overflow;
  262        *(wpt++)= '-';
  263        rpt+= 7;
  264 
  265      } else if(strncmp(rpt, "@@", 2) == 0 ||
  266                strncmp(rpt, "@{", 2) == 0 ||
  267                strncmp(rpt, "@}", 2) == 0) {
  268        /* @@ , @{, @} will get stripped of their first @. */
  269        if((wpt - line_out) + 1 > 255)
  270          goto overflow;
  271        *(wpt++)= *(rpt + 1);
  272        rpt++;
  273 
  274      } else {
  275        if((wpt - line_out) + 1 > 255)
  276          goto overflow;
  277        *(wpt++)= *(rpt);
  278 
  279      }
  280 
  281    } else if(*rpt == '\\') {
  282      /* "\" becomes "\\" */
  283      if((wpt - line_out) + 2 > 255)
  284        goto overflow;
  285      *(wpt++)= '\\';
  286      *(wpt++)= '\\';
  287 
  288    } else if((wpt - line_out) + 1 > 255) {
  289 overflow:;
  290      Mx1_report_error(m, "Line length overflow while text substitution", 0);
  291      return(-1);
  292    } else
  293      *(wpt++)= *rpt;
  294 
  295  }
  296  *wpt= 0;
  297  return(1);
  298 }
  299 
  300 
  301 /*
  302   @return 1= line_out is valid, 0= do not put out line_out, -1 = error
  303 */
  304 int Mx1_convert(struct Mx1 *m, char line_in[256], char line_out[256], int flag)
  305 {
  306  int l, num, keep= 0, ret, raw, i, backslash_count;
  307  char word[256], buf[256], *remainder, *wpt;
  308 
  309  m->count_in++;
  310  l= strlen(line_in);
  311 
  312  if(m->skipping > 0) {
  313    m->skipping--;
  314    return(0);
  315  }
  316 
  317  /* The first line gets discarded. */
  318  if(m->count_in == 1)
  319    return(0);
  320 
  321  /* Line start "@c man " will become "", the remainder is put out unaltered. */
  322  if(strncmp(line_in, "@c man ", 7) == 0) {
  323    strcpy(line_out, line_in + 7);
  324    m->count_out++;
  325    return(1);
  326  }
  327 
  328  /* Lines "@*" will be converted to ".br" */
  329  if(strcmp(line_in, "@*") == 0) {
  330    strcpy(line_out, ".br");
  331    m->count_out++;
  332    return(1);
  333  }
  334 
  335  /* @c man-ignore-lines N  will discard N following lines.
  336     "@c man-ignore-lines begin" discards all following lines
  337     up to "@c man-ignore-lines end".
  338  */
  339  if(strncmp(line_in, "@c man-ignore-lines ", 20) == 0) {
  340    if(strcmp(line_in + 20, "begin") == 0) {
  341      m->skipping--;
  342      return(0);
  343    } else if(strcmp(line_in + 20, "end") == 0) {
  344      if(m->skipping < 0)
  345        m->skipping++;
  346      return(0);
  347    } else if(m->skipping == 0) {
  348      num= 0;
  349      sscanf(line_in + 20, "%d", &num);
  350      if(num > 0) {
  351        m->skipping= num;
  352        return(0);
  353      }
  354    }
  355    Mx1_report_error(m, "Inappropriate use of '@c man-ignore-lines'", 0);
  356    return(-1);
  357  }
  358 
  359  /* Line blocks of "@menu" "@end menu" will be discarded. */
  360  if(strcmp(line_in, "@menu") == 0) {
  361    m->skipping--;
  362    return(0);
  363  }
  364  if(strcmp(line_in, "@end menu") == 0) {
  365    if(m->skipping < 0)
  366       m->skipping++;
  367    return(0);
  368  }
  369  if(m->skipping)
  370    return(0);
  371 
  372  /* "@item -word words" becomes "\fB\-word\fR words". */
  373  /* "@item word words" becomes "\fBword\fR words". */
  374  if(strncmp(line_in, "@item ", 6) == 0) {
  375    ret= Mx1__get_word(line_in + 6, word, &remainder, 0);
  376    if(ret <= 0) {
  377      Mx1_report_error(m, "Found no word after @item", 0);
  378      return(0);
  379    }
  380    strcpy(buf, "\\fB");
  381    if(word[0] == '-') {
  382      if(l >= 255) {
  383 length_overflow:;
  384        Mx1_report_error(m, "Line length overflow while converting @item", 0);
  385        return(-1);
  386      }
  387      strcat(buf, "\\");
  388    }
  389 
  390    /* Substitute option text */
  391    raw= strlen(buf);
  392    strcat(buf, word);
  393    ret= Mx1_substitute(m, buf, line_out, raw, strlen(buf), 0);
  394    if(ret <= 0)
  395      return(-1);
  396 
  397    if(strlen(line_out) + 3 + strlen(remainder) > 255)
  398      goto length_overflow;
  399    strcpy(buf, line_out);
  400    strcat(buf, "\\fR");
  401    raw= strlen(buf);
  402    strcat(buf, remainder);
  403 
  404    /* Substitute arguments text */
  405    ret= Mx1_substitute(m, buf, line_out, raw, strlen(buf), 0);
  406    if(ret <= 0)
  407      return(-1);
  408    m->count_out++;
  409    return(1);
  410  }
  411 
  412  /* @strong{... } gets mapped to \fB...\fR . */
  413  /* @command{... } gets mapped to \fB...\fR . */
  414  /* @minus{} will become "-". */
  415  /* Mapped {...} content is subject to the rules except {...} mapping. */
  416  /* @@ , @{, @} will get stripped of their first @. */
  417  /* "\" becomes "\\" */
  418 
  419  if(line_in[0] != '@' ||
  420     Mx1_is_any_wrap(m, line_in, NULL, 0) > 0 ||
  421     strncmp(line_in, "@minus{}", 8) == 0 ||
  422     strncmp(line_in, "@@", 2) == 0 ||
  423     strncmp(line_in, "@{", 2) == 0 ||
  424     strncmp(line_in, "@}", 2) == 0 ) {
  425    keep= 1;
  426    ret= Mx1_substitute(m, line_in, line_out, 0, strlen(line_in), 0);
  427    if(ret <= 0)
  428      return(-1);
  429  }
  430 
  431  /* Other lines which begin by "@" will be discarded. */
  432  if(! keep) {
  433    if(line_in[0] == '@')
  434      return(0);
  435    strcpy(line_out, line_in);
  436  }
  437 
  438  /* "-" which are not preceded by an uneven number of "\"  will get
  439     prepended one "\".
  440  */
  441  l= strlen(line_out);
  442  backslash_count= 0;
  443  wpt= buf;
  444  for(i= 0; i < l; i++) {
  445    if(line_out[i] == '\\')
  446      backslash_count++;
  447    else if(line_out[i] == '-') {
  448      if(backslash_count % 2 == 0)
  449        *(wpt++)= '\\';
  450      backslash_count= 0;
  451    } else
  452      backslash_count= 0;
  453    *(wpt++)= line_out[i];
  454  }
  455  *wpt= 0;
  456  strcpy(line_out, buf);
  457  m->count_out++;
  458  return(1);
  459 }
  460 
  461 
  462 int main(int argc, char **argv)
  463 {
  464  int ret, l, as_filter= 0, i;
  465  char line_in[256], line_out[256], *got;
  466  static char name_in[1024]= {"xorriso/xorriso.texi"};
  467  static char name_out[1024]= {"xorriso/xorriso.1"};
  468  struct Mx1 m;
  469  FILE *fp_in= stdin, *fp_out= stdout;
  470  
  471  Mx1_init(&m, argv[0], 0);
  472 
  473  if(argc < 2) {
  474 usage:;
  475    fprintf(stderr, "usage:  %s -auto|-filter [-xorrisofs]\n", argv[0]);
  476    fprintf(stderr, "  -auto       xorriso/xorriso.texi -> xorriso/xorriso.1\n");
  477    fprintf(stderr, "  -filter     stdin                -> stdout\n");
  478    fprintf(stderr, "  -xorrisofs  process xorriso/xorrisofs.texi\n");
  479    fprintf(stderr, "  -xorrecord  process xorriso/xorrecord.texi\n");
  480    fprintf(stderr, "  -xorriso-tcltk  process xorriso/-xorriso-tcltk.texi\n");
  481    fprintf(stderr, "  -xorriso-dd-target  process xorriso/-xorriso-dd-target.texi\n");
  482    exit(2);
  483  }
  484  for(i= 1; i < argc; i++) {
  485    if(strcmp(argv[i], "-filter") == 0) {
  486      as_filter= 1;
  487    } else if(strcmp(argv[i], "-auto") == 0) {
  488      as_filter= 0;
  489    } else if(strcmp(argv[i], "-xorrisofs") == 0) {
  490      strcpy(name_in, "xorriso/xorrisofs.texi");
  491      strcpy(name_out, "xorriso/xorrisofs.1");
  492    } else if(strcmp(argv[i], "-xorrecord") == 0) {
  493      strcpy(name_in, "xorriso/xorrecord.texi");
  494      strcpy(name_out, "xorriso/xorrecord.1");
  495    } else if(strcmp(argv[i], "-xorriso-tcltk") == 0) {
  496      strcpy(name_in, "xorriso/xorriso-tcltk.texi");
  497      strcpy(name_out, "xorriso/xorriso-tcltk.1");
  498    } else if(strcmp(argv[i], "-xorriso-dd-target") == 0) {
  499      strcpy(name_in, "xorriso-dd-target/xorriso-dd-target.texi");
  500      strcpy(name_out, "xorriso-dd-target/xorriso-dd-target.1");
  501    } else {
  502      fprintf(stderr, "%s : unknown option %s\n", argv[0], argv[i]);
  503      goto usage;
  504    }
  505  }
  506 
  507  if(!as_filter) {
  508    fp_in= fopen(name_in, "r");
  509    if(fp_in == NULL) {
  510      fprintf(stderr, "%s : failed to fopen( %s ,r) : %d %s\n",
  511              argv[0], name_in, errno, strerror(errno));
  512      exit(3);
  513    }
  514    fp_out= fopen(name_out, "w");
  515    if(fp_out == NULL) {
  516      fprintf(stderr, "%s : failed to fopen( %s ,w) : %d %s\n",
  517              argv[0], name_out, errno, strerror(errno));
  518      exit(4);
  519    }
  520  }
  521  while(1) {
  522    got= fgets(line_in, sizeof(line_in), fp_in);
  523    if(got == NULL)
  524  break;
  525    l= strlen(line_in);
  526    while(l > 0) {
  527      if(line_in[l - 1] == '\r' || line_in[l - 1] == '\n') {
  528        line_in[l - 1] = 0;
  529        l--;
  530      } else
  531    break;
  532    }
  533    ret= Mx1_convert(&m, line_in, line_out, 0);
  534    if(ret < 0)
  535      exit(1);
  536    if(ret == 0)
  537  continue;
  538    fprintf(fp_out, "%s\n", line_out);
  539  }
  540  exit(0);
  541 }