dosinst.c
资源名称:vim53src.zip [点击查看]
上传用户:gddssl
上传日期:2007-01-06
资源大小:1003k
文件大小:7k
源码类别:
编辑器/阅读器
开发平台:
DOS
- /* vi:set ts=8 sts=4 sw=4:
- *
- * VIM - Vi IMproved by Bram Moolenaar
- *
- * Do ":help uganda" in Vim to read copying and usage conditions.
- * Do ":help credits" in Vim to see a list of people who contributed.
- */
- /*
- * install.c: Minimalistic install program for Vim on DOS/MS-Windows
- *
- * Compile with Makefile.bcc or Makefile.djg.
- */
- #include <stdio.h>
- #include <stdlib.h>
- #include <ctype.h>
- #include <string.h>
- #include <sys/stat.h>
- #ifdef WIN32
- # include <direct.h>
- #else
- # include <dir.h>
- #endif
- /*
- * Obtain a choice from a table.
- * First entry is a question, others are choices.
- */
- int
- get_choice(char **table, int entries)
- {
- int answer;
- int idx;
- char dummy[100];
- do
- {
- for (idx = 0; idx < entries; ++idx)
- {
- if (idx)
- printf("[%d] ", idx);
- printf(table[idx]);
- printf("n");
- }
- printf("Choice: ");
- if (scanf("%d", &answer) != 1)
- {
- scanf("%99s", dummy);
- answer = 0;
- }
- }
- while (answer < 1 || answer >= entries);
- return answer;
- }
- /*
- * Append a line to the autoexec.bat file.
- */
- void
- append_autoexec(char *s, char *v)
- {
- FILE *fd;
- fd = fopen("c:\autoexec.bat", "a");
- if (fd == NULL)
- {
- printf("ERROR: Cannot open c:\autoexec.bat for appendingn");
- exit(1);
- }
- fprintf(fd, s, v);
- fclose(fd);
- printf("This line has been appended to c:\autoexec.bat:n");
- printf(s, v);
- }
- /*
- * Move a file to another directory.
- */
- void
- move_file(char *fname, char *dir)
- {
- struct stat st;
- char cmd[1000];
- /* if the file doesn't exist, silently skip it */
- if (stat(fname, &st) < 0)
- return;
- sprintf(cmd, "move %s %s", fname, dir);
- system(cmd);
- if (stat(fname, &st) >= 0)
- printf("ERROR: Moving "%s" to "%s" failedn", fname, dir);
- }
- /*
- * Ask for directory from $PATH to move the .exe files to.
- */
- void
- move_to_path(void)
- {
- char *path;
- char **names = NULL;
- char *p, *s;
- int count;
- int idx;
- char answer[10];
- path = getenv("PATH");
- if (path == NULL)
- {
- printf("ERROR: The variable $PATH is not setn");
- return;
- }
- /*
- * first round: count number of names in path;
- * second round: save names to path[].
- */
- for (;;)
- {
- count = 1;
- for (p = path; *p; )
- {
- s = strchr(p, ';');
- if (s == NULL)
- s = p + strlen(p);
- if (names != NULL)
- {
- names[count] = malloc(s - p + 1);
- if (names[count] == NULL)
- {
- printf("ERROR: out of memoryn");
- exit(1);
- }
- strncpy(names[count], p, s - p);
- names[count][s - p] = 0;
- }
- ++count;
- p = s;
- if (*p)
- ++p;
- }
- if (names != NULL)
- break;
- names = malloc(count * sizeof(char *));
- if (names == NULL)
- {
- printf("ERROR: out of memoryn");
- exit(1);
- }
- }
- names[0] = "Select directory to move Vim executables to";
- idx = get_choice(names, count);
- printf("nYou have selected the directory:n");
- printf(names[idx]);
- printf("nDo you want to move the Vim executables there? (Y/N) ");
- if (scanf(" %c", answer) < 1 || toupper(answer[0]) != 'Y')
- printf("Skipping moving Vim executablesn");
- else
- {
- move_file("vim.exe", names[idx]);
- move_file("gvim.exe", names[idx]);
- move_file("xxd.exe", names[idx]);
- move_file("ctags.exe", names[idx]);
- move_file("vimrun.exe", names[idx]);
- }
- }
- #define TABLE_SIZE(s) sizeof(s) / sizeof(char *)
- int
- main(int argc, char **argv)
- {
- char answer[10];
- char *(def_choices[]) =
- {
- "nChoose the default way to run Vim:",
- "normal Vim setup",
- "with syntax highlighting and other features",
- "Vi compatible",
- };
- char *(select_choices[]) =
- {
- "nChoose the way how text is selected:",
- "with Visual mode (the Unix way)",
- "with Select mode (the Windows way)",
- "mouse with Select mode, keys with Visual mode",
- };
- char *(exe_choices[]) =
- {
- "nChoose the way to run Vim:",
- "set $PATH in c:\autoexec.bat",
- "move executables to a directory already in $PATH",
- "do nothing",
- };
- int def;
- int select;
- int exe;
- FILE *fd;
- char cwd[1000];
- if (
- #ifdef WIN32
- _getcwd
- #else
- getcwd
- #endif
- (cwd, 1000) == NULL)
- {
- printf("ERROR: Cannot get name of current directoryn");
- exit(1);
- }
- /*
- * Ask the user if he really wants to install Vim.
- */
- printf("This program will set up the installation of Vimn");
- printf("It prepares the _vimrc file, $VIM and the executables.n");
- printf("Do you want to continue? (Y/N) ");
- if (scanf(" %c", answer) < 1 || toupper(answer[0]) != 'Y')
- exit(0);
- /*
- * Ask for contents of _vimrc.
- */
- def = get_choice(def_choices, TABLE_SIZE(def_choices));
- select = get_choice(select_choices, TABLE_SIZE(select_choices));
- printf("nYou have chosen:n");
- printf("[%d] %sn", def, def_choices[def]);
- printf("[%d] %sn", select, select_choices[select]);
- if ((fd = fopen("_vimrc", "r")) != NULL)
- {
- fclose(fd);
- printf("nThere already exists a _vimrc in the current directory.");
- printf("nDo you want to overwrite it? (Y/N) ");
- }
- else
- printf("nDo you want to write the _vimrc in the current directory? (Y/N) ");
- if (scanf(" %c", answer) < 1 || toupper(answer[0]) != 'Y')
- printf("Skipping writing of _vimrcn");
- else
- {
- fd = fopen("_vimrc", "w");
- if (fd == NULL)
- {
- printf("ERROR: Cannot open _vimrc for writingn");
- exit(1);
- }
- switch (def)
- {
- case 1: fprintf(fd, "set nocompatiblen");
- break;
- case 2: fprintf(fd, "set nocompatiblen");
- fprintf(fd, "source $VIM/vimrc_examplen");
- break;
- case 3: fprintf(fd, "set compatiblen");
- break;
- }
- switch (select)
- {
- case 1: fprintf(fd, "behave xtermn");
- break;
- case 2: fprintf(fd, "source $VIM/mswin.vimn");
- break;
- case 3: fprintf(fd, "behave xtermn");
- fprintf(fd, "set selectmode=mousen");
- break;
- }
- fclose(fd);
- printf("_vimrc has been writtenn");
- }
- /*
- * Set $VIM somehow
- */
- printf("nI can append a command to c:\autoexec.bat to set $VIM.n");
- printf("(this will not work if c:\autoexec.bat contains sections)n");
- printf("Do you want me to append to your c:\autoexec.bat? (Y/N) ");
- if (scanf(" %c", answer) < 1 || toupper(answer[0]) != 'Y')
- printf("Skipping appending to c:\autoexec.batn");
- else
- append_autoexec("set VIM=%sn", cwd);
- /*
- * Set PATH or move executables.
- */
- printf("nTo be able to run Vim it must be in your $PATH.");
- exe = get_choice(exe_choices, TABLE_SIZE(exe_choices));
- switch (exe)
- {
- case 1: append_autoexec("set PATH=%%PATH%%;%sn", cwd);
- break;
- case 2: move_to_path();
- break;
- case 3: printf("Skipping setting $PATHn");
- break;
- }
- printf("nThat finishes the installation. Happy Vimming!n");
- return 0;
- }