13950Sache/*
26345Sache *  prgbox.c -- implements the message box and info box
33950Sache *
43950Sache *  AUTHOR: Savio Lam (lam836@cs.cuhk.hk)
53950Sache *
63950Sache *  This program is free software; you can redistribute it and/or
73950Sache *  modify it under the terms of the GNU General Public License
83950Sache *  as published by the Free Software Foundation; either version 2
93950Sache *  of the License, or (at your option) any later version.
103950Sache *
113950Sache *  This program is distributed in the hope that it will be useful,
123950Sache *  but WITHOUT ANY WARRANTY; without even the implied warranty of
133950Sache *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
143950Sache *  GNU General Public License for more details.
153950Sache *
163950Sache *  You should have received a copy of the GNU General Public License
173950Sache *  along with this program; if not, write to the Free Software
183950Sache *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
193950Sache */
203950Sache
21114603Sobrien#include <sys/cdefs.h>
22114603Sobrien__FBSDID("$FreeBSD$");
233950Sache
2467176Sbrian#include <sys/types.h>
2567176Sbrian
263950Sache#include <dialog.h>
276345Sache#include <errno.h>
286345Sache#include <sys/wait.h>
293950Sache#include "dialog.priv.h"
303950Sache
313950Sache/*
323950Sache * Display a message box. Program will pause and display an "OK" button
333950Sache * if the parameter 'pause' is non-zero.
343950Sache */
354527Sacheint dialog_prgbox(unsigned char *title, const unsigned char *line, int height, int width, int pause, int use_shell)
363950Sache{
373950Sache  int i, x, y, key = 0;
383950Sache  WINDOW *dialog;
393950Sache  FILE *f;
406345Sache  const unsigned char *name;
413950Sache  unsigned char *s, buf[MAX_LEN];
426345Sache  int status;
433950Sache
444565Sache  if (height < 0 || width < 0) {
454565Sache    endwin();
464565Sache    fprintf(stderr, "\nAutosizing is impossible in dialog_prgbox().\n");
474565Sache    exit(-1);
484565Sache  }
496035Sache  width = MAX(width,10);
506035Sache
514658Sache  if (width > COLS)
524658Sache	width = COLS;
534658Sache  if (height > LINES)
544658Sache	height = LINES;
553950Sache  /* center dialog box on screen */
5613135Sjkh  x = DialogX ? DialogX : (COLS - width)/2;
5713135Sjkh  y = DialogY ? DialogY : (LINES - height)/2;
583950Sache
593950Sache#ifdef HAVE_NCURSES
603950Sache  if (use_shadow)
613950Sache    draw_shadow(stdscr, y, x, height, width);
623950Sache#endif
633950Sache  dialog = newwin(height, width, y, x);
644024Sache  if (dialog == NULL) {
654024Sache    endwin();
664024Sache    fprintf(stderr, "\nnewwin(%d,%d,%d,%d) failed, maybe wrong dims\n", height,width,y,x);
674024Sache    exit(1);
684024Sache  }
693950Sache  keypad(dialog, TRUE);
703950Sache
713950Sache  draw_box(dialog, 0, 0, height, width, dialog_attr, border_attr);
723950Sache
733950Sache  if (title != NULL) {
743950Sache    wattrset(dialog, title_attr);
753950Sache    wmove(dialog, 0, (width - strlen(title))/2 - 1);
763950Sache    waddch(dialog, ' ');
773950Sache    waddstr(dialog, title);
783950Sache    waddch(dialog, ' ');
793950Sache  }
803950Sache  wattrset(dialog, dialog_attr);
813950Sache  wmove(dialog, 1, 2);
823950Sache
833950Sache  if (!use_shell) {
843950Sache    char cmdline[MAX_LEN];
857330Sache    char *av[51], **ap = av, *val, *p;
863950Sache
873950Sache    strcpy(cmdline, line);
887330Sache    p = cmdline;
897330Sache    while ((val = strsep(&p," \t")) != NULL) {
907330Sache      if (*val != '\0')
917330Sache	*ap++ = val;
923950Sache    }
933950Sache    *ap = NULL;
946345Sache    f = raw_popen(name = av[0], av, "r");
953950Sache  } else
966345Sache    f = raw_popen(name = line, NULL, "r");
973950Sache
986345Sache  status = -1;
996345Sache  if (f == NULL) {
1006345Sache  err:
1016345Sache      sprintf(buf, "%s: %s\n", name, strerror(errno));
1026345Sache  prr:
1036345Sache      print_autowrap(dialog, buf, height-(pause?3:1), width-2, width, 1, 2, FALSE, TRUE);
1046345Sache      wrefresh(dialog);
1056345Sache  } else {
1066345Sache    while (fgets(buf, sizeof(buf), f) != NULL) {
1076345Sache      i = strlen(buf);
1086345Sache      if (buf[i-1] == '\n')
1096345Sache	buf[i-1] = '\0';
1106345Sache      s = buf;
1116345Sache      while ((s = strchr(s, '\t')) != NULL)
1126345Sache	*s++ = ' ';
1136345Sache      print_autowrap(dialog, buf, height-(pause?3:1), width-2, width, 1, 2, FALSE, TRUE);
1146345Sache      print_autowrap(dialog, "\n", height-(pause?3:1), width-2, width, 1, 2, FALSE, FALSE);
1156345Sache      wrefresh(dialog);
1166345Sache    }
1176345Sache    if ((status = raw_pclose(f)) == -1)
1186345Sache      goto err;
1196345Sache    if (WIFEXITED(status) && WEXITSTATUS(status) == 127) {
1206345Sache      sprintf(buf, "%s: program not found\n", name);
1216345Sache      goto prr;
1226345Sache    }
1233950Sache  }
1243950Sache
1253950Sache  if (pause) {
1263950Sache    wattrset(dialog, border_attr);
1273950Sache    wmove(dialog, height-3, 0);
1283950Sache    waddch(dialog, ACS_LTEE);
1293950Sache    for (i = 0; i < width-2; i++)
1303950Sache      waddch(dialog, ACS_HLINE);
1313950Sache    wattrset(dialog, dialog_attr);
1323950Sache    waddch(dialog, ACS_RTEE);
1333950Sache    wmove(dialog, height-2, 1);
1343950Sache    for (i = 0; i < width-2; i++)
1353950Sache    waddch(dialog, ' ');
1366458Sache    display_helpline(dialog, height-1, width);
13720359Sjkh    print_button(dialog, "  OK  ", height-2, width/2-6, TRUE);
1383950Sache    wrefresh(dialog);
1393950Sache    while (key != ESC && key != '\n' && key != ' ' && key != '\r')
1403950Sache      key = wgetch(dialog);
1413950Sache    if (key == '\r')
1423950Sache      key = '\n';
1433950Sache  }
1443950Sache  else {
1453950Sache    key = '\n';
1463950Sache    wrefresh(dialog);
1473950Sache  }
1483950Sache
1493950Sache  delwin(dialog);
1506345Sache  return (status);
1513950Sache}
1523950Sache/* End of dialog_msgbox() */
153