home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / cmd / xfe / e_kit_parser.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  27.4 KB  |  946 lines

  1. /* -*- Mode: C; tab-width: 8; 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.  e_kit_parser.c
  20.  By Daniel Malmer 
  21. **********************************************************************/
  22.  
  23. #include <stdio.h>
  24. #include <stdlib.h>
  25. #include <string.h>
  26. #include <ctype.h>
  27.  
  28. #include <errno.h>
  29. #include <assert.h>
  30.  
  31. #include "xp_md5.h"
  32. #include "sec.h"
  33.  
  34. #define OUTPUT_FILENAME "Netscape.lock"
  35.  
  36. static char buf[BUFSIZ];
  37.  
  38. int verbose = 0;
  39.  
  40. int fatal_errors = 0;
  41. int lineno;
  42.  
  43. #define CHUNKSIZE 64;
  44.  
  45. char* output_buf = NULL;
  46. int output_max_size = 0;
  47.  
  48. struct mapping;
  49.  
  50. typedef int  validation_func(char*);
  51. typedef void processing_func(struct mapping*, char*);
  52.  
  53. struct mapping {
  54.     char* token;
  55.     char* resource;
  56.     validation_func* validate;
  57.     processing_func* process;
  58.     char* desens[5];
  59. };
  60.  
  61. extern void error(char*);
  62. extern void warn(char*);
  63.  
  64. extern void default_handler(struct mapping*, char*);
  65. extern void boolean_handler(struct mapping*, char*);
  66. extern void menu_handler(struct mapping*, char*);
  67. extern void proxy_handler(struct mapping*, char*);
  68. extern void windoze_handler(struct mapping*, char*);
  69. extern void mac_handler(struct mapping*, char*);
  70.  
  71. extern int boolean_validate(char*);
  72. extern int number_validate(char*);
  73. extern int user_agent_validate(char*);
  74. extern int proxy_type_validate(char*);
  75.  
  76. extern void append_to_output_file(char*);
  77. extern int matches(char* str1, char* str2);
  78.  
  79. struct proxy {
  80.   char* name;
  81.   char* resource;
  82.   char* host;
  83.   char* port;
  84. };
  85.  
  86. struct proxy proxies[] = {
  87.   {"ftp_proxy",    "ftp",    NULL, NULL},
  88.   {"gopher_proxy", "gopher", NULL, NULL},
  89.   {"http_proxy",   "http",   NULL, NULL},
  90.   {"wais_proxy",   "wais",   NULL, NULL},
  91.   {"https_proxy",  "https",  NULL, NULL},
  92. };
  93.  
  94. #define NUM_PROXIES (sizeof(proxies)/sizeof(proxies[0]))
  95.  
  96. struct mapping mappings[] = {
  97. {"logo button url", ".logoButtonUrl", NULL, default_handler, {NULL}},
  98. {"home page", ".homePage", NULL, default_handler, 
  99.   {"*general_prefs*startupFrame*startupBox.home",
  100.    "*general_prefs*startupFrame*startupBox.blank",
  101.    "*general_prefs*startupFrame*startupBox.homeText",
  102.   }
  103. },
  104. {"nntp_server", ".nntpServer", NULL, default_handler, 
  105.     {"*mailnews_prefs*newsFrame*newshostText"}
  106. },
  107. {"smtp_server", ".smtpServer", NULL, default_handler,
  108.     {"*mailnews_prefs*outMailFrame*smtpText"}
  109. },
  110. {"pop_server", ".popServer", NULL, default_handler,
  111.     {"*mailnews_prefs*inMailFrame*srvrText"}
  112. },
  113. {"autoload home page", ".autoloadHomePage", boolean_validate, default_handler,
  114.  {"*general_prefs*startupFrame*startupBox.home",
  115.   "*general_prefs*startupFrame*startupBox.blank"
  116.  }
  117. },
  118. {"leave on server", ".leaveOnServer", boolean_validate, boolean_handler, 
  119.     {"*mailnews_prefs*inMailFrame*msgRemove",
  120.      "*mailnews_prefs*inMailFrame*msgLeave"
  121.     }
  122. },
  123. {"proxy type", ".proxyMode", proxy_type_validate, default_handler,
  124.     {"*network_prefs*proxiesFrame*manualToggle",
  125.      "*network_prefs*proxiesFrame*autoToggle",
  126.      "*network_prefs*proxiesFrame*noProxiesToggle"},
  127. },
  128. {"ftp_proxy", ".ftpProxy", NULL, proxy_handler, {NULL}},
  129. {"gopher_proxy", ".gopherProxy", NULL, proxy_handler, {NULL}},
  130. {"http_proxy", ".httpProxy", NULL, proxy_handler, {NULL}},
  131. {"wais_proxy", ".waisProxy", NULL, proxy_handler, {NULL}},
  132. {"https_proxy", ".httpsProxy", NULL, proxy_handler, {NULL}},
  133. {"ftp_proxyport", ".ftpProxyPort", number_validate, proxy_handler, {NULL}},
  134. {"gopher_proxyport", ".gopherProxyPort", number_validate, proxy_handler, {NULL}},
  135. {"http_proxyport", ".httpProxyPort", number_validate, proxy_handler, {NULL}},
  136. {"news_proxyport", ".newsProxyPort", number_validate, proxy_handler, {NULL}},
  137. {"wais_proxyport", ".waisProxyPort", number_validate, proxy_handler, {NULL}},
  138. {"https_proxyport", ".httpsProxyPort", number_validate, proxy_handler, {NULL}},
  139. {"socks_server", ".socksServer", NULL, default_handler, 
  140.     {"*proxiesFrame*socksText"}
  141. },
  142. {"socks_serverport", ".socksServerPort", number_validate, default_handler,
  143.     {"*proxiesFrame*socksPort"}
  144. },
  145. {"no_proxy", ".noProxy", NULL, default_handler, 
  146.     {"*proxiesFrame*noText"}
  147. },
  148. {"auto config url", ".autoconfigUrl", NULL, default_handler, 
  149.     {"*network_prefs*proxiesFrame*locationText"}
  150. },
  151. {"user agent", ".userAgent", user_agent_validate, default_handler, {NULL}},
  152. {"x animation file", ".animationFile", NULL, default_handler, {NULL}},
  153. {"button 1", "*urlBar*inetIndex.labelString", NULL, default_handler, {NULL}},
  154. {"button 2", "*urlBar*inetWhite.labelString", NULL, default_handler, {NULL}},
  155. {"button 3", "*urlBar*inetYellow.labelString", NULL, default_handler, {NULL}},
  156. {"button 4", "*urlBar*whatsNew.labelString", NULL, default_handler, {NULL}},
  157. {"button 5", "*urlBar*whatsCool.labelString", NULL, default_handler, {NULL}},
  158. {"button 1 url", ".inetIndexBUrl", NULL, default_handler, {NULL}},
  159. {"button 2 url", ".inetWhiteBUrl", NULL, default_handler, {NULL}},
  160. {"button 3 url", ".inetYellowBUrl", NULL, default_handler, {NULL}},
  161. {"button 4 url", ".whatsNewBUrl", NULL, default_handler, {NULL}},
  162. {"button 5 url", ".whatsCoolBUrl", NULL, default_handler, {NULL}},
  163. {"directory 1", "*menuBar*inetIndex", NULL, menu_handler, {NULL}},
  164. {"directory 2", "*menuBar*inetWhite", NULL, menu_handler, {NULL}},
  165. {"directory 3", "*menuBar*inetSearch", NULL, menu_handler, {NULL}},
  166. {"directory 4", "*menuBar*whatsNew", NULL, menu_handler, {NULL}},
  167. {"directory 5", "*menuBar*whatsCool", NULL, menu_handler, {NULL}},
  168. {"directory 6", "*menuBar*directoryMenu6", NULL, menu_handler, {NULL}},
  169. {"directory 7", "*menuBar*directoryMenu7", NULL, menu_handler, {NULL}},
  170. {"directory 8", "*menuBar*directoryMenu8", NULL, menu_handler, {NULL}},
  171. {"directory 9", "*menuBar*directoryMenu9", NULL, menu_handler, {NULL}},
  172. {"directory 10", "*menuBar*directoryMenu10", NULL, menu_handler, {NULL}},
  173. {"directory 11", "*menuBar*directoryMenu11", NULL, menu_handler, {NULL}},
  174. {"directory 12", "*menuBar*directoryMenu12", NULL, menu_handler, {NULL}},
  175. {"directory 13", "*menuBar*directoryMenu13", NULL, menu_handler, {NULL}},
  176. {"directory 14", "*menuBar*directoryMenu14", NULL, menu_handler, {NULL}},
  177. {"directory 15", "*menuBar*directoryMenu15", NULL, menu_handler, {NULL}},
  178. {"directory 16", "*menuBar*directoryMenu16", NULL, menu_handler, {NULL}},
  179. {"directory 17", "*menuBar*directoryMenu17", NULL, menu_handler, {NULL}},
  180. {"directory 18", "*menuBar*directoryMenu18", NULL, menu_handler, {NULL}},
  181. {"directory 19", "*menuBar*directoryMenu19", NULL, menu_handler, {NULL}},
  182. {"directory 20", "*menuBar*directoryMenu20", NULL, menu_handler, {NULL}},
  183. {"directory 21", "*menuBar*directoryMenu21", NULL, menu_handler, {NULL}},
  184. {"directory 22", "*menuBar*directoryMenu22", NULL, menu_handler, {NULL}},
  185. {"directory 23", "*menuBar*directoryMenu23", NULL, menu_handler, {NULL}},
  186. {"directory 24", "*menuBar*directoryMenu24", NULL, menu_handler, {NULL}},
  187. {"directory 25", "*menuBar*directoryMenu25", NULL, menu_handler, {NULL}},
  188. {"directory 1 url", ".netscapeUrl", NULL, default_handler, {NULL}},
  189. {"directory 2 url", ".whatsNewUrl", NULL, default_handler, {NULL}},
  190. {"directory 3 url", ".whatsCoolUrl", NULL, default_handler, {NULL}},
  191. {"directory 4 url", ".directoryMenu4Url", NULL, default_handler, {NULL}},
  192. {"directory 5 url", ".galleriaUrl", NULL, default_handler, {NULL}},
  193. {"directory 6 url", ".inetIndexUrl", NULL, default_handler, {NULL}},
  194. {"directory 7 url", ".inetSearchUrl", NULL, default_handler, {NULL}},
  195. {"directory 8 url", ".inetWhiteUrl", NULL, default_handler, {NULL}},
  196. {"directory 9 url", ".inetAboutUrl", NULL, default_handler, {NULL}},
  197. {"directory 10 url", ".directoryMenu10Url", NULL, default_handler, {NULL}},
  198. {"directory 11 url", ".directoryMenu11Url", NULL, default_handler, {NULL}},
  199. {"directory 12 url", ".directoryMenu12Url", NULL, default_handler, {NULL}},
  200. {"directory 13 url", ".directoryMenu13Url", NULL, default_handler, {NULL}},
  201. {"directory 14 url", ".directoryMenu14Url", NULL, default_handler, {NULL}},
  202. {"directory 15 url", ".directoryMenu15Url", NULL, default_handler, {NULL}},
  203. {"directory 16 url", ".directoryMenu16Url", NULL, default_handler, {NULL}},
  204. {"directory 17 url", ".directoryMenu17Url", NULL, default_handler, {NULL}},
  205. {"directory 18 url", ".directoryMenu18Url", NULL, default_handler, {NULL}},
  206. {"directory 19 url", ".directoryMenu19Url", NULL, default_handler, {NULL}},
  207. {"directory 20 url", ".directoryMenu20Url", NULL, default_handler, {NULL}},
  208. {"directory 21 url", ".directoryMenu21Url", NULL, default_handler, {NULL}},
  209. {"directory 22 url", ".directoryMenu22Url", NULL, default_handler, {NULL}},
  210. {"directory 23 url", ".directoryMenu23Url", NULL, default_handler, {NULL}},
  211. {"directory 24 url", ".directoryMenu24Url", NULL, default_handler, {NULL}},
  212. {"directory 25 url", ".directoryMenu25Url", NULL, default_handler, {NULL}},
  213. {"directory 1 prompt", NULL, NULL, windoze_handler, {NULL}},
  214. {"directory 2 prompt", NULL, NULL, windoze_handler, {NULL}},
  215. {"directory 3 prompt", NULL, NULL, windoze_handler, {NULL}},
  216. {"directory 4 prompt", NULL, NULL, windoze_handler, {NULL}},
  217. {"directory 5 prompt", NULL, NULL, windoze_handler, {NULL}},
  218. {"directory 6 prompt", NULL, NULL, windoze_handler, {NULL}},
  219. {"directory 7 prompt", NULL, NULL, windoze_handler, {NULL}},
  220. {"directory 8 prompt", NULL, NULL, windoze_handler, {NULL}},
  221. {"directory 9 prompt", NULL, NULL, windoze_handler, {NULL}},
  222. {"directory 10 prompt", NULL, NULL, windoze_handler, {NULL}},
  223. {"directory 11 prompt", NULL, NULL, windoze_handler, {NULL}},
  224. {"directory 12 prompt", NULL, NULL, windoze_handler, {NULL}},
  225. {"directory 13 prompt", NULL, NULL, windoze_handler, {NULL}},
  226. {"directory 14 prompt", NULL, NULL, windoze_handler, {NULL}},
  227. {"directory 15 prompt", NULL, NULL, windoze_handler, {NULL}},
  228. {"directory 16 prompt", NULL, NULL, windoze_handler, {NULL}},
  229. {"directory 17 prompt", NULL, NULL, windoze_handler, {NULL}},
  230. {"directory 18 prompt", NULL, NULL, windoze_handler, {NULL}},
  231. {"directory 19 prompt", NULL, NULL, windoze_handler, {NULL}},
  232. {"directory 20 prompt", NULL, NULL, windoze_handler, {NULL}},
  233. {"directory 21 prompt", NULL, NULL, windoze_handler, {NULL}},
  234. {"directory 22 prompt", NULL, NULL, windoze_handler, {NULL}},
  235. {"directory 23 prompt", NULL, NULL, windoze_handler, {NULL}},
  236. {"directory 24 prompt", NULL, NULL, windoze_handler, {NULL}},
  237. {"directory 25 prompt", NULL, NULL, windoze_handler, {NULL}},
  238. {"help 1", "*menuBar*manual", NULL, menu_handler, {NULL}},
  239. {"help 2", "*menuBar*relnotes", NULL, menu_handler, {NULL}},
  240. {"help 3", "*menuBar*productInfo", NULL, menu_handler, {NULL}},
  241. {"help 4", "*menuBar*feedback", NULL, menu_handler, {NULL}},
  242. {"help 5", "*menuBar*intl", NULL, menu_handler, {NULL}},
  243. {"help 6", "*menuBar*abourSecurity", NULL, menu_handler, {NULL}},
  244. {"help 7", "*menuBar*helpMenu7", NULL, menu_handler, {NULL}},
  245. {"help 8", "*menuBar*registration", NULL, menu_handler, {NULL}},
  246. {"help 9", "*menuBar*upgrade", NULL, menu_handler, {NULL}},
  247. {"help 10", "*menuBar*services", NULL, menu_handler, {NULL}},
  248. {"help 11", "*menuBar*aboutUsenet", NULL, menu_handler, {NULL}},
  249. {"help 12", "*menuBar*aboutPlugins", NULL, menu_handler, {NULL}},
  250. {"help 13", "*menuBar*helpMenu13", NULL, menu_handler, {NULL}},
  251. {"help 14", "*menuBar*helpMenu14", NULL, menu_handler, {NULL}},
  252. {"help 15", "*menuBar*helpMenu15", NULL, menu_handler, {NULL}},
  253. {"help 16", "*menuBar*helpMenu16", NULL, menu_handler, {NULL}},
  254. {"help 17", "*menuBar*helpMenu17", NULL, menu_handler, {NULL}},
  255. {"help 18", "*menuBar*helpMenu18", NULL, menu_handler, {NULL}},
  256. {"help 19", "*menuBar*helpMenu19", NULL, menu_handler, {NULL}},
  257. {"help 20", "*menuBar*helpMenu20", NULL, menu_handler, {NULL}},
  258. {"help 21", "*menuBar*helpMenu21", NULL, menu_handler, {NULL}},
  259. {"help 22", "*menuBar*helpMenu22", NULL, menu_handler, {NULL}},
  260. {"help 23", "*menuBar*helpMenu23", NULL, menu_handler, {NULL}},
  261. {"help 24", "*menuBar*helpMenu24", NULL, menu_handler, {NULL}},
  262. {"help 25", "*menuBar*helpMenu25", NULL, menu_handler, {NULL}},
  263. {"help 1 url", ".manualUrl", NULL, default_handler, {NULL}},
  264. {"help 2 url", ".relnotesUrl", NULL, default_handler, {NULL}},
  265. {"help 3 url", ".productInfoUrl", NULL, default_handler, {NULL}},
  266. {"help 4 url", ".feedbackUrl", NULL, default_handler, {NULL}},
  267. {"help 5 url", ".intlUrl", NULL, default_handler, {NULL}},
  268. {"help 6 url", ".aboutSecurityUrl", NULL, default_handler, {NULL}},
  269. {"help 7 url", ".helpMenu7Url", NULL, default_handler, {NULL}},
  270. {"help 8 url", ".registrationUrl", NULL, default_handler, {NULL}},
  271. {"help 9 url", ".upgradeUrl", NULL, default_handler, {NULL}},
  272. {"help 10 url", ".servicesUrl", NULL, default_handler, {NULL}},
  273. {"help 11 url", ".aboutUsenetUrl", NULL, default_handler, {NULL}},
  274. {"help 12 url", ".aboutpluginsUrl", NULL, default_handler, {NULL}},
  275. {"help 13 url", ".helpMenu13Url", NULL, default_handler, {NULL}},
  276. {"help 14 url", ".helpMenu14Url", NULL, default_handler, {NULL}},
  277. {"help 15 url", ".helpMenu15Url", NULL, default_handler, {NULL}},
  278. {"help 16 url", ".helpMenu16Url", NULL, default_handler, {NULL}},
  279. {"help 17 url", ".helpMenu17Url", NULL, default_handler, {NULL}},
  280. {"help 18 url", ".helpMenu18Url", NULL, default_handler, {NULL}},
  281. {"help 19 url", ".helpMenu19Url", NULL, default_handler, {NULL}},
  282. {"help 20 url", ".helpMenu20Url", NULL, default_handler, {NULL}},
  283. {"help 21 url", ".helpMenu21Url", NULL, default_handler, {NULL}},
  284. {"help 22 url", ".helpMenu22Url", NULL, default_handler, {NULL}},
  285. {"help 23 url", ".helpMenu23Url", NULL, default_handler, {NULL}},
  286. {"help 24 url", ".helpMenu24Url", NULL, default_handler, {NULL}},
  287. {"help 25 url", ".helpMenu25Url", NULL, default_handler, {NULL}},
  288. {"help 1 prompt", NULL, NULL, windoze_handler, {NULL}},
  289. {"help 2 prompt", NULL, NULL, windoze_handler, {NULL}},
  290. {"help 3 prompt", NULL, NULL, windoze_handler, {NULL}},
  291. {"help 4 prompt", NULL, NULL, windoze_handler, {NULL}},
  292. {"help 5 prompt", NULL, NULL, windoze_handler, {NULL}},
  293. {"help 6 prompt", NULL, NULL, windoze_handler, {NULL}},
  294. {"help 7 prompt", NULL, NULL, windoze_handler, {NULL}},
  295. {"help 8 prompt", NULL, NULL, windoze_handler, {NULL}},
  296. {"help 9 prompt", NULL, NULL, windoze_handler, {NULL}},
  297. {"help 10 prompt", NULL, NULL, windoze_handler, {NULL}},
  298. {"help 11 prompt", NULL, NULL, windoze_handler, {NULL}},
  299. {"help 12 prompt", NULL, NULL, windoze_handler, {NULL}},
  300. {"help 13 prompt", NULL, NULL, windoze_handler, {NULL}},
  301. {"help 14 prompt", NULL, NULL, windoze_handler, {NULL}},
  302. {"help 15 prompt", NULL, NULL, windoze_handler, {NULL}},
  303. {"help 16 prompt", NULL, NULL, windoze_handler, {NULL}},
  304. {"help 17 prompt", NULL, NULL, windoze_handler, {NULL}},
  305. {"help 18 prompt", NULL, NULL, windoze_handler, {NULL}},
  306. {"help 19 prompt", NULL, NULL, windoze_handler, {NULL}},
  307. {"help 20 prompt", NULL, NULL, windoze_handler, {NULL}},
  308. {"help 21 prompt", NULL, NULL, windoze_handler, {NULL}},
  309. {"help 22 prompt", NULL, NULL, windoze_handler, {NULL}},
  310. {"help 23 prompt", NULL, NULL, windoze_handler, {NULL}},
  311. {"help 24 prompt", NULL, NULL, windoze_handler, {NULL}},
  312. {"help 25 prompt", NULL, NULL, windoze_handler, {NULL}},
  313. };
  314.  
  315. #define NUM_MAPPINGS (sizeof(mappings)/sizeof(mappings[0]))
  316.  
  317.  
  318. /*
  319.  * boolean_validate
  320.  */
  321. int
  322. boolean_validate(char* arg)
  323. {
  324.     if ( !matches(arg, "yes")   &&
  325.          !matches(arg, "no")    &&
  326.          !matches(arg, "true")  &&
  327.          !matches(arg, "false") ) {
  328.         error("Valid values are 'yes', 'no', 'true' or 'false'.");
  329.         return 0;
  330.     }
  331.  
  332.     return 1;
  333. }
  334.  
  335.  
  336. /*
  337.  * number_validate
  338.  */
  339. int
  340. number_validate(char* arg)
  341. {
  342.     char* ptr;
  343.  
  344.     for ( ptr = arg; *ptr != '\0'; ptr++ ) {
  345.         if ( !isdigit(*ptr) ) {
  346.             error("Value must be a number.");
  347.             return 0;
  348.         }
  349.     }
  350.  
  351.     return 1;
  352. }
  353.  
  354.  
  355. /*
  356.  * proxy_type_validate
  357.  */
  358. int
  359. proxy_type_validate(char* arg)
  360. {
  361.     if ( matches(arg, "none") ||
  362.          matches(arg, "0") ) {
  363.         strcpy(arg, "0");
  364.         return 1;
  365.     }
  366.  
  367.     if ( matches(arg, "manual") ||
  368.          matches(arg, "1") ) {
  369.         strcpy(arg, "1");
  370.         return 1;
  371.     }
  372.  
  373.     if ( matches(arg, "automatic") ||
  374.          matches(arg, "2") ) {
  375.         strcpy(arg, "2");
  376.         return 1;
  377.     }
  378.  
  379.     error("Valid values are 'None', 'Manual', 'Automatic', or 0-2.");
  380.  
  381.     return 0;
  382. }
  383.  
  384.  
  385. /*
  386.  * user_agent_validate
  387.  */
  388. int
  389. user_agent_validate(char* arg)
  390. {
  391.     char* ptr;
  392.  
  393.     if ( strlen(arg) > 10 ) {
  394.         error("User Agent strings are limited to 10 characters or less.");
  395.         return 0;
  396.     }
  397.  
  398.     for ( ptr = arg; *ptr != '\0'; ptr++ ) {
  399.         if ( !isalnum(*ptr) && *ptr != '-' && *ptr != '_' ) {
  400.             error("User Agent strings consist of alphanumerics, '-', and '_'.");
  401.             return 0;
  402.         }
  403.     }
  404.  
  405.     return 1;
  406. }
  407.  
  408.  
  409. /*
  410.  * usage
  411.  */
  412. void
  413. usage(char* progname)
  414. {
  415.     fprintf(stderr, "Usage: %s filename\n", progname);
  416. }
  417.  
  418.  
  419. /*
  420.  * warn
  421.  */
  422. void
  423. warn(char* msg)
  424. {
  425.     fprintf(stderr, "Warning: Line %d: %s\n", lineno, msg);
  426. }
  427.  
  428.  
  429. /*
  430.  * error
  431.  */
  432. void
  433. error(char* msg)
  434. {
  435.     fatal_errors++;
  436.     fprintf(stderr, "Error: Line %d: %s\n", lineno, msg);
  437. }
  438.  
  439.  
  440. /*
  441.  * is_blank
  442.  * Returns True if the line has nothing on it except for whitespace.
  443.  */
  444. int
  445. is_blank(char* line)
  446. {
  447.     assert(line);
  448.  
  449.     while ( *line && isspace(*line) ) line++;
  450.  
  451.     return ( *line == '\0' );
  452. }
  453.  
  454.  
  455. /*
  456.  * is_comment
  457.  * Returns True if the line is a comment.
  458.  * Comments begin with the '#' character.
  459.  * Comments can only appear on lines by themselves.
  460.  */
  461. int
  462. is_comment(char* line)
  463. {
  464.     assert(line);
  465.  
  466.     while ( *line && isspace(*line) ) line++;
  467.  
  468.     return ( *line == '#' );
  469. }
  470.  
  471.  
  472. /*
  473.  * is_section
  474.  */
  475. int
  476. is_section(char* line)
  477. {
  478.     for ( ; isspace(*line); line++ ) ;
  479.  
  480.     if ( *line != '[' ) return 0;
  481.  
  482.     for ( ; *line != ']' && *line != '\0'; line++ ) ;
  483.  
  484.     if ( *line != ']' ) return 0;
  485.  
  486.     for ( line++; *line != '\0'; line++ ) if (!isspace(*line)) return 0;
  487.  
  488.     return 1;
  489. }
  490.  
  491.  
  492. /*
  493.  * get_left_side
  494.  */
  495. char*
  496. get_left_side(char* line)
  497. {
  498.     char* left_side;
  499.     char* ptr;
  500.  
  501.     assert(line);
  502.  
  503.     if ( !strchr(line, '=') ) {
  504.         error("Missing '='.");
  505.         return NULL;
  506.     }
  507.  
  508.     while ( *line && isspace(*line) ) line++;
  509.     
  510.     left_side = strdup(line);
  511.  
  512.     for ( ptr = strchr(left_side, '='); *ptr == '=' || isspace(*ptr); ptr-- ) {
  513.         *ptr = '\0';
  514.     }
  515.  
  516.     return left_side; 
  517. }
  518.  
  519.  
  520. /*
  521.  * get_right_side
  522.  */
  523. char*
  524. get_right_side(char* line)
  525. {
  526.     char* right_side;
  527.     char* ptr;
  528.  
  529.     assert(line);
  530.  
  531.     for ( ptr = strchr(line, '=') + 1; isspace(*ptr); ptr++ ) ;
  532.  
  533.     right_side = ptr;
  534.  
  535.     if ( right_side[0] != '\0' ) {
  536.         for ( ptr = strchr(right_side, '\0') - 1; isspace(*ptr); ptr-- ) {
  537.             *ptr = '\0';
  538.         }
  539.     }
  540.  
  541.     if ( right_side[0] == '"' || *ptr == '"' ) {
  542.         if ( right_side[0] == '"' && *ptr == '"' ) {
  543.             right_side++;
  544.             *ptr = '\0';
  545.         } else {
  546.             error("Unbalanced quotation mark.");
  547.             return NULL;
  548.         }
  549.     }
  550.  
  551.     return strdup(right_side);
  552. }
  553.  
  554.  
  555. /*
  556.  * default_handler
  557.  */
  558. void
  559. default_handler(struct mapping* mapping, char* right_side)
  560. {
  561.     int i;
  562.     assert(mapping);
  563.  
  564.     sprintf(buf, "Netscape%s: %s\n", mapping->resource, right_side);
  565.     append_to_output_file(buf);
  566.  
  567.     for ( i = 0; mapping->desens[i] != NULL; i++ ) {
  568.         sprintf(buf, "Netscape%s.sensitive: False\n", mapping->desens[i]);
  569.         append_to_output_file(buf);
  570.     }
  571. }
  572.  
  573.  
  574. /*
  575.  * windoze_handler
  576.  */
  577. void
  578. windoze_handler(struct mapping* mapping, char* right_side)
  579. {
  580.   static int warned = 0;
  581.  
  582.   if ( !warned ) {
  583.     warn("Menu prompts are ignored on this platform.");
  584.     warned = 1;
  585.   }
  586. }
  587.  
  588.  
  589. /*
  590.  * mac_handler
  591.  */
  592. void
  593. mac_handler(struct mapping* mapping, char* right_side)
  594. {
  595.     warn("Ignoring rule used only on Macintosh platform.");
  596. }
  597.  
  598.  
  599. /*
  600.  * boolean_handler
  601.  * The right side has been validated, so it should only be
  602.  * 'yes', 'no', 'true', or 'false'.
  603.  */
  604. void
  605. boolean_handler(struct mapping* mapping, char* right_side)
  606. {
  607.     if ( matches(right_side, "true") || matches(right_side, "yes") ) {
  608.         default_handler(mapping, "True");
  609.     } else {
  610.         default_handler(mapping, "False");
  611.     }
  612. }
  613.  
  614.  
  615. /*
  616.  * get_mnemonic
  617.  */
  618. char
  619. get_mnemonic(char* str)
  620. {
  621.     char mnemonic = '\0';
  622.     char* ptr;
  623.  
  624.     for ( ptr = str; *ptr != '\0'; ptr++, str++ ) {
  625.         if ( *ptr == '&' ) {
  626.             ptr++;
  627.             if ( *ptr != '&' ) mnemonic = *ptr;
  628.         }
  629.         *str = *ptr;
  630.     }
  631.  
  632.     *str = *ptr;
  633.  
  634.     return mnemonic;
  635. }
  636.  
  637.  
  638. /*
  639.  * menu_handler
  640.  */
  641. void
  642. menu_handler(struct mapping* mapping, char* right_side)
  643. {
  644.     char mnemonic = get_mnemonic(right_side);
  645.     
  646.     sprintf(buf, "Netscape%s.labelString: %s\n", mapping->resource, right_side);
  647.     append_to_output_file(buf);
  648.  
  649.     if ( mnemonic ) {
  650.         sprintf(buf, "Netscape%s.mnemonic: %c\n", mapping->resource, mnemonic);
  651.         append_to_output_file(buf);
  652.     }
  653. }
  654.  
  655.  
  656. /*
  657.  * proxy_handler
  658.  */
  659. void
  660. proxy_handler(struct mapping* mapping, char* right_side)
  661. {
  662.     int i;
  663.  
  664.     for ( i = 0; i < NUM_PROXIES; i++ ) {
  665.         if ( strstr(mapping->token, proxies[i].name) ) {
  666.             if ( strstr(mapping->token, "proxyport") ) {
  667.                 proxies[i].port = strdup(right_side);
  668.             } else {
  669.                 proxies[i].host = strdup(right_side);
  670.             }
  671.         }
  672.     }
  673. }
  674.  
  675.  
  676. /*
  677.  * handle_rule
  678.  */
  679. void
  680. handle_rule(struct mapping* mapping, char* line)
  681. {
  682.     char* right_side = get_right_side(line);
  683.  
  684.     if ( right_side == NULL ) return;
  685.  
  686.     if ( mapping->validate == NULL || mapping->validate(right_side) == 1 ) {
  687.         assert(mapping->process);
  688.         mapping->process(mapping, right_side);
  689.     }
  690.  
  691.     free(right_side);
  692. }
  693.  
  694.  
  695. /*
  696.  * matches
  697.  */
  698. int
  699. matches(char* str1, char* str2)
  700. {
  701.     assert(str1);
  702.     assert(str2);
  703.  
  704.     for ( ; *str1 && *str2; str1++, str2++ ) {
  705.         if ( tolower(*str1) != tolower(*str2) ) return 0;
  706.     }
  707.  
  708.     return ( *str1 == '\0' && *str2 == '\0' );
  709. }
  710.  
  711.  
  712. /*
  713.  * process_input_line
  714.  */
  715. void
  716. process_input_line(char* line)
  717. {
  718.     int i;
  719.     char* left_side;
  720.  
  721.     if ( is_blank(line)   || 
  722.          is_comment(line) ||
  723.          is_section(line)    ) return;
  724.  
  725.     *strchr(line, '\n') = '\0';
  726.  
  727.     left_side = get_left_side(line);
  728.     if ( left_side == NULL ) return;
  729.  
  730.     for ( i = 0; i < NUM_MAPPINGS; i++ ) {
  731.         if ( matches(left_side, mappings[i].token) ) {
  732.             handle_rule(&(mappings[i]), line);
  733.             break;
  734.         }
  735.     }
  736.  
  737.     if ( i == NUM_MAPPINGS ) {
  738.         sprintf(buf, "Unrecognized token '%s'.", left_side);
  739.         error(buf);
  740.     }
  741.  
  742.     free(left_side);
  743. }
  744.  
  745.  
  746. /*
  747.  * append_to_output_file
  748.  */
  749. void
  750. append_to_output_file(char* line)
  751. {
  752.     while ( output_buf == NULL || 
  753.             strlen(output_buf) + strlen(line) >= output_max_size ) {
  754.         output_max_size+= CHUNKSIZE;
  755.         output_buf = (char*) realloc(output_buf, output_max_size);
  756.     }
  757.  
  758.     strcat(output_buf, line);
  759. }
  760.  
  761.  
  762. /*
  763.  * obscure
  764.  */
  765. void
  766. obscure(char* buf)
  767. {
  768.     int i;
  769.     static int offset = 0;
  770.     int len = buf ? strlen(buf) : 0;
  771.  
  772.     for ( i = 0; i < len; i++, offset++ ) {
  773.         buf[i]+= ( 40 + (offset % 10) );
  774.     }
  775. }
  776.  
  777.  
  778. /*
  779.  * write_hash
  780.  */
  781. void
  782. write_hash(FILE* file)
  783. {
  784.     sprintf(buf, "! %s\n", XP_Md5PCPrintable(output_buf, strlen(output_buf)));
  785.  
  786.     if ( verbose ) {
  787.         printf("Hash is '%s'", buf);
  788.     }
  789.  
  790.     obscure(buf);
  791.  
  792.     if ( fwrite(buf, sizeof(char), strlen(buf), file) != strlen(buf) ) {
  793.         perror("fwrite");
  794.         exit(errno);
  795.     }
  796. }
  797.  
  798.  
  799. /*
  800.  * write_output_file
  801.  */
  802. void
  803. write_output_file(void)
  804. {
  805.     FILE* output_file;
  806.  
  807.     if ( fatal_errors ) {
  808.         fprintf(stderr, "No output file created due to error%s.\n", 
  809.                          fatal_errors == 1 ? "" : "s");
  810.         return;
  811.     }
  812.  
  813.     if ( output_buf == NULL ) return;
  814.  
  815.     if ( (output_file = fopen(OUTPUT_FILENAME, "w")) == NULL ) {
  816.         perror(OUTPUT_FILENAME);
  817.         exit(errno);
  818.     }
  819.  
  820.     write_hash(output_file);
  821.  
  822.     obscure(output_buf);
  823.  
  824.     if ( fwrite(output_buf, sizeof(char), strlen(output_buf), output_file) !=
  825.          strlen(output_buf) ) {
  826.         perror(OUTPUT_FILENAME);
  827.         exit(errno);
  828.     }
  829.  
  830.     if ( fclose(output_file) == -1 ) {
  831.         perror(OUTPUT_FILENAME);
  832.         exit(errno);
  833.     }
  834.  
  835.     fprintf(stderr, "Wrote '%s'\n", OUTPUT_FILENAME);
  836. }
  837.  
  838.  
  839. /*
  840.  * output_proxies
  841.  */
  842. void
  843. output_proxies(void)
  844. {
  845.     int i;
  846.  
  847.     for ( i = 0; i < NUM_PROXIES; i++ ) {
  848.         if ( proxies[i].host && proxies[i].port ) {
  849.             sprintf(buf, "Netscape.%sProxy: %s:%s\n", proxies[i].resource,  
  850.                                                      proxies[i].host,
  851.                                                      proxies[i].port);
  852.             append_to_output_file(buf);
  853.  
  854.             sprintf(buf, "Netscape*proxiesBox*%sText.sensitive: False\n", 
  855.                          proxies[i].resource);
  856.             append_to_output_file(buf);
  857.             sprintf(buf, "Netscape*proxiesBox*%sPort.sensitive: False\n", 
  858.                          proxies[i].resource);
  859.             append_to_output_file(buf);
  860.  
  861.             continue;
  862.         }
  863.         if ( proxies[i].host && !proxies[i].port ) {
  864.             sprintf(buf, "Specified %s without %s_port\n", proxies[i].name,
  865.                                                            proxies[i].name);
  866.             error(buf);
  867.         }
  868.         if ( !proxies[i].host && proxies[i].port ) {
  869.             sprintf(buf, "Specified %s_port without %s\n", proxies[i].name,
  870.                                                            proxies[i].name);
  871.             error(buf);
  872.         }
  873.     }
  874. }
  875.  
  876.  
  877. /*
  878.  * main
  879.  */
  880. int
  881. main(int argc, char* argv[])
  882. {
  883.     FILE* file;
  884.     char* filename;
  885.  
  886.     switch ( argc ) {
  887.         case 2:
  888.             verbose = 0;
  889.             filename = argv[1];
  890.             break;
  891. #ifdef DEBUG
  892.         case 3:
  893.             if ( !strcmp(argv[1], "-v") ) {
  894.                 verbose = 1;
  895.                 filename = argv[2];
  896.             } else {
  897.                 usage(argv[0]);
  898.                 exit(1);
  899.             }
  900.             break;
  901. #endif
  902.         default:
  903.             usage(argv[0]);
  904.             exit(1);
  905.             break;
  906.     }
  907.  
  908.     if ( (file = fopen(filename, "r")) == NULL ) {
  909.         perror(filename);
  910.         exit(errno);
  911.     }
  912.  
  913.     append_to_output_file("! e-kit v2.0b1\n");
  914.  
  915.     for ( lineno = 1; fgets(buf, sizeof(buf), file) != NULL; lineno++ ) {
  916.         if ( !strchr(buf, '\n') ) {
  917.             warn("Line too long.  Ignored.");
  918.         } else {
  919.             process_input_line(buf);
  920.         }
  921.     }
  922.  
  923.     output_proxies();
  924.  
  925.     write_output_file();
  926.  
  927.     return 0;
  928. }
  929.  
  930.  
  931. #ifdef HPUX
  932. /*
  933.  * pull_in_MD5_HashBuf
  934.  * Doesn't get linked in otherwise.
  935.  * You can use this if the '-u MD5_HashBuf' flag
  936.  * isn't supported.
  937.  */
  938. static void
  939. pull_in_MD5_HashBuf(void)
  940. {
  941.     MD5_HashBuf(0, 0, 0);
  942. }
  943. #endif
  944.  
  945.  
  946.