Fixed broken scrollbars on some pages.
[vimprobable2.git] / utilities.c
blobb6b13188aaa17bb3da51fed9b1ee88199c0af03f
1 /*
2 (c) 2009 by Leon Winter
3 (c) 2009-2011 by Hannes Schueller
4 (c) 2009-2010 by Matto Fransen
5 (c) 2010-2011 by Hans-Peter Deifel
6 (c) 2010-2011 by Thomas Adam
7 see LICENSE file
8 */
10 #include "includes.h"
11 #include "vimprobable.h"
12 #include "main.h"
13 #include "utilities.h"
15 extern char commandhistory[COMMANDHISTSIZE][255];
16 extern Command commands[COMMANDSIZE];
17 extern int lastcommand, maxcommands, commandpointer;
18 extern KeyList *keylistroot;
19 extern Key keys[];
20 extern char *error_msg;
21 extern gboolean complete_case_sensitive;
22 extern char *config_base;
23 static GList *dynamic_searchengines = NULL;
25 gboolean read_rcfile(const char *config)
27 int t;
28 char s[255];
29 FILE *fpin;
30 gboolean returnval = TRUE;
32 if ((fpin = fopen(config, "r")) == NULL)
33 return FALSE;
34 while (fgets(s, 254, fpin)) {
36 * ignore lines that begin with #, / and such
38 if (!isalpha(s[0]))
39 continue;
40 t = strlen(s);
41 s[t - 1] = '\0';
42 if (!process_line(s))
43 returnval = FALSE;
45 fclose(fpin);
46 return returnval;
49 void save_command_history(char *line)
51 char *c;
53 c = line;
54 while (isspace(*c) && *c)
55 c++;
56 if (!strlen(c))
57 return;
58 strncpy(commandhistory[lastcommand], ":", 1);
59 strncat(commandhistory[lastcommand], c, 254);
60 lastcommand++;
61 if (maxcommands < COMMANDHISTSIZE - 1)
62 maxcommands++;
63 if (lastcommand == COMMANDHISTSIZE)
64 lastcommand = 0;
65 commandpointer = lastcommand;
68 gboolean
69 process_save_qmark(const char *bm, WebKitWebView *webview)
71 FILE *fp;
72 const char *filename;
73 const char *uri = webkit_web_view_get_uri(webview);
74 char qmarks[10][101];
75 char buf[100];
76 int i, mark, l=0;
77 Arg a;
78 mark = -1;
79 mark = atoi(bm);
80 if ( mark < 1 || mark > 9 )
82 a.i = Error;
83 a.s = g_strdup_printf("Invalid quickmark, only 1-9");
84 echo(&a);
85 g_free(a.s);
86 return TRUE;
88 if ( uri == NULL ) return FALSE;
89 for( i=0; i < 9; ++i ) strcpy( qmarks[i], "");
91 filename = g_strdup_printf(QUICKMARK_FILE);
93 /* get current quickmarks */
95 fp = fopen(filename, "r");
96 if (fp != NULL){
97 for( i=0; i < 10; ++i ) {
98 if (feof(fp)) {
99 break;
101 fgets(buf, 100, fp);
102 l = 0;
103 while (buf[l] && l < 100 && buf[l] != '\n') {
104 qmarks[i][l]=buf[l];
105 l++;
107 qmarks[i][l]='\0';
109 fclose(fp);
112 /* save quickmarks */
113 strcpy( qmarks[mark-1], uri );
114 fp = fopen(filename, "w");
115 if (fp == NULL) return FALSE;
116 for( i=0; i < 10; ++i )
117 fprintf(fp, "%s\n", qmarks[i]);
118 fclose(fp);
119 a.i = Error;
120 a.s = g_strdup_printf("Saved as quickmark %d: %s", mark, uri);
121 echo(&a);
122 g_free(a.s);
124 return TRUE;
127 void
128 make_keyslist(void)
130 int i;
131 KeyList *ptr, *current;
133 ptr = NULL;
134 current = NULL;
135 i = 0;
136 while ( keys[i].key != 0 )
138 current = malloc(sizeof(KeyList));
139 if (current == NULL) {
140 printf("Not enough memory\n");
141 exit(-1);
143 current->Element = keys[i];
144 current->next = NULL;
145 if (keylistroot == NULL) keylistroot = current;
146 if (ptr != NULL) ptr->next = current;
147 ptr = current;
148 i++;
152 gboolean
153 parse_colour(char *color) {
154 char goodcolor[8];
155 int colorlen;
157 colorlen = (int)strlen(color);
159 goodcolor[0] = '#';
160 goodcolor[7] = '\0';
162 /* help the user a bit by making string like
163 #a10 and strings like ffffff full 6digit
164 strings with # in front :)
167 if (color[0] == '#') {
168 switch (colorlen) {
169 case 7:
170 strncpy(goodcolor, color, 7);
171 break;
172 case 4:
173 goodcolor[1] = color[1];
174 goodcolor[2] = color[1];
175 goodcolor[3] = color[2];
176 goodcolor[4] = color[2];
177 goodcolor[5] = color[3];
178 goodcolor[6] = color[3];
179 break;
180 case 2:
181 goodcolor[1] = color[1];
182 goodcolor[2] = color[1];
183 goodcolor[3] = color[1];
184 goodcolor[4] = color[1];
185 goodcolor[5] = color[1];
186 goodcolor[6] = color[1];
187 break;
189 } else {
190 switch (colorlen) {
191 case 6:
192 strncpy(&goodcolor[1], color, 6);
193 break;
194 case 3:
195 goodcolor[1] = color[0];
196 goodcolor[2] = color[0];
197 goodcolor[3] = color[1];
198 goodcolor[4] = color[1];
199 goodcolor[5] = color[2];
200 goodcolor[6] = color[2];
201 break;
202 case 1:
203 goodcolor[1] = color[0];
204 goodcolor[2] = color[0];
205 goodcolor[3] = color[0];
206 goodcolor[4] = color[0];
207 goodcolor[5] = color[0];
208 goodcolor[6] = color[0];
209 break;
213 if (strlen (goodcolor) != 7) {
214 return FALSE;
215 } else {
216 strncpy(color, goodcolor, 8);
217 return TRUE;
221 gboolean
222 process_line_arg(const Arg *arg) {
223 return process_line(arg->s);
226 gboolean
227 changemapping(Key *search_key, int maprecord, char *cmd) {
228 KeyList *current, *newkey;
229 Arg a = { .s = cmd };
231 /* sanity check */
232 if (maprecord < 0 && cmd == NULL) {
233 /* possible states:
234 * - maprecord >= 0 && cmd == NULL: mapping to internal symbol
235 * - maprecord < 0 && cmd != NULL: mapping to command line
236 * - maprecord >= 0 && cmd != NULL: cmd will be ignored, treated as mapping to internal symbol
237 * - anything else (gets in here): an error, hence we return FALSE */
238 return FALSE;
241 current = keylistroot;
243 if (current)
244 while (current->next != NULL) {
245 if (
246 current->Element.mask == search_key->mask &&
247 current->Element.modkey == search_key->modkey &&
248 current->Element.key == search_key->key
250 if (maprecord >= 0) {
251 /* mapping to an internal signal */
252 current->Element.func = commands[maprecord].func;
253 current->Element.arg = commands[maprecord].arg;
254 } else {
255 /* mapping to a command line */
256 current->Element.func = process_line_arg;
257 current->Element.arg = a;
259 return TRUE;
261 current = current->next;
263 newkey = malloc(sizeof(KeyList));
264 if (newkey == NULL) {
265 printf("Not enough memory\n");
266 exit (-1);
268 newkey->Element.mask = search_key->mask;
269 newkey->Element.modkey = search_key->modkey;
270 newkey->Element.key = search_key->key;
271 if (maprecord >= 0) {
272 /* mapping to an internal signal */
273 newkey->Element.func = commands[maprecord].func;
274 newkey->Element.arg = commands[maprecord].arg;
275 } else {
276 /* mapping to a command line */
277 newkey->Element.func = process_line_arg;
278 newkey->Element.arg = a;
280 newkey->next = NULL;
282 if (keylistroot == NULL) keylistroot = newkey;
284 if (current != NULL) current->next = newkey;
286 return TRUE;
289 gboolean
290 mappings(const Arg *arg) {
291 char line[255];
293 if (!arg->s) {
294 set_error("Missing argument.");
295 return FALSE;
297 strncpy(line, arg->s, 254);
298 if (process_map_line(line))
299 return TRUE;
300 else {
301 set_error("Invalid mapping.");
302 return FALSE;
306 int
307 get_modkey(char key) {
308 switch (key) {
309 case '1':
310 return GDK_MOD1_MASK;
311 case '2':
312 return GDK_MOD2_MASK;
313 case '3':
314 return GDK_MOD3_MASK;
315 case '4':
316 return GDK_MOD4_MASK;
317 case '5':
318 return GDK_MOD5_MASK;
319 default:
320 return FALSE;
324 gboolean
325 process_mapping(char *keystring, int maprecord, char *cmd) {
326 Key search_key;
328 search_key.mask = 0;
329 search_key.modkey = 0;
330 search_key.key = 0;
332 if (strlen(keystring) == 1) {
333 search_key.key = keystring[0];
336 if (strlen(keystring) == 2) {
337 search_key.modkey= keystring[0];
338 search_key.key = keystring[1];
341 /* process stuff like <S-v> for Shift-v or <C-v> for Ctrl-v (strlen == 5),
342 stuff like <S-v>a for Shift-v,a or <C-v>a for Ctrl-v,a (strlen == 6 && keystring[4] == '>')
343 stuff like <M1-v> for Mod1-v (strlen == 6 && keystring[5] == '>')
344 or stuff like <M1-v>a for Mod1-v,a (strlen = 7)
346 if (
347 ((strlen(keystring) == 5 || strlen(keystring) == 6) && keystring[0] == '<' && keystring[4] == '>') ||
348 ((strlen(keystring) == 6 || strlen(keystring) == 7) && keystring[0] == '<' && keystring[5] == '>')
350 switch (toupper(keystring[1])) {
351 case 'S':
352 search_key.mask = GDK_SHIFT_MASK;
353 if (strlen(keystring) == 5) {
354 keystring[3] = toupper(keystring[3]);
355 } else {
356 keystring[3] = tolower(keystring[3]);
357 keystring[5] = toupper(keystring[5]);
359 break;
360 case 'C':
361 search_key.mask = GDK_CONTROL_MASK;
362 break;
363 case 'M':
364 search_key.mask = get_modkey(keystring[2]);
365 break;
367 if (!search_key.mask)
368 return FALSE;
369 if (strlen(keystring) == 5) {
370 search_key.key = keystring[3];
371 } else if (strlen(keystring) == 7) {
372 search_key.modkey = keystring[4];
373 search_key.key = keystring[6];
374 } else {
375 if (search_key.mask == 'S' || search_key.mask == 'C') {
376 search_key.modkey = keystring[3];
377 search_key.key = keystring[5];
378 } else {
379 search_key.key = keystring[4];
384 /* process stuff like a<S-v> for a,Shift-v or a<C-v> for a,Ctrl-v (strlen == 6)
385 or stuff like a<M1-v> for a,Mod1-v (strlen == 7)
387 if (
388 (strlen(keystring) == 6 && keystring[1] == '<' && keystring[5] == '>') ||
389 (strlen(keystring) == 7 && keystring[1] == '<' && keystring[6] == '>')
391 switch (toupper(keystring[2])) {
392 case 'S':
393 search_key.mask = GDK_SHIFT_MASK;
394 keystring[4] = toupper(keystring[4]);
395 break;
396 case 'C':
397 search_key.mask = GDK_CONTROL_MASK;
398 break;
399 case 'M':
400 search_key.mask = get_modkey(keystring[3]);
401 break;
403 if (!search_key.mask)
404 return FALSE;
405 search_key.modkey= keystring[0];
406 if (strlen(keystring) == 6) {
407 search_key.key = keystring[4];
408 } else {
409 search_key.key = keystring[5];
412 return (changemapping(&search_key, maprecord, cmd));
415 gboolean
416 process_map_line(char *line) {
417 int listlen, i;
418 char *c, *cmd;
419 my_pair.line = line;
420 c = search_word(0);
422 if (!strlen(my_pair.what))
423 return FALSE;
424 while (isspace(*c) && *c)
425 c++;
427 if (*c == ':' || *c == '=')
428 c++;
429 my_pair.line = c;
430 c = search_word(1);
431 if (!strlen(my_pair.value))
432 return FALSE;
433 listlen = LENGTH(commands);
434 for (i = 0; i < listlen; i++) {
435 /* commands is fixed size */
436 if (commands[i].cmd == NULL)
437 break;
438 if (strlen(commands[i].cmd) == strlen(my_pair.value) && strncmp(commands[i].cmd, my_pair.value, strlen(my_pair.value)) == 0) {
439 /* map to an internal symbol */
440 return process_mapping(my_pair.what, i, NULL);
443 /* if this is reached, the mapping is not for one of the internal symbol - test for command line structure */
444 if (strlen(my_pair.value) > 1 && strncmp(my_pair.value, ":", 1) == 0) {
445 /* The string begins with a colon, like a command line, but it's not _just_ a colon,
446 * i.e. increasing the pointer by one will not go 'out of bounds'.
447 * We don't actually check that the command line after the = is valid.
448 * This is user responsibility, the worst case is the new mapping simply doing nothing.
449 * Since we will pass the command to the same function which also handles the config file lines,
450 * we have to strip the colon itself (a colon counts as a commented line there - like in vim).
451 * Last, but not least, the second argument being < 0 signifies to the function that this is a
452 * command line mapping, not a mapping to an existing internal symbol. */
453 cmd = (char *)malloc(sizeof(char) * strlen(my_pair.value));
454 strncpy(cmd, (my_pair.value + 1), strlen(my_pair.value) - 1);
455 cmd[strlen(cmd)] = '\0';
456 return process_mapping(my_pair.what, -1, cmd);
458 return FALSE;
461 gboolean
462 build_taglist(const Arg *arg, FILE *f) {
463 int k = 0, in_tag = 0;
464 int t = 0, marker = 0;
465 char foundtab[MAXTAGSIZE+1];
466 while (arg->s[k]) {
467 if (!isspace(arg->s[k]) && !in_tag) {
468 in_tag = 1;
469 marker = k;
471 if (isspace(arg->s[k]) && in_tag) {
472 /* found a tag */
473 t = 0;
474 while (marker < k && t < MAXTAGSIZE) foundtab[t++] = arg->s[marker++];
475 foundtab[t] = '\0';
476 fprintf(f, " [%s]", foundtab);
477 in_tag = 0;
479 k++;
481 if (in_tag) {
482 t = 0;
483 while (marker < strlen(arg->s) && t < MAXTAGSIZE) foundtab[t++] = arg->s[marker++];
484 foundtab[t] = '\0';
485 fprintf(f, " [%s]", foundtab );
487 return TRUE;
490 void
491 set_error(const char *error) {
492 /* it should never happen that set_error is called more than once,
493 * but to avoid any potential memory leaks, we ignore any subsequent
494 * error if the current one has not been shown */
495 if (error_msg == NULL) {
496 error_msg = g_strdup_printf("%s", error);
500 void
501 give_feedback(const char *feedback)
503 Arg a = { .i = Info };
505 a.s = g_strdup_printf("%s", feedback);
506 echo(&a);
507 g_free(a.s);
510 Listelement *
511 complete_list(const char *searchfor, const int mode, Listelement *elementlist)
513 FILE *f;
514 const char *filename;
515 Listelement *candidatelist = NULL, *candidatepointer = NULL;
516 char s[255] = "", readelement[MAXTAGSIZE + 1] = "";
517 int i, t, n = 0;
519 if (mode == 2) {
520 /* open in history file */
521 filename = g_strdup_printf(HISTORY_STORAGE_FILENAME);
522 } else {
523 /* open in bookmark file (for tags and bookmarks) */
524 filename = g_strdup_printf(BOOKMARKS_STORAGE_FILENAME);
526 f = fopen(filename, "r");
527 if (f == NULL) {
528 g_free((gpointer)filename);
529 return (elementlist);
532 while (fgets(s, 254, f)) {
533 if (mode == 1) {
534 /* just tags (could be more than one per line) */
535 i = 0;
536 while (s[i] && i < 254) {
537 while (s[i] != '[' && s[i])
538 i++;
539 if (s[i] != '[')
540 continue;
541 i++;
542 t = 0;
543 while (s[i] != ']' && s[i] && t < MAXTAGSIZE)
544 readelement[t++] = s[i++];
545 readelement[t] = '\0';
546 candidatelist = add_list(readelement, candidatelist);
547 i++;
549 } else {
550 /* complete string (bookmarks & history) */
551 candidatelist = add_list(s, candidatelist);
553 candidatepointer = candidatelist;
554 while (candidatepointer != NULL) {
555 if (!complete_case_sensitive) {
556 g_strdown(candidatepointer->element);
558 if (!strlen(searchfor) || strstr(candidatepointer->element, searchfor) != NULL) {
559 /* only use string up to the first space */
560 memset(readelement, 0, MAXTAGSIZE + 1);
561 if (strchr(candidatepointer->element, ' ') != NULL) {
562 i = strcspn(candidatepointer->element, " ");
563 strncpy(readelement, candidatepointer->element, i);
564 } else {
565 strncpy(readelement, candidatepointer->element, MAXTAGSIZE);
567 /* in the case of URLs without title, remove the line break */
568 if (readelement[strlen(readelement) - 1] == '\n') {
569 readelement[strlen(readelement) - 1] = '\0';
571 elementlist = add_list(readelement, elementlist);
572 n = count_list(elementlist);
574 if (n >= MAX_LIST_SIZE)
575 break;
576 candidatepointer = candidatepointer->next;
578 free_list(candidatelist);
579 candidatelist = NULL;
580 if (n >= MAX_LIST_SIZE)
581 break;
583 g_free((gpointer)filename);
584 return (elementlist);
587 Listelement *
588 add_list(const char *element, Listelement *elementlist)
590 int n, i = 0;
591 Listelement *newelement, *elementpointer, *lastelement;
593 if (elementlist == NULL) { /* first element */
594 newelement = malloc(sizeof(Listelement));
595 if (newelement == NULL)
596 return (elementlist);
597 strncpy(newelement->element, element, 254);
598 newelement->next = NULL;
599 return newelement;
601 elementpointer = elementlist;
602 n = strlen(element);
604 /* check if element is already in list */
605 while (elementpointer != NULL) {
606 if (strlen(elementpointer->element) == n &&
607 strncmp(elementpointer->element, element, n) == 0)
608 return (elementlist);
609 lastelement = elementpointer;
610 elementpointer = elementpointer->next;
611 i++;
613 /* add to list */
614 newelement = malloc(sizeof(Listelement));
615 if (newelement == NULL)
616 return (elementlist);
617 lastelement->next = newelement;
618 strncpy(newelement->element, element, 254);
619 newelement->next = NULL;
620 return elementlist;
623 void
624 free_list(Listelement *elementlist)
626 Listelement *elementpointer;
628 while (elementlist != NULL) {
629 elementpointer = elementlist->next;
630 free(elementlist);
631 elementlist = elementpointer;
636 count_list(Listelement *elementlist)
638 Listelement *elementpointer = elementlist;
639 int n = 0;
641 while (elementpointer != NULL) {
642 n++;
643 elementpointer = elementpointer->next;
646 return n;
649 /* split the string at the first occurence of whitespace and return the
650 * position of the second half.
651 * Unlike strtok, the substrings can be empty and the second string is
652 * stripped of trailing and leading whitespace.
653 * Return -1 if `string' contains no whitespace */
654 static int split_string_at_whitespace(char *string)
656 int index = strcspn(string, "\n\t ");
657 if (string[index] != '\0') {
658 string[index++] = 0;
659 g_strstrip(string+index);
660 return index;
662 return -1;
665 /* return TRUE, if the string contains exactly one unescaped %s and no other
666 * printf directives */
667 static gboolean sanity_check_search_url(const char *string)
669 int was_percent_char = 0, percent_s_count = 0;
671 for (; *string; string++) {
672 switch (*string) {
673 case '%':
674 was_percent_char = !was_percent_char;
675 break;
676 case 's':
677 if (was_percent_char)
678 percent_s_count++;
679 was_percent_char = 0;
680 break;
681 default:
682 if (was_percent_char)
683 return FALSE;
684 was_percent_char = 0;
685 break;
689 return !was_percent_char && percent_s_count == 1;
692 enum ConfigFileError
693 read_searchengines(const char *filename)
695 FILE *file;
696 char buffer[BUFFERSIZE], c;
697 int linum = 0, index;
698 gboolean found_malformed_lines = FALSE;
699 Searchengine *new;
701 if (access(filename, F_OK) != 0)
702 return FILE_NOT_FOUND;
704 file = fopen(filename, "r");
705 if (file == NULL)
706 return READING_FAILED;
708 while (fgets(buffer, BUFFERSIZE, file)) {
709 linum++;
711 /* skip empty lines */
712 if (!strcmp(buffer, "\n")) continue;
714 /* skip too long lines */
715 if (buffer[strlen(buffer)-1] != '\n') {
716 c = getc(file);
717 if (c != EOF) { /* this is not the last line */
718 while ((c=getc(file)) != EOF && c != '\n');
719 fprintf(stderr, "searchengines: syntax error on line %d\n", linum);
720 found_malformed_lines = TRUE;
721 continue;
725 /* split line at whitespace */
726 index = split_string_at_whitespace(buffer);
728 if (index < 0 || buffer[0] == '\0' || buffer[index] == '\0'
729 || !sanity_check_search_url(buffer+index)) {
730 fprintf(stderr, "searchengines: syntax error on line %d\n", linum);
731 found_malformed_lines = TRUE;
732 continue;
735 new = malloc(sizeof(Searchengine));
736 if (new == NULL) {
737 fprintf(stderr, "Memory exhausted while loading search engines.\n");
738 exit(EXIT_FAILURE);
741 new->handle = g_strdup(buffer);
742 new->uri = g_strdup(buffer+index);
744 dynamic_searchengines = g_list_prepend(dynamic_searchengines, new);
747 if (ferror(file)) {
748 fclose(file);
749 return READING_FAILED;
752 fclose(file);
754 return found_malformed_lines ? SYNTAX_ERROR : SUCCESS;
757 void make_searchengines_list(Searchengine *searchengines, int length)
759 int i;
760 for (i = 0; i < length; i++, searchengines++) {
761 dynamic_searchengines = g_list_prepend(dynamic_searchengines, searchengines);
765 /* find a searchengine with a given handle and return its URI or NULL if
766 * nothing is found.
767 * The returned string is internal and must not be freed or modified. */
768 char *find_uri_for_searchengine(const char *handle)
770 GList *l;
772 if (dynamic_searchengines != NULL) {
773 for (l = dynamic_searchengines; l; l = g_list_next(l)) {
774 Searchengine *s = (Searchengine*)l->data;
775 if (!strcmp(s->handle, handle)) {
776 return s->uri;
781 return NULL;