home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Interactive Guide / c-cplusplus-interactive-guide.iso / c_ref / csource4 / 235_01 / ov.c < prev    next >
Encoding:
C/C++ Source or Header  |  1987-06-18  |  14.9 KB  |  398 lines

  1. /*  093  27-Feb-87  ov.c
  2.  
  3.                             O V E R V I E W
  4.  
  5.         This program allows the user to perform common operations on
  6.         disk files in a visual, tabular manner.
  7.  
  8.         Copyright (c) 1987 by Blue Sky Software.  All rights reserved.
  9. */
  10.  
  11. #define VERSION "OverView 1.02, 1-Mar-87"
  12.  
  13. #include <stdio.h>
  14. #include <setjmp.h>
  15. #include <dos.h>
  16. #include "ov.h"
  17.  
  18.  
  19. int brkhit;                            /* NZ when ^Break/^C hit */
  20. int winupdate;                         /* NZ when windows need updating */
  21. unsigned char anyshowall;              /* NZ when show all is active */
  22. unsigned char dir_display;             /* NZ when dir tree displayed */
  23. unsigned char def_display;             /* NZ when define screen displayed */
  24. unsigned char help_display;            /* NZ when help is active */
  25. unsigned char view_display;            /* NZ when viewing a file */
  26. unsigned char restricted;              /* NZ when some functions disabled */
  27.  
  28. char *ovname = NULL;                   /* name used to invoke (if >= DOS 3) */
  29.  
  30. char *initialdir;                      /* name of initial dir */
  31.  
  32. unsigned int dataseg;                  /* ov's data segment reg value */
  33.  
  34. FILE_ENT files[MAX_FILES];             /* array of file entries */
  35.  
  36. char *cantopen = "Can't open ";        /* common message */
  37.  
  38. jmp_buf back_to_main;                  /* error handling jmp_buf block */
  39.  
  40. extern unsigned char _osmajor;         /* DOS major version # */
  41.  
  42. /* function declarations */
  43.  
  44. char *getcwd();
  45. FILE_ENT *nxtfile();
  46. int sel_all(), define(), help(), show_all();
  47. int quit(), login(), erase_current(), erase_tagged(), view(), sortname();
  48. int copy_current(), copy_tagged(), info(), renew(), do_cmd(), spawn_cli();
  49. int dtree(), dir_login(), dir_mkdir(), dir_rmdir(), dir_exit(), sel_attrib();
  50. int ren_cur(), ren_tag(), set_mask(), clr_mask(), invert_mask(), sel_tagged();
  51. int sort_asc(), sort_desc(), sort_name(), sort_ext(), sort_date(), sort_size();
  52. int set_vol(), tag_current(), tag_name(), tag_date(), tag_reset(), tag_today();
  53. int tag_modified(), tag_invert(), attrib_current(), attrib_tagged(), execute();
  54. int win_open(), win_close(), win_expand(), win_next(), win_prev(), sort_none();
  55.  
  56. /* define the current window structure */
  57.  
  58. struct window cw = { NULL, NULL, 0, FIRST_NROW, NAME_ROWS, FIRST_NROW,
  59.    NAME_ROWS, 0, 0, 0, 0, 0, 0, 0, NULL, 0, 0, 0, 0, NULL, 'A', sortname, 0, 0,
  60.    RDONLY | HIDDEN | SYSTEM | DIR | ARCHIVE, 1 };
  61.  
  62. /* define some of the menus */
  63.  
  64. char current[] = "Current";
  65. char tagged[] = "Tagged";
  66.  
  67. extern MENU top_file_menu[], top_dir_menu[];
  68.  
  69. MENU *top_menu = top_file_menu;
  70.  
  71. MENU attrib_menu[] = {
  72.    { current,"Change the current file's attributes", attrib_current, NULL },
  73.    { tagged, "Change the attributes of all tagged files", attrib_tagged, NULL },
  74.    { NULL, NULL, NULL, NULL }
  75. };
  76.  
  77. MENU copy_menu[] = {
  78.    { current, "Copy the current file", copy_current, NULL },
  79.    { tagged, "Copy the tagged files", copy_tagged, NULL },
  80.    { NULL, NULL, NULL, NULL }
  81. };
  82.  
  83. MENU erase_menu[] = {
  84.    { current, "Erase the current file", erase_current, NULL },
  85.    { tagged, "Erase all tagged files", erase_tagged, NULL },
  86.    { NULL, NULL, NULL, NULL }
  87. };
  88.  
  89. MENU mask_menu[] = {
  90.    { "Set", "Set the file mask", set_mask, NULL },
  91.    { "Clear", "Clear the file mask", clr_mask, NULL },
  92.    { "Invert", "Invert the meaning of the mask", invert_mask, NULL },
  93.    { NULL, NULL, NULL, NULL }
  94. };
  95.  
  96. MENU ren_menu[] = {
  97.    { current, "Rename or move the current file", ren_cur, NULL },
  98.    { tagged, "Move all tagged files", ren_tag, NULL },
  99.    { NULL, NULL, NULL, NULL }
  100. };
  101.  
  102. MENU select_menu[] = {
  103.    { "Attrib", "Set selection attributes", sel_attrib, NULL },
  104.    { "Mask", "Set, Clear, or Invert file name selection mask", NULL, mask_menu },
  105.    { "Reset", "Reset selection criteria to defaults", sel_all, NULL },
  106.    { "Showall", "Toggle selection of all files on current drive", show_all, NULL },
  107.    { "Tagged", "Select all tagged files", sel_tagged, NULL },
  108.    { NULL, NULL, NULL, NULL }
  109. };
  110.  
  111. MENU sort_submenu[] = {
  112.    { "Ascending", "Sort files in ascending order", sort_asc, NULL },
  113.    { "Descending", "Sort files in descending order", sort_desc, NULL },
  114.    { NULL, NULL, NULL, NULL }
  115. };
  116.  
  117. MENU sort_menu[] = {
  118.    { "Name", "Sort by primary name", sort_name, NULL },
  119.    { "Ext", "Sort by extension", sort_ext, NULL },
  120.    { "Date", "Sort by date", sort_date, NULL },
  121.    { "Size", "Sort by size", sort_size, NULL },
  122.    { "Options", "Set sort options", NULL, sort_submenu },
  123.    { "Unsorted", "Do not sort entries", sort_none, NULL },
  124.    { NULL, NULL, NULL, NULL }
  125. };
  126.  
  127. MENU tag_menu[] = {
  128.    { current, "Tag/Untag the current file", tag_current, NULL },
  129.    { "Date", "Tag files by date and time", tag_date, NULL },
  130.    { "Invert", "Invert the tag state of all files", tag_invert, NULL },
  131.    { "Modified", "Tag files with Archive attribute", tag_modified, NULL },
  132.    { "Name", "Tag files by name", tag_name, NULL },
  133.    { "Reset", "Untag all files", tag_reset, NULL },
  134.    { "Today", "Tag all files created/modified today", tag_today, NULL },
  135.    { NULL, NULL, NULL, NULL }
  136. };
  137.  
  138. MENU win_menu[] = {
  139.    { "Open", "Open an additional window", win_open, NULL },
  140.    { "Close", "Close the current window", win_close, NULL },
  141.    { "Expand", "Expand the current window to full screen (closes others)",
  142.                                                          win_expand, NULL },
  143.    { "Next", "Switch to the next window", win_next, NULL },
  144.    { "Prev", "Switch to the previous window", win_prev, NULL },
  145.    { NULL, NULL, NULL, NULL }
  146. };
  147.  
  148. MENU other_menu[] = {
  149.    { "Attrib", "Change the attributes of one or more files", NULL, attrib_menu },
  150.    { "Command", "Execute a single DOS command", do_cmd, NULL },
  151.    { "Define", "Define screen parameters", define, NULL },
  152.    { "Interperter", "Spawn a DOS command interpreter", spawn_cli, NULL },
  153.    { "Select", "Set file selection criteria", NULL, select_menu },
  154.    { "Volume", "Create or change the volume label", set_vol, NULL },
  155.    { "Window", "Open, close, switch file display windows", NULL, win_menu },
  156.    { "Xecute", "Execute a program, command, or batch file", execute, NULL },
  157.    { "Other", "Help, Copy, Dir, Erase, Info, Login, New, Rename, Sort, Tag, View, Quit", NULL, top_file_menu },
  158.    { NULL, NULL, NULL, NULL }
  159. };
  160.  
  161. MENU top_file_menu[] = {
  162.    { "Help", "Get help on using OVERVIEW", help, NULL },
  163.    { "Copy", "Copy one or more files", NULL, copy_menu },
  164.    { "Dir", "Display the disk's directory tree", dtree, top_dir_menu },
  165.    { "Erase", "Erase (delete) one or more files", NULL, erase_menu },
  166.    { "Info", "Toggle display of extra file information", info, NULL },
  167.    { "Login", "Login (switch) to another disk/directory", login, NULL },
  168.    { "New", "Reread and redisplay the directory", renew, NULL },
  169.    { "Rename", "Rename or move one or more files", NULL, ren_menu },
  170.    { "Sort", "Sort the file entries by Name, Extension, Date, or Size", NULL, sort_menu },
  171.    { "Tag", "Tag one or more files", NULL, tag_menu },
  172.    { "View", "View the current file", view, NULL },
  173.    { "Other", "Attrib, Command, Define, Interperter, Select, Volume, Window, Xecute", NULL, other_menu },
  174.    { "Quit", "Exit OVERVIEW and return to DOS", quit, NULL },
  175.    { NULL, NULL, NULL, NULL }
  176. };
  177.  
  178.  
  179.  
  180. /******************************************************************************
  181.  **                           M A I N                                        **
  182.  *****************************************************************************/
  183.  
  184. main(argc,argv)
  185. int argc;
  186. char *argv[];
  187. {
  188.    setvattrib(DIS_NORM);       /* select initial video attribute */
  189.  
  190.    /* display the signon while initializing */
  191.  
  192.    init_tty();                         /* init the terminal settings */
  193.    ovlogo(VERSION);                    /* display the logo screen */
  194.  
  195.    /* init internal data structures and display a screen of files */
  196.  
  197.    {  struct SREGS sr;                 /* get OverView's data seg reg value */
  198.       segread(&sr);                    /*   for a couple of other routines  */
  199.       dataseg = sr.ds;                 /*   to use */
  200.    }
  201.  
  202.    if (_osmajor >= 3)                  /* if dos 3.0 or more, argv[0] has */
  203.       ovname = *argv;                  /* the name ov was started with */
  204.  
  205.    getcwd(cw.dirbuf,MAX_PATHLEN);          /* get current directory pathname */
  206.    initdrive(*cw.dirbuf);                  /* init DRIVE_ENT 4 current drive */
  207.    initialdir = Strdup(cw.dirbuf);         /* save the initial dir spec */
  208.  
  209.    getfiles();                             /* get files from current directory */
  210.    adjust_window();                        /* set # rows, cols, etc */
  211.  
  212.    getchr();                               /* any keypress to continue */
  213.  
  214.    setup_file_scr();                       /* display the static screen image */
  215.    update_header();                        /* display header info */
  216.    update_window(1);                       /*   and a window of data */
  217.  
  218.  
  219.    grabbrk(&brkhit);           /* grab the ^Break/^C interrupt handler */
  220.  
  221.    /* call setjmp to establish the error handling jmp_buf,  errors in
  222.       subfuntions will return here to start the next command */
  223.  
  224.    setjmp(back_to_main);
  225.  
  226.    /* main processing, obey each user command, terminate when done or
  227.       user enters the EOF character or hits ^Break/^C */
  228.  
  229.    while (1) {
  230.       obey();             /* only returns after ^Z or ^Break/^C */
  231.       quit();             /* doesn't return if quitting */
  232.    }
  233.  
  234. }
  235.  
  236.  
  237. /******************************************************************************
  238.  **                           O B E Y                                        **
  239.  *****************************************************************************/
  240.  
  241. obey() {               /* obey / process a user command */
  242.  
  243.    register int ch, i;
  244.    char *dp, *dirplus();
  245.  
  246.    menu_init();                /* display the initial menu */
  247.  
  248.    while ((ch = getchr()) != EOF_CH) { /* get a char from the user */
  249.  
  250.       brkhit = 0;              /* clr brk flag before each ov command */
  251.  
  252.       switch (ch) {
  253.  
  254.          case LEFT: case RIGHT:        /* left, right arrows */
  255.          case UP:   case DOWN:         /* up, down arrows */
  256.          case HOME: case END:
  257.          case PGDN: case PGUP:
  258.  
  259.             if (dir_display)
  260.                dir_move(ch);           /* move the dir pointer */
  261.             else if (view_display)
  262.                view_move(ch);          /* move within the view buffer/file */
  263.             else if (def_display)
  264.                def_move(ch);           /* move within define screen mode */
  265.             else if (!restricted)
  266.                move_file(ch);          /* move the file pointer */
  267.  
  268.             break;
  269.  
  270.  
  271.          case ' ': case 9:             /* advance the menu selection pointer */
  272.             menu_advance();
  273.             break;
  274.  
  275.  
  276.          case 8:                       /* backup the menu selection pointer */
  277.             menu_backup();
  278.             break;
  279.  
  280.  
  281.          case RETURN:                  /* CR - do current menu selection */
  282.             menu_do_current();
  283.             break;
  284.  
  285.  
  286.          case 27:                      /* escape - return to top menu */
  287.             menu_init();               /* reset the menu subsystem */
  288.             break;
  289.  
  290.  
  291.          case TAG:                     /* single key command to tag current */
  292.             if (!restricted) {
  293.                tag_current();
  294.                if (cw.curidx < cw.nfiles-1)
  295.                   move_file(DOWN);
  296.             }
  297.             break;
  298.  
  299.  
  300.          case GOPAR:                   /* login to the parent dir */
  301.             if (dir_display)
  302.                dir_move(GOPAR);
  303.             else
  304.                if (!restricted) {
  305.                   switch_dir(dp = dirplus(&files[cw.curidx],".."));
  306.                   free(dp);
  307.                   menu_init();
  308.                }
  309.             break;
  310.  
  311.          case GOSUB:                   /* login to dir under file ptr */
  312.             if (dir_display) {
  313.                dir_login();
  314.                menu_init();
  315.             } else {
  316.                if (!restricted)
  317.                   if (files[cw.curidx].flags & DIR) {
  318.                      switch_dir(dp = dirplus(&files[cw.curidx],
  319.                                              files[cw.curidx].name));
  320.                      free(dp);
  321.                      menu_init();
  322.                   } else
  323.                      beep();
  324.             }
  325.             break;
  326.  
  327.          case NEXTT:                   /* goto next tagged file */
  328.          case PREVT:                   /* goto prev tagged file */
  329.             if (!restricted)
  330.                tag_find(ch == NEXTT ? 1 : -1);
  331.             break;
  332.  
  333.          case OPENW:                   /* open another file display window */
  334.             if (!restricted)
  335.                win_open();
  336.             break;
  337.  
  338.          case CLOSEW:                  /* close current file display window */
  339.             if (!restricted)
  340.                win_close();
  341.             break;
  342.  
  343.          case NEXTW:                   /* goto next file display window */
  344.             if (!restricted)
  345.                win_next();
  346.             break;
  347.  
  348.          case PREVW:                   /* goto prev file display window */
  349.             if (!restricted)
  350.                win_prev();
  351.             break;
  352.  
  353.          default:                      /* see if the 1st letter of selection */
  354.  
  355.             if (!menu_do_char(ch))     /* do menu selection starting with ch */
  356.                beep();                 /* beep if not a valid selection */
  357.             break;
  358.  
  359.       }
  360.  
  361.       if (winupdate) {                 /* some commands ask for one or */
  362.          refresh_screen(1);            /* more windows to be updated */
  363.          update_header();              /* make sure header is current */
  364.          winupdate = 0;
  365.       }
  366.    }
  367. }
  368.  
  369.  
  370. /******************************************************************************
  371.  **                             Q U I T                                      **
  372.  *****************************************************************************/
  373.  
  374. quit() {               /* exit from overview */
  375.  
  376.    int ch;
  377.  
  378.    /* Ask the user if he really wants to exit (default = N).  If so, check
  379.       to see if we are still in the initial startup dir.  If so, just exit.
  380.       If not, ask if we should return to the original dir before
  381.       quitting (default = return). */
  382.  
  383.    ch = ask("Quit OVERVIEW and return to DOS? (y/N): ");
  384.  
  385.    if (yes(ch)) {
  386.  
  387.       getcwd(cw.dirbuf,MAX_PATHLEN);   /* can mess cw.dibuf up cause quitting */
  388.  
  389.       if (strcmp(initialdir,cw.dirbuf) != 0) {
  390.          ch = ask("Exit to current directory, or Return to original? (e/R): ");
  391.          if (toupper(ch) != 'E')
  392.             change_dir(initialdir);
  393.       }
  394.       reset_tty();                     /* restore pre OV tty settings */
  395.       exit();                          /* So long, thanks for all the fish */
  396.    }
  397. }
  398.