home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Spezial / SPEZIAL2_97.zip / SPEZIAL2_97.iso / ANWEND / ONLINE / ELM23-2 / ELM23-2.ZIP / src / save_opts.c < prev    next >
C/C++ Source or Header  |  1992-04-01  |  11KB  |  404 lines

  1.  
  2. static char rcsid[] = "@(#)$Id: save_opts.c,v 4.1 90/04/28 22:44:00 syd Exp $";
  3.  
  4. /*******************************************************************************
  5.  *  The Elm Mail System  -  $Revision: 4.1 $   $State: Exp $
  6.  *
  7.  *             Copyright (c) 1986, 1987 Dave Taylor
  8.  *             Copyright (c) 1988, 1989, 1990 USENET Community Trust
  9.  *******************************************************************************
  10.  * Bug reports, patches, comments, suggestions should be sent to:
  11.  *
  12.  *    Syd Weinstein, Elm Coordinator
  13.  *    elm@DSI.COM            dsinc!elm
  14.  *
  15.  *******************************************************************************
  16.  * $Log:    save_opts.c,v $
  17.  * Revision 4.1  90/04/28  22:44:00  syd
  18.  * checkin of Elm 2.3 as of Release PL0
  19.  *
  20.  *
  21.  ******************************************************************************/
  22.  
  23. /** This file contains the routine needed to allow the users to change the
  24.     Elm parameters and then save the configuration in a ".elm/elmrc" file in
  25.     their home directory.  With any luck this will allow them never to have
  26.     to actually EDIT the file!!
  27.  
  28. **/
  29.  
  30. #include "headers.h"
  31. #include <errno.h>
  32.  
  33. #undef onoff
  34. #define   onoff(n)    (n == 1? "ON":"OFF")
  35.  
  36. #define absolute(x)        ((x) < 0? -(x) : (x))
  37.  
  38. #ifndef OS2
  39. extern  int errno;
  40. #endif
  41. extern char version_buff[];
  42.  
  43. char *error_name(), *sort_name();
  44. long  ftell();
  45.  
  46. #include "save_opts.h"
  47.  
  48. FILE *elminfo;        /* informational file as needed... */
  49.  
  50. save_options()
  51. {
  52.     /** Save the options currently specified to a file.  This is a
  53.         fairly complex routine since it tries to put in meaningful
  54.         comments and such as it goes along.  The comments are
  55.         extracted from the file ELMRC_INFO as defined in the sysdefs.h
  56.         file.  THAT file has the format;
  57.  
  58.         varname
  59.           <comment>
  60.           <comment>
  61.         <blank line>
  62.  
  63.         and each comment is written ABOVE the variable to be added.  This
  64.         program also tries to make 'pretty' stuff like the alternatives
  65.         and such.
  66.     **/
  67.  
  68.     FILE *newelmrc;
  69.     char  oldfname[SLEN], newfname[SLEN], inffname[SLEN];
  70.  
  71.     sprintf(newfname, "%s/%s", home, elmrcfile);
  72.     sprintf(oldfname, "%s/%s", home, old_elmrcfile);
  73.  
  74.     /** first off, let's see if they already HAVE a .elm/elmrc file **/
  75.  
  76.     save_file_stats(newfname);
  77.     if (access(newfname, ACCESS_EXISTS) != -1) {
  78.       /** YES!  Copy it to the file ".old.elmrc".. **/
  79.       if (rename(newfname, oldfname) < 0)
  80.         dprint(2, (debugfile, "Unable to rename %s to %s\n",
  81.            newfname, oldfname));
  82.       (void) chown(oldfname, userid, groupid);
  83.  
  84.     }
  85.  
  86.     /** now let's open the datafile if we can... **/
  87.     sprintf(inffname,"%s/%s", helphome, elmrc_info);
  88.  
  89.     if ((elminfo = fopen(inffname, "r")) == NULL)
  90.       error1("Warning: saving without comments! Can't get to %s.",
  91.           inffname);
  92.  
  93.     /** next, open the new .elm/elmrc file... **/
  94.  
  95.     if ((newelmrc = fopen(newfname, "w")) == NULL) {
  96.        error2("Can't save configuration! Can't write to %s [%s].",
  97.            newfname, error_name(errno));
  98.        return;
  99.     }
  100.  
  101.     save_user_options(elminfo, newelmrc);
  102.     restore_file_stats(newfname);
  103.  
  104.     error1("Options saved in file %s.", newfname);
  105. }
  106.  
  107. save_user_options(elminfo_fd, newelmrc)
  108. FILE *elminfo_fd, *newelmrc;
  109. {
  110.     /** save the information in the file.  If elminfo_fd == NULL don't look
  111.         for comments!
  112.     **/
  113.  
  114.     if (elminfo_fd != NULL)
  115.       build_offset_table(elminfo_fd);
  116.  
  117.     fprintf(newelmrc,
  118.           "#\n# .elm/elmrc - options file for the ELM mail system\n#\n");
  119.  
  120.     if (strlen(full_username) > 0)
  121.       fprintf(newelmrc, "# Saved automatically by ELM %s for %s\n#\n\n",
  122.           version_buff, full_username);
  123.     else
  124.       fprintf(newelmrc, "# Saved automatically by ELM %s\n#\n\n", version_buff);
  125.     fprintf(newelmrc,"# For yes/no settings with ?, ON means yes, OFF means no\n\n");
  126.  
  127.     save_option_string(CALENDAR, raw_calendar_file, newelmrc, FALSE);
  128.     save_option_string(EDITOR, raw_editor, newelmrc, FALSE);
  129.  
  130.     save_option_char(ESCAPECHAR, escape_char, newelmrc);
  131.  
  132.     save_option_string(FULLNAME, full_username, newelmrc, FALSE);
  133.     save_option_string(RECEIVEDMAIL, raw_recvdmail, newelmrc, FALSE);
  134.     save_option_string(MAILDIR, raw_folders, newelmrc, FALSE);
  135.     save_option_string(TMPDIR, temp_dir, newelmrc, FALSE);
  136.     save_option_string(PAGER, raw_pager, newelmrc, FALSE);
  137.     save_option_string(PREFIX, prefixchars, newelmrc, TRUE);
  138.     save_option_string(PRINT, raw_printout, newelmrc, FALSE);
  139.     save_option_string(SENTMAIL, raw_sentmail, newelmrc, FALSE);
  140.     save_option_string(SHELL, raw_shell, newelmrc, FALSE);
  141.  
  142.     save_option_string(LOCALSIGNATURE, raw_local_signature,
  143.        newelmrc, FALSE);
  144.     save_option_string(REMOTESIGNATURE, raw_remote_signature,
  145.       newelmrc, FALSE);
  146.     save_option_on_off(SIGDASHES, sig_dashes, newelmrc);
  147.  
  148.     save_option_sort(SORTBY, newelmrc);
  149.  
  150.     save_option_on_off(ALWAYSDELETE, always_del, newelmrc);
  151.     save_option_on_off(ALWAYSSTORE, always_store, newelmrc);
  152.     save_option_on_off(ALWAYSKEEP, always_keep, newelmrc);
  153.     save_option_on_off(ARROW, arrow_cursor, newelmrc);
  154.     save_option_on_off(ASK, question_me, newelmrc);
  155.     save_option_on_off(ASKCC, prompt_for_cc, newelmrc);
  156.     save_option_string(ATTRIBUTION, attribution, newelmrc, FALSE);
  157.     save_option_on_off(AUTOCOPY, auto_copy, newelmrc);
  158.  
  159.     save_option_number(BOUNCEBACK, bounceback, newelmrc);
  160.  
  161.     save_option_on_off(COPY, auto_cc, newelmrc);
  162.     save_option_on_off(FORCENAME, force_name, newelmrc);
  163.     save_option_on_off(FORMS, (allow_forms != NO), newelmrc);
  164.     save_option_on_off(KEEPEMPTY, keep_empty_files, newelmrc);
  165.     save_option_on_off(KEYPAD, hp_terminal, newelmrc);
  166.     save_option_on_off(MENU, mini_menu, newelmrc);
  167.     save_option_on_off(MOVEPAGE, move_when_paged, newelmrc);
  168.     save_option_on_off(NAMES, names_only, newelmrc);
  169.     save_option_on_off(NOHEADER, noheader, newelmrc);
  170.     save_option_on_off(POINTNEW, point_to_new, newelmrc);
  171.     save_option_on_off(PROMPTAFTER, prompt_after_pager, newelmrc);
  172.     save_option_on_off(RESOLVE, resolve_mode, newelmrc);
  173.     save_option_on_off(SAVENAME, save_by_name, newelmrc);
  174.     save_option_on_off(SOFTKEYS, hp_softkeys, newelmrc);
  175.  
  176.     save_option_number(TIMEOUT, (int) timeout, newelmrc);
  177.  
  178.     save_option_on_off(TITLES, title_messages, newelmrc);
  179.  
  180.     save_option_number(USERLEVEL, user_level, newelmrc);
  181.  
  182.     save_option_on_off(WARNINGS, warnings, newelmrc);
  183.     save_option_on_off(WEED, filter, newelmrc);
  184.  
  185.     save_option_weedlist(WEEDOUT, newelmrc);
  186.     save_option_alternatives(ALTERNATIVES, alternative_addresses, newelmrc);
  187.  
  188.     fclose(newelmrc);
  189.     if ( elminfo_fd != NULL ) {
  190.         fclose(elminfo_fd);
  191.     }
  192. }
  193.  
  194. save_option_string(iindex, value, fd, underscores)
  195. int iindex, underscores;
  196. char *value;
  197. FILE *fd;
  198. {
  199.     /** Save a string option to the file... only subtlety is when we
  200.         save strings with spaces in 'em - translate to underscores!
  201.     **/
  202.  
  203.     char     buffer[SLEN], *bufptr;
  204.  
  205.     add_comment(iindex, fd);
  206.  
  207.     strcpy(buffer, value);
  208.  
  209.     if (underscores)
  210.       for (bufptr = buffer; *bufptr; bufptr++)
  211.         if (*bufptr == SPACE) *bufptr = '_';
  212.  
  213.     fprintf(fd, "%s = %s\n\n", save_info[iindex].name, buffer);
  214. }
  215.  
  216. save_option_sort(iindex, fd)
  217. int iindex;
  218. FILE *fd;
  219. {
  220.     /** save the current sorting option to a file **/
  221.  
  222.     add_comment(iindex, fd);
  223.  
  224.     fprintf(fd, "%s = %s\n\n", save_info[iindex].name,
  225.         sort_name(SHORT));
  226. }
  227.  
  228. save_option_char(iindex, value, fd)
  229. int iindex;
  230. char value;
  231. FILE *fd;
  232. {
  233.     /** Save a character option to the file **/
  234.  
  235.     add_comment(iindex, fd);
  236.  
  237.     fprintf(fd, "%s = %c\n\n", save_info[iindex].name, value);
  238. }
  239.  
  240. save_option_number(iindex, value, fd)
  241. int iindex, value;
  242. FILE *fd;
  243. {
  244.     /** Save a binary option to the file - boy is THIS easy!! **/
  245.  
  246.     add_comment(iindex, fd);
  247.  
  248.     fprintf(fd, "%s = %d\n\n", save_info[iindex].name, value);
  249. }
  250.  
  251. save_option_on_off(iindex, value, fd)
  252. int iindex, value;
  253. FILE *fd;
  254. {
  255.     /** Save a binary option to the file - boy is THIS easy!! **/
  256.  
  257.     add_comment(iindex, fd);
  258.  
  259.     fprintf(fd, "%s = %s\n\n", save_info[iindex].name, onoff(value));
  260. }
  261.  
  262. save_option_weedlist(iindex, fd)
  263. int iindex;
  264. FILE *fd;
  265. {
  266.     /** save a list of weedout headers to the file **/
  267.  
  268.     int length_so_far = 0, i;
  269.  
  270.     add_comment(iindex, fd);
  271.  
  272.     length_so_far = strlen(save_info[iindex].name) + 4;
  273.  
  274.     fprintf(fd, "%s = ", save_info[iindex].name);
  275.  
  276.     /** first off, skip till we get past the default list **/
  277.  
  278.     for (i = 0; i < weedcount; i++)
  279.       if (strcmp(weedlist[i],"*end-of-defaults*") == 0)
  280.         break;
  281.  
  282.     while (i < weedcount) {
  283.       if (strcmp(weedlist[i], "*end-of-defaults*") != 0)
  284.         break;
  285.       i++;    /* and get PAST it too! */
  286.     }
  287.  
  288.     while (i < weedcount) {
  289.       if (strlen(weedlist[i]) + length_so_far > 78) {
  290.         fprintf(fd, "\n\t");
  291.         length_so_far = 8;
  292.       }
  293.       fprintf(fd, "\"%s\" ", weedlist[i]);
  294.       length_so_far += (strlen(weedlist[i]) + 4);
  295.       i++;
  296.     }
  297.     fprintf(fd, "\t\"*end-of-user-headers*\"\n\n");
  298. }
  299.  
  300. save_option_alternatives(iindex, list, fd)
  301. int iindex;
  302. struct addr_rec *list;
  303. FILE *fd;
  304. {
  305.     /** save a list of options to the file **/
  306.     int length_so_far = 0;
  307.     struct addr_rec     *alternate;
  308.  
  309.     if (list == NULL) return;    /* nothing to do! */
  310.  
  311.     add_comment(iindex, fd);
  312.  
  313.     alternate = list;    /* don't LOSE the top!! */
  314.  
  315.     length_so_far = strlen(save_info[iindex].name) + 4;
  316.  
  317.     fprintf(fd, "%s = ", save_info[iindex].name);
  318.  
  319.     while (alternate != NULL) {
  320.       if (strlen(alternate->address) + length_so_far > 78) {
  321.         fprintf(fd, "\n\t");
  322.         length_so_far = 8;
  323.       }
  324.       fprintf(fd, "%s  ", alternate->address);
  325.       length_so_far += (strlen(alternate->address) + 3);
  326.       alternate = alternate->next;
  327.     }
  328.     fprintf(fd, "\n\n");
  329. }
  330.  
  331. add_comment(iindex, fd)
  332. int iindex;
  333. FILE *fd;
  334. {
  335.     /** get to and add the comment to the file **/
  336.     char buffer[SLEN];
  337.  
  338.     /** first off, add the comment from the comment file, if available **/
  339.  
  340.     if (save_info[iindex].offset > 0L) {
  341.       if (fseek(elminfo, save_info[iindex].offset, 0) == -1) {
  342.         dprint(1,(debugfile,
  343.            "** error %s seeking to %ld in elm-info file!\n",
  344.            error_name(errno), save_info[iindex].offset));
  345.       }
  346.       else while (fgets(buffer, SLEN, elminfo) != NULL) {
  347.         if (buffer[0] != '#')
  348.            break;
  349.         else
  350.            fprintf(fd, "%s", buffer);
  351.       }
  352.     }
  353. }
  354.  
  355. build_offset_table(elminfo_fd)
  356. FILE *elminfo_fd;
  357. {
  358.     /** read in the info file and build the table of offsets.
  359.         This is a rather laborious puppy, but at least we can
  360.         do a binary search through the array for each element and
  361.         then we have it all at once!
  362.     **/
  363.  
  364.     char line_buffer[SLEN];
  365.  
  366.     while (fgets(line_buffer, SLEN, elminfo_fd) != NULL) {
  367.       if (strlen(line_buffer) > 1)
  368.         if (line_buffer[0] != '#' && !whitespace(line_buffer[0])) {
  369.            no_ret(line_buffer);
  370.            if (find_and_store_loc(line_buffer, ftell(elminfo_fd))) {
  371.              dprint(1, (debugfile,"** Couldn't find and store \"%s\" **\n",
  372.              line_buffer));
  373.            }
  374.         }
  375.     }
  376. }
  377.  
  378. find_and_store_loc(name, offset)
  379. char *name;
  380. long  offset;
  381. {
  382.     /** given the name and offset, find it in the table and store it **/
  383.  
  384.     int first = 0, last, middle, compare;
  385.  
  386.     last = NUMBER_OF_SAVEABLE_OPTIONS;
  387.  
  388.     while (first <= last) {
  389.  
  390.       middle = (first+last) / 2;
  391.  
  392.       if ((compare = strcmp(name, save_info[middle].name)) < 0) /* a < b */
  393.         last = middle - 1;
  394.       else if (compare == 0) {                    /* a = b */
  395.         save_info[middle].offset = offset;
  396.         return(0);
  397.       }
  398.       else  /* greater */                        /* a > b */
  399.         first = middle + 1;
  400.     }
  401.  
  402.     return(-1);
  403. }
  404.