9. Edit

<<argparse_entries>>=
{"edit", 4, p_edit, NULL},
<<static_funcdefs>>=
static int p_edit(int argc, char *argv[]);
<<functions>>=
static int p_edit(int argc, char *argv[])
{
    weewiki_d ww;
    char fname[128];
    char cmd[256];
    struct tm tm;
    time_t t;
    FILE *fp;

    t = time(NULL);
    tm = *localtime(&t);

    strftime(fname, 128128, "tmp_%m%d%y%H%M%S.org", &tm);
    fprintf(stderr, "tmpname is %s\n", fname);

    if (argc < 2) {
        fprintf(stderr,
                "Usage: %s key\n",
                argv[0]);
        return 1;
    }

    weewiki_init(&ww);
    weewiki_open(&ww, weewiki_dbname_get());

    if (weewiki_exists(&ww, argv[1])) {
        fprintf(stderr, "pulling %s to %s\n", argv[1], fname);
        weewiki_pull(&ww, argv[1], fname);
    } else {
        fp = fopen(fname, "w");
        fprintf(fp, "A new page.");
        fclose(fp);
    }

    sprintf(cmd, "$EDITOR %s", fname);
    system(cmd);
    weewiki_push(&ww, fname, argv[1]);
    weewiki_close(&ww);
    weewiki_clean(&ww);
    remove(fname);
    return 1;
}



prev | home | next