Пример #1
0
/*
	prints the entries in raw_list to create a menu in columns
	this routine looks a lot like the one that formats the wide who-list

	flags is FORMAT_NUMBERED|FORMAT_NO_UNDERSCORES
*/
void print_columns(User *usr, StringList *raw_list, int flags) {
int term_width, total, cols, rows, i, j, buflen, len, max_width, idx;
StringList *sl, *sl_cols[16];
char buf[MAX_LINE*4], format[MAX_LINE], filename[MAX_PATHLEN], *p;

	if (usr == NULL || raw_list == NULL)
		return;

	term_width = usr->display->term_width;
	if (term_width >= MAX_LONGLINE)
		term_width = MAX_LONGLINE;

	total = 0;
	max_width = 10;
	for(sl = raw_list; sl != NULL; sl = sl->next) {
		len = strlen(sl->str);
		if (len > max_width)
			max_width = len;
		total++;
	}
	if (flags & FORMAT_NUMBERED)
		bufprintf(format, sizeof(format), "%c%%3d %c%%-%ds", (char)color_by_name("green"), (char)color_by_name("yellow"), max_width);
	else
		bufprintf(format, sizeof(format), "%c%%-%ds", (char)color_by_name("yellow"), max_width);

	cols = term_width / (max_width+6);
	if (cols < 1)
		cols = 1;
	else
		if (cols > 15)
			cols = 15;

	rows = total / cols;
	if (total % cols)
		rows++;

	memset(sl_cols, 0, sizeof(StringList *) * cols);

/* fill in array of pointers to columns */

	sl = raw_list;
	for(i = 0; i < cols; i++) {
		sl_cols[i] = sl;
		for(j = 0; j < rows; j++) {
			if (sl == NULL)
				break;

			sl = sl->next;
		}
	}

/* make the menu text */

	for(j = 0; j < rows; j++) {
		idx = j + 1;

		buf[0] = 0;
		buflen = 0;

		for(i = 0; i < cols; i++) {
			if (sl_cols[i] == NULL || sl_cols[i]->str == NULL)
				continue;

			cstrcpy(filename, sl_cols[i]->str, MAX_PATHLEN);

			if (flags & FORMAT_NO_UNDERSCORES) {
				p = filename;
				while((p = cstrchr(p, '_')) != NULL)
					*p = ' ';
			}
			if (flags & FORMAT_NUMBERED)
				buflen += bufprintf(buf+buflen, sizeof(buf) - buflen, format, idx, filename);
			else
				buflen += bufprintf(buf+buflen, sizeof(buf) - buflen, format, filename);
			idx += rows;

			if ((i+1) < cols) {
				buf[buflen++] = ' ';
				buf[buflen++] = ' ';
				buf[buflen] = 0;
			}
			sl_cols[i] = sl_cols[i]->next;
		}
		buf[buflen++] = '\n';
		buf[buflen] = 0;
		Put(usr, buf);
	}
}
Пример #2
0
static void load_syntax_from_file(char *dirname,char *filename,char *whole_chars_left,char *whole_chars_right)
{
  char include_file[MAX_PATH],include_full_file[MAX_PATH];
  HANDLE f=CreateFileA(filename,GENERIC_READ,0,NULL,OPEN_EXISTING,0,NULL);
  if(f!=INVALID_HANDLE_VALUE)
  {
    char *line; int size;
    while(true)
    {
      int res=read_line(f,&line,&size);
      if(res<2)
      {
        line[size]=0;
        char **argv; int argc;
        argc=get_arg(line,&argv);
        free(line);
        if(argc>0)
        {
          if((!_stricmp(argv[0],"include"))&&(argc>1))
          {
            unsigned long res=ExpandEnvironmentStringsA(argv[1],include_file,sizeof(include_file));
            if(!(res&&(res<=sizeof(include_file))))
              strcpy(include_file,argv[1]);
            if((strlen(include_file)>2)&&(((include_file[1]==':')&&((include_file[2]=='\\')||(include_file[2]=='/')))||((include_file[0]=='\\')&&(include_file[1]=='\\'))))
              strcpy(include_full_file,include_file);
            else
            {
              strcpy(include_full_file,dirname);
              strcat(include_full_file,include_file);
            }
            load_syntax_from_file(dirname,include_full_file,whole_chars_left,whole_chars_right);
          }
          if((!_stricmp(argv[0],"file"))&&(argc>2))
          {
            Rules *new_rules=(Rules *)realloc(rules,(rules_count+1)*sizeof(Rules));
            if(!new_rules) goto line_end;
            rules=new_rules;
            rules[rules_count].name=(TCHAR*)malloc((strlen(argv[1])+1)*sizeof(TCHAR));
            if(!rules[rules_count].name) goto line_end;
            MultiByteToWideChar(CP_OEMCP,0,argv[1],-1,rules[rules_count].name,strlen(argv[1])+1);
            rules[rules_count].mask=(TCHAR*)malloc((strlen(argv[2])+1)*sizeof(TCHAR));
            if(!rules[rules_count].mask) goto line_end;
            MultiByteToWideChar(CP_OEMCP,0,argv[2],-1,rules[rules_count].mask,strlen(argv[2])+1);
            if(argc>3)
            {
              rules[rules_count].start=(TCHAR*)malloc((strlen(argv[3])+1)*sizeof(TCHAR));
              if(!rules[rules_count].start) goto line_end;
              MultiByteToWideChar(CP_OEMCP,0,argv[3],-1,rules[rules_count].start,strlen(argv[3])+1);
            }
            rules[rules_count].contexts=NULL;
            rules[rules_count].contexts_count=0;
            rules_count++;
            string_to_whole(whole_chars_left,DEFAULT_WHOLE);
            string_to_whole(whole_chars_right,DEFAULT_WHOLE);
          }
          else if((!_stricmp(argv[0],"wholechars"))&&(argc>2))
          {
            if(!_stricmp(argv[1],"left"))
              string_to_whole(whole_chars_left,argv[2]);
            else if(!_stricmp(argv[1],"right"))
              string_to_whole(whole_chars_right,argv[2]);
          }
          else if(rules_count)
          {
            if(!_stricmp(argv[0],"context"))
            {
              if(!(rules[rules_count-1].contexts_count))
              {
                if(argc==1) goto line_end;
                if(_stricmp(argv[1],"default")) goto line_end;
                rules[rules_count-1].contexts=(Context *)malloc(sizeof(Context));
                rules[rules_count-1].contexts->left=NULL;
                rules[rules_count-1].contexts->right=NULL;
                rules[rules_count-1].contexts->color.Flags=ABCF_4BIT;
                rules[rules_count-1].contexts->color.ForegroundColor=0;
                rules[rules_count-1].contexts->color.BackgroundColor=0;
                rules[rules_count-1].contexts->color.Reserved=NULL;
                rules[rules_count-1].contexts->color.ForegroundDefault=true;
                rules[rules_count-1].contexts->color.BackgroundDefault=true;
                rules[rules_count-1].contexts->exclusive=0;
                rules[rules_count-1].contexts->exclusive_left=-1;
                rules[rules_count-1].contexts->exclusive_right=-1;
                rules[rules_count-1].contexts->line_start_left=0;
                rules[rules_count-1].contexts->line_start_right=0;
                rules[rules_count-1].contexts->whole_chars_left=NULL;
                rules[rules_count-1].contexts->whole_chars_right=NULL;
                rules[rules_count-1].contexts->keywords=NULL;
                rules[rules_count-1].contexts->keywords_count=0;
                if(argc>2)
                {
                  rules[rules_count-1].contexts->color.ForegroundColor=color_by_name(argv[2]);
                  rules[rules_count-1].contexts->color.ForegroundDefault=false;
                }
                if(argc>3)
                {
                  rules[rules_count-1].contexts->color.BackgroundColor=color_by_name(argv[3]);
                  rules[rules_count-1].contexts->color.BackgroundDefault=false;
                }
                rules[rules_count-1].contexts_count=1;
              }
              else
              {
                int key_count=1,whole_left=0,whole_right=0,exclusive=0,line_start_left=0,line_start_right=0;
                char *left,*right;
                if(argc<(key_count+2)) goto line_end;
                if(!_stricmp(argv[key_count],"exclusive"))
                {
                  key_count++;
                  exclusive=1;
                }
                if(argc<(key_count+2)) goto line_end;
                if(!_stricmp(argv[key_count],"whole"))
                {
                  key_count++;
                  whole_left=whole_right=1;
                }
                else if(!_stricmp(argv[key_count],"wholeleft"))
                {
                  key_count++;
                  whole_left=1;
                }
                else if(!_stricmp(argv[key_count],"wholeright"))
                {
                  key_count++;
                  whole_right=1;
                }
                if(argc<(key_count+2)) goto line_end;
                if(!_stricmp(argv[key_count],"linestart"))
                {
                  key_count++;
                  line_start_left=1;
                }
                if(argc<(key_count+2)) goto line_end;
                left=argv[key_count++];
                if(!_stricmp(argv[key_count],"linestart"))
                {
                  key_count++;
                  line_start_right=1;
                }
                if(argc<(key_count+1)) goto line_end;
                right=argv[key_count++];
                Context *NewContexts=(Context *)realloc(rules[rules_count-1].contexts,(rules[rules_count-1].contexts_count+1)*sizeof(Context));
                if(NewContexts)
                {
                  rules[rules_count-1].contexts=NewContexts;
                  NewContexts=rules[rules_count-1].contexts+rules[rules_count-1].contexts_count;
                  NewContexts->left=(char *)malloc(strlen(left)+1);
                  if(NewContexts->left)
                    strcpy(NewContexts->left,left);
                  NewContexts->right=(char *)malloc(strlen(right)+1);
                  if(NewContexts->right)
                    strcpy(NewContexts->right,right);
                  NewContexts->color=rules[rules_count-1].contexts[0].color;
                  NewContexts->exclusive=exclusive;
                  NewContexts->exclusive_left=-1;
                  NewContexts->exclusive_right=-1;
                  NewContexts->line_start_left=line_start_left;
                  NewContexts->line_start_right=line_start_right;
                  NewContexts->whole_chars_left=NULL;
                  if(whole_left)
                  {
                    NewContexts->whole_chars_left=(char *)malloc(WHOLE_SIZE);
                    if(NewContexts->whole_chars_left)
                      memcpy(NewContexts->whole_chars_left,whole_chars_left,WHOLE_SIZE);
                  }
                  NewContexts->whole_chars_right=NULL;
                  if(whole_left)
                  {
                    NewContexts->whole_chars_right=(char *)malloc(WHOLE_SIZE);
                    if(NewContexts->whole_chars_right)
                      memcpy(NewContexts->whole_chars_right,whole_chars_right,WHOLE_SIZE);
                  }
                  NewContexts->keywords=NULL;
                  NewContexts->keywords_count=0;
                  if(argc>key_count)
                  {
                    NewContexts->color.ForegroundColor=color_by_name(argv[key_count++]);
                    NewContexts->color.ForegroundDefault=false;
                  }
                  if(argc>key_count)
                  {
                    NewContexts->color.BackgroundColor=color_by_name(argv[key_count++]);
                    NewContexts->color.BackgroundDefault=false;
                  }
                  rules[rules_count-1].contexts_count++;
                }
              }
            }
            else if(!_stricmp(argv[0],"keyword"))
            {
              if(rules[rules_count-1].contexts_count)
              {
                int key_count=1,whole_left=0,whole_right=0,line_start=0,recursive=0;
                char *keyword;
                if(argc<(key_count+1)) goto line_end;
                if(!_stricmp(argv[key_count],"whole"))
                {
                  key_count++;
                  whole_left=whole_right=1;
                }
                else if(!_stricmp(argv[key_count],"wholeleft"))
                {
                  key_count++;
                  whole_left=1;
                }
                else if(!_stricmp(argv[key_count],"wholeright"))
                {
                  key_count++;
                  whole_right=1;
                }
                if(argc<(key_count+1)) goto line_end;
                if(!_stricmp(argv[key_count],"linestart"))
                {
                  key_count++;
                  line_start=1;
                }
                if(argc<(key_count+1)) goto line_end;
                if(!_stricmp(argv[key_count],"recursive"))
                {
                  key_count++;
                  recursive=1;
                }
                if(argc<(key_count+1)) goto line_end;
                keyword=argv[key_count++];
                Keyword *NewKeyword=(Keyword *)realloc(rules[rules_count-1].contexts[rules[rules_count-1].contexts_count-1].keywords,(rules[rules_count-1].contexts[rules[rules_count-1].contexts_count-1].keywords_count+1)*sizeof(Keyword));
                if(NewKeyword)
                {
                  rules[rules_count-1].contexts[rules[rules_count-1].contexts_count-1].keywords=NewKeyword;
                  NewKeyword=rules[rules_count-1].contexts[rules[rules_count-1].contexts_count-1].keywords+rules[rules_count-1].contexts[rules[rules_count-1].contexts_count-1].keywords_count;
                  NewKeyword->keyword=(char *)malloc(strlen(keyword)+1);
                  if(NewKeyword->keyword)
                    strcpy(NewKeyword->keyword,keyword);
                  NewKeyword->line_start=line_start;
                  NewKeyword->color=rules[rules_count-1].contexts[rules[rules_count-1].contexts_count-1].color;
                  NewKeyword->exclusive=0;
                  NewKeyword->recursive=recursive;
                  NewKeyword->whole_chars_left=NULL;
                  if(whole_left)
                  {
                    NewKeyword->whole_chars_left=(char *)malloc(WHOLE_SIZE);
                    if(NewKeyword->whole_chars_left)
                      memcpy(NewKeyword->whole_chars_left,whole_chars_left,WHOLE_SIZE);
                  }
                  NewKeyword->whole_chars_right=NULL;
                  if(whole_right)
                  {
                    NewKeyword->whole_chars_right=(char *)malloc(WHOLE_SIZE);
                    if(NewKeyword->whole_chars_right)
                      memcpy(NewKeyword->whole_chars_right,whole_chars_right,WHOLE_SIZE);
                  }
                  if(argc>key_count)
                  {
                    NewKeyword->color.ForegroundColor=color_by_name(argv[key_count++]);
                    NewKeyword->color.ForegroundDefault=false;
                  }
                  if(argc>key_count)
                  {
                    NewKeyword->color.BackgroundColor=color_by_name(argv[key_count++]);
                    NewKeyword->color.BackgroundDefault=false;
                  }
                  rules[rules_count-1].contexts[rules[rules_count-1].contexts_count-1].keywords_count++;
                }
              }
            }
          }
        }
line_end:
        free_arg(argv,argc);
        if(res)
          break;
      }
      else //error
      {
        break;
      }
    }
    CloseHandle(f);
  }
}