int get_opt(Tasks *entry, const char *opts) { if (entry == NULL) { WARNING("Bad parameter -> entry == NULL."); return -1; } if (opts == NULL) { WARNING("Bad parameter -> opts == NULL."); return -1; } int option = 0; do { show_tasks(entry); if (STRCMP(opts, ==, "yn")) printf("Are you sure? <y/n>: "); else printf("action: "); option = get_char(); } while (!opt_is_valid(opts, option));
void process_command(char* cmd){ char* CMD[10]; memset(CMD,0,10); int i,pid,total,status; total=0; split_by_space(CMD,cmd,&total); if (total==0) return; if (strcmp(CMD[0],"clear")==0){ clear_screen(); } else if (strcmp(CMD[0],"run")==0){ if (total<2 || (pid=run(CMD[1]))==-1){ display_string("this program is not exists or already running!\n"); } else{ display_string("run successful "); display_string("pid = "); display_int(pid); display_string(" name = "); display_string(CMD[1]); display_string("\n"); } } else if (strcmp(CMD[0],"sleep")==0){ if (total<2 || sleep(CMD[1])==-1){ } else{ display_string("Process "); display_string(CMD[1]); display_string(" is sleeping now!\n"); } } else if (strcmp(CMD[0],"kill")==0){ if (total<2 || !is_number(CMD[1])){ display_string("Usage: kill <pid>\n"); } else if (kill(CMD[1])==-1){ display_string("No such process\n"); } else{ display_string("The process with pid="); display_string(CMD[1]); display_string(" was stopped\n"); } } else if (strcmp(CMD[0],"killall")==0){ if (total<2){ display_string("Usage: killall <name>"); } else{ if (killall(CMD[1])==-1){ display_string("No such process\n"); } else{ display_string("the process with name="); display_string(CMD[1]); display_string(" was stopped\n"); } } } else if (strcmp(CMD[0],"setp")==0){ if (total<3 || !is_number(CMD[2])){ display_string("Usage: setp <name> <num>\n"); } else if (setp(CMD[1],atoi(CMD[2]))==-1){ display_string("i can't set the priority of "); display_string(CMD[1]); display_string("\n"); } else { display_string("set priority successful\n"); } } else if (strcmp(CMD[0],"ps")==0){ if (show_tasks(CMD[1])==-1){ display_string("I can't recognize the parameter\n"); } } else if (strcmp(CMD[0],"ls")==0){ ls(); } else if (strcmp(CMD[0],"rm")==0){ if (total<2){ display_string("Usage: rm <filename>\n"); } else{ if (rm(CMD[1])==-1){ display_string(CMD[1]); display_string(" no exist!\n"); } } } else if (strcmp(CMD[0],"help")==0){ show_help(); } else if (strcmp(CMD[0],"edit")==0){ if (total==1){ display_string("Usage: edit <filename>\n"); } else start_editor(CMD[1]); } else if (strcmp(CMD[0],"dump")==0){ if (dump_mem(CMD[1],CMD[2])==-1){ display_string("I can't recognize the parameter\n"); } } else{ display_string(cmd); display_string(": "); display_string("no such command\n"); } }
/** * Function reads console params and calls appropriate functions. * */ int main(int agrc, char * agrv[]) { int i, j, limit=50, desc=0; char sort_by[COL_TITLE_LEN] = "date"; struct category category_to_add; struct task task_to_add; DIR* dir = opendir(".."); struct dirent* dent; FILE* fptr; regex_t et_schedule; regcomp(&et_schedule, "^et_.*$", 0); for(i=1;i<agrc;i++) { if(strcmp(agrv[i], "-f") == 0) { if(i+1 < agrc) set_output_format(agrv[i+1]); } } for(i=1;i<agrc;i++) { if(strcmp(agrv[i], "-a") == 0) { for(j=1;j<agrc;j++) { if(strcmp(agrv[j], "-c") == 0) { get_category_values(agrc, agrv, &category_to_add); throw_errors(); add_category(&category_to_add); throw_errors(); return 0; } else if(strcmp(agrv[j], "-t") == 0) { get_task_values(agrc, agrv, &task_to_add); throw_errors(); add_task(&task_to_add); throw_errors(); return 0; } } } else if(strcmp(agrv[i], "-ac") == 0 || strcmp(agrv[i], "-ca") == 0) { get_category_values(agrc, agrv, &category_to_add); throw_errors(); add_category(&category_to_add); throw_errors(); return 0; } else if(strcmp(agrv[i], "-at") == 0 || strcmp(agrv[i], "-ta") == 0) { get_task_values(agrc, agrv, &task_to_add); throw_errors(); add_task(&task_to_add); throw_errors(); return 0; } } for(i=1;i<agrc;i++) { if(strcmp(agrv[i], "-c") == 0) { show_categories(); throw_errors(); } else if(strcmp(agrv[i], "-t") == 0) { if(i+1 < agrc && agrv[i+1][0] != '-') { /*GET sql info about the task*/ dir = opendir("/home/hafron/.pal/"); if(dir) { while((dent=readdir(dir))) { if( ! regexec(&et_schedule, dent->d_name, 0, NULL, 0) ) { puts(dent->d_name); if(fptr = fopen(dent->d_name,"r")) { fclose(fptr) ; } else { error("CANNOT_OPEN_SCHEDULE_FILE"); throw_errors(); } } } closedir(dir); } else { error("CANNOT_OPEN_PAL_DIRECTORY"); throw_errors(); } } else { for(j=1;j<agrc;j++) { if(strcmp(agrv[j], "-l") == 0) { if(j+1 < agrc && agrv[j+1][0] != '-') limit = atoi(agrv[j+1]); } else if(strcmp(agrv[j], "-d") == 0) { desc = 1; } else if(strcmp(agrv[j], "-s") == 0) { if(j+1 < agrc && agrv[j+1][0] != '-') strncpy(sort_by, agrv[j+1], COL_TITLE_LEN); } } show_tasks(limit, sort_by, desc); } throw_errors(); } } return 0; }