"Fossies" - the Fresh Open Source Software Archive

Member "libisoburn-1.5.6/xorriso/make_xorriso_1.c" (19 Oct 2022, 13658 Bytes) of package /linux/misc/libisoburn-1.5.6.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 latest Fossies "Diffs" side-by-side code changes report: 1.5.4_vs_1.5.6.

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