musicmud-2.1.6/data/
musicmud-2.1.6/data/help/
musicmud-2.1.6/data/policy/
musicmud-2.1.6/data/wild/
musicmud-2.1.6/data/world/
musicmud-2.1.6/doc/
musicmud-2.1.6/src/ident/
musicmud-2.1.6/src/lua/
musicmud-2.1.6/src/lua/include/
musicmud-2.1.6/src/lua/src/lib/
musicmud-2.1.6/src/lua/src/lua/
musicmud-2.1.6/src/lua/src/luac/
/* 
 * MusicMUD Daemon, version 1.0
 * Copyright (C) 1998-2003 Abigail Brady
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 * 
 */
#include <stdio.h>
#include <unistd.h>

#include "musicmud.h"
#include "State.h"
#include "Pager.h"
#include "ctype.h"
#include "misc.h"

bool (*pagerstate_display)(FILE *, Player*,PagerState *) = 0;

PagerState::PagerState(FILE *f, bool p) : PersonState("pager"), f(f), do_pclose(p) {
}

bool PagerState::display(Player *who) {
	if (pagerstate_display)
		return pagerstate_display(f, who, this);
	return true;
}

int PagerState::percent() const
{
  long sofar = ftell(f);
  fseek(f, 0, SEEK_END);
  long size = ftell(f);
  fseek(f, sofar, SEEK_SET);
  if (!size)
    return 0;
  return (sofar * 100) / size;
}

void PagerState::reset() {
  fseek(f, 0, SEEK_SET);
}

PagerState::~PagerState() {
  if (do_pclose)
    pclose(f);
  else
    xclose(f);
}

void state_pager(Player *who, const char *what) {
  if (what) {
    if (tolower(*what)=='q') {
      who->pop_state();
      return;
    }
    if (*what=='0') {
      PagerState *p = (PagerState*)who->get_person_state();
      p->reset();
    }
    if (*what=='*') {
      who->interpret(what+1);
      return;
    }
  }
  if (what) {
    who->printf("\033[A\33[0K\2");
    who->curcol = 0;
  }
  PagerState *p = (PagerState*)who->get_person_state();
  if (p->display(who))
    who->pop_state();
  else {
    p->set("prompt", ssprintf("^W(^BEnter^n to continue, or ^BQ^n to quit: [%i%%%%]^W)^n", 
    			      p->percent()).c_str());
  }
}

void register_pager() {
		states->add(*new State("pager",
				       "pager>",
				       state_pager ) );
}