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