2 (c) 2009 by Leon Winter
3 (c) 2009-2012 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
12 #include "vimprobable.h"
14 #include "utilities.h"
17 extern Command commands
[COMMANDSIZE
];
19 extern gboolean complete_case_sensitive
;
20 static GList
*dynamic_searchengines
= NULL
, *dynamic_uri_handlers
= NULL
;
22 void add_modkeys(char key
);
24 void save_command_history(char *line
)
26 State
*s
= &client
.state
;
29 while (isspace(*c
) && *c
)
34 if (COMMANDHISTSIZE
<= g_list_length(s
->commandhistory
)) {
35 /* if list is too long - remove items from beginning */
36 s
->commandhistory
= g_list_delete_link(s
->commandhistory
, g_list_first(s
->commandhistory
));
38 s
->commandhistory
= g_list_append(s
->commandhistory
, g_strdup(c
));
42 process_save_qmark(const char *bm
, WebKitWebView
*webview
)
46 const char *uri
= webkit_web_view_get_uri(webview
);
52 if ( mark
< 1 || mark
> 9 ) {
53 echo_message(Error
, "Invalid quickmark, only 1-9");
56 if ( uri
== NULL
) return FALSE
;
57 for( i
=0; i
< 9; ++i
) strcpy( qmarks
[i
], "");
59 filename
= g_strdup_printf(QUICKMARK_FILE
);
61 /* get current quickmarks */
63 fp
= fopen(filename
, "r");
65 for( i
=0; i
< 10; ++i
) {
71 while (buf
[l
] && l
< 100 && buf
[l
] != '\n') {
81 strcpy( qmarks
[mark
-1], uri
);
82 fp
= fopen(filename
, "w");
83 g_free((gpointer
*)filename
);
84 if (fp
== NULL
) return FALSE
;
85 for( i
=0; i
< 10; ++i
)
86 fprintf(fp
, "%s\n", qmarks
[i
]);
88 echo_message(Error
, "Saved as quickmark %d: %s", mark
, uri
);
97 KeyList
*ptr
, *current
;
102 while ( keys
[i
].key
!= 0 )
104 current
= malloc(sizeof(KeyList
));
105 if (current
== NULL
) {
106 printf("Not enough memory\n");
109 current
->Element
= keys
[i
];
110 current
->next
= NULL
;
111 if (client
.config
.keylistroot
== NULL
)
112 client
.config
.keylistroot
= current
;
121 parse_colour(char *color
) {
125 colorlen
= (int)strlen(color
);
130 /* help the user a bit by making string like
131 #a10 and strings like ffffff full 6digit
132 strings with # in front :)
135 if (color
[0] == '#') {
138 strncpy(goodcolor
, color
, 7);
141 goodcolor
[1] = color
[1];
142 goodcolor
[2] = color
[1];
143 goodcolor
[3] = color
[2];
144 goodcolor
[4] = color
[2];
145 goodcolor
[5] = color
[3];
146 goodcolor
[6] = color
[3];
149 goodcolor
[1] = color
[1];
150 goodcolor
[2] = color
[1];
151 goodcolor
[3] = color
[1];
152 goodcolor
[4] = color
[1];
153 goodcolor
[5] = color
[1];
154 goodcolor
[6] = color
[1];
160 strncpy(&goodcolor
[1], color
, 6);
163 goodcolor
[1] = color
[0];
164 goodcolor
[2] = color
[0];
165 goodcolor
[3] = color
[1];
166 goodcolor
[4] = color
[1];
167 goodcolor
[5] = color
[2];
168 goodcolor
[6] = color
[2];
171 goodcolor
[1] = color
[0];
172 goodcolor
[2] = color
[0];
173 goodcolor
[3] = color
[0];
174 goodcolor
[4] = color
[0];
175 goodcolor
[5] = color
[0];
176 goodcolor
[6] = color
[0];
181 if (strlen (goodcolor
) != 7) {
184 strncpy(color
, goodcolor
, 8);
190 process_line_arg(const Arg
*arg
) {
191 return process_line(arg
->s
);
195 changemapping(Key
*search_key
, int maprecord
, char *cmd
) {
196 KeyList
*current
, *newkey
;
197 Arg a
= { .s
= cmd
};
200 if (maprecord
< 0 && cmd
== NULL
) {
202 * - maprecord >= 0 && cmd == NULL: mapping to internal symbol
203 * - maprecord < 0 && cmd != NULL: mapping to command line
204 * - maprecord >= 0 && cmd != NULL: cmd will be ignored, treated as mapping to internal symbol
205 * - anything else (gets in here): an error, hence we return FALSE */
209 current
= client
.config
.keylistroot
;
212 while (current
->next
!= NULL
) {
214 current
->Element
.mask
== search_key
->mask
&&
215 current
->Element
.modkey
== search_key
->modkey
&&
216 current
->Element
.key
== search_key
->key
218 if (maprecord
>= 0) {
219 /* mapping to an internal signal */
220 current
->Element
.func
= commands
[maprecord
].func
;
221 current
->Element
.arg
= commands
[maprecord
].arg
;
223 /* mapping to a command line */
224 current
->Element
.func
= process_line_arg
;
225 current
->Element
.arg
= a
;
229 current
= current
->next
;
231 newkey
= malloc(sizeof(KeyList
));
232 if (newkey
== NULL
) {
233 printf("Not enough memory\n");
236 newkey
->Element
.mask
= search_key
->mask
;
237 newkey
->Element
.modkey
= search_key
->modkey
;
238 newkey
->Element
.key
= search_key
->key
;
239 if (maprecord
>= 0) {
240 /* mapping to an internal signal */
241 newkey
->Element
.func
= commands
[maprecord
].func
;
242 newkey
->Element
.arg
= commands
[maprecord
].arg
;
244 /* mapping to a command line */
245 newkey
->Element
.func
= process_line_arg
;
246 newkey
->Element
.arg
= a
;
248 add_modkeys(newkey
->Element
.modkey
);
252 if (client
.config
.keylistroot
== NULL
)
253 client
.config
.keylistroot
= newkey
;
256 current
->next
= newkey
;
261 void add_modkeys(char key
)
264 len
= strlen(client
.config
.modkeys
);
266 if (client
.config
.modkeys
[k
] == key
) return;
269 client
.config
.modkeys
= realloc(client
.config
.modkeys
, len
+ 2);
270 client
.config
.modkeys
[len
] = key
;
271 client
.config
.modkeys
[len
+1] = '\0';
275 mappings(const Arg
*arg
) {
279 set_error("Missing argument.");
282 strncpy(line
, arg
->s
, 254);
283 if (process_map_line(line
))
286 set_error("Invalid mapping.");
292 get_modkey(char key
) {
295 return GDK_MOD1_MASK
;
297 return GDK_MOD2_MASK
;
299 return GDK_MOD3_MASK
;
301 return GDK_MOD4_MASK
;
303 return GDK_MOD5_MASK
;
310 process_colon(char *alias
, char *cmd
) {
313 /* mapping one colon alias to another colon command */
314 a
= malloc(sizeof(Alias
));
316 fprintf(stderr
, "Memory exhausted while saving new command.\n");
321 client
.config
.colon_aliases
= g_list_prepend(client
.config
.colon_aliases
, a
);
326 process_mapping(char *keystring
, int maprecord
, char *cmd
) {
330 search_key
.modkey
= 0;
333 if (strlen(keystring
) == 1) {
334 search_key
.key
= keystring
[0];
337 if (strlen(keystring
) == 2) {
338 search_key
.modkey
= keystring
[0];
339 search_key
.key
= keystring
[1];
342 /* process stuff like <S-v> for Shift-v or <C-v> for Ctrl-v (strlen == 5),
343 stuff like <S-v>a for Shift-v,a or <C-v>a for Ctrl-v,a (strlen == 6 && keystring[4] == '>')
344 stuff like <M1-v> for Mod1-v (strlen == 6 && keystring[5] == '>')
345 or stuff like <M1-v>a for Mod1-v,a (strlen = 7)
348 ((strlen(keystring
) == 5 || strlen(keystring
) == 6) && keystring
[0] == '<' && keystring
[4] == '>') ||
349 ((strlen(keystring
) == 6 || strlen(keystring
) == 7) && keystring
[0] == '<' && keystring
[5] == '>')
351 switch (toupper(keystring
[1])) {
353 search_key
.mask
= GDK_SHIFT_MASK
;
354 if (strlen(keystring
) == 5) {
355 keystring
[3] = toupper(keystring
[3]);
357 keystring
[3] = tolower(keystring
[3]);
358 keystring
[5] = toupper(keystring
[5]);
362 search_key
.mask
= GDK_CONTROL_MASK
;
365 search_key
.mask
= get_modkey(keystring
[2]);
368 if (!search_key
.mask
)
370 if (strlen(keystring
) == 5) {
371 search_key
.key
= keystring
[3];
372 } else if (strlen(keystring
) == 7) {
373 search_key
.modkey
= keystring
[4];
374 search_key
.key
= keystring
[6];
376 if (search_key
.mask
== 'S' || search_key
.mask
== 'C') {
377 search_key
.modkey
= keystring
[3];
378 search_key
.key
= keystring
[5];
380 search_key
.key
= keystring
[4];
385 /* process stuff like a<S-v> for a,Shift-v or a<C-v> for a,Ctrl-v (strlen == 6)
386 or stuff like a<M1-v> for a,Mod1-v (strlen == 7)
389 (strlen(keystring
) == 6 && keystring
[1] == '<' && keystring
[5] == '>') ||
390 (strlen(keystring
) == 7 && keystring
[1] == '<' && keystring
[6] == '>')
392 switch (toupper(keystring
[2])) {
394 search_key
.mask
= GDK_SHIFT_MASK
;
395 keystring
[4] = toupper(keystring
[4]);
398 search_key
.mask
= GDK_CONTROL_MASK
;
401 search_key
.mask
= get_modkey(keystring
[3]);
404 if (!search_key
.mask
)
406 search_key
.modkey
= keystring
[0];
407 if (strlen(keystring
) == 6) {
408 search_key
.key
= keystring
[4];
410 search_key
.key
= keystring
[5];
413 return (changemapping(&search_key
, maprecord
, cmd
));
417 process_map_line(char *line
) {
423 if (!strlen(my_pair
.what
))
425 while (isspace(*c
) && *c
)
428 if (*c
== ':' || *c
== '=')
432 if (!strlen(my_pair
.value
))
434 listlen
= LENGTH(commands
);
435 for (i
= 0; i
< listlen
; i
++) {
436 /* commands is fixed size */
437 if (commands
[i
].cmd
== NULL
)
439 if (strlen(commands
[i
].cmd
) == strlen(my_pair
.value
) && strncmp(commands
[i
].cmd
, my_pair
.value
, strlen(my_pair
.value
)) == 0) {
440 /* map to an internal symbol */
441 return process_mapping(my_pair
.what
, i
, NULL
);
444 /* if this is reached, the mapping is not for one of the internal symbol - test for command line structure */
445 if (strlen(my_pair
.value
) > 1 && strncmp(my_pair
.value
, ":", 1) == 0) {
446 /* The string begins with a colon, like a command line, but it's not _just_ a colon,
447 * i.e. increasing the pointer by one will not go 'out of bounds'.
448 * We don't actually check that the command line after the = is valid.
449 * This is user responsibility, the worst case is the new mapping simply doing nothing.
450 * Since we will pass the command to the same function which also handles the config file lines,
451 * we have to strip the colon itself (a colon counts as a commented line there - like in vim).
452 * Last, but not least, the second argument being < 0 signifies to the function that this is a
453 * command line mapping, not a mapping to an existing internal symbol. */
454 cmd
= (char *)malloc(sizeof(char) * strlen(my_pair
.value
));
455 memset(cmd
, 0, strlen(my_pair
.value
));
456 strncpy(cmd
, (my_pair
.value
+ 1), strlen(my_pair
.value
) - 1);
457 if (strncmp(my_pair
.what
, ":", 1) == 0) {
458 /* map new colon command */
459 return process_colon(my_pair
.what
, cmd
);
461 /* map key binding */
462 return process_mapping(my_pair
.what
, -1, cmd
);
469 build_taglist(const Arg
*arg
, FILE *f
) {
470 int k
= 0, in_tag
= 0;
471 int t
= 0, marker
= 0;
472 char foundtab
[MAXTAGSIZE
+1];
474 if (!isspace(arg
->s
[k
]) && !in_tag
) {
478 if (isspace(arg
->s
[k
]) && in_tag
) {
481 while (marker
< k
&& t
< MAXTAGSIZE
) foundtab
[t
++] = arg
->s
[marker
++];
483 fprintf(f
, " [%s]", foundtab
);
490 while (marker
< strlen(arg
->s
) && t
< MAXTAGSIZE
) foundtab
[t
++] = arg
->s
[marker
++];
492 fprintf(f
, " [%s]", foundtab
);
498 set_error(const char *error
) {
499 /* it should never happen that set_error is called more than once,
500 * but to avoid any potential memory leaks, we ignore any subsequent
501 * error if the current one has not been shown */
502 if (client
.state
.error_msg
== NULL
) {
503 client
.state
.error_msg
= g_strdup_printf("%s", error
);
508 echo_message(const MessageType type
, const char *format
, ...)
513 va_start(ap
, format
);
515 a
.s
= g_strdup_vprintf(format
, ap
);
522 complete_list(const char *searchfor
, const int mode
, Listelement
*elementlist
)
525 const char *filename
;
526 Listelement
*candidatelist
= NULL
, *candidatepointer
= NULL
;
527 char s
[255] = "", readelement
[MAXTAGSIZE
+ 1] = "";
531 /* open in history file */
532 filename
= g_strdup_printf(HISTORY_STORAGE_FILENAME
);
534 /* open in bookmark file (for tags and bookmarks) */
535 filename
= g_strdup_printf(BOOKMARKS_STORAGE_FILENAME
);
537 f
= fopen(filename
, "r");
539 g_free((gpointer
*)filename
);
540 return (elementlist
);
543 while (fgets(s
, 254, f
)) {
545 /* just tags (could be more than one per line) */
547 while (s
[i
] && i
< 254) {
548 while (s
[i
] != '[' && s
[i
])
554 while (s
[i
] != ']' && s
[i
] && t
< MAXTAGSIZE
)
555 readelement
[t
++] = s
[i
++];
556 readelement
[t
] = '\0';
557 candidatelist
= add_list(readelement
, candidatelist
);
561 /* complete string (bookmarks & history) */
562 candidatelist
= add_list(s
, candidatelist
);
564 candidatepointer
= candidatelist
;
565 while (candidatepointer
!= NULL
) {
566 strncpy(s
, candidatepointer
->element
, sizeof(s
));
567 if (!complete_case_sensitive
) {
570 if (!strlen(searchfor
) || strstr(s
, searchfor
) != NULL
) {
571 /* only use string up to the first space */
572 memset(readelement
, 0, MAXTAGSIZE
+ 1);
573 if (strchr(candidatepointer
->element
, ' ') != NULL
) {
574 i
= strcspn(candidatepointer
->element
, " ");
577 strncpy(readelement
, candidatepointer
->element
, i
);
579 strncpy(readelement
, candidatepointer
->element
, MAXTAGSIZE
);
581 /* in the case of URLs without title, remove the line break */
582 if (readelement
[strlen(readelement
) - 1] == '\n') {
583 readelement
[strlen(readelement
) - 1] = '\0';
585 elementlist
= add_list(readelement
, elementlist
);
586 n
= count_list(elementlist
);
588 if (n
>= MAX_LIST_SIZE
)
590 candidatepointer
= candidatepointer
->next
;
592 free_list(candidatelist
);
593 candidatelist
= NULL
;
594 if (n
>= MAX_LIST_SIZE
)
597 g_free((gpointer
)filename
);
598 return (elementlist
);
602 add_list(const char *element
, Listelement
*elementlist
)
605 Listelement
*newelement
, *elementpointer
, *lastelement
;
607 if (elementlist
== NULL
) { /* first element */
608 newelement
= malloc(sizeof(Listelement
));
609 if (newelement
== NULL
)
610 return (elementlist
);
611 strncpy(newelement
->element
, element
, 254);
612 newelement
->next
= NULL
;
615 elementpointer
= elementlist
;
618 /* check if element is already in list */
619 while (elementpointer
!= NULL
) {
620 if (strlen(elementpointer
->element
) == n
&&
621 strncmp(elementpointer
->element
, element
, n
) == 0)
622 return (elementlist
);
623 lastelement
= elementpointer
;
624 elementpointer
= elementpointer
->next
;
628 newelement
= malloc(sizeof(Listelement
));
629 if (newelement
== NULL
)
630 return (elementlist
);
631 lastelement
->next
= newelement
;
632 strncpy(newelement
->element
, element
, 254);
633 newelement
->next
= NULL
;
638 free_list(Listelement
*elementlist
)
640 Listelement
*elementpointer
;
642 while (elementlist
!= NULL
) {
643 elementpointer
= elementlist
->next
;
645 elementlist
= elementpointer
;
650 count_list(Listelement
*elementlist
)
652 Listelement
*elementpointer
= elementlist
;
655 while (elementpointer
!= NULL
) {
657 elementpointer
= elementpointer
->next
;
663 /* split the string at the first occurence of whitespace and return the
664 * position of the second half.
665 * Unlike strtok, the substrings can be empty and the second string is
666 * stripped of trailing and leading whitespace.
667 * Return -1 if `string' contains no whitespace */
668 static int split_string_at_whitespace(char *string
)
670 int index
= strcspn(string
, "\n\t ");
671 if (string
[index
] != '\0') {
673 g_strstrip(string
+index
);
679 /* return TRUE, if the string contains exactly one unescaped %s and no other
680 * printf directives */
681 static gboolean
sanity_check_search_url(const char *string
)
683 int was_percent_char
= 0, percent_s_count
= 0;
685 for (; *string
; string
++) {
688 was_percent_char
= !was_percent_char
;
691 if (was_percent_char
)
693 was_percent_char
= 0;
696 if (was_percent_char
)
698 was_percent_char
= 0;
703 return !was_percent_char
&& percent_s_count
== 1;
706 void make_searchengines_list(Searchengine
*searchengines
, int length
)
709 for (i
= 0; i
< length
; i
++, searchengines
++) {
710 dynamic_searchengines
= g_list_prepend(dynamic_searchengines
, searchengines
);
714 /* find a searchengine with a given handle and return its URI or NULL if
716 * The returned string is internal and must not be freed or modified. */
717 char *find_uri_for_searchengine(const char *handle
)
721 if (dynamic_searchengines
!= NULL
) {
722 for (l
= dynamic_searchengines
; l
; l
= g_list_next(l
)) {
723 Searchengine
*s
= (Searchengine
*)l
->data
;
724 if (!strcmp(s
->handle
, handle
)) {
734 read_rcfile(const char *config
)
736 int t
, linum
= 0, index
;
737 char s
[255], *buffer
;
739 gboolean found_malformed_lines
= FALSE
;
741 URIHandler
*newhandler
;
743 if (access(config
, F_OK
) != 0)
744 return FILE_NOT_FOUND
;
746 fpin
= fopen(config
, "r");
748 return READING_FAILED
;
750 while (fgets(s
, 254, fpin
)) {
753 * ignore lines that begin with #, / and such
759 if (strncmp(s
, "searchengine", 12) == 0) {
761 while (buffer
[0] == ' ')
763 /* split line at whitespace */
764 index
= split_string_at_whitespace(buffer
);
765 if (index
< 0 || buffer
[0] == '\0' || buffer
[index
] == '\0'
766 || !sanity_check_search_url(buffer
+index
)) {
767 fprintf(stderr
, "searchengines: syntax error on line %d\n", linum
);
768 found_malformed_lines
= TRUE
;
771 new = malloc(sizeof(Searchengine
));
773 fprintf(stderr
, "Memory exhausted while loading search engines.\n");
776 new->handle
= g_strdup(buffer
);
777 new->uri
= g_strdup(buffer
+index
);
778 dynamic_searchengines
= g_list_prepend(dynamic_searchengines
, new);
779 } else if (strncmp(s
, "handler", 7) == 0) {
781 while (buffer
[0] == ' ')
783 /* split line at whitespace */
784 index
= split_string_at_whitespace(buffer
);
785 if (index
< 0 || buffer
[0] == '\0' || buffer
[index
] == '\0'
786 || !sanity_check_search_url(buffer
+index
)) { /* this sanity check is also valid for handler definitions */
787 fprintf(stderr
, "URI handlers: syntax error on line %d\n", linum
);
788 found_malformed_lines
= TRUE
;
791 newhandler
= malloc(sizeof(URIHandler
));
792 if (newhandler
== NULL
) {
793 fprintf(stderr
, "Memory exhausted while loading uri handlers.\n");
796 newhandler
->handle
= g_strdup(buffer
);
797 newhandler
->handler
= g_strdup(buffer
+index
);
798 dynamic_uri_handlers
= g_list_prepend(dynamic_uri_handlers
, newhandler
);
800 if (!process_line(s
))
801 found_malformed_lines
= TRUE
;
805 return found_malformed_lines
? SYNTAX_ERROR
: SUCCESS
;
808 void make_uri_handlers_list(URIHandler
*uri_handlers
, int length
)
811 for (i
= 0; i
< length
; i
++, uri_handlers
++) {
812 dynamic_uri_handlers
= g_list_prepend(dynamic_uri_handlers
, uri_handlers
);
817 /* spawn a child process handling a protocol encoded in URI.
819 On success, pid will contain the pid of the spawned child.
820 If you pass NULL as child_pid, glib will reap the child. */
822 open_handler_pid(char *uri
, GPid
*child_pid
) {
824 char *p
= NULL
, *arg
, arg_temp
[MAX_SETTING_SIZE
], *temp
, temp2
[MAX_SETTING_SIZE
] = "", *temp3
;
827 GSpawnFlags flags
= G_SPAWN_SEARCH_PATH
;
830 flags
|= G_SPAWN_DO_NOT_REAP_CHILD
;
832 p
= strchr(uri
, ':');
834 if (dynamic_uri_handlers
!= NULL
) {
835 for (l
= dynamic_uri_handlers
; l
; l
= g_list_next(l
)) {
836 URIHandler
*s
= (URIHandler
*)l
->data
;
837 if (strlen(uri
) >= strlen(s
->handle
) && strncmp(s
->handle
, uri
, strlen(s
->handle
)) == 0) {
838 if (strlen(s
->handler
) > 0) {
839 arg
= (uri
+ strlen(s
->handle
));
840 strncpy(temp2
, s
->handler
, MAX_SETTING_SIZE
);
841 temp
= strtok(temp2
, " ");
843 while (temp
!= NULL
) {
844 if (strstr(temp
, "%s")) {
846 memset(arg_temp
, 0, MAX_SETTING_SIZE
);
847 while (strncmp(temp3
, "%s", 2) != 0) {
848 strncat(arg_temp
, temp3
, 1);
851 strcat(arg_temp
, arg
);
854 strcat(arg_temp
, temp3
);
859 temp
= strtok(NULL
, " ");
863 g_spawn_async(NULL
, argv
, NULL
, flags
,
864 NULL
, NULL
, child_pid
, NULL
);
876 open_handler(char *uri
) {
877 return open_handler_pid(uri
, NULL
);