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