/*{{{  #includes*/
#ifdef CONFIG_H
#   include "config.h"
#endif

#include <sys/types.h>
#include <ctype.h>
#include <string.h>
#include <signal.h>
#include <pwd.h>
#include <limits.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>

#include <local/bool.h>

#define MISC_C
#define I_DISPLAY_C
#define I_GETMSG_C
#define I_FINDS_C
#define I_FOLDING_C
#define I_LOOP_C
#define I_MAIN_C
#define I_MESSAGES_C
#define I_ORIEDT_C
#define I_PROMPT_C
#define I_VIRTUAL_C

#include "origami.h"
#include "macros.h"
#include "../h/envvar_str.h"
/*}}}  */

#ifndef OS_FILEPROMPT
   /*{{{  fileprompt*/
   public unsigned char *fileprompt(unsigned char n[_POSIX_PATH_MAX+1])
   {
     readprompt(n,get_msg(M_FILENAME),_POSIX_PATH_MAX);
     /*{{{  maybe expand a leading ~/ to value of $HOME*/
     if (n[0]=='~' && ((n[1]==PATH_C) || !n[1]))
      { static char *h=0;

        if (!h)
         { h=getenv(HOME);
           if (!h) h=CURR_DIR;
         }
        strcpy((char*)print_buffer,h);
        strcat((char*)print_buffer,(char*)n+1);
        strcpy((char*)n,(char*)print_buffer);
      }
     /*}}}  */
     if (!*n)
       if (real_tail == real_head)
         *n = '\0';
       else if (!aborted)
         strcpy((char *)n,(char *)get_data(real_tail));
     return(n);
   }
   /*}}}  */
#endif
/*{{{  environment-variables*/
public void put_vars(void)
{
  /*{{{  static vars, to hold the environment data*/
  static char envfile[_POSIX_PATH_MAX+10];
  static char envword[LINELEN+10];
  static char envline[LINELEN+10];
  static char envposx[30];
  static char envposy[30];
  static char envpid[30];
  /*}}}  */

  /*{{{  filename*/
  strcpy(envfile,ORIFILE);
  strcat(envfile,eq);
  strcat(envfile,(char*)(real_tail==real_head?empty_text:get_data(real_tail)));
  putenv(envfile);
  /*}}}  */
  { char x[LINELEN+1];
    char *s,*w;
    int xoff;

    /*{{{  line*/
    copyin((unsigned char*)x,current,FALSE);
    strcpy(envline,ORILINE);
    strcat(envline,eq);
    /*{{{  append enter_depth_spaces ' '*/
    { int x=enter_depth_spaces;

      while (x--) strcat(envline,(char*)tspace+2);
    }
    /*}}}  */
    strcat(envline,x);
    putenv(envline);
    /*}}}  */
    /*{{{  positionx*/
    strcpy(envposx,ORIXPOS);
    strcat(envposx,eq);
    xoff=scr2txt(enter_depth_spaces,scr_pos,(unsigned char*)x)+1+enter_depth_spaces;
    strcat(envposx,i_to_a(xoff));
    putenv(envposx);
    /*}}}  */
    /*{{{  word*/
    strcpy(envword,ORIWORD);
    strcat(envword,eq);
    /*{{{  append the word*/
    /*{{{  s=&current char in line*/
    xoff-=enter_depth_spaces+1;
    s=x;
    while (*s && xoff--) s++;
    /*}}}  */
    /*{{{  maybe move to start of word*/
    if (isalpha(*(s))||isdigit(*(s))||(*(s)=='_'))
       while ((s!=x)&&(isalpha(*(s-1))||isdigit(*(s-1))||(*(s-1)=='_'))) s--;
    /*}}}  */
    /*{{{  append the word*/
    w=envword+strlen(envword);
    while (*s && (isalpha(*s)||isdigit(*s)||*s=='_')) *w++=*s++;
    *w='\0';
    /*}}}  */
    /*}}}  */
    putenv(envword);
    /*}}}  */
  }
  /*{{{  positiony*/
  strcpy(envposy,ORIYPOS);
  strcat(envposy,eq);
  strcat(envposy,i_to_a(line_no(current)-1));
  putenv(envposy);
  /*}}}  */
  /*{{{  pid*/
  strcpy(envpid,ORIPID);
  strcat(envpid,eq);
  strcat(envpid,i_to_a(getpid()));
  putenv(envpid);
  /*}}}  */
}
/*}}}  */
