Example #1
0
char *get_thru_arg()
{
  int c = input_stack::peek_char();
  while (c == ' ') {
    input_stack::get_char();
    c = input_stack::peek_char();
  }
  if (c != EOF && csalpha(c)) {
    // looks like a macro
    input_stack::get_char();
    token_buffer = c;
    for (;;) {
      c = input_stack::peek_char();
      if (c == EOF || (!csalnum(c) && c != '_'))
	break;
      input_stack::get_char();
      token_buffer += char(c);
    }
    context_buffer = token_buffer;
    token_buffer += '\0';
    char *def = macro_table.lookup(token_buffer.contents());
    if (def)
      return strsave(def);
    // I guess it wasn't a macro after all; so push the macro name back.
    // -2 because we added a '\0'
    for (int i = token_buffer.length() - 2; i >= 0; i--)
      input_stack::push_back(token_buffer[i]);
  }
  if (get_delimited()) {
    token_buffer += '\0';
    return strsave(token_buffer.contents());
  }
  else
    return 0;
}
Example #2
0
File: ref.cpp Project: att/uwin
void sortify_title(const char *s, int len, string &key)
{
  const char *end = s + len;
  for (; s < end && (*s == ' ' || *s == '\n'); s++) 
    ;
  const char *ptr = s;
  for (;;) {
    const char *token_start = ptr;
    if (!get_token(&ptr, end))
      break;
    if (ptr - token_start == 1
	&& (*token_start == ' ' || *token_start == '\n'))
      break;
  }
  if (ptr < end) {
    unsigned int first_word_len = ptr - s - 1;
    const char *ae = articles.contents() + articles.length();
    for (const char *a = articles.contents();
	 a < ae;
	 a = strchr(a, '\0') + 1)
      if (first_word_len == strlen(a)) {
	unsigned int j;
	for (j = 0; j < first_word_len; j++)
	  if (a[j] != cmlower(s[j]))
	    break;
	if (j >= first_word_len) {
	  s = ptr;
	  for (; s < end && (*s == ' ' || *s == '\n'); s++)
	    ;
	  break;
	}
      }
  }
  sortify_words(s, end, 0, key);
}
Example #3
0
void do_gsize()
{
  int t = get_token(2);
  if (t != TEXT && t != QUOTED_TEXT) {
    lex_error("bad argument to gsize command");
    return;
  }
  token_buffer += '\0';
  if (!set_gsize(token_buffer.contents()))
    lex_error("invalid size `%1'", token_buffer.contents());
}
Example #4
0
File: command.cpp Project: att/uwin
int input_item::get_location(const char **filenamep, int *linenop)
{
  *filenamep = filename;
  if (ptr == buffer.contents())
    *linenop = first_lineno;
  else {
    int ln = first_lineno;
    const char *e = ptr - 1;
    for (const char *p = buffer.contents(); p < e; p++)
      if (*p == '\n')
	ln++;
    *linenop = ln;
  }
  return 1;
}
Example #5
0
void do_space()
{
  int t = get_token(2);
  if (t != TEXT && t != QUOTED_TEXT) {
    lex_error("bad argument to space command");
    return;
  }
  token_buffer += '\0';
  char *ptr;
  long n = strtol(token_buffer.contents(), &ptr, 10);
  if (n == 0 && ptr == token_buffer.contents())
    lex_error("bad argument `%1' to space command", token_buffer.contents());
  else
    set_space(int(n));
}
Example #6
0
void put_string(const string &s, FILE *fp)
{
  int len = s.length();
  const char *ptr = s.contents();
  for (int i = 0; i < len; i++)
    putc(ptr[i], fp);
}
Example #7
0
File: ref.cpp Project: att/uwin
int join_fields(string &f)
{
  const char *ptr = f.contents();
  int len = f.length();
  int nfield_seps = 0;
  int j;
  for (j = 0; j < len; j++)
    if (ptr[j] == FIELD_SEPARATOR)
      nfield_seps++;
  if (nfield_seps == 0)
    return 0;
  string temp;
  int field_seps_left = nfield_seps;
  for (j = 0; j < len; j++) {
    if (ptr[j] == FIELD_SEPARATOR) {
      if (nfield_seps == 1)
	temp += join_authors_exactly_two;
      else if (--field_seps_left == 0)
	temp += join_authors_last_two;
      else
	temp += join_authors_default;
    }
    else
      temp += ptr[j];
  }
  f = temp;
  return nfield_seps;
}
Example #8
0
int file_input::read_line()
{
  for (;;) {
    line.clear();
    lineno++;
    for (;;) {
      int c = getc(fp);
      if (c == EOF)
	break;
      else if (invalid_input_char(c))
	lex_error("invalid input character code %1", c);
      else {
	line += char(c);
	if (c == '\n') 
	  break;
      }
    }
    if (line.length() == 0)
      return 0;
    if (!(line.length() >= 3 && line[0] == '.' && line[1] == 'E'
	  && (line[2] == 'Q' || line[2] == 'N')
	  && (line.length() == 3 || line[3] == ' ' || line[3] == '\n'
	      || compatible_flag))) {
      line += '\0';
      ptr = line.contents();
      return 1;
    }
  }
}
Example #9
0
int copy_thru_input::peek()
{
  if (ap) {
    if (*ap != '\0')
      return (unsigned char)*ap;
    ap = 0;
  }
  for (;;) {
    if (p == 0) {
      if (!get_line())
	break;
      p = body;
    }
    if (*p == '\0')
      return '\n';
    while (*p >= ARG1 && *p <= ARG1 + 8) {
      int i = *p++ - ARG1;
      if (i < argc && line[argv[i]] != '\0') {
	ap = line.contents() + argv[i];
	return (unsigned char)*ap;
      }
    }
    if (*p != '\0')
      return (unsigned char)*p;
  }
  return EOF;
}
Example #10
0
void do_ifdef()
{
  int t = get_token();
  if (t != TEXT) {
    lex_error("bad ifdef");
    return;
  }
  token_buffer += '\0';
  definition *def = macro_table.lookup(token_buffer.contents());
  int result = def && def->is_macro && !def->is_simple;
  get_delimited_text();
  if (result) {
    token_buffer += '\0';
    current_input = new macro_input(token_buffer.contents(), current_input);
  }
}
Example #11
0
File: command.cpp Project: att/uwin
static void search_ignore_command(int argc, argument *argv)
{
  if (argc > 0)
    search_ignore_fields = argv[0].s;
  else
    search_ignore_fields = "XYZ";
  search_ignore_fields += '\0';
  linear_ignore_fields = search_ignore_fields.contents();
}
Example #12
0
void do_undef()
{
  int t = get_token();
  if (t != TEXT) {
    lex_error("bad undef command");
    return;
  }
  token_buffer += '\0';
  macro_table.define(token_buffer.contents(), 0);
}
Example #13
0
void do_gbfont()
{
  int t = get_token(2);
  if (t != TEXT && t != QUOTED_TEXT) {
    lex_error("bad argument to gbfont command");
    return;
  }
  token_buffer += '\0';
  set_gbfont(token_buffer.contents());
}
Example #14
0
void do_undef()
{
  int t = get_token(0);		// do not expand what we are undefining
  if (t != VARIABLE && t != LABEL) {
    lex_error("can only define variable or placename");
    return;
  }
  token_buffer += '\0';
  macro_table.define(token_buffer.contents(), 0);
}
Example #15
0
void possible_command::insert_args(string s)
{
  const char *p = s.contents();
  const char *end = p + s.length();
  int l = 0;
  if (p >= end)
    return;
  // find the total number of arguments in our string
  do {
    l++;
    p = strchr(p, '\0') + 1;
  } while (p < end);
  // now insert each argument preserving the order
  for (int i = l - 1; i >= 0; i--) {
    p = s.contents();
    for (int j = 0; j < i; j++)
      p = strchr(p, '\0') + 1;
    insert_arg(p);
  }
}
Example #16
0
void interpolate_macro_with_args(const char *body)
{
  char *argv[9];
  int argc = 0;
  int i;
  for (i = 0; i < 9; i++)
    argv[i] = 0;
  int level = 0;
  int c;
  enum { NORMAL, IN_STRING, IN_STRING_QUOTED } state = NORMAL;
  do {
    token_buffer.clear();
    for (;;) {
      c = input_stack::get_char();
      if (c == EOF) {
	lex_error("end of input while scanning macro arguments");
	break;
      }
      if (state == NORMAL && level == 0 && (c == ',' || c == ')')) {
	if (token_buffer.length() > 0) {
	  token_buffer +=  '\0';
	  argv[argc] = strsave(token_buffer.contents());
	}
	// for `foo()', argc = 0
	if (argc > 0 || c != ')' || i > 0)
	  argc++;
	break;
      }
      token_buffer += char(c);
      switch (state) {
      case NORMAL:
	if (c == '"')
	  state = IN_STRING;
	else if (c == '(')
	  level++;
	else if (c == ')')
	  level--;
	break;
      case IN_STRING:
	if (c == '"')
	  state = NORMAL;
	else if (c == '\\')
	  state = IN_STRING_QUOTED;
	break;
      case IN_STRING_QUOTED:
	state = IN_STRING;
	break;
      }
    }
  } while (c != ')' && c != EOF);
  input_stack::push(new argument_macro_input(body, argc, argv));
}
Example #17
0
void yyerror(const char *s)
{
  const char *filename;
  int lineno;
  const char *context = 0;
  if (lookahead_token == -1) {
    if (context_buffer.length() > 0) {
      context_buffer += '\0';
      context = context_buffer.contents();
    }
  }
  else {
    if (old_context_buffer.length() > 0) {
      old_context_buffer += '\0';
      context = old_context_buffer.contents();
    }
  }
  if (!input_stack::get_location(&filename, &lineno)) {
    if (context) {
      if (context[0] == '\n' && context[1] == '\0')
	error("%1 before newline", s);
      else
	error("%1 before `%2'", s, context);
    }
    else
      error("%1 at end of picture", s);
  }
  else {
    if (context) {
      if (context[0] == '\n' && context[1] == '\0')
	error_with_file_and_line(filename, lineno, "%1 before newline", s);
      else
	error_with_file_and_line(filename, lineno, "%1 before `%2'",
				 s, context);
    }
    else
      error_with_file_and_line(filename, lineno, "%1 at end of picture", s);
  }
}
Example #18
0
void do_definition(int is_simple)
{
  int t = get_token();
  if (t != TEXT) {
    lex_error("bad definition");
    return;
  }
  token_buffer += '\0';
  const char *name = token_buffer.contents();
  definition *def = macro_table.lookup(name);
  if (def == 0) {
    def = new definition[1];
    macro_table.define(name, def);
  }
  else if (def->is_macro) {
    a_delete def->contents;
  }
  get_delimited_text();
  token_buffer += '\0';
  def->is_macro = 1;
  def->contents = strsave(token_buffer.contents());
  def->is_simple = is_simple;
}
Example #19
0
static void write_hash_table()
{
  const int minus_one = -1;
  int li = 0;
  for (int i = 0; i < hash_table_size; i++) {
    block *ptr = hash_table[i].ptr;
    if (!ptr)
      hash_table[i].count = -1;
    else {
      hash_table[i].count = li;
      block *rev = 0;
      while (ptr) {
	block *tem = ptr;
	ptr = ptr->next;
	tem->next = rev;
	rev = tem;
      }
      while (rev) {
	fwrite_or_die(rev->v, sizeof(int), rev->used, indxfp);
	li += rev->used;
	block *tem = rev;
	rev = rev->next;
	delete tem;
      }
      fwrite_or_die(&minus_one, sizeof(int), 1, indxfp);
      li += 1;
    }
  }
  if (sizeof(table_entry) == sizeof(int))
    fwrite_or_die(hash_table, sizeof(int), hash_table_size, indxfp);
  else {
    // write it out word by word
    for (int i = 0; i < hash_table_size; i++)
      fwrite_or_die(&hash_table[i].count, sizeof(int), 1, indxfp);
  }
  fwrite_or_die(filenames.contents(), 1, filenames.length(), indxfp);
  if (fseek(indxfp, 0, 0) < 0)
    fatal("error seeking on index file: %1", strerror(errno));
  index_header h;
  h.magic = INDEX_MAGIC;
  h.version = INDEX_VERSION;
  h.tags_size = ntags;
  h.lists_size = li;
  h.table_size = hash_table_size;
  h.strings_size = filenames.length();
  h.truncate = truncate_len;
  h.shortest = shortest_len;
  h.common = n_ignore_words;
  fwrite_or_die(&h, sizeof(h), 1, indxfp);
}
Example #20
0
void do_define()
{
  int t = get_token(0);		// do not expand what we are defining
  if (t != VARIABLE && t != LABEL) {
    lex_error("can only define variable or placename");
    return;
  }
  token_buffer += '\0';
  string nm = token_buffer;
  const char *name = nm.contents();
  if (!get_delimited())
    return;
  token_buffer += '\0';
  macro_table.define(name, strsave(token_buffer.contents()));
}
Example #21
0
void do_chartype()
{
  int t = get_token(2);
  if (t != TEXT && t != QUOTED_TEXT) {
    lex_error("bad chartype");
    return;
  }
  token_buffer += '\0';
  string type = token_buffer;
  t = get_token();
  if (t != TEXT && t != QUOTED_TEXT) {
    lex_error("bad chartype");
    return;
  }
  token_buffer += '\0';
  set_char_type(type.contents(), strsave(token_buffer.contents()));
}
Example #22
0
void do_include()
{
  int t = get_token(2);
  if (t != TEXT && t != QUOTED_TEXT) {
    lex_error("bad filename for include");
    return;
  }
  token_buffer += '\0';
  const char *filename = token_buffer.contents();
  errno = 0;
  FILE *fp = fopen(filename, "r");
  if (fp == 0) {
    lex_error("can't open included file `%1'", filename);
    return;
  }
  current_input = new file_input(fp, filename, current_input);
}
Example #23
0
void do_set()
{
  int t = get_token(2);
  if (t != TEXT && t != QUOTED_TEXT) {
    lex_error("bad set");
    return;
  }
  token_buffer += '\0';
  string param = token_buffer;
  t = get_token();
  if (t != TEXT && t != QUOTED_TEXT) {
    lex_error("bad set");
    return;
  }
  token_buffer += '\0';
  int n;
  if (sscanf(&token_buffer[0], "%d", &n) != 1) {
    lex_error("bad number `%1'", token_buffer.contents());
    return;
  }
  set_param(param.contents(), n);
}
Example #24
0
void interpolate_macro_with_args(const char *body)
{
  char *argv[9];
  int argc = 0;
  int i;
  for (i = 0; i < 9; i++)
    argv[i] = 0;
  int level = 0;
  int c;
  do {
    token_buffer.clear();
    for (;;) {
      c = get_char();
      if (c == EOF) {
	lex_error("end of input while scanning macro arguments");
	break;
      }
      if (level == 0 && (c == ',' || c == ')')) {
	if (token_buffer.length() > 0) {
	  token_buffer +=  '\0';
	  argv[argc] = strsave(token_buffer.contents());
	}
	// for `foo()', argc = 0
	if (argc > 0 || c != ')' || i > 0)
	  argc++;
	break;
      }
      token_buffer += char(c);
      if (c == '(')
	level++;
      else if (c == ')')
	level--;
    }
  } while (c != ')' && c != EOF);
  current_input = new argument_macro_input(body, argc, argv, current_input);
}
Example #25
0
File: ref.cpp Project: att/uwin
void capitalize_field(string &str)
{
  string temp;
  capitalize(str.contents(), str.contents() + str.length(), temp);
  str.move(temp);
}
Example #26
0
static void print_ps_string(const string &s, FILE *outfp)
{
  int len = s.length();
  const char *str = s.contents();
  int funny = 0;
  if (str[0] == '(')
    funny = 1;
  else {
    for (int i = 0; i < len; i++)
      if (str[i] <= 040 || str[i] > 0176) {
	funny = 1;
	break;
      }
  }
  if (!funny) {
    put_string(s, outfp);
    return;
  }
  int level = 0;
  int i;
  for (i = 0; i < len; i++)
    if (str[i] == '(')
      level++;
    else if (str[i] == ')' && --level < 0)
      break;
  putc('(', outfp);
  for (i = 0; i < len; i++)
    switch (str[i]) {
    case '(':
    case ')':
      if (level != 0)
	putc('\\', outfp);
      putc(str[i], outfp);
      break;
    case '\\':
      fputs("\\\\", outfp);
      break;
    case '\n':
      fputs("\\n", outfp);
      break;
    case '\r':
      fputs("\\r", outfp);
      break;
    case '\t':
      fputs("\\t", outfp);
      break;
    case '\b':
      fputs("\\b", outfp);
      break;
    case '\f':
      fputs("\\f", outfp);
      break;
    default:
      if (str[i] < 040 || str[i] > 0176)
	fprintf(outfp, "\\%03o", str[i] & 0377);
      else
	putc(str[i], outfp);
      break;
    }
  putc(')', outfp);
}
Example #27
0
void mtsm::add_tag(FILE *fp, string s)
{
  fflush(fp);
  s += '\0';
  fputs(s.contents(), fp);
}
Example #28
0
int yylex()
{
  for (;;) {
    int tk = get_token(1);
    switch(tk) {
    case UNDEF:
      do_undef();
      break;
    case SDEFINE:
      do_definition(1);
      break;
    case DEFINE:
      do_definition(0);
      break;
    case TDEFINE:
      if (!nroff)
	do_definition(0);
      else
	ignore_definition();
      break;
    case NDEFINE:
      if (nroff)
	do_definition(0);
      else
	ignore_definition();
      break;
    case GSIZE:
      do_gsize();
      break;
    case GFONT:
      do_gfont();
      break;
    case GRFONT:
      do_grfont();
      break;
    case GBFONT:
      do_gbfont();
      break;
    case SPACE:
      do_space();
      break;
    case INCLUDE:
      do_include();
      break;
    case IFDEF:
      do_ifdef();
      break;
    case DELIM:
      do_delim();
      break;
    case CHARTYPE:
      do_chartype();
      break;
    case SET:
      do_set();
      break;
    case QUOTED_TEXT:
    case TEXT:
      token_buffer += '\0';
      yylval.str = strsave(token_buffer.contents());
      // fall through
    default:
      return tk;
    }
  }
}
Example #29
0
// Handle an inline equation.  Return 1 if it was an inline equation,
// otherwise.
static int inline_equation(FILE *fp, string &linebuf, string &str)
{
  linebuf += '\0';
  char *ptr = &linebuf[0];
  char *start = delim_search(ptr, start_delim);
  if (!start) {
    // It wasn't a delimiter after all.
    linebuf.set_length(linebuf.length() - 1); // strip the '\0'
    return 0;
  }
  start_string();
  inline_flag = 1;
  for (;;) {
    if (no_newline_in_delim_flag && strchr(start + 1, end_delim) == 0) {
      error("missing `%1'", end_delim);
      char *nl = strchr(start + 1, '\n');
      if (nl != 0)
	*nl = '\0';
      do_text(ptr);
      break;
    }
    int start_lineno = current_lineno;
    *start = '\0';
    do_text(ptr);
    ptr = start + 1;
    str.clear();
    for (;;) {
      char *end = strchr(ptr, end_delim);
      if (end != 0) {
	*end = '\0';
	str += ptr;
	ptr = end + 1;
	break;
      }
      str += ptr;
      if (!read_line(fp, &linebuf))
	fatal("unterminated `%1' at line %2, looking for `%3'",
	      start_delim, start_lineno, end_delim);
      linebuf += '\0';
      ptr = &linebuf[0];
    }
    str += '\0';
    if (output_format == troff && html) {
      printf(".as1 %s ", LINE_STRING);
      html_begin_suppress();
      printf("\n");
    }
    init_lex(str.contents(), current_filename, start_lineno);
    yyparse();
    if (output_format == troff && html) {
      printf(".as1 %s ", LINE_STRING);
      html_end_suppress();
      printf("\n");
    }
    if (output_format == mathml)
      printf("\n");
    if (xhtml) {
      /* skip leading spaces */
      while ((*ptr != '\0') && (*ptr == ' '))
	ptr++;
    }
    start = delim_search(ptr, start_delim);
    if (start == 0) {
      char *nl = strchr(ptr, '\n');
      if (nl != 0)
	*nl = '\0';
      do_text(ptr);
      break;
    }
  }
  restore_compatibility();
  if (output_format == troff)
    printf(".lf %d\n", current_lineno);
  output_string();
  if (output_format == troff)
    printf(".lf %d\n", current_lineno + 1);
  return 1;
}
Example #30
0
int get_token(int lookup_flag = 0)
{
  for (;;) {
    int c = get_char();
    while (c == ' ' || c == '\n')
      c = get_char();
    switch (c) {
    case EOF:
      {
	add_context("end of input");
      }
      return 0;
    case '"':
      {
	int quoted = 0;
	token_buffer.clear();
	for (;;) {
	  c = get_char();
	  if (c == EOF) {
	    lex_error("missing \"");
	    break;
	  }
	  else if (c == '\n') {
	    lex_error("newline before end of quoted text");
	    break;
	  }
	  else if (c == '"') {
	    if (!quoted)
	      break;
	    token_buffer[token_buffer.length() - 1] = '"';
	    quoted = 0;
	  }
	  else {
	    token_buffer += c;
	    quoted = quoted ? 0 : c == '\\';
	  }
	}
      }
      add_quoted_context(token_buffer);
      return QUOTED_TEXT;
    case '{':
    case '}':
    case '^':
    case '~':
    case '\t':
      add_context(c);
      return c;
    default:
      {
	int break_flag = 0;
	int quoted = 0;
	token_buffer.clear();
	if (c == '\\')
	  quoted = 1;
	else
	  token_buffer += c;
	int done = 0;
	while (!done) {
	  c = peek_char();
	  if (!quoted && lookup_flag != 0 && c == '(') {
	    token_buffer += '\0';
	    definition *def = macro_table.lookup(token_buffer.contents());
	    if (def && def->is_macro && !def->is_simple) {
	      (void)get_char();	// skip initial '('
	      interpolate_macro_with_args(def->contents);
	      break_flag = 1;
	      break;
	    }
	    token_buffer.set_length(token_buffer.length() - 1);
	  }
	  if (quoted) {
	    quoted = 0;
	    switch (c) {
	    case EOF:
	      lex_error("`\\' ignored at end of equation");
	      done = 1;
	      break;
	    case '\n':
	      lex_error("`\\' ignored because followed by newline");
	      done = 1;
	      break;
	    case '\t':
	      lex_error("`\\' ignored because followed by tab");
	      done = 1;
	      break;
	    case '"':
	      (void)get_char();
	      token_buffer += '"';
	      break;
	    default:
	      (void)get_char();
	      token_buffer += '\\';
	      token_buffer += c;
	      break;
	    }
	  }
	  else {
	    switch (c) {
	    case EOF:
	    case '{':
	    case '}':
	    case '^':
	    case '~':
	    case '"':
	    case ' ':
	    case '\t':
	    case '\n':
	      done = 1;
	      break;
	    case '\\':
	      (void)get_char();
	      quoted = 1;
	      break;
	    default:
	      (void)get_char();
	      token_buffer += char(c);
	      break;
	    }
	  }
	}
	if (break_flag || token_buffer.length() == 0)
	  break;
	if (lookup_flag != 0) {
	  token_buffer += '\0';
	  definition *def = macro_table.lookup(token_buffer.contents());
	  token_buffer.set_length(token_buffer.length() - 1);
	  if (def) {
	    if (def->is_macro) {
	      current_input = new macro_input(def->contents, current_input);
	      break;
	    }
	    else if (lookup_flag == 1) {
	      add_context(token_buffer);
	      return def->tok;
	    }
	  }
	}
	add_context(token_buffer);
	return TEXT;
      }
    }
  }
}