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