Index: app/src/common/Makefile
===================================================================
--- app/src/common/Makefile	(revision 25ae32e4c2354e0ed6756bbe1de83f39cd814652)
+++ app/src/common/Makefile	(revision 25ae32e4c2354e0ed6756bbe1de83f39cd814652)
@@ -0,0 +1,8 @@
+# main: cmdline.c main_template.cc
+# 	g++ -o main cmdline.c common.cc main_template.cc
+
+# cmdline.c cmdline.h : cmdline.ggo
+# 	gengetopt -i cmdline.ggo
+
+# cmdline.ggo: cmdline_common.ggo cmdline_program.ggo
+# 	cat cmdline_common.ggo cmdline_program.ggo > cmdline.ggo
Index: app/src/common/README
===================================================================
--- app/src/common/README	(revision 25ae32e4c2354e0ed6756bbe1de83f39cd814652)
+++ app/src/common/README	(revision 25ae32e4c2354e0ed6756bbe1de83f39cd814652)
@@ -0,0 +1,18 @@
+Propozycja ujednolicenia dzialania klocka na poziomie
+funkcji main. Parametry meta - zdefiniowane dla
+wszystkich, poza tok, programow, definiujace ich zachowanie
+w systemie klockow.
+
+cmdline_common.ggo		- deklaracje parametrow meta
+
+cmdline_program.ggo		- przyklad deklaracji parametrow programu
+				  nazwa docelowa np. cmdline_guess.ggo
+
+common.cc			- zmienne globalne zawierajace informacje
+				  przekazane przez parametry meta 
+common.h			
+
+main_template.cc		- szkielet funkcji main
+
+Makefile			- sposob kompilacji
+
Index: app/src/common/cmdline_common.ggo
===================================================================
--- app/src/common/cmdline_common.ggo	(revision 25ae32e4c2354e0ed6756bbe1de83f39cd814652)
+++ app/src/common/cmdline_common.ggo	(revision 25ae32e4c2354e0ed6756bbe1de83f39cd814652)
@@ -0,0 +1,34 @@
+#section "Common UTT options"
+
+
+option  "input"		f	"Input file" string no hidden
+
+option  "output"	o	"Output file" string no hidden
+
+option  "fail"		e	"Output file for unsuccesfully processed segments " string no hidden
+
+option 	"only-fail"	-	"Print only segments the program failed to process" flag off hidden
+
+option 	"no-fail"	-	"Print only segments the program processed" flag off hidden 
+
+option  "copy"		c       "Copy succesfully processed segments to standard output" flag off hidden
+
+option  "process"	p	"Process segments with this tag" string no multiple
+
+option  "select"	s	"Select only segments with this field" string no multiple
+
+option  "ignore"	S	"Select only segments without this field" string no multiple
+
+option	"output-field"	O	"Output field name" string no
+
+option	"input-field"	I	"Input field name" string no multiple
+
+option	"interactive"	i	"Toggle interactive mode" flag off
+
+option  "config"	-	"Configuration file" string typestr="FILENAME" no
+
+option 	"one-field"	1	"Print all results in one segments (creates ambiguous annotation)" flag off
+
+option	"one-line"	-	"Print annotation alternatives as additional fields" flag off
+
+option	"language"	-	"Language." string no
Index: app/src/common/cmdline_program.ggo
===================================================================
--- app/src/common/cmdline_program.ggo	(revision 25ae32e4c2354e0ed6756bbe1de83f39cd814652)
+++ app/src/common/cmdline_program.ggo	(revision 25ae32e4c2354e0ed6756bbe1de83f39cd814652)
@@ -0,0 +1,5 @@
+package "guess"
+version "0.1"
+
+option  "color"		l       "Show guessed descriptions in colour." flag off
+
Index: app/src/common/common.cc
===================================================================
--- app/src/common/common.cc	(revision 25ae32e4c2354e0ed6756bbe1de83f39cd814652)
+++ app/src/common/common.cc	(revision 25ae32e4c2354e0ed6756bbe1de83f39cd814652)
@@ -0,0 +1,264 @@
+#include <stdlib.h>
+#include <string.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <unistd.h>
+#include "common.h"
+
+#include <stdio.h>
+#include <locale.h>
+
+FILE* inputf=stdin;
+FILE* outputf=stdout;
+FILE* failedf=stdout;
+bool copy_processed=0;
+bool one_field=false;
+bool one_line=false;
+char output_field_prefix[32];
+char input_field_prefix[32];
+
+extern int argc;
+extern char **argv;
+
+
+// tilde (home dir) expansion in path
+int expand_path(char* inpath, char* outpath)
+{
+  if(inpath[0]=='~')
+    sprintf(outpath,"%s%s",getenv("HOME"),inpath+1);
+  else
+    strcpy(outpath,inpath);
+  return 0; // no problem
+}
+
+
+
+
+/*
+parameters:
+        -name - field name, long or short
+        +prefix - field name with ':' appended if long name
+return value:
+        1 if correct field name, 0 otherwise
+examples:
+name    prefix  r.v.
+lem     lem:    1
+@       @       1
+::      'undef' 0
+a,b     'undef' 0
+*/
+int fieldprefix(char *name, char *prefix)
+{
+  if (ispunct(name[0]) && name[1]=='\0') // correct short name
+  {
+    strcpy(prefix, name); return 1;
+  }
+
+  int i=0;
+  while(name[i]!='\0' && isalnum(name[i])) ++i;
+  
+  if(name[i]=='\0' && i>0) // correct long name
+  {
+    sprintf(prefix,"%s:",name); return 1;
+  }
+
+  // incorrect
+  return 0;
+}
+
+
+
+void set_program_name(char program_name[], char* argv0)
+{
+  if (char* p_name = strrchr(argv0, '/'))
+    strcpy(program_name,p_name+1);
+  else
+    strcpy(program_name,argv0);
+}
+
+
+
+extern void process_config_files(gengetopt_args_info* args, char* argv0)
+{
+
+  char program_name[256];
+  char config_file[256];
+  char config_file_tmp[256];
+
+  set_program_name(program_name,argv0);
+
+  // obsÅuga pliku konfiguracyjnego podanego w linii komend
+  if (args->config_given) {
+    if (file_accessible(args->config_arg) == 0) {
+      if (cmdline_parser_configfile(args->config_arg,
+				    args,
+				    0, // 0 - nie nadpisuj wartoÅci parametrÃ³w
+				    0, // 0 - nie inicjuj
+				    0) != 0) {
+	fprintf(stderr, "Error in config file (%s)\n", args->config_arg);
+	exit(1);
+      }
+    }
+  }
+
+  if(args->one_line_given && !one_line) one_line=true, one_field=false;
+  if(args->one_field_given && !one_field) one_line=false, one_field=true;
+
+  // obsluga pliku konfiguracyjnego uzytkownika dla programu
+  sprintf(config_file_tmp, "%s/%s.conf", USER_CONFIG_DIR, program_name);
+  expand_path(config_file_tmp, config_file);
+  if (file_accessible(config_file) == 0) {
+    if (cmdline_parser_configfile(config_file,
+				  args,
+				  0, // 0 - nie nadpisuj danych
+				  0, // 0 - nie inicjuj struktury
+				  0) != 0) {
+      fprintf(stderr, "Error in config file (%s)\n", config_file);
+      exit(1);
+    }
+  }
+
+  if(args->one_line_given && !one_line) one_line=true, one_field=false;
+  if(args->one_field_given && !one_field) one_line=false, one_field=true;
+
+
+  // obsluga pliku konfiguracyjnego uzytkownika globalnego
+  sprintf(config_file_tmp, "%s/utt.conf", USER_CONFIG_DIR);
+  expand_path(config_file_tmp, config_file);
+  if (file_accessible(config_file) == 0) {
+    if (cmdline_parser_configfile(config_file,
+				  args,
+				  0, // 0 - nie nadpisuj danych
+				  0, // 0 - nie inicjuj struktury
+				  0) != 0) {
+      fprintf(stderr, "Error in config file (%s)\n", config_file);
+      exit(1);
+    }
+  }
+
+  if(args->one_line_given && !one_line) one_line=true, one_field=false;
+  if(args->one_field_given && !one_field) one_line=false, one_field=true;
+
+
+
+  // obsluga systemowego pliku konfiguracyjnego dla programu
+  sprintf(config_file, "%s/%s.conf", SYSTEM_CONFIG_DIR, program_name);
+  if (file_accessible(config_file) == 0) {
+    if (cmdline_parser_configfile(config_file,
+				  args,
+				  0, // 0 - nie zmieniaj danych wczesniejszych
+				  0, // 0 - nie inicjuj struktury
+				  0  // 0 - nie sprawdzaj wymaganych parametrow
+				  ) != 0) {
+      fprintf(stderr, "Error in config file (%s)\n", config_file);
+      exit(1);
+    }
+  }
+
+  if(args->one_line_given && !one_line) one_line=true, one_field=false;
+  if(args->one_field_given && !one_field) one_line=false, one_field=true;
+
+
+  // obsluga systemowego pliku konfiguracyjnego globalnego
+  sprintf(config_file, "%s/utt.conf", SYSTEM_CONFIG_DIR);
+  if (file_accessible(config_file) == 0) {
+    if (cmdline_parser_configfile(config_file,
+				  args,
+				  0, // 0 - nie zmieniaj danych wczesniejszych
+				  0, // 0 - nie inicjuj struktury
+				  0  // 0 - nie sprawdzaj wymaganych parametrow
+				  ) != 0) {
+      fprintf(stderr, "Error in config file (%s)\n", config_file);
+      exit(1);
+    }
+  }
+
+  if(args->one_line_given && !one_line) one_line=true, one_field=false;
+  if(args->one_field_given && !one_field) one_line=false, one_field=true;
+
+}
+
+
+void process_common_options(gengetopt_args_info* args, char* argv0)
+{
+  char program_name[256];
+
+  set_program_name(program_name,argv0);
+
+  setlocale(LC_CTYPE,"");
+  setlocale(LC_COLLATE, "");
+
+  if(args->help_given)
+      cmdline_parser_print_help ();
+
+  if(args->input_given)
+    if(!(inputf=fopen(args->input_arg,"r")))
+    {
+      fprintf(stderr,"No such file: %s.\n", args->input_arg);
+      exit(1);
+    }
+  
+  if(args->output_given)
+    if(!(outputf=fopen(args->output_arg,"w")))
+    {
+      fprintf(stderr,"Cannot open output file: %s.\n", args->output_arg);
+      exit(1);
+    }
+  
+  if(args->fail_given)
+      if(!(failedf=fopen(args->fail_arg,"w")))
+      {
+	fprintf(stderr,"Cannot open output file: %s.\n", args->fail_arg);
+	exit(1);
+      }
+
+  if(args->input_field_given)
+    fieldprefix(args->input_field_arg[0],input_field_prefix);
+  else
+    strcpy(input_field_prefix, "4");
+
+  if(args->output_field_given)
+    fieldprefix(args->output_field_arg,output_field_prefix);
+  else
+    sprintf(output_field_prefix, "%s%c", program_name, INFIELD_SEP);
+
+  if ((args->copy_given))
+    copy_processed=true;
+}
+
+// sprawdza istnienie pliku
+int file_accessible(const char* path) {
+    return access(path, R_OK);
+}
+
+// sprawdza istnienie pliku konfiguracyjnego
+int config_file_exists(const char* dir, const char* filename) {
+    struct stat dir_stat;
+    struct stat file_stat;
+
+    char* path = (char*)malloc(strlen(dir) + strlen(filename) + 2); // + '\0' + '/'
+
+    sprintf(path, "%s/%s", dir, filename);
+
+    if (stat(dir, &dir_stat) != 0)
+	return -1;
+
+    if (stat(path, &file_stat) != 0)
+	return -1;
+
+    if (!S_ISDIR(dir_stat.st_mode))
+	return -1; // katalog nie jest katalogiem
+
+    if (!S_ISREG(file_stat.st_mode))
+	return -1; // plik konfiguracyjny nie jest plikiem
+
+    if (access(dir, X_OK) != 0)
+	return -1; // nie mamy prawa zmienic katalogu
+
+    if (access(path, R_OK) != 0)
+	return -1; // nie mamy prawa odczytu pliku
+
+    free(path);
+
+    return 0;
+}
Index: app/src/common/common.h
===================================================================
--- app/src/common/common.h	(revision 25ae32e4c2354e0ed6756bbe1de83f39cd814652)
+++ app/src/common/common.h	(revision 25ae32e4c2354e0ed6756bbe1de83f39cd814652)
@@ -0,0 +1,416 @@
+#ifndef __COMMON_H
+#define __COMMON_H
+
+#include <stdio.h>
+#include <ctype.h>
+
+#include "../lib/const.h"
+
+#include _CMDLINE_FILE
+
+
+/**************************************************
+ * Stale dotyczace wejscia/wyjscia
+ */
+
+#define EMPTYFORM '*'
+#define INFIELD_SEP ':'
+#define MAXAUX 16
+#define FIELD_SEP " \t\n"
+
+
+// katalogi z plikami konfiguracyjnymi
+// nowe
+// stare - do wyrzucenia
+// #define CONFIG_DIR ".utt/conf"
+
+// nazwa zmiennej okreslajaca sciezke do danych
+
+// #define UTT_DIR_VAR "UTT_DIR"
+
+// sciezka do plikow z danymi (np UTT_DIR/pliki) wzgledem $HOME!
+
+// #define UTT_DIR_DEFAULT ".utt/pl/"
+
+/**************************************************/
+
+
+extern FILE* inputf;
+extern FILE* outputf;
+extern FILE* failedf;
+
+extern char* input_filename;
+extern char* output_filename;
+extern char* failed_filename;
+extern bool one_line;
+extern bool one_field;
+
+extern char input_field_prefix[];
+extern char output_field_prefix[];
+
+extern bool copy_processed;
+extern bool append_output;
+extern bool append_failed;
+
+//sciezka do katalogu z danymi
+extern char utt_dir[];
+
+extern void process_common_options(gengetopt_args_info* args, char* argv0);
+extern void process_config_files(gengetopt_args_info* args, char* argv0);
+
+extern int expand_path(char* inpath, char* outpath);
+
+extern int fieldprefix(char *name, char *prefix);
+
+
+/**************************************************
+ * problems with casing                           */
+// sprawdzenie wielkosci liter
+// warto¶æ zwracana:
+// 0 - wszystkie ma³e litery
+// 1 - pierwsza wielka, reszta male
+// 2 - wszystkie wielkie
+// 3 - inne
+inline int casing(char* s)
+{
+  int ret = isupper(*s) ? 1 : 0;
+  while(*++s != '\0')
+  {
+    if(isupper(*s))
+    {
+      if(ret==1) ret=2;
+      else if(ret==0) ret=3;
+    }
+    else
+    {
+      if(ret==2) ret=3;
+    }
+  }
+  return ret;
+}
+
+// 
+inline void tolowers(char* s, char* d)
+{
+  *d=tolower(*s);
+  while(*s != '\0') * ++d = tolower(* ++s);
+}
+
+
+// przepisuje s do d
+// nadajac wielko¶æ liter zgodnie z warto¶ci± casing
+// casing - warto¶æ zwracana przez casing()
+// je¶li casing==3 przepisuje bez zmian (za ma³o informacji)
+inline void restorecasing(char *s, char *d, int casing)
+{
+  switch(casing)
+  {
+  case 0:
+  case 3:
+    *d=*s;
+    while(*s != '\0') * ++d = * ++s;
+    break;
+  case 1:
+    *d=toupper(*s);
+    while(*s != '\0') * ++d = * ++s;
+    break;
+  case 2:
+    *d=toupper(*s);
+    while(*s != '\0') * ++d = toupper(* ++s);
+    break;
+  }
+}
+
+/**************************************************/
+
+/*
+parameters:
+  -seg  - segment
+  -pref - field name or "1", "2", "3", "4" for the first four fields
+  +val  - field contents
+return value:
+  1 if specified field exists, 0 otherwise
+*/
+
+inline int getfield(char* seg, const char* pref, char* val)
+{
+
+  char* p=seg;
+  char* p0;
+
+  while(isspace(*p)) ++p;
+
+  // field "1"
+  p0=p; while(isdigit(*p)) ++p;
+  if(*pref=='1') if(p!=p0) { strncpy(val,p0,p-p0); val[p-p0]='\0'; return 1; } else return 0;
+  
+  while(isspace(*p)) ++p;
+
+  // field "2"
+  p0=p; while(isdigit(*p)) ++p;
+  if(*pref=='2') if(p!=p0) { strncpy(val,p0,p-p0); val[p-p0]='\0'; return 1; } else return 0;
+
+  while(isspace(*p)) ++p;
+
+  // field "3"
+  p0=p; while(isgraph(*p)) ++p; 
+  if(*pref=='3') if(p!=p0) { strncpy(val,p0,p-p0); val[p-p0]='\0'; return 1; } else return 0;
+
+  while(isspace(*p)) ++p;
+
+  // field "4"
+  p0=p; while(isgraph(*p)) ++p;
+  if(*pref=='4') if(p!=p0) { strncpy(val,p0,p-p0); val[p-p0]='\0'; return 1; } else return 0;
+
+  while(isspace(*p)) ++p;
+
+  // annotation fields
+  do p=strstr(p,pref); while(p!=NULL && *(p-1)!=' ' && *(p-1)!='\t');
+  
+  if(p==NULL) return 0;
+  else
+  {
+    p+=strlen(pref);
+    int len=strcspn(p,FIELD_SEP "\n\r\f\0");
+    strncpy(val,p,len);
+    val[len]='\0';
+    return 1;
+  }
+}
+
+
+inline
+bool process_seg(char* seg, gengetopt_args_info& args)
+{
+  char buf[256];
+  bool ret = !args.process_given;
+  if(args.process_given)
+    {
+      getfield(seg,"3",buf);
+      for(int i=0; i<args.process_given; ++i)
+	if(strcmp(args.process_arg[i],buf)==0)
+	  {
+	    ret=true;
+	    break;
+	  }
+    }
+  for(int i=0; i<args.select_given; ++i)
+    if(! getfield(seg,args.select_arg[i],buf))
+      ret=false;
+  for(int i=0; i<args.ignore_given; ++i)
+    if(getfield(seg,args.ignore_arg[i],buf))
+      ret=false;
+  return ret;
+}
+
+
+/*
+parameters:
+  -+seg - segment
+  -pref - prefix of the new field
+  -val  - contents of the new field
+return value:
+  1 - success, 0 - fail (limit on segment length exceeded)
+ */
+inline
+int addfield(char *seg, const char *pref, const char *val)
+     // zalozenie, ze seg konczy sie znakiem \n
+{
+  if(strlen(seg)+strlen(pref)+strlen(val) >= MAX_LINE) return 0; // bezpieczniej, ale wolniej
+
+  int seglen=strlen(seg);
+  sprintf(seg+(seglen-1)," %s%s\n",pref,val);
+  return 1;
+}
+
+/**************************************************/
+
+struct Seg
+{
+  int filepos, len;
+  char* tag;
+  char* form;
+  char* aux[MAXAUX];
+  int auxn;
+
+  bool parse(char* line);
+  char* getfield(char* fieldname);
+  void print(char* line);
+  bool addfield(char* s);
+  bool clearfields();
+};
+
+/**************************************************/
+
+/* definicja struktury wejscia/wyjscia
+ */
+struct Segment
+{
+  int filepos, len;
+  char* tag;
+  char* form;
+  char* aux[MAXAUX];
+  int auxn;
+
+  bool parse(char* line);
+  char* getfield(char* fieldname);
+  void print(char* line);
+  bool addfield(char* s);
+  bool clearfields();
+};
+
+/*
+ * Sprawdza czy nalezy przetwarzac dany segment.
+ */
+
+inline
+bool process_seg(Segment& s, gengetopt_args_info& args)
+{
+  bool ret = !args.process_given;
+
+  for(int i=0; i<args.process_given; ++i)
+    if(strcmp(args.process_arg[i],s.tag)==0)
+      {
+        ret=true;
+        break;
+      }
+
+  for(int i=0; i<args.select_given; ++i)
+    if(! s.getfield(args.select_arg[i]))
+      ret=false;
+
+  for(int i=0; i<args.ignore_given; ++i)
+    if(s.getfield(args.ignore_arg[i]))
+      ret=false;
+
+  return ret;
+}
+
+
+/*
+ * FUNKCJE OBSLUGUJACE WEJSCIE/WYJSCIE
+ */
+// napisy zostaj na miejscu (w line), tylko wskazniki sa ustawian
+// i zara dopisywane zera s dopisywane
+
+inline
+bool Segment::parse(char* line)
+{
+  auxn=0;
+  char* field;
+  if((field=strtok(line,FIELD_SEP))!=NULL)
+    filepos=atoi(field); // nie sprawdzana poprawnosc
+  else
+    return false;
+  if((field=strtok(NULL,FIELD_SEP))!=NULL)
+    len=atoi(field); // nie sprawdzana poprawnosc
+  else return false;
+  if((tag=strtok(NULL,FIELD_SEP))==NULL) return false;
+  if((form=strtok(NULL,FIELD_SEP))==NULL)
+    return true;
+  else
+    if(form[0] == EMPTYFORM && form[1] =='\0')
+      form=NULL;
+
+  while((aux[auxn]=strtok(NULL,FIELD_SEP))!=NULL) ++auxn;
+
+  return true;
+}
+
+
+inline char* Segment::getfield(char* f)
+{
+  int flen=strlen(f);
+  if(isalnum(*f))
+  {
+    for(int i=0; i<auxn; ++i)
+      if(strncmp(aux[i],f,flen)==0 && aux[i][flen]==INFIELD_SEP)
+	return aux[i]+flen+1;
+  } else
+  {
+    for(int i=0; i<auxn; ++i)
+    {
+      if(*f==*(aux[i]))
+	return aux[i]+1;
+    }
+  }
+  return NULL;
+}
+
+inline bool Segment::clearfields() {
+  for (int i=0; i<auxn; ++i) {
+    //    free(aux[i]);
+    aux[i] = NULL;
+  }
+  auxn=0;
+  return true;
+}
+
+inline // NIEEFEKTYWNE
+void Segment::print(char* line)
+{
+  sprintf(line,"%04d %02d %s", filepos, len, tag);
+  if(form)
+    {
+      strcat(line," ");
+      strcat(line,form);
+    }
+  else
+    if(auxn)
+      strcat(line," *");
+
+  for(int i=0; i<auxn; ++i)
+    {
+      strcat(line," ");
+      strcat(line,aux[i]);
+    }
+
+  strcat(line,"\n");
+}
+
+
+inline
+bool Segment::addfield(char* s)
+{
+  if(auxn<MAXAUX)
+    {
+      aux[auxn++]=s;
+      return true;
+    }
+  else
+    return false;
+}
+
+/**************************************************
+ * funkcje pomocne w operacjach na plikach        *
+ *  konfiguracyjnych                              *
+ **************************************************/
+
+// sprawdza istnienie pliku
+int file_accessible(const char* path);
+
+// sprawdza istnienie pliku konfiguracyjnego
+int config_file(const char* dir, const char* filename);
+
+/**************************************************/
+
+/* Pobiera wejscie
+ * parametry:
+ * - args - tablica stringow okresnajacych pola wejsciowe
+ * - args_len - rozmiar args
+ * - seg - segment
+ * wartosc - wskaznik do wejscia
+ */
+inline char*  getInput(char** args, int args_len, Segment seg) {
+  char* formp = NULL;
+  for (int i=0; i<args_len; ++i) {
+    if ('4' == args[i][0])
+      return seg.form;
+    if ((formp = seg.getfield(args[i])) != NULL) {
+      return formp;
+    }
+  }
+  return formp;
+}
+
+#endif
Index: app/src/common/main_template.cc
===================================================================
--- app/src/common/main_template.cc	(revision 25ae32e4c2354e0ed6756bbe1de83f39cd814652)
+++ app/src/common/main_template.cc	(revision 25ae32e4c2354e0ed6756bbe1de83f39cd814652)
@@ -0,0 +1,20 @@
+#include <stdlib.h>
+
+#include "common.h"
+
+main(int argc, char* argv[])
+{
+  gengetopt_args_info args;
+
+  if(cmdline_parser(argc,argv,&args) != 0)
+    exit(1);
+  
+  process_common_options(args);
+
+  //
+  // TU KOD
+  //
+
+  cmdline_parser_free(&args);
+  
+}
