Ticket #1755: rtems-bin2c.patch

File rtems-bin2c.patch, 3.0 KB (added by dufault, on 03/06/11 at 11:09:22)

Patch to give acceptable guard define for path names

  • tools/build/rtems-bin2c.c

    diff -r 84d22a9725b7 tools/build/rtems-bin2c.c
    a b  
    5252  return c;
    5353}
    5454
     55/* Turn a path name into something acceptable for a pre-processor define.
     56 * This isn't perfect, it just catches '/', '.', and '-'.
     57 */
     58char *preprocessor_guard(const char *path_name, char *out, size_t outlen)
     59{
     60  int c, i, o;
     61  if (outlen <= 3) {
     62    return 0;
     63  }
     64  out[0] = '_';
     65  out[1] = '_';
     66  for (i = 0, o = 2; o < (outlen - 1) && (c = path_name[i]); i++, o++) {
     67    switch(c) {
     68      case '/':
     69      case '.':
     70      case '-':
     71      out[o] = '_';
     72      break;
     73
     74      default:
     75      out[o] = c;
     76      break;
     77    }
     78  }
     79  out[o] = 0;
     80  return out;
     81}
     82
    5583void process(const char *ifname, const char *ofname)
    5684{
    5785  FILE *ifile, *ocfile, *ohfile;
    5886  char buf[PATH_MAX], *p;
     87  char guard[PATH_MAX];
    5988  char obasename[PATH_MAX];
    6089  char ocname[PATH_MAX];
    6190  char ohname[PATH_MAX];
     
    176205  /*****************************************************************/
    177206
    178207  if ( createH ) {
    179   /* print H file header */
    180   fprintf(
    181     ohfile,
    182     "/*\n"
    183     " *  Extern declarations for C structure representing binary file %s\n"
    184     " *\n"
    185     " *  WARNING: Automatically generated -- do not edit!\n"
    186     " */\n"
    187     "\n"
    188     "#ifndef __%s_h\n"
    189     "#define __%s_h\n"
    190     "\n"
    191     "#include <sys/types.h>\n"
    192     "\n",
    193     ifbasename,  /* header */
    194     obasename,  /* ifndef */
    195     obasename   /* define */
    196   );
     208    preprocessor_guard(ohname, guard, sizeof(guard));
     209    /* print H file header */
     210    fprintf(
     211      ohfile,
     212      "/*\n"
     213      " *  Extern declarations for C structure representing binary file %s\n"
     214      " *\n"
     215      " *  WARNING: Automatically generated by rtems-bin2c -- do not edit!\n"
     216      " */\n"
     217      "\n"
     218      "#ifndef %s\n"
     219      "#define %s\n"
     220      "\n"
     221      "#include <sys/types.h>\n"
     222      "\n",
     223      ifbasename,  /* header */
     224      guard,  /* ifndef */
     225      guard   /* define */
     226    );
    197227
    198   /* print structure */
    199   fprintf(
    200     ohfile,
    201     "extern %s%sunsigned char %s[];",
    202     ((usestatic) ? "static " : ""),
    203     ((useconst) ? "const " : ""),
    204     buf
    205   );
    206   /* print sizeof */
    207   fprintf(
    208     ohfile,
    209     "\n"
    210     "extern %s%ssize_t %s_size;\n",
    211     ((usestatic) ? "static " : ""),
    212     ((useconst) ? "const " : ""),
    213     buf
    214   );
     228    /* print structure */
     229    fprintf(
     230      ohfile,
     231      "extern %s%sunsigned char %s[];",
     232      ((usestatic) ? "static " : ""),
     233      ((useconst) ? "const " : ""),
     234      buf
     235    );
     236    /* print sizeof */
     237    fprintf(
     238      ohfile,
     239      "\n"
     240      "extern %s%ssize_t %s_size;\n",
     241      ((usestatic) ? "static " : ""),
     242      ((useconst) ? "const " : ""),
     243      buf
     244    );
    215245
    216   fprintf(
    217     ohfile,
    218     "\n"
    219     "#endif\n"
    220   );
     246    fprintf(
     247      ohfile,
     248      "\n"
     249      "#endif\n"
     250    );
    221251  } /* createH */
    222252 
    223253  /*****************************************************************/