home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / unix / volume16 / pcomm2 / part03 / d_print.c < prev    next >
C/C++ Source or Header  |  1988-09-14  |  3KB  |  170 lines

  1. /*
  2.  * The print option of the dialing directory.  A carriage return will
  3.  * send the dialing directory to the print spool program, otherwise the
  4.  * selected file will be used.
  5.  */
  6.  
  7. #include <stdio.h>
  8. #include <curses.h>
  9. #include "config.h"
  10. #include "dial_dir.h"
  11. #include "misc.h"
  12.  
  13. void
  14. print_dir()
  15. {
  16.     FILE *fp, *popen();
  17.     WINDOW *p_win, *newwin();
  18.     char *file, *e_get_str(), buf[80];
  19.     int is_printer, i, can;
  20.     void error_win();
  21.     unsigned int sleep();
  22.  
  23.     p_win = newwin(5, 54, 0, 26);
  24.  
  25.     mvwaddstr(p_win, 2, 3, "Print to: (printer)");
  26.     box(p_win, VERT, HORZ);
  27.     wmove(p_win, 2, 13);
  28.     wrefresh(p_win);
  29.  
  30.     /*
  31.      * This is a special version of get_str() that looks at the
  32.      * first character to see if it should erase the default answer
  33.      * already on the screen.
  34.      */
  35.     if ((file = e_get_str(p_win, 40)) == NULL) {
  36.                     /* erase because it overlaps dm_win */
  37.         werase(p_win);
  38.         wrefresh(p_win);
  39.         delwin(p_win);
  40.         return;
  41.     }
  42.     is_printer = 0;
  43.                     /* the default (printer) */
  44.     if (*file == NULL) {
  45.         if (!(fp = popen(LPRINT, "w"))) {
  46.             sprintf(buf, "'%s'", LPRINT);
  47.             error_win(0, "Can't open printer program", buf);
  48.             werase(p_win);
  49.             wrefresh(p_win);
  50.             delwin(p_win);
  51.             return;
  52.         }
  53.         is_printer++;
  54.     }
  55.                     /* the requested file */
  56.     else {
  57.         /*
  58.          * Check to see if the file already exists (and if we
  59.          * have write permission too).  Currently only allows
  60.          * you to bail out or overwrite the file.
  61.          */
  62.         if (!(can = can_write(file))) {
  63.             sprintf(buf, "'%s'", file);
  64.             error_win(0, "No write permission on file", buf);
  65.             werase(p_win);
  66.             wrefresh(p_win);
  67.             delwin(p_win);
  68.             return;
  69.         }
  70.         if (can == 2) {
  71.             werase(p_win);
  72.             mvwprintw(p_win, 2, 3, "File '%s' already exists!", file);
  73.             beep();
  74.             box(p_win, VERT, HORZ);
  75.             if (!yes_prompt(p_win, 3, 3, A_BOLD, "Overwrite")) {
  76.                 werase(p_win);
  77.                 wrefresh(p_win);
  78.                 delwin(p_win);
  79.                 return;
  80.             }
  81.         }
  82.         fp = fopen(file, "w");
  83.     }
  84.  
  85.     werase(p_win);
  86.     mvwaddstr(p_win, 2, 13, "Printing Pcomm directory");
  87.     box(p_win, VERT, HORZ);
  88.     wrefresh(p_win);
  89.  
  90.     /*
  91.      * Only prints up to the end of the physical file, not the entire
  92.      * structure.  I gave some thought about not printing empty entries,
  93.      * but...
  94.      */
  95.     for (i=1; i<=dir->d_entries; i++)
  96.         fprintf(fp, "%4d- %-20.20s %18.18s  %5d-%c-%d-%d  %c  %-14.14s\n",
  97.          i, dir->name[i], dir->number[i], dir->baud[i], dir->parity[i],
  98.          dir->dbits[i], dir->sbits[i], dir->duplex[i], dir->index[i]);
  99.  
  100.     if (is_printer)
  101.         pclose(fp);
  102.     else {
  103.                     /* a dramatic delay... */
  104.         sleep(1);
  105.         fclose(fp);
  106.     }
  107.  
  108.     werase(p_win);
  109.     wrefresh(p_win);
  110.     delwin(p_win);
  111.     return;
  112. }
  113.  
  114. /*
  115.  * Get a string from a window but erase the line first.
  116.  */
  117.  
  118. static char *
  119. e_get_str(win, num)
  120. WINDOW *win;
  121. int num;
  122. {
  123.     int count, x, y, done_it;
  124.     char ans;
  125.     static char buf[80];
  126.  
  127.     done_it = 0;
  128.     count = 0;
  129.     while ((ans = wgetch(win)) != '\r') {
  130.                     /* do our own backspace */
  131.         if (ans == BS) {
  132.             if (!count) {
  133.                 beep();
  134.                 continue;
  135.             }
  136.             count--;
  137.             buf[count] = NULL;
  138.             getyx(win, y, x);
  139.             x--;
  140.             wmove(win, y, x);
  141.             waddch(win, (chtype) ' ');
  142.             wmove(win, y, x);
  143.             wrefresh(win);
  144.             continue;
  145.         }
  146.                     /* exceeded the max? */
  147.         if (count == num) {
  148.             beep();
  149.             continue;
  150.         }
  151.                     /* an <ESC> anywhere in the string */
  152.         if (ans == ESC)
  153.             return(NULL);
  154.                     /* erase the default answer */
  155.         if (!done_it) {
  156.             waddstr(win, "         ");
  157.             wmove(win, 2, 13);
  158.             wrefresh(win);
  159.             done_it++;
  160.         }
  161.  
  162.         buf[count] = ans;
  163.         waddch(win, (chtype) ans);
  164.         wrefresh(win);
  165.         count++;
  166.     }
  167.     buf[count] = NULL;
  168.     return(buf);
  169. }
  170.