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
11 #include "vimprobable.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
;
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
)
30 gboolean returnval
= TRUE
;
32 if ((fpin
= fopen(config
, "r")) == NULL
)
34 while (fgets(s
, 254, fpin
)) {
36 * ignore lines that begin with #, / and such
49 void save_command_history(char *line
)
54 while (isspace(*c
) && *c
)
58 strncpy(commandhistory
[lastcommand
], ":", 1);
59 strncat(commandhistory
[lastcommand
], c
, 254);
61 if (maxcommands
< COMMANDHISTSIZE
- 1)
63 if (lastcommand
== COMMANDHISTSIZE
)
65 commandpointer
= lastcommand
;
69 process_save_qmark(const char *bm
, WebKitWebView
*webview
)
73 const char *uri
= webkit_web_view_get_uri(webview
);
80 if ( mark
< 1 || mark
> 9 )
83 a
.s
= g_strdup_printf("Invalid quickmark, only 1-9");
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");
97 for( i
=0; i
< 10; ++i
) {
103 while (buf
[l
] && l
< 100 && buf
[l
] != '\n') {
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
]);
120 a
.s
= g_strdup_printf("Saved as quickmark %d: %s", mark
, uri
);
131 KeyList
*ptr
, *current
;
136 while ( keys
[i
].key
!= 0 )
138 current
= malloc(sizeof(KeyList
));
139 if (current
== NULL
) {
140 printf("Not enough memory\n");
143 current
->Element
= keys
[i
];
144 current
->next
= NULL
;
145 if (keylistroot
== NULL
) keylistroot
= current
;
146 if (ptr
!= NULL
) ptr
->next
= current
;
153 parse_colour(char *color
) {
157 colorlen
= (int)strlen(color
);
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] == '#') {
170 strncpy(goodcolor
, color
, 7);
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];
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];
192 strncpy(&goodcolor
[1], color
, 6);
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];
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];
213 if (strlen (goodcolor
) != 7) {
216 strncpy(color
, goodcolor
, 8);
222 process_line_arg(const Arg
*arg
) {
223 return process_line(arg
->s
);
227 changemapping(Key
*search_key
, int maprecord
, char *cmd
) {
228 KeyList
*current
, *newkey
;
229 Arg a
= { .s
= cmd
};
232 if (maprecord
< 0 && cmd
== NULL
) {
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 */
241 current
= keylistroot
;
244 while (current
->next
!= NULL
) {
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
;
255 /* mapping to a command line */
256 current
->Element
.func
= process_line_arg
;
257 current
->Element
.arg
= a
;
261 current
= current
->next
;
263 newkey
= malloc(sizeof(KeyList
));
264 if (newkey
== NULL
) {
265 printf("Not enough memory\n");
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
;
276 /* mapping to a command line */
277 newkey
->Element
.func
= process_line_arg
;
278 newkey
->Element
.arg
= a
;
282 if (keylistroot
== NULL
) keylistroot
= newkey
;
284 if (current
!= NULL
) current
->next
= newkey
;
290 mappings(const Arg
*arg
) {
294 set_error("Missing argument.");
297 strncpy(line
, arg
->s
, 254);
298 if (process_map_line(line
))
301 set_error("Invalid mapping.");
307 get_modkey(char key
) {
310 return GDK_MOD1_MASK
;
312 return GDK_MOD2_MASK
;
314 return GDK_MOD3_MASK
;
316 return GDK_MOD4_MASK
;
318 return GDK_MOD5_MASK
;
325 process_mapping(char *keystring
, int maprecord
, char *cmd
) {
329 search_key
.modkey
= 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)
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])) {
352 search_key
.mask
= GDK_SHIFT_MASK
;
353 if (strlen(keystring
) == 5) {
354 keystring
[3] = toupper(keystring
[3]);
356 keystring
[3] = tolower(keystring
[3]);
357 keystring
[5] = toupper(keystring
[5]);
361 search_key
.mask
= GDK_CONTROL_MASK
;
364 search_key
.mask
= get_modkey(keystring
[2]);
367 if (!search_key
.mask
)
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];
375 if (search_key
.mask
== 'S' || search_key
.mask
== 'C') {
376 search_key
.modkey
= keystring
[3];
377 search_key
.key
= keystring
[5];
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)
388 (strlen(keystring
) == 6 && keystring
[1] == '<' && keystring
[5] == '>') ||
389 (strlen(keystring
) == 7 && keystring
[1] == '<' && keystring
[6] == '>')
391 switch (toupper(keystring
[2])) {
393 search_key
.mask
= GDK_SHIFT_MASK
;
394 keystring
[4] = toupper(keystring
[4]);
397 search_key
.mask
= GDK_CONTROL_MASK
;
400 search_key
.mask
= get_modkey(keystring
[3]);
403 if (!search_key
.mask
)
405 search_key
.modkey
= keystring
[0];
406 if (strlen(keystring
) == 6) {
407 search_key
.key
= keystring
[4];
409 search_key
.key
= keystring
[5];
412 return (changemapping(&search_key
, maprecord
, cmd
));
416 process_map_line(char *line
) {
422 if (!strlen(my_pair
.what
))
424 while (isspace(*c
) && *c
)
427 if (*c
== ':' || *c
== '=')
431 if (!strlen(my_pair
.value
))
433 listlen
= LENGTH(commands
);
434 for (i
= 0; i
< listlen
; i
++) {
435 /* commands is fixed size */
436 if (commands
[i
].cmd
== NULL
)
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
);
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];
467 if (!isspace(arg
->s
[k
]) && !in_tag
) {
471 if (isspace(arg
->s
[k
]) && in_tag
) {
474 while (marker
< k
&& t
< MAXTAGSIZE
) foundtab
[t
++] = arg
->s
[marker
++];
476 fprintf(f
, " [%s]", foundtab
);
483 while (marker
< strlen(arg
->s
) && t
< MAXTAGSIZE
) foundtab
[t
++] = arg
->s
[marker
++];
485 fprintf(f
, " [%s]", foundtab
);
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
);
501 give_feedback(const char *feedback
)
503 Arg a
= { .i
= Info
};
505 a
.s
= g_strdup_printf("%s", feedback
);
511 complete_list(const char *searchfor
, const int mode
, Listelement
*elementlist
)
514 const char *filename
;
515 Listelement
*candidatelist
= NULL
, *candidatepointer
= NULL
;
516 char s
[255] = "", readelement
[MAXTAGSIZE
+ 1] = "";
520 /* open in history file */
521 filename
= g_strdup_printf(HISTORY_STORAGE_FILENAME
);
523 /* open in bookmark file (for tags and bookmarks) */
524 filename
= g_strdup_printf(BOOKMARKS_STORAGE_FILENAME
);
526 f
= fopen(filename
, "r");
528 g_free((gpointer
)filename
);
529 return (elementlist
);
532 while (fgets(s
, 254, f
)) {
534 /* just tags (could be more than one per line) */
536 while (s
[i
] && i
< 254) {
537 while (s
[i
] != '[' && s
[i
])
543 while (s
[i
] != ']' && s
[i
] && t
< MAXTAGSIZE
)
544 readelement
[t
++] = s
[i
++];
545 readelement
[t
] = '\0';
546 candidatelist
= add_list(readelement
, candidatelist
);
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
, " ");
565 strncpy(readelement
, candidatepointer
->element
, i
);
567 strncpy(readelement
, candidatepointer
->element
, MAXTAGSIZE
);
569 /* in the case of URLs without title, remove the line break */
570 if (readelement
[strlen(readelement
) - 1] == '\n') {
571 readelement
[strlen(readelement
) - 1] = '\0';
573 elementlist
= add_list(readelement
, elementlist
);
574 n
= count_list(elementlist
);
576 if (n
>= MAX_LIST_SIZE
)
578 candidatepointer
= candidatepointer
->next
;
580 free_list(candidatelist
);
581 candidatelist
= NULL
;
582 if (n
>= MAX_LIST_SIZE
)
585 g_free((gpointer
)filename
);
586 return (elementlist
);
590 add_list(const char *element
, Listelement
*elementlist
)
593 Listelement
*newelement
, *elementpointer
, *lastelement
;
595 if (elementlist
== NULL
) { /* first element */
596 newelement
= malloc(sizeof(Listelement
));
597 if (newelement
== NULL
)
598 return (elementlist
);
599 strncpy(newelement
->element
, element
, 254);
600 newelement
->next
= NULL
;
603 elementpointer
= elementlist
;
606 /* check if element is already in list */
607 while (elementpointer
!= NULL
) {
608 if (strlen(elementpointer
->element
) == n
&&
609 strncmp(elementpointer
->element
, element
, n
) == 0)
610 return (elementlist
);
611 lastelement
= elementpointer
;
612 elementpointer
= elementpointer
->next
;
616 newelement
= malloc(sizeof(Listelement
));
617 if (newelement
== NULL
)
618 return (elementlist
);
619 lastelement
->next
= newelement
;
620 strncpy(newelement
->element
, element
, 254);
621 newelement
->next
= NULL
;
626 free_list(Listelement
*elementlist
)
628 Listelement
*elementpointer
;
630 while (elementlist
!= NULL
) {
631 elementpointer
= elementlist
->next
;
633 elementlist
= elementpointer
;
638 count_list(Listelement
*elementlist
)
640 Listelement
*elementpointer
= elementlist
;
643 while (elementpointer
!= NULL
) {
645 elementpointer
= elementpointer
->next
;
651 /* split the string at the first occurence of whitespace and return the
652 * position of the second half.
653 * Unlike strtok, the substrings can be empty and the second string is
654 * stripped of trailing and leading whitespace.
655 * Return -1 if `string' contains no whitespace */
656 static int split_string_at_whitespace(char *string
)
658 int index
= strcspn(string
, "\n\t ");
659 if (string
[index
] != '\0') {
661 g_strstrip(string
+index
);
667 /* return TRUE, if the string contains exactly one unescaped %s and no other
668 * printf directives */
669 static gboolean
sanity_check_search_url(const char *string
)
671 int was_percent_char
= 0, percent_s_count
= 0;
673 for (; *string
; string
++) {
676 was_percent_char
= !was_percent_char
;
679 if (was_percent_char
)
681 was_percent_char
= 0;
684 if (was_percent_char
)
686 was_percent_char
= 0;
691 return !was_percent_char
&& percent_s_count
== 1;
695 read_searchengines(const char *filename
)
698 char buffer
[BUFFERSIZE
], c
;
699 int linum
= 0, index
;
700 gboolean found_malformed_lines
= FALSE
;
703 if (access(filename
, F_OK
) != 0)
704 return FILE_NOT_FOUND
;
706 file
= fopen(filename
, "r");
708 return READING_FAILED
;
710 while (fgets(buffer
, BUFFERSIZE
, file
)) {
713 /* skip empty lines */
714 if (!strcmp(buffer
, "\n")) continue;
716 /* skip too long lines */
717 if (buffer
[strlen(buffer
)-1] != '\n') {
719 if (c
!= EOF
) { /* this is not the last line */
720 while ((c
=getc(file
)) != EOF
&& c
!= '\n');
721 fprintf(stderr
, "searchengines: syntax error on line %d\n", linum
);
722 found_malformed_lines
= TRUE
;
727 /* split line at whitespace */
728 index
= split_string_at_whitespace(buffer
);
730 if (index
< 0 || buffer
[0] == '\0' || buffer
[index
] == '\0'
731 || !sanity_check_search_url(buffer
+index
)) {
732 fprintf(stderr
, "searchengines: syntax error on line %d\n", linum
);
733 found_malformed_lines
= TRUE
;
737 new = malloc(sizeof(Searchengine
));
739 fprintf(stderr
, "Memory exhausted while loading search engines.\n");
743 new->handle
= g_strdup(buffer
);
744 new->uri
= g_strdup(buffer
+index
);
746 dynamic_searchengines
= g_list_prepend(dynamic_searchengines
, new);
751 return READING_FAILED
;
756 return found_malformed_lines
? SYNTAX_ERROR
: SUCCESS
;
759 void make_searchengines_list(Searchengine
*searchengines
, int length
)
762 for (i
= 0; i
< length
; i
++, searchengines
++) {
763 dynamic_searchengines
= g_list_prepend(dynamic_searchengines
, searchengines
);
767 /* find a searchengine with a given handle and return its URI or NULL if
769 * The returned string is internal and must not be freed or modified. */
770 char *find_uri_for_searchengine(const char *handle
)
774 if (dynamic_searchengines
!= NULL
) {
775 for (l
= dynamic_searchengines
; l
; l
= g_list_next(l
)) {
776 Searchengine
*s
= (Searchengine
*)l
->data
;
777 if (!strcmp(s
->handle
, handle
)) {