reg_entries.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874
  1. /***************************************************************************
  2. * reg_entries.c
  3. *
  4. * Fri May 13 11:35:33 2005
  5. * Copyright 2005 User
  6. * Email
  7. ****************************************************************************/
  8. /*
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Library General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  22. */
  23. #include <dirent.h>
  24. #include "globals.h"
  25. int menu_created;
  26. /*
  27. Returns 1, if the string is a submenu item, 0 otherwise.
  28. Very primitive
  29. */
  30. int is_submenu(char *item)
  31. {
  32. if (strchr(item, '[') == NULL) {
  33. return (0);
  34. }
  35. if (strrchr(item, ']') == NULL) {
  36. return (0);
  37. }
  38. if (strchr(item, '[') > strrchr(item, ']')) {
  39. return (0);
  40. }
  41. return (1);
  42. }
  43. /*
  44. If "Extensions" menu does not yet exist, it will be created immediately
  45. to the left of the "Help" menu in GIS Manager's main menu bar.
  46. Returns the line nr after which the "Extension" menu entries should be
  47. inserted.
  48. */
  49. int check_ext_menu(char **tcl_lines)
  50. {
  51. int idx;
  52. /* check if "Extensions" menu exists */
  53. idx = find_pos("\"&Xtns\" all options 1", tcl_lines, 0);
  54. if (idx == -1) { /* does not exist: create a new one */
  55. idx = find_pos("\"&Help\" all options", tcl_lines, 0);
  56. if (idx == -1) { /* Help menu does not exist: place at far right */
  57. idx = find_pos("}]", tcl_lines, 0);
  58. if (idx == -1) {
  59. print_error(ERR_REGISTER_ENTRIES_GISMAN,
  60. "could not parse 'menu.tcl'.\n");
  61. }
  62. insert_str(" \"&Xtns\" all options 1 {\n", idx, tcl_lines);
  63. idx++;
  64. insert_str(" }\n", idx, tcl_lines);
  65. return (idx);
  66. }
  67. insert_str(" \"&Xtns\" all options 1 {\n", idx, tcl_lines);
  68. idx++;
  69. insert_str(" }\n", idx, tcl_lines);
  70. }
  71. return (idx);
  72. }
  73. /*
  74. Creates a new submenu for this extension under "Extensions" in GIS Managers
  75. main menu bar.
  76. Returns line no. of submenu, after which additional entries should be appended
  77. to menu.tcl.
  78. */
  79. int new_submenu(char *pkg_short_name, char *menu, char **tcl_lines)
  80. {
  81. char tmp[MAXSTR];
  82. char tmp2[MAXSTR];
  83. char searchstr[MAXSTR];
  84. int idx, idx2;
  85. int insert_here;
  86. int last;
  87. char *first_quote;
  88. char *second_quote;
  89. int len;
  90. int terminator;
  91. /* Store the position of the "Extensions" entry and start looking for submenu from there */
  92. idx = find_pos("\"&Xtns\" all options", tcl_lines, 0);
  93. last = find_pos("\" all options", tcl_lines, idx + 1) - 1; /* find end of "Extensions" menu */
  94. if (last == -1) {
  95. last = find_pos("}]", tcl_lines, 0); /* end of menu.tcl */
  96. }
  97. /* check if the line is a valid submenu specifier */
  98. if (!is_submenu(menu)) {
  99. print_warning
  100. ("first line not a submenu specifier in 'entries-gisman'.\n");
  101. return (-1);
  102. }
  103. else {
  104. /* check if a submenu with this name does already exist */
  105. len = (strrchr(menu, ']') - strchr(menu, '[')) / sizeof(char);
  106. strncpy(tmp, strchr(menu, '[') + sizeof(char), len);
  107. tmp[len - 1] = '\0'; /* get rid of [] */
  108. sprintf(searchstr, "{cascad \"%s\"", tmp); /* this is what we need to look for */
  109. idx2 = find_pos(searchstr, tcl_lines, idx);
  110. if ((idx2 != -1) && (idx2 < last)) {
  111. print_warning("submenu '%s' exists in GIS Manager's Xtns menu.\n",
  112. tmp);
  113. return (-1);
  114. }
  115. /* ELSE: create a new submenu in the right place */
  116. insert_here = idx + 1; /* by default: place submenu in first line after "Extensions" menu */
  117. idx2 = find_pos("{cascad ", tcl_lines, idx); /* go through all submenus in "Extensions" */
  118. while ((idx2 != -1) && (idx2 < last)) {
  119. /* check for alphabetical order */
  120. first_quote = strchr(tcl_lines[idx2], '\"'); /* get name of submenu w/o quotation marks */
  121. second_quote = strchr(first_quote + sizeof(char), '\"');
  122. len = (second_quote - first_quote) / sizeof(char);
  123. strncpy(tmp2, first_quote + sizeof(char), len);
  124. tmp2[len - 1] = '\0'; /* get rid of "" */
  125. if (strcmp(tmp, tmp2) < 0) {
  126. insert_here = idx2;
  127. break;
  128. }
  129. idx++;
  130. idx2 = find_pos("{cascad ", tcl_lines, idx);
  131. }
  132. /* create new submenu and return line no in menu.tcl for insertion of entries */
  133. sprintf(tmp, " \t\t\t%s {} \"\" 1 {\n", searchstr);
  134. insert_str(tmp, insert_here, tcl_lines);
  135. insert_str(" \t\t\t}}\n", insert_here + 1, tcl_lines);
  136. /* create an uninstall entry in menu.tcl */
  137. terminator = find_pos("}]", tcl_lines, 0);
  138. /* this is just a comment that tells the uninstall function which submenu to remove */
  139. sprintf(tmp, "#(DO_NOT_REMOVE_THIS_COMMENT) <%s> %s {} \"\" 1 {\n",
  140. pkg_short_name, searchstr);
  141. insert_str(tmp, terminator + 1, tcl_lines);
  142. /* return next line for insertion of menu entries and commands! */
  143. return (insert_here + 1);
  144. }
  145. return (-1);
  146. }
  147. /*
  148. Inserts a new menu entry into the extension's own submenu under "Extensions".
  149. Returns the line number in menu.tcl after which the next entry should be
  150. inserted.
  151. */
  152. int new_item(char *item, char *menu, char **tcl_lines, int line_no)
  153. {
  154. char *token;
  155. int num_tokens;
  156. char entry[MAXSTR];
  157. char command[MAXSTR];
  158. char tmp[MAXSTR];
  159. /* remove dangling white spaces and EOL chars */
  160. chop(item);
  161. token = strtok(item, ";");
  162. if (token == NULL) {
  163. print_warning("invalid token in 'entries-gisman'.\n");
  164. return (-1);
  165. }
  166. strcpy(entry, token); /* get menu entry string */
  167. num_tokens = 0;
  168. while (token != NULL) {
  169. token = strtok(NULL, ";");
  170. if (token != NULL) {
  171. strcpy(command, token); /* get command string */
  172. }
  173. num_tokens++;
  174. }
  175. if (num_tokens > 2) {
  176. print_warning("invalid number of tokens (%i) in 'entries-gisman'.\n",
  177. num_tokens);
  178. return (-1);
  179. }
  180. /* just a separator or a 'real' menu entry? */
  181. if ((!strcmp(entry, "-")) && (!strcmp(entry, "-"))) {
  182. sprintf(tmp, " \t\t\t {separator}\n");
  183. }
  184. else {
  185. sprintf(tmp, " \t\t\t {command \"%s\" {} \"%s\" {} -command {%s }}\n",
  186. entry, command, command);
  187. }
  188. insert_str(tmp, line_no, tcl_lines);
  189. /* return line no. for next entry */
  190. line_no++;
  191. return (line_no);
  192. }
  193. /*
  194. Checks if there are any entries in entries-gisman.
  195. Reads GISBASE/etc/dm/menu.tcl into an array of strings.
  196. Adds a new item "Extensions" to the menu bar, if it is missing.
  197. Adds new submenus and menu items to the GIS manager, as stated in entries-gisman
  198. Writes the new menu structure to a temporary file which will then be installed
  199. using the 'su' function.
  200. */
  201. void register_entries_gisman(char *pkg_short_name, char *gisbase)
  202. {
  203. char file[MAXSTR];
  204. char str[MAXSTR];
  205. char menu[MAXSTR];
  206. int len;
  207. char **line;
  208. int n_entries, n_lines, i;
  209. int n_lines_org, n_lines_new;
  210. int line_no;
  211. FILE *f_gisman, *f_in, *f_out;
  212. /* check if entries-gisman exists and is readable */
  213. sprintf(file, "../entries-gisman");
  214. f_gisman = fopen(file, "r");
  215. if (f_gisman == NULL) {
  216. if (errno == ENOENT) {
  217. /* file does not exist */
  218. return;
  219. }
  220. else {
  221. /* sth. strange happened */
  222. fclose(f_gisman);
  223. print_error(ERR_REGISTER_ENTRIES_GISMAN,
  224. "checking for file '%s': %s\n", file,
  225. strerror(errno));
  226. }
  227. }
  228. /* check if menu.tcl exists and is readable */
  229. sprintf(file, "%s/etc/dm/menu.tcl", gisbase);
  230. f_in = fopen(file, "r");
  231. if (f_in == NULL) {
  232. if (errno == ENOENT) {
  233. /* file does not exist */
  234. return;
  235. }
  236. else {
  237. /* sth. strange happened */
  238. fclose(f_in);
  239. print_error(ERR_REGISTER_ENTRIES_GISMAN,
  240. "checking for file '%s': %s\n", file,
  241. strerror(errno));
  242. }
  243. }
  244. /* create a temporary menu.tcl file for write access */
  245. /* TODO: Do not hardcode temp paths */
  246. strcpy(TMP_GISMAN, "/tmp/grass.extensions.db.XXXXXX"); /* TMP_GISMAN is a global variable */
  247. mkstemp(TMP_GISMAN);
  248. f_out = fopen(TMP_GISMAN, "w+");
  249. if (f_out == NULL) {
  250. print_error(ERR_REGISTER_ENTRIES_GISMAN,
  251. "could not create temp file '%s': %s\n \
  252. Make sure that directory /tmp exists on your system and you have write permission.\n", TMP_GISMAN, strerror(errno));
  253. }
  254. atexit(&exit_db); /* now need to register an at exit func to remove tmpdb automatically! */
  255. /* everything fine: create a shell command to install gisman-entries and modified menu.tcl */
  256. /* this also creates a backup-copy of menu.tcl */
  257. if (VERBOSE) {
  258. sprintf(str,
  259. "mkdir --verbose %s/etc/dm/gem-entries ; cp -vf ../entries-gisman %s/etc/dm/gem-entries/%s ; \
  260. cp -vf %s/etc/dm/menu.tcl %s/etc/dm/menu.tcl.gem.bak ; \
  261. cp -vf %s %s/etc/dm/menu.tcl ; chmod -v a+r %s/etc/dm/menu.tcl ;",
  262. gisbase, gisbase, pkg_short_name, gisbase, gisbase, TMP_GISMAN, gisbase, gisbase);
  263. }
  264. else {
  265. sprintf(str,
  266. "mkdir %s/etc/dm/gem-entries &> %s ; cp -f ../entries-gisman %s/etc/dm/gem-entries/%s &> %s ; \
  267. cp -f %s/etc/dm/menu.tcl %s/etc/dm/menu.tcl.gem.bak &> %s ; \
  268. cp -f %s %s/etc/dm/menu.tcl &> %s ; chmod a+r %s/etc/dm/menu.tcl &> %s ;",
  269. gisbase, TMP_NULL, gisbase, pkg_short_name, TMP_NULL, gisbase, gisbase, TMP_NULL, TMP_GISMAN, gisbase, TMP_NULL, gisbase, TMP_NULL);
  270. }
  271. strcpy(GISMAN_CMD, str);
  272. /* count number of lines in entries-gisman */
  273. n_entries = 0;
  274. while (fgets(str, MAXSTR, f_gisman) != NULL) {
  275. n_entries++;
  276. }
  277. if (n_entries == 0) {
  278. return;
  279. }
  280. rewind(f_gisman);
  281. /* count number of lines in menu.tcl */
  282. n_lines = 0;
  283. while (fgets(str, MAXSTR, f_in) != NULL) {
  284. n_lines++;
  285. }
  286. if (n_lines == 0) {
  287. return;
  288. }
  289. n_lines_org = n_lines;
  290. rewind(f_in);
  291. /* create an array large enough to hold all lines in menu.tcl */
  292. /* plus the entries that are to be added from entries-gisman */
  293. /* plus one NULL terminator */
  294. /* and copy all lines from menu.tcl into this */
  295. line = (char **)calloc(n_lines + (n_entries * 2) + 6, sizeof(char *));
  296. for (i = 0; i < (n_lines + (n_entries * 2) + 6); i++) {
  297. line[i] = NULL;
  298. }
  299. i = 0;
  300. while (fgets(str, MAXSTR, f_in) != NULL) {
  301. line[i] = (char *)malloc((1 + strlen(str)) * sizeof(char));
  302. strcpy(line[i], str);
  303. i++;
  304. }
  305. check_ext_menu(line); /* create "Extensions" menu if necessary */
  306. /* read all lines from entries-gisman and add to menus */
  307. i = 1;
  308. while (nc_fgets_nb(str, MAXSTR, f_gisman) != NULL) {
  309. if (i == 1) {
  310. /* store name of menu item */
  311. len = (strrchr(str, ']') - strchr(str, '[')) / sizeof(char);
  312. strncpy(menu, strchr(str, '[') + sizeof(char), len);
  313. menu[len - 1] = '\0'; /* get rid of [] */
  314. line_no = new_submenu(pkg_short_name, str, line);
  315. if (line_no < 0) {
  316. print_warning("no GIS Manager menu entries created.\n");
  317. break;
  318. }
  319. i++;
  320. }
  321. else {
  322. line_no = new_item(str, menu, line, line_no);
  323. if (line_no < 0) {
  324. print_warning("error creating GIS Manager menu entries.\n");
  325. break;
  326. }
  327. i++;
  328. }
  329. }
  330. /* write output to tmpfile */
  331. i = 0;
  332. while (line[i] != NULL) {
  333. fprintf(f_out, line[i]);
  334. i++;
  335. }
  336. fflush(f_out);
  337. /* check for accidental corruption of menu.tcl: if tmpfile has less lines than installed
  338. menu.tcl, we did sth. wrong and should leave the orginal file untouched! */
  339. rewind(f_out);
  340. n_lines_new = 0;
  341. while (fgets(str, MAXSTR, f_out) != NULL) {
  342. n_lines_new++;
  343. }
  344. if ((n_lines_new == 0) || (n_lines_new < n_lines_org)) {
  345. print_warning
  346. ("file truncation detected. Retaining orginal file 'menu.tcl'.\n");
  347. strcpy(GISMAN_CMD, "");
  348. }
  349. /* close files */
  350. fclose(f_in);
  351. fclose(f_gisman);
  352. fclose(f_out);
  353. /* free memory */
  354. for (i = 0; i < (n_lines + (n_entries * 2) + 6); i++) {
  355. free(line[i]);
  356. }
  357. free(line);
  358. }
  359. /*
  360. This version is for Michael Barton's new version of the GIS Manager (gis.m)
  361. It is much simpler and more flexible, because gis.m can build menus
  362. from files at runtime.
  363. All we have to do is make sure there is a folder 'Xtns' in $GISBASE/etc/gm
  364. and we copy 'entries-gisman2' (if provided) into that folder using a
  365. filename 'extension name'.gem!
  366. */
  367. void register_entries_gisman2(char *pkg_short_name, char *gisbase)
  368. {
  369. char file[MAXSTR];
  370. FILE *f_gisman2;
  371. char str[MAXSTR];
  372. /* check if entries-gisman2 exists and is readable */
  373. sprintf(file, "../entries-gisman2");
  374. f_gisman2 = fopen(file, "r");
  375. if (f_gisman2 == NULL) {
  376. if (errno == ENOENT) {
  377. /* file does not exist */
  378. return;
  379. }
  380. else {
  381. /* sth. strange happened */
  382. fclose(f_gisman2);
  383. print_error(ERR_REGISTER_ENTRIES_GISMAN2,
  384. "checking for file '%s': %s\n", file,
  385. strerror(errno));
  386. }
  387. }
  388. /* let's just blindly create an 'Xtns' dir: if it exists already: no problem */
  389. /* and then copy file into it! */
  390. if (VERBOSE) {
  391. sprintf(str,
  392. "mkdir --verbose -p %s/etc/gm/Xtns ; cp -fv ../entries-gisman2 %s/etc/gm/Xtns/%s.gem ; ",
  393. gisbase, gisbase, pkg_short_name);
  394. }
  395. else {
  396. sprintf(str,
  397. "mkdir -p %s/etc/gm/Xtns ; cp -f ../entries-gisman2 %s/etc/gm/Xtns/%s.gem ; ",
  398. gisbase, gisbase, pkg_short_name);
  399. }
  400. strcpy(GISMAN2_CMD, str);
  401. }
  402. /*
  403. Checks for a comment left by the new_submenu () function in menu.tcl.
  404. If it exists, the submenu specified in that comment will be removed along
  405. with all its entries.
  406. Returns -1 on failure, number of removed entries otherwise.
  407. If no more submenu entries exist, this will also remove the "Xtns" menu.
  408. */
  409. int deregister_entries_gisman(char *pkg_short_name, char *gisbase)
  410. {
  411. char file[MAXSTR];
  412. char str[MAXSTR];
  413. char tmp[MAXSTR];
  414. char **line;
  415. int n_lines, i;
  416. int n_lines_org, n_lines_new;
  417. FILE *f_in, *f_out;
  418. int pos;
  419. int start, end;
  420. int start_sub, end_sub;
  421. char *lq, *rq;
  422. int num_removed;
  423. /* check if menu.tcl exists and is readable */
  424. sprintf(file, "%s/etc/dm/menu.tcl", gisbase);
  425. f_in = fopen(file, "r");
  426. if (f_in == NULL) {
  427. if (errno == ENOENT) {
  428. /* file does not exist */
  429. return (0);
  430. }
  431. else {
  432. /* sth. strange happened */
  433. fclose(f_in);
  434. print_error(ERR_DEREGISTER_ENTRIES_GISMAN,
  435. "checking for file '%s': %s\n", file,
  436. strerror(errno));
  437. }
  438. }
  439. /* create a temporary menu.tcl file for write access */
  440. /* TODO: Do not hardcode temp paths */
  441. strcpy(TMP_GISMAN, "/tmp/grass.extensions.db.XXXXXX"); /* TMP_GISMAN is a global variable */
  442. mkstemp(TMP_GISMAN);
  443. f_out = fopen(TMP_GISMAN, "w+");
  444. if (f_out == NULL) {
  445. print_error(ERR_REGISTER_ENTRIES_GISMAN,
  446. "could not create temp file '%s': %s\n \
  447. Make sure that directory /tmp exists on your system and you have write permission.\n", TMP_GISMAN, strerror(errno));
  448. }
  449. atexit(&exit_db); /* now need to register an at exit func to remove tmpdb automatically! */
  450. /* everything fine: create a shell command to copy modified menu.tcl on uninstall */
  451. if (VERBOSE) {
  452. sprintf(str, "cp -vf %s/etc/dm/menu.tcl %s/etc/dm/menu.tcl.gem.bak ; \
  453. cp -vf %s %s/etc/dm/menu.tcl ; chmod -v a+r %s/etc/dm/menu.tcl ;", gisbase, gisbase, TMP_GISMAN, gisbase, gisbase);
  454. }
  455. else {
  456. sprintf(str,
  457. "cp -f %s/etc/dm/menu.tcl %s/etc/dm/menu.tcl.gem.bak &> %s ; \
  458. cp -f %s %s/etc/dm/menu.tcl &> %s ; chmod a+r %s/etc/dm/menu.tcl &> %s ;", gisbase, gisbase, TMP_NULL, TMP_GISMAN, gisbase, TMP_NULL, gisbase, TMP_NULL);
  459. }
  460. strcpy(GISMAN_CMD, str);
  461. /* count number of lines in menu.tcl */
  462. n_lines = 0;
  463. while (fgets(str, MAXSTR, f_in) != NULL) {
  464. n_lines++;
  465. }
  466. if (n_lines == 0) {
  467. return (-1);
  468. }
  469. rewind(f_in);
  470. n_lines_org = n_lines;
  471. /* create an array large enough to hold all lines in menu.tcl */
  472. /* plus one NULL terminator */
  473. /* and copy all lines from menu.tcl into this */
  474. line = (char **)calloc(n_lines + 1, sizeof(char *));
  475. for (i = 0; i < n_lines + 1; i++) {
  476. line[i] = NULL;
  477. }
  478. i = 0;
  479. while (fgets(str, MAXSTR, f_in) != NULL) {
  480. line[i] = (char *)malloc((1 + strlen(str)) * sizeof(char));
  481. strcpy(line[i], str);
  482. i++;
  483. }
  484. /* search for uninstall comment */
  485. sprintf(str, "#(DO_NOT_REMOVE_THIS_COMMENT) <%s> {cascad",
  486. pkg_short_name);
  487. pos = find_pos(str, line, 0);
  488. if (pos == -1) {
  489. print_warning
  490. ("could not find uninstall information in 'menu.tcl'.\n");
  491. return (-1);
  492. }
  493. /* copy name of submenu to search for */
  494. lq = strchr(line[pos], '\"');
  495. lq++;
  496. rq = strchr(lq, '\"');
  497. strcpy(tmp, lq);
  498. tmp[(rq - lq) / sizeof(char)] = '\0';
  499. /* now find "Xtns" menu start and end */
  500. start = find_pos("\"&Xtns\" all options 1", line, 0);
  501. end = find_pos("\" all options", line, start + 1) - 1;
  502. if (end == -1) {
  503. end = find_pos("}]", line, 0); /* end of menu.tcl */
  504. }
  505. if (start == -1) {
  506. print_warning("menu 'Xtns' does not exist.\n");
  507. return (-1);
  508. }
  509. /* now find our submenu and set the search range to it */
  510. sprintf(str, "{cascad \"%s\"", tmp);
  511. start_sub = find_pos(str, line, start);
  512. if ((start_sub == -1) || (start_sub > end)) {
  513. print_warning("could not find submenu entry '%s' in 'menu.tcl'.\n",
  514. tmp);
  515. return (-1);
  516. }
  517. end_sub = find_pos(" \t\t\t}}", line, start_sub);
  518. if ((end_sub == -1) || (end_sub > end)) {
  519. print_warning
  520. ("could not find end of submenu entry '%s' in 'menu.tcl'.\n",
  521. tmp);
  522. return (-1);
  523. }
  524. num_removed = 0;
  525. /* now kill every line in between start and end! */
  526. for (i = 0; i < ((end_sub - start_sub) + 1); i++) {
  527. delete_str(start_sub, line);
  528. num_removed++;
  529. }
  530. /* now kill the uninstall comment */
  531. sprintf(str, "#(DO_NOT_REMOVE_THIS_COMMENT) <%s> {cascad",
  532. pkg_short_name);
  533. pos = find_pos(str, line, 0);
  534. delete_str(pos, line);
  535. num_removed++;
  536. /* check if there are any submenus left in "Xtns" and if not: remove Xtns menu */
  537. start = find_pos("\"&Xtns\" all options 1", line, 0);
  538. end = find_pos("\" all options", line, start + 1) - 1;
  539. if (end - start < 3) {
  540. for (i = 0; i < ((end - start) + 1); i++) {
  541. delete_str(start, line);
  542. num_removed++;
  543. }
  544. }
  545. /* write output to tmpfile */
  546. i = 0;
  547. while (line[i] != NULL) {
  548. fprintf(f_out, line[i]);
  549. i++;
  550. }
  551. fflush(f_out);
  552. /* check for accidental corruption of menu.tcl: if tmpfile is empty (=0 lines),
  553. we did sth. wrong and should leave the orginal file untouched! */
  554. rewind(f_out);
  555. n_lines_new = 0;
  556. while (fgets(str, MAXSTR, f_out) != NULL) {
  557. n_lines_new++;
  558. }
  559. if ((n_lines_new == 0)) {
  560. print_warning
  561. ("file truncation detected. Retaining orginal file 'menu.tcl'.\n");
  562. strcpy(GISMAN_CMD, "");
  563. }
  564. /* close files */
  565. fclose(f_in);
  566. fclose(f_out);
  567. /* free memory */
  568. for (i = 0; i < n_lines + 1; i++) {
  569. free(line[i]);
  570. }
  571. free(line);
  572. return (num_removed);
  573. }
  574. /*
  575. This version is for Michael Barton's new GIS Manager.
  576. In this case, all we have to do is delete the .gem file!
  577. */
  578. void deregister_entries_gisman2(char *pkg_short_name, char *gisbase)
  579. {
  580. char file[MAXSTR];
  581. FILE *f_gisman2;
  582. char str[MAXSTR];
  583. /* check if entries-gisman2 exists and is readable */
  584. sprintf(file, "%s/etc/gm/Xtns/%s.gem", gisbase, pkg_short_name);
  585. f_gisman2 = fopen(file, "r");
  586. if (f_gisman2 == NULL) {
  587. if (errno == ENOENT) {
  588. /* file does not exist */
  589. return;
  590. }
  591. else {
  592. /* sth. strange happened */
  593. fclose(f_gisman2);
  594. print_error(ERR_DEREGISTER_ENTRIES_GISMAN2,
  595. "checking for file '%s': %s\n", file,
  596. strerror(errno));
  597. }
  598. }
  599. if (VERBOSE) {
  600. sprintf(str, "rm -vf %s/etc/gm/Xtns/%s.gem ; ",
  601. gisbase, pkg_short_name);
  602. }
  603. else {
  604. sprintf(str, "rm -f %s/etc/gm/Xtns/%s.gem ; ",
  605. gisbase, pkg_short_name);
  606. }
  607. strcpy(GISMAN_CMD, str);
  608. }
  609. /*
  610. Returns number of restored entries
  611. */
  612. int restore_entries_gisman(char *gisbase)
  613. {
  614. char str[MAXSTR];
  615. char menu[MAXSTR];
  616. char file[MAXSTR];
  617. char dir[MAXSTR];
  618. char pkg_short_name[MAXSTR];
  619. int len;
  620. char **line;
  621. int n_entries, n_lines, i;
  622. int line_no;
  623. FILE *f_gisman, *f_in, *f_out;
  624. DIR *dirp;
  625. struct dirent *ep;
  626. int num_restored;
  627. int n_files;
  628. /* check if menu.tcl exists and is readable */
  629. sprintf(file, "%s/etc/dm/menu.tcl", gisbase);
  630. f_in = fopen(file, "r");
  631. if (f_in == NULL) {
  632. if (errno == ENOENT) {
  633. /* file does not exist */
  634. return (0);
  635. }
  636. else {
  637. /* sth. strange happened */
  638. fclose(f_in);
  639. print_error(ERR_REGISTER_ENTRIES_GISMAN,
  640. "checking for file '%s': %s\n", file,
  641. strerror(errno));
  642. }
  643. }
  644. /* create a temporary menu.tcl file for write access */
  645. /* TODO: Do not hardcode temp paths */
  646. strcpy(TMP_GISMAN, "/tmp/grass.extensions.db.XXXXXX"); /* TMP_GISMAN is a global variable */
  647. mkstemp(TMP_GISMAN);
  648. f_out = fopen(TMP_GISMAN, "w+");
  649. if (f_out == NULL) {
  650. print_error(ERR_REGISTER_ENTRIES_GISMAN,
  651. "could not create temp file '%s': %s\n \
  652. Make sure that directory /tmp exists on your system and you have write permission.\n", TMP_GISMAN, strerror(errno));
  653. }
  654. atexit(&exit_db); /* now need to register an at exit func to remove tmpdb automatically! */
  655. /* everything fine: create a shell command to copy modified menu.tcl on uninstall */
  656. if (VERBOSE) {
  657. sprintf(str, "cp -vf %s/etc/dm/menu.tcl %s/etc/dm/menu.tcl.gem.bak ; \
  658. cp -vf %s %s/etc/dm/menu.tcl ; chmod -v a+r %s/etc/dm/menu.tcl ;", gisbase, gisbase, TMP_GISMAN, gisbase, gisbase);
  659. }
  660. else {
  661. sprintf(str,
  662. "cp -f %s/etc/dm/menu.tcl %s/etc/dm/menu.tcl.gem.bak &> %s ; \
  663. cp -f %s %s/etc/dm/menu.tcl &> %s ; chmod a+r %s/etc/dm/menu.tcl &> %s ;", gisbase, gisbase, TMP_NULL, TMP_GISMAN, gisbase, TMP_NULL, gisbase, TMP_NULL);
  664. }
  665. strcpy(GISMAN_CMD, str);
  666. /* allocate a pointer to the directory structure */
  667. sprintf(dir, "%s/etc/dm/gem-entries", gisbase);
  668. dirp = opendir(dir);
  669. if (dirp == NULL) {
  670. /* directory does not exist or is not accessible */
  671. return (0);
  672. }
  673. /* PASS 1 */
  674. /* open all files in gem-entries and count the number of lines each has */
  675. n_entries = 0;
  676. n_files = 0;
  677. while ((ep = readdir(dirp))) {
  678. sprintf(file, "%s/%s", dir, ep->d_name);
  679. f_gisman = fopen(file, "r");
  680. if ((!strcmp(ep->d_name, ".")) || (!strcmp(ep->d_name, ".."))) {
  681. fclose(f_gisman);
  682. continue;
  683. }
  684. if (f_gisman == NULL) {
  685. fclose(f_gisman);
  686. continue;
  687. }
  688. /* count number of lines in file */
  689. while (fgets(str, MAXSTR, f_gisman) != NULL) {
  690. n_entries++;
  691. }
  692. n_files++;
  693. fclose(f_gisman);
  694. }
  695. closedir(dirp);
  696. /* count number of lines in menu.tcl */
  697. n_lines = 0;
  698. while (fgets(str, MAXSTR, f_in) != NULL) {
  699. n_lines++;
  700. }
  701. if (n_lines == 0) {
  702. return (0);
  703. }
  704. rewind(f_in);
  705. /* create an array large enough to hold all lines in menu.tcl */
  706. /* plus the entries that are to be added from the files in gem-entries/ */
  707. /* plus space for uninstall comments */
  708. /* plus one NULL terminator */
  709. /* and copy all lines from menu.tcl into this */
  710. line =
  711. (char **)calloc(n_lines + (n_entries * 2) + (n_files * 5) + 1,
  712. sizeof(char *));
  713. for (i = 0; i < (n_lines + (n_entries * 2) + (n_files * 5) + 1); i++) {
  714. line[i] = NULL;
  715. }
  716. i = 0;
  717. while (fgets(str, MAXSTR, f_in) != NULL) {
  718. line[i] = (char *)malloc((1 + strlen(str)) * sizeof(char));
  719. strcpy(line[i], str);
  720. i++;
  721. }
  722. line[i] = NULL; /* add NULL terminator */
  723. check_ext_menu(line); /* create "Extensions" menu if necessary */
  724. /* PASS 2: re-create submenus and all menu items if necessary */
  725. dirp = opendir(dir);
  726. num_restored = 0;
  727. while ((ep = readdir(dirp))) {
  728. sprintf(file, "%s/%s", dir, ep->d_name);
  729. if ((!strcmp(ep->d_name, ".")) || (!strcmp(ep->d_name, ".."))) {
  730. continue;
  731. }
  732. f_gisman = fopen(file, "r");
  733. if (f_gisman == NULL) {
  734. continue;
  735. }
  736. /* read all lines from entries-gisman and add to menus */
  737. i = 1;
  738. while (nc_fgets_nb(str, MAXSTR, f_gisman) != NULL) {
  739. if (i == 1) {
  740. /* store name of menu item */
  741. len = (strrchr(str, ']') - strchr(str, '[')) / sizeof(char);
  742. strncpy(menu, strchr(str, '[') + sizeof(char), len);
  743. menu[len - 1] = '\0'; /* get rid of [] */
  744. line_no = new_submenu(pkg_short_name, str, line);
  745. if (line_no < 0) {
  746. break;
  747. }
  748. i++;
  749. num_restored++;
  750. }
  751. else {
  752. line_no = new_item(str, menu, line, line_no);
  753. if (line_no < 0) {
  754. break;
  755. }
  756. i++;
  757. num_restored++;
  758. }
  759. }
  760. fclose(f_gisman);
  761. }
  762. closedir(dirp);
  763. /* write output to tmpfile */
  764. i = 0;
  765. while (line[i] != NULL) {
  766. fprintf(f_out, line[i]);
  767. i++;
  768. }
  769. fflush(f_out);
  770. /* close remaining files */
  771. fclose(f_in);
  772. fclose(f_out);
  773. /* free memory */
  774. for (i = 0; i < (n_lines + (n_entries * 2) + (n_files * 5) + 1); i++) {
  775. free(line[i]);
  776. }
  777. free(line);
  778. return (num_restored);
  779. }