home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / misc / volume39 / ncftp / part04 / set.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-08-25  |  7.6 KB  |  360 lines

  1. /* Set.c */
  2.  
  3. /*  $RCSfile: set.c,v $
  4.  *  $Revision: 14020.12 $
  5.  *  $Date: 93/07/09 11:45:48 $
  6.  */
  7.  
  8. #include "sys.h"
  9.  
  10. #include <string.h>
  11. #include <ctype.h>
  12.  
  13. #include "util.h"
  14. #include "cmds.h"
  15. #include "main.h"
  16. #include "set.h"
  17. #include "defaults.h"
  18. #include "copyright.h"
  19.  
  20. /* Set.c globals: */
  21. char *verbose_msgs[4] = {
  22.     "Not printing anything.\n",
  23.     "Only printing necessary error messages.\n",
  24.     "Printing error messages and announcements from the remote host.\n",
  25.     "Printing all messages, errors, acknowledgments, and announcements.\n"
  26. };
  27.  
  28. char *short_verbose_msgs[4] = {
  29.     "Quiet (-1)",
  30.     "Errors Only (0)",
  31.     "Terse (1)",
  32.     "Verbose (2)"
  33. };
  34.  
  35. string                        vstr;
  36.  
  37. /* Set.c externs: */
  38. extern int                    progress_meter, connected;
  39. extern int                    parsing_rc, keep_recent;
  40. extern string                pager, anon_password, prompt;
  41. extern str32                curtypename;
  42. extern long                    logsize;
  43. extern FILE                    *logf;
  44. extern longstring            rcname, logfname, lcwd;
  45. extern int                    auto_binary, ansi_escapes, debug;
  46. extern int                    mprompt, remote_is_unix, verbose;
  47. extern int                    startup_msg, anon_open;
  48. #ifndef NO_TIPS
  49. extern int                    tips;
  50. #endif
  51. #ifdef GATEWAY
  52. extern string                gateway, gate_login;
  53. #endif
  54.  
  55. /* The variables must be sorted in alphabetical order, or else
  56.  * match_var() will choke.
  57.  */
  58. struct var vars[] = {
  59.     VARENTRY("anon-open",        BOOL, 0, &anon_open,    NULL),
  60.     VARENTRY("anon-password",    STR,  0, anon_password,    NULL),
  61.     VARENTRY("ansi-escapes",    BOOL, 0, &ansi_escapes,    NULL),
  62.     VARENTRY("auto-binary",        BOOL, 0, &auto_binary,    NULL),
  63.     VARENTRY("debug",            INT,  0, &debug,        NULL),
  64. #ifdef GATEWAY
  65.     VARENTRY("gateway-login",    STR,  0, gate_login,    set_gatelogin),
  66.     VARENTRY("gateway-host",    STR,  0, gateway,        NULL),
  67. #endif
  68.     VARENTRY("local-dir",        STR,  0, lcwd,            set_ldir),
  69.     VARENTRY("logfile",            STR,  0, logfname,        set_log),
  70.     VARENTRY("logsize",            LONG, 0, &logsize,        NULL),
  71.     VARENTRY("mprompt",            BOOL, 0, &mprompt,        NULL),
  72.     VARENTRY("netrc",            -STR, 0, rcname,        NULL),
  73.     VARENTRY("pager",            STR,  0, pager + 1,        set_pager),
  74.     VARENTRY("prompt",            STR,  0, prompt,        set_prompt),
  75.     VARENTRY("progress-reports",INT,  0, &progress_meter,NULL),
  76.     VARENTRY("recent-list",        BOOL, 0, &keep_recent,    NULL),
  77.     VARENTRY("remote-is-unix",    BOOL, 1, &remote_is_unix,NULL),
  78.     VARENTRY("startup-msg",        BOOL, 0, &startup_msg,    NULL),  /* TAR */
  79. #ifndef NO_TIPS
  80.     VARENTRY("tips",            BOOL, 0, &tips,            NULL),
  81. #endif
  82.     VARENTRY("type",            STR,  1, curtypename,    set_type),
  83.     VARENTRY("verbose",            STR,  0, vstr,            set_verbose),
  84. };
  85.  
  86.  
  87. void set_verbose(char *new, int unset)
  88. {
  89.     int i, c;
  90.  
  91.     if (unset == -1) verbose = !verbose;
  92.     else if (unset || !new) verbose = V_ERRS;
  93.     else {
  94.         if (isalpha(*new)) {
  95.             c = islower(*new) ? toupper(*new) : *new;    
  96.             for (i=0; i<(int)(sizeof(short_verbose_msgs)/sizeof(char *)); i++) {
  97.                 if (short_verbose_msgs[i][0] == c)
  98.                     verbose = i - 1;
  99.             }
  100.         } else {
  101.             i = atoi(new);
  102.             if (i < V_QUIET) i = V_QUIET;
  103.             else if (i > V_VERBOSE) i = V_VERBOSE;
  104.             verbose = i;
  105.         }
  106.     }
  107.     (void) Strncpy(vstr, short_verbose_msgs[verbose+1]);
  108.     if (!parsing_rc && NOT_VQUIET) 
  109.         (void) fputs(verbose_msgs[verbose+1], stdout);
  110. }    /* set_verbose */
  111.  
  112.  
  113.  
  114.  
  115. void set_prompt(char *new, int unset)
  116. {
  117.     (void) Strncpy(prompt, (unset || !new) ? dPROMPT : new);
  118.     init_prompt();
  119. }    /* set_prompt */
  120.  
  121.  
  122.  
  123.  
  124. void set_log(char *fname, int unset)
  125. {
  126.     if (logf) {
  127.         (void) fclose(logf);
  128.         logf = NULL;
  129.     }
  130.     if (!unset && fname) {
  131.         (void) Strncpy(logfname, fname);
  132.         logf = fopen (LocalDotPath(logfname), "a");
  133.     }
  134. }    /* set_log */
  135.  
  136.  
  137.  
  138.  
  139. void set_pager(char *new, int unset)
  140. {
  141.     if (unset)
  142.         (void) strcpy(pager, "-");
  143.     else {
  144.         if (!new)
  145.             new = dPAGER;
  146.         if (!new[0])
  147.             (void) Strncpy(pager, "-");
  148.         else {
  149.             (void) sprintf(pager, "|%s", (*new == '|' ? new + 1 : new));
  150.             (void) LocalPath(pager + 1);
  151.         }
  152.     }
  153. }    /* set_pager */
  154.  
  155.  
  156.  
  157.  
  158. void set_type(char *newtype, int unset)
  159. {
  160.     int t = verbose;
  161.     verbose = V_QUIET;
  162.     if (!connected && t > V_QUIET)
  163.         (void) printf("Not connected.\n");
  164.     else if (newtype != NULL && !unset)
  165.         (void) _settype(newtype);
  166.     verbose = t;
  167. }    /* set_type */
  168.  
  169.  
  170.  
  171.  
  172. void set_ldir(char *ldir, int unset)
  173. {
  174.     int t = verbose;
  175.     char *argv[2];
  176.  
  177.     if (ldir && !unset) {
  178.         verbose = V_QUIET;
  179.         argv[1] = ldir;
  180.         (void) lcd(2, argv);
  181.         verbose = t;
  182.     }
  183. }    /* set_ldir */
  184.  
  185.  
  186.  
  187.  
  188. #ifdef GATEWAY
  189. void set_gatelogin(char *glogin, int unset)
  190. {
  191.     if (unset || !glogin) {
  192.         gate_login[0] = gateway[0] = 0;
  193.     } else
  194.         (void) strcpy(gate_login, glogin);
  195. }    /* set_gatelogin */
  196. #endif
  197.  
  198.  
  199.  
  200.  
  201. struct var *match_var(char *varname)
  202. {
  203.     int i, c, ambig;
  204.     struct var *v;
  205.  
  206.     c = strlen(varname);
  207.     for (i=0, v=vars; i<NVARS; i++, v++) {
  208.         if (strcmp(v->name, varname) == 0)
  209.             return v;    /* exact match. */
  210.         if (c < v->nmlen) {
  211.             if (strncmp(v->name, varname, c) == 0) {
  212.                 /* Now make sure that it only matches one var name. */
  213.                 if (c >= v[1].nmlen || (i == (NVARS - 1)))
  214.                     ambig = 0;
  215.                 else
  216.                     ambig = !strncmp(v[1].name, varname, c);
  217.                 if (!ambig)
  218.                     return v;
  219.                 (void) fprintf(stderr, "%s: ambiguous variable name.\n", varname);
  220.                 goto xx;
  221.             }
  222.         }
  223.     }
  224.     (void) fprintf(stderr, "%s: unknown variable.\n", varname);
  225. xx:
  226.     return ((struct var *)0);
  227. }    /* match_var */
  228.  
  229.  
  230.  
  231.  
  232. void show_var(struct var *v)
  233. {
  234.     int c;
  235.  
  236.     if (v != (struct var *)0) {
  237.         (void) printf("%-20s= ", v->name);
  238.         c = v->type;
  239.         if (c < 0) c = -c;
  240.         if (v->conn_required && !connected)
  241.             (void) printf("(not connected)\n");
  242.         else switch (c) {
  243.             case INT:
  244.                 (void) printf("%d\n", *(int *)v->var); break;
  245.             case LONG:
  246.                 (void) printf("%ld\n", *(long *)v->var); break;
  247.             case STR:
  248.                 (void) printf("\"%s\"\n", (char *)v->var); break;
  249.             case BOOL:
  250.                 (void) printf("%s\n", *(int *)v->var == 0 ? "no" : "yes");
  251.         }
  252.     }
  253. }    /* show_var */
  254.  
  255.  
  256.  
  257.  
  258. void show(char *varname)
  259. {
  260.     int i;
  261.     struct var *v;
  262.  
  263.     if ((varname == NULL)    /* (Denotes show all vars) */
  264.         || (strcmp("all", varname) == 0))
  265.     {
  266.         for (i=0; i<NVARS; i++)
  267.             show_var(&vars[i]);
  268.     } else {
  269.         if ((v = match_var(varname)) != (struct var *)0)
  270.             show_var(v);
  271.     }
  272. }    /* show */
  273.  
  274.  
  275.  
  276.  
  277. int do_show(int argc, char **argv)
  278. {
  279.     int i;
  280.  
  281.     if (argc < 2)
  282.         show(NULL);
  283.     else
  284.         for (i=1; i<argc; i++)
  285.             show(argv[i]);
  286.     return NOERR;
  287. }    /* do_show */
  288.  
  289.  
  290.  
  291.  
  292. int set(int argc, char **argv)
  293. {
  294.     int unset;
  295.     struct var *v;
  296.     char *var, *val = NULL;
  297.  
  298.     if (argc < 2 || strncmp(argv[1], "all", (size_t)3) == 0) {
  299.         show(NULL);        /* show all variables. */
  300.     } else {
  301.         unset = argv[0][0] == 'u';
  302.         var = argv[1];
  303.         if (argc > 2) {
  304.             /* could be '= value' or just 'value.' */
  305.             if (*argv[2] == '=') {
  306.                 if (argc > 3)
  307.                     val = argv[3];
  308.                 else return USAGE;    /* can't do 'set var =' */
  309.             } else
  310.                 val = argv[2];
  311.             if (val[0] == 0)
  312.                 val = NULL;
  313.         }
  314.         v = match_var(var);
  315.         if (v != NULL) {
  316.             if (v->conn_required && !connected)
  317.                 (void) fprintf(stderr, "%s: must be connected.\n", var);
  318.             else if (v->type < 0)    
  319.                 (void) fprintf(stderr, "%s: read-only variable.\n", var);
  320.             else if (v->proc != (setvarproc) 0) {
  321.                 (*v->proc)(val, unset);        /* a custom set proc. */
  322.             } else if (unset) switch(v->type) {
  323.                 case BOOL:
  324.                 case INT:
  325.                     *(int *) v->var = 0; break;
  326.                 case LONG:
  327.                     *(long *) v->var = 0; break;
  328.                 case STR:
  329.                     *(char *) v->var = 0; break;
  330.             } else {
  331.                 if (val == NULL) switch(v->type) {
  332.                     /* User just said "set varname" */
  333.                     case BOOL:
  334.                     case INT:
  335.                         *(int *) v->var = 1; break;
  336.                     case LONG:
  337.                         *(long *) v->var = 1; break;
  338.                     case STR:
  339.                         *(char *) v->var = 0; break;
  340.                 } else {
  341.                     /* User said "set varname = value" */
  342.                     switch (v->type) {
  343.                         case BOOL:
  344.                             *(int *)v->var = StrToBool(val); break;
  345.                         case INT:
  346.                             (void) sscanf(val, "%d", (int *) v->var); break;
  347.                         case LONG:
  348.                             (void) sscanf(val, "%ld", (long *) v->var); break;
  349.                         case STR:
  350.                             (void) strcpy(v->var, val); break;
  351.                     }
  352.                 }
  353.             }
  354.         }
  355.     }
  356.     return NOERR;
  357. }    /* set */
  358.  
  359. /* eof Set.c */
  360.