home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 3: Developer Tools / Linux Cubed Series 3 - Developer Tools.iso / utils / shell / dialog-0.000 / dialog-0 / dialog-0.6c / samples / install / setup.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-09-12  |  8.1 KB  |  336 lines

  1. /* Copyright (C) 1995 Florian La Roche */
  2. /* Who wants to help coding? I don't like doing this... */
  3.  
  4. /* You can just start setup as normal user and see how far it is coded
  5.    right now. This will do a fake installation and won't actually chnage
  6.    any data on your computer. */
  7.  
  8.  
  9. /* TODO: write a good package selection code
  10.          change functions to return better error code
  11.  */
  12.  
  13. /* Show an extra text-box with the contents of all external commands,
  14.    before they are executed. So you can abort the installation, if any
  15.    wrong commands are to be executed. (So don't format wrong partition.) */
  16. #define VERBOSE 1
  17.  
  18. /* If defined, don't actually execute any comands and don't actually modify
  19.    any files. So you can test any possible installation without doing any
  20.    damage to your computer.
  21.    The file FDISK.TEST is used instead of real "fdisk -l" output, so that
  22.    it can be started as normal user. */
  23. #define DEBUG_THIS 1
  24.  
  25. #include <stdio.h>
  26. #include <strings.h>
  27. #include <stdlib.h>
  28. #include <ctype.h>
  29. #include <curses.h>
  30. #include <stdarg.h>
  31. #include <unistd.h>
  32. #include "dialog.h"
  33.  
  34. /* max length of a partition name like e.g. '/dev/hda1' */
  35. #define MAX_DEV_NAME 25
  36.  
  37. /* max number of possible Linux/Swap/MsDos partitions */
  38. #define MAX_PARTS 20
  39.  
  40. char *progname = NULL;
  41.  
  42. void error (const char *s) {
  43.     fprintf(stderr, "%s: %s\n", progname, s);
  44.     exit(1);
  45. }
  46.  
  47. int my_system (const char *s, ...) {
  48.     int ret, i;
  49.     va_list ap;
  50.     char sh[200];
  51.  
  52.     va_start(ap, s);
  53.     vsprintf(sh, s, ap);
  54.     va_end(ap);
  55.  
  56. #ifdef    VERBOSE
  57.     i = dialog_msgbox("I will run the following command:", sh, 10, 65, 1);
  58.     dialog_clear();
  59. #ifdef DEBUG_THIS
  60.     return 0;
  61. #endif
  62. #endif
  63.     ret = system(sh);
  64.     if (!(ret >> 8))
  65.         return 0;
  66.     i = dialog_msgbox("Error-Exit on the following command:",
  67.         sh, 12, 73, 1);
  68.     dialog_clear();
  69.     return 1;
  70. }
  71.  
  72. /* We support to install from DOS/Linux-partitions. */
  73. enum partition_type {
  74.     MsDos,
  75.     Linux,
  76.     Swap
  77. };
  78.  
  79. struct partition {
  80.     enum partition_type type;
  81.     char name[MAX_DEV_NAME];
  82.     int blocks;
  83.     int flag;
  84. }partitions[MAX_PARTS];
  85. int num_partition = 0;
  86. int num_linux = 0;
  87. int num_swap = 0;
  88. int num_msdos = 0;
  89.  
  90. int get_line(char *line, int size, FILE *f) {
  91.     char *ptr = line;
  92.     int c;
  93.  
  94.     if (feof(f))
  95.         return -1;
  96.     while (size-- && ((c = getc(f)) != EOF) && (c != '\n'))
  97.         *ptr++ = c;
  98.     *ptr++ = '\0';
  99.     return (int)(ptr - line);
  100. }
  101.  
  102. void read_partitions (void) {
  103.     FILE *f;
  104.     char line[200];
  105.     int length;
  106. #ifndef DEBUG_THIS
  107.     int ret = system("fdisk -l 2>/dev/null 1>/tmp/fdisk.output");
  108.     if ((ret >> 8) != 0) {
  109.         error("fdisk didn't run");
  110.     }
  111.     if ((f = fopen("/tmp/fdisk.output", "r")) == NULL)
  112. #else
  113.     if ((f = fopen("FDISK.TEST", "r")) == NULL)
  114. #endif
  115.         error("cannot read fdisk output");
  116.  
  117.     while (num_partition <= MAX_PARTS
  118.             && (length = get_line(line, 200, f)) >= 0) {
  119.         if (strncmp(line, "/dev/", 5) == 0) {
  120.             int n = 0;
  121.             char *s = line + 5;
  122.             char *t = partitions[num_partition].name;
  123.             strcpy(t, "/dev/");
  124.             t += 5;
  125.             while (n < MAX_DEV_NAME && *s != '\0'
  126.                     && !isspace((unsigned char)*s)) {
  127.                 *t++ = *s++;
  128.                 n++;
  129.             }
  130.             *t = '\0';
  131.             /* Read the size of the partition. */
  132.             t = line + 37;
  133.             while (isspace((unsigned char)*t))
  134.                 t++;
  135.             partitions[num_partition].blocks = atoi(t);
  136.             if (strstr(line, "Linux native")) {
  137.                 partitions[num_partition].type = Linux;
  138.                 num_partition++;
  139.                 num_linux++;
  140.             } else if (strstr(line, "Linux swap")) {
  141.                 partitions[num_partition].type = Swap;
  142.                 num_partition++;
  143.                 num_swap++;
  144.             } else if (strstr(line, "DOS")) {
  145.                 partitions[num_partition].type = MsDos;
  146.                 num_partition++;
  147.                 num_msdos++;
  148.             }
  149.         }
  150.     }
  151.     fclose(f);
  152. #ifndef DEBUG_THIS
  153.     unlink("/tmp/fdisk.output");
  154. #endif
  155. }
  156.  
  157. int select_partition (const char *title, const char *prompt, int y, int x) {
  158.     int i, num, ret;
  159.     char info[MAX_PARTS][40];
  160.     const char *items[MAX_PARTS * 2];
  161.     int num_pa[MAX_PARTS];
  162.  
  163.     num = 0;
  164.     for (i = 0; i < num_partition; i++) {
  165.         if (partitions[i].type == Linux) {
  166.             items[num*2] = partitions[i].name;
  167.             sprintf(info[num], "Linux partition with %d blocks",
  168.                 partitions[i].blocks);
  169.             items[num*2+1] = info[num];
  170.             num_pa[num] = i;
  171.             num++;
  172.         }
  173.     }
  174.     ret = dialog_menu(title, prompt, y + num, x, num, num, items);
  175.     dialog_clear();
  176.     if (ret >= 0)        /* item selected */
  177.         ret = num_pa[ret];
  178.     return ret;
  179. }
  180.  
  181. int select_install_partition (void) {
  182.     return select_partition("Select Install Partition",
  183.         "\\nWhere do you want to install Linux?\\n", 9, 60);
  184. }
  185.  
  186. int select_source_partition (void) {
  187.     return select_partition("Select Source Partition",
  188.         "\\nOn which partition is the source?\\n", 9, 60);
  189. }
  190.  
  191. const char *null = ">/dev/null 2>/dev/null";
  192. const char *install_partition = NULL;
  193.  
  194. void extract_packages (const char *source_path) {
  195. #ifndef    DEBUG_THIS
  196.     FILE *f;
  197. #endif
  198.  
  199.     if (my_system("mkdir -p /install/var/installed/packages %s", null))
  200.         return;
  201.     if (my_system("cd /install; for i in /source%s/*.tgz; do "
  202.         "tar xzplvvkf $i >> var/installed/packages/base "
  203.         "2>>var/installed/packages/ERROR; done", source_path))
  204.         return;
  205. #ifndef    DEBUG_THIS
  206.     if ((f = fopen("/install/etc/fstab", "w")) == NULL) {
  207.         /* i = */ dialog_msgbox("Error", "Cannot write /etc/fstab",
  208.             12, 40, 1);
  209.         return;
  210.     }
  211.     fprintf(f, "%s / ext2 defaults 1 1\n", install_partition);
  212.     fprintf(f, "none /proc proc defaults 0 2\n");
  213.     /* XXX write swap-partitions */
  214.     fclose(f);
  215. #endif
  216. }
  217.  
  218. void install_premounted (void) {
  219.     extract_packages("");
  220. }
  221.  
  222. void install_harddisk (void) {
  223.     const char *name;
  224.     int part, ret;
  225.  
  226.     if ((part = select_source_partition()) <= -1) return;
  227.     name = partitions[part].name;
  228.  
  229.     if (my_system("mount -t ext2 %s /source %s", name, null))
  230.         return;
  231.     ret = dialog_inputbox("Path in partition",
  232.         "Please enter the directory in which the "
  233.         "source files are.", 13, 50, "");
  234.     dialog_clear();
  235.     if (ret != 0) return;
  236.             /* XXX strdup */
  237.     extract_packages(strdup(dialog_input_result));
  238.     if (my_system("umount /source %s", null))
  239.         return;
  240. }
  241.  
  242. void install_nfs (void) {
  243.     if (my_system("ifconfig eth0 134.96.81.36 netmask 255.255.255.224 "
  244.         "broadcast 134.96.81.63 %s", null))
  245.         return;
  246.     if (my_system("route add -net 134.96.81.32 %s", null))
  247.         return;
  248.     if (my_system("mount -t nfs 134.96.81.38:"
  249.         "/local/ftp/pub/linux/ELF.binary/tar /source %s", null))
  250.         return;
  251.     extract_packages("/base");
  252.     if (my_system("umount /source %s", null))
  253.         return;
  254.     if (my_system("ifconfig eth0 down %s", null))
  255.         return;
  256. }
  257.  
  258.  
  259. void main_install (void) {
  260.     int part, ret;
  261.     const char *name;
  262.     const char *items1 [] = {
  263.         "1",    "Harddisk Install",
  264.         "2",    "Network Install(NFS)",
  265.         "3",    "Premounted on /source"
  266.     };
  267.  
  268.     if (num_linux == 0) {
  269.         /* XXX */
  270.         return;
  271.     }
  272.     if ((part = select_install_partition()) <= -1) return;
  273.     install_partition = name = partitions[part].name;
  274.     if (my_system("mke2fs %s %s", name, null)) return;
  275.     if (my_system("mount -t ext2 %s /install %s", name, null)) return;
  276.     ret = dialog_menu("Choose install medium",
  277.         "\\nPlease say from where you want to install.\\n",
  278.         12, 62, 3, 3, items1);
  279.     dialog_clear();
  280.     switch (ret) {
  281.         case 0:    install_harddisk(); break;
  282.         case 1:    install_nfs(); break;
  283.         case 2:    install_premounted(); break;
  284.         case -2:            /* cancel */
  285.         case -1:    break;        /* esc */
  286.     }
  287.     if (my_system("umount /install %s", null))
  288.         return;
  289. }
  290.  
  291. int main (int argc, char **argv) {
  292.     int stop = 0;
  293.     int ret;
  294.     const char *items1 [] = {
  295.         "1",        "Display a help text",
  296.         "2",        "Start an installation",
  297.         "3",        "Exit to the shell"
  298.     };
  299.  
  300.     progname = argv[0];
  301.  
  302.     read_partitions();
  303.     if (num_linux == 0) {
  304.         printf(    "\n\nPlease start \"fdisk\" or \"cfdisk\" and create a"
  305.             "\nnative Linux-partition to install Linux on.\n\n");
  306.         exit(1);
  307.     }
  308.  
  309.     init_dialog();
  310.  
  311.     while (!stop) {
  312.         ret = dialog_menu("Linux Install Utility",
  313.             "\\nCopyright (C) 1995 Florian La Roche\\n"
  314.             "\\nPre-Alpha version, be careful, read the doc!!!"
  315.             "\\nemail: florian@jurix.jura.uni-sb.de, "
  316.             "flla@stud.uni-sb.de\\n",
  317.             15, 64, 3, 3, items1);
  318.         dialog_clear();
  319.         switch (ret) {
  320.             case 0:    ret = dialog_textbox("Help Text",
  321.                     "/usr/share/setup.help", 20, 70);
  322.                 dialog_clear();
  323.                 break;
  324.             case 1:    main_install(); break;
  325.             case 2:    stop = 1; break;
  326.             case -2:    /* cancel */
  327.             case -1:    stop = 1;    /* esc */
  328.         }
  329.     }
  330.     end_dialog();
  331.     printf("\nExecute \"reboot\" to restart your computer...\n");
  332.  
  333.     exit(0);
  334. }
  335.  
  336.