void sort(t_env *e, t_info *to_add, t_info **begin) { if (e->lowercase_r && e->t) reverse_time_sort(begin, to_add); else if (e->t) time_sort(begin, to_add); else if (e->lowercase_r) reverse_sort(begin, to_add); else ascii_sort(begin, to_add); }
void interactive_categorize() { char *s; FILE *input; if( (s = get_input_line("Sort current mailbox by category: ", "")) ) { if( s[0] != '\0' ) { cat[1].fullfilename = sanitize_path(s, extn); if( load_category(&cat[1]) && (input = fopen(emails.filename, "rb")) ) { sanitize_model_options(&m_options, &m_cp, &cat[1]); ephemeral_message("Please wait, recalculating scores"); /* loaded category successfully, now free old resources */ free_category(&cat[0]); memcpy(&cat[0], &cat[1], sizeof(category_t)); free_emails(); init_emails(); read_mbox_and_sort_list(input); if( u_options & (1<<U_OPTION_REVSORT) ) { reverse_sort(); recalculate_limited(); } fclose(input); } else { ephemeral_message("Sorry, the category could not be loaded"); } } free(s); } }
/* this is the main key processing loop for interactive mode */ void display_results_interactively() { bool_t done = 0; int c, c1; /* set up the terminal etc. */ SLang_init_tty(-1, 0, 0); SLtt_get_terminfo(); if(-1 == SLsmg_init_smg() ) { fprintf (stderr, "Unable to initialize terminal."); return; } SLkp_init(); /* for cursor keys */ SLsignal(SIGWINCH, sigwinch_handler); init_disp_state(); while(!done) { redraw_current_state(); c = SLang_getkey(); /* if it's a meta combination, translate */ if( c == '' ) { c1 = SLang_getkey(); switch(c1) { case '<': c = SL_KEY_HOME; break; case '>': c = SL_KEY_END; break; case 'v': case 'V': c = SL_KEY_PPAGE; break; case 'r': case 'R': c = '?'; break; case 's': case 'S': c = '/'; break; default: /* could be special cursor keys */ SLang_ungetkey(c1); SLang_ungetkey(c); c = SLkp_getkey(); } } /* handle key press */ switch(c) { case 'q': case 'Q': done = 1; break; case 'c': interactive_categorize(); disp.highlighted = disp.first_visible = 0; break; case '\r': case 'v': if( emails.num_limited > 0 ) { view_current_email(); } break; case SL_KEY_F(1): if( emails.num_limited > 0 && disp.fkey_cmd[0] ) { interactive_pipe_all_tagged(disp.fkey_cmd[0]); } break; case SL_KEY_F(2): if( emails.num_limited > 0 && disp.fkey_cmd[1] ) { interactive_pipe_all_tagged(disp.fkey_cmd[1]); } break; case SL_KEY_F(3): if( emails.num_limited > 0 && disp.fkey_cmd[2] ) { interactive_pipe_all_tagged(disp.fkey_cmd[2]); } break; case SL_KEY_F(4): if( emails.num_limited > 0 && disp.fkey_cmd[3] ) { interactive_pipe_all_tagged(disp.fkey_cmd[3]); } break; case SL_KEY_F(5): if( emails.num_limited > 0 && disp.fkey_cmd[4] ) { interactive_pipe_all_tagged(disp.fkey_cmd[4]); } break; case SL_KEY_F(6): if( emails.num_limited > 0 && disp.fkey_cmd[5] ) { interactive_pipe_all_tagged(disp.fkey_cmd[5]); } break; case SL_KEY_F(7): if( emails.num_limited > 0 && disp.fkey_cmd[6] ) { interactive_pipe_all_tagged(disp.fkey_cmd[6]); } break; case SL_KEY_F(8): if( emails.num_limited > 0 && disp.fkey_cmd[7] ) { interactive_pipe_all_tagged(disp.fkey_cmd[7]); } break; case SL_KEY_F(9): if( emails.num_limited > 0 && disp.fkey_cmd[8] ) { interactive_pipe_all_tagged(disp.fkey_cmd[8]); } break; case SL_KEY_F(10): if( emails.num_limited > 0 && disp.fkey_cmd[9] ) { interactive_pipe_all_tagged(disp.fkey_cmd[9]); } break; case 's': if( emails.num_limited > 0 ) { interactive_pipe_current(""); } break; case 'S': if( emails.num_limited > 0 ) { interactive_pipe_all_tagged(""); } break; case 'o': if( ++emails.score_type >= MAX_SCORES ) { emails.score_type = 0; } qsort(emails.list, emails.num_emails, sizeof(mbox_item), compare_scores); recalculate_limited(); break; case 'p': if( ++emails.index_format >= MAX_FORMATS ) { emails.index_format = 0; } break; case 't': if( emails.num_limited > 0 ) { tag_current(1); } break; case 'T': if( emails.num_limited > 0 ) { tag_all_limited(1); } break; case 'u': if( emails.num_limited > 0 ) { tag_current(0); } break; case 'U': if( emails.num_limited > 0 ) { tag_all_limited(0); } break; case 'z': reverse_sort(); recalculate_limited(); break; case 'G': case SL_KEY_END: if( emails.num_limited > 0 ) { disp.first_visible = emails.num_limited - 1; } else { disp.highlighted = disp.first_visible = 0; } break; case '1': case SL_KEY_HOME: disp.first_visible = 0; break; case '': case SL_KEY_PPAGE: if( disp.first_visible > disp.num_rows ) { disp.first_visible -= disp.num_rows; } else { disp.first_visible = 0; disp.highlighted = 0; } /* assert emails.num_limited >= disp.first_visible */ if( disp.highlighted > (emails.num_limited - disp.first_visible) ) { disp.highlighted = (emails.num_limited - disp.first_visible); } break; case 'k': case '': case SL_KEY_UP: if( disp.highlighted > 0 ) { disp.highlighted--; } else { if( disp.first_visible > 1 ) { disp.first_visible -= 1; } else { disp.first_visible = 0; } } break; case 'j': case '': case SL_KEY_DOWN: if( emails.num_limited > 0 ) { if( disp.highlighted < (emails.num_limited - disp.first_visible - 1) ) { if( disp.highlighted < (disp.num_rows - 3) ) { disp.highlighted++; } else { if( (disp.first_visible += 1) >= emails.num_limited ) { disp.first_visible = emails.num_limited - 1; } } } } else { disp.highlighted = disp.first_visible = 0; } break; case '': case '': case ' ': case SL_KEY_NPAGE: if( emails.num_limited > 0 ) { if( (disp.first_visible += disp.num_rows) >= emails.num_limited ) { disp.first_visible = emails.num_limited - 1; } if( disp.highlighted > (emails.num_limited - disp.first_visible) ) { disp.highlighted = (emails.num_limited - disp.first_visible) - 1; } } else { disp.highlighted = disp.first_visible = 0; } break; case '?': interactive_search(0, ""); disp.highlighted = disp.first_visible = 0; break; case '/': interactive_search(1, ""); disp.highlighted = disp.first_visible = 0; break; default: break; } } /* we're done */ SLsmg_reset_smg(); SLang_reset_tty(); }
int main(int argc, char **argv) { regex_count_t k; FILE *input; signed char op; void (*preprocess_fun)(void) = NULL; void (*postprocess_fun)(void) = NULL; progname = "mailinspect"; inputfile = "stdin"; inputline = 0; /* set up internationalization */ if( !setlocale(LC_ALL, "") ) { errormsg(E_WARNING, "could not set locale, internationalization disabled\n"); } else { if( u_options & (1<<U_OPTION_VERBOSE) ) { errormsg(E_WARNING, "international locales not supported\n"); } } #if defined(HAVE_GETPAGESIZE) system_pagesize = getpagesize(); #endif if( system_pagesize == -1 ) { system_pagesize = BUFSIZ; } /* parse the options */ while( (op = getopt(argc, argv, "c:g:G:iIjo:p:s:VzZ")) > -1 ) { switch(op) { case 'j': m_options |= (1<<M_OPTION_CASEN); break; case 'I': u_options |= (1<<U_OPTION_INTERACTIVE); break; case 'V': fprintf(stdout, "mailinspect version %s\n", VERSION); fprintf(stdout, COPYBLURB, "mailinspect"); exit(1); break; case 'z': u_options |= (1<<U_OPTION_REVSORT); break; case 'o': if( !*optarg ) { errormsg(E_ERROR, "you need a number with the -o switch\n"); usage(argv); exit(0); } u_options |= (1<<U_OPTION_SCORES); if( (emails.score_type = atoi(optarg)) >= MAX_SCORES ) { emails.score_type = 0; } break; case 'p': if( !*optarg ) { errormsg(E_ERROR, "you need a number with the -p switch\n"); usage(argv); exit(0); } u_options |= (1<<U_OPTION_FORMAT); if( (emails.index_format = atoi(optarg)) >= MAX_FORMATS ) { emails.index_format = 0; } break; case 'c': if( cat_count >= 1 ) { errormsg(E_WARNING, "maximum reached, category ignored\n"); } else { u_options |= (1<<U_OPTION_CLASSIFY); cat[cat_count].fullfilename = sanitize_path(optarg, ""); if( !*optarg ) { errormsg(E_ERROR, "category needs a name\n"); usage(argv); exit(0); } if( !load_category(&cat[cat_count]) ) { errormsg(E_FATAL, "could not load category %s\n", cat[cat_count].fullfilename); } sanitize_model_options(&m_options, &m_cp, &cat[cat_count]); cat_count++; } break; case 'G': case 'g': tagre_inclex[tagre_count] = (op == 'g') ? TAGRE_INCLUDE : TAGRE_EXCLUDE; if( regcomp(&tagre[tagre_count].regex, optarg, regcomp_flags) != 0 ) { errormsg(E_WARNING, "could not compile regular expression '%s', ignored\n", optarg); } else { tagre[tagre_count].string = optarg; tagre_count++; } break; case 'i': m_options |= (1<<M_OPTION_I18N); #if defined HAVE_LANGINFO_H if( !strcmp(nl_langinfo(CODESET), "UTF-8") ) { errormsg(E_WARNING, "you have UTF-8, so -i is not needed.\n"); } #endif break; case 's': execute_command = optarg; break; default: break; } } /* end option processing */ /* consistency checks */ if( !(u_options & (1<<U_OPTION_CLASSIFY)) ) { errormsg(E_ERROR, "please use -c option\n"); usage(argv); exit(0); } if( !(u_options & (1<<U_OPTION_FORMAT)) ) { if( u_options & (1<<U_OPTION_INTERACTIVE) ) { emails.index_format = 1; } else { emails.index_format = 0; } } if( !(u_options & (1<<U_OPTION_SCORES)) ) { emails.score_type = 0; } if( m_options & (1<<M_OPTION_CASEN) ) { regcomp_flags = REG_EXTENDED|REG_NOSUB; } else { regcomp_flags = REG_EXTENDED|REG_NOSUB|REG_ICASE; } /* setup */ if( m_options & (1<<M_OPTION_CALCENTROPY) ) { init_empirical(&empirical, default_max_tokens, default_max_hash_bits); /* sets cached to zero */ } init_file_handling(); init_emails(); /* now do processing */ if( preprocess_fun ) { (*preprocess_fun)(); } /* now process the first file on the command line */ if( (optind > -1) && *(argv + optind) ) { /* if it's a filename, process it */ if( (input = fopen(argv[optind], "rb")) ) { if( cat[0].model_num_docs == 0 ) { errormsg(E_WARNING, "category %s was not created for emails." " Messages may not be sorted optimally.\n", cat[0].filename); } if( u_options & (1<<U_OPTION_INTERACTIVE) ) { fprintf(stderr, "Reading %s, please wait...\n", argv[optind]); } emails.filename = argv[optind]; read_mbox_and_sort_list(input); fclose(input); if( u_options & (1<<U_OPTION_REVSORT) ) { reverse_sort(); } if( u_options & (1<<U_OPTION_INTERACTIVE) ) { #if defined COMPILE_INTERACTIVE_MODE display_results_interactively(); #else errormsg(E_ERROR, "\n" " sorry, interactive mode not available.\n" " Make sure you have libslang and libreadline\n" " on your system and reconfigure/recompile.\n"); #endif } else { if( execute_command ) { pipe_all_to_command(execute_command); } else { display_results(stdout); } } } else { errormsg(E_ERROR, "couldn't open %s\n", argv[optind]); usage(argv); exit(0); } } else { errormsg(E_ERROR, "no mbox file specified.\n"); usage(argv); exit(0); } if( postprocess_fun ) { (*postprocess_fun)(); } cleanup_file_handling(); for(k = 0; k < regex_count; k++) { regfree(&re[k].regex); } for(k = 0; k < antiregex_count; k++) { regfree(&re[k+MAX_RE].regex); } exit(1); }