home *** CD-ROM | disk | FTP | other *** search
/ Amiga MA Magazine 1998 #6 / amigamamagazinepolishissue1998.iso / packery / xpk_source / xfh / options.c < prev    next >
C/C++ Source or Header  |  1996-10-19  |  19KB  |  711 lines

  1. /* options.c - Routines to handle the setting of user options.
  2.    Copyright (C) 1991, 1992, 1993 Kristian Nielsen.
  3.  
  4.    This file is part of XFH, the compressing file system handler.
  5.  
  6.    This program is free software; you can redistribute it and/or modify
  7.    it under the terms of the GNU General Public License as published by
  8.    the Free Software Foundation; either version 2 of the License, or
  9.    (at your option) any later version.
  10.  
  11.    This program is distributed in the hope that it will be useful,
  12.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.    GNU General Public License for more details.
  15.  
  16.    You should have received a copy of the GNU General Public License
  17.    along with this program; if not, write to the Free Software
  18.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.            */
  19.  
  20. #include "CFS.h"
  21. #include "dossupport.h"
  22.  
  23. #include <pragma/icon_lib.h>
  24.  
  25. #include <ctype.h>
  26. #include <string.h>
  27. #include <stdlib.h>
  28.  
  29. #define EOL '\n'         /* Seperates lines in option files. */
  30. #define STARTUP_SEP '!'  /* Seperates entries ("lines") in startup string. */
  31. #define QUOTE_CHAR '\"'  /* Will be stripped from startup string. ("*"->*) */
  32. #define DEFCHAR '='
  33. #define FAKEDEFCHAR '&'  /* Chars usable in options ('optname * option').  */
  34.  
  35. struct Library *IconBase;
  36.  
  37. static BOOL parse_bool_opt(glb glob, char *opt, BOOL *var){
  38.   debug(("parse_bool_opt(): '%s'.\n",opt));
  39.   if(MatchToolValue(opt, "OFF")) *var = FALSE;
  40.   else if(MatchToolValue(opt, "NO")) *var = FALSE;
  41.   else if(MatchToolValue(opt, "ON")) *var = TRUE;
  42.   else if(MatchToolValue(opt, "YES")) *var = TRUE;
  43.   else{
  44.     debug(("Illegal bolean value.\n"));
  45.     return FALSE;
  46.   }
  47.   debug(("Result: %ld.\n",*var));
  48.   return TRUE;
  49. }
  50.  
  51. /* This function can be used to set a boolean option. */
  52.  
  53. static BOOL bool_opt(glb glob, char *opt, BOOL *var, BOOL default_val){
  54.    if(!opt){
  55.       *var = default_val;
  56.       return TRUE;
  57.    }else{
  58.       return parse_bool_opt(glob, opt, var);
  59.    }
  60. }
  61.  
  62. /* These two functions can be used to set/sleanup string options. */
  63.  
  64. static void string_cleanup(glb glob, char **var){
  65.    if(*var){
  66.       dosfree(*var);
  67.       *var = NULL;
  68.    }
  69. }
  70.  
  71. static BOOL string_opt(glb glob, char *opt, char **var, char *default_val){
  72.    int len;
  73.    char *p;
  74.    
  75.    if(!opt){
  76.       /* Check for a default value of NULL. */
  77.       if(!default_val){
  78.          string_cleanup(glob, var);
  79.          *var = default_val;
  80.          return TRUE;
  81.       }else{
  82.          opt = default_val;
  83.       }
  84.    }
  85.    len = strlen(opt)+1;
  86.    if(!(p = dosalloc(len))){
  87.       OUTOFMEM;
  88.       return FALSE;
  89.    }
  90.    strcpy(p,opt);
  91.    string_cleanup(glob, var);
  92.    *var = p;
  93.    return TRUE;
  94. }
  95.  
  96. /* The option setting functions.
  97.  * Each function is passed the remainder of the options string as
  98.  * entered by the user, and is expected to set the appropriate option
  99.  * and return a boolean succes/failure.
  100.  *
  101.  * If called with a NULL argument, should set a default option.
  102.  *
  103.  * NOTE: A cleanup function will not be called without the corresponding
  104.  * set function being called first.
  105.  */
  106.  
  107. static BOOL opt_rootdir(glb glob, char *opt){
  108.    char default_name[MAXFILENAME+4];
  109.    
  110.    /* Get assign name to directory to serve as our default root. Obtained
  111.     * by pruning the first char of the device name (ie. XDH0:->DH0:). */
  112.    if(!glob->devname || !glob->devname[0]
  113.                      || strlen(glob->devname)>MAXFILENAME+1){
  114.       debug(("PANIC: Bad device name.\n"));
  115.       return FALSE;
  116.    }
  117.    strncpy(default_name,&glob->devname[1],MAXFILENAME+2);
  118.    strcat(default_name,":");
  119.    
  120.    return string_opt(glob, opt, &glob->xRootName, default_name);
  121. }
  122.  
  123. static void cleanup_rootdir(glb glob){
  124.    string_cleanup(glob, &glob->xRootName);
  125. }
  126.  
  127.  
  128. /* Option 'VOLUMENAME'. */
  129. static void cleanup_volname(glb glob){
  130.    string_cleanup(glob, &glob->uservolname);
  131. }
  132.  
  133. static BOOL opt_volname(glb glob, char *opt){
  134.   /* The default is NULL - this will signify to createvolnode() that it
  135.    * should create a default volume name.
  136.    */
  137.   cleanup_volname(glob);
  138.   if(!string_opt(glob, opt, &glob->uservolname, NULL)) return FALSE;
  139.   if(glob->volnode){
  140.     return SetVolumeNameVolNode(glob, glob->uservolname);
  141.   }else{
  142.     return TRUE;   /* createvolnode will set name. */
  143.   }
  144. }
  145.  
  146.  
  147. /* Option 'AUTOCOMPRESS'. */
  148. static BOOL opt_autocompress(glb glob, char *opt){
  149.    return bool_opt(glob, opt, &glob->autocompress, FALSE);
  150. }
  151.  
  152.  
  153. /* Option 'XSCAN'. */
  154. static BOOL opt_xscan(glb glob, char *opt){
  155.    return bool_opt(glob, opt, &glob->xscan, FALSE);
  156. }
  157.  
  158.  
  159. /* Option 'STEPDOWN'. */
  160. static BOOL opt_stepdown(glb glob, char *opt){
  161.    return bool_opt(glob, opt, &glob->stepdown, FALSE);
  162. }
  163.  
  164.  
  165. /* Option 'PACKMODE'. */
  166. static BOOL opt_packmode(glb glob, char *opt){
  167.    return string_opt(glob, opt, &glob->packmode, "NUKE");
  168. }
  169.  
  170. static void cleanup_packmode(glb glob){
  171.    string_cleanup(glob, &glob->packmode);
  172. }
  173.  
  174.  
  175. /* Option 'PASSWORD'. */
  176. static BOOL opt_password(glb glob, char *opt){
  177.    /* The default is NULL - no password (valid acc. to 'xpk.doc'). */
  178.    return string_opt(glob, opt, &glob->xpkpassword, NULL);
  179. }
  180.  
  181. static void cleanup_password(glb glob){
  182.    string_cleanup(glob, &glob->xpkpassword);
  183. }
  184.  
  185.  
  186. /* Option 'TRUNCATEONPACK'. */
  187. static BOOL opt_truncateonpack(glb glob, char *opt){
  188.    return bool_opt(glob, opt, &glob->truncateonpack, FALSE);
  189. }
  190.  
  191.  
  192. /* Option 'XPKPRIORITY'. */
  193. static BOOL opt_xpkpriority(glb glob, char *opt){
  194.    if(opt){
  195.       glob->xpksetpri = TRUE;
  196.       glob->xpkpri = atoi(opt);
  197.    }else{
  198.       glob->xpksetpri = FALSE;
  199.    }
  200.    return TRUE;
  201. }
  202.  
  203.  
  204. /* Option 'CREATEVOLUME'. */
  205. static BOOL opt_createvol(glb glob, char *opt){
  206.    return bool_opt(glob, opt, &glob->createvolnode, TRUE);
  207. }
  208.  
  209.  
  210. /* Option 'FAILONEXNEXT'. */
  211. static BOOL opt_failonexnext(glb glob, char *opt){
  212.    return bool_opt(glob, opt, &glob->FailOnExNext, TRUE);
  213. }
  214.  
  215.  
  216. /* Option 'COMPRESSREADWRITE'. */
  217. static BOOL opt_compressrw(glb glob, char *opt){
  218.    return bool_opt(glob, opt, &glob->compressreadwrite, TRUE);
  219. }
  220.       
  221.      
  222. /* Option 'ALLOWAPPEND'. */
  223. static BOOL opt_allowappend(glb glob, char *opt){
  224.    return bool_opt(glob, opt, &glob->allowappend, TRUE);
  225. }
  226.       
  227.      
  228. /* Option 'KILLSTARTUP'. */
  229. static void cleanup_kstartup(glb glob){
  230.    DevNode_Stuff_Startup_String(glob, glob->bcplstartup);
  231. }
  232.  
  233.  
  234. static BOOL opt_killstartup(glb glob, char *opt){
  235.    BOOL tmp;
  236.  
  237.    if(!bool_opt(glob, opt, &tmp, TRUE)){
  238.       return FALSE;
  239.    }
  240.    if(tmp){
  241.       DevNode_Stuff_Startup_String(glob, 0L);
  242.    }else{
  243.       cleanup_kstartup(glob);
  244.    }
  245.    return TRUE;
  246. }
  247.  
  248.  
  249. /* Option 'PORTNAME'. */
  250. static BOOL opt_arexxportname(glb glob, char *opt){
  251.    return string_opt(glob, opt, &glob->userarexxportname, NULL);
  252. }
  253.  
  254. /* Option 'ENVOYKLUDGE'. */
  255. static BOOL opt_envoykludge(glb glob, char *opt){
  256.    return bool_opt(glob, opt, &glob->EnvoyKludge, FALSE);
  257. }
  258.  
  259.  
  260. static void cleanup_arexxpname(glb glob){
  261.    string_cleanup(glob, &glob->userarexxportname);
  262. }
  263.  
  264.  
  265. /* The 'options table'. */
  266.  
  267. static struct opttable{
  268.    char *opt;
  269.    BOOL (*set)(glb, char *);     /* 'set' function. */
  270.    void (*cleanup)(glb);         /* 'cleanup' function. */
  271.    BOOL persistent;              /* True -> option can be set only once. */
  272. } opttable[] = {
  273.    "ROOTDIR",           opt_rootdir,         cleanup_rootdir,   TRUE,
  274.    "VOLUMENAME",        opt_volname,         cleanup_volname,   FALSE,
  275.    "AUTOCOMPRESS",      opt_autocompress,    NULL,              FALSE,
  276.    "XSCAN",             opt_xscan,           NULL,              FALSE,
  277.    "STEPDOWN",          opt_stepdown,        NULL,              FALSE,
  278.    "PACKMODE",          opt_packmode,        cleanup_packmode,  FALSE,
  279.    "PASSWORD",          opt_password,        cleanup_password,  FALSE,
  280.    "TRUNCATEONPACK",    opt_truncateonpack,  NULL,              FALSE,
  281.    "XPKPRIORITY",       opt_xpkpriority,     NULL,              FALSE,
  282.    "CREATEVOLUME",      opt_createvol,       NULL,              TRUE,
  283.    "FAILONEXNEXT",      opt_failonexnext,    NULL,              FALSE,
  284.    "KILLSTARTUP",    opt_killstartup,     cleanup_kstartup,  FALSE,
  285.    "COMPRESSREADWRITE", opt_compressrw,      NULL,              FALSE,
  286.    "ALLOWAPPEND",       opt_allowappend,     NULL,              FALSE,
  287.    "PORTNAME",          opt_arexxportname,   cleanup_arexxpname,TRUE,
  288.    "ENVOYKLUDGE",       opt_envoykludge,     NULL,              FALSE,
  289.    NULL
  290. };
  291.  
  292.  
  293. /* This function sets the options according to an option file (in
  294.  * char ** form as returned by buf2array()). The first time it is called
  295.  * initial should be TRUE to set default and persistent options.
  296.  * And initial should be TRUE ONLY the first time this is called!!!
  297.  */
  298. static BOOL SetOptions(glb glob, char **opts, BOOL initial){
  299.   struct opttable *p,*r;
  300.   char *q;
  301.   
  302.   {char **p;for(p=opts;*p;p++) debug(("'%s'\n",*p));}
  303.   for(p=opttable;p->opt;p++){
  304.     q = FindToolType((UBYTE **)opts, p->opt);
  305.     debug(("Setting option '%s': (%ld).\n",p->opt,!!q));
  306.     if((q && !p->persistent) || initial){
  307.       if( !(*p->set)(glob, q) ){
  308.     debug(("Error occured seting option '%s'.\n",p->opt));
  309.     goto optionerror;
  310.       }
  311.     }
  312.   }
  313.   /* All options successfull. */
  314.   if(initial) glob->optionsset = TRUE;
  315.   return TRUE;
  316.   
  317.  optionerror:
  318.   /* An error occured during option setting. Cleanup any set options
  319.    * if nessesary and return, reporting failure.
  320.    */
  321.   glob->badoption = p->opt;     /* For error report. */
  322.   if(initial){
  323.     glob->optionsset = FALSE;  /* Just to be safe. */
  324.     for(r=opttable;r<p;r++){
  325.       if(r->cleanup) (*r->cleanup)(glob);
  326.     }
  327.   }
  328.   return FALSE;
  329. }
  330.  
  331.  
  332. /* Set a single option. This is done by faking an option file with a single
  333.  * entry.
  334.  */
  335. BOOL set_option(glb glob, char *p){
  336.    char *array[2];
  337.    
  338.    debug(("set_option() received '%s'.\n",p));
  339.    array[0]=p;
  340.    array[1]=NULL;
  341.    return SetOptions(glob, array, FALSE);
  342. }
  343.  
  344.  
  345. /* Set a single option using an opt/val pair. */
  346. BOOL set_option_value(glb glob, char *opt, char *val){
  347.   char *p;
  348.   BOOL res;
  349.  
  350.   if(!(p=dosalloc(strlen(opt)+strlen("=")+strlen(val)+1))){
  351.     debug(("set_option_value(): no mem.\n"));
  352.     OUTOFMEM;
  353.     return FALSE;
  354.   }
  355.   strcpy(p,opt);
  356.   strcat(p,"=");
  357.   strcat(p,val);
  358.   res = set_option(glob, p);
  359.   dosfree(p);
  360.   return res;
  361. }
  362. static void CleanupAllOptions(glb glob){
  363.    struct opttable *p;
  364.    
  365.    if(glob->optionsset){
  366.       for(p=opttable; p->opt; p++){
  367.          if(p->cleanup) (*p->cleanup)(glob);
  368.       }
  369.       glob->optionsset = FALSE;
  370.    }
  371. }
  372.  
  373. /* Create string array (char**) from buffer (changing "sep" chars to '\0').
  374.  * Also removes whitespace at end of lines.
  375.  * The buffer MUST be (at least) 1 byte longer than 'len'.
  376.  * Returns NULL if not enough memory available.
  377.  */
  378. static char **buf2array(glb glob, char *buf, int len, char sep){
  379.   int i,j;
  380.   int numlines=0;
  381.   char **p,**q;
  382.   
  383.   debug(("String: '%s'.\n",buf));
  384.   for(i=0;i<len;i++) if(buf[i]==sep) numlines++;
  385.   /* The array size is numlines+2: one extra 'safe' line, and a 
  386.    * NULL-terminator. */
  387.   if(!(p=q=dosalloc(sizeof(*p)*(numlines+2)))){
  388.     OUTOFMEM;
  389.     return NULL;
  390.   }
  391.   *q++ = buf;
  392.   for(i=0;i<len;i++){
  393.     if(buf[i]==sep){
  394.       for(j=i-1; (j>=0) && buf[j] && isspace(buf[j]); j--){
  395.     debug(("Skipping '%lc'.\n",buf[j]));
  396.       }
  397.       buf[j+1]='\0';
  398.       *q++=&buf[i+1];
  399.     }
  400.   }
  401.   /* Fix the last line (may not be "sep" terminated). */
  402.   for(j=i-1; (j>=0) && buf[j] && isspace(buf[j]); j--);
  403.   buf[j+1]='\0';
  404.   if(!**(q-1)) *(q-1) = NULL; /* Remove last line if empty. */
  405.   return p;
  406. }
  407.  
  408.  
  409. static void deleteoptionfromarray(glb glob, char **array, char *opt){
  410.   char *p;
  411.   char **q;
  412.  
  413.   p = FindToolType((UBYTE **)array, opt);
  414.   if(!p) return;
  415.   /* Delete the entry corresponding to p. */
  416.   for(q=array; *q; q++){
  417.     if(*q<=p && (!q[1] || q[1] > p)) goto deleteit;
  418.   }
  419.   debug(("Strange: Bugus pointer returned by FindToolType().\n"));
  420.   return;
  421.  deleteit:
  422.   while(q[0] = q[1]) q++;
  423.   return;
  424. }
  425.  
  426.  
  427. static BOOL writearraytofile(glb glob, char **array, struct FileHandle *fh){
  428.   char **p;
  429.  
  430.   for(p=array; *p; p++){
  431.     if(!xWriteStr(glob, fh, *p)) return FALSE;
  432.     if(!xWriteStr(glob, fh, "\n")) return FALSE;
  433.   }
  434.   return TRUE;
  435. }
  436.  
  437.  
  438. /* Attempt to read an option file. If the file could be read, it sets
  439.  * the arg to point to a buffer containing the data. This buffer must
  440.  * be deallocated by the caller (using dosfree()).
  441.  * Returns TRUE if the file was found, FALSE otherwise.
  442.  * NOTE: The arg may be returned NULL even if TRUE is returned.
  443.  * This signifies an 'out of memory' condition.
  444.  */
  445. static BOOL ReadOptFile(glb glob, struct FileLock *parent, char *name,
  446.                         char **buf, LONG *size){
  447.    struct FileHandle *fh;
  448.    
  449.    *buf=NULL;
  450.    if(!(fh = xOpen(glob, parent, name, MODE_OLDFILE))) return FALSE;
  451.    *size = xSeek(glob, fh, 0, OFFSET_END);
  452.    if(*size==-1L){
  453.       xClose(glob, fh);
  454.       return FALSE;
  455.    }
  456.    *size = xSeek(glob, fh, 0, OFFSET_BEGINNING);
  457.    if(*size==-1L){
  458.       xClose(glob, fh);
  459.       return FALSE;
  460.    }
  461.    if(!(*buf = dosalloc(*size+1))){
  462.       OUTOFMEM;
  463.       xClose(glob, fh);
  464.       return TRUE;
  465.    }
  466.    if(*size != xRead(glob, fh, *buf, *size)){
  467.       xClose(glob, fh);
  468.       dosfree(buf);
  469.       *buf = NULL;
  470.       return FALSE;
  471.    }
  472.    xClose(glob, fh);
  473.    return TRUE;
  474. }
  475.  
  476.  
  477. static BOOL writeoption(glb glob, struct FileHandle *fh, char *opt, char *val){
  478.   if(!xWriteStr(glob, fh, opt)) return FALSE;
  479.   if(!xWriteStr(glob, fh, "=")) return FALSE;
  480.   if(!xWriteStr(glob, fh, val)) return FALSE;
  481.   if(!xWriteStr(glob, fh, "\n"))return FALSE;
  482.   return TRUE;
  483. }
  484.  
  485.  
  486. /* Set a single option, but make it permanent (in :.xfhrc).
  487.  *
  488.  * A little magic is neede here since we share the .xfhrc file with
  489.  * the user. So, first read it, find existing entry (if any), then
  490.  * write back the file, changing the entry in the process.
  491.  */
  492. BOOL SetOptionPermanent(glb glob, char *opt, char *val){
  493.   struct FileHandle *fh;
  494.   char *buf;
  495.   LONG len;
  496.   char **array;
  497.   BOOL res;
  498.   
  499.   if(!ReadOptFile(glob, glob->xrootlock, ALTOPTIONPATH, &buf, &len)){
  500.     debug((".xfhrc file nonexisting, creating new.\n"));
  501.     if(!(fh=xOpen(glob, glob->xrootlock, ALTOPTIONPATH, MODE_NEWFILE))){
  502.       debug(("Error: Unable to open file: %ld.\n",glob->ioerr));
  503.       return FALSE;
  504.     }
  505.     if(writeoption(glob, fh, opt, val)){
  506.       if(!xClose(glob, fh)) return FALSE;
  507.       return set_option_value(glob, opt, val);
  508.     }else{
  509.       SAVEIOERR;
  510.       xClose(glob, fh);
  511.       RESTIOERR;
  512.       return FALSE;
  513.     }
  514.   }
  515.   if(!buf) return FALSE;
  516.   /* Ok, there's a file there already. Change it. */
  517.   if(!(array = buf2array(glob, buf, len, EOL))){
  518.     dosfree(buf);
  519.     return FALSE;
  520.   }
  521.   deleteoptionfromarray(glob, array, opt);
  522.   if(!(fh=xOpen(glob, glob->xrootlock, ALTOPTIONPATH, MODE_NEWFILE))){
  523.     debug(("Error: Unable to open file again: %ld.\n",glob->ioerr));
  524.     dosfree(array);
  525.     dosfree(buf);
  526.     return FALSE;
  527.   }
  528.   res = writearraytofile(glob, array, fh);
  529.   dosfree(array);
  530.   dosfree(buf);
  531.   if(!res){
  532.     SAVEIOERR;
  533.     xClose(glob, fh);
  534.     RESTIOERR;
  535.     return FALSE;
  536.   }
  537.   if(!writeoption(glob, fh, opt, val)){
  538.     SAVEIOERR;
  539.     xClose(glob, fh);
  540.     RESTIOERR;
  541.     return FALSE;
  542.   }
  543.   if(!xClose(glob, fh)) return FALSE;
  544.   return set_option_value(glob, opt, val);
  545. }
  546.  
  547.  
  548. /* Called to read additional option files (after setting the initial
  549.  * options).
  550.  */
  551. BOOL SetOptionsFromFile(glb glob, struct FileLock *lock, char *name){
  552.    char *buf;
  553.    LONG len;
  554.    char **array;
  555.    BOOL res;
  556.    
  557.    if(!ReadOptFile(glob, lock, name, &buf, &len)) return TRUE;
  558.    if(!buf) return FALSE;
  559.    if(!(array = buf2array(glob, buf, len, EOL))){
  560.       dosfree(buf);
  561.       return FALSE;
  562.    }
  563.    res = SetOptions(glob, array, FALSE);
  564.    dosfree(array);
  565.    dosfree(buf);
  566.    return res;
  567. }
  568.  
  569.  
  570. /* Set options given in the "Startup" keyword in the mountlist.
  571.  * NOTE: this function will modify "startup".
  572.  * ONLY called by SetOptionsDayOne() !!!
  573.  */
  574. static BOOL SetOptionsFromStartup(glb glob, char *startup){
  575.   char **array;
  576.   char *p;
  577.   BOOL res;
  578.  
  579.   /* Change FAKEDEFCHAR to DEFCHAR (hack to fix bug in mount cmd). */
  580.   for(p=startup;*p;p++){
  581.     if(*p==FAKEDEFCHAR) *p=DEFCHAR;
  582.   }
  583.   if(!(array = buf2array(glob, startup, strlen(startup), STARTUP_SEP))){
  584.     return FALSE;
  585.   }
  586.   res = SetOptions(glob, array, TRUE);
  587.   debug(("SetOptions(): result %ld\n",res));
  588.   dosfree(array);
  589.   return res;
  590. }
  591.  
  592.  
  593. /* Set all options to their default, since there wasn't an option file.
  594.   * ONLY called by SetOptionsDayOne() !!!
  595.  */
  596. static BOOL SetDefaultOptions(glb glob){
  597.    static char *fakeopts[] = {NULL};
  598.    
  599.    /* Just pass SetOptions a fake, empty option file. */
  600.    return SetOptions(glob, fakeopts, TRUE);
  601. }
  602.  
  603.  
  604. /* Set options from primary option file.
  605.  * ONLY called by SetOptionsDayOne() !!!
  606.  */
  607. static BOOL SetOptionsFromPrimary(glb glob, char *filename){
  608.    char *buf;
  609.    LONG len;
  610.    char **array;
  611.    struct MsgPort *procid;
  612.    struct FileLock *lock;
  613.    BPTR bcpllock;
  614.    BOOL res;
  615.  
  616.    /* ToDo: This won't work with multi-assigns. */
  617.    procid = DoDeviceProc( (LONG *)&bcpllock, filename, glob );
  618.    /* ToDo: Nasty case if we get a non-null procid but a null lock.
  619.     * For now this is regarded as an error.
  620.     */
  621.    if( !procid || !bcpllock ){
  622.       return SetDefaultOptions(glob);
  623.    }
  624.    lock = b2c(bcpllock);
  625.  
  626.    if(!ReadOptFile(glob, lock, filename, &buf, &len)){
  627.        return SetDefaultOptions(glob);
  628.    }
  629.    if(!buf) return FALSE;
  630.    if(!(array = buf2array(glob, buf, len, EOL))){
  631.       dosfree(buf);
  632.       return FALSE;
  633.    }
  634.    res = SetOptions(glob, array, TRUE);
  635.    dosfree(array);
  636.    dosfree(buf);
  637.    return res;
  638. }
  639.  
  640.  
  641. /* Called ONCE to set the initial options.
  642.  * These may either come directly in the "Startup" string, or in an
  643.  * option file. */
  644. static BOOL SetOptionsDayOne(glb glob){
  645.    char *startup, *s2;
  646.    BOOL res;
  647.  
  648.    /* Check for missing startup string. */
  649.    if(!glob->bcplstartup) return SetDefaultOptions(glob);
  650.    if(!(startup = copybstr(glob->bcplstartup))){
  651.       debug(("Error: No memory for STARTUP string.\n"));
  652.       OUTOFMEM;
  653.       return FALSE;
  654.    }
  655.    /* Check for an empty startup string (""). */
  656.    if(!strcmp("",startup) || !strcmp("\"\"",startup)){
  657.       res = SetDefaultOptions(glob);
  658.    }else{
  659.       /* Check for a quoted string (ie. "foo" -> foo). */
  660.       if(startup[0]==startup[strlen(startup)-1] && startup[0]==QUOTE_CHAR){
  661.          startup[strlen(startup)-1] = '\0';
  662.          s2 = &startup[1];
  663.       }else{
  664.          s2 = startup;
  665.       }
  666.       debug(("Startup string: '%s'\n",s2));
  667.  
  668.       /* If the first character of the STARTUP string is STARTUP_SEP, the
  669.        * string is taken to be a list of option commands seperated by
  670.        * STARTUP_SEP. Otherwise, it is taken to be the name of an option
  671.        * file (which should have an absolute path).
  672.        */
  673.       if(s2[0]==STARTUP_SEP){
  674.          debug(("Setting options from string.\n"));
  675.          res = SetOptionsFromStartup(glob, &s2[1]);
  676.       }else{
  677.          debug(("Setting options from option file '%s'.\n",s2));
  678.          res = SetOptionsFromPrimary(glob, s2);
  679.       }
  680.    }
  681.    freestr(startup);
  682.    return res;
  683. }
  684.  
  685.  
  686. BOOL InitOptions(glb glob){
  687.  
  688.    glob->optionsset = FALSE;
  689.    
  690.    if(!(glob->IconBase = OpenLibrary("icon.library",0L))){
  691.       debug(("Error: InitOptions: Cannot open icon.library.\n"));
  692.       return FALSE;
  693.    }
  694.    IconBase = glob->IconBase;
  695.    
  696.    SetOptionsDayOne(glob);
  697. }
  698.  
  699. void CleanupOptions(glb glob){
  700.    
  701.    CleanupAllOptions(glob);
  702.    
  703.    if(glob->IconBase){
  704.       CloseLibrary(IconBase);
  705.       glob->IconBase = NULL;
  706.    }
  707. }
  708.  
  709.  
  710. /* End of options.c */
  711.