home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD2.mdf / c / library / os2 / less / edit.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-06-27  |  9.2 KB  |  495 lines

  1. #include "less.h"
  2.  
  3. #if __MSDOS__
  4. #include <fcntl.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #include <io.h>
  8. #endif
  9.  
  10. #ifdef OS2
  11. #include <stdio.h>
  12. #include <fcntl.h>
  13. #include <io.h>
  14. #endif
  15.  
  16. extern FILE *popen();
  17.  
  18. #define    ISPIPE(fd)    ((fd)==0)
  19. extern int ispipe, iscompressed, wascompressed;
  20. extern int new_file;
  21. extern int errmsgs;
  22. extern int quit_at_eof;
  23. extern int file;
  24. extern int cbufs;
  25. extern char *every_first_cmd;
  26. extern int any_display;
  27. extern int force_open;
  28. extern int is_tty;
  29. extern IFILE curr_ifile;
  30. extern IFILE old_ifile;
  31. extern struct scrpos initial_scrpos;
  32.  
  33. extern FILE *   compress;
  34. extern FILE *   oldcompr;
  35.  
  36. #if LOGFILE
  37. extern int logfile;
  38. extern int force_logfile;
  39. extern char *namelogfile;
  40. #endif
  41.  
  42.  
  43. /*
  44.  * Edit a new file.
  45.  * Filename == "-" means standard input.
  46.  * Filename == NULL means just close the current file.
  47.  */
  48.     public int
  49. edit(filename, just_looking)
  50.     register char *filename;
  51.     int just_looking;
  52. {
  53.     register int f;
  54.     char *s;
  55.     int answer;
  56.     int no_display;
  57.     struct scrpos scrpos;
  58.     PARG parg;
  59.  
  60.     if (filename == NULL)
  61.     {
  62.         /*
  63.          * Close the current file, but don't open a new one.
  64.          */
  65.         f = -1;
  66.     } else if (strcmp(filename, "-") == 0)
  67.     {
  68.         /*
  69.          * Use standard input.
  70.          */
  71.         f = 0;
  72.     } else if ((parg.p_string = bad_file(filename)) != NULL)
  73.     {
  74.         error("%s", &parg);
  75.         free(parg.p_string);
  76.         return (1);
  77.         } else if (isZfile(filename))
  78.     {
  79.                 char cmd[255];
  80.  
  81.                 sprintf(cmd, "compress -dc <%s", filename);
  82.         compress = popen(cmd, "r");
  83.                 f = fileno(compress);
  84.  
  85.         if ( compress == NULL || f < 0 )
  86.         {
  87.                         s = errno_message(filename);
  88.                         error(s);
  89.                         free(s);
  90.             return (1);
  91.                 }
  92.  
  93.         iscompressed = 1;
  94. #if __MSDOS__
  95.     } else if ((f = open(filename, O_RDONLY|O_BINARY)) < 0)
  96. #else
  97. #ifdef OS2
  98.     } else if ((f = open(filename, O_RDONLY|O_BINARY)) < 0)
  99. #else
  100.     } else if ((f = open(filename, 0)) < 0)
  101. #endif
  102. #endif
  103.     {
  104.         parg.p_string = errno_message(filename);
  105.         error("%s", &parg);
  106.         free(parg.p_string);
  107.         return (1);
  108.     } else if (!force_open && !just_looking && bin_file(f))
  109.     {
  110.         parg.p_string = filename;
  111.         answer = query("\"%s\" may be a binary file.  Continue? ",
  112.             &parg);
  113.         if (answer != 'y' && answer != 'Y')
  114.         {
  115.             close(f);
  116.             return (1);
  117.         }
  118.     }
  119.  
  120.     if (f >= 0 && isatty(f))
  121.     {
  122.         /*
  123.          * Not really necessary to call this an error,
  124.          * but if the control terminal (for commands)
  125.          * and the input file (for data) are the same,
  126.          * we get weird results at best.
  127.          */
  128. #if __MSDOS__
  129.         parg.p_string = "less -?";
  130. #else
  131.         parg.p_string = "less -\\?";
  132. #endif
  133. #ifdef OS2
  134.                 usage();
  135.         error("\nCannot take input from a terminal.");
  136. #else
  137.         error("Cannot take input from a terminal (\"%s\" for help)",
  138.             &parg);
  139. #endif
  140.         if (!ISPIPE(f))
  141.             close(f);
  142.         return (1);
  143.     }
  144.  
  145. #if LOGFILE
  146.     s = namelogfile;
  147.     end_logfile();
  148.     if (f >= 0 && ISPIPE(f) && s != NULL && is_tty)
  149.         use_logfile(s);
  150. #endif
  151.  
  152.     /*
  153.      * We are now committed to using the new file.
  154.      * Close the current input file and set up to use the new one.
  155.      */
  156.     if (curr_ifile != NULL_IFILE)
  157.     {
  158.         /*
  159.          * Save the current position so that we can return to
  160.          * the same position if we edit this file again.
  161.          */
  162.         get_scrpos(&scrpos);
  163.         if (scrpos.pos != NULL_POSITION)
  164.         {
  165.             store_pos(curr_ifile, &scrpos);
  166.             lastmark();
  167.         }
  168.     }
  169.  
  170.     /*
  171.      * Close the current file, unless it is a pipe.
  172.      */
  173.         if (wascompressed)
  174.             pclose(oldcompr);
  175.         else
  176.     if (!ISPIPE(file))
  177.         close(file);
  178.     file = f;
  179.         oldcompr = compress;
  180.  
  181.     if (f < 0)
  182.         return (1);
  183.  
  184.     /*
  185.      * Get the new ifile.
  186.      * Get the saved position for that file.
  187.      */
  188.     old_ifile = curr_ifile;
  189.     curr_ifile = get_ifile(filename, curr_ifile);
  190.     get_pos(curr_ifile, &initial_scrpos);
  191.  
  192.     ispipe = ISPIPE(f) || iscompressed;
  193.         wascompressed = iscompressed;
  194.         iscompressed = 0;
  195.     if (ispipe)
  196.         ch_pipe();
  197.     else
  198.         ch_nonpipe();
  199.     (void) ch_nbuf(cbufs);
  200.     ch_flush();
  201.  
  202.     new_file = 1;
  203.  
  204. #if  __MSDOS__
  205.     top_filename();
  206. #endif
  207.  
  208.     if (every_first_cmd != NULL)
  209.         ungetsc(every_first_cmd);
  210.  
  211.     no_display = !any_display;
  212.     flush();
  213.     any_display = 1;
  214.  
  215.     if (is_tty)
  216.     {
  217.         /*
  218.          * Output is to a real tty.
  219.          */
  220.  
  221.         /*
  222.          * Indicate there is nothing displayed yet.
  223.          */
  224.         pos_clear();
  225.         clr_linenum();
  226.         if (no_display && errmsgs > 0)
  227.         {
  228.             /*
  229.              * We displayed some messages on error output
  230.              * (file descriptor 2; see error() function).
  231.              * Before erasing the screen contents,
  232.              * display the file name and wait for a keystroke.
  233.              */
  234.             parg.p_string = filename;
  235.             error("%s", &parg);
  236.         }
  237.     }
  238.     return (0);
  239. }
  240.  
  241. /*
  242.  * Edit a space-separated list of files.
  243.  * For each filename in the list, enter it into the ifile list.
  244.  * Then edit the first one.
  245.  */
  246.     public void
  247. edit_list(list)
  248.     char *list;
  249. {
  250.     register char *s;
  251.     register char *es;
  252.     register char *filename;
  253.     char *good_filename;
  254.     IFILE save_curr_ifile;
  255.  
  256.     /*
  257.      * good_filename keeps track of the first valid filename.
  258.      */
  259.     good_filename = NULL;
  260.     s = list;
  261.     es = s + strlen(s);
  262.     save_curr_ifile = curr_ifile;
  263.     while ((s = skipsp(s)) < es)
  264.     {
  265.         /*
  266.          * Get the next filename and null terminate it.
  267.          */
  268.         filename = s;
  269.         while (*s != ' ' && *s != '\0')
  270.             s++;
  271.         if (*s != '\0')
  272.             *s++ = '\0';
  273.         /*
  274.          * Try to edit the file.
  275.          * This enters it into the command line list (if it is good).
  276.          * If it is the first good file we've seen, remember it.
  277.          * {{ A little weirdness here: if any of the filenames
  278.          *    are already in the list, subsequent ones get
  279.          *    entered after the position where that one already
  280.          *    was, instead of at the end. }}
  281.          */
  282.         if (edit(filename, 1) == 0 && good_filename == NULL)
  283.             good_filename = filename;
  284.     }
  285.  
  286.     /*
  287.      * Edit the first valid filename in the list.
  288.      */
  289.     if (good_filename != NULL)
  290.     {
  291.         curr_ifile = save_curr_ifile;
  292.         (void) edit(good_filename, 0);
  293.     }
  294. }
  295.  
  296. /*
  297.  * Edit the first file in the command line (ifile) list.
  298.  */
  299.     public int
  300. edit_first()
  301. {
  302.     curr_ifile = NULL_IFILE;
  303.     return (edit_next(1));
  304. }
  305.  
  306. /*
  307.  * Edit the last file in the command line (ifile) list.
  308.  */
  309.     public int
  310. edit_last()
  311. {
  312.     curr_ifile = NULL_IFILE;
  313.     return (edit_prev(1));
  314. }
  315.  
  316.  
  317. /*
  318.  * Edit the next file in the command line (ifile) list.
  319.  */
  320.     public int
  321. edit_next(n)
  322.     int n;
  323. {
  324.     IFILE h;
  325.  
  326.     h = curr_ifile;
  327.     while (--n >= 0 || edit(get_filename(h), 0))
  328.     {
  329.         if ((h = next_ifile(h)) == NULL_IFILE)
  330.             /*
  331.              * Reached end of the ifile list.
  332.              */
  333.             return (1);
  334.     }
  335.     /*
  336.      * Found a file that we can edit.
  337.      */
  338.     return (0);
  339. }
  340.  
  341. /*
  342.  * Edit the previous file in the command line list.
  343.  */
  344.     public int
  345. edit_prev(n)
  346.     int n;
  347. {
  348.     IFILE h;
  349.  
  350.     h = curr_ifile;
  351.     while (--n >= 0 || edit(get_filename(h), 0))
  352.     {
  353.         if ((h = prev_ifile(h)) == NULL_IFILE)
  354.             /*
  355.              * Reached beginning of the ifile list.
  356.              */
  357.             return (1);
  358.     }
  359.     /*
  360.      * Found a file that we can edit.
  361.      */
  362.     return (0);
  363. }
  364.  
  365. /*
  366.  * Edit a specific file in the command line (ifile) list.
  367.  */
  368.     public int
  369. edit_index(n)
  370.     int n;
  371. {
  372.     IFILE h;
  373.  
  374.     h = NULL_IFILE;
  375.     do
  376.     {
  377.         if ((h = next_ifile(h)) == NULL_IFILE)
  378.         {
  379.             /*
  380.              * Reached end of the list without finding it.
  381.              */
  382.             return (1);
  383.         }
  384.     } while (get_index(h) != n);
  385.  
  386.     return (edit(get_filename(h), 0));
  387. }
  388.  
  389. /*
  390.  * Copy a file directly to standard output.
  391.  * Used if standard output is not a tty.
  392.  */
  393.     public void
  394. cat_file()
  395. {
  396.     register int c;
  397.  
  398.     while ((c = ch_forw_get()) != EOI)
  399.         putchr(c);
  400.     flush();
  401. }
  402.  
  403. #if LOGFILE
  404.  
  405. /*
  406.  * If the user asked for a log file and our input file
  407.  * is standard input, create the log file.
  408.  * We take care not to blindly overwrite an existing file.
  409.  */
  410.     public void
  411. use_logfile(filename)
  412.     char *filename;
  413. {
  414.     register int exists;
  415.     register int answer;
  416.     PARG parg;
  417.  
  418.     /*
  419.      * {{ We could use access() here. }}
  420.      */
  421.     exists = open(filename, 0);
  422.     close(exists);
  423.     exists = (exists >= 0);
  424.  
  425.     /*
  426.      * Decide whether to overwrite the log file or append to it.
  427.      * (If it doesn't exist we "overwrite" it.
  428.      */
  429.     if (!exists || force_logfile)
  430.     {
  431.         /*
  432.          * Overwrite (or create) the log file.
  433.          */
  434.         answer = 'O';
  435.     } else
  436.     {
  437.         /*
  438.          * Ask user what to do.
  439.          */
  440.         parg.p_string = filename;
  441.         answer = query("Warning: \"%s\" exists; Overwrite, Append or Don't log? ", &parg);
  442.     }
  443.  
  444. loop:
  445.     switch (answer)
  446.     {
  447.     case 'O': case 'o':
  448.         /*
  449.          * Overwrite: create the file.
  450.          */
  451.         logfile = creat(filename, 0644);
  452.         break;
  453.     case 'A': case 'a':
  454.         /*
  455.          * Append: open the file and seek to the end.
  456.          */
  457. #if __MSDOS__
  458.         logfile = open(filename, O_APPEND|O_WRONLY);
  459. #else
  460.         logfile = open(filename, 1);
  461. #endif
  462.         if (lseek(logfile, (offset_t)0, 2) == BAD_LSEEK)
  463.         {
  464.             close(logfile);
  465.             logfile = -1;
  466.         }
  467.         break;
  468.     case 'D': case 'd':
  469.         /*
  470.          * Don't do anything.
  471.          */
  472.         return;
  473.     case 'q':
  474.         quit(0);
  475.         /*NOTREACHED*/
  476.     default:
  477.         /*
  478.          * Eh?
  479.          */
  480.         answer = query("Overwrite, Append, or Don't log? ", NULL_PARG);
  481.         goto loop;
  482.     }
  483.  
  484.     if (logfile < 0)
  485.     {
  486.         /*
  487.          * Error in opening logfile.
  488.          */
  489.         parg.p_string = filename;
  490.         error("Cannot write to \"%s\"", &parg);
  491.     }
  492. }
  493.  
  494. #endif
  495.