home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / modules / libpref / l10n / resources.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  9.2 KB  |  374 lines

  1. /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
  2.  *
  3.  * The contents of this file are subject to the Netscape Public License
  4.  * Version 1.0 (the "NPL"); you may not use this file except in
  5.  * compliance with the NPL.  You may obtain a copy of the NPL at
  6.  * http://www.mozilla.org/NPL/
  7.  *
  8.  * Software distributed under the NPL is distributed on an "AS IS" basis,
  9.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
  10.  * for the specific language governing rights and limitations under the
  11.  * NPL.
  12.  *
  13.  * The Initial Developer of this code under the NPL is Netscape
  14.  * Communications Corporation.  Portions created by Netscape are
  15.  * Copyright (C) 1998 Netscape Communications Corporation.  All Rights
  16.  * Reserved.
  17.  */
  18.  
  19. /**********************************************************************
  20.  resources.c
  21.  
  22.  Turns a Netscape.ad file into a netscape.cfg file so we can do
  23.  l10n for Dogbert.
  24.  
  25.  Usage:
  26.         resources Netscape.ad
  27. **********************************************************************/
  28.  
  29. #include <stdio.h>
  30. #include <errno.h>
  31. #include <string.h>
  32.  
  33. #include <sechash.h>
  34.  
  35.  
  36. #define OUT_FILE_NAME "netscape.cfg"
  37. #define COUNT(a) (sizeof(a)/sizeof((a)[0]))
  38.  
  39.  
  40. struct mapping {
  41.     char* resource;
  42.     char* pref_name;
  43.     char* verb;
  44.     char* value;
  45.     char mnemonic;
  46. };
  47.  
  48.  
  49. struct mapping map[] = {
  50.  
  51.     /* Help Menu labels */
  52.     {"*menuBar*manual", "menu.help.item_0.label"},
  53.     {"*menuBar*relnotes", "menu.help.item_1.label"},
  54.     {"*menuBar*productInfo", "menu.help.item_2.label"},
  55.     {"*menuBar*upgrade", "menu.help.item_4.label"},
  56.     {"*menuBar*registration", "menu.help.item_5.label"},
  57.     {"*menuBar*feedback", "menu.help.item_6.label"},
  58.     {"*menuBar*intl", "menu.help.item_8.label"},
  59.     {"*menuBar*aboutSecurity", "menu.help.item_9.label"},
  60.     {"*menuBar*aboutUsenet", "menu.help.item_10.label"},
  61.     {"*menuBar*aboutplugins", "menu.help.item_12.label"},
  62.     {"*menuBar*aboutfonts", "menu.help.item_13.label"},
  63.  
  64.     /* Help Menu URLs */
  65.     {"*url*manual", "menu.help.item_0.url"},
  66.     {"*url*relnotes", "menu.help.item_1.url"},
  67.     {"*url*productInfo", "menu.help.item_2.url"},
  68.     {"*url*feedback", "menu.help.item_4.url"},
  69.     {"*url*registration", "menu.help.item_5.url"},
  70.     {"*url*upgrade", "menu.help.item_6.url"},
  71.     {"*url*intl", "menu.help.item_8.url"},
  72.     {"*url*aboutSecurity", "menu.help.item_9.url"},
  73.     {"*url*aboutUsenet", "menu.help.item_10.url"},
  74.     {"*url*aboutplugins", "menu.help.item_12.url"},
  75.     {"*url*aboutfonts", "menu.help.item_13.url"},
  76.  
  77.     /* Communicator/Bookmarks/Guide labels */
  78.     {"*menuBar*inetIndex", "menu.places.item_0.label"},
  79.     {"*menuBar*inetWhite", "menu.places.item_1.label"},
  80.     {"*menuBar*inetYellow", "menu.places.item_2.label"},
  81.     {"*menuBar*whatsNew", "menu.places.item_3.label"},
  82.     {"*menuBar*whatsCool", "menu.places.item_4.label"},
  83.  
  84.     /* Communicator/Bookmarks/Guide URLs */
  85.     {"*url.about", "menu.places.item_0.url"},
  86.     {"*url.white", "menu.places.item_1.url"},
  87.     {"*url.yellow", "menu.places.item_2.url"},
  88.     {"*url.whats_new", "menu.places.item_3.url"},
  89.     {"*url.whats_cool", "menu.places.item_4.url"},
  90.  
  91.     /* Toolbar labels */
  92.     {"*toolBar*destinations*inetIndex", "toolbar.places.item_0.label"},
  93.     {"*toolBar*destinations*inetWhite", "toolbar.places.item_1.label"},
  94.     {"*toolBar*destinations*inetYellow", "toolbar.places.item_2.label"},
  95.     {"*toolBar*destinations*whatsNew", "toolbar.places.item_3.label"},
  96.     {"*toolBar*destinations*whatsCool", "toolbar.places.item_4.label"},
  97.  
  98.     /* Toolbar URLs */
  99.     {"*url.about", "toolbar.places.item_0.url"},
  100.     {"*url.white", "toolbar.places.item_1.url"},
  101.     {"*url.yellow", "toolbar.places.item_2.url"},
  102.     {"*url.whats_new", "toolbar.places.item_3.url"},
  103.     {"*url.whats_cool", "toolbar.places.item_4.url"},
  104.  
  105.     /* i18n stuff */
  106.     {"*url.netscape", "browser.startup.homepage", "pref"},
  107.     {"*versionLocale", "intl.accept_languages", "pref"},
  108.  
  109.     /* Personal Toolbar labels */
  110.     /* Personal Toolbar URLs */
  111. };
  112.  
  113.  
  114. char output_buf[10 * 1024];
  115.  
  116.  
  117. /*
  118.  * usage
  119.  */
  120. void
  121. usage(char* prog_name)
  122. {
  123.     fprintf(stderr, "Usage: %s filename\n", prog_name);
  124. }
  125.  
  126.  
  127. /*
  128.  * append_to_file
  129.  */
  130. void
  131. append_to_file(char buf[])
  132. {
  133.     strcat(output_buf, buf);
  134. }
  135.  
  136.  
  137. /*
  138.  * get_value
  139.  */
  140. char*
  141. get_value(char buf[])
  142. {
  143.     char* ptr = strchr(buf, ':') + 1;
  144.     while ( isspace(*ptr) ) ptr++;
  145.     return ptr;
  146. }
  147.  
  148.  
  149. /*
  150.  * is_label
  151.  */
  152. int
  153. is_label(char buf[])
  154. {
  155.     return ( strstr(buf, ".labelString:") != NULL );
  156. }
  157.  
  158.  
  159. /*
  160.  * is_mnemonic
  161.  */
  162. int
  163. is_mnemonic(char buf[])
  164. {
  165.     return ( strstr(buf, ".mnemonic:") != NULL );
  166. }
  167.  
  168.  
  169. /*
  170.  * is_url
  171.  */
  172. int
  173. is_url(char buf[])
  174. {
  175.     return ( !strncmp(buf, "*url.", 5) );
  176. }
  177.  
  178.  
  179. /*
  180.  * process_line
  181.  */
  182. void
  183. process_line(char buf[])
  184. {
  185.     int i;
  186.  
  187.     for ( i = 0; i < COUNT(map); i++ ) {
  188.         if ( !strncmp(map[i].resource, buf, strlen(map[i].resource)) ) {
  189.             if ( is_label(buf) ) {
  190.                 map[i].value = strdup(get_value(buf));
  191.             } else
  192.             if ( is_mnemonic(buf) ) {
  193.                 map[i].mnemonic = *get_value(buf);
  194.             } else {
  195.                 map[i].value = strdup(get_value(buf));
  196.             }
  197.         }
  198.     }
  199. }
  200.  
  201.  
  202. /*
  203.  * obscure
  204.  */
  205. static void
  206. obscure(char* buf)
  207. {
  208.     int i;
  209.     static int offset = 0;
  210.     int len = buf ? strlen(buf) : 0;
  211.  
  212.     for ( i = 0; i < len; i++, offset++ ) {
  213.         buf[i]+= 7;
  214.     }
  215. }
  216.  
  217.  
  218. /*
  219.  * write_hash
  220.  */
  221. static void
  222. write_hash(FILE* file)
  223. {
  224.     char buf[256];
  225.     unsigned char digest[16];
  226.     unsigned char magic_key[] = "VonGloda5652TX75235ISBN";
  227.     int len;
  228.  
  229.     MD5Context* md5_cxt = MD5_NewContext();
  230.     MD5_Begin(md5_cxt);
  231.  
  232.     /* start with the magic key */
  233.     MD5_Update(md5_cxt, magic_key, sizeof(magic_key));
  234.  
  235.     MD5_Update(md5_cxt, output_buf, strlen(output_buf));
  236.  
  237.     MD5_End(md5_cxt, digest, &len, 16);
  238.  
  239.     MD5_DestroyContext(md5_cxt, PR_TRUE);
  240.     sprintf(buf, "// %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x\n",
  241.             digest[0], digest[1], digest[2], digest[3],
  242.             digest[4], digest[5], digest[6], digest[7],
  243.             digest[8], digest[9], digest[10], digest[11],
  244.             digest[12], digest[13], digest[14], digest[15]);
  245.  
  246.     obscure(buf);
  247.  
  248.     if ( fwrite(buf, sizeof(char), strlen(buf), file) != strlen(buf) ) {
  249.         perror("fwrite");
  250.         exit(errno);
  251.     }
  252. }
  253.  
  254.  
  255. /*
  256.  * output_file
  257.  */
  258. void
  259. output_file(FILE* out)
  260. {
  261.     int i, j, k;
  262.     int len;
  263.     char buf[256];
  264.  
  265.     append_to_file("with ( PrefConfig ) {\n");
  266.  
  267.     for ( i = 0; i < COUNT(map); i++ ) {
  268.         if ( map[i].value ) {
  269.             if ( !is_url(map[i].resource) ) {
  270.                 if ( strchr(map[i].value, '&') ) {
  271.                     for ( j = 0, k = 0; j < strlen(map[i].value); j++, k++ ) { 
  272.                         buf[k] = map[i].value[j];
  273.                         if ( buf[k] == '&' ) buf[++k] = '&';
  274.                     }
  275.                     free(map[i].value);
  276.                     buf[k] = '\0';
  277.                     map[i].value = strdup(buf);
  278.                 }
  279.                 if ( map[i].mnemonic ) {
  280.                     for ( j = 0, k = 0; j < strlen(map[i].value); j++, k++ ) { 
  281.                         buf[k] = map[i].value[j];
  282.                         if ( buf[k] == map[i].mnemonic ) {
  283.                             buf[k++] = '&';
  284.                             buf[k] = map[i].mnemonic;
  285.                             map[i].mnemonic = '\0';
  286.                         }
  287.                     }
  288.                     free(map[i].value);
  289.                     buf[k] = '\0';
  290.                     map[i].value = strdup(buf);
  291.                 }
  292.             }
  293.             sprintf(buf, "%s(\"%s\", \"%s\");\n", 
  294.                     map[i].verb ? map[i].verb : "config", 
  295.                     map[i].pref_name, 
  296.                     map[i].value);
  297.             append_to_file(buf);
  298.         }
  299.     }
  300.  
  301.     append_to_file("}\n");
  302.     write_hash(out);
  303.     len = strlen(output_buf);
  304.     obscure(output_buf);
  305.     fwrite(output_buf, sizeof(char), len, out);
  306. }
  307.  
  308.  
  309. /*
  310.  * fatal
  311.  */
  312. void
  313. fatal(char* progname, int error)
  314. {
  315.     fprintf(stderr, "%s failed\n", progname);
  316.     exit(error);
  317. }
  318.  
  319.  
  320. /*
  321.  * main
  322.  */
  323. int
  324. main(int argc, char* argv[])
  325. {
  326.     FILE* in;
  327.     FILE* out;
  328.     char buf[64 * 1024];
  329.     int line = 0;
  330.  
  331.     if ( argc != 2 ) {
  332.         usage(argv[0]);
  333.         fatal(argv[0], 1);
  334.     }
  335.  
  336.     if ( (in = fopen(argv[1], "r")) == NULL ) {
  337.         perror(argv[1]);
  338.         fatal(argv[0], errno);
  339.     }
  340.  
  341.     if ( (out = fopen(OUT_FILE_NAME, "w")) == NULL ) {
  342.         perror(OUT_FILE_NAME);
  343.         fatal(argv[0], errno);
  344.     }
  345.  
  346.     while ( fgets(buf, sizeof(buf), in) != NULL ) {
  347.         line++;
  348.         if ( strchr(buf, '\n') == NULL ) {
  349.             fprintf(stderr, "%s, line %d: Line too long\n", argv[1], line);
  350.             fatal(argv[0], 1);
  351.         }
  352.         *(char*) strchr(buf, '\n') = '\0';
  353.         process_line(buf);
  354.     }
  355.  
  356.     output_file(out);
  357.  
  358.     if ( fclose(in) != 0 ) {
  359.         perror(argv[1]);
  360.         fatal(argv[0], errno);
  361.     }
  362.  
  363.     if ( fclose(out) != 0 ) {
  364.         perror(OUT_FILE_NAME);
  365.         fatal(argv[0], errno);
  366.     }
  367.  
  368.     fprintf(stderr, "Created " OUT_FILE_NAME "\n");
  369.  
  370.     return 0;
  371. }
  372.  
  373.  
  374.