Skip to content

Commit 16a722f

Browse files
committed
start fixing up nip4-batch
1 parent da266e8 commit 16a722f

File tree

4 files changed

+120
-37
lines changed

4 files changed

+120
-37
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
- fix an infobar race
1717
- program: add find unresolved syms
1818
- lots more vips7 compat macros
19+
- fix nip4-batch as a shebang interpreter
1920

2021
## 9.0.12 2025/08/21
2122

TODO

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1+
- test other nip4-batch options
2+
3+
benchmark on banana
4+
5+
- add tab completion ... somewhat like VS intellisense
6+
7+
should work in workspace entry box, and in program window
8+
9+
- generate little vips8 wrappers automatically on startup
10+
11+
vips_linear options in a b
12+
= vips_call9 "linear" [in, a, b] options;
13+
14+
could be in a separate namespace? eg.
15+
16+
vips8.linear
117

218
- program: ^W should parse the text and block on error
319

src/dump.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
/* A lot of this file is just for debugging. Uncomment to enable all the
3333
* debugging code.
3434
*/
35+
3536
/*
3637
#define DEBUG
3738
*/

src/main-batch.c

Lines changed: 102 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -29,46 +29,55 @@
2929
#include "nip4.h"
3030

3131
static char *main_option_output = NULL;
32-
static gboolean main_option_print_main = FALSE;
3332
static gboolean main_option_test = FALSE;
3433
static char *main_option_prefix = NULL;
3534
static gboolean main_option_version = FALSE;
35+
static gboolean main_option_workspace = FALSE;
3636

37-
//static iOpenFile *main_stdin = NULL;
38-
static char *main_option_script = NULL;
37+
// need testing
3938
static char *main_option_expression = NULL;
40-
static gboolean main_option_stdin_ws = FALSE;
41-
static gboolean main_option_stdin_def = FALSE;
4239
static char **main_option_set = NULL;
4340
static gboolean main_option_verbose = FALSE;
4441

42+
// not needed
43+
static gboolean main_option_print_main = TRUE;
44+
static char *main_option_script = NULL;
45+
static gboolean main_option_stdin_ws = FALSE;
46+
static gboolean main_option_stdin_def = TRUE;
47+
48+
4549
static GOptionEntry main_batch_options[] = {
4650
{ "output", 'o', 0, G_OPTION_ARG_FILENAME, &main_option_output,
4751
N_("write value of 'main' to FILE"), "FILE" },
48-
{ "print-main", 'p', 0, G_OPTION_ARG_NONE, &main_option_print_main,
49-
N_( "print value of 'main' to stdout" ), NULL },
5052
{ "test", 'T', 0, G_OPTION_ARG_NONE, &main_option_test,
5153
N_("test for errors and quit"), NULL },
5254
{ "prefix", 'x', 0, G_OPTION_ARG_FILENAME, &main_option_prefix,
5355
N_("start as if installed to PREFIX"), "PREFIX" },
5456
{ "version", 'v', 0, G_OPTION_ARG_NONE, &main_option_version,
5557
N_("print version number"), NULL },
58+
{ "workspace", 'v', 0, G_OPTION_ARG_NONE, &main_option_workspace,
59+
N_("load args as workspaces"), NULL },
60+
61+
// no longer needed
62+
{ "print-main", 'p', 0, G_OPTION_ARG_NONE, &main_option_print_main,
63+
N_( "print value of 'main' to stdout" ), NULL },
64+
{ "script", 's', 0, G_OPTION_ARG_FILENAME, &main_option_script,
65+
N_("load FILE as a set of definitions"), "FILE" },
66+
{ "stdin-ws", 'w', 0, G_OPTION_ARG_NONE, &main_option_stdin_ws,
67+
N_("load stdin as a workspace"), NULL },
68+
{ "stdin-def", 'd', 0, G_OPTION_ARG_NONE, &main_option_stdin_def,
69+
N_("load stdin as a set of definitions"), NULL },
5670

5771
// these options need testing and fixing
5872
{ "expression", 'e', 0, G_OPTION_ARG_STRING, &main_option_expression,
5973
N_("evaluate and print EXPRESSION"), "EXPRESSION" },
60-
{ "script", 's', 0, G_OPTION_ARG_FILENAME, &main_option_script,
61-
N_("load FILE as a set of definitions"), "FILE" },
6274
{ "set", '=', 0, G_OPTION_ARG_STRING_ARRAY, &main_option_set,
6375
N_("set values"), NULL },
6476
{ "verbose", 'V', 0, G_OPTION_ARG_NONE, &main_option_verbose,
6577
N_("verbose error output"), NULL },
66-
{ "stdin-ws", 'w', 0, G_OPTION_ARG_NONE, &main_option_stdin_ws,
67-
N_("load stdin as a workspace"), NULL },
68-
{ "stdin-def", 'd', 0, G_OPTION_ARG_NONE, &main_option_stdin_def,
69-
N_("load stdin as a set of definitions"), NULL },
7078
{ "i18n", 'i', 0, G_OPTION_ARG_NONE, &main_option_i18n,
7179
N_("output strings for internationalisation"), NULL },
80+
7281
{ NULL }
7382
};
7483

@@ -313,6 +322,46 @@ main_print_ws(Workspace *ws, gboolean *found)
313322
return NULL;
314323
}
315324

325+
/* Make nip's argc/argv[].
326+
*/
327+
static void
328+
main_build_argv(int argc, char **argv)
329+
{
330+
Toolkit *kit = toolkit_new(main_toolkitgroup, "_args");
331+
332+
char txt[MAX_STRSIZE];
333+
VipsBuf buf = VIPS_BUF_STATIC(txt);
334+
335+
vips_buf_rewind(&buf);
336+
vips_buf_appendf(&buf, "argc = %d;", argc);
337+
attach_input_string(vips_buf_all(&buf));
338+
(void) parse_onedef(kit, -1);
339+
340+
vips_buf_rewind(&buf);
341+
vips_buf_appendf(&buf, "argv = [");
342+
for (int i = 0; i < argc; i++) {
343+
/* Ignore "--" args. Consider eg.
344+
*
345+
* ./try201.nip4 -o x.v -- -12 ~/pics/shark.jpg
346+
*
347+
* if we didn't remove --, all scripts would need to.
348+
*/
349+
if (g_str_equal(argv[i], "--"))
350+
continue;
351+
352+
if(i > 0)
353+
vips_buf_appendf(&buf, ", ");
354+
vips_buf_appendf(&buf, "\"%s\"", argv[i]);
355+
}
356+
vips_buf_appendf(&buf, "];");
357+
358+
attach_input_string(vips_buf_all(&buf));
359+
if (!parse_onedef(kit, -1))
360+
main_log_add("%s\n", error_get_sub());
361+
362+
filemodel_set_modified(FILEMODEL(kit), FALSE);
363+
}
364+
316365
int
317366
main(int argc, char **argv)
318367
{
@@ -327,7 +376,7 @@ main(int argc, char **argv)
327376
g_set_application_name(_(PACKAGE));
328377

329378
GOptionContext *context =
330-
g_option_context_new(_("- batch interface to image processing spreadsheet"));
379+
g_option_context_new(_("- batch interface to nip4"));
331380
GOptionGroup *main_group = g_option_group_new(NULL, NULL, NULL, NULL, NULL);
332381
g_option_group_add_entries(main_group, main_batch_options);
333382
vips_add_option_entries(main_group);
@@ -385,13 +434,32 @@ main(int argc, char **argv)
385434

386435
main_startup(argc, argv);
387436

388-
// empty workspace we load images etc into
389-
Workspacegroup *wsg =
390-
workspacegroup_new_blank(main_workspaceroot, "untitled");
391-
Workspace *ws = workspacegroup_get_workspace(wsg);
437+
/* In the default script mode (used for #! scripts), we load argv[1] as a
438+
* set of defs and pass the main func any remaining arguments.
439+
*
440+
* In --workspace mode we load all the args as workspaces / images / defs
441+
* etc.
442+
*/
443+
444+
Workspacegroup *wsg;
445+
if (!main_option_workspace &&
446+
argc > 1) {
447+
// load argv[1] as a set of defs
448+
if (!toolkit_new_from_file(main_toolkitgroup, argv[1]))
449+
main_log_add("%s\n", error_get_sub());
450+
451+
// the rest of argc/argv become nip4 defs
452+
main_build_argv(argc - 1, argv + 1);
453+
}
454+
else {
455+
// load args as workspaces ... the empty wsg is something for images
456+
// etc to load into
457+
wsg = workspacegroup_new_blank(main_workspaceroot, "untitled");
458+
Workspace *ws = workspacegroup_get_workspace(wsg);
392459

393-
for (int i = 1; i < argc; i++)
394-
main_load_file(ws, argv[i]);
460+
for (int i = 1; i < argc; i++)
461+
main_load_file(ws, argv[i]);
462+
}
395463

396464
if (main_option_set)
397465
for (int i = 0; main_option_set[i]; i++) {
@@ -410,26 +478,23 @@ main(int argc, char **argv)
410478
main_error_exit("--test: errors found");
411479
}
412480

413-
if (main_option_print_main ||
414-
main_option_output) {
415-
Symbol *sym;
416-
gboolean found;
481+
/* Print or save all the mains we can find: one at the top level,
482+
* one in each workspace.
483+
*/
484+
Symbol *sym;
485+
gboolean found;
417486

418-
symbol_recalculate_all_force(TRUE);
487+
symbol_recalculate_all_force(TRUE);
419488

420-
/* Process all the mains we can find: one at the top level,
421-
* one in each workspace.
422-
*/
423-
found = FALSE;
424-
if ((sym = compile_lookup(symbol_root->expr->compile, "main"))) {
425-
main_print_main(sym);
426-
found = TRUE;
427-
}
428-
workspace_map((workspace_map_fn) main_print_ws, &found, NULL);
429-
430-
if (!found)
431-
main_error_exit("%s", _("no \"main\" found"));
489+
found = FALSE;
490+
if ((sym = compile_lookup(symbol_root->expr->compile, "main"))) {
491+
main_print_main(sym);
492+
found = TRUE;
432493
}
494+
workspace_map((workspace_map_fn) main_print_ws, &found, NULL);
495+
496+
if (!found)
497+
main_error_exit("%s", _("no \"main\" found"));
433498

434499
main_shutdown();
435500

0 commit comments

Comments
 (0)