/** * anjuta_token_first_word: * @list: a #AnjutaToken object being a list * * Get the first word of the list. A word is an item in the list which is not * a space or a separator. * * Return value: A #AnjutaToken representing the first word or %NULL. */ AnjutaToken * anjuta_token_first_word (AnjutaToken *list) { AnjutaToken *item; for (item = anjuta_token_first_item (list); item != NULL; item = anjuta_token_next_item (item)) { if (anjuta_token_list (item) != list) { item = NULL; break; } switch (anjuta_token_get_type (item)) { case ANJUTA_TOKEN_START: case ANJUTA_TOKEN_NEXT: continue; case ANJUTA_TOKEN_LAST: item = NULL; break; default: if (anjuta_token_is_empty (item) || (anjuta_token_get_flags(item) & ANJUTA_TOKEN_REMOVED)) continue; break; } break; } return item; }
void amp_group_node_update_variable (AmpGroupNode *group, AnjutaToken *variable) { AnjutaToken *arg; char *name = NULL; AnjutaToken *value = NULL; AmpVariable *var; arg = anjuta_token_first_item (variable); name = g_strstrip (anjuta_token_evaluate (arg)); value = anjuta_token_last_item (variable); var = (AmpVariable *)g_hash_table_lookup (group->variables, name); if (var != NULL) { var->value = value; } else { var = amp_variable_new (name, 0, value); g_hash_table_insert (group->variables, var->name, var); } if (name) g_free (name); }
AnjutaToken * anjuta_token_insert_word_after (AnjutaToken *list, AnjutaToken *sibling, AnjutaToken *item) { AnjutaToken *token; if (list == NULL) list = anjuta_token_list (sibling); for (token = anjuta_token_first_item (list); token != NULL;) { AnjutaToken *next; next = anjuta_token_next_item (token); switch (anjuta_token_get_type (token)) { case ANJUTA_TOKEN_LAST: anjuta_token_insert_before (token, anjuta_token_new_static (ANJUTA_TOKEN_NEXT | ANJUTA_TOKEN_ADDED, NULL)); anjuta_token_insert_before (token, item); return item; case ANJUTA_TOKEN_START: if ((sibling == NULL) || (token == sibling)) { if (next != NULL) anjuta_token_insert_after (token, anjuta_token_new_static (ANJUTA_TOKEN_NEXT | ANJUTA_TOKEN_ADDED, NULL)); anjuta_token_insert_after (token, item); return item; } break; case ANJUTA_TOKEN_NEXT: if (token == sibling) { token = anjuta_token_insert_after (token, item); anjuta_token_insert_after (token, anjuta_token_new_static (ANJUTA_TOKEN_NEXT | ANJUTA_TOKEN_ADDED, NULL)); return item; } break; default: if (token == sibling) { token = anjuta_token_insert_after (token, anjuta_token_new_static (ANJUTA_TOKEN_NEXT | ANJUTA_TOKEN_ADDED, NULL)); anjuta_token_insert_after (token, item); return item; } break; } if (next == NULL) { token = anjuta_token_insert_after (token, anjuta_token_new_static (ANJUTA_TOKEN_NEXT | ANJUTA_TOKEN_ADDED, NULL)); anjuta_token_insert_after (token, item); return item; } token = next; } anjuta_token_prepend_items (list, item); return item; }
void mkp_project_update_variable (MkpProject *project, AnjutaToken *variable) { AnjutaToken *arg; char *name = NULL; MakeTokenType assign = 0; AnjutaToken *value = NULL; //fprintf(stdout, "update variable"); //anjuta_token_dump (variable); arg = anjuta_token_first_item (variable); name = g_strstrip (anjuta_token_evaluate (arg)); arg = anjuta_token_next_item (arg); //g_message ("new variable %s", name); switch (anjuta_token_get_type (arg)) { case MK_TOKEN_EQUAL: case MK_TOKEN_IMMEDIATE_EQUAL: case MK_TOKEN_CONDITIONAL_EQUAL: case MK_TOKEN_APPEND: assign = anjuta_token_get_type (arg); break; default: break; } value = anjuta_token_next_item (arg); if (assign != 0) { MkpVariable *var; //g_message ("assign %d name %s value %s\n", assign, name, anjuta_token_evaluate (value)); var = (MkpVariable *)g_hash_table_lookup (project->variables, name); if (var != NULL) { var->assign = assign; var->value = value; } else { var = mkp_variable_new (name, assign, value); g_hash_table_insert (project->variables, var->name, var); } } //g_message ("update variable %s", name); if (name) g_free (name); }
void amp_module_node_add_token (AmpModuleNode *module, AnjutaToken *token) { gchar *name; module->module = token; name = anjuta_token_evaluate (anjuta_token_first_item (token)); if (name != NULL) { g_free (module->base.name); module->base.name = name; } }
AnjutaToken * anjuta_token_nth_word (AnjutaToken *list, guint n) { AnjutaToken *item; gboolean no_item = TRUE; for (item = anjuta_token_first_item (list); item != NULL; item = anjuta_token_next_item (item)) { if (anjuta_token_list (item) != list) { item = NULL; break; } switch (anjuta_token_get_type (item)) { case ANJUTA_TOKEN_START: break; case ANJUTA_TOKEN_NEXT: if (no_item) { if (n == 0) return NULL; n--; } no_item = TRUE; break; case ANJUTA_TOKEN_LAST: return NULL; default: if (n == 0) return item; n--; no_item = FALSE; break; } } return NULL; }
AnjutaToken * anjuta_token_replace_nth_word (AnjutaToken *list, guint n, AnjutaToken *item) { AnjutaToken *token; gboolean no_item = TRUE; token = anjuta_token_first_item (list); if (token == NULL) { token = anjuta_token_insert_after (token, anjuta_token_new_static (ANJUTA_TOKEN_LAST | ANJUTA_TOKEN_ADDED, NULL)); anjuta_token_merge (list, token); } for (n++;;) { AnjutaToken *next; switch (anjuta_token_get_type (token)) { case ANJUTA_TOKEN_LAST: if (no_item) { n--; if (n == 0) { token = anjuta_token_insert_before (token, item); return token; } } token = anjuta_token_insert_before (token, anjuta_token_new_static (ANJUTA_TOKEN_NEXT | ANJUTA_TOKEN_ADDED, NULL)); no_item = TRUE; break; case ANJUTA_TOKEN_NEXT: if (no_item) { n--; if (n == 0) { token = anjuta_token_insert_before (token, item); return token; } } no_item = TRUE; break; case ANJUTA_TOKEN_ITEM: n--; if (n == 0) { anjuta_token_set_flags (token, ANJUTA_TOKEN_REMOVED); token = anjuta_token_insert_before (token, item); return token; } no_item = FALSE; break; default: break; } next = anjuta_token_next_item (token); if (next == NULL) { token = anjuta_token_insert_after (token, anjuta_token_new_static (ANJUTA_TOKEN_LAST | ANJUTA_TOKEN_ADDED, NULL)); anjuta_token_merge (list, token); } else { token = next; } } }
void anjuta_token_style_update (AnjutaTokenStyle *style, AnjutaToken *list) { AnjutaToken *token; AnjutaToken *next_token; guint prev = 0; guint next = 0; guint line_width = 0; guint sep_count = 0; /* Initialize first line width */ for (token = list; token != NULL; token = anjuta_token_previous (token)) { gchar *value; gchar *eol = NULL; gsize len = 0; value = anjuta_token_evaluate (token); if (value != NULL) { eol = strrchr (value, '\n'); len = strlen (value); g_free (value); } if (eol != NULL) { line_width = value + len - eol; break; } line_width += len; } for (token = anjuta_token_first_item (list); token != NULL; token = next_token) { gchar *value = NULL; const gchar *eol; gsize len; gint type; next_token = anjuta_token_next_item (token); type = anjuta_token_get_type (token); next = next_token == NULL ? 0 : anjuta_token_get_type (next_token); value = anjuta_token_evaluate (token); if (value == NULL) continue; len = strlen (value); eol = strrchr (value, '\n'); if (eol != NULL) len -= (eol - value); g_free (value); line_width += len; switch (type) { case ANJUTA_TOKEN_START: case ANJUTA_TOKEN_LAST: case ANJUTA_TOKEN_NEXT: break; default: if (eol != NULL) { line_width = len; sep_count = 0; } continue; } value = anjuta_token_evaluate (token); anjuta_token_style_insert_separator_between (style, 0, type, value); if (type == ANJUTA_TOKEN_NEXT) { anjuta_token_style_insert_separator_between (style, next, prev, value); anjuta_token_style_insert_separator_between (style, next, ANJUTA_TOKEN_ANY, value); anjuta_token_style_insert_separator_between (style, ANJUTA_TOKEN_ANY, prev, value); } g_free (value); if (eol == NULL) { sep_count++; } else { if ((sep_count > 1) && (line_width > style->max_width)) { style->max_width = line_width; } sep_count = 0; line_width = len; } } }
void mkp_project_add_rule (MkpProject *project, AnjutaToken *group) { AnjutaToken *targ; AnjutaToken *dep; AnjutaToken *arg; gboolean double_colon = FALSE; //fprintf(stdout, "add rule\n"); //anjuta_token_dump (group); targ = anjuta_token_first_item (group); arg = anjuta_token_next_word (targ); if (anjuta_token_get_type (arg) == MK_TOKEN_DOUBLE_COLON) double_colon = TRUE; dep = anjuta_token_next_word (arg); for (arg = anjuta_token_first_word (targ); arg != NULL; arg = anjuta_token_next_word (arg)) { AnjutaToken *src = NULL; gchar *target = NULL; gboolean order = FALSE; gboolean no_token = TRUE; MkpRule *rule = NULL; switch (anjuta_token_get_type (arg)) { case MK_TOKEN__PHONY: for (src = anjuta_token_first_word (dep); src != NULL; src = anjuta_token_next_word (src)) { if (anjuta_token_get_type (src) != MK_TOKEN_ORDER) { target = anjuta_token_evaluate (src); rule = g_hash_table_lookup (project->rules, target); if (rule == NULL) { rule = mkp_rule_new (target, NULL); g_hash_table_insert (project->rules, rule->name, rule); } rule->phony = TRUE; //g_message (" with target %s", target); if (target != NULL) g_free (target); } } break; case MK_TOKEN__SUFFIXES: for (src = anjuta_token_first_word (dep); src != NULL; src = anjuta_token_next_word (src)) { if (anjuta_token_get_type (src) != MK_TOKEN_ORDER) { gchar *suffix; suffix = anjuta_token_evaluate (src); /* The pointer value must only be not NULL, it does not matter if it is * invalid */ g_hash_table_replace (project->suffix, suffix, suffix); //g_message (" with suffix %s", suffix); no_token = FALSE; } } if (no_token == TRUE) { /* Clear all suffix */ g_hash_table_remove_all (project->suffix); } break; case MK_TOKEN__DEFAULT: case MK_TOKEN__PRECIOUS: case MK_TOKEN__INTERMEDIATE: case MK_TOKEN__SECONDARY: case MK_TOKEN__SECONDEXPANSION: case MK_TOKEN__DELETE_ON_ERROR: case MK_TOKEN__IGNORE: case MK_TOKEN__LOW_RESOLUTION_TIME: case MK_TOKEN__SILENT: case MK_TOKEN__EXPORT_ALL_VARIABLES: case MK_TOKEN__NOTPARALLEL: /* Do nothing with these targets, just ignore them */ break; default: target = g_strstrip (anjuta_token_evaluate (arg)); if (*target == '\0') break; //g_message ("add rule =%s=", target); rule = g_hash_table_lookup (project->rules, target); if (rule == NULL) { rule = mkp_rule_new (target, group); g_hash_table_insert (project->rules, rule->name, rule); } else { rule->rule = group; } for (src = anjuta_token_first_word (dep); src != NULL; src = anjuta_token_next_word (src)) { gchar *src_name = anjuta_token_evaluate (src); if (src_name != NULL) { //g_message (" with source %s", src_name); if (anjuta_token_get_type (src) == MK_TOKEN_ORDER) { order = TRUE; } rule->prerequisite = g_list_prepend (rule->prerequisite, src_name); } } if (target != NULL) g_free (target); } } }
gboolean amp_target_node_create_token (AmpProject *project, AmpTargetNode *target, GError **error) { AnjutaToken *args; AnjutaToken *var; AnjutaToken *prev; AmpNodeInfo *info; gchar *targetname; const gchar *name; GList *last; AnjutaProjectNode *sibling; AmpGroupNode *parent; gboolean after; /* Get parent target */ parent = AMP_GROUP_NODE (anjuta_project_node_parent_type (ANJUTA_PROJECT_NODE (target), ANJUTA_PROJECT_GROUP)); info = (AmpNodeInfo *)amp_project_get_type_info (project, anjuta_project_node_get_full_type (ANJUTA_PROJECT_NODE (target))); name = anjuta_project_node_get_name (ANJUTA_PROJECT_NODE (target)); /* Find a sibling if possible */ after = TRUE; for (sibling = anjuta_project_node_prev_sibling (ANJUTA_PROJECT_NODE (target)); sibling != NULL; sibling = anjuta_project_node_prev_sibling (sibling)) { if (anjuta_project_node_get_node_type (sibling) == ANJUTA_PROJECT_TARGET) break; } if (sibling == NULL) { after = FALSE; for (sibling = anjuta_project_node_next_sibling (ANJUTA_PROJECT_NODE (target)); sibling != NULL; sibling = anjuta_project_node_next_sibling (sibling)) { if (anjuta_project_node_get_node_type (sibling) == ANJUTA_PROJECT_TARGET) break; } } if (sibling == NULL) after = TRUE; /* Add in Makefile.am */ targetname = g_strconcat (amp_target_node_get_install_directory (target) != NULL ? amp_target_node_get_install_directory (target) : info->install, "_", info->prefix, NULL); // Get token corresponding to sibling and check if the target are compatible args = NULL; var = NULL; prev = NULL; if (sibling != NULL) { last = amp_target_node_get_token (AMP_TARGET_NODE (sibling), ANJUTA_TOKEN_ARGUMENT); if (last != NULL) { AnjutaToken *token = (AnjutaToken *)last->data; /* Check that the sibling is of the same kind */ token = anjuta_token_list (token); if (token != NULL) { token = anjuta_token_list (token); var = token; if (token != NULL) { token = anjuta_token_first_item (token); if (token != NULL) { gchar *value; value = anjuta_token_evaluate (token); if ((value != NULL) && (strcmp (targetname, value) == 0)) { g_free (value); prev = (AnjutaToken *)last->data; args = anjuta_token_list (prev); } } } } } } /* Check if a valid target variable is already defined */ if (args == NULL) { for (last = amp_group_node_get_token (parent, AM_GROUP_TARGET); last != NULL; last = g_list_next (last)) { gchar *value = anjuta_token_evaluate (anjuta_token_first_word ((AnjutaToken *)last->data)); if ((value != NULL) && (strcmp (targetname, value) == 0)) { g_free (value); args = anjuta_token_last_item ((AnjutaToken *)last->data); break; } g_free (value); } } if (args == NULL) { args = amp_project_write_target (parent, info->token, targetname, FALSE, NULL); } g_free (targetname); switch (anjuta_project_node_get_full_type (ANJUTA_PROJECT_NODE (target)) & ANJUTA_PROJECT_ID_MASK) { case ANJUTA_PROJECT_SHAREDLIB: case ANJUTA_PROJECT_STATICLIB: case ANJUTA_PROJECT_LT_MODULE: case ANJUTA_PROJECT_PROGRAM: amp_target_add_in_list (project, args, ANJUTA_PROJECT_NODE (target), after, prev); break; default: if (args != NULL) { amp_target_node_add_token (target, AM_TOKEN__SOURCES, args); } break; } return TRUE; }
static AnjutaToken * anjuta_token_find_target_property_position (AmpTargetNode *target, AnjutaTokenType type) { AnjutaToken *pos = NULL; gboolean after = FALSE; GList *list; AmpGroupNode *group; AnjutaToken *makefile; group = AMP_GROUP_NODE (anjuta_project_node_parent_type (ANJUTA_PROJECT_NODE (target), ANJUTA_PROJECT_GROUP)); /* Try to find a better position */ /* 1. With the other properties of the target */ list = amp_target_node_get_all_token (target); if (list != NULL) { GList *link; AnjutaTokenType best = 0; for (link = list; link != NULL; link = g_list_next (link)) { AnjutaToken *token = (AnjutaToken *)link->data; AnjutaTokenType existing = anjuta_token_get_type (token); if ((existing < AM_TOKEN_FIRST_ORDERED_TARGET_MACRO) || (existing > AM_TOKEN_LAST_ORDERED_TARGET_MACRO)) { token = anjuta_token_list (token); if (token != NULL) existing = anjuta_token_get_type (token); } if ((existing >= AM_TOKEN_FIRST_ORDERED_TARGET_MACRO) && (existing <= AM_TOKEN_LAST_ORDERED_TARGET_MACRO)) { if (existing > type) { if ((best == 0) || ((existing - type) < best)) { best = existing - type; pos = token; after = FALSE; } } else { if ((best == 0) || ((type -existing) < best)) { best = type - existing; pos = token; after = TRUE; } } } } g_list_free (list); } /* 2. With properties of sibling targets */ if (pos == NULL) { AnjutaProjectNode *prev = ANJUTA_PROJECT_NODE (target); AnjutaProjectNode *next = ANJUTA_PROJECT_NODE (target); AmpTargetNode *sibling; AnjutaTokenFile *makefile; AnjutaToken *target_list = NULL; GList *link; link = amp_target_node_get_token (target, ANJUTA_TOKEN_ARGUMENT); if ((link != NULL) && (link->data != NULL)) { target_list = anjuta_token_list ((AnjutaToken *)link->data); } makefile = amp_group_node_get_make_token_file (group); if (makefile != NULL) { after = TRUE; while ((prev != NULL) || (next != NULL)) { /* Find sibling */ if (after) { while (prev != NULL) { prev = anjuta_project_node_prev_sibling (prev); if (anjuta_project_node_get_node_type (prev) == ANJUTA_PROJECT_TARGET) break; } sibling = AMP_TARGET_NODE (prev); } else { while (next != NULL) { next = anjuta_project_node_next_sibling (next); if (anjuta_project_node_get_node_type (next) == ANJUTA_PROJECT_TARGET) break; } sibling = AMP_TARGET_NODE (next); } list = sibling == NULL ? NULL : amp_target_node_get_all_token (sibling); /* Check that the target is in the same list */ if ((list != NULL) && (target_list != NULL)) { AnjutaToken *token; link = amp_target_node_get_token (sibling, ANJUTA_TOKEN_ARGUMENT); if ((link != NULL) && (link->data != NULL)) { token = anjuta_token_list ((AnjutaToken *)link->data); } if ((token != NULL) && (target_list != token)) { /* Target is in another list, do not use it, nor following ones */ list = NULL; if (after) { prev = NULL; } else { next = NULL; } } } if (list != NULL) { gsize best = 0; for (link = list; link != NULL; link = g_list_next (link)) { AnjutaToken *token = (AnjutaToken *)link->data; AnjutaTokenType existing = anjuta_token_get_type (token); if ((existing < AM_TOKEN_FIRST_ORDERED_TARGET_MACRO) || (existing > AM_TOKEN_LAST_ORDERED_TARGET_MACRO)) { token = anjuta_token_list (token); if (token != NULL) existing = anjuta_token_get_type (token); } if ((existing >= AM_TOKEN_FIRST_ORDERED_TARGET_MACRO) && (existing <= AM_TOKEN_LAST_ORDERED_TARGET_MACRO)) { gsize tpos; tpos = anjuta_token_file_get_token_position (makefile, token); if ((best == 0) || (after && (tpos > best)) || (!after && (tpos < best))) { pos = token; best = tpos; } } } g_list_free (list); list = NULL; if (best != 0) break; } after = after ? FALSE : TRUE; } } } /* 3. After target declaration */ if (pos == NULL) { list = amp_target_node_get_token (AMP_TARGET_NODE (target), ANJUTA_TOKEN_ARGUMENT); if (list != NULL) { pos = (AnjutaToken *)list->data; if (pos != NULL) { pos = anjuta_token_list (pos); if (pos != NULL) { pos = anjuta_token_list (pos); } } } after = TRUE; } /* 4. At the end of the file */ if (pos == NULL) { makefile = amp_group_node_get_makefile_token (group); for (pos = anjuta_token_first_item (makefile); (pos != NULL) && (anjuta_token_next_item (pos) != NULL); pos = anjuta_token_next_item (pos)); after = TRUE; } /* 5. Create new file */ if (pos == NULL) { /* Empty file */ pos = anjuta_token_new_string (ANJUTA_TOKEN_COMMENT | ANJUTA_TOKEN_ADDED, "## Process this file with automake to produce Makefile.in\n"); anjuta_token_append_child (makefile, pos); amp_group_node_update_makefile (group, pos); } /* Find end of line */ if (after) { while (pos != NULL) { if (anjuta_token_get_type (pos) == ANJUTA_TOKEN_EOL) break; if (anjuta_token_next (pos) == NULL) { pos = anjuta_token_insert_token_list (after, pos, ANJUTA_TOKEN_EOL, "\n", NULL); break; } pos = anjuta_token_next (pos); } } pos = anjuta_token_insert_token_list (after, pos, ANJUTA_TOKEN_EOL, "\n", NULL); pos = anjuta_token_insert_token_list (after, pos, ANJUTA_TOKEN_EOL, "\n", NULL); amp_group_node_update_makefile (group, pos); return pos; }
static AnjutaToken * anjuta_token_find_group_property_position (AmpGroupNode *group, AnjutaTokenType type) { AnjutaToken *pos = NULL; gboolean after = FALSE; GList *list; AnjutaToken *makefile; /* Try to find a better position */ /* 1. With the other properties of the group */ list = amp_group_node_get_all_token (group); if (list != NULL) { GList *link; AnjutaTokenType best = 0; for (link = list; link != NULL; link = g_list_next (link)) { AnjutaToken *token = (AnjutaToken *)link->data; AnjutaTokenType existing = anjuta_token_get_type (token); if ((existing < AM_TOKEN_FIRST_ORDERED_TARGET_MACRO) || (existing > AM_TOKEN_LAST_ORDERED_TARGET_MACRO)) { token = anjuta_token_list (token); if (token != NULL) existing = anjuta_token_get_type (token); } if ((existing >= AM_TOKEN_FIRST_ORDERED_TARGET_MACRO) && (existing <= AM_TOKEN_LAST_ORDERED_TARGET_MACRO)) { if (existing > type) { if ((best == 0) || ((existing - type) < best)) { best = existing - type; pos = token; after = FALSE; } } else { if ((best == 0) || ((type -existing) < best)) { best = type - existing; pos = token; after = TRUE; } } } } g_list_free (list); } /* 2. At the end of the file */ if (pos == NULL) { makefile = amp_group_node_get_makefile_token (group); for (pos = anjuta_token_first_item (makefile); (pos != NULL) && (anjuta_token_next_item (pos) != NULL); pos = anjuta_token_next_item (pos)); after = TRUE; } /* makefile can be NULL for the root node if there isn't any corresponding Makefile.am */ if (makefile != NULL) { /* 3. Create new file */ if (pos == NULL) { /* Empty file */ pos = anjuta_token_new_string (ANJUTA_TOKEN_COMMENT | ANJUTA_TOKEN_ADDED, "## Process this file with automake to produce Makefile.in\n"); anjuta_token_append_child (makefile, pos); amp_group_node_update_makefile (group, pos); } /* Find end of line */ if (after) { while (pos != NULL) { if (anjuta_token_get_type (pos) == ANJUTA_TOKEN_EOL) break; if (anjuta_token_next (pos) == NULL) { pos = anjuta_token_insert_token_list (after, pos, ANJUTA_TOKEN_EOL, "\n", NULL); break; } pos = anjuta_token_next (pos); } } pos = anjuta_token_insert_token_list (after, pos, ANJUTA_TOKEN_EOL, "\n", NULL); pos = anjuta_token_insert_token_list (after, pos, ANJUTA_TOKEN_EOL, "\n", NULL); amp_group_node_update_makefile (group, pos); } return pos; }