int maxargs(A_stm stm) { switch (stm->kind) { case A_compoundStm: return max( maxargs(stm->u.compound.stm1), maxargs(stm->u.compound.stm2) ); case A_assignStm: return maxargs_exp(stm->u.assign.exp); case A_printStm: return max( count_exp(stm->u.print.exps), maxargs_expList(stm->u.print.exps) ); default: /* This should not happen! */ assert(!"Wrong kind-value for A_stm!"); } }
int maxargs(A_stm statement) { if (statement->kind == A_printStm) { A_expList exp_list = statement->u.print.exps; return max(count_exp_list(exp_list), max_exp_list(exp_list)); } else if (statement->kind == A_assignStm) { return max_exp(statement->u.assign.exp); } else if (statement->kind == A_compoundStm) { return max(maxargs(statement->u.compound.stm1), maxargs(statement->u.compound.stm2)); } else { return 0; } }
void cmd_switch(int argc, char **argv) { OPT_HELP("Switch between active connections. Usage:\n" " switch [options] [number | name]\n" "Options:\n" " -h, --help show this help\n" "The argument can either be the connection number, host name" " or its alias\n" "Without argument, switch to the next active connection\n"); maxargs(optind); if(argc > optind) { listitem *tmp = ftplist_search(argv[optind]); if(tmp) gvCurrentFtp = tmp; } else { if(gvCurrentFtp == gvFtpList->last) gvCurrentFtp = gvFtpList->first; else { if(gvCurrentFtp->next) gvCurrentFtp = gvCurrentFtp->next; } } ftp_use((Ftp *)gvCurrentFtp->data); }
int maxargs_exp(A_exp exp) { switch (exp->kind) { case A_idExp: return 0; case A_numExp: return 0; case A_opExp: return max( maxargs_exp(exp->u.op.left), maxargs_exp(exp->u.op.right) ); case A_eseqExp: return max( maxargs(exp->u.eseq.stm), maxargs_exp(exp->u.eseq.exp) ); default: /* This should not happen! */ assert(!"Wrong kind-value for A_exp->kind!"); } }
/* local change directory */ void cmd_lcd(int argc, char **argv) { char *e = 0, tmp[PATH_MAX+1]; char *te; OPT_HELP("Change local working directory. Usage:\n" " lcd [options] [directory]\n" "Options:\n" " -h, --help show this help\n" "if [directory] is '-', lcd changes to the previous working directory\n" "if omitted, changes to home directory\n"); maxargs(optind); if(argc < optind + 1) e = gvLocalHomeDir; else { e = argv[optind]; if(strcmp(e, "-") == 0) e = gvLocalPrevDir; } if(!e) return; getcwd(tmp, PATH_MAX); te = tilde_expand_home(e, gvLocalHomeDir); if(chdir(te) == -1) perror(te); else { free(gvLocalPrevDir); gvLocalPrevDir = xstrdup(tmp); } free(te); cmd_lpwd(0, 0); }
void test_maxargs_exp_op_nested(void** state) { A_stm stm = A_PrintStm(A_LastExpList(A_OpExp( A_OpExp( A_OpExp( A_EseqExp( A_PrintStm(A_PairExpList( A_NumExp(7), A_PairExpList( A_NumExp(8), A_LastExpList(A_NumExp(9)) ) )), A_NumExp(14) ), A_plus, A_NumExp(5) ), A_times, A_NumExp(19) ), A_minus, A_NumExp(42) ))); assert_int_equal(3, maxargs(stm)); }
void cmd_cd(int argc, char **argv) { char *e; #if 0 OPT_HELP("Change working directory. Usage:\n" " cd [options] [directory]\n" "Options:\n" " -h, --help show this help\n" "if [directory] is '-', cd changes to the previous working" " directory\n" "if omitted, changes to home directory\n"); maxargs(optind); #endif maxargs_nohelp(1); need_connected(); need_loggedin(); if(argc <= 1) e = ftp->homedir; else if(strcmp(argv[1], "-") == 0) { e = ftp->prevdir; if(e == 0) /* no previous directory */ return; } else e = argv[1]; e = tilde_expand_home(e, ftp->homedir); ftp_chdir(e); free(e); }
void cmd_url(int argc, char **argv) { int c; struct option longopts[] = { {"no-encoding", no_argument, 0, 'e'}, {"help", no_argument, 0, 'h'}, {0, 0, 0, 0} }; bool no_encoding = false; optind = 0; while((c = getopt_long(argc, argv, "eh", longopts, 0)) != EOF) { switch(c) { case 'h': show_help(_("Print the current URL."), "url [options]", _(" -e, --no-encoding don't encode URL as RFC1738 says\n")); return; case 'e': no_encoding = true; break; case '?': return; } } maxargs(optind - 1); need_connected(); need_loggedin(); printf("ftp://"); if(no_encoding) { printf("%s@%s", ftp->url->username, ftp->url->hostname); if(ftp->url->port != 21) printf(":%d", ftp->url->port); printf("/%s\n", ftp->curdir); return; } if(!url_isanon(ftp->url)) { char *e = encode_url_username(ftp->url->username); printf("%s@", e); free(e); } printf("%s", ftp->url->hostname); if(ftp->url->port != 21) printf(":%u", ftp->url->port); if(strcmp(ftp->curdir, ftp->homedir) != 0) { char *d; char *e = ftp->curdir; #if 0 if(strncmp(ftp->curdir, ftp->homedir, strlen(ftp->homedir)) == 0) e = ftp->curdir + strlen(ftp->homedir) + 1; #endif d = encode_url_directory(e); printf("/%s", d); free(d); } printf("\n"); }
void cmd_nop(int argc, char **argv) { OPT_HELP_NEW(_("Do nothing (send a NOOP command)."), "nop [options]", NULL); maxargs(optind - 1); need_connected(); ftp_noop(); }
static void test_maxargs_nested(void** state) { // maxargs(stm) can work with nested statements A_stm stm = A_CompoundStm( A_PrintStm(A_LastExpList(A_NumExp(7))), A_AssignStm("a", A_NumExp(7)) ); assert_int_equal(1, maxargs(stm)); }
int max_exp(A_exp exp) { if (exp->kind == A_idExp || exp->kind == A_numExp) { return 0; } else if (exp->kind == A_opExp) { return max(max_exp(exp->u.op.left), max_exp(exp->u.op.right)); } else { return max(maxargs(exp->u.eseq.stm), max_exp(exp->u.eseq.exp)); } }
int maxargs(A_stm root) { int left; int right; switch(root->kind) { case A_compoundStm: left = maxargs(root->u.compound.stm1); right = maxargs(root->u.compound.stm2); return left > right ? left : right; case A_assignStm: return maxargs_exp(root->u.assign.exp); case A_printStm: left = count_explist(root->u.print.exps); right = maxargs_explist(root->u.print.exps);; return left > right ? left : right; }; printf("should not arrived 2.\n"); return -1; }
void cmd_cdup(int argc, char **argv) { OPT_HELP_NEW(_("Change to parent working directory."), "cdup [options]", NULL); maxargs(optind - 1); need_connected(); need_loggedin(); ftp_cdup(); }
static void test_maxargs_two(void** state) { // maxargs(stm) returns 2 stm is an A_PrintStm with 2 members A_stm stm = A_PrintStm( A_PairExpList( A_NumExp(7), A_LastExpList(A_NumExp(42)) ) ); assert_int_equal(2, maxargs(stm)); }
void cmd_pwd(int argc, char **argv) { OPT_HELP_NEW(_("Print the current working directory."), "pwd [options]", NULL); maxargs(optind - 1); need_connected(); need_loggedin(); ftp_pwd(); }
void cmd_cache(int argc, char **argv) { int c; struct option longopts[] = { {"clear", no_argument, 0, 'c'}, {"list", no_argument, 0, 'l'}, {"touch", no_argument, 0, 't'}, {"help", no_argument, 0, 'h'}, {0, 0, 0, 0} }; bool touch = false; optind = 0; while((c = getopt_long(argc, argv, "clt::h", longopts, 0)) != EOF) { switch(c) { case 'c': ftp_cache_clear(); return; case 'l': ftp_cache_list_contents(); return; case 't': touch = true; break; case 'h': printf(_("Control the directory cache. Usage:\n" " cache [option] [directories]\n" "Options:\n" " -c, --clear clear whole directory cache\n" " -l, --list list contents of cache\n" " -t, --touch remove directories from cache\n" " if none given, remove current" " directory\n" " -h, --help show this help\n")); return; case '?': return; } } need_connected(); need_loggedin(); if(touch) { if(optind < argc) { int i; for(i = optind; i < argc; i++) ftp_cache_flush_mark(argv[i]); } else ftp_cache_flush_mark(ftp->curdir); } else { minargs(1); maxargs(0); } }
void cmd_cdup(int argc, char **argv) { OPT_HELP("Change to parent working directory. Usage:\n" " cdup [options]\n" "Options:\n" " -h, --help show this help\n"); maxargs(optind - 1); need_connected(); need_loggedin(); ftp_cdup(); }
void cmd_nop(int argc, char **argv) { OPT_HELP("Do nothing (send a NOOP command). Usage:\n" " nop [options]\n" "Options:\n" " -h, --help show this help\n"); maxargs(optind - 1); need_connected(); ftp_noop(); }
static void test_maxargs_multiple(void** state) { // maxargs(stm) works with multiple 'A_printStm's A_stm stm = A_CompoundStm( A_PrintStm(A_PairExpList( A_NumExp(7), A_LastExpList(A_NumExp(9)) )), A_PrintStm(A_LastExpList(A_NumExp(8))) ); assert_int_equal(2, maxargs(stm)); }
static void test_maxargs_assign(void** state) { // maxargs(stm) works with prints within assignments A_stm stm = A_AssignStm( "a", A_EseqExp( A_PrintStm(A_LastExpList(A_NumExp(7))), A_NumExp(42) ) ); assert_int_equal(1, maxargs(stm)); }
void cmd_mv(int argc, char **argv) { OPT_HELP_NEW(_("Rename or move a file."), "mv [options] <src> <dest>", NULL); minargs(optind + 1); maxargs(optind + 1); need_connected(); need_loggedin(); ftp_set_tmp_verbosity(vbError); if(ftp_rename(argv[optind], argv[optind + 1]) == 0) printf("%s -> %s\n", argv[optind], argv[optind + 1]); }
void cmd_idle(int argc, char **argv) { OPT_HELP_NEW(_("Get or set idle timeout."), "idle [options] [timeout]", _("Without the timeout option, print the current idle timeout\n")); maxargs(optind); need_connected(); need_loggedin(); if(argc - 1 == optind) ftp_idle(argv[optind]); else ftp_idle(0); }
void cmd_pwd(int argc, char **argv) { OPT_HELP("Print the current working directory. Usage:\n" " pwd [options]\n" "Options:\n" " -h, --help show this help\n"); maxargs(optind - 1); need_connected(); need_loggedin(); ftp_pwd(); }
static void test_maxargs_deep_nested(void** state) { // maxargs(stm) can work with deeply nested statements A_stm stm = A_CompoundStm( A_AssignStm("a", A_NumExp(0)), A_CompoundStm( A_PrintStm(A_PairExpList( A_NumExp(7), A_LastExpList(A_NumExp(42)) )), A_AssignStm("a", A_NumExp(5)) ) ); assert_int_equal(2, maxargs(stm)); }
void cmd_close(int argc, char **argv) { if(argv) { OPT_HELP_NEW(_("Close open connection"), "close [options]", NULL); maxargs(optind - 1); } ftp_quit(); if(list_numitem(gvFtpList) > 1) { list_delitem(gvFtpList, gvCurrentFtp); gvCurrentFtp = gvFtpList->first; ftp_use((Ftp *)gvCurrentFtp->data); } }
void test_maxargs_exp_op(void** state) { A_stm stm = A_PrintStm(A_LastExpList(A_OpExp( A_EseqExp( A_PrintStm(A_PairExpList( A_NumExp(8), A_LastExpList(A_NumExp(9)) )), A_NumExp(7) ), A_plus, A_NumExp(17) ))); assert_int_equal(2, maxargs(stm)); }
static void test_maxargs_print_nested(void** state) { // maxargs(stm) works with prints within prints A_stm stm = A_PrintStm(A_LastExpList( A_EseqExp( A_PrintStm( A_PairExpList( A_NumExp(7), A_LastExpList(A_NumExp(8)) ) ), A_NumExp(42) ) )); assert_int_equal(2, maxargs(stm)); }
void cmd_rstatus(int argc, char **argv) { OPT_HELP_NEW(_("Show status of remote host."), "rstatus [options]", NULL); maxargs(optind - 1); need_connected(); #ifdef HAVE_LIBSSH if(ftp->session) { printf("No status for SSH connection\n"); return; } #endif ftp_set_tmp_verbosity(vbCommand); ftp_cmd("STAT"); }
void cmd_mv(int argc, char **argv) { OPT_HELP("Rename or move a file. Usage:\n" " mv [options] <src> <dest>\n" "Options:\n" " -h, --help show this help\n"); minargs(optind + 1); maxargs(optind + 1); need_connected(); need_loggedin(); ftp_set_tmp_verbosity(vbError); if(ftp_rename(argv[optind], argv[optind + 1]) == 0) printf("%s -> %s\n", argv[optind], argv[optind + 1]); }
void cmd_idle(int argc, char **argv) { OPT_HELP("Get or set idle timeout. Usage:\n" " idle [options] [timeout]\n" "Options:\n" " -h, --help show this help\n" "Without the timeout option, print the current idle timeout\n"); maxargs(optind); need_connected(); need_loggedin(); if(argc - 1 == optind) ftp_idle(argv[optind]); else ftp_idle(0); }