Ejemplo n.º 1
0
/* 刷屏和读取选项  */
int read_select(void) {			
	int c;

	system("clear");

	printf("**********************************************\n");
	printf("*      Math Class Stystem                    *\n");
	printf("*         1)  Find                           *\n");
	printf("*         2)  Insert                         *\n");
	printf("*         3)  Delete                         *\n");
	printf("*         4)  List                           *\n");
	printf("*         5)  Erase All                      *\n");
//	printf("*         6)  Delete duplication             *\n");
	printf("*         7)  Update                         *\n");
	printf("*         8)  Sort                           *\n");
	printf("*         9)  Save to file                   *\n");
 //printf("*         @)  Load from file                 *\n");
	printf("*         0)  Exit                           *\n");
	printf("**********************************************\n");
	printf("\nPlease enter your choose:");

	c = getchar();	/* 这两行用于使程序暂停,获取选项后程序返回 */
	any_key();
	return c;
}
Ejemplo n.º 2
0
static void
rewrite_folder(void)
{
    register FILE  *src, *dst;
    char           *oldfile;
    register int    c;
    register long   cnt;
    register article_header *ah, **ahp;
    register article_number n;

    if (strchr(backup_folder_path, '/'))
	oldfile = backup_folder_path;
    else
	oldfile = relative(nn_directory, backup_folder_path);

    if (move_file(group_path_name, oldfile, 1) < 0) {
	tprintf("\r\n\nCannot backup folder in %s\n", oldfile);
	goto confirm;
    }
    if ((src = open_file(oldfile, OPEN_READ)) == NULL) {
	tprintf("\rCannot open %s\n\r", oldfile);
	goto move_back;
    }
    if ((dst = open_file(group_path_name, OPEN_CREATE)) == NULL) {
	fclose(src);
	tprintf("\rCannot create %s\n\r", group_path_name);
	goto move_back;
    }
    sort_articles(-2);

    tprintf("\rCompressing folder...\n\r");
    fl;

    get_folder_type(src);

    for (ahp = articles, n = n_articles; --n >= 0; ahp++) {
	ah = *ahp;
	cnt = ah->lpos - ah->hpos;
	if (folder_rewrite_trace)
	    tprintf("%s\t%s (%ld-%ld=%ld)\n\r",
		    ah->attr == A_CANCEL ? "CANCEL" : "KEEP",
		    ah->subject, ah->hpos, (long) (ah->lpos), cnt);
	if (ah->attr == A_CANCEL)
	    continue;
	fseek(src, ah->hpos, 0);
	mailbox_format(dst, -1);
	while (--cnt >= 0) {
	    if ((c = getc(src)) == EOF)
		break;
	    putc(c, dst);
	}
	mailbox_format(dst, 0);
    }

    fclose(src);
    if (fclose(dst) == EOF)
	goto move_back;
    if (!keep_backup_folder)
	unlink(oldfile);
    if (folder_rewrite_trace)
	goto confirm;
    return;

move_back:
    if (move_file(oldfile, group_path_name, 2) == 0)
	tprintf("Cannot create new file -- Folder restored\n\r");
    else
	tprintf("Cannot create new file\n\n\rFolder saved in %s\n\r", oldfile);

confirm:
    any_key(0);
}
Ejemplo n.º 3
0
int
folder_menu(char *path, int mode)
{
    FILE           *folder;
    register article_header *ah;
    news_header_buffer dgbuf;
    char            buffer[256];
    int             more, length, re, menu_cmd, was_raw;
    memory_marker   mem_marker;
    group_header    fake_group;
    int             cc_save;
    char            folder_name[FILENAME], folder_file[FILENAME];
    int             orig_layout;
    char           *orig_hdr_lines;

    orig_layout = fmt_linenum;
    orig_hdr_lines = header_lines;

    strcpy(folder_name, path);
    fake_group.group_name = folder_name;
    fake_group.kill_list = NULL;
    if (!expand_file_name(folder_file, folder_name, 1))
	return ME_NO_REDRAW;
    fake_group.archive_file = path = folder_file;
    fake_group.next_group = fake_group.prev_group = NULL;
    fake_group.group_flag = G_FOLDER | G_FAKED;
    fake_group.master_flag = 0;
    fake_group.save_file = NULL;
    current_group = NULL;
    init_group(&fake_group);

    folder = open_file(path, OPEN_READ);
    if (folder == NULL) {
	msg("%s not found", path);
	return ME_NO_REDRAW;
    }
    switch (get_folder_type(folder)) {
	case 0:
	    msg("Reading: %-.65s", path);
	    break;
	case 1:
	case 2:
	    msg("Reading %s folder: %-.50s",
		current_folder_type == 1 ? "mail" : "mmdf", path);
	    break;
	default:
	    msg("Folder is empty");
	    fclose(folder);
	    return ME_NO_REDRAW;
    }
    rewind(folder);

    was_raw = no_raw();
    s_keyboard = 0;

    current_group = &fake_group;

    mark_memory(&mem_marker);

    ah = alloc_art();

    more = 1;
    while (more && (more = get_digest_article(folder, dgbuf)) >= 0) {
	if (s_keyboard)
	    break;

	ah->a_number = 0;
	ah->flag = A_FOLDER;
	ah->attr = 0;

	ah->lines = digest.dg_lines;

	ah->hpos = digest.dg_hpos;
	ah->fpos = digest.dg_fpos;
	ah->lpos = digest.dg_lpos;

	if (digest.dg_from) {
	    length = pack_name(buffer, digest.dg_from, Name_Length);
	    ah->sender = alloc_str(length);
	    strcpy(ah->sender, buffer);
	    ah->name_length = length;
	    if (mode == 1)
		fold_string(ah->sender);
	} else {
	    ah->sender = "";
	    ah->name_length = 0;
	}

	if (digest.dg_subj) {
	    length = pack_subject(buffer, digest.dg_subj, &re, 255);
	    ah->replies = re;
	    ah->subject = alloc_str(length);
	    strcpy(ah->subject, buffer);
	    ah->subj_length = length;
	    if (mode == 1 && length > 1)
		fold_string(ah->subject + 1);
	} else {
	    ah->replies = 0;
	    ah->subject = "";
	    ah->subj_length = 0;
	}

	ah->t_stamp = digest.dg_date ? pack_date(digest.dg_date) : 0;

	add_article(ah);
	ah = alloc_art();
    }

    fclose(folder);

    if (s_keyboard) {
	menu_cmd = ME_NO_REDRAW;
    } else if (n_articles == 0) {
	msg("Not a folder (no article header)");
	menu_cmd = ME_NO_REDRAW;
    } else {
	if (n_articles > 1) {
	    clrdisp();
	    prompt_line = 2;
	    if (mode == 0 && !dont_sort_folders)
		sort_articles(-1);
	    else if (mode == 1) {
		article_number  n;
		for (n = 0; n < n_articles; n++) {
		    ah = articles[n];
		    if (n == 0)
			ah->flag |= A_ROOT_ART;
		    else if (strcmp(ah->sender, articles[n - 1]->sender) == 0)
			articles[n - 1]->flag |= A_NEXT_SAME;
		    else
			ah->flag |= A_ROOT_ART;
		}
		bypass_consolidation = consolidated_manual ? 2 : 1;
	    } else
		no_sort_articles();
	} else
	    no_sort_articles();

	cc_save = cancel_count;
	cancel_count = 0;
	if (mode == 1) {
	    fmt_linenum = -1;
	    header_lines = "*";
	}
reenter_menu:
	ignore_fancy_select = 1;
	menu_cmd = menu(folder_header);
	ignore_fancy_select = 0;

	if (mode == 0 && cancel_count) {
	    register article_number cur;

	    cancel_count = 0;
	    for (cur = 0; cur < n_articles; cur++) {
		if (articles[cur]->attr == A_CANCEL)
		    cancel_count++;
	    }
	}
	if (mode == 0 && cancel_count) {
	    clrdisp();
	    tprintf("\rFolder: %s\n\rFile:   %s\n\n\r", folder_name, folder_file);
	    if (cancel_count == n_articles)
		tprintf("Cancel all articles and remove folder? ");
	    else
		tprintf("Remove %d article%s from folder? ",
			cancel_count, plural((long) cancel_count));
	    fl;

	    switch (yes(1)) {
		case 1:
		    tprintf("\n\n");
		    if (cancel_count == n_articles) {
			if (unlink(group_path_name) < 0 &&
			    nn_truncate(group_path_name, (off_t) 0) < 0) {
			    tprintf("\rCould not unlink %s\n\r", group_path_name);
			    any_key(0);
			}
		    } else
			rewrite_folder();
		    break;
		case 0:
		    break;
		default:
		    goto reenter_menu;
	    }
	}
	cancel_count = cc_save;
    }

    release_memory(&mem_marker);
    if (fmt_linenum == -1) {
	fmt_linenum = orig_layout;
	header_lines = orig_hdr_lines;
    }
    if (was_raw)
	nn_raw();

    return menu_cmd;
}
Ejemplo n.º 4
0
Archivo: kill.c Proyecto: jheiss/nn
static int
compile_kill_file(void)
{
    FILE           *killf, *compf, *patternf, *dropf;
    comp_kill_header header;
    comp_kill_entry entry;
    time_t          now, age;
    long            cur_line_start;
    char            line[512];
    register char  *cp, *np;
    register int    c;
    group_header   *gh;
    flag_type       flag, fields[10];
    int             any_errors, nfield, nf, len;

    any_errors = 0;
    header.ckh_entries = header.ckh_regexp_size = 0;

    killf = open_file(relative(nn_directory, KILL_FILE),
                      OPEN_READ | DONT_CREATE);
    if (killf == NULL)
        return 0;

    dropf = NULL;

    compf = open_file(relative(nn_directory, COMPILED_KILL), OPEN_CREATE);
    if (compf == NULL)
        goto err1;

    new_temp_file();
    if ((patternf = open_file(temp_file, OPEN_CREATE)) == NULL)
        goto err2;

    msg("Compiling kill file");

    fseek(compf, sizeof(header), 0);

    now = cur_time();

next_entry:

    for (;;) {
        cur_line_start = ftell(killf);

        if (fgets(line, 512, killf) == NULL)
            break;

        cp = line;
        while (*cp && isascii(*cp) && isspace(*cp))
            cp++;
        if (*cp == NUL || *cp == '#' || !isascii(*cp))
            continue;

        if ((np = strchr(cp, ':')) == NULL)
            goto bad_entry;

        /* optional "age:" */

        if (np != cp && isdigit(*cp)) {
            *np++ = NUL;
            age = (time_t) atol(cp);
            if (age < now)
                goto drop_entry;
            cp = np;
            if ((np = strchr(cp, ':')) == NULL)
                goto bad_entry;
        }
        /* "group-name:"  or "/regexp:" or ":" for all groups */

        flag = COMP_KILL_ENTRY;

        if (np == cp) {
            entry.ck_group = -1;
            np++;
        } else {
            *np++ = NUL;
            if (*cp == '/') {
                entry.ck_group = (long) ftell(patternf);
                cp++;
                len = strlen(cp) + 1;
                if (fwrite(cp, sizeof(char), len, patternf) != len)
                    goto err3;
                flag |= GROUP_REGEXP | GROUP_REGEXP_HDR;
            } else {
                if ((gh = lookup(cp)) == NULL || (gh->master_flag & M_IGNORE_GROUP)) {
                    tprintf("Unknown/ignored group in kill file: %s\n", cp);
                    any_errors++;
                    goto drop_entry;
                }
                entry.ck_group = gh->group_num;
            }
        }

        /* flags */

        cp = np;
        nfield = 0;

        for (;;) {
            switch (*cp++) {
            case EXT_AND_MATCH:
            case EXT_OR_MATCH:
                fields[nfield++] = flag;
                flag &= ~(AND_MATCH | ON_SUBJECT | ON_SENDER |
                          KILL_CASE_MATCH | KILL_ON_REGEXP |
                          GROUP_REGEXP_HDR);
                flag |= (cp[-1] == EXT_AND_MATCH) ? AND_MATCH : OR_MATCH;
                continue;
            case EXT_AUTO_KILL:
                flag |= AUTO_KILL;
                continue;
            case EXT_AUTO_SELECT:
                flag |= AUTO_SELECT;
                continue;
            case EXT_ON_FOLLOW_UP:
                flag |= ON_FOLLOW_UP;
                continue;
            case EXT_ON_NOT_FOLLOW_UP:
                flag |= ON_NOT_FOLLOW_UP;
                continue;
            case EXT_ON_ANY_REFERENCES:
                flag |= ON_ANY_REFERENCES;
                continue;
            case EXT_ON_SUBJECT:
                flag |= ON_SUBJECT;
                continue;
            case EXT_ON_SENDER:
                flag |= ON_SENDER;
                continue;
            case EXT_KILL_CASE_MATCH:
                flag |= KILL_CASE_MATCH;
                continue;
            case EXT_KILL_UNLESS_MATCH:
                flag |= KILL_UNLESS_MATCH;
                continue;
            case EXT_KILL_ON_REGEXP:
                flag |= KILL_ON_REGEXP;
                continue;
            case ':':
                break;
            case NL:
                goto bad_entry;
            default:
                tprintf("Ignored flag '%c' in kill file\n", cp[-1]);
                any_errors++;
                continue;
            }
            break;
        }

        fields[nfield++] = flag;

        for (nf = 0; --nfield >= 0; nf++) {
            entry.ck_flag = flag = fields[nf];
            np = cp;
            if ((cp = get_pattern(np, &len, nfield)) == NULL)
                goto bad_entry;

            if ((flag & KILL_CASE_MATCH) == 0)
                fold_string(np);

            entry.ck_pattern_index = ftell(patternf);

            if (fwrite((char *) &entry, sizeof(entry), 1, compf) != 1)
                goto err3;

            if (fwrite(np, sizeof(char), len, patternf) != len)
                goto err3;

            header.ckh_entries++;
            if (flag & GROUP_REGEXP)
                header.ckh_regexp_size++;
        }
    }

    header.ckh_pattern_size = ftell(patternf);

    fclose(patternf);
    patternf = open_file(temp_file, OPEN_READ | OPEN_UNLINK);
    if (patternf == NULL)
        goto err2;

    header.ckh_pattern_offset = ftell(compf);

    while ((c = getc(patternf)) != EOF)
        putc(c, compf);

    fclose(patternf);

    rewind(compf);

    header.ckh_magic = COMP_KILL_MAGIC;
    header.ckh_db_check = master.db_created;

    if (fwrite((char *) &header, sizeof(header), 1, compf) != 1)
        goto err2;

    fclose(compf);
    fclose(killf);
    if (dropf != NULL)
        fclose(dropf);

    if (any_errors) {
        tputc(NL);
        any_key(0);
    }
    return 1;

bad_entry:
    tprintf("Incomplete kill file entry:\n%s", line);
    fl;
    any_errors++;

drop_entry:
    if (dropf == NULL) {
        dropf = open_file(relative(nn_directory, KILL_FILE),
                          OPEN_UPDATE | DONT_CREATE);
        if (dropf == NULL)
            goto next_entry;
    }
    fseek(dropf, cur_line_start, 0);
    fwrite("# ", sizeof(char), 2, dropf);
    goto next_entry;

err3:
    fclose(patternf);
    unlink(temp_file);
err2:
    fclose(compf);
    rm_kill_file();
err1:
    fclose(killf);
    if (dropf != NULL)
        fclose(dropf);

    msg("cannot compile kill file");
    return 0;
}
Ejemplo n.º 5
0
int main(void) 
{
	int i;
	DARR *sturoot;

   if ((sturoot = load_darr("db.db")) == NULL){
      printf("load db error.\n");
      if ((sturoot = create_darr(sizeof(struct stu))) == NULL) {
         printf("create date error.\n");
         return -1; 
      }   
   }   

	/* 
	** 0,q for exit; 1,f for find; 2, i for insert; 3,d for delete, 4, l for list, 5, e for erase;
	** 6, a for delete duplication; 7, u for update, 8, s for sort, 9 for Save data, @ for Load data;
	*/
	while ((i = read_select()) != '0' && i != 'q' && i != 'Q')			/* 主循环,获取控制输入*/
		switch (i){
		case '1': case 'F': case 'f':
				printf("Please enter the search condition: [ID],[Name],[Scope]\n");
				{
					struct stu tmp, *ret;
					if (getline(&tmp) == 0) {
						printf("Input Format wrong!\n");
					} else if ((ret =(struct stu *)find_node(sturoot,&tmp,cmp)) == NULL)
						printf("Record is not exist!");
					else
						print_node(ret);
				}
				any_key();
				break;
		case '2': case 'I': case 'i':
				if (sturoot == NULL) {
					printf("Datebase is empty, System will re-create it.\n");
      			if ((sturoot = create_darr(sizeof(struct stu))) == NULL) {
         			printf("DB is empty,create date error.\n");
         			return -1; 
      			}
				}	   
				printf("Please enter the new record: 'ID,Name,Scope:'\n");
				{
					struct stu tmp;
					if (getline(&tmp) == 0) 
						printf("Input Format wrong!\n");
					else if (inserd_node(sturoot,&tmp,0) == -1)
						printf("Input Error!");
					else
						printf("Insert OK!");
				}
				any_key();
				break;
		case '3': case 'D': case 'd':
				printf("Please enter the condition : [ID],[Name],[Scope]\n");
				{
					struct stu tmp;
					if (getline(&tmp) == 0) 
						printf("Input Format wrong!");
					else
						delete_node(sturoot,&tmp, &cmp);
				}
				any_key();
				break;
		case '4': case 'L': case 'l':
				travel(sturoot,print_node);
				any_key();
				break;
		case '5': case 'E': case 'e':
				erase(sturoot);
				printf("All data has erase!!");
				sturoot = NULL;
				any_key();
				break;
#if 0
		case '6': case 'A': case 'a':
				printf("Please enter the condition: [ID],[Name],[Scope]\n");
				{
					struct stu tmp;
					if (getline(&tmp) == 0) {
						printf("Input Format wrong!");
					} else if (delete(&sturoot, &tmp, DELETE_ALL_YES) == 0)
						printf("Record is not exist!");
				}
				any_key();
				break;
#endif
		case '7': case 'U': case 'u':
				{
					struct stu tmp_from, tmp_to, *tmp_find;
					printf("Please enter the condition: [ID],[Name],[Scope]\n");
					if (getline(&tmp_from) == 0) {
						printf("Input Format wrong!");
					} else if ((tmp_find = (struct stu *)find_node(sturoot,&tmp_from,cmp)) != NULL) {
						print_node(tmp_find);
						printf("Please enter the new data: [ID],[Name],[Scope]\n");
						if (getline(&tmp_to) == 0) 
							printf("Input Format wrong!");
						else if (update_node(tmp_find, &tmp_to) == 0)
							printf("Update Succeed.");
					} else 
							printf("Record is not exist!");
				}
				any_key();
				break;
		case '8': case 'S': case 's':
				{
					int sel;
					int (*fun)(const void *, const void*);
					printf("Please select sort by:\n1[by ID]\t2[by name]\t3[by scope]:");
					if ((sel=get_num()) <= 3 && sel >= 1) {
						switch (sel) {
							case 1:
								fun = cmp_id;
								break;
							case 2:
								fun = cmp_name;
								break;
							case 3:
								fun = cmp_scope;
								break;
						}
						darr_sort(sturoot,fun);
						printf("Sort Succeed.");
					} else
						printf("Select wrong.");
				}
				any_key();
				break;
		case '9':
				if (save_darr(sturoot, "db.db") == -1)
					fprintf(stderr, "Open file failed.\n");
				any_key();
				break;
		default:
				break;
		}

	return 0;
}
Ejemplo n.º 6
0
Archivo: main.c Proyecto: huynhrene/dex
int main(int argc, char *argv[])
{
	const char *term = getenv("TERM");
	const char *home = getenv("HOME");
	const char *tag = NULL;
	const char *rc = NULL;
	const char *command = NULL;
	char *command_history_filename;
	char *search_history_filename;
	char *editor_dir;
	bool read_rc = true;
	int i;

	if (!home)
		home = "";
	home_dir = xstrdup(home);

	for (i = 1; i < argc; i++) {
		const char *opt = argv[i];

		if (opt[0] != '-' || !opt[1])
			break;
		if (!opt[2]) {
			switch (opt[1]) {
			case 'R':
				read_rc = false;
				continue;
			case 't':
				tag = opt_arg(opt, argv[++i]);
				continue;
			case 'r':
				rc = opt_arg(opt, argv[++i]);
				continue;
			case 'c':
				command = opt_arg(opt, argv[++i]);
				continue;
			case 'V':
				printf("%s %s\nWritten by Timo Hirvonen\n", program, version);
				return 0;
			}
			if (opt[1] == '-') {
				i++;
				break;
			}
		}
		printf("Usage: %s [-R] [-V] [-c command] [-t tag] [-r rcfile] [file]...\n", argv[0]);
		return 1;
	}

	if (!isatty(1)) {
		fprintf(stderr, "stdout doesn't refer to a terminal\n");
		return 1;
	}
	if (term == NULL || term[0] == 0) {
		fprintf(stderr, "TERM not set\n");
		return 1;
	}
	switch (term_init(term)) {
	case -1:
		fprintf(stderr, "terminal is hardcopy\n");
		return 1;
	case -2:
		fprintf(stderr, "terminal could not be found\n");
		return 1;
	case -3:
		fprintf(stderr, "terminfo database could not be found\n");
		return 1;
	}

	// create this early. needed if lock-files is true
	editor_dir = editor_file("");
	mkdir(editor_dir, 0755);
	free(editor_dir);

	setlocale(LC_CTYPE, "");
	charset = nl_langinfo(CODESET);
	if (streq(charset, "UTF-8"))
		term_utf8 = true;

	exec_builtin_rc(builtin_rc);
	fill_builtin_colors();

	// NOTE: syntax_changed() uses window. should possibly create window after reading rc
	window = new_window();
	root_frame = new_root_frame(window);

	if (read_rc) {
		if (rc) {
			read_config(commands, rc, true);
		} else {
			char *filename = editor_file("rc");
			if (read_config(commands, filename, false)) {
				free(filename);
				filename = xsprintf("%s/rc", pkgdatadir);
				read_config(commands, filename, true);
			}
			free(filename);
		}
	}

	update_all_syntax_colors();
	sort_aliases();

	/* Terminal does not generate signals for control keys. */
	set_signal_handler(SIGINT, SIG_IGN);
	set_signal_handler(SIGQUIT, SIG_IGN);
	set_signal_handler(SIGPIPE, SIG_IGN);

	/* Terminal does not generate signal for ^Z but someone can send
	 * us SIGTSTP nevertheless. SIGSTOP can't be caught.
	 */
	set_signal_handler(SIGTSTP, handle_sigtstp);

	set_signal_handler(SIGCONT, handle_sigcont);
	set_signal_handler(SIGWINCH, handle_sigwinch);

	load_file_history();
	command_history_filename = editor_file("command-history");
	search_history_filename = editor_file("search-history");
	history_load(&command_history, command_history_filename, command_history_size);
	history_load(&search_history, search_history_filename, search_history_size);
	if (search_history.count)
		search_set_regexp(search_history.ptrs[search_history.count - 1]);

	/* Initialize terminal but don't update screen yet.  Also display
	 * "Press any key to continue" prompt if there were any errors
	 * during reading configuration files.
	 */
	term_raw();
	if (nr_errors) {
		any_key();
		clear_error();
	}

	editor_status = EDITOR_RUNNING;

	for (; i < argc; i++)
		window_open_buffer(window, argv[i], false, NULL);
	if (window->views.count == 0)
		window_open_empty_buffer(window);
	set_view(window->views.ptrs[0]);

	if (command || tag)
		resize();

	if (command)
		handle_command(commands, command);
	if (tag) {
		PTR_ARRAY(array);
		ptr_array_add(&array, xstrdup("tag"));
		ptr_array_add(&array, xstrdup(tag));
		ptr_array_add(&array, NULL);
		run_commands(commands, &array);
		ptr_array_free(&array);
	}
	resize();
	main_loop();
	ui_end();

	// unlock files and add files to file history
	remove_frame(root_frame);

	history_save(&command_history, command_history_filename);
	history_save(&search_history, search_history_filename);
	free(command_history_filename);
	free(search_history_filename);
	save_file_history();
	return 0;
}